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>
29 #include "si_shader.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "util/u_memory.h"
34 #include "util/u_simple_shaders.h"
36 static void si_shader_es(struct si_shader
*shader
)
38 struct si_pm4_state
*pm4
;
39 unsigned num_sgprs
, num_user_sgprs
;
40 unsigned vgpr_comp_cnt
;
43 pm4
= shader
->pm4
= CALLOC_STRUCT(si_pm4_state
);
48 va
= shader
->bo
->gpu_address
;
49 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_DATA
);
51 vgpr_comp_cnt
= shader
->uses_instanceid
? 3 : 0;
53 num_user_sgprs
= SI_VS_NUM_USER_SGPR
;
54 num_sgprs
= shader
->num_sgprs
;
55 /* One SGPR after user SGPRs is pre-loaded with es2gs_offset */
56 if ((num_user_sgprs
+ 1) > num_sgprs
) {
57 /* Last 2 reserved SGPRs are used for VCC */
58 num_sgprs
= num_user_sgprs
+ 1 + 2;
60 assert(num_sgprs
<= 104);
62 si_pm4_set_reg(pm4
, R_00B320_SPI_SHADER_PGM_LO_ES
, va
>> 8);
63 si_pm4_set_reg(pm4
, R_00B324_SPI_SHADER_PGM_HI_ES
, va
>> 40);
64 si_pm4_set_reg(pm4
, R_00B328_SPI_SHADER_PGM_RSRC1_ES
,
65 S_00B328_VGPRS((shader
->num_vgprs
- 1) / 4) |
66 S_00B328_SGPRS((num_sgprs
- 1) / 8) |
67 S_00B328_VGPR_COMP_CNT(vgpr_comp_cnt
) |
68 S_00B328_DX10_CLAMP(1));
69 si_pm4_set_reg(pm4
, R_00B32C_SPI_SHADER_PGM_RSRC2_ES
,
70 S_00B32C_USER_SGPR(num_user_sgprs
));
73 static void si_shader_gs(struct si_shader
*shader
)
75 unsigned gs_vert_itemsize
= shader
->selector
->info
.num_outputs
* (16 >> 2);
76 unsigned gs_max_vert_out
= shader
->selector
->gs_max_out_vertices
;
77 unsigned gsvs_itemsize
= gs_vert_itemsize
* gs_max_vert_out
;
79 struct si_pm4_state
*pm4
;
80 unsigned num_sgprs
, num_user_sgprs
;
83 /* The GSVS_RING_ITEMSIZE register takes 15 bits */
84 assert(gsvs_itemsize
< (1 << 15));
86 pm4
= shader
->pm4
= CALLOC_STRUCT(si_pm4_state
);
91 if (gs_max_vert_out
<= 128) {
92 cut_mode
= V_028A40_GS_CUT_128
;
93 } else if (gs_max_vert_out
<= 256) {
94 cut_mode
= V_028A40_GS_CUT_256
;
95 } else if (gs_max_vert_out
<= 512) {
96 cut_mode
= V_028A40_GS_CUT_512
;
98 assert(gs_max_vert_out
<= 1024);
99 cut_mode
= V_028A40_GS_CUT_1024
;
102 si_pm4_set_reg(pm4
, R_028A40_VGT_GS_MODE
,
103 S_028A40_MODE(V_028A40_GS_SCENARIO_G
) |
104 S_028A40_CUT_MODE(cut_mode
)|
105 S_028A40_ES_WRITE_OPTIMIZE(1) |
106 S_028A40_GS_WRITE_OPTIMIZE(1));
108 si_pm4_set_reg(pm4
, R_028A60_VGT_GSVS_RING_OFFSET_1
, gsvs_itemsize
);
109 si_pm4_set_reg(pm4
, R_028A64_VGT_GSVS_RING_OFFSET_2
, gsvs_itemsize
);
110 si_pm4_set_reg(pm4
, R_028A68_VGT_GSVS_RING_OFFSET_3
, gsvs_itemsize
);
112 si_pm4_set_reg(pm4
, R_028AAC_VGT_ESGS_RING_ITEMSIZE
,
113 util_bitcount64(shader
->selector
->gs_used_inputs
) * (16 >> 2));
114 si_pm4_set_reg(pm4
, R_028AB0_VGT_GSVS_RING_ITEMSIZE
, gsvs_itemsize
);
116 si_pm4_set_reg(pm4
, R_028B38_VGT_GS_MAX_VERT_OUT
, gs_max_vert_out
);
118 si_pm4_set_reg(pm4
, R_028B5C_VGT_GS_VERT_ITEMSIZE
, gs_vert_itemsize
);
120 va
= shader
->bo
->gpu_address
;
121 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_DATA
);
122 si_pm4_set_reg(pm4
, R_00B220_SPI_SHADER_PGM_LO_GS
, va
>> 8);
123 si_pm4_set_reg(pm4
, R_00B224_SPI_SHADER_PGM_HI_GS
, va
>> 40);
125 num_user_sgprs
= SI_GS_NUM_USER_SGPR
;
126 num_sgprs
= shader
->num_sgprs
;
127 /* Two SGPRs after user SGPRs are pre-loaded with gs2vs_offset, gs_wave_id */
128 if ((num_user_sgprs
+ 2) > num_sgprs
) {
129 /* Last 2 reserved SGPRs are used for VCC */
130 num_sgprs
= num_user_sgprs
+ 2 + 2;
132 assert(num_sgprs
<= 104);
134 si_pm4_set_reg(pm4
, R_00B228_SPI_SHADER_PGM_RSRC1_GS
,
135 S_00B228_VGPRS((shader
->num_vgprs
- 1) / 4) |
136 S_00B228_SGPRS((num_sgprs
- 1) / 8) |
137 S_00B228_DX10_CLAMP(1));
138 si_pm4_set_reg(pm4
, R_00B22C_SPI_SHADER_PGM_RSRC2_GS
,
139 S_00B22C_USER_SGPR(num_user_sgprs
));
142 static void si_shader_vs(struct si_shader
*shader
)
144 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
145 struct si_pm4_state
*pm4
;
146 unsigned num_sgprs
, num_user_sgprs
;
147 unsigned nparams
, i
, vgpr_comp_cnt
;
149 unsigned window_space
=
150 shader
->selector
->info
.properties
[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION
];
152 pm4
= shader
->pm4
= CALLOC_STRUCT(si_pm4_state
);
157 va
= shader
->bo
->gpu_address
;
158 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_DATA
);
160 vgpr_comp_cnt
= shader
->uses_instanceid
? 3 : 0;
162 if (shader
->is_gs_copy_shader
)
163 num_user_sgprs
= SI_GSCOPY_NUM_USER_SGPR
;
165 num_user_sgprs
= SI_VS_NUM_USER_SGPR
;
167 num_sgprs
= shader
->num_sgprs
;
168 if (num_user_sgprs
> num_sgprs
) {
169 /* Last 2 reserved SGPRs are used for VCC */
170 num_sgprs
= num_user_sgprs
+ 2;
172 assert(num_sgprs
<= 104);
174 /* Certain attributes (position, psize, etc.) don't count as params.
175 * VS is required to export at least one param and r600_shader_from_tgsi()
176 * takes care of adding a dummy export.
178 for (nparams
= 0, i
= 0 ; i
< info
->num_outputs
; i
++) {
179 switch (info
->output_semantic_name
[i
]) {
180 case TGSI_SEMANTIC_CLIPVERTEX
:
181 case TGSI_SEMANTIC_POSITION
:
182 case TGSI_SEMANTIC_PSIZE
:
191 si_pm4_set_reg(pm4
, R_0286C4_SPI_VS_OUT_CONFIG
,
192 S_0286C4_VS_EXPORT_COUNT(nparams
- 1));
194 si_pm4_set_reg(pm4
, R_02870C_SPI_SHADER_POS_FORMAT
,
195 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP
) |
196 S_02870C_POS1_EXPORT_FORMAT(shader
->nr_pos_exports
> 1 ?
197 V_02870C_SPI_SHADER_4COMP
:
198 V_02870C_SPI_SHADER_NONE
) |
199 S_02870C_POS2_EXPORT_FORMAT(shader
->nr_pos_exports
> 2 ?
200 V_02870C_SPI_SHADER_4COMP
:
201 V_02870C_SPI_SHADER_NONE
) |
202 S_02870C_POS3_EXPORT_FORMAT(shader
->nr_pos_exports
> 3 ?
203 V_02870C_SPI_SHADER_4COMP
:
204 V_02870C_SPI_SHADER_NONE
));
206 si_pm4_set_reg(pm4
, R_00B120_SPI_SHADER_PGM_LO_VS
, va
>> 8);
207 si_pm4_set_reg(pm4
, R_00B124_SPI_SHADER_PGM_HI_VS
, va
>> 40);
208 si_pm4_set_reg(pm4
, R_00B128_SPI_SHADER_PGM_RSRC1_VS
,
209 S_00B128_VGPRS((shader
->num_vgprs
- 1) / 4) |
210 S_00B128_SGPRS((num_sgprs
- 1) / 8) |
211 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt
) |
212 S_00B128_DX10_CLAMP(1));
213 si_pm4_set_reg(pm4
, R_00B12C_SPI_SHADER_PGM_RSRC2_VS
,
214 S_00B12C_USER_SGPR(num_user_sgprs
) |
215 S_00B12C_SO_BASE0_EN(!!shader
->selector
->so
.stride
[0]) |
216 S_00B12C_SO_BASE1_EN(!!shader
->selector
->so
.stride
[1]) |
217 S_00B12C_SO_BASE2_EN(!!shader
->selector
->so
.stride
[2]) |
218 S_00B12C_SO_BASE3_EN(!!shader
->selector
->so
.stride
[3]) |
219 S_00B12C_SO_EN(!!shader
->selector
->so
.num_outputs
));
221 si_pm4_set_reg(pm4
, R_028818_PA_CL_VTE_CNTL
,
222 S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1));
224 si_pm4_set_reg(pm4
, R_028818_PA_CL_VTE_CNTL
,
225 S_028818_VTX_W0_FMT(1) |
226 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
227 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
228 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
231 static void si_shader_ps(struct si_shader
*shader
)
233 struct tgsi_shader_info
*info
= &shader
->selector
->info
;
234 struct si_pm4_state
*pm4
;
236 unsigned num_sgprs
, num_user_sgprs
;
237 unsigned spi_baryc_cntl
= 0, spi_ps_input_ena
;
240 pm4
= shader
->pm4
= CALLOC_STRUCT(si_pm4_state
);
245 for (i
= 0; i
< info
->num_inputs
; i
++) {
246 switch (info
->input_semantic_name
[i
]) {
247 case TGSI_SEMANTIC_POSITION
:
248 /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION
250 * 0 -> Position = pixel center (default)
251 * 1 -> Position = pixel centroid
252 * 2 -> Position = at sample position
254 switch (info
->input_interpolate_loc
[i
]) {
255 case TGSI_INTERPOLATE_LOC_CENTROID
:
256 spi_baryc_cntl
|= S_0286E0_POS_FLOAT_LOCATION(1);
258 case TGSI_INTERPOLATE_LOC_SAMPLE
:
259 spi_baryc_cntl
|= S_0286E0_POS_FLOAT_LOCATION(2);
263 if (info
->properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
264 TGSI_FS_COORD_PIXEL_CENTER_INTEGER
)
265 spi_baryc_cntl
|= S_0286E0_POS_FLOAT_ULC(1);
270 si_pm4_set_reg(pm4
, R_0286E0_SPI_BARYC_CNTL
, spi_baryc_cntl
);
271 spi_ps_input_ena
= shader
->spi_ps_input_ena
;
272 /* we need to enable at least one of them, otherwise we hang the GPU */
273 assert(G_0286CC_PERSP_SAMPLE_ENA(spi_ps_input_ena
) ||
274 G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena
) ||
275 G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena
) ||
276 G_0286CC_PERSP_PULL_MODEL_ENA(spi_ps_input_ena
) ||
277 G_0286CC_LINEAR_SAMPLE_ENA(spi_ps_input_ena
) ||
278 G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena
) ||
279 G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena
) ||
280 G_0286CC_LINE_STIPPLE_TEX_ENA(spi_ps_input_ena
));
282 si_pm4_set_reg(pm4
, R_0286CC_SPI_PS_INPUT_ENA
, spi_ps_input_ena
);
283 si_pm4_set_reg(pm4
, R_0286D0_SPI_PS_INPUT_ADDR
, spi_ps_input_ena
);
285 si_pm4_set_reg(pm4
, R_028710_SPI_SHADER_Z_FORMAT
, shader
->spi_shader_z_format
);
286 si_pm4_set_reg(pm4
, R_028714_SPI_SHADER_COL_FORMAT
,
287 shader
->spi_shader_col_format
);
288 si_pm4_set_reg(pm4
, R_02823C_CB_SHADER_MASK
, shader
->cb_shader_mask
);
290 va
= shader
->bo
->gpu_address
;
291 si_pm4_add_bo(pm4
, shader
->bo
, RADEON_USAGE_READ
, RADEON_PRIO_SHADER_DATA
);
292 si_pm4_set_reg(pm4
, R_00B020_SPI_SHADER_PGM_LO_PS
, va
>> 8);
293 si_pm4_set_reg(pm4
, R_00B024_SPI_SHADER_PGM_HI_PS
, va
>> 40);
295 num_user_sgprs
= SI_PS_NUM_USER_SGPR
;
296 num_sgprs
= shader
->num_sgprs
;
297 /* One SGPR after user SGPRs is pre-loaded with {prim_mask, lds_offset} */
298 if ((num_user_sgprs
+ 1) > num_sgprs
) {
299 /* Last 2 reserved SGPRs are used for VCC */
300 num_sgprs
= num_user_sgprs
+ 1 + 2;
302 assert(num_sgprs
<= 104);
304 si_pm4_set_reg(pm4
, R_00B028_SPI_SHADER_PGM_RSRC1_PS
,
305 S_00B028_VGPRS((shader
->num_vgprs
- 1) / 4) |
306 S_00B028_SGPRS((num_sgprs
- 1) / 8) |
307 S_00B028_DX10_CLAMP(1));
308 si_pm4_set_reg(pm4
, R_00B02C_SPI_SHADER_PGM_RSRC2_PS
,
309 S_00B02C_EXTRA_LDS_SIZE(shader
->lds_size
) |
310 S_00B02C_USER_SGPR(num_user_sgprs
));
313 static void si_shader_init_pm4_state(struct si_shader
*shader
)
315 switch (shader
->selector
->type
) {
316 case PIPE_SHADER_VERTEX
:
317 if (shader
->key
.vs
.as_es
)
318 si_shader_es(shader
);
320 si_shader_vs(shader
);
322 case PIPE_SHADER_GEOMETRY
:
323 si_shader_gs(shader
);
324 si_shader_vs(shader
->gs_copy_shader
);
326 case PIPE_SHADER_FRAGMENT
:
327 si_shader_ps(shader
);
334 /* Compute the key for the hw shader variant */
335 static INLINE
void si_shader_selector_key(struct pipe_context
*ctx
,
336 struct si_shader_selector
*sel
,
337 union si_shader_key
*key
)
339 struct si_context
*sctx
= (struct si_context
*)ctx
;
340 memset(key
, 0, sizeof(*key
));
342 if (sel
->type
== PIPE_SHADER_VERTEX
) {
344 if (!sctx
->vertex_elements
)
347 for (i
= 0; i
< sctx
->vertex_elements
->count
; ++i
)
348 key
->vs
.instance_divisors
[i
] = sctx
->vertex_elements
->elements
[i
].instance_divisor
;
350 if (sctx
->gs_shader
) {
352 key
->vs
.gs_used_inputs
= sctx
->gs_shader
->gs_used_inputs
;
354 } else if (sel
->type
== PIPE_SHADER_FRAGMENT
) {
355 if (sel
->info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
])
356 key
->ps
.last_cbuf
= MAX2(sctx
->framebuffer
.state
.nr_cbufs
, 1) - 1;
357 key
->ps
.export_16bpc
= sctx
->framebuffer
.export_16bpc
;
359 if (sctx
->queued
.named
.rasterizer
) {
360 key
->ps
.color_two_side
= sctx
->queued
.named
.rasterizer
->two_side
;
362 if (sctx
->queued
.named
.blend
) {
363 key
->ps
.alpha_to_one
= sctx
->queued
.named
.blend
->alpha_to_one
&&
364 sctx
->queued
.named
.rasterizer
->multisample_enable
&&
365 !sctx
->framebuffer
.cb0_is_integer
;
368 if (sctx
->queued
.named
.dsa
) {
369 key
->ps
.alpha_func
= sctx
->queued
.named
.dsa
->alpha_func
;
371 /* Alpha-test should be disabled if colorbuffer 0 is integer. */
372 if (sctx
->framebuffer
.cb0_is_integer
)
373 key
->ps
.alpha_func
= PIPE_FUNC_ALWAYS
;
375 key
->ps
.alpha_func
= PIPE_FUNC_ALWAYS
;
380 /* Select the hw shader variant depending on the current state. */
381 static int si_shader_select(struct pipe_context
*ctx
,
382 struct si_shader_selector
*sel
)
384 union si_shader_key key
;
385 struct si_shader
* shader
= NULL
;
388 si_shader_selector_key(ctx
, sel
, &key
);
390 /* Check if we don't need to change anything.
391 * This path is also used for most shaders that don't need multiple
392 * variants, it will cost just a computation of the key and this
394 if (likely(sel
->current
&& memcmp(&sel
->current
->key
, &key
, sizeof(key
)) == 0)) {
398 /* lookup if we have other variants in the list */
399 if (sel
->num_shaders
> 1) {
400 struct si_shader
*p
= sel
->current
, *c
= p
->next_variant
;
402 while (c
&& memcmp(&c
->key
, &key
, sizeof(key
)) != 0) {
408 p
->next_variant
= c
->next_variant
;
414 shader
->next_variant
= sel
->current
;
415 sel
->current
= shader
;
417 shader
= CALLOC(1, sizeof(struct si_shader
));
418 shader
->selector
= sel
;
421 shader
->next_variant
= sel
->current
;
422 sel
->current
= shader
;
423 r
= si_shader_create((struct si_screen
*)ctx
->screen
, shader
);
425 R600_ERR("Failed to build shader variant (type=%u) %d\n",
431 si_shader_init_pm4_state(shader
);
438 static void *si_create_shader_state(struct pipe_context
*ctx
,
439 const struct pipe_shader_state
*state
,
440 unsigned pipe_shader_type
)
442 struct si_shader_selector
*sel
= CALLOC_STRUCT(si_shader_selector
);
445 sel
->type
= pipe_shader_type
;
446 sel
->tokens
= tgsi_dup_tokens(state
->tokens
);
447 sel
->so
= state
->stream_output
;
448 tgsi_scan_shader(state
->tokens
, &sel
->info
);
450 switch (pipe_shader_type
) {
451 case PIPE_SHADER_GEOMETRY
:
452 sel
->gs_output_prim
=
453 sel
->info
.properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
];
454 sel
->gs_max_out_vertices
=
455 sel
->info
.properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
457 for (i
= 0; i
< sel
->info
.num_inputs
; i
++) {
458 unsigned name
= sel
->info
.input_semantic_name
[i
];
459 unsigned index
= sel
->info
.input_semantic_index
[i
];
462 case TGSI_SEMANTIC_PRIMID
:
465 sel
->gs_used_inputs
|=
466 1llu << si_shader_io_get_unique_index(name
, index
);
474 static void *si_create_fs_state(struct pipe_context
*ctx
,
475 const struct pipe_shader_state
*state
)
477 return si_create_shader_state(ctx
, state
, PIPE_SHADER_FRAGMENT
);
480 static void *si_create_gs_state(struct pipe_context
*ctx
,
481 const struct pipe_shader_state
*state
)
483 return si_create_shader_state(ctx
, state
, PIPE_SHADER_GEOMETRY
);
486 static void *si_create_vs_state(struct pipe_context
*ctx
,
487 const struct pipe_shader_state
*state
)
489 return si_create_shader_state(ctx
, state
, PIPE_SHADER_VERTEX
);
492 static void si_bind_vs_shader(struct pipe_context
*ctx
, void *state
)
494 struct si_context
*sctx
= (struct si_context
*)ctx
;
495 struct si_shader_selector
*sel
= state
;
497 if (sctx
->vs_shader
== sel
|| !sel
)
500 sctx
->vs_shader
= sel
;
501 sctx
->clip_regs
.dirty
= true;
504 static void si_bind_gs_shader(struct pipe_context
*ctx
, void *state
)
506 struct si_context
*sctx
= (struct si_context
*)ctx
;
507 struct si_shader_selector
*sel
= state
;
509 if (sctx
->gs_shader
== sel
)
512 sctx
->gs_shader
= sel
;
513 sctx
->clip_regs
.dirty
= true;
514 sctx
->last_rast_prim
= -1; /* reset this so that it gets updated */
517 static void si_make_dummy_ps(struct si_context
*sctx
)
519 if (!sctx
->dummy_pixel_shader
) {
520 sctx
->dummy_pixel_shader
=
521 util_make_fragment_cloneinput_shader(&sctx
->b
.b
, 0,
522 TGSI_SEMANTIC_GENERIC
,
523 TGSI_INTERPOLATE_CONSTANT
);
527 static void si_bind_ps_shader(struct pipe_context
*ctx
, void *state
)
529 struct si_context
*sctx
= (struct si_context
*)ctx
;
530 struct si_shader_selector
*sel
= state
;
532 /* skip if supplied shader is one already in use */
533 if (sctx
->ps_shader
== sel
)
536 /* use a dummy shader if binding a NULL shader */
538 si_make_dummy_ps(sctx
);
539 sel
= sctx
->dummy_pixel_shader
;
542 sctx
->ps_shader
= sel
;
545 static void si_delete_shader_selector(struct pipe_context
*ctx
,
546 struct si_shader_selector
*sel
)
548 struct si_context
*sctx
= (struct si_context
*)ctx
;
549 struct si_shader
*p
= sel
->current
, *c
;
553 if (sel
->type
== PIPE_SHADER_GEOMETRY
) {
554 si_pm4_delete_state(sctx
, gs
, p
->pm4
);
555 si_pm4_delete_state(sctx
, vs
, p
->gs_copy_shader
->pm4
);
556 } else if (sel
->type
== PIPE_SHADER_FRAGMENT
)
557 si_pm4_delete_state(sctx
, ps
, p
->pm4
);
558 else if (p
->key
.vs
.as_es
)
559 si_pm4_delete_state(sctx
, es
, p
->pm4
);
561 si_pm4_delete_state(sctx
, vs
, p
->pm4
);
562 si_shader_destroy(ctx
, p
);
571 static void si_delete_vs_shader(struct pipe_context
*ctx
, void *state
)
573 struct si_context
*sctx
= (struct si_context
*)ctx
;
574 struct si_shader_selector
*sel
= (struct si_shader_selector
*)state
;
576 if (sctx
->vs_shader
== sel
) {
577 sctx
->vs_shader
= NULL
;
580 si_delete_shader_selector(ctx
, sel
);
583 static void si_delete_gs_shader(struct pipe_context
*ctx
, void *state
)
585 struct si_context
*sctx
= (struct si_context
*)ctx
;
586 struct si_shader_selector
*sel
= (struct si_shader_selector
*)state
;
588 if (sctx
->gs_shader
== sel
) {
589 sctx
->gs_shader
= NULL
;
592 si_delete_shader_selector(ctx
, sel
);
595 static void si_delete_ps_shader(struct pipe_context
*ctx
, void *state
)
597 struct si_context
*sctx
= (struct si_context
*)ctx
;
598 struct si_shader_selector
*sel
= (struct si_shader_selector
*)state
;
600 if (sctx
->ps_shader
== sel
) {
601 sctx
->ps_shader
= NULL
;
604 si_delete_shader_selector(ctx
, sel
);
607 static void si_update_spi_map(struct si_context
*sctx
)
609 struct si_shader
*ps
= sctx
->ps_shader
->current
;
610 struct si_shader
*vs
= si_get_vs_state(sctx
);
611 struct tgsi_shader_info
*psinfo
= &ps
->selector
->info
;
612 struct tgsi_shader_info
*vsinfo
= &vs
->selector
->info
;
613 struct si_pm4_state
*pm4
= CALLOC_STRUCT(si_pm4_state
);
616 for (i
= 0; i
< psinfo
->num_inputs
; i
++) {
617 unsigned name
= psinfo
->input_semantic_name
[i
];
618 unsigned index
= psinfo
->input_semantic_index
[i
];
619 unsigned interpolate
= psinfo
->input_interpolate
[i
];
620 unsigned param_offset
= ps
->ps_input_param_offset
[i
];
622 if (name
== TGSI_SEMANTIC_POSITION
||
623 name
== TGSI_SEMANTIC_FACE
)
624 /* Read from preloaded VGPRs, not parameters */
630 if (interpolate
== TGSI_INTERPOLATE_CONSTANT
||
631 (interpolate
== TGSI_INTERPOLATE_COLOR
&& sctx
->flatshade
))
632 tmp
|= S_028644_FLAT_SHADE(1);
634 if (name
== TGSI_SEMANTIC_GENERIC
&&
635 sctx
->sprite_coord_enable
& (1 << index
)) {
636 tmp
|= S_028644_PT_SPRITE_TEX(1);
639 for (j
= 0; j
< vsinfo
->num_outputs
; j
++) {
640 if (name
== vsinfo
->output_semantic_name
[j
] &&
641 index
== vsinfo
->output_semantic_index
[j
]) {
642 tmp
|= S_028644_OFFSET(vs
->vs_output_param_offset
[j
]);
647 if (j
== vsinfo
->num_outputs
) {
648 /* No corresponding output found, load defaults into input */
649 tmp
|= S_028644_OFFSET(0x20);
653 R_028644_SPI_PS_INPUT_CNTL_0
+ param_offset
* 4,
656 if (name
== TGSI_SEMANTIC_COLOR
&&
657 ps
->key
.ps
.color_two_side
) {
658 name
= TGSI_SEMANTIC_BCOLOR
;
664 si_pm4_set_reg(pm4
, R_0286D8_SPI_PS_IN_CONTROL
,
665 S_0286D8_NUM_INTERP(ps
->nparam
) |
666 S_0286D8_BC_OPTIMIZE_DISABLE(sctx
->bc_optimize_disable
));
668 si_pm4_set_state(sctx
, spi
, pm4
);
671 /* Initialize state related to ESGS / GSVS ring buffers */
672 static void si_init_gs_rings(struct si_context
*sctx
)
674 unsigned esgs_ring_size
= 128 * 1024;
675 unsigned gsvs_ring_size
= 64 * 1024 * 1024;
677 assert(!sctx
->gs_rings
);
678 sctx
->gs_rings
= CALLOC_STRUCT(si_pm4_state
);
680 sctx
->esgs_ring
= pipe_buffer_create(sctx
->b
.b
.screen
, PIPE_BIND_CUSTOM
,
681 PIPE_USAGE_DEFAULT
, esgs_ring_size
);
683 sctx
->gsvs_ring
= pipe_buffer_create(sctx
->b
.b
.screen
, PIPE_BIND_CUSTOM
,
684 PIPE_USAGE_DEFAULT
, gsvs_ring_size
);
686 if (sctx
->b
.chip_class
>= CIK
) {
687 si_pm4_set_reg(sctx
->gs_rings
, R_030900_VGT_ESGS_RING_SIZE
,
688 esgs_ring_size
/ 256);
689 si_pm4_set_reg(sctx
->gs_rings
, R_030904_VGT_GSVS_RING_SIZE
,
690 gsvs_ring_size
/ 256);
692 si_pm4_set_reg(sctx
->gs_rings
, R_0088C8_VGT_ESGS_RING_SIZE
,
693 esgs_ring_size
/ 256);
694 si_pm4_set_reg(sctx
->gs_rings
, R_0088CC_VGT_GSVS_RING_SIZE
,
695 gsvs_ring_size
/ 256);
698 si_set_ring_buffer(&sctx
->b
.b
, PIPE_SHADER_VERTEX
, SI_RING_ESGS
,
699 sctx
->esgs_ring
, 0, esgs_ring_size
,
701 si_set_ring_buffer(&sctx
->b
.b
, PIPE_SHADER_GEOMETRY
, SI_RING_ESGS
,
702 sctx
->esgs_ring
, 0, esgs_ring_size
,
704 si_set_ring_buffer(&sctx
->b
.b
, PIPE_SHADER_VERTEX
, SI_RING_GSVS
,
705 sctx
->gsvs_ring
, 0, gsvs_ring_size
,
709 void si_update_shaders(struct si_context
*sctx
)
711 struct pipe_context
*ctx
= (struct pipe_context
*)sctx
;
712 struct si_state_rasterizer
*rs
= sctx
->queued
.named
.rasterizer
;
713 bool bc_optimize_disable
;
715 if (sctx
->gs_shader
) {
716 si_shader_select(ctx
, sctx
->gs_shader
);
717 si_pm4_bind_state(sctx
, gs
, sctx
->gs_shader
->current
->pm4
);
718 si_pm4_bind_state(sctx
, vs
, sctx
->gs_shader
->current
->gs_copy_shader
->pm4
);
720 sctx
->b
.streamout
.stride_in_dw
= sctx
->gs_shader
->so
.stride
;
722 si_shader_select(ctx
, sctx
->vs_shader
);
723 si_pm4_bind_state(sctx
, es
, sctx
->vs_shader
->current
->pm4
);
726 si_init_gs_rings(sctx
);
727 if (sctx
->emitted
.named
.gs_rings
!= sctx
->gs_rings
)
728 sctx
->b
.flags
|= R600_CONTEXT_VGT_FLUSH
;
729 si_pm4_bind_state(sctx
, gs_rings
, sctx
->gs_rings
);
731 si_set_ring_buffer(ctx
, PIPE_SHADER_GEOMETRY
, SI_RING_GSVS
,
733 sctx
->gs_shader
->gs_max_out_vertices
*
734 sctx
->gs_shader
->info
.num_outputs
* 16,
735 64, true, true, 4, 16);
738 sctx
->gs_on
= CALLOC_STRUCT(si_pm4_state
);
740 si_pm4_set_reg(sctx
->gs_on
, R_028B54_VGT_SHADER_STAGES_EN
,
741 S_028B54_ES_EN(V_028B54_ES_STAGE_REAL
) |
743 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER
));
745 si_pm4_bind_state(sctx
, gs_onoff
, sctx
->gs_on
);
747 si_shader_select(ctx
, sctx
->vs_shader
);
748 si_pm4_bind_state(sctx
, vs
, sctx
->vs_shader
->current
->pm4
);
750 sctx
->b
.streamout
.stride_in_dw
= sctx
->vs_shader
->so
.stride
;
753 sctx
->gs_off
= CALLOC_STRUCT(si_pm4_state
);
755 si_pm4_set_reg(sctx
->gs_off
, R_028A40_VGT_GS_MODE
, 0);
756 si_pm4_set_reg(sctx
->gs_off
, R_028B54_VGT_SHADER_STAGES_EN
, 0);
758 si_pm4_bind_state(sctx
, gs_onoff
, sctx
->gs_off
);
759 si_pm4_bind_state(sctx
, gs_rings
, NULL
);
760 si_pm4_bind_state(sctx
, gs
, NULL
);
761 si_pm4_bind_state(sctx
, es
, NULL
);
764 si_shader_select(ctx
, sctx
->ps_shader
);
766 if (!sctx
->ps_shader
->current
) {
767 struct si_shader_selector
*sel
;
769 /* use a dummy shader if compiling the shader (variant) failed */
770 si_make_dummy_ps(sctx
);
771 sel
= sctx
->dummy_pixel_shader
;
772 si_shader_select(ctx
, sel
);
773 sctx
->ps_shader
->current
= sel
->current
;
776 si_pm4_bind_state(sctx
, ps
, sctx
->ps_shader
->current
->pm4
);
778 /* Whether CENTER != CENTROID. */
779 bc_optimize_disable
= sctx
->framebuffer
.nr_samples
> 1 &&
780 rs
->multisample_enable
&&
781 sctx
->ps_shader
->info
.uses_centroid
;
783 if (si_pm4_state_changed(sctx
, ps
) || si_pm4_state_changed(sctx
, vs
) ||
784 sctx
->sprite_coord_enable
!= rs
->sprite_coord_enable
||
785 sctx
->flatshade
!= rs
->flatshade
||
786 sctx
->bc_optimize_disable
!= bc_optimize_disable
) {
787 sctx
->sprite_coord_enable
= rs
->sprite_coord_enable
;
788 sctx
->flatshade
= rs
->flatshade
;
789 sctx
->bc_optimize_disable
= bc_optimize_disable
;
790 si_update_spi_map(sctx
);
793 if (sctx
->ps_db_shader_control
!= sctx
->ps_shader
->current
->db_shader_control
) {
794 sctx
->ps_db_shader_control
= sctx
->ps_shader
->current
->db_shader_control
;
795 sctx
->db_render_state
.dirty
= true;
799 void si_init_shader_functions(struct si_context
*sctx
)
801 sctx
->b
.b
.create_vs_state
= si_create_vs_state
;
802 sctx
->b
.b
.create_gs_state
= si_create_gs_state
;
803 sctx
->b
.b
.create_fs_state
= si_create_fs_state
;
805 sctx
->b
.b
.bind_vs_state
= si_bind_vs_shader
;
806 sctx
->b
.b
.bind_gs_state
= si_bind_gs_shader
;
807 sctx
->b
.b
.bind_fs_state
= si_bind_ps_shader
;
809 sctx
->b
.b
.delete_vs_state
= si_delete_vs_shader
;
810 sctx
->b
.b
.delete_gs_state
= si_delete_gs_shader
;
811 sctx
->b
.b
.delete_fs_state
= si_delete_ps_shader
;