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 x, y,
113 inputs->frontfacing,
114 GET_A0(inputs),
115 GET_DADX(inputs),
116 GET_DADY(inputs),
117 cbufs,
118 NULL,
119 mask,
120 &task->thread_data,
121 strides, 0, 0, 0);
122 END_JIT_CALL();
123 }
124
125
126 /* Shade a 4x4 stamp which may be partially outside the rectangle,
127 * according to the mask parameter.
128 */
129 static inline void
partial(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,unsigned ix,unsigned iy,unsigned mask)130 partial(struct lp_rasterizer_task *task,
131 const struct lp_rast_shader_inputs *inputs,
132 unsigned ix, unsigned iy,
133 unsigned mask)
134 {
135 /* Unfortunately we can end up generating full blocks on this path,
136 * need to catch them.
137 */
138 assert(mask != 0x0);
139 shade_quads(task, inputs, ix * STAMP_SIZE, iy * STAMP_SIZE, mask);
140 }
141
142
143 /**
144 * Run the full SoA shader.
145 */
146 void
lp_rast_linear_rect_fallback(struct lp_rasterizer_task * task,const struct lp_rast_shader_inputs * inputs,const struct u_rect * box)147 lp_rast_linear_rect_fallback(struct lp_rasterizer_task *task,
148 const struct lp_rast_shader_inputs *inputs,
149 const struct u_rect *box)
150 {
151 /* The interior of the rectangle (if there is one) will be
152 * rasterized as full 4x4 stamps.
153 *
154 * At each edge of the rectangle, however, there will be a fringe
155 * of partial blocks where the edge lands somewhere in the middle
156 * of a 4x4-pixel stamp.
157 *
158 * For each edge, precalculate a mask of the pixels inside that
159 * edge for the first 4x4-pixel stamp.
160 *
161 * Note that at the corners, and for narrow rectangles, an
162 * individual stamp may have two or more edges active. We'll deal
163 * with that below by combining these masks as appropriate.
164 */
165 const unsigned left_mask = left_mask_tab [box->x0 & (STAMP_SIZE - 1)];
166 const unsigned right_mask = right_mask_tab [box->x1 & (STAMP_SIZE - 1)];
167 const unsigned top_mask = top_mask_tab [box->y0 & (STAMP_SIZE - 1)];
168 const unsigned bottom_mask = bottom_mask_tab [box->y1 & (STAMP_SIZE - 1)];
169
170 const unsigned ix0 = box->x0 / STAMP_SIZE;
171 const unsigned ix1 = box->x1 / STAMP_SIZE;
172 const unsigned iy0 = box->y0 / STAMP_SIZE;
173 const unsigned iy1 = box->y1 / STAMP_SIZE;
174
175 /* Various special cases.
176 */
177 if (ix0 == ix1 && iy0 == iy1) {
178 /* Rectangle is contained within a single 4x4-pixel stamp:
179 */
180 partial(task, inputs, ix0, iy0,
181 (left_mask & right_mask &
182 top_mask & bottom_mask));
183 }
184 else if (ix0 == ix1) {
185 /* Left and right edges fall on the same 4-pixel-wide column:
186 */
187 unsigned mask = left_mask & right_mask;
188 partial(task, inputs, ix0, iy0, mask & top_mask);
189 for (unsigned i = iy0 + 1; i < iy1; i++)
190 partial(task, inputs, ix0, i, mask);
191 partial(task, inputs, ix0, iy1, mask & bottom_mask);
192 }
193 else if (iy0 == iy1) {
194 /* Top and bottom edges fall on the same 4-pixel-wide row:
195 */
196 unsigned mask = top_mask & bottom_mask;
197 partial(task, inputs, ix0, iy0, mask & left_mask);
198 for (unsigned i = ix0 + 1; i < ix1; i++)
199 partial(task, inputs, i, iy0, mask);
200 partial(task, inputs, ix1, iy0, mask & right_mask);
201 }
202 else {
203 /* Each pair of edges falls in a separate 4-pixel-wide
204 * row/column.
205 */
206 partial(task, inputs, ix0, iy0, left_mask & top_mask);
207 partial(task, inputs, ix0, iy1, left_mask & bottom_mask);
208 partial(task, inputs, ix1, iy0, right_mask & top_mask);
209 partial(task, inputs, ix1, iy1, right_mask & bottom_mask);
210
211 for (unsigned i = ix0 + 1; i < ix1; i++)
212 partial(task, inputs, i, iy0, top_mask);
213
214 for (unsigned i = ix0 + 1; i < ix1; i++)
215 partial(task, inputs, i, iy1, bottom_mask);
216
217 for (unsigned i = iy0 + 1; i < iy1; i++)
218 partial(task, inputs, ix0, i, left_mask);
219
220 for (unsigned i = iy0 + 1; i < iy1; i++)
221 partial(task, inputs, ix1, i, right_mask);
222
223 /* Full interior blocks
224 */
225 for (unsigned j = iy0 + 1; j < iy1; j++) {
226 for (unsigned i = ix0 + 1; i < ix1; i++) {
227 shade_quads(task, inputs, i * STAMP_SIZE, j * STAMP_SIZE, 0xffff);
228 }
229 }
230 }
231 }
232