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