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 "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "lp_setup_context.h"
37 #include "lp_state_fs.h"
38 #include "lp_state_setup.h"
39 #include "lp_context.h"
40 #include "tgsi/tgsi_scan.h"
41 #include "draw/draw_context.h"
43 #define NUM_CHANNELS 4
61 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
64 constant_coef(struct lp_setup_context
*setup
,
65 struct point_info
*info
,
70 info
->a0
[slot
][i
] = value
;
71 info
->dadx
[slot
][i
] = 0.0f
;
72 info
->dady
[slot
][i
] = 0.0f
;
77 point_persp_coeff(struct lp_setup_context
*setup
,
78 const struct point_info
*info
,
83 * Fragment shader expects pre-multiplied w for LP_INTERP_PERSPECTIVE. A
84 * better stratergy would be to take the primitive in consideration when
85 * generating the fragment shader key, and therefore avoid the per-fragment
89 float w0
= info
->v0
[0][3];
93 info
->a0
[slot
][i
] = info
->v0
[slot
][i
]*w0
;
94 info
->dadx
[slot
][i
] = 0.0f
;
95 info
->dady
[slot
][i
] = 0.0f
;
100 * Setup automatic texcoord coefficients (for sprite rendering).
101 * \param slot the vertex attribute slot to setup
102 * \param i the attribute channel in [0,3]
103 * \param sprite_coord_origin one of PIPE_SPRITE_COORD_x
104 * \param perspective does the shader expects pre-multiplied w, i.e.,
105 * LP_INTERP_PERSPECTIVE is specified in the shader key
108 texcoord_coef(struct lp_setup_context
*setup
,
109 const struct point_info
*info
,
112 unsigned sprite_coord_origin
,
115 float w0
= info
->v0
[0][3];
120 float dadx
= FIXED_ONE
/ (float)info
->dx12
;
122 float x0
= info
->v0
[0][0] - setup
->pixel_offset
;
123 float y0
= info
->v0
[0][1] - setup
->pixel_offset
;
125 info
->dadx
[slot
][0] = dadx
;
126 info
->dady
[slot
][0] = dady
;
127 info
->a0
[slot
][0] = 0.5 - (dadx
* x0
+ dady
* y0
);
130 info
->dadx
[slot
][0] *= w0
;
131 info
->dady
[slot
][0] *= w0
;
132 info
->a0
[slot
][0] *= w0
;
137 float dady
= FIXED_ONE
/ (float)info
->dx12
;
138 float x0
= info
->v0
[0][0] - setup
->pixel_offset
;
139 float y0
= info
->v0
[0][1] - setup
->pixel_offset
;
141 if (sprite_coord_origin
== PIPE_SPRITE_COORD_LOWER_LEFT
) {
145 info
->dadx
[slot
][1] = dadx
;
146 info
->dady
[slot
][1] = dady
;
147 info
->a0
[slot
][1] = 0.5 - (dadx
* x0
+ dady
* y0
);
150 info
->dadx
[slot
][1] *= w0
;
151 info
->dady
[slot
][1] *= w0
;
152 info
->a0
[slot
][1] *= w0
;
156 info
->a0
[slot
][2] = 0.0f
;
157 info
->dadx
[slot
][2] = 0.0f
;
158 info
->dady
[slot
][2] = 0.0f
;
161 info
->a0
[slot
][3] = perspective
? w0
: 1.0f
;
162 info
->dadx
[slot
][3] = 0.0f
;
163 info
->dady
[slot
][3] = 0.0f
;
169 * Special coefficient setup for gl_FragCoord.
170 * X and Y are trivial
171 * Z and W are copied from position_coef which should have already been computed.
172 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
175 setup_point_fragcoord_coef(struct lp_setup_context
*setup
,
176 struct point_info
*info
,
181 if (usage_mask
& TGSI_WRITEMASK_X
) {
182 info
->a0
[slot
][0] = 0.0;
183 info
->dadx
[slot
][0] = 1.0;
184 info
->dady
[slot
][0] = 0.0;
188 if (usage_mask
& TGSI_WRITEMASK_Y
) {
189 info
->a0
[slot
][1] = 0.0;
190 info
->dadx
[slot
][1] = 0.0;
191 info
->dady
[slot
][1] = 1.0;
195 if (usage_mask
& TGSI_WRITEMASK_Z
) {
196 constant_coef(setup
, info
, slot
, info
->v0
[0][2], 2);
200 if (usage_mask
& TGSI_WRITEMASK_W
) {
201 constant_coef(setup
, info
, slot
, info
->v0
[0][3], 3);
207 * Compute the point->coef[] array dadx, dady, a0 values.
210 setup_point_coefficients( struct lp_setup_context
*setup
,
211 struct point_info
*info
)
213 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
214 const struct lp_fragment_shader
*shader
= setup
->fs
.current
.variant
->shader
;
215 unsigned fragcoord_usage_mask
= TGSI_WRITEMASK_XYZ
;
218 /* setup interpolation for all the remaining attributes:
220 for (slot
= 0; slot
< key
->num_inputs
; slot
++) {
221 unsigned vert_attr
= key
->inputs
[slot
].src_index
;
222 unsigned usage_mask
= key
->inputs
[slot
].usage_mask
;
223 enum lp_interp interp
= key
->inputs
[slot
].interp
;
224 boolean perspective
= !!(interp
== LP_INTERP_PERSPECTIVE
);
227 if (perspective
& usage_mask
) {
228 fragcoord_usage_mask
|= TGSI_WRITEMASK_W
;
232 case LP_INTERP_POSITION
:
234 * The generated pixel interpolators will pick up the coeffs from
235 * slot 0, so all need to ensure that the usage mask is covers all
238 fragcoord_usage_mask
|= usage_mask
;
241 case LP_INTERP_LINEAR
:
242 /* Sprite tex coords may use linear interpolation someday */
244 case LP_INTERP_PERSPECTIVE
:
245 /* check if the sprite coord flag is set for this attribute.
246 * If so, set it up so it up so x and y vary from 0 to 1.
248 if (shader
->info
.base
.input_semantic_name
[slot
] == TGSI_SEMANTIC_GENERIC
) {
249 unsigned semantic_index
= shader
->info
.base
.input_semantic_index
[slot
];
250 /* Note that sprite_coord enable is a bitfield of
251 * PIPE_MAX_SHADER_OUTPUTS bits.
253 if (semantic_index
< PIPE_MAX_SHADER_OUTPUTS
&&
254 (setup
->sprite_coord_enable
& (1 << semantic_index
))) {
255 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
256 if (usage_mask
& (1 << i
)) {
257 texcoord_coef(setup
, info
, slot
+ 1, i
,
258 setup
->sprite_coord_origin
,
266 case LP_INTERP_CONSTANT
:
267 for (i
= 0; i
< NUM_CHANNELS
; i
++) {
268 if (usage_mask
& (1 << i
)) {
270 point_persp_coeff(setup
, info
, slot
+1, i
);
273 constant_coef(setup
, info
, slot
+1, info
->v0
[vert_attr
][i
], i
);
279 case LP_INTERP_FACING
:
280 for (i
= 0; i
< NUM_CHANNELS
; i
++)
281 if (usage_mask
& (1 << i
))
282 constant_coef(setup
, info
, slot
+1,
283 info
->frontfacing
? 1.0f
: -1.0f
, i
);
292 /* The internal position input is in slot zero:
294 setup_point_fragcoord_coef(setup
, info
, 0,
295 fragcoord_usage_mask
);
300 subpixel_snap(float a
)
302 return util_iround(FIXED_ONE
* a
);
306 * Print point vertex attribs (for debug).
309 print_point(struct lp_setup_context
*setup
,
310 const float (*v0
)[4],
313 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
316 debug_printf("llvmpipe point, width %f\n", size
);
317 for (i
= 0; i
< 1 + key
->num_inputs
; i
++) {
318 debug_printf(" v0[%d]: %f %f %f %f\n", i
,
319 v0
[i
][0], v0
[i
][1], v0
[i
][2], v0
[i
][3]);
325 try_setup_point( struct lp_setup_context
*setup
,
326 const float (*v0
)[4] )
328 struct llvmpipe_context
*lp_context
= (struct llvmpipe_context
*)setup
->pipe
;
329 /* x/y positions in fixed point */
330 const struct lp_setup_variant_key
*key
= &setup
->setup
.variant
->key
;
331 const int sizeAttr
= setup
->psize_slot
;
333 = (setup
->point_size_per_vertex
&& sizeAttr
> 0) ? v0
[sizeAttr
][0]
336 /* Yes this is necessary to accurately calculate bounding boxes
337 * with the two fill-conventions we support. GL (normally) ends
338 * up needing a bottom-left fill convention, which requires
339 * slightly different rounding.
341 int adj
= (setup
->bottom_edge_rule
!= 0) ? 1 : 0;
343 struct lp_scene
*scene
= setup
->scene
;
344 struct lp_rast_triangle
*point
;
347 unsigned nr_planes
= 4;
348 struct point_info info
;
349 unsigned viewport_index
= 0;
353 if (setup
->viewport_index_slot
> 0) {
354 unsigned *udata
= (unsigned*)v0
[setup
->viewport_index_slot
];
355 viewport_index
= lp_clamp_viewport_idx(*udata
);
357 if (setup
->layer_slot
> 0) {
358 layer
= *(unsigned*)v0
[setup
->layer_slot
];
359 layer
= MIN2(layer
, scene
->fb_max_layer
);
363 print_point(setup
, v0
, size
);
365 /* Bounding rectangle (in pixels) */
366 if (!lp_context
->rasterizer
||
367 lp_context
->rasterizer
->point_quad_rasterization
) {
369 * Rasterize points as quads.
372 /* Point size as fixed point integer, remove rounding errors
373 * and gives minimum width for very small points.
375 fixed_width
= MAX2(FIXED_ONE
, subpixel_snap(size
));
377 x0
= subpixel_snap(v0
[0][0] - setup
->pixel_offset
) - fixed_width
/2;
378 y0
= subpixel_snap(v0
[0][1] - setup
->pixel_offset
) - fixed_width
/2;
380 bbox
.x0
= (x0
+ (FIXED_ONE
-1)) >> FIXED_ORDER
;
381 bbox
.x1
= (x0
+ fixed_width
+ (FIXED_ONE
-1)) >> FIXED_ORDER
;
382 bbox
.y0
= (y0
+ (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
383 bbox
.y1
= (y0
+ fixed_width
+ (FIXED_ONE
-1) + adj
) >> FIXED_ORDER
;
385 /* Inclusive coordinates:
391 * OpenGL legacy rasterization rules for non-sprite points.
393 * Per OpenGL 2.1 spec, section 3.3.1, "Basic Point Rasterization".
395 * This type of point rasterization is only available in pre 3.0 contexts
396 * (or compatibilility contexts which we don't support) anyway.
399 const int x0
= subpixel_snap(v0
[0][0]);
400 const int y0
= subpixel_snap(v0
[0][1]) - adj
;
403 /* Point size as fixed point integer. For GL legacy points
404 * the point size is always a whole integer.
406 fixed_width
= MAX2(FIXED_ONE
,
407 (subpixel_snap(size
) + FIXED_ONE
/2 - 1) & ~(FIXED_ONE
-1));
408 int_width
= fixed_width
>> FIXED_ORDER
;
410 assert(setup
->pixel_offset
!= 0);
412 if (int_width
== 1) {
413 bbox
.x0
= x0
>> FIXED_ORDER
;
414 bbox
.y0
= y0
>> FIXED_ORDER
;
420 bbox
.x0
= (x0
>> FIXED_ORDER
) - (int_width
- 1)/2;
421 bbox
.y0
= (y0
>> FIXED_ORDER
) - (int_width
- 1)/2;
424 bbox
.x0
= ((x0
+ FIXED_ONE
/2) >> FIXED_ORDER
) - int_width
/2;
425 bbox
.y0
= ((y0
+ FIXED_ONE
/2) >> FIXED_ORDER
) - int_width
/2;
428 bbox
.x1
= bbox
.x0
+ int_width
- 1;
429 bbox
.y1
= bbox
.y0
+ int_width
- 1;
434 debug_printf(" bbox: (%i, %i) - (%i, %i)\n",
439 if (!u_rect_test_intersection(&setup
->draw_regions
[viewport_index
], &bbox
)) {
440 if (0) debug_printf("offscreen\n");
441 LP_COUNT(nr_culled_tris
);
445 u_rect_find_intersection(&setup
->draw_regions
[viewport_index
], &bbox
);
447 point
= lp_setup_alloc_triangle(scene
,
455 point
->v
[0][0] = v0
[0][0];
456 point
->v
[0][1] = v0
[0][1];
461 if (lp_context
->active_statistics_queries
&&
462 !llvmpipe_rasterization_disabled(lp_context
)) {
463 lp_context
->pipeline_statistics
.c_primitives
++;
466 if (draw_will_inject_frontface(lp_context
->draw
) &&
467 setup
->face_slot
> 0) {
468 point
->inputs
.frontfacing
= v0
[setup
->face_slot
][0];
470 point
->inputs
.frontfacing
= TRUE
;
475 info
.dx12
= fixed_width
;
476 info
.dy01
= fixed_width
;
478 info
.a0
= GET_A0(&point
->inputs
);
479 info
.dadx
= GET_DADX(&point
->inputs
);
480 info
.dady
= GET_DADY(&point
->inputs
);
481 info
.frontfacing
= point
->inputs
.frontfacing
;
483 /* Setup parameter interpolants:
485 setup_point_coefficients(setup
, &info
);
487 point
->inputs
.disable
= FALSE
;
488 point
->inputs
.opaque
= FALSE
;
489 point
->inputs
.layer
= layer
;
490 point
->inputs
.viewport_index
= viewport_index
;
493 struct lp_rast_plane
*plane
= GET_PLANES(point
);
497 plane
[0].c
= 1-bbox
.x0
;
502 plane
[1].c
= bbox
.x1
+1;
507 plane
[2].c
= 1-bbox
.y0
;
512 plane
[3].c
= bbox
.y1
+1;
516 return lp_setup_bin_triangle(setup
, point
, &bbox
, nr_planes
, viewport_index
);
521 lp_setup_point(struct lp_setup_context
*setup
,
522 const float (*v0
)[4])
524 if (!try_setup_point( setup
, v0
))
526 if (!lp_setup_flush_and_restart(setup
))
529 if (!try_setup_point( setup
, v0
))
536 lp_setup_choose_point( struct lp_setup_context
*setup
)
538 setup
->point
= lp_setup_point
;