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