2 * Copyright © 2014 Broadcom
3 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "os/os_misc.h"
26 #include "pipe/p_defines.h"
27 #include "pipe/p_screen.h"
28 #include "pipe/p_state.h"
30 #include "util/u_debug.h"
31 #include "util/u_memory.h"
32 #include "util/u_format.h"
34 #include "vc4_screen.h"
35 #include "vc4_context.h"
36 #include "vc4_resource.h"
38 static const struct debug_named_value debug_options
[] = {
40 "Dump command list during creation" },
41 { "qpu", VC4_DEBUG_QPU
,
42 "Dump generated QPU instructions" },
43 { "qir", VC4_DEBUG_QIR
,
44 "Dump QPU IR during program compile" },
45 { "tgsi", VC4_DEBUG_TGSI
,
46 "Dump TGSI during program compile" },
47 { "shaderdb", VC4_DEBUG_SHADERDB
,
48 "Dump program compile information for shader-db analysis" },
49 { "perf", VC4_DEBUG_PERF
,
50 "Print during performance-related events" },
51 { "norast", VC4_DEBUG_NORAST
,
52 "Skip actual hardware execution of commands" },
55 DEBUG_GET_ONCE_FLAGS_OPTION(vc4_debug
, "VC4_DEBUG", debug_options
, 0)
59 vc4_screen_get_name(struct pipe_screen
*pscreen
)
65 vc4_screen_get_vendor(struct pipe_screen
*pscreen
)
71 vc4_screen_destroy(struct pipe_screen
*pscreen
)
77 vc4_screen_get_param(struct pipe_screen
*pscreen
, enum pipe_cap param
)
80 /* Supported features (boolean caps). */
81 case PIPE_CAP_VERTEX_COLOR_UNCLAMPED
:
82 case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT
:
83 case PIPE_CAP_NPOT_TEXTURES
:
84 case PIPE_CAP_USER_CONSTANT_BUFFERS
:
85 case PIPE_CAP_TEXTURE_SHADOW_MAP
:
86 case PIPE_CAP_BLEND_EQUATION_SEPARATE
:
87 case PIPE_CAP_TWO_SIDED_STENCIL
:
90 /* lying for GL 2.0 */
91 case PIPE_CAP_OCCLUSION_QUERY
:
92 case PIPE_CAP_POINT_SPRITE
:
95 case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT
:
98 case PIPE_CAP_GLSL_FEATURE_LEVEL
:
101 case PIPE_CAP_MAX_VIEWPORTS
:
104 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT
:
105 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
:
108 /* Unsupported features. */
109 case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES
:
110 case PIPE_CAP_ANISOTROPIC_FILTER
:
111 case PIPE_CAP_TEXTURE_BUFFER_OBJECTS
:
112 case PIPE_CAP_CUBE_MAP_ARRAY
:
113 case PIPE_CAP_TEXTURE_MIRROR_CLAMP
:
114 case PIPE_CAP_TEXTURE_SWIZZLE
:
115 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR
:
116 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS
:
117 case PIPE_CAP_SEAMLESS_CUBE_MAP
:
118 case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION
:
119 case PIPE_CAP_TGSI_INSTANCEID
:
120 case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY
:
121 case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY
:
122 case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY
:
123 case PIPE_CAP_COMPUTE
:
124 case PIPE_CAP_START_INSTANCE
:
125 case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS
:
126 case PIPE_CAP_SHADER_STENCIL_EXPORT
:
127 case PIPE_CAP_TGSI_TEXCOORD
:
128 case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER
:
129 case PIPE_CAP_CONDITIONAL_RENDER
:
130 case PIPE_CAP_PRIMITIVE_RESTART
:
131 case PIPE_CAP_TEXTURE_MULTISAMPLE
:
132 case PIPE_CAP_TEXTURE_BARRIER
:
134 case PIPE_CAP_INDEP_BLEND_ENABLE
:
135 case PIPE_CAP_INDEP_BLEND_FUNC
:
136 case PIPE_CAP_DEPTH_CLIP_DISABLE
:
137 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE
:
138 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT
:
139 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER
:
140 case PIPE_CAP_TGSI_CAN_COMPACT_CONSTANTS
:
141 case PIPE_CAP_FRAGMENT_COLOR_CLAMPED
:
142 case PIPE_CAP_VERTEX_COLOR_CLAMPED
:
143 case PIPE_CAP_USER_VERTEX_BUFFERS
:
144 case PIPE_CAP_USER_INDEX_BUFFERS
:
145 case PIPE_CAP_QUERY_PIPELINE_STATISTICS
:
146 case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK
:
147 case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
:
148 case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS
:
149 case PIPE_CAP_TEXTURE_GATHER_SM5
:
150 case PIPE_CAP_FAKE_SW_MSAA
:
151 case PIPE_CAP_TEXTURE_QUERY_LOD
:
152 case PIPE_CAP_SAMPLE_SHADING
:
153 case PIPE_CAP_TEXTURE_GATHER_OFFSETS
:
154 case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET
:
155 case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION
:
156 case PIPE_CAP_MAX_TEXEL_OFFSET
:
157 case PIPE_CAP_MAX_VERTEX_STREAMS
:
158 case PIPE_CAP_DRAW_INDIRECT
:
159 case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE
:
160 case PIPE_CAP_CONDITIONAL_RENDER_INVERTED
:
164 case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
:
165 case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME
:
166 case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS
:
167 case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS
:
170 /* Geometry shader output, unsupported. */
171 case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES
:
172 case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS
:
176 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS
:
177 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS
:
178 return VC4_MAX_MIP_LEVELS
;
179 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS
:
180 /* Note: Not supported in hardware, just faking it. */
182 case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
:
185 /* Render targets. */
186 case PIPE_CAP_MAX_RENDER_TARGETS
:
190 case PIPE_CAP_QUERY_TIME_ELAPSED
:
191 case PIPE_CAP_QUERY_TIMESTAMP
:
194 case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET
:
195 case PIPE_CAP_MIN_TEXEL_OFFSET
:
198 case PIPE_CAP_ENDIANNESS
:
199 return PIPE_ENDIAN_LITTLE
;
201 case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT
:
204 case PIPE_CAP_VENDOR_ID
:
206 case PIPE_CAP_DEVICE_ID
:
208 case PIPE_CAP_ACCELERATED
:
210 case PIPE_CAP_VIDEO_MEMORY
: {
211 uint64_t system_memory
;
213 if (!os_get_total_physical_memory(&system_memory
))
216 return (int)(system_memory
>> 20);
222 fprintf(stderr
, "unknown param %d\n", param
);
228 vc4_screen_get_paramf(struct pipe_screen
*pscreen
, enum pipe_capf param
)
231 case PIPE_CAPF_MAX_LINE_WIDTH
:
232 case PIPE_CAPF_MAX_LINE_WIDTH_AA
:
233 case PIPE_CAPF_MAX_POINT_WIDTH
:
234 case PIPE_CAPF_MAX_POINT_WIDTH_AA
:
236 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY
:
238 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS
:
240 case PIPE_CAPF_GUARD_BAND_LEFT
:
241 case PIPE_CAPF_GUARD_BAND_TOP
:
242 case PIPE_CAPF_GUARD_BAND_RIGHT
:
243 case PIPE_CAPF_GUARD_BAND_BOTTOM
:
246 fprintf(stderr
, "unknown paramf %d\n", param
);
252 vc4_screen_get_shader_param(struct pipe_screen
*pscreen
, unsigned shader
,
253 enum pipe_shader_cap param
)
255 if (shader
!= PIPE_SHADER_VERTEX
&&
256 shader
!= PIPE_SHADER_FRAGMENT
) {
260 /* this is probably not totally correct.. but it's a start: */
262 case PIPE_SHADER_CAP_MAX_INSTRUCTIONS
:
263 case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS
:
264 case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS
:
265 case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS
:
267 case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH
:
269 case PIPE_SHADER_CAP_MAX_INPUTS
:
271 case PIPE_SHADER_CAP_MAX_TEMPS
:
272 return 64; /* Max native temporaries. */
273 case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE
:
274 return 64 * sizeof(float[4]);
275 case PIPE_SHADER_CAP_MAX_CONST_BUFFERS
:
277 case PIPE_SHADER_CAP_MAX_PREDS
:
278 return 0; /* nothing uses this */
279 case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED
:
281 case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR
:
282 case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR
:
283 case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR
:
284 case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR
:
286 case PIPE_SHADER_CAP_SUBROUTINES
:
288 case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED
:
290 case PIPE_SHADER_CAP_INTEGERS
:
291 case PIPE_SHADER_CAP_DOUBLES
:
293 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS
:
294 case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS
:
295 return VC4_MAX_TEXTURE_SAMPLERS
;
296 case PIPE_SHADER_CAP_PREFERRED_IR
:
297 return PIPE_SHADER_IR_TGSI
;
299 fprintf(stderr
, "unknown shader param %d\n", param
);
306 vc4_get_texture_format(enum pipe_format format
)
309 case PIPE_FORMAT_B8G8R8A8_UNORM
:
311 case PIPE_FORMAT_B8G8R8X8_UNORM
:
313 case PIPE_FORMAT_R8G8B8A8_UNORM
:
315 case PIPE_FORMAT_R8G8B8X8_UNORM
:
317 case PIPE_FORMAT_A8R8G8B8_UNORM
:
319 case PIPE_FORMAT_X8R8G8B8_UNORM
:
321 case PIPE_FORMAT_A8B8G8R8_UNORM
:
323 case PIPE_FORMAT_X8B8G8R8_UNORM
:
326 case PIPE_FORMAT_R4G4B4A4_UNORM:
328 case PIPE_FORMAT_R5G5B5A1_UNORM:
330 case PIPE_FORMAT_R5G6B5_UNORM:
333 case PIPE_FORMAT_L8_UNORM
:
335 case PIPE_FORMAT_A8_UNORM
:
337 case PIPE_FORMAT_L8A8_UNORM
:
339 /* XXX: ETC1 and more*/
346 vc4_screen_is_format_supported(struct pipe_screen
*pscreen
,
347 enum pipe_format format
,
348 enum pipe_texture_target target
,
349 unsigned sample_count
,
354 if ((target
>= PIPE_MAX_TEXTURE_TYPES
) ||
355 (sample_count
> 1) ||
356 !util_format_is_supported(format
, usage
)) {
360 if (usage
& PIPE_BIND_VERTEX_BUFFER
&&
361 (format
== PIPE_FORMAT_R32G32B32A32_FLOAT
||
362 format
== PIPE_FORMAT_R32G32B32_FLOAT
||
363 format
== PIPE_FORMAT_R32G32_FLOAT
||
364 format
== PIPE_FORMAT_R32_FLOAT
)) {
365 retval
|= PIPE_BIND_VERTEX_BUFFER
;
368 if ((usage
& PIPE_BIND_RENDER_TARGET
) &&
369 (format
== PIPE_FORMAT_B8G8R8A8_UNORM
||
370 format
== PIPE_FORMAT_B8G8R8X8_UNORM
|| /* XXX: really? */
371 format
== PIPE_FORMAT_R8G8B8A8_UNORM
||
372 format
== PIPE_FORMAT_R8G8B8X8_UNORM
|| /* XXX: really? */
373 format
== PIPE_FORMAT_A8B8G8R8_UNORM
||
374 format
== PIPE_FORMAT_X8B8G8R8_UNORM
|| /* XXX: really? */
375 format
== PIPE_FORMAT_A8R8G8B8_UNORM
||
376 format
== PIPE_FORMAT_X8R8G8B8_UNORM
|| /* XXX: really? */
377 format
== PIPE_FORMAT_R16G16B16A16_FLOAT
)) {
378 retval
|= PIPE_BIND_RENDER_TARGET
;
381 if ((usage
& PIPE_BIND_SAMPLER_VIEW
) &&
382 (vc4_get_texture_format(format
) != ~0)) {
383 retval
|= PIPE_BIND_SAMPLER_VIEW
;
386 if ((usage
& PIPE_BIND_DEPTH_STENCIL
) &&
387 (format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
||
388 format
== PIPE_FORMAT_Z24X8_UNORM
)) {
389 retval
|= PIPE_BIND_DEPTH_STENCIL
;
392 if ((usage
& PIPE_BIND_INDEX_BUFFER
) &&
393 (format
== PIPE_FORMAT_I8_UINT
||
394 format
== PIPE_FORMAT_I16_UINT
)) {
395 retval
|= PIPE_BIND_INDEX_BUFFER
;
398 if (usage
& PIPE_BIND_TRANSFER_READ
)
399 retval
|= PIPE_BIND_TRANSFER_READ
;
400 if (usage
& PIPE_BIND_TRANSFER_WRITE
)
401 retval
|= PIPE_BIND_TRANSFER_WRITE
;
404 if (retval
!= usage
) {
406 "not supported: format=%s, target=%d, sample_count=%d, "
407 "usage=0x%x, retval=0x%x\n", util_format_name(format
),
408 target
, sample_count
, usage
, retval
);
412 return retval
== usage
;
416 vc4_screen_create(int fd
)
418 struct vc4_screen
*screen
= CALLOC_STRUCT(vc4_screen
);
419 struct pipe_screen
*pscreen
;
421 pscreen
= &screen
->base
;
423 pscreen
->destroy
= vc4_screen_destroy
;
424 pscreen
->get_param
= vc4_screen_get_param
;
425 pscreen
->get_paramf
= vc4_screen_get_paramf
;
426 pscreen
->get_shader_param
= vc4_screen_get_shader_param
;
427 pscreen
->context_create
= vc4_context_create
;
428 pscreen
->is_format_supported
= vc4_screen_is_format_supported
;
432 vc4_debug
= debug_get_option_vc4_debug();
433 if (vc4_debug
& VC4_DEBUG_SHADERDB
)
434 vc4_debug
|= VC4_DEBUG_NORAST
;
436 #if USE_VC4_SIMULATOR
437 vc4_simulator_init(screen
);
440 vc4_resource_screen_init(pscreen
);
442 pscreen
->get_name
= vc4_screen_get_name
;
443 pscreen
->get_vendor
= vc4_screen_get_vendor
;
449 vc4_screen_bo_get_handle(struct pipe_screen
*pscreen
,
452 struct winsys_handle
*whandle
)
454 whandle
->stride
= stride
;
456 switch (whandle
->type
) {
457 case DRM_API_HANDLE_TYPE_SHARED
:
458 return vc4_bo_flink(bo
, &whandle
->handle
);
459 case DRM_API_HANDLE_TYPE_KMS
:
460 whandle
->handle
= bo
->handle
;
468 vc4_screen_bo_from_handle(struct pipe_screen
*pscreen
,
469 struct winsys_handle
*whandle
,
470 unsigned *out_stride
)
472 struct vc4_screen
*screen
= vc4_screen(pscreen
);
475 if (whandle
->type
!= DRM_API_HANDLE_TYPE_SHARED
) {
477 "Attempt to import unsupported handle type %d\n",
482 bo
= vc4_bo_open_name(screen
, whandle
->handle
, whandle
->stride
);
484 fprintf(stderr
, "Open name %d failed\n", whandle
->handle
);
488 *out_stride
= whandle
->stride
;