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