2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include "si_public.h"
28 #include "si_shader_internal.h"
31 #include "radeon/radeon_uvd.h"
32 #include "util/hash_table.h"
33 #include "util/u_log.h"
34 #include "util/u_memory.h"
35 #include "util/u_suballoc.h"
36 #include "util/u_tests.h"
37 #include "util/u_upload_mgr.h"
38 #include "util/xmlconfig.h"
39 #include "vl/vl_decoder.h"
40 #include "../ddebug/dd_util.h"
42 static const struct debug_named_value debug_options
[] = {
43 /* Shader logging options: */
44 { "vs", DBG(VS
), "Print vertex shaders" },
45 { "ps", DBG(PS
), "Print pixel shaders" },
46 { "gs", DBG(GS
), "Print geometry shaders" },
47 { "tcs", DBG(TCS
), "Print tessellation control shaders" },
48 { "tes", DBG(TES
), "Print tessellation evaluation shaders" },
49 { "cs", DBG(CS
), "Print compute shaders" },
50 { "noir", DBG(NO_IR
), "Don't print the LLVM IR"},
51 { "notgsi", DBG(NO_TGSI
), "Don't print the TGSI"},
52 { "noasm", DBG(NO_ASM
), "Don't print disassembled shaders"},
53 { "preoptir", DBG(PREOPT_IR
), "Print the LLVM IR before initial optimizations" },
55 /* Shader compiler options the shader cache should be aware of: */
56 { "unsafemath", DBG(UNSAFE_MATH
), "Enable unsafe math shader optimizations" },
57 { "sisched", DBG(SI_SCHED
), "Enable LLVM SI Machine Instruction Scheduler." },
59 /* Shader compiler options (with no effect on the shader cache): */
60 { "checkir", DBG(CHECK_IR
), "Enable additional sanity checks on shader IR" },
61 { "nir", DBG(NIR
), "Enable experimental NIR shaders" },
62 { "mono", DBG(MONOLITHIC_SHADERS
), "Use old-style monolithic shaders compiled on demand" },
63 { "nooptvariant", DBG(NO_OPT_VARIANT
), "Disable compiling optimized shader variants." },
65 /* Information logging options: */
66 { "info", DBG(INFO
), "Print driver information" },
67 { "tex", DBG(TEX
), "Print texture info" },
68 { "compute", DBG(COMPUTE
), "Print compute info" },
69 { "vm", DBG(VM
), "Print virtual addresses when creating resources" },
72 { "forcedma", DBG(FORCE_DMA
), "Use asynchronous DMA for all operations when possible." },
73 { "nodma", DBG(NO_ASYNC_DMA
), "Disable asynchronous DMA" },
74 { "nowc", DBG(NO_WC
), "Disable GTT write combining" },
75 { "check_vm", DBG(CHECK_VM
), "Check VM faults and dump debug info." },
76 { "reserve_vmid", DBG(RESERVE_VMID
), "Force VMID reservation per context." },
78 /* 3D engine options: */
79 { "switch_on_eop", DBG(SWITCH_ON_EOP
), "Program WD/IA to switch on end-of-packet." },
80 { "nooutoforder", DBG(NO_OUT_OF_ORDER
), "Disable out-of-order rasterization" },
81 { "nodpbb", DBG(NO_DPBB
), "Disable DPBB." },
82 { "nodfsm", DBG(NO_DFSM
), "Disable DFSM." },
83 { "dpbb", DBG(DPBB
), "Enable DPBB." },
84 { "dfsm", DBG(DFSM
), "Enable DFSM." },
85 { "nohyperz", DBG(NO_HYPERZ
), "Disable Hyper-Z" },
86 { "norbplus", DBG(NO_RB_PLUS
), "Disable RB+." },
87 { "no2d", DBG(NO_2D_TILING
), "Disable 2D tiling" },
88 { "notiling", DBG(NO_TILING
), "Disable tiling" },
89 { "nodcc", DBG(NO_DCC
), "Disable DCC." },
90 { "nodccclear", DBG(NO_DCC_CLEAR
), "Disable DCC fast clear." },
91 { "nodccfb", DBG(NO_DCC_FB
), "Disable separate DCC on the main framebuffer" },
92 { "nodccmsaa", DBG(NO_DCC_MSAA
), "Disable DCC for MSAA" },
93 { "dccmsaa", DBG(DCC_MSAA
), "Enable DCC for MSAA" },
94 { "nofmask", DBG(NO_FMASK
), "Disable MSAA compression" },
97 { "testdma", DBG(TEST_DMA
), "Invoke SDMA tests and exit." },
98 { "testvmfaultcp", DBG(TEST_VMFAULT_CP
), "Invoke a CP VM fault test and exit." },
99 { "testvmfaultsdma", DBG(TEST_VMFAULT_SDMA
), "Invoke a SDMA VM fault test and exit." },
100 { "testvmfaultshader", DBG(TEST_VMFAULT_SHADER
), "Invoke a shader VM fault test and exit." },
102 DEBUG_NAMED_VALUE_END
/* must be last */
108 static void si_destroy_context(struct pipe_context
*context
)
110 struct si_context
*sctx
= (struct si_context
*)context
;
113 /* Unreference the framebuffer normally to disable related logic
116 struct pipe_framebuffer_state fb
= {};
117 if (context
->set_framebuffer_state
)
118 context
->set_framebuffer_state(context
, &fb
);
120 si_release_all_descriptors(sctx
);
122 pipe_resource_reference(&sctx
->esgs_ring
, NULL
);
123 pipe_resource_reference(&sctx
->gsvs_ring
, NULL
);
124 pipe_resource_reference(&sctx
->tess_rings
, NULL
);
125 pipe_resource_reference(&sctx
->null_const_buf
.buffer
, NULL
);
126 r600_resource_reference(&sctx
->border_color_buffer
, NULL
);
127 free(sctx
->border_color_table
);
128 r600_resource_reference(&sctx
->scratch_buffer
, NULL
);
129 r600_resource_reference(&sctx
->compute_scratch_buffer
, NULL
);
130 r600_resource_reference(&sctx
->wait_mem_scratch
, NULL
);
132 si_pm4_free_state(sctx
, sctx
->init_config
, ~0);
133 if (sctx
->init_config_gs_rings
)
134 si_pm4_free_state(sctx
, sctx
->init_config_gs_rings
, ~0);
135 for (i
= 0; i
< ARRAY_SIZE(sctx
->vgt_shader_config
); i
++)
136 si_pm4_delete_state(sctx
, vgt_shader_config
, sctx
->vgt_shader_config
[i
]);
138 if (sctx
->fixed_func_tcs_shader
.cso
)
139 sctx
->b
.b
.delete_tcs_state(&sctx
->b
.b
, sctx
->fixed_func_tcs_shader
.cso
);
140 if (sctx
->custom_dsa_flush
)
141 sctx
->b
.b
.delete_depth_stencil_alpha_state(&sctx
->b
.b
, sctx
->custom_dsa_flush
);
142 if (sctx
->custom_blend_resolve
)
143 sctx
->b
.b
.delete_blend_state(&sctx
->b
.b
, sctx
->custom_blend_resolve
);
144 if (sctx
->custom_blend_fmask_decompress
)
145 sctx
->b
.b
.delete_blend_state(&sctx
->b
.b
, sctx
->custom_blend_fmask_decompress
);
146 if (sctx
->custom_blend_eliminate_fastclear
)
147 sctx
->b
.b
.delete_blend_state(&sctx
->b
.b
, sctx
->custom_blend_eliminate_fastclear
);
148 if (sctx
->custom_blend_dcc_decompress
)
149 sctx
->b
.b
.delete_blend_state(&sctx
->b
.b
, sctx
->custom_blend_dcc_decompress
);
150 if (sctx
->vs_blit_pos
)
151 sctx
->b
.b
.delete_vs_state(&sctx
->b
.b
, sctx
->vs_blit_pos
);
152 if (sctx
->vs_blit_pos_layered
)
153 sctx
->b
.b
.delete_vs_state(&sctx
->b
.b
, sctx
->vs_blit_pos_layered
);
154 if (sctx
->vs_blit_color
)
155 sctx
->b
.b
.delete_vs_state(&sctx
->b
.b
, sctx
->vs_blit_color
);
156 if (sctx
->vs_blit_color_layered
)
157 sctx
->b
.b
.delete_vs_state(&sctx
->b
.b
, sctx
->vs_blit_color_layered
);
158 if (sctx
->vs_blit_texcoord
)
159 sctx
->b
.b
.delete_vs_state(&sctx
->b
.b
, sctx
->vs_blit_texcoord
);
162 util_blitter_destroy(sctx
->blitter
);
164 /* Release DCC stats. */
165 for (int i
= 0; i
< ARRAY_SIZE(sctx
->b
.dcc_stats
); i
++) {
166 assert(!sctx
->b
.dcc_stats
[i
].query_active
);
168 for (int j
= 0; j
< ARRAY_SIZE(sctx
->b
.dcc_stats
[i
].ps_stats
); j
++)
169 if (sctx
->b
.dcc_stats
[i
].ps_stats
[j
])
170 sctx
->b
.b
.destroy_query(&sctx
->b
.b
,
171 sctx
->b
.dcc_stats
[i
].ps_stats
[j
]);
173 r600_texture_reference(&sctx
->b
.dcc_stats
[i
].tex
, NULL
);
176 if (sctx
->b
.query_result_shader
)
177 sctx
->b
.b
.delete_compute_state(&sctx
->b
.b
, sctx
->b
.query_result_shader
);
180 sctx
->b
.ws
->cs_destroy(sctx
->b
.gfx_cs
);
182 sctx
->b
.ws
->cs_destroy(sctx
->b
.dma_cs
);
184 sctx
->b
.ws
->ctx_destroy(sctx
->b
.ctx
);
186 if (sctx
->b
.b
.stream_uploader
)
187 u_upload_destroy(sctx
->b
.b
.stream_uploader
);
188 if (sctx
->b
.b
.const_uploader
)
189 u_upload_destroy(sctx
->b
.b
.const_uploader
);
190 if (sctx
->b
.cached_gtt_allocator
)
191 u_upload_destroy(sctx
->b
.cached_gtt_allocator
);
193 slab_destroy_child(&sctx
->b
.pool_transfers
);
194 slab_destroy_child(&sctx
->b
.pool_transfers_unsync
);
196 if (sctx
->b
.allocator_zeroed_memory
)
197 u_suballocator_destroy(sctx
->b
.allocator_zeroed_memory
);
199 sctx
->b
.ws
->fence_reference(&sctx
->b
.last_gfx_fence
, NULL
);
200 sctx
->b
.ws
->fence_reference(&sctx
->b
.last_sdma_fence
, NULL
);
201 r600_resource_reference(&sctx
->b
.eop_bug_scratch
, NULL
);
203 LLVMDisposeTargetMachine(sctx
->tm
);
205 si_saved_cs_reference(&sctx
->current_saved_cs
, NULL
);
207 _mesa_hash_table_destroy(sctx
->tex_handles
, NULL
);
208 _mesa_hash_table_destroy(sctx
->img_handles
, NULL
);
210 util_dynarray_fini(&sctx
->resident_tex_handles
);
211 util_dynarray_fini(&sctx
->resident_img_handles
);
212 util_dynarray_fini(&sctx
->resident_tex_needs_color_decompress
);
213 util_dynarray_fini(&sctx
->resident_img_needs_color_decompress
);
214 util_dynarray_fini(&sctx
->resident_tex_needs_depth_decompress
);
218 static enum pipe_reset_status
219 si_amdgpu_get_reset_status(struct pipe_context
*ctx
)
221 struct si_context
*sctx
= (struct si_context
*)ctx
;
223 return sctx
->b
.ws
->ctx_query_reset_status(sctx
->b
.ctx
);
226 static enum pipe_reset_status
si_get_reset_status(struct pipe_context
*ctx
)
228 struct si_context
*sctx
= (struct si_context
*)ctx
;
229 unsigned latest
= sctx
->b
.ws
->query_value(sctx
->b
.ws
,
230 RADEON_GPU_RESET_COUNTER
);
232 if (sctx
->b
.gpu_reset_counter
== latest
)
233 return PIPE_NO_RESET
;
235 sctx
->b
.gpu_reset_counter
= latest
;
236 return PIPE_UNKNOWN_CONTEXT_RESET
;
239 static void si_set_device_reset_callback(struct pipe_context
*ctx
,
240 const struct pipe_device_reset_callback
*cb
)
242 struct si_context
*sctx
= (struct si_context
*)ctx
;
245 sctx
->b
.device_reset_callback
= *cb
;
247 memset(&sctx
->b
.device_reset_callback
, 0,
248 sizeof(sctx
->b
.device_reset_callback
));
251 bool si_check_device_reset(struct si_context
*sctx
)
253 enum pipe_reset_status status
;
255 if (!sctx
->b
.device_reset_callback
.reset
)
258 if (!sctx
->b
.b
.get_device_reset_status
)
261 status
= sctx
->b
.b
.get_device_reset_status(&sctx
->b
.b
);
262 if (status
== PIPE_NO_RESET
)
265 sctx
->b
.device_reset_callback
.reset(sctx
->b
.device_reset_callback
.data
, status
);
269 /* Apitrace profiling:
270 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
271 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
272 * and remember its number.
273 * 3) In Mesa, enable queries and performance counters around that draw
274 * call and print the results.
275 * 4) glretrace --benchmark --markers ..
277 static void si_emit_string_marker(struct pipe_context
*ctx
,
278 const char *string
, int len
)
280 struct si_context
*sctx
= (struct si_context
*)ctx
;
282 dd_parse_apitrace_marker(string
, len
, &sctx
->apitrace_call_number
);
285 u_log_printf(sctx
->b
.log
, "\nString marker: %*s\n", len
, string
);
288 static LLVMTargetMachineRef
289 si_create_llvm_target_machine(struct si_screen
*sscreen
)
291 enum ac_target_machine_options tm_options
=
292 (sscreen
->debug_flags
& DBG(SI_SCHED
) ? AC_TM_SISCHED
: 0) |
293 (sscreen
->info
.chip_class
>= GFX9
? AC_TM_FORCE_ENABLE_XNACK
: 0) |
294 (sscreen
->info
.chip_class
< GFX9
? AC_TM_FORCE_DISABLE_XNACK
: 0) |
295 (!sscreen
->llvm_has_working_vgpr_indexing
? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH
: 0);
297 return ac_create_target_machine(sscreen
->info
.family
, tm_options
);
300 static void si_set_debug_callback(struct pipe_context
*ctx
,
301 const struct pipe_debug_callback
*cb
)
303 struct si_context
*sctx
= (struct si_context
*)ctx
;
304 struct si_screen
*screen
= sctx
->screen
;
306 util_queue_finish(&screen
->shader_compiler_queue
);
307 util_queue_finish(&screen
->shader_compiler_queue_low_priority
);
312 memset(&sctx
->debug
, 0, sizeof(sctx
->debug
));
315 static void si_set_log_context(struct pipe_context
*ctx
,
316 struct u_log_context
*log
)
318 struct si_context
*sctx
= (struct si_context
*)ctx
;
322 u_log_add_auto_logger(log
, si_auto_log_cs
, sctx
);
325 static struct pipe_context
*si_create_context(struct pipe_screen
*screen
,
328 struct si_context
*sctx
= CALLOC_STRUCT(si_context
);
329 struct si_screen
* sscreen
= (struct si_screen
*)screen
;
330 struct radeon_winsys
*ws
= sscreen
->ws
;
336 if (flags
& PIPE_CONTEXT_DEBUG
)
337 sscreen
->record_llvm_ir
= true; /* racy but not critical */
339 sctx
->b
.b
.screen
= screen
; /* this must be set first */
340 sctx
->b
.b
.priv
= NULL
;
341 sctx
->b
.b
.destroy
= si_destroy_context
;
342 sctx
->b
.b
.emit_string_marker
= si_emit_string_marker
;
343 sctx
->b
.b
.set_debug_callback
= si_set_debug_callback
;
344 sctx
->b
.b
.set_log_context
= si_set_log_context
;
345 sctx
->screen
= sscreen
; /* Easy accessing of screen/winsys. */
346 sctx
->is_debug
= (flags
& PIPE_CONTEXT_DEBUG
) != 0;
348 slab_create_child(&sctx
->b
.pool_transfers
, &sscreen
->pool_transfers
);
349 slab_create_child(&sctx
->b
.pool_transfers_unsync
, &sscreen
->pool_transfers
);
351 sctx
->b
.screen
= sscreen
;
352 sctx
->b
.ws
= sscreen
->ws
;
353 sctx
->b
.family
= sscreen
->info
.family
;
354 sctx
->b
.chip_class
= sscreen
->info
.chip_class
;
356 if (sscreen
->info
.drm_major
== 2 && sscreen
->info
.drm_minor
>= 43) {
357 sctx
->b
.b
.get_device_reset_status
= si_get_reset_status
;
358 sctx
->b
.gpu_reset_counter
=
359 sctx
->b
.ws
->query_value(sctx
->b
.ws
,
360 RADEON_GPU_RESET_COUNTER
);
363 sctx
->b
.b
.set_device_reset_callback
= si_set_device_reset_callback
;
365 si_init_context_texture_functions(sctx
);
366 si_init_query_functions(sctx
);
368 if (sctx
->b
.chip_class
== CIK
||
369 sctx
->b
.chip_class
== VI
||
370 sctx
->b
.chip_class
== GFX9
) {
371 sctx
->b
.eop_bug_scratch
= (struct r600_resource
*)
372 pipe_buffer_create(&sscreen
->b
, 0, PIPE_USAGE_DEFAULT
,
373 16 * sscreen
->info
.num_render_backends
);
374 if (!sctx
->b
.eop_bug_scratch
)
378 sctx
->b
.allocator_zeroed_memory
=
379 u_suballocator_create(&sctx
->b
.b
, sscreen
->info
.gart_page_size
,
380 0, PIPE_USAGE_DEFAULT
, 0, true);
381 if (!sctx
->b
.allocator_zeroed_memory
)
384 sctx
->b
.b
.stream_uploader
= u_upload_create(&sctx
->b
.b
, 1024 * 1024,
385 0, PIPE_USAGE_STREAM
,
386 SI_RESOURCE_FLAG_READ_ONLY
);
387 if (!sctx
->b
.b
.stream_uploader
)
390 sctx
->b
.b
.const_uploader
= u_upload_create(&sctx
->b
.b
, 128 * 1024,
391 0, PIPE_USAGE_DEFAULT
,
392 SI_RESOURCE_FLAG_32BIT
|
393 (sscreen
->cpdma_prefetch_writes_memory
?
394 0 : SI_RESOURCE_FLAG_READ_ONLY
));
395 if (!sctx
->b
.b
.const_uploader
)
398 sctx
->b
.cached_gtt_allocator
= u_upload_create(&sctx
->b
.b
, 16 * 1024,
399 0, PIPE_USAGE_STAGING
, 0);
400 if (!sctx
->b
.cached_gtt_allocator
)
403 sctx
->b
.ctx
= sctx
->b
.ws
->ctx_create(sctx
->b
.ws
);
407 if (sscreen
->info
.num_sdma_rings
&& !(sscreen
->debug_flags
& DBG(NO_ASYNC_DMA
))) {
408 sctx
->b
.dma_cs
= sctx
->b
.ws
->cs_create(sctx
->b
.ctx
, RING_DMA
,
409 (void*)si_flush_dma_cs
,
413 if (sscreen
->info
.drm_major
== 3)
414 sctx
->b
.b
.get_device_reset_status
= si_amdgpu_get_reset_status
;
416 si_init_buffer_functions(sctx
);
417 si_init_clear_functions(sctx
);
418 si_init_blit_functions(sctx
);
419 si_init_compute_functions(sctx
);
420 si_init_cp_dma_functions(sctx
);
421 si_init_debug_functions(sctx
);
422 si_init_msaa_functions(sctx
);
423 si_init_streamout_functions(sctx
);
425 if (sscreen
->info
.has_hw_decode
) {
426 sctx
->b
.b
.create_video_codec
= si_uvd_create_decoder
;
427 sctx
->b
.b
.create_video_buffer
= si_video_buffer_create
;
429 sctx
->b
.b
.create_video_codec
= vl_create_decoder
;
430 sctx
->b
.b
.create_video_buffer
= vl_video_buffer_create
;
433 sctx
->b
.gfx_cs
= ws
->cs_create(sctx
->b
.ctx
, RING_GFX
,
434 (void*)si_flush_gfx_cs
, sctx
);
437 sctx
->border_color_table
= malloc(SI_MAX_BORDER_COLORS
*
438 sizeof(*sctx
->border_color_table
));
439 if (!sctx
->border_color_table
)
442 sctx
->border_color_buffer
= (struct r600_resource
*)
443 pipe_buffer_create(screen
, 0, PIPE_USAGE_DEFAULT
,
444 SI_MAX_BORDER_COLORS
*
445 sizeof(*sctx
->border_color_table
));
446 if (!sctx
->border_color_buffer
)
449 sctx
->border_color_map
=
450 ws
->buffer_map(sctx
->border_color_buffer
->buf
,
451 NULL
, PIPE_TRANSFER_WRITE
);
452 if (!sctx
->border_color_map
)
455 si_init_all_descriptors(sctx
);
456 si_init_fence_functions(sctx
);
457 si_init_state_functions(sctx
);
458 si_init_shader_functions(sctx
);
459 si_init_viewport_functions(sctx
);
460 si_init_ia_multi_vgt_param_table(sctx
);
462 if (sctx
->b
.chip_class
>= CIK
)
463 cik_init_sdma_functions(sctx
);
465 si_init_dma_functions(sctx
);
467 if (sscreen
->debug_flags
& DBG(FORCE_DMA
))
468 sctx
->b
.b
.resource_copy_region
= sctx
->b
.dma_copy
;
470 sctx
->blitter
= util_blitter_create(&sctx
->b
.b
);
471 if (sctx
->blitter
== NULL
)
473 sctx
->blitter
->draw_rectangle
= si_draw_rectangle
;
474 sctx
->blitter
->skip_viewport_restore
= true;
476 sctx
->sample_mask
.sample_mask
= 0xffff;
478 /* these must be last */
479 si_begin_new_gfx_cs(sctx
);
481 if (sctx
->b
.chip_class
>= GFX9
) {
482 sctx
->wait_mem_scratch
= (struct r600_resource
*)
483 pipe_buffer_create(screen
, 0, PIPE_USAGE_DEFAULT
, 4);
484 if (!sctx
->wait_mem_scratch
)
487 /* Initialize the memory. */
488 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx_cs
;
489 radeon_emit(cs
, PKT3(PKT3_WRITE_DATA
, 3, 0));
490 radeon_emit(cs
, S_370_DST_SEL(V_370_MEMORY_SYNC
) |
491 S_370_WR_CONFIRM(1) |
492 S_370_ENGINE_SEL(V_370_ME
));
493 radeon_emit(cs
, sctx
->wait_mem_scratch
->gpu_address
);
494 radeon_emit(cs
, sctx
->wait_mem_scratch
->gpu_address
>> 32);
495 radeon_emit(cs
, sctx
->wait_mem_number
);
498 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
499 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
500 if (sctx
->b
.chip_class
== CIK
) {
501 sctx
->null_const_buf
.buffer
=
502 si_aligned_buffer_create(screen
,
503 SI_RESOURCE_FLAG_32BIT
,
504 PIPE_USAGE_DEFAULT
, 16,
505 sctx
->screen
->info
.tcc_cache_line_size
);
506 if (!sctx
->null_const_buf
.buffer
)
508 sctx
->null_const_buf
.buffer_size
= sctx
->null_const_buf
.buffer
->width0
;
510 for (shader
= 0; shader
< SI_NUM_SHADERS
; shader
++) {
511 for (i
= 0; i
< SI_NUM_CONST_BUFFERS
; i
++) {
512 sctx
->b
.b
.set_constant_buffer(&sctx
->b
.b
, shader
, i
,
513 &sctx
->null_const_buf
);
517 si_set_rw_buffer(sctx
, SI_HS_CONST_DEFAULT_TESS_LEVELS
,
518 &sctx
->null_const_buf
);
519 si_set_rw_buffer(sctx
, SI_VS_CONST_INSTANCE_DIVISORS
,
520 &sctx
->null_const_buf
);
521 si_set_rw_buffer(sctx
, SI_VS_CONST_CLIP_PLANES
,
522 &sctx
->null_const_buf
);
523 si_set_rw_buffer(sctx
, SI_PS_CONST_POLY_STIPPLE
,
524 &sctx
->null_const_buf
);
525 si_set_rw_buffer(sctx
, SI_PS_CONST_SAMPLE_POSITIONS
,
526 &sctx
->null_const_buf
);
528 /* Clear the NULL constant buffer, because loads should return zeros. */
529 si_clear_buffer(sctx
, sctx
->null_const_buf
.buffer
, 0,
530 sctx
->null_const_buf
.buffer
->width0
, 0,
531 SI_COHERENCY_SHADER
);
534 uint64_t max_threads_per_block
;
535 screen
->get_compute_param(screen
, PIPE_SHADER_IR_TGSI
,
536 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK
,
537 &max_threads_per_block
);
539 /* The maximum number of scratch waves. Scratch space isn't divided
540 * evenly between CUs. The number is only a function of the number of CUs.
541 * We can decrease the constant to decrease the scratch buffer size.
543 * sctx->scratch_waves must be >= the maximum posible size of
544 * 1 threadgroup, so that the hw doesn't hang from being unable
547 * The recommended value is 4 per CU at most. Higher numbers don't
548 * bring much benefit, but they still occupy chip resources (think
549 * async compute). I've seen ~2% performance difference between 4 and 32.
551 sctx
->scratch_waves
= MAX2(32 * sscreen
->info
.num_good_compute_units
,
552 max_threads_per_block
/ 64);
554 sctx
->tm
= si_create_llvm_target_machine(sscreen
);
556 /* Bindless handles. */
557 sctx
->tex_handles
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
558 _mesa_key_pointer_equal
);
559 sctx
->img_handles
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
560 _mesa_key_pointer_equal
);
562 util_dynarray_init(&sctx
->resident_tex_handles
, NULL
);
563 util_dynarray_init(&sctx
->resident_img_handles
, NULL
);
564 util_dynarray_init(&sctx
->resident_tex_needs_color_decompress
, NULL
);
565 util_dynarray_init(&sctx
->resident_img_needs_color_decompress
, NULL
);
566 util_dynarray_init(&sctx
->resident_tex_needs_depth_decompress
, NULL
);
570 fprintf(stderr
, "radeonsi: Failed to create a context.\n");
571 si_destroy_context(&sctx
->b
.b
);
575 static struct pipe_context
*si_pipe_create_context(struct pipe_screen
*screen
,
576 void *priv
, unsigned flags
)
578 struct si_screen
*sscreen
= (struct si_screen
*)screen
;
579 struct pipe_context
*ctx
;
581 if (sscreen
->debug_flags
& DBG(CHECK_VM
))
582 flags
|= PIPE_CONTEXT_DEBUG
;
584 ctx
= si_create_context(screen
, flags
);
586 if (!(flags
& PIPE_CONTEXT_PREFER_THREADED
))
589 /* Clover (compute-only) is unsupported. */
590 if (flags
& PIPE_CONTEXT_COMPUTE_ONLY
)
593 /* When shaders are logged to stderr, asynchronous compilation is
595 if (sscreen
->debug_flags
& DBG_ALL_SHADERS
)
598 /* Use asynchronous flushes only on amdgpu, since the radeon
599 * implementation for fence_server_sync is incomplete. */
600 return threaded_context_create(ctx
, &sscreen
->pool_transfers
,
601 si_replace_buffer_storage
,
602 sscreen
->info
.drm_major
>= 3 ? si_create_fence
: NULL
,
603 &((struct si_context
*)ctx
)->b
.tc
);
609 static void si_destroy_screen(struct pipe_screen
* pscreen
)
611 struct si_screen
*sscreen
= (struct si_screen
*)pscreen
;
612 struct si_shader_part
*parts
[] = {
614 sscreen
->tcs_epilogs
,
621 if (!sscreen
->ws
->unref(sscreen
->ws
))
624 util_queue_destroy(&sscreen
->shader_compiler_queue
);
625 util_queue_destroy(&sscreen
->shader_compiler_queue_low_priority
);
627 for (i
= 0; i
< ARRAY_SIZE(sscreen
->tm
); i
++)
629 LLVMDisposeTargetMachine(sscreen
->tm
[i
]);
631 for (i
= 0; i
< ARRAY_SIZE(sscreen
->tm_low_priority
); i
++)
632 if (sscreen
->tm_low_priority
[i
])
633 LLVMDisposeTargetMachine(sscreen
->tm_low_priority
[i
]);
635 /* Free shader parts. */
636 for (i
= 0; i
< ARRAY_SIZE(parts
); i
++) {
638 struct si_shader_part
*part
= parts
[i
];
640 parts
[i
] = part
->next
;
641 ac_shader_binary_clean(&part
->binary
);
645 mtx_destroy(&sscreen
->shader_parts_mutex
);
646 si_destroy_shader_cache(sscreen
);
648 si_perfcounters_destroy(sscreen
);
649 si_gpu_load_kill_thread(sscreen
);
651 mtx_destroy(&sscreen
->gpu_load_mutex
);
652 mtx_destroy(&sscreen
->aux_context_lock
);
653 sscreen
->aux_context
->destroy(sscreen
->aux_context
);
655 slab_destroy_parent(&sscreen
->pool_transfers
);
657 disk_cache_destroy(sscreen
->disk_shader_cache
);
658 sscreen
->ws
->destroy(sscreen
->ws
);
662 static bool si_init_gs_info(struct si_screen
*sscreen
)
664 /* gs_table_depth is not used by GFX9 */
665 if (sscreen
->info
.chip_class
>= GFX9
)
668 switch (sscreen
->info
.family
) {
677 sscreen
->gs_table_depth
= 16;
689 sscreen
->gs_table_depth
= 32;
696 static void si_handle_env_var_force_family(struct si_screen
*sscreen
)
698 const char *family
= debug_get_option("SI_FORCE_FAMILY", NULL
);
704 for (i
= CHIP_TAHITI
; i
< CHIP_LAST
; i
++) {
705 if (!strcmp(family
, ac_get_llvm_processor_name(i
))) {
706 /* Override family and chip_class. */
707 sscreen
->info
.family
= i
;
709 if (i
>= CHIP_VEGA10
)
710 sscreen
->info
.chip_class
= GFX9
;
711 else if (i
>= CHIP_TONGA
)
712 sscreen
->info
.chip_class
= VI
;
713 else if (i
>= CHIP_BONAIRE
)
714 sscreen
->info
.chip_class
= CIK
;
716 sscreen
->info
.chip_class
= SI
;
718 /* Don't submit any IBs. */
719 setenv("RADEON_NOOP", "1", 1);
724 fprintf(stderr
, "radeonsi: Unknown family: %s\n", family
);
728 static void si_test_vmfault(struct si_screen
*sscreen
)
730 struct pipe_context
*ctx
= sscreen
->aux_context
;
731 struct si_context
*sctx
= (struct si_context
*)ctx
;
732 struct pipe_resource
*buf
=
733 pipe_buffer_create_const0(&sscreen
->b
, 0, PIPE_USAGE_DEFAULT
, 64);
736 puts("Buffer allocation failed.");
740 r600_resource(buf
)->gpu_address
= 0; /* cause a VM fault */
742 if (sscreen
->debug_flags
& DBG(TEST_VMFAULT_CP
)) {
743 si_copy_buffer(sctx
, buf
, buf
, 0, 4, 4, 0);
744 ctx
->flush(ctx
, NULL
, 0);
745 puts("VM fault test: CP - done.");
747 if (sscreen
->debug_flags
& DBG(TEST_VMFAULT_SDMA
)) {
748 sctx
->b
.dma_clear_buffer(sctx
, buf
, 0, 4, 0);
749 ctx
->flush(ctx
, NULL
, 0);
750 puts("VM fault test: SDMA - done.");
752 if (sscreen
->debug_flags
& DBG(TEST_VMFAULT_SHADER
)) {
753 util_test_constant_buffer(ctx
, buf
);
754 puts("VM fault test: Shader - done.");
759 static void si_disk_cache_create(struct si_screen
*sscreen
)
761 /* Don't use the cache if shader dumping is enabled. */
762 if (sscreen
->debug_flags
& DBG_ALL_SHADERS
)
765 uint32_t mesa_timestamp
;
766 if (disk_cache_get_function_timestamp(si_disk_cache_create
,
770 uint32_t llvm_timestamp
;
772 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo
,
774 res
= asprintf(×tamp_str
, "%u_%u",
775 mesa_timestamp
, llvm_timestamp
);
779 /* These flags affect shader compilation. */
780 #define ALL_FLAGS (DBG(FS_CORRECT_DERIVS_AFTER_KILL) | \
784 uint64_t shader_debug_flags
= sscreen
->debug_flags
&
787 /* Add the high bits of 32-bit addresses, which affects
788 * how 32-bit addresses are expanded to 64 bits.
790 STATIC_ASSERT(ALL_FLAGS
<= UINT_MAX
);
791 shader_debug_flags
|= (uint64_t)sscreen
->info
.address32_hi
<< 32;
793 sscreen
->disk_shader_cache
=
794 disk_cache_create(si_get_family_name(sscreen
),
802 struct pipe_screen
*radeonsi_screen_create(struct radeon_winsys
*ws
,
803 const struct pipe_screen_config
*config
)
805 struct si_screen
*sscreen
= CALLOC_STRUCT(si_screen
);
806 unsigned num_threads
, num_compiler_threads
, num_compiler_threads_lowprio
, i
;
813 ws
->query_info(ws
, &sscreen
->info
);
815 sscreen
->debug_flags
= debug_get_flags_option("R600_DEBUG",
818 /* Set functions first. */
819 sscreen
->b
.context_create
= si_pipe_create_context
;
820 sscreen
->b
.destroy
= si_destroy_screen
;
822 si_init_screen_get_functions(sscreen
);
823 si_init_screen_buffer_functions(sscreen
);
824 si_init_screen_fence_functions(sscreen
);
825 si_init_screen_state_functions(sscreen
);
826 si_init_screen_texture_functions(sscreen
);
827 si_init_screen_query_functions(sscreen
);
829 /* Set these flags in debug_flags early, so that the shader cache takes
832 if (driQueryOptionb(config
->options
,
833 "glsl_correct_derivatives_after_discard"))
834 sscreen
->debug_flags
|= DBG(FS_CORRECT_DERIVS_AFTER_KILL
);
835 if (driQueryOptionb(config
->options
, "radeonsi_enable_sisched"))
836 sscreen
->debug_flags
|= DBG(SI_SCHED
);
839 if (sscreen
->debug_flags
& DBG(INFO
))
840 ac_print_gpu_info(&sscreen
->info
);
842 slab_create_parent(&sscreen
->pool_transfers
,
843 sizeof(struct r600_transfer
), 64);
845 sscreen
->force_aniso
= MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
846 if (sscreen
->force_aniso
>= 0) {
847 printf("radeonsi: Forcing anisotropy filter to %ix\n",
848 /* round down to a power of two */
849 1 << util_logbase2(sscreen
->force_aniso
));
852 (void) mtx_init(&sscreen
->aux_context_lock
, mtx_plain
);
853 (void) mtx_init(&sscreen
->gpu_load_mutex
, mtx_plain
);
855 if (!si_init_gs_info(sscreen
) ||
856 !si_init_shader_cache(sscreen
)) {
861 si_disk_cache_create(sscreen
);
863 /* Only enable as many threads as we have target machines, but at most
864 * the number of CPUs - 1 if there is more than one.
866 num_threads
= sysconf(_SC_NPROCESSORS_ONLN
);
867 num_threads
= MAX2(1, num_threads
- 1);
868 num_compiler_threads
= MIN2(num_threads
, ARRAY_SIZE(sscreen
->tm
));
869 num_compiler_threads_lowprio
=
870 MIN2(num_threads
, ARRAY_SIZE(sscreen
->tm_low_priority
));
872 if (!util_queue_init(&sscreen
->shader_compiler_queue
, "si_shader",
873 32, num_compiler_threads
,
874 UTIL_QUEUE_INIT_RESIZE_IF_FULL
)) {
875 si_destroy_shader_cache(sscreen
);
880 if (!util_queue_init(&sscreen
->shader_compiler_queue_low_priority
,
882 32, num_compiler_threads_lowprio
,
883 UTIL_QUEUE_INIT_RESIZE_IF_FULL
|
884 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY
)) {
885 si_destroy_shader_cache(sscreen
);
890 si_handle_env_var_force_family(sscreen
);
892 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
893 si_init_perfcounters(sscreen
);
895 /* Determine tessellation ring info. */
896 bool double_offchip_buffers
= sscreen
->info
.chip_class
>= CIK
&&
897 sscreen
->info
.family
!= CHIP_CARRIZO
&&
898 sscreen
->info
.family
!= CHIP_STONEY
;
899 /* This must be one less than the maximum number due to a hw limitation.
900 * Various hardware bugs in SI, CIK, and GFX9 need this.
902 unsigned max_offchip_buffers_per_se
;
904 /* Only certain chips can use the maximum value. */
905 if (sscreen
->info
.family
== CHIP_VEGA12
)
906 max_offchip_buffers_per_se
= double_offchip_buffers
? 128 : 64;
908 max_offchip_buffers_per_se
= double_offchip_buffers
? 127 : 63;
910 unsigned max_offchip_buffers
= max_offchip_buffers_per_se
*
911 sscreen
->info
.max_se
;
912 unsigned offchip_granularity
;
914 /* Hawaii has a bug with offchip buffers > 256 that can be worked
915 * around by setting 4K granularity.
917 if (sscreen
->info
.family
== CHIP_HAWAII
) {
918 sscreen
->tess_offchip_block_dw_size
= 4096;
919 offchip_granularity
= V_03093C_X_4K_DWORDS
;
921 sscreen
->tess_offchip_block_dw_size
= 8192;
922 offchip_granularity
= V_03093C_X_8K_DWORDS
;
925 sscreen
->tess_factor_ring_size
= 32768 * sscreen
->info
.max_se
;
926 assert(((sscreen
->tess_factor_ring_size
/ 4) & C_030938_SIZE
) == 0);
927 sscreen
->tess_offchip_ring_size
= max_offchip_buffers
*
928 sscreen
->tess_offchip_block_dw_size
* 4;
930 if (sscreen
->info
.chip_class
>= CIK
) {
931 if (sscreen
->info
.chip_class
>= VI
)
932 --max_offchip_buffers
;
933 sscreen
->vgt_hs_offchip_param
=
934 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers
) |
935 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity
);
937 assert(offchip_granularity
== V_03093C_X_8K_DWORDS
);
938 sscreen
->vgt_hs_offchip_param
=
939 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers
);
942 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
944 sscreen
->has_clear_state
= sscreen
->info
.chip_class
>= CIK
;
946 sscreen
->has_distributed_tess
=
947 sscreen
->info
.chip_class
>= VI
&&
948 sscreen
->info
.max_se
>= 2;
950 sscreen
->has_draw_indirect_multi
=
951 (sscreen
->info
.family
>= CHIP_POLARIS10
) ||
952 (sscreen
->info
.chip_class
== VI
&&
953 sscreen
->info
.pfp_fw_version
>= 121 &&
954 sscreen
->info
.me_fw_version
>= 87) ||
955 (sscreen
->info
.chip_class
== CIK
&&
956 sscreen
->info
.pfp_fw_version
>= 211 &&
957 sscreen
->info
.me_fw_version
>= 173) ||
958 (sscreen
->info
.chip_class
== SI
&&
959 sscreen
->info
.pfp_fw_version
>= 79 &&
960 sscreen
->info
.me_fw_version
>= 142);
962 sscreen
->has_out_of_order_rast
= sscreen
->info
.chip_class
>= VI
&&
963 sscreen
->info
.max_se
>= 2 &&
964 !(sscreen
->debug_flags
& DBG(NO_OUT_OF_ORDER
));
965 sscreen
->assume_no_z_fights
=
966 driQueryOptionb(config
->options
, "radeonsi_assume_no_z_fights");
967 sscreen
->commutative_blend_add
=
968 driQueryOptionb(config
->options
, "radeonsi_commutative_blend_add");
969 sscreen
->clear_db_cache_before_clear
=
970 driQueryOptionb(config
->options
, "radeonsi_clear_db_cache_before_clear");
971 sscreen
->has_msaa_sample_loc_bug
= (sscreen
->info
.family
>= CHIP_POLARIS10
&&
972 sscreen
->info
.family
<= CHIP_POLARIS12
) ||
973 sscreen
->info
.family
== CHIP_VEGA10
||
974 sscreen
->info
.family
== CHIP_RAVEN
;
975 sscreen
->has_ls_vgpr_init_bug
= sscreen
->info
.family
== CHIP_VEGA10
||
976 sscreen
->info
.family
== CHIP_RAVEN
;
978 if (sscreen
->debug_flags
& DBG(DPBB
)) {
979 sscreen
->dpbb_allowed
= true;
981 /* Only enable primitive binning on Raven by default. */
982 /* TODO: Investigate if binning is profitable on Vega12. */
983 sscreen
->dpbb_allowed
= sscreen
->info
.family
== CHIP_RAVEN
&&
984 !(sscreen
->debug_flags
& DBG(NO_DPBB
));
987 if (sscreen
->debug_flags
& DBG(DFSM
)) {
988 sscreen
->dfsm_allowed
= sscreen
->dpbb_allowed
;
990 sscreen
->dfsm_allowed
= sscreen
->dpbb_allowed
&&
991 !(sscreen
->debug_flags
& DBG(NO_DFSM
));
994 /* While it would be nice not to have this flag, we are constrained
995 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
998 sscreen
->llvm_has_working_vgpr_indexing
= sscreen
->info
.chip_class
<= VI
;
1000 /* Some chips have RB+ registers, but don't support RB+. Those must
1001 * always disable it.
1003 if (sscreen
->info
.family
== CHIP_STONEY
||
1004 sscreen
->info
.chip_class
>= GFX9
) {
1005 sscreen
->has_rbplus
= true;
1007 sscreen
->rbplus_allowed
=
1008 !(sscreen
->debug_flags
& DBG(NO_RB_PLUS
)) &&
1009 (sscreen
->info
.family
== CHIP_STONEY
||
1010 sscreen
->info
.family
== CHIP_VEGA12
||
1011 sscreen
->info
.family
== CHIP_RAVEN
);
1014 sscreen
->dcc_msaa_allowed
=
1015 !(sscreen
->debug_flags
& DBG(NO_DCC_MSAA
)) &&
1016 (sscreen
->debug_flags
& DBG(DCC_MSAA
) ||
1017 sscreen
->info
.chip_class
== VI
);
1019 sscreen
->cpdma_prefetch_writes_memory
= sscreen
->info
.chip_class
<= VI
;
1021 (void) mtx_init(&sscreen
->shader_parts_mutex
, mtx_plain
);
1022 sscreen
->use_monolithic_shaders
=
1023 (sscreen
->debug_flags
& DBG(MONOLITHIC_SHADERS
)) != 0;
1025 sscreen
->barrier_flags
.cp_to_L2
= SI_CONTEXT_INV_SMEM_L1
|
1026 SI_CONTEXT_INV_VMEM_L1
;
1027 if (sscreen
->info
.chip_class
<= VI
) {
1028 sscreen
->barrier_flags
.cp_to_L2
|= SI_CONTEXT_INV_GLOBAL_L2
;
1029 sscreen
->barrier_flags
.L2_to_cp
|= SI_CONTEXT_WRITEBACK_GLOBAL_L2
;
1032 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
1033 sscreen
->debug_flags
|= DBG_ALL_SHADERS
;
1035 for (i
= 0; i
< num_compiler_threads
; i
++)
1036 sscreen
->tm
[i
] = si_create_llvm_target_machine(sscreen
);
1037 for (i
= 0; i
< num_compiler_threads_lowprio
; i
++)
1038 sscreen
->tm_low_priority
[i
] = si_create_llvm_target_machine(sscreen
);
1040 /* Create the auxiliary context. This must be done last. */
1041 sscreen
->aux_context
= si_create_context(&sscreen
->b
, 0);
1043 if (sscreen
->debug_flags
& DBG(TEST_DMA
))
1044 si_test_dma(sscreen
);
1046 if (sscreen
->debug_flags
& (DBG(TEST_VMFAULT_CP
) |
1047 DBG(TEST_VMFAULT_SDMA
) |
1048 DBG(TEST_VMFAULT_SHADER
)))
1049 si_test_vmfault(sscreen
);