1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "si_build_pm4.h"
26 #include "util/u_upload_mgr.h"
27 #include "util/u_viewport.h"
28
29 #define SI_MAX_SCISSOR 16384
30
si_get_small_prim_cull_info(struct si_context * sctx,struct si_small_prim_cull_info * out)31 void si_get_small_prim_cull_info(struct si_context *sctx, struct si_small_prim_cull_info *out)
32 {
33 /* This is needed by the small primitive culling, because it's done
34 * in screen space.
35 */
36 struct si_small_prim_cull_info info;
37 unsigned num_samples = si_get_num_coverage_samples(sctx);
38 assert(num_samples >= 1);
39
40 info.scale[0] = sctx->viewports.states[0].scale[0];
41 info.scale[1] = sctx->viewports.states[0].scale[1];
42 info.translate[0] = sctx->viewports.states[0].translate[0];
43 info.translate[1] = sctx->viewports.states[0].translate[1];
44
45 /* The viewport shouldn't flip the X axis for the small prim culling to work. */
46 assert(-info.scale[0] + info.translate[0] <= info.scale[0] + info.translate[0]);
47
48 /* If the Y axis is inverted (OpenGL default framebuffer), reverse it.
49 * This is because the viewport transformation inverts the clip space
50 * bounding box, so min becomes max, which breaks small primitive
51 * culling.
52 */
53 if (sctx->viewport0_y_inverted) {
54 info.scale[1] = -info.scale[1];
55 info.translate[1] = -info.translate[1];
56 }
57
58 /* Scale the framebuffer up, so that samples become pixels and small
59 * primitive culling is the same for all sample counts.
60 * This only works with the standard DX sample positions, because
61 * the samples are evenly spaced on both X and Y axes.
62 */
63 for (unsigned i = 0; i < 2; i++) {
64 info.scale[i] *= num_samples;
65 info.translate[i] *= num_samples;
66 }
67
68 /* Better subpixel precision increases the efficiency of small
69 * primitive culling. (more precision means a tighter bounding box
70 * around primitives and more accurate elimination)
71 */
72 unsigned quant_mode = sctx->viewports.as_scissor[0].quant_mode;
73
74 if (quant_mode == SI_QUANT_MODE_12_12_FIXED_POINT_1_4096TH)
75 info.small_prim_precision = num_samples / 4096.0;
76 else if (quant_mode == SI_QUANT_MODE_14_10_FIXED_POINT_1_1024TH)
77 info.small_prim_precision = num_samples / 1024.0;
78 else
79 info.small_prim_precision = num_samples / 256.0;
80
81 *out = info;
82 }
83
si_emit_cull_state(struct si_context * sctx)84 static void si_emit_cull_state(struct si_context *sctx)
85 {
86 assert(sctx->screen->use_ngg_culling);
87
88 struct si_small_prim_cull_info info;
89 si_get_small_prim_cull_info(sctx, &info);
90
91 if (!sctx->small_prim_cull_info_buf ||
92 memcmp(&info, &sctx->last_small_prim_cull_info, sizeof(info))) {
93 unsigned offset = 0;
94
95 /* Align to 256, because the address is shifted by 8 bits. */
96 u_upload_data(sctx->b.const_uploader, 0, sizeof(info), 256, &info, &offset,
97 (struct pipe_resource **)&sctx->small_prim_cull_info_buf);
98
99 sctx->small_prim_cull_info_address = sctx->small_prim_cull_info_buf->gpu_address + offset;
100 sctx->last_small_prim_cull_info = info;
101 }
102
103 /* This will end up in SGPR6 as (value << 8), shifted by the hw. */
104 radeon_add_to_buffer_list(sctx, &sctx->gfx_cs, sctx->small_prim_cull_info_buf,
105 RADEON_USAGE_READ, RADEON_PRIO_CONST_BUFFER);
106 radeon_begin(&sctx->gfx_cs);
107 radeon_set_sh_reg(R_00B220_SPI_SHADER_PGM_LO_GS,
108 sctx->small_prim_cull_info_address >> 8);
109 radeon_end();
110
111 /* Set VS_STATE.SMALL_PRIM_PRECISION for NGG culling.
112 *
113 * small_prim_precision is 1 / 2^n. We only need n between 5 (1/32) and 12 (1/4096).
114 * Such a floating point value can be packed into 4 bits as follows:
115 * If we pass the first 4 bits of the exponent to the shader and set the next 3 bits
116 * to 1, we'll get the number exactly because all other bits are always 0. See:
117 * 1
118 * value = (0x70 | value.exponent[0:3]) << 23 = ------------------------------
119 * 2 ^ (15 - value.exponent[0:3])
120 *
121 * So pass only the first 4 bits of the float exponent to the shader.
122 */
123 sctx->current_vs_state &= C_VS_STATE_SMALL_PRIM_PRECISION;
124 sctx->current_vs_state |= S_VS_STATE_SMALL_PRIM_PRECISION(fui(info.small_prim_precision) >> 23);
125 }
126
si_set_scissor_states(struct pipe_context * pctx,unsigned start_slot,unsigned num_scissors,const struct pipe_scissor_state * state)127 static void si_set_scissor_states(struct pipe_context *pctx, unsigned start_slot,
128 unsigned num_scissors, const struct pipe_scissor_state *state)
129 {
130 struct si_context *ctx = (struct si_context *)pctx;
131 int i;
132
133 for (i = 0; i < num_scissors; i++)
134 ctx->scissors[start_slot + i] = state[i];
135
136 if (!ctx->queued.named.rasterizer->scissor_enable)
137 return;
138
139 si_mark_atom_dirty(ctx, &ctx->atoms.s.scissors);
140 }
141
142 /* Since the guard band disables clipping, we have to clip per-pixel
143 * using a scissor.
144 */
si_get_scissor_from_viewport(struct si_context * ctx,const struct pipe_viewport_state * vp,struct si_signed_scissor * scissor)145 static void si_get_scissor_from_viewport(struct si_context *ctx,
146 const struct pipe_viewport_state *vp,
147 struct si_signed_scissor *scissor)
148 {
149 float tmp, minx, miny, maxx, maxy;
150
151 /* Convert (-1, -1) and (1, 1) from clip space into window space. */
152 minx = -vp->scale[0] + vp->translate[0];
153 miny = -vp->scale[1] + vp->translate[1];
154 maxx = vp->scale[0] + vp->translate[0];
155 maxy = vp->scale[1] + vp->translate[1];
156
157 /* Handle inverted viewports. */
158 if (minx > maxx) {
159 tmp = minx;
160 minx = maxx;
161 maxx = tmp;
162 }
163 if (miny > maxy) {
164 tmp = miny;
165 miny = maxy;
166 maxy = tmp;
167 }
168
169 /* Convert to integer and round up the max bounds. */
170 scissor->minx = minx;
171 scissor->miny = miny;
172 scissor->maxx = ceilf(maxx);
173 scissor->maxy = ceilf(maxy);
174 }
175
si_clamp_scissor(struct si_context * ctx,struct pipe_scissor_state * out,struct si_signed_scissor * scissor)176 static void si_clamp_scissor(struct si_context *ctx, struct pipe_scissor_state *out,
177 struct si_signed_scissor *scissor)
178 {
179 out->minx = CLAMP(scissor->minx, 0, SI_MAX_SCISSOR);
180 out->miny = CLAMP(scissor->miny, 0, SI_MAX_SCISSOR);
181 out->maxx = CLAMP(scissor->maxx, 0, SI_MAX_SCISSOR);
182 out->maxy = CLAMP(scissor->maxy, 0, SI_MAX_SCISSOR);
183 }
184
si_clip_scissor(struct pipe_scissor_state * out,struct pipe_scissor_state * clip)185 static void si_clip_scissor(struct pipe_scissor_state *out, struct pipe_scissor_state *clip)
186 {
187 out->minx = MAX2(out->minx, clip->minx);
188 out->miny = MAX2(out->miny, clip->miny);
189 out->maxx = MIN2(out->maxx, clip->maxx);
190 out->maxy = MIN2(out->maxy, clip->maxy);
191 }
192
si_scissor_make_union(struct si_signed_scissor * out,struct si_signed_scissor * in)193 static void si_scissor_make_union(struct si_signed_scissor *out, struct si_signed_scissor *in)
194 {
195 out->minx = MIN2(out->minx, in->minx);
196 out->miny = MIN2(out->miny, in->miny);
197 out->maxx = MAX2(out->maxx, in->maxx);
198 out->maxy = MAX2(out->maxy, in->maxy);
199 out->quant_mode = MIN2(out->quant_mode, in->quant_mode);
200 }
201
si_emit_one_scissor(struct si_context * ctx,struct radeon_cmdbuf * cs,struct si_signed_scissor * vp_scissor,struct pipe_scissor_state * scissor)202 static void si_emit_one_scissor(struct si_context *ctx, struct radeon_cmdbuf *cs,
203 struct si_signed_scissor *vp_scissor,
204 struct pipe_scissor_state *scissor)
205 {
206 struct pipe_scissor_state final;
207
208 if (ctx->vs_disables_clipping_viewport) {
209 final.minx = final.miny = 0;
210 final.maxx = final.maxy = SI_MAX_SCISSOR;
211 } else {
212 si_clamp_scissor(ctx, &final, vp_scissor);
213 }
214
215 if (scissor)
216 si_clip_scissor(&final, scissor);
217
218 radeon_begin(cs);
219
220 /* Workaround for a hw bug on GFX6 that occurs when PA_SU_HARDWARE_-
221 * SCREEN_OFFSET != 0 and any_scissor.BR_X/Y <= 0.
222 */
223 if (ctx->chip_class == GFX6 && (final.maxx == 0 || final.maxy == 0)) {
224 radeon_emit(S_028250_TL_X(1) | S_028250_TL_Y(1) | S_028250_WINDOW_OFFSET_DISABLE(1));
225 radeon_emit(S_028254_BR_X(1) | S_028254_BR_Y(1));
226 radeon_end();
227 return;
228 }
229
230 radeon_emit(S_028250_TL_X(final.minx) | S_028250_TL_Y(final.miny) |
231 S_028250_WINDOW_OFFSET_DISABLE(1));
232 radeon_emit(S_028254_BR_X(final.maxx) | S_028254_BR_Y(final.maxy));
233 radeon_end();
234 }
235
236 #define MAX_PA_SU_HARDWARE_SCREEN_OFFSET 8176
237
si_emit_guardband(struct si_context * ctx)238 static void si_emit_guardband(struct si_context *ctx)
239 {
240 const struct si_state_rasterizer *rs = ctx->queued.named.rasterizer;
241 struct si_signed_scissor vp_as_scissor;
242 struct pipe_viewport_state vp;
243 float left, top, right, bottom, max_range, guardband_x, guardband_y;
244 float discard_x, discard_y;
245
246 if (ctx->vs_writes_viewport_index) {
247 /* Shaders can draw to any viewport. Make a union of all
248 * viewports. */
249 vp_as_scissor = ctx->viewports.as_scissor[0];
250 for (unsigned i = 1; i < SI_MAX_VIEWPORTS; i++) {
251 si_scissor_make_union(&vp_as_scissor, &ctx->viewports.as_scissor[i]);
252 }
253 } else {
254 vp_as_scissor = ctx->viewports.as_scissor[0];
255 }
256
257 /* Blits don't set the viewport state. The vertex shader determines
258 * the viewport size by scaling the coordinates, so we don't know
259 * how large the viewport is. Assume the worst case.
260 */
261 if (ctx->vs_disables_clipping_viewport)
262 vp_as_scissor.quant_mode = SI_QUANT_MODE_16_8_FIXED_POINT_1_256TH;
263
264 /* Determine the optimal hardware screen offset to center the viewport
265 * within the viewport range in order to maximize the guardband size.
266 */
267 int hw_screen_offset_x = (vp_as_scissor.maxx + vp_as_scissor.minx) / 2;
268 int hw_screen_offset_y = (vp_as_scissor.maxy + vp_as_scissor.miny) / 2;
269
270 /* GFX6-GFX7 need to align the offset to an ubertile consisting of all SEs. */
271 const unsigned hw_screen_offset_alignment =
272 ctx->chip_class >= GFX8 ? 16 : MAX2(ctx->screen->se_tile_repeat, 16);
273
274 /* Indexed by quantization modes */
275 static int max_viewport_size[] = {65535, 16383, 4095};
276
277 /* Ensure that the whole viewport stays representable in
278 * absolute coordinates.
279 * See comment in si_set_viewport_states.
280 */
281 assert(vp_as_scissor.maxx <= max_viewport_size[vp_as_scissor.quant_mode] &&
282 vp_as_scissor.maxy <= max_viewport_size[vp_as_scissor.quant_mode]);
283
284 hw_screen_offset_x = CLAMP(hw_screen_offset_x, 0, MAX_PA_SU_HARDWARE_SCREEN_OFFSET);
285 hw_screen_offset_y = CLAMP(hw_screen_offset_y, 0, MAX_PA_SU_HARDWARE_SCREEN_OFFSET);
286
287 /* Align the screen offset by dropping the low bits. */
288 hw_screen_offset_x &= ~(hw_screen_offset_alignment - 1);
289 hw_screen_offset_y &= ~(hw_screen_offset_alignment - 1);
290
291 /* Apply the offset to center the viewport and maximize the guardband. */
292 vp_as_scissor.minx -= hw_screen_offset_x;
293 vp_as_scissor.maxx -= hw_screen_offset_x;
294 vp_as_scissor.miny -= hw_screen_offset_y;
295 vp_as_scissor.maxy -= hw_screen_offset_y;
296
297 /* Reconstruct the viewport transformation from the scissor. */
298 vp.translate[0] = (vp_as_scissor.minx + vp_as_scissor.maxx) / 2.0;
299 vp.translate[1] = (vp_as_scissor.miny + vp_as_scissor.maxy) / 2.0;
300 vp.scale[0] = vp_as_scissor.maxx - vp.translate[0];
301 vp.scale[1] = vp_as_scissor.maxy - vp.translate[1];
302
303 /* Treat a 0x0 viewport as 1x1 to prevent division by zero. */
304 if (vp_as_scissor.minx == vp_as_scissor.maxx)
305 vp.scale[0] = 0.5;
306 if (vp_as_scissor.miny == vp_as_scissor.maxy)
307 vp.scale[1] = 0.5;
308
309 /* Find the biggest guard band that is inside the supported viewport
310 * range. The guard band is specified as a horizontal and vertical
311 * distance from (0,0) in clip space.
312 *
313 * This is done by applying the inverse viewport transformation
314 * on the viewport limits to get those limits in clip space.
315 *
316 * The viewport range is [-max_viewport_size/2 - 1, max_viewport_size/2].
317 * (-1 to the min coord because max_viewport_size is odd and ViewportBounds
318 * Min/Max are -32768, 32767).
319 */
320 assert(vp_as_scissor.quant_mode < ARRAY_SIZE(max_viewport_size));
321 max_range = max_viewport_size[vp_as_scissor.quant_mode] / 2;
322 left = (-max_range - 1 - vp.translate[0]) / vp.scale[0];
323 right = (max_range - vp.translate[0]) / vp.scale[0];
324 top = (-max_range - 1 - vp.translate[1]) / vp.scale[1];
325 bottom = (max_range - vp.translate[1]) / vp.scale[1];
326
327 assert(left <= -1 && top <= -1 && right >= 1 && bottom >= 1);
328
329 guardband_x = MIN2(-left, right);
330 guardband_y = MIN2(-top, bottom);
331
332 discard_x = 1.0;
333 discard_y = 1.0;
334
335 if (unlikely(util_prim_is_points_or_lines(ctx->current_rast_prim))) {
336 /* When rendering wide points or lines, we need to be more
337 * conservative about when to discard them entirely. */
338 float pixels;
339
340 if (ctx->current_rast_prim == PIPE_PRIM_POINTS)
341 pixels = rs->max_point_size;
342 else
343 pixels = rs->line_width;
344
345 /* Add half the point size / line width */
346 discard_x += pixels / (2.0 * vp.scale[0]);
347 discard_y += pixels / (2.0 * vp.scale[1]);
348
349 /* Discard primitives that would lie entirely outside the clip
350 * region. */
351 discard_x = MIN2(discard_x, guardband_x);
352 discard_y = MIN2(discard_y, guardband_y);
353 }
354
355 /* If any of the GB registers is updated, all of them must be updated.
356 * R_028BE8_PA_CL_GB_VERT_CLIP_ADJ, R_028BEC_PA_CL_GB_VERT_DISC_ADJ
357 * R_028BF0_PA_CL_GB_HORZ_CLIP_ADJ, R_028BF4_PA_CL_GB_HORZ_DISC_ADJ
358 */
359 radeon_begin(&ctx->gfx_cs);
360 radeon_opt_set_context_reg4(ctx, R_028BE8_PA_CL_GB_VERT_CLIP_ADJ,
361 SI_TRACKED_PA_CL_GB_VERT_CLIP_ADJ, fui(guardband_y), fui(discard_y),
362 fui(guardband_x), fui(discard_x));
363 radeon_opt_set_context_reg(ctx, R_028234_PA_SU_HARDWARE_SCREEN_OFFSET,
364 SI_TRACKED_PA_SU_HARDWARE_SCREEN_OFFSET,
365 S_028234_HW_SCREEN_OFFSET_X(hw_screen_offset_x >> 4) |
366 S_028234_HW_SCREEN_OFFSET_Y(hw_screen_offset_y >> 4));
367 radeon_opt_set_context_reg(
368 ctx, R_028BE4_PA_SU_VTX_CNTL, SI_TRACKED_PA_SU_VTX_CNTL,
369 S_028BE4_PIX_CENTER(rs->half_pixel_center) |
370 S_028BE4_QUANT_MODE(V_028BE4_X_16_8_FIXED_POINT_1_256TH + vp_as_scissor.quant_mode));
371 radeon_end_update_context_roll(ctx);
372 }
373
si_emit_scissors(struct si_context * ctx)374 static void si_emit_scissors(struct si_context *ctx)
375 {
376 struct radeon_cmdbuf *cs = &ctx->gfx_cs;
377 struct pipe_scissor_state *states = ctx->scissors;
378 bool scissor_enabled = ctx->queued.named.rasterizer->scissor_enable;
379
380 /* The simple case: Only 1 viewport is active. */
381 if (!ctx->vs_writes_viewport_index) {
382 struct si_signed_scissor *vp = &ctx->viewports.as_scissor[0];
383
384 radeon_begin(cs);
385 radeon_set_context_reg_seq(R_028250_PA_SC_VPORT_SCISSOR_0_TL, 2);
386 radeon_end();
387
388 si_emit_one_scissor(ctx, cs, vp, scissor_enabled ? &states[0] : NULL);
389 return;
390 }
391
392 /* All registers in the array need to be updated if any of them is changed.
393 * This is a hardware requirement.
394 */
395 radeon_begin(cs);
396 radeon_set_context_reg_seq(R_028250_PA_SC_VPORT_SCISSOR_0_TL, SI_MAX_VIEWPORTS * 2);
397 radeon_end();
398
399 for (unsigned i = 0; i < SI_MAX_VIEWPORTS; i++) {
400 si_emit_one_scissor(ctx, cs, &ctx->viewports.as_scissor[i],
401 scissor_enabled ? &states[i] : NULL);
402 }
403 }
404
si_set_viewport_states(struct pipe_context * pctx,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * state)405 static void si_set_viewport_states(struct pipe_context *pctx, unsigned start_slot,
406 unsigned num_viewports, const struct pipe_viewport_state *state)
407 {
408 struct si_context *ctx = (struct si_context *)pctx;
409 int i;
410
411 for (i = 0; i < num_viewports; i++) {
412 unsigned index = start_slot + i;
413 struct si_signed_scissor *scissor = &ctx->viewports.as_scissor[index];
414
415 ctx->viewports.states[index] = state[i];
416
417 si_get_scissor_from_viewport(ctx, &state[i], scissor);
418
419 int max_corner = MAX2(
420 MAX2(abs(scissor->maxx), abs(scissor->maxy)),
421 MAX2(abs(scissor->minx), abs(scissor->miny)));
422
423 /* Determine the best quantization mode (subpixel precision),
424 * but also leave enough space for the guardband.
425 *
426 * Note that primitive binning requires QUANT_MODE == 16_8 on Vega10
427 * and Raven1 for line and rectangle primitive types to work correctly.
428 * Always use 16_8 if primitive binning is possible to occur.
429 */
430 if ((ctx->family == CHIP_VEGA10 || ctx->family == CHIP_RAVEN) && ctx->screen->dpbb_allowed)
431 max_corner = 16384; /* Use QUANT_MODE == 16_8. */
432
433 /* Another constraint is that all coordinates in the viewport
434 * are representable in fixed point with respect to the
435 * surface origin.
436 *
437 * It means that PA_SU_HARDWARE_SCREEN_OFFSET can't be given
438 * an offset that would make the upper corner of the viewport
439 * greater than the maximum representable number post
440 * quantization, ie 2^quant_bits.
441 *
442 * This does not matter for 14.10 and 16.8 formats since the
443 * offset is already limited at 8k, but it means we can't use
444 * 12.12 if we are drawing to some pixels outside the lower
445 * 4k x 4k of the render target.
446 */
447
448 if (max_corner <= 1024) /* 4K scanline area for guardband */
449 scissor->quant_mode = SI_QUANT_MODE_12_12_FIXED_POINT_1_4096TH;
450 else if (max_corner <= 4096) /* 16K scanline area for guardband */
451 scissor->quant_mode = SI_QUANT_MODE_14_10_FIXED_POINT_1_1024TH;
452 else /* 64K scanline area for guardband */
453 scissor->quant_mode = SI_QUANT_MODE_16_8_FIXED_POINT_1_256TH;
454 }
455
456 if (start_slot == 0) {
457 ctx->viewport0_y_inverted =
458 -state->scale[1] + state->translate[1] > state->scale[1] + state->translate[1];
459
460 /* NGG cull state uses the viewport and quant mode. */
461 if (ctx->screen->use_ngg_culling)
462 si_mark_atom_dirty(ctx, &ctx->atoms.s.ngg_cull_state);
463 }
464
465 si_mark_atom_dirty(ctx, &ctx->atoms.s.viewports);
466 si_mark_atom_dirty(ctx, &ctx->atoms.s.guardband);
467 si_mark_atom_dirty(ctx, &ctx->atoms.s.scissors);
468 }
469
si_emit_one_viewport(struct si_context * ctx,struct pipe_viewport_state * state)470 static void si_emit_one_viewport(struct si_context *ctx, struct pipe_viewport_state *state)
471 {
472 struct radeon_cmdbuf *cs = &ctx->gfx_cs;
473
474 radeon_begin(cs);
475 radeon_emit(fui(state->scale[0]));
476 radeon_emit(fui(state->translate[0]));
477 radeon_emit(fui(state->scale[1]));
478 radeon_emit(fui(state->translate[1]));
479 radeon_emit(fui(state->scale[2]));
480 radeon_emit(fui(state->translate[2]));
481 radeon_end();
482 }
483
si_emit_viewports(struct si_context * ctx)484 static void si_emit_viewports(struct si_context *ctx)
485 {
486 struct radeon_cmdbuf *cs = &ctx->gfx_cs;
487 struct pipe_viewport_state *states = ctx->viewports.states;
488
489 /* The simple case: Only 1 viewport is active. */
490 if (!ctx->vs_writes_viewport_index) {
491 radeon_begin(cs);
492 radeon_set_context_reg_seq(R_02843C_PA_CL_VPORT_XSCALE, 6);
493 radeon_end();
494
495 si_emit_one_viewport(ctx, &states[0]);
496 return;
497 }
498
499 /* All registers in the array need to be updated if any of them is changed.
500 * This is a hardware requirement.
501 */
502 radeon_begin(cs);
503 radeon_set_context_reg_seq(R_02843C_PA_CL_VPORT_XSCALE + 0, SI_MAX_VIEWPORTS * 6);
504 radeon_end();
505
506 for (unsigned i = 0; i < SI_MAX_VIEWPORTS; i++)
507 si_emit_one_viewport(ctx, &states[i]);
508 }
509
si_viewport_zmin_zmax(const struct pipe_viewport_state * vp,bool halfz,bool window_space_position,float * zmin,float * zmax)510 static inline void si_viewport_zmin_zmax(const struct pipe_viewport_state *vp, bool halfz,
511 bool window_space_position, float *zmin, float *zmax)
512 {
513 if (window_space_position) {
514 *zmin = 0;
515 *zmax = 1;
516 return;
517 }
518 util_viewport_zmin_zmax(vp, halfz, zmin, zmax);
519 }
520
si_emit_depth_ranges(struct si_context * ctx)521 static void si_emit_depth_ranges(struct si_context *ctx)
522 {
523 struct radeon_cmdbuf *cs = &ctx->gfx_cs;
524 struct pipe_viewport_state *states = ctx->viewports.states;
525 bool clip_halfz = ctx->queued.named.rasterizer->clip_halfz;
526 bool window_space = ctx->vs_disables_clipping_viewport;
527 float zmin, zmax;
528
529 /* The simple case: Only 1 viewport is active. */
530 if (!ctx->vs_writes_viewport_index) {
531 si_viewport_zmin_zmax(&states[0], clip_halfz, window_space, &zmin, &zmax);
532
533 radeon_begin(cs);
534 radeon_set_context_reg_seq(R_0282D0_PA_SC_VPORT_ZMIN_0, 2);
535 radeon_emit(fui(zmin));
536 radeon_emit(fui(zmax));
537 radeon_end();
538 return;
539 }
540
541 /* All registers in the array need to be updated if any of them is changed.
542 * This is a hardware requirement.
543 */
544 radeon_begin(cs);
545 radeon_set_context_reg_seq(R_0282D0_PA_SC_VPORT_ZMIN_0, SI_MAX_VIEWPORTS * 2);
546 for (unsigned i = 0; i < SI_MAX_VIEWPORTS; i++) {
547 si_viewport_zmin_zmax(&states[i], clip_halfz, window_space, &zmin, &zmax);
548 radeon_emit(fui(zmin));
549 radeon_emit(fui(zmax));
550 }
551 radeon_end();
552 }
553
si_emit_viewport_states(struct si_context * ctx)554 static void si_emit_viewport_states(struct si_context *ctx)
555 {
556 si_emit_viewports(ctx);
557 si_emit_depth_ranges(ctx);
558 }
559
560 /**
561 * This reacts to 2 state changes:
562 * - VS.writes_viewport_index
563 * - VS output position in window space (enable/disable)
564 *
565 * Normally, we only emit 1 viewport and 1 scissor if no shader is using
566 * the VIEWPORT_INDEX output, and emitting the other viewports and scissors
567 * is delayed. When a shader with VIEWPORT_INDEX appears, this should be
568 * called to emit the rest.
569 */
si_update_vs_viewport_state(struct si_context * ctx)570 void si_update_vs_viewport_state(struct si_context *ctx)
571 {
572 struct si_shader_info *info = si_get_vs_info(ctx);
573 bool vs_window_space;
574
575 if (!info)
576 return;
577
578 /* When the VS disables clipping and viewport transformation. */
579 vs_window_space = info->stage == MESA_SHADER_VERTEX && info->base.vs.window_space_position;
580
581 if (ctx->vs_disables_clipping_viewport != vs_window_space) {
582 ctx->vs_disables_clipping_viewport = vs_window_space;
583 si_mark_atom_dirty(ctx, &ctx->atoms.s.scissors);
584 si_mark_atom_dirty(ctx, &ctx->atoms.s.viewports);
585 }
586
587 /* Viewport index handling. */
588 if (ctx->vs_writes_viewport_index == info->writes_viewport_index)
589 return;
590
591 /* This changes how the guardband is computed. */
592 ctx->vs_writes_viewport_index = info->writes_viewport_index;
593 si_mark_atom_dirty(ctx, &ctx->atoms.s.guardband);
594
595 /* Emit scissors and viewports that were enabled by having
596 * the ViewportIndex output.
597 */
598 if (info->writes_viewport_index) {
599 si_mark_atom_dirty(ctx, &ctx->atoms.s.scissors);
600 si_mark_atom_dirty(ctx, &ctx->atoms.s.viewports);
601 }
602 }
603
si_emit_window_rectangles(struct si_context * sctx)604 static void si_emit_window_rectangles(struct si_context *sctx)
605 {
606 /* There are four clipping rectangles. Their corner coordinates are inclusive.
607 * Every pixel is assigned a number from 0 and 15 by setting bits 0-3 depending
608 * on whether the pixel is inside cliprects 0-3, respectively. For example,
609 * if a pixel is inside cliprects 0 and 1, but outside 2 and 3, it is assigned
610 * the number 3 (binary 0011).
611 *
612 * If CLIPRECT_RULE & (1 << number), the pixel is rasterized.
613 */
614 struct radeon_cmdbuf *cs = &sctx->gfx_cs;
615 static const unsigned outside[4] = {
616 /* outside rectangle 0 */
617 V_02820C_OUT | V_02820C_IN_1 | V_02820C_IN_2 | V_02820C_IN_21 | V_02820C_IN_3 |
618 V_02820C_IN_31 | V_02820C_IN_32 | V_02820C_IN_321,
619 /* outside rectangles 0, 1 */
620 V_02820C_OUT | V_02820C_IN_2 | V_02820C_IN_3 | V_02820C_IN_32,
621 /* outside rectangles 0, 1, 2 */
622 V_02820C_OUT | V_02820C_IN_3,
623 /* outside rectangles 0, 1, 2, 3 */
624 V_02820C_OUT,
625 };
626 const unsigned disabled = 0xffff; /* all inside and outside cases */
627 unsigned num_rectangles = sctx->num_window_rectangles;
628 struct pipe_scissor_state *rects = sctx->window_rectangles;
629 unsigned rule;
630
631 assert(num_rectangles <= 4);
632
633 if (num_rectangles == 0)
634 rule = disabled;
635 else if (sctx->window_rectangles_include)
636 rule = ~outside[num_rectangles - 1];
637 else
638 rule = outside[num_rectangles - 1];
639
640 radeon_begin(cs);
641 radeon_opt_set_context_reg(sctx, R_02820C_PA_SC_CLIPRECT_RULE, SI_TRACKED_PA_SC_CLIPRECT_RULE,
642 rule);
643 if (num_rectangles == 0) {
644 radeon_end();
645 return;
646 }
647
648 radeon_set_context_reg_seq(R_028210_PA_SC_CLIPRECT_0_TL, num_rectangles * 2);
649 for (unsigned i = 0; i < num_rectangles; i++) {
650 radeon_emit(S_028210_TL_X(rects[i].minx) | S_028210_TL_Y(rects[i].miny));
651 radeon_emit(S_028214_BR_X(rects[i].maxx) | S_028214_BR_Y(rects[i].maxy));
652 }
653 radeon_end();
654 }
655
si_set_window_rectangles(struct pipe_context * ctx,bool include,unsigned num_rectangles,const struct pipe_scissor_state * rects)656 static void si_set_window_rectangles(struct pipe_context *ctx, bool include,
657 unsigned num_rectangles,
658 const struct pipe_scissor_state *rects)
659 {
660 struct si_context *sctx = (struct si_context *)ctx;
661
662 sctx->num_window_rectangles = num_rectangles;
663 sctx->window_rectangles_include = include;
664 if (num_rectangles) {
665 memcpy(sctx->window_rectangles, rects, sizeof(*rects) * num_rectangles);
666 }
667
668 si_mark_atom_dirty(sctx, &sctx->atoms.s.window_rectangles);
669 }
670
si_init_viewport_functions(struct si_context * ctx)671 void si_init_viewport_functions(struct si_context *ctx)
672 {
673 ctx->atoms.s.guardband.emit = si_emit_guardband;
674 ctx->atoms.s.scissors.emit = si_emit_scissors;
675 ctx->atoms.s.viewports.emit = si_emit_viewport_states;
676 ctx->atoms.s.window_rectangles.emit = si_emit_window_rectangles;
677 ctx->atoms.s.ngg_cull_state.emit = si_emit_cull_state;
678
679 ctx->b.set_scissor_states = si_set_scissor_states;
680 ctx->b.set_viewport_states = si_set_viewport_states;
681 ctx->b.set_window_rectangles = si_set_window_rectangles;
682
683 for (unsigned i = 0; i < 16; i++)
684 ctx->viewports.as_scissor[i].quant_mode = SI_QUANT_MODE_16_8_FIXED_POINT_1_256TH;
685 }
686