1 /**************************************************************************
3 * Copyright 2010, VMware Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Binning code for points
32 #include "lp_setup_context.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
36 #include "lp_setup_context.h"
38 #include "lp_state_fs.h"
39 #include "tgsi/tgsi_scan.h"
41 #define NUM_CHANNELS 4
53 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
56 constant_coef(struct lp_setup_context
*setup
,
57 struct lp_rast_triangle
*point
,
62 point
->inputs
.a0
[slot
][i
] = value
;
63 point
->inputs
.dadx
[slot
][i
] = 0.0f
;
64 point
->inputs
.dady
[slot
][i
] = 0.0f
;
69 perspective_coef(struct lp_setup_context
*setup
,
70 struct lp_rast_triangle
*point
,
71 const struct point_info
*info
,
77 float dadx
= FIXED_ONE
/ (float)info
->dx12
;
79 point
->inputs
.dadx
[slot
][i
] = dadx
;
80 point
->inputs
.dady
[slot
][i
] = dady
;
81 point
->inputs
.a0
[slot
][i
] = (0.5 -
82 (dadx
* ((float)info
->v0
[0][0] - setup
->pixel_offset
) +
83 dady
* ((float)info
->v0
[0][1] - setup
->pixel_offset
)));
87 float dady
= FIXED_ONE
/ (float)info
->dx12
;
89 point
->inputs
.dadx
[slot
][i
] = dadx
;
90 point
->inputs
.dady
[slot
][i
] = dady
;
91 point
->inputs
.a0
[slot
][i
] = (0.5 -
92 (dadx
* ((float)info
->v0
[0][0] - setup
->pixel_offset
) +
93 dady
* ((float)info
->v0
[0][1] - setup
->pixel_offset
)));
96 point
->inputs
.a0
[slot
][i
] = 0.0f
;
97 point
->inputs
.dadx
[slot
][i
] = 0.0f
;
98 point
->inputs
.dady
[slot
][i
] = 0.0f
;
101 point
->inputs
.a0
[slot
][i
] = 1.0f
;
102 point
->inputs
.dadx
[slot
][i
] = 0.0f
;
103 point
->inputs
.dady
[slot
][i
] = 0.0f
;
109 * Special coefficient setup for gl_FragCoord.
110 * X and Y are trivial
111 * Z and W are copied from position_coef which should have already been computed.
112 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
115 setup_point_fragcoord_coef(struct lp_setup_context
*setup
,
116 struct lp_rast_triangle
*point
,
117 const struct point_info
*info
,
122 if (usage_mask
& TGSI_WRITEMASK_X
) {
123 point
->inputs
.a0
[slot
][0] = 0.0;
124 point
->inputs
.dadx
[slot
][0] = 1.0;
125 point
->inputs
.dady
[slot
][0] = 0.0;
129 if (usage_mask
& TGSI_WRITEMASK_Y
) {
130 point
->inputs
.a0
[slot
][1] = 0.0;
131 point
->inputs
.dadx
[slot
][1] = 0.0;
132 point
->inputs
.dady
[slot
][1] = 1.0;
136 if (usage_mask
& TGSI_WRITEMASK_Z
) {
137 constant_coef(setup
, point
, slot
, info
->v0
[0][2], 2);
141 if (usage_mask
& TGSI_WRITEMASK_W
) {
142 constant_coef(setup
, point
, slot
, info
->v0
[0][3], 3);
148 * Compute the point->coef[] array dadx, dady, a0 values.
151 setup_point_coefficients( struct lp_setup_context
*setup
,
152 struct lp_rast_triangle
*point
,
153 const struct point_info
*info
)
155 const struct lp_fragment_shader
*shader
= setup
->fs
.current
.variant
->shader
;
156 unsigned fragcoord_usage_mask
= TGSI_WRITEMASK_XYZ
;
159 /* setup interpolation for all the remaining attributes:
161 for (slot
= 0; slot
< setup
->fs
.nr_inputs
; slot
++) {
162 unsigned vert_attr
= setup
->fs
.input
[slot
].src_index
;
163 unsigned usage_mask
= setup
->fs
.input
[slot
].usage_mask
;
166 switch (setup
->fs
.input
[slot
].interp
) {
167 case LP_INTERP_POSITION
:
169 * The generated pixel interpolators will pick up the coeffs from
170 * slot 0, so all need to ensure that the usage mask is covers all
173 fragcoord_usage_mask
|= usage_mask
;
176 case LP_INTERP_LINEAR
:
177 /* Sprite tex coords may use linear interpolation someday */
180 case LP_INTERP_PERSPECTIVE
:
181 /* check if the sprite coord flag is set for this attribute.
182 * If so, set it up so it up so x any y vary from 0 to 1.
184 if (shader
->info
.input_semantic_name
[slot
] == TGSI_SEMANTIC_GENERIC
) {
185 const int index
= shader
->info
.input_semantic_index
[slot
];
187 if (setup
->sprite
& (1 << index
)) {
188 for (i
= 0; i
< NUM_CHANNELS
; i
++)
189 if (usage_mask
& (1 << i
))
190 perspective_coef(setup
, point
, info
, slot
+1, vert_attr
, i
);
191 fragcoord_usage_mask
|= TGSI_WRITEMASK_W
;
196 /* Otherwise fallthrough */
198 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
199 if (usage_mask
& (1 << i
))
200 constant_coef(setup
, point
, slot
+1, info
->v0
[vert_attr
][i
], i
);
205 /* The internal position input is in slot zero:
207 setup_point_fragcoord_coef(setup
, point
, info
, 0,
208 fragcoord_usage_mask
);
213 subpixel_snap(float a
)
215 return util_iround(FIXED_ONE
* a
);
220 try_setup_point( struct lp_setup_context
*setup
,
221 const float (*v0
)[4] )
223 /* x/y positions in fixed point */
224 const int sizeAttr
= setup
->psize
;
226 = (setup
->point_size_per_vertex
&& sizeAttr
> 0) ? v0
[sizeAttr
][0]
229 /* Point size as fixed point integer, remove rounding errors
230 * and gives minimum width for very small points
232 int fixed_width
= MAX2(FIXED_ONE
,
233 (subpixel_snap(size
) + FIXED_ONE
/2 - 1) & ~(FIXED_ONE
-1));
235 const int x0
= subpixel_snap(v0
[0][0] - setup
->pixel_offset
) - fixed_width
/2;
236 const int y0
= subpixel_snap(v0
[0][1] - setup
->pixel_offset
) - fixed_width
/2;
238 struct lp_scene
*scene
= setup
->scene
;
239 struct lp_rast_triangle
*point
;
242 unsigned nr_planes
= 4;
243 struct point_info info
;
246 /* Bounding rectangle (in pixels) */
248 /* Yes this is necessary to accurately calculate bounding boxes
249 * with the two fill-conventions we support. GL (normally) ends
250 * up needing a bottom-left fill convention, which requires
251 * slightly different rounding.
253 int adj
= (setup
->pixel_offset
!= 0) ? 1 : 0;
255 bbox
.x0
= (x0
+ (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
256 bbox
.x1
= (x0
+ fixed_width
+ (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
257 bbox
.y0
= (y0
+ (FIXED_ONE
-1)) >> FIXED_ORDER
;
258 bbox
.y1
= (y0
+ fixed_width
+ (FIXED_ONE
-1)) >> FIXED_ORDER
;
260 /* Inclusive coordinates:
266 if (!u_rect_test_intersection(&setup
->draw_region
, &bbox
)) {
267 if (0) debug_printf("offscreen\n");
268 LP_COUNT(nr_culled_tris
);
272 u_rect_find_intersection(&setup
->draw_region
, &bbox
);
274 point
= lp_setup_alloc_triangle(scene
,
282 point
->v
[0][0] = v0
[0][0];
283 point
->v
[0][1] = v0
[0][1];
288 info
.dx12
= fixed_width
;
289 info
.dy01
= fixed_width
;
292 /* Setup parameter interpolants:
294 setup_point_coefficients(setup
, point
, &info
);
296 point
->inputs
.facing
= 1.0F
;
297 point
->inputs
.state
= setup
->fs
.stored
;
298 point
->inputs
.disable
= FALSE
;
299 point
->inputs
.opaque
= FALSE
;
302 point
->plane
[0].dcdx
= -1;
303 point
->plane
[0].dcdy
= 0;
304 point
->plane
[0].c
= 1-bbox
.x0
;
305 point
->plane
[0].ei
= 0;
306 point
->plane
[0].eo
= 1;
308 point
->plane
[1].dcdx
= 1;
309 point
->plane
[1].dcdy
= 0;
310 point
->plane
[1].c
= bbox
.x1
+1;
311 point
->plane
[1].ei
= -1;
312 point
->plane
[1].eo
= 0;
314 point
->plane
[2].dcdx
= 0;
315 point
->plane
[2].dcdy
= 1;
316 point
->plane
[2].c
= 1-bbox
.y0
;
317 point
->plane
[2].ei
= 0;
318 point
->plane
[2].eo
= 1;
320 point
->plane
[3].dcdx
= 0;
321 point
->plane
[3].dcdy
= -1;
322 point
->plane
[3].c
= bbox
.y1
+1;
323 point
->plane
[3].ei
= -1;
324 point
->plane
[3].eo
= 0;
327 return lp_setup_bin_triangle(setup
, point
, &bbox
, nr_planes
);
332 lp_setup_point(struct lp_setup_context
*setup
,
333 const float (*v0
)[4])
335 if (!try_setup_point( setup
, v0
))
337 lp_setup_flush_and_restart(setup
);
339 if (!try_setup_point( setup
, v0
))
346 lp_setup_choose_point( struct lp_setup_context
*setup
)
348 setup
->point
= lp_setup_point
;