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