radeonsi/gfx10: rewrite late alloc computation
[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/format/u_format.h"
31 #include "util/format/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 blend->cb_target_mask;
87 unsigned i;
88
89 /* Avoid a hang that happens when dual source blending is enabled
90 * but there is not enough color outputs. This is undefined behavior,
91 * so disable color writes completely.
92 *
93 * Reproducible with Unigine Heaven 4.0 and drirc missing.
94 */
95 if (blend->dual_src_blend &&
96 sctx->ps_shader.cso &&
97 (sctx->ps_shader.cso->info.colors_written & 0x3) != 0x3)
98 cb_target_mask = 0;
99
100 /* GFX9: Flush DFSM when CB_TARGET_MASK changes.
101 * I think we don't have to do anything between IBs.
102 */
103 if (sctx->screen->dpbb_allowed &&
104 sctx->last_cb_target_mask != cb_target_mask) {
105 sctx->last_cb_target_mask = cb_target_mask;
106
107 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
108 radeon_emit(cs, EVENT_TYPE(V_028A90_BREAK_BATCH) | EVENT_INDEX(0));
109 }
110
111 unsigned initial_cdw = cs->current.cdw;
112 radeon_opt_set_context_reg(sctx, R_028238_CB_TARGET_MASK,
113 SI_TRACKED_CB_TARGET_MASK, cb_target_mask);
114
115 if (sctx->chip_class >= GFX8) {
116 /* DCC MSAA workaround.
117 * Alternatively, we can set CB_COLORi_DCC_CONTROL.OVERWRITE_-
118 * COMBINER_DISABLE, but that would be more complicated.
119 */
120 bool oc_disable = blend->dcc_msaa_corruption_4bit & cb_target_mask &&
121 sctx->framebuffer.nr_samples >= 2;
122 unsigned watermark = sctx->framebuffer.dcc_overwrite_combiner_watermark;
123
124 radeon_opt_set_context_reg(
125 sctx, R_028424_CB_DCC_CONTROL,
126 SI_TRACKED_CB_DCC_CONTROL,
127 S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(sctx->chip_class <= GFX9) |
128 S_028424_OVERWRITE_COMBINER_WATERMARK(watermark) |
129 S_028424_OVERWRITE_COMBINER_DISABLE(oc_disable) |
130 S_028424_DISABLE_CONSTANT_ENCODE_REG(sctx->screen->info.has_dcc_constant_encode));
131 }
132
133 /* RB+ register settings. */
134 if (sctx->screen->info.rbplus_allowed) {
135 unsigned spi_shader_col_format =
136 sctx->ps_shader.cso ?
137 sctx->ps_shader.current->key.part.ps.epilog.spi_shader_col_format : 0;
138 unsigned sx_ps_downconvert = 0;
139 unsigned sx_blend_opt_epsilon = 0;
140 unsigned sx_blend_opt_control = 0;
141
142 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
143 struct si_surface *surf =
144 (struct si_surface*)sctx->framebuffer.state.cbufs[i];
145 unsigned format, swap, spi_format, colormask;
146 bool has_alpha, has_rgb;
147
148 if (!surf) {
149 /* If the color buffer is not set, the driver sets 32_R
150 * as the SPI color format, because the hw doesn't allow
151 * holes between color outputs, so also set this to
152 * enable RB+.
153 */
154 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_R << (i * 4);
155 continue;
156 }
157
158 format = G_028C70_FORMAT(surf->cb_color_info);
159 swap = G_028C70_COMP_SWAP(surf->cb_color_info);
160 spi_format = (spi_shader_col_format >> (i * 4)) & 0xf;
161 colormask = (cb_target_mask >> (i * 4)) & 0xf;
162
163 /* Set if RGB and A are present. */
164 has_alpha = !G_028C74_FORCE_DST_ALPHA_1(surf->cb_color_attrib);
165
166 if (format == V_028C70_COLOR_8 ||
167 format == V_028C70_COLOR_16 ||
168 format == V_028C70_COLOR_32)
169 has_rgb = !has_alpha;
170 else
171 has_rgb = true;
172
173 /* Check the colormask and export format. */
174 if (!(colormask & (PIPE_MASK_RGBA & ~PIPE_MASK_A)))
175 has_rgb = false;
176 if (!(colormask & PIPE_MASK_A))
177 has_alpha = false;
178
179 if (spi_format == V_028714_SPI_SHADER_ZERO) {
180 has_rgb = false;
181 has_alpha = false;
182 }
183
184 /* Disable value checking for disabled channels. */
185 if (!has_rgb)
186 sx_blend_opt_control |= S_02875C_MRT0_COLOR_OPT_DISABLE(1) << (i * 4);
187 if (!has_alpha)
188 sx_blend_opt_control |= S_02875C_MRT0_ALPHA_OPT_DISABLE(1) << (i * 4);
189
190 /* Enable down-conversion for 32bpp and smaller formats. */
191 switch (format) {
192 case V_028C70_COLOR_8:
193 case V_028C70_COLOR_8_8:
194 case V_028C70_COLOR_8_8_8_8:
195 /* For 1 and 2-channel formats, use the superset thereof. */
196 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR ||
197 spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
198 spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
199 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_8_8_8_8 << (i * 4);
200 sx_blend_opt_epsilon |= V_028758_8BIT_FORMAT << (i * 4);
201 }
202 break;
203
204 case V_028C70_COLOR_5_6_5:
205 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
206 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_5_6_5 << (i * 4);
207 sx_blend_opt_epsilon |= V_028758_6BIT_FORMAT << (i * 4);
208 }
209 break;
210
211 case V_028C70_COLOR_1_5_5_5:
212 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
213 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_1_5_5_5 << (i * 4);
214 sx_blend_opt_epsilon |= V_028758_5BIT_FORMAT << (i * 4);
215 }
216 break;
217
218 case V_028C70_COLOR_4_4_4_4:
219 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR) {
220 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_4_4_4_4 << (i * 4);
221 sx_blend_opt_epsilon |= V_028758_4BIT_FORMAT << (i * 4);
222 }
223 break;
224
225 case V_028C70_COLOR_32:
226 if (swap == V_028C70_SWAP_STD &&
227 spi_format == V_028714_SPI_SHADER_32_R)
228 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_R << (i * 4);
229 else if (swap == V_028C70_SWAP_ALT_REV &&
230 spi_format == V_028714_SPI_SHADER_32_AR)
231 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_32_A << (i * 4);
232 break;
233
234 case V_028C70_COLOR_16:
235 case V_028C70_COLOR_16_16:
236 /* For 1-channel formats, use the superset thereof. */
237 if (spi_format == V_028714_SPI_SHADER_UNORM16_ABGR ||
238 spi_format == V_028714_SPI_SHADER_SNORM16_ABGR ||
239 spi_format == V_028714_SPI_SHADER_UINT16_ABGR ||
240 spi_format == V_028714_SPI_SHADER_SINT16_ABGR) {
241 if (swap == V_028C70_SWAP_STD ||
242 swap == V_028C70_SWAP_STD_REV)
243 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_GR << (i * 4);
244 else
245 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_16_16_AR << (i * 4);
246 }
247 break;
248
249 case V_028C70_COLOR_10_11_11:
250 if (spi_format == V_028714_SPI_SHADER_FP16_ABGR)
251 sx_ps_downconvert |= V_028754_SX_RT_EXPORT_10_11_11 << (i * 4);
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 /* If there are no color outputs, the first color export is
264 * always enabled as 32_R, so also set this to enable RB+.
265 */
266 if (!sx_ps_downconvert)
267 sx_ps_downconvert = V_028754_SX_RT_EXPORT_32_R;
268
269 /* SX_PS_DOWNCONVERT, SX_BLEND_OPT_EPSILON, SX_BLEND_OPT_CONTROL */
270 radeon_opt_set_context_reg3(sctx, R_028754_SX_PS_DOWNCONVERT,
271 SI_TRACKED_SX_PS_DOWNCONVERT,
272 sx_ps_downconvert, sx_blend_opt_epsilon,
273 sx_blend_opt_control);
274 }
275 if (initial_cdw != cs->current.cdw)
276 sctx->context_roll = true;
277 }
278
279 /*
280 * Blender functions
281 */
282
283 static uint32_t si_translate_blend_function(int blend_func)
284 {
285 switch (blend_func) {
286 case PIPE_BLEND_ADD:
287 return V_028780_COMB_DST_PLUS_SRC;
288 case PIPE_BLEND_SUBTRACT:
289 return V_028780_COMB_SRC_MINUS_DST;
290 case PIPE_BLEND_REVERSE_SUBTRACT:
291 return V_028780_COMB_DST_MINUS_SRC;
292 case PIPE_BLEND_MIN:
293 return V_028780_COMB_MIN_DST_SRC;
294 case PIPE_BLEND_MAX:
295 return V_028780_COMB_MAX_DST_SRC;
296 default:
297 PRINT_ERR("Unknown blend function %d\n", blend_func);
298 assert(0);
299 break;
300 }
301 return 0;
302 }
303
304 static uint32_t si_translate_blend_factor(int blend_fact)
305 {
306 switch (blend_fact) {
307 case PIPE_BLENDFACTOR_ONE:
308 return V_028780_BLEND_ONE;
309 case PIPE_BLENDFACTOR_SRC_COLOR:
310 return V_028780_BLEND_SRC_COLOR;
311 case PIPE_BLENDFACTOR_SRC_ALPHA:
312 return V_028780_BLEND_SRC_ALPHA;
313 case PIPE_BLENDFACTOR_DST_ALPHA:
314 return V_028780_BLEND_DST_ALPHA;
315 case PIPE_BLENDFACTOR_DST_COLOR:
316 return V_028780_BLEND_DST_COLOR;
317 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
318 return V_028780_BLEND_SRC_ALPHA_SATURATE;
319 case PIPE_BLENDFACTOR_CONST_COLOR:
320 return V_028780_BLEND_CONSTANT_COLOR;
321 case PIPE_BLENDFACTOR_CONST_ALPHA:
322 return V_028780_BLEND_CONSTANT_ALPHA;
323 case PIPE_BLENDFACTOR_ZERO:
324 return V_028780_BLEND_ZERO;
325 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
326 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
327 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
328 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
329 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
330 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
331 case PIPE_BLENDFACTOR_INV_DST_COLOR:
332 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
333 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
334 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
335 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
336 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
337 case PIPE_BLENDFACTOR_SRC1_COLOR:
338 return V_028780_BLEND_SRC1_COLOR;
339 case PIPE_BLENDFACTOR_SRC1_ALPHA:
340 return V_028780_BLEND_SRC1_ALPHA;
341 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
342 return V_028780_BLEND_INV_SRC1_COLOR;
343 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
344 return V_028780_BLEND_INV_SRC1_ALPHA;
345 default:
346 PRINT_ERR("Bad blend factor %d not supported!\n", blend_fact);
347 assert(0);
348 break;
349 }
350 return 0;
351 }
352
353 static uint32_t si_translate_blend_opt_function(int blend_func)
354 {
355 switch (blend_func) {
356 case PIPE_BLEND_ADD:
357 return V_028760_OPT_COMB_ADD;
358 case PIPE_BLEND_SUBTRACT:
359 return V_028760_OPT_COMB_SUBTRACT;
360 case PIPE_BLEND_REVERSE_SUBTRACT:
361 return V_028760_OPT_COMB_REVSUBTRACT;
362 case PIPE_BLEND_MIN:
363 return V_028760_OPT_COMB_MIN;
364 case PIPE_BLEND_MAX:
365 return V_028760_OPT_COMB_MAX;
366 default:
367 return V_028760_OPT_COMB_BLEND_DISABLED;
368 }
369 }
370
371 static uint32_t si_translate_blend_opt_factor(int blend_fact, bool is_alpha)
372 {
373 switch (blend_fact) {
374 case PIPE_BLENDFACTOR_ZERO:
375 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
376 case PIPE_BLENDFACTOR_ONE:
377 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
378 case PIPE_BLENDFACTOR_SRC_COLOR:
379 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
380 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
381 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
382 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
383 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
384 case PIPE_BLENDFACTOR_SRC_ALPHA:
385 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
386 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
387 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
388 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
389 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
390 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
391 default:
392 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
393 }
394 }
395
396 static void si_blend_check_commutativity(struct si_screen *sscreen,
397 struct si_state_blend *blend,
398 enum pipe_blend_func func,
399 enum pipe_blendfactor src,
400 enum pipe_blendfactor dst,
401 unsigned chanmask)
402 {
403 /* Src factor is allowed when it does not depend on Dst */
404 static const uint32_t src_allowed =
405 (1u << PIPE_BLENDFACTOR_ONE) |
406 (1u << PIPE_BLENDFACTOR_SRC_COLOR) |
407 (1u << PIPE_BLENDFACTOR_SRC_ALPHA) |
408 (1u << PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) |
409 (1u << PIPE_BLENDFACTOR_CONST_COLOR) |
410 (1u << PIPE_BLENDFACTOR_CONST_ALPHA) |
411 (1u << PIPE_BLENDFACTOR_SRC1_COLOR) |
412 (1u << PIPE_BLENDFACTOR_SRC1_ALPHA) |
413 (1u << PIPE_BLENDFACTOR_ZERO) |
414 (1u << PIPE_BLENDFACTOR_INV_SRC_COLOR) |
415 (1u << PIPE_BLENDFACTOR_INV_SRC_ALPHA) |
416 (1u << PIPE_BLENDFACTOR_INV_CONST_COLOR) |
417 (1u << PIPE_BLENDFACTOR_INV_CONST_ALPHA) |
418 (1u << PIPE_BLENDFACTOR_INV_SRC1_COLOR) |
419 (1u << PIPE_BLENDFACTOR_INV_SRC1_ALPHA);
420
421 if (dst == PIPE_BLENDFACTOR_ONE &&
422 (src_allowed & (1u << src))) {
423 /* Addition is commutative, but floating point addition isn't
424 * associative: subtle changes can be introduced via different
425 * rounding.
426 *
427 * Out-of-order is also non-deterministic, which means that
428 * this breaks OpenGL invariance requirements. So only enable
429 * out-of-order additive blending if explicitly allowed by a
430 * setting.
431 */
432 if (func == PIPE_BLEND_MAX || func == PIPE_BLEND_MIN ||
433 (func == PIPE_BLEND_ADD && sscreen->commutative_blend_add))
434 blend->commutative_4bit |= chanmask;
435 }
436 }
437
438 /**
439 * Get rid of DST in the blend factors by commuting the operands:
440 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
441 */
442 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
443 unsigned *dst_factor, unsigned expected_dst,
444 unsigned replacement_src)
445 {
446 if (*src_factor == expected_dst &&
447 *dst_factor == PIPE_BLENDFACTOR_ZERO) {
448 *src_factor = PIPE_BLENDFACTOR_ZERO;
449 *dst_factor = replacement_src;
450
451 /* Commuting the operands requires reversing subtractions. */
452 if (*func == PIPE_BLEND_SUBTRACT)
453 *func = PIPE_BLEND_REVERSE_SUBTRACT;
454 else if (*func == PIPE_BLEND_REVERSE_SUBTRACT)
455 *func = PIPE_BLEND_SUBTRACT;
456 }
457 }
458
459 static bool si_blend_factor_uses_dst(unsigned factor)
460 {
461 return factor == PIPE_BLENDFACTOR_DST_COLOR ||
462 factor == PIPE_BLENDFACTOR_DST_ALPHA ||
463 factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
464 factor == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
465 factor == PIPE_BLENDFACTOR_INV_DST_COLOR;
466 }
467
468 static void *si_create_blend_state_mode(struct pipe_context *ctx,
469 const struct pipe_blend_state *state,
470 unsigned mode)
471 {
472 struct si_context *sctx = (struct si_context*)ctx;
473 struct si_state_blend *blend = CALLOC_STRUCT(si_state_blend);
474 struct si_pm4_state *pm4 = &blend->pm4;
475 uint32_t sx_mrt_blend_opt[8] = {0};
476 uint32_t color_control = 0;
477 bool logicop_enable = state->logicop_enable &&
478 state->logicop_func != PIPE_LOGICOP_COPY;
479
480 if (!blend)
481 return NULL;
482
483 blend->alpha_to_coverage = state->alpha_to_coverage;
484 blend->alpha_to_one = state->alpha_to_one;
485 blend->dual_src_blend = util_blend_state_is_dual(state, 0);
486 blend->logicop_enable = logicop_enable;
487
488 if (logicop_enable) {
489 color_control |= S_028808_ROP3(state->logicop_func | (state->logicop_func << 4));
490 } else {
491 color_control |= S_028808_ROP3(0xcc);
492 }
493
494 si_pm4_set_reg(pm4, R_028B70_DB_ALPHA_TO_MASK,
495 S_028B70_ALPHA_TO_MASK_ENABLE(state->alpha_to_coverage) |
496 S_028B70_ALPHA_TO_MASK_OFFSET0(3) |
497 S_028B70_ALPHA_TO_MASK_OFFSET1(1) |
498 S_028B70_ALPHA_TO_MASK_OFFSET2(0) |
499 S_028B70_ALPHA_TO_MASK_OFFSET3(2) |
500 S_028B70_OFFSET_ROUND(1));
501
502 if (state->alpha_to_coverage)
503 blend->need_src_alpha_4bit |= 0xf;
504
505 blend->cb_target_mask = 0;
506 blend->cb_target_enabled_4bit = 0;
507
508 for (int i = 0; i < 8; i++) {
509 /* state->rt entries > 0 only written if independent blending */
510 const int j = state->independent_blend_enable ? i : 0;
511
512 unsigned eqRGB = state->rt[j].rgb_func;
513 unsigned srcRGB = state->rt[j].rgb_src_factor;
514 unsigned dstRGB = state->rt[j].rgb_dst_factor;
515 unsigned eqA = state->rt[j].alpha_func;
516 unsigned srcA = state->rt[j].alpha_src_factor;
517 unsigned dstA = state->rt[j].alpha_dst_factor;
518
519 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
520 unsigned blend_cntl = 0;
521
522 sx_mrt_blend_opt[i] =
523 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |
524 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);
525
526 /* Only set dual source blending for MRT0 to avoid a hang. */
527 if (i >= 1 && blend->dual_src_blend) {
528 /* Vulkan does this for dual source blending. */
529 if (i == 1)
530 blend_cntl |= S_028780_ENABLE(1);
531
532 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
533 continue;
534 }
535
536 /* Only addition and subtraction equations are supported with
537 * dual source blending.
538 */
539 if (blend->dual_src_blend &&
540 (eqRGB == PIPE_BLEND_MIN || eqRGB == PIPE_BLEND_MAX ||
541 eqA == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MAX)) {
542 assert(!"Unsupported equation for dual source blending");
543 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
544 continue;
545 }
546
547 /* cb_render_state will disable unused ones */
548 blend->cb_target_mask |= (unsigned)state->rt[j].colormask << (4 * i);
549 if (state->rt[j].colormask)
550 blend->cb_target_enabled_4bit |= 0xf << (4 * i);
551
552 if (!state->rt[j].colormask || !state->rt[j].blend_enable) {
553 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
554 continue;
555 }
556
557 si_blend_check_commutativity(sctx->screen, blend,
558 eqRGB, srcRGB, dstRGB, 0x7 << (4 * i));
559 si_blend_check_commutativity(sctx->screen, blend,
560 eqA, srcA, dstA, 0x8 << (4 * i));
561
562 /* Blending optimizations for RB+.
563 * These transformations don't change the behavior.
564 *
565 * First, get rid of DST in the blend factors:
566 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
567 */
568 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
569 PIPE_BLENDFACTOR_DST_COLOR,
570 PIPE_BLENDFACTOR_SRC_COLOR);
571 si_blend_remove_dst(&eqA, &srcA, &dstA,
572 PIPE_BLENDFACTOR_DST_COLOR,
573 PIPE_BLENDFACTOR_SRC_COLOR);
574 si_blend_remove_dst(&eqA, &srcA, &dstA,
575 PIPE_BLENDFACTOR_DST_ALPHA,
576 PIPE_BLENDFACTOR_SRC_ALPHA);
577
578 /* Look up the ideal settings from tables. */
579 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
580 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
581 srcA_opt = si_translate_blend_opt_factor(srcA, true);
582 dstA_opt = si_translate_blend_opt_factor(dstA, true);
583
584 /* Handle interdependencies. */
585 if (si_blend_factor_uses_dst(srcRGB))
586 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
587 if (si_blend_factor_uses_dst(srcA))
588 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
589
590 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE &&
591 (dstRGB == PIPE_BLENDFACTOR_ZERO ||
592 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
593 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE))
594 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
595
596 /* Set the final value. */
597 sx_mrt_blend_opt[i] =
598 S_028760_COLOR_SRC_OPT(srcRGB_opt) |
599 S_028760_COLOR_DST_OPT(dstRGB_opt) |
600 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
601 S_028760_ALPHA_SRC_OPT(srcA_opt) |
602 S_028760_ALPHA_DST_OPT(dstA_opt) |
603 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
604
605 /* Set blend state. */
606 blend_cntl |= S_028780_ENABLE(1);
607 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
608 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
609 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
610
611 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
612 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
613 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
614 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
615 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
616 }
617 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
618
619 blend->blend_enable_4bit |= 0xfu << (i * 4);
620
621 if (sctx->family <= CHIP_NAVI14)
622 blend->dcc_msaa_corruption_4bit |= 0xfu << (i * 4);
623
624 /* This is only important for formats without alpha. */
625 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
626 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
627 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
628 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
629 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
630 dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA)
631 blend->need_src_alpha_4bit |= 0xfu << (i * 4);
632 }
633
634 if (sctx->family <= CHIP_NAVI14 && logicop_enable)
635 blend->dcc_msaa_corruption_4bit |= blend->cb_target_enabled_4bit;
636
637 if (blend->cb_target_mask) {
638 color_control |= S_028808_MODE(mode);
639 } else {
640 color_control |= S_028808_MODE(V_028808_CB_DISABLE);
641 }
642
643 if (sctx->screen->info.rbplus_allowed) {
644 /* Disable RB+ blend optimizations for dual source blending.
645 * Vulkan does this.
646 */
647 if (blend->dual_src_blend) {
648 for (int i = 0; i < 8; i++) {
649 sx_mrt_blend_opt[i] =
650 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_NONE) |
651 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_NONE);
652 }
653 }
654
655 for (int i = 0; i < 8; i++)
656 si_pm4_set_reg(pm4, R_028760_SX_MRT0_BLEND_OPT + i * 4,
657 sx_mrt_blend_opt[i]);
658
659 /* RB+ doesn't work with dual source blending, logic op, and RESOLVE. */
660 if (blend->dual_src_blend || logicop_enable ||
661 mode == V_028808_CB_RESOLVE)
662 color_control |= S_028808_DISABLE_DUAL_QUAD(1);
663 }
664
665 si_pm4_set_reg(pm4, R_028808_CB_COLOR_CONTROL, color_control);
666 return blend;
667 }
668
669 static void *si_create_blend_state(struct pipe_context *ctx,
670 const struct pipe_blend_state *state)
671 {
672 return si_create_blend_state_mode(ctx, state, V_028808_CB_NORMAL);
673 }
674
675 static void si_bind_blend_state(struct pipe_context *ctx, void *state)
676 {
677 struct si_context *sctx = (struct si_context *)ctx;
678 struct si_state_blend *old_blend = sctx->queued.named.blend;
679 struct si_state_blend *blend = (struct si_state_blend *)state;
680
681 if (!blend)
682 blend = (struct si_state_blend *)sctx->noop_blend;
683
684 si_pm4_bind_state(sctx, blend, blend);
685
686 if (old_blend->cb_target_mask != blend->cb_target_mask ||
687 old_blend->dual_src_blend != blend->dual_src_blend ||
688 (old_blend->blend_enable_4bit != blend->blend_enable_4bit &&
689 sctx->framebuffer.nr_samples >= 2 &&
690 sctx->screen->dcc_msaa_allowed))
691 si_mark_atom_dirty(sctx, &sctx->atoms.s.cb_render_state);
692
693 if (old_blend->cb_target_mask != blend->cb_target_mask ||
694 old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
695 old_blend->alpha_to_one != blend->alpha_to_one ||
696 old_blend->dual_src_blend != blend->dual_src_blend ||
697 old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
698 old_blend->need_src_alpha_4bit != blend->need_src_alpha_4bit)
699 sctx->do_update_shaders = true;
700
701 if (sctx->screen->dpbb_allowed &&
702 (old_blend->alpha_to_coverage != blend->alpha_to_coverage ||
703 old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
704 old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit))
705 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
706
707 if (sctx->screen->has_out_of_order_rast &&
708 ((old_blend->blend_enable_4bit != blend->blend_enable_4bit ||
709 old_blend->cb_target_enabled_4bit != blend->cb_target_enabled_4bit ||
710 old_blend->commutative_4bit != blend->commutative_4bit ||
711 old_blend->logicop_enable != blend->logicop_enable)))
712 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
713 }
714
715 static void si_delete_blend_state(struct pipe_context *ctx, void *state)
716 {
717 struct si_context *sctx = (struct si_context *)ctx;
718
719 if (sctx->queued.named.blend == state)
720 si_bind_blend_state(ctx, sctx->noop_blend);
721
722 si_pm4_delete_state(sctx, blend, (struct si_state_blend *)state);
723 }
724
725 static void si_set_blend_color(struct pipe_context *ctx,
726 const struct pipe_blend_color *state)
727 {
728 struct si_context *sctx = (struct si_context *)ctx;
729 static const struct pipe_blend_color zeros;
730
731 sctx->blend_color.state = *state;
732 sctx->blend_color.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
733 si_mark_atom_dirty(sctx, &sctx->atoms.s.blend_color);
734 }
735
736 static void si_emit_blend_color(struct si_context *sctx)
737 {
738 struct radeon_cmdbuf *cs = sctx->gfx_cs;
739
740 radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
741 radeon_emit_array(cs, (uint32_t*)sctx->blend_color.state.color, 4);
742 }
743
744 /*
745 * Clipping
746 */
747
748 static void si_set_clip_state(struct pipe_context *ctx,
749 const struct pipe_clip_state *state)
750 {
751 struct si_context *sctx = (struct si_context *)ctx;
752 struct pipe_constant_buffer cb;
753 static const struct pipe_clip_state zeros;
754
755 if (memcmp(&sctx->clip_state.state, state, sizeof(*state)) == 0)
756 return;
757
758 sctx->clip_state.state = *state;
759 sctx->clip_state.any_nonzeros = memcmp(state, &zeros, sizeof(*state)) != 0;
760 si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_state);
761
762 cb.buffer = NULL;
763 cb.user_buffer = state->ucp;
764 cb.buffer_offset = 0;
765 cb.buffer_size = 4*4*8;
766 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &cb);
767 pipe_resource_reference(&cb.buffer, NULL);
768 }
769
770 static void si_emit_clip_state(struct si_context *sctx)
771 {
772 struct radeon_cmdbuf *cs = sctx->gfx_cs;
773
774 radeon_set_context_reg_seq(cs, R_0285BC_PA_CL_UCP_0_X, 6*4);
775 radeon_emit_array(cs, (uint32_t*)sctx->clip_state.state.ucp, 6*4);
776 }
777
778 static void si_emit_clip_regs(struct si_context *sctx)
779 {
780 struct si_shader *vs = si_get_vs_state(sctx);
781 struct si_shader_selector *vs_sel = vs->selector;
782 struct si_shader_info *info = &vs_sel->info;
783 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
784 unsigned window_space =
785 info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
786 unsigned clipdist_mask = vs_sel->clipdist_mask;
787 unsigned ucp_mask = clipdist_mask ? 0 : rs->clip_plane_enable & SIX_BITS;
788 unsigned culldist_mask = vs_sel->culldist_mask;
789 unsigned total_mask;
790
791 if (vs->key.opt.clip_disable) {
792 assert(!info->culldist_writemask);
793 clipdist_mask = 0;
794 culldist_mask = 0;
795 }
796 total_mask = clipdist_mask | culldist_mask;
797
798 /* Clip distances on points have no effect, so need to be implemented
799 * as cull distances. This applies for the clipvertex case as well.
800 *
801 * Setting this for primitives other than points should have no adverse
802 * effects.
803 */
804 clipdist_mask &= rs->clip_plane_enable;
805 culldist_mask |= clipdist_mask;
806
807 unsigned initial_cdw = sctx->gfx_cs->current.cdw;
808 unsigned pa_cl_cntl = S_02881C_VS_OUT_CCDIST0_VEC_ENA((total_mask & 0x0F) != 0) |
809 S_02881C_VS_OUT_CCDIST1_VEC_ENA((total_mask & 0xF0) != 0) |
810 clipdist_mask | (culldist_mask << 8);
811
812 if (sctx->chip_class >= GFX10) {
813 radeon_opt_set_context_reg_rmw(sctx, R_02881C_PA_CL_VS_OUT_CNTL,
814 SI_TRACKED_PA_CL_VS_OUT_CNTL__CL,
815 pa_cl_cntl,
816 ~SI_TRACKED_PA_CL_VS_OUT_CNTL__VS_MASK);
817 } else {
818 radeon_opt_set_context_reg(sctx, R_02881C_PA_CL_VS_OUT_CNTL,
819 SI_TRACKED_PA_CL_VS_OUT_CNTL__CL,
820 vs_sel->pa_cl_vs_out_cntl | pa_cl_cntl);
821 }
822 radeon_opt_set_context_reg(sctx, R_028810_PA_CL_CLIP_CNTL,
823 SI_TRACKED_PA_CL_CLIP_CNTL,
824 rs->pa_cl_clip_cntl |
825 ucp_mask |
826 S_028810_CLIP_DISABLE(window_space));
827
828 if (initial_cdw != sctx->gfx_cs->current.cdw)
829 sctx->context_roll = true;
830 }
831
832 /*
833 * inferred state between framebuffer and rasterizer
834 */
835 static void si_update_poly_offset_state(struct si_context *sctx)
836 {
837 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
838
839 if (!rs->uses_poly_offset || !sctx->framebuffer.state.zsbuf) {
840 si_pm4_bind_state(sctx, poly_offset, NULL);
841 return;
842 }
843
844 /* Use the user format, not db_render_format, so that the polygon
845 * offset behaves as expected by applications.
846 */
847 switch (sctx->framebuffer.state.zsbuf->texture->format) {
848 case PIPE_FORMAT_Z16_UNORM:
849 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[0]);
850 break;
851 default: /* 24-bit */
852 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[1]);
853 break;
854 case PIPE_FORMAT_Z32_FLOAT:
855 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
856 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[2]);
857 break;
858 }
859 }
860
861 /*
862 * Rasterizer
863 */
864
865 static uint32_t si_translate_fill(uint32_t func)
866 {
867 switch(func) {
868 case PIPE_POLYGON_MODE_FILL:
869 return V_028814_X_DRAW_TRIANGLES;
870 case PIPE_POLYGON_MODE_LINE:
871 return V_028814_X_DRAW_LINES;
872 case PIPE_POLYGON_MODE_POINT:
873 return V_028814_X_DRAW_POINTS;
874 default:
875 assert(0);
876 return V_028814_X_DRAW_POINTS;
877 }
878 }
879
880 static void *si_create_rs_state(struct pipe_context *ctx,
881 const struct pipe_rasterizer_state *state)
882 {
883 struct si_screen *sscreen = ((struct si_context *)ctx)->screen;
884 struct si_state_rasterizer *rs = CALLOC_STRUCT(si_state_rasterizer);
885 struct si_pm4_state *pm4 = &rs->pm4;
886 unsigned tmp, i;
887 float psize_min, psize_max;
888
889 if (!rs) {
890 return NULL;
891 }
892
893 if (!state->front_ccw) {
894 rs->cull_front = !!(state->cull_face & PIPE_FACE_FRONT);
895 rs->cull_back = !!(state->cull_face & PIPE_FACE_BACK);
896 } else {
897 rs->cull_back = !!(state->cull_face & PIPE_FACE_FRONT);
898 rs->cull_front = !!(state->cull_face & PIPE_FACE_BACK);
899 }
900 rs->depth_clamp_any = !state->depth_clip_near || !state->depth_clip_far;
901 rs->provoking_vertex_first = state->flatshade_first;
902 rs->scissor_enable = state->scissor;
903 rs->clip_halfz = state->clip_halfz;
904 rs->two_side = state->light_twoside;
905 rs->multisample_enable = state->multisample;
906 rs->force_persample_interp = state->force_persample_interp;
907 rs->clip_plane_enable = state->clip_plane_enable;
908 rs->half_pixel_center = state->half_pixel_center;
909 rs->line_stipple_enable = state->line_stipple_enable;
910 rs->poly_stipple_enable = state->poly_stipple_enable;
911 rs->line_smooth = state->line_smooth;
912 rs->line_width = state->line_width;
913 rs->poly_smooth = state->poly_smooth;
914 rs->uses_poly_offset = state->offset_point || state->offset_line ||
915 state->offset_tri;
916 rs->clamp_fragment_color = state->clamp_fragment_color;
917 rs->clamp_vertex_color = state->clamp_vertex_color;
918 rs->flatshade = state->flatshade;
919 rs->flatshade_first = state->flatshade_first;
920 rs->sprite_coord_enable = state->sprite_coord_enable;
921 rs->rasterizer_discard = state->rasterizer_discard;
922 rs->polygon_mode_enabled = (state->fill_front != PIPE_POLYGON_MODE_FILL &&
923 !(state->cull_face & PIPE_FACE_FRONT)) ||
924 (state->fill_back != PIPE_POLYGON_MODE_FILL &&
925 !(state->cull_face & PIPE_FACE_BACK));
926 rs->polygon_mode_is_lines = (state->fill_front == PIPE_POLYGON_MODE_LINE &&
927 !(state->cull_face & PIPE_FACE_FRONT)) ||
928 (state->fill_back == PIPE_POLYGON_MODE_LINE &&
929 !(state->cull_face & PIPE_FACE_BACK));
930 rs->pa_sc_line_stipple = state->line_stipple_enable ?
931 S_028A0C_LINE_PATTERN(state->line_stipple_pattern) |
932 S_028A0C_REPEAT_COUNT(state->line_stipple_factor) : 0;
933 rs->pa_cl_clip_cntl =
934 S_028810_DX_CLIP_SPACE_DEF(state->clip_halfz) |
935 S_028810_ZCLIP_NEAR_DISABLE(!state->depth_clip_near) |
936 S_028810_ZCLIP_FAR_DISABLE(!state->depth_clip_far) |
937 S_028810_DX_RASTERIZATION_KILL(state->rasterizer_discard) |
938 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
939
940 si_pm4_set_reg(pm4, R_0286D4_SPI_INTERP_CONTROL_0,
941 S_0286D4_FLAT_SHADE_ENA(1) |
942 S_0286D4_PNT_SPRITE_ENA(state->point_quad_rasterization) |
943 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
944 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
945 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
946 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
947 S_0286D4_PNT_SPRITE_TOP_1(state->sprite_coord_mode != PIPE_SPRITE_COORD_UPPER_LEFT));
948
949 /* point size 12.4 fixed point */
950 tmp = (unsigned)(state->point_size * 8.0);
951 si_pm4_set_reg(pm4, R_028A00_PA_SU_POINT_SIZE, S_028A00_HEIGHT(tmp) | S_028A00_WIDTH(tmp));
952
953 if (state->point_size_per_vertex) {
954 psize_min = util_get_min_point_size(state);
955 psize_max = SI_MAX_POINT_SIZE;
956 } else {
957 /* Force the point size to be as if the vertex output was disabled. */
958 psize_min = state->point_size;
959 psize_max = state->point_size;
960 }
961 rs->max_point_size = psize_max;
962
963 /* Divide by two, because 0.5 = 1 pixel. */
964 si_pm4_set_reg(pm4, R_028A04_PA_SU_POINT_MINMAX,
965 S_028A04_MIN_SIZE(si_pack_float_12p4(psize_min/2)) |
966 S_028A04_MAX_SIZE(si_pack_float_12p4(psize_max/2)));
967
968 si_pm4_set_reg(pm4, R_028A08_PA_SU_LINE_CNTL,
969 S_028A08_WIDTH(si_pack_float_12p4(state->line_width/2)));
970 si_pm4_set_reg(pm4, R_028A48_PA_SC_MODE_CNTL_0,
971 S_028A48_LINE_STIPPLE_ENABLE(state->line_stipple_enable) |
972 S_028A48_MSAA_ENABLE(state->multisample ||
973 state->poly_smooth ||
974 state->line_smooth) |
975 S_028A48_VPORT_SCISSOR_ENABLE(1) |
976 S_028A48_ALTERNATE_RBS_PER_TILE(sscreen->info.chip_class >= GFX9));
977
978 si_pm4_set_reg(pm4, R_028B7C_PA_SU_POLY_OFFSET_CLAMP, fui(state->offset_clamp));
979 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
980 S_028814_PROVOKING_VTX_LAST(!state->flatshade_first) |
981 S_028814_CULL_FRONT((state->cull_face & PIPE_FACE_FRONT) ? 1 : 0) |
982 S_028814_CULL_BACK((state->cull_face & PIPE_FACE_BACK) ? 1 : 0) |
983 S_028814_FACE(!state->front_ccw) |
984 S_028814_POLY_OFFSET_FRONT_ENABLE(util_get_offset(state, state->fill_front)) |
985 S_028814_POLY_OFFSET_BACK_ENABLE(util_get_offset(state, state->fill_back)) |
986 S_028814_POLY_OFFSET_PARA_ENABLE(state->offset_point || state->offset_line) |
987 S_028814_POLY_MODE(rs->polygon_mode_enabled) |
988 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(state->fill_front)) |
989 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(state->fill_back)));
990
991 if (!rs->uses_poly_offset)
992 return rs;
993
994 rs->pm4_poly_offset = CALLOC(3, sizeof(struct si_pm4_state));
995 if (!rs->pm4_poly_offset) {
996 FREE(rs);
997 return NULL;
998 }
999
1000 /* Precalculate polygon offset states for 16-bit, 24-bit, and 32-bit zbuffers. */
1001 for (i = 0; i < 3; i++) {
1002 struct si_pm4_state *pm4 = &rs->pm4_poly_offset[i];
1003 float offset_units = state->offset_units;
1004 float offset_scale = state->offset_scale * 16.0f;
1005 uint32_t pa_su_poly_offset_db_fmt_cntl = 0;
1006
1007 if (!state->offset_units_unscaled) {
1008 switch (i) {
1009 case 0: /* 16-bit zbuffer */
1010 offset_units *= 4.0f;
1011 pa_su_poly_offset_db_fmt_cntl =
1012 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
1013 break;
1014 case 1: /* 24-bit zbuffer */
1015 offset_units *= 2.0f;
1016 pa_su_poly_offset_db_fmt_cntl =
1017 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
1018 break;
1019 case 2: /* 32-bit zbuffer */
1020 offset_units *= 1.0f;
1021 pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
1022 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
1023 break;
1024 }
1025 }
1026
1027 si_pm4_set_reg(pm4, R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE,
1028 fui(offset_scale));
1029 si_pm4_set_reg(pm4, R_028B84_PA_SU_POLY_OFFSET_FRONT_OFFSET,
1030 fui(offset_units));
1031 si_pm4_set_reg(pm4, R_028B88_PA_SU_POLY_OFFSET_BACK_SCALE,
1032 fui(offset_scale));
1033 si_pm4_set_reg(pm4, R_028B8C_PA_SU_POLY_OFFSET_BACK_OFFSET,
1034 fui(offset_units));
1035 si_pm4_set_reg(pm4, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL,
1036 pa_su_poly_offset_db_fmt_cntl);
1037 }
1038
1039 return rs;
1040 }
1041
1042 static void si_bind_rs_state(struct pipe_context *ctx, void *state)
1043 {
1044 struct si_context *sctx = (struct si_context *)ctx;
1045 struct si_state_rasterizer *old_rs =
1046 (struct si_state_rasterizer*)sctx->queued.named.rasterizer;
1047 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
1048
1049 if (!rs)
1050 rs = (struct si_state_rasterizer *)sctx->discard_rasterizer_state;
1051
1052 if (old_rs->multisample_enable != rs->multisample_enable) {
1053 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1054
1055 /* Update the small primitive filter workaround if necessary. */
1056 if (sctx->screen->info.has_msaa_sample_loc_bug &&
1057 sctx->framebuffer.nr_samples > 1)
1058 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_sample_locs);
1059 }
1060
1061 sctx->current_vs_state &= C_VS_STATE_CLAMP_VERTEX_COLOR;
1062 sctx->current_vs_state |= S_VS_STATE_CLAMP_VERTEX_COLOR(rs->clamp_vertex_color);
1063
1064 si_pm4_bind_state(sctx, rasterizer, rs);
1065 si_update_poly_offset_state(sctx);
1066
1067 if (old_rs->scissor_enable != rs->scissor_enable)
1068 si_mark_atom_dirty(sctx, &sctx->atoms.s.scissors);
1069
1070 if (old_rs->line_width != rs->line_width ||
1071 old_rs->max_point_size != rs->max_point_size ||
1072 old_rs->half_pixel_center != rs->half_pixel_center)
1073 si_mark_atom_dirty(sctx, &sctx->atoms.s.guardband);
1074
1075 if (old_rs->clip_halfz != rs->clip_halfz)
1076 si_mark_atom_dirty(sctx, &sctx->atoms.s.viewports);
1077
1078 if (old_rs->clip_plane_enable != rs->clip_plane_enable ||
1079 old_rs->pa_cl_clip_cntl != rs->pa_cl_clip_cntl)
1080 si_mark_atom_dirty(sctx, &sctx->atoms.s.clip_regs);
1081
1082 if (old_rs->clip_plane_enable != rs->clip_plane_enable ||
1083 old_rs->rasterizer_discard != rs->rasterizer_discard ||
1084 old_rs->sprite_coord_enable != rs->sprite_coord_enable ||
1085 old_rs->flatshade != rs->flatshade ||
1086 old_rs->two_side != rs->two_side ||
1087 old_rs->multisample_enable != rs->multisample_enable ||
1088 old_rs->poly_stipple_enable != rs->poly_stipple_enable ||
1089 old_rs->poly_smooth != rs->poly_smooth ||
1090 old_rs->line_smooth != rs->line_smooth ||
1091 old_rs->clamp_fragment_color != rs->clamp_fragment_color ||
1092 old_rs->force_persample_interp != rs->force_persample_interp)
1093 sctx->do_update_shaders = true;
1094 }
1095
1096 static void si_delete_rs_state(struct pipe_context *ctx, void *state)
1097 {
1098 struct si_context *sctx = (struct si_context *)ctx;
1099 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
1100
1101 if (sctx->queued.named.rasterizer == state)
1102 si_bind_rs_state(ctx, sctx->discard_rasterizer_state);
1103
1104 FREE(rs->pm4_poly_offset);
1105 si_pm4_delete_state(sctx, rasterizer, rs);
1106 }
1107
1108 /*
1109 * infeered state between dsa and stencil ref
1110 */
1111 static void si_emit_stencil_ref(struct si_context *sctx)
1112 {
1113 struct radeon_cmdbuf *cs = sctx->gfx_cs;
1114 struct pipe_stencil_ref *ref = &sctx->stencil_ref.state;
1115 struct si_dsa_stencil_ref_part *dsa = &sctx->stencil_ref.dsa_part;
1116
1117 radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
1118 radeon_emit(cs, S_028430_STENCILTESTVAL(ref->ref_value[0]) |
1119 S_028430_STENCILMASK(dsa->valuemask[0]) |
1120 S_028430_STENCILWRITEMASK(dsa->writemask[0]) |
1121 S_028430_STENCILOPVAL(1));
1122 radeon_emit(cs, S_028434_STENCILTESTVAL_BF(ref->ref_value[1]) |
1123 S_028434_STENCILMASK_BF(dsa->valuemask[1]) |
1124 S_028434_STENCILWRITEMASK_BF(dsa->writemask[1]) |
1125 S_028434_STENCILOPVAL_BF(1));
1126 }
1127
1128 static void si_set_stencil_ref(struct pipe_context *ctx,
1129 const struct pipe_stencil_ref *state)
1130 {
1131 struct si_context *sctx = (struct si_context *)ctx;
1132
1133 if (memcmp(&sctx->stencil_ref.state, state, sizeof(*state)) == 0)
1134 return;
1135
1136 sctx->stencil_ref.state = *state;
1137 si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
1138 }
1139
1140
1141 /*
1142 * DSA
1143 */
1144
1145 static uint32_t si_translate_stencil_op(int s_op)
1146 {
1147 switch (s_op) {
1148 case PIPE_STENCIL_OP_KEEP:
1149 return V_02842C_STENCIL_KEEP;
1150 case PIPE_STENCIL_OP_ZERO:
1151 return V_02842C_STENCIL_ZERO;
1152 case PIPE_STENCIL_OP_REPLACE:
1153 return V_02842C_STENCIL_REPLACE_TEST;
1154 case PIPE_STENCIL_OP_INCR:
1155 return V_02842C_STENCIL_ADD_CLAMP;
1156 case PIPE_STENCIL_OP_DECR:
1157 return V_02842C_STENCIL_SUB_CLAMP;
1158 case PIPE_STENCIL_OP_INCR_WRAP:
1159 return V_02842C_STENCIL_ADD_WRAP;
1160 case PIPE_STENCIL_OP_DECR_WRAP:
1161 return V_02842C_STENCIL_SUB_WRAP;
1162 case PIPE_STENCIL_OP_INVERT:
1163 return V_02842C_STENCIL_INVERT;
1164 default:
1165 PRINT_ERR("Unknown stencil op %d", s_op);
1166 assert(0);
1167 break;
1168 }
1169 return 0;
1170 }
1171
1172 static bool si_dsa_writes_stencil(const struct pipe_stencil_state *s)
1173 {
1174 return s->enabled && s->writemask &&
1175 (s->fail_op != PIPE_STENCIL_OP_KEEP ||
1176 s->zfail_op != PIPE_STENCIL_OP_KEEP ||
1177 s->zpass_op != PIPE_STENCIL_OP_KEEP);
1178 }
1179
1180 static bool si_order_invariant_stencil_op(enum pipe_stencil_op op)
1181 {
1182 /* REPLACE is normally order invariant, except when the stencil
1183 * reference value is written by the fragment shader. Tracking this
1184 * interaction does not seem worth the effort, so be conservative. */
1185 return op != PIPE_STENCIL_OP_INCR &&
1186 op != PIPE_STENCIL_OP_DECR &&
1187 op != PIPE_STENCIL_OP_REPLACE;
1188 }
1189
1190 /* Compute whether, assuming Z writes are disabled, this stencil state is order
1191 * invariant in the sense that the set of passing fragments as well as the
1192 * final stencil buffer result does not depend on the order of fragments. */
1193 static bool si_order_invariant_stencil_state(const struct pipe_stencil_state *state)
1194 {
1195 return !state->enabled || !state->writemask ||
1196 /* The following assumes that Z writes are disabled. */
1197 (state->func == PIPE_FUNC_ALWAYS &&
1198 si_order_invariant_stencil_op(state->zpass_op) &&
1199 si_order_invariant_stencil_op(state->zfail_op)) ||
1200 (state->func == PIPE_FUNC_NEVER &&
1201 si_order_invariant_stencil_op(state->fail_op));
1202 }
1203
1204 static void *si_create_dsa_state(struct pipe_context *ctx,
1205 const struct pipe_depth_stencil_alpha_state *state)
1206 {
1207 struct si_context *sctx = (struct si_context *)ctx;
1208 struct si_state_dsa *dsa = CALLOC_STRUCT(si_state_dsa);
1209 struct si_pm4_state *pm4 = &dsa->pm4;
1210 unsigned db_depth_control;
1211 uint32_t db_stencil_control = 0;
1212
1213 if (!dsa) {
1214 return NULL;
1215 }
1216
1217 dsa->stencil_ref.valuemask[0] = state->stencil[0].valuemask;
1218 dsa->stencil_ref.valuemask[1] = state->stencil[1].valuemask;
1219 dsa->stencil_ref.writemask[0] = state->stencil[0].writemask;
1220 dsa->stencil_ref.writemask[1] = state->stencil[1].writemask;
1221
1222 db_depth_control = S_028800_Z_ENABLE(state->depth.enabled) |
1223 S_028800_Z_WRITE_ENABLE(state->depth.writemask) |
1224 S_028800_ZFUNC(state->depth.func) |
1225 S_028800_DEPTH_BOUNDS_ENABLE(state->depth.bounds_test);
1226
1227 /* stencil */
1228 if (state->stencil[0].enabled) {
1229 db_depth_control |= S_028800_STENCIL_ENABLE(1);
1230 db_depth_control |= S_028800_STENCILFUNC(state->stencil[0].func);
1231 db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(state->stencil[0].fail_op));
1232 db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(state->stencil[0].zpass_op));
1233 db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(state->stencil[0].zfail_op));
1234
1235 if (state->stencil[1].enabled) {
1236 db_depth_control |= S_028800_BACKFACE_ENABLE(1);
1237 db_depth_control |= S_028800_STENCILFUNC_BF(state->stencil[1].func);
1238 db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(state->stencil[1].fail_op));
1239 db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(state->stencil[1].zpass_op));
1240 db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(state->stencil[1].zfail_op));
1241 }
1242 }
1243
1244 /* alpha */
1245 if (state->alpha.enabled) {
1246 dsa->alpha_func = state->alpha.func;
1247
1248 si_pm4_set_reg(pm4, R_00B030_SPI_SHADER_USER_DATA_PS_0 +
1249 SI_SGPR_ALPHA_REF * 4, fui(state->alpha.ref_value));
1250 } else {
1251 dsa->alpha_func = PIPE_FUNC_ALWAYS;
1252 }
1253
1254 si_pm4_set_reg(pm4, R_028800_DB_DEPTH_CONTROL, db_depth_control);
1255 if (state->stencil[0].enabled)
1256 si_pm4_set_reg(pm4, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
1257 if (state->depth.bounds_test) {
1258 si_pm4_set_reg(pm4, R_028020_DB_DEPTH_BOUNDS_MIN, fui(state->depth.bounds_min));
1259 si_pm4_set_reg(pm4, R_028024_DB_DEPTH_BOUNDS_MAX, fui(state->depth.bounds_max));
1260 }
1261
1262 dsa->depth_enabled = state->depth.enabled;
1263 dsa->depth_write_enabled = state->depth.enabled &&
1264 state->depth.writemask;
1265 dsa->stencil_enabled = state->stencil[0].enabled;
1266 dsa->stencil_write_enabled = state->stencil[0].enabled &&
1267 (si_dsa_writes_stencil(&state->stencil[0]) ||
1268 si_dsa_writes_stencil(&state->stencil[1]));
1269 dsa->db_can_write = dsa->depth_write_enabled ||
1270 dsa->stencil_write_enabled;
1271
1272 bool zfunc_is_ordered =
1273 state->depth.func == PIPE_FUNC_NEVER ||
1274 state->depth.func == PIPE_FUNC_LESS ||
1275 state->depth.func == PIPE_FUNC_LEQUAL ||
1276 state->depth.func == PIPE_FUNC_GREATER ||
1277 state->depth.func == PIPE_FUNC_GEQUAL;
1278
1279 bool nozwrite_and_order_invariant_stencil =
1280 !dsa->db_can_write ||
1281 (!dsa->depth_write_enabled &&
1282 si_order_invariant_stencil_state(&state->stencil[0]) &&
1283 si_order_invariant_stencil_state(&state->stencil[1]));
1284
1285 dsa->order_invariance[1].zs =
1286 nozwrite_and_order_invariant_stencil ||
1287 (!dsa->stencil_write_enabled && zfunc_is_ordered);
1288 dsa->order_invariance[0].zs = !dsa->depth_write_enabled || zfunc_is_ordered;
1289
1290 dsa->order_invariance[1].pass_set =
1291 nozwrite_and_order_invariant_stencil ||
1292 (!dsa->stencil_write_enabled &&
1293 (state->depth.func == PIPE_FUNC_ALWAYS ||
1294 state->depth.func == PIPE_FUNC_NEVER));
1295 dsa->order_invariance[0].pass_set =
1296 !dsa->depth_write_enabled ||
1297 (state->depth.func == PIPE_FUNC_ALWAYS ||
1298 state->depth.func == PIPE_FUNC_NEVER);
1299
1300 dsa->order_invariance[1].pass_last =
1301 sctx->screen->assume_no_z_fights &&
1302 !dsa->stencil_write_enabled &&
1303 dsa->depth_write_enabled && zfunc_is_ordered;
1304 dsa->order_invariance[0].pass_last =
1305 sctx->screen->assume_no_z_fights &&
1306 dsa->depth_write_enabled && zfunc_is_ordered;
1307
1308 return dsa;
1309 }
1310
1311 static void si_bind_dsa_state(struct pipe_context *ctx, void *state)
1312 {
1313 struct si_context *sctx = (struct si_context *)ctx;
1314 struct si_state_dsa *old_dsa = sctx->queued.named.dsa;
1315 struct si_state_dsa *dsa = state;
1316
1317 if (!dsa)
1318 dsa = (struct si_state_dsa *)sctx->noop_dsa;
1319
1320 si_pm4_bind_state(sctx, dsa, dsa);
1321
1322 if (memcmp(&dsa->stencil_ref, &sctx->stencil_ref.dsa_part,
1323 sizeof(struct si_dsa_stencil_ref_part)) != 0) {
1324 sctx->stencil_ref.dsa_part = dsa->stencil_ref;
1325 si_mark_atom_dirty(sctx, &sctx->atoms.s.stencil_ref);
1326 }
1327
1328 if (old_dsa->alpha_func != dsa->alpha_func)
1329 sctx->do_update_shaders = true;
1330
1331 if (sctx->screen->dpbb_allowed &&
1332 ((old_dsa->depth_enabled != dsa->depth_enabled ||
1333 old_dsa->stencil_enabled != dsa->stencil_enabled ||
1334 old_dsa->db_can_write != dsa->db_can_write)))
1335 si_mark_atom_dirty(sctx, &sctx->atoms.s.dpbb_state);
1336
1337 if (sctx->screen->has_out_of_order_rast &&
1338 (memcmp(old_dsa->order_invariance, dsa->order_invariance,
1339 sizeof(old_dsa->order_invariance))))
1340 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
1341 }
1342
1343 static void si_delete_dsa_state(struct pipe_context *ctx, void *state)
1344 {
1345 struct si_context *sctx = (struct si_context *)ctx;
1346
1347 if (sctx->queued.named.dsa == state)
1348 si_bind_dsa_state(ctx, sctx->noop_dsa);
1349
1350 si_pm4_delete_state(sctx, dsa, (struct si_state_dsa *)state);
1351 }
1352
1353 static void *si_create_db_flush_dsa(struct si_context *sctx)
1354 {
1355 struct pipe_depth_stencil_alpha_state dsa = {};
1356
1357 return sctx->b.create_depth_stencil_alpha_state(&sctx->b, &dsa);
1358 }
1359
1360 /* DB RENDER STATE */
1361
1362 static void si_set_active_query_state(struct pipe_context *ctx, bool enable)
1363 {
1364 struct si_context *sctx = (struct si_context*)ctx;
1365
1366 /* Pipeline stat & streamout queries. */
1367 if (enable) {
1368 sctx->flags &= ~SI_CONTEXT_STOP_PIPELINE_STATS;
1369 sctx->flags |= SI_CONTEXT_START_PIPELINE_STATS;
1370 } else {
1371 sctx->flags &= ~SI_CONTEXT_START_PIPELINE_STATS;
1372 sctx->flags |= SI_CONTEXT_STOP_PIPELINE_STATS;
1373 }
1374
1375 /* Occlusion queries. */
1376 if (sctx->occlusion_queries_disabled != !enable) {
1377 sctx->occlusion_queries_disabled = !enable;
1378 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1379 }
1380 }
1381
1382 void si_set_occlusion_query_state(struct si_context *sctx,
1383 bool old_perfect_enable)
1384 {
1385 si_mark_atom_dirty(sctx, &sctx->atoms.s.db_render_state);
1386
1387 bool perfect_enable = sctx->num_perfect_occlusion_queries != 0;
1388
1389 if (perfect_enable != old_perfect_enable)
1390 si_mark_atom_dirty(sctx, &sctx->atoms.s.msaa_config);
1391 }
1392
1393 void si_save_qbo_state(struct si_context *sctx, struct si_qbo_state *st)
1394 {
1395 st->saved_compute = sctx->cs_shader_state.program;
1396
1397 si_get_pipe_constant_buffer(sctx, PIPE_SHADER_COMPUTE, 0, &st->saved_const0);
1398 si_get_shader_buffers(sctx, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo);
1399
1400 st->saved_ssbo_writable_mask = 0;
1401
1402 for (unsigned i = 0; i < 3; i++) {
1403 if (sctx->const_and_shader_buffers[PIPE_SHADER_COMPUTE].writable_mask &
1404 (1u << si_get_shaderbuf_slot(i)))
1405 st->saved_ssbo_writable_mask |= 1 << i;
1406 }
1407 }
1408
1409 void si_restore_qbo_state(struct si_context *sctx, struct si_qbo_state *st)
1410 {
1411 sctx->b.bind_compute_state(&sctx->b, st->saved_compute);
1412
1413 sctx->b.set_constant_buffer(&sctx->b, PIPE_SHADER_COMPUTE, 0, &st->saved_const0);
1414 pipe_resource_reference(&st->saved_const0.buffer, NULL);
1415
1416 sctx->b.set_shader_buffers(&sctx->b, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo,
1417 st->saved_ssbo_writable_mask);
1418 for (unsigned i = 0; i < 3; ++i)
1419 pipe_resource_reference(&st->saved_ssbo[i].buffer, NULL);
1420 }
1421
1422 static void si_emit_db_render_state(struct si_context *sctx)
1423 {
1424 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1425 unsigned db_shader_control, db_render_control, db_count_control;
1426 unsigned initial_cdw = sctx->gfx_cs->current.cdw;
1427
1428 /* DB_RENDER_CONTROL */
1429 if (sctx->dbcb_depth_copy_enabled ||
1430 sctx->dbcb_stencil_copy_enabled) {
1431 db_render_control =
1432 S_028000_DEPTH_COPY(sctx->dbcb_depth_copy_enabled) |
1433 S_028000_STENCIL_COPY(sctx->dbcb_stencil_copy_enabled) |
1434 S_028000_COPY_CENTROID(1) |
1435 S_028000_COPY_SAMPLE(sctx->dbcb_copy_sample);
1436 } else if (sctx->db_flush_depth_inplace || sctx->db_flush_stencil_inplace) {
1437 db_render_control =
1438 S_028000_DEPTH_COMPRESS_DISABLE(sctx->db_flush_depth_inplace) |
1439 S_028000_STENCIL_COMPRESS_DISABLE(sctx->db_flush_stencil_inplace);
1440 } else {
1441 db_render_control =
1442 S_028000_DEPTH_CLEAR_ENABLE(sctx->db_depth_clear) |
1443 S_028000_STENCIL_CLEAR_ENABLE(sctx->db_stencil_clear);
1444 }
1445
1446 /* DB_COUNT_CONTROL (occlusion queries) */
1447 if (sctx->num_occlusion_queries > 0 &&
1448 !sctx->occlusion_queries_disabled) {
1449 bool perfect = sctx->num_perfect_occlusion_queries > 0;
1450 bool gfx10_perfect = sctx->chip_class >= GFX10 && perfect;
1451
1452 if (sctx->chip_class >= GFX7) {
1453 unsigned log_sample_rate = sctx->framebuffer.log_samples;
1454
1455 /* Stoney doesn't increment occlusion query counters
1456 * if the sample rate is 16x. Use 8x sample rate instead.
1457 */
1458 if (sctx->family == CHIP_STONEY)
1459 log_sample_rate = MIN2(log_sample_rate, 3);
1460
1461 db_count_control =
1462 S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1463 S_028004_DISABLE_CONSERVATIVE_ZPASS_COUNTS(gfx10_perfect) |
1464 S_028004_SAMPLE_RATE(log_sample_rate) |
1465 S_028004_ZPASS_ENABLE(1) |
1466 S_028004_SLICE_EVEN_ENABLE(1) |
1467 S_028004_SLICE_ODD_ENABLE(1);
1468 } else {
1469 db_count_control =
1470 S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1471 S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples);
1472 }
1473 } else {
1474 /* Disable occlusion queries. */
1475 if (sctx->chip_class >= GFX7) {
1476 db_count_control = 0;
1477 } else {
1478 db_count_control = S_028004_ZPASS_INCREMENT_DISABLE(1);
1479 }
1480 }
1481
1482 radeon_opt_set_context_reg2(sctx, R_028000_DB_RENDER_CONTROL,
1483 SI_TRACKED_DB_RENDER_CONTROL, db_render_control,
1484 db_count_control);
1485
1486 /* DB_RENDER_OVERRIDE2 */
1487 radeon_opt_set_context_reg(sctx, R_028010_DB_RENDER_OVERRIDE2,
1488 SI_TRACKED_DB_RENDER_OVERRIDE2,
1489 S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(sctx->db_depth_disable_expclear) |
1490 S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(sctx->db_stencil_disable_expclear) |
1491 S_028010_DECOMPRESS_Z_ON_FLUSH(sctx->framebuffer.nr_samples >= 4));
1492
1493 db_shader_control = sctx->ps_db_shader_control;
1494
1495 /* Bug workaround for smoothing (overrasterization) on GFX6. */
1496 if (sctx->chip_class == GFX6 && sctx->smoothing_enabled) {
1497 db_shader_control &= C_02880C_Z_ORDER;
1498 db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z);
1499 }
1500
1501 /* Disable the gl_SampleMask fragment shader output if MSAA is disabled. */
1502 if (!rs->multisample_enable)
1503 db_shader_control &= C_02880C_MASK_EXPORT_ENABLE;
1504
1505 if (sctx->screen->info.has_rbplus &&
1506 !sctx->screen->info.rbplus_allowed)
1507 db_shader_control |= S_02880C_DUAL_QUAD_DISABLE(1);
1508
1509 radeon_opt_set_context_reg(sctx, R_02880C_DB_SHADER_CONTROL,
1510 SI_TRACKED_DB_SHADER_CONTROL, db_shader_control);
1511
1512 if (initial_cdw != sctx->gfx_cs->current.cdw)
1513 sctx->context_roll = true;
1514 }
1515
1516 /*
1517 * format translation
1518 */
1519 static uint32_t si_translate_colorformat(enum pipe_format format)
1520 {
1521 const struct util_format_description *desc = util_format_description(format);
1522 if (!desc)
1523 return V_028C70_COLOR_INVALID;
1524
1525 #define HAS_SIZE(x,y,z,w) \
1526 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
1527 desc->channel[2].size == (z) && desc->channel[3].size == (w))
1528
1529 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1530 return V_028C70_COLOR_10_11_11;
1531
1532 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1533 return V_028C70_COLOR_INVALID;
1534
1535 /* hw cannot support mixed formats (except depth/stencil, since
1536 * stencil is not written to). */
1537 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1538 return V_028C70_COLOR_INVALID;
1539
1540 switch (desc->nr_channels) {
1541 case 1:
1542 switch (desc->channel[0].size) {
1543 case 8:
1544 return V_028C70_COLOR_8;
1545 case 16:
1546 return V_028C70_COLOR_16;
1547 case 32:
1548 return V_028C70_COLOR_32;
1549 }
1550 break;
1551 case 2:
1552 if (desc->channel[0].size == desc->channel[1].size) {
1553 switch (desc->channel[0].size) {
1554 case 8:
1555 return V_028C70_COLOR_8_8;
1556 case 16:
1557 return V_028C70_COLOR_16_16;
1558 case 32:
1559 return V_028C70_COLOR_32_32;
1560 }
1561 } else if (HAS_SIZE(8,24,0,0)) {
1562 return V_028C70_COLOR_24_8;
1563 } else if (HAS_SIZE(24,8,0,0)) {
1564 return V_028C70_COLOR_8_24;
1565 }
1566 break;
1567 case 3:
1568 if (HAS_SIZE(5,6,5,0)) {
1569 return V_028C70_COLOR_5_6_5;
1570 } else if (HAS_SIZE(32,8,24,0)) {
1571 return V_028C70_COLOR_X24_8_32_FLOAT;
1572 }
1573 break;
1574 case 4:
1575 if (desc->channel[0].size == desc->channel[1].size &&
1576 desc->channel[0].size == desc->channel[2].size &&
1577 desc->channel[0].size == desc->channel[3].size) {
1578 switch (desc->channel[0].size) {
1579 case 4:
1580 return V_028C70_COLOR_4_4_4_4;
1581 case 8:
1582 return V_028C70_COLOR_8_8_8_8;
1583 case 16:
1584 return V_028C70_COLOR_16_16_16_16;
1585 case 32:
1586 return V_028C70_COLOR_32_32_32_32;
1587 }
1588 } else if (HAS_SIZE(5,5,5,1)) {
1589 return V_028C70_COLOR_1_5_5_5;
1590 } else if (HAS_SIZE(1,5,5,5)) {
1591 return V_028C70_COLOR_5_5_5_1;
1592 } else if (HAS_SIZE(10,10,10,2)) {
1593 return V_028C70_COLOR_2_10_10_10;
1594 }
1595 break;
1596 }
1597 return V_028C70_COLOR_INVALID;
1598 }
1599
1600 static uint32_t si_colorformat_endian_swap(uint32_t colorformat)
1601 {
1602 if (SI_BIG_ENDIAN) {
1603 switch(colorformat) {
1604 /* 8-bit buffers. */
1605 case V_028C70_COLOR_8:
1606 return V_028C70_ENDIAN_NONE;
1607
1608 /* 16-bit buffers. */
1609 case V_028C70_COLOR_5_6_5:
1610 case V_028C70_COLOR_1_5_5_5:
1611 case V_028C70_COLOR_4_4_4_4:
1612 case V_028C70_COLOR_16:
1613 case V_028C70_COLOR_8_8:
1614 return V_028C70_ENDIAN_8IN16;
1615
1616 /* 32-bit buffers. */
1617 case V_028C70_COLOR_8_8_8_8:
1618 case V_028C70_COLOR_2_10_10_10:
1619 case V_028C70_COLOR_8_24:
1620 case V_028C70_COLOR_24_8:
1621 case V_028C70_COLOR_16_16:
1622 return V_028C70_ENDIAN_8IN32;
1623
1624 /* 64-bit buffers. */
1625 case V_028C70_COLOR_16_16_16_16:
1626 return V_028C70_ENDIAN_8IN16;
1627
1628 case V_028C70_COLOR_32_32:
1629 return V_028C70_ENDIAN_8IN32;
1630
1631 /* 128-bit buffers. */
1632 case V_028C70_COLOR_32_32_32_32:
1633 return V_028C70_ENDIAN_8IN32;
1634 default:
1635 return V_028C70_ENDIAN_NONE; /* Unsupported. */
1636 }
1637 } else {
1638 return V_028C70_ENDIAN_NONE;
1639 }
1640 }
1641
1642 static uint32_t si_translate_dbformat(enum pipe_format format)
1643 {
1644 switch (format) {
1645 case PIPE_FORMAT_Z16_UNORM:
1646 return V_028040_Z_16;
1647 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1648 case PIPE_FORMAT_X8Z24_UNORM:
1649 case PIPE_FORMAT_Z24X8_UNORM:
1650 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1651 return V_028040_Z_24; /* deprecated on AMD GCN */
1652 case PIPE_FORMAT_Z32_FLOAT:
1653 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1654 return V_028040_Z_32_FLOAT;
1655 default:
1656 return V_028040_Z_INVALID;
1657 }
1658 }
1659
1660 /*
1661 * Texture translation
1662 */
1663
1664 static uint32_t si_translate_texformat(struct pipe_screen *screen,
1665 enum pipe_format format,
1666 const struct util_format_description *desc,
1667 int first_non_void)
1668 {
1669 struct si_screen *sscreen = (struct si_screen*)screen;
1670 bool uniform = true;
1671 int i;
1672
1673 assert(sscreen->info.chip_class <= GFX9);
1674
1675 /* Colorspace (return non-RGB formats directly). */
1676 switch (desc->colorspace) {
1677 /* Depth stencil formats */
1678 case UTIL_FORMAT_COLORSPACE_ZS:
1679 switch (format) {
1680 case PIPE_FORMAT_Z16_UNORM:
1681 return V_008F14_IMG_DATA_FORMAT_16;
1682 case PIPE_FORMAT_X24S8_UINT:
1683 case PIPE_FORMAT_S8X24_UINT:
1684 /*
1685 * Implemented as an 8_8_8_8 data format to fix texture
1686 * gathers in stencil sampling. This affects at least
1687 * GL45-CTS.texture_cube_map_array.sampling on GFX8.
1688 */
1689 if (sscreen->info.chip_class <= GFX8)
1690 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1691
1692 if (format == PIPE_FORMAT_X24S8_UINT)
1693 return V_008F14_IMG_DATA_FORMAT_8_24;
1694 else
1695 return V_008F14_IMG_DATA_FORMAT_24_8;
1696 case PIPE_FORMAT_Z24X8_UNORM:
1697 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1698 return V_008F14_IMG_DATA_FORMAT_8_24;
1699 case PIPE_FORMAT_X8Z24_UNORM:
1700 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1701 return V_008F14_IMG_DATA_FORMAT_24_8;
1702 case PIPE_FORMAT_S8_UINT:
1703 return V_008F14_IMG_DATA_FORMAT_8;
1704 case PIPE_FORMAT_Z32_FLOAT:
1705 return V_008F14_IMG_DATA_FORMAT_32;
1706 case PIPE_FORMAT_X32_S8X24_UINT:
1707 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1708 return V_008F14_IMG_DATA_FORMAT_X24_8_32;
1709 default:
1710 goto out_unknown;
1711 }
1712
1713 case UTIL_FORMAT_COLORSPACE_YUV:
1714 goto out_unknown; /* TODO */
1715
1716 case UTIL_FORMAT_COLORSPACE_SRGB:
1717 if (desc->nr_channels != 4 && desc->nr_channels != 1)
1718 goto out_unknown;
1719 break;
1720
1721 default:
1722 break;
1723 }
1724
1725 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
1726 if (!sscreen->info.has_format_bc1_through_bc7)
1727 goto out_unknown;
1728
1729 switch (format) {
1730 case PIPE_FORMAT_RGTC1_SNORM:
1731 case PIPE_FORMAT_LATC1_SNORM:
1732 case PIPE_FORMAT_RGTC1_UNORM:
1733 case PIPE_FORMAT_LATC1_UNORM:
1734 return V_008F14_IMG_DATA_FORMAT_BC4;
1735 case PIPE_FORMAT_RGTC2_SNORM:
1736 case PIPE_FORMAT_LATC2_SNORM:
1737 case PIPE_FORMAT_RGTC2_UNORM:
1738 case PIPE_FORMAT_LATC2_UNORM:
1739 return V_008F14_IMG_DATA_FORMAT_BC5;
1740 default:
1741 goto out_unknown;
1742 }
1743 }
1744
1745 if (desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
1746 (sscreen->info.family == CHIP_STONEY ||
1747 sscreen->info.family == CHIP_VEGA10 ||
1748 sscreen->info.family == CHIP_RAVEN)) {
1749 switch (format) {
1750 case PIPE_FORMAT_ETC1_RGB8:
1751 case PIPE_FORMAT_ETC2_RGB8:
1752 case PIPE_FORMAT_ETC2_SRGB8:
1753 return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
1754 case PIPE_FORMAT_ETC2_RGB8A1:
1755 case PIPE_FORMAT_ETC2_SRGB8A1:
1756 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
1757 case PIPE_FORMAT_ETC2_RGBA8:
1758 case PIPE_FORMAT_ETC2_SRGBA8:
1759 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
1760 case PIPE_FORMAT_ETC2_R11_UNORM:
1761 case PIPE_FORMAT_ETC2_R11_SNORM:
1762 return V_008F14_IMG_DATA_FORMAT_ETC2_R;
1763 case PIPE_FORMAT_ETC2_RG11_UNORM:
1764 case PIPE_FORMAT_ETC2_RG11_SNORM:
1765 return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
1766 default:
1767 goto out_unknown;
1768 }
1769 }
1770
1771 if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
1772 if (!sscreen->info.has_format_bc1_through_bc7)
1773 goto out_unknown;
1774
1775 switch (format) {
1776 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1777 case PIPE_FORMAT_BPTC_SRGBA:
1778 return V_008F14_IMG_DATA_FORMAT_BC7;
1779 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1780 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1781 return V_008F14_IMG_DATA_FORMAT_BC6;
1782 default:
1783 goto out_unknown;
1784 }
1785 }
1786
1787 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1788 switch (format) {
1789 case PIPE_FORMAT_R8G8_B8G8_UNORM:
1790 case PIPE_FORMAT_G8R8_B8R8_UNORM:
1791 return V_008F14_IMG_DATA_FORMAT_GB_GR;
1792 case PIPE_FORMAT_G8R8_G8B8_UNORM:
1793 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1794 return V_008F14_IMG_DATA_FORMAT_BG_RG;
1795 default:
1796 goto out_unknown;
1797 }
1798 }
1799
1800 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
1801 if (!sscreen->info.has_format_bc1_through_bc7)
1802 goto out_unknown;
1803
1804 switch (format) {
1805 case PIPE_FORMAT_DXT1_RGB:
1806 case PIPE_FORMAT_DXT1_RGBA:
1807 case PIPE_FORMAT_DXT1_SRGB:
1808 case PIPE_FORMAT_DXT1_SRGBA:
1809 return V_008F14_IMG_DATA_FORMAT_BC1;
1810 case PIPE_FORMAT_DXT3_RGBA:
1811 case PIPE_FORMAT_DXT3_SRGBA:
1812 return V_008F14_IMG_DATA_FORMAT_BC2;
1813 case PIPE_FORMAT_DXT5_RGBA:
1814 case PIPE_FORMAT_DXT5_SRGBA:
1815 return V_008F14_IMG_DATA_FORMAT_BC3;
1816 default:
1817 goto out_unknown;
1818 }
1819 }
1820
1821 if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
1822 return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
1823 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
1824 return V_008F14_IMG_DATA_FORMAT_10_11_11;
1825 }
1826
1827 /* R8G8Bx_SNORM - TODO CxV8U8 */
1828
1829 /* hw cannot support mixed formats (except depth/stencil, since only
1830 * depth is read).*/
1831 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1832 goto out_unknown;
1833
1834 /* See whether the components are of the same size. */
1835 for (i = 1; i < desc->nr_channels; i++) {
1836 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
1837 }
1838
1839 /* Non-uniform formats. */
1840 if (!uniform) {
1841 switch(desc->nr_channels) {
1842 case 3:
1843 if (desc->channel[0].size == 5 &&
1844 desc->channel[1].size == 6 &&
1845 desc->channel[2].size == 5) {
1846 return V_008F14_IMG_DATA_FORMAT_5_6_5;
1847 }
1848 goto out_unknown;
1849 case 4:
1850 if (desc->channel[0].size == 5 &&
1851 desc->channel[1].size == 5 &&
1852 desc->channel[2].size == 5 &&
1853 desc->channel[3].size == 1) {
1854 return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
1855 }
1856 if (desc->channel[0].size == 1 &&
1857 desc->channel[1].size == 5 &&
1858 desc->channel[2].size == 5 &&
1859 desc->channel[3].size == 5) {
1860 return V_008F14_IMG_DATA_FORMAT_5_5_5_1;
1861 }
1862 if (desc->channel[0].size == 10 &&
1863 desc->channel[1].size == 10 &&
1864 desc->channel[2].size == 10 &&
1865 desc->channel[3].size == 2) {
1866 return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
1867 }
1868 goto out_unknown;
1869 }
1870 goto out_unknown;
1871 }
1872
1873 if (first_non_void < 0 || first_non_void > 3)
1874 goto out_unknown;
1875
1876 /* uniform formats */
1877 switch (desc->channel[first_non_void].size) {
1878 case 4:
1879 switch (desc->nr_channels) {
1880 #if 0 /* Not supported for render targets */
1881 case 2:
1882 return V_008F14_IMG_DATA_FORMAT_4_4;
1883 #endif
1884 case 4:
1885 return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
1886 }
1887 break;
1888 case 8:
1889 switch (desc->nr_channels) {
1890 case 1:
1891 return V_008F14_IMG_DATA_FORMAT_8;
1892 case 2:
1893 return V_008F14_IMG_DATA_FORMAT_8_8;
1894 case 4:
1895 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1896 }
1897 break;
1898 case 16:
1899 switch (desc->nr_channels) {
1900 case 1:
1901 return V_008F14_IMG_DATA_FORMAT_16;
1902 case 2:
1903 return V_008F14_IMG_DATA_FORMAT_16_16;
1904 case 4:
1905 return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
1906 }
1907 break;
1908 case 32:
1909 switch (desc->nr_channels) {
1910 case 1:
1911 return V_008F14_IMG_DATA_FORMAT_32;
1912 case 2:
1913 return V_008F14_IMG_DATA_FORMAT_32_32;
1914 #if 0 /* Not supported for render targets */
1915 case 3:
1916 return V_008F14_IMG_DATA_FORMAT_32_32_32;
1917 #endif
1918 case 4:
1919 return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
1920 }
1921 }
1922
1923 out_unknown:
1924 return ~0;
1925 }
1926
1927 static unsigned si_tex_wrap(unsigned wrap)
1928 {
1929 switch (wrap) {
1930 default:
1931 case PIPE_TEX_WRAP_REPEAT:
1932 return V_008F30_SQ_TEX_WRAP;
1933 case PIPE_TEX_WRAP_CLAMP:
1934 return V_008F30_SQ_TEX_CLAMP_HALF_BORDER;
1935 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1936 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
1937 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1938 return V_008F30_SQ_TEX_CLAMP_BORDER;
1939 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1940 return V_008F30_SQ_TEX_MIRROR;
1941 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1942 return V_008F30_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
1943 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1944 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
1945 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1946 return V_008F30_SQ_TEX_MIRROR_ONCE_BORDER;
1947 }
1948 }
1949
1950 static unsigned si_tex_mipfilter(unsigned filter)
1951 {
1952 switch (filter) {
1953 case PIPE_TEX_MIPFILTER_NEAREST:
1954 return V_008F38_SQ_TEX_Z_FILTER_POINT;
1955 case PIPE_TEX_MIPFILTER_LINEAR:
1956 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
1957 default:
1958 case PIPE_TEX_MIPFILTER_NONE:
1959 return V_008F38_SQ_TEX_Z_FILTER_NONE;
1960 }
1961 }
1962
1963 static unsigned si_tex_compare(unsigned compare)
1964 {
1965 switch (compare) {
1966 default:
1967 case PIPE_FUNC_NEVER:
1968 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
1969 case PIPE_FUNC_LESS:
1970 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
1971 case PIPE_FUNC_EQUAL:
1972 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
1973 case PIPE_FUNC_LEQUAL:
1974 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
1975 case PIPE_FUNC_GREATER:
1976 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
1977 case PIPE_FUNC_NOTEQUAL:
1978 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
1979 case PIPE_FUNC_GEQUAL:
1980 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
1981 case PIPE_FUNC_ALWAYS:
1982 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
1983 }
1984 }
1985
1986 static unsigned si_tex_dim(struct si_screen *sscreen, struct si_texture *tex,
1987 unsigned view_target, unsigned nr_samples)
1988 {
1989 unsigned res_target = tex->buffer.b.b.target;
1990
1991 if (view_target == PIPE_TEXTURE_CUBE ||
1992 view_target == PIPE_TEXTURE_CUBE_ARRAY)
1993 res_target = view_target;
1994 /* If interpreting cubemaps as something else, set 2D_ARRAY. */
1995 else if (res_target == PIPE_TEXTURE_CUBE ||
1996 res_target == PIPE_TEXTURE_CUBE_ARRAY)
1997 res_target = PIPE_TEXTURE_2D_ARRAY;
1998
1999 /* GFX9 allocates 1D textures as 2D. */
2000 if ((res_target == PIPE_TEXTURE_1D ||
2001 res_target == PIPE_TEXTURE_1D_ARRAY) &&
2002 sscreen->info.chip_class == GFX9 &&
2003 tex->surface.u.gfx9.resource_type == RADEON_RESOURCE_2D) {
2004 if (res_target == PIPE_TEXTURE_1D)
2005 res_target = PIPE_TEXTURE_2D;
2006 else
2007 res_target = PIPE_TEXTURE_2D_ARRAY;
2008 }
2009
2010 switch (res_target) {
2011 default:
2012 case PIPE_TEXTURE_1D:
2013 return V_008F1C_SQ_RSRC_IMG_1D;
2014 case PIPE_TEXTURE_1D_ARRAY:
2015 return V_008F1C_SQ_RSRC_IMG_1D_ARRAY;
2016 case PIPE_TEXTURE_2D:
2017 case PIPE_TEXTURE_RECT:
2018 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA :
2019 V_008F1C_SQ_RSRC_IMG_2D;
2020 case PIPE_TEXTURE_2D_ARRAY:
2021 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY :
2022 V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
2023 case PIPE_TEXTURE_3D:
2024 return V_008F1C_SQ_RSRC_IMG_3D;
2025 case PIPE_TEXTURE_CUBE:
2026 case PIPE_TEXTURE_CUBE_ARRAY:
2027 return V_008F1C_SQ_RSRC_IMG_CUBE;
2028 }
2029 }
2030
2031 /*
2032 * Format support testing
2033 */
2034
2035 static bool si_is_sampler_format_supported(struct pipe_screen *screen, enum pipe_format format)
2036 {
2037 struct si_screen *sscreen = (struct si_screen *)screen;
2038
2039 if (sscreen->info.chip_class >= GFX10) {
2040 const struct gfx10_format *fmt = &gfx10_format_table[format];
2041 if (!fmt->img_format || fmt->buffers_only)
2042 return false;
2043 return true;
2044 }
2045
2046 const struct util_format_description *desc = util_format_description(format);
2047 if (!desc)
2048 return false;
2049
2050 return si_translate_texformat(screen, format, desc,
2051 util_format_get_first_non_void_channel(format)) != ~0U;
2052 }
2053
2054 static uint32_t si_translate_buffer_dataformat(struct pipe_screen *screen,
2055 const struct util_format_description *desc,
2056 int first_non_void)
2057 {
2058 int i;
2059
2060 assert(((struct si_screen *)screen)->info.chip_class <= GFX9);
2061
2062 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
2063 return V_008F0C_BUF_DATA_FORMAT_10_11_11;
2064
2065 assert(first_non_void >= 0);
2066
2067 if (desc->nr_channels == 4 &&
2068 desc->channel[0].size == 10 &&
2069 desc->channel[1].size == 10 &&
2070 desc->channel[2].size == 10 &&
2071 desc->channel[3].size == 2)
2072 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
2073
2074 /* See whether the components are of the same size. */
2075 for (i = 0; i < desc->nr_channels; i++) {
2076 if (desc->channel[first_non_void].size != desc->channel[i].size)
2077 return V_008F0C_BUF_DATA_FORMAT_INVALID;
2078 }
2079
2080 switch (desc->channel[first_non_void].size) {
2081 case 8:
2082 switch (desc->nr_channels) {
2083 case 1:
2084 case 3: /* 3 loads */
2085 return V_008F0C_BUF_DATA_FORMAT_8;
2086 case 2:
2087 return V_008F0C_BUF_DATA_FORMAT_8_8;
2088 case 4:
2089 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
2090 }
2091 break;
2092 case 16:
2093 switch (desc->nr_channels) {
2094 case 1:
2095 case 3: /* 3 loads */
2096 return V_008F0C_BUF_DATA_FORMAT_16;
2097 case 2:
2098 return V_008F0C_BUF_DATA_FORMAT_16_16;
2099 case 4:
2100 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
2101 }
2102 break;
2103 case 32:
2104 switch (desc->nr_channels) {
2105 case 1:
2106 return V_008F0C_BUF_DATA_FORMAT_32;
2107 case 2:
2108 return V_008F0C_BUF_DATA_FORMAT_32_32;
2109 case 3:
2110 return V_008F0C_BUF_DATA_FORMAT_32_32_32;
2111 case 4:
2112 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2113 }
2114 break;
2115 case 64:
2116 /* Legacy double formats. */
2117 switch (desc->nr_channels) {
2118 case 1: /* 1 load */
2119 return V_008F0C_BUF_DATA_FORMAT_32_32;
2120 case 2: /* 1 load */
2121 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2122 case 3: /* 3 loads */
2123 return V_008F0C_BUF_DATA_FORMAT_32_32;
2124 case 4: /* 2 loads */
2125 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
2126 }
2127 break;
2128 }
2129
2130 return V_008F0C_BUF_DATA_FORMAT_INVALID;
2131 }
2132
2133 static uint32_t si_translate_buffer_numformat(struct pipe_screen *screen,
2134 const struct util_format_description *desc,
2135 int first_non_void)
2136 {
2137 assert(((struct si_screen *)screen)->info.chip_class <= GFX9);
2138
2139 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
2140 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
2141
2142 assert(first_non_void >= 0);
2143
2144 switch (desc->channel[first_non_void].type) {
2145 case UTIL_FORMAT_TYPE_SIGNED:
2146 case UTIL_FORMAT_TYPE_FIXED:
2147 if (desc->channel[first_non_void].size >= 32 ||
2148 desc->channel[first_non_void].pure_integer)
2149 return V_008F0C_BUF_NUM_FORMAT_SINT;
2150 else if (desc->channel[first_non_void].normalized)
2151 return V_008F0C_BUF_NUM_FORMAT_SNORM;
2152 else
2153 return V_008F0C_BUF_NUM_FORMAT_SSCALED;
2154 break;
2155 case UTIL_FORMAT_TYPE_UNSIGNED:
2156 if (desc->channel[first_non_void].size >= 32 ||
2157 desc->channel[first_non_void].pure_integer)
2158 return V_008F0C_BUF_NUM_FORMAT_UINT;
2159 else if (desc->channel[first_non_void].normalized)
2160 return V_008F0C_BUF_NUM_FORMAT_UNORM;
2161 else
2162 return V_008F0C_BUF_NUM_FORMAT_USCALED;
2163 break;
2164 case UTIL_FORMAT_TYPE_FLOAT:
2165 default:
2166 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
2167 }
2168 }
2169
2170 static unsigned si_is_vertex_format_supported(struct pipe_screen *screen,
2171 enum pipe_format format,
2172 unsigned usage)
2173 {
2174 struct si_screen *sscreen = (struct si_screen *)screen;
2175 const struct util_format_description *desc;
2176 int first_non_void;
2177 unsigned data_format;
2178
2179 assert((usage & ~(PIPE_BIND_SHADER_IMAGE |
2180 PIPE_BIND_SAMPLER_VIEW |
2181 PIPE_BIND_VERTEX_BUFFER)) == 0);
2182
2183 desc = util_format_description(format);
2184 if (!desc)
2185 return 0;
2186
2187 /* There are no native 8_8_8 or 16_16_16 data formats, and we currently
2188 * select 8_8_8_8 and 16_16_16_16 instead. This works reasonably well
2189 * for read-only access (with caveats surrounding bounds checks), but
2190 * obviously fails for write access which we have to implement for
2191 * shader images. Luckily, OpenGL doesn't expect this to be supported
2192 * anyway, and so the only impact is on PBO uploads / downloads, which
2193 * shouldn't be expected to be fast for GL_RGB anyway.
2194 */
2195 if (desc->block.bits == 3 * 8 ||
2196 desc->block.bits == 3 * 16) {
2197 if (usage & (PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW)) {
2198 usage &= ~(PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SAMPLER_VIEW);
2199 if (!usage)
2200 return 0;
2201 }
2202 }
2203
2204 if (sscreen->info.chip_class >= GFX10) {
2205 const struct gfx10_format *fmt = &gfx10_format_table[format];
2206 if (!fmt->img_format || fmt->img_format >= 128)
2207 return 0;
2208 return usage;
2209 }
2210
2211 first_non_void = util_format_get_first_non_void_channel(format);
2212 data_format = si_translate_buffer_dataformat(screen, desc, first_non_void);
2213 if (data_format == V_008F0C_BUF_DATA_FORMAT_INVALID)
2214 return 0;
2215
2216 return usage;
2217 }
2218
2219 static bool si_is_colorbuffer_format_supported(enum pipe_format format)
2220 {
2221 return si_translate_colorformat(format) != V_028C70_COLOR_INVALID &&
2222 si_translate_colorswap(format, false) != ~0U;
2223 }
2224
2225 static bool si_is_zs_format_supported(enum pipe_format format)
2226 {
2227 return si_translate_dbformat(format) != V_028040_Z_INVALID;
2228 }
2229
2230 static bool si_is_format_supported(struct pipe_screen *screen,
2231 enum pipe_format format,
2232 enum pipe_texture_target target,
2233 unsigned sample_count,
2234 unsigned storage_sample_count,
2235 unsigned usage)
2236 {
2237 struct si_screen *sscreen = (struct si_screen *)screen;
2238 unsigned retval = 0;
2239
2240 if (target >= PIPE_MAX_TEXTURE_TYPES) {
2241 PRINT_ERR("radeonsi: unsupported texture type %d\n", target);
2242 return false;
2243 }
2244
2245 if (util_format_get_num_planes(format) >= 2) {
2246 return util_format_planar_is_supported(screen, format, target,
2247 sample_count,
2248 storage_sample_count,
2249 usage);
2250 }
2251
2252 if (MAX2(1, sample_count) < MAX2(1, storage_sample_count))
2253 return false;
2254
2255 if (sample_count > 1) {
2256 if (!screen->get_param(screen, PIPE_CAP_TEXTURE_MULTISAMPLE))
2257 return false;
2258
2259 /* Only power-of-two sample counts are supported. */
2260 if (!util_is_power_of_two_or_zero(sample_count) ||
2261 !util_is_power_of_two_or_zero(storage_sample_count))
2262 return false;
2263
2264 /* MSAA support without framebuffer attachments. */
2265 if (format == PIPE_FORMAT_NONE && sample_count <= 16)
2266 return true;
2267
2268 if (!sscreen->info.has_eqaa_surface_allocator ||
2269 util_format_is_depth_or_stencil(format)) {
2270 /* Color without EQAA or depth/stencil. */
2271 if (sample_count > 8 ||
2272 sample_count != storage_sample_count)
2273 return false;
2274 } else {
2275 /* Color with EQAA. */
2276 if (sample_count > 16 ||
2277 storage_sample_count > 8)
2278 return false;
2279 }
2280 }
2281
2282 if (usage & (PIPE_BIND_SAMPLER_VIEW |
2283 PIPE_BIND_SHADER_IMAGE)) {
2284 if (target == PIPE_BUFFER) {
2285 retval |= si_is_vertex_format_supported(
2286 screen, format, usage & (PIPE_BIND_SAMPLER_VIEW |
2287 PIPE_BIND_SHADER_IMAGE));
2288 } else {
2289 if (si_is_sampler_format_supported(screen, format))
2290 retval |= usage & (PIPE_BIND_SAMPLER_VIEW |
2291 PIPE_BIND_SHADER_IMAGE);
2292 }
2293 }
2294
2295 if ((usage & (PIPE_BIND_RENDER_TARGET |
2296 PIPE_BIND_DISPLAY_TARGET |
2297 PIPE_BIND_SCANOUT |
2298 PIPE_BIND_SHARED |
2299 PIPE_BIND_BLENDABLE)) &&
2300 si_is_colorbuffer_format_supported(format)) {
2301 retval |= usage &
2302 (PIPE_BIND_RENDER_TARGET |
2303 PIPE_BIND_DISPLAY_TARGET |
2304 PIPE_BIND_SCANOUT |
2305 PIPE_BIND_SHARED);
2306 if (!util_format_is_pure_integer(format) &&
2307 !util_format_is_depth_or_stencil(format))
2308 retval |= usage & PIPE_BIND_BLENDABLE;
2309 }
2310
2311 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
2312 si_is_zs_format_supported(format)) {
2313 retval |= PIPE_BIND_DEPTH_STENCIL;
2314 }
2315
2316 if (usage & PIPE_BIND_VERTEX_BUFFER) {
2317 retval |= si_is_vertex_format_supported(screen, format,
2318 PIPE_BIND_VERTEX_BUFFER);
2319 }
2320
2321 if ((usage & PIPE_BIND_LINEAR) &&
2322 !util_format_is_compressed(format) &&
2323 !(usage & PIPE_BIND_DEPTH_STENCIL))
2324 retval |= PIPE_BIND_LINEAR;
2325
2326 return retval == usage;
2327 }
2328
2329 /*
2330 * framebuffer handling
2331 */
2332
2333 static void si_choose_spi_color_formats(struct si_surface *surf,
2334 unsigned format, unsigned swap,
2335 unsigned ntype, bool is_depth)
2336 {
2337 /* Alpha is needed for alpha-to-coverage.
2338 * Blending may be with or without alpha.
2339 */
2340 unsigned normal = 0; /* most optimal, may not support blending or export alpha */
2341 unsigned alpha = 0; /* exports alpha, but may not support blending */
2342 unsigned blend = 0; /* supports blending, but may not export alpha */
2343 unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
2344
2345 /* Choose the SPI color formats. These are required values for RB+.
2346 * Other chips have multiple choices, though they are not necessarily better.
2347 */
2348 switch (format) {
2349 case V_028C70_COLOR_5_6_5:
2350 case V_028C70_COLOR_1_5_5_5:
2351 case V_028C70_COLOR_5_5_5_1:
2352 case V_028C70_COLOR_4_4_4_4:
2353 case V_028C70_COLOR_10_11_11:
2354 case V_028C70_COLOR_11_11_10:
2355 case V_028C70_COLOR_8:
2356 case V_028C70_COLOR_8_8:
2357 case V_028C70_COLOR_8_8_8_8:
2358 case V_028C70_COLOR_10_10_10_2:
2359 case V_028C70_COLOR_2_10_10_10:
2360 if (ntype == V_028C70_NUMBER_UINT)
2361 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
2362 else if (ntype == V_028C70_NUMBER_SINT)
2363 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
2364 else
2365 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
2366 break;
2367
2368 case V_028C70_COLOR_16:
2369 case V_028C70_COLOR_16_16:
2370 case V_028C70_COLOR_16_16_16_16:
2371 if (ntype == V_028C70_NUMBER_UNORM ||
2372 ntype == V_028C70_NUMBER_SNORM) {
2373 /* UNORM16 and SNORM16 don't support blending */
2374 if (ntype == V_028C70_NUMBER_UNORM)
2375 normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
2376 else
2377 normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
2378
2379 /* Use 32 bits per channel for blending. */
2380 if (format == V_028C70_COLOR_16) {
2381 if (swap == V_028C70_SWAP_STD) { /* R */
2382 blend = V_028714_SPI_SHADER_32_R;
2383 blend_alpha = V_028714_SPI_SHADER_32_AR;
2384 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
2385 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
2386 else
2387 assert(0);
2388 } else if (format == V_028C70_COLOR_16_16) {
2389 if (swap == V_028C70_SWAP_STD) { /* RG */
2390 blend = V_028714_SPI_SHADER_32_GR;
2391 blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2392 } else if (swap == V_028C70_SWAP_ALT) /* RA */
2393 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
2394 else
2395 assert(0);
2396 } else /* 16_16_16_16 */
2397 blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2398 } else if (ntype == V_028C70_NUMBER_UINT)
2399 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
2400 else if (ntype == V_028C70_NUMBER_SINT)
2401 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
2402 else if (ntype == V_028C70_NUMBER_FLOAT)
2403 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
2404 else
2405 assert(0);
2406 break;
2407
2408 case V_028C70_COLOR_32:
2409 if (swap == V_028C70_SWAP_STD) { /* R */
2410 blend = normal = V_028714_SPI_SHADER_32_R;
2411 alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
2412 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
2413 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
2414 else
2415 assert(0);
2416 break;
2417
2418 case V_028C70_COLOR_32_32:
2419 if (swap == V_028C70_SWAP_STD) { /* RG */
2420 blend = normal = V_028714_SPI_SHADER_32_GR;
2421 alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
2422 } else if (swap == V_028C70_SWAP_ALT) /* RA */
2423 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
2424 else
2425 assert(0);
2426 break;
2427
2428 case V_028C70_COLOR_32_32_32_32:
2429 case V_028C70_COLOR_8_24:
2430 case V_028C70_COLOR_24_8:
2431 case V_028C70_COLOR_X24_8_32_FLOAT:
2432 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
2433 break;
2434
2435 default:
2436 assert(0);
2437 return;
2438 }
2439
2440 /* The DB->CB copy needs 32_ABGR. */
2441 if (is_depth)
2442 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
2443
2444 surf->spi_shader_col_format = normal;
2445 surf->spi_shader_col_format_alpha = alpha;
2446 surf->spi_shader_col_format_blend = blend;
2447 surf->spi_shader_col_format_blend_alpha = blend_alpha;
2448 }
2449
2450 static void si_initialize_color_surface(struct si_context *sctx,
2451 struct si_surface *surf)
2452 {
2453 struct si_texture *tex = (struct si_texture*)surf->base.texture;
2454 unsigned color_info, color_attrib;
2455 unsigned format, swap, ntype, endian;
2456 const struct util_format_description *desc;
2457 int firstchan;
2458 unsigned blend_clamp = 0, blend_bypass = 0;
2459
2460 desc = util_format_description(surf->base.format);
2461 for (firstchan = 0; firstchan < 4; firstchan++) {
2462 if (desc->channel[firstchan].type != UTIL_FORMAT_TYPE_VOID) {
2463 break;
2464 }
2465 }
2466 if (firstchan == 4 || desc->channel[firstchan].type == UTIL_FORMAT_TYPE_FLOAT) {
2467 ntype = V_028C70_NUMBER_FLOAT;
2468 } else {
2469 ntype = V_028C70_NUMBER_UNORM;
2470 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
2471 ntype = V_028C70_NUMBER_SRGB;
2472 else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_SIGNED) {
2473 if (desc->channel[firstchan].pure_integer) {
2474 ntype = V_028C70_NUMBER_SINT;
2475 } else {
2476 assert(desc->channel[firstchan].normalized);
2477 ntype = V_028C70_NUMBER_SNORM;
2478 }
2479 } else if (desc->channel[firstchan].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2480 if (desc->channel[firstchan].pure_integer) {
2481 ntype = V_028C70_NUMBER_UINT;
2482 } else {
2483 assert(desc->channel[firstchan].normalized);
2484 ntype = V_028C70_NUMBER_UNORM;
2485 }
2486 }
2487 }
2488
2489 format = si_translate_colorformat(surf->base.format);
2490 if (format == V_028C70_COLOR_INVALID) {
2491 PRINT_ERR("Invalid CB format: %d, disabling CB.\n", surf->base.format);
2492 }
2493 assert(format != V_028C70_COLOR_INVALID);
2494 swap = si_translate_colorswap(surf->base.format, false);
2495 endian = si_colorformat_endian_swap(format);
2496
2497 /* blend clamp should be set for all NORM/SRGB types */
2498 if (ntype == V_028C70_NUMBER_UNORM ||
2499 ntype == V_028C70_NUMBER_SNORM ||
2500 ntype == V_028C70_NUMBER_SRGB)
2501 blend_clamp = 1;
2502
2503 /* set blend bypass according to docs if SINT/UINT or
2504 8/24 COLOR variants */
2505 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
2506 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
2507 format == V_028C70_COLOR_X24_8_32_FLOAT) {
2508 blend_clamp = 0;
2509 blend_bypass = 1;
2510 }
2511
2512 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) {
2513 if (format == V_028C70_COLOR_8 ||
2514 format == V_028C70_COLOR_8_8 ||
2515 format == V_028C70_COLOR_8_8_8_8)
2516 surf->color_is_int8 = true;
2517 else if (format == V_028C70_COLOR_10_10_10_2 ||
2518 format == V_028C70_COLOR_2_10_10_10)
2519 surf->color_is_int10 = true;
2520 }
2521
2522 color_info = S_028C70_FORMAT(format) |
2523 S_028C70_COMP_SWAP(swap) |
2524 S_028C70_BLEND_CLAMP(blend_clamp) |
2525 S_028C70_BLEND_BYPASS(blend_bypass) |
2526 S_028C70_SIMPLE_FLOAT(1) |
2527 S_028C70_ROUND_MODE(ntype != V_028C70_NUMBER_UNORM &&
2528 ntype != V_028C70_NUMBER_SNORM &&
2529 ntype != V_028C70_NUMBER_SRGB &&
2530 format != V_028C70_COLOR_8_24 &&
2531 format != V_028C70_COLOR_24_8) |
2532 S_028C70_NUMBER_TYPE(ntype) |
2533 S_028C70_ENDIAN(endian);
2534
2535 /* Intensity is implemented as Red, so treat it that way. */
2536 color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == PIPE_SWIZZLE_1 ||
2537 util_format_is_intensity(surf->base.format));
2538
2539 if (tex->buffer.b.b.nr_samples > 1) {
2540 unsigned log_samples = util_logbase2(tex->buffer.b.b.nr_samples);
2541 unsigned log_fragments = util_logbase2(tex->buffer.b.b.nr_storage_samples);
2542
2543 color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
2544 S_028C74_NUM_FRAGMENTS(log_fragments);
2545
2546 if (tex->surface.fmask_offset) {
2547 color_info |= S_028C70_COMPRESSION(1);
2548 unsigned fmask_bankh = util_logbase2(tex->surface.u.legacy.fmask.bankh);
2549
2550 if (sctx->chip_class == GFX6) {
2551 /* due to a hw bug, FMASK_BANK_HEIGHT must be set on GFX6 too */
2552 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
2553 }
2554 }
2555 }
2556
2557 if (sctx->chip_class >= GFX10) {
2558 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
2559
2560 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2561 64 for APU because all of our APUs to date use DIMMs which have
2562 a request granularity size of 64B while all other chips have a
2563 32B request size */
2564 if (!sctx->screen->info.has_dedicated_vram)
2565 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
2566
2567 surf->cb_dcc_control =
2568 S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(V_028C78_MAX_BLOCK_SIZE_256B) |
2569 S_028C78_MAX_COMPRESSED_BLOCK_SIZE(V_028C78_MAX_BLOCK_SIZE_128B) |
2570 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
2571 S_028C78_INDEPENDENT_64B_BLOCKS(0) |
2572 S_028C78_INDEPENDENT_128B_BLOCKS(1);
2573 } else if (sctx->chip_class >= GFX8) {
2574 unsigned max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_256B;
2575 unsigned min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_32B;
2576
2577 /* amdvlk: [min-compressed-block-size] should be set to 32 for dGPU and
2578 64 for APU because all of our APUs to date use DIMMs which have
2579 a request granularity size of 64B while all other chips have a
2580 32B request size */
2581 if (!sctx->screen->info.has_dedicated_vram)
2582 min_compressed_block_size = V_028C78_MIN_BLOCK_SIZE_64B;
2583
2584 if (tex->buffer.b.b.nr_storage_samples > 1) {
2585 if (tex->surface.bpe == 1)
2586 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_64B;
2587 else if (tex->surface.bpe == 2)
2588 max_uncompressed_block_size = V_028C78_MAX_BLOCK_SIZE_128B;
2589 }
2590
2591 surf->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
2592 S_028C78_MIN_COMPRESSED_BLOCK_SIZE(min_compressed_block_size) |
2593 S_028C78_INDEPENDENT_64B_BLOCKS(1);
2594 }
2595
2596 /* This must be set for fast clear to work without FMASK. */
2597 if (!tex->surface.fmask_size && sctx->chip_class == GFX6) {
2598 unsigned bankh = util_logbase2(tex->surface.u.legacy.bankh);
2599 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
2600 }
2601
2602 /* GFX10 field has the same base shift as the GFX6 field */
2603 unsigned color_view = S_028C6C_SLICE_START(surf->base.u.tex.first_layer) |
2604 S_028C6C_SLICE_MAX_GFX10(surf->base.u.tex.last_layer);
2605 unsigned mip0_depth = util_max_layer(&tex->buffer.b.b, 0);
2606
2607 if (sctx->chip_class >= GFX10) {
2608 color_view |= S_028C6C_MIP_LEVEL_GFX10(surf->base.u.tex.level);
2609
2610 surf->cb_color_attrib3 = S_028EE0_MIP0_DEPTH(mip0_depth) |
2611 S_028EE0_RESOURCE_TYPE(tex->surface.u.gfx9.resource_type) |
2612 S_028EE0_RESOURCE_LEVEL(1);
2613 } else if (sctx->chip_class == GFX9) {
2614 color_view |= S_028C6C_MIP_LEVEL_GFX9(surf->base.u.tex.level);
2615 color_attrib |= S_028C74_MIP0_DEPTH(mip0_depth) |
2616 S_028C74_RESOURCE_TYPE(tex->surface.u.gfx9.resource_type);
2617 }
2618
2619 if (sctx->chip_class >= GFX9) {
2620 surf->cb_color_attrib2 = S_028C68_MIP0_WIDTH(surf->width0 - 1) |
2621 S_028C68_MIP0_HEIGHT(surf->height0 - 1) |
2622 S_028C68_MAX_MIP(tex->buffer.b.b.last_level);
2623 }
2624
2625 surf->cb_color_view = color_view;
2626 surf->cb_color_info = color_info;
2627 surf->cb_color_attrib = color_attrib;
2628
2629 /* Determine pixel shader export format */
2630 si_choose_spi_color_formats(surf, format, swap, ntype, tex->is_depth);
2631
2632 surf->color_initialized = true;
2633 }
2634
2635 static void si_init_depth_surface(struct si_context *sctx,
2636 struct si_surface *surf)
2637 {
2638 struct si_texture *tex = (struct si_texture*)surf->base.texture;
2639 unsigned level = surf->base.u.tex.level;
2640 unsigned format, stencil_format;
2641 uint32_t z_info, s_info;
2642
2643 format = si_translate_dbformat(tex->db_render_format);
2644 stencil_format = tex->surface.has_stencil ?
2645 V_028044_STENCIL_8 : V_028044_STENCIL_INVALID;
2646
2647 assert(format != V_028040_Z_INVALID);
2648 if (format == V_028040_Z_INVALID)
2649 PRINT_ERR("Invalid DB format: %d, disabling DB.\n", tex->buffer.b.b.format);
2650
2651 surf->db_depth_view = S_028008_SLICE_START(surf->base.u.tex.first_layer) |
2652 S_028008_SLICE_MAX(surf->base.u.tex.last_layer);
2653 surf->db_htile_data_base = 0;
2654 surf->db_htile_surface = 0;
2655
2656 if (sctx->chip_class >= GFX10) {
2657 surf->db_depth_view |= S_028008_SLICE_START_HI(surf->base.u.tex.first_layer >> 11) |
2658 S_028008_SLICE_MAX_HI(surf->base.u.tex.last_layer >> 11);
2659 }
2660
2661 if (sctx->chip_class >= GFX9) {
2662 assert(tex->surface.u.gfx9.surf_offset == 0);
2663 surf->db_depth_base = tex->buffer.gpu_address >> 8;
2664 surf->db_stencil_base = (tex->buffer.gpu_address +
2665 tex->surface.u.gfx9.stencil_offset) >> 8;
2666 z_info = S_028038_FORMAT(format) |
2667 S_028038_NUM_SAMPLES(util_logbase2(tex->buffer.b.b.nr_samples)) |
2668 S_028038_SW_MODE(tex->surface.u.gfx9.surf.swizzle_mode) |
2669 S_028038_MAXMIP(tex->buffer.b.b.last_level);
2670 s_info = S_02803C_FORMAT(stencil_format) |
2671 S_02803C_SW_MODE(tex->surface.u.gfx9.stencil.swizzle_mode);
2672
2673 if (sctx->chip_class == GFX9) {
2674 surf->db_z_info2 = S_028068_EPITCH(tex->surface.u.gfx9.surf.epitch);
2675 surf->db_stencil_info2 = S_02806C_EPITCH(tex->surface.u.gfx9.stencil.epitch);
2676 }
2677 surf->db_depth_view |= S_028008_MIPID(level);
2678 surf->db_depth_size = S_02801C_X_MAX(tex->buffer.b.b.width0 - 1) |
2679 S_02801C_Y_MAX(tex->buffer.b.b.height0 - 1);
2680
2681 if (si_htile_enabled(tex, level, PIPE_MASK_ZS)) {
2682 z_info |= S_028038_TILE_SURFACE_ENABLE(1) |
2683 S_028038_ALLOW_EXPCLEAR(1);
2684
2685 if (tex->tc_compatible_htile) {
2686 unsigned max_zplanes = 4;
2687
2688 if (tex->db_render_format == PIPE_FORMAT_Z16_UNORM &&
2689 tex->buffer.b.b.nr_samples > 1)
2690 max_zplanes = 2;
2691
2692 z_info |= S_028038_DECOMPRESS_ON_N_ZPLANES(max_zplanes + 1);
2693
2694 if (sctx->chip_class >= GFX10) {
2695 z_info |= S_028040_ITERATE_FLUSH(1);
2696 s_info |= S_028044_ITERATE_FLUSH(!tex->htile_stencil_disabled);
2697 } else {
2698 z_info |= S_028038_ITERATE_FLUSH(1);
2699 s_info |= S_02803C_ITERATE_FLUSH(1);
2700 }
2701 }
2702
2703 if (tex->surface.has_stencil && !tex->htile_stencil_disabled) {
2704 /* Stencil buffer workaround ported from the GFX6-GFX8 code.
2705 * See that for explanation.
2706 */
2707 s_info |= S_02803C_ALLOW_EXPCLEAR(tex->buffer.b.b.nr_samples <= 1);
2708 } else {
2709 /* Use all HTILE for depth if there's no stencil. */
2710 s_info |= S_02803C_TILE_STENCIL_DISABLE(1);
2711 }
2712
2713 surf->db_htile_data_base = (tex->buffer.gpu_address +
2714 tex->surface.htile_offset) >> 8;
2715 surf->db_htile_surface = S_028ABC_FULL_CACHE(1) |
2716 S_028ABC_PIPE_ALIGNED(tex->surface.u.gfx9.htile.pipe_aligned);
2717 if (sctx->chip_class == GFX9) {
2718 surf->db_htile_surface |=
2719 S_028ABC_RB_ALIGNED(tex->surface.u.gfx9.htile.rb_aligned);
2720 }
2721 }
2722 } else {
2723 /* GFX6-GFX8 */
2724 struct legacy_surf_level *levelinfo = &tex->surface.u.legacy.level[level];
2725
2726 assert(levelinfo->nblk_x % 8 == 0 && levelinfo->nblk_y % 8 == 0);
2727
2728 surf->db_depth_base = (tex->buffer.gpu_address +
2729 tex->surface.u.legacy.level[level].offset) >> 8;
2730 surf->db_stencil_base = (tex->buffer.gpu_address +
2731 tex->surface.u.legacy.stencil_level[level].offset) >> 8;
2732
2733 z_info = S_028040_FORMAT(format) |
2734 S_028040_NUM_SAMPLES(util_logbase2(tex->buffer.b.b.nr_samples));
2735 s_info = S_028044_FORMAT(stencil_format);
2736 surf->db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(!tex->tc_compatible_htile);
2737
2738 if (sctx->chip_class >= GFX7) {
2739 struct radeon_info *info = &sctx->screen->info;
2740 unsigned index = tex->surface.u.legacy.tiling_index[level];
2741 unsigned stencil_index = tex->surface.u.legacy.stencil_tiling_index[level];
2742 unsigned macro_index = tex->surface.u.legacy.macro_tile_index;
2743 unsigned tile_mode = info->si_tile_mode_array[index];
2744 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
2745 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
2746
2747 surf->db_depth_info |=
2748 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
2749 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
2750 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
2751 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
2752 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
2753 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
2754 z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
2755 s_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
2756 } else {
2757 unsigned tile_mode_index = si_tile_mode_index(tex, level, false);
2758 z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
2759 tile_mode_index = si_tile_mode_index(tex, level, true);
2760 s_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
2761 }
2762
2763 surf->db_depth_size = S_028058_PITCH_TILE_MAX((levelinfo->nblk_x / 8) - 1) |
2764 S_028058_HEIGHT_TILE_MAX((levelinfo->nblk_y / 8) - 1);
2765 surf->db_depth_slice = S_02805C_SLICE_TILE_MAX((levelinfo->nblk_x *
2766 levelinfo->nblk_y) / 64 - 1);
2767
2768 if (si_htile_enabled(tex, level, PIPE_MASK_ZS)) {
2769 z_info |= S_028040_TILE_SURFACE_ENABLE(1) |
2770 S_028040_ALLOW_EXPCLEAR(1);
2771
2772 if (tex->surface.has_stencil) {
2773 /* Workaround: For a not yet understood reason, the
2774 * combination of MSAA, fast stencil clear and stencil
2775 * decompress messes with subsequent stencil buffer
2776 * uses. Problem was reproduced on Verde, Bonaire,
2777 * Tonga, and Carrizo.
2778 *
2779 * Disabling EXPCLEAR works around the problem.
2780 *
2781 * Check piglit's arb_texture_multisample-stencil-clear
2782 * test if you want to try changing this.
2783 */
2784 if (tex->buffer.b.b.nr_samples <= 1)
2785 s_info |= S_028044_ALLOW_EXPCLEAR(1);
2786 } else if (!tex->tc_compatible_htile) {
2787 /* Use all of the htile_buffer for depth if there's no stencil.
2788 * This must not be set when TC-compatible HTILE is enabled
2789 * due to a hw bug.
2790 */
2791 s_info |= S_028044_TILE_STENCIL_DISABLE(1);
2792 }
2793
2794 surf->db_htile_data_base = (tex->buffer.gpu_address +
2795 tex->surface.htile_offset) >> 8;
2796 surf->db_htile_surface = S_028ABC_FULL_CACHE(1);
2797
2798 if (tex->tc_compatible_htile) {
2799 surf->db_htile_surface |= S_028ABC_TC_COMPATIBLE(1);
2800
2801 /* 0 = full compression. N = only compress up to N-1 Z planes. */
2802 if (tex->buffer.b.b.nr_samples <= 1)
2803 z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(5);
2804 else if (tex->buffer.b.b.nr_samples <= 4)
2805 z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(3);
2806 else
2807 z_info |= S_028040_DECOMPRESS_ON_N_ZPLANES(2);
2808 }
2809 }
2810 }
2811
2812 surf->db_z_info = z_info;
2813 surf->db_stencil_info = s_info;
2814
2815 surf->depth_initialized = true;
2816 }
2817
2818 void si_update_fb_dirtiness_after_rendering(struct si_context *sctx)
2819 {
2820 if (sctx->decompression_enabled)
2821 return;
2822
2823 if (sctx->framebuffer.state.zsbuf) {
2824 struct pipe_surface *surf = sctx->framebuffer.state.zsbuf;
2825 struct si_texture *tex = (struct si_texture *)surf->texture;
2826
2827 tex->dirty_level_mask |= 1 << surf->u.tex.level;
2828
2829 if (tex->surface.has_stencil)
2830 tex->stencil_dirty_level_mask |= 1 << surf->u.tex.level;
2831 }
2832
2833 unsigned compressed_cb_mask = sctx->framebuffer.compressed_cb_mask;
2834 while (compressed_cb_mask) {
2835 unsigned i = u_bit_scan(&compressed_cb_mask);
2836 struct pipe_surface *surf = sctx->framebuffer.state.cbufs[i];
2837 struct si_texture *tex = (struct si_texture*)surf->texture;
2838
2839 if (tex->surface.fmask_offset) {
2840 tex->dirty_level_mask |= 1 << surf->u.tex.level;
2841 tex->fmask_is_identity = false;
2842 }
2843 if (tex->dcc_gather_statistics)
2844 tex->separate_dcc_dirty = true;
2845 }
2846 }
2847
2848 static void si_dec_framebuffer_counters(const struct pipe_framebuffer_state *state)
2849 {
2850 for (int i = 0; i < state->nr_cbufs; ++i) {
2851 struct si_surface *surf = NULL;
2852 struct si_texture *tex;
2853
2854 if (!state->cbufs[i])
2855 continue;
2856 surf = (struct si_surface*)state->cbufs[i];
2857 tex = (struct si_texture*)surf->base.texture;
2858
2859 p_atomic_dec(&tex->framebuffers_bound);
2860 }
2861 }
2862
2863 static void si_set_framebuffer_state(struct pipe_context *ctx,
2864 const struct pipe_framebuffer_state *state)
2865 {
2866 struct si_context *sctx = (struct si_context *)ctx;
2867 struct si_surface *surf = NULL;
2868 struct si_texture *tex;
2869 bool old_any_dst_linear = sctx->framebuffer.any_dst_linear;
2870 unsigned old_nr_samples = sctx->framebuffer.nr_samples;
2871 unsigned old_colorbuf_enabled_4bit = sctx->framebuffer.colorbuf_enabled_4bit;
2872 bool old_has_zsbuf = !!sctx->framebuffer.state.zsbuf;
2873 bool old_has_stencil =
2874 old_has_zsbuf &&
2875 ((struct si_texture*)sctx->framebuffer.state.zsbuf->texture)->surface.has_stencil;
2876 bool unbound = false;
2877 int i;
2878
2879 /* Reject zero-sized framebuffers due to a hw bug on GFX6 that occurs
2880 * when PA_SU_HARDWARE_SCREEN_OFFSET != 0 and any_scissor.BR_X/Y <= 0.
2881 * We could implement the full workaround here, but it's a useless case.
2882 */
2883 if ((!state->width || !state->height) && (state->nr_cbufs || state->zsbuf)) {
2884 unreachable("the framebuffer shouldn't have zero area");
2885 return;
2886 }
2887
2888 si_update_fb_dirtiness_after_rendering(sctx);
2889
2890 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
2891 if (!sctx->framebuffer.state.cbufs[i])
2892 continue;
2893
2894 tex = (struct si_texture*)sctx->framebuffer.state.cbufs[i]->texture;
2895 if (tex->dcc_gather_statistics)
2896 vi_separate_dcc_stop_query(sctx, tex);
2897 }
2898
2899 /* Disable DCC if the formats are incompatible. */
2900 for (i = 0; i < state->nr_cbufs; i++) {
2901 if (!state->cbufs[i])
2902 continue;
2903
2904 surf = (struct si_surface*)state->cbufs[i];
2905 tex = (struct si_texture*)surf->base.texture;
2906
2907 if (!surf->dcc_incompatible)
2908 continue;
2909
2910 /* Since the DCC decompression calls back into set_framebuffer-
2911 * _state, we need to unbind the framebuffer, so that
2912 * vi_separate_dcc_stop_query isn't called twice with the same
2913 * color buffer.
2914 */
2915 if (!unbound) {
2916 util_copy_framebuffer_state(&sctx->framebuffer.state, NULL);
2917 unbound = true;
2918 }
2919
2920 if (vi_dcc_enabled(tex, surf->base.u.tex.level))
2921 if (!si_texture_disable_dcc(sctx, tex))
2922 si_decompress_dcc(sctx, tex);
2923
2924 surf->dcc_incompatible = false;
2925 }
2926
2927 /* Only flush TC when changing the framebuffer state, because
2928 * the only client not using TC that can change textures is
2929 * the framebuffer.
2930 *
2931 * Wait for compute shaders because of possible transitions:
2932 * - FB write -> shader read
2933 * - shader write -> FB read
2934 *
2935 * DB caches are flushed on demand (using si_decompress_textures).
2936 *
2937 * When MSAA is enabled, CB and TC caches are flushed on demand
2938 * (after FMASK decompression). Shader write -> FB read transitions
2939 * cannot happen for MSAA textures, because MSAA shader images are
2940 * not supported.
2941 *
2942 * Only flush and wait for CB if there is actually a bound color buffer.
2943 */
2944 if (sctx->framebuffer.uncompressed_cb_mask) {
2945 si_make_CB_shader_coherent(sctx, sctx->framebuffer.nr_samples,
2946 sctx->framebuffer.CB_has_shader_readable_metadata,
2947 sctx->framebuffer.all_DCC_pipe_aligned);
2948 }
2949
2950 sctx->flags |= SI_CONTEXT_CS_PARTIAL_FLUSH;
2951
2952 /* u_blitter doesn't invoke depth decompression when it does multiple
2953 * blits in a row, but the only case when it matters for DB is when
2954 * doing generate_mipmap. So here we flush DB manually between
2955 * individual generate_mipmap blits.
2956 * Note that lower mipmap levels aren't compressed.
2957 */
2958 if (sctx->generate_mipmap_for_depth) {
2959 si_make_DB_shader_coherent(sctx, 1, false,
2960 sctx->framebuffer.DB_has_shader_readable_metadata);
2961 } else if (sctx->chip_class == GFX9) {
2962 /* It appears that DB metadata "leaks" in a sequence of:
2963 * - depth clear
2964 * - DCC decompress for shader image writes (with DB disabled)
2965 * - render with DEPTH_BEFORE_SHADER=1
2966 * Flushing DB metadata works around the problem.