radeonsi: add derived tessellation state
[mesa.git] / src / gallium / drivers / radeonsi / si_state_shaders.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Christian König <christian.koenig@amd.com>
25 * Marek Olšák <maraeo@gmail.com>
26 */
27
28 #include "si_pipe.h"
29 #include "si_shader.h"
30 #include "sid.h"
31
32 #include "tgsi/tgsi_parse.h"
33 #include "tgsi/tgsi_ureg.h"
34 #include "util/u_memory.h"
35 #include "util/u_simple_shaders.h"
36
37 static void si_set_tesseval_regs(struct si_shader *shader,
38 struct si_pm4_state *pm4)
39 {
40 struct tgsi_shader_info *info = &shader->selector->info;
41 unsigned tes_prim_mode = info->properties[TGSI_PROPERTY_TES_PRIM_MODE];
42 unsigned tes_spacing = info->properties[TGSI_PROPERTY_TES_SPACING];
43 bool tes_vertex_order_cw = info->properties[TGSI_PROPERTY_TES_VERTEX_ORDER_CW];
44 bool tes_point_mode = info->properties[TGSI_PROPERTY_TES_POINT_MODE];
45 unsigned type, partitioning, topology;
46
47 switch (tes_prim_mode) {
48 case PIPE_PRIM_LINES:
49 type = V_028B6C_TESS_ISOLINE;
50 break;
51 case PIPE_PRIM_TRIANGLES:
52 type = V_028B6C_TESS_TRIANGLE;
53 break;
54 case PIPE_PRIM_QUADS:
55 type = V_028B6C_TESS_QUAD;
56 break;
57 default:
58 assert(0);
59 return;
60 }
61
62 switch (tes_spacing) {
63 case PIPE_TESS_SPACING_FRACTIONAL_ODD:
64 partitioning = V_028B6C_PART_FRAC_ODD;
65 break;
66 case PIPE_TESS_SPACING_FRACTIONAL_EVEN:
67 partitioning = V_028B6C_PART_FRAC_EVEN;
68 break;
69 case PIPE_TESS_SPACING_EQUAL:
70 partitioning = V_028B6C_PART_INTEGER;
71 break;
72 default:
73 assert(0);
74 return;
75 }
76
77 if (tes_point_mode)
78 topology = V_028B6C_OUTPUT_POINT;
79 else if (tes_prim_mode == PIPE_PRIM_LINES)
80 topology = V_028B6C_OUTPUT_LINE;
81 else if (tes_vertex_order_cw)
82 /* for some reason, this must be the other way around */
83 topology = V_028B6C_OUTPUT_TRIANGLE_CCW;
84 else
85 topology = V_028B6C_OUTPUT_TRIANGLE_CW;
86
87 si_pm4_set_reg(pm4, R_028B6C_VGT_TF_PARAM,
88 S_028B6C_TYPE(type) |
89 S_028B6C_PARTITIONING(partitioning) |
90 S_028B6C_TOPOLOGY(topology));
91 }
92
93 static void si_shader_ls(struct si_shader *shader)
94 {
95 struct si_pm4_state *pm4;
96 unsigned num_sgprs, num_user_sgprs;
97 unsigned vgpr_comp_cnt;
98 uint64_t va;
99
100 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
101 if (pm4 == NULL)
102 return;
103
104 va = shader->bo->gpu_address;
105 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
106
107 /* We need at least 2 components for LS.
108 * VGPR0-3: (VertexID, RelAutoindex, ???, InstanceID). */
109 vgpr_comp_cnt = shader->uses_instanceid ? 3 : 1;
110
111 num_user_sgprs = SI_LS_NUM_USER_SGPR;
112 num_sgprs = shader->num_sgprs;
113 if (num_user_sgprs > num_sgprs) {
114 /* Last 2 reserved SGPRs are used for VCC */
115 num_sgprs = num_user_sgprs + 2;
116 }
117 assert(num_sgprs <= 104);
118
119 si_pm4_set_reg(pm4, R_00B520_SPI_SHADER_PGM_LO_LS, va >> 8);
120 si_pm4_set_reg(pm4, R_00B524_SPI_SHADER_PGM_HI_LS, va >> 40);
121
122 shader->ls_rsrc1 = S_00B528_VGPRS((shader->num_vgprs - 1) / 4) |
123 S_00B528_SGPRS((num_sgprs - 1) / 8) |
124 S_00B528_VGPR_COMP_CNT(vgpr_comp_cnt);
125 shader->ls_rsrc2 = S_00B52C_USER_SGPR(num_user_sgprs);
126 }
127
128 static void si_shader_hs(struct si_shader *shader)
129 {
130 struct si_pm4_state *pm4;
131 unsigned num_sgprs, num_user_sgprs;
132 uint64_t va;
133
134 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
135 if (pm4 == NULL)
136 return;
137
138 va = shader->bo->gpu_address;
139 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
140
141 num_user_sgprs = SI_TCS_NUM_USER_SGPR;
142 num_sgprs = shader->num_sgprs;
143 /* One SGPR after user SGPRs is pre-loaded with tessellation factor
144 * buffer offset. */
145 if ((num_user_sgprs + 1) > num_sgprs) {
146 /* Last 2 reserved SGPRs are used for VCC */
147 num_sgprs = num_user_sgprs + 1 + 2;
148 }
149 assert(num_sgprs <= 104);
150
151 si_pm4_set_reg(pm4, R_00B420_SPI_SHADER_PGM_LO_HS, va >> 8);
152 si_pm4_set_reg(pm4, R_00B424_SPI_SHADER_PGM_HI_HS, va >> 40);
153 si_pm4_set_reg(pm4, R_00B428_SPI_SHADER_PGM_RSRC1_HS,
154 S_00B428_VGPRS((shader->num_vgprs - 1) / 4) |
155 S_00B428_SGPRS((num_sgprs - 1) / 8));
156 si_pm4_set_reg(pm4, R_00B42C_SPI_SHADER_PGM_RSRC2_HS,
157 S_00B42C_USER_SGPR(num_user_sgprs));
158 }
159
160 static void si_shader_es(struct si_shader *shader)
161 {
162 struct si_pm4_state *pm4;
163 unsigned num_sgprs, num_user_sgprs;
164 unsigned vgpr_comp_cnt;
165 uint64_t va;
166
167 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
168
169 if (pm4 == NULL)
170 return;
171
172 va = shader->bo->gpu_address;
173 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
174
175 if (shader->selector->type == PIPE_SHADER_VERTEX) {
176 vgpr_comp_cnt = shader->uses_instanceid ? 3 : 0;
177 num_user_sgprs = SI_VS_NUM_USER_SGPR;
178 } else if (shader->selector->type == PIPE_SHADER_TESS_EVAL) {
179 vgpr_comp_cnt = 3; /* all components are needed for TES */
180 num_user_sgprs = SI_TES_NUM_USER_SGPR;
181 } else
182 assert(0);
183
184 num_sgprs = shader->num_sgprs;
185 /* One SGPR after user SGPRs is pre-loaded with es2gs_offset */
186 if ((num_user_sgprs + 1) > num_sgprs) {
187 /* Last 2 reserved SGPRs are used for VCC */
188 num_sgprs = num_user_sgprs + 1 + 2;
189 }
190 assert(num_sgprs <= 104);
191
192 si_pm4_set_reg(pm4, R_00B320_SPI_SHADER_PGM_LO_ES, va >> 8);
193 si_pm4_set_reg(pm4, R_00B324_SPI_SHADER_PGM_HI_ES, va >> 40);
194 si_pm4_set_reg(pm4, R_00B328_SPI_SHADER_PGM_RSRC1_ES,
195 S_00B328_VGPRS((shader->num_vgprs - 1) / 4) |
196 S_00B328_SGPRS((num_sgprs - 1) / 8) |
197 S_00B328_VGPR_COMP_CNT(vgpr_comp_cnt) |
198 S_00B328_DX10_CLAMP(shader->dx10_clamp_mode));
199 si_pm4_set_reg(pm4, R_00B32C_SPI_SHADER_PGM_RSRC2_ES,
200 S_00B32C_USER_SGPR(num_user_sgprs) |
201 S_00B32C_SCRATCH_EN(shader->scratch_bytes_per_wave > 0));
202
203 if (shader->selector->type == PIPE_SHADER_TESS_EVAL)
204 si_set_tesseval_regs(shader, pm4);
205 }
206
207 static void si_shader_gs(struct si_shader *shader)
208 {
209 unsigned gs_vert_itemsize = shader->selector->info.num_outputs * (16 >> 2);
210 unsigned gs_max_vert_out = shader->selector->gs_max_out_vertices;
211 unsigned gsvs_itemsize = gs_vert_itemsize * gs_max_vert_out;
212 unsigned gs_num_invocations = shader->selector->gs_num_invocations;
213 unsigned cut_mode;
214 struct si_pm4_state *pm4;
215 unsigned num_sgprs, num_user_sgprs;
216 uint64_t va;
217
218 /* The GSVS_RING_ITEMSIZE register takes 15 bits */
219 assert(gsvs_itemsize < (1 << 15));
220
221 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
222
223 if (pm4 == NULL)
224 return;
225
226 if (gs_max_vert_out <= 128) {
227 cut_mode = V_028A40_GS_CUT_128;
228 } else if (gs_max_vert_out <= 256) {
229 cut_mode = V_028A40_GS_CUT_256;
230 } else if (gs_max_vert_out <= 512) {
231 cut_mode = V_028A40_GS_CUT_512;
232 } else {
233 assert(gs_max_vert_out <= 1024);
234 cut_mode = V_028A40_GS_CUT_1024;
235 }
236
237 si_pm4_set_reg(pm4, R_028A40_VGT_GS_MODE,
238 S_028A40_MODE(V_028A40_GS_SCENARIO_G) |
239 S_028A40_CUT_MODE(cut_mode)|
240 S_028A40_ES_WRITE_OPTIMIZE(1) |
241 S_028A40_GS_WRITE_OPTIMIZE(1));
242
243 si_pm4_set_reg(pm4, R_028A60_VGT_GSVS_RING_OFFSET_1, gsvs_itemsize);
244 si_pm4_set_reg(pm4, R_028A64_VGT_GSVS_RING_OFFSET_2, gsvs_itemsize);
245 si_pm4_set_reg(pm4, R_028A68_VGT_GSVS_RING_OFFSET_3, gsvs_itemsize);
246
247 si_pm4_set_reg(pm4, R_028AAC_VGT_ESGS_RING_ITEMSIZE,
248 util_bitcount64(shader->selector->inputs_read) * (16 >> 2));
249 si_pm4_set_reg(pm4, R_028AB0_VGT_GSVS_RING_ITEMSIZE, gsvs_itemsize);
250
251 si_pm4_set_reg(pm4, R_028B38_VGT_GS_MAX_VERT_OUT, gs_max_vert_out);
252
253 si_pm4_set_reg(pm4, R_028B5C_VGT_GS_VERT_ITEMSIZE, gs_vert_itemsize);
254
255 si_pm4_set_reg(pm4, R_028B90_VGT_GS_INSTANCE_CNT,
256 S_028B90_CNT(MIN2(gs_num_invocations, 127)) |
257 S_028B90_ENABLE(gs_num_invocations > 0));
258
259 va = shader->bo->gpu_address;
260 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
261 si_pm4_set_reg(pm4, R_00B220_SPI_SHADER_PGM_LO_GS, va >> 8);
262 si_pm4_set_reg(pm4, R_00B224_SPI_SHADER_PGM_HI_GS, va >> 40);
263
264 num_user_sgprs = SI_GS_NUM_USER_SGPR;
265 num_sgprs = shader->num_sgprs;
266 /* Two SGPRs after user SGPRs are pre-loaded with gs2vs_offset, gs_wave_id */
267 if ((num_user_sgprs + 2) > num_sgprs) {
268 /* Last 2 reserved SGPRs are used for VCC */
269 num_sgprs = num_user_sgprs + 2 + 2;
270 }
271 assert(num_sgprs <= 104);
272
273 si_pm4_set_reg(pm4, R_00B228_SPI_SHADER_PGM_RSRC1_GS,
274 S_00B228_VGPRS((shader->num_vgprs - 1) / 4) |
275 S_00B228_SGPRS((num_sgprs - 1) / 8) |
276 S_00B228_DX10_CLAMP(shader->dx10_clamp_mode));
277 si_pm4_set_reg(pm4, R_00B22C_SPI_SHADER_PGM_RSRC2_GS,
278 S_00B22C_USER_SGPR(num_user_sgprs) |
279 S_00B22C_SCRATCH_EN(shader->scratch_bytes_per_wave > 0));
280 }
281
282 static void si_shader_vs(struct si_shader *shader)
283 {
284 struct si_pm4_state *pm4;
285 unsigned num_sgprs, num_user_sgprs;
286 unsigned nparams, vgpr_comp_cnt;
287 uint64_t va;
288 unsigned window_space =
289 shader->selector->info.properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
290
291 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
292
293 if (pm4 == NULL)
294 return;
295
296 va = shader->bo->gpu_address;
297 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
298
299 if (shader->is_gs_copy_shader) {
300 vgpr_comp_cnt = 0; /* only VertexID is needed for GS-COPY. */
301 num_user_sgprs = SI_GSCOPY_NUM_USER_SGPR;
302 } else if (shader->selector->type == PIPE_SHADER_VERTEX) {
303 vgpr_comp_cnt = shader->uses_instanceid ? 3 : 0;
304 num_user_sgprs = SI_VS_NUM_USER_SGPR;
305 } else if (shader->selector->type == PIPE_SHADER_TESS_EVAL) {
306 vgpr_comp_cnt = 3; /* all components are needed for TES */
307 num_user_sgprs = SI_TES_NUM_USER_SGPR;
308 } else
309 assert(0);
310
311 num_sgprs = shader->num_sgprs;
312 if (num_user_sgprs > num_sgprs) {
313 /* Last 2 reserved SGPRs are used for VCC */
314 num_sgprs = num_user_sgprs + 2;
315 }
316 assert(num_sgprs <= 104);
317
318 /* VS is required to export at least one param. */
319 nparams = MAX2(shader->nr_param_exports, 1);
320 si_pm4_set_reg(pm4, R_0286C4_SPI_VS_OUT_CONFIG,
321 S_0286C4_VS_EXPORT_COUNT(nparams - 1));
322
323 si_pm4_set_reg(pm4, R_02870C_SPI_SHADER_POS_FORMAT,
324 S_02870C_POS0_EXPORT_FORMAT(V_02870C_SPI_SHADER_4COMP) |
325 S_02870C_POS1_EXPORT_FORMAT(shader->nr_pos_exports > 1 ?
326 V_02870C_SPI_SHADER_4COMP :
327 V_02870C_SPI_SHADER_NONE) |
328 S_02870C_POS2_EXPORT_FORMAT(shader->nr_pos_exports > 2 ?
329 V_02870C_SPI_SHADER_4COMP :
330 V_02870C_SPI_SHADER_NONE) |
331 S_02870C_POS3_EXPORT_FORMAT(shader->nr_pos_exports > 3 ?
332 V_02870C_SPI_SHADER_4COMP :
333 V_02870C_SPI_SHADER_NONE));
334
335 si_pm4_set_reg(pm4, R_00B120_SPI_SHADER_PGM_LO_VS, va >> 8);
336 si_pm4_set_reg(pm4, R_00B124_SPI_SHADER_PGM_HI_VS, va >> 40);
337 si_pm4_set_reg(pm4, R_00B128_SPI_SHADER_PGM_RSRC1_VS,
338 S_00B128_VGPRS((shader->num_vgprs - 1) / 4) |
339 S_00B128_SGPRS((num_sgprs - 1) / 8) |
340 S_00B128_VGPR_COMP_CNT(vgpr_comp_cnt) |
341 S_00B128_DX10_CLAMP(shader->dx10_clamp_mode));
342 si_pm4_set_reg(pm4, R_00B12C_SPI_SHADER_PGM_RSRC2_VS,
343 S_00B12C_USER_SGPR(num_user_sgprs) |
344 S_00B12C_SO_BASE0_EN(!!shader->selector->so.stride[0]) |
345 S_00B12C_SO_BASE1_EN(!!shader->selector->so.stride[1]) |
346 S_00B12C_SO_BASE2_EN(!!shader->selector->so.stride[2]) |
347 S_00B12C_SO_BASE3_EN(!!shader->selector->so.stride[3]) |
348 S_00B12C_SO_EN(!!shader->selector->so.num_outputs) |
349 S_00B12C_SCRATCH_EN(shader->scratch_bytes_per_wave > 0));
350 if (window_space)
351 si_pm4_set_reg(pm4, R_028818_PA_CL_VTE_CNTL,
352 S_028818_VTX_XY_FMT(1) | S_028818_VTX_Z_FMT(1));
353 else
354 si_pm4_set_reg(pm4, R_028818_PA_CL_VTE_CNTL,
355 S_028818_VTX_W0_FMT(1) |
356 S_028818_VPORT_X_SCALE_ENA(1) | S_028818_VPORT_X_OFFSET_ENA(1) |
357 S_028818_VPORT_Y_SCALE_ENA(1) | S_028818_VPORT_Y_OFFSET_ENA(1) |
358 S_028818_VPORT_Z_SCALE_ENA(1) | S_028818_VPORT_Z_OFFSET_ENA(1));
359
360 if (shader->selector->type == PIPE_SHADER_TESS_EVAL)
361 si_set_tesseval_regs(shader, pm4);
362 }
363
364 static void si_shader_ps(struct si_shader *shader)
365 {
366 struct tgsi_shader_info *info = &shader->selector->info;
367 struct si_pm4_state *pm4;
368 unsigned i, spi_ps_in_control;
369 unsigned num_sgprs, num_user_sgprs;
370 unsigned spi_baryc_cntl = 0, spi_ps_input_ena;
371 uint64_t va;
372
373 pm4 = shader->pm4 = CALLOC_STRUCT(si_pm4_state);
374
375 if (pm4 == NULL)
376 return;
377
378 for (i = 0; i < info->num_inputs; i++) {
379 switch (info->input_semantic_name[i]) {
380 case TGSI_SEMANTIC_POSITION:
381 /* SPI_BARYC_CNTL.POS_FLOAT_LOCATION
382 * Possible vaules:
383 * 0 -> Position = pixel center (default)
384 * 1 -> Position = pixel centroid
385 * 2 -> Position = at sample position
386 */
387 switch (info->input_interpolate_loc[i]) {
388 case TGSI_INTERPOLATE_LOC_CENTROID:
389 spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(1);
390 break;
391 case TGSI_INTERPOLATE_LOC_SAMPLE:
392 spi_baryc_cntl |= S_0286E0_POS_FLOAT_LOCATION(2);
393 break;
394 }
395
396 if (info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] ==
397 TGSI_FS_COORD_PIXEL_CENTER_INTEGER)
398 spi_baryc_cntl |= S_0286E0_POS_FLOAT_ULC(1);
399 break;
400 }
401 }
402
403 spi_ps_in_control = S_0286D8_NUM_INTERP(shader->nparam) |
404 S_0286D8_BC_OPTIMIZE_DISABLE(1);
405
406 si_pm4_set_reg(pm4, R_0286E0_SPI_BARYC_CNTL, spi_baryc_cntl);
407 spi_ps_input_ena = shader->spi_ps_input_ena;
408 /* we need to enable at least one of them, otherwise we hang the GPU */
409 assert(G_0286CC_PERSP_SAMPLE_ENA(spi_ps_input_ena) ||
410 G_0286CC_PERSP_CENTER_ENA(spi_ps_input_ena) ||
411 G_0286CC_PERSP_CENTROID_ENA(spi_ps_input_ena) ||
412 G_0286CC_PERSP_PULL_MODEL_ENA(spi_ps_input_ena) ||
413 G_0286CC_LINEAR_SAMPLE_ENA(spi_ps_input_ena) ||
414 G_0286CC_LINEAR_CENTER_ENA(spi_ps_input_ena) ||
415 G_0286CC_LINEAR_CENTROID_ENA(spi_ps_input_ena) ||
416 G_0286CC_LINE_STIPPLE_TEX_ENA(spi_ps_input_ena));
417
418 si_pm4_set_reg(pm4, R_0286CC_SPI_PS_INPUT_ENA, spi_ps_input_ena);
419 si_pm4_set_reg(pm4, R_0286D0_SPI_PS_INPUT_ADDR, spi_ps_input_ena);
420 si_pm4_set_reg(pm4, R_0286D8_SPI_PS_IN_CONTROL, spi_ps_in_control);
421
422 si_pm4_set_reg(pm4, R_028710_SPI_SHADER_Z_FORMAT, shader->spi_shader_z_format);
423 si_pm4_set_reg(pm4, R_028714_SPI_SHADER_COL_FORMAT,
424 shader->spi_shader_col_format);
425 si_pm4_set_reg(pm4, R_02823C_CB_SHADER_MASK, shader->cb_shader_mask);
426
427 va = shader->bo->gpu_address;
428 si_pm4_add_bo(pm4, shader->bo, RADEON_USAGE_READ, RADEON_PRIO_SHADER_DATA);
429 si_pm4_set_reg(pm4, R_00B020_SPI_SHADER_PGM_LO_PS, va >> 8);
430 si_pm4_set_reg(pm4, R_00B024_SPI_SHADER_PGM_HI_PS, va >> 40);
431
432 num_user_sgprs = SI_PS_NUM_USER_SGPR;
433 num_sgprs = shader->num_sgprs;
434 /* One SGPR after user SGPRs is pre-loaded with {prim_mask, lds_offset} */
435 if ((num_user_sgprs + 1) > num_sgprs) {
436 /* Last 2 reserved SGPRs are used for VCC */
437 num_sgprs = num_user_sgprs + 1 + 2;
438 }
439 assert(num_sgprs <= 104);
440
441 si_pm4_set_reg(pm4, R_00B028_SPI_SHADER_PGM_RSRC1_PS,
442 S_00B028_VGPRS((shader->num_vgprs - 1) / 4) |
443 S_00B028_SGPRS((num_sgprs - 1) / 8) |
444 S_00B028_DX10_CLAMP(shader->dx10_clamp_mode));
445 si_pm4_set_reg(pm4, R_00B02C_SPI_SHADER_PGM_RSRC2_PS,
446 S_00B02C_EXTRA_LDS_SIZE(shader->lds_size) |
447 S_00B02C_USER_SGPR(num_user_sgprs) |
448 S_00B32C_SCRATCH_EN(shader->scratch_bytes_per_wave > 0));
449 }
450
451 static void si_shader_init_pm4_state(struct si_shader *shader)
452 {
453
454 if (shader->pm4)
455 si_pm4_free_state_simple(shader->pm4);
456
457 switch (shader->selector->type) {
458 case PIPE_SHADER_VERTEX:
459 if (shader->key.vs.as_ls)
460 si_shader_ls(shader);
461 else if (shader->key.vs.as_es)
462 si_shader_es(shader);
463 else
464 si_shader_vs(shader);
465 break;
466 case PIPE_SHADER_TESS_CTRL:
467 si_shader_hs(shader);
468 break;
469 case PIPE_SHADER_TESS_EVAL:
470 if (shader->key.tes.as_es)
471 si_shader_es(shader);
472 else
473 si_shader_vs(shader);
474 break;
475 case PIPE_SHADER_GEOMETRY:
476 si_shader_gs(shader);
477 si_shader_vs(shader->gs_copy_shader);
478 break;
479 case PIPE_SHADER_FRAGMENT:
480 si_shader_ps(shader);
481 break;
482 default:
483 assert(0);
484 }
485 }
486
487 /* Compute the key for the hw shader variant */
488 static inline void si_shader_selector_key(struct pipe_context *ctx,
489 struct si_shader_selector *sel,
490 union si_shader_key *key)
491 {
492 struct si_context *sctx = (struct si_context *)ctx;
493 unsigned i;
494
495 memset(key, 0, sizeof(*key));
496
497 switch (sel->type) {
498 case PIPE_SHADER_VERTEX:
499 if (sctx->vertex_elements)
500 for (i = 0; i < sctx->vertex_elements->count; ++i)
501 key->vs.instance_divisors[i] =
502 sctx->vertex_elements->elements[i].instance_divisor;
503
504 if (sctx->tes_shader)
505 key->vs.as_ls = 1;
506 else if (sctx->gs_shader) {
507 key->vs.as_es = 1;
508 key->vs.es_enabled_outputs = sctx->gs_shader->inputs_read;
509 }
510 break;
511 case PIPE_SHADER_TESS_CTRL:
512 key->tcs.prim_mode =
513 sctx->tes_shader->info.properties[TGSI_PROPERTY_TES_PRIM_MODE];
514 break;
515 case PIPE_SHADER_TESS_EVAL:
516 if (sctx->gs_shader) {
517 key->tes.as_es = 1;
518 key->tes.es_enabled_outputs = sctx->gs_shader->inputs_read;
519 }
520 break;
521 case PIPE_SHADER_GEOMETRY:
522 break;
523 case PIPE_SHADER_FRAGMENT: {
524 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
525
526 if (sel->info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS])
527 key->ps.last_cbuf = MAX2(sctx->framebuffer.state.nr_cbufs, 1) - 1;
528 key->ps.export_16bpc = sctx->framebuffer.export_16bpc;
529
530 if (rs) {
531 bool is_poly = (sctx->current_rast_prim >= PIPE_PRIM_TRIANGLES &&
532 sctx->current_rast_prim <= PIPE_PRIM_POLYGON) ||
533 sctx->current_rast_prim >= PIPE_PRIM_TRIANGLES_ADJACENCY;
534 bool is_line = !is_poly && sctx->current_rast_prim != PIPE_PRIM_POINTS;
535
536 key->ps.color_two_side = rs->two_side;
537
538 if (sctx->queued.named.blend) {
539 key->ps.alpha_to_one = sctx->queued.named.blend->alpha_to_one &&
540 rs->multisample_enable &&
541 !sctx->framebuffer.cb0_is_integer;
542 }
543
544 key->ps.poly_stipple = rs->poly_stipple_enable && is_poly;
545 key->ps.poly_line_smoothing = ((is_poly && rs->poly_smooth) ||
546 (is_line && rs->line_smooth)) &&
547 sctx->framebuffer.nr_samples <= 1;
548 }
549
550 key->ps.alpha_func = PIPE_FUNC_ALWAYS;
551 /* Alpha-test should be disabled if colorbuffer 0 is integer. */
552 if (sctx->queued.named.dsa &&
553 !sctx->framebuffer.cb0_is_integer)
554 key->ps.alpha_func = sctx->queued.named.dsa->alpha_func;
555 break;
556 }
557 default:
558 assert(0);
559 }
560 }
561
562 /* Select the hw shader variant depending on the current state. */
563 static int si_shader_select(struct pipe_context *ctx,
564 struct si_shader_selector *sel)
565 {
566 struct si_context *sctx = (struct si_context *)ctx;
567 union si_shader_key key;
568 struct si_shader * shader = NULL;
569 int r;
570
571 si_shader_selector_key(ctx, sel, &key);
572
573 /* Check if we don't need to change anything.
574 * This path is also used for most shaders that don't need multiple
575 * variants, it will cost just a computation of the key and this
576 * test. */
577 if (likely(sel->current && memcmp(&sel->current->key, &key, sizeof(key)) == 0)) {
578 return 0;
579 }
580
581 /* lookup if we have other variants in the list */
582 if (sel->num_shaders > 1) {
583 struct si_shader *p = sel->current, *c = p->next_variant;
584
585 while (c && memcmp(&c->key, &key, sizeof(key)) != 0) {
586 p = c;
587 c = c->next_variant;
588 }
589
590 if (c) {
591 p->next_variant = c->next_variant;
592 shader = c;
593 }
594 }
595
596 if (shader) {
597 shader->next_variant = sel->current;
598 sel->current = shader;
599 } else {
600 shader = CALLOC(1, sizeof(struct si_shader));
601 shader->selector = sel;
602 shader->key = key;
603
604 shader->next_variant = sel->current;
605 sel->current = shader;
606 r = si_shader_create((struct si_screen*)ctx->screen, sctx->tm,
607 shader);
608 if (unlikely(r)) {
609 R600_ERR("Failed to build shader variant (type=%u) %d\n",
610 sel->type, r);
611 sel->current = NULL;
612 FREE(shader);
613 return r;
614 }
615 si_shader_init_pm4_state(shader);
616 sel->num_shaders++;
617 }
618
619 return 0;
620 }
621
622 static void *si_create_shader_state(struct pipe_context *ctx,
623 const struct pipe_shader_state *state,
624 unsigned pipe_shader_type)
625 {
626 struct si_screen *sscreen = (struct si_screen *)ctx->screen;
627 struct si_shader_selector *sel = CALLOC_STRUCT(si_shader_selector);
628 int i;
629
630 sel->type = pipe_shader_type;
631 sel->tokens = tgsi_dup_tokens(state->tokens);
632 sel->so = state->stream_output;
633 tgsi_scan_shader(state->tokens, &sel->info);
634
635 switch (pipe_shader_type) {
636 case PIPE_SHADER_GEOMETRY:
637 sel->gs_output_prim =
638 sel->info.properties[TGSI_PROPERTY_GS_OUTPUT_PRIM];
639 sel->gs_max_out_vertices =
640 sel->info.properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES];
641 sel->gs_num_invocations =
642 sel->info.properties[TGSI_PROPERTY_GS_INVOCATIONS];
643
644 for (i = 0; i < sel->info.num_inputs; i++) {
645 unsigned name = sel->info.input_semantic_name[i];
646 unsigned index = sel->info.input_semantic_index[i];
647
648 switch (name) {
649 case TGSI_SEMANTIC_PRIMID:
650 break;
651 default:
652 sel->inputs_read |=
653 1llu << si_shader_io_get_unique_index(name, index);
654 }
655 }
656 break;
657
658 case PIPE_SHADER_VERTEX:
659 case PIPE_SHADER_TESS_CTRL:
660 for (i = 0; i < sel->info.num_outputs; i++) {
661 unsigned name = sel->info.output_semantic_name[i];
662 unsigned index = sel->info.output_semantic_index[i];
663
664 switch (name) {
665 case TGSI_SEMANTIC_TESSINNER:
666 case TGSI_SEMANTIC_TESSOUTER:
667 case TGSI_SEMANTIC_PATCH:
668 sel->patch_outputs_written |=
669 1llu << si_shader_io_get_unique_index(name, index);
670 break;
671 default:
672 sel->outputs_written |=
673 1llu << si_shader_io_get_unique_index(name, index);
674 }
675 }
676 break;
677 }
678
679 if (sscreen->b.debug_flags & DBG_PRECOMPILE)
680 si_shader_select(ctx, sel);
681
682 return sel;
683 }
684
685 static void *si_create_fs_state(struct pipe_context *ctx,
686 const struct pipe_shader_state *state)
687 {
688 return si_create_shader_state(ctx, state, PIPE_SHADER_FRAGMENT);
689 }
690
691 static void *si_create_gs_state(struct pipe_context *ctx,
692 const struct pipe_shader_state *state)
693 {
694 return si_create_shader_state(ctx, state, PIPE_SHADER_GEOMETRY);
695 }
696
697 static void *si_create_vs_state(struct pipe_context *ctx,
698 const struct pipe_shader_state *state)
699 {
700 return si_create_shader_state(ctx, state, PIPE_SHADER_VERTEX);
701 }
702
703 static void *si_create_tcs_state(struct pipe_context *ctx,
704 const struct pipe_shader_state *state)
705 {
706 return si_create_shader_state(ctx, state, PIPE_SHADER_TESS_CTRL);
707 }
708
709 static void *si_create_tes_state(struct pipe_context *ctx,
710 const struct pipe_shader_state *state)
711 {
712 return si_create_shader_state(ctx, state, PIPE_SHADER_TESS_EVAL);
713 }
714
715 static void si_bind_vs_shader(struct pipe_context *ctx, void *state)
716 {
717 struct si_context *sctx = (struct si_context *)ctx;
718 struct si_shader_selector *sel = state;
719
720 if (sctx->vs_shader == sel || !sel)
721 return;
722
723 sctx->vs_shader = sel;
724 sctx->clip_regs.dirty = true;
725 }
726
727 static void si_bind_gs_shader(struct pipe_context *ctx, void *state)
728 {
729 struct si_context *sctx = (struct si_context *)ctx;
730 struct si_shader_selector *sel = state;
731 bool enable_changed = !!sctx->gs_shader != !!sel;
732
733 if (sctx->gs_shader == sel)
734 return;
735
736 sctx->gs_shader = sel;
737 sctx->clip_regs.dirty = true;
738 sctx->last_rast_prim = -1; /* reset this so that it gets updated */
739
740 if (enable_changed)
741 si_shader_change_notify(sctx);
742 }
743
744 static void si_bind_tcs_shader(struct pipe_context *ctx, void *state)
745 {
746 struct si_context *sctx = (struct si_context *)ctx;
747 struct si_shader_selector *sel = state;
748 bool enable_changed = !!sctx->tcs_shader != !!sel;
749
750 if (sctx->tcs_shader == sel)
751 return;
752
753 sctx->tcs_shader = sel;
754
755 if (enable_changed)
756 sctx->last_tcs = NULL; /* invalidate derived tess state */
757 }
758
759 static void si_bind_tes_shader(struct pipe_context *ctx, void *state)
760 {
761 struct si_context *sctx = (struct si_context *)ctx;
762 struct si_shader_selector *sel = state;
763 bool enable_changed = !!sctx->tes_shader != !!sel;
764
765 if (sctx->tes_shader == sel)
766 return;
767
768 sctx->tes_shader = sel;
769 sctx->clip_regs.dirty = true;
770 sctx->last_rast_prim = -1; /* reset this so that it gets updated */
771
772 if (enable_changed) {
773 si_shader_change_notify(sctx);
774 sctx->last_tes_sh_base = -1; /* invalidate derived tess state */
775 }
776 }
777
778 static void si_make_dummy_ps(struct si_context *sctx)
779 {
780 if (!sctx->dummy_pixel_shader) {
781 sctx->dummy_pixel_shader =
782 util_make_fragment_cloneinput_shader(&sctx->b.b, 0,
783 TGSI_SEMANTIC_GENERIC,
784 TGSI_INTERPOLATE_CONSTANT);
785 }
786 }
787
788 static void si_bind_ps_shader(struct pipe_context *ctx, void *state)
789 {
790 struct si_context *sctx = (struct si_context *)ctx;
791 struct si_shader_selector *sel = state;
792
793 /* skip if supplied shader is one already in use */
794 if (sctx->ps_shader == sel)
795 return;
796
797 /* use a dummy shader if binding a NULL shader */
798 if (!sel) {
799 si_make_dummy_ps(sctx);
800 sel = sctx->dummy_pixel_shader;
801 }
802
803 sctx->ps_shader = sel;
804 }
805
806 static void si_delete_shader_selector(struct pipe_context *ctx,
807 struct si_shader_selector *sel)
808 {
809 struct si_context *sctx = (struct si_context *)ctx;
810 struct si_shader *p = sel->current, *c;
811
812 while (p) {
813 c = p->next_variant;
814 switch (sel->type) {
815 case PIPE_SHADER_VERTEX:
816 if (p->key.vs.as_ls)
817 si_pm4_delete_state(sctx, ls, p->pm4);
818 else if (p->key.vs.as_es)
819 si_pm4_delete_state(sctx, es, p->pm4);
820 else
821 si_pm4_delete_state(sctx, vs, p->pm4);
822 break;
823 case PIPE_SHADER_TESS_CTRL:
824 si_pm4_delete_state(sctx, hs, p->pm4);
825 break;
826 case PIPE_SHADER_TESS_EVAL:
827 if (p->key.tes.as_es)
828 si_pm4_delete_state(sctx, es, p->pm4);
829 else
830 si_pm4_delete_state(sctx, vs, p->pm4);
831 break;
832 case PIPE_SHADER_GEOMETRY:
833 si_pm4_delete_state(sctx, gs, p->pm4);
834 si_pm4_delete_state(sctx, vs, p->gs_copy_shader->pm4);
835 break;
836 case PIPE_SHADER_FRAGMENT:
837 si_pm4_delete_state(sctx, ps, p->pm4);
838 break;
839 }
840
841 si_shader_destroy(ctx, p);
842 free(p);
843 p = c;
844 }
845
846 free(sel->tokens);
847 free(sel);
848 }
849
850 static void si_delete_vs_shader(struct pipe_context *ctx, void *state)
851 {
852 struct si_context *sctx = (struct si_context *)ctx;
853 struct si_shader_selector *sel = (struct si_shader_selector *)state;
854
855 if (sctx->vs_shader == sel) {
856 sctx->vs_shader = NULL;
857 }
858
859 si_delete_shader_selector(ctx, sel);
860 }
861
862 static void si_delete_gs_shader(struct pipe_context *ctx, void *state)
863 {
864 struct si_context *sctx = (struct si_context *)ctx;
865 struct si_shader_selector *sel = (struct si_shader_selector *)state;
866
867 if (sctx->gs_shader == sel) {
868 sctx->gs_shader = NULL;
869 }
870
871 si_delete_shader_selector(ctx, sel);
872 }
873
874 static void si_delete_ps_shader(struct pipe_context *ctx, void *state)
875 {
876 struct si_context *sctx = (struct si_context *)ctx;
877 struct si_shader_selector *sel = (struct si_shader_selector *)state;
878
879 if (sctx->ps_shader == sel) {
880 sctx->ps_shader = NULL;
881 }
882
883 si_delete_shader_selector(ctx, sel);
884 }
885
886 static void si_delete_tcs_shader(struct pipe_context *ctx, void *state)
887 {
888 struct si_context *sctx = (struct si_context *)ctx;
889 struct si_shader_selector *sel = (struct si_shader_selector *)state;
890
891 if (sctx->tcs_shader == sel) {
892 sctx->tcs_shader = NULL;
893 }
894
895 si_delete_shader_selector(ctx, sel);
896 }
897
898 static void si_delete_tes_shader(struct pipe_context *ctx, void *state)
899 {
900 struct si_context *sctx = (struct si_context *)ctx;
901 struct si_shader_selector *sel = (struct si_shader_selector *)state;
902
903 if (sctx->tes_shader == sel) {
904 sctx->tes_shader = NULL;
905 }
906
907 si_delete_shader_selector(ctx, sel);
908 }
909
910 static void si_update_spi_map(struct si_context *sctx)
911 {
912 struct si_shader *ps = sctx->ps_shader->current;
913 struct si_shader *vs = si_get_vs_state(sctx);
914 struct tgsi_shader_info *psinfo = &ps->selector->info;
915 struct tgsi_shader_info *vsinfo = &vs->selector->info;
916 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
917 unsigned i, j, tmp;
918
919 for (i = 0; i < psinfo->num_inputs; i++) {
920 unsigned name = psinfo->input_semantic_name[i];
921 unsigned index = psinfo->input_semantic_index[i];
922 unsigned interpolate = psinfo->input_interpolate[i];
923 unsigned param_offset = ps->ps_input_param_offset[i];
924
925 if (name == TGSI_SEMANTIC_POSITION ||
926 name == TGSI_SEMANTIC_FACE)
927 /* Read from preloaded VGPRs, not parameters */
928 continue;
929
930 bcolor:
931 tmp = 0;
932
933 if (interpolate == TGSI_INTERPOLATE_CONSTANT ||
934 (interpolate == TGSI_INTERPOLATE_COLOR && sctx->flatshade))
935 tmp |= S_028644_FLAT_SHADE(1);
936
937 if (name == TGSI_SEMANTIC_PCOORD ||
938 (name == TGSI_SEMANTIC_TEXCOORD &&
939 sctx->sprite_coord_enable & (1 << index))) {
940 tmp |= S_028644_PT_SPRITE_TEX(1);
941 }
942
943 for (j = 0; j < vsinfo->num_outputs; j++) {
944 if (name == vsinfo->output_semantic_name[j] &&
945 index == vsinfo->output_semantic_index[j]) {
946 tmp |= S_028644_OFFSET(vs->vs_output_param_offset[j]);
947 break;
948 }
949 }
950
951 if (j == vsinfo->num_outputs && !G_028644_PT_SPRITE_TEX(tmp)) {
952 /* No corresponding output found, load defaults into input.
953 * Don't set any other bits.
954 * (FLAT_SHADE=1 completely changes behavior) */
955 tmp = S_028644_OFFSET(0x20);
956 }
957
958 si_pm4_set_reg(pm4,
959 R_028644_SPI_PS_INPUT_CNTL_0 + param_offset * 4,
960 tmp);
961
962 if (name == TGSI_SEMANTIC_COLOR &&
963 ps->key.ps.color_two_side) {
964 name = TGSI_SEMANTIC_BCOLOR;
965 param_offset++;
966 goto bcolor;
967 }
968 }
969
970 si_pm4_set_state(sctx, spi, pm4);
971 }
972
973 /* Initialize state related to ESGS / GSVS ring buffers */
974 static void si_init_gs_rings(struct si_context *sctx)
975 {
976 unsigned esgs_ring_size = 128 * 1024;
977 unsigned gsvs_ring_size = 64 * 1024 * 1024;
978
979 assert(!sctx->gs_rings);
980 sctx->gs_rings = CALLOC_STRUCT(si_pm4_state);
981
982 sctx->esgs_ring = pipe_buffer_create(sctx->b.b.screen, PIPE_BIND_CUSTOM,
983 PIPE_USAGE_DEFAULT, esgs_ring_size);
984
985 sctx->gsvs_ring = pipe_buffer_create(sctx->b.b.screen, PIPE_BIND_CUSTOM,
986 PIPE_USAGE_DEFAULT, gsvs_ring_size);
987
988 if (sctx->b.chip_class >= CIK) {
989 si_pm4_set_reg(sctx->gs_rings, R_030900_VGT_ESGS_RING_SIZE,
990 esgs_ring_size / 256);
991 si_pm4_set_reg(sctx->gs_rings, R_030904_VGT_GSVS_RING_SIZE,
992 gsvs_ring_size / 256);
993 } else {
994 si_pm4_set_reg(sctx->gs_rings, R_0088C8_VGT_ESGS_RING_SIZE,
995 esgs_ring_size / 256);
996 si_pm4_set_reg(sctx->gs_rings, R_0088CC_VGT_GSVS_RING_SIZE,
997 gsvs_ring_size / 256);
998 }
999
1000 si_set_ring_buffer(&sctx->b.b, PIPE_SHADER_VERTEX, SI_RING_ESGS,
1001 sctx->esgs_ring, 0, esgs_ring_size,
1002 true, true, 4, 64);
1003 si_set_ring_buffer(&sctx->b.b, PIPE_SHADER_GEOMETRY, SI_RING_ESGS,
1004 sctx->esgs_ring, 0, esgs_ring_size,
1005 false, false, 0, 0);
1006 si_set_ring_buffer(&sctx->b.b, PIPE_SHADER_VERTEX, SI_RING_GSVS,
1007 sctx->gsvs_ring, 0, gsvs_ring_size,
1008 false, false, 0, 0);
1009 }
1010
1011 /**
1012 * @returns 1 if \p sel has been updated to use a new scratch buffer and 0
1013 * otherwise.
1014 */
1015 static unsigned si_update_scratch_buffer(struct si_context *sctx,
1016 struct si_shader_selector *sel)
1017 {
1018 struct si_shader *shader;
1019 uint64_t scratch_va = sctx->scratch_buffer->gpu_address;
1020
1021 if (!sel)
1022 return 0;
1023
1024 shader = sel->current;
1025
1026 /* This shader doesn't need a scratch buffer */
1027 if (shader->scratch_bytes_per_wave == 0)
1028 return 0;
1029
1030 /* This shader is already configured to use the current
1031 * scratch buffer. */
1032 if (shader->scratch_bo == sctx->scratch_buffer)
1033 return 0;
1034
1035 assert(sctx->scratch_buffer);
1036
1037 si_shader_apply_scratch_relocs(sctx, shader, scratch_va);
1038
1039 /* Replace the shader bo with a new bo that has the relocs applied. */
1040 si_shader_binary_upload(sctx->screen, shader);
1041
1042 /* Update the shader state to use the new shader bo. */
1043 si_shader_init_pm4_state(shader);
1044
1045 r600_resource_reference(&shader->scratch_bo, sctx->scratch_buffer);
1046
1047 return 1;
1048 }
1049
1050 static unsigned si_get_current_scratch_buffer_size(struct si_context *sctx)
1051 {
1052 if (!sctx->scratch_buffer)
1053 return 0;
1054
1055 return sctx->scratch_buffer->b.b.width0;
1056 }
1057
1058 static unsigned si_get_scratch_buffer_bytes_per_wave(struct si_context *sctx,
1059 struct si_shader_selector *sel)
1060 {
1061 if (!sel)
1062 return 0;
1063
1064 return sel->current->scratch_bytes_per_wave;
1065 }
1066
1067 static unsigned si_get_max_scratch_bytes_per_wave(struct si_context *sctx)
1068 {
1069
1070 return MAX3(si_get_scratch_buffer_bytes_per_wave(sctx, sctx->ps_shader),
1071 si_get_scratch_buffer_bytes_per_wave(sctx, sctx->gs_shader),
1072 si_get_scratch_buffer_bytes_per_wave(sctx, sctx->vs_shader));
1073 }
1074
1075 static void si_update_spi_tmpring_size(struct si_context *sctx)
1076 {
1077 unsigned current_scratch_buffer_size =
1078 si_get_current_scratch_buffer_size(sctx);
1079 unsigned scratch_bytes_per_wave =
1080 si_get_max_scratch_bytes_per_wave(sctx);
1081 unsigned scratch_needed_size = scratch_bytes_per_wave *
1082 sctx->scratch_waves;
1083
1084 if (scratch_needed_size > 0) {
1085
1086 if (scratch_needed_size > current_scratch_buffer_size) {
1087 /* Create a bigger scratch buffer */
1088 pipe_resource_reference(
1089 (struct pipe_resource**)&sctx->scratch_buffer,
1090 NULL);
1091
1092 sctx->scratch_buffer =
1093 si_resource_create_custom(&sctx->screen->b.b,
1094 PIPE_USAGE_DEFAULT, scratch_needed_size);
1095 }
1096
1097 /* Update the shaders, so they are using the latest scratch. The
1098 * scratch buffer may have been changed since these shaders were
1099 * last used, so we still need to try to update them, even if
1100 * they require scratch buffers smaller than the current size.
1101 */
1102 if (si_update_scratch_buffer(sctx, sctx->ps_shader))
1103 si_pm4_bind_state(sctx, ps, sctx->ps_shader->current->pm4);
1104 if (si_update_scratch_buffer(sctx, sctx->gs_shader))
1105 si_pm4_bind_state(sctx, gs, sctx->gs_shader->current->pm4);
1106
1107 /* VS can be bound as ES or VS. */
1108 if (sctx->gs_shader) {
1109 if (si_update_scratch_buffer(sctx, sctx->vs_shader))
1110 si_pm4_bind_state(sctx, es, sctx->vs_shader->current->pm4);
1111 } else {
1112 if (si_update_scratch_buffer(sctx, sctx->vs_shader))
1113 si_pm4_bind_state(sctx, vs, sctx->vs_shader->current->pm4);
1114 }
1115 }
1116
1117 /* The LLVM shader backend should be reporting aligned scratch_sizes. */
1118 assert((scratch_needed_size & ~0x3FF) == scratch_needed_size &&
1119 "scratch size should already be aligned correctly.");
1120
1121 sctx->spi_tmpring_size = S_0286E8_WAVES(sctx->scratch_waves) |
1122 S_0286E8_WAVESIZE(scratch_bytes_per_wave >> 10);
1123 }
1124
1125 static void si_init_tess_factor_ring(struct si_context *sctx)
1126 {
1127 assert(!sctx->tf_state);
1128 sctx->tf_state = CALLOC_STRUCT(si_pm4_state);
1129
1130 sctx->tf_ring = pipe_buffer_create(sctx->b.b.screen, PIPE_BIND_CUSTOM,
1131 PIPE_USAGE_DEFAULT,
1132 32768 * sctx->screen->b.info.max_se);
1133 sctx->b.clear_buffer(&sctx->b.b, sctx->tf_ring, 0,
1134 sctx->tf_ring->width0, fui(0), false);
1135 assert(((sctx->tf_ring->width0 / 4) & C_030938_SIZE) == 0);
1136
1137 if (sctx->b.chip_class >= CIK) {
1138 si_pm4_set_reg(sctx->tf_state, R_030938_VGT_TF_RING_SIZE,
1139 S_030938_SIZE(sctx->tf_ring->width0 / 4));
1140 si_pm4_set_reg(sctx->tf_state, R_030940_VGT_TF_MEMORY_BASE,
1141 r600_resource(sctx->tf_ring)->gpu_address >> 8);
1142 } else {
1143 si_pm4_set_reg(sctx->tf_state, R_008988_VGT_TF_RING_SIZE,
1144 S_008988_SIZE(sctx->tf_ring->width0 / 4));
1145 si_pm4_set_reg(sctx->tf_state, R_0089B8_VGT_TF_MEMORY_BASE,
1146 r600_resource(sctx->tf_ring)->gpu_address >> 8);
1147 }
1148 si_pm4_add_bo(sctx->tf_state, r600_resource(sctx->tf_ring),
1149 RADEON_USAGE_READWRITE, RADEON_PRIO_SHADER_RESOURCE_RW);
1150 si_pm4_bind_state(sctx, tf_ring, sctx->tf_state);
1151
1152 si_set_ring_buffer(&sctx->b.b, PIPE_SHADER_TESS_CTRL,
1153 SI_RING_TESS_FACTOR, sctx->tf_ring, 0,
1154 sctx->tf_ring->width0, false, false, 0, 0);
1155
1156 sctx->b.flags |= SI_CONTEXT_VGT_FLUSH;
1157 }
1158
1159 /**
1160 * This is used when TCS is NULL in the VS->TCS->TES chain. In this case,
1161 * VS passes its outputs to TES directly, so the fixed-function shader only
1162 * has to write TESSOUTER and TESSINNER.
1163 */
1164 static void si_generate_fixed_func_tcs(struct si_context *sctx)
1165 {
1166 struct ureg_src const0, const1;
1167 struct ureg_dst tessouter, tessinner;
1168 struct ureg_program *ureg = ureg_create(TGSI_PROCESSOR_TESS_CTRL);
1169
1170 if (!ureg)
1171 return; /* if we get here, we're screwed */
1172
1173 assert(!sctx->fixed_func_tcs_shader);
1174
1175 ureg_DECL_constant2D(ureg, 0, 1, SI_DRIVER_STATE_CONST_BUF);
1176 const0 = ureg_src_dimension(ureg_src_register(TGSI_FILE_CONSTANT, 0),
1177 SI_DRIVER_STATE_CONST_BUF);
1178 const1 = ureg_src_dimension(ureg_src_register(TGSI_FILE_CONSTANT, 1),
1179 SI_DRIVER_STATE_CONST_BUF);
1180
1181 tessouter = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSOUTER, 0);
1182 tessinner = ureg_DECL_output(ureg, TGSI_SEMANTIC_TESSINNER, 0);
1183
1184 ureg_MOV(ureg, tessouter, const0);
1185 ureg_MOV(ureg, tessinner, const1);
1186 ureg_END(ureg);
1187
1188 sctx->fixed_func_tcs_shader =
1189 ureg_create_shader_and_destroy(ureg, &sctx->b.b);
1190 assert(sctx->fixed_func_tcs_shader);
1191 }
1192
1193 static void si_update_vgt_shader_config(struct si_context *sctx)
1194 {
1195 /* Calculate the index of the config.
1196 * 0 = VS, 1 = VS+GS, 2 = VS+Tess, 3 = VS+Tess+GS */
1197 unsigned index = 2*!!sctx->tes_shader + !!sctx->gs_shader;
1198 struct si_pm4_state **pm4 = &sctx->vgt_shader_config[index];
1199
1200 if (!*pm4) {
1201 uint32_t stages = 0;
1202
1203 *pm4 = CALLOC_STRUCT(si_pm4_state);
1204
1205 if (sctx->tes_shader) {
1206 stages |= S_028B54_LS_EN(V_028B54_LS_STAGE_ON) |
1207 S_028B54_HS_EN(1);
1208
1209 if (sctx->gs_shader)
1210 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_DS) |
1211 S_028B54_GS_EN(1) |
1212 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
1213 else
1214 stages |= S_028B54_VS_EN(V_028B54_VS_STAGE_DS);
1215 } else if (sctx->gs_shader) {
1216 stages |= S_028B54_ES_EN(V_028B54_ES_STAGE_REAL) |
1217 S_028B54_GS_EN(1) |
1218 S_028B54_VS_EN(V_028B54_VS_STAGE_COPY_SHADER);
1219 }
1220
1221 si_pm4_set_reg(*pm4, R_028B54_VGT_SHADER_STAGES_EN, stages);
1222 if (!sctx->gs_shader)
1223 si_pm4_set_reg(*pm4, R_028A40_VGT_GS_MODE, 0);
1224 }
1225 si_pm4_bind_state(sctx, vgt_shader_config, *pm4);
1226 }
1227
1228 void si_update_shaders(struct si_context *sctx)
1229 {
1230 struct pipe_context *ctx = (struct pipe_context*)sctx;
1231 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1232
1233 /* Update stages before GS. */
1234 if (sctx->tes_shader) {
1235 if (!sctx->tf_state)
1236 si_init_tess_factor_ring(sctx);
1237
1238 /* VS as LS */
1239 si_shader_select(ctx, sctx->vs_shader);
1240 si_pm4_bind_state(sctx, ls, sctx->vs_shader->current->pm4);
1241
1242 if (sctx->tcs_shader) {
1243 si_shader_select(ctx, sctx->tcs_shader);
1244 si_pm4_bind_state(sctx, hs, sctx->tcs_shader->current->pm4);
1245 } else {
1246 if (!sctx->fixed_func_tcs_shader)
1247 si_generate_fixed_func_tcs(sctx);
1248 si_shader_select(ctx, sctx->fixed_func_tcs_shader);
1249 si_pm4_bind_state(sctx, hs,
1250 sctx->fixed_func_tcs_shader->current->pm4);
1251 }
1252
1253 si_shader_select(ctx, sctx->tes_shader);
1254 if (sctx->gs_shader) {
1255 /* TES as ES */
1256 si_pm4_bind_state(sctx, es, sctx->tes_shader->current->pm4);
1257 } else {
1258 /* TES as VS */
1259 si_pm4_bind_state(sctx, vs, sctx->tes_shader->current->pm4);
1260 sctx->b.streamout.stride_in_dw = sctx->tes_shader->so.stride;
1261 }
1262 } else if (sctx->gs_shader) {
1263 /* VS as ES */
1264 si_shader_select(ctx, sctx->vs_shader);
1265 si_pm4_bind_state(sctx, es, sctx->vs_shader->current->pm4);
1266 } else {
1267 /* VS as VS */
1268 si_shader_select(ctx, sctx->vs_shader);
1269 si_pm4_bind_state(sctx, vs, sctx->vs_shader->current->pm4);
1270 sctx->b.streamout.stride_in_dw = sctx->vs_shader->so.stride;
1271 }
1272
1273 /* Update GS. */
1274 if (sctx->gs_shader) {
1275 si_shader_select(ctx, sctx->gs_shader);
1276 si_pm4_bind_state(sctx, gs, sctx->gs_shader->current->pm4);
1277 si_pm4_bind_state(sctx, vs, sctx->gs_shader->current->gs_copy_shader->pm4);
1278 sctx->b.streamout.stride_in_dw = sctx->gs_shader->so.stride;
1279
1280 if (!sctx->gs_rings)
1281 si_init_gs_rings(sctx);
1282 if (sctx->emitted.named.gs_rings != sctx->gs_rings)
1283 sctx->b.flags |= SI_CONTEXT_VGT_FLUSH;
1284 si_pm4_bind_state(sctx, gs_rings, sctx->gs_rings);
1285
1286 si_set_ring_buffer(ctx, PIPE_SHADER_GEOMETRY, SI_RING_GSVS,
1287 sctx->gsvs_ring,
1288 sctx->gs_shader->gs_max_out_vertices *
1289 sctx->gs_shader->info.num_outputs * 16,
1290 64, true, true, 4, 16);
1291 } else {
1292 si_pm4_bind_state(sctx, gs_rings, NULL);
1293 si_pm4_bind_state(sctx, gs, NULL);
1294 si_pm4_bind_state(sctx, es, NULL);
1295 }
1296
1297 si_update_vgt_shader_config(sctx);
1298
1299 si_shader_select(ctx, sctx->ps_shader);
1300
1301 if (!sctx->ps_shader->current) {
1302 struct si_shader_selector *sel;
1303
1304 /* use a dummy shader if compiling the shader (variant) failed */
1305 si_make_dummy_ps(sctx);
1306 sel = sctx->dummy_pixel_shader;
1307 si_shader_select(ctx, sel);
1308 sctx->ps_shader->current = sel->current;
1309 }
1310
1311 si_pm4_bind_state(sctx, ps, sctx->ps_shader->current->pm4);
1312
1313 if (si_pm4_state_changed(sctx, ps) || si_pm4_state_changed(sctx, vs) ||
1314 sctx->sprite_coord_enable != rs->sprite_coord_enable ||
1315 sctx->flatshade != rs->flatshade) {
1316 sctx->sprite_coord_enable = rs->sprite_coord_enable;
1317 sctx->flatshade = rs->flatshade;
1318 si_update_spi_map(sctx);
1319 }
1320
1321 if (si_pm4_state_changed(sctx, ps) || si_pm4_state_changed(sctx, vs) ||
1322 si_pm4_state_changed(sctx, gs)) {
1323 si_update_spi_tmpring_size(sctx);
1324 }
1325
1326 if (sctx->ps_db_shader_control != sctx->ps_shader->current->db_shader_control) {
1327 sctx->ps_db_shader_control = sctx->ps_shader->current->db_shader_control;
1328 sctx->db_render_state.dirty = true;
1329 }
1330
1331 if (sctx->smoothing_enabled != sctx->ps_shader->current->key.ps.poly_line_smoothing) {
1332 sctx->smoothing_enabled = sctx->ps_shader->current->key.ps.poly_line_smoothing;
1333 sctx->msaa_config.dirty = true;
1334
1335 if (sctx->b.chip_class == SI)
1336 sctx->db_render_state.dirty = true;
1337 }
1338 }
1339
1340 void si_init_shader_functions(struct si_context *sctx)
1341 {
1342 sctx->b.b.create_vs_state = si_create_vs_state;
1343 sctx->b.b.create_tcs_state = si_create_tcs_state;
1344 sctx->b.b.create_tes_state = si_create_tes_state;
1345 sctx->b.b.create_gs_state = si_create_gs_state;
1346 sctx->b.b.create_fs_state = si_create_fs_state;
1347
1348 sctx->b.b.bind_vs_state = si_bind_vs_shader;
1349 sctx->b.b.bind_tcs_state = si_bind_tcs_shader;
1350 sctx->b.b.bind_tes_state = si_bind_tes_shader;
1351 sctx->b.b.bind_gs_state = si_bind_gs_shader;
1352 sctx->b.b.bind_fs_state = si_bind_ps_shader;
1353
1354 sctx->b.b.delete_vs_state = si_delete_vs_shader;
1355 sctx->b.b.delete_tcs_state = si_delete_tcs_shader;
1356 sctx->b.b.delete_tes_state = si_delete_tes_shader;
1357 sctx->b.b.delete_gs_state = si_delete_gs_shader;
1358 sctx->b.b.delete_fs_state = si_delete_ps_shader;
1359 }