v3d_screen.c 25.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2014-2017 Broadcom
 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

25 26
#include <sys/sysinfo.h>

27
#include "common/v3d_device_info.h"
Dylan Baker's avatar
Dylan Baker committed
28
#include "util/os_misc.h"
29 30 31 32 33 34
#include "pipe/p_defines.h"
#include "pipe/p_screen.h"
#include "pipe/p_state.h"

#include "util/u_debug.h"
#include "util/u_memory.h"
35
#include "util/format/u_format.h"
36
#include "util/u_hash_table.h"
37
#include "util/u_screen.h"
38
#include "util/u_transfer_helper.h"
39
#include "util/ralloc.h"
40
#include "util/xmlconfig.h"
41 42

#include <xf86drm.h>
43 44 45
#include "v3d_screen.h"
#include "v3d_context.h"
#include "v3d_resource.h"
46
#include "compiler/v3d_compiler.h"
47
#include "drm-uapi/drm_fourcc.h"
48 49

static const char *
50
v3d_screen_get_name(struct pipe_screen *pscreen)
51
{
52
        struct v3d_screen *screen = v3d_screen(pscreen);
53 54 55

        if (!screen->name) {
                screen->name = ralloc_asprintf(screen,
56
                                               "V3D %d.%d",
57 58 59 60 61 62 63 64
                                               screen->devinfo.ver / 10,
                                               screen->devinfo.ver % 10);
        }

        return screen->name;
}

static const char *
65
v3d_screen_get_vendor(struct pipe_screen *pscreen)
66 67 68 69 70
{
        return "Broadcom";
}

static void
71
v3d_screen_destroy(struct pipe_screen *pscreen)
72
{
73
        struct v3d_screen *screen = v3d_screen(pscreen);
74

75
        _mesa_hash_table_destroy(screen->bo_handles, NULL);
76
        v3d_bufmgr_destroy(pscreen);
77
        slab_destroy_parent(&screen->transfer_pool);
78
        free(screen->ro);
79

80 81
        if (using_v3d_simulator)
                v3d_simulator_destroy(screen);
82 83

        v3d_compiler_free(screen->compiler);
84
        u_transfer_helper_destroy(pscreen->transfer_helper);
85 86 87 88 89

        close(screen->fd);
        ralloc_free(pscreen);
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103
static bool
v3d_has_feature(struct v3d_screen *screen, enum drm_v3d_param feature)
{
        struct drm_v3d_get_param p = {
                .param = feature,
        };
        int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_GET_PARAM, &p);

        if (ret != 0)
                return false;

        return p.value;
}

104
static int
105
v3d_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
106
{
107
        struct v3d_screen *screen = v3d_screen(pscreen);
108

109 110 111 112 113 114 115 116 117 118 119 120
        switch (param) {
                /* Supported features (boolean caps). */
        case PIPE_CAP_VERTEX_COLOR_CLAMPED:
        case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
        case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
        case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
        case PIPE_CAP_NPOT_TEXTURES:
        case PIPE_CAP_SHAREABLE_SHADERS:
        case PIPE_CAP_BLEND_EQUATION_SEPARATE:
        case PIPE_CAP_TEXTURE_MULTISAMPLE:
        case PIPE_CAP_TEXTURE_SWIZZLE:
        case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
121
        case PIPE_CAP_START_INSTANCE:
122
        case PIPE_CAP_TGSI_INSTANCEID:
123 124 125
        case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
        case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
        case PIPE_CAP_VERTEX_SHADER_SATURATE:
126
        case PIPE_CAP_PRIMITIVE_RESTART:
127
        case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
128 129 130 131
        case PIPE_CAP_OCCLUSION_QUERY:
        case PIPE_CAP_POINT_SPRITE:
        case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
        case PIPE_CAP_DRAW_INDIRECT:
132
        case PIPE_CAP_MULTI_DRAW_INDIRECT:
133
        case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
134
        case PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET:
135
        case PIPE_CAP_TGSI_CAN_READ_OUTPUTS:
136
        case PIPE_CAP_TGSI_PACK_HALF_FLOAT:
137
        case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
138
        case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
139
        case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
140 141
                return 1;

142 143 144 145
        case PIPE_CAP_TEXTURE_QUERY_LOD:
                return screen->devinfo.ver >= 42;
                break;

146 147 148 149 150 151 152
        case PIPE_CAP_PACKED_UNIFORMS:
                /* We can't enable this flag, because it results in load_ubo
                 * intrinsics across a 16b boundary, but v3d's TMU general
                 * memory accesses wrap on 16b boundaries.
                 */
                return 0;

153 154 155
        case PIPE_CAP_NIR_IMAGES_AS_DEREF:
                return 0;

156
        case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
157 158 159 160 161 162 163 164 165
                /* XXX perf: we don't want to emit these extra blits for
                 * glReadPixels(), since we still have to do an uncached read
                 * from the GPU of the result after waiting for the TFU blit
                 * to happen.  However, disabling this introduces instability
                 * in
                 * dEQP-GLES31.functional.image_load_store.early_fragment_tests.*
                 * and corruption in chromium's rendering.
                 */
                return 1;
166

167 168 169
        case PIPE_CAP_COMPUTE:
                return screen->has_csd && screen->devinfo.ver >= 41;

170
        case PIPE_CAP_GENERATE_MIPMAP:
Eric Anholt's avatar
tfu  
Eric Anholt committed
171
                return v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_TFU);
172

173 174 175
        case PIPE_CAP_INDEP_BLEND_ENABLE:
                return screen->devinfo.ver >= 40;

176 177 178
        case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
                return 256;

179 180 181 182 183
        case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
                if (screen->devinfo.ver < 40)
                        return 0;
                return 4;

184
        case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
185 186 187 188
                if (screen->has_cache_flush)
                        return 4;
                else
                        return 0; /* Disables shader storage */
189 190

        case PIPE_CAP_GLSL_FEATURE_LEVEL:
191
                return 330;
192

193 194 195
        case PIPE_CAP_ESSL_FEATURE_LEVEL:
                return 310;

196 197 198
	case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
		return 140;

199 200
        case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
                return 1;
201 202
        case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
                return 0;
203 204 205 206 207 208 209 210 211 212
        case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
                if (screen->devinfo.ver >= 40)
                        return 0;
                else
                        return 1;
        case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
                if (screen->devinfo.ver >= 40)
                        return 1;
                else
                        return 0;
213 214

        case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
215
        case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
216 217 218 219 220 221
        case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
                return 1;

        case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
                return 4;

222 223 224
        case PIPE_CAP_MAX_VARYINGS:
                return V3D_MAX_FS_INPUTS / 4;

225
                /* Texturing. */
226 227 228
        case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
                if (screen->devinfo.ver < 40)
                        return 2048;
229 230
                else if (screen->nonmsaa_texture_size_limit)
                        return 7680;
231 232
                else
                        return 4096;
233 234
        case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
        case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
235 236 237 238
                if (screen->devinfo.ver < 40)
                        return 12;
                else
                        return V3D_MAX_MIP_LEVELS;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
                return 2048;

                /* Render targets. */
        case PIPE_CAP_MAX_RENDER_TARGETS:
                return 4;

        case PIPE_CAP_VENDOR_ID:
                return 0x14E4;
        case PIPE_CAP_ACCELERATED:
                return 1;
        case PIPE_CAP_VIDEO_MEMORY: {
                uint64_t system_memory;

                if (!os_get_total_physical_memory(&system_memory))
                        return 0;

                return (int)(system_memory >> 20);
        }
        case PIPE_CAP_UMA:
                return 1;

Iago Toral's avatar
Iago Toral committed
261 262 263 264 265 266 267 268 269 270
        /* Geometry shaders */
        case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
                /* Minimum required by GLES 3.2 */
                return 1024;
        case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
                /* MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS / 4 */
                return 256;
        case PIPE_CAP_MAX_GS_INVOCATIONS:
                return 32;

271
        default:
272
                return u_pipe_screen_get_param_defaults(pscreen, param);
273 274 275 276
        }
}

static float
277
v3d_screen_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
278 279 280 281 282 283 284 285 286 287 288 289 290
{
        switch (param) {
        case PIPE_CAPF_MAX_LINE_WIDTH:
        case PIPE_CAPF_MAX_LINE_WIDTH_AA:
                return 32;

        case PIPE_CAPF_MAX_POINT_WIDTH:
        case PIPE_CAPF_MAX_POINT_WIDTH_AA:
                return 512.0f;

        case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
                return 0.0f;
        case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
291
                return 16.0f;
292 293 294 295 296

        case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
        case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
        case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
                return 0.0f;
297 298 299 300 301 302 303
        default:
                fprintf(stderr, "unknown paramf %d\n", param);
                return 0;
        }
}

static int
304
v3d_screen_get_shader_param(struct pipe_screen *pscreen, unsigned shader,
305 306
                           enum pipe_shader_cap param)
{
307 308
        struct v3d_screen *screen = v3d_screen(pscreen);

309 310 311 312 313 314 315 316
        switch (shader) {
        case PIPE_SHADER_VERTEX:
        case PIPE_SHADER_FRAGMENT:
                break;
        case PIPE_SHADER_COMPUTE:
                if (!screen->has_csd)
                        return 0;
                break;
Iago Toral's avatar
Iago Toral committed
317 318 319 320
        case PIPE_SHADER_GEOMETRY:
                if (screen->devinfo.ver < 41)
                        return 0;
                break;
321
        default:
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                return 0;
        }

        /* this is probably not totally correct.. but it's a start: */
        switch (param) {
        case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
        case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
        case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
        case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
                return 16384;

        case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
                return UINT_MAX;

        case PIPE_SHADER_CAP_MAX_INPUTS:
Iago Toral's avatar
Iago Toral committed
337 338
                switch (shader) {
                case PIPE_SHADER_VERTEX:
339
                        return V3D_MAX_VS_INPUTS / 4;
Iago Toral's avatar
Iago Toral committed
340 341 342 343 344 345 346
                case PIPE_SHADER_GEOMETRY:
                        return V3D_MAX_GS_INPUTS / 4;
                case PIPE_SHADER_FRAGMENT:
                        return V3D_MAX_FS_INPUTS / 4;
                default:
                        return 0;
                };
347
        case PIPE_SHADER_CAP_MAX_OUTPUTS:
348 349 350
                if (shader == PIPE_SHADER_FRAGMENT)
                        return 4;
                else
351
                        return V3D_MAX_FS_INPUTS / 4;
352 353 354
        case PIPE_SHADER_CAP_MAX_TEMPS:
                return 256; /* GL_MAX_PROGRAM_TEMPORARIES_ARB */
        case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
355 356 357
                /* Note: Limited by the offset size in
                 * v3d_unit_data_create().
                 */
358 359 360 361 362 363
                return 16 * 1024 * sizeof(float);
        case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
                return 16;
        case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
                return 0;
        case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
364
                return 1;
365 366
        case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
                return 0;
367 368
        case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
                return 1;
369 370 371 372 373 374 375
        case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
                return 1;
        case PIPE_SHADER_CAP_SUBROUTINES:
                return 0;
        case PIPE_SHADER_CAP_INTEGERS:
                return 1;
        case PIPE_SHADER_CAP_FP16:
376 377
        case PIPE_SHADER_CAP_FP16_DERIVATIVES:
        case PIPE_SHADER_CAP_INT16:
378 379 380 381 382 383
        case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
        case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
        case PIPE_SHADER_CAP_TGSI_LDEXP_SUPPORTED:
        case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
        case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
        case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
384 385
        case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
        case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
386 387 388
                return 0;
        case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
        case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
389
                return V3D_MAX_TEXTURE_SAMPLERS;
390 391

        case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
392
                if (screen->has_cache_flush) {
Iago Toral's avatar
Iago Toral committed
393 394
                        if (shader == PIPE_SHADER_VERTEX ||
                            shader == PIPE_SHADER_GEOMETRY) {
395
                                return 0;
Iago Toral's avatar
Iago Toral committed
396
                        }
397 398 399 400
                        return PIPE_MAX_SHADER_BUFFERS;
                 } else {
                        return 0;
                 }
401

402
        case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
403 404 405 406 407 408
                if (screen->has_cache_flush) {
                        if (screen->devinfo.ver < 41)
                                return 0;
                        else
                                return PIPE_MAX_SHADER_IMAGES;
                } else {
409
                        return 0;
410
                }
411

412 413 414
        case PIPE_SHADER_CAP_PREFERRED_IR:
                return PIPE_SHADER_IR_NIR;
        case PIPE_SHADER_CAP_SUPPORTED_IRS:
415
                return 1 << PIPE_SHADER_IR_NIR;
416 417 418 419 420 421 422 423 424 425 426 427
        case PIPE_SHADER_CAP_MAX_UNROLL_ITERATIONS_HINT:
                return 32;
        case PIPE_SHADER_CAP_LOWER_IF_THRESHOLD:
        case PIPE_SHADER_CAP_TGSI_SKIP_MERGE_REGISTERS:
                return 0;
        default:
                fprintf(stderr, "unknown shader param %d\n", param);
                return 0;
        }
        return 0;
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
static int
v3d_get_compute_param(struct pipe_screen *pscreen, enum pipe_shader_ir ir_type,
                      enum pipe_compute_cap param, void *ret)
{
        struct v3d_screen *screen = v3d_screen(pscreen);

        if (!screen->has_csd)
                return 0;

#define RET(x) do {                                     \
                if (ret)                                \
                        memcpy(ret, x, sizeof(x));      \
                return sizeof(x);                       \
        } while (0)

        switch (param) {
        case PIPE_COMPUTE_CAP_ADDRESS_BITS:
                RET((uint32_t []) { 32 });
                break;

        case PIPE_COMPUTE_CAP_IR_TARGET:
                sprintf(ret, "v3d");
                return strlen(ret);

        case PIPE_COMPUTE_CAP_GRID_DIMENSION:
                RET((uint64_t []) { 3 });

        case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
                /* GL_MAX_COMPUTE_SHADER_WORK_GROUP_COUNT: The CSD has a
                 * 16-bit field for the number of workgroups in each
                 * dimension.
                 */
                RET(((uint64_t []) { 65535, 65535, 65535 }));

        case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
                /* GL_MAX_COMPUTE_WORK_GROUP_SIZE */
                RET(((uint64_t []) { 256, 256, 256 }));

        case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
        case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
                /* GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS: This is
                 * limited by WG_SIZE in the CSD.
                 */
                RET((uint64_t []) { 256 });

        case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
                RET((uint64_t []) { 1024 * 1024 * 1024 });

        case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
                /* GL_MAX_COMPUTE_SHARED_MEMORY_SIZE */
                RET((uint64_t []) { 32768 });

        case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
        case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
                RET((uint64_t []) { 4096 });

        case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE: {
                struct sysinfo si;
                sysinfo(&si);
                RET((uint64_t []) { si.totalram });
        }

        case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
                /* OpenCL only */
                RET((uint32_t []) { 0 });

        case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
                RET((uint32_t []) { 1 });

        case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
                RET((uint32_t []) { 1 });

        case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
                RET((uint32_t []) { 16 });

        }

        return 0;
}

508
static bool
509
v3d_screen_is_format_supported(struct pipe_screen *pscreen,
510 511 512
                               enum pipe_format format,
                               enum pipe_texture_target target,
                               unsigned sample_count,
513
                               unsigned storage_sample_count,
514 515
                               unsigned usage)
{
516
        struct v3d_screen *screen = v3d_screen(pscreen);
517

518 519 520
        if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
                return false;

521
        if (sample_count > 1 && sample_count != V3D_MAX_SAMPLES)
522
                return false;
523

524
        if (target >= PIPE_MAX_TEXTURE_TYPES) {
525
                return false;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
        }

        if (usage & PIPE_BIND_VERTEX_BUFFER) {
                switch (format) {
                case PIPE_FORMAT_R32G32B32A32_FLOAT:
                case PIPE_FORMAT_R32G32B32_FLOAT:
                case PIPE_FORMAT_R32G32_FLOAT:
                case PIPE_FORMAT_R32_FLOAT:
                case PIPE_FORMAT_R32G32B32A32_SNORM:
                case PIPE_FORMAT_R32G32B32_SNORM:
                case PIPE_FORMAT_R32G32_SNORM:
                case PIPE_FORMAT_R32_SNORM:
                case PIPE_FORMAT_R32G32B32A32_SSCALED:
                case PIPE_FORMAT_R32G32B32_SSCALED:
                case PIPE_FORMAT_R32G32_SSCALED:
                case PIPE_FORMAT_R32_SSCALED:
                case PIPE_FORMAT_R16G16B16A16_UNORM:
                case PIPE_FORMAT_R16G16B16_UNORM:
                case PIPE_FORMAT_R16G16_UNORM:
                case PIPE_FORMAT_R16_UNORM:
                case PIPE_FORMAT_R16G16B16A16_SNORM:
                case PIPE_FORMAT_R16G16B16_SNORM:
                case PIPE_FORMAT_R16G16_SNORM:
                case PIPE_FORMAT_R16_SNORM:
                case PIPE_FORMAT_R16G16B16A16_USCALED:
                case PIPE_FORMAT_R16G16B16_USCALED:
                case PIPE_FORMAT_R16G16_USCALED:
                case PIPE_FORMAT_R16_USCALED:
                case PIPE_FORMAT_R16G16B16A16_SSCALED:
                case PIPE_FORMAT_R16G16B16_SSCALED:
                case PIPE_FORMAT_R16G16_SSCALED:
                case PIPE_FORMAT_R16_SSCALED:
                case PIPE_FORMAT_R8G8B8A8_UNORM:
                case PIPE_FORMAT_R8G8B8_UNORM:
                case PIPE_FORMAT_R8G8_UNORM:
                case PIPE_FORMAT_R8_UNORM:
                case PIPE_FORMAT_R8G8B8A8_SNORM:
                case PIPE_FORMAT_R8G8B8_SNORM:
                case PIPE_FORMAT_R8G8_SNORM:
                case PIPE_FORMAT_R8_SNORM:
                case PIPE_FORMAT_R8G8B8A8_USCALED:
                case PIPE_FORMAT_R8G8B8_USCALED:
                case PIPE_FORMAT_R8G8_USCALED:
                case PIPE_FORMAT_R8_USCALED:
                case PIPE_FORMAT_R8G8B8A8_SSCALED:
                case PIPE_FORMAT_R8G8B8_SSCALED:
                case PIPE_FORMAT_R8G8_SSCALED:
                case PIPE_FORMAT_R8_SSCALED:
574 575 576 577 578 579 580 581
                case PIPE_FORMAT_R10G10B10A2_UNORM:
                case PIPE_FORMAT_B10G10R10A2_UNORM:
                case PIPE_FORMAT_R10G10B10A2_SNORM:
                case PIPE_FORMAT_B10G10R10A2_SNORM:
                case PIPE_FORMAT_R10G10B10A2_USCALED:
                case PIPE_FORMAT_B10G10R10A2_USCALED:
                case PIPE_FORMAT_R10G10B10A2_SSCALED:
                case PIPE_FORMAT_B10G10R10A2_SSCALED:
582 583
                        break;
                default:
584
                        return false;
585 586 587
                }
        }

588 589 590
        /* FORMAT_NONE gets allowed for ARB_framebuffer_no_attachments's probe
         * of FRAMEBUFFER_MAX_SAMPLES
         */
591
        if ((usage & PIPE_BIND_RENDER_TARGET) &&
592
            format != PIPE_FORMAT_NONE &&
593
            !v3d_rt_format_supported(&screen->devinfo, format)) {
594
                return false;
595 596 597
        }

        if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
598
            !v3d_tex_format_supported(&screen->devinfo, format)) {
599
                return false;
600 601 602
        }

        if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
603 604 605 606 607
            !(format == PIPE_FORMAT_S8_UINT_Z24_UNORM ||
              format == PIPE_FORMAT_X8Z24_UNORM ||
              format == PIPE_FORMAT_Z16_UNORM ||
              format == PIPE_FORMAT_Z32_FLOAT ||
              format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)) {
608
                return false;
609 610 611
        }

        if ((usage & PIPE_BIND_INDEX_BUFFER) &&
612 613 614
            !(format == PIPE_FORMAT_I8_UINT ||
              format == PIPE_FORMAT_I16_UINT ||
              format == PIPE_FORMAT_I32_UINT)) {
615
                return false;
616 617
        }

618
        return true;
619 620 621
}

static const void *
622
v3d_screen_get_compiler_options(struct pipe_screen *pscreen,
623 624 625 626 627
                                enum pipe_shader_ir ir, unsigned shader)
{
        return &v3d_nir_options;
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
static void
v3d_screen_query_dmabuf_modifiers(struct pipe_screen *pscreen,
                                  enum pipe_format format, int max,
                                  uint64_t *modifiers,
                                  unsigned int *external_only,
                                  int *count)
{
        int i;
        uint64_t available_modifiers[] = {
                DRM_FORMAT_MOD_BROADCOM_UIF,
                DRM_FORMAT_MOD_LINEAR,
        };
        int num_modifiers = ARRAY_SIZE(available_modifiers);

        if (!modifiers) {
                *count = num_modifiers;
                return;
        }

        *count = MIN2(max, num_modifiers);
        for (i = 0; i < *count; i++) {
                modifiers[i] = available_modifiers[i];
                if (external_only)
                        external_only[i] = false;
       }
}

655
struct pipe_screen *
656 657
v3d_screen_create(int fd, const struct pipe_screen_config *config,
                  struct renderonly *ro)
658
{
659
        struct v3d_screen *screen = rzalloc(NULL, struct v3d_screen);
660 661 662 663
        struct pipe_screen *pscreen;

        pscreen = &screen->base;

664 665 666 667
        pscreen->destroy = v3d_screen_destroy;
        pscreen->get_param = v3d_screen_get_param;
        pscreen->get_paramf = v3d_screen_get_paramf;
        pscreen->get_shader_param = v3d_screen_get_shader_param;
668
        pscreen->get_compute_param = v3d_get_compute_param;
669 670
        pscreen->context_create = v3d_context_create;
        pscreen->is_format_supported = v3d_screen_is_format_supported;
671 672

        screen->fd = fd;
Eric Anholt's avatar
Eric Anholt committed
673 674 675 676 677 678 679 680
        if (ro) {
                screen->ro = renderonly_dup(ro);
                if (!screen->ro) {
                        fprintf(stderr, "Failed to dup renderonly object\n");
                        ralloc_free(screen);
                        return NULL;
                }
        }
681 682
        list_inithead(&screen->bo_cache.time_list);
        (void)mtx_init(&screen->bo_handles_mutex, mtx_plain);
683
        screen->bo_handles = util_hash_table_create_ptr_keys();
684

685
#if defined(USE_V3D_SIMULATOR)
686
        v3d_simulator_init(screen);
687 688
#endif

689
        if (!v3d_get_device_info(screen->fd, &screen->devinfo, &v3d_ioctl))
690 691
                goto fail;

692 693 694 695 696 697 698 699
        /* We have to driCheckOption for the simulator mode to not assertion
         * fail on not having our XML config.
         */
        const char *nonmsaa_name = "v3d_nonmsaa_texture_size_limit";
        screen->nonmsaa_texture_size_limit =
                driCheckOption(config->options, nonmsaa_name, DRI_BOOL) &&
                driQueryOptionb(config->options, nonmsaa_name);

700
        slab_create_parent(&screen->transfer_pool, sizeof(struct v3d_transfer), 16);
701

Eric Anholt's avatar
Eric Anholt committed
702
        screen->has_csd = v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_CSD);
703 704
        screen->has_cache_flush =
                v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_CACHE_FLUSH);
705

706
        v3d_fence_init(screen);
707 708 709

        v3d_process_debug_variable();

710
        v3d_resource_screen_init(pscreen);
711 712 713

        screen->compiler = v3d_compiler_init(&screen->devinfo);

714 715 716 717
        pscreen->get_name = v3d_screen_get_name;
        pscreen->get_vendor = v3d_screen_get_vendor;
        pscreen->get_device_vendor = v3d_screen_get_vendor;
        pscreen->get_compiler_options = v3d_screen_get_compiler_options;
718
        pscreen->query_dmabuf_modifiers = v3d_screen_query_dmabuf_modifiers;
719 720 721 722 723 724 725 726

        return pscreen;

fail:
        close(fd);
        ralloc_free(pscreen);
        return NULL;
}