Merge remote branch 'origin/master' into nv50-compiler
[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 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 static void perspective_coef( struct lp_setup_context *setup,
67 struct lp_rast_triangle *point,
68 const struct point_info *info,
69 unsigned slot,
70 unsigned vert_attr,
71 unsigned i)
72 {
73 if (i == 0) {
74 float dadx = FIXED_ONE / (float)info->dx12;
75 float dady = 0.0f;
76 point->inputs.dadx[slot][i] = dadx;
77 point->inputs.dady[slot][i] = dady;
78 point->inputs.a0[slot][i] = (0.5 -
79 (dadx * ((float)info->v0[0][0] - setup->pixel_offset) +
80 dady * ((float)info->v0[0][1] - setup->pixel_offset)));
81 }
82
83 else if (i == 1) {
84 float dadx = 0.0f;
85 float dady = FIXED_ONE / (float)info->dx12;
86
87 point->inputs.dadx[slot][i] = dadx;
88 point->inputs.dady[slot][i] = dady;
89 point->inputs.a0[slot][i] = (0.5 -
90 (dadx * ((float)info->v0[0][0] - setup->pixel_offset) +
91 dady * ((float)info->v0[0][1] - setup->pixel_offset)));
92 }
93
94 else if (i == 2) {
95 point->inputs.a0[slot][i] = 0.0f;
96 point->inputs.dadx[slot][i] = 0.0f;
97 point->inputs.dady[slot][i] = 0.0f;
98 }
99
100 else if (i == 3) {
101 point->inputs.a0[slot][i] = 1.0f;
102 point->inputs.dadx[slot][i] = 0.0f;
103 point->inputs.dady[slot][i] = 0.0f;
104 }
105
106 }
107
108
109 /**
110 * Special coefficient setup for gl_FragCoord.
111 * X and Y are trivial
112 * Z and W are copied from position_coef which should have already been computed.
113 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
114 */
115 static void
116 setup_point_fragcoord_coef(struct lp_setup_context *setup,
117 struct lp_rast_triangle *point,
118 const struct point_info *info,
119 unsigned slot,
120 unsigned usage_mask)
121 {
122 /*X*/
123 if (usage_mask & TGSI_WRITEMASK_X) {
124 point->inputs.a0[slot][0] = 0.0;
125 point->inputs.dadx[slot][0] = 1.0;
126 point->inputs.dady[slot][0] = 0.0;
127 }
128
129 /*Y*/
130 if (usage_mask & TGSI_WRITEMASK_Y) {
131 point->inputs.a0[slot][1] = 0.0;
132 point->inputs.dadx[slot][1] = 0.0;
133 point->inputs.dady[slot][1] = 1.0;
134 }
135
136 /*Z*/
137 if (usage_mask & TGSI_WRITEMASK_Z) {
138 constant_coef(setup, point, slot, info->v0[0][2], 2);
139 }
140
141 /*W*/
142 if (usage_mask & TGSI_WRITEMASK_W) {
143 constant_coef(setup, point, slot, info->v0[0][3], 3);
144 }
145 }
146
147 /**
148 * Compute the point->coef[] array dadx, dady, a0 values.
149 */
150 static void
151 setup_point_coefficients( struct lp_setup_context *setup,
152 struct lp_rast_triangle *point,
153 const struct point_info *info)
154 {
155 unsigned fragcoord_usage_mask = TGSI_WRITEMASK_XYZ;
156 unsigned slot;
157
158 /* setup interpolation for all the remaining attributes:
159 */
160 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
161 unsigned vert_attr = setup->fs.input[slot].src_index;
162 unsigned usage_mask = setup->fs.input[slot].usage_mask;
163 unsigned i;
164
165 switch (setup->fs.input[slot].interp) {
166 case LP_INTERP_POSITION:
167 /*
168 * The generated pixel interpolators will pick up the coeffs from
169 * slot 0, so all need to ensure that the usage mask is covers all
170 * usages.
171 */
172 fragcoord_usage_mask |= usage_mask;
173 break;
174
175 case LP_INTERP_PERSPECTIVE:
176 /* For point sprite textures */
177 if (setup->fs.current.variant->shader->info.input_semantic_name[slot]
178 == TGSI_SEMANTIC_GENERIC)
179 {
180 int index = setup->fs.current.variant->shader->info.input_semantic_index[slot];
181
182 if (setup->sprite & (1 << index)) {
183 for (i = 0; i < NUM_CHANNELS; i++)
184 if (usage_mask & (1 << i))
185 perspective_coef(setup, point, info, slot+1, vert_attr, i);
186 fragcoord_usage_mask |= TGSI_WRITEMASK_W;
187 break;
188 }
189 }
190
191 /* Otherwise fallthrough */
192 default:
193 for (i = 0; i < NUM_CHANNELS; i++) {
194 if (usage_mask & (1 << i))
195 constant_coef(setup, point, slot+1, info->v0[vert_attr][i], i);
196 }
197 }
198 }
199
200 /* The internal position input is in slot zero:
201 */
202 setup_point_fragcoord_coef(setup, point, info, 0,
203 fragcoord_usage_mask);
204 }
205
206 static INLINE int
207 subpixel_snap(float a)
208 {
209 return util_iround(FIXED_ONE * a);
210 }
211
212
213 static void lp_setup_point( struct lp_setup_context *setup,
214 const float (*v0)[4] )
215 {
216 /* x/y positions in fixed point */
217 const int sizeAttr = setup->psize;
218 const float size
219 = (setup->point_size_per_vertex && sizeAttr > 0) ? v0[sizeAttr][0]
220 : setup->point_size;
221
222 /* Point size as fixed point integer, remove rounding errors
223 * and gives minimum width for very small points
224 */
225 int fixed_width = MAX2(FIXED_ONE,
226 (subpixel_snap(size) + FIXED_ONE/2 - 1) & ~(FIXED_ONE-1));
227
228 const int x0 = subpixel_snap(v0[0][0] - setup->pixel_offset) - fixed_width/2;
229 const int y0 = subpixel_snap(v0[0][1] - setup->pixel_offset) - fixed_width/2;
230
231 struct lp_scene *scene = lp_setup_get_current_scene(setup);
232 struct lp_rast_triangle *point;
233 unsigned bytes;
234 struct u_rect bbox;
235 unsigned nr_planes = 4;
236 struct point_info info;
237
238
239 /* Bounding rectangle (in pixels) */
240 {
241 /* Yes this is necessary to accurately calculate bounding boxes
242 * with the two fill-conventions we support. GL (normally) ends
243 * up needing a bottom-left fill convention, which requires
244 * slightly different rounding.
245 */
246 int adj = (setup->pixel_offset != 0) ? 1 : 0;
247
248 bbox.x0 = (x0 + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
249 bbox.x1 = (x0 + fixed_width + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
250 bbox.y0 = (y0 + (FIXED_ONE-1)) >> FIXED_ORDER;
251 bbox.y1 = (y0 + fixed_width + (FIXED_ONE-1)) >> FIXED_ORDER;
252
253 /* Inclusive coordinates:
254 */
255 bbox.x1--;
256 bbox.y1--;
257 }
258
259 if (!u_rect_test_intersection(&setup->draw_region, &bbox)) {
260 if (0) debug_printf("offscreen\n");
261 LP_COUNT(nr_culled_tris);
262 return;
263 }
264
265 u_rect_find_intersection(&setup->draw_region, &bbox);
266
267 point = lp_setup_alloc_triangle(scene,
268 setup->fs.nr_inputs,
269 nr_planes,
270 &bytes);
271 if (!point)
272 return;
273
274 #ifdef DEBUG
275 point->v[0][0] = v0[0][0];
276 point->v[0][1] = v0[0][1];
277 #endif
278
279 info.v0 = v0;
280 info.dx01 = 0;
281 info.dx12 = fixed_width;
282 info.dy01 = fixed_width;
283 info.dy12 = 0;
284
285 /* Setup parameter interpolants:
286 */
287 setup_point_coefficients(setup, point, &info);
288
289 point->inputs.facing = 1.0F;
290 point->inputs.state = setup->fs.stored;
291
292 {
293 point->plane[0].dcdx = -1;
294 point->plane[0].dcdy = 0;
295 point->plane[0].c = 1-bbox.x0;
296 point->plane[0].ei = 0;
297 point->plane[0].eo = 1;
298
299 point->plane[1].dcdx = 1;
300 point->plane[1].dcdy = 0;
301 point->plane[1].c = bbox.x1+1;
302 point->plane[1].ei = -1;
303 point->plane[1].eo = 0;
304
305 point->plane[2].dcdx = 0;
306 point->plane[2].dcdy = 1;
307 point->plane[2].c = 1-bbox.y0;
308 point->plane[2].ei = 0;
309 point->plane[2].eo = 1;
310
311 point->plane[3].dcdx = 0;
312 point->plane[3].dcdy = -1;
313 point->plane[3].c = bbox.y1+1;
314 point->plane[3].ei = -1;
315 point->plane[3].eo = 0;
316 }
317
318 lp_setup_bin_triangle(setup, point, &bbox, nr_planes);
319 }
320
321
322 void
323 lp_setup_choose_point( struct lp_setup_context *setup )
324 {
325 setup->point = lp_setup_point;
326 }
327
328