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