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