radeonsi: fully enable 2x DCC MSAA for array and non-array textures
[mesa.git] / src / gallium / drivers / radeonsi / si_pipe.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
12 *
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
15 * Software.
16 *
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.
24 */
25
26 #include "si_pipe.h"
27 #include "si_public.h"
28 #include "si_shader_internal.h"
29 #include "sid.h"
30
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 "driver_ddebug/dd_util.h"
41
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" },
54
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." },
58
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." },
64
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" },
70
71 /* Driver options: */
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." },
77
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 { "nofmask", DBG(NO_FMASK), "Disable MSAA compression" },
94
95 /* Tests: */
96 { "testdma", DBG(TEST_DMA), "Invoke SDMA tests and exit." },
97 { "testvmfaultcp", DBG(TEST_VMFAULT_CP), "Invoke a CP VM fault test and exit." },
98 { "testvmfaultsdma", DBG(TEST_VMFAULT_SDMA), "Invoke a SDMA VM fault test and exit." },
99 { "testvmfaultshader", DBG(TEST_VMFAULT_SHADER), "Invoke a shader VM fault test and exit." },
100
101 DEBUG_NAMED_VALUE_END /* must be last */
102 };
103
104 /*
105 * pipe_context
106 */
107 static void si_destroy_context(struct pipe_context *context)
108 {
109 struct si_context *sctx = (struct si_context *)context;
110 int i;
111
112 /* Unreference the framebuffer normally to disable related logic
113 * properly.
114 */
115 struct pipe_framebuffer_state fb = {};
116 if (context->set_framebuffer_state)
117 context->set_framebuffer_state(context, &fb);
118
119 si_release_all_descriptors(sctx);
120
121 pipe_resource_reference(&sctx->esgs_ring, NULL);
122 pipe_resource_reference(&sctx->gsvs_ring, NULL);
123 pipe_resource_reference(&sctx->tess_rings, NULL);
124 pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);
125 r600_resource_reference(&sctx->border_color_buffer, NULL);
126 free(sctx->border_color_table);
127 r600_resource_reference(&sctx->scratch_buffer, NULL);
128 r600_resource_reference(&sctx->compute_scratch_buffer, NULL);
129 r600_resource_reference(&sctx->wait_mem_scratch, NULL);
130
131 si_pm4_free_state(sctx, sctx->init_config, ~0);
132 if (sctx->init_config_gs_rings)
133 si_pm4_free_state(sctx, sctx->init_config_gs_rings, ~0);
134 for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)
135 si_pm4_delete_state(sctx, vgt_shader_config, sctx->vgt_shader_config[i]);
136
137 if (sctx->fixed_func_tcs_shader.cso)
138 sctx->b.delete_tcs_state(&sctx->b, sctx->fixed_func_tcs_shader.cso);
139 if (sctx->custom_dsa_flush)
140 sctx->b.delete_depth_stencil_alpha_state(&sctx->b, sctx->custom_dsa_flush);
141 if (sctx->custom_blend_resolve)
142 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_resolve);
143 if (sctx->custom_blend_fmask_decompress)
144 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_fmask_decompress);
145 if (sctx->custom_blend_eliminate_fastclear)
146 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_eliminate_fastclear);
147 if (sctx->custom_blend_dcc_decompress)
148 sctx->b.delete_blend_state(&sctx->b, sctx->custom_blend_dcc_decompress);
149 if (sctx->vs_blit_pos)
150 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos);
151 if (sctx->vs_blit_pos_layered)
152 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_pos_layered);
153 if (sctx->vs_blit_color)
154 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color);
155 if (sctx->vs_blit_color_layered)
156 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_color_layered);
157 if (sctx->vs_blit_texcoord)
158 sctx->b.delete_vs_state(&sctx->b, sctx->vs_blit_texcoord);
159
160 if (sctx->blitter)
161 util_blitter_destroy(sctx->blitter);
162
163 /* Release DCC stats. */
164 for (int i = 0; i < ARRAY_SIZE(sctx->dcc_stats); i++) {
165 assert(!sctx->dcc_stats[i].query_active);
166
167 for (int j = 0; j < ARRAY_SIZE(sctx->dcc_stats[i].ps_stats); j++)
168 if (sctx->dcc_stats[i].ps_stats[j])
169 sctx->b.destroy_query(&sctx->b,
170 sctx->dcc_stats[i].ps_stats[j]);
171
172 r600_texture_reference(&sctx->dcc_stats[i].tex, NULL);
173 }
174
175 if (sctx->query_result_shader)
176 sctx->b.delete_compute_state(&sctx->b, sctx->query_result_shader);
177
178 if (sctx->gfx_cs)
179 sctx->ws->cs_destroy(sctx->gfx_cs);
180 if (sctx->dma_cs)
181 sctx->ws->cs_destroy(sctx->dma_cs);
182 if (sctx->ctx)
183 sctx->ws->ctx_destroy(sctx->ctx);
184
185 if (sctx->b.stream_uploader)
186 u_upload_destroy(sctx->b.stream_uploader);
187 if (sctx->b.const_uploader)
188 u_upload_destroy(sctx->b.const_uploader);
189 if (sctx->cached_gtt_allocator)
190 u_upload_destroy(sctx->cached_gtt_allocator);
191
192 slab_destroy_child(&sctx->pool_transfers);
193 slab_destroy_child(&sctx->pool_transfers_unsync);
194
195 if (sctx->allocator_zeroed_memory)
196 u_suballocator_destroy(sctx->allocator_zeroed_memory);
197
198 sctx->ws->fence_reference(&sctx->last_gfx_fence, NULL);
199 sctx->ws->fence_reference(&sctx->last_sdma_fence, NULL);
200 r600_resource_reference(&sctx->eop_bug_scratch, NULL);
201
202 LLVMDisposeTargetMachine(sctx->tm);
203
204 si_saved_cs_reference(&sctx->current_saved_cs, NULL);
205
206 _mesa_hash_table_destroy(sctx->tex_handles, NULL);
207 _mesa_hash_table_destroy(sctx->img_handles, NULL);
208
209 util_dynarray_fini(&sctx->resident_tex_handles);
210 util_dynarray_fini(&sctx->resident_img_handles);
211 util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);
212 util_dynarray_fini(&sctx->resident_img_needs_color_decompress);
213 util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);
214 FREE(sctx);
215 }
216
217 static enum pipe_reset_status
218 si_amdgpu_get_reset_status(struct pipe_context *ctx)
219 {
220 struct si_context *sctx = (struct si_context *)ctx;
221
222 return sctx->ws->ctx_query_reset_status(sctx->ctx);
223 }
224
225 static enum pipe_reset_status si_get_reset_status(struct pipe_context *ctx)
226 {
227 struct si_context *sctx = (struct si_context *)ctx;
228 unsigned latest = sctx->ws->query_value(sctx->ws,
229 RADEON_GPU_RESET_COUNTER);
230
231 if (sctx->gpu_reset_counter == latest)
232 return PIPE_NO_RESET;
233
234 sctx->gpu_reset_counter = latest;
235 return PIPE_UNKNOWN_CONTEXT_RESET;
236 }
237
238 static void si_set_device_reset_callback(struct pipe_context *ctx,
239 const struct pipe_device_reset_callback *cb)
240 {
241 struct si_context *sctx = (struct si_context *)ctx;
242
243 if (cb)
244 sctx->device_reset_callback = *cb;
245 else
246 memset(&sctx->device_reset_callback, 0,
247 sizeof(sctx->device_reset_callback));
248 }
249
250 bool si_check_device_reset(struct si_context *sctx)
251 {
252 enum pipe_reset_status status;
253
254 if (!sctx->device_reset_callback.reset)
255 return false;
256
257 if (!sctx->b.get_device_reset_status)
258 return false;
259
260 status = sctx->b.get_device_reset_status(&sctx->b);
261 if (status == PIPE_NO_RESET)
262 return false;
263
264 sctx->device_reset_callback.reset(sctx->device_reset_callback.data, status);
265 return true;
266 }
267
268 /* Apitrace profiling:
269 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
270 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
271 * and remember its number.
272 * 3) In Mesa, enable queries and performance counters around that draw
273 * call and print the results.
274 * 4) glretrace --benchmark --markers ..
275 */
276 static void si_emit_string_marker(struct pipe_context *ctx,
277 const char *string, int len)
278 {
279 struct si_context *sctx = (struct si_context *)ctx;
280
281 dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);
282
283 if (sctx->log)
284 u_log_printf(sctx->log, "\nString marker: %*s\n", len, string);
285 }
286
287 static LLVMTargetMachineRef
288 si_create_llvm_target_machine(struct si_screen *sscreen)
289 {
290 enum ac_target_machine_options tm_options =
291 (sscreen->debug_flags & DBG(SI_SCHED) ? AC_TM_SISCHED : 0) |
292 (sscreen->info.chip_class >= GFX9 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
293 (sscreen->info.chip_class < GFX9 ? AC_TM_FORCE_DISABLE_XNACK : 0) |
294 (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0);
295
296 return ac_create_target_machine(sscreen->info.family, tm_options);
297 }
298
299 static void si_set_debug_callback(struct pipe_context *ctx,
300 const struct pipe_debug_callback *cb)
301 {
302 struct si_context *sctx = (struct si_context *)ctx;
303 struct si_screen *screen = sctx->screen;
304
305 util_queue_finish(&screen->shader_compiler_queue);
306 util_queue_finish(&screen->shader_compiler_queue_low_priority);
307
308 if (cb)
309 sctx->debug = *cb;
310 else
311 memset(&sctx->debug, 0, sizeof(sctx->debug));
312 }
313
314 static void si_set_log_context(struct pipe_context *ctx,
315 struct u_log_context *log)
316 {
317 struct si_context *sctx = (struct si_context *)ctx;
318 sctx->log = log;
319
320 if (log)
321 u_log_add_auto_logger(log, si_auto_log_cs, sctx);
322 }
323
324 static struct pipe_context *si_create_context(struct pipe_screen *screen,
325 unsigned flags)
326 {
327 struct si_context *sctx = CALLOC_STRUCT(si_context);
328 struct si_screen* sscreen = (struct si_screen *)screen;
329 struct radeon_winsys *ws = sscreen->ws;
330 int shader, i;
331
332 if (!sctx)
333 return NULL;
334
335 if (flags & PIPE_CONTEXT_DEBUG)
336 sscreen->record_llvm_ir = true; /* racy but not critical */
337
338 sctx->b.screen = screen; /* this must be set first */
339 sctx->b.priv = NULL;
340 sctx->b.destroy = si_destroy_context;
341 sctx->b.emit_string_marker = si_emit_string_marker;
342 sctx->b.set_debug_callback = si_set_debug_callback;
343 sctx->b.set_log_context = si_set_log_context;
344 sctx->screen = sscreen; /* Easy accessing of screen/winsys. */
345 sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0;
346
347 slab_create_child(&sctx->pool_transfers, &sscreen->pool_transfers);
348 slab_create_child(&sctx->pool_transfers_unsync, &sscreen->pool_transfers);
349
350 sctx->ws = sscreen->ws;
351 sctx->family = sscreen->info.family;
352 sctx->chip_class = sscreen->info.chip_class;
353
354 if (sscreen->info.drm_major == 2 && sscreen->info.drm_minor >= 43) {
355 sctx->b.get_device_reset_status = si_get_reset_status;
356 sctx->gpu_reset_counter =
357 sctx->ws->query_value(sctx->ws,
358 RADEON_GPU_RESET_COUNTER);
359 }
360
361 sctx->b.set_device_reset_callback = si_set_device_reset_callback;
362
363 si_init_context_texture_functions(sctx);
364 si_init_query_functions(sctx);
365
366 if (sctx->chip_class == CIK ||
367 sctx->chip_class == VI ||
368 sctx->chip_class == GFX9) {
369 sctx->eop_bug_scratch = (struct r600_resource*)
370 pipe_buffer_create(&sscreen->b, 0, PIPE_USAGE_DEFAULT,
371 16 * sscreen->info.num_render_backends);
372 if (!sctx->eop_bug_scratch)
373 goto fail;
374 }
375
376 sctx->allocator_zeroed_memory =
377 u_suballocator_create(&sctx->b, sscreen->info.gart_page_size,
378 0, PIPE_USAGE_DEFAULT, 0, true);
379 if (!sctx->allocator_zeroed_memory)
380 goto fail;
381
382 sctx->b.stream_uploader = u_upload_create(&sctx->b, 1024 * 1024,
383 0, PIPE_USAGE_STREAM,
384 SI_RESOURCE_FLAG_READ_ONLY);
385 if (!sctx->b.stream_uploader)
386 goto fail;
387
388 sctx->b.const_uploader = u_upload_create(&sctx->b, 128 * 1024,
389 0, PIPE_USAGE_DEFAULT,
390 SI_RESOURCE_FLAG_32BIT |
391 (sscreen->cpdma_prefetch_writes_memory ?
392 0 : SI_RESOURCE_FLAG_READ_ONLY));
393 if (!sctx->b.const_uploader)
394 goto fail;
395
396 sctx->cached_gtt_allocator = u_upload_create(&sctx->b, 16 * 1024,
397 0, PIPE_USAGE_STAGING, 0);
398 if (!sctx->cached_gtt_allocator)
399 goto fail;
400
401 sctx->ctx = sctx->ws->ctx_create(sctx->ws);
402 if (!sctx->ctx)
403 goto fail;
404
405 if (sscreen->info.num_sdma_rings && !(sscreen->debug_flags & DBG(NO_ASYNC_DMA))) {
406 sctx->dma_cs = sctx->ws->cs_create(sctx->ctx, RING_DMA,
407 (void*)si_flush_dma_cs,
408 sctx);
409 }
410
411 if (sscreen->info.drm_major == 3)
412 sctx->b.get_device_reset_status = si_amdgpu_get_reset_status;
413
414 si_init_buffer_functions(sctx);
415 si_init_clear_functions(sctx);
416 si_init_blit_functions(sctx);
417 si_init_compute_functions(sctx);
418 si_init_cp_dma_functions(sctx);
419 si_init_debug_functions(sctx);
420 si_init_msaa_functions(sctx);
421 si_init_streamout_functions(sctx);
422
423 if (sscreen->info.has_hw_decode) {
424 sctx->b.create_video_codec = si_uvd_create_decoder;
425 sctx->b.create_video_buffer = si_video_buffer_create;
426 } else {
427 sctx->b.create_video_codec = vl_create_decoder;
428 sctx->b.create_video_buffer = vl_video_buffer_create;
429 }
430
431 sctx->gfx_cs = ws->cs_create(sctx->ctx, RING_GFX,
432 (void*)si_flush_gfx_cs, sctx);
433
434 /* Border colors. */
435 sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS *
436 sizeof(*sctx->border_color_table));
437 if (!sctx->border_color_table)
438 goto fail;
439
440 sctx->border_color_buffer = (struct r600_resource*)
441 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT,
442 SI_MAX_BORDER_COLORS *
443 sizeof(*sctx->border_color_table));
444 if (!sctx->border_color_buffer)
445 goto fail;
446
447 sctx->border_color_map =
448 ws->buffer_map(sctx->border_color_buffer->buf,
449 NULL, PIPE_TRANSFER_WRITE);
450 if (!sctx->border_color_map)
451 goto fail;
452
453 si_init_all_descriptors(sctx);
454 si_init_fence_functions(sctx);
455 si_init_state_functions(sctx);
456 si_init_shader_functions(sctx);
457 si_init_viewport_functions(sctx);
458 si_init_ia_multi_vgt_param_table(sctx);
459
460 if (sctx->chip_class >= CIK)
461 cik_init_sdma_functions(sctx);
462 else
463 si_init_dma_functions(sctx);
464
465 if (sscreen->debug_flags & DBG(FORCE_DMA))
466 sctx->b.resource_copy_region = sctx->dma_copy;
467
468 sctx->blitter = util_blitter_create(&sctx->b);
469 if (sctx->blitter == NULL)
470 goto fail;
471 sctx->blitter->draw_rectangle = si_draw_rectangle;
472 sctx->blitter->skip_viewport_restore = true;
473
474 sctx->sample_mask.sample_mask = 0xffff;
475
476 if (sctx->chip_class >= GFX9) {
477 sctx->wait_mem_scratch = (struct r600_resource*)
478 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT, 4);
479 if (!sctx->wait_mem_scratch)
480 goto fail;
481
482 /* Initialize the memory. */
483 struct radeon_winsys_cs *cs = sctx->gfx_cs;
484 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 3, 0));
485 radeon_emit(cs, S_370_DST_SEL(V_370_MEMORY_SYNC) |
486 S_370_WR_CONFIRM(1) |
487 S_370_ENGINE_SEL(V_370_ME));
488 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address);
489 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address >> 32);
490 radeon_emit(cs, sctx->wait_mem_number);
491 radeon_add_to_buffer_list(sctx, cs, sctx->wait_mem_scratch,
492 RADEON_USAGE_WRITE, RADEON_PRIO_FENCE);
493 }
494
495 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
496 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
497 if (sctx->chip_class == CIK) {
498 sctx->null_const_buf.buffer =
499 si_aligned_buffer_create(screen,
500 SI_RESOURCE_FLAG_32BIT,
501 PIPE_USAGE_DEFAULT, 16,
502 sctx->screen->info.tcc_cache_line_size);
503 if (!sctx->null_const_buf.buffer)
504 goto fail;
505 sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;
506
507 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
508 for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {
509 sctx->b.set_constant_buffer(&sctx->b, shader, i,
510 &sctx->null_const_buf);
511 }
512 }
513
514 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS,
515 &sctx->null_const_buf);
516 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS,
517 &sctx->null_const_buf);
518 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES,
519 &sctx->null_const_buf);
520 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE,
521 &sctx->null_const_buf);
522 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS,
523 &sctx->null_const_buf);
524
525 /* Clear the NULL constant buffer, because loads should return zeros. */
526 si_clear_buffer(sctx, sctx->null_const_buf.buffer, 0,
527 sctx->null_const_buf.buffer->width0, 0,
528 SI_COHERENCY_SHADER);
529 }
530
531 uint64_t max_threads_per_block;
532 screen->get_compute_param(screen, PIPE_SHADER_IR_TGSI,
533 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,
534 &max_threads_per_block);
535
536 /* The maximum number of scratch waves. Scratch space isn't divided
537 * evenly between CUs. The number is only a function of the number of CUs.
538 * We can decrease the constant to decrease the scratch buffer size.
539 *
540 * sctx->scratch_waves must be >= the maximum posible size of
541 * 1 threadgroup, so that the hw doesn't hang from being unable
542 * to start any.
543 *
544 * The recommended value is 4 per CU at most. Higher numbers don't
545 * bring much benefit, but they still occupy chip resources (think
546 * async compute). I've seen ~2% performance difference between 4 and 32.
547 */
548 sctx->scratch_waves = MAX2(32 * sscreen->info.num_good_compute_units,
549 max_threads_per_block / 64);
550
551 sctx->tm = si_create_llvm_target_machine(sscreen);
552
553 /* Bindless handles. */
554 sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
555 _mesa_key_pointer_equal);
556 sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
557 _mesa_key_pointer_equal);
558
559 util_dynarray_init(&sctx->resident_tex_handles, NULL);
560 util_dynarray_init(&sctx->resident_img_handles, NULL);
561 util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);
562 util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);
563 util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);
564
565 /* this must be last */
566 si_begin_new_gfx_cs(sctx);
567 return &sctx->b;
568 fail:
569 fprintf(stderr, "radeonsi: Failed to create a context.\n");
570 si_destroy_context(&sctx->b);
571 return NULL;
572 }
573
574 static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen,
575 void *priv, unsigned flags)
576 {
577 struct si_screen *sscreen = (struct si_screen *)screen;
578 struct pipe_context *ctx;
579
580 if (sscreen->debug_flags & DBG(CHECK_VM))
581 flags |= PIPE_CONTEXT_DEBUG;
582
583 ctx = si_create_context(screen, flags);
584
585 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
586 return ctx;
587
588 /* Clover (compute-only) is unsupported. */
589 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
590 return ctx;
591
592 /* When shaders are logged to stderr, asynchronous compilation is
593 * disabled too. */
594 if (sscreen->debug_flags & DBG_ALL_SHADERS)
595 return ctx;
596
597 /* Use asynchronous flushes only on amdgpu, since the radeon
598 * implementation for fence_server_sync is incomplete. */
599 return threaded_context_create(ctx, &sscreen->pool_transfers,
600 si_replace_buffer_storage,
601 sscreen->info.drm_major >= 3 ? si_create_fence : NULL,
602 &((struct si_context*)ctx)->tc);
603 }
604
605 /*
606 * pipe_screen
607 */
608 static void si_destroy_screen(struct pipe_screen* pscreen)
609 {
610 struct si_screen *sscreen = (struct si_screen *)pscreen;
611 struct si_shader_part *parts[] = {
612 sscreen->vs_prologs,
613 sscreen->tcs_epilogs,
614 sscreen->gs_prologs,
615 sscreen->ps_prologs,
616 sscreen->ps_epilogs
617 };
618 unsigned i;
619
620 if (!sscreen->ws->unref(sscreen->ws))
621 return;
622
623 util_queue_destroy(&sscreen->shader_compiler_queue);
624 util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);
625
626 for (i = 0; i < ARRAY_SIZE(sscreen->tm); i++)
627 if (sscreen->tm[i])
628 LLVMDisposeTargetMachine(sscreen->tm[i]);
629
630 for (i = 0; i < ARRAY_SIZE(sscreen->tm_low_priority); i++)
631 if (sscreen->tm_low_priority[i])
632 LLVMDisposeTargetMachine(sscreen->tm_low_priority[i]);
633
634 /* Free shader parts. */
635 for (i = 0; i < ARRAY_SIZE(parts); i++) {
636 while (parts[i]) {
637 struct si_shader_part *part = parts[i];
638
639 parts[i] = part->next;
640 ac_shader_binary_clean(&part->binary);
641 FREE(part);
642 }
643 }
644 mtx_destroy(&sscreen->shader_parts_mutex);
645 si_destroy_shader_cache(sscreen);
646
647 si_perfcounters_destroy(sscreen);
648 si_gpu_load_kill_thread(sscreen);
649
650 mtx_destroy(&sscreen->gpu_load_mutex);
651 mtx_destroy(&sscreen->aux_context_lock);
652 sscreen->aux_context->destroy(sscreen->aux_context);
653
654 slab_destroy_parent(&sscreen->pool_transfers);
655
656 disk_cache_destroy(sscreen->disk_shader_cache);
657 sscreen->ws->destroy(sscreen->ws);
658 FREE(sscreen);
659 }
660
661 static void si_init_gs_info(struct si_screen *sscreen)
662 {
663 sscreen->gs_table_depth = ac_get_gs_table_depth(sscreen->info.chip_class,
664 sscreen->info.family);
665 }
666
667 static void si_handle_env_var_force_family(struct si_screen *sscreen)
668 {
669 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
670 unsigned i;
671
672 if (!family)
673 return;
674
675 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
676 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
677 /* Override family and chip_class. */
678 sscreen->info.family = i;
679
680 if (i >= CHIP_VEGA10)
681 sscreen->info.chip_class = GFX9;
682 else if (i >= CHIP_TONGA)
683 sscreen->info.chip_class = VI;
684 else if (i >= CHIP_BONAIRE)
685 sscreen->info.chip_class = CIK;
686 else
687 sscreen->info.chip_class = SI;
688
689 /* Don't submit any IBs. */
690 setenv("RADEON_NOOP", "1", 1);
691 return;
692 }
693 }
694
695 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
696 exit(1);
697 }
698
699 static void si_test_vmfault(struct si_screen *sscreen)
700 {
701 struct pipe_context *ctx = sscreen->aux_context;
702 struct si_context *sctx = (struct si_context *)ctx;
703 struct pipe_resource *buf =
704 pipe_buffer_create_const0(&sscreen->b, 0, PIPE_USAGE_DEFAULT, 64);
705
706 if (!buf) {
707 puts("Buffer allocation failed.");
708 exit(1);
709 }
710
711 r600_resource(buf)->gpu_address = 0; /* cause a VM fault */
712
713 if (sscreen->debug_flags & DBG(TEST_VMFAULT_CP)) {
714 si_copy_buffer(sctx, buf, buf, 0, 4, 4, 0);
715 ctx->flush(ctx, NULL, 0);
716 puts("VM fault test: CP - done.");
717 }
718 if (sscreen->debug_flags & DBG(TEST_VMFAULT_SDMA)) {
719 sctx->dma_clear_buffer(sctx, buf, 0, 4, 0);
720 ctx->flush(ctx, NULL, 0);
721 puts("VM fault test: SDMA - done.");
722 }
723 if (sscreen->debug_flags & DBG(TEST_VMFAULT_SHADER)) {
724 util_test_constant_buffer(ctx, buf);
725 puts("VM fault test: Shader - done.");
726 }
727 exit(0);
728 }
729
730 static void si_disk_cache_create(struct si_screen *sscreen)
731 {
732 /* Don't use the cache if shader dumping is enabled. */
733 if (sscreen->debug_flags & DBG_ALL_SHADERS)
734 return;
735
736 uint32_t mesa_timestamp;
737 if (disk_cache_get_function_timestamp(si_disk_cache_create,
738 &mesa_timestamp)) {
739 char *timestamp_str;
740 int res = -1;
741 uint32_t llvm_timestamp;
742
743 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo,
744 &llvm_timestamp)) {
745 res = asprintf(&timestamp_str, "%u_%u",
746 mesa_timestamp, llvm_timestamp);
747 }
748
749 if (res != -1) {
750 /* These flags affect shader compilation. */
751 #define ALL_FLAGS (DBG(FS_CORRECT_DERIVS_AFTER_KILL) | \
752 DBG(SI_SCHED) | \
753 DBG(UNSAFE_MATH) | \
754 DBG(NIR))
755 uint64_t shader_debug_flags = sscreen->debug_flags &
756 ALL_FLAGS;
757
758 /* Add the high bits of 32-bit addresses, which affects
759 * how 32-bit addresses are expanded to 64 bits.
760 */
761 STATIC_ASSERT(ALL_FLAGS <= UINT_MAX);
762 shader_debug_flags |= (uint64_t)sscreen->info.address32_hi << 32;
763
764 sscreen->disk_shader_cache =
765 disk_cache_create(si_get_family_name(sscreen),
766 timestamp_str,
767 shader_debug_flags);
768 free(timestamp_str);
769 }
770 }
771 }
772
773 struct pipe_screen *radeonsi_screen_create(struct radeon_winsys *ws,
774 const struct pipe_screen_config *config)
775 {
776 struct si_screen *sscreen = CALLOC_STRUCT(si_screen);
777 unsigned num_threads, num_compiler_threads, num_compiler_threads_lowprio, i;
778
779 if (!sscreen) {
780 return NULL;
781 }
782
783 sscreen->ws = ws;
784 ws->query_info(ws, &sscreen->info);
785
786 sscreen->debug_flags = debug_get_flags_option("R600_DEBUG",
787 debug_options, 0);
788
789 /* Set functions first. */
790 sscreen->b.context_create = si_pipe_create_context;
791 sscreen->b.destroy = si_destroy_screen;
792
793 si_init_screen_get_functions(sscreen);
794 si_init_screen_buffer_functions(sscreen);
795 si_init_screen_fence_functions(sscreen);
796 si_init_screen_state_functions(sscreen);
797 si_init_screen_texture_functions(sscreen);
798 si_init_screen_query_functions(sscreen);
799
800 /* Set these flags in debug_flags early, so that the shader cache takes
801 * them into account.
802 */
803 if (driQueryOptionb(config->options,
804 "glsl_correct_derivatives_after_discard"))
805 sscreen->debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);
806 if (driQueryOptionb(config->options, "radeonsi_enable_sisched"))
807 sscreen->debug_flags |= DBG(SI_SCHED);
808
809
810 if (sscreen->debug_flags & DBG(INFO))
811 ac_print_gpu_info(&sscreen->info);
812
813 slab_create_parent(&sscreen->pool_transfers,
814 sizeof(struct r600_transfer), 64);
815
816 sscreen->force_aniso = MIN2(16, debug_get_num_option("R600_TEX_ANISO", -1));
817 if (sscreen->force_aniso >= 0) {
818 printf("radeonsi: Forcing anisotropy filter to %ix\n",
819 /* round down to a power of two */
820 1 << util_logbase2(sscreen->force_aniso));
821 }
822
823 (void) mtx_init(&sscreen->aux_context_lock, mtx_plain);
824 (void) mtx_init(&sscreen->gpu_load_mutex, mtx_plain);
825
826 si_init_gs_info(sscreen);
827 if (!si_init_shader_cache(sscreen)) {
828 FREE(sscreen);
829 return NULL;
830 }
831
832 si_disk_cache_create(sscreen);
833
834 /* Only enable as many threads as we have target machines, but at most
835 * the number of CPUs - 1 if there is more than one.
836 */
837 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
838 num_threads = MAX2(1, num_threads - 1);
839 num_compiler_threads = MIN2(num_threads, ARRAY_SIZE(sscreen->tm));
840 num_compiler_threads_lowprio =
841 MIN2(num_threads, ARRAY_SIZE(sscreen->tm_low_priority));
842
843 if (!util_queue_init(&sscreen->shader_compiler_queue, "si_shader",
844 32, num_compiler_threads,
845 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
846 si_destroy_shader_cache(sscreen);
847 FREE(sscreen);
848 return NULL;
849 }
850
851 if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority,
852 "si_shader_low",
853 32, num_compiler_threads_lowprio,
854 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
855 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY)) {
856 si_destroy_shader_cache(sscreen);
857 FREE(sscreen);
858 return NULL;
859 }
860
861 si_handle_env_var_force_family(sscreen);
862
863 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
864 si_init_perfcounters(sscreen);
865
866 /* Determine tessellation ring info. */
867 bool double_offchip_buffers = sscreen->info.chip_class >= CIK &&
868 sscreen->info.family != CHIP_CARRIZO &&
869 sscreen->info.family != CHIP_STONEY;
870 /* This must be one less than the maximum number due to a hw limitation.
871 * Various hardware bugs in SI, CIK, and GFX9 need this.
872 */
873 unsigned max_offchip_buffers_per_se;
874
875 /* Only certain chips can use the maximum value. */
876 if (sscreen->info.family == CHIP_VEGA12)
877 max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
878 else
879 max_offchip_buffers_per_se = double_offchip_buffers ? 127 : 63;
880
881 unsigned max_offchip_buffers = max_offchip_buffers_per_se *
882 sscreen->info.max_se;
883 unsigned offchip_granularity;
884
885 /* Hawaii has a bug with offchip buffers > 256 that can be worked
886 * around by setting 4K granularity.
887 */
888 if (sscreen->info.family == CHIP_HAWAII) {
889 sscreen->tess_offchip_block_dw_size = 4096;
890 offchip_granularity = V_03093C_X_4K_DWORDS;
891 } else {
892 sscreen->tess_offchip_block_dw_size = 8192;
893 offchip_granularity = V_03093C_X_8K_DWORDS;
894 }
895
896 sscreen->tess_factor_ring_size = 32768 * sscreen->info.max_se;
897 assert(((sscreen->tess_factor_ring_size / 4) & C_030938_SIZE) == 0);
898 sscreen->tess_offchip_ring_size = max_offchip_buffers *
899 sscreen->tess_offchip_block_dw_size * 4;
900
901 if (sscreen->info.chip_class >= CIK) {
902 if (sscreen->info.chip_class >= VI)
903 --max_offchip_buffers;
904 sscreen->vgt_hs_offchip_param =
905 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
906 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity);
907 } else {
908 assert(offchip_granularity == V_03093C_X_8K_DWORDS);
909 sscreen->vgt_hs_offchip_param =
910 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers);
911 }
912
913 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
914 * on SI. */
915 sscreen->has_clear_state = sscreen->info.chip_class >= CIK;
916
917 sscreen->has_distributed_tess =
918 sscreen->info.chip_class >= VI &&
919 sscreen->info.max_se >= 2;
920
921 sscreen->has_draw_indirect_multi =
922 (sscreen->info.family >= CHIP_POLARIS10) ||
923 (sscreen->info.chip_class == VI &&
924 sscreen->info.pfp_fw_version >= 121 &&
925 sscreen->info.me_fw_version >= 87) ||
926 (sscreen->info.chip_class == CIK &&
927 sscreen->info.pfp_fw_version >= 211 &&
928 sscreen->info.me_fw_version >= 173) ||
929 (sscreen->info.chip_class == SI &&
930 sscreen->info.pfp_fw_version >= 79 &&
931 sscreen->info.me_fw_version >= 142);
932
933 sscreen->has_out_of_order_rast = sscreen->info.chip_class >= VI &&
934 sscreen->info.max_se >= 2 &&
935 !(sscreen->debug_flags & DBG(NO_OUT_OF_ORDER));
936 sscreen->assume_no_z_fights =
937 driQueryOptionb(config->options, "radeonsi_assume_no_z_fights");
938 sscreen->commutative_blend_add =
939 driQueryOptionb(config->options, "radeonsi_commutative_blend_add");
940 sscreen->clear_db_cache_before_clear =
941 driQueryOptionb(config->options, "radeonsi_clear_db_cache_before_clear");
942 sscreen->has_msaa_sample_loc_bug = (sscreen->info.family >= CHIP_POLARIS10 &&
943 sscreen->info.family <= CHIP_POLARIS12) ||
944 sscreen->info.family == CHIP_VEGA10 ||
945 sscreen->info.family == CHIP_RAVEN;
946 sscreen->has_ls_vgpr_init_bug = sscreen->info.family == CHIP_VEGA10 ||
947 sscreen->info.family == CHIP_RAVEN;
948
949 if (sscreen->debug_flags & DBG(DPBB)) {
950 sscreen->dpbb_allowed = true;
951 } else {
952 /* Only enable primitive binning on Raven by default. */
953 /* TODO: Investigate if binning is profitable on Vega12. */
954 sscreen->dpbb_allowed = sscreen->info.family == CHIP_RAVEN &&
955 !(sscreen->debug_flags & DBG(NO_DPBB));
956 }
957
958 if (sscreen->debug_flags & DBG(DFSM)) {
959 sscreen->dfsm_allowed = sscreen->dpbb_allowed;
960 } else {
961 sscreen->dfsm_allowed = sscreen->dpbb_allowed &&
962 !(sscreen->debug_flags & DBG(NO_DFSM));
963 }
964
965 /* While it would be nice not to have this flag, we are constrained
966 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
967 * on GFX9.
968 */
969 sscreen->llvm_has_working_vgpr_indexing = sscreen->info.chip_class <= VI;
970
971 /* Some chips have RB+ registers, but don't support RB+. Those must
972 * always disable it.
973 */
974 if (sscreen->info.family == CHIP_STONEY ||
975 sscreen->info.chip_class >= GFX9) {
976 sscreen->has_rbplus = true;
977
978 sscreen->rbplus_allowed =
979 !(sscreen->debug_flags & DBG(NO_RB_PLUS)) &&
980 (sscreen->info.family == CHIP_STONEY ||
981 sscreen->info.family == CHIP_VEGA12 ||
982 sscreen->info.family == CHIP_RAVEN);
983 }
984
985 sscreen->dcc_msaa_allowed =
986 !(sscreen->debug_flags & DBG(NO_DCC_MSAA));
987
988 sscreen->cpdma_prefetch_writes_memory = sscreen->info.chip_class <= VI;
989
990 (void) mtx_init(&sscreen->shader_parts_mutex, mtx_plain);
991 sscreen->use_monolithic_shaders =
992 (sscreen->debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;
993
994 sscreen->barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SMEM_L1 |
995 SI_CONTEXT_INV_VMEM_L1;
996 if (sscreen->info.chip_class <= VI) {
997 sscreen->barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_GLOBAL_L2;
998 sscreen->barrier_flags.L2_to_cp |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
999 }
1000
1001 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
1002 sscreen->debug_flags |= DBG_ALL_SHADERS;
1003
1004 for (i = 0; i < num_compiler_threads; i++)
1005 sscreen->tm[i] = si_create_llvm_target_machine(sscreen);
1006 for (i = 0; i < num_compiler_threads_lowprio; i++)
1007 sscreen->tm_low_priority[i] = si_create_llvm_target_machine(sscreen);
1008
1009 /* Create the auxiliary context. This must be done last. */
1010 sscreen->aux_context = si_create_context(&sscreen->b, 0);
1011
1012 if (sscreen->debug_flags & DBG(TEST_DMA))
1013 si_test_dma(sscreen);
1014
1015 if (sscreen->debug_flags & (DBG(TEST_VMFAULT_CP) |
1016 DBG(TEST_VMFAULT_SDMA) |
1017 DBG(TEST_VMFAULT_SHADER)))
1018 si_test_vmfault(sscreen);
1019
1020 return &sscreen->b;
1021 }