xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/llvmpipe/lp_rast_linear_fallback.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007-2021 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 /*
29  * Rasterization for binned rectangles within a tile
30  */
31 
32 #include <limits.h>
33 #include "util/u_math.h"
34 #include "lp_debug.h"
35 #include "lp_perf.h"
36 #include "lp_rast_priv.h"
37 
38 
39 /* Our 16-pixel stamps are layed out as:
40  *
41  *    0  1  2  3
42  *    4  5  6  7
43  *    8  9  10 11
44  *    12 13 14 15
45  *
46  * Define bitmasks for each row and column in this layout:
47  */
48 #define COLUMN0 ((1<<0)|(1<<4)|(1<<8) |(1<<12))
49 #define COLUMN1 ((1<<1)|(1<<5)|(1<<9) |(1<<13))
50 #define COLUMN2 ((1<<2)|(1<<6)|(1<<10)|(1<<14))
51 #define COLUMN3 ((1<<3)|(1<<7)|(1<<11)|(1<<15))
52 
53 #define ROW0 ((1<<0) |(1<<1) |(1<<2) |(1<<3))
54 #define ROW1 ((1<<4) |(1<<5) |(1<<6) |(1<<7))
55 #define ROW2 ((1<<8) |(1<<9) |(1<<10)|(1<<11))
56 #define ROW3 ((1<<12)|(1<<13)|(1<<14)|(1<<15))
57 
58 #define STAMP_SIZE 4
59 
60 static const unsigned left_mask_tab[STAMP_SIZE] = {
61    COLUMN0 | COLUMN1 | COLUMN2 | COLUMN3,
62    COLUMN1 | COLUMN2 | COLUMN3,
63    COLUMN2 | COLUMN3,
64    COLUMN3,
65 };
66 
67 static const unsigned right_mask_tab[STAMP_SIZE] = {
68    COLUMN0,
69    COLUMN0 | COLUMN1,
70    COLUMN0 | COLUMN1 | COLUMN2,
71    COLUMN0 | COLUMN1 | COLUMN2 | COLUMN3,
72 };
73 
74 static const unsigned top_mask_tab[STAMP_SIZE] = {
75    ROW0 | ROW1 | ROW2 | ROW3,
76    ROW1 | ROW2 | ROW3,
77    ROW2 | ROW3,
78    ROW3,
79 };
80 
81 static const unsigned bottom_mask_tab[STAMP_SIZE] = {
82    ROW0,
83    ROW0 | ROW1,
84    ROW0 | ROW1 | ROW2,
85    ROW0 | ROW1 | ROW2 | ROW3,
86 };
87 
88 
89 
90 static void
shade_quads(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,unsigned x,unsigned y,unsigned mask)91 shade_quads(struct lp_rasterizer_task *task,
92             const struct lp_rast_shader_inputs *inputs,
93             unsigned x, unsigned y,
94             unsigned mask)
95 {
96    const struct lp_rast_state *state = task->state;
97    const struct lp_fragment_shader_variant *variant = state->variant;
98    const struct lp_scene *scene = task->scene;
99    const unsigned stride = scene->cbufs[0].stride;
100    uint8_t *cbufs[1] = { scene->cbufs[0].map + y * stride + x * 4 };
101    unsigned strides[1] = { stride };
102 
103    assert(!variant->key.depth.enabled);
104 
105    /* Propagate non-interpolated raster state */
106    task->thread_data.raster_state.viewport_index = inputs->viewport_index;
107 
108    /* run shader on 4x4 block */
109    BEGIN_JIT_CALL(state, task);
110    const unsigned fn_index = mask == 0xffff ? RAST_WHOLE : RAST_EDGE_TEST;
111    variant->jit_function[fn_index](&state->jit_context,
112                                    &state->jit_resources,
113                                    x, y,
114                                    inputs->frontfacing,
115                                    GET_A0(inputs),
116                                    GET_DADX(inputs),
117                                    GET_DADY(inputs),
118                                    cbufs,
119                                    NULL,
120                                    mask,
121                                    &task->thread_data,
122                                    strides, 0, 0, 0);
123    END_JIT_CALL();
124 }
125 
126 
127 /* Shade a 4x4 stamp which may be partially outside the rectangle,
128  * according to the mask parameter.
129  */
130 static inline void
partial(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,unsigned ix,unsigned iy,unsigned mask)131 partial(struct lp_rasterizer_task *task,
132         const struct lp_rast_shader_inputs *inputs,
133         unsigned ix, unsigned iy,
134         unsigned mask)
135 {
136    /* Unfortunately we can end up generating full blocks on this path,
137     * need to catch them.
138     */
139    assert(mask != 0x0);
140    shade_quads(task, inputs, ix * STAMP_SIZE, iy * STAMP_SIZE, mask);
141 }
142 
143 
144 /**
145  * Run the full SoA shader.
146  */
147 void
lp_rast_linear_rect_fallback(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,const struct u_rect * box)148 lp_rast_linear_rect_fallback(struct lp_rasterizer_task *task,
149                              const struct lp_rast_shader_inputs *inputs,
150                              const struct u_rect *box)
151 {
152    /* The interior of the rectangle (if there is one) will be
153     * rasterized as full 4x4 stamps.
154     *
155     * At each edge of the rectangle, however, there will be a fringe
156     * of partial blocks where the edge lands somewhere in the middle
157     * of a 4x4-pixel stamp.
158     *
159     * For each edge, precalculate a mask of the pixels inside that
160     * edge for the first 4x4-pixel stamp.
161     *
162     * Note that at the corners, and for narrow rectangles, an
163     * individual stamp may have two or more edges active.  We'll deal
164     * with that below by combining these masks as appropriate.
165     */
166    const unsigned left_mask   = left_mask_tab   [box->x0 & (STAMP_SIZE - 1)];
167    const unsigned right_mask  = right_mask_tab  [box->x1 & (STAMP_SIZE - 1)];
168    const unsigned top_mask    = top_mask_tab    [box->y0 & (STAMP_SIZE - 1)];
169    const unsigned bottom_mask = bottom_mask_tab [box->y1 & (STAMP_SIZE - 1)];
170 
171    const unsigned ix0 = box->x0 / STAMP_SIZE;
172    const unsigned ix1 = box->x1 / STAMP_SIZE;
173    const unsigned iy0 = box->y0 / STAMP_SIZE;
174    const unsigned iy1 = box->y1 / STAMP_SIZE;
175 
176    /* Various special cases */
177    if (ix0 == ix1 && iy0 == iy1) {
178       /* Rectangle is contained within a single 4x4-pixel stamp */
179       partial(task, inputs, ix0, iy0,
180               (left_mask & right_mask &
181                top_mask & bottom_mask));
182    }
183    else if (ix0 == ix1) {
184       /* Left and right edges fall on the same 4-pixel-wide column */
185       unsigned mask = left_mask & right_mask;
186       partial(task, inputs, ix0, iy0, mask & top_mask);
187       for (unsigned i = iy0 + 1; i < iy1; i++)
188          partial(task, inputs, ix0, i, mask);
189       partial(task, inputs, ix0, iy1, mask & bottom_mask);
190    }
191    else if (iy0 == iy1) {
192       /* Top and bottom edges fall on the same 4-pixel-wide row */
193       unsigned mask = top_mask & bottom_mask;
194       partial(task, inputs, ix0, iy0, mask & left_mask);
195       for (unsigned i = ix0 + 1; i < ix1; i++)
196          partial(task, inputs, i, iy0, mask);
197       partial(task, inputs, ix1, iy0, mask & right_mask);
198    } else {
199       /* Each pair of edges falls in a separate 4-pixel-wide
200        * row/column.
201        */
202       partial(task, inputs, ix0, iy0, left_mask  & top_mask);
203       partial(task, inputs, ix0, iy1, left_mask  & bottom_mask);
204       partial(task, inputs, ix1, iy0, right_mask & top_mask);
205       partial(task, inputs, ix1, iy1, right_mask & bottom_mask);
206 
207       for (unsigned i = ix0 + 1; i < ix1; i++)
208          partial(task, inputs, i, iy0, top_mask);
209 
210       for (unsigned i = ix0 + 1; i < ix1; i++)
211          partial(task, inputs, i, iy1, bottom_mask);
212 
213       for (unsigned i = iy0 + 1; i < iy1; i++)
214          partial(task, inputs, ix0, i, left_mask);
215 
216       for (unsigned i = iy0 + 1; i < iy1; i++)
217          partial(task, inputs, ix1, i, right_mask);
218 
219       /* Full interior blocks */
220       for (unsigned j = iy0 + 1; j < iy1; j++) {
221          for (unsigned i = ix0 + 1; i < ix1; i++) {
222             shade_quads(task, inputs, i * STAMP_SIZE, j * STAMP_SIZE, 0xffff);
223          }
224       }
225    }
226 }
227