radeonsi: set CB_COLORn_INFO.SIMPLE_FLOAT
[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 "si_shader.h"
29 #include "sid.h"
30 #include "radeon/r600_cs.h"
31
32 #include "util/u_dual_blend.h"
33 #include "util/u_format.h"
34 #include "util/u_format_s3tc.h"
35 #include "util/u_memory.h"
36 #include "util/u_pstipple.h"
37 #include "util/u_resource.h"
38
39 /* Initialize an external atom (owned by ../radeon). */
40 static void
41 si_init_external_atom(struct si_context *sctx, struct r600_atom *atom,
42 struct r600_atom **list_elem)
43 {
44 atom->id = list_elem - sctx->atoms.array + 1;
45 *list_elem = atom;
46 }
47
48 /* Initialize an atom owned by radeonsi. */
49 void si_init_atom(struct si_context *sctx, struct r600_atom *atom,
50 struct r600_atom **list_elem,
51 void (*emit_func)(struct si_context *ctx, struct r600_atom *state))
52 {
53 atom->emit = (void*)emit_func;
54 atom->id = list_elem - sctx->atoms.array + 1; /* index+1 in the atom array */
55 *list_elem = atom;
56 }
57
58 static unsigned si_map_swizzle(unsigned swizzle)
59 {
60 switch (swizzle) {
61 case PIPE_SWIZZLE_Y:
62 return V_008F0C_SQ_SEL_Y;
63 case PIPE_SWIZZLE_Z:
64 return V_008F0C_SQ_SEL_Z;
65 case PIPE_SWIZZLE_W:
66 return V_008F0C_SQ_SEL_W;
67 case PIPE_SWIZZLE_0:
68 return V_008F0C_SQ_SEL_0;
69 case PIPE_SWIZZLE_1:
70 return V_008F0C_SQ_SEL_1;
71 default: /* PIPE_SWIZZLE_X */
72 return V_008F0C_SQ_SEL_X;
73 }
74 }
75
76 static uint32_t S_FIXED(float value, uint32_t frac_bits)
77 {
78 return value * (1 << frac_bits);
79 }
80
81 /* 12.4 fixed-point */
82 static unsigned si_pack_float_12p4(float x)
83 {
84 return x <= 0 ? 0 :
85 x >= 4096 ? 0xffff : x * 16;
86 }
87
88 /*
89 * Inferred framebuffer and blender state.
90 *
91 * One of the reasons CB_TARGET_MASK must be derived from the framebuffer state
92 * is that:
93 * - The blend state mask is 0xf most of the time.
94 * - The COLOR1 format isn't INVALID because of possible dual-source blending,
95 * so COLOR1 is enabled pretty much all the time.
96 * So CB_TARGET_MASK is the only register that can disable COLOR1.
97 *
98 * Another reason is to avoid a hang with dual source blending.
99 */
100 static void si_emit_cb_render_state(struct si_context *sctx, struct r600_atom *atom)
101 {
102 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
103 struct si_state_blend *blend = sctx->queued.named.blend;
104 uint32_t cb_target_mask = 0, i;
105
106 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++)
107 if (sctx->framebuffer.state.cbufs[i])
108 cb_target_mask |= 0xf << (4*i);
109
110 if (blend)
111 cb_target_mask &= blend->cb_target_mask;
112
113 /* Avoid a hang that happens when dual source blending is enabled
114 * but there is not enough color outputs. This is undefined behavior,
115 * so disable color writes completely.
116 *
117 * Reproducible with Unigine Heaven 4.0 and drirc missing.
118 */
119 if (blend && blend->dual_src_blend &&
120 sctx->ps_shader.cso &&
121 (sctx->ps_shader.cso->info.colors_written & 0x3) != 0x3)
122 cb_target_mask = 0;
123
124 radeon_set_context_reg(cs, R_028238_CB_TARGET_MASK, cb_target_mask);
125
126 /* STONEY-specific register settings. */
127 if (sctx->b.family == CHIP_STONEY) {
128 unsigned spi_shader_col_format =
129 sctx->ps_shader.cso ?
130 sctx->ps_shader.current->key.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 if (sctx->screen->b.debug_flags & DBG_NO_RB_PLUS) {
252 sx_ps_downconvert = 0;
253 sx_blend_opt_epsilon = 0;
254 sx_blend_opt_control = 0;
255 }
256
257 radeon_set_context_reg_seq(cs, R_028754_SX_PS_DOWNCONVERT, 3);
258 radeon_emit(cs, sx_ps_downconvert); /* R_028754_SX_PS_DOWNCONVERT */
259 radeon_emit(cs, sx_blend_opt_epsilon); /* R_028758_SX_BLEND_OPT_EPSILON */
260 radeon_emit(cs, sx_blend_opt_control); /* R_02875C_SX_BLEND_OPT_CONTROL */
261 }
262 }
263
264 /*
265 * Blender functions
266 */
267
268 static uint32_t si_translate_blend_function(int blend_func)
269 {
270 switch (blend_func) {
271 case PIPE_BLEND_ADD:
272 return V_028780_COMB_DST_PLUS_SRC;
273 case PIPE_BLEND_SUBTRACT:
274 return V_028780_COMB_SRC_MINUS_DST;
275 case PIPE_BLEND_REVERSE_SUBTRACT:
276 return V_028780_COMB_DST_MINUS_SRC;
277 case PIPE_BLEND_MIN:
278 return V_028780_COMB_MIN_DST_SRC;
279 case PIPE_BLEND_MAX:
280 return V_028780_COMB_MAX_DST_SRC;
281 default:
282 R600_ERR("Unknown blend function %d\n", blend_func);
283 assert(0);
284 break;
285 }
286 return 0;
287 }
288
289 static uint32_t si_translate_blend_factor(int blend_fact)
290 {
291 switch (blend_fact) {
292 case PIPE_BLENDFACTOR_ONE:
293 return V_028780_BLEND_ONE;
294 case PIPE_BLENDFACTOR_SRC_COLOR:
295 return V_028780_BLEND_SRC_COLOR;
296 case PIPE_BLENDFACTOR_SRC_ALPHA:
297 return V_028780_BLEND_SRC_ALPHA;
298 case PIPE_BLENDFACTOR_DST_ALPHA:
299 return V_028780_BLEND_DST_ALPHA;
300 case PIPE_BLENDFACTOR_DST_COLOR:
301 return V_028780_BLEND_DST_COLOR;
302 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
303 return V_028780_BLEND_SRC_ALPHA_SATURATE;
304 case PIPE_BLENDFACTOR_CONST_COLOR:
305 return V_028780_BLEND_CONSTANT_COLOR;
306 case PIPE_BLENDFACTOR_CONST_ALPHA:
307 return V_028780_BLEND_CONSTANT_ALPHA;
308 case PIPE_BLENDFACTOR_ZERO:
309 return V_028780_BLEND_ZERO;
310 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
311 return V_028780_BLEND_ONE_MINUS_SRC_COLOR;
312 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
313 return V_028780_BLEND_ONE_MINUS_SRC_ALPHA;
314 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
315 return V_028780_BLEND_ONE_MINUS_DST_ALPHA;
316 case PIPE_BLENDFACTOR_INV_DST_COLOR:
317 return V_028780_BLEND_ONE_MINUS_DST_COLOR;
318 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
319 return V_028780_BLEND_ONE_MINUS_CONSTANT_COLOR;
320 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
321 return V_028780_BLEND_ONE_MINUS_CONSTANT_ALPHA;
322 case PIPE_BLENDFACTOR_SRC1_COLOR:
323 return V_028780_BLEND_SRC1_COLOR;
324 case PIPE_BLENDFACTOR_SRC1_ALPHA:
325 return V_028780_BLEND_SRC1_ALPHA;
326 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
327 return V_028780_BLEND_INV_SRC1_COLOR;
328 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
329 return V_028780_BLEND_INV_SRC1_ALPHA;
330 default:
331 R600_ERR("Bad blend factor %d not supported!\n", blend_fact);
332 assert(0);
333 break;
334 }
335 return 0;
336 }
337
338 static uint32_t si_translate_blend_opt_function(int blend_func)
339 {
340 switch (blend_func) {
341 case PIPE_BLEND_ADD:
342 return V_028760_OPT_COMB_ADD;
343 case PIPE_BLEND_SUBTRACT:
344 return V_028760_OPT_COMB_SUBTRACT;
345 case PIPE_BLEND_REVERSE_SUBTRACT:
346 return V_028760_OPT_COMB_REVSUBTRACT;
347 case PIPE_BLEND_MIN:
348 return V_028760_OPT_COMB_MIN;
349 case PIPE_BLEND_MAX:
350 return V_028760_OPT_COMB_MAX;
351 default:
352 return V_028760_OPT_COMB_BLEND_DISABLED;
353 }
354 }
355
356 static uint32_t si_translate_blend_opt_factor(int blend_fact, bool is_alpha)
357 {
358 switch (blend_fact) {
359 case PIPE_BLENDFACTOR_ZERO:
360 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_ALL;
361 case PIPE_BLENDFACTOR_ONE:
362 return V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE;
363 case PIPE_BLENDFACTOR_SRC_COLOR:
364 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0
365 : V_028760_BLEND_OPT_PRESERVE_C1_IGNORE_C0;
366 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
367 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1
368 : V_028760_BLEND_OPT_PRESERVE_C0_IGNORE_C1;
369 case PIPE_BLENDFACTOR_SRC_ALPHA:
370 return V_028760_BLEND_OPT_PRESERVE_A1_IGNORE_A0;
371 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
372 return V_028760_BLEND_OPT_PRESERVE_A0_IGNORE_A1;
373 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
374 return is_alpha ? V_028760_BLEND_OPT_PRESERVE_ALL_IGNORE_NONE
375 : V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
376 default:
377 return V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
378 }
379 }
380
381 /**
382 * Get rid of DST in the blend factors by commuting the operands:
383 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
384 */
385 static void si_blend_remove_dst(unsigned *func, unsigned *src_factor,
386 unsigned *dst_factor, unsigned expected_dst,
387 unsigned replacement_src)
388 {
389 if (*src_factor == expected_dst &&
390 *dst_factor == PIPE_BLENDFACTOR_ZERO) {
391 *src_factor = PIPE_BLENDFACTOR_ZERO;
392 *dst_factor = replacement_src;
393
394 /* Commuting the operands requires reversing subtractions. */
395 if (*func == PIPE_BLEND_SUBTRACT)
396 *func = PIPE_BLEND_REVERSE_SUBTRACT;
397 else if (*func == PIPE_BLEND_REVERSE_SUBTRACT)
398 *func = PIPE_BLEND_SUBTRACT;
399 }
400 }
401
402 static bool si_blend_factor_uses_dst(unsigned factor)
403 {
404 return factor == PIPE_BLENDFACTOR_DST_COLOR ||
405 factor == PIPE_BLENDFACTOR_DST_ALPHA ||
406 factor == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
407 factor == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
408 factor == PIPE_BLENDFACTOR_INV_DST_COLOR;
409 }
410
411 static void *si_create_blend_state_mode(struct pipe_context *ctx,
412 const struct pipe_blend_state *state,
413 unsigned mode)
414 {
415 struct si_context *sctx = (struct si_context*)ctx;
416 struct si_state_blend *blend = CALLOC_STRUCT(si_state_blend);
417 struct si_pm4_state *pm4 = &blend->pm4;
418 uint32_t sx_mrt_blend_opt[8] = {0};
419 uint32_t color_control = 0;
420
421 if (!blend)
422 return NULL;
423
424 blend->alpha_to_coverage = state->alpha_to_coverage;
425 blend->alpha_to_one = state->alpha_to_one;
426 blend->dual_src_blend = util_blend_state_is_dual(state, 0);
427
428 if (state->logicop_enable) {
429 color_control |= S_028808_ROP3(state->logicop_func | (state->logicop_func << 4));
430 } else {
431 color_control |= S_028808_ROP3(0xcc);
432 }
433
434 si_pm4_set_reg(pm4, R_028B70_DB_ALPHA_TO_MASK,
435 S_028B70_ALPHA_TO_MASK_ENABLE(state->alpha_to_coverage) |
436 S_028B70_ALPHA_TO_MASK_OFFSET0(2) |
437 S_028B70_ALPHA_TO_MASK_OFFSET1(2) |
438 S_028B70_ALPHA_TO_MASK_OFFSET2(2) |
439 S_028B70_ALPHA_TO_MASK_OFFSET3(2));
440
441 if (state->alpha_to_coverage)
442 blend->need_src_alpha_4bit |= 0xf;
443
444 blend->cb_target_mask = 0;
445 for (int i = 0; i < 8; i++) {
446 /* state->rt entries > 0 only written if independent blending */
447 const int j = state->independent_blend_enable ? i : 0;
448
449 unsigned eqRGB = state->rt[j].rgb_func;
450 unsigned srcRGB = state->rt[j].rgb_src_factor;
451 unsigned dstRGB = state->rt[j].rgb_dst_factor;
452 unsigned eqA = state->rt[j].alpha_func;
453 unsigned srcA = state->rt[j].alpha_src_factor;
454 unsigned dstA = state->rt[j].alpha_dst_factor;
455
456 unsigned srcRGB_opt, dstRGB_opt, srcA_opt, dstA_opt;
457 unsigned blend_cntl = 0;
458
459 sx_mrt_blend_opt[i] =
460 S_028760_COLOR_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED) |
461 S_028760_ALPHA_COMB_FCN(V_028760_OPT_COMB_BLEND_DISABLED);
462
463 /* Only set dual source blending for MRT0 to avoid a hang. */
464 if (i >= 1 && blend->dual_src_blend)
465 continue;
466
467 /* Only addition and subtraction equations are supported with
468 * dual source blending.
469 */
470 if (blend->dual_src_blend &&
471 (eqRGB == PIPE_BLEND_MIN || eqRGB == PIPE_BLEND_MAX ||
472 eqA == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MAX)) {
473 assert(!"Unsupported equation for dual source blending");
474 continue;
475 }
476
477 if (!state->rt[j].colormask)
478 continue;
479
480 /* cb_render_state will disable unused ones */
481 blend->cb_target_mask |= (unsigned)state->rt[j].colormask << (4 * i);
482
483 if (!state->rt[j].blend_enable) {
484 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
485 continue;
486 }
487
488 /* Blending optimizations for Stoney.
489 * These transformations don't change the behavior.
490 *
491 * First, get rid of DST in the blend factors:
492 * func(src * DST, dst * 0) ---> func(src * 0, dst * SRC)
493 */
494 si_blend_remove_dst(&eqRGB, &srcRGB, &dstRGB,
495 PIPE_BLENDFACTOR_DST_COLOR,
496 PIPE_BLENDFACTOR_SRC_COLOR);
497 si_blend_remove_dst(&eqA, &srcA, &dstA,
498 PIPE_BLENDFACTOR_DST_COLOR,
499 PIPE_BLENDFACTOR_SRC_COLOR);
500 si_blend_remove_dst(&eqA, &srcA, &dstA,
501 PIPE_BLENDFACTOR_DST_ALPHA,
502 PIPE_BLENDFACTOR_SRC_ALPHA);
503
504 /* Look up the ideal settings from tables. */
505 srcRGB_opt = si_translate_blend_opt_factor(srcRGB, false);
506 dstRGB_opt = si_translate_blend_opt_factor(dstRGB, false);
507 srcA_opt = si_translate_blend_opt_factor(srcA, true);
508 dstA_opt = si_translate_blend_opt_factor(dstA, true);
509
510 /* Handle interdependencies. */
511 if (si_blend_factor_uses_dst(srcRGB))
512 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
513 if (si_blend_factor_uses_dst(srcA))
514 dstA_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_NONE;
515
516 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE &&
517 (dstRGB == PIPE_BLENDFACTOR_ZERO ||
518 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
519 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE))
520 dstRGB_opt = V_028760_BLEND_OPT_PRESERVE_NONE_IGNORE_A0;
521
522 /* Set the final value. */
523 sx_mrt_blend_opt[i] =
524 S_028760_COLOR_SRC_OPT(srcRGB_opt) |
525 S_028760_COLOR_DST_OPT(dstRGB_opt) |
526 S_028760_COLOR_COMB_FCN(si_translate_blend_opt_function(eqRGB)) |
527 S_028760_ALPHA_SRC_OPT(srcA_opt) |
528 S_028760_ALPHA_DST_OPT(dstA_opt) |
529 S_028760_ALPHA_COMB_FCN(si_translate_blend_opt_function(eqA));
530
531 /* Set blend state. */
532 blend_cntl |= S_028780_ENABLE(1);
533 blend_cntl |= S_028780_COLOR_COMB_FCN(si_translate_blend_function(eqRGB));
534 blend_cntl |= S_028780_COLOR_SRCBLEND(si_translate_blend_factor(srcRGB));
535 blend_cntl |= S_028780_COLOR_DESTBLEND(si_translate_blend_factor(dstRGB));
536
537 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
538 blend_cntl |= S_028780_SEPARATE_ALPHA_BLEND(1);
539 blend_cntl |= S_028780_ALPHA_COMB_FCN(si_translate_blend_function(eqA));
540 blend_cntl |= S_028780_ALPHA_SRCBLEND(si_translate_blend_factor(srcA));
541 blend_cntl |= S_028780_ALPHA_DESTBLEND(si_translate_blend_factor(dstA));
542 }
543 si_pm4_set_reg(pm4, R_028780_CB_BLEND0_CONTROL + i * 4, blend_cntl);
544
545 blend->blend_enable_4bit |= 0xfu << (i * 4);
546
547 /* This is only important for formats without alpha. */
548 if (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
549 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
550 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
551 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
552 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
553 dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA)
554 blend->need_src_alpha_4bit |= 0xfu << (i * 4);
555 }
556
557 if (blend->cb_target_mask) {
558 color_control |= S_028808_MODE(mode);
559 } else {
560 color_control |= S_028808_MODE(V_028808_CB_DISABLE);
561 }
562
563 if (sctx->b.family == CHIP_STONEY) {
564 for (int i = 0; i < 8; i++)
565 si_pm4_set_reg(pm4, R_028760_SX_MRT0_BLEND_OPT + i * 4,
566 sx_mrt_blend_opt[i]);
567
568 /* RB+ doesn't work with dual source blending, logic op, and RESOLVE. */
569 if (blend->dual_src_blend || state->logicop_enable ||
570 mode == V_028808_CB_RESOLVE)
571 color_control |= S_028808_DISABLE_DUAL_QUAD(1);
572 }
573
574 si_pm4_set_reg(pm4, R_028808_CB_COLOR_CONTROL, color_control);
575 return blend;
576 }
577
578 static void *si_create_blend_state(struct pipe_context *ctx,
579 const struct pipe_blend_state *state)
580 {
581 return si_create_blend_state_mode(ctx, state, V_028808_CB_NORMAL);
582 }
583
584 static void si_bind_blend_state(struct pipe_context *ctx, void *state)
585 {
586 struct si_context *sctx = (struct si_context *)ctx;
587 si_pm4_bind_state(sctx, blend, (struct si_state_blend *)state);
588 si_mark_atom_dirty(sctx, &sctx->cb_render_state);
589 sctx->do_update_shaders = true;
590 }
591
592 static void si_delete_blend_state(struct pipe_context *ctx, void *state)
593 {
594 struct si_context *sctx = (struct si_context *)ctx;
595 si_pm4_delete_state(sctx, blend, (struct si_state_blend *)state);
596 }
597
598 static void si_set_blend_color(struct pipe_context *ctx,
599 const struct pipe_blend_color *state)
600 {
601 struct si_context *sctx = (struct si_context *)ctx;
602
603 if (memcmp(&sctx->blend_color.state, state, sizeof(*state)) == 0)
604 return;
605
606 sctx->blend_color.state = *state;
607 si_mark_atom_dirty(sctx, &sctx->blend_color.atom);
608 }
609
610 static void si_emit_blend_color(struct si_context *sctx, struct r600_atom *atom)
611 {
612 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
613
614 radeon_set_context_reg_seq(cs, R_028414_CB_BLEND_RED, 4);
615 radeon_emit_array(cs, (uint32_t*)sctx->blend_color.state.color, 4);
616 }
617
618 /*
619 * Clipping
620 */
621
622 static void si_set_clip_state(struct pipe_context *ctx,
623 const struct pipe_clip_state *state)
624 {
625 struct si_context *sctx = (struct si_context *)ctx;
626 struct pipe_constant_buffer cb;
627
628 if (memcmp(&sctx->clip_state.state, state, sizeof(*state)) == 0)
629 return;
630
631 sctx->clip_state.state = *state;
632 si_mark_atom_dirty(sctx, &sctx->clip_state.atom);
633
634 cb.buffer = NULL;
635 cb.user_buffer = state->ucp;
636 cb.buffer_offset = 0;
637 cb.buffer_size = 4*4*8;
638 si_set_rw_buffer(sctx, SI_VS_CONST_CLIP_PLANES, &cb);
639 pipe_resource_reference(&cb.buffer, NULL);
640 }
641
642 static void si_emit_clip_state(struct si_context *sctx, struct r600_atom *atom)
643 {
644 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
645
646 radeon_set_context_reg_seq(cs, R_0285BC_PA_CL_UCP_0_X, 6*4);
647 radeon_emit_array(cs, (uint32_t*)sctx->clip_state.state.ucp, 6*4);
648 }
649
650 #define SIX_BITS 0x3F
651
652 static void si_emit_clip_regs(struct si_context *sctx, struct r600_atom *atom)
653 {
654 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
655 struct tgsi_shader_info *info = si_get_vs_info(sctx);
656 unsigned window_space =
657 info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION];
658 unsigned clipdist_mask =
659 info->writes_clipvertex ? SIX_BITS : info->clipdist_writemask;
660
661 radeon_set_context_reg(cs, R_02881C_PA_CL_VS_OUT_CNTL,
662 S_02881C_USE_VTX_POINT_SIZE(info->writes_psize) |
663 S_02881C_USE_VTX_EDGE_FLAG(info->writes_edgeflag) |
664 S_02881C_USE_VTX_RENDER_TARGET_INDX(info->writes_layer) |
665 S_02881C_USE_VTX_VIEWPORT_INDX(info->writes_viewport_index) |
666 S_02881C_VS_OUT_CCDIST0_VEC_ENA((clipdist_mask & 0x0F) != 0) |
667 S_02881C_VS_OUT_CCDIST1_VEC_ENA((clipdist_mask & 0xF0) != 0) |
668 S_02881C_VS_OUT_MISC_VEC_ENA(info->writes_psize ||
669 info->writes_edgeflag ||
670 info->writes_layer ||
671 info->writes_viewport_index) |
672 S_02881C_VS_OUT_MISC_SIDE_BUS_ENA(1) |
673 (sctx->queued.named.rasterizer->clip_plane_enable &
674 clipdist_mask));
675 radeon_set_context_reg(cs, R_028810_PA_CL_CLIP_CNTL,
676 sctx->queued.named.rasterizer->pa_cl_clip_cntl |
677 (clipdist_mask ? 0 :
678 sctx->queued.named.rasterizer->clip_plane_enable & SIX_BITS) |
679 S_028810_CLIP_DISABLE(window_space));
680
681 /* reuse needs to be set off if we write oViewport */
682 radeon_set_context_reg(cs, R_028AB4_VGT_REUSE_OFF,
683 S_028AB4_REUSE_OFF(info->writes_viewport_index));
684 }
685
686 /*
687 * inferred state between framebuffer and rasterizer
688 */
689 static void si_update_poly_offset_state(struct si_context *sctx)
690 {
691 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
692
693 if (!rs || !rs->uses_poly_offset || !sctx->framebuffer.state.zsbuf)
694 return;
695
696 switch (sctx->framebuffer.state.zsbuf->texture->format) {
697 case PIPE_FORMAT_Z16_UNORM:
698 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[0]);
699 break;
700 default: /* 24-bit */
701 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[1]);
702 break;
703 case PIPE_FORMAT_Z32_FLOAT:
704 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
705 si_pm4_bind_state(sctx, poly_offset, &rs->pm4_poly_offset[2]);
706 break;
707 }
708 }
709
710 /*
711 * Rasterizer
712 */
713
714 static uint32_t si_translate_fill(uint32_t func)
715 {
716 switch(func) {
717 case PIPE_POLYGON_MODE_FILL:
718 return V_028814_X_DRAW_TRIANGLES;
719 case PIPE_POLYGON_MODE_LINE:
720 return V_028814_X_DRAW_LINES;
721 case PIPE_POLYGON_MODE_POINT:
722 return V_028814_X_DRAW_POINTS;
723 default:
724 assert(0);
725 return V_028814_X_DRAW_POINTS;
726 }
727 }
728
729 static void *si_create_rs_state(struct pipe_context *ctx,
730 const struct pipe_rasterizer_state *state)
731 {
732 struct si_state_rasterizer *rs = CALLOC_STRUCT(si_state_rasterizer);
733 struct si_pm4_state *pm4 = &rs->pm4;
734 unsigned tmp, i;
735 float psize_min, psize_max;
736
737 if (!rs) {
738 return NULL;
739 }
740
741 rs->scissor_enable = state->scissor;
742 rs->two_side = state->light_twoside;
743 rs->multisample_enable = state->multisample;
744 rs->force_persample_interp = state->force_persample_interp;
745 rs->clip_plane_enable = state->clip_plane_enable;
746 rs->line_stipple_enable = state->line_stipple_enable;
747 rs->poly_stipple_enable = state->poly_stipple_enable;
748 rs->line_smooth = state->line_smooth;
749 rs->poly_smooth = state->poly_smooth;
750 rs->uses_poly_offset = state->offset_point || state->offset_line ||
751 state->offset_tri;
752 rs->clamp_fragment_color = state->clamp_fragment_color;
753 rs->flatshade = state->flatshade;
754 rs->sprite_coord_enable = state->sprite_coord_enable;
755 rs->rasterizer_discard = state->rasterizer_discard;
756 rs->pa_sc_line_stipple = state->line_stipple_enable ?
757 S_028A0C_LINE_PATTERN(state->line_stipple_pattern) |
758 S_028A0C_REPEAT_COUNT(state->line_stipple_factor) : 0;
759 rs->pa_cl_clip_cntl =
760 S_028810_DX_CLIP_SPACE_DEF(state->clip_halfz) |
761 S_028810_ZCLIP_NEAR_DISABLE(!state->depth_clip) |
762 S_028810_ZCLIP_FAR_DISABLE(!state->depth_clip) |
763 S_028810_DX_RASTERIZATION_KILL(state->rasterizer_discard) |
764 S_028810_DX_LINEAR_ATTR_CLIP_ENA(1);
765
766 si_pm4_set_reg(pm4, R_0286D4_SPI_INTERP_CONTROL_0,
767 S_0286D4_FLAT_SHADE_ENA(1) |
768 S_0286D4_PNT_SPRITE_ENA(1) |
769 S_0286D4_PNT_SPRITE_OVRD_X(V_0286D4_SPI_PNT_SPRITE_SEL_S) |
770 S_0286D4_PNT_SPRITE_OVRD_Y(V_0286D4_SPI_PNT_SPRITE_SEL_T) |
771 S_0286D4_PNT_SPRITE_OVRD_Z(V_0286D4_SPI_PNT_SPRITE_SEL_0) |
772 S_0286D4_PNT_SPRITE_OVRD_W(V_0286D4_SPI_PNT_SPRITE_SEL_1) |
773 S_0286D4_PNT_SPRITE_TOP_1(state->sprite_coord_mode != PIPE_SPRITE_COORD_UPPER_LEFT));
774
775 /* point size 12.4 fixed point */
776 tmp = (unsigned)(state->point_size * 8.0);
777 si_pm4_set_reg(pm4, R_028A00_PA_SU_POINT_SIZE, S_028A00_HEIGHT(tmp) | S_028A00_WIDTH(tmp));
778
779 if (state->point_size_per_vertex) {
780 psize_min = util_get_min_point_size(state);
781 psize_max = 8192;
782 } else {
783 /* Force the point size to be as if the vertex output was disabled. */
784 psize_min = state->point_size;
785 psize_max = state->point_size;
786 }
787 /* Divide by two, because 0.5 = 1 pixel. */
788 si_pm4_set_reg(pm4, R_028A04_PA_SU_POINT_MINMAX,
789 S_028A04_MIN_SIZE(si_pack_float_12p4(psize_min/2)) |
790 S_028A04_MAX_SIZE(si_pack_float_12p4(psize_max/2)));
791
792 tmp = (unsigned)state->line_width * 8;
793 si_pm4_set_reg(pm4, R_028A08_PA_SU_LINE_CNTL, S_028A08_WIDTH(tmp));
794 si_pm4_set_reg(pm4, R_028A48_PA_SC_MODE_CNTL_0,
795 S_028A48_LINE_STIPPLE_ENABLE(state->line_stipple_enable) |
796 S_028A48_MSAA_ENABLE(state->multisample ||
797 state->poly_smooth ||
798 state->line_smooth) |
799 S_028A48_VPORT_SCISSOR_ENABLE(1));
800
801 si_pm4_set_reg(pm4, R_028BE4_PA_SU_VTX_CNTL,
802 S_028BE4_PIX_CENTER(state->half_pixel_center) |
803 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH));
804
805 si_pm4_set_reg(pm4, R_028B7C_PA_SU_POLY_OFFSET_CLAMP, fui(state->offset_clamp));
806 si_pm4_set_reg(pm4, R_028814_PA_SU_SC_MODE_CNTL,
807 S_028814_PROVOKING_VTX_LAST(!state->flatshade_first) |
808 S_028814_CULL_FRONT((state->cull_face & PIPE_FACE_FRONT) ? 1 : 0) |
809 S_028814_CULL_BACK((state->cull_face & PIPE_FACE_BACK) ? 1 : 0) |
810 S_028814_FACE(!state->front_ccw) |
811 S_028814_POLY_OFFSET_FRONT_ENABLE(util_get_offset(state, state->fill_front)) |
812 S_028814_POLY_OFFSET_BACK_ENABLE(util_get_offset(state, state->fill_back)) |
813 S_028814_POLY_OFFSET_PARA_ENABLE(state->offset_point || state->offset_line) |
814 S_028814_POLY_MODE(state->fill_front != PIPE_POLYGON_MODE_FILL ||
815 state->fill_back != PIPE_POLYGON_MODE_FILL) |
816 S_028814_POLYMODE_FRONT_PTYPE(si_translate_fill(state->fill_front)) |
817 S_028814_POLYMODE_BACK_PTYPE(si_translate_fill(state->fill_back)));
818 si_pm4_set_reg(pm4, R_00B130_SPI_SHADER_USER_DATA_VS_0 +
819 SI_SGPR_VS_STATE_BITS * 4, state->clamp_vertex_color);
820
821 /* Precalculate polygon offset states for 16-bit, 24-bit, and 32-bit zbuffers. */
822 for (i = 0; i < 3; i++) {
823 struct si_pm4_state *pm4 = &rs->pm4_poly_offset[i];
824 float offset_units = state->offset_units;
825 float offset_scale = state->offset_scale * 16.0f;
826 uint32_t pa_su_poly_offset_db_fmt_cntl = 0;
827
828 if (!state->offset_units_unscaled) {
829 switch (i) {
830 case 0: /* 16-bit zbuffer */
831 offset_units *= 4.0f;
832 pa_su_poly_offset_db_fmt_cntl =
833 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-16);
834 break;
835 case 1: /* 24-bit zbuffer */
836 offset_units *= 2.0f;
837 pa_su_poly_offset_db_fmt_cntl =
838 S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-24);
839 break;
840 case 2: /* 32-bit zbuffer */
841 offset_units *= 1.0f;
842 pa_su_poly_offset_db_fmt_cntl = S_028B78_POLY_OFFSET_NEG_NUM_DB_BITS(-23) |
843 S_028B78_POLY_OFFSET_DB_IS_FLOAT_FMT(1);
844 break;
845 }
846 }
847
848 si_pm4_set_reg(pm4, R_028B80_PA_SU_POLY_OFFSET_FRONT_SCALE,
849 fui(offset_scale));
850 si_pm4_set_reg(pm4, R_028B84_PA_SU_POLY_OFFSET_FRONT_OFFSET,
851 fui(offset_units));
852 si_pm4_set_reg(pm4, R_028B88_PA_SU_POLY_OFFSET_BACK_SCALE,
853 fui(offset_scale));
854 si_pm4_set_reg(pm4, R_028B8C_PA_SU_POLY_OFFSET_BACK_OFFSET,
855 fui(offset_units));
856 si_pm4_set_reg(pm4, R_028B78_PA_SU_POLY_OFFSET_DB_FMT_CNTL,
857 pa_su_poly_offset_db_fmt_cntl);
858 }
859
860 return rs;
861 }
862
863 static void si_bind_rs_state(struct pipe_context *ctx, void *state)
864 {
865 struct si_context *sctx = (struct si_context *)ctx;
866 struct si_state_rasterizer *old_rs =
867 (struct si_state_rasterizer*)sctx->queued.named.rasterizer;
868 struct si_state_rasterizer *rs = (struct si_state_rasterizer *)state;
869
870 if (!state)
871 return;
872
873 if (sctx->framebuffer.nr_samples > 1 &&
874 (!old_rs || old_rs->multisample_enable != rs->multisample_enable)) {
875 si_mark_atom_dirty(sctx, &sctx->db_render_state);
876
877 if (sctx->b.family >= CHIP_POLARIS10)
878 si_mark_atom_dirty(sctx, &sctx->msaa_sample_locs.atom);
879 }
880
881 r600_set_scissor_enable(&sctx->b, rs->scissor_enable);
882
883 si_pm4_bind_state(sctx, rasterizer, rs);
884 si_update_poly_offset_state(sctx);
885
886 si_mark_atom_dirty(sctx, &sctx->clip_regs);
887 sctx->do_update_shaders = true;
888 }
889
890 static void si_delete_rs_state(struct pipe_context *ctx, void *state)
891 {
892 struct si_context *sctx = (struct si_context *)ctx;
893
894 if (sctx->queued.named.rasterizer == state)
895 si_pm4_bind_state(sctx, poly_offset, NULL);
896 si_pm4_delete_state(sctx, rasterizer, (struct si_state_rasterizer *)state);
897 }
898
899 /*
900 * infeered state between dsa and stencil ref
901 */
902 static void si_emit_stencil_ref(struct si_context *sctx, struct r600_atom *atom)
903 {
904 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
905 struct pipe_stencil_ref *ref = &sctx->stencil_ref.state;
906 struct si_dsa_stencil_ref_part *dsa = &sctx->stencil_ref.dsa_part;
907
908 radeon_set_context_reg_seq(cs, R_028430_DB_STENCILREFMASK, 2);
909 radeon_emit(cs, S_028430_STENCILTESTVAL(ref->ref_value[0]) |
910 S_028430_STENCILMASK(dsa->valuemask[0]) |
911 S_028430_STENCILWRITEMASK(dsa->writemask[0]) |
912 S_028430_STENCILOPVAL(1));
913 radeon_emit(cs, S_028434_STENCILTESTVAL_BF(ref->ref_value[1]) |
914 S_028434_STENCILMASK_BF(dsa->valuemask[1]) |
915 S_028434_STENCILWRITEMASK_BF(dsa->writemask[1]) |
916 S_028434_STENCILOPVAL_BF(1));
917 }
918
919 static void si_set_stencil_ref(struct pipe_context *ctx,
920 const struct pipe_stencil_ref *state)
921 {
922 struct si_context *sctx = (struct si_context *)ctx;
923
924 if (memcmp(&sctx->stencil_ref.state, state, sizeof(*state)) == 0)
925 return;
926
927 sctx->stencil_ref.state = *state;
928 si_mark_atom_dirty(sctx, &sctx->stencil_ref.atom);
929 }
930
931
932 /*
933 * DSA
934 */
935
936 static uint32_t si_translate_stencil_op(int s_op)
937 {
938 switch (s_op) {
939 case PIPE_STENCIL_OP_KEEP:
940 return V_02842C_STENCIL_KEEP;
941 case PIPE_STENCIL_OP_ZERO:
942 return V_02842C_STENCIL_ZERO;
943 case PIPE_STENCIL_OP_REPLACE:
944 return V_02842C_STENCIL_REPLACE_TEST;
945 case PIPE_STENCIL_OP_INCR:
946 return V_02842C_STENCIL_ADD_CLAMP;
947 case PIPE_STENCIL_OP_DECR:
948 return V_02842C_STENCIL_SUB_CLAMP;
949 case PIPE_STENCIL_OP_INCR_WRAP:
950 return V_02842C_STENCIL_ADD_WRAP;
951 case PIPE_STENCIL_OP_DECR_WRAP:
952 return V_02842C_STENCIL_SUB_WRAP;
953 case PIPE_STENCIL_OP_INVERT:
954 return V_02842C_STENCIL_INVERT;
955 default:
956 R600_ERR("Unknown stencil op %d", s_op);
957 assert(0);
958 break;
959 }
960 return 0;
961 }
962
963 static void *si_create_dsa_state(struct pipe_context *ctx,
964 const struct pipe_depth_stencil_alpha_state *state)
965 {
966 struct si_state_dsa *dsa = CALLOC_STRUCT(si_state_dsa);
967 struct si_pm4_state *pm4 = &dsa->pm4;
968 unsigned db_depth_control;
969 uint32_t db_stencil_control = 0;
970
971 if (!dsa) {
972 return NULL;
973 }
974
975 dsa->stencil_ref.valuemask[0] = state->stencil[0].valuemask;
976 dsa->stencil_ref.valuemask[1] = state->stencil[1].valuemask;
977 dsa->stencil_ref.writemask[0] = state->stencil[0].writemask;
978 dsa->stencil_ref.writemask[1] = state->stencil[1].writemask;
979
980 db_depth_control = S_028800_Z_ENABLE(state->depth.enabled) |
981 S_028800_Z_WRITE_ENABLE(state->depth.writemask) |
982 S_028800_ZFUNC(state->depth.func) |
983 S_028800_DEPTH_BOUNDS_ENABLE(state->depth.bounds_test);
984
985 /* stencil */
986 if (state->stencil[0].enabled) {
987 db_depth_control |= S_028800_STENCIL_ENABLE(1);
988 db_depth_control |= S_028800_STENCILFUNC(state->stencil[0].func);
989 db_stencil_control |= S_02842C_STENCILFAIL(si_translate_stencil_op(state->stencil[0].fail_op));
990 db_stencil_control |= S_02842C_STENCILZPASS(si_translate_stencil_op(state->stencil[0].zpass_op));
991 db_stencil_control |= S_02842C_STENCILZFAIL(si_translate_stencil_op(state->stencil[0].zfail_op));
992
993 if (state->stencil[1].enabled) {
994 db_depth_control |= S_028800_BACKFACE_ENABLE(1);
995 db_depth_control |= S_028800_STENCILFUNC_BF(state->stencil[1].func);
996 db_stencil_control |= S_02842C_STENCILFAIL_BF(si_translate_stencil_op(state->stencil[1].fail_op));
997 db_stencil_control |= S_02842C_STENCILZPASS_BF(si_translate_stencil_op(state->stencil[1].zpass_op));
998 db_stencil_control |= S_02842C_STENCILZFAIL_BF(si_translate_stencil_op(state->stencil[1].zfail_op));
999 }
1000 }
1001
1002 /* alpha */
1003 if (state->alpha.enabled) {
1004 dsa->alpha_func = state->alpha.func;
1005
1006 si_pm4_set_reg(pm4, R_00B030_SPI_SHADER_USER_DATA_PS_0 +
1007 SI_SGPR_ALPHA_REF * 4, fui(state->alpha.ref_value));
1008 } else {
1009 dsa->alpha_func = PIPE_FUNC_ALWAYS;
1010 }
1011
1012 si_pm4_set_reg(pm4, R_028800_DB_DEPTH_CONTROL, db_depth_control);
1013 si_pm4_set_reg(pm4, R_02842C_DB_STENCIL_CONTROL, db_stencil_control);
1014 if (state->depth.bounds_test) {
1015 si_pm4_set_reg(pm4, R_028020_DB_DEPTH_BOUNDS_MIN, fui(state->depth.bounds_min));
1016 si_pm4_set_reg(pm4, R_028024_DB_DEPTH_BOUNDS_MAX, fui(state->depth.bounds_max));
1017 }
1018
1019 return dsa;
1020 }
1021
1022 static void si_bind_dsa_state(struct pipe_context *ctx, void *state)
1023 {
1024 struct si_context *sctx = (struct si_context *)ctx;
1025 struct si_state_dsa *dsa = state;
1026
1027 if (!state)
1028 return;
1029
1030 si_pm4_bind_state(sctx, dsa, dsa);
1031
1032 if (memcmp(&dsa->stencil_ref, &sctx->stencil_ref.dsa_part,
1033 sizeof(struct si_dsa_stencil_ref_part)) != 0) {
1034 sctx->stencil_ref.dsa_part = dsa->stencil_ref;
1035 si_mark_atom_dirty(sctx, &sctx->stencil_ref.atom);
1036 }
1037 sctx->do_update_shaders = true;
1038 }
1039
1040 static void si_delete_dsa_state(struct pipe_context *ctx, void *state)
1041 {
1042 struct si_context *sctx = (struct si_context *)ctx;
1043 si_pm4_delete_state(sctx, dsa, (struct si_state_dsa *)state);
1044 }
1045
1046 static void *si_create_db_flush_dsa(struct si_context *sctx)
1047 {
1048 struct pipe_depth_stencil_alpha_state dsa = {};
1049
1050 return sctx->b.b.create_depth_stencil_alpha_state(&sctx->b.b, &dsa);
1051 }
1052
1053 /* DB RENDER STATE */
1054
1055 static void si_set_active_query_state(struct pipe_context *ctx, boolean enable)
1056 {
1057 struct si_context *sctx = (struct si_context*)ctx;
1058
1059 /* Pipeline stat & streamout queries. */
1060 if (enable) {
1061 sctx->b.flags &= ~R600_CONTEXT_STOP_PIPELINE_STATS;
1062 sctx->b.flags |= R600_CONTEXT_START_PIPELINE_STATS;
1063 } else {
1064 sctx->b.flags &= ~R600_CONTEXT_START_PIPELINE_STATS;
1065 sctx->b.flags |= R600_CONTEXT_STOP_PIPELINE_STATS;
1066 }
1067
1068 /* Occlusion queries. */
1069 if (sctx->occlusion_queries_disabled != !enable) {
1070 sctx->occlusion_queries_disabled = !enable;
1071 si_mark_atom_dirty(sctx, &sctx->db_render_state);
1072 }
1073 }
1074
1075 static void si_set_occlusion_query_state(struct pipe_context *ctx, bool enable)
1076 {
1077 struct si_context *sctx = (struct si_context*)ctx;
1078
1079 si_mark_atom_dirty(sctx, &sctx->db_render_state);
1080 }
1081
1082 static void si_emit_db_render_state(struct si_context *sctx, struct r600_atom *state)
1083 {
1084 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
1085 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
1086 unsigned db_shader_control;
1087
1088 radeon_set_context_reg_seq(cs, R_028000_DB_RENDER_CONTROL, 2);
1089
1090 /* DB_RENDER_CONTROL */
1091 if (sctx->dbcb_depth_copy_enabled ||
1092 sctx->dbcb_stencil_copy_enabled) {
1093 radeon_emit(cs,
1094 S_028000_DEPTH_COPY(sctx->dbcb_depth_copy_enabled) |
1095 S_028000_STENCIL_COPY(sctx->dbcb_stencil_copy_enabled) |
1096 S_028000_COPY_CENTROID(1) |
1097 S_028000_COPY_SAMPLE(sctx->dbcb_copy_sample));
1098 } else if (sctx->db_flush_depth_inplace || sctx->db_flush_stencil_inplace) {
1099 radeon_emit(cs,
1100 S_028000_DEPTH_COMPRESS_DISABLE(sctx->db_flush_depth_inplace) |
1101 S_028000_STENCIL_COMPRESS_DISABLE(sctx->db_flush_stencil_inplace));
1102 } else {
1103 radeon_emit(cs,
1104 S_028000_DEPTH_CLEAR_ENABLE(sctx->db_depth_clear) |
1105 S_028000_STENCIL_CLEAR_ENABLE(sctx->db_stencil_clear));
1106 }
1107
1108 /* DB_COUNT_CONTROL (occlusion queries) */
1109 if (sctx->b.num_occlusion_queries > 0 &&
1110 !sctx->occlusion_queries_disabled) {
1111 bool perfect = sctx->b.num_perfect_occlusion_queries > 0;
1112
1113 if (sctx->b.chip_class >= CIK) {
1114 radeon_emit(cs,
1115 S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1116 S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples) |
1117 S_028004_ZPASS_ENABLE(1) |
1118 S_028004_SLICE_EVEN_ENABLE(1) |
1119 S_028004_SLICE_ODD_ENABLE(1));
1120 } else {
1121 radeon_emit(cs,
1122 S_028004_PERFECT_ZPASS_COUNTS(perfect) |
1123 S_028004_SAMPLE_RATE(sctx->framebuffer.log_samples));
1124 }
1125 } else {
1126 /* Disable occlusion queries. */
1127 if (sctx->b.chip_class >= CIK) {
1128 radeon_emit(cs, 0);
1129 } else {
1130 radeon_emit(cs, S_028004_ZPASS_INCREMENT_DISABLE(1));
1131 }
1132 }
1133
1134 /* DB_RENDER_OVERRIDE2 */
1135 radeon_set_context_reg(cs, R_028010_DB_RENDER_OVERRIDE2,
1136 S_028010_DISABLE_ZMASK_EXPCLEAR_OPTIMIZATION(sctx->db_depth_disable_expclear) |
1137 S_028010_DISABLE_SMEM_EXPCLEAR_OPTIMIZATION(sctx->db_stencil_disable_expclear) |
1138 S_028010_DECOMPRESS_Z_ON_FLUSH(sctx->framebuffer.nr_samples >= 4));
1139
1140 db_shader_control = S_02880C_ALPHA_TO_MASK_DISABLE(sctx->framebuffer.cb0_is_integer) |
1141 sctx->ps_db_shader_control;
1142
1143 /* Bug workaround for smoothing (overrasterization) on SI. */
1144 if (sctx->b.chip_class == SI && sctx->smoothing_enabled) {
1145 db_shader_control &= C_02880C_Z_ORDER;
1146 db_shader_control |= S_02880C_Z_ORDER(V_02880C_LATE_Z);
1147 }
1148
1149 /* Disable the gl_SampleMask fragment shader output if MSAA is disabled. */
1150 if (sctx->framebuffer.nr_samples <= 1 || (rs && !rs->multisample_enable))
1151 db_shader_control &= C_02880C_MASK_EXPORT_ENABLE;
1152
1153 if (sctx->b.family == CHIP_STONEY &&
1154 sctx->screen->b.debug_flags & DBG_NO_RB_PLUS)
1155 db_shader_control |= S_02880C_DUAL_QUAD_DISABLE(1);
1156
1157 radeon_set_context_reg(cs, R_02880C_DB_SHADER_CONTROL,
1158 db_shader_control);
1159 }
1160
1161 /*
1162 * format translation
1163 */
1164 static uint32_t si_translate_colorformat(enum pipe_format format)
1165 {
1166 const struct util_format_description *desc = util_format_description(format);
1167
1168 #define HAS_SIZE(x,y,z,w) \
1169 (desc->channel[0].size == (x) && desc->channel[1].size == (y) && \
1170 desc->channel[2].size == (z) && desc->channel[3].size == (w))
1171
1172 if (format == PIPE_FORMAT_R11G11B10_FLOAT) /* isn't plain */
1173 return V_028C70_COLOR_10_11_11;
1174
1175 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
1176 return V_028C70_COLOR_INVALID;
1177
1178 /* hw cannot support mixed formats (except depth/stencil, since
1179 * stencil is not written to). */
1180 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1181 return V_028C70_COLOR_INVALID;
1182
1183 switch (desc->nr_channels) {
1184 case 1:
1185 switch (desc->channel[0].size) {
1186 case 8:
1187 return V_028C70_COLOR_8;
1188 case 16:
1189 return V_028C70_COLOR_16;
1190 case 32:
1191 return V_028C70_COLOR_32;
1192 }
1193 break;
1194 case 2:
1195 if (desc->channel[0].size == desc->channel[1].size) {
1196 switch (desc->channel[0].size) {
1197 case 8:
1198 return V_028C70_COLOR_8_8;
1199 case 16:
1200 return V_028C70_COLOR_16_16;
1201 case 32:
1202 return V_028C70_COLOR_32_32;
1203 }
1204 } else if (HAS_SIZE(8,24,0,0)) {
1205 return V_028C70_COLOR_24_8;
1206 } else if (HAS_SIZE(24,8,0,0)) {
1207 return V_028C70_COLOR_8_24;
1208 }
1209 break;
1210 case 3:
1211 if (HAS_SIZE(5,6,5,0)) {
1212 return V_028C70_COLOR_5_6_5;
1213 } else if (HAS_SIZE(32,8,24,0)) {
1214 return V_028C70_COLOR_X24_8_32_FLOAT;
1215 }
1216 break;
1217 case 4:
1218 if (desc->channel[0].size == desc->channel[1].size &&
1219 desc->channel[0].size == desc->channel[2].size &&
1220 desc->channel[0].size == desc->channel[3].size) {
1221 switch (desc->channel[0].size) {
1222 case 4:
1223 return V_028C70_COLOR_4_4_4_4;
1224 case 8:
1225 return V_028C70_COLOR_8_8_8_8;
1226 case 16:
1227 return V_028C70_COLOR_16_16_16_16;
1228 case 32:
1229 return V_028C70_COLOR_32_32_32_32;
1230 }
1231 } else if (HAS_SIZE(5,5,5,1)) {
1232 return V_028C70_COLOR_1_5_5_5;
1233 } else if (HAS_SIZE(10,10,10,2)) {
1234 return V_028C70_COLOR_2_10_10_10;
1235 }
1236 break;
1237 }
1238 return V_028C70_COLOR_INVALID;
1239 }
1240
1241 static uint32_t si_colorformat_endian_swap(uint32_t colorformat)
1242 {
1243 if (SI_BIG_ENDIAN) {
1244 switch(colorformat) {
1245 /* 8-bit buffers. */
1246 case V_028C70_COLOR_8:
1247 return V_028C70_ENDIAN_NONE;
1248
1249 /* 16-bit buffers. */
1250 case V_028C70_COLOR_5_6_5:
1251 case V_028C70_COLOR_1_5_5_5:
1252 case V_028C70_COLOR_4_4_4_4:
1253 case V_028C70_COLOR_16:
1254 case V_028C70_COLOR_8_8:
1255 return V_028C70_ENDIAN_8IN16;
1256
1257 /* 32-bit buffers. */
1258 case V_028C70_COLOR_8_8_8_8:
1259 case V_028C70_COLOR_2_10_10_10:
1260 case V_028C70_COLOR_8_24:
1261 case V_028C70_COLOR_24_8:
1262 case V_028C70_COLOR_16_16:
1263 return V_028C70_ENDIAN_8IN32;
1264
1265 /* 64-bit buffers. */
1266 case V_028C70_COLOR_16_16_16_16:
1267 return V_028C70_ENDIAN_8IN16;
1268
1269 case V_028C70_COLOR_32_32:
1270 return V_028C70_ENDIAN_8IN32;
1271
1272 /* 128-bit buffers. */
1273 case V_028C70_COLOR_32_32_32_32:
1274 return V_028C70_ENDIAN_8IN32;
1275 default:
1276 return V_028C70_ENDIAN_NONE; /* Unsupported. */
1277 }
1278 } else {
1279 return V_028C70_ENDIAN_NONE;
1280 }
1281 }
1282
1283 static uint32_t si_translate_dbformat(enum pipe_format format)
1284 {
1285 switch (format) {
1286 case PIPE_FORMAT_Z16_UNORM:
1287 return V_028040_Z_16;
1288 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1289 case PIPE_FORMAT_X8Z24_UNORM:
1290 case PIPE_FORMAT_Z24X8_UNORM:
1291 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1292 return V_028040_Z_24; /* deprecated on SI */
1293 case PIPE_FORMAT_Z32_FLOAT:
1294 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1295 return V_028040_Z_32_FLOAT;
1296 default:
1297 return V_028040_Z_INVALID;
1298 }
1299 }
1300
1301 /*
1302 * Texture translation
1303 */
1304
1305 static uint32_t si_translate_texformat(struct pipe_screen *screen,
1306 enum pipe_format format,
1307 const struct util_format_description *desc,
1308 int first_non_void)
1309 {
1310 struct si_screen *sscreen = (struct si_screen*)screen;
1311 bool enable_compressed_formats = (sscreen->b.info.drm_major == 2 &&
1312 sscreen->b.info.drm_minor >= 31) ||
1313 sscreen->b.info.drm_major == 3;
1314 bool uniform = true;
1315 int i;
1316
1317 /* Colorspace (return non-RGB formats directly). */
1318 switch (desc->colorspace) {
1319 /* Depth stencil formats */
1320 case UTIL_FORMAT_COLORSPACE_ZS:
1321 switch (format) {
1322 case PIPE_FORMAT_Z16_UNORM:
1323 return V_008F14_IMG_DATA_FORMAT_16;
1324 case PIPE_FORMAT_X24S8_UINT:
1325 case PIPE_FORMAT_Z24X8_UNORM:
1326 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1327 return V_008F14_IMG_DATA_FORMAT_8_24;
1328 case PIPE_FORMAT_X8Z24_UNORM:
1329 case PIPE_FORMAT_S8X24_UINT:
1330 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1331 return V_008F14_IMG_DATA_FORMAT_24_8;
1332 case PIPE_FORMAT_S8_UINT:
1333 return V_008F14_IMG_DATA_FORMAT_8;
1334 case PIPE_FORMAT_Z32_FLOAT:
1335 return V_008F14_IMG_DATA_FORMAT_32;
1336 case PIPE_FORMAT_X32_S8X24_UINT:
1337 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1338 return V_008F14_IMG_DATA_FORMAT_X24_8_32;
1339 default:
1340 goto out_unknown;
1341 }
1342
1343 case UTIL_FORMAT_COLORSPACE_YUV:
1344 goto out_unknown; /* TODO */
1345
1346 case UTIL_FORMAT_COLORSPACE_SRGB:
1347 if (desc->nr_channels != 4 && desc->nr_channels != 1)
1348 goto out_unknown;
1349 break;
1350
1351 default:
1352 break;
1353 }
1354
1355 if (desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
1356 if (!enable_compressed_formats)
1357 goto out_unknown;
1358
1359 switch (format) {
1360 case PIPE_FORMAT_RGTC1_SNORM:
1361 case PIPE_FORMAT_LATC1_SNORM:
1362 case PIPE_FORMAT_RGTC1_UNORM:
1363 case PIPE_FORMAT_LATC1_UNORM:
1364 return V_008F14_IMG_DATA_FORMAT_BC4;
1365 case PIPE_FORMAT_RGTC2_SNORM:
1366 case PIPE_FORMAT_LATC2_SNORM:
1367 case PIPE_FORMAT_RGTC2_UNORM:
1368 case PIPE_FORMAT_LATC2_UNORM:
1369 return V_008F14_IMG_DATA_FORMAT_BC5;
1370 default:
1371 goto out_unknown;
1372 }
1373 }
1374
1375 if (desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
1376 sscreen->b.family == CHIP_STONEY) {
1377 switch (format) {
1378 case PIPE_FORMAT_ETC1_RGB8:
1379 case PIPE_FORMAT_ETC2_RGB8:
1380 case PIPE_FORMAT_ETC2_SRGB8:
1381 return V_008F14_IMG_DATA_FORMAT_ETC2_RGB;
1382 case PIPE_FORMAT_ETC2_RGB8A1:
1383 case PIPE_FORMAT_ETC2_SRGB8A1:
1384 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA1;
1385 case PIPE_FORMAT_ETC2_RGBA8:
1386 case PIPE_FORMAT_ETC2_SRGBA8:
1387 return V_008F14_IMG_DATA_FORMAT_ETC2_RGBA;
1388 case PIPE_FORMAT_ETC2_R11_UNORM:
1389 case PIPE_FORMAT_ETC2_R11_SNORM:
1390 return V_008F14_IMG_DATA_FORMAT_ETC2_R;
1391 case PIPE_FORMAT_ETC2_RG11_UNORM:
1392 case PIPE_FORMAT_ETC2_RG11_SNORM:
1393 return V_008F14_IMG_DATA_FORMAT_ETC2_RG;
1394 default:
1395 goto out_unknown;
1396 }
1397 }
1398
1399 if (desc->layout == UTIL_FORMAT_LAYOUT_BPTC) {
1400 if (!enable_compressed_formats)
1401 goto out_unknown;
1402
1403 switch (format) {
1404 case PIPE_FORMAT_BPTC_RGBA_UNORM:
1405 case PIPE_FORMAT_BPTC_SRGBA:
1406 return V_008F14_IMG_DATA_FORMAT_BC7;
1407 case PIPE_FORMAT_BPTC_RGB_FLOAT:
1408 case PIPE_FORMAT_BPTC_RGB_UFLOAT:
1409 return V_008F14_IMG_DATA_FORMAT_BC6;
1410 default:
1411 goto out_unknown;
1412 }
1413 }
1414
1415 if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
1416 switch (format) {
1417 case PIPE_FORMAT_R8G8_B8G8_UNORM:
1418 case PIPE_FORMAT_G8R8_B8R8_UNORM:
1419 return V_008F14_IMG_DATA_FORMAT_GB_GR;
1420 case PIPE_FORMAT_G8R8_G8B8_UNORM:
1421 case PIPE_FORMAT_R8G8_R8B8_UNORM:
1422 return V_008F14_IMG_DATA_FORMAT_BG_RG;
1423 default:
1424 goto out_unknown;
1425 }
1426 }
1427
1428 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
1429 if (!enable_compressed_formats)
1430 goto out_unknown;
1431
1432 if (!util_format_s3tc_enabled) {
1433 goto out_unknown;
1434 }
1435
1436 switch (format) {
1437 case PIPE_FORMAT_DXT1_RGB:
1438 case PIPE_FORMAT_DXT1_RGBA:
1439 case PIPE_FORMAT_DXT1_SRGB:
1440 case PIPE_FORMAT_DXT1_SRGBA:
1441 return V_008F14_IMG_DATA_FORMAT_BC1;
1442 case PIPE_FORMAT_DXT3_RGBA:
1443 case PIPE_FORMAT_DXT3_SRGBA:
1444 return V_008F14_IMG_DATA_FORMAT_BC2;
1445 case PIPE_FORMAT_DXT5_RGBA:
1446 case PIPE_FORMAT_DXT5_SRGBA:
1447 return V_008F14_IMG_DATA_FORMAT_BC3;
1448 default:
1449 goto out_unknown;
1450 }
1451 }
1452
1453 if (format == PIPE_FORMAT_R9G9B9E5_FLOAT) {
1454 return V_008F14_IMG_DATA_FORMAT_5_9_9_9;
1455 } else if (format == PIPE_FORMAT_R11G11B10_FLOAT) {
1456 return V_008F14_IMG_DATA_FORMAT_10_11_11;
1457 }
1458
1459 /* R8G8Bx_SNORM - TODO CxV8U8 */
1460
1461 /* hw cannot support mixed formats (except depth/stencil, since only
1462 * depth is read).*/
1463 if (desc->is_mixed && desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
1464 goto out_unknown;
1465
1466 /* See whether the components are of the same size. */
1467 for (i = 1; i < desc->nr_channels; i++) {
1468 uniform = uniform && desc->channel[0].size == desc->channel[i].size;
1469 }
1470
1471 /* Non-uniform formats. */
1472 if (!uniform) {
1473 switch(desc->nr_channels) {
1474 case 3:
1475 if (desc->channel[0].size == 5 &&
1476 desc->channel[1].size == 6 &&
1477 desc->channel[2].size == 5) {
1478 return V_008F14_IMG_DATA_FORMAT_5_6_5;
1479 }
1480 goto out_unknown;
1481 case 4:
1482 if (desc->channel[0].size == 5 &&
1483 desc->channel[1].size == 5 &&
1484 desc->channel[2].size == 5 &&
1485 desc->channel[3].size == 1) {
1486 return V_008F14_IMG_DATA_FORMAT_1_5_5_5;
1487 }
1488 if (desc->channel[0].size == 10 &&
1489 desc->channel[1].size == 10 &&
1490 desc->channel[2].size == 10 &&
1491 desc->channel[3].size == 2) {
1492 return V_008F14_IMG_DATA_FORMAT_2_10_10_10;
1493 }
1494 goto out_unknown;
1495 }
1496 goto out_unknown;
1497 }
1498
1499 if (first_non_void < 0 || first_non_void > 3)
1500 goto out_unknown;
1501
1502 /* uniform formats */
1503 switch (desc->channel[first_non_void].size) {
1504 case 4:
1505 switch (desc->nr_channels) {
1506 #if 0 /* Not supported for render targets */
1507 case 2:
1508 return V_008F14_IMG_DATA_FORMAT_4_4;
1509 #endif
1510 case 4:
1511 return V_008F14_IMG_DATA_FORMAT_4_4_4_4;
1512 }
1513 break;
1514 case 8:
1515 switch (desc->nr_channels) {
1516 case 1:
1517 return V_008F14_IMG_DATA_FORMAT_8;
1518 case 2:
1519 return V_008F14_IMG_DATA_FORMAT_8_8;
1520 case 4:
1521 return V_008F14_IMG_DATA_FORMAT_8_8_8_8;
1522 }
1523 break;
1524 case 16:
1525 switch (desc->nr_channels) {
1526 case 1:
1527 return V_008F14_IMG_DATA_FORMAT_16;
1528 case 2:
1529 return V_008F14_IMG_DATA_FORMAT_16_16;
1530 case 4:
1531 return V_008F14_IMG_DATA_FORMAT_16_16_16_16;
1532 }
1533 break;
1534 case 32:
1535 switch (desc->nr_channels) {
1536 case 1:
1537 return V_008F14_IMG_DATA_FORMAT_32;
1538 case 2:
1539 return V_008F14_IMG_DATA_FORMAT_32_32;
1540 #if 0 /* Not supported for render targets */
1541 case 3:
1542 return V_008F14_IMG_DATA_FORMAT_32_32_32;
1543 #endif
1544 case 4:
1545 return V_008F14_IMG_DATA_FORMAT_32_32_32_32;
1546 }
1547 }
1548
1549 out_unknown:
1550 /* R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format)); */
1551 return ~0;
1552 }
1553
1554 static unsigned si_tex_wrap(unsigned wrap)
1555 {
1556 switch (wrap) {
1557 default:
1558 case PIPE_TEX_WRAP_REPEAT:
1559 return V_008F30_SQ_TEX_WRAP;
1560 case PIPE_TEX_WRAP_CLAMP:
1561 return V_008F30_SQ_TEX_CLAMP_HALF_BORDER;
1562 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
1563 return V_008F30_SQ_TEX_CLAMP_LAST_TEXEL;
1564 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
1565 return V_008F30_SQ_TEX_CLAMP_BORDER;
1566 case PIPE_TEX_WRAP_MIRROR_REPEAT:
1567 return V_008F30_SQ_TEX_MIRROR;
1568 case PIPE_TEX_WRAP_MIRROR_CLAMP:
1569 return V_008F30_SQ_TEX_MIRROR_ONCE_HALF_BORDER;
1570 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
1571 return V_008F30_SQ_TEX_MIRROR_ONCE_LAST_TEXEL;
1572 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
1573 return V_008F30_SQ_TEX_MIRROR_ONCE_BORDER;
1574 }
1575 }
1576
1577 static unsigned si_tex_mipfilter(unsigned filter)
1578 {
1579 switch (filter) {
1580 case PIPE_TEX_MIPFILTER_NEAREST:
1581 return V_008F38_SQ_TEX_Z_FILTER_POINT;
1582 case PIPE_TEX_MIPFILTER_LINEAR:
1583 return V_008F38_SQ_TEX_Z_FILTER_LINEAR;
1584 default:
1585 case PIPE_TEX_MIPFILTER_NONE:
1586 return V_008F38_SQ_TEX_Z_FILTER_NONE;
1587 }
1588 }
1589
1590 static unsigned si_tex_compare(unsigned compare)
1591 {
1592 switch (compare) {
1593 default:
1594 case PIPE_FUNC_NEVER:
1595 return V_008F30_SQ_TEX_DEPTH_COMPARE_NEVER;
1596 case PIPE_FUNC_LESS:
1597 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESS;
1598 case PIPE_FUNC_EQUAL:
1599 return V_008F30_SQ_TEX_DEPTH_COMPARE_EQUAL;
1600 case PIPE_FUNC_LEQUAL:
1601 return V_008F30_SQ_TEX_DEPTH_COMPARE_LESSEQUAL;
1602 case PIPE_FUNC_GREATER:
1603 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATER;
1604 case PIPE_FUNC_NOTEQUAL:
1605 return V_008F30_SQ_TEX_DEPTH_COMPARE_NOTEQUAL;
1606 case PIPE_FUNC_GEQUAL:
1607 return V_008F30_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL;
1608 case PIPE_FUNC_ALWAYS:
1609 return V_008F30_SQ_TEX_DEPTH_COMPARE_ALWAYS;
1610 }
1611 }
1612
1613 static unsigned si_tex_dim(unsigned res_target, unsigned view_target,
1614 unsigned nr_samples)
1615 {
1616 if (view_target == PIPE_TEXTURE_CUBE ||
1617 view_target == PIPE_TEXTURE_CUBE_ARRAY)
1618 res_target = view_target;
1619
1620 switch (res_target) {
1621 default:
1622 case PIPE_TEXTURE_1D:
1623 return V_008F1C_SQ_RSRC_IMG_1D;
1624 case PIPE_TEXTURE_1D_ARRAY:
1625 return V_008F1C_SQ_RSRC_IMG_1D_ARRAY;
1626 case PIPE_TEXTURE_2D:
1627 case PIPE_TEXTURE_RECT:
1628 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA :
1629 V_008F1C_SQ_RSRC_IMG_2D;
1630 case PIPE_TEXTURE_2D_ARRAY:
1631 return nr_samples > 1 ? V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY :
1632 V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
1633 case PIPE_TEXTURE_3D:
1634 return V_008F1C_SQ_RSRC_IMG_3D;
1635 case PIPE_TEXTURE_CUBE:
1636 case PIPE_TEXTURE_CUBE_ARRAY:
1637 return V_008F1C_SQ_RSRC_IMG_CUBE;
1638 }
1639 }
1640
1641 /*
1642 * Format support testing
1643 */
1644
1645 static bool si_is_sampler_format_supported(struct pipe_screen *screen, enum pipe_format format)
1646 {
1647 return si_translate_texformat(screen, format, util_format_description(format),
1648 util_format_get_first_non_void_channel(format)) != ~0U;
1649 }
1650
1651 static uint32_t si_translate_buffer_dataformat(struct pipe_screen *screen,
1652 const struct util_format_description *desc,
1653 int first_non_void)
1654 {
1655 unsigned type;
1656 int i;
1657
1658 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
1659 return V_008F0C_BUF_DATA_FORMAT_10_11_11;
1660
1661 assert(first_non_void >= 0);
1662 type = desc->channel[first_non_void].type;
1663
1664 if (type == UTIL_FORMAT_TYPE_FIXED)
1665 return V_008F0C_BUF_DATA_FORMAT_INVALID;
1666
1667 if (desc->nr_channels == 4 &&
1668 desc->channel[0].size == 10 &&
1669 desc->channel[1].size == 10 &&
1670 desc->channel[2].size == 10 &&
1671 desc->channel[3].size == 2)
1672 return V_008F0C_BUF_DATA_FORMAT_2_10_10_10;
1673
1674 /* See whether the components are of the same size. */
1675 for (i = 0; i < desc->nr_channels; i++) {
1676 if (desc->channel[first_non_void].size != desc->channel[i].size)
1677 return V_008F0C_BUF_DATA_FORMAT_INVALID;
1678 }
1679
1680 switch (desc->channel[first_non_void].size) {
1681 case 8:
1682 switch (desc->nr_channels) {
1683 case 1:
1684 return V_008F0C_BUF_DATA_FORMAT_8;
1685 case 2:
1686 return V_008F0C_BUF_DATA_FORMAT_8_8;
1687 case 3:
1688 case 4:
1689 return V_008F0C_BUF_DATA_FORMAT_8_8_8_8;
1690 }
1691 break;
1692 case 16:
1693 switch (desc->nr_channels) {
1694 case 1:
1695 return V_008F0C_BUF_DATA_FORMAT_16;
1696 case 2:
1697 return V_008F0C_BUF_DATA_FORMAT_16_16;
1698 case 3:
1699 case 4:
1700 return V_008F0C_BUF_DATA_FORMAT_16_16_16_16;
1701 }
1702 break;
1703 case 32:
1704 /* From the Southern Islands ISA documentation about MTBUF:
1705 * 'Memory reads of data in memory that is 32 or 64 bits do not
1706 * undergo any format conversion.'
1707 */
1708 if (type != UTIL_FORMAT_TYPE_FLOAT &&
1709 !desc->channel[first_non_void].pure_integer)
1710 return V_008F0C_BUF_DATA_FORMAT_INVALID;
1711
1712 switch (desc->nr_channels) {
1713 case 1:
1714 return V_008F0C_BUF_DATA_FORMAT_32;
1715 case 2:
1716 return V_008F0C_BUF_DATA_FORMAT_32_32;
1717 case 3:
1718 return V_008F0C_BUF_DATA_FORMAT_32_32_32;
1719 case 4:
1720 return V_008F0C_BUF_DATA_FORMAT_32_32_32_32;
1721 }
1722 break;
1723 }
1724
1725 return V_008F0C_BUF_DATA_FORMAT_INVALID;
1726 }
1727
1728 static uint32_t si_translate_buffer_numformat(struct pipe_screen *screen,
1729 const struct util_format_description *desc,
1730 int first_non_void)
1731 {
1732 if (desc->format == PIPE_FORMAT_R11G11B10_FLOAT)
1733 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
1734
1735 assert(first_non_void >= 0);
1736
1737 switch (desc->channel[first_non_void].type) {
1738 case UTIL_FORMAT_TYPE_SIGNED:
1739 if (desc->channel[first_non_void].normalized)
1740 return V_008F0C_BUF_NUM_FORMAT_SNORM;
1741 else if (desc->channel[first_non_void].pure_integer)
1742 return V_008F0C_BUF_NUM_FORMAT_SINT;
1743 else
1744 return V_008F0C_BUF_NUM_FORMAT_SSCALED;
1745 break;
1746 case UTIL_FORMAT_TYPE_UNSIGNED:
1747 if (desc->channel[first_non_void].normalized)
1748 return V_008F0C_BUF_NUM_FORMAT_UNORM;
1749 else if (desc->channel[first_non_void].pure_integer)
1750 return V_008F0C_BUF_NUM_FORMAT_UINT;
1751 else
1752 return V_008F0C_BUF_NUM_FORMAT_USCALED;
1753 break;
1754 case UTIL_FORMAT_TYPE_FLOAT:
1755 default:
1756 return V_008F0C_BUF_NUM_FORMAT_FLOAT;
1757 }
1758 }
1759
1760 static bool si_is_vertex_format_supported(struct pipe_screen *screen, enum pipe_format format)
1761 {
1762 const struct util_format_description *desc;
1763 int first_non_void;
1764 unsigned data_format;
1765
1766 desc = util_format_description(format);
1767 first_non_void = util_format_get_first_non_void_channel(format);
1768 data_format = si_translate_buffer_dataformat(screen, desc, first_non_void);
1769 return data_format != V_008F0C_BUF_DATA_FORMAT_INVALID;
1770 }
1771
1772 static bool si_is_colorbuffer_format_supported(enum pipe_format format)
1773 {
1774 return si_translate_colorformat(format) != V_028C70_COLOR_INVALID &&
1775 r600_translate_colorswap(format, false) != ~0U;
1776 }
1777
1778 static bool si_is_zs_format_supported(enum pipe_format format)
1779 {
1780 return si_translate_dbformat(format) != V_028040_Z_INVALID;
1781 }
1782
1783 static boolean si_is_format_supported(struct pipe_screen *screen,
1784 enum pipe_format format,
1785 enum pipe_texture_target target,
1786 unsigned sample_count,
1787 unsigned usage)
1788 {
1789 unsigned retval = 0;
1790
1791 if (target >= PIPE_MAX_TEXTURE_TYPES) {
1792 R600_ERR("r600: unsupported texture type %d\n", target);
1793 return false;
1794 }
1795
1796 if (!util_format_is_supported(format, usage))
1797 return false;
1798
1799 if (sample_count > 1) {
1800 if (!screen->get_param(screen, PIPE_CAP_TEXTURE_MULTISAMPLE))
1801 return false;
1802
1803 if (usage & PIPE_BIND_SHADER_IMAGE)
1804 return false;
1805
1806 switch (sample_count) {
1807 case 2:
1808 case 4:
1809 case 8:
1810 break;
1811 case 16:
1812 if (format == PIPE_FORMAT_NONE)
1813 return true;
1814 else
1815 return false;
1816 default:
1817 return false;
1818 }
1819 }
1820
1821 if (usage & (PIPE_BIND_SAMPLER_VIEW |
1822 PIPE_BIND_SHADER_IMAGE)) {
1823 if (target == PIPE_BUFFER) {
1824 if (si_is_vertex_format_supported(screen, format))
1825 retval |= usage & (PIPE_BIND_SAMPLER_VIEW |
1826 PIPE_BIND_SHADER_IMAGE);
1827 } else {
1828 if (si_is_sampler_format_supported(screen, format))
1829 retval |= usage & (PIPE_BIND_SAMPLER_VIEW |
1830 PIPE_BIND_SHADER_IMAGE);
1831 }
1832 }
1833
1834 if ((usage & (PIPE_BIND_RENDER_TARGET |
1835 PIPE_BIND_DISPLAY_TARGET |
1836 PIPE_BIND_SCANOUT |
1837 PIPE_BIND_SHARED |
1838 PIPE_BIND_BLENDABLE)) &&
1839 si_is_colorbuffer_format_supported(format)) {
1840 retval |= usage &
1841 (PIPE_BIND_RENDER_TARGET |
1842 PIPE_BIND_DISPLAY_TARGET |
1843 PIPE_BIND_SCANOUT |
1844 PIPE_BIND_SHARED);
1845 if (!util_format_is_pure_integer(format) &&
1846 !util_format_is_depth_or_stencil(format))
1847 retval |= usage & PIPE_BIND_BLENDABLE;
1848 }
1849
1850 if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
1851 si_is_zs_format_supported(format)) {
1852 retval |= PIPE_BIND_DEPTH_STENCIL;
1853 }
1854
1855 if ((usage & PIPE_BIND_VERTEX_BUFFER) &&
1856 si_is_vertex_format_supported(screen, format)) {
1857 retval |= PIPE_BIND_VERTEX_BUFFER;
1858 }
1859
1860 if (usage & PIPE_BIND_TRANSFER_READ)
1861 retval |= PIPE_BIND_TRANSFER_READ;
1862 if (usage & PIPE_BIND_TRANSFER_WRITE)
1863 retval |= PIPE_BIND_TRANSFER_WRITE;
1864
1865 if ((usage & PIPE_BIND_LINEAR) &&
1866 !util_format_is_compressed(format) &&
1867 !(usage & PIPE_BIND_DEPTH_STENCIL))
1868 retval |= PIPE_BIND_LINEAR;
1869
1870 return retval == usage;
1871 }
1872
1873 /*
1874 * framebuffer handling
1875 */
1876
1877 static void si_choose_spi_color_formats(struct r600_surface *surf,
1878 unsigned format, unsigned swap,
1879 unsigned ntype, bool is_depth)
1880 {
1881 /* Alpha is needed for alpha-to-coverage.
1882 * Blending may be with or without alpha.
1883 */
1884 unsigned normal = 0; /* most optimal, may not support blending or export alpha */
1885 unsigned alpha = 0; /* exports alpha, but may not support blending */
1886 unsigned blend = 0; /* supports blending, but may not export alpha */
1887 unsigned blend_alpha = 0; /* least optimal, supports blending and exports alpha */
1888
1889 /* Choose the SPI color formats. These are required values for Stoney/RB+.
1890 * Other chips have multiple choices, though they are not necessarily better.
1891 */
1892 switch (format) {
1893 case V_028C70_COLOR_5_6_5:
1894 case V_028C70_COLOR_1_5_5_5:
1895 case V_028C70_COLOR_5_5_5_1:
1896 case V_028C70_COLOR_4_4_4_4:
1897 case V_028C70_COLOR_10_11_11:
1898 case V_028C70_COLOR_11_11_10:
1899 case V_028C70_COLOR_8:
1900 case V_028C70_COLOR_8_8:
1901 case V_028C70_COLOR_8_8_8_8:
1902 case V_028C70_COLOR_10_10_10_2:
1903 case V_028C70_COLOR_2_10_10_10:
1904 if (ntype == V_028C70_NUMBER_UINT)
1905 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
1906 else if (ntype == V_028C70_NUMBER_SINT)
1907 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
1908 else
1909 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
1910 break;
1911
1912 case V_028C70_COLOR_16:
1913 case V_028C70_COLOR_16_16:
1914 case V_028C70_COLOR_16_16_16_16:
1915 if (ntype == V_028C70_NUMBER_UNORM ||
1916 ntype == V_028C70_NUMBER_SNORM) {
1917 /* UNORM16 and SNORM16 don't support blending */
1918 if (ntype == V_028C70_NUMBER_UNORM)
1919 normal = alpha = V_028714_SPI_SHADER_UNORM16_ABGR;
1920 else
1921 normal = alpha = V_028714_SPI_SHADER_SNORM16_ABGR;
1922
1923 /* Use 32 bits per channel for blending. */
1924 if (format == V_028C70_COLOR_16) {
1925 if (swap == V_028C70_SWAP_STD) { /* R */
1926 blend = V_028714_SPI_SHADER_32_R;
1927 blend_alpha = V_028714_SPI_SHADER_32_AR;
1928 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
1929 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
1930 else
1931 assert(0);
1932 } else if (format == V_028C70_COLOR_16_16) {
1933 if (swap == V_028C70_SWAP_STD) { /* RG */
1934 blend = V_028714_SPI_SHADER_32_GR;
1935 blend_alpha = V_028714_SPI_SHADER_32_ABGR;
1936 } else if (swap == V_028C70_SWAP_ALT) /* RA */
1937 blend = blend_alpha = V_028714_SPI_SHADER_32_AR;
1938 else
1939 assert(0);
1940 } else /* 16_16_16_16 */
1941 blend = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
1942 } else if (ntype == V_028C70_NUMBER_UINT)
1943 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_UINT16_ABGR;
1944 else if (ntype == V_028C70_NUMBER_SINT)
1945 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_SINT16_ABGR;
1946 else if (ntype == V_028C70_NUMBER_FLOAT)
1947 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_FP16_ABGR;
1948 else
1949 assert(0);
1950 break;
1951
1952 case V_028C70_COLOR_32:
1953 if (swap == V_028C70_SWAP_STD) { /* R */
1954 blend = normal = V_028714_SPI_SHADER_32_R;
1955 alpha = blend_alpha = V_028714_SPI_SHADER_32_AR;
1956 } else if (swap == V_028C70_SWAP_ALT_REV) /* A */
1957 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
1958 else
1959 assert(0);
1960 break;
1961
1962 case V_028C70_COLOR_32_32:
1963 if (swap == V_028C70_SWAP_STD) { /* RG */
1964 blend = normal = V_028714_SPI_SHADER_32_GR;
1965 alpha = blend_alpha = V_028714_SPI_SHADER_32_ABGR;
1966 } else if (swap == V_028C70_SWAP_ALT) /* RA */
1967 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_AR;
1968 else
1969 assert(0);
1970 break;
1971
1972 case V_028C70_COLOR_32_32_32_32:
1973 case V_028C70_COLOR_8_24:
1974 case V_028C70_COLOR_24_8:
1975 case V_028C70_COLOR_X24_8_32_FLOAT:
1976 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
1977 break;
1978
1979 default:
1980 assert(0);
1981 return;
1982 }
1983
1984 /* The DB->CB copy needs 32_ABGR. */
1985 if (is_depth)
1986 alpha = blend = blend_alpha = normal = V_028714_SPI_SHADER_32_ABGR;
1987
1988 surf->spi_shader_col_format = normal;
1989 surf->spi_shader_col_format_alpha = alpha;
1990 surf->spi_shader_col_format_blend = blend;
1991 surf->spi_shader_col_format_blend_alpha = blend_alpha;
1992 }
1993
1994 static void si_initialize_color_surface(struct si_context *sctx,
1995 struct r600_surface *surf)
1996 {
1997 struct r600_texture *rtex = (struct r600_texture*)surf->base.texture;
1998 unsigned color_info, color_attrib, color_view;
1999 unsigned format, swap, ntype, endian;
2000 const struct util_format_description *desc;
2001 int i;
2002 unsigned blend_clamp = 0, blend_bypass = 0;
2003
2004 color_view = S_028C6C_SLICE_START(surf->base.u.tex.first_layer) |
2005 S_028C6C_SLICE_MAX(surf->base.u.tex.last_layer);
2006
2007 desc = util_format_description(surf->base.format);
2008 for (i = 0; i < 4; i++) {
2009 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
2010 break;
2011 }
2012 }
2013 if (i == 4 || desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT) {
2014 ntype = V_028C70_NUMBER_FLOAT;
2015 } else {
2016 ntype = V_028C70_NUMBER_UNORM;
2017 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
2018 ntype = V_028C70_NUMBER_SRGB;
2019 else if (desc->channel[i].type == UTIL_FORMAT_TYPE_SIGNED) {
2020 if (desc->channel[i].pure_integer) {
2021 ntype = V_028C70_NUMBER_SINT;
2022 } else {
2023 assert(desc->channel[i].normalized);
2024 ntype = V_028C70_NUMBER_SNORM;
2025 }
2026 } else if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) {
2027 if (desc->channel[i].pure_integer) {
2028 ntype = V_028C70_NUMBER_UINT;
2029 } else {
2030 assert(desc->channel[i].normalized);
2031 ntype = V_028C70_NUMBER_UNORM;
2032 }
2033 }
2034 }
2035
2036 format = si_translate_colorformat(surf->base.format);
2037 if (format == V_028C70_COLOR_INVALID) {
2038 R600_ERR("Invalid CB format: %d, disabling CB.\n", surf->base.format);
2039 }
2040 assert(format != V_028C70_COLOR_INVALID);
2041 swap = r600_translate_colorswap(surf->base.format, false);
2042 endian = si_colorformat_endian_swap(format);
2043
2044 /* blend clamp should be set for all NORM/SRGB types */
2045 if (ntype == V_028C70_NUMBER_UNORM ||
2046 ntype == V_028C70_NUMBER_SNORM ||
2047 ntype == V_028C70_NUMBER_SRGB)
2048 blend_clamp = 1;
2049
2050 /* set blend bypass according to docs if SINT/UINT or
2051 8/24 COLOR variants */
2052 if (ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT ||
2053 format == V_028C70_COLOR_8_24 || format == V_028C70_COLOR_24_8 ||
2054 format == V_028C70_COLOR_X24_8_32_FLOAT) {
2055 blend_clamp = 0;
2056 blend_bypass = 1;
2057 }
2058
2059 if ((ntype == V_028C70_NUMBER_UINT || ntype == V_028C70_NUMBER_SINT) &&
2060 (format == V_028C70_COLOR_8 ||
2061 format == V_028C70_COLOR_8_8 ||
2062 format == V_028C70_COLOR_8_8_8_8))
2063 surf->color_is_int8 = true;
2064
2065 color_info = S_028C70_FORMAT(format) |
2066 S_028C70_COMP_SWAP(swap) |
2067 S_028C70_BLEND_CLAMP(blend_clamp) |
2068 S_028C70_BLEND_BYPASS(blend_bypass) |
2069 S_028C70_SIMPLE_FLOAT(1) |
2070 S_028C70_NUMBER_TYPE(ntype) |
2071 S_028C70_ENDIAN(endian);
2072
2073 /* Intensity is implemented as Red, so treat it that way. */
2074 color_attrib = S_028C74_FORCE_DST_ALPHA_1(desc->swizzle[3] == PIPE_SWIZZLE_1 ||
2075 util_format_is_intensity(surf->base.format));
2076
2077 if (rtex->resource.b.b.nr_samples > 1) {
2078 unsigned log_samples = util_logbase2(rtex->resource.b.b.nr_samples);
2079
2080 color_attrib |= S_028C74_NUM_SAMPLES(log_samples) |
2081 S_028C74_NUM_FRAGMENTS(log_samples);
2082
2083 if (rtex->fmask.size) {
2084 color_info |= S_028C70_COMPRESSION(1);
2085 unsigned fmask_bankh = util_logbase2(rtex->fmask.bank_height);
2086
2087 if (sctx->b.chip_class == SI) {
2088 /* due to a hw bug, FMASK_BANK_HEIGHT must be set on SI too */
2089 color_attrib |= S_028C74_FMASK_BANK_HEIGHT(fmask_bankh);
2090 }
2091 }
2092 }
2093
2094 surf->cb_color_view = color_view;
2095 surf->cb_color_info = color_info;
2096 surf->cb_color_attrib = color_attrib;
2097
2098 if (sctx->b.chip_class >= VI) {
2099 unsigned max_uncompressed_block_size = 2;
2100
2101 if (rtex->surface.nsamples > 1) {
2102 if (rtex->surface.bpe == 1)
2103 max_uncompressed_block_size = 0;
2104 else if (rtex->surface.bpe == 2)
2105 max_uncompressed_block_size = 1;
2106 }
2107
2108 surf->cb_dcc_control = S_028C78_MAX_UNCOMPRESSED_BLOCK_SIZE(max_uncompressed_block_size) |
2109 S_028C78_INDEPENDENT_64B_BLOCKS(1);
2110 }
2111
2112 /* This must be set for fast clear to work without FMASK. */
2113 if (!rtex->fmask.size && sctx->b.chip_class == SI) {
2114 unsigned bankh = util_logbase2(rtex->surface.bankh);
2115 surf->cb_color_attrib |= S_028C74_FMASK_BANK_HEIGHT(bankh);
2116 }
2117
2118 /* Determine pixel shader export format */
2119 si_choose_spi_color_formats(surf, format, swap, ntype, rtex->is_depth);
2120
2121 surf->color_initialized = true;
2122 }
2123
2124 static void si_init_depth_surface(struct si_context *sctx,
2125 struct r600_surface *surf)
2126 {
2127 struct r600_texture *rtex = (struct r600_texture*)surf->base.texture;
2128 unsigned level = surf->base.u.tex.level;
2129 struct radeon_surf_level *levelinfo = &rtex->surface.level[level];
2130 unsigned format;
2131 uint32_t z_info, s_info, db_depth_info;
2132 uint64_t z_offs, s_offs;
2133 uint32_t db_htile_data_base, db_htile_surface;
2134
2135 format = si_translate_dbformat(rtex->resource.b.b.format);
2136
2137 if (format == V_028040_Z_INVALID) {
2138 R600_ERR("Invalid DB format: %d, disabling DB.\n", rtex->resource.b.b.format);
2139 }
2140 assert(format != V_028040_Z_INVALID);
2141
2142 s_offs = z_offs = rtex->resource.gpu_address;
2143 z_offs += rtex->surface.level[level].offset;
2144 s_offs += rtex->surface.stencil_level[level].offset;
2145
2146 db_depth_info = S_02803C_ADDR5_SWIZZLE_MASK(1);
2147
2148 z_info = S_028040_FORMAT(format);
2149 if (rtex->resource.b.b.nr_samples > 1) {
2150 z_info |= S_028040_NUM_SAMPLES(util_logbase2(rtex->resource.b.b.nr_samples));
2151 }
2152
2153 if (rtex->surface.flags & RADEON_SURF_SBUFFER)
2154 s_info = S_028044_FORMAT(V_028044_STENCIL_8);
2155 else
2156 s_info = S_028044_FORMAT(V_028044_STENCIL_INVALID);
2157
2158 if (sctx->b.chip_class >= CIK) {
2159 struct radeon_info *info = &sctx->screen->b.info;
2160 unsigned index = rtex->surface.tiling_index[level];
2161 unsigned stencil_index = rtex->surface.stencil_tiling_index[level];
2162 unsigned macro_index = rtex->surface.macro_tile_index;
2163 unsigned tile_mode = info->si_tile_mode_array[index];
2164 unsigned stencil_tile_mode = info->si_tile_mode_array[stencil_index];
2165 unsigned macro_mode = info->cik_macrotile_mode_array[macro_index];
2166
2167 db_depth_info |=
2168 S_02803C_ARRAY_MODE(G_009910_ARRAY_MODE(tile_mode)) |
2169 S_02803C_PIPE_CONFIG(G_009910_PIPE_CONFIG(tile_mode)) |
2170 S_02803C_BANK_WIDTH(G_009990_BANK_WIDTH(macro_mode)) |
2171 S_02803C_BANK_HEIGHT(G_009990_BANK_HEIGHT(macro_mode)) |
2172 S_02803C_MACRO_TILE_ASPECT(G_009990_MACRO_TILE_ASPECT(macro_mode)) |
2173 S_02803C_NUM_BANKS(G_009990_NUM_BANKS(macro_mode));
2174 z_info |= S_028040_TILE_SPLIT(G_009910_TILE_SPLIT(tile_mode));
2175 s_info |= S_028044_TILE_SPLIT(G_009910_TILE_SPLIT(stencil_tile_mode));
2176 } else {
2177 unsigned tile_mode_index = si_tile_mode_index(rtex, level, false);
2178 z_info |= S_028040_TILE_MODE_INDEX(tile_mode_index);
2179 tile_mode_index = si_tile_mode_index(rtex, level, true);
2180 s_info |= S_028044_TILE_MODE_INDEX(tile_mode_index);
2181 }
2182
2183 /* HiZ aka depth buffer htile */
2184 /* use htile only for first level */
2185 if (rtex->htile_buffer && !level) {
2186 z_info |= S_028040_TILE_SURFACE_ENABLE(1) |
2187 S_028040_ALLOW_EXPCLEAR(1);
2188
2189 if (rtex->surface.flags & RADEON_SURF_SBUFFER) {
2190 /* Workaround: For a not yet understood reason, the
2191 * combination of MSAA, fast stencil clear and stencil
2192 * decompress messes with subsequent stencil buffer
2193 * uses. Problem was reproduced on Verde, Bonaire,
2194 * Tonga, and Carrizo.
2195 *
2196 * Disabling EXPCLEAR works around the problem.
2197 *
2198 * Check piglit's arb_texture_multisample-stencil-clear
2199 * test if you want to try changing this.
2200 */
2201 if (rtex->resource.b.b.nr_samples <= 1)
2202 s_info |= S_028044_ALLOW_EXPCLEAR(1);
2203 } else
2204 /* Use all of the htile_buffer for depth if there's no stencil. */
2205 s_info |= S_028044_TILE_STENCIL_DISABLE(1);
2206
2207 uint64_t va = rtex->htile_buffer->gpu_address;
2208 db_htile_data_base = va >> 8;
2209 db_htile_surface = S_028ABC_FULL_CACHE(1);
2210 } else {
2211 db_htile_data_base = 0;
2212 db_htile_surface = 0;
2213 }
2214
2215 assert(levelinfo->nblk_x % 8 == 0 && levelinfo->nblk_y % 8 == 0);
2216
2217 surf->db_depth_view = S_028008_SLICE_START(surf->base.u.tex.first_layer) |
2218 S_028008_SLICE_MAX(surf->base.u.tex.last_layer);
2219 surf->db_htile_data_base = db_htile_data_base;
2220 surf->db_depth_info = db_depth_info;
2221 surf->db_z_info = z_info;
2222 surf->db_stencil_info = s_info;
2223 surf->db_depth_base = z_offs >> 8;
2224 surf->db_stencil_base = s_offs >> 8;
2225 surf->db_depth_size = S_028058_PITCH_TILE_MAX((levelinfo->nblk_x / 8) - 1) |
2226 S_028058_HEIGHT_TILE_MAX((levelinfo->nblk_y / 8) - 1);
2227 surf->db_depth_slice = S_02805C_SLICE_TILE_MAX((levelinfo->nblk_x *
2228 levelinfo->nblk_y) / 64 - 1);
2229 surf->db_htile_surface = db_htile_surface;
2230
2231 surf->depth_initialized = true;
2232 }
2233
2234 static void si_dec_framebuffer_counters(const struct pipe_framebuffer_state *state)
2235 {
2236 for (int i = 0; i < state->nr_cbufs; ++i) {
2237 struct r600_surface *surf = NULL;
2238 struct r600_texture *rtex;
2239
2240 if (!state->cbufs[i])
2241 continue;
2242 surf = (struct r600_surface*)state->cbufs[i];
2243 rtex = (struct r600_texture*)surf->base.texture;
2244
2245 p_atomic_dec(&rtex->framebuffers_bound);
2246 }
2247 }
2248
2249 static void si_set_framebuffer_state(struct pipe_context *ctx,
2250 const struct pipe_framebuffer_state *state)
2251 {
2252 struct si_context *sctx = (struct si_context *)ctx;
2253 struct pipe_constant_buffer constbuf = {0};
2254 struct r600_surface *surf = NULL;
2255 struct r600_texture *rtex;
2256 bool old_cb0_is_integer = sctx->framebuffer.cb0_is_integer;
2257 bool old_any_dst_linear = sctx->framebuffer.any_dst_linear;
2258 unsigned old_nr_samples = sctx->framebuffer.nr_samples;
2259 int i;
2260
2261 for (i = 0; i < sctx->framebuffer.state.nr_cbufs; i++) {
2262 if (!sctx->framebuffer.state.cbufs[i])
2263 continue;
2264
2265 rtex = (struct r600_texture*)sctx->framebuffer.state.cbufs[i]->texture;
2266 if (rtex->dcc_gather_statistics)
2267 vi_separate_dcc_stop_query(ctx, rtex);
2268 }
2269
2270 /* Only flush TC when changing the framebuffer state, because
2271 * the only client not using TC that can change textures is
2272 * the framebuffer.
2273 *
2274 * Flush all CB and DB caches here because all buffers can be used
2275 * for write by both TC (with shader image stores) and CB/DB.
2276 */
2277 sctx->b.flags |= SI_CONTEXT_INV_VMEM_L1 |
2278 SI_CONTEXT_INV_GLOBAL_L2 |
2279 SI_CONTEXT_FLUSH_AND_INV_FRAMEBUFFER |
2280 SI_CONTEXT_CS_PARTIAL_FLUSH;
2281
2282 /* Take the maximum of the old and new count. If the new count is lower,
2283 * dirtying is needed to disable the unbound colorbuffers.
2284 */
2285 sctx->framebuffer.dirty_cbufs |=
2286 (1 << MAX2(sctx->framebuffer.state.nr_cbufs, state->nr_cbufs)) - 1;
2287 sctx->framebuffer.dirty_zsbuf |= sctx->framebuffer.state.zsbuf != state->zsbuf;
2288
2289 si_dec_framebuffer_counters(&sctx->framebuffer.state);
2290 util_copy_framebuffer_state(&sctx->framebuffer.state, state);
2291
2292 sctx->framebuffer.spi_shader_col_format = 0;
2293 sctx->framebuffer.spi_shader_col_format_alpha = 0;
2294 sctx->framebuffer.spi_shader_col_format_blend = 0;
2295 sctx->framebuffer.spi_shader_col_format_blend_alpha = 0;
2296 sctx->framebuffer.color_is_int8 = 0;
2297
2298 sctx->framebuffer.compressed_cb_mask = 0;
2299 sctx->framebuffer.nr_samples = util_framebuffer_get_num_samples(state);
2300 sctx->framebuffer.log_samples = util_logbase2(sctx->framebuffer.nr_samples);
2301 sctx->framebuffer.cb0_is_integer = state->nr_cbufs && state->cbufs[0] &&
2302 util_format_is_pure_integer(state->cbufs[0]->format);
2303 sctx->framebuffer.any_dst_linear = false;
2304
2305 if (sctx->framebuffer.cb0_is_integer != old_cb0_is_integer)
2306 si_mark_atom_dirty(sctx, &sctx->db_render_state);
2307
2308 for (i = 0; i < state->nr_cbufs; i++) {
2309 if (!state->cbufs[i])
2310 continue;
2311
2312 surf = (struct r600_surface*)state->cbufs[i];
2313 rtex = (struct r600_texture*)surf->base.texture;
2314
2315 if (!surf->color_initialized) {
2316 si_initialize_color_surface(sctx, surf);
2317 }
2318
2319 sctx->framebuffer.spi_shader_col_format |=
2320 surf->spi_shader_col_format << (i * 4);
2321 sctx->framebuffer.spi_shader_col_format_alpha |=
2322 surf->spi_shader_col_format_alpha << (i * 4);
2323 sctx->framebuffer.spi_shader_col_format_blend |=
2324 surf->spi_shader_col_format_blend << (i * 4);
2325 sctx->framebuffer.spi_shader_col_format_blend_alpha |=
2326 surf->spi_shader_col_format_blend_alpha << (i * 4);
2327
2328 if (surf->color_is_int8)
2329 sctx->framebuffer.color_is_int8 |= 1 << i;
2330
2331 if (rtex->fmask.size && rtex->cmask.size) {
2332 sctx->framebuffer.compressed_cb_mask |= 1 << i;
2333 }
2334
2335 if (surf->level_info->mode == RADEON_SURF_MODE_LINEAR_ALIGNED)
2336 sctx->framebuffer.any_dst_linear = true;
2337
2338 r600_context_add_resource_size(ctx, surf->base.texture);
2339
2340 p_atomic_inc(&rtex->framebuffers_bound);
2341
2342 if (rtex->dcc_gather_statistics) {
2343 /* Dirty tracking must be enabled for DCC usage analysis. */
2344 sctx->framebuffer.compressed_cb_mask |= 1 << i;
2345 vi_separate_dcc_start_query(ctx, rtex);
2346 }
2347 }
2348 /* Set the second SPI format for possible dual-src blending. */
2349 if (i == 1 && surf) {
2350 sctx->framebuffer.spi_shader_col_format |=
2351 surf->spi_shader_col_format << (i * 4);
2352 sctx->framebuffer.spi_shader_col_format_alpha |=
2353 surf->spi_shader_col_format_alpha << (i * 4);
2354 sctx->framebuffer.spi_shader_col_format_blend |=
2355 surf->spi_shader_col_format_blend << (i * 4);
2356 sctx->framebuffer.spi_shader_col_format_blend_alpha |=
2357 surf->spi_shader_col_format_blend_alpha << (i * 4);
2358 }
2359
2360 if (state->zsbuf) {
2361 surf = (struct r600_surface*)state->zsbuf;
2362
2363 if (!surf->depth_initialized) {
2364 si_init_depth_surface(sctx, surf);
2365 }
2366 r600_context_add_resource_size(ctx, surf->base.texture);
2367 }
2368
2369 si_update_poly_offset_state(sctx);
2370 si_mark_atom_dirty(sctx, &sctx->cb_render_state);
2371 si_mark_atom_dirty(sctx, &sctx->framebuffer.atom);
2372
2373 if (sctx->framebuffer.any_dst_linear != old_any_dst_linear)
2374 si_mark_atom_dirty(sctx, &sctx->msaa_config);
2375
2376 if (sctx->framebuffer.nr_samples != old_nr_samples) {
2377 si_mark_atom_dirty(sctx, &sctx->msaa_config);
2378 si_mark_atom_dirty(sctx, &sctx->db_render_state);
2379
2380 /* Set sample locations as fragment shader constants. */
2381 switch (sctx->framebuffer.nr_samples) {
2382 case 1:
2383 constbuf.user_buffer = sctx->b.sample_locations_1x;
2384 break;
2385 case 2:
2386 constbuf.user_buffer = sctx->b.sample_locations_2x;
2387 break;
2388 case 4:
2389 constbuf.user_buffer = sctx->b.sample_locations_4x;
2390 break;
2391 case 8:
2392 constbuf.user_buffer = sctx->b.sample_locations_8x;
2393 break;
2394 case 16:
2395 constbuf.user_buffer = sctx->b.sample_locations_16x;
2396 break;
2397 default:
2398 R600_ERR("Requested an invalid number of samples %i.\n",
2399 sctx->framebuffer.nr_samples);
2400 assert(0);
2401 }
2402 constbuf.buffer_size = sctx->framebuffer.nr_samples * 2 * 4;
2403 si_set_rw_buffer(sctx, SI_PS_CONST_SAMPLE_POSITIONS, &constbuf);
2404
2405 si_mark_atom_dirty(sctx, &sctx->msaa_sample_locs.atom);
2406 }
2407
2408 sctx->need_check_render_feedback = true;
2409 sctx->do_update_shaders = true;
2410 }
2411
2412 static void si_emit_framebuffer_state(struct si_context *sctx, struct r600_atom *atom)
2413 {
2414 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
2415 struct pipe_framebuffer_state *state = &sctx->framebuffer.state;
2416 unsigned i, nr_cbufs = state->nr_cbufs;
2417 struct r600_texture *tex = NULL;
2418 struct r600_surface *cb = NULL;
2419 unsigned cb_color_info = 0;
2420
2421 /* Colorbuffers. */
2422 for (i = 0; i < nr_cbufs; i++) {
2423 unsigned pitch_tile_max, slice_tile_max, tile_mode_index;
2424 unsigned cb_color_base, cb_color_fmask, cb_color_attrib;
2425 unsigned cb_color_pitch, cb_color_slice, cb_color_fmask_slice;
2426
2427 if (!(sctx->framebuffer.dirty_cbufs & (1 << i)))
2428 continue;
2429
2430 cb = (struct r600_surface*)state->cbufs[i];
2431 if (!cb) {
2432 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C,
2433 S_028C70_FORMAT(V_028C70_COLOR_INVALID));
2434 continue;
2435 }
2436
2437 tex = (struct r600_texture *)cb->base.texture;
2438 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
2439 &tex->resource, RADEON_USAGE_READWRITE,
2440 tex->surface.nsamples > 1 ?
2441 RADEON_PRIO_COLOR_BUFFER_MSAA :
2442 RADEON_PRIO_COLOR_BUFFER);
2443
2444 if (tex->cmask_buffer && tex->cmask_buffer != &tex->resource) {
2445 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
2446 tex->cmask_buffer, RADEON_USAGE_READWRITE,
2447 RADEON_PRIO_CMASK);
2448 }
2449
2450 if (tex->dcc_separate_buffer)
2451 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
2452 tex->dcc_separate_buffer,
2453 RADEON_USAGE_READWRITE,
2454 RADEON_PRIO_DCC);
2455
2456 /* Compute mutable surface parameters. */
2457 pitch_tile_max = cb->level_info->nblk_x / 8 - 1;
2458 slice_tile_max = cb->level_info->nblk_x *
2459 cb->level_info->nblk_y / 64 - 1;
2460 tile_mode_index = si_tile_mode_index(tex, cb->base.u.tex.level, false);
2461
2462 cb_color_base = (tex->resource.gpu_address + cb->level_info->offset) >> 8;
2463 cb_color_pitch = S_028C64_TILE_MAX(pitch_tile_max);
2464 cb_color_slice = S_028C68_TILE_MAX(slice_tile_max);
2465 cb_color_attrib = cb->cb_color_attrib |
2466 S_028C74_TILE_MODE_INDEX(tile_mode_index);
2467
2468 if (tex->fmask.size) {
2469 if (sctx->b.chip_class >= CIK)
2470 cb_color_pitch |= S_028C64_FMASK_TILE_MAX(tex->fmask.pitch_in_pixels / 8 - 1);
2471 cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tex->fmask.tile_mode_index);
2472 cb_color_fmask = (tex->resource.gpu_address + tex->fmask.offset) >> 8;
2473 cb_color_fmask_slice = S_028C88_TILE_MAX(tex->fmask.slice_tile_max);
2474 } else {
2475 /* This must be set for fast clear to work without FMASK. */
2476 if (sctx->b.chip_class >= CIK)
2477 cb_color_pitch |= S_028C64_FMASK_TILE_MAX(pitch_tile_max);
2478 cb_color_attrib |= S_028C74_FMASK_TILE_MODE_INDEX(tile_mode_index);
2479 cb_color_fmask = cb_color_base;
2480 cb_color_fmask_slice = S_028C88_TILE_MAX(slice_tile_max);
2481 }
2482
2483 cb_color_info = cb->cb_color_info | tex->cb_color_info;
2484
2485 if (tex->dcc_offset && cb->level_info->dcc_enabled) {
2486 bool is_msaa_resolve_dst = state->cbufs[0] &&
2487 state->cbufs[0]->texture->nr_samples > 1 &&
2488 state->cbufs[1] == &cb->base &&
2489 state->cbufs[1]->texture->nr_samples <= 1;
2490
2491 if (!is_msaa_resolve_dst)
2492 cb_color_info |= S_028C70_DCC_ENABLE(1);
2493 }
2494
2495 radeon_set_context_reg_seq(cs, R_028C60_CB_COLOR0_BASE + i * 0x3C,
2496 sctx->b.chip_class >= VI ? 14 : 13);
2497 radeon_emit(cs, cb_color_base); /* R_028C60_CB_COLOR0_BASE */
2498 radeon_emit(cs, cb_color_pitch); /* R_028C64_CB_COLOR0_PITCH */
2499 radeon_emit(cs, cb_color_slice); /* R_028C68_CB_COLOR0_SLICE */
2500 radeon_emit(cs, cb->cb_color_view); /* R_028C6C_CB_COLOR0_VIEW */
2501 radeon_emit(cs, cb_color_info); /* R_028C70_CB_COLOR0_INFO */
2502 radeon_emit(cs, cb_color_attrib); /* R_028C74_CB_COLOR0_ATTRIB */
2503 radeon_emit(cs, cb->cb_dcc_control); /* R_028C78_CB_COLOR0_DCC_CONTROL */
2504 radeon_emit(cs, tex->cmask.base_address_reg); /* R_028C7C_CB_COLOR0_CMASK */
2505 radeon_emit(cs, tex->cmask.slice_tile_max); /* R_028C80_CB_COLOR0_CMASK_SLICE */
2506 radeon_emit(cs, cb_color_fmask); /* R_028C84_CB_COLOR0_FMASK */
2507 radeon_emit(cs, cb_color_fmask_slice); /* R_028C88_CB_COLOR0_FMASK_SLICE */
2508 radeon_emit(cs, tex->color_clear_value[0]); /* R_028C8C_CB_COLOR0_CLEAR_WORD0 */
2509 radeon_emit(cs, tex->color_clear_value[1]); /* R_028C90_CB_COLOR0_CLEAR_WORD1 */
2510
2511 if (sctx->b.chip_class >= VI) /* R_028C94_CB_COLOR0_DCC_BASE */
2512 radeon_emit(cs, ((!tex->dcc_separate_buffer ? tex->resource.gpu_address : 0) +
2513 tex->dcc_offset +
2514 tex->surface.level[cb->base.u.tex.level].dcc_offset) >> 8);
2515 }
2516 /* set CB_COLOR1_INFO for possible dual-src blending */
2517 if (i == 1 && state->cbufs[0] &&
2518 sctx->framebuffer.dirty_cbufs & (1 << 0)) {
2519 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + 1 * 0x3C,
2520 cb_color_info);
2521 i++;
2522 }
2523 for (; i < 8 ; i++)
2524 if (sctx->framebuffer.dirty_cbufs & (1 << i))
2525 radeon_set_context_reg(cs, R_028C70_CB_COLOR0_INFO + i * 0x3C, 0);
2526
2527 /* ZS buffer. */
2528 if (state->zsbuf && sctx->framebuffer.dirty_zsbuf) {
2529 struct r600_surface *zb = (struct r600_surface*)state->zsbuf;
2530 struct r600_texture *rtex = (struct r600_texture*)zb->base.texture;
2531
2532 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
2533 &rtex->resource, RADEON_USAGE_READWRITE,
2534 zb->base.texture->nr_samples > 1 ?
2535 RADEON_PRIO_DEPTH_BUFFER_MSAA :
2536 RADEON_PRIO_DEPTH_BUFFER);
2537
2538 if (zb->db_htile_data_base) {
2539 radeon_add_to_buffer_list(&sctx->b, &sctx->b.gfx,
2540 rtex->htile_buffer, RADEON_USAGE_READWRITE,
2541 RADEON_PRIO_HTILE);
2542 }
2543
2544 radeon_set_context_reg(cs, R_028008_DB_DEPTH_VIEW, zb->db_depth_view);
2545 radeon_set_context_reg(cs, R_028014_DB_HTILE_DATA_BASE, zb->db_htile_data_base);
2546
2547 radeon_set_context_reg_seq(cs, R_02803C_DB_DEPTH_INFO, 9);
2548 radeon_emit(cs, zb->db_depth_info); /* R_02803C_DB_DEPTH_INFO */
2549 radeon_emit(cs, zb->db_z_info | /* R_028040_DB_Z_INFO */
2550 S_028040_ZRANGE_PRECISION(rtex->depth_clear_value != 0));
2551 radeon_emit(cs, zb->db_stencil_info); /* R_028044_DB_STENCIL_INFO */
2552 radeon_emit(cs, zb->db_depth_base); /* R_028048_DB_Z_READ_BASE */
2553 radeon_emit(cs, zb->db_stencil_base); /* R_02804C_DB_STENCIL_READ_BASE */
2554 radeon_emit(cs, zb->db_depth_base); /* R_028050_DB_Z_WRITE_BASE */
2555 radeon_emit(cs, zb->db_stencil_base); /* R_028054_DB_STENCIL_WRITE_BASE */
2556 radeon_emit(cs, zb->db_depth_size); /* R_028058_DB_DEPTH_SIZE */
2557 radeon_emit(cs, zb->db_depth_slice); /* R_02805C_DB_DEPTH_SLICE */
2558
2559 radeon_set_context_reg_seq(cs, R_028028_DB_STENCIL_CLEAR, 2);
2560 radeon_emit(cs, rtex->stencil_clear_value); /* R_028028_DB_STENCIL_CLEAR */
2561 radeon_emit(cs, fui(rtex->depth_clear_value)); /* R_02802C_DB_DEPTH_CLEAR */
2562
2563 radeon_set_context_reg(cs, R_028ABC_DB_HTILE_SURFACE, zb->db_htile_surface);
2564 } else if (sctx->framebuffer.dirty_zsbuf) {
2565 radeon_set_context_reg_seq(cs, R_028040_DB_Z_INFO, 2);
2566 radeon_emit(cs, S_028040_FORMAT(V_028040_Z_INVALID)); /* R_028040_DB_Z_INFO */
2567 radeon_emit(cs, S_028044_FORMAT(V_028044_STENCIL_INVALID)); /* R_028044_DB_STENCIL_INFO */
2568 }
2569
2570 /* Framebuffer dimensions. */
2571 /* PA_SC_WINDOW_SCISSOR_TL is set in si_init_config() */
2572 radeon_set_context_reg(cs, R_028208_PA_SC_WINDOW_SCISSOR_BR,
2573 S_028208_BR_X(state->width) | S_028208_BR_Y(state->height));
2574
2575 sctx->framebuffer.dirty_cbufs = 0;
2576 sctx->framebuffer.dirty_zsbuf = false;
2577 }
2578
2579 static void si_emit_msaa_sample_locs(struct si_context *sctx,
2580 struct r600_atom *atom)
2581 {
2582 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
2583 unsigned nr_samples = sctx->framebuffer.nr_samples;
2584
2585 /* Smoothing (only possible with nr_samples == 1) uses the same
2586 * sample locations as the MSAA it simulates.
2587 */
2588 if (nr_samples <= 1 && sctx->smoothing_enabled)
2589 nr_samples = SI_NUM_SMOOTH_AA_SAMPLES;
2590
2591 /* On Polaris, the small primitive filter uses the sample locations
2592 * even when MSAA is off, so we need to make sure they're set to 0.
2593 */
2594 if ((nr_samples > 1 || sctx->b.family >= CHIP_POLARIS10) &&
2595 (nr_samples != sctx->msaa_sample_locs.nr_samples)) {
2596 sctx->msaa_sample_locs.nr_samples = nr_samples;
2597 cayman_emit_msaa_sample_locs(cs, nr_samples);
2598 }
2599
2600 if (sctx->b.family >= CHIP_POLARIS10) {
2601 struct si_state_rasterizer *rs = sctx->queued.named.rasterizer;
2602 unsigned small_prim_filter_cntl =
2603 S_028830_SMALL_PRIM_FILTER_ENABLE(1) |
2604 S_028830_LINE_FILTER_DISABLE(1); /* line bug */
2605
2606 /* The alternative of setting sample locations to 0 would
2607 * require a DB flush to avoid Z errors, see
2608 * https://bugs.freedesktop.org/show_bug.cgi?id=96908
2609 */
2610 if (sctx->framebuffer.nr_samples > 1 && rs && !rs->multisample_enable)
2611 small_prim_filter_cntl &= C_028830_SMALL_PRIM_FILTER_ENABLE;
2612
2613 radeon_set_context_reg(cs, R_028830_PA_SU_SMALL_PRIM_FILTER_CNTL,
2614 small_prim_filter_cntl);
2615 }
2616 }
2617
2618 static void si_emit_msaa_config(struct si_context *sctx, struct r600_atom *atom)
2619 {
2620 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
2621 unsigned num_tile_pipes = sctx->screen->b.info.num_tile_pipes;
2622 /* 33% faster rendering to linear color buffers */
2623 bool dst_is_linear = sctx->framebuffer.any_dst_linear;
2624 unsigned sc_mode_cntl_1 =
2625 S_028A4C_WALK_SIZE(dst_is_linear) |
2626 S_028A4C_WALK_FENCE_ENABLE(!dst_is_linear) |
2627 S_028A4C_WALK_FENCE_SIZE(num_tile_pipes == 2 ? 2 : 3) |
2628 /* always 1: */
2629 S_028A4C_WALK_ALIGN8_PRIM_FITS_ST(1) |
2630 S_028A4C_SUPERTILE_WALK_ORDER_ENABLE(1) |
2631 S_028A4C_TILE_WALK_ORDER_ENABLE(1) |
2632 S_028A4C_MULTI_SHADER_ENGINE_PRIM_DISCARD_ENABLE(1) |
2633 S_028A4C_FORCE_EOV_CNTDWN_ENABLE(1) |
2634 S_028A4C_FORCE_EOV_REZ_ENABLE(1);
2635
2636 cayman_emit_msaa_config(cs, sctx->framebuffer.nr_samples,
2637 sctx->ps_iter_samples,
2638 sctx->smoothing_enabled ? SI_NUM_SMOOTH_AA_SAMPLES : 0,
2639 sc_mode_cntl_1);
2640 }
2641
2642 static void si_set_min_samples(struct pipe_context *ctx, unsigned min_samples)
2643 {
2644 struct si_context *sctx = (struct si_context *)ctx;
2645
2646 if (sctx->ps_iter_samples == min_samples)
2647 return;
2648
2649 sctx->ps_iter_samples = min_samples;
2650 sctx->do_update_shaders = true;
2651
2652 if (sctx->framebuffer.nr_samples > 1)
2653 si_mark_atom_dirty(sctx, &sctx->msaa_config);
2654 }
2655
2656 /*
2657 * Samplers
2658 */
2659
2660 /**
2661 * Build the sampler view descriptor for a buffer texture.
2662 * @param state 256-bit descriptor; only the high 128 bits are filled in
2663 */
2664 void
2665 si_make_buffer_descriptor(struct si_screen *screen, struct r600_resource *buf,
2666 enum pipe_format format,
2667 unsigned first_element, unsigned last_element,
2668 uint32_t *state)
2669 {
2670 const struct util_format_description *desc;
2671 int first_non_void;
2672 uint64_t va;
2673 unsigned stride;
2674 unsigned num_records;
2675 unsigned num_format, data_format;
2676
2677 desc = util_format_description(format);
2678 first_non_void = util_format_get_first_non_void_channel(format);
2679 stride = desc->block.bits / 8;
2680 va = buf->gpu_address + first_element * stride;
2681 num_format = si_translate_buffer_numformat(&screen->b.b, desc, first_non_void);
2682 data_format = si_translate_buffer_dataformat(&screen->b.b, desc, first_non_void);
2683
2684 num_records = last_element + 1 - first_element;
2685 num_records = MIN2(num_records, buf->b.b.width0 / stride);
2686
2687 if (screen->b.chip_class >= VI)
2688 num_records *= stride;
2689
2690 state[4] = va;
2691 state[5] = S_008F04_BASE_ADDRESS_HI(va >> 32) |
2692 S_008F04_STRIDE(stride);
2693 state[6] = num_records;
2694 state[7] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
2695 S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
2696 S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
2697 S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3])) |
2698 S_008F0C_NUM_FORMAT(num_format) |
2699 S_008F0C_DATA_FORMAT(data_format);
2700 }
2701
2702 /**
2703 * Build the sampler view descriptor for a texture.
2704 */
2705 void
2706 si_make_texture_descriptor(struct si_screen *screen,
2707 struct r600_texture *tex,
2708 bool sampler,
2709 enum pipe_texture_target target,
2710 enum pipe_format pipe_format,
2711 const unsigned char state_swizzle[4],
2712 unsigned first_level, unsigned last_level,
2713 unsigned first_layer, unsigned last_layer,
2714 unsigned width, unsigned height, unsigned depth,
2715 uint32_t *state,
2716 uint32_t *fmask_state)
2717 {
2718 struct pipe_resource *res = &tex->resource.b.b;
2719 const struct util_format_description *desc;
2720 unsigned char swizzle[4];
2721 int first_non_void;
2722 unsigned num_format, data_format, type;
2723 uint64_t va;
2724
2725 desc = util_format_description(pipe_format);
2726
2727 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
2728 const unsigned char swizzle_xxxx[4] = {0, 0, 0, 0};
2729 const unsigned char swizzle_yyyy[4] = {1, 1, 1, 1};
2730
2731 switch (pipe_format) {
2732 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
2733 case PIPE_FORMAT_X24S8_UINT:
2734 case PIPE_FORMAT_X32_S8X24_UINT:
2735 case PIPE_FORMAT_X8Z24_UNORM:
2736 util_format_compose_swizzles(swizzle_yyyy, state_swizzle, swizzle);
2737 break;
2738 default:
2739 util_format_compose_swizzles(swizzle_xxxx, state_swizzle, swizzle);
2740 }
2741 } else {
2742 util_format_compose_swizzles(desc->swizzle, state_swizzle, swizzle);
2743 }
2744
2745 first_non_void = util_format_get_first_non_void_channel(pipe_format);
2746
2747 switch (pipe_format) {
2748 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
2749 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
2750 break;
2751 default:
2752 if (first_non_void < 0) {
2753 if (util_format_is_compressed(pipe_format)) {
2754 switch (pipe_format) {
2755 case PIPE_FORMAT_DXT1_SRGB:
2756 case PIPE_FORMAT_DXT1_SRGBA:
2757 case PIPE_FORMAT_DXT3_SRGBA:
2758 case PIPE_FORMAT_DXT5_SRGBA:
2759 case PIPE_FORMAT_BPTC_SRGBA:
2760 case PIPE_FORMAT_ETC2_SRGB8:
2761 case PIPE_FORMAT_ETC2_SRGB8A1:
2762 case PIPE_FORMAT_ETC2_SRGBA8:
2763 num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
2764 break;
2765 case PIPE_FORMAT_RGTC1_SNORM:
2766 case PIPE_FORMAT_LATC1_SNORM:
2767 case PIPE_FORMAT_RGTC2_SNORM:
2768 case PIPE_FORMAT_LATC2_SNORM:
2769 case PIPE_FORMAT_ETC2_R11_SNORM:
2770 case PIPE_FORMAT_ETC2_RG11_SNORM:
2771 /* implies float, so use SNORM/UNORM to determine
2772 whether data is signed or not */
2773 case PIPE_FORMAT_BPTC_RGB_FLOAT:
2774 num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
2775 break;
2776 default:
2777 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
2778 break;
2779 }
2780 } else if (desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
2781 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
2782 } else {
2783 num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
2784 }
2785 } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
2786 num_format = V_008F14_IMG_NUM_FORMAT_SRGB;
2787 } else {
2788 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
2789
2790 switch (desc->channel[first_non_void].type) {
2791 case UTIL_FORMAT_TYPE_FLOAT:
2792 num_format = V_008F14_IMG_NUM_FORMAT_FLOAT;
2793 break;
2794 case UTIL_FORMAT_TYPE_SIGNED:
2795 if (desc->channel[first_non_void].normalized)
2796 num_format = V_008F14_IMG_NUM_FORMAT_SNORM;
2797 else if (desc->channel[first_non_void].pure_integer)
2798 num_format = V_008F14_IMG_NUM_FORMAT_SINT;
2799 else
2800 num_format = V_008F14_IMG_NUM_FORMAT_SSCALED;
2801 break;
2802 case UTIL_FORMAT_TYPE_UNSIGNED:
2803 if (desc->channel[first_non_void].normalized)
2804 num_format = V_008F14_IMG_NUM_FORMAT_UNORM;
2805 else if (desc->channel[first_non_void].pure_integer)
2806 num_format = V_008F14_IMG_NUM_FORMAT_UINT;
2807 else
2808 num_format = V_008F14_IMG_NUM_FORMAT_USCALED;
2809 }
2810 }
2811 }
2812
2813 data_format = si_translate_texformat(&screen->b.b, pipe_format, desc, first_non_void);
2814 if (data_format == ~0) {
2815 data_format = 0;
2816 }
2817
2818 if (!sampler &&
2819 (res->target == PIPE_TEXTURE_CUBE ||
2820 res->target == PIPE_TEXTURE_CUBE_ARRAY ||
2821 res->target == PIPE_TEXTURE_3D)) {
2822 /* For the purpose of shader images, treat cube maps and 3D
2823 * textures as 2D arrays. For 3D textures, the address
2824 * calculations for mipmaps are different, so we rely on the
2825 * caller to effectively disable mipmaps.
2826 */
2827 type = V_008F1C_SQ_RSRC_IMG_2D_ARRAY;
2828
2829 assert(res->target != PIPE_TEXTURE_3D || (first_level == 0 && last_level == 0));
2830 } else {
2831 type = si_tex_dim(res->target, target, res->nr_samples);
2832 }
2833
2834 if (type == V_008F1C_SQ_RSRC_IMG_1D_ARRAY) {
2835 height = 1;
2836 depth = res->array_size;
2837 } else if (type == V_008F1C_SQ_RSRC_IMG_2D_ARRAY ||
2838 type == V_008F1C_SQ_RSRC_IMG_2D_MSAA_ARRAY) {
2839 if (sampler || res->target != PIPE_TEXTURE_3D)
2840 depth = res->array_size;
2841 } else if (type == V_008F1C_SQ_RSRC_IMG_CUBE)
2842 depth = res->array_size / 6;
2843
2844 state[0] = 0;
2845 state[1] = (S_008F14_DATA_FORMAT(data_format) |
2846 S_008F14_NUM_FORMAT(num_format));
2847 state[2] = (S_008F18_WIDTH(width - 1) |
2848 S_008F18_HEIGHT(height - 1));
2849 state[3] = (S_008F1C_DST_SEL_X(si_map_swizzle(swizzle[0])) |
2850 S_008F1C_DST_SEL_Y(si_map_swizzle(swizzle[1])) |
2851 S_008F1C_DST_SEL_Z(si_map_swizzle(swizzle[2])) |
2852 S_008F1C_DST_SEL_W(si_map_swizzle(swizzle[3])) |
2853 S_008F1C_BASE_LEVEL(res->nr_samples > 1 ?
2854 0 : first_level) |
2855 S_008F1C_LAST_LEVEL(res->nr_samples > 1 ?
2856 util_logbase2(res->nr_samples) :
2857 last_level) |
2858 S_008F1C_POW2_PAD(res->last_level > 0) |
2859 S_008F1C_TYPE(type));
2860 state[4] = S_008F20_DEPTH(depth - 1);
2861 state[5] = (S_008F24_BASE_ARRAY(first_layer) |
2862 S_008F24_LAST_ARRAY(last_layer));
2863 state[6] = 0;
2864 state[7] = 0;
2865
2866 if (tex->dcc_offset) {
2867 unsigned swap = r600_translate_colorswap(pipe_format, false);
2868
2869 state[6] = S_008F28_ALPHA_IS_ON_MSB(swap <= 1);
2870 } else {
2871 /* The last dword is unused by hw. The shader uses it to clear
2872 * bits in the first dword of sampler state.
2873 */
2874 if (screen->b.chip_class <= CIK && res->nr_samples <= 1) {
2875 if (first_level == last_level)
2876 state[7] = C_008F30_MAX_ANISO_RATIO;
2877 else
2878 state[7] = 0xffffffff;
2879 }
2880 }
2881
2882 /* Initialize the sampler view for FMASK. */
2883 if (tex->fmask.size) {
2884 uint32_t fmask_format;
2885
2886 va = tex->resource.gpu_address + tex->fmask.offset;
2887
2888 switch (res->nr_samples) {
2889 case 2:
2890 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S2_F2;
2891 break;
2892 case 4:
2893 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK8_S4_F4;
2894 break;
2895 case 8:
2896 fmask_format = V_008F14_IMG_DATA_FORMAT_FMASK32_S8_F8;
2897 break;
2898 default:
2899 assert(0);
2900 fmask_format = V_008F14_IMG_DATA_FORMAT_INVALID;
2901 }
2902
2903 fmask_state[0] = va >> 8;
2904 fmask_state[1] = S_008F14_BASE_ADDRESS_HI(va >> 40) |
2905 S_008F14_DATA_FORMAT(fmask_format) |
2906 S_008F14_NUM_FORMAT(V_008F14_IMG_NUM_FORMAT_UINT);
2907 fmask_state[2] = S_008F18_WIDTH(width - 1) |
2908 S_008F18_HEIGHT(height - 1);
2909 fmask_state[3] = S_008F1C_DST_SEL_X(V_008F1C_SQ_SEL_X) |
2910 S_008F1C_DST_SEL_Y(V_008F1C_SQ_SEL_X) |
2911 S_008F1C_DST_SEL_Z(V_008F1C_SQ_SEL_X) |
2912 S_008F1C_DST_SEL_W(V_008F1C_SQ_SEL_X) |
2913 S_008F1C_TILING_INDEX(tex->fmask.tile_mode_index) |
2914 S_008F1C_TYPE(si_tex_dim(res->target, target, 0));
2915 fmask_state[4] = S_008F20_DEPTH(depth - 1) |
2916 S_008F20_PITCH(tex->fmask.pitch_in_pixels - 1);
2917 fmask_state[5] = S_008F24_BASE_ARRAY(first_layer) |
2918 S_008F24_LAST_ARRAY(last_layer);
2919 fmask_state[6] = 0;
2920 fmask_state[7] = 0;
2921 }
2922 }
2923
2924 /**
2925 * Create a sampler view.
2926 *
2927 * @param ctx context
2928 * @param texture texture
2929 * @param state sampler view template
2930 * @param width0 width0 override (for compressed textures as int)
2931 * @param height0 height0 override (for compressed textures as int)
2932 * @param force_level set the base address to the level (for compressed textures)
2933 */
2934 struct pipe_sampler_view *
2935 si_create_sampler_view_custom(struct pipe_context *ctx,
2936 struct pipe_resource *texture,
2937 const struct pipe_sampler_view *state,
2938 unsigned width0, unsigned height0,
2939 unsigned force_level)
2940 {
2941 struct si_context *sctx = (struct si_context*)ctx;
2942 struct si_sampler_view *view = CALLOC_STRUCT(si_sampler_view);
2943 struct r600_texture *tmp = (struct r600_texture*)texture;
2944 unsigned base_level, first_level, last_level;
2945 unsigned char state_swizzle[4];
2946 unsigned height, depth, width;
2947 unsigned last_layer = state->u.tex.last_layer;
2948 enum pipe_format pipe_format;
2949 const struct radeon_surf_level *surflevel;
2950
2951 if (!view)
2952 return NULL;
2953
2954 /* initialize base object */
2955 view->base = *state;
2956 view->base.texture = NULL;
2957 view->base.reference.count = 1;
2958 view->base.context = ctx;
2959
2960 /* NULL resource, obey swizzle (only ZERO and ONE make sense). */
2961 if (!texture) {
2962 view->state[3] = S_008F1C_DST_SEL_X(si_map_swizzle(state->swizzle_r)) |
2963 S_008F1C_DST_SEL_Y(si_map_swizzle(state->swizzle_g)) |
2964 S_008F1C_DST_SEL_Z(si_map_swizzle(state->swizzle_b)) |
2965 S_008F1C_DST_SEL_W(si_map_swizzle(state->swizzle_a)) |
2966 S_008F1C_TYPE(V_008F1C_SQ_RSRC_IMG_1D);
2967 return &view->base;
2968 }
2969
2970 pipe_resource_reference(&view->base.texture, texture);
2971
2972 if (state->format == PIPE_FORMAT_X24S8_UINT ||
2973 state->format == PIPE_FORMAT_S8X24_UINT ||
2974 state->format == PIPE_FORMAT_X32_S8X24_UINT ||
2975 state->format == PIPE_FORMAT_S8_UINT)
2976 view->is_stencil_sampler = true;
2977
2978 /* Buffer resource. */
2979 if (texture->target == PIPE_BUFFER) {
2980 si_make_buffer_descriptor(sctx->screen,
2981 (struct r600_resource *)texture,
2982 state->format,
2983 state->u.buf.first_element,
2984 state->u.buf.last_element,
2985 view->state);
2986
2987 LIST_ADDTAIL(&view->list, &sctx->b.texture_buffers);
2988 return &view->base;
2989 }
2990
2991 state_swizzle[0] = state->swizzle_r;
2992 state_swizzle[1] = state->swizzle_g;
2993 state_swizzle[2] = state->swizzle_b;
2994 state_swizzle[3] = state->swizzle_a;
2995
2996 base_level = 0;
2997 first_level = state->u.tex.first_level;
2998 last_level = state->u.tex.last_level;
2999 width = width0;
3000 height = height0;
3001 depth = texture->depth0;
3002
3003 if (force_level) {
3004 assert(force_level == first_level &&
3005 force_level == last_level);
3006 base_level = force_level;
3007 first_level = 0;
3008 last_level = 0;
3009 width = u_minify(width, force_level);
3010 height = u_minify(height, force_level);
3011 depth = u_minify(depth, force_level);
3012 }
3013
3014 /* This is not needed if state trackers set last_layer correctly. */
3015 if (state->target == PIPE_TEXTURE_1D ||
3016 state->target == PIPE_TEXTURE_2D ||
3017 state->target == PIPE_TEXTURE_RECT ||
3018 state->target == PIPE_TEXTURE_CUBE)
3019 last_layer = state->u.tex.first_layer;
3020
3021 /* Texturing with separate depth and stencil. */
3022 pipe_format = state->format;
3023
3024 /* Depth/stencil texturing sometimes needs separate texture. */
3025 if (tmp->is_depth && !r600_can_sample_zs(tmp, view->is_stencil_sampler)) {
3026 if (!tmp->flushed_depth_texture &&
3027 !r600_init_flushed_depth_texture(ctx, texture, NULL)) {
3028 pipe_resource_reference(&view->base.texture, NULL);
3029 FREE(view);
3030 return NULL;
3031 }
3032
3033 assert(tmp->flushed_depth_texture);
3034
3035 /* Override format for the case where the flushed texture
3036 * contains only Z or only S.
3037 */
3038 if (tmp->flushed_depth_texture->resource.b.b.format != tmp->resource.b.b.format)
3039 pipe_format = tmp->flushed_depth_texture->resource.b.b.format;
3040
3041 tmp = tmp->flushed_depth_texture;
3042 }
3043
3044 surflevel = tmp->surface.level;
3045
3046 if (tmp->db_compatible) {
3047 switch (pipe_format) {
3048 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
3049 pipe_format = PIPE_FORMAT_Z32_FLOAT;
3050 break;
3051 case PIPE_FORMAT_X8Z24_UNORM:
3052 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
3053 /* Z24 is always stored like this for DB
3054 * compatibility.
3055 */
3056 pipe_format = PIPE_FORMAT_Z24X8_UNORM;
3057 break;
3058 case PIPE_FORMAT_X24S8_UINT:
3059 case PIPE_FORMAT_S8X24_UINT:
3060 case PIPE_FORMAT_X32_S8X24_UINT:
3061 pipe_format = PIPE_FORMAT_S8_UINT;
3062 surflevel = tmp->surface.stencil_level;
3063 break;
3064 default:;
3065 }
3066 }
3067
3068 si_make_texture_descriptor(sctx->screen, tmp, true,
3069 state->target, pipe_format, state_swizzle,
3070 first_level, last_level,
3071 state->u.tex.first_layer, last_layer,
3072 width, height, depth,
3073 view->state, view->fmask_state);
3074
3075 view->base_level_info = &surflevel[base_level];
3076 view->base_level = base_level;
3077 view->block_width = util_format_get_blockwidth(pipe_format);
3078 return &view->base;
3079 }
3080
3081 static struct pipe_sampler_view *
3082 si_create_sampler_view(struct pipe_context *ctx,
3083 struct pipe_resource *texture,
3084 const struct pipe_sampler_view *state)
3085 {
3086 return si_create_sampler_view_custom(ctx, texture, state,
3087 texture ? texture->width0 : 0,
3088 texture ? texture->height0 : 0, 0);
3089 }
3090
3091 static void si_sampler_view_destroy(struct pipe_context *ctx,
3092 struct pipe_sampler_view *state)
3093 {
3094 struct si_sampler_view *view = (struct si_sampler_view *)state;
3095
3096 if (state->texture && state->texture->target == PIPE_BUFFER)
3097 LIST_DELINIT(&view->list);
3098
3099 pipe_resource_reference(&state->texture, NULL);
3100 FREE(view);
3101 }
3102
3103 static bool wrap_mode_uses_border_color(unsigned wrap, bool linear_filter)
3104 {
3105 return wrap == PIPE_TEX_WRAP_CLAMP_TO_BORDER ||
3106 wrap == PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER ||
3107 (linear_filter &&
3108 (wrap == PIPE_TEX_WRAP_CLAMP ||
3109 wrap == PIPE_TEX_WRAP_MIRROR_CLAMP));
3110 }
3111
3112 static bool sampler_state_needs_border_color(const struct pipe_sampler_state *state)
3113 {
3114 bool linear_filter = state->min_img_filter != PIPE_TEX_FILTER_NEAREST ||
3115 state->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
3116
3117 return (state->border_color.ui[0] || state->border_color.ui[1] ||
3118 state->border_color.ui[2] || state->border_color.ui[3]) &&
3119 (wrap_mode_uses_border_color(state->wrap_s, linear_filter) ||
3120 wrap_mode_uses_border_color(state->wrap_t, linear_filter) ||
3121 wrap_mode_uses_border_color(state->wrap_r, linear_filter));
3122 }
3123
3124 static void *si_create_sampler_state(struct pipe_context *ctx,
3125 const struct pipe_sampler_state *state)
3126 {
3127 struct si_context *sctx = (struct si_context *)ctx;
3128 struct r600_common_screen *rscreen = sctx->b.screen;
3129 struct si_sampler_state *rstate = CALLOC_STRUCT(si_sampler_state);
3130 unsigned border_color_type, border_color_index = 0;
3131 unsigned max_aniso = rscreen->force_aniso >= 0 ? rscreen->force_aniso
3132 : state->max_anisotropy;
3133 unsigned max_aniso_ratio = r600_tex_aniso_filter(max_aniso);
3134
3135 if (!rstate) {
3136 return NULL;
3137 }
3138
3139 if (!sampler_state_needs_border_color(state))
3140 border_color_type = V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
3141 else if (state->border_color.f[0] == 0 &&
3142 state->border_color.f[1] == 0 &&
3143 state->border_color.f[2] == 0 &&
3144 state->border_color.f[3] == 0)
3145 border_color_type = V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
3146 else if (state->border_color.f[0] == 0 &&
3147 state->border_color.f[1] == 0 &&
3148 state->border_color.f[2] == 0 &&
3149 state->border_color.f[3] == 1)
3150 border_color_type = V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_BLACK;
3151 else if (state->border_color.f[0] == 1 &&
3152 state->border_color.f[1] == 1 &&
3153 state->border_color.f[2] == 1 &&
3154 state->border_color.f[3] == 1)
3155 border_color_type = V_008F3C_SQ_TEX_BORDER_COLOR_OPAQUE_WHITE;
3156 else {
3157 int i;
3158
3159 border_color_type = V_008F3C_SQ_TEX_BORDER_COLOR_REGISTER;
3160
3161 /* Check if the border has been uploaded already. */
3162 for (i = 0; i < sctx->border_color_count; i++)
3163 if (memcmp(&sctx->border_color_table[i], &state->border_color,
3164 sizeof(state->border_color)) == 0)
3165 break;
3166
3167 if (i >= SI_MAX_BORDER_COLORS) {
3168 /* Getting 4096 unique border colors is very unlikely. */
3169 fprintf(stderr, "radeonsi: The border color table is full. "
3170 "Any new border colors will be just black. "
3171 "Please file a bug.\n");
3172 border_color_type = V_008F3C_SQ_TEX_BORDER_COLOR_TRANS_BLACK;
3173 } else {
3174 if (i == sctx->border_color_count) {
3175 /* Upload a new border color. */
3176 memcpy(&sctx->border_color_table[i], &state->border_color,
3177 sizeof(state->border_color));
3178 util_memcpy_cpu_to_le32(&sctx->border_color_map[i],
3179 &state->border_color,
3180 sizeof(state->border_color));
3181 sctx->border_color_count++;
3182 }
3183
3184 border_color_index = i;
3185 }
3186 }
3187
3188 rstate->val[0] = (S_008F30_CLAMP_X(si_tex_wrap(state->wrap_s)) |
3189 S_008F30_CLAMP_Y(si_tex_wrap(state->wrap_t)) |
3190 S_008F30_CLAMP_Z(si_tex_wrap(state->wrap_r)) |
3191 S_008F30_MAX_ANISO_RATIO(max_aniso_ratio) |
3192 S_008F30_DEPTH_COMPARE_FUNC(si_tex_compare(state->compare_func)) |
3193 S_008F30_FORCE_UNNORMALIZED(!state->normalized_coords) |
3194 S_008F30_DISABLE_CUBE_WRAP(!state->seamless_cube_map) |
3195 S_008F30_COMPAT_MODE(sctx->b.chip_class >= VI));
3196 rstate->val[1] = (S_008F34_MIN_LOD(S_FIXED(CLAMP(state->min_lod, 0, 15), 8)) |
3197 S_008F34_MAX_LOD(S_FIXED(CLAMP(state->max_lod, 0, 15), 8)));
3198 rstate->val[2] = (S_008F38_LOD_BIAS(S_FIXED(CLAMP(state->lod_bias, -16, 16), 8)) |
3199 S_008F38_XY_MAG_FILTER(eg_tex_filter(state->mag_img_filter, max_aniso)) |
3200 S_008F38_XY_MIN_FILTER(eg_tex_filter(state->min_img_filter, max_aniso)) |
3201 S_008F38_MIP_FILTER(si_tex_mipfilter(state->min_mip_filter)) |
3202 S_008F38_MIP_POINT_PRECLAMP(1) |
3203 S_008F38_DISABLE_LSB_CEIL(1) |
3204 S_008F38_FILTER_PREC_FIX(1) |
3205 S_008F38_ANISO_OVERRIDE(sctx->b.chip_class >= VI));
3206 rstate->val[3] = S_008F3C_BORDER_COLOR_PTR(border_color_index) |
3207 S_008F3C_BORDER_COLOR_TYPE(border_color_type);
3208 return rstate;
3209 }
3210
3211 static void si_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
3212 {
3213 struct si_context *sctx = (struct si_context *)ctx;
3214
3215 if (sctx->sample_mask.sample_mask == (uint16_t)sample_mask)
3216 return;
3217
3218 sctx->sample_mask.sample_mask = sample_mask;
3219 si_mark_atom_dirty(sctx, &sctx->sample_mask.atom);
3220 }
3221
3222 static void si_emit_sample_mask(struct si_context *sctx, struct r600_atom *atom)
3223 {
3224 struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
3225 unsigned mask = sctx->sample_mask.sample_mask;
3226
3227 /* Needed for line and polygon smoothing as well as for the Polaris
3228 * small primitive filter. We expect the state tracker to take care of
3229 * this for us.
3230 */
3231 assert(mask == 0xffff || sctx->framebuffer.nr_samples > 1 ||
3232 (mask & 1 && sctx->blitter->running));
3233
3234 radeon_set_context_reg_seq(cs, R_028C38_PA_SC_AA_MASK_X0Y0_X1Y0, 2);
3235 radeon_emit(cs, mask | (mask << 16));
3236 radeon_emit(cs, mask | (mask << 16));
3237 }
3238
3239 static void si_delete_sampler_state(struct pipe_context *ctx, void *state)
3240 {
3241 free(state);
3242 }
3243
3244 /*
3245 * Vertex elements & buffers
3246 */
3247
3248 static void *si_create_vertex_elements(struct pipe_context *ctx,
3249 unsigned count,
3250 const struct pipe_vertex_element *elements)
3251 {
3252 struct si_vertex_element *v = CALLOC_STRUCT(si_vertex_element);
3253 int i;
3254
3255 assert(count <= SI_MAX_ATTRIBS);
3256 if (!v)
3257 return NULL;
3258
3259 v->count = count;
3260 for (i = 0; i < count; ++i) {
3261 const struct util_format_description *desc;
3262 unsigned data_format, num_format;
3263 int first_non_void;
3264
3265 desc = util_format_description(elements[i].src_format);
3266 first_non_void = util_format_get_first_non_void_channel(elements[i].src_format);
3267 data_format = si_translate_buffer_dataformat(ctx->screen, desc, first_non_void);
3268 num_format = si_translate_buffer_numformat(ctx->screen, desc, first_non_void);
3269
3270 v->rsrc_word3[i] = S_008F0C_DST_SEL_X(si_map_swizzle(desc->swizzle[0])) |
3271 S_008F0C_DST_SEL_Y(si_map_swizzle(desc->swizzle[1])) |
3272 S_008F0C_DST_SEL_Z(si_map_swizzle(desc->swizzle[2])) |
3273 S_008F0C_DST_SEL_W(si_map_swizzle(desc->swizzle[3])) |
3274 S_008F0C_NUM_FORMAT(num_format) |
3275 S_008F0C_DATA_FORMAT(data_format);
3276 v->format_size[i] = desc->block.bits / 8;
3277 }
3278 memcpy(v->elements, elements, sizeof(struct pipe_vertex_element) * count);
3279
3280 return v;
3281 }
3282
3283 static void si_bind_vertex_elements(struct pipe_context *ctx, void *state)
3284 {
3285 struct si_context *sctx = (struct si_context *)ctx;
3286 struct si_vertex_element *v = (struct si_vertex_element*)state;
3287
3288 sctx->vertex_elements = v;
3289 sctx->vertex_buffers_dirty = true;
3290 sctx->do_update_shaders = true;
3291 }
3292
3293 static void si_delete_vertex_element(struct pipe_context *ctx, void *state)
3294 {
3295 struct si_context *sctx = (struct si_context *)ctx;
3296
3297 if (sctx->vertex_elements == state)
3298 sctx->vertex_elements = NULL;
3299 FREE(state);
3300 }
3301
3302 static void si_set_vertex_buffers(struct pipe_context *ctx,
3303 unsigned start_slot, unsigned count,
3304 const struct pipe_vertex_buffer *buffers)
3305 {
3306 struct si_context *sctx = (struct si_context *)ctx;
3307 struct pipe_vertex_buffer *dst = sctx->vertex_buffer + start_slot;
3308 int i;
3309
3310 assert(start_slot + count <= ARRAY_SIZE(sctx->vertex_buffer));
3311
3312 if (buffers) {
3313 for (i = 0; i < count; i++) {
3314 const struct pipe_vertex_buffer *src = buffers + i;
3315 struct pipe_vertex_buffer *dsti = dst + i;
3316
3317 pipe_resource_reference(&dsti->buffer, src->buffer);
3318 dsti->buffer_offset = src->buffer_offset;
3319 dsti->stride = src->stride;
3320 r600_context_add_resource_size(ctx, src->buffer);
3321 }
3322 } else {
3323 for (i = 0; i < count; i++) {
3324 pipe_resource_reference(&dst[i].buffer, NULL);
3325 }
3326 }
3327 sctx->vertex_buffers_dirty = true;
3328 }
3329
3330 static void si_set_index_buffer(struct pipe_context *ctx,
3331 const struct pipe_index_buffer *ib)
3332 {
3333 struct si_context *sctx = (struct si_context *)ctx;
3334
3335 if (ib) {
3336 pipe_resource_reference(&sctx->index_buffer.buffer, ib->buffer);
3337 memcpy(&sctx->index_buffer, ib, sizeof(*ib));
3338 r600_context_add_resource_size(ctx, ib->buffer);
3339 } else {
3340 pipe_resource_reference(&sctx->index_buffer.buffer, NULL);
3341 }
3342 }
3343
3344 /*
3345 * Misc
3346 */
3347
3348 static void si_set_tess_state(struct pipe_context *ctx,
3349 const float default_outer_level[4],
3350 const float default_inner_level[2])
3351 {
3352 struct si_context *sctx = (struct si_context *)ctx;
3353 struct pipe_constant_buffer cb;
3354 float array[8];
3355
3356 memcpy(array, default_outer_level, sizeof(float) * 4);
3357 memcpy(array+4, default_inner_level, sizeof(float) * 2);
3358
3359 cb.buffer = NULL;
3360 cb.user_buffer = NULL;
3361 cb.buffer_size = sizeof(array);
3362
3363 si_upload_const_buffer(sctx, (struct r600_resource**)&cb.buffer,
3364 (void*)array, sizeof(array),
3365 &cb.buffer_offset);
3366
3367 si_set_rw_buffer(sctx, SI_HS_CONST_DEFAULT_TESS_LEVELS, &cb);
3368 pipe_resource_reference(&cb.buffer, NULL);
3369 }
3370
3371 static void si_texture_barrier(struct pipe_context *ctx)
3372 {
3373 struct si_context *sctx = (struct si_context *)ctx;
3374
3375 sctx->b.flags |= SI_CONTEXT_INV_VMEM_L1 |
3376 SI_CONTEXT_INV_GLOBAL_L2 |
3377 SI_CONTEXT_FLUSH_AND_INV_CB |
3378 SI_CONTEXT_CS_PARTIAL_FLUSH;
3379 }
3380
3381 static void si_memory_barrier(struct pipe_context *ctx, unsigned flags)
3382 {
3383 struct si_context *sctx = (struct si_context *)ctx;
3384
3385 /* Subsequent commands must wait for all shader invocations to
3386 * complete. */
3387 sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
3388 SI_CONTEXT_CS_PARTIAL_FLUSH;
3389
3390 if (flags & PIPE_BARRIER_CONSTANT_BUFFER)
3391 sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1 |
3392 SI_CONTEXT_INV_VMEM_L1;
3393
3394 if (flags & (PIPE_BARRIER_VERTEX_BUFFER |
3395 PIPE_BARRIER_SHADER_BUFFER |
3396 PIPE_BARRIER_TEXTURE |
3397 PIPE_BARRIER_IMAGE |
3398 PIPE_BARRIER_STREAMOUT_BUFFER |
3399 PIPE_BARRIER_GLOBAL_BUFFER)) {
3400 /* As far as I can tell, L1 contents are written back to L2
3401 * automatically at end of shader, but the contents of other
3402 * L1 caches might still be stale. */
3403 sctx->b.flags |= SI_CONTEXT_INV_VMEM_L1;
3404 }
3405
3406 if (flags & PIPE_BARRIER_INDEX_BUFFER) {
3407 sctx->b.flags |= SI_CONTEXT_INV_VMEM_L1;
3408
3409 /* Indices are read through TC L2 since VI. */
3410 if (sctx->screen->b.chip_class <= CIK)
3411 sctx->b.flags |= SI_CONTEXT_INV_GLOBAL_L2;
3412 }
3413
3414 if (flags & PIPE_BARRIER_FRAMEBUFFER)
3415 sctx->b.flags |= SI_CONTEXT_FLUSH_AND_INV_FRAMEBUFFER;
3416
3417 if (flags & (PIPE_BARRIER_MAPPED_BUFFER |
3418 PIPE_BARRIER_FRAMEBUFFER |
3419 PIPE_BARRIER_INDIRECT_BUFFER)) {
3420 /* Not sure if INV_GLOBAL_L2 is the best thing here.
3421 *
3422 * We need to make sure that TC L1 & L2 are written back to
3423 * memory, because neither CPU accesses nor CB fetches consider
3424 * TC, but there's no need to invalidate any TC cache lines. */
3425 sctx->b.flags |= SI_CONTEXT_INV_GLOBAL_L2;
3426 }
3427 }
3428
3429 static void *si_create_blend_custom(struct si_context *sctx, unsigned mode)
3430 {
3431 struct pipe_blend_state blend;
3432
3433 memset(&blend, 0, sizeof(blend));
3434 blend.independent_blend_enable = true;
3435 blend.rt[0].colormask = 0xf;
3436 return si_create_blend_state_mode(&sctx->b.b, &blend, mode);
3437 }
3438
3439 static void si_need_gfx_cs_space(struct pipe_context *ctx, unsigned num_dw,
3440 bool include_draw_vbo)
3441 {
3442 si_need_cs_space((struct si_context*)ctx);
3443 }
3444
3445 static void si_init_config(struct si_context *sctx);
3446
3447 void si_init_state_functions(struct si_context *sctx)
3448 {
3449 si_init_external_atom(sctx, &sctx->b.render_cond_atom, &sctx->atoms.s.render_cond);
3450 si_init_external_atom(sctx, &sctx->b.streamout.begin_atom, &sctx->atoms.s.streamout_begin);
3451 si_init_external_atom(sctx, &sctx->b.streamout.enable_atom, &sctx->atoms.s.streamout_enable);
3452 si_init_external_atom(sctx, &sctx->b.scissors.atom, &sctx->atoms.s.scissors);
3453 si_init_external_atom(sctx, &sctx->b.viewports.atom, &sctx->atoms.s.viewports);
3454
3455 si_init_atom(sctx, &sctx->cache_flush, &sctx->atoms.s.cache_flush, si_emit_cache_flush);
3456 si_init_atom(sctx, &sctx->framebuffer.atom, &sctx->atoms.s.framebuffer, si_emit_framebuffer_state);
3457 si_init_atom(sctx, &sctx->msaa_sample_locs.atom, &sctx->atoms.s.msaa_sample_locs, si_emit_msaa_sample_locs);
3458 si_init_atom(sctx, &sctx->db_render_state, &sctx->atoms.s.db_render_state, si_emit_db_render_state);
3459 si_init_atom(sctx, &sctx->msaa_config, &sctx->atoms.s.msaa_config, si_emit_msaa_config);
3460 si_init_atom(sctx, &sctx->sample_mask.atom, &sctx->atoms.s.sample_mask, si_emit_sample_mask);
3461 si_init_atom(sctx, &sctx->cb_render_state, &sctx->atoms.s.cb_render_state, si_emit_cb_render_state);
3462 si_init_atom(sctx, &sctx->blend_color.atom, &sctx->atoms.s.blend_color, si_emit_blend_color);
3463 si_init_atom(sctx, &sctx->clip_regs, &sctx->atoms.s.clip_regs, si_emit_clip_regs);
3464 si_init_atom(sctx, &sctx->clip_state.atom, &sctx->atoms.s.clip_state, si_emit_clip_state);
3465 si_init_atom(sctx, &sctx->stencil_ref.atom, &sctx->atoms.s.stencil_ref, si_emit_stencil_ref);
3466
3467 sctx->b.b.create_blend_state = si_create_blend_state;
3468 sctx->b.b.bind_blend_state = si_bind_blend_state;
3469 sctx->b.b.delete_blend_state = si_delete_blend_state;
3470 sctx->b.b.set_blend_color = si_set_blend_color;
3471
3472 sctx->b.b.create_rasterizer_state = si_create_rs_state;
3473 sctx->b.b.bind_rasterizer_state = si_bind_rs_state;
3474 sctx->b.b.delete_rasterizer_state = si_delete_rs_state;
3475
3476 sctx->b.b.create_depth_stencil_alpha_state = si_create_dsa_state;
3477 sctx->b.b.bind_depth_stencil_alpha_state = si_bind_dsa_state;
3478 sctx->b.b.delete_depth_stencil_alpha_state = si_delete_dsa_state;
3479
3480 sctx->custom_dsa_flush = si_create_db_flush_dsa(sctx);
3481 sctx->custom_blend_resolve = si_create_blend_custom(sctx, V_028808_CB_RESOLVE);
3482 sctx->custom_blend_decompress = si_create_blend_custom(sctx, V_028808_CB_FMASK_DECOMPRESS);
3483 sctx->custom_blend_fastclear = si_create_blend_custom(sctx, V_028808_CB_ELIMINATE_FAST_CLEAR);
3484 sctx->custom_blend_dcc_decompress = si_create_blend_custom(sctx, V_028808_CB_DCC_DECOMPRESS);
3485
3486 sctx->b.b.set_clip_state = si_set_clip_state;
3487 sctx->b.b.set_stencil_ref = si_set_stencil_ref;
3488
3489 sctx->b.b.set_framebuffer_state = si_set_framebuffer_state;
3490 sctx->b.b.get_sample_position = cayman_get_sample_position;
3491
3492 sctx->b.b.create_sampler_state = si_create_sampler_state;
3493 sctx->b.b.delete_sampler_state = si_delete_sampler_state;
3494
3495 sctx->b.b.create_sampler_view = si_create_sampler_view;
3496 sctx->b.b.sampler_view_destroy = si_sampler_view_destroy;
3497
3498 sctx->b.b.set_sample_mask = si_set_sample_mask;
3499
3500 sctx->b.b.create_vertex_elements_state = si_create_vertex_elements;
3501 sctx->b.b.bind_vertex_elements_state = si_bind_vertex_elements;
3502 sctx->b.b.delete_vertex_elements_state = si_delete_vertex_element;
3503 sctx->b.b.set_vertex_buffers = si_set_vertex_buffers;
3504 sctx->b.b.set_index_buffer = si_set_index_buffer;
3505
3506 sctx->b.b.texture_barrier = si_texture_barrier;
3507 sctx->b.b.memory_barrier = si_memory_barrier;
3508 sctx->b.b.set_min_samples = si_set_min_samples;
3509 sctx->b.b.set_tess_state = si_set_tess_state;
3510
3511 sctx->b.b.set_active_query_state = si_set_active_query_state;
3512 sctx->b.set_occlusion_query_state = si_set_occlusion_query_state;
3513 sctx->b.need_gfx_cs_space = si_need_gfx_cs_space;
3514
3515 sctx->b.b.draw_vbo = si_draw_vbo;
3516
3517 si_init_config(sctx);
3518 }
3519
3520 static uint32_t si_get_bo_metadata_word1(struct r600_common_screen *rscreen)
3521 {
3522 return (ATI_VENDOR_ID << 16) | rscreen->info.pci_id;
3523 }
3524
3525 static void si_query_opaque_metadata(struct r600_common_screen *rscreen,
3526 struct r600_texture *rtex,
3527 struct radeon_bo_metadata *md)
3528 {
3529 struct si_screen *sscreen = (struct si_screen*)rscreen;
3530 struct pipe_resource *res = &rtex->resource.b.b;
3531 static const unsigned char swizzle[] = {
3532 PIPE_SWIZZLE_X,
3533 PIPE_SWIZZLE_Y,
3534 PIPE_SWIZZLE_Z,
3535 PIPE_SWIZZLE_W
3536 };
3537 uint32_t desc[8], i;
3538 bool is_array = util_resource_is_array_texture(res);
3539
3540 /* DRM 2.x.x doesn't support this. */
3541 if (rscreen->info.drm_major != 3)
3542 return;
3543
3544 assert(rtex->dcc_separate_buffer == NULL);
3545 assert(rtex->fmask.size == 0);
3546
3547 /* Metadata image format format version 1:
3548 * [0] = 1 (metadata format identifier)
3549 * [1] = (VENDOR_ID << 16) | PCI_ID
3550 * [2:9] = image descriptor for the whole resource
3551 * [2] is always 0, because the base address is cleared
3552 * [9] is the DCC offset bits [39:8] from the beginning of
3553 * the buffer
3554 * [10:10+LAST_LEVEL] = mipmap level offset bits [39:8] for each level
3555 */
3556
3557 md->metadata[0] = 1; /* metadata image format version 1 */
3558
3559 /* TILE_MODE_INDEX is ambiguous without a PCI ID. */
3560 md->metadata[1] = si_get_bo_metadata_word1(rscreen);
3561
3562 si_make_texture_descriptor(sscreen, rtex, true,
3563 res->target, res->format,
3564 swizzle, 0, res->last_level, 0,
3565 is_array ? res->array_size - 1 : 0,
3566 res->width0, res->height0, res->depth0,
3567 desc, NULL);
3568
3569 si_set_mutable_tex_desc_fields(rtex, &rtex->surface.level[0], 0, 0,
3570 rtex->surface.blk_w, false, desc);
3571
3572 /* Clear the base address and set the relative DCC offset. */
3573 desc[0] = 0;
3574 desc[1] &= C_008F14_BASE_ADDRESS_HI;
3575 desc[7] = rtex->dcc_offset >> 8;
3576
3577 /* Dwords [2:9] contain the image descriptor. */
3578 memcpy(&md->metadata[2], desc, sizeof(desc));
3579
3580 /* Dwords [10:..] contain the mipmap level offsets. */
3581 for (i = 0; i <= res->last_level; i++)
3582 md->metadata[10+i] = rtex->surface.level[i].offset >> 8;
3583
3584 md->size_metadata = (11 + res->last_level) * 4;
3585 }
3586
3587 static void si_apply_opaque_metadata(struct r600_common_screen *rscreen,
3588 struct r600_texture *rtex,
3589 struct radeon_bo_metadata *md)
3590 {
3591 uint32_t *desc = &md->metadata[2];
3592
3593 if (rscreen->chip_class < VI)
3594 return;
3595
3596 /* Return if DCC is enabled. The texture should be set up with it
3597 * already.
3598 */
3599 if (md->size_metadata >= 11 * 4 &&
3600 md->metadata[0] != 0 &&
3601 md->metadata[1] == si_get_bo_metadata_word1(rscreen) &&
3602 G_008F28_COMPRESSION_EN(desc[6])) {
3603 assert(rtex->dcc_offset == ((uint64_t)desc[7] << 8));
3604 return;
3605 }
3606
3607 /* Disable DCC. These are always set by texture_from_handle and must
3608 * be cleared here.
3609 */
3610 rtex->dcc_offset = 0;
3611 }
3612
3613 void si_init_screen_state_functions(struct si_screen *sscreen)
3614 {
3615 sscreen->b.b.is_format_supported = si_is_format_supported;
3616 sscreen->b.query_opaque_metadata = si_query_opaque_metadata;
3617 sscreen->b.apply_opaque_metadata = si_apply_opaque_metadata;
3618 }
3619
3620 static void
3621 si_write_harvested_raster_configs(struct si_context *sctx,
3622 struct si_pm4_state *pm4,
3623 unsigned raster_config,
3624 unsigned raster_config_1)
3625 {
3626 unsigned sh_per_se = MAX2(sctx->screen->b.info.max_sh_per_se, 1);
3627 unsigned num_se = MAX2(sctx->screen->b.info.max_se, 1);
3628 unsigned rb_mask = sctx->screen->b.info.enabled_rb_mask;
3629 unsigned num_rb = MIN2(sctx->screen->b.info.num_render_backends, 16);
3630 unsigned rb_per_pkr = MIN2(num_rb / num_se / sh_per_se, 2);
3631 unsigned rb_per_se = num_rb / num_se;
3632 unsigned se_mask[4];
3633 unsigned se;
3634
3635 se_mask[0] = ((1 << rb_per_se) - 1);
3636 se_mask[1] = (se_mask[0] << rb_per_se);
3637 se_mask[2] = (se_mask[1] << rb_per_se);
3638 se_mask[3] = (se_mask[2] << rb_per_se);
3639
3640 se_mask[0] &= rb_mask;
3641 se_mask[1] &= rb_mask;
3642 se_mask[2] &= rb_mask;
3643 se_mask[3] &= rb_mask;
3644
3645 assert(num_se == 1 || num_se == 2 || num_se == 4);
3646 assert(sh_per_se == 1 || sh_per_se == 2);
3647 assert(rb_per_pkr == 1 || rb_per_pkr == 2);
3648
3649 /* XXX: I can't figure out what the *_XSEL and *_YSEL
3650 * fields are for, so I'm leaving them as their default
3651 * values. */
3652
3653 for (se = 0; se < num_se; se++) {
3654 unsigned raster_config_se = raster_config;
3655 unsigned pkr0_mask = ((1 << rb_per_pkr) - 1) << (se * rb_per_se);
3656 unsigned pkr1_mask = pkr0_mask << rb_per_pkr;
3657 int idx = (se / 2) * 2;
3658
3659 if ((num_se > 1) && (!se_mask[idx] || !se_mask[idx + 1])) {
3660 raster_config_se &= C_028350_SE_MAP;
3661
3662 if (!se_mask[idx]) {
3663 raster_config_se |=
3664 S_028350_SE_MAP(V_028350_RASTER_CONFIG_SE_MAP_3);
3665 } else {
3666 raster_config_se |=
3667 S_028350_SE_MAP(V_028350_RASTER_CONFIG_SE_MAP_0);
3668 }
3669 }
3670
3671 pkr0_mask &= rb_mask;
3672 pkr1_mask &= rb_mask;
3673 if (rb_per_se > 2 && (!pkr0_mask || !pkr1_mask)) {
3674 raster_config_se &= C_028350_PKR_MAP;
3675
3676 if (!pkr0_mask) {
3677 raster_config_se |=
3678 S_028350_PKR_MAP(V_028350_RASTER_CONFIG_PKR_MAP_3);
3679 } else {
3680 raster_config_se |=
3681 S_028350_PKR_MAP(V_028350_RASTER_CONFIG_PKR_MAP_0);
3682 }
3683 }
3684
3685 if (rb_per_se >= 2) {
3686 unsigned rb0_mask = 1 << (se * rb_per_se);
3687 unsigned rb1_mask = rb0_mask << 1;
3688
3689 rb0_mask &= rb_mask;
3690 rb1_mask &= rb_mask;
3691 if (!rb0_mask || !rb1_mask) {
3692 raster_config_se &= C_028350_RB_MAP_PKR0;
3693
3694 if (!rb0_mask) {
3695 raster_config_se |=
3696 S_028350_RB_MAP_PKR0(V_028350_RASTER_CONFIG_RB_MAP_3);
3697 } else {
3698 raster_config_se |=
3699 S_028350_RB_MAP_PKR0(V_028350_RASTER_CONFIG_RB_MAP_0);
3700 }
3701 }
3702
3703 if (rb_per_se > 2) {
3704 rb0_mask = 1 << (se * rb_per_se + rb_per_pkr);
3705 rb1_mask = rb0_mask << 1;
3706 rb0_mask &= rb_mask;
3707 rb1_mask &= rb_mask;
3708 if (!rb0_mask || !rb1_mask) {
3709 raster_config_se &= C_028350_RB_MAP_PKR1;
3710
3711 if (!rb0_mask) {
3712 raster_config_se |=
3713 S_028350_RB_MAP_PKR1(V_028350_RASTER_CONFIG_RB_MAP_3);
3714 } else {
3715 raster_config_se |=
3716 S_028350_RB_MAP_PKR1(V_028350_RASTER_CONFIG_RB_MAP_0);
3717 }
3718 }
3719 }
3720 }
3721
3722 /* GRBM_GFX_INDEX has a different offset on SI and CI+ */
3723 if (sctx->b.chip_class < CIK)
3724 si_pm4_set_reg(pm4, GRBM_GFX_INDEX,
3725 SE_INDEX(se) | SH_BROADCAST_WRITES |
3726 INSTANCE_BROADCAST_WRITES);
3727 else
3728 si_pm4_set_reg(pm4, R_030800_GRBM_GFX_INDEX,
3729 S_030800_SE_INDEX(se) | S_030800_SH_BROADCAST_WRITES(1) |
3730 S_030800_INSTANCE_BROADCAST_WRITES(1));
3731 si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG, raster_config_se);
3732 }
3733
3734 /* GRBM_GFX_INDEX has a different offset on SI and CI+ */
3735 if (sctx->b.chip_class < CIK)
3736 si_pm4_set_reg(pm4, GRBM_GFX_INDEX,
3737 SE_BROADCAST_WRITES | SH_BROADCAST_WRITES |
3738 INSTANCE_BROADCAST_WRITES);
3739 else {
3740 si_pm4_set_reg(pm4, R_030800_GRBM_GFX_INDEX,
3741 S_030800_SE_BROADCAST_WRITES(1) | S_030800_SH_BROADCAST_WRITES(1) |
3742 S_030800_INSTANCE_BROADCAST_WRITES(1));
3743
3744 if ((num_se > 2) && ((!se_mask[0] && !se_mask[1]) ||
3745 (!se_mask[2] && !se_mask[3]))) {
3746 raster_config_1 &= C_028354_SE_PAIR_MAP;
3747
3748 if (!se_mask[0] && !se_mask[1]) {
3749 raster_config_1 |=
3750 S_028354_SE_PAIR_MAP(V_028354_RASTER_CONFIG_SE_PAIR_MAP_3);
3751 } else {
3752 raster_config_1 |=
3753 S_028354_SE_PAIR_MAP(V_028354_RASTER_CONFIG_SE_PAIR_MAP_0);
3754 }
3755 }
3756
3757 si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1, raster_config_1);
3758 }
3759 }
3760
3761 static void si_init_config(struct si_context *sctx)
3762 {
3763 struct si_screen *sscreen = sctx->screen;
3764 unsigned num_rb = MIN2(sctx->screen->b.info.num_render_backends, 16);
3765 unsigned rb_mask = sctx->screen->b.info.enabled_rb_mask;
3766 unsigned raster_config, raster_config_1;
3767 uint64_t border_color_va = sctx->border_color_buffer->gpu_address;
3768 struct si_pm4_state *pm4 = CALLOC_STRUCT(si_pm4_state);
3769 int i;
3770
3771 if (!pm4)
3772 return;
3773
3774 si_pm4_cmd_begin(pm4, PKT3_CONTEXT_CONTROL);
3775 si_pm4_cmd_add(pm4, CONTEXT_CONTROL_LOAD_ENABLE(1));
3776 si_pm4_cmd_add(pm4, CONTEXT_CONTROL_SHADOW_ENABLE(1));
3777 si_pm4_cmd_end(pm4, false);
3778
3779 si_pm4_set_reg(pm4, R_028A18_VGT_HOS_MAX_TESS_LEVEL, fui(64));
3780 si_pm4_set_reg(pm4, R_028A1C_VGT_HOS_MIN_TESS_LEVEL, fui(0));
3781
3782 /* FIXME calculate these values somehow ??? */
3783 si_pm4_set_reg(pm4, R_028A54_VGT_GS_PER_ES, SI_GS_PER_ES);
3784 si_pm4_set_reg(pm4, R_028A58_VGT_ES_PER_GS, 0x40);
3785 si_pm4_set_reg(pm4, R_028A5C_VGT_GS_PER_VS, 0x2);
3786
3787 si_pm4_set_reg(pm4, R_028A8C_VGT_PRIMITIVEID_RESET, 0x0);
3788 si_pm4_set_reg(pm4, R_028B28_VGT_STRMOUT_DRAW_OPAQUE_OFFSET, 0);
3789
3790 si_pm4_set_reg(pm4, R_028B98_VGT_STRMOUT_BUFFER_CONFIG, 0x0);
3791 si_pm4_set_reg(pm4, R_028AB8_VGT_VTX_CNT_EN, 0x0);
3792 if (sctx->b.chip_class < CIK)
3793 si_pm4_set_reg(pm4, R_008A14_PA_CL_ENHANCE, S_008A14_NUM_CLIP_SEQ(3) |
3794 S_008A14_CLIP_VTX_REORDER_ENA(1));
3795
3796 si_pm4_set_reg(pm4, R_028BD4_PA_SC_CENTROID_PRIORITY_0, 0x76543210);
3797 si_pm4_set_reg(pm4, R_028BD8_PA_SC_CENTROID_PRIORITY_1, 0xfedcba98);
3798
3799 si_pm4_set_reg(pm4, R_02882C_PA_SU_PRIM_FILTER_CNTL, 0);
3800
3801 for (i = 0; i < 16; i++) {
3802 si_pm4_set_reg(pm4, R_0282D0_PA_SC_VPORT_ZMIN_0 + i*8, 0);
3803 si_pm4_set_reg(pm4, R_0282D4_PA_SC_VPORT_ZMAX_0 + i*8, fui(1.0));
3804 }
3805
3806 switch (sctx->screen->b.family) {
3807 case CHIP_TAHITI:
3808 case CHIP_PITCAIRN:
3809 raster_config = 0x2a00126a;
3810 raster_config_1 = 0x00000000;
3811 break;
3812 case CHIP_VERDE:
3813 raster_config = 0x0000124a;
3814 raster_config_1 = 0x00000000;
3815 break;
3816 case CHIP_OLAND:
3817 raster_config = 0x00000082;
3818 raster_config_1 = 0x00000000;
3819 break;
3820 case CHIP_HAINAN:
3821 raster_config = 0x00000000;
3822 raster_config_1 = 0x00000000;
3823 break;
3824 case CHIP_BONAIRE:
3825 raster_config = 0x16000012;
3826 raster_config_1 = 0x00000000;
3827 break;
3828 case CHIP_HAWAII:
3829 raster_config = 0x3a00161a;
3830 raster_config_1 = 0x0000002e;
3831 break;
3832 case CHIP_FIJI:
3833 if (sscreen->b.info.cik_macrotile_mode_array[0] == 0x000000e8) {
3834 /* old kernels with old tiling config */
3835 raster_config = 0x16000012;
3836 raster_config_1 = 0x0000002a;
3837 } else {
3838 raster_config = 0x3a00161a;
3839 raster_config_1 = 0x0000002e;
3840 }
3841 break;
3842 case CHIP_POLARIS10:
3843 raster_config = 0x16000012;
3844 raster_config_1 = 0x0000002a;
3845 break;
3846 case CHIP_POLARIS11:
3847 raster_config = 0x16000012;
3848 raster_config_1 = 0x00000000;
3849 break;
3850 case CHIP_TONGA:
3851 raster_config = 0x16000012;
3852 raster_config_1 = 0x0000002a;
3853 break;
3854 case CHIP_ICELAND:
3855 if (num_rb == 1)
3856 raster_config = 0x00000000;
3857 else
3858 raster_config = 0x00000002;
3859 raster_config_1 = 0x00000000;
3860 break;
3861 case CHIP_CARRIZO:
3862 raster_config = 0x00000002;
3863 raster_config_1 = 0x00000000;
3864 break;
3865 case CHIP_KAVERI:
3866 /* KV should be 0x00000002, but that causes problems with radeon */
3867 raster_config = 0x00000000; /* 0x00000002 */
3868 raster_config_1 = 0x00000000;
3869 break;
3870 case CHIP_KABINI:
3871 case CHIP_MULLINS:
3872 case CHIP_STONEY:
3873 raster_config = 0x00000000;
3874 raster_config_1 = 0x00000000;
3875 break;
3876 default:
3877 fprintf(stderr,
3878 "radeonsi: Unknown GPU, using 0 for raster_config\n");
3879 raster_config = 0x00000000;
3880 raster_config_1 = 0x00000000;
3881 break;
3882 }
3883
3884 /* Always use the default config when all backends are enabled
3885 * (or when we failed to determine the enabled backends).
3886 */
3887 if (!rb_mask || util_bitcount(rb_mask) >= num_rb) {
3888 si_pm4_set_reg(pm4, R_028350_PA_SC_RASTER_CONFIG,
3889 raster_config);
3890 if (sctx->b.chip_class >= CIK)
3891 si_pm4_set_reg(pm4, R_028354_PA_SC_RASTER_CONFIG_1,
3892 raster_config_1);
3893 } else {
3894 si_write_harvested_raster_configs(sctx, pm4, raster_config, raster_config_1);
3895 }
3896
3897 si_pm4_set_reg(pm4, R_028204_PA_SC_WINDOW_SCISSOR_TL, S_028204_WINDOW_OFFSET_DISABLE(1));
3898 si_pm4_set_reg(pm4, R_028240_PA_SC_GENERIC_SCISSOR_TL, S_028240_WINDOW_OFFSET_DISABLE(1));
3899 si_pm4_set_reg(pm4, R_028244_PA_SC_GENERIC_SCISSOR_BR,
3900 S_028244_BR_X(16384) | S_028244_BR_Y(16384));
3901 si_pm4_set_reg(pm4, R_028030_PA_SC_SCREEN_SCISSOR_TL, 0);
3902 si_pm4_set_reg(pm4, R_028034_PA_SC_SCREEN_SCISSOR_BR,
3903 S_028034_BR_X(16384) | S_028034_BR_Y(16384));
3904
3905 si_pm4_set_reg(pm4, R_02820C_PA_SC_CLIPRECT_RULE, 0xFFFF);
3906 si_pm4_set_reg(pm4, R_028230_PA_SC_EDGERULE,
3907 S_028230_ER_TRI(0xA) |
3908 S_028230_ER_POINT(0xA) |
3909 S_028230_ER_RECT(0xA) |
3910 /* Required by DX10_DIAMOND_TEST_ENA: */
3911 S_028230_ER_LINE_LR(0x1A) |
3912 S_028230_ER_LINE_RL(0x26) |
3913 S_028230_ER_LINE_TB(0xA) |
3914 S_028230_ER_LINE_BT(0xA));
3915 /* PA_SU_HARDWARE_SCREEN_OFFSET must be 0 due to hw bug on SI */
3916 si_pm4_set_reg(pm4, R_028234_PA_SU_HARDWARE_SCREEN_OFFSET, 0);
3917 si_pm4_set_reg(pm4, R_028820_PA_CL_NANINF_CNTL, 0);
3918 si_pm4_set_reg(pm4, R_028AC0_DB_SRESULTS_COMPARE_STATE0, 0x0);
3919 si_pm4_set_reg(pm4, R_028AC4_DB_SRESULTS_COMPARE_STATE1, 0x0);
3920 si_pm4_set_reg(pm4, R_028AC8_DB_PRELOAD_CONTROL, 0x0);
3921 si_pm4_set_reg(pm4, R_02800C_DB_RENDER_OVERRIDE,
3922 S_02800C_FORCE_HIS_ENABLE0(V_02800C_FORCE_DISABLE) |
3923 S_02800C_FORCE_HIS_ENABLE1(V_02800C_FORCE_DISABLE));
3924
3925 si_pm4_set_reg(pm4, R_028400_VGT_MAX_VTX_INDX, ~0);
3926 si_pm4_set_reg(pm4, R_028404_VGT_MIN_VTX_INDX, 0);
3927 si_pm4_set_reg(pm4, R_028408_VGT_INDX_OFFSET, 0);
3928
3929 if (sctx->b.chip_class >= CIK) {
3930 si_pm4_set_reg(pm4, R_00B51C_SPI_SHADER_PGM_RSRC3_LS, S_00B51C_CU_EN(0xffff));
3931 si_pm4_set_reg(pm4, R_00B41C_SPI_SHADER_PGM_RSRC3_HS, 0);
3932 si_pm4_set_reg(pm4, R_00B31C_SPI_SHADER_PGM_RSRC3_ES, S_00B31C_CU_EN(0xffff));
3933 si_pm4_set_reg(pm4, R_00B21C_SPI_SHADER_PGM_RSRC3_GS, S_00B21C_CU_EN(0xffff));
3934
3935 if (sscreen->b.info.num_good_compute_units /
3936 (sscreen->b.info.max_se * sscreen->b.info.max_sh_per_se) <= 4) {
3937 /* Too few available compute units per SH. Disallowing
3938 * VS to run on CU0 could hurt us more than late VS
3939 * allocation would help.
3940 *
3941 * LATE_ALLOC_VS = 2 is the highest safe number.
3942 */
3943 si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS, S_00B118_CU_EN(0xffff));
3944 si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS, S_00B11C_LIMIT(2));
3945 } else {
3946 /* Set LATE_ALLOC_VS == 31. It should be less than
3947 * the number of scratch waves. Limitations:
3948 * - VS can't execute on CU0.
3949 * - If HS writes outputs to LDS, LS can't execute on CU0.
3950 */
3951 si_pm4_set_reg(pm4, R_00B118_SPI_SHADER_PGM_RSRC3_VS, S_00B118_CU_EN(0xfffe));
3952 si_pm4_set_reg(pm4, R_00B11C_SPI_SHADER_LATE_ALLOC_VS, S_00B11C_LIMIT(31));
3953 }
3954
3955 si_pm4_set_reg(pm4, R_00B01C_SPI_SHADER_PGM_RSRC3_PS, S_00B01C_CU_EN(0xffff));
3956 }
3957
3958 if (sctx->b.chip_class >= VI) {
3959 unsigned vgt_tess_distribution;
3960
3961 si_pm4_set_reg(pm4, R_028424_CB_DCC_CONTROL,
3962 S_028424_OVERWRITE_COMBINER_MRT_SHARING_DISABLE(1) |
3963 S_028424_OVERWRITE_COMBINER_WATERMARK(4));
3964 if (sctx->b.family < CHIP_POLARIS10)
3965 si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 30);
3966 si_pm4_set_reg(pm4, R_028C5C_VGT_OUT_DEALLOC_CNTL, 32);
3967
3968 vgt_tess_distribution =
3969 S_028B50_ACCUM_ISOLINE(32) |
3970 S_028B50_ACCUM_TRI(11) |
3971 S_028B50_ACCUM_QUAD(11) |
3972 S_028B50_DONUT_SPLIT(16);
3973
3974 /* Testing with Unigine Heaven extreme tesselation yielded best results
3975 * with TRAP_SPLIT = 3.
3976 */
3977 if (sctx->b.family == CHIP_FIJI ||
3978 sctx->b.family >= CHIP_POLARIS10)
3979 vgt_tess_distribution |= S_028B50_TRAP_SPLIT(3);
3980
3981 si_pm4_set_reg(pm4, R_028B50_VGT_TESS_DISTRIBUTION, vgt_tess_distribution);
3982 } else {
3983 si_pm4_set_reg(pm4, R_028C58_VGT_VERTEX_REUSE_BLOCK_CNTL, 14);
3984 si_pm4_set_reg(pm4, R_028C5C_VGT_OUT_DEALLOC_CNTL, 16);
3985 }
3986
3987 if (sctx->b.family == CHIP_STONEY)
3988 si_pm4_set_reg(pm4, R_028C40_PA_SC_SHADER_CONTROL, 0);
3989
3990 si_pm4_set_reg(pm4, R_028080_TA_BC_BASE_ADDR, border_color_va >> 8);
3991 if (sctx->b.chip_class >= CIK)
3992 si_pm4_set_reg(pm4, R_028084_TA_BC_BASE_ADDR_HI, border_color_va >> 40);
3993 si_pm4_add_bo(pm4, sctx->border_color_buffer, RADEON_USAGE_READ,
3994 RADEON_PRIO_BORDER_COLORS);
3995
3996 si_pm4_upload_indirect_buffer(sctx, pm4);
3997 sctx->init_config = pm4;
3998 }