2 * Copyright 2012 Advanced Micro Devices, Inc.
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:
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
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.
24 * Christian König <christian.koenig@amd.com>
25 * Marek Olšák <maraeo@gmail.com>
31 #include "radeon/r600_cs.h"
33 #include "tgsi/tgsi_parse.h"
34 #include "tgsi/tgsi_ureg.h"
35 #include "util/hash_table.h"
36 #include "util/crc32.h"
37 #include "util/u_memory.h"
38 #include "util/u_prim.h"
40 #include "util/disk_cache.h"
41 #include "util/mesa-sha1.h"
46 * Return the TGSI binary in a buffer. The first 4 bytes contain its size as
49 static void *si_get_tgsi_binary(struct si_shader_selector
*sel
)
51 unsigned tgsi_size
= tgsi_num_tokens(sel
->tokens
) *
52 sizeof(struct tgsi_token
);
53 unsigned size
= 4 + tgsi_size
+ sizeof(sel
->so
);
54 char *result
= (char*)MALLOC(size
);
59 *((uint32_t*)result
) = size
;
60 memcpy(result
+ 4, sel
->tokens
, tgsi_size
);
61 memcpy(result
+ 4 + tgsi_size
, &sel
->so
, sizeof(sel
->so
));
65 /** Copy "data" to "ptr" and return the next dword following copied data. */
66 static uint32_t *write_data(uint32_t *ptr
, const void *data
, unsigned size
)
68 /* data may be NULL if size == 0 */
70 memcpy(ptr
, data
, size
);
71 ptr
+= DIV_ROUND_UP(size
, 4);
75 /** Read data from "ptr". Return the next dword following the data. */
76 static uint32_t *read_data(uint32_t *ptr
, void *data
, unsigned size
)
78 memcpy(data
, ptr
, size
);
79 ptr
+= DIV_ROUND_UP(size
, 4);
84 * Write the size as uint followed by the data. Return the next dword
85 * following the copied data.
87 static uint32_t *write_chunk(uint32_t *ptr
, const void *data
, unsigned size
)
90 return write_data(ptr
, data
, size
);
94 * Read the size as uint followed by the data. Return both via parameters.
95 * Return the next dword following the data.
97 static uint32_t *read_chunk(uint32_t *ptr
, void **data
, unsigned *size
)
100 assert(*data
== NULL
);
103 *data
= malloc(*size
);
104 return read_data(ptr
, *data
, *size
);
108 * Return the shader binary in a buffer. The first 4 bytes contain its size
111 static void *si_get_shader_binary(struct si_shader
*shader
)
113 /* There is always a size of data followed by the data itself. */
114 unsigned relocs_size
= shader
->binary
.reloc_count
*
115 sizeof(shader
->binary
.relocs
[0]);
116 unsigned disasm_size
= strlen(shader
->binary
.disasm_string
) + 1;
117 unsigned llvm_ir_size
= shader
->binary
.llvm_ir_string
?
118 strlen(shader
->binary
.llvm_ir_string
) + 1 : 0;
121 4 + /* CRC32 of the data below */
122 align(sizeof(shader
->config
), 4) +
123 align(sizeof(shader
->info
), 4) +
124 4 + align(shader
->binary
.code_size
, 4) +
125 4 + align(shader
->binary
.rodata_size
, 4) +
126 4 + align(relocs_size
, 4) +
127 4 + align(disasm_size
, 4) +
128 4 + align(llvm_ir_size
, 4);
129 void *buffer
= CALLOC(1, size
);
130 uint32_t *ptr
= (uint32_t*)buffer
;
136 ptr
++; /* CRC32 is calculated at the end. */
138 ptr
= write_data(ptr
, &shader
->config
, sizeof(shader
->config
));
139 ptr
= write_data(ptr
, &shader
->info
, sizeof(shader
->info
));
140 ptr
= write_chunk(ptr
, shader
->binary
.code
, shader
->binary
.code_size
);
141 ptr
= write_chunk(ptr
, shader
->binary
.rodata
, shader
->binary
.rodata_size
);
142 ptr
= write_chunk(ptr
, shader
->binary
.relocs
, relocs_size
);
143 ptr
= write_chunk(ptr
, shader
->binary
.disasm_string
, disasm_size
);
144 ptr
= write_chunk(ptr
, shader
->binary
.llvm_ir_string
, llvm_ir_size
);
145 assert((char *)ptr
- (char *)buffer
== size
);
148 ptr
= (uint32_t*)buffer
;
150 *ptr
= util_hash_crc32(ptr
+ 1, size
- 8);
155 static bool si_load_shader_binary(struct si_shader
*shader
, void *binary
)
157 uint32_t *ptr
= (uint32_t*)binary
;
158 uint32_t size
= *ptr
++;
159 uint32_t crc32
= *ptr
++;
162 if (util_hash_crc32(ptr
, size
- 8) != crc32
) {
163 fprintf(stderr
, "radeonsi: binary shader has invalid CRC32\n");
167 ptr
= read_data(ptr
, &shader
->config
, sizeof(shader
->config
));
168 ptr
= read_data(ptr
, &shader
->info
, sizeof(shader
->info
));
169 ptr
= read_chunk(ptr
, (void**)&shader
->binary
.code
,
170 &shader
->binary
.code_size
);
171 ptr
= read_chunk(ptr
, (void**)&shader
->binary
.rodata
,
172 &shader
->binary
.rodata_size
);
173 ptr
= read_chunk(ptr
, (void**)&shader
->binary
.relocs
, &chunk_size
);
174 shader
->binary
.reloc_count
= chunk_size
/ sizeof(shader
->binary
.relocs
[0]);
175 ptr
= read_chunk(ptr
, (void**)&shader
->binary
.disasm_string
, &chunk_size
);
176 ptr
= read_chunk(ptr
, (void**)&shader
->binary
.llvm_ir_string
, &chunk_size
);
182 * Insert a shader into the cache. It's assumed the shader is not in the cache.
183 * Use si_shader_cache_load_shader before calling this.
185 * Returns false on failure, in which case the tgsi_binary should be freed.
187 static bool si_shader_cache_insert_shader(struct si_screen
*sscreen
,
189 struct si_shader
*shader
,
190 bool insert_into_disk_cache
)
193 struct hash_entry
*entry
;
194 uint8_t key
[CACHE_KEY_SIZE
];
196 entry
= _mesa_hash_table_search(sscreen
->shader_cache
, tgsi_binary
);
198 return false; /* already added */
200 hw_binary
= si_get_shader_binary(shader
);
204 if (_mesa_hash_table_insert(sscreen
->shader_cache
, tgsi_binary
,
205 hw_binary
) == NULL
) {
210 if (sscreen
->b
.disk_shader_cache
&& insert_into_disk_cache
) {
211 disk_cache_compute_key(sscreen
->b
.disk_shader_cache
, tgsi_binary
,
212 *((uint32_t *)tgsi_binary
), key
);
213 disk_cache_put(sscreen
->b
.disk_shader_cache
, key
, hw_binary
,
214 *((uint32_t *) hw_binary
));
220 static bool si_shader_cache_load_shader(struct si_screen
*sscreen
,
222 struct si_shader
*shader
)
224 struct hash_entry
*entry
=
225 _mesa_hash_table_search(sscreen
->shader_cache
, tgsi_binary
);
227 if (sscreen
->b
.disk_shader_cache
) {
228 unsigned char sha1
[CACHE_KEY_SIZE
];
229 size_t tg_size
= *((uint32_t *) tgsi_binary
);
231 disk_cache_compute_key(sscreen
->b
.disk_shader_cache
,
232 tgsi_binary
, tg_size
, sha1
);
236 disk_cache_get(sscreen
->b
.disk_shader_cache
,
241 if (binary_size
< sizeof(uint32_t) ||
242 *((uint32_t*)buffer
) != binary_size
) {
243 /* Something has gone wrong discard the item
244 * from the cache and rebuild/link from
247 assert(!"Invalid radeonsi shader disk cache "
250 disk_cache_remove(sscreen
->b
.disk_shader_cache
,
257 if (!si_load_shader_binary(shader
, buffer
)) {
263 if (!si_shader_cache_insert_shader(sscreen
, tgsi_binary
,
270 if (si_load_shader_binary(shader
, entry
->data
))
275 p_atomic_inc(&sscreen
->b
.num_shader_cache_hits
);
279 static uint32_t si_shader_cache_key_hash(const void *key
)
281 /* The first dword is the key size. */
282 return util_hash_crc32(key
, *(uint32_t*)key
);
285 static bool si_shader_cache_key_equals(const void *a
, const void *b
)
287 uint32_t *keya
= (uint32_t*)a
;
288 uint32_t *keyb
= (uint32_t*)b
;
290 /* The first dword is the key size. */
294 return memcmp(keya
, keyb
, *keya
) == 0;
297 static void si_destroy_shader_cache_entry(struct hash_entry
*entry
)
299 FREE((void*)entry
->key
);
303 bool si_init_shader_cache(struct si_screen
*sscreen
)
305 (void) mtx_init(&sscreen
->shader_cache_mutex
, mtx_plain
);
306 sscreen
->shader_cache
=
307 _mesa_hash_table_create(NULL
,
308 si_shader_cache_key_hash
,
309 si_shader_cache_key_equals
);
311 return sscreen
->shader_cache
!= NULL
;
314 void si_destroy_shader_cache(struct si_screen
*sscreen
)
316 if (sscreen
->shader_cache
)
317 _mesa_hash_table_destroy(sscreen
->shader_cache
,
318 si_destroy_shader_cache_entry
);
319 mtx_destroy(&sscreen
->shader_cache_mutex
);
324 static void si_set_tesseval_regs(struct si_screen
*sscreen
,
325 struct si_shader
*shader
,
326 struct si_pm4_state
*pm4
)
328 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
329 unsigned tes_prim_mode
= info
->properties
[TGSI_PROPERTY_TES_PRIM_MODE
];
330 unsigned tes_spacing
= info
->properties
[TGSI_PROPERTY_TES_SPACING
];
331 bool tes_vertex_order_cw
= info
->properties
[TGSI_PROPERTY_TES_VERTEX_ORDER_CW
];
332 bool tes_point_mode
= info
->properties
[TGSI_PROPERTY_TES_POINT_MODE
];
333 unsigned type
, partitioning
, topology
, distribution_mode
;
335 switch (tes_prim_mode
) {
336 case PIPE_PRIM_LINES
:
337 type
= V_028B6C_TESS_ISOLINE
;
339 case PIPE_PRIM_TRIANGLES
:
340 type
= V_028B6C_TESS_TRIANGLE
;
342 case PIPE_PRIM_QUADS
:
343 type
= V_028B6C_TESS_QUAD
;
350 switch (tes_spacing
) {
351 case PIPE_TESS_SPACING_FRACTIONAL_ODD
:
352 partitioning
= V_028B6C_PART_FRAC_ODD
;
354 case PIPE_TESS_SPACING_FRACTIONAL_EVEN
:
355 partitioning
= V_028B6C_PART_FRAC_EVEN
;
357 case PIPE_TESS_SPACING_EQUAL
:
358 partitioning
= V_028B6C_PART_INTEGER
;
366 topology
= V_028B6C_OUTPUT_POINT
;
367 else if (tes_prim_mode
== PIPE_PRIM_LINES
)
368 topology
= V_028B6C_OUTPUT_LINE
;
369 else if (tes_vertex_order_cw
)
370 /* for some reason, this must be the other way around */
371 topology
= V_028B6C_OUTPUT_TRIANGLE_CCW
;
373 topology
= V_028B6C_OUTPUT_TRIANGLE_CW
;
375 if (sscreen
->has_distributed_tess
) {
376 if (sscreen
->b
.family
== CHIP_FIJI
||
377 sscreen
->b
.family
>= CHIP_POLARIS10
)
378 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_TRAPEZOIDS
;
380 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_DONUTS
;
382 distribution_mode
= V_028B6C_DISTRIBUTION_MODE_NO_DIST
;
384 si_pm4_set_reg(pm4
, R_028B6C_VGT_TF_PARAM
,
385 S_028B6C_TYPE(type
) |
386 S_028B6C_PARTITIONING(partitioning
) |
387 S_028B6C_TOPOLOGY(topology
) |
388 S_028B6C_DISTRIBUTION_MODE(distribution_mode
));
391 /* Polaris needs different VTX_REUSE_DEPTH settings depending on
392 * whether the "fractional odd" tessellation spacing is used.
394 * Possible VGT configurations and which state should set the register:
396 * Reg set in | VGT shader configuration | Value
397 * ------------------------------------------------------
399 * VS as ES | ES -> GS -> VS | 30
400 * TES as VS | LS -> HS -> VS | 14 or 30
401 * TES as ES | LS -> HS -> ES -> GS -> VS | 14 or 30
403 static void polaris_set_vgt_vertex_reuse(struct si_screen
*sscreen
,
404 struct si_shader
*shader
,
405 struct si_pm4_state
*pm4
)
407 unsigned type
= shader
->selector
->type
;
409 if (sscreen
->b
.family
< CHIP_POLARIS10
)
412 /* VS as VS, or VS as ES: */
413 if ((type
== PIPE_SHADER_VERTEX
&&
414 !shader
->key
.as_ls
&&
415 !shader
->is_gs_copy_shader
) ||
416 /* TES as VS, or TES as ES: */
417 type
== PIPE_SHADER_TESS_EVAL
) {
418 unsigned vtx_reuse_depth
= 30;
420 if (type
== PIPE_SHADER_TESS_EVAL
&&
421 shader
->selector
->info
.properties
[TGSI_PROPERTY_TES_SPACING
] ==
422 PIPE_TESS_SPACING_FRACTIONAL_ODD
)
423 vtx_reuse_depth
= 14;
425 si_pm4_set_reg(pm4
, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL
,
430 static struct si_pm4_state
*si_get_shader_pm4_state(struct si_shader
*shader
)
433 si_pm4_clear_state(shader
->pm4
);
435 shader
->pm4
= CALLOC_STRUCT(si_pm4_state
);
440 static void si_shader_ls(struct si_screen
*sscreen
, struct si_shader
*shader
)
442 struct si_pm4_state
*pm4
;
443 unsigned vgpr_comp_cnt
;
446 assert(sscreen
->b
.chip_class
<= VI
);
448 pm4
= si_get_shader_pm4_state(shader
);
452 va
= shader
->bo
->gpu_address
;
453 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
455 /* We need at least 2 components for LS.
456 * VGPR0-3: (VertexID, RelAutoindex, ???, InstanceID). */
457 vgpr_comp_cnt
= shader
->info
.uses_instanceid
? 3 : 1;
459 si_pm4_set_reg(pm4
, R_00B520_SPI_SHADER_PGM_LO_LS
, va
>> 8);
460 si_pm4_set_reg(pm4
, R_00B524_SPI_SHADER_PGM_HI_LS
, va
>> 40);
462 shader
->config
.rsrc1
= S_00B528_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
463 S_00B528_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
464 S_00B528_VGPR_COMP_CNT(vgpr_comp_cnt
) |
465 S_00B528_DX10_CLAMP(1) |
466 S_00B528_FLOAT_MODE(shader
->config
.float_mode
);
467 shader
->config
.rsrc2
= S_00B52C_USER_SGPR(SI_LS_NUM_USER_SGPR
) |
468 S_00B52C_SCRATCH_EN(shader
->config
.scratch_bytes_per_wave
> 0);
471 static void si_shader_hs(struct si_screen
*sscreen
, struct si_shader
*shader
)
473 struct si_pm4_state
*pm4
;
476 pm4
= si_get_shader_pm4_state(shader
);
480 va
= shader
->bo
->gpu_address
;
481 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
483 si_pm4_set_reg(pm4
, R_00B420_SPI_SHADER_PGM_LO_HS
, va
>> 8);
484 si_pm4_set_reg(pm4
, R_00B424_SPI_SHADER_PGM_HI_HS
, va
>> 40);
485 si_pm4_set_reg(pm4
, R_00B428_SPI_SHADER_PGM_RSRC1_HS
,
486 S_00B428_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
487 S_00B428_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
488 S_00B428_DX10_CLAMP(1) |
489 S_00B428_FLOAT_MODE(shader
->config
.float_mode
));
490 si_pm4_set_reg(pm4
, R_00B42C_SPI_SHADER_PGM_RSRC2_HS
,
491 S_00B42C_USER_SGPR(SI_TCS_NUM_USER_SGPR
) |
492 S_00B42C_OC_LDS_EN(sscreen
->b
.chip_class
<= VI
) |
493 S_00B42C_SCRATCH_EN(shader
->config
.scratch_bytes_per_wave
> 0));
496 static void si_shader_es(struct si_screen
*sscreen
, struct si_shader
*shader
)
498 struct si_pm4_state
*pm4
;
499 unsigned num_user_sgprs
;
500 unsigned vgpr_comp_cnt
;
504 assert(sscreen
->b
.chip_class
<= VI
);
506 pm4
= si_get_shader_pm4_state(shader
);
510 va
= shader
->bo
->gpu_address
;
511 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
513 if (shader
->selector
->type
== PIPE_SHADER_VERTEX
) {
514 vgpr_comp_cnt
= shader
->info
.uses_instanceid
? 3 : 0;
515 num_user_sgprs
= SI_ES_NUM_USER_SGPR
;
516 } else if (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
) {
517 vgpr_comp_cnt
= 3; /* all components are needed for TES */
518 num_user_sgprs
= SI_TES_NUM_USER_SGPR
;
520 unreachable("invalid shader selector type");
522 oc_lds_en
= shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
? 1 : 0;
524 si_pm4_set_reg(pm4
, R_028AAC_VGT_ESGS_RING_ITEMSIZE
,
525 shader
->selector
->esgs_itemsize
/ 4);
526 si_pm4_set_reg(pm4
, R_00B320_SPI_SHADER_PGM_LO_ES
, va
>> 8);
527 si_pm4_set_reg(pm4
, R_00B324_SPI_SHADER_PGM_HI_ES
, va
>> 40);
528 si_pm4_set_reg(pm4
, R_00B328_SPI_SHADER_PGM_RSRC1_ES
,
529 S_00B328_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
530 S_00B328_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
531 S_00B328_VGPR_COMP_CNT(vgpr_comp_cnt
) |
532 S_00B328_DX10_CLAMP(1) |
533 S_00B328_FLOAT_MODE(shader
->config
.float_mode
));
534 si_pm4_set_reg(pm4
, R_00B32C_SPI_SHADER_PGM_RSRC2_ES
,
535 S_00B32C_USER_SGPR(num_user_sgprs
) |
536 S_00B32C_OC_LDS_EN(oc_lds_en
) |
537 S_00B32C_SCRATCH_EN(shader
->config
.scratch_bytes_per_wave
> 0));
539 if (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
)
540 si_set_tesseval_regs(sscreen
, shader
, pm4
);
542 polaris_set_vgt_vertex_reuse(sscreen
, shader
, pm4
);
546 * Calculate the appropriate setting of VGT_GS_MODE when \p shader is a
549 static uint32_t si_vgt_gs_mode(struct si_shader_selector
*sel
)
551 unsigned gs_max_vert_out
= sel
->gs_max_out_vertices
;
554 if (gs_max_vert_out
<= 128) {
555 cut_mode
= V_028A40_GS_CUT_128
;
556 } else if (gs_max_vert_out
<= 256) {
557 cut_mode
= V_028A40_GS_CUT_256
;
558 } else if (gs_max_vert_out
<= 512) {
559 cut_mode
= V_028A40_GS_CUT_512
;
561 assert(gs_max_vert_out
<= 1024);
562 cut_mode
= V_028A40_GS_CUT_1024
;
565 return S_028A40_MODE(V_028A40_GS_SCENARIO_G
) |
566 S_028A40_CUT_MODE(cut_mode
)|
567 S_028A40_ES_WRITE_OPTIMIZE(1) |
568 S_028A40_GS_WRITE_OPTIMIZE(1);
571 static void si_shader_gs(struct si_shader
*shader
)
573 struct si_shader_selector
*sel
= shader
->selector
;
574 const ubyte
*num_components
= sel
->info
.num_stream_output_components
;
575 unsigned gs_num_invocations
= sel
->gs_num_invocations
;
576 struct si_pm4_state
*pm4
;
578 unsigned max_stream
= sel
->max_gs_stream
;
581 pm4
= si_get_shader_pm4_state(shader
);
585 si_pm4_set_reg(pm4
, R_028A40_VGT_GS_MODE
, si_vgt_gs_mode(shader
->selector
));
587 offset
= num_components
[0] * sel
->gs_max_out_vertices
;
588 si_pm4_set_reg(pm4
, R_028A60_VGT_GSVS_RING_OFFSET_1
, offset
);
590 offset
+= num_components
[1] * sel
->gs_max_out_vertices
;
591 si_pm4_set_reg(pm4
, R_028A64_VGT_GSVS_RING_OFFSET_2
, offset
);
593 offset
+= num_components
[2] * sel
->gs_max_out_vertices
;
594 si_pm4_set_reg(pm4
, R_028A68_VGT_GSVS_RING_OFFSET_3
, offset
);
596 offset
+= num_components
[3] * sel
->gs_max_out_vertices
;
597 si_pm4_set_reg(pm4
, R_028AB0_VGT_GSVS_RING_ITEMSIZE
, offset
);
599 /* The GSVS_RING_ITEMSIZE register takes 15 bits */
600 assert(offset
< (1 << 15));
602 si_pm4_set_reg(pm4
, R_028B38_VGT_GS_MAX_VERT_OUT
, shader
->selector
->gs_max_out_vertices
);
604 si_pm4_set_reg(pm4
, R_028B5C_VGT_GS_VERT_ITEMSIZE
, num_components
[0]);
605 si_pm4_set_reg(pm4
, R_028B60_VGT_GS_VERT_ITEMSIZE_1
, (max_stream
>= 1) ? num_components
[1] : 0);
606 si_pm4_set_reg(pm4
, R_028B64_VGT_GS_VERT_ITEMSIZE_2
, (max_stream
>= 2) ? num_components
[2] : 0);
607 si_pm4_set_reg(pm4
, R_028B68_VGT_GS_VERT_ITEMSIZE_3
, (max_stream
>= 3) ? num_components
[3] : 0);
609 si_pm4_set_reg(pm4
, R_028B90_VGT_GS_INSTANCE_CNT
,
610 S_028B90_CNT(MIN2(gs_num_invocations
, 127)) |
611 S_028B90_ENABLE(gs_num_invocations
> 0));
613 va
= shader
->bo
->gpu_address
;
614 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
615 si_pm4_set_reg(pm4
, R_00B220_SPI_SHADER_PGM_LO_GS
, va
>> 8);
616 si_pm4_set_reg(pm4
, R_00B224_SPI_SHADER_PGM_HI_GS
, va
>> 40);
618 si_pm4_set_reg(pm4
, R_00B228_SPI_SHADER_PGM_RSRC1_GS
,
619 S_00B228_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
620 S_00B228_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
621 S_00B228_DX10_CLAMP(1) |
622 S_00B228_FLOAT_MODE(shader
->config
.float_mode
));
623 si_pm4_set_reg(pm4
, R_00B22C_SPI_SHADER_PGM_RSRC2_GS
,
624 S_00B22C_USER_SGPR(SI_GS_NUM_USER_SGPR
) |
625 S_00B22C_SCRATCH_EN(shader
->config
.scratch_bytes_per_wave
> 0));
629 * Compute the state for \p shader, which will run as a vertex shader on the
632 * If \p gs is non-NULL, it points to the geometry shader for which this shader
633 * is the copy shader.
635 static void si_shader_vs(struct si_screen
*sscreen
, struct si_shader
*shader
,
636 struct si_shader_selector
*gs
)
638 struct si_pm4_state
*pm4
;
639 unsigned num_user_sgprs
;
640 unsigned nparams
, vgpr_comp_cnt
;
643 unsigned window_space
=
644 shader
->selector
->info
.properties
[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
];
645 bool enable_prim_id
= si_vs_exports_prim_id(shader
);
647 pm4
= si_get_shader_pm4_state(shader
);
651 /* We always write VGT_GS_MODE in the VS state, because every switch
652 * between different shader pipelines involving a different GS or no
653 * GS at all involves a switch of the VS (different GS use different
654 * copy shaders). On the other hand, when the API switches from a GS to
655 * no GS and then back to the same GS used originally, the GS state is
659 si_pm4_set_reg(pm4
, R_028A40_VGT_GS_MODE
,
660 S_028A40_MODE(enable_prim_id
? V_028A40_GS_SCENARIO_A
: 0));
661 si_pm4_set_reg(pm4
, R_028A84_VGT_PRIMITIVEID_EN
, enable_prim_id
);
663 si_pm4_set_reg(pm4
, R_028A40_VGT_GS_MODE
, si_vgt_gs_mode(gs
));
664 si_pm4_set_reg(pm4
, R_028A84_VGT_PRIMITIVEID_EN
, 0);
667 va
= shader
->bo
->gpu_address
;
668 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
671 vgpr_comp_cnt
= 0; /* only VertexID is needed for GS-COPY. */
672 num_user_sgprs
= SI_GSCOPY_NUM_USER_SGPR
;
673 } else if (shader
->selector
->type
== PIPE_SHADER_VERTEX
) {
674 vgpr_comp_cnt
= shader
->info
.uses_instanceid
? 3 : (enable_prim_id
? 2 : 0);
675 num_user_sgprs
= SI_VS_NUM_USER_SGPR
;
676 } else if (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
) {
677 vgpr_comp_cnt
= 3; /* all components are needed for TES */
678 num_user_sgprs
= SI_TES_NUM_USER_SGPR
;
680 unreachable("invalid shader selector type");
682 /* VS is required to export at least one param. */
683 nparams
= MAX2(shader
->info
.nr_param_exports
, 1);
684 si_pm4_set_reg(pm4
, R_0286C4_SPI_VS_OUT_CONFIG
,
685 S_0286C4_VS_EXPORT_COUNT(nparams
- 1));
687 si_pm4_set_reg(pm4
, R_02870C_SPI_SHADER_POS_FORMAT
,
688 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP
) |
689 S_02870C_POS1_EXPORT_FORMAT(shader
->info
.nr_pos_exports
> 1 ?
690 V_02870C_SPI_SHADER_4COMP
:
691 V_02870C_SPI_SHADER_NONE
) |
692 S_02870C_POS2_EXPORT_FORMAT(shader
->info
.nr_pos_exports
> 2 ?
693 V_02870C_SPI_SHADER_4COMP
:
694 V_02870C_SPI_SHADER_NONE
) |
695 S_02870C_POS3_EXPORT_FORMAT(shader
->info
.nr_pos_exports
> 3 ?
696 V_02870C_SPI_SHADER_4COMP
:
697 V_02870C_SPI_SHADER_NONE
));
699 oc_lds_en
= shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
? 1 : 0;
701 si_pm4_set_reg(pm4
, R_00B120_SPI_SHADER_PGM_LO_VS
, va
>> 8);
702 si_pm4_set_reg(pm4
, R_00B124_SPI_SHADER_PGM_HI_VS
, va
>> 40);
703 si_pm4_set_reg(pm4
, R_00B128_SPI_SHADER_PGM_RSRC1_VS
,
704 S_00B128_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
705 S_00B128_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
706 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt
) |
707 S_00B128_DX10_CLAMP(1) |
708 S_00B128_FLOAT_MODE(shader
->config
.float_mode
));
709 si_pm4_set_reg(pm4
, R_00B12C_SPI_SHADER_PGM_RSRC2_VS
,
710 S_00B12C_USER_SGPR(num_user_sgprs
) |
711 S_00B12C_OC_LDS_EN(oc_lds_en
) |
712 S_00B12C_SO_BASE0_EN(!!shader
->selector
->so
.stride
[0]) |
713 S_00B12C_SO_BASE1_EN(!!shader
->selector
->so
.stride
[1]) |
714 S_00B12C_SO_BASE2_EN(!!shader
->selector
->so
.stride
[2]) |
715 S_00B12C_SO_BASE3_EN(!!shader
->selector
->so
.stride
[3]) |
716 S_00B12C_SO_EN(!!shader
->selector
->so
.num_outputs
) |
717 S_00B12C_SCRATCH_EN(shader
->config
.scratch_bytes_per_wave
> 0));
719 si_pm4_set_reg(pm4
, R_028818_PA_CL_VTE_CNTL
,
720 S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1));
722 si_pm4_set_reg(pm4
, R_028818_PA_CL_VTE_CNTL
,
723 S_028818_VTX_W0_FMT(1) |
724 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
725 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
726 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
728 if (shader
->selector
->type
== PIPE_SHADER_TESS_EVAL
)
729 si_set_tesseval_regs(sscreen
, shader
, pm4
);
731 polaris_set_vgt_vertex_reuse(sscreen
, shader
, pm4
);
734 static unsigned si_get_ps_num_interp(struct si_shader
*ps
)
736 struct tgsi_shader_info
*info
= &ps
->selector
->info
;
737 unsigned num_colors
= !!(info
->colors_read
& 0x0f) +
738 !!(info
->colors_read
& 0xf0);
739 unsigned num_interp
= ps
->selector
->info
.num_inputs
+
740 (ps
->key
.part
.ps
.prolog
.color_two_side
? num_colors
: 0);
742 assert(num_interp
<= 32);
743 return MIN2(num_interp
, 32);
746 static unsigned si_get_spi_shader_col_format(struct si_shader
*shader
)
748 unsigned value
= shader
->key
.part
.ps
.epilog
.spi_shader_col_format
;
749 unsigned i
, num_targets
= (util_last_bit(value
) + 3) / 4;
751 /* If the i-th target format is set, all previous target formats must
752 * be non-zero to avoid hangs.
754 for (i
= 0; i
< num_targets
; i
++)
755 if (!(value
& (0xf << (i
* 4))))
756 value
|= V_028714_SPI_SHADER_32_R
<< (i
* 4);
761 static unsigned si_get_cb_shader_mask(unsigned spi_shader_col_format
)
763 unsigned i
, cb_shader_mask
= 0;
765 for (i
= 0; i
< 8; i
++) {
766 switch ((spi_shader_col_format
>> (i
* 4)) & 0xf) {
767 case V_028714_SPI_SHADER_ZERO
:
769 case V_028714_SPI_SHADER_32_R
:
770 cb_shader_mask
|= 0x1 << (i
* 4);
772 case V_028714_SPI_SHADER_32_GR
:
773 cb_shader_mask
|= 0x3 << (i
* 4);
775 case V_028714_SPI_SHADER_32_AR
:
776 cb_shader_mask
|= 0x9 << (i
* 4);
778 case V_028714_SPI_SHADER_FP16_ABGR
:
779 case V_028714_SPI_SHADER_UNORM16_ABGR
:
780 case V_028714_SPI_SHADER_SNORM16_ABGR
:
781 case V_028714_SPI_SHADER_UINT16_ABGR
:
782 case V_028714_SPI_SHADER_SINT16_ABGR
:
783 case V_028714_SPI_SHADER_32_ABGR
:
784 cb_shader_mask
|= 0xf << (i
* 4);
790 return cb_shader_mask
;
793 static void si_shader_ps(struct si_shader
*shader
)
795 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
796 struct si_pm4_state
*pm4
;
797 unsigned spi_ps_in_control
, spi_shader_col_format
, cb_shader_mask
;
798 unsigned spi_baryc_cntl
= S_0286E0_FRONT_FACE_ALL_BITS(1);
800 unsigned input_ena
= shader
->config
.spi_ps_input_ena
;
802 /* we need to enable at least one of them, otherwise we hang the GPU */
803 assert(G_0286CC_PERSP_SAMPLE_ENA(input_ena
) ||
804 G_0286CC_PERSP_CENTER_ENA(input_ena
) ||
805 G_0286CC_PERSP_CENTROID_ENA(input_ena
) ||
806 G_0286CC_PERSP_PULL_MODEL_ENA(input_ena
) ||
807 G_0286CC_LINEAR_SAMPLE_ENA(input_ena
) ||
808 G_0286CC_LINEAR_CENTER_ENA(input_ena
) ||
809 G_0286CC_LINEAR_CENTROID_ENA(input_ena
) ||
810 G_0286CC_LINE_STIPPLE_TEX_ENA(input_ena
));
811 /* POS_W_FLOAT_ENA requires one of the perspective weights. */
812 assert(!G_0286CC_POS_W_FLOAT_ENA(input_ena
) ||
813 G_0286CC_PERSP_SAMPLE_ENA(input_ena
) ||
814 G_0286CC_PERSP_CENTER_ENA(input_ena
) ||
815 G_0286CC_PERSP_CENTROID_ENA(input_ena
) ||
816 G_0286CC_PERSP_PULL_MODEL_ENA(input_ena
));
818 /* Validate interpolation optimization flags (read as implications). */
819 assert(!shader
->key
.part
.ps
.prolog
.bc_optimize_for_persp
||
820 (G_0286CC_PERSP_CENTER_ENA(input_ena
) &&
821 G_0286CC_PERSP_CENTROID_ENA(input_ena
)));
822 assert(!shader
->key
.part
.ps
.prolog
.bc_optimize_for_linear
||
823 (G_0286CC_LINEAR_CENTER_ENA(input_ena
) &&
824 G_0286CC_LINEAR_CENTROID_ENA(input_ena
)));
825 assert(!shader
->key
.part
.ps
.prolog
.force_persp_center_interp
||
826 (!G_0286CC_PERSP_SAMPLE_ENA(input_ena
) &&
827 !G_0286CC_PERSP_CENTROID_ENA(input_ena
)));
828 assert(!shader
->key
.part
.ps
.prolog
.force_linear_center_interp
||
829 (!G_0286CC_LINEAR_SAMPLE_ENA(input_ena
) &&
830 !G_0286CC_LINEAR_CENTROID_ENA(input_ena
)));
831 assert(!shader
->key
.part
.ps
.prolog
.force_persp_sample_interp
||
832 (!G_0286CC_PERSP_CENTER_ENA(input_ena
) &&
833 !G_0286CC_PERSP_CENTROID_ENA(input_ena
)));
834 assert(!shader
->key
.part
.ps
.prolog
.force_linear_sample_interp
||
835 (!G_0286CC_LINEAR_CENTER_ENA(input_ena
) &&
836 !G_0286CC_LINEAR_CENTROID_ENA(input_ena
)));
838 /* Validate cases when the optimizations are off (read as implications). */
839 assert(shader
->key
.part
.ps
.prolog
.bc_optimize_for_persp
||
840 !G_0286CC_PERSP_CENTER_ENA(input_ena
) ||
841 !G_0286CC_PERSP_CENTROID_ENA(input_ena
));
842 assert(shader
->key
.part
.ps
.prolog
.bc_optimize_for_linear
||
843 !G_0286CC_LINEAR_CENTER_ENA(input_ena
) ||
844 !G_0286CC_LINEAR_CENTROID_ENA(input_ena
));
846 pm4
= si_get_shader_pm4_state(shader
);
850 /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION
852 * 0 -> Position = pixel center
853 * 1 -> Position = pixel centroid
854 * 2 -> Position = at sample position
856 * From GLSL 4.5 specification, section 7.1:
857 * "The variable gl_FragCoord is available as an input variable from
858 * within fragment shaders and it holds the window relative coordinates
859 * (x, y, z, 1/w) values for the fragment. If multi-sampling, this
860 * value can be for any location within the pixel, or one of the
861 * fragment samples. The use of centroid does not further restrict
862 * this value to be inside the current primitive."
864 * Meaning that centroid has no effect and we can return anything within
865 * the pixel. Thus, return the value at sample position, because that's
866 * the most accurate one shaders can get.
868 spi_baryc_cntl
|= S_0286E0_POS_FLOAT_LOCATION(2);
870 if (info
->properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
871 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)
872 spi_baryc_cntl
|= S_0286E0_POS_FLOAT_ULC(1);
874 spi_shader_col_format
= si_get_spi_shader_col_format(shader
);
875 cb_shader_mask
= si_get_cb_shader_mask(spi_shader_col_format
);
877 /* Ensure that some export memory is always allocated, for two reasons:
879 * 1) Correctness: The hardware ignores the EXEC mask if no export
880 * memory is allocated, so KILL and alpha test do not work correctly
882 * 2) Performance: Every shader needs at least a NULL export, even when
883 * it writes no color/depth output. The NULL export instruction
884 * stalls without this setting.
886 * Don't add this to CB_SHADER_MASK.
888 if (!spi_shader_col_format
&&
889 !info
->writes_z
&& !info
->writes_stencil
&& !info
->writes_samplemask
)
890 spi_shader_col_format
= V_028714_SPI_SHADER_32_R
;
892 si_pm4_set_reg(pm4
, R_0286CC_SPI_PS_INPUT_ENA
, input_ena
);
893 si_pm4_set_reg(pm4
, R_0286D0_SPI_PS_INPUT_ADDR
,
894 shader
->config
.spi_ps_input_addr
);
896 /* Set interpolation controls. */
897 spi_ps_in_control
= S_0286D8_NUM_INTERP(si_get_ps_num_interp(shader
));
900 si_pm4_set_reg(pm4
, R_0286E0_SPI_BARYC_CNTL
, spi_baryc_cntl
);
901 si_pm4_set_reg(pm4
, R_0286D8_SPI_PS_IN_CONTROL
, spi_ps_in_control
);
903 si_pm4_set_reg(pm4
, R_028710_SPI_SHADER_Z_FORMAT
,
904 si_get_spi_shader_z_format(info
->writes_z
,
905 info
->writes_stencil
,
906 info
->writes_samplemask
));
908 si_pm4_set_reg(pm4
, R_028714_SPI_SHADER_COL_FORMAT
, spi_shader_col_format
);
909 si_pm4_set_reg(pm4
, R_02823C_CB_SHADER_MASK
, cb_shader_mask
);
911 va
= shader
->bo
->gpu_address
;
912 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_BINARY
);
913 si_pm4_set_reg(pm4
, R_00B020_SPI_SHADER_PGM_LO_PS
, va
>> 8);
914 si_pm4_set_reg(pm4
, R_00B024_SPI_SHADER_PGM_HI_PS
, va
>> 40);
916 si_pm4_set_reg(pm4
, R_00B028_SPI_SHADER_PGM_RSRC1_PS
,
917 S_00B028_VGPRS((shader
->config
.num_vgprs
- 1) / 4) |
918 S_00B028_SGPRS((shader
->config
.num_sgprs
- 1) / 8) |
919 S_00B028_DX10_CLAMP(1) |
920 S_00B028_FLOAT_MODE(shader
->config
.float_mode
));
921 si_pm4_set_reg(pm4
, R_00B02C_SPI_SHADER_PGM_RSRC2_PS
,
922 S_00B02C_EXTRA_LDS_SIZE(shader
->config
.lds_size
) |
923 S_00B02C_USER_SGPR(SI_PS_NUM_USER_SGPR
) |
924 S_00B32C_SCRATCH_EN(shader
->config
.scratch_bytes_per_wave
> 0));
927 static void si_shader_init_pm4_state(struct si_screen
*sscreen
,
928 struct si_shader
*shader
)
930 switch (shader
->selector
->type
) {
931 case PIPE_SHADER_VERTEX
:
932 if (shader
->key
.as_ls
)
933 si_shader_ls(sscreen
, shader
);
934 else if (shader
->key
.as_es
)
935 si_shader_es(sscreen
, shader
);
937 si_shader_vs(sscreen
, shader
, NULL
);
939 case PIPE_SHADER_TESS_CTRL
:
940 si_shader_hs(sscreen
, shader
);
942 case PIPE_SHADER_TESS_EVAL
:
943 if (shader
->key
.as_es
)
944 si_shader_es(sscreen
, shader
);
946 si_shader_vs(sscreen
, shader
, NULL
);
948 case PIPE_SHADER_GEOMETRY
:
949 si_shader_gs(shader
);
951 case PIPE_SHADER_FRAGMENT
:
952 si_shader_ps(shader
);
959 static unsigned si_get_alpha_test_func(struct si_context
*sctx
)
961 /* Alpha-test should be disabled if colorbuffer 0 is integer. */
962 if (sctx
->queued
.named
.dsa
)
963 return sctx
->queued
.named
.dsa
->alpha_func
;
965 return PIPE_FUNC_ALWAYS
;
968 static void si_shader_selector_key_hw_vs(struct si_context
*sctx
,
969 struct si_shader_selector
*vs
,
970 struct si_shader_key
*key
)
972 struct si_shader_selector
*ps
= sctx
->ps_shader
.cso
;
974 key
->opt
.hw_vs
.clip_disable
=
975 sctx
->queued
.named
.rasterizer
->clip_plane_enable
== 0 &&
976 (vs
->info
.clipdist_writemask
||
977 vs
->info
.writes_clipvertex
) &&
978 !vs
->info
.culldist_writemask
;
980 /* Find out if PS is disabled. */
981 bool ps_disabled
= true;
983 bool ps_modifies_zs
= ps
->info
.uses_kill
||
985 ps
->info
.writes_stencil
||
986 ps
->info
.writes_samplemask
||
987 si_get_alpha_test_func(sctx
) != PIPE_FUNC_ALWAYS
;
989 unsigned ps_colormask
= sctx
->framebuffer
.colorbuf_enabled_4bit
&
990 sctx
->queued
.named
.blend
->cb_target_mask
;
991 if (!ps
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
])
992 ps_colormask
&= ps
->colors_written_4bit
;
994 ps_disabled
= sctx
->queued
.named
.rasterizer
->rasterizer_discard
||
997 !ps
->info
.writes_memory
);
1000 /* Find out which VS outputs aren't used by the PS. */
1001 uint64_t outputs_written
= vs
->outputs_written
;
1002 uint32_t outputs_written2
= vs
->outputs_written2
;
1003 uint64_t inputs_read
= 0;
1004 uint32_t inputs_read2
= 0;
1006 outputs_written
&= ~0x3; /* ignore POSITION, PSIZE */
1009 inputs_read
= ps
->inputs_read
;
1010 inputs_read2
= ps
->inputs_read2
;
1013 uint64_t linked
= outputs_written
& inputs_read
;
1014 uint32_t linked2
= outputs_written2
& inputs_read2
;
1016 key
->opt
.hw_vs
.kill_outputs
= ~linked
& outputs_written
;
1017 key
->opt
.hw_vs
.kill_outputs2
= ~linked2
& outputs_written2
;
1020 /* Compute the key for the hw shader variant */
1021 static inline void si_shader_selector_key(struct pipe_context
*ctx
,
1022 struct si_shader_selector
*sel
,
1023 struct si_shader_key
*key
)
1025 struct si_context
*sctx
= (struct si_context
*)ctx
;
1028 memset(key
, 0, sizeof(*key
));
1030 switch (sel
->type
) {
1031 case PIPE_SHADER_VERTEX
:
1032 if (sctx
->vertex_elements
) {
1033 unsigned count
= MIN2(sel
->info
.num_inputs
,
1034 sctx
->vertex_elements
->count
);
1035 for (i
= 0; i
< count
; ++i
)
1036 key
->part
.vs
.prolog
.instance_divisors
[i
] =
1037 sctx
->vertex_elements
->elements
[i
].instance_divisor
;
1039 memcpy(key
->mono
.vs
.fix_fetch
,
1040 sctx
->vertex_elements
->fix_fetch
, count
);
1042 if (sctx
->tes_shader
.cso
)
1044 else if (sctx
->gs_shader
.cso
)
1047 si_shader_selector_key_hw_vs(sctx
, sel
, key
);
1049 if (sctx
->ps_shader
.cso
&& sctx
->ps_shader
.cso
->info
.uses_primid
)
1050 key
->part
.vs
.epilog
.export_prim_id
= 1;
1053 case PIPE_SHADER_TESS_CTRL
:
1054 key
->part
.tcs
.epilog
.prim_mode
=
1055 sctx
->tes_shader
.cso
->info
.properties
[TGSI_PROPERTY_TES_PRIM_MODE
];
1056 key
->part
.tcs
.epilog
.tes_reads_tess_factors
=
1057 sctx
->tes_shader
.cso
->info
.reads_tess_factors
;
1059 if (sel
== sctx
->fixed_func_tcs_shader
.cso
)
1060 key
->mono
.tcs
.inputs_to_copy
= sctx
->vs_shader
.cso
->outputs_written
;
1062 case PIPE_SHADER_TESS_EVAL
:
1063 if (sctx
->gs_shader
.cso
)
1066 si_shader_selector_key_hw_vs(sctx
, sel
, key
);
1068 if (sctx
->ps_shader
.cso
&& sctx
->ps_shader
.cso
->info
.uses_primid
)
1069 key
->part
.tes
.epilog
.export_prim_id
= 1;
1072 case PIPE_SHADER_GEOMETRY
:
1073 key
->part
.gs
.prolog
.tri_strip_adj_fix
= sctx
->gs_tri_strip_adj_fix
;
1075 case PIPE_SHADER_FRAGMENT
: {
1076 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
1077 struct si_state_blend
*blend
= sctx
->queued
.named
.blend
;
1079 if (sel
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1080 sel
->info
.colors_written
== 0x1)
1081 key
->part
.ps
.epilog
.last_cbuf
= MAX2(sctx
->framebuffer
.state
.nr_cbufs
, 1) - 1;
1084 /* Select the shader color format based on whether
1085 * blending or alpha are needed.
1087 key
->part
.ps
.epilog
.spi_shader_col_format
=
1088 (blend
->blend_enable_4bit
& blend
->need_src_alpha_4bit
&
1089 sctx
->framebuffer
.spi_shader_col_format_blend_alpha
) |
1090 (blend
->blend_enable_4bit
& ~blend
->need_src_alpha_4bit
&
1091 sctx
->framebuffer
.spi_shader_col_format_blend
) |
1092 (~blend
->blend_enable_4bit
& blend
->need_src_alpha_4bit
&
1093 sctx
->framebuffer
.spi_shader_col_format_alpha
) |
1094 (~blend
->blend_enable_4bit
& ~blend
->need_src_alpha_4bit
&
1095 sctx
->framebuffer
.spi_shader_col_format
);
1097 /* The output for dual source blending should have
1098 * the same format as the first output.
1100 if (blend
->dual_src_blend
)
1101 key
->part
.ps
.epilog
.spi_shader_col_format
|=
1102 (key
->part
.ps
.epilog
.spi_shader_col_format
& 0xf) << 4;
1104 key
->part
.ps
.epilog
.spi_shader_col_format
= sctx
->framebuffer
.spi_shader_col_format
;
1106 /* If alpha-to-coverage is enabled, we have to export alpha
1107 * even if there is no color buffer.
1109 if (!(key
->part
.ps
.epilog
.spi_shader_col_format
& 0xf) &&
1110 blend
&& blend
->alpha_to_coverage
)
1111 key
->part
.ps
.epilog
.spi_shader_col_format
|= V_028710_SPI_SHADER_32_AR
;
1113 /* On SI and CIK except Hawaii, the CB doesn't clamp outputs
1114 * to the range supported by the type if a channel has less
1115 * than 16 bits and the export format is 16_ABGR.
1117 if (sctx
->b
.chip_class
<= CIK
&& sctx
->b
.family
!= CHIP_HAWAII
) {
1118 key
->part
.ps
.epilog
.color_is_int8
= sctx
->framebuffer
.color_is_int8
;
1119 key
->part
.ps
.epilog
.color_is_int10
= sctx
->framebuffer
.color_is_int10
;
1122 /* Disable unwritten outputs (if WRITE_ALL_CBUFS isn't enabled). */
1123 if (!key
->part
.ps
.epilog
.last_cbuf
) {
1124 key
->part
.ps
.epilog
.spi_shader_col_format
&= sel
->colors_written_4bit
;
1125 key
->part
.ps
.epilog
.color_is_int8
&= sel
->info
.colors_written
;
1126 key
->part
.ps
.epilog
.color_is_int10
&= sel
->info
.colors_written
;
1130 bool is_poly
= (sctx
->current_rast_prim
>= PIPE_PRIM_TRIANGLES
&&
1131 sctx
->current_rast_prim
<= PIPE_PRIM_POLYGON
) ||
1132 sctx
->current_rast_prim
>= PIPE_PRIM_TRIANGLES_ADJACENCY
;
1133 bool is_line
= !is_poly
&& sctx
->current_rast_prim
!= PIPE_PRIM_POINTS
;
1135 key
->part
.ps
.prolog
.color_two_side
= rs
->two_side
&& sel
->info
.colors_read
;
1136 key
->part
.ps
.prolog
.flatshade_colors
= rs
->flatshade
&& sel
->info
.colors_read
;
1138 if (sctx
->queued
.named
.blend
) {
1139 key
->part
.ps
.epilog
.alpha_to_one
= sctx
->queued
.named
.blend
->alpha_to_one
&&
1140 rs
->multisample_enable
;
1143 key
->part
.ps
.prolog
.poly_stipple
= rs
->poly_stipple_enable
&& is_poly
;
1144 key
->part
.ps
.epilog
.poly_line_smoothing
= ((is_poly
&& rs
->poly_smooth
) ||
1145 (is_line
&& rs
->line_smooth
)) &&
1146 sctx
->framebuffer
.nr_samples
<= 1;
1147 key
->part
.ps
.epilog
.clamp_color
= rs
->clamp_fragment_color
;
1149 if (rs
->force_persample_interp
&&
1150 rs
->multisample_enable
&&
1151 sctx
->framebuffer
.nr_samples
> 1 &&
1152 sctx
->ps_iter_samples
> 1) {
1153 key
->part
.ps
.prolog
.force_persp_sample_interp
=
1154 sel
->info
.uses_persp_center
||
1155 sel
->info
.uses_persp_centroid
;
1157 key
->part
.ps
.prolog
.force_linear_sample_interp
=
1158 sel
->info
.uses_linear_center
||
1159 sel
->info
.uses_linear_centroid
;
1160 } else if (rs
->multisample_enable
&&
1161 sctx
->framebuffer
.nr_samples
> 1) {
1162 key
->part
.ps
.prolog
.bc_optimize_for_persp
=
1163 sel
->info
.uses_persp_center
&&
1164 sel
->info
.uses_persp_centroid
;
1165 key
->part
.ps
.prolog
.bc_optimize_for_linear
=
1166 sel
->info
.uses_linear_center
&&
1167 sel
->info
.uses_linear_centroid
;
1169 /* Make sure SPI doesn't compute more than 1 pair
1170 * of (i,j), which is the optimization here. */
1171 key
->part
.ps
.prolog
.force_persp_center_interp
=
1172 sel
->info
.uses_persp_center
+
1173 sel
->info
.uses_persp_centroid
+
1174 sel
->info
.uses_persp_sample
> 1;
1176 key
->part
.ps
.prolog
.force_linear_center_interp
=
1177 sel
->info
.uses_linear_center
+
1178 sel
->info
.uses_linear_centroid
+
1179 sel
->info
.uses_linear_sample
> 1;
1183 key
->part
.ps
.epilog
.alpha_func
= si_get_alpha_test_func(sctx
);
1191 static void si_build_shader_variant(void *job
, int thread_index
)
1193 struct si_shader
*shader
= (struct si_shader
*)job
;
1194 struct si_shader_selector
*sel
= shader
->selector
;
1195 struct si_screen
*sscreen
= sel
->screen
;
1196 LLVMTargetMachineRef tm
;
1197 struct pipe_debug_callback
*debug
= &shader
->compiler_ctx_state
.debug
;
1200 if (thread_index
>= 0) {
1201 assert(thread_index
< ARRAY_SIZE(sscreen
->tm
));
1202 tm
= sscreen
->tm
[thread_index
];
1206 tm
= shader
->compiler_ctx_state
.tm
;
1209 r
= si_shader_create(sscreen
, tm
, shader
, debug
);
1211 R600_ERR("Failed to build shader variant (type=%u) %d\n",
1213 shader
->compilation_failed
= true;
1217 if (shader
->compiler_ctx_state
.is_debug_context
) {
1218 FILE *f
= open_memstream(&shader
->shader_log
,
1219 &shader
->shader_log_size
);
1221 si_shader_dump(sscreen
, shader
, NULL
, sel
->type
, f
, false);
1226 si_shader_init_pm4_state(sscreen
, shader
);
1229 /* Select the hw shader variant depending on the current state. */
1230 static int si_shader_select_with_key(struct si_screen
*sscreen
,
1231 struct si_shader_ctx_state
*state
,
1232 struct si_compiler_ctx_state
*compiler_state
,
1233 struct si_shader_key
*key
,
1236 static const struct si_shader_key zeroed
;
1237 struct si_shader_selector
*sel
= state
->cso
;
1238 struct si_shader
*current
= state
->current
;
1239 struct si_shader
*iter
, *shader
= NULL
;
1241 if (unlikely(sscreen
->b
.debug_flags
& DBG_NO_OPT_VARIANT
)) {
1242 memset(&key
->opt
, 0, sizeof(key
->opt
));
1246 /* Check if we don't need to change anything.
1247 * This path is also used for most shaders that don't need multiple
1248 * variants, it will cost just a computation of the key and this
1250 if (likely(current
&&
1251 memcmp(¤t
->key
, key
, sizeof(*key
)) == 0 &&
1252 (!current
->is_optimized
||
1253 util_queue_fence_is_signalled(¤t
->optimized_ready
))))
1254 return current
->compilation_failed
? -1 : 0;
1256 /* This must be done before the mutex is locked, because async GS
1257 * compilation calls this function too, and therefore must enter
1260 * Only wait if we are in a draw call. Don't wait if we are
1261 * in a compiler thread.
1263 if (thread_index
< 0)
1264 util_queue_fence_wait(&sel
->ready
);
1266 mtx_lock(&sel
->mutex
);
1268 /* Find the shader variant. */
1269 for (iter
= sel
->first_variant
; iter
; iter
= iter
->next_variant
) {
1270 /* Don't check the "current" shader. We checked it above. */
1271 if (current
!= iter
&&
1272 memcmp(&iter
->key
, key
, sizeof(*key
)) == 0) {
1273 /* If it's an optimized shader and its compilation has
1274 * been started but isn't done, use the unoptimized
1275 * shader so as not to cause a stall due to compilation.
1277 if (iter
->is_optimized
&&
1278 !util_queue_fence_is_signalled(&iter
->optimized_ready
)) {
1279 memset(&key
->opt
, 0, sizeof(key
->opt
));
1280 mtx_unlock(&sel
->mutex
);
1284 if (iter
->compilation_failed
) {
1285 mtx_unlock(&sel
->mutex
);
1286 return -1; /* skip the draw call */
1289 state
->current
= iter
;
1290 mtx_unlock(&sel
->mutex
);
1295 /* Build a new shader. */
1296 shader
= CALLOC_STRUCT(si_shader
);
1298 mtx_unlock(&sel
->mutex
);
1301 shader
->selector
= sel
;
1303 shader
->compiler_ctx_state
= *compiler_state
;
1305 /* Compile the main shader part if it doesn't exist. This can happen
1306 * if the initial guess was wrong. */
1307 struct si_shader
**mainp
= si_get_main_shader_part(sel
, key
);
1308 bool is_pure_monolithic
=
1309 sscreen
->use_monolithic_shaders
||
1310 memcmp(&key
->mono
, &zeroed
.mono
, sizeof(key
->mono
)) != 0;
1312 if (!*mainp
&& !is_pure_monolithic
) {
1313 struct si_shader
*main_part
= CALLOC_STRUCT(si_shader
);
1317 mtx_unlock(&sel
->mutex
);
1318 return -ENOMEM
; /* skip the draw call */
1321 main_part
->selector
= sel
;
1322 main_part
->key
.as_es
= key
->as_es
;
1323 main_part
->key
.as_ls
= key
->as_ls
;
1325 if (si_compile_tgsi_shader(sscreen
, compiler_state
->tm
,
1327 &compiler_state
->debug
) != 0) {
1330 mtx_unlock(&sel
->mutex
);
1331 return -ENOMEM
; /* skip the draw call */
1336 /* Monolithic-only shaders don't make a distinction between optimized
1337 * and unoptimized. */
1338 shader
->is_monolithic
=
1339 is_pure_monolithic
||
1340 memcmp(&key
->opt
, &zeroed
.opt
, sizeof(key
->opt
)) != 0;
1342 shader
->is_optimized
=
1343 !is_pure_monolithic
&&
1344 memcmp(&key
->opt
, &zeroed
.opt
, sizeof(key
->opt
)) != 0;
1345 if (shader
->is_optimized
)
1346 util_queue_fence_init(&shader
->optimized_ready
);
1348 if (!sel
->last_variant
) {
1349 sel
->first_variant
= shader
;
1350 sel
->last_variant
= shader
;
1352 sel
->last_variant
->next_variant
= shader
;
1353 sel
->last_variant
= shader
;
1356 /* If it's an optimized shader, compile it asynchronously. */
1357 if (shader
->is_optimized
&&
1358 !is_pure_monolithic
&&
1360 /* Compile it asynchronously. */
1361 util_queue_add_job(&sscreen
->shader_compiler_queue
,
1362 shader
, &shader
->optimized_ready
,
1363 si_build_shader_variant
, NULL
);
1365 /* Use the default (unoptimized) shader for now. */
1366 memset(&key
->opt
, 0, sizeof(key
->opt
));
1367 mtx_unlock(&sel
->mutex
);
1371 assert(!shader
->is_optimized
);
1372 si_build_shader_variant(shader
, thread_index
);
1374 if (!shader
->compilation_failed
)
1375 state
->current
= shader
;
1377 mtx_unlock(&sel
->mutex
);
1378 return shader
->compilation_failed
? -1 : 0;
1381 static int si_shader_select(struct pipe_context
*ctx
,
1382 struct si_shader_ctx_state
*state
,
1383 struct si_compiler_ctx_state
*compiler_state
)
1385 struct si_context
*sctx
= (struct si_context
*)ctx
;
1386 struct si_shader_key key
;
1388 si_shader_selector_key(ctx
, state
->cso
, &key
);
1389 return si_shader_select_with_key(sctx
->screen
, state
, compiler_state
,
1393 static void si_parse_next_shader_property(const struct tgsi_shader_info
*info
,
1394 struct si_shader_key
*key
)
1396 unsigned next_shader
= info
->properties
[TGSI_PROPERTY_NEXT_SHADER
];
1398 switch (info
->processor
) {
1399 case PIPE_SHADER_VERTEX
:
1400 switch (next_shader
) {
1401 case PIPE_SHADER_GEOMETRY
:
1404 case PIPE_SHADER_TESS_CTRL
:
1405 case PIPE_SHADER_TESS_EVAL
:
1409 /* If POSITION isn't written, it can't be a HW VS.
1410 * Assume that it's a HW LS. (the next shader is TCS)
1411 * This heuristic is needed for separate shader objects.
1413 if (!info
->writes_position
)
1418 case PIPE_SHADER_TESS_EVAL
:
1419 if (next_shader
== PIPE_SHADER_GEOMETRY
||
1420 !info
->writes_position
)
1427 * Compile the main shader part or the monolithic shader as part of
1428 * si_shader_selector initialization. Since it can be done asynchronously,
1429 * there is no way to report compile failures to applications.
1431 void si_init_shader_selector_async(void *job
, int thread_index
)
1433 struct si_shader_selector
*sel
= (struct si_shader_selector
*)job
;
1434 struct si_screen
*sscreen
= sel
->screen
;
1435 LLVMTargetMachineRef tm
;
1436 struct pipe_debug_callback
*debug
= &sel
->compiler_ctx_state
.debug
;
1439 if (thread_index
>= 0) {
1440 assert(thread_index
< ARRAY_SIZE(sscreen
->tm
));
1441 tm
= sscreen
->tm
[thread_index
];
1445 tm
= sel
->compiler_ctx_state
.tm
;
1448 /* Compile the main shader part for use with a prolog and/or epilog.
1449 * If this fails, the driver will try to compile a monolithic shader
1452 if (!sscreen
->use_monolithic_shaders
) {
1453 struct si_shader
*shader
= CALLOC_STRUCT(si_shader
);
1457 fprintf(stderr
, "radeonsi: can't allocate a main shader part\n");
1461 shader
->selector
= sel
;
1462 si_parse_next_shader_property(&sel
->info
, &shader
->key
);
1464 tgsi_binary
= si_get_tgsi_binary(sel
);
1466 /* Try to load the shader from the shader cache. */
1467 mtx_lock(&sscreen
->shader_cache_mutex
);
1470 si_shader_cache_load_shader(sscreen
, tgsi_binary
, shader
)) {
1471 mtx_unlock(&sscreen
->shader_cache_mutex
);
1473 mtx_unlock(&sscreen
->shader_cache_mutex
);
1475 /* Compile the shader if it hasn't been loaded from the cache. */
1476 if (si_compile_tgsi_shader(sscreen
, tm
, shader
, false,
1480 fprintf(stderr
, "radeonsi: can't compile a main shader part\n");
1485 mtx_lock(&sscreen
->shader_cache_mutex
);
1486 if (!si_shader_cache_insert_shader(sscreen
, tgsi_binary
, shader
, true))
1488 mtx_unlock(&sscreen
->shader_cache_mutex
);
1492 *si_get_main_shader_part(sel
, &shader
->key
) = shader
;
1494 /* Unset "outputs_written" flags for outputs converted to
1495 * DEFAULT_VAL, so that later inter-shader optimizations don't
1496 * try to eliminate outputs that don't exist in the final
1499 * This is only done if non-monolithic shaders are enabled.
1501 if ((sel
->type
== PIPE_SHADER_VERTEX
||
1502 sel
->type
== PIPE_SHADER_TESS_EVAL
) &&
1503 !shader
->key
.as_ls
&&
1504 !shader
->key
.as_es
) {
1507 for (i
= 0; i
< sel
->info
.num_outputs
; i
++) {
1508 unsigned offset
= shader
->info
.vs_output_param_offset
[i
];
1510 if (offset
<= EXP_PARAM_OFFSET_31
)
1513 unsigned name
= sel
->info
.output_semantic_name
[i
];
1514 unsigned index
= sel
->info
.output_semantic_index
[i
];
1518 case TGSI_SEMANTIC_GENERIC
:
1519 /* don't process indices the function can't handle */
1523 case TGSI_SEMANTIC_CLIPDIST
:
1524 id
= si_shader_io_get_unique_index(name
, index
);
1525 sel
->outputs_written
&= ~(1ull << id
);
1527 case TGSI_SEMANTIC_POSITION
: /* ignore these */
1528 case TGSI_SEMANTIC_PSIZE
:
1529 case TGSI_SEMANTIC_CLIPVERTEX
:
1530 case TGSI_SEMANTIC_EDGEFLAG
:
1533 id
= si_shader_io_get_unique_index2(name
, index
);
1534 sel
->outputs_written2
&= ~(1u << id
);
1540 /* Pre-compilation. */
1541 if (sscreen
->b
.debug_flags
& DBG_PRECOMPILE
) {
1542 struct si_shader_ctx_state state
= {sel
};
1543 struct si_shader_key key
;
1545 memset(&key
, 0, sizeof(key
));
1546 si_parse_next_shader_property(&sel
->info
, &key
);
1548 /* Set reasonable defaults, so that the shader key doesn't
1549 * cause any code to be eliminated.
1551 switch (sel
->type
) {
1552 case PIPE_SHADER_TESS_CTRL
:
1553 key
.part
.tcs
.epilog
.prim_mode
= PIPE_PRIM_TRIANGLES
;
1555 case PIPE_SHADER_FRAGMENT
:
1556 key
.part
.ps
.prolog
.bc_optimize_for_persp
=
1557 sel
->info
.uses_persp_center
&&
1558 sel
->info
.uses_persp_centroid
;
1559 key
.part
.ps
.prolog
.bc_optimize_for_linear
=
1560 sel
->info
.uses_linear_center
&&
1561 sel
->info
.uses_linear_centroid
;
1562 key
.part
.ps
.epilog
.alpha_func
= PIPE_FUNC_ALWAYS
;
1563 for (i
= 0; i
< 8; i
++)
1564 if (sel
->info
.colors_written
& (1 << i
))
1565 key
.part
.ps
.epilog
.spi_shader_col_format
|=
1566 V_028710_SPI_SHADER_FP16_ABGR
<< (i
* 4);
1570 if (si_shader_select_with_key(sscreen
, &state
,
1571 &sel
->compiler_ctx_state
, &key
,
1573 fprintf(stderr
, "radeonsi: can't create a monolithic shader\n");
1576 /* The GS copy shader is always pre-compiled. */
1577 if (sel
->type
== PIPE_SHADER_GEOMETRY
) {
1578 sel
->gs_copy_shader
= si_generate_gs_copy_shader(sscreen
, tm
, sel
, debug
);
1579 if (!sel
->gs_copy_shader
) {
1580 fprintf(stderr
, "radeonsi: can't create GS copy shader\n");
1584 si_shader_vs(sscreen
, sel
->gs_copy_shader
, sel
);
1588 static void *si_create_shader_selector(struct pipe_context
*ctx
,
1589 const struct pipe_shader_state
*state
)
1591 struct si_screen
*sscreen
= (struct si_screen
*)ctx
->screen
;
1592 struct si_context
*sctx
= (struct si_context
*)ctx
;
1593 struct si_shader_selector
*sel
= CALLOC_STRUCT(si_shader_selector
);
1599 sel
->screen
= sscreen
;
1600 sel
->compiler_ctx_state
.tm
= sctx
->tm
;
1601 sel
->compiler_ctx_state
.debug
= sctx
->b
.debug
;
1602 sel
->compiler_ctx_state
.is_debug_context
= sctx
->is_debug
;
1603 sel
->tokens
= tgsi_dup_tokens(state
->tokens
);
1609 sel
->so
= state
->stream_output
;
1610 tgsi_scan_shader(state
->tokens
, &sel
->info
);
1611 sel
->type
= sel
->info
.processor
;
1612 p_atomic_inc(&sscreen
->b
.num_shaders_created
);
1614 /* Set which opcode uses which (i,j) pair. */
1615 if (sel
->info
.uses_persp_opcode_interp_centroid
)
1616 sel
->info
.uses_persp_centroid
= true;
1618 if (sel
->info
.uses_linear_opcode_interp_centroid
)
1619 sel
->info
.uses_linear_centroid
= true;
1621 if (sel
->info
.uses_persp_opcode_interp_offset
||
1622 sel
->info
.uses_persp_opcode_interp_sample
)
1623 sel
->info
.uses_persp_center
= true;
1625 if (sel
->info
.uses_linear_opcode_interp_offset
||
1626 sel
->info
.uses_linear_opcode_interp_sample
)
1627 sel
->info
.uses_linear_center
= true;
1629 switch (sel
->type
) {
1630 case PIPE_SHADER_GEOMETRY
:
1631 sel
->gs_output_prim
=
1632 sel
->info
.properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
];
1633 sel
->gs_max_out_vertices
=
1634 sel
->info
.properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
1635 sel
->gs_num_invocations
=
1636 sel
->info
.properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
1637 sel
->gsvs_vertex_size
= sel
->info
.num_outputs
* 16;
1638 sel
->max_gsvs_emit_size
= sel
->gsvs_vertex_size
*
1639 sel
->gs_max_out_vertices
;
1641 sel
->max_gs_stream
= 0;
1642 for (i
= 0; i
< sel
->so
.num_outputs
; i
++)
1643 sel
->max_gs_stream
= MAX2(sel
->max_gs_stream
,
1644 sel
->so
.output
[i
].stream
);
1646 sel
->gs_input_verts_per_prim
=
1647 u_vertices_per_prim(sel
->info
.properties
[TGSI_PROPERTY_GS_INPUT_PRIM
]);
1650 case PIPE_SHADER_TESS_CTRL
:
1651 /* Always reserve space for these. */
1652 sel
->patch_outputs_written
|=
1653 (1llu << si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSINNER
, 0)) |
1654 (1llu << si_shader_io_get_unique_index(TGSI_SEMANTIC_TESSOUTER
, 0));
1656 case PIPE_SHADER_VERTEX
:
1657 case PIPE_SHADER_TESS_EVAL
:
1658 for (i
= 0; i
< sel
->info
.num_outputs
; i
++) {
1659 unsigned name
= sel
->info
.output_semantic_name
[i
];
1660 unsigned index
= sel
->info
.output_semantic_index
[i
];
1663 case TGSI_SEMANTIC_TESSINNER
:
1664 case TGSI_SEMANTIC_TESSOUTER
:
1665 case TGSI_SEMANTIC_PATCH
:
1666 sel
->patch_outputs_written
|=
1667 1llu << si_shader_io_get_unique_index(name
, index
);
1670 case TGSI_SEMANTIC_GENERIC
:
1671 /* don't process indices the function can't handle */
1675 case TGSI_SEMANTIC_POSITION
:
1676 case TGSI_SEMANTIC_PSIZE
:
1677 case TGSI_SEMANTIC_CLIPDIST
:
1678 sel
->outputs_written
|=
1679 1llu << si_shader_io_get_unique_index(name
, index
);
1681 case TGSI_SEMANTIC_CLIPVERTEX
: /* ignore these */
1682 case TGSI_SEMANTIC_EDGEFLAG
:
1685 sel
->outputs_written2
|=
1686 1u << si_shader_io_get_unique_index2(name
, index
);
1689 sel
->esgs_itemsize
= util_last_bit64(sel
->outputs_written
) * 16;
1692 case PIPE_SHADER_FRAGMENT
:
1693 for (i
= 0; i
< sel
->info
.num_inputs
; i
++) {
1694 unsigned name
= sel
->info
.input_semantic_name
[i
];
1695 unsigned index
= sel
->info
.input_semantic_index
[i
];
1698 case TGSI_SEMANTIC_CLIPDIST
:
1699 case TGSI_SEMANTIC_GENERIC
:
1701 1llu << si_shader_io_get_unique_index(name
, index
);
1703 case TGSI_SEMANTIC_PCOORD
: /* ignore this */
1706 sel
->inputs_read2
|=
1707 1u << si_shader_io_get_unique_index2(name
, index
);
1711 for (i
= 0; i
< 8; i
++)
1712 if (sel
->info
.colors_written
& (1 << i
))
1713 sel
->colors_written_4bit
|= 0xf << (4 * i
);
1715 for (i
= 0; i
< sel
->info
.num_inputs
; i
++) {
1716 if (sel
->info
.input_semantic_name
[i
] == TGSI_SEMANTIC_COLOR
) {
1717 int index
= sel
->info
.input_semantic_index
[i
];
1718 sel
->color_attr_index
[index
] = i
;
1724 /* DB_SHADER_CONTROL */
1725 sel
->db_shader_control
=
1726 S_02880C_Z_EXPORT_ENABLE(sel
->info
.writes_z
) |
1727 S_02880C_STENCIL_TEST_VAL_EXPORT_ENABLE(sel
->info
.writes_stencil
) |
1728 S_02880C_MASK_EXPORT_ENABLE(sel
->info
.writes_samplemask
) |
1729 S_02880C_KILL_ENABLE(sel
->info
.uses_kill
);
1731 switch (sel
->info
.properties
[TGSI_PROPERTY_FS_DEPTH_LAYOUT
]) {
1732 case TGSI_FS_DEPTH_LAYOUT_GREATER
:
1733 sel
->db_shader_control
|=
1734 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_GREATER_THAN_Z
);
1736 case TGSI_FS_DEPTH_LAYOUT_LESS
:
1737 sel
->db_shader_control
|=
1738 S_02880C_CONSERVATIVE_Z_EXPORT(V_02880C_EXPORT_LESS_THAN_Z
);
1742 /* Z_ORDER, EXEC_ON_HIER_FAIL and EXEC_ON_NOOP should be set as following:
1744 * | early Z/S | writes_mem | allow_ReZ? | Z_ORDER | EXEC_ON_HIER_FAIL | EXEC_ON_NOOP
1745 * --|-----------|------------|------------|--------------------|-------------------|-------------
1746 * 1a| false | false | true | EarlyZ_Then_ReZ | 0 | 0
1747 * 1b| false | false | false | EarlyZ_Then_LateZ | 0 | 0
1748 * 2 | false | true | n/a | LateZ | 1 | 0
1749 * 3 | true | false | n/a | EarlyZ_Then_LateZ | 0 | 0
1750 * 4 | true | true | n/a | EarlyZ_Then_LateZ | 0 | 1
1752 * In cases 3 and 4, HW will force Z_ORDER to EarlyZ regardless of what's set in the register.
1753 * In case 2, NOOP_CULL is a don't care field. In case 2, 3 and 4, ReZ doesn't make sense.
1755 * Don't use ReZ without profiling !!!
1757 * ReZ decreases performance by 15% in DiRT: Showdown on Ultra settings, which has pretty complex
1760 if (sel
->info
.properties
[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL
]) {
1762 sel
->db_shader_control
|= S_02880C_DEPTH_BEFORE_SHADER(1) |
1763 S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z
) |
1764 S_02880C_EXEC_ON_NOOP(sel
->info
.writes_memory
);
1765 } else if (sel
->info
.writes_memory
) {
1767 sel
->db_shader_control
|= S_02880C_Z_ORDER(V_02880C_LATE_Z
) |
1768 S_02880C_EXEC_ON_HIER_FAIL(1);
1771 sel
->db_shader_control
|= S_02880C_Z_ORDER(V_02880C_EARLY_Z_THEN_LATE_Z
);
1774 (void) mtx_init(&sel
->mutex
, mtx_plain
);
1775 util_queue_fence_init(&sel
->ready
);
1777 if ((sctx
->b
.debug
.debug_message
&& !sctx
->b
.debug
.async
) ||
1779 r600_can_dump_shader(&sscreen
->b
, sel
->info
.processor
))
1780 si_init_shader_selector_async(sel
, -1);
1782 util_queue_add_job(&sscreen
->shader_compiler_queue
, sel
,
1783 &sel
->ready
, si_init_shader_selector_async
,
1789 static void si_bind_vs_shader(struct pipe_context
*ctx
, void *state
)
1791 struct si_context
*sctx
= (struct si_context
*)ctx
;
1792 struct si_shader_selector
*sel
= state
;
1794 if (sctx
->vs_shader
.cso
== sel
)
1797 sctx
->vs_shader
.cso
= sel
;
1798 sctx
->vs_shader
.current
= sel
? sel
->first_variant
: NULL
;
1799 sctx
->do_update_shaders
= true;
1800 si_mark_atom_dirty(sctx
, &sctx
->clip_regs
);
1801 r600_update_vs_writes_viewport_index(&sctx
->b
, si_get_vs_info(sctx
));
1804 static void si_bind_gs_shader(struct pipe_context
*ctx
, void *state
)
1806 struct si_context
*sctx
= (struct si_context
*)ctx
;
1807 struct si_shader_selector
*sel
= state
;
1808 bool enable_changed
= !!sctx
->gs_shader
.cso
!= !!sel
;
1810 if (sctx
->gs_shader
.cso
== sel
)
1813 sctx
->gs_shader
.cso
= sel
;
1814 sctx
->gs_shader
.current
= sel
? sel
->first_variant
: NULL
;
1815 sctx
->ia_multi_vgt_param_key
.u
.uses_gs
= sel
!= NULL
;
1816 sctx
->do_update_shaders
= true;
1817 si_mark_atom_dirty(sctx
, &sctx
->clip_regs
);
1818 sctx
->last_rast_prim
= -1; /* reset this so that it gets updated */
1821 si_shader_change_notify(sctx
);
1822 r600_update_vs_writes_viewport_index(&sctx
->b
, si_get_vs_info(sctx
));
1825 static void si_update_tcs_tes_uses_prim_id(struct si_context
*sctx
)
1827 sctx
->ia_multi_vgt_param_key
.u
.tcs_tes_uses_prim_id
=
1828 (sctx
->tes_shader
.cso
&&
1829 sctx
->tes_shader
.cso
->info
.uses_primid
) ||
1830 (sctx
->tcs_shader
.cso
&&
1831 sctx
->tcs_shader
.cso
->info
.uses_primid
);
1834 static void si_bind_tcs_shader(struct pipe_context
*ctx
, void *state
)
1836 struct si_context
*sctx
= (struct si_context
*)ctx
;
1837 struct si_shader_selector
*sel
= state
;
1838 bool enable_changed
= !!sctx
->tcs_shader
.cso
!= !!sel
;
1840 if (sctx
->tcs_shader
.cso
== sel
)
1843 sctx
->tcs_shader
.cso
= sel
;
1844 sctx
->tcs_shader
.current
= sel
? sel
->first_variant
: NULL
;
1845 si_update_tcs_tes_uses_prim_id(sctx
);
1846 sctx
->do_update_shaders
= true;
1849 sctx
->last_tcs
= NULL
; /* invalidate derived tess state */
1852 static void si_bind_tes_shader(struct pipe_context
*ctx
, void *state
)
1854 struct si_context
*sctx
= (struct si_context
*)ctx
;
1855 struct si_shader_selector
*sel
= state
;
1856 bool enable_changed
= !!sctx
->tes_shader
.cso
!= !!sel
;
1858 if (sctx
->tes_shader
.cso
== sel
)
1861 sctx
->tes_shader
.cso
= sel
;
1862 sctx
->tes_shader
.current
= sel
? sel
->first_variant
: NULL
;
1863 sctx
->ia_multi_vgt_param_key
.u
.uses_tess
= sel
!= NULL
;
1864 si_update_tcs_tes_uses_prim_id(sctx
);
1865 sctx
->do_update_shaders
= true;
1866 si_mark_atom_dirty(sctx
, &sctx
->clip_regs
);
1867 sctx
->last_rast_prim
= -1; /* reset this so that it gets updated */
1869 if (enable_changed
) {
1870 si_shader_change_notify(sctx
);
1871 sctx
->last_tes_sh_base
= -1; /* invalidate derived tess state */
1873 r600_update_vs_writes_viewport_index(&sctx
->b
, si_get_vs_info(sctx
));
1876 static void si_bind_ps_shader(struct pipe_context
*ctx
, void *state
)
1878 struct si_context
*sctx
= (struct si_context
*)ctx
;
1879 struct si_shader_selector
*sel
= state
;
1881 /* skip if supplied shader is one already in use */
1882 if (sctx
->ps_shader
.cso
== sel
)
1885 sctx
->ps_shader
.cso
= sel
;
1886 sctx
->ps_shader
.current
= sel
? sel
->first_variant
: NULL
;
1887 sctx
->do_update_shaders
= true;
1888 si_mark_atom_dirty(sctx
, &sctx
->cb_render_state
);
1891 static void si_delete_shader(struct si_context
*sctx
, struct si_shader
*shader
)
1893 if (shader
->is_optimized
) {
1894 util_queue_fence_wait(&shader
->optimized_ready
);
1895 util_queue_fence_destroy(&shader
->optimized_ready
);
1899 switch (shader
->selector
->type
) {
1900 case PIPE_SHADER_VERTEX
:
1901 if (shader
->key
.as_ls
) {
1902 assert(sctx
->b
.chip_class
<= VI
);
1903 si_pm4_delete_state(sctx
, ls
, shader
->pm4
);
1904 } else if (shader
->key
.as_es
) {
1905 assert(sctx
->b
.chip_class
<= VI
);
1906 si_pm4_delete_state(sctx
, es
, shader
->pm4
);
1908 si_pm4_delete_state(sctx
, vs
, shader
->pm4
);
1911 case PIPE_SHADER_TESS_CTRL
:
1912 si_pm4_delete_state(sctx
, hs
, shader
->pm4
);
1914 case PIPE_SHADER_TESS_EVAL
:
1915 if (shader
->key
.as_es
) {
1916 assert(sctx
->b
.chip_class
<= VI
);
1917 si_pm4_delete_state(sctx
, es
, shader
->pm4
);
1919 si_pm4_delete_state(sctx
, vs
, shader
->pm4
);
1922 case PIPE_SHADER_GEOMETRY
:
1923 if (shader
->is_gs_copy_shader
)
1924 si_pm4_delete_state(sctx
, vs
, shader
->pm4
);
1926 si_pm4_delete_state(sctx
, gs
, shader
->pm4
);
1928 case PIPE_SHADER_FRAGMENT
:
1929 si_pm4_delete_state(sctx
, ps
, shader
->pm4
);
1934 si_shader_destroy(shader
);
1938 static void si_delete_shader_selector(struct pipe_context
*ctx
, void *state
)
1940 struct si_context
*sctx
= (struct si_context
*)ctx
;
1941 struct si_shader_selector
*sel
= (struct si_shader_selector
*)state
;
1942 struct si_shader
*p
= sel
->first_variant
, *c
;
1943 struct si_shader_ctx_state
*current_shader
[SI_NUM_SHADERS
] = {
1944 [PIPE_SHADER_VERTEX
] = &sctx
->vs_shader
,
1945 [PIPE_SHADER_TESS_CTRL
] = &sctx
->tcs_shader
,
1946 [PIPE_SHADER_TESS_EVAL
] = &sctx
->tes_shader
,
1947 [PIPE_SHADER_GEOMETRY
] = &sctx
->gs_shader
,
1948 [PIPE_SHADER_FRAGMENT
] = &sctx
->ps_shader
,
1951 util_queue_fence_wait(&sel
->ready
);
1953 if (current_shader
[sel
->type
]->cso
== sel
) {
1954 current_shader
[sel
->type
]->cso
= NULL
;
1955 current_shader
[sel
->type
]->current
= NULL
;
1959 c
= p
->next_variant
;
1960 si_delete_shader(sctx
, p
);
1964 if (sel
->main_shader_part
)
1965 si_delete_shader(sctx
, sel
->main_shader_part
);
1966 if (sel
->main_shader_part_ls
)
1967 si_delete_shader(sctx
, sel
->main_shader_part_ls
);
1968 if (sel
->main_shader_part_es
)
1969 si_delete_shader(sctx
, sel
->main_shader_part_es
);
1970 if (sel
->gs_copy_shader
)
1971 si_delete_shader(sctx
, sel
->gs_copy_shader
);
1973 util_queue_fence_destroy(&sel
->ready
);
1974 mtx_destroy(&sel
->mutex
);
1979 static unsigned si_get_ps_input_cntl(struct si_context
*sctx
,
1980 struct si_shader
*vs
, unsigned name
,
1981 unsigned index
, unsigned interpolate
)
1983 struct tgsi_shader_info
*vsinfo
= &vs
->selector
->info
;
1984 unsigned j
, offset
, ps_input_cntl
= 0;
1986 if (interpolate
== TGSI_INTERPOLATE_CONSTANT
||
1987 (interpolate
== TGSI_INTERPOLATE_COLOR
&& sctx
->flatshade
))
1988 ps_input_cntl
|= S_028644_FLAT_SHADE(1);
1990 if (name
== TGSI_SEMANTIC_PCOORD
||
1991 (name
== TGSI_SEMANTIC_TEXCOORD
&&
1992 sctx
->sprite_coord_enable
& (1 << index
))) {
1993 ps_input_cntl
|= S_028644_PT_SPRITE_TEX(1);
1996 for (j
= 0; j
< vsinfo
->num_outputs
; j
++) {
1997 if (name
== vsinfo
->output_semantic_name
[j
] &&
1998 index
== vsinfo
->output_semantic_index
[j
]) {
1999 offset
= vs
->info
.vs_output_param_offset
[j
];
2001 if (offset
<= EXP_PARAM_OFFSET_31
) {
2002 /* The input is loaded from parameter memory. */
2003 ps_input_cntl
|= S_028644_OFFSET(offset
);
2004 } else if (!G_028644_PT_SPRITE_TEX(ps_input_cntl
)) {
2005 if (offset
== EXP_PARAM_UNDEFINED
) {
2006 /* This can happen with depth-only rendering. */
2009 /* The input is a DEFAULT_VAL constant. */
2010 assert(offset
>= EXP_PARAM_DEFAULT_VAL_0000
&&
2011 offset
<= EXP_PARAM_DEFAULT_VAL_1111
);
2012 offset
-= EXP_PARAM_DEFAULT_VAL_0000
;
2015 ps_input_cntl
= S_028644_OFFSET(0x20) |
2016 S_028644_DEFAULT_VAL(offset
);
2022 if (name
== TGSI_SEMANTIC_PRIMID
)
2023 /* PrimID is written after the last output. */
2024 ps_input_cntl
|= S_028644_OFFSET(vs
->info
.vs_output_param_offset
[vsinfo
->num_outputs
]);
2025 else if (j
== vsinfo
->num_outputs
&& !G_028644_PT_SPRITE_TEX(ps_input_cntl
)) {
2026 /* No corresponding output found, load defaults into input.
2027 * Don't set any other bits.
2028 * (FLAT_SHADE=1 completely changes behavior) */
2029 ps_input_cntl
= S_028644_OFFSET(0x20);
2030 /* D3D 9 behaviour. GL is undefined */
2031 if (name
== TGSI_SEMANTIC_COLOR
&& index
== 0)
2032 ps_input_cntl
|= S_028644_DEFAULT_VAL(3);
2034 return ps_input_cntl
;
2037 static void si_emit_spi_map(struct si_context
*sctx
, struct r600_atom
*atom
)
2039 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
2040 struct si_shader
*ps
= sctx
->ps_shader
.current
;
2041 struct si_shader
*vs
= si_get_vs_state(sctx
);
2042 struct tgsi_shader_info
*psinfo
= ps
? &ps
->selector
->info
: NULL
;
2043 unsigned i
, num_interp
, num_written
= 0, bcol_interp
[2];
2045 if (!ps
|| !ps
->selector
->info
.num_inputs
)
2048 num_interp
= si_get_ps_num_interp(ps
);
2049 assert(num_interp
> 0);
2050 radeon_set_context_reg_seq(cs
, R_028644_SPI_PS_INPUT_CNTL_0
, num_interp
);
2052 for (i
= 0; i
< psinfo
->num_inputs
; i
++) {
2053 unsigned name
= psinfo
->input_semantic_name
[i
];
2054 unsigned index
= psinfo
->input_semantic_index
[i
];
2055 unsigned interpolate
= psinfo
->input_interpolate
[i
];
2057 radeon_emit(cs
, si_get_ps_input_cntl(sctx
, vs
, name
, index
,
2061 if (name
== TGSI_SEMANTIC_COLOR
) {
2062 assert(index
< ARRAY_SIZE(bcol_interp
));
2063 bcol_interp
[index
] = interpolate
;
2067 if (ps
->key
.part
.ps
.prolog
.color_two_side
) {
2068 unsigned bcol
= TGSI_SEMANTIC_BCOLOR
;
2070 for (i
= 0; i
< 2; i
++) {
2071 if (!(psinfo
->colors_read
& (0xf << (i
* 4))))
2074 radeon_emit(cs
, si_get_ps_input_cntl(sctx
, vs
, bcol
,
2075 i
, bcol_interp
[i
]));
2079 assert(num_interp
== num_written
);
2083 * Writing CONFIG or UCONFIG VGT registers requires VGT_FLUSH before that.
2085 static void si_init_config_add_vgt_flush(struct si_context
*sctx
)
2087 if (sctx
->init_config_has_vgt_flush
)
2090 /* Done by Vulkan before VGT_FLUSH. */
2091 si_pm4_cmd_begin(sctx
->init_config
, PKT3_EVENT_WRITE
);
2092 si_pm4_cmd_add(sctx
->init_config
,
2093 EVENT_TYPE(V_028A90_VS_PARTIAL_FLUSH
) | EVENT_INDEX(4));
2094 si_pm4_cmd_end(sctx
->init_config
, false);
2096 /* VGT_FLUSH is required even if VGT is idle. It resets VGT pointers. */
2097 si_pm4_cmd_begin(sctx
->init_config
, PKT3_EVENT_WRITE
);
2098 si_pm4_cmd_add(sctx
->init_config
, EVENT_TYPE(V_028A90_VGT_FLUSH
) | EVENT_INDEX(0));
2099 si_pm4_cmd_end(sctx
->init_config
, false);
2100 sctx
->init_config_has_vgt_flush
= true;
2103 /* Initialize state related to ESGS / GSVS ring buffers */
2104 static bool si_update_gs_ring_buffers(struct si_context
*sctx
)
2106 struct si_shader_selector
*es
=
2107 sctx
->tes_shader
.cso
? sctx
->tes_shader
.cso
: sctx
->vs_shader
.cso
;
2108 struct si_shader_selector
*gs
= sctx
->gs_shader
.cso
;
2109 struct si_pm4_state
*pm4
;
2111 /* Chip constants. */
2112 unsigned num_se
= sctx
->screen
->b
.info
.max_se
;
2113 unsigned wave_size
= 64;
2114 unsigned max_gs_waves
= 32 * num_se
; /* max 32 per SE on GCN */
2115 unsigned gs_vertex_reuse
= 16 * num_se
; /* GS_VERTEX_REUSE register (per SE) */
2116 unsigned alignment
= 256 * num_se
;
2117 /* The maximum size is 63.999 MB per SE. */
2118 unsigned max_size
= ((unsigned)(63.999 * 1024 * 1024) & ~255) * num_se
;
2120 /* Calculate the minimum size. */
2121 unsigned min_esgs_ring_size
= align(es
->esgs_itemsize
* gs_vertex_reuse
*
2122 wave_size
, alignment
);
2124 /* These are recommended sizes, not minimum sizes. */
2125 unsigned esgs_ring_size
= max_gs_waves
* 2 * wave_size
*
2126 es
->esgs_itemsize
* gs
->gs_input_verts_per_prim
;
2127 unsigned gsvs_ring_size
= max_gs_waves
* 2 * wave_size
*
2128 gs
->max_gsvs_emit_size
;
2130 min_esgs_ring_size
= align(min_esgs_ring_size
, alignment
);
2131 esgs_ring_size
= align(esgs_ring_size
, alignment
);
2132 gsvs_ring_size
= align(gsvs_ring_size
, alignment
);
2134 esgs_ring_size
= CLAMP(esgs_ring_size
, min_esgs_ring_size
, max_size
);
2135 gsvs_ring_size
= MIN2(gsvs_ring_size
, max_size
);
2137 /* Some rings don't have to be allocated if shaders don't use them.
2138 * (e.g. no varyings between ES and GS or GS and VS)
2140 * GFX9 doesn't have the ESGS ring.
2142 bool update_esgs
= sctx
->b
.chip_class
<= VI
&&
2144 (!sctx
->esgs_ring
||
2145 sctx
->esgs_ring
->width0
< esgs_ring_size
);
2146 bool update_gsvs
= gsvs_ring_size
&&
2147 (!sctx
->gsvs_ring
||
2148 sctx
->gsvs_ring
->width0
< gsvs_ring_size
);
2150 if (!update_esgs
&& !update_gsvs
)
2154 pipe_resource_reference(&sctx
->esgs_ring
, NULL
);
2156 r600_aligned_buffer_create(sctx
->b
.b
.screen
,
2157 R600_RESOURCE_FLAG_UNMAPPABLE
,
2159 esgs_ring_size
, alignment
);
2160 if (!sctx
->esgs_ring
)
2165 pipe_resource_reference(&sctx
->gsvs_ring
, NULL
);
2167 r600_aligned_buffer_create(sctx
->b
.b
.screen
,
2168 R600_RESOURCE_FLAG_UNMAPPABLE
,
2170 gsvs_ring_size
, alignment
);
2171 if (!sctx
->gsvs_ring
)
2175 /* Create the "init_config_gs_rings" state. */
2176 pm4
= CALLOC_STRUCT(si_pm4_state
);
2180 if (sctx
->b
.chip_class
>= CIK
) {
2181 if (sctx
->esgs_ring
) {
2182 assert(sctx
->b
.chip_class
<= VI
);
2183 si_pm4_set_reg(pm4
, R_030900_VGT_ESGS_RING_SIZE
,
2184 sctx
->esgs_ring
->width0
/ 256);
2186 if (sctx
->gsvs_ring
)
2187 si_pm4_set_reg(pm4
, R_030904_VGT_GSVS_RING_SIZE
,
2188 sctx
->gsvs_ring
->width0
/ 256);
2190 if (sctx
->esgs_ring
)
2191 si_pm4_set_reg(pm4
, R_0088C8_VGT_ESGS_RING_SIZE
,
2192 sctx
->esgs_ring
->width0
/ 256);
2193 if (sctx
->gsvs_ring
)
2194 si_pm4_set_reg(pm4
, R_0088CC_VGT_GSVS_RING_SIZE
,
2195 sctx
->gsvs_ring
->width0
/ 256);
2198 /* Set the state. */
2199 if (sctx
->init_config_gs_rings
)
2200 si_pm4_free_state(sctx
, sctx
->init_config_gs_rings
, ~0);
2201 sctx
->init_config_gs_rings
= pm4
;
2203 if (!sctx
->init_config_has_vgt_flush
) {
2204 si_init_config_add_vgt_flush(sctx
);
2205 si_pm4_upload_indirect_buffer(sctx
, sctx
->init_config
);
2208 /* Flush the context to re-emit both init_config states. */
2209 sctx
->b
.initial_gfx_cs_size
= 0; /* force flush */
2210 si_context_gfx_flush(sctx
, RADEON_FLUSH_ASYNC
, NULL
);
2212 /* Set ring bindings. */
2213 if (sctx
->esgs_ring
) {
2214 assert(sctx
->b
.chip_class
<= VI
);
2215 si_set_ring_buffer(&sctx
->b
.b
, SI_ES_RING_ESGS
,
2216 sctx
->esgs_ring
, 0, sctx
->esgs_ring
->width0
,
2217 true, true, 4, 64, 0);
2218 si_set_ring_buffer(&sctx
->b
.b
, SI_GS_RING_ESGS
,
2219 sctx
->esgs_ring
, 0, sctx
->esgs_ring
->width0
,
2220 false, false, 0, 0, 0);
2222 if (sctx
->gsvs_ring
) {
2223 si_set_ring_buffer(&sctx
->b
.b
, SI_RING_GSVS
,
2224 sctx
->gsvs_ring
, 0, sctx
->gsvs_ring
->width0
,
2225 false, false, 0, 0, 0);
2232 * @returns 1 if \p sel has been updated to use a new scratch buffer
2234 * < 0 if there was a failure
2236 static int si_update_scratch_buffer(struct si_context
*sctx
,
2237 struct si_shader
*shader
)
2239 uint64_t scratch_va
= sctx
->scratch_buffer
->gpu_address
;
2245 /* This shader doesn't need a scratch buffer */
2246 if (shader
->config
.scratch_bytes_per_wave
== 0)
2249 /* This shader is already configured to use the current
2250 * scratch buffer. */
2251 if (shader
->scratch_bo
== sctx
->scratch_buffer
)
2254 assert(sctx
->scratch_buffer
);
2256 si_shader_apply_scratch_relocs(sctx
, shader
, &shader
->config
, scratch_va
);
2258 /* Replace the shader bo with a new bo that has the relocs applied. */
2259 r
= si_shader_binary_upload(sctx
->screen
, shader
);
2263 /* Update the shader state to use the new shader bo. */
2264 si_shader_init_pm4_state(sctx
->screen
, shader
);
2266 r600_resource_reference(&shader
->scratch_bo
, sctx
->scratch_buffer
);
2271 static unsigned si_get_current_scratch_buffer_size(struct si_context
*sctx
)
2273 return sctx
->scratch_buffer
? sctx
->scratch_buffer
->b
.b
.width0
: 0;
2276 static unsigned si_get_scratch_buffer_bytes_per_wave(struct si_shader
*shader
)
2278 return shader
? shader
->config
.scratch_bytes_per_wave
: 0;
2281 static unsigned si_get_max_scratch_bytes_per_wave(struct si_context
*sctx
)
2285 bytes
= MAX2(bytes
, si_get_scratch_buffer_bytes_per_wave(sctx
->ps_shader
.current
));
2286 bytes
= MAX2(bytes
, si_get_scratch_buffer_bytes_per_wave(sctx
->gs_shader
.current
));
2287 bytes
= MAX2(bytes
, si_get_scratch_buffer_bytes_per_wave(sctx
->vs_shader
.current
));
2288 bytes
= MAX2(bytes
, si_get_scratch_buffer_bytes_per_wave(sctx
->tcs_shader
.current
));
2289 bytes
= MAX2(bytes
, si_get_scratch_buffer_bytes_per_wave(sctx
->tes_shader
.current
));
2293 static bool si_update_spi_tmpring_size(struct si_context
*sctx
)
2295 unsigned current_scratch_buffer_size
=
2296 si_get_current_scratch_buffer_size(sctx
);
2297 unsigned scratch_bytes_per_wave
=
2298 si_get_max_scratch_bytes_per_wave(sctx
);
2299 unsigned scratch_needed_size
= scratch_bytes_per_wave
*
2300 sctx
->scratch_waves
;
2301 unsigned spi_tmpring_size
;
2304 if (scratch_needed_size
> 0) {
2305 if (scratch_needed_size
> current_scratch_buffer_size
) {
2306 /* Create a bigger scratch buffer */
2307 r600_resource_reference(&sctx
->scratch_buffer
, NULL
);
2309 sctx
->scratch_buffer
= (struct r600_resource
*)
2310 r600_aligned_buffer_create(&sctx
->screen
->b
.b
,
2311 R600_RESOURCE_FLAG_UNMAPPABLE
,
2313 scratch_needed_size
, 256);
2314 if (!sctx
->scratch_buffer
)
2317 si_mark_atom_dirty(sctx
, &sctx
->scratch_state
);
2318 r600_context_add_resource_size(&sctx
->b
.b
,
2319 &sctx
->scratch_buffer
->b
.b
);
2322 /* Update the shaders, so they are using the latest scratch. The
2323 * scratch buffer may have been changed since these shaders were
2324 * last used, so we still need to try to update them, even if
2325 * they require scratch buffers smaller than the current size.
2327 r
= si_update_scratch_buffer(sctx
, sctx
->ps_shader
.current
);
2331 si_pm4_bind_state(sctx
, ps
, sctx
->ps_shader
.current
->pm4
);
2333 r
= si_update_scratch_buffer(sctx
, sctx
->gs_shader
.current
);
2337 si_pm4_bind_state(sctx
, gs
, sctx
->gs_shader
.current
->pm4
);
2339 r
= si_update_scratch_buffer(sctx
, sctx
->tcs_shader
.current
);
2343 si_pm4_bind_state(sctx
, hs
, sctx
->tcs_shader
.current
->pm4
);
2345 /* VS can be bound as LS, ES, or VS. */
2346 r
= si_update_scratch_buffer(sctx
, sctx
->vs_shader
.current
);
2350 if (sctx
->tes_shader
.current
)
2351 si_pm4_bind_state(sctx
, ls
, sctx
->vs_shader
.current
->pm4
);
2352 else if (sctx
->gs_shader
.current
)
2353 si_pm4_bind_state(sctx
, es
, sctx
->vs_shader
.current
->pm4
);
2355 si_pm4_bind_state(sctx
, vs
, sctx
->vs_shader
.current
->pm4
);
2358 /* TES can be bound as ES or VS. */
2359 r
= si_update_scratch_buffer(sctx
, sctx
->tes_shader
.current
);
2363 if (sctx
->gs_shader
.current
)
2364 si_pm4_bind_state(sctx
, es
, sctx
->tes_shader
.current
->pm4
);
2366 si_pm4_bind_state(sctx
, vs
, sctx
->tes_shader
.current
->pm4
);
2370 /* The LLVM shader backend should be reporting aligned scratch_sizes. */
2371 assert((scratch_needed_size
& ~0x3FF) == scratch_needed_size
&&
2372 "scratch size should already be aligned correctly.");
2374 spi_tmpring_size
= S_0286E8_WAVES(sctx
->scratch_waves
) |
2375 S_0286E8_WAVESIZE(scratch_bytes_per_wave
>> 10);
2376 if (spi_tmpring_size
!= sctx
->spi_tmpring_size
) {
2377 sctx
->spi_tmpring_size
= spi_tmpring_size
;
2378 si_mark_atom_dirty(sctx
, &sctx
->scratch_state
);
2383 static void si_init_tess_factor_ring(struct si_context
*sctx
)
2385 bool double_offchip_buffers
= sctx
->b
.chip_class
>= CIK
&&
2386 sctx
->b
.family
!= CHIP_CARRIZO
&&
2387 sctx
->b
.family
!= CHIP_STONEY
;
2388 unsigned max_offchip_buffers_per_se
= double_offchip_buffers
? 128 : 64;
2389 unsigned max_offchip_buffers
= max_offchip_buffers_per_se
*
2390 sctx
->screen
->b
.info
.max_se
;
2391 unsigned offchip_granularity
;
2393 switch (sctx
->screen
->tess_offchip_block_dw_size
) {
2398 offchip_granularity
= V_03093C_X_8K_DWORDS
;
2401 offchip_granularity
= V_03093C_X_4K_DWORDS
;
2405 switch (sctx
->b
.chip_class
) {
2407 max_offchip_buffers
= MIN2(max_offchip_buffers
, 126);
2412 max_offchip_buffers
= MIN2(max_offchip_buffers
, 508);
2419 assert(!sctx
->tf_ring
);
2420 sctx
->tf_ring
= r600_aligned_buffer_create(sctx
->b
.b
.screen
,
2421 R600_RESOURCE_FLAG_UNMAPPABLE
,
2423 32768 * sctx
->screen
->b
.info
.max_se
,
2428 assert(((sctx
->tf_ring
->width0
/ 4) & C_030938_SIZE
) == 0);
2430 sctx
->tess_offchip_ring
=
2431 r600_aligned_buffer_create(sctx
->b
.b
.screen
,
2432 R600_RESOURCE_FLAG_UNMAPPABLE
,
2434 max_offchip_buffers
*
2435 sctx
->screen
->tess_offchip_block_dw_size
* 4,
2437 if (!sctx
->tess_offchip_ring
)
2440 si_init_config_add_vgt_flush(sctx
);
2442 /* Append these registers to the init config state. */
2443 if (sctx
->b
.chip_class
>= CIK
) {
2444 if (sctx
->b
.chip_class
>= VI
)
2445 --max_offchip_buffers
;
2447 si_pm4_set_reg(sctx
->init_config
, R_030938_VGT_TF_RING_SIZE
,
2448 S_030938_SIZE(sctx
->tf_ring
->width0
/ 4));
2449 si_pm4_set_reg(sctx
->init_config
, R_030940_VGT_TF_MEMORY_BASE
,
2450 r600_resource(sctx
->tf_ring
)->gpu_address
>> 8);
2451 if (sctx
->b
.chip_class
>= GFX9
)
2452 si_pm4_set_reg(sctx
->init_config
, R_030944_VGT_TF_MEMORY_BASE_HI
,
2453 r600_resource(sctx
->tf_ring
)->gpu_address
>> 40);
2454 si_pm4_set_reg(sctx
->init_config
, R_03093C_VGT_HS_OFFCHIP_PARAM
,
2455 S_03093C_OFFCHIP_BUFFERING(max_offchip_buffers
) |
2456 S_03093C_OFFCHIP_GRANULARITY(offchip_granularity
));
2458 assert(offchip_granularity
== V_03093C_X_8K_DWORDS
);
2459 si_pm4_set_reg(sctx
->init_config
, R_008988_VGT_TF_RING_SIZE
,
2460 S_008988_SIZE(sctx
->tf_ring
->width0
/ 4));
2461 si_pm4_set_reg(sctx
->init_config
, R_0089B8_VGT_TF_MEMORY_BASE
,
2462 r600_resource(sctx
->tf_ring
)->gpu_address
>> 8);
2463 si_pm4_set_reg(sctx
->init_config
, R_0089B0_VGT_HS_OFFCHIP_PARAM
,
2464 S_0089B0_OFFCHIP_BUFFERING(max_offchip_buffers
));
2467 /* Flush the context to re-emit the init_config state.
2468 * This is done only once in a lifetime of a context.
2470 si_pm4_upload_indirect_buffer(sctx
, sctx
->init_config
);
2471 sctx
->b
.initial_gfx_cs_size
= 0; /* force flush */
2472 si_context_gfx_flush(sctx
, RADEON_FLUSH_ASYNC
, NULL
);
2474 si_set_ring_buffer(&sctx
->b
.b
, SI_HS_RING_TESS_FACTOR
, sctx
->tf_ring
,
2475 0, sctx
->tf_ring
->width0
, false, false, 0, 0, 0);
2477 si_set_ring_buffer(&sctx
->b
.b
, SI_HS_RING_TESS_OFFCHIP
,
2478 sctx
->tess_offchip_ring
, 0,
2479 sctx
->tess_offchip_ring
->width0
, false, false, 0, 0, 0);
2483 * This is used when TCS is NULL in the VS->TCS->TES chain. In this case,
2484 * VS passes its outputs to TES directly, so the fixed-function shader only
2485 * has to write TESSOUTER and TESSINNER.
2487 static void si_generate_fixed_func_tcs(struct si_context
*sctx
)
2489 struct ureg_src outer
, inner
;
2490 struct ureg_dst tessouter
, tessinner
;
2491 struct ureg_program
*ureg
= ureg_create(PIPE_SHADER_TESS_CTRL
);
2494 return; /* if we get here, we're screwed */
2496 assert(!sctx
->fixed_func_tcs_shader
.cso
);
2498 outer
= ureg_DECL_system_value(ureg
,
2499 TGSI_SEMANTIC_DEFAULT_TESSOUTER_SI
, 0);
2500 inner
= ureg_DECL_system_value(ureg
,
2501 TGSI_SEMANTIC_DEFAULT_TESSINNER_SI
, 0);
2503 tessouter
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_TESSOUTER
, 0);
2504 tessinner
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_TESSINNER
, 0);
2506 ureg_MOV(ureg
, tessouter
, outer
);
2507 ureg_MOV(ureg
, tessinner
, inner
);
2510 sctx
->fixed_func_tcs_shader
.cso
=
2511 ureg_create_shader_and_destroy(ureg
, &sctx
->b
.b
);
2514 static void si_update_vgt_shader_config(struct si_context
*sctx
)
2516 /* Calculate the index of the config.
2517 * 0 = VS, 1 = VS+GS, 2 = VS+Tess, 3 = VS+Tess+GS */
2518 unsigned index
= 2*!!sctx
->tes_shader
.cso
+ !!sctx
->gs_shader
.cso
;
2519 struct si_pm4_state
**pm4
= &sctx
->vgt_shader_config
[index
];
2522 uint32_t stages
= 0;
2524 *pm4
= CALLOC_STRUCT(si_pm4_state
);
2526 if (sctx
->tes_shader
.cso
) {
2527 stages
|= S_028B54_LS_EN(V_028B54_LS_STAGE_ON
) |
2528 S_028B54_HS_EN(1) | S_028B54_DYNAMIC_HS(1);
2530 if (sctx
->gs_shader
.cso
)
2531 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_DS
) |
2533 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER
);
2535 stages
|= S_028B54_VS_EN(V_028B54_VS_STAGE_DS
);
2536 } else if (sctx
->gs_shader
.cso
) {
2537 stages
|= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL
) |
2539 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER
);
2542 si_pm4_set_reg(*pm4
, R_028B54_VGT_SHADER_STAGES_EN
, stages
);
2544 si_pm4_bind_state(sctx
, vgt_shader_config
, *pm4
);
2547 static void si_update_so(struct si_context
*sctx
, struct si_shader_selector
*shader
)
2549 struct pipe_stream_output_info
*so
= &shader
->so
;
2550 uint32_t enabled_stream_buffers_mask
= 0;
2553 for (i
= 0; i
< so
->num_outputs
; i
++)
2554 enabled_stream_buffers_mask
|= (1 << so
->output
[i
].output_buffer
) << (so
->output
[i
].stream
* 4);
2555 sctx
->b
.streamout
.enabled_stream_buffers_mask
= enabled_stream_buffers_mask
;
2556 sctx
->b
.streamout
.stride_in_dw
= shader
->so
.stride
;
2559 bool si_update_shaders(struct si_context
*sctx
)
2561 struct pipe_context
*ctx
= (struct pipe_context
*)sctx
;
2562 struct si_compiler_ctx_state compiler_state
;
2563 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
2564 struct si_shader
*old_vs
= si_get_vs_state(sctx
);
2565 bool old_clip_disable
= old_vs
? old_vs
->key
.opt
.hw_vs
.clip_disable
: false;
2568 compiler_state
.tm
= sctx
->tm
;
2569 compiler_state
.debug
= sctx
->b
.debug
;
2570 compiler_state
.is_debug_context
= sctx
->is_debug
;
2572 /* Update stages before GS. */
2573 if (sctx
->tes_shader
.cso
) {
2574 if (!sctx
->tf_ring
) {
2575 si_init_tess_factor_ring(sctx
);
2581 if (sctx
->b
.chip_class
<= VI
) {
2582 r
= si_shader_select(ctx
, &sctx
->vs_shader
,
2586 si_pm4_bind_state(sctx
, ls
, sctx
->vs_shader
.current
->pm4
);
2589 if (sctx
->tcs_shader
.cso
) {
2590 r
= si_shader_select(ctx
, &sctx
->tcs_shader
,
2594 si_pm4_bind_state(sctx
, hs
, sctx
->tcs_shader
.current
->pm4
);
2596 if (!sctx
->fixed_func_tcs_shader
.cso
) {
2597 si_generate_fixed_func_tcs(sctx
);
2598 if (!sctx
->fixed_func_tcs_shader
.cso
)
2602 r
= si_shader_select(ctx
, &sctx
->fixed_func_tcs_shader
,
2606 si_pm4_bind_state(sctx
, hs
,
2607 sctx
->fixed_func_tcs_shader
.current
->pm4
);
2610 if (sctx
->gs_shader
.cso
) {
2612 if (sctx
->b
.chip_class
<= VI
) {
2613 r
= si_shader_select(ctx
, &sctx
->tes_shader
,
2617 si_pm4_bind_state(sctx
, es
, sctx
->tes_shader
.current
->pm4
);
2621 r
= si_shader_select(ctx
, &sctx
->tes_shader
,
2625 si_pm4_bind_state(sctx
, vs
, sctx
->tes_shader
.current
->pm4
);
2626 si_update_so(sctx
, sctx
->tes_shader
.cso
);
2628 } else if (sctx
->gs_shader
.cso
) {
2629 if (sctx
->b
.chip_class
<= VI
) {
2631 r
= si_shader_select(ctx
, &sctx
->vs_shader
,
2635 si_pm4_bind_state(sctx
, es
, sctx
->vs_shader
.current
->pm4
);
2637 si_pm4_bind_state(sctx
, ls
, NULL
);
2638 si_pm4_bind_state(sctx
, hs
, NULL
);
2642 r
= si_shader_select(ctx
, &sctx
->vs_shader
, &compiler_state
);
2645 si_pm4_bind_state(sctx
, vs
, sctx
->vs_shader
.current
->pm4
);
2646 si_update_so(sctx
, sctx
->vs_shader
.cso
);
2648 si_pm4_bind_state(sctx
, ls
, NULL
);
2649 si_pm4_bind_state(sctx
, hs
, NULL
);
2653 if (sctx
->gs_shader
.cso
) {
2654 r
= si_shader_select(ctx
, &sctx
->gs_shader
, &compiler_state
);
2657 si_pm4_bind_state(sctx
, gs
, sctx
->gs_shader
.current
->pm4
);
2658 si_pm4_bind_state(sctx
, vs
, sctx
->gs_shader
.cso
->gs_copy_shader
->pm4
);
2659 si_update_so(sctx
, sctx
->gs_shader
.cso
);
2661 if (!si_update_gs_ring_buffers(sctx
))
2664 si_pm4_bind_state(sctx
, gs
, NULL
);
2665 if (sctx
->b
.chip_class
<= VI
)
2666 si_pm4_bind_state(sctx
, es
, NULL
);
2669 si_update_vgt_shader_config(sctx
);
2671 if (old_clip_disable
!= si_get_vs_state(sctx
)->key
.opt
.hw_vs
.clip_disable
)
2672 si_mark_atom_dirty(sctx
, &sctx
->clip_regs
);
2674 if (sctx
->ps_shader
.cso
) {
2675 unsigned db_shader_control
;
2677 r
= si_shader_select(ctx
, &sctx
->ps_shader
, &compiler_state
);
2680 si_pm4_bind_state(sctx
, ps
, sctx
->ps_shader
.current
->pm4
);
2683 sctx
->ps_shader
.cso
->db_shader_control
|
2684 S_02880C_KILL_ENABLE(si_get_alpha_test_func(sctx
) != PIPE_FUNC_ALWAYS
);
2686 if (si_pm4_state_changed(sctx
, ps
) || si_pm4_state_changed(sctx
, vs
) ||
2687 sctx
->sprite_coord_enable
!= rs
->sprite_coord_enable
||
2688 sctx
->flatshade
!= rs
->flatshade
) {
2689 sctx
->sprite_coord_enable
= rs
->sprite_coord_enable
;
2690 sctx
->flatshade
= rs
->flatshade
;
2691 si_mark_atom_dirty(sctx
, &sctx
->spi_map
);
2694 if (sctx
->screen
->b
.rbplus_allowed
&& si_pm4_state_changed(sctx
, ps
))
2695 si_mark_atom_dirty(sctx
, &sctx
->cb_render_state
);
2697 if (sctx
->ps_db_shader_control
!= db_shader_control
) {
2698 sctx
->ps_db_shader_control
= db_shader_control
;
2699 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
2702 if (sctx
->smoothing_enabled
!= sctx
->ps_shader
.current
->key
.part
.ps
.epilog
.poly_line_smoothing
) {
2703 sctx
->smoothing_enabled
= sctx
->ps_shader
.current
->key
.part
.ps
.epilog
.poly_line_smoothing
;
2704 si_mark_atom_dirty(sctx
, &sctx
->msaa_config
);
2706 if (sctx
->b
.chip_class
== SI
)
2707 si_mark_atom_dirty(sctx
, &sctx
->db_render_state
);
2709 if (sctx
->framebuffer
.nr_samples
<= 1)
2710 si_mark_atom_dirty(sctx
, &sctx
->msaa_sample_locs
.atom
);
2714 if (si_pm4_state_changed(sctx
, ls
) ||
2715 si_pm4_state_changed(sctx
, hs
) ||
2716 si_pm4_state_changed(sctx
, es
) ||
2717 si_pm4_state_changed(sctx
, gs
) ||
2718 si_pm4_state_changed(sctx
, vs
) ||
2719 si_pm4_state_changed(sctx
, ps
)) {
2720 if (!si_update_spi_tmpring_size(sctx
))
2724 if (sctx
->b
.chip_class
>= CIK
)
2725 si_mark_atom_dirty(sctx
, &sctx
->prefetch_L2
);
2727 sctx
->do_update_shaders
= false;
2731 static void si_emit_scratch_state(struct si_context
*sctx
,
2732 struct r600_atom
*atom
)
2734 struct radeon_winsys_cs
*cs
= sctx
->b
.gfx
.cs
;
2736 radeon_set_context_reg(cs
, R_0286E8_SPI_TMPRING_SIZE
,
2737 sctx
->spi_tmpring_size
);
2739 if (sctx
->scratch_buffer
) {
2740 radeon_add_to_buffer_list(&sctx
->b
, &sctx
->b
.gfx
,
2741 sctx
->scratch_buffer
, RADEON_USAGE_READWRITE
,
2742 RADEON_PRIO_SCRATCH_BUFFER
);
2746 void si_init_shader_functions(struct si_context
*sctx
)
2748 si_init_atom(sctx
, &sctx
->spi_map
, &sctx
->atoms
.s
.spi_map
, si_emit_spi_map
);
2749 si_init_atom(sctx
, &sctx
->scratch_state
, &sctx
->atoms
.s
.scratch_state
,
2750 si_emit_scratch_state
);
2752 sctx
->b
.b
.create_vs_state
= si_create_shader_selector
;
2753 sctx
->b
.b
.create_tcs_state
= si_create_shader_selector
;
2754 sctx
->b
.b
.create_tes_state
= si_create_shader_selector
;
2755 sctx
->b
.b
.create_gs_state
= si_create_shader_selector
;
2756 sctx
->b
.b
.create_fs_state
= si_create_shader_selector
;
2758 sctx
->b
.b
.bind_vs_state
= si_bind_vs_shader
;
2759 sctx
->b
.b
.bind_tcs_state
= si_bind_tcs_shader
;
2760 sctx
->b
.b
.bind_tes_state
= si_bind_tes_shader
;
2761 sctx
->b
.b
.bind_gs_state
= si_bind_gs_shader
;
2762 sctx
->b
.b
.bind_fs_state
= si_bind_ps_shader
;
2764 sctx
->b
.b
.delete_vs_state
= si_delete_shader_selector
;
2765 sctx
->b
.b
.delete_tcs_state
= si_delete_shader_selector
;
2766 sctx
->b
.b
.delete_tes_state
= si_delete_shader_selector
;
2767 sctx
->b
.b
.delete_gs_state
= si_delete_shader_selector
;
2768 sctx
->b
.b
.delete_fs_state
= si_delete_shader_selector
;