gallium/llvmpipe: remove duplicated include
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup_point.c
1 /**************************************************************************
2 *
3 * Copyright 2010, 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 * Binning code for points
30 */
31
32 #include "lp_setup_context.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "lp_perf.h"
36 #include "lp_rast.h"
37 #include "lp_state_fs.h"
38 #include "tgsi/tgsi_scan.h"
39
40 #define NUM_CHANNELS 4
41
42 struct point_info {
43 /* x,y deltas */
44 int dy01, dy12;
45 int dx01, dx12;
46
47 const float (*v0)[4];
48 };
49
50
51 /**
52 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
53 */
54 static void
55 constant_coef(struct lp_setup_context *setup,
56 struct lp_rast_triangle *point,
57 unsigned slot,
58 const float value,
59 unsigned i)
60 {
61 point->inputs.a0[slot][i] = value;
62 point->inputs.dadx[slot][i] = 0.0f;
63 point->inputs.dady[slot][i] = 0.0f;
64 }
65
66
67 /**
68 * Setup automatic texcoord coefficients (for sprite rendering).
69 * \param slot the vertex attribute slot to setup
70 * \param i the attribute channel in [0,3]
71 * \param sprite_coord_origin one of PIPE_SPRITE_COORD_x
72 * \param perspective_proj will the TEX instruction do a divide by Q?
73 */
74 static void
75 texcoord_coef(struct lp_setup_context *setup,
76 struct lp_rast_triangle *point,
77 const struct point_info *info,
78 unsigned slot,
79 unsigned i,
80 unsigned sprite_coord_origin,
81 boolean perspective_proj)
82 {
83 assert(i < 4);
84
85 if (i == 0) {
86 float dadx = FIXED_ONE / (float)info->dx12;
87 float dady = 0.0f;
88 float x0 = info->v0[0][0] - setup->pixel_offset;
89 float y0 = info->v0[0][1] - setup->pixel_offset;
90
91 point->inputs.dadx[slot][0] = dadx;
92 point->inputs.dady[slot][0] = dady;
93 point->inputs.a0[slot][0] = 0.5 - (dadx * x0 + dady * y0);
94
95 if (!perspective_proj) {
96 /* Divide coefficients by vertex.w here.
97 *
98 * It would be clearer to always multiply by w0 above and
99 * then divide it out for perspective projection here, but
100 * doing it this way involves less algebra.
101 */
102 float w0 = info->v0[0][3];
103 point->inputs.dadx[slot][0] *= w0;
104 point->inputs.dady[slot][0] *= w0;
105 point->inputs.a0[slot][0] *= w0;
106 }
107 }
108 else if (i == 1) {
109 float dadx = 0.0f;
110 float dady = FIXED_ONE / (float)info->dx12;
111 float x0 = info->v0[0][0] - setup->pixel_offset;
112 float y0 = info->v0[0][1] - setup->pixel_offset;
113
114 if (sprite_coord_origin == PIPE_SPRITE_COORD_LOWER_LEFT) {
115 dady = -dady;
116 }
117
118 point->inputs.dadx[slot][1] = dadx;
119 point->inputs.dady[slot][1] = dady;
120 point->inputs.a0[slot][1] = 0.5 - (dadx * x0 + dady * y0);
121
122 if (!perspective_proj) {
123 float w0 = info->v0[0][3];
124 point->inputs.dadx[slot][1] *= w0;
125 point->inputs.dady[slot][1] *= w0;
126 point->inputs.a0[slot][1] *= w0;
127 }
128 }
129 else if (i == 2) {
130 point->inputs.a0[slot][2] = 0.0f;
131 point->inputs.dadx[slot][2] = 0.0f;
132 point->inputs.dady[slot][2] = 0.0f;
133 }
134 else {
135 point->inputs.a0[slot][3] = 1.0f;
136 point->inputs.dadx[slot][3] = 0.0f;
137 point->inputs.dady[slot][3] = 0.0f;
138 }
139 }
140
141
142 /**
143 * Special coefficient setup for gl_FragCoord.
144 * X and Y are trivial
145 * Z and W are copied from position_coef which should have already been computed.
146 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
147 */
148 static void
149 setup_point_fragcoord_coef(struct lp_setup_context *setup,
150 struct lp_rast_triangle *point,
151 const struct point_info *info,
152 unsigned slot,
153 unsigned usage_mask)
154 {
155 /*X*/
156 if (usage_mask & TGSI_WRITEMASK_X) {
157 point->inputs.a0[slot][0] = 0.0;
158 point->inputs.dadx[slot][0] = 1.0;
159 point->inputs.dady[slot][0] = 0.0;
160 }
161
162 /*Y*/
163 if (usage_mask & TGSI_WRITEMASK_Y) {
164 point->inputs.a0[slot][1] = 0.0;
165 point->inputs.dadx[slot][1] = 0.0;
166 point->inputs.dady[slot][1] = 1.0;
167 }
168
169 /*Z*/
170 if (usage_mask & TGSI_WRITEMASK_Z) {
171 constant_coef(setup, point, slot, info->v0[0][2], 2);
172 }
173
174 /*W*/
175 if (usage_mask & TGSI_WRITEMASK_W) {
176 constant_coef(setup, point, slot, info->v0[0][3], 3);
177 }
178 }
179
180
181 /**
182 * Compute the point->coef[] array dadx, dady, a0 values.
183 */
184 static void
185 setup_point_coefficients( struct lp_setup_context *setup,
186 struct lp_rast_triangle *point,
187 const struct point_info *info)
188 {
189 const struct lp_fragment_shader *shader = setup->fs.current.variant->shader;
190 unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
191 unsigned slot;
192
193 /* setup interpolation for all the remaining attributes:
194 */
195 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
196 unsigned vert_attr = setup->fs.input[slot].src_index;
197 unsigned usage_mask = setup->fs.input[slot].usage_mask;
198 unsigned i;
199
200 switch (setup->fs.input[slot].interp) {
201 case LP_INTERP_POSITION:
202 /*
203 * The generated pixel interpolators will pick up the coeffs from
204 * slot 0, so all need to ensure that the usage mask is covers all
205 * usages.
206 */
207 fragcoord_usage_mask |= usage_mask;
208 break;
209
210 case LP_INTERP_LINEAR:
211 /* Sprite tex coords may use linear interpolation someday */
212 /* fall-through */
213
214 case LP_INTERP_PERSPECTIVE:
215 /* check if the sprite coord flag is set for this attribute.
216 * If so, set it up so it up so x and y vary from 0 to 1.
217 */
218 if (shader->info.input_semantic_name[slot] == TGSI_SEMANTIC_GENERIC) {
219 const int index = shader->info.input_semantic_index[slot];
220 /* Note that sprite_coord enable is a bitfield of
221 * PIPE_MAX_SHADER_OUTPUTS bits.
222 */
223 if (index < PIPE_MAX_SHADER_OUTPUTS &&
224 (setup->sprite_coord_enable & (1 << index))) {
225 for (i = 0; i < NUM_CHANNELS; i++)
226 if (usage_mask & (1 << i))
227 texcoord_coef(setup, point, info, slot + 1, i,
228 setup->sprite_coord_origin,
229 (usage_mask & TGSI_WRITEMASK_W));
230 fragcoord_usage_mask |= TGSI_WRITEMASK_W;
231 break;
232 }
233 }
234 /* FALLTHROUGH */
235 case LP_INTERP_CONSTANT:
236 for (i = 0; i < NUM_CHANNELS; i++) {
237 if (usage_mask & (1 << i))
238 constant_coef(setup, point, slot+1, info->v0[vert_attr][i], i);
239 }
240 break;
241
242 case LP_INTERP_FACING:
243 for (i = 0; i < NUM_CHANNELS; i++)
244 if (usage_mask & (1 << i))
245 constant_coef(setup, point, slot+1, 1.0, i);
246 break;
247
248 default:
249 assert(0);
250 break;
251 }
252 }
253
254 /* The internal position input is in slot zero:
255 */
256 setup_point_fragcoord_coef(setup, point, info, 0,
257 fragcoord_usage_mask);
258 }
259
260
261 static INLINE int
262 subpixel_snap(float a)
263 {
264 return util_iround(FIXED_ONE * a);
265 }
266
267
268 static boolean
269 try_setup_point( struct lp_setup_context *setup,
270 const float (*v0)[4] )
271 {
272 /* x/y positions in fixed point */
273 const int sizeAttr = setup->psize;
274 const float size
275 = (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]
276 : setup->point_size;
277
278 /* Point size as fixed point integer, remove rounding errors
279 * and gives minimum width for very small points
280 */
281 int fixed_width = MAX2(FIXED_ONE,
282 (subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));
283
284 const int x0 = subpixel_snap(v0[0][0] - setup->pixel_offset) - fixed_width/2;
285 const int y0 = subpixel_snap(v0[0][1] - setup->pixel_offset) - fixed_width/2;
286
287 struct lp_scene *scene = setup->scene;
288 struct lp_rast_triangle *point;
289 unsigned bytes;
290 struct u_rect bbox;
291 unsigned nr_planes = 4;
292 struct point_info info;
293
294
295 /* Bounding rectangle (in pixels) */
296 {
297 /* Yes this is necessary to accurately calculate bounding boxes
298 * with the two fill-conventions we support. GL (normally) ends
299 * up needing a bottom-left fill convention, which requires
300 * slightly different rounding.
301 */
302 int adj = (setup->pixel_offset != 0) ? 1 : 0;
303
304 bbox.x0 = (x0 + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
305 bbox.x1 = (x0 + fixed_width + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
306 bbox.y0 = (y0 + (FIXED_ONE-1)) >> FIXED_ORDER;
307 bbox.y1 = (y0 + fixed_width + (FIXED_ONE-1)) >> FIXED_ORDER;
308
309 /* Inclusive coordinates:
310 */
311 bbox.x1--;
312 bbox.y1--;
313 }
314
315 if (!u_rect_test_intersection(&setup->draw_region, &bbox)) {
316 if (0) debug_printf("offscreen\n");
317 LP_COUNT(nr_culled_tris);
318 return TRUE;
319 }
320
321 u_rect_find_intersection(&setup->draw_region, &bbox);
322
323 point = lp_setup_alloc_triangle(scene,
324 setup->fs.nr_inputs,
325 nr_planes,
326 &bytes);
327 if (!point)
328 return FALSE;
329
330 #ifdef DEBUG
331 point->v[0][0] = v0[0][0];
332 point->v[0][1] = v0[0][1];
333 #endif
334
335 info.v0 = v0;
336 info.dx01 = 0;
337 info.dx12 = fixed_width;
338 info.dy01 = fixed_width;
339 info.dy12 = 0;
340
341 /* Setup parameter interpolants:
342 */
343 setup_point_coefficients(setup, point, &info);
344
345 point->inputs.facing = 1.0F;
346 point->inputs.state = setup->fs.stored;
347 point->inputs.disable = FALSE;
348 point->inputs.opaque = FALSE;
349
350 {
351 point->plane[0].dcdx = -1;
352 point->plane[0].dcdy = 0;
353 point->plane[0].c = 1-bbox.x0;
354 point->plane[0].ei = 0;
355 point->plane[0].eo = 1;
356
357 point->plane[1].dcdx = 1;
358 point->plane[1].dcdy = 0;
359 point->plane[1].c = bbox.x1+1;
360 point->plane[1].ei = -1;
361 point->plane[1].eo = 0;
362
363 point->plane[2].dcdx = 0;
364 point->plane[2].dcdy = 1;
365 point->plane[2].c = 1-bbox.y0;
366 point->plane[2].ei = 0;
367 point->plane[2].eo = 1;
368
369 point->plane[3].dcdx = 0;
370 point->plane[3].dcdy = -1;
371 point->plane[3].c = bbox.y1+1;
372 point->plane[3].ei = -1;
373 point->plane[3].eo = 0;
374 }
375
376 return lp_setup_bin_triangle(setup, point, &bbox, nr_planes);
377 }
378
379
380 static void
381 lp_setup_point(struct lp_setup_context *setup,
382 const float (*v0)[4])
383 {
384 if (!try_setup_point( setup, v0 ))
385 {
386 lp_setup_flush_and_restart(setup);
387
388 if (!try_setup_point( setup, v0 ))
389 assert(0);
390 }
391 }
392
393
394 void
395 lp_setup_choose_point( struct lp_setup_context *setup )
396 {
397 setup->point = lp_setup_point;
398 }
399
400