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"
37 #include "lp_state_fs.h"
38 #include "tgsi/tgsi_scan.h"
40 #define NUM_CHANNELS 4
52 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
55 constant_coef(struct lp_setup_context
*setup
,
56 struct lp_rast_triangle
*point
,
61 point
->inputs
.a0
[slot
][i
] = value
;
62 point
->inputs
.dadx
[slot
][i
] = 0.0f
;
63 point
->inputs
.dady
[slot
][i
] = 0.0f
;
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?
75 texcoord_coef(struct lp_setup_context
*setup
,
76 struct lp_rast_triangle
*point
,
77 const struct point_info
*info
,
80 unsigned sprite_coord_origin
,
81 boolean perspective_proj
)
86 float dadx
= FIXED_ONE
/ (float)info
->dx12
;
88 float x0
= info
->v0
[0][0] - setup
->pixel_offset
;
89 float y0
= info
->v0
[0][1] - setup
->pixel_offset
;
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
);
95 if (!perspective_proj
) {
96 /* Divide coefficients by vertex.w here.
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.
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
;
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
;
114 if (sprite_coord_origin
== PIPE_SPRITE_COORD_LOWER_LEFT
) {
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
);
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
;
130 point
->inputs
.a0
[slot
][2] = 0.0f
;
131 point
->inputs
.dadx
[slot
][2] = 0.0f
;
132 point
->inputs
.dady
[slot
][2] = 0.0f
;
135 point
->inputs
.a0
[slot
][3] = 1.0f
;
136 point
->inputs
.dadx
[slot
][3] = 0.0f
;
137 point
->inputs
.dady
[slot
][3] = 0.0f
;
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.
149 setup_point_fragcoord_coef(struct lp_setup_context
*setup
,
150 struct lp_rast_triangle
*point
,
151 const struct point_info
*info
,
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;
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;
170 if (usage_mask
& TGSI_WRITEMASK_Z
) {
171 constant_coef(setup
, point
, slot
, info
->v0
[0][2], 2);
175 if (usage_mask
& TGSI_WRITEMASK_W
) {
176 constant_coef(setup
, point
, slot
, info
->v0
[0][3], 3);
182 * Compute the point->coef[] array dadx, dady, a0 values.
185 setup_point_coefficients( struct lp_setup_context
*setup
,
186 struct lp_rast_triangle
*point
,
187 const struct point_info
*info
)
189 const struct lp_fragment_shader
*shader
= setup
->fs
.current
.variant
->shader
;
190 unsigned fragcoord_usage_mask
= TGSI_WRITEMASK_XYZ
;
193 /* setup interpolation for all the remaining attributes:
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
;
200 switch (setup
->fs
.input
[slot
].interp
) {
201 case LP_INTERP_POSITION
:
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
207 fragcoord_usage_mask
|= usage_mask
;
210 case LP_INTERP_LINEAR
:
211 /* Sprite tex coords may use linear interpolation someday */
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.
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.
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
;
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
);
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
);
254 /* The internal position input is in slot zero:
256 setup_point_fragcoord_coef(setup
, point
, info
, 0,
257 fragcoord_usage_mask
);
262 subpixel_snap(float a
)
264 return util_iround(FIXED_ONE
* a
);
269 try_setup_point( struct lp_setup_context
*setup
,
270 const float (*v0
)[4] )
272 /* x/y positions in fixed point */
273 const int sizeAttr
= setup
->psize
;
275 = (setup
->point_size_per_vertex
&& sizeAttr
> 0) ? v0
[sizeAttr
][0]
278 /* Point size as fixed point integer, remove rounding errors
279 * and gives minimum width for very small points
281 int fixed_width
= MAX2(FIXED_ONE
,
282 (subpixel_snap(size
) + FIXED_ONE
/2 - 1) & ~(FIXED_ONE
-1));
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;
287 struct lp_scene
*scene
= setup
->scene
;
288 struct lp_rast_triangle
*point
;
291 unsigned nr_planes
= 4;
292 struct point_info info
;
295 /* Bounding rectangle (in pixels) */
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.
302 int adj
= (setup
->pixel_offset
!= 0) ? 1 : 0;
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
;
309 /* Inclusive coordinates:
315 if (!u_rect_test_intersection(&setup
->draw_region
, &bbox
)) {
316 if (0) debug_printf("offscreen\n");
317 LP_COUNT(nr_culled_tris
);
321 u_rect_find_intersection(&setup
->draw_region
, &bbox
);
323 point
= lp_setup_alloc_triangle(scene
,
331 point
->v
[0][0] = v0
[0][0];
332 point
->v
[0][1] = v0
[0][1];
337 info
.dx12
= fixed_width
;
338 info
.dy01
= fixed_width
;
341 /* Setup parameter interpolants:
343 setup_point_coefficients(setup
, point
, &info
);
345 point
->inputs
.facing
= 1.0F
;
346 point
->inputs
.state
= setup
->fs
.stored
;
347 point
->inputs
.disable
= FALSE
;
348 point
->inputs
.opaque
= FALSE
;
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;
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;
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;
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;
376 return lp_setup_bin_triangle(setup
, point
, &bbox
, nr_planes
);
381 lp_setup_point(struct lp_setup_context
*setup
,
382 const float (*v0
)[4])
384 if (!try_setup_point( setup
, v0
))
386 lp_setup_flush_and_restart(setup
);
388 if (!try_setup_point( setup
, v0
))
395 lp_setup_choose_point( struct lp_setup_context
*setup
)
397 setup
->point
= lp_setup_point
;