radeonsi/gfx9: rework the gfx9 scissor bug workaround (v2)
[mesa.git] / src / gallium / drivers / radeonsi / si_state.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "si_build_pm4.h"
26 #include "gfx9d.h"
27 #include "si_query.h"
28
29 #include "util/u_dual_blend.h"
30 #include "util/u_format.h"
31 #include "util/u_format_s3tc.h"
32 #include "util/u_memory.h"
33 #include "util/u_resource.h"
34 #include "util/u_upload_mgr.h"
35 #include "util/fast_idiv_by_const.h"
36
37 static unsigned si_map_swizzle(unsigned swizzle)
38 {
39 switch (swizzle) {
40 case PIPE_SWIZZLE_Y:
41 return V_008F0C_SQ_SEL_Y;
42 case PIPE_SWIZZLE_Z:
43 return V_008F0C_SQ_SEL_Z;
44 case PIPE_SWIZZLE_W:
45 return V_008F0C_SQ_SEL_W;
46 case PIPE_SWIZZLE_0:
47 return V_008F0C_SQ_SEL_0;
48 case PIPE_SWIZZLE_1:
49 return V_008F0C_SQ_SEL_1;
50 default: /* PIPE_SWIZZLE_X */
51 return V_008F0C_SQ_SEL_X;
52 }
53 }
54
55 /* 12.4 fixed-point */
56 static unsigned si_pack_float_12p4(float x)
57 {
58 return x <= 0 ? 0 :
59 x >= 4096 ? 0xffff : x * 16;
60 }
61
62 /*
63 * Inferred framebuffer and blender state.
64 *
65 * CB_TARGET_MASK is emitted here to avoid a hang with dual source blending
66 * if there is not enough PS outputs.
67 */
68 static void si_emit_cb_render_state(struct si_context *sctx)
69 {
70 struct radeon_cmdbuf *cs = sctx->gfx_cs;
71 struct si_state_blend *blend = sctx->queued.named.blend;
72 /* CB_COLORn_INFO.FORMAT=INVALID should disable unbound colorbuffers,
73 * but you never know. */
74 uint32_t cb_target_mask = sctx->framebuffer.colorbuf_enabled_4bit;
75 unsigned i;
76
77 if (blend)
78 cb_target_mask &= blend->cb_target_mask;
79
80 /* Avoid a hang that happens when dual source blending is enabled
81 * but there is not enough color outputs. This is undefined behavior,
82 * so disable color writes completely.
83 *
84 * Reproducible with Unigine Heaven 4.0 and drirc missing.
85 */
86 if (blend && blend->dual_src_blend &&
87 sctx->ps_shader.cso &&
88 (sctx->ps_shader.cso->info.colors_written & 0x3) != 0x3)
89 cb_target_mask = 0;
90
91 /* GFX9: Flush DFSM when CB_TARGET_MASK changes.
92 * I think we don't have to do anything between IBs.
93 */
94 if (sctx->screen->dfsm_allowed &&
95 sctx->last_cb_target_mask != cb_target_mask) {
96 sctx->last_cb_target_mask = cb_target_mask;
97
98 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
99 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
100 }
101
102 unsigned initial_cdw = cs->current.cdw;
103 radeon_opt_set_context_reg(sctx, R_028238_CB_TARGET_MASK,
104 SI_TRACKED_CB_TARGET_MASK, cb_target_mask);
105
106 if (sctx->chip_class >= VI) {
107 /* DCC MSAA workaround for blending.
108 * Alternatively, we can set CB_COLORi_DCC_CONTROL.OVERWRITE_-
109 * COMBINER_DISABLE, but that would be more complicated.
110 */
111 bool oc_disable = (sctx->chip_class == VI ||
112 sctx->chip_class == GFX9) &&
113 blend &&
114 blend->blend_enable_4bit & cb_target_mask &&
115 sctx->framebuffer.nr_samples >= 2;
116 unsigned watermark = sctx->framebuffer.dcc_overwrite_combiner_watermark;
117
118 radeon_opt_set_context_reg(
119 sctx, R_028424_CB_DCC_CONTROL,
120 SI_TRACKED_CB_DCC_CONTROL,
121 S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(1) |
122 S_028424_OVERWRITE_COMBINER_WATERMARK(watermark) |
123 S_028424_OVERWRITE_COMBINER_DISABLE(oc_disable) |
124 S_028424_DISABLE_CONSTANT_ENCODE_REG(sctx->screen->has_dcc_constant_encode));
125 }
126
127 /* RB+ register settings. */
128 if (sctx->screen->rbplus_allowed) {
129 unsigned spi_shader_col_format =
130 sctx->ps_shader.cso ?
131 sctx->ps_shader.current->key.part.ps.epilog.spi_shader_col_format : 0;
132 unsigned sx_ps_downconvert = 0;
133 unsigned sx_blend_opt_epsilon = 0;
134 unsigned sx_blend_opt_control = 0;
135
136 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
137 struct si_surface *surf =
138 (struct si_surface*)sctx->framebuffer.state.cbufs[i];
139 unsigned format, swap, spi_format, colormask;
140 bool has_alpha, has_rgb;
141
142 if (!surf)
143 continue;
144
145 format = G_028C70_FORMAT(surf->cb_color_info);
146 swap = G_028C70_COMP_SWAP(surf->cb_color_info);
147 spi_format = (spi_shader_col_format >> (i * 4)) & 0xf;
148 colormask = (cb_target_mask >> (i * 4)) & 0xf;
149
150 /* Set if RGB and A are present. */
151 has_alpha = !G_028C74_FORCE_DST_ALPHA_1(surf->cb_color_attrib);
152
153 if (format == V_028C70_COLOR_8 ||
154 format == V_028C70_COLOR_16 ||
155 format == V_028C70_COLOR_32)
156 has_rgb = !has_alpha;
157 else
158 has_rgb = true;
159
160 /* Check the colormask and export format. */
161 if (!(colormask & (PIPE_MASK_RGBA & ~PIPE_MASK_A)))
162 has_rgb = false;
163 if (!(colormask & PIPE_MASK_A))
164 has_alpha = false;
165
166 if (spi_format == V_028714_SPI_SHADER_ZERO) {
167 has_rgb = false;
168 has_alpha = false;
169 }
170
171 /* Disable value checking for disabled channels. */
172 if (!has_rgb)
173 sx_blend_opt_control |= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i * 4);
174 if (!has_alpha)
175 sx_blend_opt_control |= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i * 4);
176
177 /* Enable down-conversion for 32bpp and smaller formats. */
178 switch (format) {
179 case V_028C70_COLOR_8:
180 case V_028C70_COLOR_8_8:
181 case V_028C70_COLOR_8_8_8_8:
182 /* For 1 and 2-channel formats, use the superset thereof. */
183 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR ||
184 spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
185 spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
186 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_8_8_8_8 << (i * 4);
187 sx_blend_opt_epsilon |= V_028758_8BIT_FORMAT << (i * 4);
188 }
189 break;
190
191 case V_028C70_COLOR_5_6_5:
192 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
193 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_5_6_5 << (i * 4);
194 sx_blend_opt_epsilon |= V_028758_6BIT_FORMAT << (i * 4);
195 }
196 break;
197
198 case V_028C70_COLOR_1_5_5_5:
199 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
200 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_1_5_5_5 << (i * 4);
201 sx_blend_opt_epsilon |= V_028758_5BIT_FORMAT << (i * 4);
202 }
203 break;
204
205 case V_028C70_COLOR_4_4_4_4:
206 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
207 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_4_4_4_4 << (i * 4);
208 sx_blend_opt_epsilon |= V_028758_4BIT_FORMAT << (i * 4);
209 }
210 break;
211
212 case V_028C70_COLOR_32:
213 if (swap == V_028C70_SWAP_STD &&
214 spi_format == V_028714_SPI_SHADER_32_R)
215 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_R << (i * 4);
216 else if (swap == V_028C70_SWAP_ALT_REV &&
217 spi_format == V_028714_SPI_SHADER_32_AR)
218 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_A << (i * 4);
219 break;
220
221 case V_028C70_COLOR_16:
222 case V_028C70_COLOR_16_16:
223 /* For 1-channel formats, use the superset thereof. */
224 if (spi_format == V_028714_SPI_SHADER_UNORM16_ABGR ||
225 spi_format == V_028714_SPI_SHADER_SNORM16_ABGR ||
226 spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
227 spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
228 if (swap == V_028C70_SWAP_STD ||
229 swap == V_028C70_SWAP_STD_REV)
230 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_GR << (i * 4);
231 else
232 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_AR << (i * 4);
233 }
234 break;
235
236 case V_028C70_COLOR_10_11_11:
237 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
238 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_10_11_11 << (i * 4);
239 sx_blend_opt_epsilon |= V_028758_11BIT_FORMAT << (i * 4);
240 }
241 break;
242
243 case V_028C70_COLOR_2_10_10_10:
244 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
245 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_2_10_10_10 << (i * 4);
246 sx_blend_opt_epsilon |= V_028758_10BIT_FORMAT << (i * 4);
247 }
248 break;
249 }
250 }
251
252 /* SX_PS_DOWNCONVERT, SX_BLEND_OPT_EPSILON, SX_BLEND_OPT_CONTROL */
253 radeon_opt_set_context_reg3(sctx, R_028754_SX_PS_DOWNCONVERT,
254 SI_TRACKED_SX_PS_DOWNCONVERT,
255 sx_ps_downconvert, sx_blend_opt_epsilon,
256 sx_blend_opt_control);
257 }
258 if (initial_cdw != cs->current.cdw)
259 sctx->context_roll = true;
260 }
261
262 /*
263 * Blender functions
264 */
265
266 static uint32_t si_translate_blend_function(int blend_func)
267 {
268 switch (blend_func) {
269 case PIPE_BLEND_ADD:
270 return V_028780_COMB_DST_PLUS_SRC;
271 case PIPE_BLEND_SUBTRACT:
272 return V_028780_COMB_SRC_MINUS_DST;
273 case PIPE_BLEND_REVERSE_SUBTRACT:
274 return V_028780_COMB_DST_MINUS_SRC;
275 case PIPE_BLEND_MIN:
276 return V_028780_COMB_MIN_DST_SRC;
277 case PIPE_BLEND_MAX:
278 return V_028780_COMB_MAX_DST_SRC;
279 default:
280 PRINT_ERR("Unknown blend function %d\n", blend_func);
281 assert(0);
282 break;
283 }
284 return 0;
285 }
286
287 static uint32_t si_translate_blend_factor(int blend_fact)
288 {
289 switch (blend_fact) {
290 case PIPE_BLENDFACTOR_ONE:
291 return V_028780_BLEND_ONE;
292 case PIPE_BLENDFACTOR_SRC_COLOR:
293 return V_028780_BLEND_SRC_COLOR;
294 case PIPE_BLENDFACTOR_SRC_ALPHA:
295 return V_028780_BLEND_SRC_ALPHA;
296 case PIPE_BLENDFACTOR_DST_ALPHA:
297 return V_028780_BLEND_DST_ALPHA;
298 case PIPE_BLENDFACTOR_DST_COLOR:
299 return V_028780_BLEND_DST_COLOR;
300 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
301 return V_028780_BLEND_SRC_ALPHA_SATURATE;
302 case PIPE_BLENDFACTOR_CONST_COLOR:
303 return V_028780_BLEND_CONSTANT_COLOR;
304 case PIPE_BLENDFACTOR_CONST_ALPHA:
305 return V_028780_BLEND_CONSTANT_ALPHA;
306 case PIPE_BLENDFACTOR_ZERO:
307 return V_028780_BLEND_ZERO;
308 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
309 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
310 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
311 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
312 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
313 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
314 case PIPE_BLENDFACTOR_INV_DST_COLOR:
315 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
316 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
317 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
318 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
319 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
320 case PIPE_BLENDFACTOR_SRC1_COLOR:
321 return V_028780_BLEND_SRC1_COLOR;
322 case PIPE_BLENDFACTOR_SRC1_ALPHA:
323 return V_028780_BLEND_SRC1_ALPHA;
324 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
325 return V_028780_BLEND_INV_SRC1_COLOR;
326 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
327 return V_028780_BLEND_INV_SRC1_ALPHA;
328 default:
329 PRINT_ERR("Bad blend factor %d not supported!\n", blend_fact);
330 assert(0);
331 break;
332 }
333 return 0;
334 }
335
336 static uint32_t si_translate_blend_opt_function(int blend_func)
337 {
338 switch (blend_func) {
339 case PIPE_BLEND_ADD:
340 return V_028760_OPT_COMB_ADD;
341 case PIPE_BLEND_SUBTRACT:
342 return V_028760_OPT_COMB_SUBTRACT;
343 case PIPE_BLEND_REVERSE_SUBTRACT:
344 return V_028760_OPT_COMB_REVSUBTRACT;
345 case PIPE_BLEND_MIN:
346 return V_028760_OPT_COMB_MIN;
347 case PIPE_BLEND_MAX:
348 return V_028760_OPT_COMB_MAX;
349 default:
350 return V_028760_OPT_COMB_BLEND_DISABLED;
351 }
352 }
353
354 static uint32_t si_translate_blend_opt_factor(int blend_fact, bool is_alpha)
355 {
356 switch (blend_fact) {
357 case PIPE_BLENDFACTOR_ZERO:
358 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
359 case PIPE_BLENDFACTOR_ONE:
360 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
361 case PIPE_BLENDFACTOR_SRC_COLOR:
362 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
363 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
364 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
365 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
366 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
367 case PIPE_BLENDFACTOR_SRC_ALPHA:
368 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
369 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
370 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
371 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
372 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
373 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
374 default:
375 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
376 }
377 }
378
379 static void si_blend_check_commutativity(struct si_screen *sscreen,
380 struct si_state_blend *blend,
381 enum pipe_blend_func func,
382 enum pipe_blendfactor src,
383 enum pipe_blendfactor dst,
384 unsigned chanmask)
385 {
386 /* Src factor is allowed when it does not depend on Dst */
387 static const uint32_t src_allowed =
388 (1u << PIPE_BLENDFACTOR_ONE) |
389 (1u << PIPE_BLENDFACTOR_SRC_COLOR) |
390 (1u << PIPE_BLENDFACTOR_SRC_ALPHA) |
391 (1u << PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) |
392 (1u << PIPE_BLENDFACTOR_CONST_COLOR) |
393 (1u << PIPE_BLENDFACTOR_CONST_ALPHA) |
394 (1u << PIPE_BLENDFACTOR_SRC1_COLOR) |
395 (1u << PIPE_BLENDFACTOR_SRC1_ALPHA) |
396 (1u << PIPE_BLENDFACTOR_ZERO) |
397 (1u << PIPE_BLENDFACTOR_INV_SRC_COLOR) |
398 (1u << PIPE_BLENDFACTOR_INV_SRC_ALPHA) |
399 (1u << PIPE_BLENDFACTOR_INV_CONST_COLOR) |
400 (1u << PIPE_BLENDFACTOR_INV_CONST_ALPHA) |
401 (1u << PIPE_BLENDFACTOR_INV_SRC1_COLOR) |
402 (1u << PIPE_BLENDFACTOR_INV_SRC1_ALPHA);
403
404 if (dst == PIPE_BLENDFACTOR_ONE &&
405 (src_allowed & (1u << src))) {
406 /* Addition is commutative, but floating point addition isn't
407 * associative: subtle changes can be introduced via different
408 * rounding.
409 *
410 * Out-of-order is also non-deterministic, which means that
411 * this breaks OpenGL invariance requirements. So only enable
412 * out-of-order additive blending if explicitly allowed by a
413 * setting.
414 */
415 if (func == PIPE_BLEND_MAX || func == PIPE_BLEND_MIN ||
416 (func == PIPE_BLEND_ADD && sscreen->commutative_blend_add))
417 blend->commutative_4bit |= chanmask;
418 }
419 }
420
421 /**
422 * Get rid of DST in the blend factors by commuting the operands:
423 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
424 */
425 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
426 unsigned *dst_factor, unsigned expected_dst,
427 unsigned replacement_src)
428 {
429 if (*src_factor == expected_dst &&
430 *dst_factor == PIPE_BLENDFACTOR_ZERO) {
431 *src_factor = PIPE_BLENDFACTOR_ZERO;
432 *dst_factor = replacement_src;
433
434 /* Commuting the operands requires reversing subtractions. */
435 if (*func == PIPE_BLEND_SUBTRACT)
436 *func = PIPE_BLEND_REVERSE_SUBTRACT;
437 else if (*func == PIPE_BLEND_REVERSE_SUBTRACT)
438 *func = PIPE_BLEND_SUBTRACT;
439 }
440 }
441
442 static bool si_blend_factor_uses_dst(unsigned factor)
443 {
444 return factor == PIPE_BLENDFACTOR_DST_COLOR ||
445 factor == PIPE_BLENDFACTOR_DST_ALPHA ||
446 factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
447 factor == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
448 factor == PIPE_BLENDFACTOR_INV_DST_COLOR;
449 }
450
451 static void *si_create_blend_state_mode(struct pipe_context *ctx,
452 const struct pipe_blend_state *state,
453 unsigned mode)
454 {
455 struct si_context *sctx = (struct si_context*)ctx;
456 struct si_state_blend *blend = CALLOC_STRUCT(si_state_blend);
457 struct si_pm4_state *pm4 = &blend->pm4;
458 uint32_t sx_mrt_blend_opt[8] = {0};
459 uint32_t color_control = 0;
460
461 if (!blend)
462 return NULL;
463
464 blend->alpha_to_coverage = state->alpha_to_coverage;
465 blend->alpha_to_one = state->alpha_to_one;
466 blend->dual_src_blend = util_blend_state_is_dual(state, 0);
467 blend->logicop_enable = state->logicop_enable;
468
469 if (state->logicop_enable) {
470 color_control |= S_028808_ROP3(state->logicop_func | (state->logicop_func << 4));
471 } else {
472 color_control |= S_028808_ROP3(0xcc);
473 }
474
475 si_pm4_set_reg(pm4, R_028B70_DB_ALPHA_TO_MASK,
476 S_028B70_ALPHA_TO_MASK_ENABLE(state->alpha_to_coverage) |
477 S_028B70_ALPHA_TO_MASK_OFFSET0(3) |
478 S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
479 S_028B70_ALPHA_TO_MASK_OFFSET2(0) |
480 S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
481 S_028B70_OFFSET_ROUND(1));
482
483 if (state->alpha_to_coverage)
484 blend->need_src_alpha_4bit |= 0xf;
485
486 blend->cb_target_mask = 0;
487 blend->cb_target_enabled_4bit = 0;
488
489 for (int i = 0; i < 8; i++) {
490 /* state->rt entries > 0 only written if independent blending */
491 const int j = state->independent_blend_enable ? i : 0;
492
493 unsigned eqRGB = state->rt[j].rgb_func;
494 unsigned srcRGB = state->rt[j].rgb_src_factor;
495 unsigned dstRGB = state->rt[j].rgb_dst_factor;
496 unsigned eqA = state->rt[j].alpha_func;
497 unsigned srcA = state->rt[j].alpha_src_factor;
498 unsigned dstA = state->rt[j].alpha_dst_factor;
499
500 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
501 unsigned blend_cntl = 0;
502
503 sx_mrt_blend_opt[i] =
504 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |
505 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);
506
507 /* Only set dual source blending for MRT0 to avoid a hang. */
508 if (i >= 1 && blend->dual_src_blend) {
509 /* Vulkan does this for dual source blending. */
510 if (i == 1)
511 blend_cntl |= S_028780_ENABLE(1);
512
513 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
514 continue;
515 }
516
517 /* Only addition and subtraction equations are supported with
518 * dual source blending.
519 */
520 if (blend->dual_src_blend &&
521 (eqRGB == PIPE_BLEND_MIN || eqRGB == PIPE_BLEND_MAX ||
522 eqA == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MAX)) {
523 assert(!"Unsupported equation for dual source blending");
524 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
525 continue;
526 }
527
528 /* cb_render_state will disable unused ones */
529 blend->cb_target_mask |= (unsigned)state->rt[j].colormask << (4 * i);
530 if (state->rt[j].colormask)
531 blend->cb_target_enabled_4bit |= 0xf << (4 * i);
532
533 if (!state->rt[j].colormask || !state->rt[j].blend_enable) {
534 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
535 continue;
536 }
537
538 si_blend_check_commutativity(sctx->screen, blend,
539 eqRGB, srcRGB, dstRGB, 0x7 << (4 * i));
540 si_blend_check_commutativity(sctx->screen, blend,
541 eqA, srcA, dstA, 0x8 << (4 * i));
542
543 /* Blending optimizations for RB+.
544 * These transformations don't change the behavior.
545 *
546 * First, get rid of DST in the blend factors:
547 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
548 */
549 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
550 PIPE_BLENDFACTOR_DST_COLOR,
551 PIPE_BLENDFACTOR_SRC_COLOR);
552 si_blend_remove_dst(&eqA, &srcA, &dstA,
553 PIPE_BLENDFACTOR_DST_COLOR,
554 PIPE_BLENDFACTOR_SRC_COLOR);
555 si_blend_remove_dst(&eqA, &srcA, &dstA,
556 PIPE_BLENDFACTOR_DST_ALPHA,
557 PIPE_BLENDFACTOR_SRC_ALPHA);
558
559 /* Look up the ideal settings from tables. */
560 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
561 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
562 srcA_opt = si_translate_blend_opt_factor(srcA, true);
563 dstA_opt = si_translate_blend_opt_factor(dstA, true);
564
565 /* Handle interdependencies. */
566 if (si_blend_factor_uses_dst(srcRGB))
567 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
568 if (si_blend_factor_uses_dst(srcA))
569 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
570
571 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE &&
572 (dstRGB == PIPE_BLENDFACTOR_ZERO ||
573 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
574 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE))
575 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
576
577 /* Set the final value. */
578 sx_mrt_blend_opt[i] =
579 S_028760_COLOR_SRC_OPT(srcRGB_opt) |
580 S_028760_COLOR_DST_OPT(dstRGB_opt) |
581 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
582 S_028760_ALPHA_SRC_OPT(srcA_opt) |
583 S_028760_ALPHA_DST_OPT(dstA_opt) |
584 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
585
586 /* Set blend state. */
587 blend_cntl |= S_028780_ENABLE(1);
588 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
589 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
590 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
591
592 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
593 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
594 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
595 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
596 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
597 }
598 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
599
600 blend->blend_enable_4bit |= 0xfu << (i * 4);
601
602 /* This is only important for formats without alpha. */
603 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
604 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
605 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
606 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
607 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
608 dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA)
609 blend->need_src_alpha_4bit |= 0xfu << (i * 4);
610 }
611
612 if (blend->cb_target_mask) {
613 color_control |= S_028808_MODE(mode);
614 } else {
615 color_control |= S_028808_MODE(V_028808_CB_DISABLE);
616 }
617
618 if (sctx->screen->rbplus_allowed) {
619 /* Disable RB+ blend optimizations for dual source blending.
620 * Vulkan does this.
621 */
622 if (blend->dual_src_blend) {
623 for (int i = 0; i < 8; i++) {
624 sx_mrt_blend_opt[i] =
625 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |
626 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);
627 }
628 }
629
630 for (int i = 0; i < 8; i++)
631 si_pm4_set_reg(pm4, R_028760_SX_MRT0_BLEND_OPT + i * 4,
632 sx_mrt_blend_opt[i]);
633
634 /* RB+ doesn't work with dual source blending, logic op, and RESOLVE. */
635 if (blend->dual_src_blend || state->logicop_enable ||
636 mode == V_028808_CB_RESOLVE)
637 color_control |= S_028808_DISABLE_DUAL_QUAD(1);
638 }
639
640 si_pm4_set_reg(pm4, R_028808_CB_COLOR_CONTROL, color_control);
641 return blend;
642 }
643
644 static void *si_create_blend_state(struct pipe_context *ctx,
645 const struct pipe_blend_state *state)
646 {
647 return si_create_blend_state_mode(ctx, state, V_028808_CB_NORMAL);
648 }
649
650 static void si_bind_blend_state(struct pipe_context *ctx, void *state)
651 {
652 struct si_context *sctx = (struct si_context *)ctx;
653 struct si_state_blend *old_blend = sctx->queued.named.blend;
654 struct si_state_blend *blend = (struct si_state_blend *)state;
655
656 if (!state)
657 return;
658
659 si_pm4_bind_state(sctx, blend, state);
660
661 if (!old_blend ||
662 old_blend->cb_target_mask != blend->cb_target_mask ||
663 old_blend->dual_src_blend != blend->dual_src_blend ||
664 (old_blend->blend_enable_4bit != blend->blend_enable_4bit &&
665 sctx->framebuffer.nr_samples >= 2 &&
666 sctx->screen->dcc_msaa_allowed))
667 si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
668
669 if (!old_blend ||
670 old_blend->cb_target_mask != blend->cb_target_mask ||
671 old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
672 old_blend->alpha_to_one != blend->alpha_to_one ||
673 old_blend->dual_src_blend != blend->dual_src_blend ||
674 old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
675 old_blend->need_src_alpha_4bit != blend->need_src_alpha_4bit)
676 sctx->do_update_shaders = true;
677
678 if (sctx->screen->dpbb_allowed &&
679 (!old_blend ||
680 old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
681 old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
682 old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit))
683 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
684
685 if (sctx->screen->has_out_of_order_rast &&
686 (!old_blend ||
687 (old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
688 old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit ||
689 old_blend->commutative_4bit != blend->commutative_4bit ||
690 old_blend->logicop_enable != blend->logicop_enable)))
691 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
692 }
693
694 static void si_delete_blend_state(struct pipe_context *ctx, void *state)
695 {
696 struct si_context *sctx = (struct si_context *)ctx;
697 si_pm4_delete_state(sctx, blend, (struct si_state_blend *)state);
698 }
699
700 static void si_set_blend_color(struct pipe_context *ctx,
701 const struct pipe_blend_color *state)
702 {
703 struct si_context *sctx = (struct si_context *)ctx;
704 static const struct pipe_blend_color zeros;
705
706 sctx->blend_color.state = *state;
707 sctx->blend_color.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
708 si_mark_atom_dirty(sctx, &sctx->atoms.s.blend_color);
709 }
710
711 static void si_emit_blend_color(struct si_context *sctx)
712 {
713 struct radeon_cmdbuf *cs = sctx->gfx_cs;
714
715 radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
716 radeon_emit_array(cs, (uint32_t*)sctx->blend_color.state.color, 4);
717 }
718
719 /*
720 * Clipping
721 */
722
723 static void si_set_clip_state(struct pipe_context *ctx,
724 const struct pipe_clip_state *state)
725 {
726 struct si_context *sctx = (struct si_context *)ctx;
727 struct pipe_constant_buffer cb;
728 static const struct pipe_clip_state zeros;
729
730 if (memcmp(&sctx->clip_state.state, state, sizeof(*state)) == 0)
731 return;
732
733 sctx->clip_state.state = *state;
734 sctx->clip_state.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
735 si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_state);
736
737 cb.buffer = NULL;
738 cb.user_buffer = state->ucp;
739 cb.buffer_offset = 0;
740 cb.buffer_size = 4*4*8;
741 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &cb);
742 pipe_resource_reference(&cb.buffer, NULL);
743 }
744
745 static void si_emit_clip_state(struct si_context *sctx)
746 {
747 struct radeon_cmdbuf *cs = sctx->gfx_cs;
748
749 radeon_set_context_reg_seq(cs, R_0285BC_PA_CL_UCP_0_X, 6*4);
750 radeon_emit_array(cs, (uint32_t*)sctx->clip_state.state.ucp, 6*4);
751 }
752
753 static void si_emit_clip_regs(struct si_context *sctx)
754 {
755 struct si_shader *vs = si_get_vs_state(sctx);
756 struct si_shader_selector *vs_sel = vs->selector;
757 struct tgsi_shader_info *info = &vs_sel->info;
758 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
759 unsigned window_space =
760 info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
761 unsigned clipdist_mask = vs_sel->clipdist_mask;
762 unsigned ucp_mask = clipdist_mask ? 0 : rs->clip_plane_enable & SIX_BITS;
763 unsigned culldist_mask = vs_sel->culldist_mask;
764 unsigned total_mask;
765
766 if (vs->key.opt.clip_disable) {
767 assert(!info->culldist_writemask);
768 clipdist_mask = 0;
769 culldist_mask = 0;
770 }
771 total_mask = clipdist_mask | culldist_mask;
772
773 /* Clip distances on points have no effect, so need to be implemented
774 * as cull distances. This applies for the clipvertex case as well.
775 *
776 * Setting this for primitives other than points should have no adverse
777 * effects.
778 */
779 clipdist_mask &= rs->clip_plane_enable;
780 culldist_mask |= clipdist_mask;
781
782 unsigned initial_cdw = sctx->gfx_cs->current.cdw;
783 radeon_opt_set_context_reg(sctx, R_02881C_PA_CL_VS_OUT_CNTL,
784 SI_TRACKED_PA_CL_VS_OUT_CNTL,
785 vs_sel->pa_cl_vs_out_cntl |
786 S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0F) != 0) |
787 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xF0) != 0) |
788 clipdist_mask | (culldist_mask << 8));
789 radeon_opt_set_context_reg(sctx, R_028810_PA_CL_CLIP_CNTL,
790 SI_TRACKED_PA_CL_CLIP_CNTL,
791 rs->pa_cl_clip_cntl |
792 ucp_mask |
793 S_028810_CLIP_DISABLE(window_space));
794
795 if (initial_cdw != sctx->gfx_cs->current.cdw)
796 sctx->context_roll = true;
797 }
798
799 /*
800 * inferred state between framebuffer and rasterizer
801 */
802 static void si_update_poly_offset_state(struct si_context *sctx)
803 {
804 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
805
806 if (!rs || !rs->uses_poly_offset || !sctx->framebuffer.state.zsbuf) {
807 si_pm4_bind_state(sctx, poly_offset, NULL);
808 return;
809 }
810
811 /* Use the user format, not db_render_format, so that the polygon
812 * offset behaves as expected by applications.
813 */
814 switch (sctx->framebuffer.state.zsbuf->texture->format) {
815 case PIPE_FORMAT_Z16_UNORM:
816 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[0]);
817 break;
818 default: /* 24-bit */
819 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[1]);
820 break;
821 case PIPE_FORMAT_Z32_FLOAT:
822 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
823 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[2]);
824 break;
825 }
826 }
827
828 /*
829 * Rasterizer
830 */
831
832 static uint32_t si_translate_fill(uint32_t func)
833 {
834 switch(func) {
835 case PIPE_POLYGON_MODE_FILL:
836 return V_028814_X_DRAW_TRIANGLES;
837 case PIPE_POLYGON_MODE_LINE:
838 return V_028814_X_DRAW_LINES;
839 case PIPE_POLYGON_MODE_POINT:
840 return V_028814_X_DRAW_POINTS;
841 default:
842 assert(0);
843 return V_028814_X_DRAW_POINTS;
844 }
845 }
846
847 static void *si_create_rs_state(struct pipe_context *ctx,
848 const struct pipe_rasterizer_state *state)
849 {
850 struct si_screen *sscreen = ((struct si_context *)ctx)->screen;
851 struct si_state_rasterizer *rs = CALLOC_STRUCT(si_state_rasterizer);
852 struct si_pm4_state *pm4 = &rs->pm4;
853 unsigned tmp, i;
854 float psize_min, psize_max;
855
856 if (!rs) {
857 return NULL;
858 }
859
860 rs->scissor_enable = state->scissor;
861 rs->clip_halfz = state->clip_halfz;
862 rs->two_side = state->light_twoside;
863 rs->multisample_enable = state->multisample;
864 rs->force_persample_interp = state->force_persample_interp;
865 rs->clip_plane_enable = state->clip_plane_enable;
866 rs->half_pixel_center = state->half_pixel_center;
867 rs->line_stipple_enable = state->line_stipple_enable;
868 rs->poly_stipple_enable = state->poly_stipple_enable;
869 rs->line_smooth = state->line_smooth;
870 rs->line_width = state->line_width;
871 rs->poly_smooth = state->poly_smooth;
872 rs->uses_poly_offset = state->offset_point || state->offset_line ||
873 state->offset_tri;
874 rs->clamp_fragment_color = state->clamp_fragment_color;
875 rs->clamp_vertex_color = state->clamp_vertex_color;
876 rs->flatshade = state->flatshade;
877 rs->sprite_coord_enable = state->sprite_coord_enable;
878 rs->rasterizer_discard = state->rasterizer_discard;
879 rs->pa_sc_line_stipple = state->line_stipple_enable ?
880 S_028A0C_LINE_PATTERN(state->line_stipple_pattern) |
881 S_028A0C_REPEAT_COUNT(state->line_stipple_factor) : 0;
882 rs->pa_cl_clip_cntl =
883 S_028810_DX_CLIP_SPACE_DEF(state->clip_halfz) |
884 S_028810_ZCLIP_NEAR_DISABLE(!state->depth_clip_near) |
885 S_028810_ZCLIP_FAR_DISABLE(!state->depth_clip_far) |
886 S_028810_DX_RASTERIZATION_KILL(state->rasterizer_discard) |
887 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
888
889 si_pm4_set_reg(pm4, R_0286D4_SPI_INTERP_CONTROL_0,
890 S_0286D4_FLAT_SHADE_ENA(1) |
891 S_0286D4_PNT_SPRITE_ENA(state->point_quad_rasterization) |
892 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
893 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
894 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
895 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
896 S_0286D4_PNT_SPRITE_TOP_1(state->sprite_coord_mode != PIPE_SPRITE_COORD_UPPER_LEFT));
897
898 /* point size 12.4 fixed point */
899 tmp = (unsigned)(state->point_size * 8.0);
900 si_pm4_set_reg(pm4, R_028A00_PA_SU_POINT_SIZE, S_028A00_HEIGHT(tmp) | S_028A00_WIDTH(tmp));
901
902 if (state->point_size_per_vertex) {
903 psize_min = util_get_min_point_size(state);
904 psize_max = SI_MAX_POINT_SIZE;
905 } else {
906 /* Force the point size to be as if the vertex output was disabled. */
907 psize_min = state->point_size;
908 psize_max = state->point_size;
909 }
910 rs->max_point_size = psize_max;
911
912 /* Divide by two, because 0.5 = 1 pixel. */
913 si_pm4_set_reg(pm4, R_028A04_PA_SU_POINT_MINMAX,
914 S_028A04_MIN_SIZE(si_pack_float_12p4(psize_min/2)) |
915 S_028A04_MAX_SIZE(si_pack_float_12p4(psize_max/2)));
916
917 si_pm4_set_reg(pm4, R_028A08_PA_SU_LINE_CNTL,
918 S_028A08_WIDTH(si_pack_float_12p4(state->line_width/2)));
919 si_pm4_set_reg(pm4, R_028A48_PA_SC_MODE_CNTL_0,
920 S_028A48_LINE_STIPPLE_ENABLE(state->line_stipple_enable) |
921 S_028A48_MSAA_ENABLE(state->multisample ||
922 state->poly_smooth ||
923 state->line_smooth) |
924 S_028A48_VPORT_SCISSOR_ENABLE(1) |
925 S_028A48_ALTERNATE_RBS_PER_TILE(sscreen->info.chip_class >= GFX9));
926
927 si_pm4_set_reg(pm4, R_028B7C_PA_SU_POLY_OFFSET_CLAMP, fui(state->offset_clamp));
928 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
929 S_028814_PROVOKING_VTX_LAST(!state->flatshade_first) |
930 S_028814_CULL_FRONT((state->cull_face & PIPE_FACE_FRONT) ? 1 : 0) |
931 S_028814_CULL_BACK((state->cull_face & PIPE_FACE_BACK) ? 1 : 0) |
932 S_028814_FACE(!state->front_ccw) |
933 S_028814_POLY_OFFSET_FRONT_ENABLE(util_get_offset(state, state->fill_front)) |
934 S_028814_POLY_OFFSET_BACK_ENABLE(util_get_offset(state, state->fill_back)) |
935 S_028814_POLY_OFFSET_PARA_ENABLE(state->offset_point || state->offset_line) |
936 S_028814_POLY_MODE(state->fill_front != PIPE_POLYGON_MODE_FILL ||
937 state->fill_back != PIPE_POLYGON_MODE_FILL) |
938 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(state->fill_front)) |
939 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(state->fill_back)));
940
941 if (!rs->uses_poly_offset)
942 return rs;
943
944 rs->pm4_poly_offset = CALLOC(3, sizeof(struct si_pm4_state));
945 if (!rs->pm4_poly_offset) {
946 FREE(rs);
947 return NULL;
948 }
949
950 /* Precalculate polygon offset states for 16-bit, 24-bit, and 32-bit zbuffers. */
951 for (i = 0; i < 3; i++) {
952 struct si_pm4_state *pm4 = &rs->pm4_poly_offset[i];
953 float offset_units = state->offset_units;
954 float offset_scale = state->offset_scale * 16.0f;
955 uint32_t pa_su_poly_offset_db_fmt_cntl = 0;
956
957 if (!state->offset_units_unscaled) {
958 switch (i) {
959 case 0: /* 16-bit zbuffer */
960 offset_units *= 4.0f;
961 pa_su_poly_offset_db_fmt_cntl =
962 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
963 break;
964 case 1: /* 24-bit zbuffer */
965 offset_units *= 2.0f;
966 pa_su_poly_offset_db_fmt_cntl =
967 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
968 break;
969 case 2: /* 32-bit zbuffer */
970 offset_units *= 1.0f;
971 pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
972 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
973 break;
974 }
975 }
976
977 si_pm4_set_reg(pm4, R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE,
978 fui(offset_scale));
979 si_pm4_set_reg(pm4, R_028B84_PA_SU_POLY_OFFSET_FRONT_OFFSET,
980 fui(offset_units));
981 si_pm4_set_reg(pm4, R_028B88_PA_SU_POLY_OFFSET_BACK_SCALE,
982 fui(offset_scale));
983 si_pm4_set_reg(pm4, R_028B8C_PA_SU_POLY_OFFSET_BACK_OFFSET,
984 fui(offset_units));
985 si_pm4_set_reg(pm4, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL,
986 pa_su_poly_offset_db_fmt_cntl);
987 }
988
989 return rs;
990 }
991
992 static void si_bind_rs_state(struct pipe_context *ctx, void *state)
993 {
994 struct si_context *sctx = (struct si_context *)ctx;
995 struct si_state_rasterizer *old_rs =
996 (struct si_state_rasterizer*)sctx->queued.named.rasterizer;
997 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
998
999 if (!state)
1000 return;
1001
1002 if (!old_rs || old_rs->multisample_enable != rs->multisample_enable) {
1003 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1004
1005 /* Update the small primitive filter workaround if necessary. */
1006 if (sctx->screen->has_msaa_sample_loc_bug &&
1007 sctx->framebuffer.nr_samples > 1)
1008 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
1009 }
1010
1011 sctx->current_vs_state &= C_VS_STATE_CLAMP_VERTEX_COLOR;
1012 sctx->current_vs_state |= S_VS_STATE_CLAMP_VERTEX_COLOR(rs->clamp_vertex_color);
1013
1014 si_pm4_bind_state(sctx, rasterizer, rs);
1015 si_update_poly_offset_state(sctx);
1016
1017 if (!old_rs ||
1018 old_rs->scissor_enable != rs->scissor_enable) {
1019 sctx->scissors.dirty_mask = (1 << SI_MAX_VIEWPORTS) - 1;
1020 si_mark_atom_dirty(sctx, &sctx->atoms.s.scissors);
1021 }
1022
1023 if (!old_rs ||
1024 old_rs->line_width != rs->line_width ||
1025 old_rs->max_point_size != rs->max_point_size ||
1026 old_rs->half_pixel_center != rs->half_pixel_center)
1027 si_mark_atom_dirty(sctx, &sctx->atoms.s.guardband);
1028
1029 if (!old_rs ||
1030 old_rs->clip_halfz != rs->clip_halfz) {
1031 sctx->viewports.depth_range_dirty_mask = (1 << SI_MAX_VIEWPORTS) - 1;
1032 si_mark_atom_dirty(sctx, &sctx->atoms.s.viewports);
1033 }
1034
1035 if (!old_rs ||
1036 old_rs->clip_plane_enable != rs->clip_plane_enable ||
1037 old_rs->pa_cl_clip_cntl != rs->pa_cl_clip_cntl)
1038 si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_regs);
1039
1040 sctx->ia_multi_vgt_param_key.u.line_stipple_enabled =
1041 rs->line_stipple_enable;
1042
1043 if (!old_rs ||
1044 old_rs->clip_plane_enable != rs->clip_plane_enable ||
1045 old_rs->rasterizer_discard != rs->rasterizer_discard ||
1046 old_rs->sprite_coord_enable != rs->sprite_coord_enable ||
1047 old_rs->flatshade != rs->flatshade ||
1048 old_rs->two_side != rs->two_side ||
1049 old_rs->multisample_enable != rs->multisample_enable ||
1050 old_rs->poly_stipple_enable != rs->poly_stipple_enable ||
1051 old_rs->poly_smooth != rs->poly_smooth ||
1052 old_rs->line_smooth != rs->line_smooth ||
1053 old_rs->clamp_fragment_color != rs->clamp_fragment_color ||
1054 old_rs->force_persample_interp != rs->force_persample_interp)
1055 sctx->do_update_shaders = true;
1056 }
1057
1058 static void si_delete_rs_state(struct pipe_context *ctx, void *state)
1059 {
1060 struct si_context *sctx = (struct si_context *)ctx;
1061 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
1062
1063 if (sctx->queued.named.rasterizer == state)
1064 si_pm4_bind_state(sctx, poly_offset, NULL);
1065
1066 FREE(rs->pm4_poly_offset);
1067 si_pm4_delete_state(sctx, rasterizer, rs);
1068 }
1069
1070 /*
1071 * infeered state between dsa and stencil ref
1072 */
1073 static void si_emit_stencil_ref(struct si_context *sctx)
1074 {
1075 struct radeon_cmdbuf *cs = sctx->gfx_cs;
1076 struct pipe_stencil_ref *ref = &sctx->stencil_ref.state;
1077 struct si_dsa_stencil_ref_part *dsa = &sctx->stencil_ref.dsa_part;
1078
1079 radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
1080 radeon_emit(cs, S_028430_STENCILTESTVAL(ref->ref_value[0]) |
1081 S_028430_STENCILMASK(dsa->valuemask[0]) |
1082 S_028430_STENCILWRITEMASK(dsa->writemask[0]) |
1083 S_028430_STENCILOPVAL(1));
1084 radeon_emit(cs, S_028434_STENCILTESTVAL_BF(ref->ref_value[1]) |
1085 S_028434_STENCILMASK_BF(dsa->valuemask[1]) |
1086 S_028434_STENCILWRITEMASK_BF(dsa->writemask[1]) |
1087 S_028434_STENCILOPVAL_BF(1));
1088 }
1089
1090 static void si_set_stencil_ref(struct pipe_context *ctx,
1091 const struct pipe_stencil_ref *state)
1092 {
1093 struct si_context *sctx = (struct si_context *)ctx;
1094
1095 if (memcmp(&sctx->stencil_ref.state, state, sizeof(*state)) == 0)
1096 return;
1097
1098 sctx->stencil_ref.state = *state;
1099 si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
1100 }
1101
1102
1103 /*
1104 * DSA
1105 */
1106
1107 static uint32_t si_translate_stencil_op(int s_op)
1108 {
1109 switch (s_op) {
1110 case PIPE_STENCIL_OP_KEEP:
1111 return V_02842C_STENCIL_KEEP;
1112 case PIPE_STENCIL_OP_ZERO:
1113 return V_02842C_STENCIL_ZERO;
1114 case PIPE_STENCIL_OP_REPLACE:
1115 return V_02842C_STENCIL_REPLACE_TEST;
1116 case PIPE_STENCIL_OP_INCR:
1117 return V_02842C_STENCIL_ADD_CLAMP;
1118 case PIPE_STENCIL_OP_DECR:
1119 return V_02842C_STENCIL_SUB_CLAMP;
1120 case PIPE_STENCIL_OP_INCR_WRAP:
1121 return V_02842C_STENCIL_ADD_WRAP;
1122 case PIPE_STENCIL_OP_DECR_WRAP:
1123 return V_02842C_STENCIL_SUB_WRAP;
1124 case PIPE_STENCIL_OP_INVERT:
1125 return V_02842C_STENCIL_INVERT;
1126 default:
1127 PRINT_ERR("Unknown stencil op %d", s_op);
1128 assert(0);
1129 break;
1130 }
1131 return 0;
1132 }
1133
1134 static bool si_dsa_writes_stencil(const struct pipe_stencil_state *s)
1135 {
1136 return s->enabled && s->writemask &&
1137 (s->fail_op != PIPE_STENCIL_OP_KEEP ||
1138 s->zfail_op != PIPE_STENCIL_OP_KEEP ||
1139 s->zpass_op != PIPE_STENCIL_OP_KEEP);
1140 }
1141
1142 static bool si_order_invariant_stencil_op(enum pipe_stencil_op op)
1143 {
1144 /* REPLACE is normally order invariant, except when the stencil
1145 * reference value is written by the fragment shader. Tracking this
1146 * interaction does not seem worth the effort, so be conservative. */
1147 return op != PIPE_STENCIL_OP_INCR &&
1148 op != PIPE_STENCIL_OP_DECR &&
1149 op != PIPE_STENCIL_OP_REPLACE;
1150 }
1151
1152 /* Compute whether, assuming Z writes are disabled, this stencil state is order
1153 * invariant in the sense that the set of passing fragments as well as the
1154 * final stencil buffer result does not depend on the order of fragments. */
1155 static bool si_order_invariant_stencil_state(const struct pipe_stencil_state *state)
1156 {
1157 return !state->enabled || !state->writemask ||
1158 /* The following assumes that Z writes are disabled. */
1159 (state->func == PIPE_FUNC_ALWAYS &&
1160 si_order_invariant_stencil_op(state->zpass_op) &&
1161 si_order_invariant_stencil_op(state->zfail_op)) ||
1162 (state->func == PIPE_FUNC_NEVER &&
1163 si_order_invariant_stencil_op(state->fail_op));
1164 }
1165
1166 static void *si_create_dsa_state(struct pipe_context *ctx,
1167 const struct pipe_depth_stencil_alpha_state *state)
1168 {
1169 struct si_context *sctx = (struct si_context *)ctx;
1170 struct si_state_dsa *dsa = CALLOC_STRUCT(si_state_dsa);
1171 struct si_pm4_state *pm4 = &dsa->pm4;
1172 unsigned db_depth_control;
1173 uint32_t db_stencil_control = 0;
1174
1175 if (!dsa) {
1176 return NULL;
1177 }
1178
1179 dsa->stencil_ref.valuemask[0] = state->stencil[0].valuemask;
1180 dsa->stencil_ref.valuemask[1] = state->stencil[1].valuemask;
1181 dsa->stencil_ref.writemask[0] = state->stencil[0].writemask;
1182 dsa->stencil_ref.writemask[1] = state->stencil[1].writemask;
1183
1184 db_depth_control = S_028800_Z_ENABLE(state->depth.enabled) |
1185 S_028800_Z_WRITE_ENABLE(state->depth.writemask) |
1186 S_028800_ZFUNC(state->depth.func) |
1187 S_028800_DEPTH_BOUNDS_ENABLE(state->depth.bounds_test);
1188
1189 /* stencil */
1190 if (state->stencil[0].enabled) {
1191 db_depth_control |= S_028800_STENCIL_ENABLE(1);
1192 db_depth_control |= S_028800_STENCILFUNC(state->stencil[0].func);
1193 db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(state->stencil[0].fail_op));
1194 db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(state->stencil[0].zpass_op));
1195 db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(state->stencil[0].zfail_op));
1196
1197 if (state->stencil[1].enabled) {
1198 db_depth_control |= S_028800_BACKFACE_ENABLE(1);
1199 db_depth_control |= S_028800_STENCILFUNC_BF(state->stencil[1].func);
1200 db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(state->stencil[1].fail_op));
1201 db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(state->stencil[1].zpass_op));
1202 db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(state->stencil[1].zfail_op));
1203 }
1204 }
1205
1206 /* alpha */
1207 if (state->alpha.enabled) {
1208 dsa->alpha_func = state->alpha.func;
1209
1210 si_pm4_set_reg(pm4, R_00B030_SPI_SHADER_USER_DATA_PS_0 +
1211 SI_SGPR_ALPHA_REF * 4, fui(state->alpha.ref_value));
1212 } else {
1213 dsa->alpha_func = PIPE_FUNC_ALWAYS;
1214 }
1215
1216 si_pm4_set_reg(pm4, R_028800_DB_DEPTH_CONTROL, db_depth_control);
1217 if (state->stencil[0].enabled)
1218 si_pm4_set_reg(pm4, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
1219 if (state->depth.bounds_test) {
1220 si_pm4_set_reg(pm4, R_028020_DB_DEPTH_BOUNDS_MIN, fui(state->depth.bounds_min));
1221 si_pm4_set_reg(pm4, R_028024_DB_DEPTH_BOUNDS_MAX, fui(state->depth.bounds_max));
1222 }
1223
1224 dsa->depth_enabled = state->depth.enabled;
1225 dsa->depth_write_enabled = state->depth.enabled &&
1226 state->depth.writemask;
1227 dsa->stencil_enabled = state->stencil[0].enabled;
1228 dsa->stencil_write_enabled = state->stencil[0].enabled &&
1229 (si_dsa_writes_stencil(&state->stencil[0]) ||
1230 si_dsa_writes_stencil(&state->stencil[1]));
1231 dsa->db_can_write = dsa->depth_write_enabled ||
1232 dsa->stencil_write_enabled;
1233
1234 bool zfunc_is_ordered =
1235 state->depth.func == PIPE_FUNC_NEVER ||
1236 state->depth.func == PIPE_FUNC_LESS ||
1237 state->depth.func == PIPE_FUNC_LEQUAL ||
1238 state->depth.func == PIPE_FUNC_GREATER ||
1239 state->depth.func == PIPE_FUNC_GEQUAL;
1240
1241 bool nozwrite_and_order_invariant_stencil =
1242 !dsa->db_can_write ||
1243 (!dsa->depth_write_enabled &&
1244 si_order_invariant_stencil_state(&state->stencil[0]) &&
1245 si_order_invariant_stencil_state(&state->stencil[1]));
1246
1247 dsa->order_invariance[1].zs =
1248 nozwrite_and_order_invariant_stencil ||
1249 (!dsa->stencil_write_enabled && zfunc_is_ordered);
1250 dsa->order_invariance[0].zs = !dsa->depth_write_enabled || zfunc_is_ordered;
1251
1252 dsa->order_invariance[1].pass_set =
1253 nozwrite_and_order_invariant_stencil ||
1254 (!dsa->stencil_write_enabled &&
1255 (state->depth.func == PIPE_FUNC_ALWAYS ||
1256 state->depth.func == PIPE_FUNC_NEVER));
1257 dsa->order_invariance[0].pass_set =
1258 !dsa->depth_write_enabled ||
1259 (state->depth.func == PIPE_FUNC_ALWAYS ||
1260 state->depth.func == PIPE_FUNC_NEVER);
1261
1262 dsa->order_invariance[1].pass_last =
1263 sctx->screen->assume_no_z_fights &&
1264 !dsa->stencil_write_enabled &&
1265 dsa->depth_write_enabled && zfunc_is_ordered;
1266 dsa->order_invariance[0].pass_last =
1267 sctx->screen->assume_no_z_fights &&
1268 dsa->depth_write_enabled && zfunc_is_ordered;
1269
1270 return dsa;
1271 }
1272
1273 static void si_bind_dsa_state(struct pipe_context *ctx, void *state)
1274 {
1275 struct si_context *sctx = (struct si_context *)ctx;
1276 struct si_state_dsa *old_dsa = sctx->queued.named.dsa;
1277 struct si_state_dsa *dsa = state;
1278
1279 if (!state)
1280 return;
1281
1282 si_pm4_bind_state(sctx, dsa, dsa);
1283
1284 if (memcmp(&dsa->stencil_ref, &sctx->stencil_ref.dsa_part,
1285 sizeof(struct si_dsa_stencil_ref_part)) != 0) {
1286 sctx->stencil_ref.dsa_part = dsa->stencil_ref;
1287 si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
1288 }
1289
1290 if (!old_dsa || old_dsa->alpha_func != dsa->alpha_func)
1291 sctx->do_update_shaders = true;
1292
1293 if (sctx->screen->dpbb_allowed &&
1294 (!old_dsa ||
1295 (old_dsa->depth_enabled != dsa->depth_enabled ||
1296 old_dsa->stencil_enabled != dsa->stencil_enabled ||
1297 old_dsa->db_can_write != dsa->db_can_write)))
1298 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
1299
1300 if (sctx->screen->has_out_of_order_rast &&
1301 (!old_dsa ||
1302 memcmp(old_dsa->order_invariance, dsa->order_invariance,
1303 sizeof(old_dsa->order_invariance))))
1304 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
1305 }
1306
1307 static void si_delete_dsa_state(struct pipe_context *ctx, void *state)
1308 {
1309 struct si_context *sctx = (struct si_context *)ctx;
1310 si_pm4_delete_state(sctx, dsa, (struct si_state_dsa *)state);
1311 }
1312
1313 static void *si_create_db_flush_dsa(struct si_context *sctx)
1314 {
1315 struct pipe_depth_stencil_alpha_state dsa = {};
1316
1317 return sctx->b.create_depth_stencil_alpha_state(&sctx->b, &dsa);
1318 }
1319
1320 /* DB RENDER STATE */
1321
1322 static void si_set_active_query_state(struct pipe_context *ctx, boolean enable)
1323 {
1324 struct si_context *sctx = (struct si_context*)ctx;
1325
1326 /* Pipeline stat & streamout queries. */
1327 if (enable) {
1328 sctx->flags &= ~SI_CONTEXT_STOP_PIPELINE_STATS;
1329 sctx->flags |= SI_CONTEXT_START_PIPELINE_STATS;
1330 } else {
1331 sctx->flags &= ~SI_CONTEXT_START_PIPELINE_STATS;
1332 sctx->flags |= SI_CONTEXT_STOP_PIPELINE_STATS;
1333 }
1334
1335 /* Occlusion queries. */
1336 if (sctx->occlusion_queries_disabled != !enable) {
1337 sctx->occlusion_queries_disabled = !enable;
1338 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1339 }
1340 }
1341
1342 void si_set_occlusion_query_state(struct si_context *sctx,
1343 bool old_perfect_enable)
1344 {
1345 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1346
1347 bool perfect_enable = sctx->num_perfect_occlusion_queries != 0;
1348
1349 if (perfect_enable != old_perfect_enable)
1350 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
1351 }
1352
1353 void si_save_qbo_state(struct si_context *sctx, struct si_qbo_state *st)
1354 {
1355 st->saved_compute = sctx->cs_shader_state.program;
1356
1357 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &st->saved_const0);
1358 si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo);
1359
1360 st->saved_ssbo_writable_mask = 0;
1361
1362 for (unsigned i = 0; i < 3; i++) {
1363 if (sctx->const_and_shader_buffers[PIPE_SHADER_COMPUTE].writable_mask &
1364 (1u << si_get_shaderbuf_slot(i)))
1365 st->saved_ssbo_writable_mask |= 1 << i;
1366 }
1367 }
1368
1369 static void si_emit_db_render_state(struct si_context *sctx)
1370 {
1371 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1372 unsigned db_shader_control, db_render_control, db_count_control;
1373 unsigned initial_cdw = sctx->gfx_cs->current.cdw;
1374
1375 /* DB_RENDER_CONTROL */
1376 if (sctx->dbcb_depth_copy_enabled ||
1377 sctx->dbcb_stencil_copy_enabled) {
1378 db_render_control =
1379 S_028000_DEPTH_COPY(sctx->dbcb_depth_copy_enabled) |
1380 S_028000_STENCIL_COPY(sctx->dbcb_stencil_copy_enabled) |
1381 S_028000_COPY_CENTROID(1) |
1382 S_028000_COPY_SAMPLE(sctx->dbcb_copy_sample);
1383 } else if (sctx->db_flush_depth_inplace || sctx->db_flush_stencil_inplace) {
1384 db_render_control =
1385 S_028000_DEPTH_COMPRESS_DISABLE(sctx->db_flush_depth_inplace) |
1386 S_028000_STENCIL_COMPRESS_DISABLE(sctx->db_flush_stencil_inplace);
1387 } else {
1388 db_render_control =
1389 S_028000_DEPTH_CLEAR_ENABLE(sctx->db_depth_clear) |
1390 S_028000_STENCIL_CLEAR_ENABLE(sctx->db_stencil_clear);
1391 }
1392
1393 /* DB_COUNT_CONTROL (occlusion queries) */
1394 if (sctx->num_occlusion_queries > 0 &&
1395 !sctx->occlusion_queries_disabled) {
1396 bool perfect = sctx->num_perfect_occlusion_queries > 0;
1397
1398 if (sctx->chip_class >= CIK) {
1399 unsigned log_sample_rate = sctx->framebuffer.log_samples;
1400
1401 /* Stoney doesn't increment occlusion query counters
1402 * if the sample rate is 16x. Use 8x sample rate instead.
1403 */
1404 if (sctx->family == CHIP_STONEY)
1405 log_sample_rate = MIN2(log_sample_rate, 3);
1406
1407 db_count_control =
1408 S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1409 S_028004_SAMPLE_RATE(log_sample_rate) |
1410 S_028004_ZPASS_ENABLE(1) |
1411 S_028004_SLICE_EVEN_ENABLE(1) |
1412 S_028004_SLICE_ODD_ENABLE(1);
1413 } else {
1414 db_count_control =
1415 S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1416 S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples);
1417 }
1418 } else {
1419 /* Disable occlusion queries. */
1420 if (sctx->chip_class >= CIK) {
1421 db_count_control = 0;
1422 } else {
1423 db_count_control = S_028004_ZPASS_INCREMENT_DISABLE(1);
1424 }
1425 }
1426
1427 radeon_opt_set_context_reg2(sctx, R_028000_DB_RENDER_CONTROL,
1428 SI_TRACKED_DB_RENDER_CONTROL, db_render_control,
1429 db_count_control);
1430
1431 /* DB_RENDER_OVERRIDE2 */
1432 radeon_opt_set_context_reg(sctx, R_028010_DB_RENDER_OVERRIDE2,
1433 SI_TRACKED_DB_RENDER_OVERRIDE2,
1434 S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(sctx->db_depth_disable_expclear) |
1435 S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(sctx->db_stencil_disable_expclear) |
1436 S_028010_DECOMPRESS_Z_ON_FLUSH(sctx->framebuffer.nr_samples >= 4));
1437
1438 db_shader_control = sctx->ps_db_shader_control;
1439
1440 /* Bug workaround for smoothing (overrasterization) on SI. */
1441 if (sctx->chip_class == SI && sctx->smoothing_enabled) {
1442 db_shader_control &= C_02880C_Z_ORDER;
1443 db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z);
1444 }
1445
1446 /* Disable the gl_SampleMask fragment shader output if MSAA is disabled. */
1447 if (!rs->multisample_enable)
1448 db_shader_control &= C_02880C_MASK_EXPORT_ENABLE;
1449
1450 if (sctx->screen->has_rbplus &&
1451 !sctx->screen->rbplus_allowed)
1452 db_shader_control |= S_02880C_DUAL_QUAD_DISABLE(1);
1453
1454 radeon_opt_set_context_reg(sctx, R_02880C_DB_SHADER_CONTROL,
1455 SI_TRACKED_DB_SHADER_CONTROL, db_shader_control);
1456
1457 if (initial_cdw != sctx->gfx_cs->current.cdw)
1458 sctx->context_roll = true;
1459 }
1460
1461 /*
1462 * format translation
1463 */
1464 static uint32_t si_translate_colorformat(enum pipe_format format)
1465 {
1466 const struct util_format_description *desc = util_format_description(format);
1467 if (!desc)
1468 return V_028C70_COLOR_INVALID;
1469
1470 #define HAS_SIZE(x,y,z,w) \
1471 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
1472 desc->channel[2].size == (z) && desc->channel[3].size == (w))
1473
1474 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1475 return V_028C70_COLOR_10_11_11;
1476
1477 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1478 return V_028C70_COLOR_INVALID;
1479
1480 /* hw cannot support mixed formats (except depth/stencil, since
1481 * stencil is not written to). */
1482 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1483 return V_028C70_COLOR_INVALID;
1484
1485 switch (desc->nr_channels) {
1486 case 1:
1487 switch (desc->channel[0].size) {
1488 case 8:
1489 return V_028C70_COLOR_8;
1490 case 16:
1491 return V_028C70_COLOR_16;
1492 case 32:
1493 return V_028C70_COLOR_32;
1494 }
1495 break;
1496 case 2:
1497 if (desc->channel[0].size == desc->channel[1].size) {
1498 switch (desc->channel[0].size) {
1499 case 8:
1500 return V_028C70_COLOR_8_8;
1501 case 16:
1502 return V_028C70_COLOR_16_16;
1503 case 32:
1504 return V_028C70_COLOR_32_32;
1505 }
1506 } else if (HAS_SIZE(8,24,0,0)) {
1507 return V_028C70_COLOR_24_8;
1508 } else if (HAS_SIZE(24,8,0,0)) {
1509 return V_028C70_COLOR_8_24;
1510 }
1511 break;
1512 case 3:
1513 if (HAS_SIZE(5,6,5,0)) {
1514 return V_028C70_COLOR_5_6_5;
1515 } else if (HAS_SIZE(32,8,24,0)) {
1516 return V_028C70_COLOR_X24_8_32_FLOAT;
1517 }
1518 break;
1519 case 4:
1520 if (desc->channel[0].size == desc->channel[1].size &&
1521 desc->channel[0].size == desc->channel[2].size &&
1522 desc->channel[0].size == desc->channel[3].size) {
1523 switch (desc->channel[0].size) {
1524 case 4:
1525 return V_028C70_COLOR_4_4_4_4;
1526 case 8:
1527 return V_028C70_COLOR_8_8_8_8;
1528 case 16:
1529 return V_028C70_COLOR_16_16_16_16;
1530 case 32:
1531 return V_028C70_COLOR_32_32_32_32;
1532 }
1533 } else if (HAS_SIZE(5,5,5,1)) {
1534 return V_028C70_COLOR_1_5_5_5;
1535 } else if (HAS_SIZE(1,5,5,5)) {
1536 return V_028C70_COLOR_5_5_5_1;
1537 } else if (HAS_SIZE(10,10,10,2)) {
1538 return V_028C70_COLOR_2_10_10_10;
1539 }
1540 break;
1541 }
1542 return V_028C70_COLOR_INVALID;
1543 }
1544
1545 static uint32_t si_colorformat_endian_swap(uint32_t colorformat)
1546 {
1547 if (SI_BIG_ENDIAN) {
1548 switch(colorformat) {
1549 /* 8-bit buffers. */
1550 case V_028C70_COLOR_8:
1551 return V_028C70_ENDIAN_NONE;
1552
1553 /* 16-bit buffers. */
1554 case V_028C70_COLOR_5_6_5:
1555 case V_028C70_COLOR_1_5_5_5:
1556 case V_028C70_COLOR_4_4_4_4:
1557 case V_028C70_COLOR_16:
1558 case V_028C70_COLOR_8_8:
1559 return V_028C70_ENDIAN_8IN16;
1560
1561 /* 32-bit buffers. */
1562 case V_028C70_COLOR_8_8_8_8:
1563 case V_028C70_COLOR_2_10_10_10:
1564 case V_028C70_COLOR_8_24:
1565 case V_028C70_COLOR_24_8:
1566 case V_028C70_COLOR_16_16:
1567 return V_028C70_ENDIAN_8IN32;
1568
1569 /* 64-bit buffers. */
1570 case V_028C70_COLOR_16_16_16_16:
1571 return V_028C70_ENDIAN_8IN16;
1572
1573 case V_028C70_COLOR_32_32:
1574 return V_028C70_ENDIAN_8IN32;
1575
1576 /* 128-bit buffers. */
1577 case V_028C70_COLOR_32_32_32_32:
1578 return V_028C70_ENDIAN_8IN32;
1579 default:
1580 return V_028C70_ENDIAN_NONE; /* Unsupported. */
1581 }
1582 } else {
1583 return V_028C70_ENDIAN_NONE;
1584 }
1585 }
1586
1587 static uint32_t si_translate_dbformat(enum pipe_format format)
1588 {
1589 switch (format) {
1590 case PIPE_FORMAT_Z16_UNORM:
1591 return V_028040_Z_16;
1592 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1593 case PIPE_FORMAT_X8Z24_UNORM:
1594 case PIPE_FORMAT_Z24X8_UNORM:
1595 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1596 return V_028040_Z_24; /* deprecated on SI */
1597 case PIPE_FORMAT_Z32_FLOAT:
1598 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1599 return V_028040_Z_32_FLOAT;
1600 default:
1601 return V_028040_Z_INVALID;
1602 }
1603 }
1604
1605 /*
1606 * Texture translation
1607 */
1608
1609 static uint32_t si_translate_texformat(struct pipe_screen *screen,
1610 enum pipe_format format,
1611 const struct util_format_description *desc,
1612 int first_non_void)
1613 {
1614 struct si_screen *sscreen = (struct si_screen*)screen;
1615 bool uniform = true;
1616 int i;
1617
1618 /* Colorspace (return non-RGB formats directly). */
1619 switch (desc->colorspace) {
1620 /* Depth stencil formats */
1621 case UTIL_FORMAT_COLORSPACE_ZS:
1622 switch (format) {
1623 case PIPE_FORMAT_Z16_UNORM:
1624 return V_008F14_IMG_DATA_FORMAT_16;
1625 case PIPE_FORMAT_X24S8_UINT:
1626 case PIPE_FORMAT_S8X24_UINT:
1627 /*
1628 * Implemented as an 8_8_8_8 data format to fix texture
1629 * gathers in stencil sampling. This affects at least
1630 * GL45-CTS.texture_cube_map_array.sampling on VI.
1631 */
1632 if (sscreen->info.chip_class <= VI)
1633 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1634
1635 if (format == PIPE_FORMAT_X24S8_UINT)
1636 return V_008F14_IMG_DATA_FORMAT_8_24;
1637 else
1638 return V_008F14_IMG_DATA_FORMAT_24_8;
1639 case PIPE_FORMAT_Z24X8_UNORM:
1640 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1641 return V_008F14_IMG_DATA_FORMAT_8_24;
1642 case PIPE_FORMAT_X8Z24_UNORM:
1643 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1644 return V_008F14_IMG_DATA_FORMAT_24_8;
1645 case PIPE_FORMAT_S8_UINT:
1646 return V_008F14_IMG_DATA_FORMAT_8;
1647 case PIPE_FORMAT_Z32_FLOAT:
1648 return V_008F14_IMG_DATA_FORMAT_32;
1649 case PIPE_FORMAT_X32_S8X24_UINT:
1650 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1651 return V_008F14_IMG_DATA_FORMAT_X24_8_32;
1652 default:
1653 goto out_unknown;
1654 }
1655
1656 case UTIL_FORMAT_COLORSPACE_YUV:
1657 goto out_unknown; /* TODO */
1658
1659 case UTIL_FORMAT_COLORSPACE_SRGB:
1660 if (desc->nr_channels != 4 && desc->nr_channels != 1)
1661 goto out_unknown;
1662 break;
1663
1664 default:
1665 break;
1666 }
1667
1668 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
1669 if (!sscreen->info.has_format_bc1_through_bc7)
1670 goto out_unknown;
1671
1672 switch (format) {
1673 case PIPE_FORMAT_RGTC1_SNORM:
1674 case PIPE_FORMAT_LATC1_SNORM:
1675 case PIPE_FORMAT_RGTC1_UNORM:
1676 case PIPE_FORMAT_LATC1_UNORM:
1677 return V_008F14_IMG_DATA_FORMAT_BC4;
1678 case PIPE_FORMAT_RGTC2_SNORM:
1679 case PIPE_FORMAT_LATC2_SNORM:
1680 case PIPE_FORMAT_RGTC2_UNORM:
1681 case PIPE_FORMAT_LATC2_UNORM:
1682 return V_008F14_IMG_DATA_FORMAT_BC5;
1683 default:
1684 goto out_unknown;
1685 }
1686 }
1687
1688 if (desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
1689 (sscreen->info.family == CHIP_STONEY ||
1690 sscreen->info.family == CHIP_VEGA10 ||
1691 sscreen->info.family == CHIP_RAVEN)) {
1692 switch (format) {
1693 case PIPE_FORMAT_ETC1_RGB8:
1694 case PIPE_FORMAT_ETC2_RGB8:
1695 case PIPE_FORMAT_ETC2_SRGB8:
1696 return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
1697 case PIPE_FORMAT_ETC2_RGB8A1:
1698 case PIPE_FORMAT_ETC2_SRGB8A1:
1699 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
1700 case PIPE_FORMAT_ETC2_RGBA8:
1701 case PIPE_FORMAT_ETC2_SRGBA8:
1702 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
1703 case PIPE_FORMAT_ETC2_R11_UNORM:
1704 case PIPE_FORMAT_ETC2_R11_SNORM:
1705 return V_008F14_IMG_DATA_FORMAT_ETC2_R;
1706 case PIPE_FORMAT_ETC2_RG11_UNORM:
1707 case PIPE_FORMAT_ETC2_RG11_SNORM:
1708 return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
1709 default:
1710 goto out_unknown;
1711 }
1712 }
1713
1714 if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
1715 if (!sscreen->info.has_format_bc1_through_bc7)
1716 goto out_unknown;
1717
1718 switch (format) {
1719 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1720 case PIPE_FORMAT_BPTC_SRGBA:
1721 return V_008F14_IMG_DATA_FORMAT_BC7;
1722 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1723 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1724 return V_008F14_IMG_DATA_FORMAT_BC6;
1725 default:
1726 goto out_unknown;
1727 }
1728 }
1729
1730 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1731 switch (format) {
1732 case PIPE_FORMAT_R8G8_B8G8_UNORM:
1733 case PIPE_FORMAT_G8R8_B8R8_UNORM:
1734 return V_008F14_IMG_DATA_FORMAT_GB_GR;
1735 case PIPE_FORMAT_G8R8_G8B8_UNORM:
1736 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1737 return V_008F14_IMG_DATA_FORMAT_BG_RG;
1738 default:
1739 goto out_unknown;
1740 }
1741 }
1742
1743 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
1744 if (!sscreen->info.has_format_bc1_through_bc7)
1745 goto out_unknown;
1746
1747 switch (format) {
1748 case PIPE_FORMAT_DXT1_RGB:
1749 case PIPE_FORMAT_DXT1_RGBA:
1750 case PIPE_FORMAT_DXT1_SRGB:
1751 case PIPE_FORMAT_DXT1_SRGBA:
1752 return V_008F14_IMG_DATA_FORMAT_BC1;
1753 case PIPE_FORMAT_DXT3_RGBA:
1754 case PIPE_FORMAT_DXT3_SRGBA:
1755 return V_008F14_IMG_DATA_FORMAT_BC2;
1756 case PIPE_FORMAT_DXT5_RGBA:
1757 case PIPE_FORMAT_DXT5_SRGBA:
1758 return V_008F14_IMG_DATA_FORMAT_BC3;
1759 default:
1760 goto out_unknown;
1761 }
1762 }
1763
1764 if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
1765 return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
1766 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
1767 return V_008F14_IMG_DATA_FORMAT_10_11_11;
1768 }
1769
1770 /* R8G8Bx_SNORM - TODO CxV8U8 */
1771
1772 /* hw cannot support mixed formats (except depth/stencil, since only
1773 * depth is read).*/
1774 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1775 goto out_unknown;
1776
1777 /* See whether the components are of the same size. */
1778 for (i = 1; i < desc->nr_channels; i++) {
1779 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
1780 }
1781
1782 /* Non-uniform formats. */
1783 if (!uniform) {
1784 switch(desc->nr_channels) {
1785 case 3:
1786 if (desc->channel[0].size == 5 &&
1787 desc->channel[1].size == 6 &&
1788 desc->channel[2].size == 5) {
1789 return V_008F14_IMG_DATA_FORMAT_5_6_5;
1790 }
1791 goto out_unknown;
1792 case 4:
1793 if (desc->channel[0].size == 5 &&
1794 desc->channel[1].size == 5 &&
1795 desc->channel[2].size == 5 &&
1796 desc->channel[3].size == 1) {
1797 return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
1798 }
1799 if (desc->channel[0].size == 1 &&
1800 desc->channel[1].size == 5 &&
1801 desc->channel[2].size == 5 &&
1802 desc->channel[3].size == 5) {
1803 return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
1804 }
1805 if (desc->channel[0].size == 10 &&
1806 desc->channel[1].size == 10 &&
1807 desc->channel[2].size == 10 &&
1808 desc->channel[3].size == 2) {
1809 return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
1810 }
1811 goto out_unknown;
1812 }
1813 goto out_unknown;
1814 }
1815
1816 if (first_non_void < 0 || first_non_void > 3)
1817 goto out_unknown;
1818
1819 /* uniform formats */
1820 switch (desc->channel[first_non_void].size) {
1821 case 4:
1822 switch (desc->nr_channels) {
1823 #if 0 /* Not supported for render targets */
1824 case 2:
1825 return V_008F14_IMG_DATA_FORMAT_4_4;
1826 #endif
1827 case 4:
1828 return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
1829 }
1830 break;
1831 case 8:
1832 switch (desc->nr_channels) {
1833 case 1:
1834 return V_008F14_IMG_DATA_FORMAT_8;
1835 case 2:
1836 return V_008F14_IMG_DATA_FORMAT_8_8;
1837 case 4:
1838 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1839 }
1840 break;
1841 case 16:
1842 switch (desc->nr_channels) {
1843 case 1:
1844 return V_008F14_IMG_DATA_FORMAT_16;
1845 case 2:
1846 return V_008F14_IMG_DATA_FORMAT_16_16;
1847 case 4:
1848 return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
1849 }
1850 break;
1851 case 32:
1852 switch (desc->nr_channels) {
1853 case 1:
1854 return V_008F14_IMG_DATA_FORMAT_32;
1855 case 2:
1856 return V_008F14_IMG_DATA_FORMAT_32_32;
1857 #if 0 /* Not supported for render targets */
1858 case 3:
1859 return V_008F14_IMG_DATA_FORMAT_32_32_32;
1860 #endif
1861 case 4:
1862 return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
1863 }
1864 }
1865
1866 out_unknown:
1867 return ~0;
1868 }
1869
1870 static unsigned si_tex_wrap(unsigned wrap)
1871 {
1872 switch (wrap) {
1873 default:
1874 case PIPE_TEX_WRAP_REPEAT:
1875 return V_008F30_SQ_TEX_WRAP;
1876 case PIPE_TEX_WRAP_CLAMP:
1877 return V_008F30_SQ_TEX_CLAMP_HALF_BORDER;
1878 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1879 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
1880 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1881 return V_008F30_SQ_TEX_CLAMP_BORDER;
1882 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1883 return V_008F30_SQ_TEX_MIRROR;
1884 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1885 return V_008F30_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
1886 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1887 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
1888 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1889 return V_008F30_SQ_TEX_MIRROR_ONCE_BORDER;
1890 }
1891 }
1892
1893 static unsigned si_tex_mipfilter(unsigned filter)
1894 {
1895 switch (filter) {
1896 case PIPE_TEX_MIPFILTER_NEAREST:
1897 return V_008F38_SQ_TEX_Z_FILTER_POINT;
1898 case PIPE_TEX_MIPFILTER_LINEAR:
1899 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
1900 default:
1901 case PIPE_TEX_MIPFILTER_NONE:
1902 return V_008F38_SQ_TEX_Z_FILTER_NONE;
1903 }
1904 }
1905
1906 static unsigned si_tex_compare(unsigned compare)
1907 {
1908 switch (compare) {
1909 default:
1910 case PIPE_FUNC_NEVER:
1911 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
1912 case PIPE_FUNC_LESS:
1913 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
1914 case PIPE_FUNC_EQUAL:
1915 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
1916 case PIPE_FUNC_LEQUAL:
1917 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
1918 case PIPE_FUNC_GREATER:
1919 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
1920 case PIPE_FUNC_NOTEQUAL:
1921 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
1922 case PIPE_FUNC_GEQUAL:
1923 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
1924 case PIPE_FUNC_ALWAYS:
1925 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
1926 }
1927 }
1928
1929 static unsigned si_tex_dim(struct si_screen *sscreen, struct si_texture *tex,
1930 unsigned view_target, unsigned nr_samples)
1931 {
1932 unsigned res_target = tex->buffer.b.b.target;
1933
1934 if (view_target == PIPE_TEXTURE_CUBE ||
1935 view_target == PIPE_TEXTURE_CUBE_ARRAY)
1936 res_target = view_target;
1937 /* If interpreting cubemaps as something else, set 2D_ARRAY. */
1938 else if (res_target == PIPE_TEXTURE_CUBE ||
1939 res_target == PIPE_TEXTURE_CUBE_ARRAY)
1940 res_target = PIPE_TEXTURE_2D_ARRAY;
1941
1942 /* GFX9 allocates 1D textures as 2D. */
1943 if ((res_target == PIPE_TEXTURE_1D ||
1944 res_target == PIPE_TEXTURE_1D_ARRAY) &&
1945 sscreen->info.chip_class >= GFX9 &&
1946 tex->surface.u.gfx9.resource_type == RADEON_RESOURCE_2D) {
1947 if (res_target == PIPE_TEXTURE_1D)
1948 res_target = PIPE_TEXTURE_2D;
1949 else
1950 res_target = PIPE_TEXTURE_2D_ARRAY;
1951 }
1952
1953 switch (res_target) {
1954 default:
1955 case PIPE_TEXTURE_1D:
1956 return V_008F1C_SQ_RSRC_IMG_1D;
1957 case PIPE_TEXTURE_1D_ARRAY:
1958 return V_008F1C_SQ_RSRC_IMG_1D_ARRAY;
1959 case PIPE_TEXTURE_2D:
1960 case PIPE_TEXTURE_RECT:
1961 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA :
1962 V_008F1C_SQ_RSRC_IMG_2D;
1963 case PIPE_TEXTURE_2D_ARRAY:
1964 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY :
1965 V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
1966 case PIPE_TEXTURE_3D:
1967 return V_008F1C_SQ_RSRC_IMG_3D;
1968 case PIPE_TEXTURE_CUBE:
1969 case PIPE_TEXTURE_CUBE_ARRAY:
1970 return V_008F1C_SQ_RSRC_IMG_CUBE;
1971 }
1972 }
1973
1974 /*
1975 * Format support testing
1976 */
1977
1978 static bool si_is_sampler_format_supported(struct pipe_screen *screen, enum pipe_format format)
1979 {
1980 const struct util_format_description *desc = util_format_description(format);
1981 if (!desc)
1982 return false;
1983
1984 return si_translate_texformat(screen, format, desc,
1985 util_format_get_first_non_void_channel(format)) != ~0U;
1986 }
1987
1988 static uint32_t si_translate_buffer_dataformat(struct pipe_screen *screen,
1989 const struct util_format_description *desc,
1990 int first_non_void)
1991 {
1992 int i;
1993
1994 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
1995 return V_008F0C_BUF_DATA_FORMAT_10_11_11;
1996
1997 assert(first_non_void >= 0);
1998
1999 if (desc->nr_channels == 4 &&
2000 desc->channel[0].size == 10 &&
2001 desc->channel[1].size == 10 &&
2002 desc->channel[2].size == 10 &&
2003 desc->channel[3].size == 2)
2004 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
2005
2006 /* See whether the components are of the same size. */
2007 for (i = 0; i < desc->nr_channels; i++) {
2008 if (desc->channel[first_non_void].size != desc->channel[i].size)
2009 return V_008F0C_BUF_DATA_FORMAT_INVALID;
2010 }
2011
2012 switch (desc->channel[first_non_void].size) {
2013 case 8:
2014 switch (desc->nr_channels) {
2015 case 1:
2016 case 3: /* 3 loads */
2017 return V_008F0C_BUF_DATA_FORMAT_8;
2018 case 2:
2019 return V_008F0C_BUF_DATA_FORMAT_8_8;
2020 case 4:
2021 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
2022 }
2023 break;
2024 case 16:
2025 switch (desc->nr_channels) {
2026 case 1:
2027 case 3: /* 3 loads */
2028 return V_008F0C_BUF_DATA_FORMAT_16;
2029 case 2:
2030 return V_008F0C_BUF_DATA_FORMAT_16_16;
2031 case 4:
2032 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
2033 }
2034 break;
2035 case 32:
2036 switch (desc->nr_channels) {
2037 case 1:
2038 return V_008F0C_BUF_DATA_FORMAT_32;
2039 case 2:
2040 return V_008F0C_BUF_DATA_FORMAT_32_32;
2041 case 3:
2042 return V_008F0C_BUF_DATA_FORMAT_32_32_32;
2043 case 4:
2044 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2045 }
2046 break;
2047 case 64:
2048 /* Legacy double formats. */
2049 switch (desc->nr_channels) {
2050 case 1: /* 1 load */
2051 return V_008F0C_BUF_DATA_FORMAT_32_32;
2052 case 2: /* 1 load */
2053 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2054 case 3: /* 3 loads */
2055 return V_008F0C_BUF_DATA_FORMAT_32_32;
2056 case 4: /* 2 loads */
2057 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2058 }
2059 break;
2060 }
2061
2062 return V_008F0C_BUF_DATA_FORMAT_INVALID;
2063 }
2064
2065 static uint32_t si_translate_buffer_numformat(struct pipe_screen *screen,
2066 const struct util_format_description *desc,
2067 int first_non_void)
2068 {
2069 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
2070 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
2071
2072 assert(first_non_void >= 0);
2073
2074 switch (desc->channel[first_non_void].type) {
2075 case UTIL_FORMAT_TYPE_SIGNED:
2076 case UTIL_FORMAT_TYPE_FIXED:
2077 if (desc->channel[first_non_void].size >= 32 ||
2078 desc->channel[first_non_void].pure_integer)
2079 return V_008F0C_BUF_NUM_FORMAT_SINT;
2080 else if (desc->channel[first_non_void].normalized)
2081 return V_008F0C_BUF_NUM_FORMAT_SNORM;
2082 else
2083 return V_008F0C_BUF_NUM_FORMAT_SSCALED;
2084 break;
2085 case UTIL_FORMAT_TYPE_UNSIGNED:
2086 if (desc->channel[first_non_void].size >= 32 ||
2087 desc->channel[first_non_void].pure_integer)
2088 return V_008F0C_BUF_NUM_FORMAT_UINT;
2089 else if (desc->channel[first_non_void].normalized)
2090 return V_008F0C_BUF_NUM_FORMAT_UNORM;
2091 else
2092 return V_008F0C_BUF_NUM_FORMAT_USCALED;
2093 break;
2094 case UTIL_FORMAT_TYPE_FLOAT:
2095 default:
2096 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
2097 }
2098 }
2099
2100 static unsigned si_is_vertex_format_supported(struct pipe_screen *screen,
2101 enum pipe_format format,
2102 unsigned usage)
2103 {
2104 const struct util_format_description *desc;
2105 int first_non_void;
2106 unsigned data_format;
2107
2108 assert((usage & ~(PIPE_BIND_SHADER_IMAGE |
2109 PIPE_BIND_SAMPLER_VIEW |
2110 PIPE_BIND_VERTEX_BUFFER)) == 0);
2111
2112 desc = util_format_description(format);
2113 if (!desc)
2114 return 0;
2115
2116 /* There are no native 8_8_8 or 16_16_16 data formats, and we currently
2117 * select 8_8_8_8 and 16_16_16_16 instead. This works reasonably well
2118 * for read-only access (with caveats surrounding bounds checks), but
2119 * obviously fails for write access which we have to implement for
2120 * shader images. Luckily, OpenGL doesn't expect this to be supported
2121 * anyway, and so the only impact is on PBO uploads / downloads, which
2122 * shouldn't be expected to be fast for GL_RGB anyway.
2123 */
2124 if (desc->block.bits == 3 * 8 ||
2125 desc->block.bits == 3 * 16) {
2126 if (usage & (PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW)) {
2127 usage &= ~(PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW);
2128 if (!usage)
2129 return 0;
2130 }
2131 }
2132
2133 first_non_void = util_format_get_first_non_void_channel(format);
2134 data_format = si_translate_buffer_dataformat(screen, desc, first_non_void);
2135 if (data_format == V_008F0C_BUF_DATA_FORMAT_INVALID)
2136 return 0;
2137
2138 return usage;
2139 }
2140
2141 static bool si_is_colorbuffer_format_supported(enum pipe_format format)
2142 {
2143 return si_translate_colorformat(format) != V_028C70_COLOR_INVALID &&
2144 si_translate_colorswap(format, false) != ~0U;
2145 }
2146
2147 static bool si_is_zs_format_supported(enum pipe_format format)
2148 {
2149 return si_translate_dbformat(format) != V_028040_Z_INVALID;
2150 }
2151
2152 static boolean si_is_format_supported(struct pipe_screen *screen,
2153 enum pipe_format format,
2154 enum pipe_texture_target target,
2155 unsigned sample_count,
2156 unsigned storage_sample_count,
2157 unsigned usage)
2158 {
2159 struct si_screen *sscreen = (struct si_screen *)screen;
2160 unsigned retval = 0;
2161
2162 if (target >= PIPE_MAX_TEXTURE_TYPES) {
2163 PRINT_ERR("radeonsi: unsupported texture type %d\n", target);
2164 return false;
2165 }
2166
2167 if (MAX2(1, sample_count) < MAX2(1, storage_sample_count))
2168 return false;
2169
2170 if (sample_count > 1) {
2171 if (!screen->get_param(screen, PIPE_CAP_TEXTURE_MULTISAMPLE))
2172 return false;
2173
2174 if (usage & PIPE_BIND_SHADER_IMAGE)
2175 return false;
2176
2177 /* Only power-of-two sample counts are supported. */
2178 if (!util_is_power_of_two_or_zero(sample_count) ||
2179 !util_is_power_of_two_or_zero(storage_sample_count))
2180 return false;
2181
2182 /* MSAA support without framebuffer attachments. */
2183 if (format == PIPE_FORMAT_NONE && sample_count <= 16)
2184 return true;
2185
2186 if (!sscreen->info.has_eqaa_surface_allocator ||
2187 util_format_is_depth_or_stencil(format)) {
2188 /* Color without EQAA or depth/stencil. */
2189 if (sample_count > 8 ||
2190 sample_count != storage_sample_count)
2191 return false;
2192 } else {
2193 /* Color with EQAA. */
2194 if (sample_count > 16 ||
2195 storage_sample_count > 8)
2196 return false;
2197 }
2198 }
2199
2200 if (usage & (PIPE_BIND_SAMPLER_VIEW |
2201 PIPE_BIND_SHADER_IMAGE)) {
2202 if (target == PIPE_BUFFER) {
2203 retval |= si_is_vertex_format_supported(
2204 screen, format, usage & (PIPE_BIND_SAMPLER_VIEW |
2205 PIPE_BIND_SHADER_IMAGE));
2206 } else {
2207 if (si_is_sampler_format_supported(screen, format))
2208 retval |= usage & (PIPE_BIND_SAMPLER_VIEW |
2209 PIPE_BIND_SHADER_IMAGE);
2210 }
2211 }
2212
2213 if ((usage & (PIPE_BIND_RENDER_TARGET |
2214 PIPE_BIND_DISPLAY_TARGET |
2215 PIPE_BIND_SCANOUT |
2216 PIPE_BIND_SHARED |
2217 PIPE_BIND_BLENDABLE)) &&
2218 si_is_colorbuffer_format_supported(format)) {
2219 retval |= usage &
2220 (PIPE_BIND_RENDER_TARGET |
2221 PIPE_BIND_DISPLAY_TARGET |
2222 PIPE_BIND_SCANOUT |
2223 PIPE_BIND_SHARED);
2224 if (!util_format_is_pure_integer(format) &&
2225 !util_format_is_depth_or_stencil(format))
2226 retval |= usage & PIPE_BIND_BLENDABLE;
2227 }
2228
2229 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
2230 si_is_zs_format_supported(format)) {
2231 retval |= PIPE_BIND_DEPTH_STENCIL;
2232 }
2233
2234 if (usage & PIPE_BIND_VERTEX_BUFFER) {
2235 retval |= si_is_vertex_format_supported(screen, format,
2236 PIPE_BIND_VERTEX_BUFFER);
2237 }
2238
2239 if ((usage & PIPE_BIND_LINEAR) &&
2240 !util_format_is_compressed(format) &&
2241 !(usage & PIPE_BIND_DEPTH_STENCIL))
2242 retval |= PIPE_BIND_LINEAR;
2243
2244 return retval == usage;
2245 }
2246
2247 /*
2248 * framebuffer handling
2249 */
2250
2251 static void si_choose_spi_color_formats(struct si_surface *surf,
2252 unsigned format, unsigned swap,
2253 unsigned ntype, bool is_depth)
2254 {
2255 /* Alpha is needed for alpha-to-coverage.
2256 * Blending may be with or without alpha.
2257 */
2258 unsigned normal = 0; /* most optimal, may not support blending or export alpha */
2259 unsigned alpha = 0; /* exports alpha, but may not support blending */
2260 unsigned blend = 0; /* supports blending, but may not export alpha */
2261 unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
2262
2263 /* Choose the SPI color formats. These are required values for RB+.
2264 * Other chips have multiple choices, though they are not necessarily better.
2265 */
2266 switch (format) {
2267 case V_028C70_COLOR_5_6_5:
2268 case V_028C70_COLOR_1_5_5_5:
2269 case V_028C70_COLOR_5_5_5_1:
2270 case V_028C70_COLOR_4_4_4_4:
2271 case V_028C70_COLOR_10_11_11:
2272 case V_028C70_COLOR_11_11_10:
2273 case V_028C70_COLOR_8:
2274 case V_028C70_COLOR_8_8:
2275 case V_028C70_COLOR_8_8_8_8:
2276 case V_028C70_COLOR_10_10_10_2:
2277 case V_028C70_COLOR_2_10_10_10:
2278 if (ntype == V_028C70_NUMBER_UINT)
2279 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
2280 else if (ntype == V_028C70_NUMBER_SINT)
2281 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
2282 else
2283 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
2284 break;
2285
2286 case V_028C70_COLOR_16:
2287 case V_028C70_COLOR_16_16:
2288 case V_028C70_COLOR_16_16_16_16:
2289 if (ntype == V_028C70_NUMBER_UNORM ||
2290 ntype == V_028C70_NUMBER_SNORM) {
2291 /* UNORM16 and SNORM16 don't support blending */
2292 if (ntype == V_028C70_NUMBER_UNORM)
2293 normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
2294 else
2295 normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
2296
2297 /* Use 32 bits per channel for blending. */
2298 if (format == V_028C70_COLOR_16) {
2299 if (swap == V_028C70_SWAP_STD) { /* R */
2300 blend = V_028714_SPI_SHADER_32_R;
2301 blend_alpha = V_028714_SPI_SHADER_32_AR;
2302 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
2303 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
2304 else
2305 assert(0);
2306 } else if (format == V_028C70_COLOR_16_16) {
2307 if (swap == V_028C70_SWAP_STD) { /* RG */
2308 blend = V_028714_SPI_SHADER_32_GR;
2309 blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2310 } else if (swap == V_028C70_SWAP_ALT) /* RA */
2311 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
2312 else
2313 assert(0);
2314 } else /* 16_16_16_16 */
2315 blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2316 } else if (ntype == V_028C70_NUMBER_UINT)
2317 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
2318 else if (ntype == V_028C70_NUMBER_SINT)
2319 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
2320 else if (ntype == V_028C70_NUMBER_FLOAT)
2321 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
2322 else
2323 assert(0);
2324 break;
2325
2326 case V_028C70_COLOR_32:
2327 if (swap == V_028C70_SWAP_STD) { /* R */
2328 blend = normal = V_028714_SPI_SHADER_32_R;
2329 alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
2330 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
2331 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
2332 else
2333 assert(0);
2334 break;
2335
2336 case V_028C70_COLOR_32_32:
2337 if (swap == V_028C70_SWAP_STD) { /* RG */
2338 blend = normal = V_028714_SPI_SHADER_32_GR;
2339 alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2340 } else if (swap == V_028C70_SWAP_ALT) /* RA */
2341 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
2342 else
2343 assert(0);
2344 break;
2345
2346 case V_028C70_COLOR_32_32_32_32:
2347 case V_028C70_COLOR_8_24:
2348 case V_028C70_COLOR_24_8:
2349 case V_028C70_COLOR_X24_8_32_FLOAT:
2350 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
2351 break;
2352
2353 default:
2354 assert(0);
2355 return;
2356 }
2357
2358 /* The DB->CB copy needs 32_ABGR. */
2359 if (is_depth)
2360 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
2361
2362 surf->spi_shader_col_format = normal;
2363 surf->spi_shader_col_format_alpha = alpha;
2364 surf->spi_shader_col_format_blend = blend;
2365 surf->spi_shader_col_format_blend_alpha = blend_alpha;
2366 }
2367
2368 static void si_initialize_color_surface(struct si_context *sctx,
2369 struct si_surface *surf)
2370 {
2371 struct si_texture *tex = (struct si_texture*)surf->base.texture;
2372 unsigned color_info, color_attrib;
2373 unsigned format, swap, ntype, endian;
2374 const struct util_format_description *desc;
2375 int firstchan;
2376 unsigned blend_clamp = 0, blend_bypass = 0;
2377
2378 desc = util_format_description(surf->base.format);
2379 for (firstchan = 0; firstchan < 4; firstchan++) {
2380 if (desc->channel[firstchan].type != UTIL_FORMAT_TYPE_VOID) {
2381 break;
2382 }
2383 }
2384 if (firstchan == 4 || desc->channel[firstchan].type == UTIL_FORMAT_TYPE_FLOAT) {
2385 ntype = V_028C70_NUMBER_FLOAT;
2386 } else {
2387 ntype = V_028C70_NUMBER_UNORM;
2388 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
2389 ntype = V_028C70_NUMBER_SRGB;
2390 else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_SIGNED) {
2391 if (desc->channel[firstchan].pure_integer) {
2392 ntype = V_028C70_NUMBER_SINT;
2393 } else {
2394 assert(desc->channel[firstchan].normalized);
2395 ntype = V_028C70_NUMBER_SNORM;
2396 }
2397 } else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2398 if (desc->channel[firstchan].pure_integer) {
2399 ntype = V_028C70_NUMBER_UINT;
2400 } else {
2401 assert(desc->channel[firstchan].normalized);
2402 ntype = V_028C70_NUMBER_UNORM;
2403 }
2404 }
2405 }
2406
2407 format = si_translate_colorformat(surf->base.format);
2408 if (format == V_028C70_COLOR_INVALID) {
2409 PRINT_ERR("Invalid CB format: %d, disabling CB.\n", surf->base.format);
2410 }
2411 assert(format != V_028C70_COLOR_INVALID);
2412 swap = si_translate_colorswap(surf->base.format, false);
2413 endian = si_colorformat_endian_swap(format);
2414
2415 /* blend clamp should be set for all NORM/SRGB types */
2416 if (ntype == V_028C70_NUMBER_UNORM ||
2417 ntype == V_028C70_NUMBER_SNORM ||
2418 ntype == V_028C70_NUMBER_SRGB)
2419 blend_clamp = 1;
2420
2421 /* set blend bypass according to docs if SINT/UINT or
2422 8/24 COLOR variants */
2423 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
2424 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
2425 format == V_028C70_COLOR_X24_8_32_FLOAT) {
2426 blend_clamp = 0;
2427 blend_bypass = 1;
2428 }
2429
2430 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) {
2431 if (format == V_028C70_COLOR_8 ||
2432 format == V_028C70_COLOR_8_8 ||
2433 format == V_028C70_COLOR_8_8_8_8)
2434 surf->color_is_int8 = true;
2435 else if (format == V_028C70_COLOR_10_10_10_2 ||
2436 format == V_028C70_COLOR_2_10_10_10)
2437 surf->color_is_int10 = true;
2438 }
2439
2440 color_info = S_028C70_FORMAT(format) |
2441 S_028C70_COMP_SWAP(swap) |
2442 S_028C70_BLEND_CLAMP(blend_clamp) |
2443 S_028C70_BLEND_BYPASS(blend_bypass) |
2444 S_028C70_SIMPLE_FLOAT(1) |
2445 S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
2446 ntype != V_028C70_NUMBER_SNORM &&
2447 ntype != V_028C70_NUMBER_SRGB &&
2448 format != V_028C70_COLOR_8_24 &&
2449 format != V_028C70_COLOR_24_8) |
2450 S_028C70_NUMBER_TYPE(ntype) |
2451 S_028C70_ENDIAN(endian);
2452
2453 /* Intensity is implemented as Red, so treat it that way. */
2454 color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == PIPE_SWIZZLE_1 ||
2455 util_format_is_intensity(surf->base.format));
2456
2457 if (tex->buffer.b.b.nr_samples > 1) {
2458 unsigned log_samples = util_logbase2(tex->buffer.b.b.nr_samples);
2459 unsigned log_fragments = util_logbase2(tex->buffer.b.b.nr_storage_samples);
2460
2461 color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
2462 S_028C74_NUM_FRAGMENTS(log_fragments);
2463
2464 if (tex->surface.fmask_size) {
2465 color_info |= S_028C70_COMPRESSION(1);
2466 unsigned fmask_bankh = util_logbase2(tex->surface.u.legacy.fmask.bankh);
2467
2468 if (sctx->chip_class == SI) {
2469 /* due to a hw bug, FMASK_BANK_HEIGHT must be set on SI too */
2470 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
2471 }
2472 }
2473 }
2474
2475 if (sctx->chip_class >= VI) {
2476 unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
2477 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
2478
2479 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2480 64 for APU because all of our APUs to date use DIMMs which have
2481 a request granularity size of 64B while all other chips have a
2482 32B request size */
2483 if (!sctx->screen->info.has_dedicated_vram)
2484 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
2485
2486 if (tex->buffer.b.b.nr_storage_samples > 1) {
2487 if (tex->surface.bpe == 1)
2488 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
2489 else if (tex->surface.bpe == 2)
2490 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
2491 }
2492
2493 surf->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
2494 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
2495 S_028C78_INDEPENDENT_64B_BLOCKS(1);
2496 }
2497
2498 /* This must be set for fast clear to work without FMASK. */
2499 if (!tex->surface.fmask_size && sctx->chip_class == SI) {
2500 unsigned bankh = util_logbase2(tex->surface.u.legacy.bankh);
2501 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
2502 }
2503
2504 unsigned color_view = S_028C6C_SLICE_START(surf->base.u.tex.first_layer) |
2505 S_028C6C_SLICE_MAX(surf->base.u.tex.last_layer);
2506
2507 if (sctx->chip_class >= GFX9) {
2508 unsigned mip0_depth = util_max_layer(&tex->buffer.b.b, 0);
2509
2510 color_view |= S_028C6C_MIP_LEVEL(surf->base.u.tex.level);
2511 color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
2512 S_028C74_RESOURCE_TYPE(tex->surface.u.gfx9.resource_type);
2513 surf->cb_color_attrib2 = S_028C68_MIP0_WIDTH(surf->width0 - 1) |
2514 S_028C68_MIP0_HEIGHT(surf->height0 - 1) |
2515 S_028C68_MAX_MIP(tex->buffer.b.b.last_level);
2516 }
2517
2518 surf->cb_color_view = color_view;
2519 surf->cb_color_info = color_info;
2520 surf->cb_color_attrib = color_attrib;
2521
2522 /* Determine pixel shader export format */
2523 si_choose_spi_color_formats(surf, format, swap, ntype, tex->is_depth);
2524
2525 surf->color_initialized = true;
2526 }
2527
2528 static void si_init_depth_surface(struct si_context *sctx,
2529 struct si_surface *surf)
2530 {
2531 struct si_texture *tex = (struct si_texture*)surf->base.texture;
2532 unsigned level = surf->base.u.tex.level;
2533 unsigned format, stencil_format;
2534 uint32_t z_info, s_info;
2535
2536 format = si_translate_dbformat(tex->db_render_format);
2537 stencil_format = tex->surface.has_stencil ?
2538 V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
2539
2540 assert(format != V_028040_Z_INVALID);
2541 if (format == V_028040_Z_INVALID)
2542 PRINT_ERR("Invalid DB format: %d, disabling DB.\n", tex->buffer.b.b.format);
2543
2544 surf->db_depth_view = S_028008_SLICE_START(surf->base.u.tex.first_layer) |
2545 S_028008_SLICE_MAX(surf->base.u.tex.last_layer);
2546 surf->db_htile_data_base = 0;
2547 surf->db_htile_surface = 0;
2548
2549 if (sctx->chip_class >= GFX9) {
2550 assert(tex->surface.u.gfx9.surf_offset == 0);
2551 surf->db_depth_base = tex->buffer.gpu_address >> 8;
2552 surf->db_stencil_base = (tex->buffer.gpu_address +
2553 tex->surface.u.gfx9.stencil_offset) >> 8;
2554 z_info = S_028038_FORMAT(format) |
2555 S_028038_NUM_SAMPLES(util_logbase2(tex->buffer.b.b.nr_samples)) |
2556 S_028038_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
2557 S_028038_MAXMIP(tex->buffer.b.b.last_level);
2558 s_info = S_02803C_FORMAT(stencil_format) |
2559 S_02803C_SW_MODE(tex->surface.u.gfx9.stencil.swizzle_mode);
2560 surf->db_z_info2 = S_028068_EPITCH(tex->surface.u.gfx9.surf.epitch);
2561 surf->db_stencil_info2 = S_02806C_EPITCH(tex->surface.u.gfx9.stencil.epitch);
2562 surf->db_depth_view |= S_028008_MIPID(level);
2563 surf->db_depth_size = S_02801C_X_MAX(tex->buffer.b.b.width0 - 1) |
2564 S_02801C_Y_MAX(tex->buffer.b.b.height0 - 1);
2565
2566 if (si_htile_enabled(tex, level)) {
2567 z_info |= S_028038_TILE_SURFACE_ENABLE(1) |
2568 S_028038_ALLOW_EXPCLEAR(1);
2569
2570 if (tex->tc_compatible_htile) {
2571 unsigned max_zplanes = 4;
2572
2573 if (tex->db_render_format == PIPE_FORMAT_Z16_UNORM &&
2574 tex->buffer.b.b.nr_samples > 1)
2575 max_zplanes = 2;
2576
2577 z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes + 1) |
2578 S_028038_ITERATE_FLUSH(1);
2579 s_info |= S_02803C_ITERATE_FLUSH(1);
2580 }
2581
2582 if (tex->surface.has_stencil) {
2583 /* Stencil buffer workaround ported from the SI-CI-VI code.
2584 * See that for explanation.
2585 */
2586 s_info |= S_02803C_ALLOW_EXPCLEAR(tex->buffer.b.b.nr_samples <= 1);
2587 } else {
2588 /* Use all HTILE for depth if there's no stencil. */
2589 s_info |= S_02803C_TILE_STENCIL_DISABLE(1);
2590 }
2591
2592 surf->db_htile_data_base = (tex->buffer.gpu_address +
2593 tex->htile_offset) >> 8;
2594 surf->db_htile_surface = S_028ABC_FULL_CACHE(1) |
2595 S_028ABC_PIPE_ALIGNED(tex->surface.u.gfx9.htile.pipe_aligned) |
2596 S_028ABC_RB_ALIGNED(tex->surface.u.gfx9.htile.rb_aligned);
2597 }
2598 } else {
2599 /* SI-CI-VI */
2600 struct legacy_surf_level *levelinfo = &tex->surface.u.legacy.level[level];
2601
2602 assert(levelinfo->nblk_x % 8 == 0 && levelinfo->nblk_y % 8 == 0);
2603
2604 surf->db_depth_base = (tex->buffer.gpu_address +
2605 tex->surface.u.legacy.level[level].offset) >> 8;
2606 surf->db_stencil_base = (tex->buffer.gpu_address +
2607 tex->surface.u.legacy.stencil_level[level].offset) >> 8;
2608
2609 z_info = S_028040_FORMAT(format) |
2610 S_028040_NUM_SAMPLES(util_logbase2(tex->buffer.b.b.nr_samples));
2611 s_info = S_028044_FORMAT(stencil_format);
2612 surf->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(!tex->tc_compatible_htile);
2613
2614 if (sctx->chip_class >= CIK) {
2615 struct radeon_info *info = &sctx->screen->info;
2616 unsigned index = tex->surface.u.legacy.tiling_index[level];
2617 unsigned stencil_index = tex->surface.u.legacy.stencil_tiling_index[level];
2618 unsigned macro_index = tex->surface.u.legacy.macro_tile_index;
2619 unsigned tile_mode = info->si_tile_mode_array[index];
2620 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
2621 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
2622
2623 surf->db_depth_info |=
2624 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
2625 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
2626 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
2627 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
2628 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
2629 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
2630 z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
2631 s_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
2632 } else {
2633 unsigned tile_mode_index = si_tile_mode_index(tex, level, false);
2634 z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
2635 tile_mode_index = si_tile_mode_index(tex, level, true);
2636 s_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
2637 }
2638
2639 surf->db_depth_size = S_028058_PITCH_TILE_MAX((levelinfo->nblk_x / 8) - 1) |
2640 S_028058_HEIGHT_TILE_MAX((levelinfo->nblk_y / 8) - 1);
2641 surf->db_depth_slice = S_02805C_SLICE_TILE_MAX((levelinfo->nblk_x *
2642 levelinfo->nblk_y) / 64 - 1);
2643
2644 if (si_htile_enabled(tex, level)) {
2645 z_info |= S_028040_TILE_SURFACE_ENABLE(1) |
2646 S_028040_ALLOW_EXPCLEAR(1);
2647
2648 if (tex->surface.has_stencil) {
2649 /* Workaround: For a not yet understood reason, the
2650 * combination of MSAA, fast stencil clear and stencil
2651 * decompress messes with subsequent stencil buffer
2652 * uses. Problem was reproduced on Verde, Bonaire,
2653 * Tonga, and Carrizo.
2654 *
2655 * Disabling EXPCLEAR works around the problem.
2656 *
2657 * Check piglit's arb_texture_multisample-stencil-clear
2658 * test if you want to try changing this.
2659 */
2660 if (tex->buffer.b.b.nr_samples <= 1)
2661 s_info |= S_028044_ALLOW_EXPCLEAR(1);
2662 } else if (!tex->tc_compatible_htile) {
2663 /* Use all of the htile_buffer for depth if there's no stencil.
2664 * This must not be set when TC-compatible HTILE is enabled
2665 * due to a hw bug.
2666 */
2667 s_info |= S_028044_TILE_STENCIL_DISABLE(1);
2668 }
2669
2670 surf->db_htile_data_base = (tex->buffer.gpu_address +
2671 tex->htile_offset) >> 8;
2672 surf->db_htile_surface = S_028ABC_FULL_CACHE(1);
2673
2674 if (tex->tc_compatible_htile) {
2675 surf->db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
2676
2677 /* 0 = full compression. N = only compress up to N-1 Z planes. */
2678 if (tex->buffer.b.b.nr_samples <= 1)
2679 z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(5);
2680 else if (tex->buffer.b.b.nr_samples <= 4)
2681 z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(3);
2682 else
2683 z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(2);
2684 }
2685 }
2686 }
2687
2688 surf->db_z_info = z_info;
2689 surf->db_stencil_info = s_info;
2690
2691 surf->depth_initialized = true;
2692 }
2693
2694 void si_update_fb_dirtiness_after_rendering(struct si_context *sctx)
2695 {
2696 if (sctx->decompression_enabled)
2697 return;
2698
2699 if (sctx->framebuffer.state.zsbuf) {
2700 struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
2701 struct si_texture *tex = (struct si_texture *)surf->texture;
2702
2703 tex->dirty_level_mask |= 1 << surf->u.tex.level;
2704
2705 if (tex->surface.has_stencil)
2706 tex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
2707 }
2708
2709 unsigned compressed_cb_mask = sctx->framebuffer.compressed_cb_mask;
2710 while (compressed_cb_mask) {
2711 unsigned i = u_bit_scan(&compressed_cb_mask);
2712 struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
2713 struct si_texture *tex = (struct si_texture*)surf->texture;
2714
2715 if (tex->surface.fmask_size)
2716 tex->dirty_level_mask |= 1 << surf->u.tex.level;
2717 if (tex->dcc_gather_statistics)
2718 tex->separate_dcc_dirty = true;
2719 }
2720 }
2721
2722 static void si_dec_framebuffer_counters(const struct pipe_framebuffer_state *state)
2723 {
2724 for (int i = 0; i < state->nr_cbufs; ++i) {
2725 struct si_surface *surf = NULL;
2726 struct si_texture *tex;
2727
2728 if (!state->cbufs[i])
2729 continue;
2730 surf = (struct si_surface*)state->cbufs[i];
2731 tex = (struct si_texture*)surf->base.texture;
2732
2733 p_atomic_dec(&tex->framebuffers_bound);
2734 }
2735 }
2736
2737 static void si_set_framebuffer_state(struct pipe_context *ctx,
2738 const struct pipe_framebuffer_state *state)
2739 {
2740 struct si_context *sctx = (struct si_context *)ctx;
2741 struct si_surface *surf = NULL;
2742 struct si_texture *tex;
2743 bool old_any_dst_linear = sctx->framebuffer.any_dst_linear;
2744 unsigned old_nr_samples = sctx->framebuffer.nr_samples;
2745 unsigned old_colorbuf_enabled_4bit = sctx->framebuffer.colorbuf_enabled_4bit;
2746 bool old_has_zsbuf = !!sctx->framebuffer.state.zsbuf;
2747 bool old_has_stencil =
2748 old_has_zsbuf &&
2749 ((struct si_texture*)sctx->framebuffer.state.zsbuf->texture)->surface.has_stencil;
2750 bool unbound = false;
2751 int i;
2752
2753 /* Reject zero-sized framebuffers due to a hw bug on SI that occurs
2754 * when PA_SU_HARDWARE_SCREEN_OFFSET != 0 and any_scissor.BR_X/Y <= 0.
2755 * We could implement the full workaround here, but it's a useless case.
2756 */
2757 if ((!state->width || !state->height) && (state->nr_cbufs || state->zsbuf)) {
2758 unreachable("the framebuffer shouldn't have zero area");
2759 return;
2760 }
2761
2762 si_update_fb_dirtiness_after_rendering(sctx);
2763
2764 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
2765 if (!sctx->framebuffer.state.cbufs[i])
2766 continue;
2767
2768 tex = (struct si_texture*)sctx->framebuffer.state.cbufs[i]->texture;
2769 if (tex->dcc_gather_statistics)
2770 vi_separate_dcc_stop_query(sctx, tex);
2771 }
2772
2773 /* Disable DCC if the formats are incompatible. */
2774 for (i = 0; i < state->nr_cbufs; i++) {
2775 if (!state->cbufs[i])
2776 continue;
2777
2778 surf = (struct si_surface*)state->cbufs[i];
2779 tex = (struct si_texture*)surf->base.texture;
2780
2781 if (!surf->dcc_incompatible)
2782 continue;
2783
2784 /* Since the DCC decompression calls back into set_framebuffer-
2785 * _state, we need to unbind the framebuffer, so that
2786 * vi_separate_dcc_stop_query isn't called twice with the same
2787 * color buffer.
2788 */
2789 if (!unbound) {
2790 util_copy_framebuffer_state(&sctx->framebuffer.state, NULL);
2791 unbound = true;
2792 }
2793
2794 if (vi_dcc_enabled(tex, surf->base.u.tex.level))
2795 if (!si_texture_disable_dcc(sctx, tex))
2796 si_decompress_dcc(sctx, tex);
2797
2798 surf->dcc_incompatible = false;
2799 }
2800
2801 /* Only flush TC when changing the framebuffer state, because
2802 * the only client not using TC that can change textures is
2803 * the framebuffer.
2804 *
2805 * Wait for compute shaders because of possible transitions:
2806 * - FB write -> shader read
2807 * - shader write -> FB read
2808 *
2809 * DB caches are flushed on demand (using si_decompress_textures).
2810 *
2811 * When MSAA is enabled, CB and TC caches are flushed on demand
2812 * (after FMASK decompression). Shader write -> FB read transitions
2813 * cannot happen for MSAA textures, because MSAA shader images are
2814 * not supported.
2815 *
2816 * Only flush and wait for CB if there is actually a bound color buffer.
2817 */
2818 if (sctx->framebuffer.uncompressed_cb_mask) {
2819 si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
2820 sctx->framebuffer.CB_has_shader_readable_metadata,
2821 sctx->framebuffer.all_DCC_pipe_aligned);
2822 }
2823
2824 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
2825
2826 /* u_blitter doesn't invoke depth decompression when it does multiple
2827 * blits in a row, but the only case when it matters for DB is when
2828 * doing generate_mipmap. So here we flush DB manually between
2829 * individual generate_mipmap blits.
2830 * Note that lower mipmap levels aren't compressed.
2831 */
2832 if (sctx->generate_mipmap_for_depth) {
2833 si_make_DB_shader_coherent(sctx, 1, false,
2834 sctx->framebuffer.DB_has_shader_readable_metadata);
2835 } else if (sctx->chip_class == GFX9) {
2836 /* It appears that DB metadata "leaks" in a sequence of:
2837 * - depth clear
2838 * - DCC decompress for shader image writes (with DB disabled)
2839 * - render with DEPTH_BEFORE_SHADER=1
2840 * Flushing DB metadata works around the problem.
2841 */
2842 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_DB_META;
2843 }
2844
2845 /* Take the maximum of the old and new count. If the new count is lower,
2846 * dirtying is needed to disable the unbound colorbuffers.
2847 */
2848 sctx->framebuffer.dirty_cbufs |=
2849 (1 << MAX2(sctx->framebuffer.state.nr_cbufs, state->nr_cbufs)) - 1;
2850 sctx->framebuffer.dirty_zsbuf |= sctx->framebuffer.state.zsbuf != state->zsbuf;
2851
2852 si_dec_framebuffer_counters(&sctx->framebuffer.state);
2853 util_copy_framebuffer_state(&sctx->framebuffer.state, state);
2854
2855 sctx->framebuffer.colorbuf_enabled_4bit = 0;
2856 sctx->framebuffer.spi_shader_col_format = 0;
2857 sctx->framebuffer.spi_shader_col_format_alpha = 0;
2858 sctx->framebuffer.spi_shader_col_format_blend = 0;
2859 sctx->framebuffer.spi_shader_col_format_blend_alpha = 0;
2860 sctx->framebuffer.color_is_int8 = 0;
2861 sctx->framebuffer.color_is_int10 = 0;
2862
2863 sctx->framebuffer.compressed_cb_mask = 0;
2864 sctx->framebuffer.uncompressed_cb_mask = 0;
2865 sctx->framebuffer.nr_samples = util_framebuffer_get_num_samples(state);
2866 sctx->framebuffer.nr_color_samples = sctx->framebuffer.nr_samples;
2867 sctx->framebuffer.log_samples = util_logbase2(sctx->framebuffer.nr_samples);
2868 sctx->framebuffer.any_dst_linear = false;
2869 sctx->framebuffer.CB_has_shader_readable_metadata = false;
2870 sctx->framebuffer.DB_has_shader_readable_metadata = false;
2871 sctx->framebuffer.all_DCC_pipe_aligned = true;
2872 unsigned num_bpp64_colorbufs = 0;
2873
2874 for (i = 0; i < state->nr_cbufs; i++) {
2875 if (!state->cbufs[i])
2876 continue;
2877
2878 surf = (struct si_surface*)state->cbufs[i];
2879 tex = (struct si_texture*)surf->base.texture;
2880
2881 if (!surf->color_initialized) {
2882 si_initialize_color_surface(sctx, surf);
2883 }
2884
2885 sctx->framebuffer.colorbuf_enabled_4bit |= 0xf << (i * 4);
2886 sctx->framebuffer.spi_shader_col_format |=
2887 surf->spi_shader_col_format << (i * 4);
2888 sctx->framebuffer.spi_shader_col_format_alpha |=
2889 surf->spi_shader_col_format_alpha << (i * 4);
2890 sctx->framebuffer.spi_shader_col_format_blend |=
2891 surf->spi_shader_col_format_blend << (i * 4);
2892 sctx->framebuffer.spi_shader_col_format_blend_alpha |=
2893 surf->spi_shader_col_format_blend_alpha << (i * 4);
2894
2895 if (surf->color_is_int8)
2896 sctx->framebuffer.color_is_int8 |= 1 << i;
2897 if (surf->color_is_int10)
2898 sctx->framebuffer.color_is_int10 |= 1 << i;
2899
2900 if (tex->surface.fmask_size)
2901 sctx->framebuffer.compressed_cb_mask |= 1 << i;
2902 else
2903 sctx->framebuffer.uncompressed_cb_mask |= 1 << i;
2904
2905 /* Don't update nr_color_samples for non-AA buffers.
2906 * (e.g. destination of MSAA resolve)
2907 */
2908 if (tex->buffer.b.b.nr_samples >= 2 &&
2909 tex->buffer.b.b.nr_storage_samples < tex->buffer.b.b.nr_samples) {
2910 sctx->framebuffer.nr_color_samples =
2911 MIN2(sctx->framebuffer.nr_color_samples,
2912 tex->buffer.b.b.nr_storage_samples);
2913 sctx->framebuffer.nr_color_samples =
2914 MAX2(1, sctx->framebuffer.nr_color_samples);
2915 }
2916
2917 if (tex->surface.is_linear)
2918 sctx->framebuffer.any_dst_linear = true;
2919 if (tex->surface.bpe >= 8)
2920 num_bpp64_colorbufs++;
2921
2922 if (vi_dcc_enabled(tex, surf->base.u.tex.level)) {
2923 sctx->framebuffer.CB_has_shader_readable_metadata = true;
2924
2925 if (sctx->chip_class >= GFX9 &&
2926 !tex->surface.u.gfx9.dcc.pipe_aligned)
2927 sctx->framebuffer.all_DCC_pipe_aligned = false;
2928 }
2929
2930 si_context_add_resource_size(sctx, surf->base.texture);
2931
2932 p_atomic_inc(&tex->framebuffers_bound);
2933
2934 if (tex->dcc_gather_statistics) {
2935 /* Dirty tracking must be enabled for DCC usage analysis. */
2936 sctx->framebuffer.compressed_cb_mask |= 1 << i;
2937 vi_separate_dcc_start_query(sctx, tex);
2938 }
2939 }
2940
2941 /* For optimal DCC performance. */
2942 if (sctx->chip_class == VI)
2943 sctx->framebuffer.dcc_overwrite_combiner_watermark = 4;
2944 else if (num_bpp64_colorbufs >= 5)
2945 sctx->framebuffer.dcc_overwrite_combiner_watermark = 8;
2946 else
2947 sctx->framebuffer.dcc_overwrite_combiner_watermark = 6;
2948
2949 struct si_texture *zstex = NULL;
2950
2951 if (state->zsbuf) {
2952 surf = (struct si_surface*)state->zsbuf;
2953 zstex = (struct si_texture*)surf->base.texture;
2954
2955 if (!surf->depth_initialized) {
2956 si_init_depth_surface(sctx, surf);
2957 }
2958
2959 if (vi_tc_compat_htile_enabled(zstex, surf->base.u.tex.level))
2960 sctx->framebuffer.DB_has_shader_readable_metadata = true;
2961
2962 si_context_add_resource_size(sctx, surf->base.texture);
2963 }
2964
2965 si_update_ps_colorbuf0_slot(sctx);
2966 si_update_poly_offset_state(sctx);
2967 si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
2968 si_mark_atom_dirty(sctx, &sctx->atoms.s.framebuffer);
2969
2970 if (sctx->screen->dpbb_allowed)
2971 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
2972
2973 if (sctx->framebuffer.any_dst_linear != old_any_dst_linear)
2974 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
2975
2976 if (sctx->screen->has_out_of_order_rast &&
2977 (sctx->framebuffer.colorbuf_enabled_4bit != old_colorbuf_enabled_4bit ||
2978 !!sctx->framebuffer.state.zsbuf != old_has_zsbuf ||
2979 (zstex && zstex->surface.has_stencil != old_has_stencil)))
2980 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
2981
2982 if (sctx->framebuffer.nr_samples != old_nr_samples) {
2983 struct pipe_constant_buffer constbuf = {0};
2984
2985 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
2986 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
2987
2988 constbuf.buffer = sctx->sample_pos_buffer;
2989
2990 /* Set sample locations as fragment shader constants. */
2991 switch (sctx->framebuffer.nr_samples) {
2992 case 1:
2993 constbuf.buffer_offset = 0;
2994 break;
2995 case 2:
2996 constbuf.buffer_offset = (ubyte*)sctx->sample_positions.x2 -
2997 (ubyte*)sctx->sample_positions.x1;
2998 break;
2999 case 4:
3000 constbuf.buffer_offset = (ubyte*)sctx->sample_positions.x4 -
3001 (ubyte*)sctx->sample_positions.x1;
3002 break;
3003 case 8:
3004 constbuf.buffer_offset = (ubyte*)sctx->sample_positions.x8 -
3005 (ubyte*)sctx->sample_positions.x1;
3006 break;
3007 case 16:
3008 constbuf.buffer_offset = (ubyte*)sctx->sample_positions.x16 -
3009 (ubyte*)sctx->sample_positions.x1;
3010 break;
3011 default:
3012 PRINT_ERR("Requested an invalid number of samples %i.\n",
3013 sctx->framebuffer.nr_samples);
3014 assert(0);
3015 }
3016 constbuf.buffer_size = sctx->framebuffer.nr_samples * 2 * 4;
3017 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &constbuf);
3018
3019 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
3020 }
3021
3022 sctx->do_update_shaders = true;
3023
3024 if (!sctx->decompression_enabled) {
3025 /* Prevent textures decompression when the framebuffer state
3026 * changes come from the decompression passes themselves.
3027 */
3028 sctx->need_check_render_feedback = true;
3029 }
3030 }
3031
3032 static void si_emit_framebuffer_state(struct si_context *sctx)
3033 {
3034 struct radeon_cmdbuf *cs = sctx->gfx_cs;
3035 struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
3036 unsigned i, nr_cbufs = state->nr_cbufs;
3037 struct si_texture *tex = NULL;
3038 struct si_surface *cb = NULL;
3039 unsigned cb_color_info = 0;
3040
3041 /* Colorbuffers. */
3042 for (i = 0; i < nr_cbufs; i++) {
3043 uint64_t cb_color_base, cb_color_fmask, cb_color_cmask, cb_dcc_base;
3044 unsigned cb_color_attrib;
3045
3046 if (!(sctx->framebuffer.dirty_cbufs & (1 << i)))
3047 continue;
3048
3049 cb = (struct si_surface*)state->cbufs[i];
3050 if (!cb) {
3051 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C,
3052 S_028C70_FORMAT(V_028C70_COLOR_INVALID));
3053 continue;
3054 }
3055
3056 tex = (struct si_texture *)cb->base.texture;
3057 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
3058 &tex->buffer, RADEON_USAGE_READWRITE,
3059 tex->buffer.b.b.nr_samples > 1 ?
3060 RADEON_PRIO_COLOR_BUFFER_MSAA :
3061 RADEON_PRIO_COLOR_BUFFER);
3062
3063 if (tex->cmask_buffer && tex->cmask_buffer != &tex->buffer) {
3064 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
3065 tex->cmask_buffer, RADEON_USAGE_READWRITE,
3066 RADEON_PRIO_SEPARATE_META);
3067 }
3068
3069 if (tex->dcc_separate_buffer)
3070 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
3071 tex->dcc_separate_buffer,
3072 RADEON_USAGE_READWRITE,
3073 RADEON_PRIO_SEPARATE_META);
3074
3075 /* Compute mutable surface parameters. */
3076 cb_color_base = tex->buffer.gpu_address >> 8;
3077 cb_color_fmask = 0;
3078 cb_color_cmask = tex->cmask_base_address_reg;
3079 cb_dcc_base = 0;
3080 cb_color_info = cb->cb_color_info | tex->cb_color_info;
3081 cb_color_attrib = cb->cb_color_attrib;
3082
3083 if (cb->base.u.tex.level > 0)
3084 cb_color_info &= C_028C70_FAST_CLEAR;
3085
3086 if (tex->surface.fmask_size) {
3087 cb_color_fmask = (tex->buffer.gpu_address + tex->fmask_offset) >> 8;
3088 cb_color_fmask |= tex->surface.fmask_tile_swizzle;
3089 }
3090
3091 /* Set up DCC. */
3092 if (vi_dcc_enabled(tex, cb->base.u.tex.level)) {
3093 bool is_msaa_resolve_dst = state->cbufs[0] &&
3094 state->cbufs[0]->texture->nr_samples > 1 &&
3095 state->cbufs[1] == &cb->base &&
3096 state->cbufs[1]->texture->nr_samples <= 1;
3097
3098 if (!is_msaa_resolve_dst)
3099 cb_color_info |= S_028C70_DCC_ENABLE(1);
3100
3101 cb_dcc_base = ((!tex->dcc_separate_buffer ? tex->buffer.gpu_address : 0) +
3102 tex->dcc_offset) >> 8;
3103 cb_dcc_base |= tex->surface.tile_swizzle;
3104 }
3105
3106 if (sctx->chip_class >= GFX9) {
3107 struct gfx9_surf_meta_flags meta;
3108
3109 if (tex->dcc_offset)
3110 meta = tex->surface.u.gfx9.dcc;
3111 else
3112 meta = tex->surface.u.gfx9.cmask;
3113
3114 /* Set mutable surface parameters. */
3115 cb_color_base += tex->surface.u.gfx9.surf_offset >> 8;
3116 cb_color_base |= tex->surface.tile_swizzle;
3117 if (!tex->surface.fmask_size)
3118 cb_color_fmask = cb_color_base;
3119 if (cb->base.u.tex.level > 0)
3120 cb_color_cmask = cb_color_base;
3121 cb_color_attrib |= S_028C74_COLOR_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
3122 S_028C74_FMASK_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode) |
3123 S_028C74_RB_ALIGNED(meta.rb_aligned) |
3124 S_028C74_PIPE_ALIGNED(meta.pipe_aligned);
3125
3126 radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C, 15);
3127 radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
3128 radeon_emit(cs, S_028C64_BASE_256B(cb_color_base >> 32)); /* CB_COLOR0_BASE_EXT */
3129 radeon_emit(cs, cb->cb_color_attrib2); /* CB_COLOR0_ATTRIB2 */
3130 radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
3131 radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
3132 radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
3133 radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
3134 radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
3135 radeon_emit(cs, S_028C80_BASE_256B(cb_color_cmask >> 32)); /* CB_COLOR0_CMASK_BASE_EXT */
3136 radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
3137 radeon_emit(cs, S_028C88_BASE_256B(cb_color_fmask >> 32)); /* CB_COLOR0_FMASK_BASE_EXT */
3138 radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
3139 radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
3140 radeon_emit(cs, cb_dcc_base); /* CB_COLOR0_DCC_BASE */
3141 radeon_emit(cs, S_028C98_BASE_256B(cb_dcc_base >> 32)); /* CB_COLOR0_DCC_BASE_EXT */
3142
3143 radeon_set_context_reg(cs, R_0287A0_CB_MRT0_EPITCH + i * 4,
3144 S_0287A0_EPITCH(tex->surface.u.gfx9.surf.epitch));
3145 } else {
3146 /* Compute mutable surface parameters (SI-CI-VI). */
3147 const struct legacy_surf_level *level_info =
3148 &tex->surface.u.legacy.level[cb->base.u.tex.level];
3149 unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
3150 unsigned cb_color_pitch, cb_color_slice, cb_color_fmask_slice;
3151
3152 cb_color_base += level_info->offset >> 8;
3153 /* Only macrotiled modes can set tile swizzle. */
3154 if (level_info->mode == RADEON_SURF_MODE_2D)
3155 cb_color_base |= tex->surface.tile_swizzle;
3156
3157 if (!tex->surface.fmask_size)
3158 cb_color_fmask = cb_color_base;
3159 if (cb->base.u.tex.level > 0)
3160 cb_color_cmask = cb_color_base;
3161 if (cb_dcc_base)
3162 cb_dcc_base += level_info->dcc_offset >> 8;
3163
3164 pitch_tile_max = level_info->nblk_x / 8 - 1;
3165 slice_tile_max = level_info->nblk_x *
3166 level_info->nblk_y / 64 - 1;
3167 tile_mode_index = si_tile_mode_index(tex, cb->base.u.tex.level, false);
3168
3169 cb_color_attrib |= S_028C74_TILE_MODE_INDEX(tile_mode_index);
3170 cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
3171 cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
3172
3173 if (tex->surface.fmask_size) {
3174 if (sctx->chip_class >= CIK)
3175 cb_color_pitch |= S_028C64_FMASK_TILE_MAX(tex->surface.u.legacy.fmask.pitch_in_pixels / 8 - 1);
3176 cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tex->surface.u.legacy.fmask.tiling_index);
3177 cb_color_fmask_slice = S_028C88_TILE_MAX(tex->surface.u.legacy.fmask.slice_tile_max);
3178 } else {
3179 /* This must be set for fast clear to work without FMASK. */
3180 if (sctx->chip_class >= CIK)
3181 cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
3182 cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
3183 cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
3184 }
3185
3186 radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C,
3187 sctx->chip_class >= VI ? 14 : 13);
3188 radeon_emit(cs, cb_color_base); /* CB_COLOR0_BASE */
3189 radeon_emit(cs, cb_color_pitch); /* CB_COLOR0_PITCH */
3190 radeon_emit(cs, cb_color_slice); /* CB_COLOR0_SLICE */
3191 radeon_emit(cs, cb->cb_color_view); /* CB_COLOR0_VIEW */
3192 radeon_emit(cs, cb_color_info); /* CB_COLOR0_INFO */
3193 radeon_emit(cs, cb_color_attrib); /* CB_COLOR0_ATTRIB */
3194 radeon_emit(cs, cb->cb_dcc_control); /* CB_COLOR0_DCC_CONTROL */
3195 radeon_emit(cs, cb_color_cmask); /* CB_COLOR0_CMASK */
3196 radeon_emit(cs, tex->surface.u.legacy.cmask_slice_tile_max); /* CB_COLOR0_CMASK_SLICE */
3197 radeon_emit(cs, cb_color_fmask); /* CB_COLOR0_FMASK */
3198 radeon_emit(cs, cb_color_fmask_slice); /* CB_COLOR0_FMASK_SLICE */
3199 radeon_emit(cs, tex->color_clear_value[0]); /* CB_COLOR0_CLEAR_WORD0 */
3200 radeon_emit(cs, tex->color_clear_value[1]); /* CB_COLOR0_CLEAR_WORD1 */
3201
3202 if (sctx->chip_class >= VI) /* R_028C94_CB_COLOR0_DCC_BASE */
3203 radeon_emit(cs, cb_dcc_base);
3204 }
3205 }
3206 for (; i < 8 ; i++)
3207 if (sctx->framebuffer.dirty_cbufs & (1 << i))
3208 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C, 0);
3209
3210 /* ZS buffer. */
3211 if (state->zsbuf && sctx->framebuffer.dirty_zsbuf) {
3212 struct si_surface *zb = (struct si_surface*)state->zsbuf;
3213 struct si_texture *tex = (struct si_texture*)zb->base.texture;
3214
3215 radeon_add_to_buffer_list(sctx, sctx->gfx_cs,
3216 &tex->buffer, RADEON_USAGE_READWRITE,
3217 zb->base.texture->nr_samples > 1 ?
3218 RADEON_PRIO_DEPTH_BUFFER_MSAA :
3219 RADEON_PRIO_DEPTH_BUFFER);
3220
3221 if (sctx->chip_class >= GFX9) {
3222 radeon_set_context_reg_seq(cs, R_028014_DB_HTILE_DATA_BASE, 3);
3223 radeon_emit(cs, zb->db_htile_data_base); /* DB_HTILE_DATA_BASE */
3224 radeon_emit(cs, S_028018_BASE_HI(zb->db_htile_data_base >> 32)); /* DB_HTILE_DATA_BASE_HI */
3225 radeon_emit(cs, zb->db_depth_size); /* DB_DEPTH_SIZE */
3226
3227 radeon_set_context_reg_seq(cs, R_028038_DB_Z_INFO, 10);
3228 radeon_emit(cs, zb->db_z_info | /* DB_Z_INFO */
3229 S_028038_ZRANGE_PRECISION(tex->depth_clear_value != 0));
3230 radeon_emit(cs, zb->db_stencil_info); /* DB_STENCIL_INFO */
3231 radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
3232 radeon_emit(cs, S_028044_BASE_HI(zb->db_depth_base >> 32)); /* DB_Z_READ_BASE_HI */
3233 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
3234 radeon_emit(cs, S_02804C_BASE_HI(zb->db_stencil_base >> 32)); /* DB_STENCIL_READ_BASE_HI */
3235 radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
3236 radeon_emit(cs, S_028054_BASE_HI(zb->db_depth_base >> 32)); /* DB_Z_WRITE_BASE_HI */
3237 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
3238 radeon_emit(cs, S_02805C_BASE_HI(zb->db_stencil_base >> 32)); /* DB_STENCIL_WRITE_BASE_HI */
3239
3240 radeon_set_context_reg_seq(cs, R_028068_DB_Z_INFO2, 2);
3241 radeon_emit(cs, zb->db_z_info2); /* DB_Z_INFO2 */
3242 radeon_emit(cs, zb->db_stencil_info2); /* DB_STENCIL_INFO2 */
3243 } else {
3244 radeon_set_context_reg(cs, R_028014_DB_HTILE_DATA_BASE, zb->db_htile_data_base);
3245
3246 radeon_set_context_reg_seq(cs, R_02803C_DB_DEPTH_INFO, 9);
3247 radeon_emit(cs, zb->db_depth_info); /* DB_DEPTH_INFO */
3248 radeon_emit(cs, zb->db_z_info | /* DB_Z_INFO */
3249 S_028040_ZRANGE_PRECISION(tex->depth_clear_value != 0));
3250 radeon_emit(cs, zb->db_stencil_info); /* DB_STENCIL_INFO */
3251 radeon_emit(cs, zb->db_depth_base); /* DB_Z_READ_BASE */
3252 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_READ_BASE */
3253 radeon_emit(cs, zb->db_depth_base); /* DB_Z_WRITE_BASE */
3254 radeon_emit(cs, zb->db_stencil_base); /* DB_STENCIL_WRITE_BASE */
3255 radeon_emit(cs, zb->db_depth_size); /* DB_DEPTH_SIZE */
3256 radeon_emit(cs, zb->db_depth_slice); /* DB_DEPTH_SLICE */
3257 }
3258
3259 radeon_set_context_reg_seq(cs, R_028028_DB_STENCIL_CLEAR, 2);
3260 radeon_emit(cs, tex->stencil_clear_value); /* R_028028_DB_STENCIL_CLEAR */
3261 radeon_emit(cs, fui(tex->depth_clear_value)); /* R_02802C_DB_DEPTH_CLEAR */
3262
3263 radeon_set_context_reg(cs, R_028008_DB_DEPTH_VIEW, zb->db_depth_view);
3264 radeon_set_context_reg(cs, R_028ABC_DB_HTILE_SURFACE, zb->db_htile_surface);
3265 } else if (sctx->framebuffer.dirty_zsbuf) {
3266 if (sctx->chip_class >= GFX9)
3267 radeon_set_context_reg_seq(cs, R_028038_DB_Z_INFO, 2);
3268 else
3269 radeon_set_context_reg_seq(cs, R_028040_DB_Z_INFO, 2);
3270
3271 radeon_emit(cs, S_028040_FORMAT(V_028040_Z_INVALID)); /* DB_Z_INFO */
3272 radeon_emit(cs, S_028044_FORMAT(V_028044_STENCIL_INVALID)); /* DB_STENCIL_INFO */
3273 }
3274
3275 /* Framebuffer dimensions. */
3276 /* PA_SC_WINDOW_SCISSOR_TL is set in si_init_config() */
3277 radeon_set_context_reg(cs, R_028208_PA_SC_WINDOW_SCISSOR_BR,
3278 S_028208_BR_X(state->width) | S_028208_BR_Y(state->height));
3279
3280 if (sctx->screen->dfsm_allowed) {
3281 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3282 radeon_emit(cs, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0));
3283 }
3284
3285 sctx->framebuffer.dirty_cbufs = 0;
3286 sctx->framebuffer.dirty_zsbuf = false;
3287 }
3288
3289 static void si_emit_msaa_sample_locs(struct si_context *sctx)
3290 {
3291 struct radeon_cmdbuf *cs = sctx->gfx_cs;
3292 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
3293 unsigned nr_samples = sctx->framebuffer.nr_samples;
3294 bool has_msaa_sample_loc_bug = sctx->screen->has_msaa_sample_loc_bug;
3295
3296 /* Smoothing (only possible with nr_samples == 1) uses the same
3297 * sample locations as the MSAA it simulates.
3298 */
3299 if (nr_samples <= 1 && sctx->smoothing_enabled)
3300 nr_samples = SI_NUM_SMOOTH_AA_SAMPLES;
3301
3302 /* On Polaris, the small primitive filter uses the sample locations
3303 * even when MSAA is off, so we need to make sure they're set to 0.
3304 */
3305 if ((nr_samples >= 2 || has_msaa_sample_loc_bug) &&
3306 nr_samples != sctx->sample_locs_num_samples) {
3307 sctx->sample_locs_num_samples = nr_samples;
3308 si_emit_sample_locations(cs, nr_samples);
3309 }
3310
3311 if (sctx->family >= CHIP_POLARIS10) {
3312 unsigned small_prim_filter_cntl =
3313 S_028830_SMALL_PRIM_FILTER_ENABLE(1) |
3314 /* line bug */
3315 S_028830_LINE_FILTER_DISABLE(sctx->family <= CHIP_POLARIS12);
3316
3317 /* The alternative of setting sample locations to 0 would
3318 * require a DB flush to avoid Z errors, see
3319 * https://bugs.freedesktop.org/show_bug.cgi?id=96908
3320 */
3321 if (has_msaa_sample_loc_bug &&
3322 sctx->framebuffer.nr_samples > 1 &&
3323 !rs->multisample_enable)
3324 small_prim_filter_cntl &= C_028830_SMALL_PRIM_FILTER_ENABLE;
3325
3326 radeon_opt_set_context_reg(sctx,
3327 R_028830_PA_SU_SMALL_PRIM_FILTER_CNTL,
3328 SI_TRACKED_PA_SU_SMALL_PRIM_FILTER_CNTL,
3329 small_prim_filter_cntl);
3330 }
3331
3332 /* The exclusion bits can be set to improve rasterization efficiency
3333 * if no sample lies on the pixel boundary (-8 sample offset).
3334 */
3335 bool exclusion = sctx->chip_class >= CIK &&
3336 (!rs->multisample_enable || nr_samples != 16);
3337 radeon_opt_set_context_reg(sctx, R_02882C_PA_SU_PRIM_FILTER_CNTL,
3338 SI_TRACKED_PA_SU_PRIM_FILTER_CNTL,
3339 S_02882C_XMAX_RIGHT_EXCLUSION(exclusion) |
3340 S_02882C_YMAX_BOTTOM_EXCLUSION(exclusion));
3341 }
3342
3343 static bool si_out_of_order_rasterization(struct si_context *sctx)
3344 {
3345 struct si_state_blend *blend = sctx->queued.named.blend;
3346 struct si_state_dsa *dsa = sctx->queued.named.dsa;
3347
3348 if (!sctx->screen->has_out_of_order_rast)
3349 return false;
3350
3351 unsigned colormask = sctx->framebuffer.colorbuf_enabled_4bit;
3352
3353 if (blend) {
3354 colormask &= blend->cb_target_enabled_4bit;
3355 } else {
3356 colormask = 0;
3357 }
3358
3359 /* Conservative: No logic op. */
3360 if (colormask && blend->logicop_enable)
3361 return false;
3362
3363 struct si_dsa_order_invariance dsa_order_invariant = {
3364 .zs = true, .pass_set = true, .pass_last = false
3365 };
3366
3367 if (sctx->framebuffer.state.zsbuf) {
3368 struct si_texture *zstex =
3369 (struct si_texture*)sctx->framebuffer.state.zsbuf->texture;
3370 bool has_stencil = zstex->surface.has_stencil;
3371 dsa_order_invariant = dsa->order_invariance[has_stencil];
3372 if (!dsa_order_invariant.zs)
3373 return false;
3374
3375 /* The set of PS invocations is always order invariant,
3376 * except when early Z/S tests are requested. */
3377 if (sctx->ps_shader.cso &&
3378 sctx->ps_shader.cso->info.writes_memory &&
3379 sctx->ps_shader.cso->info.properties[TGSI_PROPERTY_FS_EARLY_DEPTH_STENCIL] &&
3380 !dsa_order_invariant.pass_set)
3381 return false;
3382
3383 if (sctx->num_perfect_occlusion_queries != 0 &&
3384 !dsa_order_invariant.pass_set)
3385 return false;
3386 }
3387
3388 if (!colormask)
3389 return true;
3390
3391 unsigned blendmask = colormask & blend->blend_enable_4bit;
3392
3393 if (blendmask) {
3394 /* Only commutative blending. */
3395 if (blendmask & ~blend->commutative_4bit)
3396 return false;
3397
3398 if (!dsa_order_invariant.pass_set)
3399 return false;
3400 }
3401
3402 if (colormask & ~blendmask) {
3403 if (!dsa_order_invariant.pass_last)
3404 return false;
3405 }
3406
3407 return true;
3408 }
3409
3410 static void si_emit_msaa_config(struct si_context *sctx)
3411 {
3412 struct radeon_cmdbuf *cs = sctx->gfx_cs;
3413 unsigned num_tile_pipes = sctx->screen->info.num_tile_pipes;
3414 /* 33% faster rendering to linear color buffers */
3415 bool dst_is_linear = sctx->framebuffer.any_dst_linear;
3416 bool out_of_order_rast = si_out_of_order_rasterization(sctx);
3417 unsigned sc_mode_cntl_1 =
3418 S_028A4C_WALK_SIZE(dst_is_linear) |
3419 S_028A4C_WALK_FENCE_ENABLE(!dst_is_linear) |
3420 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
3421 S_028A4C_OUT_OF_ORDER_PRIMITIVE_ENABLE(out_of_order_rast) |
3422 S_028A4C_OUT_OF_ORDER_WATER_MARK(0x7) |
3423 /* always 1: */
3424 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
3425 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
3426 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
3427 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
3428 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
3429 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
3430 unsigned db_eqaa = S_028804_HIGH_QUALITY_INTERSECTIONS(1) |
3431 S_028804_INCOHERENT_EQAA_READS(1) |
3432 S_028804_INTERPOLATE_COMP_Z(1) |
3433 S_028804_STATIC_ANCHOR_ASSOCIATIONS(1);
3434 unsigned coverage_samples, color_samples, z_samples;
3435
3436 /* S: Coverage samples (up to 16x):
3437 * - Scan conversion samples (PA_SC_AA_CONFIG.MSAA_NUM_SAMPLES)
3438 * - CB FMASK samples (CB_COLORi_ATTRIB.NUM_SAMPLES)
3439 *
3440 * Z: Z/S samples (up to 8x, must be <= coverage samples and >= color samples):
3441 * - Value seen by DB (DB_Z_INFO.NUM_SAMPLES)
3442 * - Value seen by CB, must be correct even if Z/S is unbound (DB_EQAA.MAX_ANCHOR_SAMPLES)
3443 * # Missing samples are derived from Z planes if Z is compressed (up to 16x quality), or
3444 * # from the closest defined sample if Z is uncompressed (same quality as the number of
3445 * # Z samples).
3446 *
3447 * F: Color samples (up to 8x, must be <= coverage samples):
3448 * - CB color samples (CB_COLORi_ATTRIB.NUM_FRAGMENTS)
3449 * - PS iter samples (DB_EQAA.PS_ITER_SAMPLES)
3450 *
3451 * Can be anything between coverage and color samples:
3452 * - SampleMaskIn samples (PA_SC_AA_CONFIG.MSAA_EXPOSED_SAMPLES)
3453 * - SampleMaskOut samples (DB_EQAA.MASK_EXPORT_NUM_SAMPLES)
3454 * - Alpha-to-coverage samples (DB_EQAA.ALPHA_TO_MASK_NUM_SAMPLES)
3455 * - Occlusion query samples (DB_COUNT_CONTROL.SAMPLE_RATE)
3456 * # All are currently set the same as coverage samples.
3457 *
3458 * If color samples < coverage samples, FMASK has a higher bpp to store an "unknown"
3459 * flag for undefined color samples. A shader-based resolve must handle unknowns
3460 * or mask them out with AND. Unknowns can also be guessed from neighbors via
3461 * an edge-detect shader-based resolve, which is required to make "color samples = 1"
3462 * useful. The CB resolve always drops unknowns.
3463 *
3464 * Sensible AA configurations:
3465 * EQAA 16s 8z 8f - might look the same as 16x MSAA if Z is compressed
3466 * EQAA 16s 8z 4f - might look the same as 16x MSAA if Z is compressed
3467 * EQAA 16s 4z 4f - might look the same as 16x MSAA if Z is compressed
3468 * EQAA 8s 8z 8f = 8x MSAA
3469 * EQAA 8s 8z 4f - might look the same as 8x MSAA
3470 * EQAA 8s 8z 2f - might look the same as 8x MSAA with low-density geometry
3471 * EQAA 8s 4z 4f - might look the same as 8x MSAA if Z is compressed
3472 * EQAA 8s 4z 2f - might look the same as 8x MSAA with low-density geometry if Z is compressed
3473 * EQAA 4s 4z 4f = 4x MSAA
3474 * EQAA 4s 4z 2f - might look the same as 4x MSAA with low-density geometry
3475 * EQAA 2s 2z 2f = 2x MSAA
3476 */
3477 if (sctx->framebuffer.nr_samples > 1) {
3478 coverage_samples = sctx->framebuffer.nr_samples;
3479 color_samples = sctx->framebuffer.nr_color_samples;
3480
3481 if (sctx->framebuffer.state.zsbuf) {
3482 z_samples = sctx->framebuffer.state.zsbuf->texture->nr_samples;
3483 z_samples = MAX2(1, z_samples);
3484 } else {
3485 z_samples = coverage_samples;
3486 }
3487 } else if (sctx->smoothing_enabled) {
3488 coverage_samples = color_samples = z_samples = SI_NUM_SMOOTH_AA_SAMPLES;
3489 } else {
3490 coverage_samples = color_samples = z_samples = 1;
3491 }
3492
3493 /* Required by OpenGL line rasterization.
3494 *
3495 * TODO: We should also enable perpendicular endcaps for AA lines,
3496 * but that requires implementing line stippling in the pixel
3497 * shader. SC can only do line stippling with axis-aligned
3498 * endcaps.
3499 */
3500 unsigned sc_line_cntl = S_028BDC_DX10_DIAMOND_TEST_ENA(1);
3501 unsigned sc_aa_config = 0;
3502
3503 if (coverage_samples > 1) {
3504 /* distance from the pixel center, indexed by log2(nr_samples) */
3505 static unsigned max_dist[] = {
3506 0, /* unused */
3507 4, /* 2x MSAA */
3508 6, /* 4x MSAA */
3509 7, /* 8x MSAA */
3510 8, /* 16x MSAA */
3511 };
3512 unsigned log_samples = util_logbase2(coverage_samples);
3513 unsigned log_z_samples = util_logbase2(z_samples);
3514 unsigned ps_iter_samples = si_get_ps_iter_samples(sctx);
3515 unsigned log_ps_iter_samples = util_logbase2(ps_iter_samples);
3516
3517 sc_line_cntl |= S_028BDC_EXPAND_LINE_WIDTH(1);
3518 sc_aa_config = S_028BE0_MSAA_NUM_SAMPLES(log_samples) |
3519 S_028BE0_MAX_SAMPLE_DIST(max_dist[log_samples]) |
3520 S_028BE0_MSAA_EXPOSED_SAMPLES(log_samples);
3521
3522 if (sctx->framebuffer.nr_samples > 1) {
3523 db_eqaa |= S_028804_MAX_ANCHOR_SAMPLES(log_z_samples) |
3524 S_028804_PS_ITER_SAMPLES(log_ps_iter_samples) |
3525 S_028804_MASK_EXPORT_NUM_SAMPLES(log_samples) |
3526 S_028804_ALPHA_TO_MASK_NUM_SAMPLES(log_samples);
3527 sc_mode_cntl_1 |= S_028A4C_PS_ITER_SAMPLE(ps_iter_samples > 1);
3528 } else if (sctx->smoothing_enabled) {
3529 db_eqaa |= S_028804_OVERRASTERIZATION_AMOUNT(log_samples);
3530 }
3531 }
3532
3533 unsigned initial_cdw = cs->current.cdw;
3534
3535 /* R_028BDC_PA_SC_LINE_CNTL, R_028BE0_PA_SC_AA_CONFIG */
3536 radeon_opt_set_context_reg2(sctx, R_028BDC_PA_SC_LINE_CNTL,
3537 SI_TRACKED_PA_SC_LINE_CNTL, sc_line_cntl,
3538 sc_aa_config);
3539 /* R_028804_DB_EQAA */
3540 radeon_opt_set_context_reg(sctx, R_028804_DB_EQAA, SI_TRACKED_DB_EQAA,
3541 db_eqaa);
3542 /* R_028A4C_PA_SC_MODE_CNTL_1 */
3543 radeon_opt_set_context_reg(sctx, R_028A4C_PA_SC_MODE_CNTL_1,
3544 SI_TRACKED_PA_SC_MODE_CNTL_1, sc_mode_cntl_1);
3545
3546 if (initial_cdw != cs->current.cdw) {
3547 sctx->context_roll = true;
3548
3549 /* GFX9: Flush DFSM when the AA mode changes. */
3550 if (sctx->screen->dfsm_allowed) {
3551 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
3552 radeon_emit(cs, EVENT_TYPE(V_028A90_FLUSH_DFSM) | EVENT_INDEX(0));
3553 }
3554 }
3555 }
3556
3557 void si_update_ps_iter_samples(struct si_context *sctx)
3558 {
3559 if (sctx->framebuffer.nr_samples > 1)
3560 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
3561 if (sctx->screen->dpbb_allowed)
3562 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
3563 }
3564
3565 static void si_set_min_samples(struct pipe_context *ctx, unsigned min_samples)
3566 {
3567 struct si_context *sctx = (struct si_context *)ctx;
3568
3569 /* The hardware can only do sample shading with 2^n samples. */
3570 min_samples = util_next_power_of_two(min_samples);
3571
3572 if (sctx->ps_iter_samples == min_samples)
3573 return;
3574
3575 sctx->ps_iter_samples = min_samples;
3576 sctx->do_update_shaders = true;
3577
3578 si_update_ps_iter_samples(sctx);
3579 }
3580
3581 /*
3582 * Samplers
3583 */
3584
3585 /**
3586 * Build the sampler view descriptor for a buffer texture.
3587 * @param state 256-bit descriptor; only the high 128 bits are filled in
3588 */
3589 void
3590 si_make_buffer_descriptor(struct si_screen *screen, struct si_resource *buf,
3591 enum pipe_format format,
3592 unsigned offset, unsigned size,
3593 uint32_t *state)
3594 {
3595 const struct util_format_description *desc;
3596 int first_non_void;
3597 unsigned stride;
3598 unsigned num_records;
3599 unsigned num_format, data_format;
3600
3601 desc = util_format_description(format);
3602 first_non_void = util_format_get_first_non_void_channel(format);
3603 stride = desc->block.bits / 8;
3604 num_format = si_translate_buffer_numformat(&screen->b, desc, first_non_void);
3605 data_format = si_translate_buffer_dataformat(&screen->b, desc, first_non_void);
3606
3607 num_records = size / stride;
3608 num_records = MIN2(num_records, (buf->b.b.width0 - offset) / stride);
3609
3610 /* The NUM_RECORDS field has a different meaning depending on the chip,
3611 * instruction type, STRIDE, and SWIZZLE_ENABLE.
3612 *
3613 * SI-CIK:
3614 * - If STRIDE == 0, it's in byte units.
3615 * - If STRIDE != 0, it's in units of STRIDE, used with inst.IDXEN.
3616 *
3617 * VI:
3618 * - For SMEM and STRIDE == 0, it's in byte units.
3619 * - For SMEM and STRIDE != 0, it's in units of STRIDE.
3620 * - For VMEM and STRIDE == 0 or SWIZZLE_ENABLE == 0, it's in byte units.
3621 * - For VMEM and STRIDE != 0 and SWIZZLE_ENABLE == 1, it's in units of STRIDE.
3622 * NOTE: There is incompatibility between VMEM and SMEM opcodes due to SWIZZLE_-
3623 * ENABLE. The workaround is to set STRIDE = 0 if SWIZZLE_ENABLE == 0 when
3624 * using SMEM. This can be done in the shader by clearing STRIDE with s_and.
3625 * That way the same descriptor can be used by both SMEM and VMEM.
3626 *
3627 * GFX9:
3628 * - For SMEM and STRIDE == 0, it's in byte units.
3629 * - For SMEM and STRIDE != 0, it's in units of STRIDE.
3630 * - For VMEM and inst.IDXEN == 0 or STRIDE == 0, it's in byte units.
3631 * - For VMEM and inst.IDXEN == 1 and STRIDE != 0, it's in units of STRIDE.
3632 */
3633 if (screen->info.chip_class >= GFX9 && HAVE_LLVM < 0x0800)
3634 /* When vindex == 0, LLVM < 8.0 sets IDXEN = 0, thus changing units
3635 * from STRIDE to bytes. This works around it by setting
3636 * NUM_RECORDS to at least the size of one element, so that
3637 * the first element is readable when IDXEN == 0.
3638 */
3639 num_records = num_records ? MAX2(num_records, stride) : 0;
3640 else if (screen->info.chip_class == VI)
3641 num_records *= stride;
3642
3643 state[4] = 0;
3644 state[5] = S_008F04_STRIDE(stride);
3645 state[6] = num_records;
3646 state[7] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
3647 S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
3648 S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
3649 S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3])) |
3650 S_008F0C_NUM_FORMAT(num_format) |
3651 S_008F0C_DATA_FORMAT(data_format);
3652 }
3653
3654 static unsigned gfx9_border_color_swizzle(const unsigned char swizzle[4])
3655 {
3656 unsigned bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
3657
3658 if (swizzle[3] == PIPE_SWIZZLE_X) {
3659 /* For the pre-defined border color values (white, opaque
3660 * black, transparent black), the only thing that matters is
3661 * that the alpha channel winds up in the correct place
3662 * (because the RGB channels are all the same) so either of
3663 * these enumerations will work.
3664 */
3665 if (swizzle[2] == PIPE_SWIZZLE_Y)
3666 bc_swizzle = V_008F20_BC_SWIZZLE_WZYX;
3667 else
3668 bc_swizzle = V_008F20_BC_SWIZZLE_WXYZ;
3669 } else if (swizzle[0] == PIPE_SWIZZLE_X) {
3670 if (swizzle[1] == PIPE_SWIZZLE_Y)
3671 bc_swizzle = V_008F20_BC_SWIZZLE_XYZW;
3672 else
3673 bc_swizzle = V_008F20_BC_SWIZZLE_XWYZ;
3674 } else if (swizzle[1] == PIPE_SWIZZLE_X) {
3675 bc_swizzle = V_008F20_BC_SWIZZLE_YXWZ;
3676 } else if (swizzle[2] == PIPE_SWIZZLE_X) {
3677 bc_swizzle = V_008F20_BC_SWIZZLE_ZYXW;
3678 }
3679
3680 return bc_swizzle;
3681 }
3682
3683 /**
3684 * Build the sampler view descriptor for a texture.
3685 */
3686 void
3687 si_make_texture_descriptor(struct si_screen *screen,
3688 struct si_texture *tex,
3689 bool sampler,
3690 enum pipe_texture_target target,
3691 enum pipe_format pipe_format,
3692 const unsigned char state_swizzle[4],
3693 unsigned first_level, unsigned last_level,
3694 unsigned first_layer, unsigned last_layer,
3695 unsigned width, unsigned height, unsigned depth,
3696 uint32_t *state,
3697 uint32_t *fmask_state)
3698 {
3699 struct pipe_resource *res = &tex->buffer.b.b;
3700 const struct util_format_description *desc;
3701 unsigned char swizzle[4];
3702 int first_non_void;
3703 unsigned num_format, data_format, type, num_samples;
3704 uint64_t va;
3705
3706 desc = util_format_description(pipe_format);
3707
3708 num_samples = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ?
3709 MAX2(1, res->nr_samples) :
3710 MAX2(1, res->nr_storage_samples);
3711
3712 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
3713 const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
3714 const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
3715 const unsigned char swizzle_wwww[4] = {3, 3, 3, 3};
3716
3717 switch (pipe_format) {
3718 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
3719 case PIPE_FORMAT_X32_S8X24_UINT:
3720 case PIPE_FORMAT_X8Z24_UNORM:
3721 util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
3722 break;
3723 case PIPE_FORMAT_X24S8_UINT:
3724 /*
3725 * X24S8 is implemented as an 8_8_8_8 data format, to
3726 * fix texture gathers. This affects at least
3727 * GL45-CTS.texture_cube_map_array.sampling on VI.
3728 */
3729 if (screen->info.chip_class <= VI)
3730 util_format_compose_swizzles(swizzle_wwww, state_swizzle, swizzle);
3731 else
3732 util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
3733 break;
3734 default:
3735 util_format_compose_swizzles(swizzle_xxxx, state_swizzle, swizzle);
3736 }
3737 } else {
3738 util_format_compose_swizzles(desc->swizzle, state_swizzle, swizzle);
3739 }
3740
3741 first_non_void = util_format_get_first_non_void_channel(pipe_format);
3742
3743 switch (pipe_format) {
3744 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
3745 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3746 break;
3747 default:
3748 if (first_non_void < 0) {
3749 if (util_format_is_compressed(pipe_format)) {
3750 switch (pipe_format) {
3751 case PIPE_FORMAT_DXT1_SRGB:
3752 case PIPE_FORMAT_DXT1_SRGBA:
3753 case PIPE_FORMAT_DXT3_SRGBA:
3754 case PIPE_FORMAT_DXT5_SRGBA:
3755 case PIPE_FORMAT_BPTC_SRGBA:
3756 case PIPE_FORMAT_ETC2_SRGB8:
3757 case PIPE_FORMAT_ETC2_SRGB8A1:
3758 case PIPE_FORMAT_ETC2_SRGBA8:
3759 num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
3760 break;
3761 case PIPE_FORMAT_RGTC1_SNORM:
3762 case PIPE_FORMAT_LATC1_SNORM:
3763 case PIPE_FORMAT_RGTC2_SNORM:
3764 case PIPE_FORMAT_LATC2_SNORM:
3765 case PIPE_FORMAT_ETC2_R11_SNORM:
3766 case PIPE_FORMAT_ETC2_RG11_SNORM:
3767 /* implies float, so use SNORM/UNORM to determine
3768 whether data is signed or not */
3769 case PIPE_FORMAT_BPTC_RGB_FLOAT:
3770 num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
3771 break;
3772 default:
3773 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3774 break;
3775 }
3776 } else if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
3777 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3778 } else {
3779 num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
3780 }
3781 } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
3782 num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
3783 } else {
3784 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3785
3786 switch (desc->channel[first_non_void].type) {
3787 case UTIL_FORMAT_TYPE_FLOAT:
3788 num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
3789 break;
3790 case UTIL_FORMAT_TYPE_SIGNED:
3791 if (desc->channel[first_non_void].normalized)
3792 num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
3793 else if (desc->channel[first_non_void].pure_integer)
3794 num_format = V_008F14_IMG_NUM_FORMAT_SINT;
3795 else
3796 num_format = V_008F14_IMG_NUM_FORMAT_SSCALED;
3797 break;
3798 case UTIL_FORMAT_TYPE_UNSIGNED:
3799 if (desc->channel[first_non_void].normalized)
3800 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
3801 else if (desc->channel[first_non_void].pure_integer)
3802 num_format = V_008F14_IMG_NUM_FORMAT_UINT;
3803 else
3804 num_format = V_008F14_IMG_NUM_FORMAT_USCALED;
3805 }
3806 }
3807 }
3808
3809 data_format = si_translate_texformat(&screen->b, pipe_format, desc, first_non_void);
3810 if (data_format == ~0) {
3811 data_format = 0;
3812 }
3813
3814 /* S8 with Z32 HTILE needs a special format. */
3815 if (screen->info.chip_class >= GFX9 &&
3816 pipe_format == PIPE_FORMAT_S8_UINT &&
3817 tex->tc_compatible_htile)
3818 data_format = V_008F14_IMG_DATA_FORMAT_S8_32;
3819
3820 if (!sampler &&
3821 (res->target == PIPE_TEXTURE_CUBE ||
3822 res->target == PIPE_TEXTURE_CUBE_ARRAY ||
3823 (screen->info.chip_class <= VI &&
3824 res->target == PIPE_TEXTURE_3D))) {
3825 /* For the purpose of shader images, treat cube maps and 3D
3826 * textures as 2D arrays. For 3D textures, the address
3827 * calculations for mipmaps are different, so we rely on the
3828 * caller to effectively disable mipmaps.
3829 */
3830 type = V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
3831
3832 assert(res->target != PIPE_TEXTURE_3D || (first_level == 0 && last_level == 0));
3833 } else {
3834 type = si_tex_dim(screen, tex, target, num_samples);
3835 }
3836
3837 if (type == V_008F1C_SQ_RSRC_IMG_1D_ARRAY) {
3838 height = 1;
3839 depth = res->array_size;
3840 } else if (type == V_008F1C_SQ_RSRC_IMG_2D_ARRAY ||
3841 type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
3842 if (sampler || res->target != PIPE_TEXTURE_3D)
3843 depth = res->array_size;
3844 } else if (type == V_008F1C_SQ_RSRC_IMG_CUBE)
3845 depth = res->array_size / 6;
3846
3847 state[0] = 0;
3848 state[1] = (S_008F14_DATA_FORMAT_GFX6(data_format) |
3849 S_008F14_NUM_FORMAT_GFX6(num_format));
3850 state[2] = (S_008F18_WIDTH(width - 1) |
3851 S_008F18_HEIGHT(height - 1) |
3852 S_008F18_PERF_MOD(4));
3853 state[3] = (S_008F1C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
3854 S_008F1C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
3855 S_008F1C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
3856 S_008F1C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
3857 S_008F1C_BASE_LEVEL(num_samples > 1 ? 0 : first_level) |
3858 S_008F1C_LAST_LEVEL(num_samples > 1 ?
3859 util_logbase2(num_samples) :
3860 last_level) |
3861 S_008F1C_TYPE(type));
3862 state[4] = 0;
3863 state[5] = S_008F24_BASE_ARRAY(first_layer);
3864 state[6] = 0;
3865 state[7] = 0;
3866
3867 if (screen->info.chip_class >= GFX9) {
3868 unsigned bc_swizzle = gfx9_border_color_swizzle(desc->swizzle);
3869
3870 /* Depth is the the last accessible layer on Gfx9.
3871 * The hw doesn't need to know the total number of layers.
3872 */
3873 if (type == V_008F1C_SQ_RSRC_IMG_3D)
3874 state[4] |= S_008F20_DEPTH(depth - 1);
3875 else
3876 state[4] |= S_008F20_DEPTH(last_layer);
3877
3878 state[4] |= S_008F20_BC_SWIZZLE(bc_swizzle);
3879 state[5] |= S_008F24_MAX_MIP(num_samples > 1 ?
3880 util_logbase2(num_samples) :
3881 tex->buffer.b.b.last_level);
3882 } else {
3883 state[3] |= S_008F1C_POW2_PAD(res->last_level > 0);
3884 state[4] |= S_008F20_DEPTH(depth - 1);
3885 state[5] |= S_008F24_LAST_ARRAY(last_layer);
3886 }
3887
3888 if (tex->dcc_offset) {
3889 state[6] = S_008F28_ALPHA_IS_ON_MSB(vi_alpha_is_on_msb(pipe_format));
3890 } else {
3891 /* The last dword is unused by hw. The shader uses it to clear
3892 * bits in the first dword of sampler state.
3893 */
3894 if (screen->info.chip_class <= CIK && res->nr_samples <= 1) {
3895 if (first_level == last_level)
3896 state[7] = C_008F30_MAX_ANISO_RATIO;
3897 else
3898 state[7] = 0xffffffff;
3899 }
3900 }
3901
3902 /* Initialize the sampler view for FMASK. */
3903 if (tex->surface.fmask_size) {
3904 uint32_t data_format, num_format;
3905
3906 va = tex->buffer.gpu_address + tex->fmask_offset;
3907
3908 #define FMASK(s,f) (((unsigned)(MAX2(1, s)) * 16) + (MAX2(1, f)))
3909 if (screen->info.chip_class >= GFX9) {
3910 data_format = V_008F14_IMG_DATA_FORMAT_FMASK;
3911 switch (FMASK(res->nr_samples, res->nr_storage_samples)) {
3912 case FMASK(2,1):
3913 num_format = V_008F14_IMG_FMASK_8_2_1;
3914 break;
3915 case FMASK(2,2):
3916 num_format = V_008F14_IMG_FMASK_8_2_2;
3917 break;
3918 case FMASK(4,1):
3919 num_format = V_008F14_IMG_FMASK_8_4_1;
3920 break;
3921 case FMASK(4,2):
3922 num_format = V_008F14_IMG_FMASK_8_4_2;
3923 break;
3924 case FMASK(4,4):
3925 num_format = V_008F14_IMG_FMASK_8_4_4;
3926 break;
3927 case FMASK(8,1):
3928 num_format = V_008F14_IMG_FMASK_8_8_1;
3929 break;
3930 case FMASK(8,2):
3931 num_format = V_008F14_IMG_FMASK_16_8_2;
3932 break;
3933 case FMASK(8,4):
3934 num_format = V_008F14_IMG_FMASK_32_8_4;
3935 break;
3936 case FMASK(8,8):
3937 num_format = V_008F14_IMG_FMASK_32_8_8;
3938 break;
3939 case FMASK(16,1):
3940 num_format = V_008F14_IMG_FMASK_16_16_1;
3941 break;
3942 case FMASK(16,2):
3943 num_format = V_008F14_IMG_FMASK_32_16_2;
3944 break;
3945 case FMASK(16,4):
3946 num_format = V_008F14_IMG_FMASK_64_16_4;
3947 break;
3948 case FMASK(16,8):
3949 num_format = V_008F14_IMG_FMASK_64_16_8;
3950 break;
3951 default:
3952 unreachable("invalid nr_samples");
3953 }
3954 } else {
3955 switch (FMASK(res->nr_samples, res->nr_storage_samples)) {
3956 case FMASK(2,1):
3957 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F1;
3958 break;
3959 case FMASK(2,2):
3960 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2;
3961 break;
3962 case FMASK(4,1):
3963 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F1;
3964 break;
3965 case FMASK(4,2):
3966 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F2;
3967 break;
3968 case FMASK(4,4):
3969 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4;
3970 break;
3971 case FMASK(8,1):
3972 data_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S8_F1;
3973 break;
3974 case FMASK(8,2):
3975 data_format = V_008F14_IMG_DATA_FORMAT_FMASK16_S8_F2;
3976 break;
3977 case FMASK(8,4):
3978 data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F4;
3979 break;
3980 case FMASK(8,8):
3981 data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8;
3982 break;
3983 case FMASK(16,1):
3984 data_format = V_008F14_IMG_DATA_FORMAT_FMASK16_S16_F1;
3985 break;
3986 case FMASK(16,2):
3987 data_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S16_F2;
3988 break;
3989 case FMASK(16,4):
3990 data_format = V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F4;
3991 break;
3992 case FMASK(16,8):
3993 data_format = V_008F14_IMG_DATA_FORMAT_FMASK64_S16_F8;
3994 break;
3995 default:
3996 unreachable("invalid nr_samples");
3997 }
3998 num_format = V_008F14_IMG_NUM_FORMAT_UINT;
3999 }
4000 #undef FMASK
4001
4002 fmask_state[0] = (va >> 8) | tex->surface.fmask_tile_swizzle;
4003 fmask_state[1] = S_008F14_BASE_ADDRESS_HI(va >> 40) |
4004 S_008F14_DATA_FORMAT_GFX6(data_format) |
4005 S_008F14_NUM_FORMAT_GFX6(num_format);
4006 fmask_state[2] = S_008F18_WIDTH(width - 1) |
4007 S_008F18_HEIGHT(height - 1);
4008 fmask_state[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X) |
4009 S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X) |
4010 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X) |
4011 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X) |
4012 S_008F1C_TYPE(si_tex_dim(screen, tex, target, 0));
4013 fmask_state[4] = 0;
4014 fmask_state[5] = S_008F24_BASE_ARRAY(first_layer);
4015 fmask_state[6] = 0;
4016 fmask_state[7] = 0;
4017
4018 if (screen->info.chip_class >= GFX9) {
4019 fmask_state[3] |= S_008F1C_SW_MODE(tex->surface.u.gfx9.fmask.swizzle_mode);
4020 fmask_state[4] |= S_008F20_DEPTH(last_layer) |
4021 S_008F20_PITCH_GFX9(tex->surface.u.gfx9.fmask.epitch);
4022 fmask_state[5] |= S_008F24_META_PIPE_ALIGNED(tex->surface.u.gfx9.cmask.pipe_aligned) |
4023 S_008F24_META_RB_ALIGNED(tex->surface.u.gfx9.cmask.rb_aligned);
4024 } else {
4025 fmask_state[3] |= S_008F1C_TILING_INDEX(tex->surface.u.legacy.fmask.tiling_index);
4026 fmask_state[4] |= S_008F20_DEPTH(depth - 1) |
4027 S_008F20_PITCH_GFX6(tex->surface.u.legacy.fmask.pitch_in_pixels - 1);
4028 fmask_state[5] |= S_008F24_LAST_ARRAY(last_layer);
4029 }
4030 }
4031 }
4032
4033 /**
4034 * Create a sampler view.
4035 *
4036 * @param ctx context
4037 * @param texture texture
4038 * @param state sampler view template
4039 * @param width0 width0 override (for compressed textures as int)
4040 * @param height0 height0 override (for compressed textures as int)
4041 * @param force_level set the base address to the level (for compressed textures)
4042 */
4043 struct pipe_sampler_view *
4044 si_create_sampler_view_custom(struct pipe_context *ctx,
4045 struct pipe_resource *texture,
4046 const struct pipe_sampler_view *state,
4047 unsigned width0, unsigned height0,
4048 unsigned force_level)
4049 {
4050 struct si_context *sctx = (struct si_context*)ctx;
4051 struct si_sampler_view *view = CALLOC_STRUCT(si_sampler_view);
4052 struct si_texture *tex = (struct si_texture*)texture;
4053 unsigned base_level, first_level, last_level;
4054 unsigned char state_swizzle[4];
4055 unsigned height, depth, width;
4056 unsigned last_layer = state->u.tex.last_layer;
4057 enum pipe_format pipe_format;
4058 const struct legacy_surf_level *surflevel;
4059
4060 if (!view)
4061 return NULL;
4062
4063 /* initialize base object */
4064 view->base = *state;
4065 view->base.texture = NULL;
4066 view->base.reference.count = 1;
4067 view->base.context = ctx;
4068
4069 assert(texture);
4070 pipe_resource_reference(&view->base.texture, texture);
4071
4072 if (state->format == PIPE_FORMAT_X24S8_UINT ||
4073 state->format == PIPE_FORMAT_S8X24_UINT ||
4074 state->format == PIPE_FORMAT_X32_S8X24_UINT ||
4075 state->format == PIPE_FORMAT_S8_UINT)
4076 view->is_stencil_sampler = true;
4077
4078 /* Buffer resource. */
4079 if (texture->target == PIPE_BUFFER) {
4080 si_make_buffer_descriptor(sctx->screen,
4081 si_resource(texture),
4082 state->format,
4083 state->u.buf.offset,
4084 state->u.buf.size,
4085 view->state);
4086 return &view->base;
4087 }
4088
4089 state_swizzle[0] = state->swizzle_r;
4090 state_swizzle[1] = state->swizzle_g;
4091 state_swizzle[2] = state->swizzle_b;
4092 state_swizzle[3] = state->swizzle_a;
4093
4094 base_level = 0;
4095 first_level = state->u.tex.first_level;
4096 last_level = state->u.tex.last_level;
4097 width = width0;
4098 height = height0;
4099 depth = texture->depth0;
4100
4101 if (sctx->chip_class <= VI && force_level) {
4102 assert(force_level == first_level &&
4103 force_level == last_level);
4104 base_level = force_level;
4105 first_level = 0;
4106 last_level = 0;
4107 width = u_minify(width, force_level);
4108 height = u_minify(height, force_level);
4109 depth = u_minify(depth, force_level);
4110 }
4111
4112 /* This is not needed if state trackers set last_layer correctly. */
4113 if (state->target == PIPE_TEXTURE_1D ||
4114 state->target == PIPE_TEXTURE_2D ||
4115 state->target == PIPE_TEXTURE_RECT ||
4116 state->target == PIPE_TEXTURE_CUBE)
4117 last_layer = state->u.tex.first_layer;
4118
4119 /* Texturing with separate depth and stencil. */
4120 pipe_format = state->format;
4121
4122 /* Depth/stencil texturing sometimes needs separate texture. */
4123 if (tex->is_depth && !si_can_sample_zs(tex, view->is_stencil_sampler)) {
4124 if (!tex->flushed_depth_texture &&
4125 !si_init_flushed_depth_texture(ctx, texture, NULL)) {
4126 pipe_resource_reference(&view->base.texture, NULL);
4127 FREE(view);
4128 return NULL;
4129 }
4130
4131 assert(tex->flushed_depth_texture);
4132
4133 /* Override format for the case where the flushed texture
4134 * contains only Z or only S.
4135 */
4136 if (tex->flushed_depth_texture->buffer.b.b.format != tex->buffer.b.b.format)
4137 pipe_format = tex->flushed_depth_texture->buffer.b.b.format;
4138
4139 tex = tex->flushed_depth_texture;
4140 }
4141
4142 surflevel = tex->surface.u.legacy.level;
4143
4144 if (tex->db_compatible) {
4145 if (!view->is_stencil_sampler)
4146 pipe_format = tex->db_render_format;
4147
4148 switch (pipe_format) {
4149 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
4150 pipe_format = PIPE_FORMAT_Z32_FLOAT;
4151 break;
4152 case PIPE_FORMAT_X8Z24_UNORM:
4153 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
4154 /* Z24 is always stored like this for DB
4155 * compatibility.
4156 */
4157 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
4158 break;
4159 case PIPE_FORMAT_X24S8_UINT:
4160 case PIPE_FORMAT_S8X24_UINT:
4161 case PIPE_FORMAT_X32_S8X24_UINT:
4162 pipe_format = PIPE_FORMAT_S8_UINT;
4163 surflevel = tex->surface.u.legacy.stencil_level;
4164 break;
4165 default:;
4166 }
4167 }
4168
4169 view->dcc_incompatible =
4170 vi_dcc_formats_are_incompatible(texture,
4171 state->u.tex.first_level,
4172 state->format);
4173
4174 si_make_texture_descriptor(sctx->screen, tex, true,
4175 state->target, pipe_format, state_swizzle,
4176 first_level, last_level,
4177 state->u.tex.first_layer, last_layer,
4178 width, height, depth,
4179 view->state, view->fmask_state);
4180
4181 unsigned num_format = G_008F14_NUM_FORMAT_GFX6(view->state[1]);
4182 view->is_integer =
4183 num_format == V_008F14_IMG_NUM_FORMAT_USCALED ||
4184 num_format == V_008F14_IMG_NUM_FORMAT_SSCALED ||
4185 num_format == V_008F14_IMG_NUM_FORMAT_UINT ||
4186 num_format == V_008F14_IMG_NUM_FORMAT_SINT;
4187 view->base_level_info = &surflevel[base_level];
4188 view->base_level = base_level;
4189 view->block_width = util_format_get_blockwidth(pipe_format);
4190 return &view->base;
4191 }
4192
4193 static struct pipe_sampler_view *
4194 si_create_sampler_view(struct pipe_context *ctx,
4195 struct pipe_resource *texture,
4196 const struct pipe_sampler_view *state)
4197 {
4198 return si_create_sampler_view_custom(ctx, texture, state,
4199 texture ? texture->width0 : 0,
4200 texture ? texture->height0 : 0, 0);
4201 }
4202
4203 static void si_sampler_view_destroy(struct pipe_context *ctx,
4204 struct pipe_sampler_view *state)
4205 {
4206 struct si_sampler_view *view = (struct si_sampler_view *)state;
4207
4208 pipe_resource_reference(&state->texture, NULL);
4209 FREE(view);
4210 }
4211
4212 static bool wrap_mode_uses_border_color(unsigned wrap, bool linear_filter)
4213 {
4214 return wrap == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
4215 wrap == PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER ||
4216 (linear_filter &&
4217 (wrap == PIPE_TEX_WRAP_CLAMP ||
4218 wrap == PIPE_TEX_WRAP_MIRROR_CLAMP));
4219 }
4220
4221 static uint32_t si_translate_border_color(struct si_context *sctx,
4222 const struct pipe_sampler_state *state,
4223 const union pipe_color_union *color,
4224 bool is_integer)
4225 {
4226 bool linear_filter = state->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
4227 state->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
4228
4229 if (!wrap_mode_uses_border_color(state->wrap_s, linear_filter) &&
4230 !wrap_mode_uses_border_color(state->wrap_t, linear_filter) &&
4231 !wrap_mode_uses_border_color(state->wrap_r, linear_filter))
4232 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK);
4233
4234 #define simple_border_types(elt) \
4235 do { \
4236 if (color->elt[0] == 0 && color->elt[1] == 0 && \
4237 color->elt[2] == 0 && color->elt[3] == 0) \
4238 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK); \
4239 if (color->elt[0] == 0 && color->elt[1] == 0 && \
4240 color->elt[2] == 0 && color->elt[3] == 1) \
4241 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK); \
4242 if (color->elt[0] == 1 && color->elt[1] == 1 && \
4243 color->elt[2] == 1 && color->elt[3] == 1) \
4244 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE); \
4245 } while (false)
4246
4247 if (is_integer)
4248 simple_border_types(ui);
4249 else
4250 simple_border_types(f);
4251
4252 #undef simple_border_types
4253
4254 int i;
4255
4256 /* Check if the border has been uploaded already. */
4257 for (i = 0; i < sctx->border_color_count; i++)
4258 if (memcmp(&sctx->border_color_table[i], color,
4259 sizeof(*color)) == 0)
4260 break;
4261
4262 if (i >= SI_MAX_BORDER_COLORS) {
4263 /* Getting 4096 unique border colors is very unlikely. */
4264 fprintf(stderr, "radeonsi: The border color table is full. "
4265 "Any new border colors will be just black. "
4266 "Please file a bug.\n");
4267 return S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK);
4268 }
4269
4270 if (i == sctx->border_color_count) {
4271 /* Upload a new border color. */
4272 memcpy(&sctx->border_color_table[i], color,
4273 sizeof(*color));
4274 util_memcpy_cpu_to_le32(&sctx->border_color_map[i],
4275 color, sizeof(*color));
4276 sctx->border_color_count++;
4277 }
4278
4279 return S_008F3C_BORDER_COLOR_PTR(i) |
4280 S_008F3C_BORDER_COLOR_TYPE(V_008F3C_SQ_TEX_BORDER_COLOR_REGISTER);
4281 }
4282
4283 static inline int S_FIXED(float value, unsigned frac_bits)
4284 {
4285 return value * (1 << frac_bits);
4286 }
4287
4288 static inline unsigned si_tex_filter(unsigned filter, unsigned max_aniso)
4289 {
4290 if (filter == PIPE_TEX_FILTER_LINEAR)
4291 return max_aniso > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_BILINEAR
4292 : V_008F38_SQ_TEX_XY_FILTER_BILINEAR;
4293 else
4294 return max_aniso > 1 ? V_008F38_SQ_TEX_XY_FILTER_ANISO_POINT
4295 : V_008F38_SQ_TEX_XY_FILTER_POINT;
4296 }
4297
4298 static inline unsigned si_tex_aniso_filter(unsigned filter)
4299 {
4300 if (filter < 2)
4301 return 0;
4302 if (filter < 4)
4303 return 1;
4304 if (filter < 8)
4305 return 2;
4306 if (filter < 16)
4307 return 3;
4308 return 4;
4309 }
4310
4311 static void *si_create_sampler_state(struct pipe_context *ctx,
4312 const struct pipe_sampler_state *state)
4313 {
4314 struct si_context *sctx = (struct si_context *)ctx;
4315 struct si_screen *sscreen = sctx->screen;
4316 struct si_sampler_state *rstate = CALLOC_STRUCT(si_sampler_state);
4317 unsigned max_aniso = sscreen->force_aniso >= 0 ? sscreen->force_aniso
4318 : state->max_anisotropy;
4319 unsigned max_aniso_ratio = si_tex_aniso_filter(max_aniso);
4320 union pipe_color_union clamped_border_color;
4321
4322 if (!rstate) {
4323 return NULL;
4324 }
4325
4326 #ifdef DEBUG
4327 rstate->magic = SI_SAMPLER_STATE_MAGIC;
4328 #endif
4329 rstate->val[0] = (S_008F30_CLAMP_X(si_tex_wrap(state->wrap_s)) |
4330 S_008F30_CLAMP_Y(si_tex_wrap(state->wrap_t)) |
4331 S_008F30_CLAMP_Z(si_tex_wrap(state->wrap_r)) |
4332 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
4333 S_008F30_DEPTH_COMPARE_FUNC(si_tex_compare(state->compare_func)) |
4334 S_008F30_FORCE_UNNORMALIZED(!state->normalized_coords) |
4335 S_008F30_ANISO_THRESHOLD(max_aniso_ratio >> 1) |
4336 S_008F30_ANISO_BIAS(max_aniso_ratio) |
4337 S_008F30_DISABLE_CUBE_WRAP(!state->seamless_cube_map) |
4338 S_008F30_COMPAT_MODE(sctx->chip_class >= VI));
4339 rstate->val[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(state->min_lod, 0, 15), 8)) |
4340 S_008F34_MAX_LOD(S_FIXED(CLAMP(state->max_lod, 0, 15), 8)) |
4341 S_008F34_PERF_MIP(max_aniso_ratio ? max_aniso_ratio + 6 : 0));
4342 rstate->val[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(state->lod_bias, -16, 16), 8)) |
4343 S_008F38_XY_MAG_FILTER(si_tex_filter(state->mag_img_filter, max_aniso)) |
4344 S_008F38_XY_MIN_FILTER(si_tex_filter(state->min_img_filter, max_aniso)) |
4345 S_008F38_MIP_FILTER(si_tex_mipfilter(state->min_mip_filter)) |
4346 S_008F38_MIP_POINT_PRECLAMP(0) |
4347 S_008F38_DISABLE_LSB_CEIL(sctx->chip_class <= VI) |
4348 S_008F38_FILTER_PREC_FIX(1) |
4349 S_008F38_ANISO_OVERRIDE(sctx->chip_class >= VI));
4350 rstate->val[3] = si_translate_border_color(sctx, state, &state->border_color, false);
4351
4352 /* Create sampler resource for integer textures. */
4353 memcpy(rstate->integer_val, rstate->val, sizeof(rstate->val));
4354 rstate->integer_val[3] = si_translate_border_color(sctx, state, &state->border_color, true);
4355
4356 /* Create sampler resource for upgraded depth textures. */
4357 memcpy(rstate->upgraded_depth_val, rstate->val, sizeof(rstate->val));
4358
4359 for (unsigned i = 0; i < 4; ++i) {
4360 /* Use channel 0 on purpose, so that we can use OPAQUE_WHITE
4361 * when the border color is 1.0. */
4362 clamped_border_color.f[i] = CLAMP(state->border_color.f[0], 0, 1);
4363 }
4364
4365 if (memcmp(&state->border_color, &clamped_border_color, sizeof(clamped_border_color)) == 0)
4366 rstate->upgraded_depth_val[3] |= S_008F3C_UPGRADED_DEPTH(1);
4367 else
4368 rstate->upgraded_depth_val[3] =
4369 si_translate_border_color(sctx, state, &clamped_border_color, false) |
4370 S_008F3C_UPGRADED_DEPTH(1);
4371
4372 return rstate;
4373 }
4374
4375 static void si_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
4376 {
4377 struct si_context *sctx = (struct si_context *)ctx;
4378
4379 if (sctx->sample_mask == (uint16_t)sample_mask)
4380 return;
4381
4382 sctx->sample_mask = sample_mask;
4383 si_mark_atom_dirty(sctx, &sctx->atoms.s.sample_mask);
4384 }
4385
4386 static void si_emit_sample_mask(struct si_context *sctx)
4387 {
4388 struct radeon_cmdbuf *cs = sctx->gfx_cs;
4389 unsigned mask = sctx->sample_mask;
4390
4391 /* Needed for line and polygon smoothing as well as for the Polaris
4392 * small primitive filter. We expect the state tracker to take care of
4393 * this for us.
4394 */
4395 assert(mask == 0xffff || sctx->framebuffer.nr_samples > 1 ||
4396 (mask & 1 && sctx->blitter->running));
4397
4398 radeon_set_context_reg_seq(cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
4399 radeon_emit(cs, mask | (mask << 16));
4400 radeon_emit(cs, mask | (mask << 16));
4401 }
4402
4403 static void si_delete_sampler_state(struct pipe_context *ctx, void *state)
4404 {
4405 #ifdef DEBUG
4406 struct si_sampler_state *s = state;
4407
4408 assert(s->magic == SI_SAMPLER_STATE_MAGIC);
4409 s->magic = 0;
4410 #endif
4411 free(state);
4412 }
4413
4414 /*
4415 * Vertex elements & buffers
4416 */
4417
4418 struct util_fast_udiv_info32 {
4419 unsigned multiplier; /* the "magic number" multiplier */
4420 unsigned pre_shift; /* shift for the dividend before multiplying */
4421 unsigned post_shift; /* shift for the dividend after multiplying */
4422 int increment; /* 0 or 1; if set then increment the numerator, using one of
4423 the two strategies */
4424 };
4425
4426 static struct util_fast_udiv_info32
4427 util_compute_fast_udiv_info32(uint32_t D, unsigned num_bits)
4428 {
4429 struct util_fast_udiv_info info =
4430 util_compute_fast_udiv_info(D, num_bits, 32);
4431
4432 struct util_fast_udiv_info32 result = {
4433 info.multiplier,
4434 info.pre_shift,
4435 info.post_shift,
4436 info.increment,
4437 };
4438 return result;
4439 }
4440
4441 static void *si_create_vertex_elements(struct pipe_context *ctx,
4442 unsigned count,
4443 const struct pipe_vertex_element *elements)
4444 {
4445 struct si_screen *sscreen = (struct si_screen*)ctx->screen;
4446 struct si_vertex_elements *v = CALLOC_STRUCT(si_vertex_elements);
4447 bool used[SI_NUM_VERTEX_BUFFERS] = {};
4448 struct util_fast_udiv_info32 divisor_factors[SI_MAX_ATTRIBS] = {};
4449 STATIC_ASSERT(sizeof(struct util_fast_udiv_info32) == 16);
4450 STATIC_ASSERT(sizeof(divisor_factors[0].multiplier) == 4);
4451 STATIC_ASSERT(sizeof(divisor_factors[0].pre_shift) == 4);
4452 STATIC_ASSERT(sizeof(divisor_factors[0].post_shift) == 4);
4453 STATIC_ASSERT(sizeof(divisor_factors[0].increment) == 4);
4454 int i;
4455
4456 assert(count <= SI_MAX_ATTRIBS);
4457 if (!v)
4458 return NULL;
4459
4460 v->count = count;
4461 v->desc_list_byte_size = align(count * 16, SI_CPDMA_ALIGNMENT);
4462
4463 for (i = 0; i < count; ++i) {
4464 const struct util_format_description *desc;
4465 const struct util_format_channel_description *channel;
4466 unsigned data_format, num_format;
4467 int first_non_void;
4468 unsigned vbo_index = elements[i].vertex_buffer_index;
4469 unsigned char swizzle[4];
4470
4471 if (vbo_index >= SI_NUM_VERTEX_BUFFERS) {
4472 FREE(v);
4473 return NULL;
4474 }
4475
4476 unsigned instance_divisor = elements[i].instance_divisor;
4477 if (instance_divisor) {
4478 v->uses_instance_divisors = true;
4479
4480 if (instance_divisor == 1) {
4481 v->instance_divisor_is_one |= 1u << i;
4482 } else {
4483 v->instance_divisor_is_fetched |= 1u << i;
4484 divisor_factors[i] =
4485 util_compute_fast_udiv_info32(instance_divisor, 32);
4486 }
4487 }
4488
4489 if (!used[vbo_index]) {
4490 v->first_vb_use_mask |= 1 << i;
4491 used[vbo_index] = true;
4492 }
4493
4494 desc = util_format_description(elements[i].src_format);
4495 first_non_void = util_format_get_first_non_void_channel(elements[i].src_format);
4496 data_format = si_translate_buffer_dataformat(ctx->screen, desc, first_non_void);
4497 num_format = si_translate_buffer_numformat(ctx->screen, desc, first_non_void);
4498 channel = first_non_void >= 0 ? &desc->channel[first_non_void] : NULL;
4499 memcpy(swizzle, desc->swizzle, sizeof(swizzle));
4500
4501 v->format_size[i] = desc->block.bits / 8;
4502 v->src_offset[i] = elements[i].src_offset;
4503 v->vertex_buffer_index[i] = vbo_index;
4504
4505 /* The hardware always treats the 2-bit alpha channel as
4506 * unsigned, so a shader workaround is needed. The affected
4507 * chips are VI and older except Stoney (GFX8.1).
4508 */
4509 if (data_format == V_008F0C_BUF_DATA_FORMAT_2_10_10_10 &&
4510 sscreen->info.chip_class <= VI &&
4511 sscreen->info.family != CHIP_STONEY) {
4512 if (num_format == V_008F0C_BUF_NUM_FORMAT_SNORM) {
4513 v->fix_fetch[i] = SI_FIX_FETCH_A2_SNORM;
4514 } else if (num_format == V_008F0C_BUF_NUM_FORMAT_SSCALED) {
4515 v->fix_fetch[i] = SI_FIX_FETCH_A2_SSCALED;
4516 } else if (num_format == V_008F0C_BUF_NUM_FORMAT_SINT) {
4517 /* This isn't actually used in OpenGL. */
4518 v->fix_fetch[i] = SI_FIX_FETCH_A2_SINT;
4519 }
4520 } else if (channel && channel->type == UTIL_FORMAT_TYPE_FIXED) {
4521 if (desc->swizzle[3] == PIPE_SWIZZLE_1)
4522 v->fix_fetch[i] = SI_FIX_FETCH_RGBX_32_FIXED;
4523 else
4524 v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_FIXED;
4525 } else if (channel && channel->size == 32 && !channel->pure_integer) {
4526 if (channel->type == UTIL_FORMAT_TYPE_SIGNED) {
4527 if (channel->normalized) {
4528 if (desc->swizzle[3] == PIPE_SWIZZLE_1)
4529 v->fix_fetch[i] = SI_FIX_FETCH_RGBX_32_SNORM;
4530 else
4531 v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_SNORM;
4532 } else {
4533 v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_SSCALED;
4534 }
4535 } else if (channel->type == UTIL_FORMAT_TYPE_UNSIGNED) {
4536 if (channel->normalized) {
4537 if (desc->swizzle[3] == PIPE_SWIZZLE_1)
4538 v->fix_fetch[i] = SI_FIX_FETCH_RGBX_32_UNORM;
4539 else
4540 v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_UNORM;
4541 } else {
4542 v->fix_fetch[i] = SI_FIX_FETCH_RGBA_32_USCALED;
4543 }
4544 }
4545 } else if (channel && channel->size == 64 &&
4546 channel->type == UTIL_FORMAT_TYPE_FLOAT) {
4547 switch (desc->nr_channels) {
4548 case 1:
4549 case 2:
4550 v->fix_fetch[i] = SI_FIX_FETCH_RG_64_FLOAT;
4551 swizzle[0] = PIPE_SWIZZLE_X;
4552 swizzle[1] = PIPE_SWIZZLE_Y;
4553 swizzle[2] = desc->nr_channels == 2 ? PIPE_SWIZZLE_Z : PIPE_SWIZZLE_0;
4554 swizzle[3] = desc->nr_channels == 2 ? PIPE_SWIZZLE_W : PIPE_SWIZZLE_0;
4555 break;
4556 case 3:
4557 v->fix_fetch[i] = SI_FIX_FETCH_RGB_64_FLOAT;
4558 swizzle[0] = PIPE_SWIZZLE_X; /* 3 loads */
4559 swizzle[1] = PIPE_SWIZZLE_Y;
4560 swizzle[2] = PIPE_SWIZZLE_0;
4561 swizzle[3] = PIPE_SWIZZLE_0;
4562 break;
4563 case 4:
4564 v->fix_fetch[i] = SI_FIX_FETCH_RGBA_64_FLOAT;
4565 swizzle[0] = PIPE_SWIZZLE_X; /* 2 loads */
4566 swizzle[1] = PIPE_SWIZZLE_Y;
4567 swizzle[2] = PIPE_SWIZZLE_Z;
4568 swizzle[3] = PIPE_SWIZZLE_W;
4569 break;
4570 default:
4571 assert(0);
4572 }
4573 } else if (channel && desc->nr_channels == 3) {
4574 assert(desc->swizzle[0] == PIPE_SWIZZLE_X);
4575
4576 if (channel->size == 8) {
4577 if (channel->pure_integer)
4578 v->fix_fetch[i] = SI_FIX_FETCH_RGB_8_INT;
4579 else
4580 v->fix_fetch[i] = SI_FIX_FETCH_RGB_8;
4581 } else if (channel->size == 16) {
4582 if (channel->pure_integer)
4583 v->fix_fetch[i] = SI_FIX_FETCH_RGB_16_INT;
4584 else
4585 v->fix_fetch[i] = SI_FIX_FETCH_RGB_16;
4586 }
4587 }
4588
4589 v->rsrc_word3[i] = S_008F0C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
4590 S_008F0C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
4591 S_008F0C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
4592 S_008F0C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
4593 S_008F0C_NUM_FORMAT(num_format) |
4594 S_008F0C_DATA_FORMAT(data_format);
4595 }
4596
4597 if (v->instance_divisor_is_fetched) {
4598 unsigned num_divisors = util_last_bit(v->instance_divisor_is_fetched);
4599
4600 v->instance_divisor_factor_buffer =
4601 (struct si_resource*)
4602 pipe_buffer_create(&sscreen->b, 0, PIPE_USAGE_DEFAULT,
4603 num_divisors * sizeof(divisor_factors[0]));
4604 if (!v->instance_divisor_factor_buffer) {
4605 FREE(v);
4606 return NULL;
4607 }
4608 void *map = sscreen->ws->buffer_map(v->instance_divisor_factor_buffer->buf,
4609 NULL, PIPE_TRANSFER_WRITE);
4610 memcpy(map , divisor_factors, num_divisors * sizeof(divisor_factors[0]));
4611 }
4612 return v;
4613 }
4614
4615 static void si_bind_vertex_elements(struct pipe_context *ctx, void *state)
4616 {
4617 struct si_context *sctx = (struct si_context *)ctx;
4618 struct si_vertex_elements *old = sctx->vertex_elements;
4619 struct si_vertex_elements *v = (struct si_vertex_elements*)state;
4620
4621 sctx->vertex_elements = v;
4622 sctx->vertex_buffers_dirty = true;
4623
4624 if (v &&
4625 (!old ||
4626 old->count != v->count ||
4627 old->uses_instance_divisors != v->uses_instance_divisors ||
4628 v->uses_instance_divisors || /* we don't check which divisors changed */
4629 memcmp(old->fix_fetch, v->fix_fetch, sizeof(v->fix_fetch[0]) * v->count)))
4630 sctx->do_update_shaders = true;
4631
4632 if (v && v->instance_divisor_is_fetched) {
4633 struct pipe_constant_buffer cb;
4634
4635 cb.buffer = &v->instance_divisor_factor_buffer->b.b;
4636 cb.user_buffer = NULL;
4637 cb.buffer_offset = 0;
4638 cb.buffer_size = 0xffffffff;
4639 si_set_rw_buffer(sctx, SI_VS_CONST_INSTANCE_DIVISORS, &cb);
4640 }
4641 }
4642
4643 static void si_delete_vertex_element(struct pipe_context *ctx, void *state)
4644 {
4645 struct si_context *sctx = (struct si_context *)ctx;
4646 struct si_vertex_elements *v = (struct si_vertex_elements*)state;
4647
4648 if (sctx->vertex_elements == state)
4649 sctx->vertex_elements = NULL;
4650 si_resource_reference(&v->instance_divisor_factor_buffer, NULL);
4651 FREE(state);
4652 }
4653
4654 static void si_set_vertex_buffers(struct pipe_context *ctx,
4655 unsigned start_slot, unsigned count,
4656 const struct pipe_vertex_buffer *buffers)
4657 {
4658 struct si_context *sctx = (struct si_context *)ctx;
4659 struct pipe_vertex_buffer *dst = sctx->vertex_buffer + start_slot;
4660 int i;
4661
4662 assert(start_slot + count <= ARRAY_SIZE(sctx->vertex_buffer));
4663
4664 if (buffers) {
4665 for (i = 0; i < count; i++) {
4666 const struct pipe_vertex_buffer *src = buffers + i;
4667 struct pipe_vertex_buffer *dsti = dst + i;
4668 struct pipe_resource *buf = src->buffer.resource;
4669
4670 pipe_resource_reference(&dsti->buffer.resource, buf);
4671 dsti->buffer_offset = src->buffer_offset;
4672 dsti->stride = src->stride;
4673 si_context_add_resource_size(sctx, buf);
4674 if (buf)
4675 si_resource(buf)->bind_history |= PIPE_BIND_VERTEX_BUFFER;
4676 }
4677 } else {
4678 for (i = 0; i < count; i++) {
4679 pipe_resource_reference(&dst[i].buffer.resource, NULL);
4680 }
4681 }
4682 sctx->vertex_buffers_dirty = true;
4683 }
4684
4685 /*
4686 * Misc
4687 */
4688
4689 static void si_set_tess_state(struct pipe_context *ctx,
4690 const float default_outer_level[4],
4691 const float default_inner_level[2])
4692 {
4693 struct si_context *sctx = (struct si_context *)ctx;
4694 struct pipe_constant_buffer cb;
4695 float array[8];
4696
4697 memcpy(array, default_outer_level, sizeof(float) * 4);
4698 memcpy(array+4, default_inner_level, sizeof(float) * 2);
4699
4700 cb.buffer = NULL;
4701 cb.user_buffer = NULL;
4702 cb.buffer_size = sizeof(array);
4703
4704 si_upload_const_buffer(sctx, (struct si_resource**)&cb.buffer,
4705 (void*)array, sizeof(array),
4706 &cb.buffer_offset);
4707
4708 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &cb);
4709 pipe_resource_reference(&cb.buffer, NULL);
4710 }
4711
4712 static void si_texture_barrier(struct pipe_context *ctx, unsigned flags)
4713 {
4714 struct si_context *sctx = (struct si_context *)ctx;
4715
4716 si_update_fb_dirtiness_after_rendering(sctx);
4717
4718 /* Multisample surfaces are flushed in si_decompress_textures. */
4719 if (sctx->framebuffer.uncompressed_cb_mask) {
4720 si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
4721 sctx->framebuffer.CB_has_shader_readable_metadata,
4722 sctx->framebuffer.all_DCC_pipe_aligned);
4723 }
4724 }
4725
4726 /* This only ensures coherency for shader image/buffer stores. */
4727 void si_memory_barrier(struct pipe_context *ctx, unsigned flags)
4728 {
4729 struct si_context *sctx = (struct si_context *)ctx;
4730
4731 if (!(flags & ~PIPE_BARRIER_UPDATE))
4732 return;
4733
4734 /* Subsequent commands must wait for all shader invocations to
4735 * complete. */
4736 sctx->flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
4737 SI_CONTEXT_CS_PARTIAL_FLUSH;
4738
4739 if (flags & PIPE_BARRIER_CONSTANT_BUFFER)
4740 sctx->flags |= SI_CONTEXT_INV_SMEM_L1 |
4741 SI_CONTEXT_INV_VMEM_L1;
4742
4743 if (flags & (PIPE_BARRIER_VERTEX_BUFFER |
4744 PIPE_BARRIER_SHADER_BUFFER |
4745 PIPE_BARRIER_TEXTURE |
4746 PIPE_BARRIER_IMAGE |
4747 PIPE_BARRIER_STREAMOUT_BUFFER |
4748 PIPE_BARRIER_GLOBAL_BUFFER)) {
4749 /* As far as I can tell, L1 contents are written back to L2
4750 * automatically at end of shader, but the contents of other
4751 * L1 caches might still be stale. */
4752 sctx->flags |= SI_CONTEXT_INV_VMEM_L1;
4753 }
4754
4755 if (flags & PIPE_BARRIER_INDEX_BUFFER) {
4756 /* Indices are read through TC L2 since VI.
4757 * L1 isn't used.
4758 */
4759 if (sctx->screen->info.chip_class <= CIK)
4760 sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
4761 }
4762
4763 /* MSAA color, any depth and any stencil are flushed in
4764 * si_decompress_textures when needed.
4765 */
4766 if (flags & PIPE_BARRIER_FRAMEBUFFER &&
4767 sctx->framebuffer.uncompressed_cb_mask) {
4768 sctx->flags |= SI_CONTEXT_FLUSH_AND_INV_CB;
4769
4770 if (sctx->chip_class <= VI)
4771 sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
4772 }
4773
4774 /* Indirect buffers use TC L2 on GFX9, but not older hw. */
4775 if (sctx->screen->info.chip_class <= VI &&
4776 flags & PIPE_BARRIER_INDIRECT_BUFFER)
4777 sctx->flags |= SI_CONTEXT_WRITEBACK_GLOBAL_L2;
4778 }
4779
4780 static void *si_create_blend_custom(struct si_context *sctx, unsigned mode)
4781 {
4782 struct pipe_blend_state blend;
4783
4784 memset(&blend, 0, sizeof(blend));
4785 blend.independent_blend_enable = true;
4786 blend.rt[0].colormask = 0xf;
4787 return si_create_blend_state_mode(&sctx->b, &blend, mode);
4788 }
4789
4790 static void si_init_config(struct si_context *sctx);
4791
4792 void si_init_state_functions(struct si_context *sctx)
4793 {
4794 sctx->atoms.s.framebuffer.emit = si_emit_framebuffer_state;
4795 sctx->atoms.s.msaa_sample_locs.emit = si_emit_msaa_sample_locs;
4796 sctx->atoms.s.db_render_state.emit = si_emit_db_render_state;
4797 sctx->atoms.s.dpbb_state.emit = si_emit_dpbb_state;
4798 sctx->atoms.s.msaa_config.emit = si_emit_msaa_config;
4799 sctx->atoms.s.sample_mask.emit = si_emit_sample_mask;
4800 sctx->atoms.s.cb_render_state.emit = si_emit_cb_render_state;
4801 sctx->atoms.s.blend_color.emit = si_emit_blend_color;
4802 sctx->atoms.s.clip_regs.emit = si_emit_clip_regs;
4803 sctx->atoms.s.clip_state.emit = si_emit_clip_state;
4804 sctx->atoms.s.stencil_ref.emit = si_emit_stencil_ref;
4805
4806 sctx->b.create_blend_state = si_create_blend_state;
4807 sctx->b.bind_blend_state = si_bind_blend_state;
4808 sctx->b.delete_blend_state = si_delete_blend_state;
4809 sctx->b.set_blend_color = si_set_blend_color;
4810
4811 sctx->b.create_rasterizer_state = si_create_rs_state;
4812 sctx->b.bind_rasterizer_state = si_bind_rs_state;
4813 sctx->b.delete_rasterizer_state = si_delete_rs_state;
4814
4815 sctx->b.create_depth_stencil_alpha_state = si_create_dsa_state;
4816 sctx->b.bind_depth_stencil_alpha_state = si_bind_dsa_state;
4817 sctx->b.delete_depth_stencil_alpha_state = si_delete_dsa_state;
4818
4819 sctx->custom_dsa_flush = si_create_db_flush_dsa(sctx);
4820 sctx->custom_blend_resolve = si_create_blend_custom(sctx, V_028808_CB_RESOLVE);
4821 sctx->custom_blend_fmask_decompress = si_create_blend_custom(sctx, V_028808_CB_FMASK_DECOMPRESS);
4822 sctx->custom_blend_eliminate_fastclear = si_create_blend_custom(sctx, V_028808_CB_ELIMINATE_FAST_CLEAR);
4823 sctx->custom_blend_dcc_decompress = si_create_blend_custom(sctx, V_028808_CB_DCC_DECOMPRESS);
4824
4825 sctx->b.set_clip_state = si_set_clip_state;
4826 sctx->b.set_stencil_ref = si_set_stencil_ref;
4827
4828 sctx->b.set_framebuffer_state = si_set_framebuffer_state;
4829
4830 sctx->b.create_sampler_state = si_create_sampler_state;
4831 sctx->b.delete_sampler_state = si_delete_sampler_state;
4832
4833 sctx->b.create_sampler_view = si_create_sampler_view;
4834 sctx->b.sampler_view_destroy = si_sampler_view_destroy;
4835
4836 sctx->b.set_sample_mask = si_set_sample_mask;
4837
4838 sctx->b.create_vertex_elements_state = si_create_vertex_elements;
4839 sctx->b.bind_vertex_elements_state = si_bind_vertex_elements;
4840 sctx->b.delete_vertex_elements_state = si_delete_vertex_element;
4841 sctx->b.set_vertex_buffers = si_set_vertex_buffers;
4842
4843 sctx->b.texture_barrier = si_texture_barrier;
4844 sctx->b.set_min_samples = si_set_min_samples;
4845 sctx->b.set_tess_state = si_set_tess_state;
4846
4847 sctx->b.set_active_query_state = si_set_active_query_state;
4848
4849 si_init_config(sctx);
4850 }
4851
4852 void si_init_screen_state_functions(struct si_screen *sscreen)
4853 {
4854 sscreen->b.is_format_supported = si_is_format_supported;
4855 }
4856
4857 static void si_set_grbm_gfx_index(struct si_context *sctx,
4858 struct si_pm4_state *pm4, unsigned value)
4859 {
4860 unsigned reg = sctx->chip_class >= CIK ? R_030800_GRBM_GFX_INDEX :
4861 R_00802C_GRBM_GFX_INDEX;
4862 si_pm4_set_reg(pm4, reg, value);
4863 }
4864
4865 static void si_set_grbm_gfx_index_se(struct si_context *sctx,
4866 struct si_pm4_state *pm4, unsigned se)
4867 {
4868 assert(se == ~0 || se < sctx->screen->info.max_se);
4869 si_set_grbm_gfx_index(sctx, pm4,
4870 (se == ~0 ? S_030800_SE_BROADCAST_WRITES(1) :
4871 S_030800_SE_INDEX(se)) |
4872 S_030800_SH_BROADCAST_WRITES(1) |
4873 S_030800_INSTANCE_BROADCAST_WRITES(1));
4874 }
4875
4876 static void
4877 si_write_harvested_raster_configs(struct si_context *sctx,
4878 struct si_pm4_state *pm4,
4879 unsigned raster_config,
4880 unsigned raster_config_1)
4881 {
4882 unsigned num_se = MAX2(sctx->screen->info.max_se, 1);
4883 unsigned raster_config_se[4];
4884 unsigned se;
4885
4886 ac_get_harvested_configs(&sctx->screen->info,
4887 raster_config,
4888 &raster_config_1,
4889 raster_config_se);
4890
4891 for (se = 0; se < num_se; se++) {
4892 si_set_grbm_gfx_index_se(sctx, pm4, se);
4893 si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG, raster_config_se[se]);
4894 }
4895 si_set_grbm_gfx_index(sctx, pm4, ~0);
4896
4897 if (sctx->chip_class >= CIK) {
4898 si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1, raster_config_1);
4899 }
4900 }
4901
4902 static void si_set_raster_config(struct si_context *sctx, struct si_pm4_state *pm4)
4903 {
4904 struct si_screen *sscreen = sctx->screen;
4905 unsigned num_rb = MIN2(sscreen->info.num_render_backends, 16);
4906 unsigned rb_mask = sscreen->info.enabled_rb_mask;
4907 unsigned raster_config = sscreen->pa_sc_raster_config;
4908 unsigned raster_config_1 = sscreen->pa_sc_raster_config_1;
4909
4910 if (!rb_mask || util_bitcount(rb_mask) >= num_rb) {
4911 /* Always use the default config when all backends are enabled
4912 * (or when we failed to determine the enabled backends).
4913 */
4914 si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG,
4915 raster_config);
4916 if (sctx->chip_class >= CIK)
4917 si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1,
4918 raster_config_1);
4919 } else {
4920 si_write_harvested_raster_configs(sctx, pm4, raster_config, raster_config_1);
4921 }
4922 }
4923
4924 static void si_init_config(struct si_context *sctx)
4925 {
4926 struct si_screen *sscreen = sctx->screen;
4927 uint64_t border_color_va = sctx->border_color_buffer->gpu_address;
4928 bool has_clear_state = sscreen->has_clear_state;
4929 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
4930
4931 /* SI, radeon kernel disabled CLEAR_STATE. */
4932 assert(has_clear_state || sscreen->info.chip_class == SI ||
4933 sscreen->info.drm_major != 3);
4934
4935 if (!pm4)
4936 return;
4937
4938 si_pm4_cmd_begin(pm4, PKT3_CONTEXT_CONTROL);
4939 si_pm4_cmd_add(pm4, CONTEXT_CONTROL_LOAD_ENABLE(1));
4940 si_pm4_cmd_add(pm4, CONTEXT_CONTROL_SHADOW_ENABLE(1));
4941 si_pm4_cmd_end(pm4, false);
4942
4943 if (has_clear_state) {
4944 si_pm4_cmd_begin(pm4, PKT3_CLEAR_STATE);
4945 si_pm4_cmd_add(pm4, 0);
4946 si_pm4_cmd_end(pm4, false);
4947 }
4948
4949 if (sctx->chip_class <= VI)
4950 si_set_raster_config(sctx, pm4);
4951
4952 si_pm4_set_reg(pm4, R_028A18_VGT_HOS_MAX_TESS_LEVEL, fui(64));
4953 if (!has_clear_state)
4954 si_pm4_set_reg(pm4, R_028A1C_VGT_HOS_MIN_TESS_LEVEL, fui(0));
4955
4956 /* FIXME calculate these values somehow ??? */
4957 if (sctx->chip_class <= VI) {
4958 si_pm4_set_reg(pm4, R_028A54_VGT_GS_PER_ES, SI_GS_PER_ES);
4959 si_pm4_set_reg(pm4, R_028A58_VGT_ES_PER_GS, 0x40);
4960 }
4961
4962 if (!has_clear_state) {
4963 si_pm4_set_reg(pm4, R_028A5C_VGT_GS_PER_VS, 0x2);
4964 si_pm4_set_reg(pm4, R_028A8C_VGT_PRIMITIVEID_RESET, 0x0);
4965 si_pm4_set_reg(pm4, R_028B98_VGT_STRMOUT_BUFFER_CONFIG, 0x0);
4966 }
4967
4968 si_pm4_set_reg(pm4, R_028AA0_VGT_INSTANCE_STEP_RATE_0, 1);
4969 if (!has_clear_state)
4970 si_pm4_set_reg(pm4, R_028AB8_VGT_VTX_CNT_EN, 0x0);
4971 if (sctx->chip_class < CIK)
4972 si_pm4_set_reg(pm4, R_008A14_PA_CL_ENHANCE, S_008A14_NUM_CLIP_SEQ(3) |
4973 S_008A14_CLIP_VTX_REORDER_ENA(1));
4974
4975 /* CLEAR_STATE doesn't clear these correctly on certain generations.
4976 * I don't know why. Deduced by trial and error.
4977 */
4978 if (sctx->chip_class <= CIK) {
4979 si_pm4_set_reg(pm4, R_028B28_VGT_STRMOUT_DRAW_OPAQUE_OFFSET, 0);
4980 si_pm4_set_reg(pm4, R_028204_PA_SC_WINDOW_SCISSOR_TL, S_028204_WINDOW_OFFSET_DISABLE(1));
4981 si_pm4_set_reg(pm4, R_028240_PA_SC_GENERIC_SCISSOR_TL, S_028240_WINDOW_OFFSET_DISABLE(1));
4982 si_pm4_set_reg(pm4, R_028244_PA_SC_GENERIC_SCISSOR_BR,
4983 S_028244_BR_X(16384) | S_028244_BR_Y(16384));
4984 si_pm4_set_reg(pm4, R_028030_PA_SC_SCREEN_SCISSOR_TL, 0);
4985 si_pm4_set_reg(pm4, R_028034_PA_SC_SCREEN_SCISSOR_BR,
4986 S_028034_BR_X(16384) | S_028034_BR_Y(16384));
4987 }
4988
4989 if (!has_clear_state) {
4990 si_pm4_set_reg(pm4, R_028230_PA_SC_EDGERULE,
4991 S_028230_ER_TRI(0xA) |
4992 S_028230_ER_POINT(0xA) |
4993 S_028230_ER_RECT(0xA) |
4994 /* Required by DX10_DIAMOND_TEST_ENA: */
4995 S_028230_ER_LINE_LR(0x1A) |
4996 S_028230_ER_LINE_RL(0x26) |
4997 S_028230_ER_LINE_TB(0xA) |
4998 S_028230_ER_LINE_BT(0xA));
4999 si_pm4_set_reg(pm4, R_028820_PA_CL_NANINF_CNTL, 0);
5000 si_pm4_set_reg(pm4, R_028AC0_DB_SRESULTS_COMPARE_STATE0, 0x0);
5001 si_pm4_set_reg(pm4, R_028AC4_DB_SRESULTS_COMPARE_STATE1, 0x0);
5002 si_pm4_set_reg(pm4, R_028AC8_DB_PRELOAD_CONTROL, 0x0);
5003 si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE, 0);
5004 }
5005
5006 if (sctx->chip_class >= GFX9) {
5007 si_pm4_set_reg(pm4, R_030920_VGT_MAX_VTX_INDX, ~0);
5008 si_pm4_set_reg(pm4, R_030924_VGT_MIN_VTX_INDX, 0);
5009 si_pm4_set_reg(pm4, R_030928_VGT_INDX_OFFSET, 0);
5010 } else {
5011 /* These registers, when written, also overwrite the CLEAR_STATE
5012 * context, so we can't rely on CLEAR_STATE setting them.
5013 * It would be an issue if there was another UMD changing them.
5014 */
5015 si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
5016 si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
5017 si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET, 0);
5018 }
5019
5020 if (sctx->chip_class >= CIK) {
5021 if (sctx->chip_class >= GFX9) {
5022 si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS,
5023 S_00B41C_CU_EN(0xffff) | S_00B41C_WAVE_LIMIT(0x3F));
5024 } else {
5025 si_pm4_set_reg(pm4, R_00B51C_SPI_SHADER_PGM_RSRC3_LS,
5026 S_00B51C_CU_EN(0xffff) | S_00B51C_WAVE_LIMIT(0x3F));
5027 si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS,
5028 S_00B41C_WAVE_LIMIT(0x3F));
5029 si_pm4_set_reg(pm4, R_00B31C_SPI_SHADER_PGM_RSRC3_ES,
5030 S_00B31C_CU_EN(0xffff) | S_00B31C_WAVE_LIMIT(0x3F));
5031
5032 /* If this is 0, Bonaire can hang even if GS isn't being used.
5033 * Other chips are unaffected. These are suboptimal values,
5034 * but we don't use on-chip GS.
5035 */
5036 si_pm4_set_reg(pm4, R_028A44_VGT_GS_ONCHIP_CNTL,
5037 S_028A44_ES_VERTS_PER_SUBGRP(64) |
5038 S_028A44_GS_PRIMS_PER_SUBGRP(4));
5039 }
5040 si_pm4_set_reg(pm4, R_00B21C_SPI_SHADER_PGM_RSRC3_GS,
5041 S_00B21C_CU_EN(0xffff) | S_00B21C_WAVE_LIMIT(0x3F));
5042
5043 /* Compute LATE_ALLOC_VS.LIMIT. */
5044 unsigned num_cu_per_sh = sscreen->info.num_good_cu_per_sh;
5045 unsigned late_alloc_limit; /* The limit is per SH. */
5046
5047 if (sctx->family == CHIP_KABINI) {
5048 late_alloc_limit = 0; /* Potential hang on Kabini. */
5049 } else if (num_cu_per_sh <= 4) {
5050 /* Too few available compute units per SH. Disallowing
5051 * VS to run on one CU could hurt us more than late VS
5052 * allocation would help.
5053 *
5054 * 2 is the highest safe number that allows us to keep
5055 * all CUs enabled.
5056 */
5057 late_alloc_limit = 2;
5058 } else {
5059 /* This is a good initial value, allowing 1 late_alloc
5060 * wave per SIMD on num_cu - 2.
5061 */
5062 late_alloc_limit = (num_cu_per_sh - 2) * 4;
5063
5064 /* The limit is 0-based, so 0 means 1. */
5065 assert(late_alloc_limit > 0 && late_alloc_limit <= 64);
5066 late_alloc_limit -= 1;
5067 }
5068
5069 /* VS can't execute on one CU if the limit is > 2. */
5070 si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS,
5071 S_00B118_CU_EN(late_alloc_limit > 2 ? 0xfffe : 0xffff) |
5072 S_00B118_WAVE_LIMIT(0x3F));
5073 si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS,
5074 S_00B11C_LIMIT(late_alloc_limit));
5075 si_pm4_set_reg(pm4, R_00B01C_SPI_SHADER_PGM_RSRC3_PS,
5076 S_00B01C_CU_EN(0xffff) | S_00B01C_WAVE_LIMIT(0x3F));
5077 }
5078
5079 if (sctx->chip_class >= VI) {
5080 unsigned vgt_tess_distribution;
5081
5082 vgt_tess_distribution =
5083 S_028B50_ACCUM_ISOLINE(32) |
5084 S_028B50_ACCUM_TRI(11) |
5085 S_028B50_ACCUM_QUAD(11) |
5086 S_028B50_DONUT_SPLIT(16);
5087
5088 /* Testing with Unigine Heaven extreme tesselation yielded best results
5089 * with TRAP_SPLIT = 3.
5090 */
5091 if (sctx->family == CHIP_FIJI ||
5092 sctx->family >= CHIP_POLARIS10)
5093 vgt_tess_distribution |= S_028B50_TRAP_SPLIT(3);
5094
5095 si_pm4_set_reg(pm4, R_028B50_VGT_TESS_DISTRIBUTION, vgt_tess_distribution);
5096 } else if (!has_clear_state) {
5097 si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 14);
5098 si_pm4_set_reg(pm4, R_028C5C_VGT_OUT_DEALLOC_CNTL, 16);
5099 }
5100
5101 si_pm4_set_reg(pm4, R_028080_TA_BC_BASE_ADDR, border_color_va >> 8);
5102 if (sctx->chip_class >= CIK) {
5103 si_pm4_set_reg(pm4, R_028084_TA_BC_BASE_ADDR_HI,
5104 S_028084_ADDRESS(border_color_va >> 40));
5105 }
5106 si_pm4_add_bo(pm4, sctx->border_color_buffer, RADEON_USAGE_READ,
5107 RADEON_PRIO_BORDER_COLORS);
5108
5109 if (sctx->chip_class >= GFX9) {
5110 unsigned num_se = sscreen->info.max_se;
5111 unsigned pc_lines = 0;
5112
5113 switch (sctx->family) {
5114 case CHIP_VEGA10:
5115 case CHIP_VEGA12:
5116 case CHIP_VEGA20:
5117 pc_lines = 4096;
5118 break;
5119 case CHIP_RAVEN:
5120 case CHIP_RAVEN2:
5121 pc_lines = 1024;
5122 break;
5123 default:
5124 assert(0);
5125 }
5126
5127 si_pm4_set_reg(pm4, R_028C48_PA_SC_BINNER_CNTL_1,
5128 S_028C48_MAX_ALLOC_COUNT(MIN2(128, pc_lines / (4 * num_se))) |
5129 S_028C48_MAX_PRIM_PER_BATCH(1023));
5130 si_pm4_set_reg(pm4, R_028C4C_PA_SC_CONSERVATIVE_RASTERIZATION_CNTL,
5131 S_028C4C_NULL_SQUAD_AA_MASK_ENABLE(1));
5132 si_pm4_set_reg(pm4, R_030968_VGT_INSTANCE_BASE_ID, 0);
5133 }
5134
5135 si_pm4_upload_indirect_buffer(sctx, pm4);
5136 sctx->init_config = pm4;
5137 }