radeonsi: set all pipe buffer functions in r600_buffer_common.c
[mesa.git] / src / gallium / drivers / radeonsi / si_pipe.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "si_pipe.h"
25 #include "si_public.h"
26 #include "si_shader_internal.h"
27 #include "sid.h"
28
29 #include "radeon/radeon_uvd.h"
30 #include "util/hash_table.h"
31 #include "util/u_log.h"
32 #include "util/u_memory.h"
33 #include "util/u_suballoc.h"
34 #include "util/u_tests.h"
35 #include "util/xmlconfig.h"
36 #include "vl/vl_decoder.h"
37 #include "../ddebug/dd_util.h"
38
39 /*
40 * pipe_context
41 */
42 static void si_destroy_context(struct pipe_context *context)
43 {
44 struct si_context *sctx = (struct si_context *)context;
45 int i;
46
47 /* Unreference the framebuffer normally to disable related logic
48 * properly.
49 */
50 struct pipe_framebuffer_state fb = {};
51 if (context->set_framebuffer_state)
52 context->set_framebuffer_state(context, &fb);
53
54 si_release_all_descriptors(sctx);
55
56 pipe_resource_reference(&sctx->esgs_ring, NULL);
57 pipe_resource_reference(&sctx->gsvs_ring, NULL);
58 pipe_resource_reference(&sctx->tf_ring, NULL);
59 pipe_resource_reference(&sctx->tess_offchip_ring, NULL);
60 pipe_resource_reference(&sctx->null_const_buf.buffer, NULL);
61 r600_resource_reference(&sctx->border_color_buffer, NULL);
62 free(sctx->border_color_table);
63 r600_resource_reference(&sctx->scratch_buffer, NULL);
64 r600_resource_reference(&sctx->compute_scratch_buffer, NULL);
65 r600_resource_reference(&sctx->wait_mem_scratch, NULL);
66
67 si_pm4_free_state(sctx, sctx->init_config, ~0);
68 if (sctx->init_config_gs_rings)
69 si_pm4_free_state(sctx, sctx->init_config_gs_rings, ~0);
70 for (i = 0; i < ARRAY_SIZE(sctx->vgt_shader_config); i++)
71 si_pm4_delete_state(sctx, vgt_shader_config, sctx->vgt_shader_config[i]);
72
73 if (sctx->fixed_func_tcs_shader.cso)
74 sctx->b.b.delete_tcs_state(&sctx->b.b, sctx->fixed_func_tcs_shader.cso);
75 if (sctx->custom_dsa_flush)
76 sctx->b.b.delete_depth_stencil_alpha_state(&sctx->b.b, sctx->custom_dsa_flush);
77 if (sctx->custom_blend_resolve)
78 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_resolve);
79 if (sctx->custom_blend_fmask_decompress)
80 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_fmask_decompress);
81 if (sctx->custom_blend_eliminate_fastclear)
82 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_eliminate_fastclear);
83 if (sctx->custom_blend_dcc_decompress)
84 sctx->b.b.delete_blend_state(&sctx->b.b, sctx->custom_blend_dcc_decompress);
85 if (sctx->vs_blit_pos)
86 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos);
87 if (sctx->vs_blit_pos_layered)
88 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_pos_layered);
89 if (sctx->vs_blit_color)
90 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color);
91 if (sctx->vs_blit_color_layered)
92 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_color_layered);
93 if (sctx->vs_blit_texcoord)
94 sctx->b.b.delete_vs_state(&sctx->b.b, sctx->vs_blit_texcoord);
95
96 if (sctx->blitter)
97 util_blitter_destroy(sctx->blitter);
98
99 si_common_context_cleanup(&sctx->b);
100
101 LLVMDisposeTargetMachine(sctx->tm);
102
103 si_saved_cs_reference(&sctx->current_saved_cs, NULL);
104
105 _mesa_hash_table_destroy(sctx->tex_handles, NULL);
106 _mesa_hash_table_destroy(sctx->img_handles, NULL);
107
108 util_dynarray_fini(&sctx->resident_tex_handles);
109 util_dynarray_fini(&sctx->resident_img_handles);
110 util_dynarray_fini(&sctx->resident_tex_needs_color_decompress);
111 util_dynarray_fini(&sctx->resident_img_needs_color_decompress);
112 util_dynarray_fini(&sctx->resident_tex_needs_depth_decompress);
113 FREE(sctx);
114 }
115
116 static enum pipe_reset_status
117 si_amdgpu_get_reset_status(struct pipe_context *ctx)
118 {
119 struct si_context *sctx = (struct si_context *)ctx;
120
121 return sctx->b.ws->ctx_query_reset_status(sctx->b.ctx);
122 }
123
124 /* Apitrace profiling:
125 * 1) qapitrace : Tools -> Profile: Measure CPU & GPU times
126 * 2) In the middle panel, zoom in (mouse wheel) on some bad draw call
127 * and remember its number.
128 * 3) In Mesa, enable queries and performance counters around that draw
129 * call and print the results.
130 * 4) glretrace --benchmark --markers ..
131 */
132 static void si_emit_string_marker(struct pipe_context *ctx,
133 const char *string, int len)
134 {
135 struct si_context *sctx = (struct si_context *)ctx;
136
137 dd_parse_apitrace_marker(string, len, &sctx->apitrace_call_number);
138
139 if (sctx->b.log)
140 u_log_printf(sctx->b.log, "\nString marker: %*s\n", len, string);
141 }
142
143 static LLVMTargetMachineRef
144 si_create_llvm_target_machine(struct si_screen *sscreen)
145 {
146 enum ac_target_machine_options tm_options =
147 (sscreen->b.debug_flags & DBG(SI_SCHED) ? AC_TM_SISCHED : 0) |
148 (sscreen->b.chip_class >= GFX9 ? AC_TM_FORCE_ENABLE_XNACK : 0) |
149 (sscreen->b.chip_class < GFX9 ? AC_TM_FORCE_DISABLE_XNACK : 0) |
150 (!sscreen->llvm_has_working_vgpr_indexing ? AC_TM_PROMOTE_ALLOCA_TO_SCRATCH : 0);
151
152 return ac_create_target_machine(sscreen->b.family, tm_options);
153 }
154
155 static void si_set_debug_callback(struct pipe_context *ctx,
156 const struct pipe_debug_callback *cb)
157 {
158 struct si_context *sctx = (struct si_context *)ctx;
159 struct si_screen *screen = sctx->screen;
160
161 util_queue_finish(&screen->shader_compiler_queue);
162 util_queue_finish(&screen->shader_compiler_queue_low_priority);
163
164 if (cb)
165 sctx->debug = *cb;
166 else
167 memset(&sctx->debug, 0, sizeof(sctx->debug));
168 }
169
170 static void si_set_log_context(struct pipe_context *ctx,
171 struct u_log_context *log)
172 {
173 struct si_context *sctx = (struct si_context *)ctx;
174 sctx->b.log = log;
175
176 if (log)
177 u_log_add_auto_logger(log, si_auto_log_cs, sctx);
178 }
179
180 static struct pipe_context *si_create_context(struct pipe_screen *screen,
181 unsigned flags)
182 {
183 struct si_context *sctx = CALLOC_STRUCT(si_context);
184 struct si_screen* sscreen = (struct si_screen *)screen;
185 struct radeon_winsys *ws = sscreen->b.ws;
186 int shader, i;
187
188 if (!sctx)
189 return NULL;
190
191 if (flags & PIPE_CONTEXT_DEBUG)
192 sscreen->record_llvm_ir = true; /* racy but not critical */
193
194 sctx->b.b.screen = screen; /* this must be set first */
195 sctx->b.b.priv = NULL;
196 sctx->b.b.destroy = si_destroy_context;
197 sctx->b.b.emit_string_marker = si_emit_string_marker;
198 sctx->b.b.set_debug_callback = si_set_debug_callback;
199 sctx->b.b.set_log_context = si_set_log_context;
200 sctx->b.set_atom_dirty = (void *)si_set_atom_dirty;
201 sctx->screen = sscreen; /* Easy accessing of screen/winsys. */
202 sctx->is_debug = (flags & PIPE_CONTEXT_DEBUG) != 0;
203
204 if (!si_common_context_init(&sctx->b, &sscreen->b, flags))
205 goto fail;
206
207 if (sscreen->b.info.drm_major == 3)
208 sctx->b.b.get_device_reset_status = si_amdgpu_get_reset_status;
209
210 si_init_buffer_functions(sctx);
211 si_init_clear_functions(sctx);
212 si_init_blit_functions(sctx);
213 si_init_compute_functions(sctx);
214 si_init_cp_dma_functions(sctx);
215 si_init_debug_functions(sctx);
216 si_init_msaa_functions(sctx);
217 si_init_streamout_functions(sctx);
218
219 if (sscreen->b.info.has_hw_decode) {
220 sctx->b.b.create_video_codec = si_uvd_create_decoder;
221 sctx->b.b.create_video_buffer = si_video_buffer_create;
222 } else {
223 sctx->b.b.create_video_codec = vl_create_decoder;
224 sctx->b.b.create_video_buffer = vl_video_buffer_create;
225 }
226
227 sctx->b.gfx.cs = ws->cs_create(sctx->b.ctx, RING_GFX,
228 si_context_gfx_flush, sctx);
229 sctx->b.gfx.flush = si_context_gfx_flush;
230
231 /* Border colors. */
232 sctx->border_color_table = malloc(SI_MAX_BORDER_COLORS *
233 sizeof(*sctx->border_color_table));
234 if (!sctx->border_color_table)
235 goto fail;
236
237 sctx->border_color_buffer = (struct r600_resource*)
238 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT,
239 SI_MAX_BORDER_COLORS *
240 sizeof(*sctx->border_color_table));
241 if (!sctx->border_color_buffer)
242 goto fail;
243
244 sctx->border_color_map =
245 ws->buffer_map(sctx->border_color_buffer->buf,
246 NULL, PIPE_TRANSFER_WRITE);
247 if (!sctx->border_color_map)
248 goto fail;
249
250 si_init_all_descriptors(sctx);
251 si_init_fence_functions(sctx);
252 si_init_state_functions(sctx);
253 si_init_shader_functions(sctx);
254 si_init_viewport_functions(sctx);
255 si_init_ia_multi_vgt_param_table(sctx);
256
257 if (sctx->b.chip_class >= CIK)
258 cik_init_sdma_functions(sctx);
259 else
260 si_init_dma_functions(sctx);
261
262 if (sscreen->b.debug_flags & DBG(FORCE_DMA))
263 sctx->b.b.resource_copy_region = sctx->b.dma_copy;
264
265 sctx->blitter = util_blitter_create(&sctx->b.b);
266 if (sctx->blitter == NULL)
267 goto fail;
268 sctx->blitter->draw_rectangle = si_draw_rectangle;
269 sctx->blitter->skip_viewport_restore = true;
270
271 sctx->sample_mask.sample_mask = 0xffff;
272
273 /* these must be last */
274 si_begin_new_cs(sctx);
275
276 if (sctx->b.chip_class >= GFX9) {
277 sctx->wait_mem_scratch = (struct r600_resource*)
278 pipe_buffer_create(screen, 0, PIPE_USAGE_DEFAULT, 4);
279 if (!sctx->wait_mem_scratch)
280 goto fail;
281
282 /* Initialize the memory. */
283 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
284 radeon_emit(cs, PKT3(PKT3_WRITE_DATA, 3, 0));
285 radeon_emit(cs, S_370_DST_SEL(V_370_MEMORY_SYNC) |
286 S_370_WR_CONFIRM(1) |
287 S_370_ENGINE_SEL(V_370_ME));
288 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address);
289 radeon_emit(cs, sctx->wait_mem_scratch->gpu_address >> 32);
290 radeon_emit(cs, sctx->wait_mem_number);
291 }
292
293 /* CIK cannot unbind a constant buffer (S_BUFFER_LOAD doesn't skip loads
294 * if NUM_RECORDS == 0). We need to use a dummy buffer instead. */
295 if (sctx->b.chip_class == CIK) {
296 sctx->null_const_buf.buffer =
297 si_aligned_buffer_create(screen,
298 R600_RESOURCE_FLAG_UNMAPPABLE,
299 PIPE_USAGE_DEFAULT, 16,
300 sctx->screen->b.info.tcc_cache_line_size);
301 if (!sctx->null_const_buf.buffer)
302 goto fail;
303 sctx->null_const_buf.buffer_size = sctx->null_const_buf.buffer->width0;
304
305 for (shader = 0; shader < SI_NUM_SHADERS; shader++) {
306 for (i = 0; i < SI_NUM_CONST_BUFFERS; i++) {
307 sctx->b.b.set_constant_buffer(&sctx->b.b, shader, i,
308 &sctx->null_const_buf);
309 }
310 }
311
312 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS,
313 &sctx->null_const_buf);
314 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS,
315 &sctx->null_const_buf);
316 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES,
317 &sctx->null_const_buf);
318 si_set_rw_buffer(sctx, SI_PS_CONST_POLY_STIPPLE,
319 &sctx->null_const_buf);
320 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS,
321 &sctx->null_const_buf);
322
323 /* Clear the NULL constant buffer, because loads should return zeros. */
324 si_clear_buffer(&sctx->b.b, sctx->null_const_buf.buffer, 0,
325 sctx->null_const_buf.buffer->width0, 0,
326 R600_COHERENCY_SHADER);
327 }
328
329 uint64_t max_threads_per_block;
330 screen->get_compute_param(screen, PIPE_SHADER_IR_TGSI,
331 PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK,
332 &max_threads_per_block);
333
334 /* The maximum number of scratch waves. Scratch space isn't divided
335 * evenly between CUs. The number is only a function of the number of CUs.
336 * We can decrease the constant to decrease the scratch buffer size.
337 *
338 * sctx->scratch_waves must be >= the maximum posible size of
339 * 1 threadgroup, so that the hw doesn't hang from being unable
340 * to start any.
341 *
342 * The recommended value is 4 per CU at most. Higher numbers don't
343 * bring much benefit, but they still occupy chip resources (think
344 * async compute). I've seen ~2% performance difference between 4 and 32.
345 */
346 sctx->scratch_waves = MAX2(32 * sscreen->b.info.num_good_compute_units,
347 max_threads_per_block / 64);
348
349 sctx->tm = si_create_llvm_target_machine(sscreen);
350
351 /* Bindless handles. */
352 sctx->tex_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
353 _mesa_key_pointer_equal);
354 sctx->img_handles = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
355 _mesa_key_pointer_equal);
356
357 util_dynarray_init(&sctx->resident_tex_handles, NULL);
358 util_dynarray_init(&sctx->resident_img_handles, NULL);
359 util_dynarray_init(&sctx->resident_tex_needs_color_decompress, NULL);
360 util_dynarray_init(&sctx->resident_img_needs_color_decompress, NULL);
361 util_dynarray_init(&sctx->resident_tex_needs_depth_decompress, NULL);
362
363 return &sctx->b.b;
364 fail:
365 fprintf(stderr, "radeonsi: Failed to create a context.\n");
366 si_destroy_context(&sctx->b.b);
367 return NULL;
368 }
369
370 static struct pipe_context *si_pipe_create_context(struct pipe_screen *screen,
371 void *priv, unsigned flags)
372 {
373 struct si_screen *sscreen = (struct si_screen *)screen;
374 struct pipe_context *ctx;
375
376 if (sscreen->b.debug_flags & DBG(CHECK_VM))
377 flags |= PIPE_CONTEXT_DEBUG;
378
379 ctx = si_create_context(screen, flags);
380
381 if (!(flags & PIPE_CONTEXT_PREFER_THREADED))
382 return ctx;
383
384 /* Clover (compute-only) is unsupported. */
385 if (flags & PIPE_CONTEXT_COMPUTE_ONLY)
386 return ctx;
387
388 /* When shaders are logged to stderr, asynchronous compilation is
389 * disabled too. */
390 if (sscreen->b.debug_flags & DBG_ALL_SHADERS)
391 return ctx;
392
393 /* Use asynchronous flushes only on amdgpu, since the radeon
394 * implementation for fence_server_sync is incomplete. */
395 return threaded_context_create(ctx, &sscreen->b.pool_transfers,
396 si_replace_buffer_storage,
397 sscreen->b.info.drm_major >= 3 ? si_create_fence : NULL,
398 &((struct si_context*)ctx)->b.tc);
399 }
400
401 /*
402 * pipe_screen
403 */
404
405 static void si_destroy_screen(struct pipe_screen* pscreen)
406 {
407 struct si_screen *sscreen = (struct si_screen *)pscreen;
408 struct si_shader_part *parts[] = {
409 sscreen->vs_prologs,
410 sscreen->tcs_epilogs,
411 sscreen->gs_prologs,
412 sscreen->ps_prologs,
413 sscreen->ps_epilogs
414 };
415 unsigned i;
416
417 if (!sscreen->b.ws->unref(sscreen->b.ws))
418 return;
419
420 util_queue_destroy(&sscreen->shader_compiler_queue);
421 util_queue_destroy(&sscreen->shader_compiler_queue_low_priority);
422
423 for (i = 0; i < ARRAY_SIZE(sscreen->tm); i++)
424 if (sscreen->tm[i])
425 LLVMDisposeTargetMachine(sscreen->tm[i]);
426
427 for (i = 0; i < ARRAY_SIZE(sscreen->tm_low_priority); i++)
428 if (sscreen->tm_low_priority[i])
429 LLVMDisposeTargetMachine(sscreen->tm_low_priority[i]);
430
431 /* Free shader parts. */
432 for (i = 0; i < ARRAY_SIZE(parts); i++) {
433 while (parts[i]) {
434 struct si_shader_part *part = parts[i];
435
436 parts[i] = part->next;
437 ac_shader_binary_clean(&part->binary);
438 FREE(part);
439 }
440 }
441 mtx_destroy(&sscreen->shader_parts_mutex);
442 si_destroy_shader_cache(sscreen);
443 si_destroy_common_screen(&sscreen->b);
444 }
445
446 static bool si_init_gs_info(struct si_screen *sscreen)
447 {
448 /* gs_table_depth is not used by GFX9 */
449 if (sscreen->b.chip_class >= GFX9)
450 return true;
451
452 switch (sscreen->b.family) {
453 case CHIP_OLAND:
454 case CHIP_HAINAN:
455 case CHIP_KAVERI:
456 case CHIP_KABINI:
457 case CHIP_MULLINS:
458 case CHIP_ICELAND:
459 case CHIP_CARRIZO:
460 case CHIP_STONEY:
461 sscreen->gs_table_depth = 16;
462 return true;
463 case CHIP_TAHITI:
464 case CHIP_PITCAIRN:
465 case CHIP_VERDE:
466 case CHIP_BONAIRE:
467 case CHIP_HAWAII:
468 case CHIP_TONGA:
469 case CHIP_FIJI:
470 case CHIP_POLARIS10:
471 case CHIP_POLARIS11:
472 case CHIP_POLARIS12:
473 sscreen->gs_table_depth = 32;
474 return true;
475 default:
476 return false;
477 }
478 }
479
480 static void si_handle_env_var_force_family(struct si_screen *sscreen)
481 {
482 const char *family = debug_get_option("SI_FORCE_FAMILY", NULL);
483 unsigned i;
484
485 if (!family)
486 return;
487
488 for (i = CHIP_TAHITI; i < CHIP_LAST; i++) {
489 if (!strcmp(family, ac_get_llvm_processor_name(i))) {
490 /* Override family and chip_class. */
491 sscreen->b.family = sscreen->b.info.family = i;
492
493 if (i >= CHIP_VEGA10)
494 sscreen->b.chip_class = sscreen->b.info.chip_class = GFX9;
495 else if (i >= CHIP_TONGA)
496 sscreen->b.chip_class = sscreen->b.info.chip_class = VI;
497 else if (i >= CHIP_BONAIRE)
498 sscreen->b.chip_class = sscreen->b.info.chip_class = CIK;
499 else
500 sscreen->b.chip_class = sscreen->b.info.chip_class = SI;
501
502 /* Don't submit any IBs. */
503 setenv("RADEON_NOOP", "1", 1);
504 return;
505 }
506 }
507
508 fprintf(stderr, "radeonsi: Unknown family: %s\n", family);
509 exit(1);
510 }
511
512 static void si_test_vmfault(struct si_screen *sscreen)
513 {
514 struct pipe_context *ctx = sscreen->b.aux_context;
515 struct si_context *sctx = (struct si_context *)ctx;
516 struct pipe_resource *buf =
517 pipe_buffer_create(&sscreen->b.b, 0, PIPE_USAGE_DEFAULT, 64);
518
519 if (!buf) {
520 puts("Buffer allocation failed.");
521 exit(1);
522 }
523
524 r600_resource(buf)->gpu_address = 0; /* cause a VM fault */
525
526 if (sscreen->b.debug_flags & DBG(TEST_VMFAULT_CP)) {
527 si_copy_buffer(sctx, buf, buf, 0, 4, 4, 0);
528 ctx->flush(ctx, NULL, 0);
529 puts("VM fault test: CP - done.");
530 }
531 if (sscreen->b.debug_flags & DBG(TEST_VMFAULT_SDMA)) {
532 sctx->b.dma_clear_buffer(ctx, buf, 0, 4, 0);
533 ctx->flush(ctx, NULL, 0);
534 puts("VM fault test: SDMA - done.");
535 }
536 if (sscreen->b.debug_flags & DBG(TEST_VMFAULT_SHADER)) {
537 util_test_constant_buffer(ctx, buf);
538 puts("VM fault test: Shader - done.");
539 }
540 exit(0);
541 }
542
543 static void si_disk_cache_create(struct si_screen *sscreen)
544 {
545 /* Don't use the cache if shader dumping is enabled. */
546 if (sscreen->b.debug_flags & DBG_ALL_SHADERS)
547 return;
548
549 /* TODO: remove this once gallium supports a nir cache */
550 if (sscreen->b.debug_flags & DBG(NIR))
551 return;
552
553 uint32_t mesa_timestamp;
554 if (disk_cache_get_function_timestamp(si_disk_cache_create,
555 &mesa_timestamp)) {
556 char *timestamp_str;
557 int res = -1;
558 uint32_t llvm_timestamp;
559
560 if (disk_cache_get_function_timestamp(LLVMInitializeAMDGPUTargetInfo,
561 &llvm_timestamp)) {
562 res = asprintf(&timestamp_str, "%u_%u",
563 mesa_timestamp, llvm_timestamp);
564 }
565
566 if (res != -1) {
567 /* These flags affect shader compilation. */
568 uint64_t shader_debug_flags =
569 sscreen->b.debug_flags &
570 (DBG(FS_CORRECT_DERIVS_AFTER_KILL) |
571 DBG(SI_SCHED) |
572 DBG(UNSAFE_MATH));
573
574 sscreen->b.disk_shader_cache =
575 disk_cache_create(si_get_family_name(sscreen),
576 timestamp_str,
577 shader_debug_flags);
578 free(timestamp_str);
579 }
580 }
581 }
582
583 struct pipe_screen *radeonsi_screen_create(struct radeon_winsys *ws,
584 const struct pipe_screen_config *config)
585 {
586 struct si_screen *sscreen = CALLOC_STRUCT(si_screen);
587 unsigned num_threads, num_compiler_threads, num_compiler_threads_lowprio, i;
588
589 if (!sscreen) {
590 return NULL;
591 }
592
593 sscreen->b.ws = ws;
594 ws->query_info(ws, &sscreen->b.info);
595
596 sscreen->b.family = sscreen->b.info.family;
597 sscreen->b.chip_class = sscreen->b.info.chip_class;
598
599 /* Set functions first. */
600 sscreen->b.b.context_create = si_pipe_create_context;
601 sscreen->b.b.destroy = si_destroy_screen;
602
603 si_init_screen_get_functions(sscreen);
604 si_init_screen_buffer_functions(sscreen);
605 si_init_screen_fence_functions(sscreen);
606 si_init_screen_state_functions(sscreen);
607
608 /* Set these flags in debug_flags early, so that the shader cache takes
609 * them into account.
610 */
611 if (driQueryOptionb(config->options,
612 "glsl_correct_derivatives_after_discard"))
613 sscreen->b.debug_flags |= DBG(FS_CORRECT_DERIVS_AFTER_KILL);
614 if (driQueryOptionb(config->options, "radeonsi_enable_sisched"))
615 sscreen->b.debug_flags |= DBG(SI_SCHED);
616
617 if (!si_common_screen_init(&sscreen->b, ws) ||
618 !si_init_gs_info(sscreen) ||
619 !si_init_shader_cache(sscreen)) {
620 FREE(sscreen);
621 return NULL;
622 }
623
624 si_disk_cache_create(sscreen);
625
626 /* Only enable as many threads as we have target machines, but at most
627 * the number of CPUs - 1 if there is more than one.
628 */
629 num_threads = sysconf(_SC_NPROCESSORS_ONLN);
630 num_threads = MAX2(1, num_threads - 1);
631 num_compiler_threads = MIN2(num_threads, ARRAY_SIZE(sscreen->tm));
632 num_compiler_threads_lowprio =
633 MIN2(num_threads, ARRAY_SIZE(sscreen->tm_low_priority));
634
635 if (!util_queue_init(&sscreen->shader_compiler_queue, "si_shader",
636 32, num_compiler_threads,
637 UTIL_QUEUE_INIT_RESIZE_IF_FULL)) {
638 si_destroy_shader_cache(sscreen);
639 FREE(sscreen);
640 return NULL;
641 }
642
643 if (!util_queue_init(&sscreen->shader_compiler_queue_low_priority,
644 "si_shader_low",
645 32, num_compiler_threads_lowprio,
646 UTIL_QUEUE_INIT_RESIZE_IF_FULL |
647 UTIL_QUEUE_INIT_USE_MINIMUM_PRIORITY)) {
648 si_destroy_shader_cache(sscreen);
649 FREE(sscreen);
650 return NULL;
651 }
652
653 si_handle_env_var_force_family(sscreen);
654
655 if (!debug_get_bool_option("RADEON_DISABLE_PERFCOUNTERS", false))
656 si_init_perfcounters(sscreen);
657
658 /* Hawaii has a bug with offchip buffers > 256 that can be worked
659 * around by setting 4K granularity.
660 */
661 sscreen->tess_offchip_block_dw_size =
662 sscreen->b.family == CHIP_HAWAII ? 4096 : 8192;
663
664 /* The mere presense of CLEAR_STATE in the IB causes random GPU hangs
665 * on SI. */
666 sscreen->has_clear_state = sscreen->b.chip_class >= CIK;
667
668 sscreen->has_distributed_tess =
669 sscreen->b.chip_class >= VI &&
670 sscreen->b.info.max_se >= 2;
671
672 sscreen->has_draw_indirect_multi =
673 (sscreen->b.family >= CHIP_POLARIS10) ||
674 (sscreen->b.chip_class == VI &&
675 sscreen->b.info.pfp_fw_version >= 121 &&
676 sscreen->b.info.me_fw_version >= 87) ||
677 (sscreen->b.chip_class == CIK &&
678 sscreen->b.info.pfp_fw_version >= 211 &&
679 sscreen->b.info.me_fw_version >= 173) ||
680 (sscreen->b.chip_class == SI &&
681 sscreen->b.info.pfp_fw_version >= 79 &&
682 sscreen->b.info.me_fw_version >= 142);
683
684 sscreen->has_out_of_order_rast = sscreen->b.chip_class >= VI &&
685 sscreen->b.info.max_se >= 2 &&
686 !(sscreen->b.debug_flags & DBG(NO_OUT_OF_ORDER));
687 sscreen->assume_no_z_fights =
688 driQueryOptionb(config->options, "radeonsi_assume_no_z_fights");
689 sscreen->commutative_blend_add =
690 driQueryOptionb(config->options, "radeonsi_commutative_blend_add");
691 sscreen->clear_db_cache_before_clear =
692 driQueryOptionb(config->options, "radeonsi_clear_db_cache_before_clear");
693 sscreen->has_msaa_sample_loc_bug = (sscreen->b.family >= CHIP_POLARIS10 &&
694 sscreen->b.family <= CHIP_POLARIS12) ||
695 sscreen->b.family == CHIP_VEGA10 ||
696 sscreen->b.family == CHIP_RAVEN;
697 sscreen->has_ls_vgpr_init_bug = sscreen->b.family == CHIP_VEGA10 ||
698 sscreen->b.family == CHIP_RAVEN;
699
700 if (sscreen->b.debug_flags & DBG(DPBB)) {
701 sscreen->dpbb_allowed = true;
702 } else {
703 /* Only enable primitive binning on Raven by default. */
704 sscreen->dpbb_allowed = sscreen->b.family == CHIP_RAVEN &&
705 !(sscreen->b.debug_flags & DBG(NO_DPBB));
706 }
707
708 if (sscreen->b.debug_flags & DBG(DFSM)) {
709 sscreen->dfsm_allowed = sscreen->dpbb_allowed;
710 } else {
711 sscreen->dfsm_allowed = sscreen->dpbb_allowed &&
712 !(sscreen->b.debug_flags & DBG(NO_DFSM));
713 }
714
715 /* While it would be nice not to have this flag, we are constrained
716 * by the reality that LLVM 5.0 doesn't have working VGPR indexing
717 * on GFX9.
718 */
719 sscreen->llvm_has_working_vgpr_indexing = sscreen->b.chip_class <= VI;
720
721 /* Some chips have RB+ registers, but don't support RB+. Those must
722 * always disable it.
723 */
724 if (sscreen->b.family == CHIP_STONEY ||
725 sscreen->b.chip_class >= GFX9) {
726 sscreen->b.has_rbplus = true;
727
728 sscreen->b.rbplus_allowed =
729 !(sscreen->b.debug_flags & DBG(NO_RB_PLUS)) &&
730 (sscreen->b.family == CHIP_STONEY ||
731 sscreen->b.family == CHIP_RAVEN);
732 }
733
734 sscreen->b.dcc_msaa_allowed =
735 !(sscreen->b.debug_flags & DBG(NO_DCC_MSAA)) &&
736 (sscreen->b.debug_flags & DBG(DCC_MSAA) ||
737 sscreen->b.chip_class == VI);
738
739 (void) mtx_init(&sscreen->shader_parts_mutex, mtx_plain);
740 sscreen->use_monolithic_shaders =
741 (sscreen->b.debug_flags & DBG(MONOLITHIC_SHADERS)) != 0;
742
743 sscreen->b.barrier_flags.cp_to_L2 = SI_CONTEXT_INV_SMEM_L1 |
744 SI_CONTEXT_INV_VMEM_L1;
745 if (sscreen->b.chip_class <= VI) {
746 sscreen->b.barrier_flags.cp_to_L2 |= SI_CONTEXT_INV_GLOBAL_L2;
747 sscreen->b.barrier_flags.L2_to_cp |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
748 }
749
750 sscreen->b.barrier_flags.compute_to_L2 = SI_CONTEXT_CS_PARTIAL_FLUSH;
751
752 if (debug_get_bool_option("RADEON_DUMP_SHADERS", false))
753 sscreen->b.debug_flags |= DBG_ALL_SHADERS;
754
755 for (i = 0; i < num_compiler_threads; i++)
756 sscreen->tm[i] = si_create_llvm_target_machine(sscreen);
757 for (i = 0; i < num_compiler_threads_lowprio; i++)
758 sscreen->tm_low_priority[i] = si_create_llvm_target_machine(sscreen);
759
760 /* Create the auxiliary context. This must be done last. */
761 sscreen->b.aux_context = si_create_context(&sscreen->b.b, 0);
762
763 if (sscreen->b.debug_flags & DBG(TEST_DMA))
764 si_test_dma(sscreen);
765
766 if (sscreen->b.debug_flags & (DBG(TEST_VMFAULT_CP) |
767 DBG(TEST_VMFAULT_SDMA) |
768 DBG(TEST_VMFAULT_SHADER)))
769 si_test_vmfault(sscreen);
770
771 return &sscreen->b.b;
772 }