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