gallium/util: replace pipe_mutex_init() with mtx_init()
[mesa.git] / src / gallium / drivers / radeonsi / si_state_shaders.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
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 * Authors:
24 * Christian König <christian.koenig@amd.com>
25 * Marek Olšák <maraeo@gmail.com>
26 */
27
28 #include "si_pipe.h"
29 #include "sid.h"
30 #include "radeon/r600_cs.h"
31
32 #include "tgsi/tgsi_parse.h"
33 #include "tgsi/tgsi_ureg.h"
34 #include "util/hash_table.h"
35 #include "util/crc32.h"
36 #include "util/u_memory.h"
37 #include "util/u_prim.h"
38
39 #include "util/disk_cache.h"
40 #include "util/mesa-sha1.h"
41
42 /* SHADER_CACHE */
43
44 /**
45 * Return the TGSI binary in a buffer. The first 4 bytes contain its size as
46 * integer.
47 */
48 static void *si_get_tgsi_binary(struct si_shader_selector *sel)
49 {
50 unsigned tgsi_size = tgsi_num_tokens(sel->tokens) *
51 sizeof(struct tgsi_token);
52 unsigned size = 4 + tgsi_size + sizeof(sel->so);
53 char *result = (char*)MALLOC(size);
54
55 if (!result)
56 return NULL;
57
58 *((uint32_t*)result) = size;
59 memcpy(result + 4, sel->tokens, tgsi_size);
60 memcpy(result + 4 + tgsi_size, &sel->so, sizeof(sel->so));
61 return result;
62 }
63
64 /** Copy "data" to "ptr" and return the next dword following copied data. */
65 static uint32_t *write_data(uint32_t *ptr, const void *data, unsigned size)
66 {
67 /* data may be NULL if size == 0 */
68 if (size)
69 memcpy(ptr, data, size);
70 ptr += DIV_ROUND_UP(size, 4);
71 return ptr;
72 }
73
74 /** Read data from "ptr". Return the next dword following the data. */
75 static uint32_t *read_data(uint32_t *ptr, void *data, unsigned size)
76 {
77 memcpy(data, ptr, size);
78 ptr += DIV_ROUND_UP(size, 4);
79 return ptr;
80 }
81
82 /**
83 * Write the size as uint followed by the data. Return the next dword
84 * following the copied data.
85 */
86 static uint32_t *write_chunk(uint32_t *ptr, const void *data, unsigned size)
87 {
88 *ptr++ = size;
89 return write_data(ptr, data, size);
90 }
91
92 /**
93 * Read the size as uint followed by the data. Return both via parameters.
94 * Return the next dword following the data.
95 */
96 static uint32_t *read_chunk(uint32_t *ptr, void **data, unsigned *size)
97 {
98 *size = *ptr++;
99 assert(*data == NULL);
100 if (!*size)
101 return ptr;
102 *data = malloc(*size);
103 return read_data(ptr, *data, *size);
104 }
105
106 /**
107 * Return the shader binary in a buffer. The first 4 bytes contain its size
108 * as integer.
109 */
110 static void *si_get_shader_binary(struct si_shader *shader)
111 {
112 /* There is always a size of data followed by the data itself. */
113 unsigned relocs_size = shader->binary.reloc_count *
114 sizeof(shader->binary.relocs[0]);
115 unsigned disasm_size = strlen(shader->binary.disasm_string) + 1;
116 unsigned llvm_ir_size = shader->binary.llvm_ir_string ?
117 strlen(shader->binary.llvm_ir_string) + 1 : 0;
118 unsigned size =
119 4 + /* total size */
120 4 + /* CRC32 of the data below */
121 align(sizeof(shader->config), 4) +
122 align(sizeof(shader->info), 4) +
123 4 + align(shader->binary.code_size, 4) +
124 4 + align(shader->binary.rodata_size, 4) +
125 4 + align(relocs_size, 4) +
126 4 + align(disasm_size, 4) +
127 4 + align(llvm_ir_size, 4);
128 void *buffer = CALLOC(1, size);
129 uint32_t *ptr = (uint32_t*)buffer;
130
131 if (!buffer)
132 return NULL;
133
134 *ptr++ = size;
135 ptr++; /* CRC32 is calculated at the end. */
136
137 ptr = write_data(ptr, &shader->config, sizeof(shader->config));
138 ptr = write_data(ptr, &shader->info, sizeof(shader->info));
139 ptr = write_chunk(ptr, shader->binary.code, shader->binary.code_size);
140 ptr = write_chunk(ptr, shader->binary.rodata, shader->binary.rodata_size);
141 ptr = write_chunk(ptr, shader->binary.relocs, relocs_size);
142 ptr = write_chunk(ptr, shader->binary.disasm_string, disasm_size);
143 ptr = write_chunk(ptr, shader->binary.llvm_ir_string, llvm_ir_size);
144 assert((char *)ptr - (char *)buffer == size);
145
146 /* Compute CRC32. */
147 ptr = (uint32_t*)buffer;
148 ptr++;
149 *ptr = util_hash_crc32(ptr + 1, size - 8);
150
151 return buffer;
152 }
153
154 static bool si_load_shader_binary(struct si_shader *shader, void *binary)
155 {
156 uint32_t *ptr = (uint32_t*)binary;
157 uint32_t size = *ptr++;
158 uint32_t crc32 = *ptr++;
159 unsigned chunk_size;
160
161 if (util_hash_crc32(ptr, size - 8) != crc32) {
162 fprintf(stderr, "radeonsi: binary shader has invalid CRC32\n");
163 return false;
164 }
165
166 ptr = read_data(ptr, &shader->config, sizeof(shader->config));
167 ptr = read_data(ptr, &shader->info, sizeof(shader->info));
168 ptr = read_chunk(ptr, (void**)&shader->binary.code,
169 &shader->binary.code_size);
170 ptr = read_chunk(ptr, (void**)&shader->binary.rodata,
171 &shader->binary.rodata_size);
172 ptr = read_chunk(ptr, (void**)&shader->binary.relocs, &chunk_size);
173 shader->binary.reloc_count = chunk_size / sizeof(shader->binary.relocs[0]);
174 ptr = read_chunk(ptr, (void**)&shader->binary.disasm_string, &chunk_size);
175 ptr = read_chunk(ptr, (void**)&shader->binary.llvm_ir_string, &chunk_size);
176
177 return true;
178 }
179
180 /**
181 * Insert a shader into the cache. It's assumed the shader is not in the cache.
182 * Use si_shader_cache_load_shader before calling this.
183 *
184 * Returns false on failure, in which case the tgsi_binary should be freed.
185 */
186 static bool si_shader_cache_insert_shader(struct si_screen *sscreen,
187 void *tgsi_binary,
188 struct si_shader *shader,
189 bool insert_into_disk_cache)
190 {
191 void *hw_binary;
192 struct hash_entry *entry;
193 uint8_t key[CACHE_KEY_SIZE];
194
195 entry = _mesa_hash_table_search(sscreen->shader_cache, tgsi_binary);
196 if (entry)
197 return false; /* already added */
198
199 hw_binary = si_get_shader_binary(shader);
200 if (!hw_binary)
201 return false;
202
203 if (_mesa_hash_table_insert(sscreen->shader_cache, tgsi_binary,
204 hw_binary) == NULL) {
205 FREE(hw_binary);
206 return false;
207 }
208
209 if (sscreen->b.disk_shader_cache && insert_into_disk_cache) {
210 _mesa_sha1_compute(tgsi_binary, *((uint32_t *)tgsi_binary), key);
211 disk_cache_put(sscreen->b.disk_shader_cache, key, hw_binary,
212 *((uint32_t *) hw_binary));
213 }
214
215 return true;
216 }
217
218 static bool si_shader_cache_load_shader(struct si_screen *sscreen,
219 void *tgsi_binary,
220 struct si_shader *shader)
221 {
222 struct hash_entry *entry =
223 _mesa_hash_table_search(sscreen->shader_cache, tgsi_binary);
224 if (!entry) {
225 if (sscreen->b.disk_shader_cache) {
226 unsigned char sha1[CACHE_KEY_SIZE];
227 size_t tg_size = *((uint32_t *) tgsi_binary);
228
229 _mesa_sha1_compute(tgsi_binary, tg_size, sha1);
230
231 size_t binary_size;
232 uint8_t *buffer =
233 disk_cache_get(sscreen->b.disk_shader_cache,
234 sha1, &binary_size);
235 if (!buffer)
236 return false;
237
238 if (binary_size < sizeof(uint32_t) ||
239 *((uint32_t*)buffer) != binary_size) {
240 /* Something has gone wrong discard the item
241 * from the cache and rebuild/link from
242 * source.
243 */
244 assert(!"Invalid radeonsi shader disk cache "
245 "item!");
246
247 disk_cache_remove(sscreen->b.disk_shader_cache,
248 sha1);
249 free(buffer);
250
251 return false;
252 }
253
254 if (!si_load_shader_binary(shader, buffer)) {
255 free(buffer);
256 return false;
257 }
258 free(buffer);
259
260 if (!si_shader_cache_insert_shader(sscreen, tgsi_binary,
261 shader, false))
262 FREE(tgsi_binary);
263 } else {
264 return false;
265 }
266 } else {
267 if (si_load_shader_binary(shader, entry->data))
268 FREE(tgsi_binary);
269 else
270 return false;
271 }
272 p_atomic_inc(&sscreen->b.num_shader_cache_hits);
273 return true;
274 }
275
276 static uint32_t si_shader_cache_key_hash(const void *key)
277 {
278 /* The first dword is the key size. */
279 return util_hash_crc32(key, *(uint32_t*)key);
280 }
281
282 static bool si_shader_cache_key_equals(const void *a, const void *b)
283 {
284 uint32_t *keya = (uint32_t*)a;
285 uint32_t *keyb = (uint32_t*)b;
286
287 /* The first dword is the key size. */
288 if (*keya != *keyb)
289 return false;
290
291 return memcmp(keya, keyb, *keya) == 0;
292 }
293
294 static void si_destroy_shader_cache_entry(struct hash_entry *entry)
295 {
296 FREE((void*)entry->key);
297 FREE(entry->data);
298 }
299
300 bool si_init_shader_cache(struct si_screen *sscreen)
301 {
302 (void) mtx_init(&sscreen->shader_cache_mutex, mtx_plain);
303 sscreen->shader_cache =
304 _mesa_hash_table_create(NULL,
305 si_shader_cache_key_hash,
306 si_shader_cache_key_equals);
307
308 return sscreen->shader_cache != NULL;
309 }
310
311 void si_destroy_shader_cache(struct si_screen *sscreen)
312 {
313 if (sscreen->shader_cache)
314 _mesa_hash_table_destroy(sscreen->shader_cache,
315 si_destroy_shader_cache_entry);
316 pipe_mutex_destroy(sscreen->shader_cache_mutex);
317 }
318
319 /* SHADER STATES */
320
321 static void si_set_tesseval_regs(struct si_screen *sscreen,
322 struct si_shader *shader,
323 struct si_pm4_state *pm4)
324 {
325 struct tgsi_shader_info *info = &shader->selector->info;
326 unsigned tes_prim_mode = info->properties[TGSI_PROPERTY_TES_PRIM_MODE];
327 unsigned tes_spacing = info->properties[TGSI_PROPERTY_TES_SPACING];
328 bool tes_vertex_order_cw = info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW];
329 bool tes_point_mode = info->properties[TGSI_PROPERTY_TES_POINT_MODE];
330 unsigned type, partitioning, topology, distribution_mode;
331
332 switch (tes_prim_mode) {
333 case PIPE_PRIM_LINES:
334 type = V_028B6C_TESS_ISOLINE;
335 break;
336 case PIPE_PRIM_TRIANGLES:
337 type = V_028B6C_TESS_TRIANGLE;
338 break;
339 case PIPE_PRIM_QUADS:
340 type = V_028B6C_TESS_QUAD;
341 break;
342 default:
343 assert(0);
344 return;
345 }
346
347 switch (tes_spacing) {
348 case PIPE_TESS_SPACING_FRACTIONAL_ODD:
349 partitioning = V_028B6C_PART_FRAC_ODD;
350 break;
351 case PIPE_TESS_SPACING_FRACTIONAL_EVEN:
352 partitioning = V_028B6C_PART_FRAC_EVEN;
353 break;
354 case PIPE_TESS_SPACING_EQUAL:
355 partitioning = V_028B6C_PART_INTEGER;
356 break;
357 default:
358 assert(0);
359 return;
360 }
361
362 if (tes_point_mode)
363 topology = V_028B6C_OUTPUT_POINT;
364 else if (tes_prim_mode == PIPE_PRIM_LINES)
365 topology = V_028B6C_OUTPUT_LINE;
366 else if (tes_vertex_order_cw)
367 /* for some reason, this must be the other way around */
368 topology = V_028B6C_OUTPUT_TRIANGLE_CCW;
369 else
370 topology = V_028B6C_OUTPUT_TRIANGLE_CW;
371
372 if (sscreen->has_distributed_tess) {
373 if (sscreen->b.family == CHIP_FIJI ||
374 sscreen->b.family >= CHIP_POLARIS10)
375 distribution_mode = V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS;
376 else
377 distribution_mode = V_028B6C_DISTRIBUTION_MODE_DONUTS;
378 } else
379 distribution_mode = V_028B6C_DISTRIBUTION_MODE_NO_DIST;
380
381 si_pm4_set_reg(pm4, R_028B6C_VGT_TF_PARAM,
382 S_028B6C_TYPE(type) |
383 S_028B6C_PARTITIONING(partitioning) |
384 S_028B6C_TOPOLOGY(topology) |
385 S_028B6C_DISTRIBUTION_MODE(distribution_mode));
386 }
387
388 /* Polaris needs different VTX_REUSE_DEPTH settings depending on
389 * whether the "fractional odd" tessellation spacing is used.
390 *
391 * Possible VGT configurations and which state should set the register:
392 *
393 * Reg set in | VGT shader configuration | Value
394 * ------------------------------------------------------
395 * VS as VS | VS | 30
396 * VS as ES | ES -> GS -> VS | 30
397 * TES as VS | LS -> HS -> VS | 14 or 30
398 * TES as ES | LS -> HS -> ES -> GS -> VS | 14 or 30
399 */
400 static void polaris_set_vgt_vertex_reuse(struct si_screen *sscreen,
401 struct si_shader *shader,
402 struct si_pm4_state *pm4)
403 {
404 unsigned type = shader->selector->type;
405
406 if (sscreen->b.family < CHIP_POLARIS10)
407 return;
408
409 /* VS as VS, or VS as ES: */
410 if ((type == PIPE_SHADER_VERTEX &&
411 !shader->key.as_ls &&
412 !shader->is_gs_copy_shader) ||
413 /* TES as VS, or TES as ES: */
414 type == PIPE_SHADER_TESS_EVAL) {
415 unsigned vtx_reuse_depth = 30;
416
417 if (type == PIPE_SHADER_TESS_EVAL &&
418 shader->selector->info.properties[TGSI_PROPERTY_TES_SPACING] ==
419 PIPE_TESS_SPACING_FRACTIONAL_ODD)
420 vtx_reuse_depth = 14;
421
422 si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL,
423 vtx_reuse_depth);
424 }
425 }
426
427 static struct si_pm4_state *si_get_shader_pm4_state(struct si_shader *shader)
428 {
429 if (shader->pm4)
430 si_pm4_clear_state(shader->pm4);
431 else
432 shader->pm4 = CALLOC_STRUCT(si_pm4_state);
433
434 return shader->pm4;
435 }
436
437 static void si_shader_ls(struct si_shader *shader)
438 {
439 struct si_pm4_state *pm4;
440 unsigned vgpr_comp_cnt;
441 uint64_t va;
442
443 pm4 = si_get_shader_pm4_state(shader);
444 if (!pm4)
445 return;
446
447 va = shader->bo->gpu_address;
448 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
449
450 /* We need at least 2 components for LS.
451 * VGPR0-3: (VertexID, RelAutoindex, ???, InstanceID). */
452 vgpr_comp_cnt = shader->info.uses_instanceid ? 3 : 1;
453
454 si_pm4_set_reg(pm4, R_00B520_SPI_SHADER_PGM_LO_LS, va >> 8);
455 si_pm4_set_reg(pm4, R_00B524_SPI_SHADER_PGM_HI_LS, va >> 40);
456
457 shader->config.rsrc1 = S_00B528_VGPRS((shader->config.num_vgprs - 1) / 4) |
458 S_00B528_SGPRS((shader->config.num_sgprs - 1) / 8) |
459 S_00B528_VGPR_COMP_CNT(vgpr_comp_cnt) |
460 S_00B528_DX10_CLAMP(1) |
461 S_00B528_FLOAT_MODE(shader->config.float_mode);
462 shader->config.rsrc2 = S_00B52C_USER_SGPR(SI_LS_NUM_USER_SGPR) |
463 S_00B52C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0);
464 }
465
466 static void si_shader_hs(struct si_shader *shader)
467 {
468 struct si_pm4_state *pm4;
469 uint64_t va;
470
471 pm4 = si_get_shader_pm4_state(shader);
472 if (!pm4)
473 return;
474
475 va = shader->bo->gpu_address;
476 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
477
478 si_pm4_set_reg(pm4, R_00B420_SPI_SHADER_PGM_LO_HS, va >> 8);
479 si_pm4_set_reg(pm4, R_00B424_SPI_SHADER_PGM_HI_HS, va >> 40);
480 si_pm4_set_reg(pm4, R_00B428_SPI_SHADER_PGM_RSRC1_HS,
481 S_00B428_VGPRS((shader->config.num_vgprs - 1) / 4) |
482 S_00B428_SGPRS((shader->config.num_sgprs - 1) / 8) |
483 S_00B428_DX10_CLAMP(1) |
484 S_00B428_FLOAT_MODE(shader->config.float_mode));
485 si_pm4_set_reg(pm4, R_00B42C_SPI_SHADER_PGM_RSRC2_HS,
486 S_00B42C_USER_SGPR(SI_TCS_NUM_USER_SGPR) |
487 S_00B42C_OC_LDS_EN(1) |
488 S_00B42C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0));
489 }
490
491 static void si_shader_es(struct si_screen *sscreen, struct si_shader *shader)
492 {
493 struct si_pm4_state *pm4;
494 unsigned num_user_sgprs;
495 unsigned vgpr_comp_cnt;
496 uint64_t va;
497 unsigned oc_lds_en;
498
499 pm4 = si_get_shader_pm4_state(shader);
500 if (!pm4)
501 return;
502
503 va = shader->bo->gpu_address;
504 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
505
506 if (shader->selector->type == PIPE_SHADER_VERTEX) {
507 vgpr_comp_cnt = shader->info.uses_instanceid ? 3 : 0;
508 num_user_sgprs = SI_ES_NUM_USER_SGPR;
509 } else if (shader->selector->type == PIPE_SHADER_TESS_EVAL) {
510 vgpr_comp_cnt = 3; /* all components are needed for TES */
511 num_user_sgprs = SI_TES_NUM_USER_SGPR;
512 } else
513 unreachable("invalid shader selector type");
514
515 oc_lds_en = shader->selector->type == PIPE_SHADER_TESS_EVAL ? 1 : 0;
516
517 si_pm4_set_reg(pm4, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
518 shader->selector->esgs_itemsize / 4);
519 si_pm4_set_reg(pm4, R_00B320_SPI_SHADER_PGM_LO_ES, va >> 8);
520 si_pm4_set_reg(pm4, R_00B324_SPI_SHADER_PGM_HI_ES, va >> 40);
521 si_pm4_set_reg(pm4, R_00B328_SPI_SHADER_PGM_RSRC1_ES,
522 S_00B328_VGPRS((shader->config.num_vgprs - 1) / 4) |
523 S_00B328_SGPRS((shader->config.num_sgprs - 1) / 8) |
524 S_00B328_VGPR_COMP_CNT(vgpr_comp_cnt) |
525 S_00B328_DX10_CLAMP(1) |
526 S_00B328_FLOAT_MODE(shader->config.float_mode));
527 si_pm4_set_reg(pm4, R_00B32C_SPI_SHADER_PGM_RSRC2_ES,
528 S_00B32C_USER_SGPR(num_user_sgprs) |
529 S_00B32C_OC_LDS_EN(oc_lds_en) |
530 S_00B32C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0));
531
532 if (shader->selector->type == PIPE_SHADER_TESS_EVAL)
533 si_set_tesseval_regs(sscreen, shader, pm4);
534
535 polaris_set_vgt_vertex_reuse(sscreen, shader, pm4);
536 }
537
538 /**
539 * Calculate the appropriate setting of VGT_GS_MODE when \p shader is a
540 * geometry shader.
541 */
542 static uint32_t si_vgt_gs_mode(struct si_shader_selector *sel)
543 {
544 unsigned gs_max_vert_out = sel->gs_max_out_vertices;
545 unsigned cut_mode;
546
547 if (gs_max_vert_out <= 128) {
548 cut_mode = V_028A40_GS_CUT_128;
549 } else if (gs_max_vert_out <= 256) {
550 cut_mode = V_028A40_GS_CUT_256;
551 } else if (gs_max_vert_out <= 512) {
552 cut_mode = V_028A40_GS_CUT_512;
553 } else {
554 assert(gs_max_vert_out <= 1024);
555 cut_mode = V_028A40_GS_CUT_1024;
556 }
557
558 return S_028A40_MODE(V_028A40_GS_SCENARIO_G) |
559 S_028A40_CUT_MODE(cut_mode)|
560 S_028A40_ES_WRITE_OPTIMIZE(1) |
561 S_028A40_GS_WRITE_OPTIMIZE(1);
562 }
563
564 static void si_shader_gs(struct si_shader *shader)
565 {
566 struct si_shader_selector *sel = shader->selector;
567 const ubyte *num_components = sel->info.num_stream_output_components;
568 unsigned gs_num_invocations = sel->gs_num_invocations;
569 struct si_pm4_state *pm4;
570 uint64_t va;
571 unsigned max_stream = sel->max_gs_stream;
572 unsigned offset;
573
574 pm4 = si_get_shader_pm4_state(shader);
575 if (!pm4)
576 return;
577
578 si_pm4_set_reg(pm4, R_028A40_VGT_GS_MODE, si_vgt_gs_mode(shader->selector));
579
580 offset = num_components[0] * sel->gs_max_out_vertices;
581 si_pm4_set_reg(pm4, R_028A60_VGT_GSVS_RING_OFFSET_1, offset);
582 if (max_stream >= 1)
583 offset += num_components[1] * sel->gs_max_out_vertices;
584 si_pm4_set_reg(pm4, R_028A64_VGT_GSVS_RING_OFFSET_2, offset);
585 if (max_stream >= 2)
586 offset += num_components[2] * sel->gs_max_out_vertices;
587 si_pm4_set_reg(pm4, R_028A68_VGT_GSVS_RING_OFFSET_3, offset);
588 if (max_stream >= 3)
589 offset += num_components[3] * sel->gs_max_out_vertices;
590 si_pm4_set_reg(pm4, R_028AB0_VGT_GSVS_RING_ITEMSIZE, offset);
591
592 /* The GSVS_RING_ITEMSIZE register takes 15 bits */
593 assert(offset < (1 << 15));
594
595 si_pm4_set_reg(pm4, R_028B38_VGT_GS_MAX_VERT_OUT, shader->selector->gs_max_out_vertices);
596
597 si_pm4_set_reg(pm4, R_028B5C_VGT_GS_VERT_ITEMSIZE, num_components[0]);
598 si_pm4_set_reg(pm4, R_028B60_VGT_GS_VERT_ITEMSIZE_1, (max_stream >= 1) ? num_components[1] : 0);
599 si_pm4_set_reg(pm4, R_028B64_VGT_GS_VERT_ITEMSIZE_2, (max_stream >= 2) ? num_components[2] : 0);
600 si_pm4_set_reg(pm4, R_028B68_VGT_GS_VERT_ITEMSIZE_3, (max_stream >= 3) ? num_components[3] : 0);
601
602 si_pm4_set_reg(pm4, R_028B90_VGT_GS_INSTANCE_CNT,
603 S_028B90_CNT(MIN2(gs_num_invocations, 127)) |
604 S_028B90_ENABLE(gs_num_invocations > 0));
605
606 va = shader->bo->gpu_address;
607 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
608 si_pm4_set_reg(pm4, R_00B220_SPI_SHADER_PGM_LO_GS, va >> 8);
609 si_pm4_set_reg(pm4, R_00B224_SPI_SHADER_PGM_HI_GS, va >> 40);
610
611 si_pm4_set_reg(pm4, R_00B228_SPI_SHADER_PGM_RSRC1_GS,
612 S_00B228_VGPRS((shader->config.num_vgprs - 1) / 4) |
613 S_00B228_SGPRS((shader->config.num_sgprs - 1) / 8) |
614 S_00B228_DX10_CLAMP(1) |
615 S_00B228_FLOAT_MODE(shader->config.float_mode));
616 si_pm4_set_reg(pm4, R_00B22C_SPI_SHADER_PGM_RSRC2_GS,
617 S_00B22C_USER_SGPR(SI_GS_NUM_USER_SGPR) |
618 S_00B22C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0));
619 }
620
621 /**
622 * Compute the state for \p shader, which will run as a vertex shader on the
623 * hardware.
624 *
625 * If \p gs is non-NULL, it points to the geometry shader for which this shader
626 * is the copy shader.
627 */
628 static void si_shader_vs(struct si_screen *sscreen, struct si_shader *shader,
629 struct si_shader_selector *gs)
630 {
631 struct si_pm4_state *pm4;
632 unsigned num_user_sgprs;
633 unsigned nparams, vgpr_comp_cnt;
634 uint64_t va;
635 unsigned oc_lds_en;
636 unsigned window_space =
637 shader->selector->info.properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
638 bool enable_prim_id = si_vs_exports_prim_id(shader);
639
640 pm4 = si_get_shader_pm4_state(shader);
641 if (!pm4)
642 return;
643
644 /* We always write VGT_GS_MODE in the VS state, because every switch
645 * between different shader pipelines involving a different GS or no
646 * GS at all involves a switch of the VS (different GS use different
647 * copy shaders). On the other hand, when the API switches from a GS to
648 * no GS and then back to the same GS used originally, the GS state is
649 * not sent again.
650 */
651 if (!gs) {
652 si_pm4_set_reg(pm4, R_028A40_VGT_GS_MODE,
653 S_028A40_MODE(enable_prim_id ? V_028A40_GS_SCENARIO_A : 0));
654 si_pm4_set_reg(pm4, R_028A84_VGT_PRIMITIVEID_EN, enable_prim_id);
655 } else {
656 si_pm4_set_reg(pm4, R_028A40_VGT_GS_MODE, si_vgt_gs_mode(gs));
657 si_pm4_set_reg(pm4, R_028A84_VGT_PRIMITIVEID_EN, 0);
658 }
659
660 va = shader->bo->gpu_address;
661 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
662
663 if (gs) {
664 vgpr_comp_cnt = 0; /* only VertexID is needed for GS-COPY. */
665 num_user_sgprs = SI_GSCOPY_NUM_USER_SGPR;
666 } else if (shader->selector->type == PIPE_SHADER_VERTEX) {
667 vgpr_comp_cnt = shader->info.uses_instanceid ? 3 : (enable_prim_id ? 2 : 0);
668 num_user_sgprs = SI_VS_NUM_USER_SGPR;
669 } else if (shader->selector->type == PIPE_SHADER_TESS_EVAL) {
670 vgpr_comp_cnt = 3; /* all components are needed for TES */
671 num_user_sgprs = SI_TES_NUM_USER_SGPR;
672 } else
673 unreachable("invalid shader selector type");
674
675 /* VS is required to export at least one param. */
676 nparams = MAX2(shader->info.nr_param_exports, 1);
677 si_pm4_set_reg(pm4, R_0286C4_SPI_VS_OUT_CONFIG,
678 S_0286C4_VS_EXPORT_COUNT(nparams - 1));
679
680 si_pm4_set_reg(pm4, R_02870C_SPI_SHADER_POS_FORMAT,
681 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
682 S_02870C_POS1_EXPORT_FORMAT(shader->info.nr_pos_exports > 1 ?
683 V_02870C_SPI_SHADER_4COMP :
684 V_02870C_SPI_SHADER_NONE) |
685 S_02870C_POS2_EXPORT_FORMAT(shader->info.nr_pos_exports > 2 ?
686 V_02870C_SPI_SHADER_4COMP :
687 V_02870C_SPI_SHADER_NONE) |
688 S_02870C_POS3_EXPORT_FORMAT(shader->info.nr_pos_exports > 3 ?
689 V_02870C_SPI_SHADER_4COMP :
690 V_02870C_SPI_SHADER_NONE));
691
692 oc_lds_en = shader->selector->type == PIPE_SHADER_TESS_EVAL ? 1 : 0;
693
694 si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8);
695 si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, va >> 40);
696 si_pm4_set_reg(pm4, R_00B128_SPI_SHADER_PGM_RSRC1_VS,
697 S_00B128_VGPRS((shader->config.num_vgprs - 1) / 4) |
698 S_00B128_SGPRS((shader->config.num_sgprs - 1) / 8) |
699 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt) |
700 S_00B128_DX10_CLAMP(1) |
701 S_00B128_FLOAT_MODE(shader->config.float_mode));
702 si_pm4_set_reg(pm4, R_00B12C_SPI_SHADER_PGM_RSRC2_VS,
703 S_00B12C_USER_SGPR(num_user_sgprs) |
704 S_00B12C_OC_LDS_EN(oc_lds_en) |
705 S_00B12C_SO_BASE0_EN(!!shader->selector->so.stride[0]) |
706 S_00B12C_SO_BASE1_EN(!!shader->selector->so.stride[1]) |
707 S_00B12C_SO_BASE2_EN(!!shader->selector->so.stride[2]) |
708 S_00B12C_SO_BASE3_EN(!!shader->selector->so.stride[3]) |
709 S_00B12C_SO_EN(!!shader->selector->so.num_outputs) |
710 S_00B12C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0));
711 if (window_space)
712 si_pm4_set_reg(pm4, R_028818_PA_CL_VTE_CNTL,
713 S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1));
714 else
715 si_pm4_set_reg(pm4, R_028818_PA_CL_VTE_CNTL,
716 S_028818_VTX_W0_FMT(1) |
717 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
718 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
719 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
720
721 if (shader->selector->type == PIPE_SHADER_TESS_EVAL)
722 si_set_tesseval_regs(sscreen, shader, pm4);
723
724 polaris_set_vgt_vertex_reuse(sscreen, shader, pm4);
725 }
726
727 static unsigned si_get_ps_num_interp(struct si_shader *ps)
728 {
729 struct tgsi_shader_info *info = &ps->selector->info;
730 unsigned num_colors = !!(info->colors_read & 0x0f) +
731 !!(info->colors_read & 0xf0);
732 unsigned num_interp = ps->selector->info.num_inputs +
733 (ps->key.part.ps.prolog.color_two_side ? num_colors : 0);
734
735 assert(num_interp <= 32);
736 return MIN2(num_interp, 32);
737 }
738
739 static unsigned si_get_spi_shader_col_format(struct si_shader *shader)
740 {
741 unsigned value = shader->key.part.ps.epilog.spi_shader_col_format;
742 unsigned i, num_targets = (util_last_bit(value) + 3) / 4;
743
744 /* If the i-th target format is set, all previous target formats must
745 * be non-zero to avoid hangs.
746 */
747 for (i = 0; i < num_targets; i++)
748 if (!(value & (0xf << (i * 4))))
749 value |= V_028714_SPI_SHADER_32_R << (i * 4);
750
751 return value;
752 }
753
754 static unsigned si_get_cb_shader_mask(unsigned spi_shader_col_format)
755 {
756 unsigned i, cb_shader_mask = 0;
757
758 for (i = 0; i < 8; i++) {
759 switch ((spi_shader_col_format >> (i * 4)) & 0xf) {
760 case V_028714_SPI_SHADER_ZERO:
761 break;
762 case V_028714_SPI_SHADER_32_R:
763 cb_shader_mask |= 0x1 << (i * 4);
764 break;
765 case V_028714_SPI_SHADER_32_GR:
766 cb_shader_mask |= 0x3 << (i * 4);
767 break;
768 case V_028714_SPI_SHADER_32_AR:
769 cb_shader_mask |= 0x9 << (i * 4);
770 break;
771 case V_028714_SPI_SHADER_FP16_ABGR:
772 case V_028714_SPI_SHADER_UNORM16_ABGR:
773 case V_028714_SPI_SHADER_SNORM16_ABGR:
774 case V_028714_SPI_SHADER_UINT16_ABGR:
775 case V_028714_SPI_SHADER_SINT16_ABGR:
776 case V_028714_SPI_SHADER_32_ABGR:
777 cb_shader_mask |= 0xf << (i * 4);
778 break;
779 default:
780 assert(0);
781 }
782 }
783 return cb_shader_mask;
784 }
785
786 static void si_shader_ps(struct si_shader *shader)
787 {
788 struct tgsi_shader_info *info = &shader->selector->info;
789 struct si_pm4_state *pm4;
790 unsigned spi_ps_in_control, spi_shader_col_format, cb_shader_mask;
791 unsigned spi_baryc_cntl = S_0286E0_FRONT_FACE_ALL_BITS(1);
792 uint64_t va;
793 unsigned input_ena = shader->config.spi_ps_input_ena;
794
795 /* we need to enable at least one of them, otherwise we hang the GPU */
796 assert(G_0286CC_PERSP_SAMPLE_ENA(input_ena) ||
797 G_0286CC_PERSP_CENTER_ENA(input_ena) ||
798 G_0286CC_PERSP_CENTROID_ENA(input_ena) ||
799 G_0286CC_PERSP_PULL_MODEL_ENA(input_ena) ||
800 G_0286CC_LINEAR_SAMPLE_ENA(input_ena) ||
801 G_0286CC_LINEAR_CENTER_ENA(input_ena) ||
802 G_0286CC_LINEAR_CENTROID_ENA(input_ena) ||
803 G_0286CC_LINE_STIPPLE_TEX_ENA(input_ena));
804 /* POS_W_FLOAT_ENA requires one of the perspective weights. */
805 assert(!G_0286CC_POS_W_FLOAT_ENA(input_ena) ||
806 G_0286CC_PERSP_SAMPLE_ENA(input_ena) ||
807 G_0286CC_PERSP_CENTER_ENA(input_ena) ||
808 G_0286CC_PERSP_CENTROID_ENA(input_ena) ||
809 G_0286CC_PERSP_PULL_MODEL_ENA(input_ena));
810
811 /* Validate interpolation optimization flags (read as implications). */
812 assert(!shader->key.part.ps.prolog.bc_optimize_for_persp ||
813 (G_0286CC_PERSP_CENTER_ENA(input_ena) &&
814 G_0286CC_PERSP_CENTROID_ENA(input_ena)));
815 assert(!shader->key.part.ps.prolog.bc_optimize_for_linear ||
816 (G_0286CC_LINEAR_CENTER_ENA(input_ena) &&
817 G_0286CC_LINEAR_CENTROID_ENA(input_ena)));
818 assert(!shader->key.part.ps.prolog.force_persp_center_interp ||
819 (!G_0286CC_PERSP_SAMPLE_ENA(input_ena) &&
820 !G_0286CC_PERSP_CENTROID_ENA(input_ena)));
821 assert(!shader->key.part.ps.prolog.force_linear_center_interp ||
822 (!G_0286CC_LINEAR_SAMPLE_ENA(input_ena) &&
823 !G_0286CC_LINEAR_CENTROID_ENA(input_ena)));
824 assert(!shader->key.part.ps.prolog.force_persp_sample_interp ||
825 (!G_0286CC_PERSP_CENTER_ENA(input_ena) &&
826 !G_0286CC_PERSP_CENTROID_ENA(input_ena)));
827 assert(!shader->key.part.ps.prolog.force_linear_sample_interp ||
828 (!G_0286CC_LINEAR_CENTER_ENA(input_ena) &&
829 !G_0286CC_LINEAR_CENTROID_ENA(input_ena)));
830
831 /* Validate cases when the optimizations are off (read as implications). */
832 assert(shader->key.part.ps.prolog.bc_optimize_for_persp ||
833 !G_0286CC_PERSP_CENTER_ENA(input_ena) ||
834 !G_0286CC_PERSP_CENTROID_ENA(input_ena));
835 assert(shader->key.part.ps.prolog.bc_optimize_for_linear ||
836 !G_0286CC_LINEAR_CENTER_ENA(input_ena) ||
837 !G_0286CC_LINEAR_CENTROID_ENA(input_ena));
838
839 pm4 = si_get_shader_pm4_state(shader);
840 if (!pm4)
841 return;
842
843 /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION
844 * Possible vaules:
845 * 0 -> Position = pixel center
846 * 1 -> Position = pixel centroid
847 * 2 -> Position = at sample position
848 *
849 * From GLSL 4.5 specification, section 7.1:
850 * "The variable gl_FragCoord is available as an input variable from
851 * within fragment shaders and it holds the window relative coordinates
852 * (x, y, z, 1/w) values for the fragment. If multi-sampling, this
853 * value can be for any location within the pixel, or one of the
854 * fragment samples. The use of centroid does not further restrict
855 * this value to be inside the current primitive."
856 *
857 * Meaning that centroid has no effect and we can return anything within
858 * the pixel. Thus, return the value at sample position, because that's
859 * the most accurate one shaders can get.
860 */
861 spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(2);
862
863 if (info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] ==
864 TGSI_FS_COORD_PIXEL_CENTER_INTEGER)
865 spi_baryc_cntl |= S_0286E0_POS_FLOAT_ULC(1);
866
867 spi_shader_col_format = si_get_spi_shader_col_format(shader);
868 cb_shader_mask = si_get_cb_shader_mask(spi_shader_col_format);
869
870 /* Ensure that some export memory is always allocated, for two reasons:
871 *
872 * 1) Correctness: The hardware ignores the EXEC mask if no export
873 * memory is allocated, so KILL and alpha test do not work correctly
874 * without this.
875 * 2) Performance: Every shader needs at least a NULL export, even when
876 * it writes no color/depth output. The NULL export instruction
877 * stalls without this setting.
878 *
879 * Don't add this to CB_SHADER_MASK.
880 */
881 if (!spi_shader_col_format &&
882 !info->writes_z && !info->writes_stencil && !info->writes_samplemask)
883 spi_shader_col_format = V_028714_SPI_SHADER_32_R;
884
885 si_pm4_set_reg(pm4, R_0286CC_SPI_PS_INPUT_ENA, input_ena);
886 si_pm4_set_reg(pm4, R_0286D0_SPI_PS_INPUT_ADDR,
887 shader->config.spi_ps_input_addr);
888
889 /* Set interpolation controls. */
890 spi_ps_in_control = S_0286D8_NUM_INTERP(si_get_ps_num_interp(shader));
891
892 /* Set registers. */
893 si_pm4_set_reg(pm4, R_0286E0_SPI_BARYC_CNTL, spi_baryc_cntl);
894 si_pm4_set_reg(pm4, R_0286D8_SPI_PS_IN_CONTROL, spi_ps_in_control);
895
896 si_pm4_set_reg(pm4, R_028710_SPI_SHADER_Z_FORMAT,
897 si_get_spi_shader_z_format(info->writes_z,
898 info->writes_stencil,
899 info->writes_samplemask));
900
901 si_pm4_set_reg(pm4, R_028714_SPI_SHADER_COL_FORMAT, spi_shader_col_format);
902 si_pm4_set_reg(pm4, R_02823C_CB_SHADER_MASK, cb_shader_mask);
903
904 va = shader->bo->gpu_address;
905 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_BINARY);
906 si_pm4_set_reg(pm4, R_00B020_SPI_SHADER_PGM_LO_PS, va >> 8);
907 si_pm4_set_reg(pm4, R_00B024_SPI_SHADER_PGM_HI_PS, va >> 40);
908
909 si_pm4_set_reg(pm4, R_00B028_SPI_SHADER_PGM_RSRC1_PS,
910 S_00B028_VGPRS((shader->config.num_vgprs - 1) / 4) |
911 S_00B028_SGPRS((shader->config.num_sgprs - 1) / 8) |
912 S_00B028_DX10_CLAMP(1) |
913 S_00B028_FLOAT_MODE(shader->config.float_mode));
914 si_pm4_set_reg(pm4, R_00B02C_SPI_SHADER_PGM_RSRC2_PS,
915 S_00B02C_EXTRA_LDS_SIZE(shader->config.lds_size) |
916 S_00B02C_USER_SGPR(SI_PS_NUM_USER_SGPR) |
917 S_00B32C_SCRATCH_EN(shader->config.scratch_bytes_per_wave > 0));
918 }
919
920 static void si_shader_init_pm4_state(struct si_screen *sscreen,
921 struct si_shader *shader)
922 {
923 switch (shader->selector->type) {
924 case PIPE_SHADER_VERTEX:
925 if (shader->key.as_ls)
926 si_shader_ls(shader);
927 else if (shader->key.as_es)
928 si_shader_es(sscreen, shader);
929 else
930 si_shader_vs(sscreen, shader, NULL);
931 break;
932 case PIPE_SHADER_TESS_CTRL:
933 si_shader_hs(shader);
934 break;
935 case PIPE_SHADER_TESS_EVAL:
936 if (shader->key.as_es)
937 si_shader_es(sscreen, shader);
938 else
939 si_shader_vs(sscreen, shader, NULL);
940 break;
941 case PIPE_SHADER_GEOMETRY:
942 si_shader_gs(shader);
943 break;
944 case PIPE_SHADER_FRAGMENT:
945 si_shader_ps(shader);
946 break;
947 default:
948 assert(0);
949 }
950 }
951
952 static unsigned si_get_alpha_test_func(struct si_context *sctx)
953 {
954 /* Alpha-test should be disabled if colorbuffer 0 is integer. */
955 if (sctx->queued.named.dsa)
956 return sctx->queued.named.dsa->alpha_func;
957
958 return PIPE_FUNC_ALWAYS;
959 }
960
961 static void si_shader_selector_key_hw_vs(struct si_context *sctx,
962 struct si_shader_selector *vs,
963 struct si_shader_key *key)
964 {
965 struct si_shader_selector *ps = sctx->ps_shader.cso;
966
967 key->opt.hw_vs.clip_disable =
968 sctx->queued.named.rasterizer->clip_plane_enable == 0 &&
969 (vs->info.clipdist_writemask ||
970 vs->info.writes_clipvertex) &&
971 !vs->info.culldist_writemask;
972
973 /* Find out if PS is disabled. */
974 bool ps_disabled = true;
975 if (ps) {
976 bool ps_modifies_zs = ps->info.uses_kill ||
977 ps->info.writes_z ||
978 ps->info.writes_stencil ||
979 ps->info.writes_samplemask ||
980 si_get_alpha_test_func(sctx) != PIPE_FUNC_ALWAYS;
981
982 unsigned ps_colormask = sctx->framebuffer.colorbuf_enabled_4bit &
983 sctx->queued.named.blend->cb_target_mask;
984 if (!ps->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS])
985 ps_colormask &= ps->colors_written_4bit;
986
987 ps_disabled = sctx->queued.named.rasterizer->rasterizer_discard ||
988 (!ps_colormask &&
989 !ps_modifies_zs &&
990 !ps->info.writes_memory);
991 }
992
993 /* Find out which VS outputs aren't used by the PS. */
994 uint64_t outputs_written = vs->outputs_written;
995 uint32_t outputs_written2 = vs->outputs_written2;
996 uint64_t inputs_read = 0;
997 uint32_t inputs_read2 = 0;
998
999 outputs_written &= ~0x3; /* ignore POSITION, PSIZE */
1000
1001 if (!ps_disabled) {
1002 inputs_read = ps->inputs_read;
1003 inputs_read2 = ps->inputs_read2;
1004 }
1005
1006 uint64_t linked = outputs_written & inputs_read;
1007 uint32_t linked2 = outputs_written2 & inputs_read2;
1008
1009 key->opt.hw_vs.kill_outputs = ~linked & outputs_written;
1010 key->opt.hw_vs.kill_outputs2 = ~linked2 & outputs_written2;
1011 }
1012
1013 /* Compute the key for the hw shader variant */
1014 static inline void si_shader_selector_key(struct pipe_context *ctx,
1015 struct si_shader_selector *sel,
1016 struct si_shader_key *key)
1017 {
1018 struct si_context *sctx = (struct si_context *)ctx;
1019 unsigned i;
1020
1021 memset(key, 0, sizeof(*key));
1022
1023 switch (sel->type) {
1024 case PIPE_SHADER_VERTEX:
1025 if (sctx->vertex_elements) {
1026 unsigned count = MIN2(sel->info.num_inputs,
1027 sctx->vertex_elements->count);
1028 for (i = 0; i < count; ++i)
1029 key->part.vs.prolog.instance_divisors[i] =
1030 sctx->vertex_elements->elements[i].instance_divisor;
1031
1032 memcpy(key->mono.vs.fix_fetch,
1033 sctx->vertex_elements->fix_fetch, count);
1034 }
1035 if (sctx->tes_shader.cso)
1036 key->as_ls = 1;
1037 else if (sctx->gs_shader.cso)
1038 key->as_es = 1;
1039 else {
1040 si_shader_selector_key_hw_vs(sctx, sel, key);
1041
1042 if (sctx->ps_shader.cso && sctx->ps_shader.cso->info.uses_primid)
1043 key->part.vs.epilog.export_prim_id = 1;
1044 }
1045 break;
1046 case PIPE_SHADER_TESS_CTRL:
1047 key->part.tcs.epilog.prim_mode =
1048 sctx->tes_shader.cso->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
1049 key->part.tcs.epilog.tes_reads_tess_factors =
1050 sctx->tes_shader.cso->info.reads_tess_factors;
1051
1052 if (sel == sctx->fixed_func_tcs_shader.cso)
1053 key->mono.tcs.inputs_to_copy = sctx->vs_shader.cso->outputs_written;
1054 break;
1055 case PIPE_SHADER_TESS_EVAL:
1056 if (sctx->gs_shader.cso)
1057 key->as_es = 1;
1058 else {
1059 si_shader_selector_key_hw_vs(sctx, sel, key);
1060
1061 if (sctx->ps_shader.cso && sctx->ps_shader.cso->info.uses_primid)
1062 key->part.tes.epilog.export_prim_id = 1;
1063 }
1064 break;
1065 case PIPE_SHADER_GEOMETRY:
1066 key->part.gs.prolog.tri_strip_adj_fix = sctx->gs_tri_strip_adj_fix;
1067 break;
1068 case PIPE_SHADER_FRAGMENT: {
1069 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1070 struct si_state_blend *blend = sctx->queued.named.blend;
1071
1072 if (sel->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS] &&
1073 sel->info.colors_written == 0x1)
1074 key->part.ps.epilog.last_cbuf = MAX2(sctx->framebuffer.state.nr_cbufs, 1) - 1;
1075
1076 if (blend) {
1077 /* Select the shader color format based on whether
1078 * blending or alpha are needed.
1079 */
1080 key->part.ps.epilog.spi_shader_col_format =
1081 (blend->blend_enable_4bit & blend->need_src_alpha_4bit &
1082 sctx->framebuffer.spi_shader_col_format_blend_alpha) |
1083 (blend->blend_enable_4bit & ~blend->need_src_alpha_4bit &
1084 sctx->framebuffer.spi_shader_col_format_blend) |
1085 (~blend->blend_enable_4bit & blend->need_src_alpha_4bit &
1086 sctx->framebuffer.spi_shader_col_format_alpha) |
1087 (~blend->blend_enable_4bit & ~blend->need_src_alpha_4bit &
1088 sctx->framebuffer.spi_shader_col_format);
1089
1090 /* The output for dual source blending should have
1091 * the same format as the first output.
1092 */
1093 if (blend->dual_src_blend)
1094 key->part.ps.epilog.spi_shader_col_format |=
1095 (key->part.ps.epilog.spi_shader_col_format & 0xf) << 4;
1096 } else
1097 key->part.ps.epilog.spi_shader_col_format = sctx->framebuffer.spi_shader_col_format;
1098
1099 /* If alpha-to-coverage is enabled, we have to export alpha
1100 * even if there is no color buffer.
1101 */
1102 if (!(key->part.ps.epilog.spi_shader_col_format & 0xf) &&
1103 blend && blend->alpha_to_coverage)
1104 key->part.ps.epilog.spi_shader_col_format |= V_028710_SPI_SHADER_32_AR;
1105
1106 /* On SI and CIK except Hawaii, the CB doesn't clamp outputs
1107 * to the range supported by the type if a channel has less
1108 * than 16 bits and the export format is 16_ABGR.
1109 */
1110 if (sctx->b.chip_class <= CIK && sctx->b.family != CHIP_HAWAII) {
1111 key->part.ps.epilog.color_is_int8 = sctx->framebuffer.color_is_int8;
1112 key->part.ps.epilog.color_is_int10 = sctx->framebuffer.color_is_int10;
1113 }
1114
1115 /* Disable unwritten outputs (if WRITE_ALL_CBUFS isn't enabled). */
1116 if (!key->part.ps.epilog.last_cbuf) {
1117 key->part.ps.epilog.spi_shader_col_format &= sel->colors_written_4bit;
1118 key->part.ps.epilog.color_is_int8 &= sel->info.colors_written;
1119 key->part.ps.epilog.color_is_int10 &= sel->info.colors_written;
1120 }
1121
1122 if (rs) {
1123 bool is_poly = (sctx->current_rast_prim >= PIPE_PRIM_TRIANGLES &&
1124 sctx->current_rast_prim <= PIPE_PRIM_POLYGON) ||
1125 sctx->current_rast_prim >= PIPE_PRIM_TRIANGLES_ADJACENCY;
1126 bool is_line = !is_poly && sctx->current_rast_prim != PIPE_PRIM_POINTS;
1127
1128 key->part.ps.prolog.color_two_side = rs->two_side && sel->info.colors_read;
1129 key->part.ps.prolog.flatshade_colors = rs->flatshade && sel->info.colors_read;
1130
1131 if (sctx->queued.named.blend) {
1132 key->part.ps.epilog.alpha_to_one = sctx->queued.named.blend->alpha_to_one &&
1133 rs->multisample_enable;
1134 }
1135
1136 key->part.ps.prolog.poly_stipple = rs->poly_stipple_enable && is_poly;
1137 key->part.ps.epilog.poly_line_smoothing = ((is_poly && rs->poly_smooth) ||
1138 (is_line && rs->line_smooth)) &&
1139 sctx->framebuffer.nr_samples <= 1;
1140 key->part.ps.epilog.clamp_color = rs->clamp_fragment_color;
1141
1142 if (rs->force_persample_interp &&
1143 rs->multisample_enable &&
1144 sctx->framebuffer.nr_samples > 1 &&
1145 sctx->ps_iter_samples > 1) {
1146 key->part.ps.prolog.force_persp_sample_interp =
1147 sel->info.uses_persp_center ||
1148 sel->info.uses_persp_centroid;
1149
1150 key->part.ps.prolog.force_linear_sample_interp =
1151 sel->info.uses_linear_center ||
1152 sel->info.uses_linear_centroid;
1153 } else if (rs->multisample_enable &&
1154 sctx->framebuffer.nr_samples > 1) {
1155 key->part.ps.prolog.bc_optimize_for_persp =
1156 sel->info.uses_persp_center &&
1157 sel->info.uses_persp_centroid;
1158 key->part.ps.prolog.bc_optimize_for_linear =
1159 sel->info.uses_linear_center &&
1160 sel->info.uses_linear_centroid;
1161 } else {
1162 /* Make sure SPI doesn't compute more than 1 pair
1163 * of (i,j), which is the optimization here. */
1164 key->part.ps.prolog.force_persp_center_interp =
1165 sel->info.uses_persp_center +
1166 sel->info.uses_persp_centroid +
1167 sel->info.uses_persp_sample > 1;
1168
1169 key->part.ps.prolog.force_linear_center_interp =
1170 sel->info.uses_linear_center +
1171 sel->info.uses_linear_centroid +
1172 sel->info.uses_linear_sample > 1;
1173 }
1174 }
1175
1176 key->part.ps.epilog.alpha_func = si_get_alpha_test_func(sctx);
1177 break;
1178 }
1179 default:
1180 assert(0);
1181 }
1182 }
1183
1184 static void si_build_shader_variant(void *job, int thread_index)
1185 {
1186 struct si_shader *shader = (struct si_shader *)job;
1187 struct si_shader_selector *sel = shader->selector;
1188 struct si_screen *sscreen = sel->screen;
1189 LLVMTargetMachineRef tm;
1190 struct pipe_debug_callback *debug = &shader->compiler_ctx_state.debug;
1191 int r;
1192
1193 if (thread_index >= 0) {
1194 assert(thread_index < ARRAY_SIZE(sscreen->tm));
1195 tm = sscreen->tm[thread_index];
1196 if (!debug->async)
1197 debug = NULL;
1198 } else {
1199 tm = shader->compiler_ctx_state.tm;
1200 }
1201
1202 r = si_shader_create(sscreen, tm, shader, debug);
1203 if (unlikely(r)) {
1204 R600_ERR("Failed to build shader variant (type=%u) %d\n",
1205 sel->type, r);
1206 shader->compilation_failed = true;
1207 return;
1208 }
1209
1210 if (shader->compiler_ctx_state.is_debug_context) {
1211 FILE *f = open_memstream(&shader->shader_log,
1212 &shader->shader_log_size);
1213 if (f) {
1214 si_shader_dump(sscreen, shader, NULL, sel->type, f, false);
1215 fclose(f);
1216 }
1217 }
1218
1219 si_shader_init_pm4_state(sscreen, shader);
1220 }
1221
1222 /* Select the hw shader variant depending on the current state. */
1223 static int si_shader_select_with_key(struct si_screen *sscreen,
1224 struct si_shader_ctx_state *state,
1225 struct si_compiler_ctx_state *compiler_state,
1226 struct si_shader_key *key,
1227 int thread_index)
1228 {
1229 static const struct si_shader_key zeroed;
1230 struct si_shader_selector *sel = state->cso;
1231 struct si_shader *current = state->current;
1232 struct si_shader *iter, *shader = NULL;
1233
1234 if (unlikely(sscreen->b.debug_flags & DBG_NO_OPT_VARIANT)) {
1235 memset(&key->opt, 0, sizeof(key->opt));
1236 }
1237
1238 again:
1239 /* Check if we don't need to change anything.
1240 * This path is also used for most shaders that don't need multiple
1241 * variants, it will cost just a computation of the key and this
1242 * test. */
1243 if (likely(current &&
1244 memcmp(&current->key, key, sizeof(*key)) == 0 &&
1245 (!current->is_optimized ||
1246 util_queue_fence_is_signalled(&current->optimized_ready))))
1247 return 0;
1248
1249 /* This must be done before the mutex is locked, because async GS
1250 * compilation calls this function too, and therefore must enter
1251 * the mutex first.
1252 *
1253 * Only wait if we are in a draw call. Don't wait if we are
1254 * in a compiler thread.
1255 */
1256 if (thread_index < 0)
1257 util_queue_fence_wait(&sel->ready);
1258
1259 pipe_mutex_lock(sel->mutex);
1260
1261 /* Find the shader variant. */
1262 for (iter = sel->first_variant; iter; iter = iter->next_variant) {
1263 /* Don't check the "current" shader. We checked it above. */
1264 if (current != iter &&
1265 memcmp(&iter->key, key, sizeof(*key)) == 0) {
1266 /* If it's an optimized shader and its compilation has
1267 * been started but isn't done, use the unoptimized
1268 * shader so as not to cause a stall due to compilation.
1269 */
1270 if (iter->is_optimized &&
1271 !util_queue_fence_is_signalled(&iter->optimized_ready)) {
1272 memset(&key->opt, 0, sizeof(key->opt));
1273 pipe_mutex_unlock(sel->mutex);
1274 goto again;
1275 }
1276
1277 if (iter->compilation_failed) {
1278 pipe_mutex_unlock(sel->mutex);
1279 return -1; /* skip the draw call */
1280 }
1281
1282 state->current = iter;
1283 pipe_mutex_unlock(sel->mutex);
1284 return 0;
1285 }
1286 }
1287
1288 /* Build a new shader. */
1289 shader = CALLOC_STRUCT(si_shader);
1290 if (!shader) {
1291 pipe_mutex_unlock(sel->mutex);
1292 return -ENOMEM;
1293 }
1294 shader->selector = sel;
1295 shader->key = *key;
1296 shader->compiler_ctx_state = *compiler_state;
1297
1298 /* Compile the main shader part if it doesn't exist. This can happen
1299 * if the initial guess was wrong. */
1300 struct si_shader **mainp = si_get_main_shader_part(sel, key);
1301 bool is_pure_monolithic =
1302 sscreen->use_monolithic_shaders ||
1303 memcmp(&key->mono, &zeroed.mono, sizeof(key->mono)) != 0;
1304
1305 if (!*mainp && !is_pure_monolithic) {
1306 struct si_shader *main_part = CALLOC_STRUCT(si_shader);
1307
1308 if (!main_part) {
1309 FREE(shader);
1310 pipe_mutex_unlock(sel->mutex);
1311 return -ENOMEM; /* skip the draw call */
1312 }
1313
1314 main_part->selector = sel;
1315 main_part->key.as_es = key->as_es;
1316 main_part->key.as_ls = key->as_ls;
1317
1318 if (si_compile_tgsi_shader(sscreen, compiler_state->tm,
1319 main_part, false,
1320 &compiler_state->debug) != 0) {
1321 FREE(main_part);
1322 FREE(shader);
1323 pipe_mutex_unlock(sel->mutex);
1324 return -ENOMEM; /* skip the draw call */
1325 }
1326 *mainp = main_part;
1327 }
1328
1329 /* Monolithic-only shaders don't make a distinction between optimized
1330 * and unoptimized. */
1331 shader->is_monolithic =
1332 is_pure_monolithic ||
1333 memcmp(&key->opt, &zeroed.opt, sizeof(key->opt)) != 0;
1334
1335 shader->is_optimized =
1336 !is_pure_monolithic &&
1337 memcmp(&key->opt, &zeroed.opt, sizeof(key->opt)) != 0;
1338 if (shader->is_optimized)
1339 util_queue_fence_init(&shader->optimized_ready);
1340
1341 if (!sel->last_variant) {
1342 sel->first_variant = shader;
1343 sel->last_variant = shader;
1344 } else {
1345 sel->last_variant->next_variant = shader;
1346 sel->last_variant = shader;
1347 }
1348
1349 /* If it's an optimized shader, compile it asynchronously. */
1350 if (shader->is_optimized &&
1351 !is_pure_monolithic &&
1352 thread_index < 0) {
1353 /* Compile it asynchronously. */
1354 util_queue_add_job(&sscreen->shader_compiler_queue,
1355 shader, &shader->optimized_ready,
1356 si_build_shader_variant, NULL);
1357
1358 /* Use the default (unoptimized) shader for now. */
1359 memset(&key->opt, 0, sizeof(key->opt));
1360 pipe_mutex_unlock(sel->mutex);
1361 goto again;
1362 }
1363
1364 assert(!shader->is_optimized);
1365 si_build_shader_variant(shader, thread_index);
1366
1367 if (!shader->compilation_failed)
1368 state->current = shader;
1369
1370 pipe_mutex_unlock(sel->mutex);
1371 return shader->compilation_failed ? -1 : 0;
1372 }
1373
1374 static int si_shader_select(struct pipe_context *ctx,
1375 struct si_shader_ctx_state *state,
1376 struct si_compiler_ctx_state *compiler_state)
1377 {
1378 struct si_context *sctx = (struct si_context *)ctx;
1379 struct si_shader_key key;
1380
1381 si_shader_selector_key(ctx, state->cso, &key);
1382 return si_shader_select_with_key(sctx->screen, state, compiler_state,
1383 &key, -1);
1384 }
1385
1386 static void si_parse_next_shader_property(const struct tgsi_shader_info *info,
1387 struct si_shader_key *key)
1388 {
1389 unsigned next_shader = info->properties[TGSI_PROPERTY_NEXT_SHADER];
1390
1391 switch (info->processor) {
1392 case PIPE_SHADER_VERTEX:
1393 switch (next_shader) {
1394 case PIPE_SHADER_GEOMETRY:
1395 key->as_es = 1;
1396 break;
1397 case PIPE_SHADER_TESS_CTRL:
1398 case PIPE_SHADER_TESS_EVAL:
1399 key->as_ls = 1;
1400 break;
1401 default:
1402 /* If POSITION isn't written, it can't be a HW VS.
1403 * Assume that it's a HW LS. (the next shader is TCS)
1404 * This heuristic is needed for separate shader objects.
1405 */
1406 if (!info->writes_position)
1407 key->as_ls = 1;
1408 }
1409 break;
1410
1411 case PIPE_SHADER_TESS_EVAL:
1412 if (next_shader == PIPE_SHADER_GEOMETRY ||
1413 !info->writes_position)
1414 key->as_es = 1;
1415 break;
1416 }
1417 }
1418
1419 /**
1420 * Compile the main shader part or the monolithic shader as part of
1421 * si_shader_selector initialization. Since it can be done asynchronously,
1422 * there is no way to report compile failures to applications.
1423 */
1424 void si_init_shader_selector_async(void *job, int thread_index)
1425 {
1426 struct si_shader_selector *sel = (struct si_shader_selector *)job;
1427 struct si_screen *sscreen = sel->screen;
1428 LLVMTargetMachineRef tm;
1429 struct pipe_debug_callback *debug = &sel->compiler_ctx_state.debug;
1430 unsigned i;
1431
1432 if (thread_index >= 0) {
1433 assert(thread_index < ARRAY_SIZE(sscreen->tm));
1434 tm = sscreen->tm[thread_index];
1435 if (!debug->async)
1436 debug = NULL;
1437 } else {
1438 tm = sel->compiler_ctx_state.tm;
1439 }
1440
1441 /* Compile the main shader part for use with a prolog and/or epilog.
1442 * If this fails, the driver will try to compile a monolithic shader
1443 * on demand.
1444 */
1445 if (!sscreen->use_monolithic_shaders) {
1446 struct si_shader *shader = CALLOC_STRUCT(si_shader);
1447 void *tgsi_binary;
1448
1449 if (!shader) {
1450 fprintf(stderr, "radeonsi: can't allocate a main shader part\n");
1451 return;
1452 }
1453
1454 shader->selector = sel;
1455 si_parse_next_shader_property(&sel->info, &shader->key);
1456
1457 tgsi_binary = si_get_tgsi_binary(sel);
1458
1459 /* Try to load the shader from the shader cache. */
1460 pipe_mutex_lock(sscreen->shader_cache_mutex);
1461
1462 if (tgsi_binary &&
1463 si_shader_cache_load_shader(sscreen, tgsi_binary, shader)) {
1464 pipe_mutex_unlock(sscreen->shader_cache_mutex);
1465 } else {
1466 pipe_mutex_unlock(sscreen->shader_cache_mutex);
1467
1468 /* Compile the shader if it hasn't been loaded from the cache. */
1469 if (si_compile_tgsi_shader(sscreen, tm, shader, false,
1470 debug) != 0) {
1471 FREE(shader);
1472 FREE(tgsi_binary);
1473 fprintf(stderr, "radeonsi: can't compile a main shader part\n");
1474 return;
1475 }
1476
1477 if (tgsi_binary) {
1478 pipe_mutex_lock(sscreen->shader_cache_mutex);
1479 if (!si_shader_cache_insert_shader(sscreen, tgsi_binary, shader, true))
1480 FREE(tgsi_binary);
1481 pipe_mutex_unlock(sscreen->shader_cache_mutex);
1482 }
1483 }
1484
1485 *si_get_main_shader_part(sel, &shader->key) = shader;
1486
1487 /* Unset "outputs_written" flags for outputs converted to
1488 * DEFAULT_VAL, so that later inter-shader optimizations don't
1489 * try to eliminate outputs that don't exist in the final
1490 * shader.
1491 *
1492 * This is only done if non-monolithic shaders are enabled.
1493 */
1494 if ((sel->type == PIPE_SHADER_VERTEX ||
1495 sel->type == PIPE_SHADER_TESS_EVAL) &&
1496 !shader->key.as_ls &&
1497 !shader->key.as_es) {
1498 unsigned i;
1499
1500 for (i = 0; i < sel->info.num_outputs; i++) {
1501 unsigned offset = shader->info.vs_output_param_offset[i];
1502
1503 if (offset <= EXP_PARAM_OFFSET_31)
1504 continue;
1505
1506 unsigned name = sel->info.output_semantic_name[i];
1507 unsigned index = sel->info.output_semantic_index[i];
1508 unsigned id;
1509
1510 switch (name) {
1511 case TGSI_SEMANTIC_GENERIC:
1512 /* don't process indices the function can't handle */
1513 if (index >= 60)
1514 break;
1515 /* fall through */
1516 case TGSI_SEMANTIC_CLIPDIST:
1517 id = si_shader_io_get_unique_index(name, index);
1518 sel->outputs_written &= ~(1ull << id);
1519 break;
1520 case TGSI_SEMANTIC_POSITION: /* ignore these */
1521 case TGSI_SEMANTIC_PSIZE:
1522 case TGSI_SEMANTIC_CLIPVERTEX:
1523 case TGSI_SEMANTIC_EDGEFLAG:
1524 break;
1525 default:
1526 id = si_shader_io_get_unique_index2(name, index);
1527 sel->outputs_written2 &= ~(1u << id);
1528 }
1529 }
1530 }
1531 }
1532
1533 /* Pre-compilation. */
1534 if (sscreen->b.debug_flags & DBG_PRECOMPILE) {
1535 struct si_shader_ctx_state state = {sel};
1536 struct si_shader_key key;
1537
1538 memset(&key, 0, sizeof(key));
1539 si_parse_next_shader_property(&sel->info, &key);
1540
1541 /* Set reasonable defaults, so that the shader key doesn't
1542 * cause any code to be eliminated.
1543 */
1544 switch (sel->type) {
1545 case PIPE_SHADER_TESS_CTRL:
1546 key.part.tcs.epilog.prim_mode = PIPE_PRIM_TRIANGLES;
1547 break;
1548 case PIPE_SHADER_FRAGMENT:
1549 key.part.ps.prolog.bc_optimize_for_persp =
1550 sel->info.uses_persp_center &&
1551 sel->info.uses_persp_centroid;
1552 key.part.ps.prolog.bc_optimize_for_linear =
1553 sel->info.uses_linear_center &&
1554 sel->info.uses_linear_centroid;
1555 key.part.ps.epilog.alpha_func = PIPE_FUNC_ALWAYS;
1556 for (i = 0; i < 8; i++)
1557 if (sel->info.colors_written & (1 << i))
1558 key.part.ps.epilog.spi_shader_col_format |=
1559 V_028710_SPI_SHADER_FP16_ABGR << (i * 4);
1560 break;
1561 }
1562
1563 if (si_shader_select_with_key(sscreen, &state,
1564 &sel->compiler_ctx_state, &key,
1565 thread_index))
1566 fprintf(stderr, "radeonsi: can't create a monolithic shader\n");
1567 }
1568
1569 /* The GS copy shader is always pre-compiled. */
1570 if (sel->type == PIPE_SHADER_GEOMETRY) {
1571 sel->gs_copy_shader = si_generate_gs_copy_shader(sscreen, tm, sel, debug);
1572 if (!sel->gs_copy_shader) {
1573 fprintf(stderr, "radeonsi: can't create GS copy shader\n");
1574 return;
1575 }
1576
1577 si_shader_vs(sscreen, sel->gs_copy_shader, sel);
1578 }
1579 }
1580
1581 static void *si_create_shader_selector(struct pipe_context *ctx,
1582 const struct pipe_shader_state *state)
1583 {
1584 struct si_screen *sscreen = (struct si_screen *)ctx->screen;
1585 struct si_context *sctx = (struct si_context*)ctx;
1586 struct si_shader_selector *sel = CALLOC_STRUCT(si_shader_selector);
1587 int i;
1588
1589 if (!sel)
1590 return NULL;
1591
1592 sel->screen = sscreen;
1593 sel->compiler_ctx_state.tm = sctx->tm;
1594 sel->compiler_ctx_state.debug = sctx->b.debug;
1595 sel->compiler_ctx_state.is_debug_context = sctx->is_debug;
1596 sel->tokens = tgsi_dup_tokens(state->tokens);
1597 if (!sel->tokens) {
1598 FREE(sel);
1599 return NULL;
1600 }
1601
1602 sel->so = state->stream_output;
1603 tgsi_scan_shader(state->tokens, &sel->info);
1604 sel->type = sel->info.processor;
1605 p_atomic_inc(&sscreen->b.num_shaders_created);
1606
1607 /* Set which opcode uses which (i,j) pair. */
1608 if (sel->info.uses_persp_opcode_interp_centroid)
1609 sel->info.uses_persp_centroid = true;
1610
1611 if (sel->info.uses_linear_opcode_interp_centroid)
1612 sel->info.uses_linear_centroid = true;
1613
1614 if (sel->info.uses_persp_opcode_interp_offset ||
1615 sel->info.uses_persp_opcode_interp_sample)
1616 sel->info.uses_persp_center = true;
1617
1618 if (sel->info.uses_linear_opcode_interp_offset ||
1619 sel->info.uses_linear_opcode_interp_sample)
1620 sel->info.uses_linear_center = true;
1621
1622 switch (sel->type) {
1623 case PIPE_SHADER_GEOMETRY:
1624 sel->gs_output_prim =
1625 sel->info.properties[TGSI_PROPERTY_GS_OUTPUT_PRIM];
1626 sel->gs_max_out_vertices =
1627 sel->info.properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES];
1628 sel->gs_num_invocations =
1629 sel->info.properties[TGSI_PROPERTY_GS_INVOCATIONS];
1630 sel->gsvs_vertex_size = sel->info.num_outputs * 16;
1631 sel->max_gsvs_emit_size = sel->gsvs_vertex_size *
1632 sel->gs_max_out_vertices;
1633
1634 sel->max_gs_stream = 0;
1635 for (i = 0; i < sel->so.num_outputs; i++)
1636 sel->max_gs_stream = MAX2(sel->max_gs_stream,
1637 sel->so.output[i].stream);
1638
1639 sel->gs_input_verts_per_prim =
1640 u_vertices_per_prim(sel->info.properties[TGSI_PROPERTY_GS_INPUT_PRIM]);
1641 break;
1642
1643 case PIPE_SHADER_TESS_CTRL:
1644 /* Always reserve space for these. */
1645 sel->patch_outputs_written |=
1646 (1llu << si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER, 0)) |
1647 (1llu << si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER, 0));
1648 /* fall through */
1649 case PIPE_SHADER_VERTEX:
1650 case PIPE_SHADER_TESS_EVAL:
1651 for (i = 0; i < sel->info.num_outputs; i++) {
1652 unsigned name = sel->info.output_semantic_name[i];
1653 unsigned index = sel->info.output_semantic_index[i];
1654
1655 switch (name) {
1656 case TGSI_SEMANTIC_TESSINNER:
1657 case TGSI_SEMANTIC_TESSOUTER:
1658 case TGSI_SEMANTIC_PATCH:
1659 sel->patch_outputs_written |=
1660 1llu << si_shader_io_get_unique_index(name, index);
1661 break;
1662
1663 case TGSI_SEMANTIC_GENERIC:
1664 /* don't process indices the function can't handle */
1665 if (index >= 60)
1666 break;
1667 /* fall through */
1668 case TGSI_SEMANTIC_POSITION:
1669 case TGSI_SEMANTIC_PSIZE:
1670 case TGSI_SEMANTIC_CLIPDIST:
1671 sel->outputs_written |=
1672 1llu << si_shader_io_get_unique_index(name, index);
1673 break;
1674 case TGSI_SEMANTIC_CLIPVERTEX: /* ignore these */
1675 case TGSI_SEMANTIC_EDGEFLAG:
1676 break;
1677 default:
1678 sel->outputs_written2 |=
1679 1u << si_shader_io_get_unique_index2(name, index);
1680 }
1681 }
1682 sel->esgs_itemsize = util_last_bit64(sel->outputs_written) * 16;
1683 break;
1684
1685 case PIPE_SHADER_FRAGMENT:
1686 for (i = 0; i < sel->info.num_inputs; i++) {
1687 unsigned name = sel->info.input_semantic_name[i];
1688 unsigned index = sel->info.input_semantic_index[i];
1689
1690 switch (name) {
1691 case TGSI_SEMANTIC_CLIPDIST:
1692 case TGSI_SEMANTIC_GENERIC:
1693 sel->inputs_read |=
1694 1llu << si_shader_io_get_unique_index(name, index);
1695 break;
1696 case TGSI_SEMANTIC_PCOORD: /* ignore this */
1697 break;
1698 default:
1699 sel->inputs_read2 |=
1700 1u << si_shader_io_get_unique_index2(name, index);
1701 }
1702 }
1703
1704 for (i = 0; i < 8; i++)
1705 if (sel->info.colors_written & (1 << i))
1706 sel->colors_written_4bit |= 0xf << (4 * i);
1707
1708 for (i = 0; i < sel->info.num_inputs; i++) {
1709 if (sel->info.input_semantic_name[i] == TGSI_SEMANTIC_COLOR) {
1710 int index = sel->info.input_semantic_index[i];
1711 sel->color_attr_index[index] = i;
1712 }
1713 }
1714 break;
1715 }
1716
1717 /* DB_SHADER_CONTROL */
1718 sel->db_shader_control =
1719 S_02880C_Z_EXPORT_ENABLE(sel->info.writes_z) |
1720 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(sel->info.writes_stencil) |
1721 S_02880C_MASK_EXPORT_ENABLE(sel->info.writes_samplemask) |
1722 S_02880C_KILL_ENABLE(sel->info.uses_kill);
1723
1724 switch (sel->info.properties[TGSI_PROPERTY_FS_DEPTH_LAYOUT]) {
1725 case TGSI_FS_DEPTH_LAYOUT_GREATER:
1726 sel->db_shader_control |=
1727 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z);
1728 break;
1729 case TGSI_FS_DEPTH_LAYOUT_LESS:
1730 sel->db_shader_control |=
1731 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z);
1732 break;
1733 }
1734
1735 /* Z_ORDER, EXEC_ON_HIER_FAIL and EXEC_ON_NOOP should be set as following:
1736 *
1737 * | early Z/S | writes_mem | allow_ReZ? | Z_ORDER | EXEC_ON_HIER_FAIL | EXEC_ON_NOOP
1738 * --|-----------|------------|------------|--------------------|-------------------|-------------
1739 * 1a| false | false | true | EarlyZ_Then_ReZ | 0 | 0
1740 * 1b| false | false | false | EarlyZ_Then_LateZ | 0 | 0
1741 * 2 | false | true | n/a | LateZ | 1 | 0
1742 * 3 | true | false | n/a | EarlyZ_Then_LateZ | 0 | 0
1743 * 4 | true | true | n/a | EarlyZ_Then_LateZ | 0 | 1
1744 *
1745 * In cases 3 and 4, HW will force Z_ORDER to EarlyZ regardless of what's set in the register.
1746 * In case 2, NOOP_CULL is a don't care field. In case 2, 3 and 4, ReZ doesn't make sense.
1747 *
1748 * Don't use ReZ without profiling !!!
1749 *
1750 * ReZ decreases performance by 15% in DiRT: Showdown on Ultra settings, which has pretty complex
1751 * shaders.
1752 */
1753 if (sel->info.properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL]) {
1754 /* Cases 3, 4. */
1755 sel->db_shader_control |= S_02880C_DEPTH_BEFORE_SHADER(1) |
1756 S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z) |
1757 S_02880C_EXEC_ON_NOOP(sel->info.writes_memory);
1758 } else if (sel->info.writes_memory) {
1759 /* Case 2. */
1760 sel->db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z) |
1761 S_02880C_EXEC_ON_HIER_FAIL(1);
1762 } else {
1763 /* Case 1. */
1764 sel->db_shader_control |= S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z);
1765 }
1766
1767 (void) mtx_init(&sel->mutex, mtx_plain);
1768 util_queue_fence_init(&sel->ready);
1769
1770 if ((sctx->b.debug.debug_message && !sctx->b.debug.async) ||
1771 sctx->is_debug ||
1772 r600_can_dump_shader(&sscreen->b, sel->info.processor) ||
1773 !util_queue_is_initialized(&sscreen->shader_compiler_queue))
1774 si_init_shader_selector_async(sel, -1);
1775 else
1776 util_queue_add_job(&sscreen->shader_compiler_queue, sel,
1777 &sel->ready, si_init_shader_selector_async,
1778 NULL);
1779
1780 return sel;
1781 }
1782
1783 static void si_bind_vs_shader(struct pipe_context *ctx, void *state)
1784 {
1785 struct si_context *sctx = (struct si_context *)ctx;
1786 struct si_shader_selector *sel = state;
1787
1788 if (sctx->vs_shader.cso == sel)
1789 return;
1790
1791 sctx->vs_shader.cso = sel;
1792 sctx->vs_shader.current = sel ? sel->first_variant : NULL;
1793 sctx->do_update_shaders = true;
1794 si_mark_atom_dirty(sctx, &sctx->clip_regs);
1795 r600_update_vs_writes_viewport_index(&sctx->b, si_get_vs_info(sctx));
1796 }
1797
1798 static void si_bind_gs_shader(struct pipe_context *ctx, void *state)
1799 {
1800 struct si_context *sctx = (struct si_context *)ctx;
1801 struct si_shader_selector *sel = state;
1802 bool enable_changed = !!sctx->gs_shader.cso != !!sel;
1803
1804 if (sctx->gs_shader.cso == sel)
1805 return;
1806
1807 sctx->gs_shader.cso = sel;
1808 sctx->gs_shader.current = sel ? sel->first_variant : NULL;
1809 sctx->ia_multi_vgt_param_key.u.uses_gs = sel != NULL;
1810 sctx->do_update_shaders = true;
1811 si_mark_atom_dirty(sctx, &sctx->clip_regs);
1812 sctx->last_rast_prim = -1; /* reset this so that it gets updated */
1813
1814 if (enable_changed)
1815 si_shader_change_notify(sctx);
1816 r600_update_vs_writes_viewport_index(&sctx->b, si_get_vs_info(sctx));
1817 }
1818
1819 static void si_update_tcs_tes_uses_prim_id(struct si_context *sctx)
1820 {
1821 sctx->ia_multi_vgt_param_key.u.tcs_tes_uses_prim_id =
1822 (sctx->tes_shader.cso &&
1823 sctx->tes_shader.cso->info.uses_primid) ||
1824 (sctx->tcs_shader.cso &&
1825 sctx->tcs_shader.cso->info.uses_primid);
1826 }
1827
1828 static void si_bind_tcs_shader(struct pipe_context *ctx, void *state)
1829 {
1830 struct si_context *sctx = (struct si_context *)ctx;
1831 struct si_shader_selector *sel = state;
1832 bool enable_changed = !!sctx->tcs_shader.cso != !!sel;
1833
1834 if (sctx->tcs_shader.cso == sel)
1835 return;
1836
1837 sctx->tcs_shader.cso = sel;
1838 sctx->tcs_shader.current = sel ? sel->first_variant : NULL;
1839 si_update_tcs_tes_uses_prim_id(sctx);
1840 sctx->do_update_shaders = true;
1841
1842 if (enable_changed)
1843 sctx->last_tcs = NULL; /* invalidate derived tess state */
1844 }
1845
1846 static void si_bind_tes_shader(struct pipe_context *ctx, void *state)
1847 {
1848 struct si_context *sctx = (struct si_context *)ctx;
1849 struct si_shader_selector *sel = state;
1850 bool enable_changed = !!sctx->tes_shader.cso != !!sel;
1851
1852 if (sctx->tes_shader.cso == sel)
1853 return;
1854
1855 sctx->tes_shader.cso = sel;
1856 sctx->tes_shader.current = sel ? sel->first_variant : NULL;
1857 sctx->ia_multi_vgt_param_key.u.uses_tess = sel != NULL;
1858 si_update_tcs_tes_uses_prim_id(sctx);
1859 sctx->do_update_shaders = true;
1860 si_mark_atom_dirty(sctx, &sctx->clip_regs);
1861 sctx->last_rast_prim = -1; /* reset this so that it gets updated */
1862
1863 if (enable_changed) {
1864 si_shader_change_notify(sctx);
1865 sctx->last_tes_sh_base = -1; /* invalidate derived tess state */
1866 }
1867 r600_update_vs_writes_viewport_index(&sctx->b, si_get_vs_info(sctx));
1868 }
1869
1870 static void si_bind_ps_shader(struct pipe_context *ctx, void *state)
1871 {
1872 struct si_context *sctx = (struct si_context *)ctx;
1873 struct si_shader_selector *sel = state;
1874
1875 /* skip if supplied shader is one already in use */
1876 if (sctx->ps_shader.cso == sel)
1877 return;
1878
1879 sctx->ps_shader.cso = sel;
1880 sctx->ps_shader.current = sel ? sel->first_variant : NULL;
1881 sctx->do_update_shaders = true;
1882 si_mark_atom_dirty(sctx, &sctx->cb_render_state);
1883 }
1884
1885 static void si_delete_shader(struct si_context *sctx, struct si_shader *shader)
1886 {
1887 if (shader->is_optimized) {
1888 util_queue_fence_wait(&shader->optimized_ready);
1889 util_queue_fence_destroy(&shader->optimized_ready);
1890 }
1891
1892 if (shader->pm4) {
1893 switch (shader->selector->type) {
1894 case PIPE_SHADER_VERTEX:
1895 if (shader->key.as_ls)
1896 si_pm4_delete_state(sctx, ls, shader->pm4);
1897 else if (shader->key.as_es)
1898 si_pm4_delete_state(sctx, es, shader->pm4);
1899 else
1900 si_pm4_delete_state(sctx, vs, shader->pm4);
1901 break;
1902 case PIPE_SHADER_TESS_CTRL:
1903 si_pm4_delete_state(sctx, hs, shader->pm4);
1904 break;
1905 case PIPE_SHADER_TESS_EVAL:
1906 if (shader->key.as_es)
1907 si_pm4_delete_state(sctx, es, shader->pm4);
1908 else
1909 si_pm4_delete_state(sctx, vs, shader->pm4);
1910 break;
1911 case PIPE_SHADER_GEOMETRY:
1912 if (shader->is_gs_copy_shader)
1913 si_pm4_delete_state(sctx, vs, shader->pm4);
1914 else
1915 si_pm4_delete_state(sctx, gs, shader->pm4);
1916 break;
1917 case PIPE_SHADER_FRAGMENT:
1918 si_pm4_delete_state(sctx, ps, shader->pm4);
1919 break;
1920 }
1921 }
1922
1923 si_shader_destroy(shader);
1924 free(shader);
1925 }
1926
1927 static void si_delete_shader_selector(struct pipe_context *ctx, void *state)
1928 {
1929 struct si_context *sctx = (struct si_context *)ctx;
1930 struct si_shader_selector *sel = (struct si_shader_selector *)state;
1931 struct si_shader *p = sel->first_variant, *c;
1932 struct si_shader_ctx_state *current_shader[SI_NUM_SHADERS] = {
1933 [PIPE_SHADER_VERTEX] = &sctx->vs_shader,
1934 [PIPE_SHADER_TESS_CTRL] = &sctx->tcs_shader,
1935 [PIPE_SHADER_TESS_EVAL] = &sctx->tes_shader,
1936 [PIPE_SHADER_GEOMETRY] = &sctx->gs_shader,
1937 [PIPE_SHADER_FRAGMENT] = &sctx->ps_shader,
1938 };
1939
1940 util_queue_fence_wait(&sel->ready);
1941
1942 if (current_shader[sel->type]->cso == sel) {
1943 current_shader[sel->type]->cso = NULL;
1944 current_shader[sel->type]->current = NULL;
1945 }
1946
1947 while (p) {
1948 c = p->next_variant;
1949 si_delete_shader(sctx, p);
1950 p = c;
1951 }
1952
1953 if (sel->main_shader_part)
1954 si_delete_shader(sctx, sel->main_shader_part);
1955 if (sel->main_shader_part_ls)
1956 si_delete_shader(sctx, sel->main_shader_part_ls);
1957 if (sel->main_shader_part_es)
1958 si_delete_shader(sctx, sel->main_shader_part_es);
1959 if (sel->gs_copy_shader)
1960 si_delete_shader(sctx, sel->gs_copy_shader);
1961
1962 util_queue_fence_destroy(&sel->ready);
1963 pipe_mutex_destroy(sel->mutex);
1964 free(sel->tokens);
1965 free(sel);
1966 }
1967
1968 static unsigned si_get_ps_input_cntl(struct si_context *sctx,
1969 struct si_shader *vs, unsigned name,
1970 unsigned index, unsigned interpolate)
1971 {
1972 struct tgsi_shader_info *vsinfo = &vs->selector->info;
1973 unsigned j, offset, ps_input_cntl = 0;
1974
1975 if (interpolate == TGSI_INTERPOLATE_CONSTANT ||
1976 (interpolate == TGSI_INTERPOLATE_COLOR && sctx->flatshade))
1977 ps_input_cntl |= S_028644_FLAT_SHADE(1);
1978
1979 if (name == TGSI_SEMANTIC_PCOORD ||
1980 (name == TGSI_SEMANTIC_TEXCOORD &&
1981 sctx->sprite_coord_enable & (1 << index))) {
1982 ps_input_cntl |= S_028644_PT_SPRITE_TEX(1);
1983 }
1984
1985 for (j = 0; j < vsinfo->num_outputs; j++) {
1986 if (name == vsinfo->output_semantic_name[j] &&
1987 index == vsinfo->output_semantic_index[j]) {
1988 offset = vs->info.vs_output_param_offset[j];
1989
1990 if (offset <= EXP_PARAM_OFFSET_31) {
1991 /* The input is loaded from parameter memory. */
1992 ps_input_cntl |= S_028644_OFFSET(offset);
1993 } else if (!G_028644_PT_SPRITE_TEX(ps_input_cntl)) {
1994 if (offset == EXP_PARAM_UNDEFINED) {
1995 /* This can happen with depth-only rendering. */
1996 offset = 0;
1997 } else {
1998 /* The input is a DEFAULT_VAL constant. */
1999 assert(offset >= EXP_PARAM_DEFAULT_VAL_0000 &&
2000 offset <= EXP_PARAM_DEFAULT_VAL_1111);
2001 offset -= EXP_PARAM_DEFAULT_VAL_0000;
2002 }
2003
2004 ps_input_cntl = S_028644_OFFSET(0x20) |
2005 S_028644_DEFAULT_VAL(offset);
2006 }
2007 break;
2008 }
2009 }
2010
2011 if (name == TGSI_SEMANTIC_PRIMID)
2012 /* PrimID is written after the last output. */
2013 ps_input_cntl |= S_028644_OFFSET(vs->info.vs_output_param_offset[vsinfo->num_outputs]);
2014 else if (j == vsinfo->num_outputs && !G_028644_PT_SPRITE_TEX(ps_input_cntl)) {
2015 /* No corresponding output found, load defaults into input.
2016 * Don't set any other bits.
2017 * (FLAT_SHADE=1 completely changes behavior) */
2018 ps_input_cntl = S_028644_OFFSET(0x20);
2019 /* D3D 9 behaviour. GL is undefined */
2020 if (name == TGSI_SEMANTIC_COLOR && index == 0)
2021 ps_input_cntl |= S_028644_DEFAULT_VAL(3);
2022 }
2023 return ps_input_cntl;
2024 }
2025
2026 static void si_emit_spi_map(struct si_context *sctx, struct r600_atom *atom)
2027 {
2028 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
2029 struct si_shader *ps = sctx->ps_shader.current;
2030 struct si_shader *vs = si_get_vs_state(sctx);
2031 struct tgsi_shader_info *psinfo = ps ? &ps->selector->info : NULL;
2032 unsigned i, num_interp, num_written = 0, bcol_interp[2];
2033
2034 if (!ps || !ps->selector->info.num_inputs)
2035 return;
2036
2037 num_interp = si_get_ps_num_interp(ps);
2038 assert(num_interp > 0);
2039 radeon_set_context_reg_seq(cs, R_028644_SPI_PS_INPUT_CNTL_0, num_interp);
2040
2041 for (i = 0; i < psinfo->num_inputs; i++) {
2042 unsigned name = psinfo->input_semantic_name[i];
2043 unsigned index = psinfo->input_semantic_index[i];
2044 unsigned interpolate = psinfo->input_interpolate[i];
2045
2046 radeon_emit(cs, si_get_ps_input_cntl(sctx, vs, name, index,
2047 interpolate));
2048 num_written++;
2049
2050 if (name == TGSI_SEMANTIC_COLOR) {
2051 assert(index < ARRAY_SIZE(bcol_interp));
2052 bcol_interp[index] = interpolate;
2053 }
2054 }
2055
2056 if (ps->key.part.ps.prolog.color_two_side) {
2057 unsigned bcol = TGSI_SEMANTIC_BCOLOR;
2058
2059 for (i = 0; i < 2; i++) {
2060 if (!(psinfo->colors_read & (0xf << (i * 4))))
2061 continue;
2062
2063 radeon_emit(cs, si_get_ps_input_cntl(sctx, vs, bcol,
2064 i, bcol_interp[i]));
2065 num_written++;
2066 }
2067 }
2068 assert(num_interp == num_written);
2069 }
2070
2071 /**
2072 * Writing CONFIG or UCONFIG VGT registers requires VGT_FLUSH before that.
2073 */
2074 static void si_init_config_add_vgt_flush(struct si_context *sctx)
2075 {
2076 if (sctx->init_config_has_vgt_flush)
2077 return;
2078
2079 /* Done by Vulkan before VGT_FLUSH. */
2080 si_pm4_cmd_begin(sctx->init_config, PKT3_EVENT_WRITE);
2081 si_pm4_cmd_add(sctx->init_config,
2082 EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH) | EVENT_INDEX(4));
2083 si_pm4_cmd_end(sctx->init_config, false);
2084
2085 /* VGT_FLUSH is required even if VGT is idle. It resets VGT pointers. */
2086 si_pm4_cmd_begin(sctx->init_config, PKT3_EVENT_WRITE);
2087 si_pm4_cmd_add(sctx->init_config, EVENT_TYPE(V_028A90_VGT_FLUSH) | EVENT_INDEX(0));
2088 si_pm4_cmd_end(sctx->init_config, false);
2089 sctx->init_config_has_vgt_flush = true;
2090 }
2091
2092 /* Initialize state related to ESGS / GSVS ring buffers */
2093 static bool si_update_gs_ring_buffers(struct si_context *sctx)
2094 {
2095 struct si_shader_selector *es =
2096 sctx->tes_shader.cso ? sctx->tes_shader.cso : sctx->vs_shader.cso;
2097 struct si_shader_selector *gs = sctx->gs_shader.cso;
2098 struct si_pm4_state *pm4;
2099
2100 /* Chip constants. */
2101 unsigned num_se = sctx->screen->b.info.max_se;
2102 unsigned wave_size = 64;
2103 unsigned max_gs_waves = 32 * num_se; /* max 32 per SE on GCN */
2104 unsigned gs_vertex_reuse = 16 * num_se; /* GS_VERTEX_REUSE register (per SE) */
2105 unsigned alignment = 256 * num_se;
2106 /* The maximum size is 63.999 MB per SE. */
2107 unsigned max_size = ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se;
2108
2109 /* Calculate the minimum size. */
2110 unsigned min_esgs_ring_size = align(es->esgs_itemsize * gs_vertex_reuse *
2111 wave_size, alignment);
2112
2113 /* These are recommended sizes, not minimum sizes. */
2114 unsigned esgs_ring_size = max_gs_waves * 2 * wave_size *
2115 es->esgs_itemsize * gs->gs_input_verts_per_prim;
2116 unsigned gsvs_ring_size = max_gs_waves * 2 * wave_size *
2117 gs->max_gsvs_emit_size;
2118
2119 min_esgs_ring_size = align(min_esgs_ring_size, alignment);
2120 esgs_ring_size = align(esgs_ring_size, alignment);
2121 gsvs_ring_size = align(gsvs_ring_size, alignment);
2122
2123 esgs_ring_size = CLAMP(esgs_ring_size, min_esgs_ring_size, max_size);
2124 gsvs_ring_size = MIN2(gsvs_ring_size, max_size);
2125
2126 /* Some rings don't have to be allocated if shaders don't use them.
2127 * (e.g. no varyings between ES and GS or GS and VS)
2128 */
2129 bool update_esgs = esgs_ring_size &&
2130 (!sctx->esgs_ring ||
2131 sctx->esgs_ring->width0 < esgs_ring_size);
2132 bool update_gsvs = gsvs_ring_size &&
2133 (!sctx->gsvs_ring ||
2134 sctx->gsvs_ring->width0 < gsvs_ring_size);
2135
2136 if (!update_esgs && !update_gsvs)
2137 return true;
2138
2139 if (update_esgs) {
2140 pipe_resource_reference(&sctx->esgs_ring, NULL);
2141 sctx->esgs_ring =
2142 r600_aligned_buffer_create(sctx->b.b.screen,
2143 R600_RESOURCE_FLAG_UNMAPPABLE,
2144 PIPE_USAGE_DEFAULT,
2145 esgs_ring_size, alignment);
2146 if (!sctx->esgs_ring)
2147 return false;
2148 }
2149
2150 if (update_gsvs) {
2151 pipe_resource_reference(&sctx->gsvs_ring, NULL);
2152 sctx->gsvs_ring =
2153 r600_aligned_buffer_create(sctx->b.b.screen,
2154 R600_RESOURCE_FLAG_UNMAPPABLE,
2155 PIPE_USAGE_DEFAULT,
2156 gsvs_ring_size, alignment);
2157 if (!sctx->gsvs_ring)
2158 return false;
2159 }
2160
2161 /* Create the "init_config_gs_rings" state. */
2162 pm4 = CALLOC_STRUCT(si_pm4_state);
2163 if (!pm4)
2164 return false;
2165
2166 if (sctx->b.chip_class >= CIK) {
2167 if (sctx->esgs_ring)
2168 si_pm4_set_reg(pm4, R_030900_VGT_ESGS_RING_SIZE,
2169 sctx->esgs_ring->width0 / 256);
2170 if (sctx->gsvs_ring)
2171 si_pm4_set_reg(pm4, R_030904_VGT_GSVS_RING_SIZE,
2172 sctx->gsvs_ring->width0 / 256);
2173 } else {
2174 if (sctx->esgs_ring)
2175 si_pm4_set_reg(pm4, R_0088C8_VGT_ESGS_RING_SIZE,
2176 sctx->esgs_ring->width0 / 256);
2177 if (sctx->gsvs_ring)
2178 si_pm4_set_reg(pm4, R_0088CC_VGT_GSVS_RING_SIZE,
2179 sctx->gsvs_ring->width0 / 256);
2180 }
2181
2182 /* Set the state. */
2183 if (sctx->init_config_gs_rings)
2184 si_pm4_free_state(sctx, sctx->init_config_gs_rings, ~0);
2185 sctx->init_config_gs_rings = pm4;
2186
2187 if (!sctx->init_config_has_vgt_flush) {
2188 si_init_config_add_vgt_flush(sctx);
2189 si_pm4_upload_indirect_buffer(sctx, sctx->init_config);
2190 }
2191
2192 /* Flush the context to re-emit both init_config states. */
2193 sctx->b.initial_gfx_cs_size = 0; /* force flush */
2194 si_context_gfx_flush(sctx, RADEON_FLUSH_ASYNC, NULL);
2195
2196 /* Set ring bindings. */
2197 if (sctx->esgs_ring) {
2198 si_set_ring_buffer(&sctx->b.b, SI_ES_RING_ESGS,
2199 sctx->esgs_ring, 0, sctx->esgs_ring->width0,
2200 true, true, 4, 64, 0);
2201 si_set_ring_buffer(&sctx->b.b, SI_GS_RING_ESGS,
2202 sctx->esgs_ring, 0, sctx->esgs_ring->width0,
2203 false, false, 0, 0, 0);
2204 }
2205 if (sctx->gsvs_ring) {
2206 si_set_ring_buffer(&sctx->b.b, SI_RING_GSVS,
2207 sctx->gsvs_ring, 0, sctx->gsvs_ring->width0,
2208 false, false, 0, 0, 0);
2209 }
2210
2211 return true;
2212 }
2213
2214 /**
2215 * @returns 1 if \p sel has been updated to use a new scratch buffer
2216 * 0 if not
2217 * < 0 if there was a failure
2218 */
2219 static int si_update_scratch_buffer(struct si_context *sctx,
2220 struct si_shader *shader)
2221 {
2222 uint64_t scratch_va = sctx->scratch_buffer->gpu_address;
2223 int r;
2224
2225 if (!shader)
2226 return 0;
2227
2228 /* This shader doesn't need a scratch buffer */
2229 if (shader->config.scratch_bytes_per_wave == 0)
2230 return 0;
2231
2232 /* This shader is already configured to use the current
2233 * scratch buffer. */
2234 if (shader->scratch_bo == sctx->scratch_buffer)
2235 return 0;
2236
2237 assert(sctx->scratch_buffer);
2238
2239 si_shader_apply_scratch_relocs(sctx, shader, &shader->config, scratch_va);
2240
2241 /* Replace the shader bo with a new bo that has the relocs applied. */
2242 r = si_shader_binary_upload(sctx->screen, shader);
2243 if (r)
2244 return r;
2245
2246 /* Update the shader state to use the new shader bo. */
2247 si_shader_init_pm4_state(sctx->screen, shader);
2248
2249 r600_resource_reference(&shader->scratch_bo, sctx->scratch_buffer);
2250
2251 return 1;
2252 }
2253
2254 static unsigned si_get_current_scratch_buffer_size(struct si_context *sctx)
2255 {
2256 return sctx->scratch_buffer ? sctx->scratch_buffer->b.b.width0 : 0;
2257 }
2258
2259 static unsigned si_get_scratch_buffer_bytes_per_wave(struct si_shader *shader)
2260 {
2261 return shader ? shader->config.scratch_bytes_per_wave : 0;
2262 }
2263
2264 static unsigned si_get_max_scratch_bytes_per_wave(struct si_context *sctx)
2265 {
2266 unsigned bytes = 0;
2267
2268 bytes = MAX2(bytes, si_get_scratch_buffer_bytes_per_wave(sctx->ps_shader.current));
2269 bytes = MAX2(bytes, si_get_scratch_buffer_bytes_per_wave(sctx->gs_shader.current));
2270 bytes = MAX2(bytes, si_get_scratch_buffer_bytes_per_wave(sctx->vs_shader.current));
2271 bytes = MAX2(bytes, si_get_scratch_buffer_bytes_per_wave(sctx->tcs_shader.current));
2272 bytes = MAX2(bytes, si_get_scratch_buffer_bytes_per_wave(sctx->tes_shader.current));
2273 return bytes;
2274 }
2275
2276 static bool si_update_spi_tmpring_size(struct si_context *sctx)
2277 {
2278 unsigned current_scratch_buffer_size =
2279 si_get_current_scratch_buffer_size(sctx);
2280 unsigned scratch_bytes_per_wave =
2281 si_get_max_scratch_bytes_per_wave(sctx);
2282 unsigned scratch_needed_size = scratch_bytes_per_wave *
2283 sctx->scratch_waves;
2284 unsigned spi_tmpring_size;
2285 int r;
2286
2287 if (scratch_needed_size > 0) {
2288 if (scratch_needed_size > current_scratch_buffer_size) {
2289 /* Create a bigger scratch buffer */
2290 r600_resource_reference(&sctx->scratch_buffer, NULL);
2291
2292 sctx->scratch_buffer = (struct r600_resource*)
2293 r600_aligned_buffer_create(&sctx->screen->b.b,
2294 R600_RESOURCE_FLAG_UNMAPPABLE,
2295 PIPE_USAGE_DEFAULT,
2296 scratch_needed_size, 256);
2297 if (!sctx->scratch_buffer)
2298 return false;
2299
2300 si_mark_atom_dirty(sctx, &sctx->scratch_state);
2301 r600_context_add_resource_size(&sctx->b.b,
2302 &sctx->scratch_buffer->b.b);
2303 }
2304
2305 /* Update the shaders, so they are using the latest scratch. The
2306 * scratch buffer may have been changed since these shaders were
2307 * last used, so we still need to try to update them, even if
2308 * they require scratch buffers smaller than the current size.
2309 */
2310 r = si_update_scratch_buffer(sctx, sctx->ps_shader.current);
2311 if (r < 0)
2312 return false;
2313 if (r == 1)
2314 si_pm4_bind_state(sctx, ps, sctx->ps_shader.current->pm4);
2315
2316 r = si_update_scratch_buffer(sctx, sctx->gs_shader.current);
2317 if (r < 0)
2318 return false;
2319 if (r == 1)
2320 si_pm4_bind_state(sctx, gs, sctx->gs_shader.current->pm4);
2321
2322 r = si_update_scratch_buffer(sctx, sctx->tcs_shader.current);
2323 if (r < 0)
2324 return false;
2325 if (r == 1)
2326 si_pm4_bind_state(sctx, hs, sctx->tcs_shader.current->pm4);
2327
2328 /* VS can be bound as LS, ES, or VS. */
2329 r = si_update_scratch_buffer(sctx, sctx->vs_shader.current);
2330 if (r < 0)
2331 return false;
2332 if (r == 1) {
2333 if (sctx->tes_shader.current)
2334 si_pm4_bind_state(sctx, ls, sctx->vs_shader.current->pm4);
2335 else if (sctx->gs_shader.current)
2336 si_pm4_bind_state(sctx, es, sctx->vs_shader.current->pm4);
2337 else
2338 si_pm4_bind_state(sctx, vs, sctx->vs_shader.current->pm4);
2339 }
2340
2341 /* TES can be bound as ES or VS. */
2342 r = si_update_scratch_buffer(sctx, sctx->tes_shader.current);
2343 if (r < 0)
2344 return false;
2345 if (r == 1) {
2346 if (sctx->gs_shader.current)
2347 si_pm4_bind_state(sctx, es, sctx->tes_shader.current->pm4);
2348 else
2349 si_pm4_bind_state(sctx, vs, sctx->tes_shader.current->pm4);
2350 }
2351 }
2352
2353 /* The LLVM shader backend should be reporting aligned scratch_sizes. */
2354 assert((scratch_needed_size & ~0x3FF) == scratch_needed_size &&
2355 "scratch size should already be aligned correctly.");
2356
2357 spi_tmpring_size = S_0286E8_WAVES(sctx->scratch_waves) |
2358 S_0286E8_WAVESIZE(scratch_bytes_per_wave >> 10);
2359 if (spi_tmpring_size != sctx->spi_tmpring_size) {
2360 sctx->spi_tmpring_size = spi_tmpring_size;
2361 si_mark_atom_dirty(sctx, &sctx->scratch_state);
2362 }
2363 return true;
2364 }
2365
2366 static void si_init_tess_factor_ring(struct si_context *sctx)
2367 {
2368 bool double_offchip_buffers = sctx->b.chip_class >= CIK &&
2369 sctx->b.family != CHIP_CARRIZO &&
2370 sctx->b.family != CHIP_STONEY;
2371 unsigned max_offchip_buffers_per_se = double_offchip_buffers ? 128 : 64;
2372 unsigned max_offchip_buffers = max_offchip_buffers_per_se *
2373 sctx->screen->b.info.max_se;
2374 unsigned offchip_granularity;
2375
2376 switch (sctx->screen->tess_offchip_block_dw_size) {
2377 default:
2378 assert(0);
2379 /* fall through */
2380 case 8192:
2381 offchip_granularity = V_03093C_X_8K_DWORDS;
2382 break;
2383 case 4096:
2384 offchip_granularity = V_03093C_X_4K_DWORDS;
2385 break;
2386 }
2387
2388 switch (sctx->b.chip_class) {
2389 case SI:
2390 max_offchip_buffers = MIN2(max_offchip_buffers, 126);
2391 break;
2392 case CIK:
2393 max_offchip_buffers = MIN2(max_offchip_buffers, 508);
2394 break;
2395 case VI:
2396 default:
2397 max_offchip_buffers = MIN2(max_offchip_buffers, 512);
2398 break;
2399 }
2400
2401 assert(!sctx->tf_ring);
2402 sctx->tf_ring = r600_aligned_buffer_create(sctx->b.b.screen,
2403 R600_RESOURCE_FLAG_UNMAPPABLE,
2404 PIPE_USAGE_DEFAULT,
2405 32768 * sctx->screen->b.info.max_se,
2406 256);
2407 if (!sctx->tf_ring)
2408 return;
2409
2410 assert(((sctx->tf_ring->width0 / 4) & C_030938_SIZE) == 0);
2411
2412 sctx->tess_offchip_ring =
2413 r600_aligned_buffer_create(sctx->b.b.screen,
2414 R600_RESOURCE_FLAG_UNMAPPABLE,
2415 PIPE_USAGE_DEFAULT,
2416 max_offchip_buffers *
2417 sctx->screen->tess_offchip_block_dw_size * 4,
2418 256);
2419 if (!sctx->tess_offchip_ring)
2420 return;
2421
2422 si_init_config_add_vgt_flush(sctx);
2423
2424 /* Append these registers to the init config state. */
2425 if (sctx->b.chip_class >= CIK) {
2426 if (sctx->b.chip_class >= VI)
2427 --max_offchip_buffers;
2428
2429 si_pm4_set_reg(sctx->init_config, R_030938_VGT_TF_RING_SIZE,
2430 S_030938_SIZE(sctx->tf_ring->width0 / 4));
2431 si_pm4_set_reg(sctx->init_config, R_030940_VGT_TF_MEMORY_BASE,
2432 r600_resource(sctx->tf_ring)->gpu_address >> 8);
2433 si_pm4_set_reg(sctx->init_config, R_03093C_VGT_HS_OFFCHIP_PARAM,
2434 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers) |
2435 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity));
2436 } else {
2437 assert(offchip_granularity == V_03093C_X_8K_DWORDS);
2438 si_pm4_set_reg(sctx->init_config, R_008988_VGT_TF_RING_SIZE,
2439 S_008988_SIZE(sctx->tf_ring->width0 / 4));
2440 si_pm4_set_reg(sctx->init_config, R_0089B8_VGT_TF_MEMORY_BASE,
2441 r600_resource(sctx->tf_ring)->gpu_address >> 8);
2442 si_pm4_set_reg(sctx->init_config, R_0089B0_VGT_HS_OFFCHIP_PARAM,
2443 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers));
2444 }
2445
2446 /* Flush the context to re-emit the init_config state.
2447 * This is done only once in a lifetime of a context.
2448 */
2449 si_pm4_upload_indirect_buffer(sctx, sctx->init_config);
2450 sctx->b.initial_gfx_cs_size = 0; /* force flush */
2451 si_context_gfx_flush(sctx, RADEON_FLUSH_ASYNC, NULL);
2452
2453 si_set_ring_buffer(&sctx->b.b, SI_HS_RING_TESS_FACTOR, sctx->tf_ring,
2454 0, sctx->tf_ring->width0, false, false, 0, 0, 0);
2455
2456 si_set_ring_buffer(&sctx->b.b, SI_HS_RING_TESS_OFFCHIP,
2457 sctx->tess_offchip_ring, 0,
2458 sctx->tess_offchip_ring->width0, false, false, 0, 0, 0);
2459 }
2460
2461 /**
2462 * This is used when TCS is NULL in the VS->TCS->TES chain. In this case,
2463 * VS passes its outputs to TES directly, so the fixed-function shader only
2464 * has to write TESSOUTER and TESSINNER.
2465 */
2466 static void si_generate_fixed_func_tcs(struct si_context *sctx)
2467 {
2468 struct ureg_src outer, inner;
2469 struct ureg_dst tessouter, tessinner;
2470 struct ureg_program *ureg = ureg_create(PIPE_SHADER_TESS_CTRL);
2471
2472 if (!ureg)
2473 return; /* if we get here, we're screwed */
2474
2475 assert(!sctx->fixed_func_tcs_shader.cso);
2476
2477 outer = ureg_DECL_system_value(ureg,
2478 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI, 0);
2479 inner = ureg_DECL_system_value(ureg,
2480 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI, 0);
2481
2482 tessouter = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSOUTER, 0);
2483 tessinner = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSINNER, 0);
2484
2485 ureg_MOV(ureg, tessouter, outer);
2486 ureg_MOV(ureg, tessinner, inner);
2487 ureg_END(ureg);
2488
2489 sctx->fixed_func_tcs_shader.cso =
2490 ureg_create_shader_and_destroy(ureg, &sctx->b.b);
2491 }
2492
2493 static void si_update_vgt_shader_config(struct si_context *sctx)
2494 {
2495 /* Calculate the index of the config.
2496 * 0 = VS, 1 = VS+GS, 2 = VS+Tess, 3 = VS+Tess+GS */
2497 unsigned index = 2*!!sctx->tes_shader.cso + !!sctx->gs_shader.cso;
2498 struct si_pm4_state **pm4 = &sctx->vgt_shader_config[index];
2499
2500 if (!*pm4) {
2501 uint32_t stages = 0;
2502
2503 *pm4 = CALLOC_STRUCT(si_pm4_state);
2504
2505 if (sctx->tes_shader.cso) {
2506 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
2507 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
2508
2509 if (sctx->gs_shader.cso)
2510 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
2511 S_028B54_GS_EN(1) |
2512 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
2513 else
2514 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
2515 } else if (sctx->gs_shader.cso) {
2516 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
2517 S_028B54_GS_EN(1) |
2518 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
2519 }
2520
2521 si_pm4_set_reg(*pm4, R_028B54_VGT_SHADER_STAGES_EN, stages);
2522 }
2523 si_pm4_bind_state(sctx, vgt_shader_config, *pm4);
2524 }
2525
2526 static void si_update_so(struct si_context *sctx, struct si_shader_selector *shader)
2527 {
2528 struct pipe_stream_output_info *so = &shader->so;
2529 uint32_t enabled_stream_buffers_mask = 0;
2530 int i;
2531
2532 for (i = 0; i < so->num_outputs; i++)
2533 enabled_stream_buffers_mask |= (1 << so->output[i].output_buffer) << (so->output[i].stream * 4);
2534 sctx->b.streamout.enabled_stream_buffers_mask = enabled_stream_buffers_mask;
2535 sctx->b.streamout.stride_in_dw = shader->so.stride;
2536 }
2537
2538 bool si_update_shaders(struct si_context *sctx)
2539 {
2540 struct pipe_context *ctx = (struct pipe_context*)sctx;
2541 struct si_compiler_ctx_state compiler_state;
2542 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
2543 struct si_shader *old_vs = si_get_vs_state(sctx);
2544 bool old_clip_disable = old_vs ? old_vs->key.opt.hw_vs.clip_disable : false;
2545 int r;
2546
2547 compiler_state.tm = sctx->tm;
2548 compiler_state.debug = sctx->b.debug;
2549 compiler_state.is_debug_context = sctx->is_debug;
2550
2551 /* Update stages before GS. */
2552 if (sctx->tes_shader.cso) {
2553 if (!sctx->tf_ring) {
2554 si_init_tess_factor_ring(sctx);
2555 if (!sctx->tf_ring)
2556 return false;
2557 }
2558
2559 /* VS as LS */
2560 r = si_shader_select(ctx, &sctx->vs_shader, &compiler_state);
2561 if (r)
2562 return false;
2563 si_pm4_bind_state(sctx, ls, sctx->vs_shader.current->pm4);
2564
2565 if (sctx->tcs_shader.cso) {
2566 r = si_shader_select(ctx, &sctx->tcs_shader,
2567 &compiler_state);
2568 if (r)
2569 return false;
2570 si_pm4_bind_state(sctx, hs, sctx->tcs_shader.current->pm4);
2571 } else {
2572 if (!sctx->fixed_func_tcs_shader.cso) {
2573 si_generate_fixed_func_tcs(sctx);
2574 if (!sctx->fixed_func_tcs_shader.cso)
2575 return false;
2576 }
2577
2578 r = si_shader_select(ctx, &sctx->fixed_func_tcs_shader,
2579 &compiler_state);
2580 if (r)
2581 return false;
2582 si_pm4_bind_state(sctx, hs,
2583 sctx->fixed_func_tcs_shader.current->pm4);
2584 }
2585
2586 r = si_shader_select(ctx, &sctx->tes_shader, &compiler_state);
2587 if (r)
2588 return false;
2589
2590 if (sctx->gs_shader.cso) {
2591 /* TES as ES */
2592 si_pm4_bind_state(sctx, es, sctx->tes_shader.current->pm4);
2593 } else {
2594 /* TES as VS */
2595 si_pm4_bind_state(sctx, vs, sctx->tes_shader.current->pm4);
2596 si_update_so(sctx, sctx->tes_shader.cso);
2597 }
2598 } else if (sctx->gs_shader.cso) {
2599 /* VS as ES */
2600 r = si_shader_select(ctx, &sctx->vs_shader, &compiler_state);
2601 if (r)
2602 return false;
2603 si_pm4_bind_state(sctx, es, sctx->vs_shader.current->pm4);
2604
2605 si_pm4_bind_state(sctx, ls, NULL);
2606 si_pm4_bind_state(sctx, hs, NULL);
2607 } else {
2608 /* VS as VS */
2609 r = si_shader_select(ctx, &sctx->vs_shader, &compiler_state);
2610 if (r)
2611 return false;
2612 si_pm4_bind_state(sctx, vs, sctx->vs_shader.current->pm4);
2613 si_update_so(sctx, sctx->vs_shader.cso);
2614
2615 si_pm4_bind_state(sctx, ls, NULL);
2616 si_pm4_bind_state(sctx, hs, NULL);
2617 }
2618
2619 /* Update GS. */
2620 if (sctx->gs_shader.cso) {
2621 r = si_shader_select(ctx, &sctx->gs_shader, &compiler_state);
2622 if (r)
2623 return false;
2624 si_pm4_bind_state(sctx, gs, sctx->gs_shader.current->pm4);
2625 si_pm4_bind_state(sctx, vs, sctx->gs_shader.cso->gs_copy_shader->pm4);
2626 si_update_so(sctx, sctx->gs_shader.cso);
2627
2628 if (!si_update_gs_ring_buffers(sctx))
2629 return false;
2630 } else {
2631 si_pm4_bind_state(sctx, gs, NULL);
2632 si_pm4_bind_state(sctx, es, NULL);
2633 }
2634
2635 si_update_vgt_shader_config(sctx);
2636
2637 if (old_clip_disable != si_get_vs_state(sctx)->key.opt.hw_vs.clip_disable)
2638 si_mark_atom_dirty(sctx, &sctx->clip_regs);
2639
2640 if (sctx->ps_shader.cso) {
2641 unsigned db_shader_control;
2642
2643 r = si_shader_select(ctx, &sctx->ps_shader, &compiler_state);
2644 if (r)
2645 return false;
2646 si_pm4_bind_state(sctx, ps, sctx->ps_shader.current->pm4);
2647
2648 db_shader_control =
2649 sctx->ps_shader.cso->db_shader_control |
2650 S_02880C_KILL_ENABLE(si_get_alpha_test_func(sctx) != PIPE_FUNC_ALWAYS);
2651
2652 if (si_pm4_state_changed(sctx, ps) || si_pm4_state_changed(sctx, vs) ||
2653 sctx->sprite_coord_enable != rs->sprite_coord_enable ||
2654 sctx->flatshade != rs->flatshade) {
2655 sctx->sprite_coord_enable = rs->sprite_coord_enable;
2656 sctx->flatshade = rs->flatshade;
2657 si_mark_atom_dirty(sctx, &sctx->spi_map);
2658 }
2659
2660 if (sctx->b.family == CHIP_STONEY && si_pm4_state_changed(sctx, ps))
2661 si_mark_atom_dirty(sctx, &sctx->cb_render_state);
2662
2663 if (sctx->ps_db_shader_control != db_shader_control) {
2664 sctx->ps_db_shader_control = db_shader_control;
2665 si_mark_atom_dirty(sctx, &sctx->db_render_state);
2666 }
2667
2668 if (sctx->smoothing_enabled != sctx->ps_shader.current->key.part.ps.epilog.poly_line_smoothing) {
2669 sctx->smoothing_enabled = sctx->ps_shader.current->key.part.ps.epilog.poly_line_smoothing;
2670 si_mark_atom_dirty(sctx, &sctx->msaa_config);
2671
2672 if (sctx->b.chip_class == SI)
2673 si_mark_atom_dirty(sctx, &sctx->db_render_state);
2674
2675 if (sctx->framebuffer.nr_samples <= 1)
2676 si_mark_atom_dirty(sctx, &sctx->msaa_sample_locs.atom);
2677 }
2678 }
2679
2680 if (si_pm4_state_changed(sctx, ls) ||
2681 si_pm4_state_changed(sctx, hs) ||
2682 si_pm4_state_changed(sctx, es) ||
2683 si_pm4_state_changed(sctx, gs) ||
2684 si_pm4_state_changed(sctx, vs) ||
2685 si_pm4_state_changed(sctx, ps)) {
2686 if (!si_update_spi_tmpring_size(sctx))
2687 return false;
2688 }
2689
2690 if (sctx->b.chip_class >= CIK)
2691 si_mark_atom_dirty(sctx, &sctx->prefetch_L2);
2692
2693 sctx->do_update_shaders = false;
2694 return true;
2695 }
2696
2697 static void si_emit_scratch_state(struct si_context *sctx,
2698 struct r600_atom *atom)
2699 {
2700 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
2701
2702 radeon_set_context_reg(cs, R_0286E8_SPI_TMPRING_SIZE,
2703 sctx->spi_tmpring_size);
2704
2705 if (sctx->scratch_buffer) {
2706 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
2707 sctx->scratch_buffer, RADEON_USAGE_READWRITE,
2708 RADEON_PRIO_SCRATCH_BUFFER);
2709 }
2710 }
2711
2712 void si_init_shader_functions(struct si_context *sctx)
2713 {
2714 si_init_atom(sctx, &sctx->spi_map, &sctx->atoms.s.spi_map, si_emit_spi_map);
2715 si_init_atom(sctx, &sctx->scratch_state, &sctx->atoms.s.scratch_state,
2716 si_emit_scratch_state);
2717
2718 sctx->b.b.create_vs_state = si_create_shader_selector;
2719 sctx->b.b.create_tcs_state = si_create_shader_selector;
2720 sctx->b.b.create_tes_state = si_create_shader_selector;
2721 sctx->b.b.create_gs_state = si_create_shader_selector;
2722 sctx->b.b.create_fs_state = si_create_shader_selector;
2723
2724 sctx->b.b.bind_vs_state = si_bind_vs_shader;
2725 sctx->b.b.bind_tcs_state = si_bind_tcs_shader;
2726 sctx->b.b.bind_tes_state = si_bind_tes_shader;
2727 sctx->b.b.bind_gs_state = si_bind_gs_shader;
2728 sctx->b.b.bind_fs_state = si_bind_ps_shader;
2729
2730 sctx->b.b.delete_vs_state = si_delete_shader_selector;
2731 sctx->b.b.delete_tcs_state = si_delete_shader_selector;
2732 sctx->b.b.delete_tes_state = si_delete_shader_selector;
2733 sctx->b.b.delete_gs_state = si_delete_shader_selector;
2734 sctx->b.b.delete_fs_state = si_delete_shader_selector;
2735 }