1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 #include "matrix.h" /*for floatsEqual*/
30 #include "vg_context.h"
34 #include "util_array.h"
35 #include "VG/openvg.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_state.h"
40 #include "util/u_inlines.h"
41 #include "pipe/p_screen.h"
43 #include "util/u_draw_quad.h"
44 #include "util/u_math.h"
49 #define DEBUG_POLYGON 0
61 struct pipe_resource
*vbuf
;
62 struct pipe_screen
*screen
;
65 static float *ptr_to_vertex(float *data
, int idx
)
67 return data
+ (idx
* COMPONENTS
);
71 static void polygon_print(struct polygon
*poly
)
75 debug_printf("Polygon %p, size = %d\n", poly
, poly
->num_verts
);
76 for (i
= 0; i
< poly
->num_verts
; ++i
) {
77 vert
= ptr_to_vertex(poly
->data
, i
);
78 debug_printf("%f, %f, ", vert
[0], vert
[1]);
80 debug_printf("\nend\n");
85 struct polygon
* polygon_create(int size
)
87 struct polygon
*poly
= (struct polygon
*)malloc(sizeof(struct polygon
));
89 poly
->data
= malloc(sizeof(float) * COMPONENTS
* size
);
92 poly
->dirty
= VG_TRUE
;
98 struct polygon
* polygon_create_from_data(float *data
, int size
)
100 struct polygon
*poly
= polygon_create(size
);
102 memcpy(poly
->data
, data
, sizeof(float) * COMPONENTS
* size
);
103 poly
->num_verts
= size
;
104 poly
->dirty
= VG_TRUE
;
110 void polygon_destroy(struct polygon
*poly
)
113 pipe_resource_reference(&poly
->vbuf
, NULL
);
119 void polygon_resize(struct polygon
*poly
, int new_size
)
121 float *data
= (float*)malloc(sizeof(float) * COMPONENTS
* new_size
);
122 int size
= MIN2(sizeof(float) * COMPONENTS
* new_size
,
123 sizeof(float) * COMPONENTS
* poly
->size
);
124 memcpy(data
, poly
->data
, size
);
127 poly
->size
= new_size
;
128 poly
->dirty
= VG_TRUE
;
131 int polygon_size(struct polygon
*poly
)
136 int polygon_vertex_count(struct polygon
*poly
)
138 return poly
->num_verts
;
141 float * polygon_data(struct polygon
*poly
)
146 void polygon_vertex_append(struct polygon
*p
,
151 debug_printf("Append vertex [%f, %f]\n", x
, y
);
153 if (p
->num_verts
>= p
->size
) {
154 polygon_resize(p
, p
->size
* 2);
157 vert
= ptr_to_vertex(p
->data
, p
->num_verts
);
164 void polygon_set_vertex(struct polygon
*p
, int idx
,
168 if (idx
>= p
->num_verts
) {
169 /*fixme: error reporting*/
174 vert
= ptr_to_vertex(p
->data
, idx
);
180 void polygon_vertex(struct polygon
*p
, int idx
,
184 if (idx
>= p
->num_verts
) {
185 /*fixme: error reporting*/
190 vert
= ptr_to_vertex(p
->data
, idx
);
195 void polygon_bounding_rect(struct polygon
*p
,
199 float minx
, miny
, maxx
, maxy
;
200 float *vert
= ptr_to_vertex(p
->data
, 0);
206 for (i
= 1; i
< p
->num_verts
; ++i
) {
207 vert
= ptr_to_vertex(p
->data
, i
);
208 minx
= MIN2(vert
[0], minx
);
209 miny
= MIN2(vert
[1], miny
);
211 maxx
= MAX2(vert
[0], maxx
);
212 maxy
= MAX2(vert
[1], maxy
);
217 rect
[2] = maxx
- minx
;
218 rect
[3] = maxy
- miny
;
221 int polygon_contains_point(struct polygon
*p
,
227 void polygon_append_polygon(struct polygon
*dst
,
230 if (dst
->num_verts
+ src
->num_verts
>= dst
->size
) {
231 polygon_resize(dst
, dst
->num_verts
+ src
->num_verts
* 1.5);
233 memcpy(ptr_to_vertex(dst
->data
, dst
->num_verts
),
234 src
->data
, src
->num_verts
* COMPONENTS
* sizeof(VGfloat
));
235 dst
->num_verts
+= src
->num_verts
;
238 VGboolean
polygon_is_closed(struct polygon
*p
)
240 VGfloat start
[2], end
[2];
242 polygon_vertex(p
, 0, start
);
243 polygon_vertex(p
, p
->num_verts
- 1, end
);
245 return floatsEqual(start
[0], end
[0]) && floatsEqual(start
[1], end
[1]);
248 static void set_blend_for_fill(struct pipe_blend_state
*blend
)
250 memset(blend
, 0, sizeof(struct pipe_blend_state
));
251 blend
->rt
[0].colormask
= 0; /*disable colorwrites*/
253 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
254 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
255 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
256 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
259 static void draw_polygon(struct vg_context
*ctx
,
260 struct polygon
*poly
)
263 struct pipe_context
*pipe
;
264 struct pipe_vertex_buffer vbuffer
;
265 struct pipe_vertex_element velement
;
267 vert_size
= poly
->num_verts
* COMPONENTS
* sizeof(float);
269 /*polygon_print(poly);*/
273 if (poly
->vbuf
== NULL
|| poly
->dirty
) {
275 pipe_resource_reference(&poly
->vbuf
,
278 poly
->screen
= pipe
->screen
;
279 poly
->vbuf
= pipe_user_buffer_create(poly
->screen
,
282 PIPE_BIND_VERTEX_BUFFER
);
283 poly
->dirty
= VG_FALSE
;
287 /* tell pipe about the vertex buffer */
288 memset(&vbuffer
, 0, sizeof(vbuffer
));
289 vbuffer
.buffer
= poly
->vbuf
;
290 vbuffer
.stride
= COMPONENTS
* sizeof(float); /* vertex size */
291 vbuffer
.buffer_offset
= 0;
292 vbuffer
.max_index
= poly
->num_verts
- 1;
293 pipe
->set_vertex_buffers(pipe
, 1, &vbuffer
);
295 /* tell pipe about the vertex attributes */
296 memset(&velement
, 0, sizeof(velement
));
297 velement
.src_offset
= 0;
298 velement
.instance_divisor
= 0;
299 velement
.vertex_buffer_index
= 0;
300 velement
.src_format
= PIPE_FORMAT_R32G32_FLOAT
;
301 cso_set_vertex_elements(ctx
->cso_context
, 1, &velement
);
304 pipe
->draw_arrays(pipe
, PIPE_PRIM_TRIANGLE_FAN
,
308 void polygon_fill(struct polygon
*poly
, struct vg_context
*ctx
)
310 struct pipe_depth_stencil_alpha_state dsa
;
311 struct pipe_stencil_ref sr
;
312 struct pipe_blend_state blend
;
314 VGfloat min_x
, min_y
, max_x
, max_y
;
316 polygon_bounding_rect(poly
, bounds
);
319 max_x
= bounds
[0] + bounds
[2];
320 max_y
= bounds
[1] + bounds
[3];
323 debug_printf("Poly bounds are [%f, %f], [%f, %f]\n",
324 min_x
, min_y
, max_x
, max_y
);
327 set_blend_for_fill(&blend
);
329 memset(&dsa
, 0, sizeof(struct pipe_depth_stencil_alpha_state
));
330 memset(&sr
, 0, sizeof(struct pipe_stencil_ref
));
331 /* only need a fixed 0. Rely on default or move it out at least? */
332 cso_set_stencil_ref(ctx
->cso_context
, &sr
);
334 cso_save_blend(ctx
->cso_context
);
335 cso_save_depth_stencil_alpha(ctx
->cso_context
);
337 dsa
.stencil
[0].enabled
= 1;
338 if (ctx
->state
.vg
.fill_rule
== VG_EVEN_ODD
) {
339 dsa
.stencil
[0].writemask
= 1;
340 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
341 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
342 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_INVERT
;
343 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
344 dsa
.stencil
[0].valuemask
= ~0;
346 cso_set_blend(ctx
->cso_context
, &blend
);
347 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
348 draw_polygon(ctx
, poly
);
349 } else if (ctx
->state
.vg
.fill_rule
== VG_NON_ZERO
) {
350 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
352 if (screen
->get_param(screen
, PIPE_CAP_TWO_SIDED_STENCIL
)) {
354 dsa
.stencil
[0].writemask
= ~0;
355 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
356 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
357 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_INCR_WRAP
;
358 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
359 dsa
.stencil
[0].valuemask
= ~0;
362 dsa
.stencil
[1].enabled
= 1;
363 dsa
.stencil
[1].writemask
= ~0;
364 dsa
.stencil
[1].fail_op
= PIPE_STENCIL_OP_KEEP
;
365 dsa
.stencil
[1].zfail_op
= PIPE_STENCIL_OP_KEEP
;
366 dsa
.stencil
[1].zpass_op
= PIPE_STENCIL_OP_DECR_WRAP
;
367 dsa
.stencil
[1].func
= PIPE_FUNC_ALWAYS
;
368 dsa
.stencil
[1].valuemask
= ~0;
370 cso_set_blend(ctx
->cso_context
, &blend
);
371 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
372 draw_polygon(ctx
, poly
);
374 struct pipe_rasterizer_state raster
;
376 memcpy(&raster
, &ctx
->state
.g3d
.rasterizer
, sizeof(struct pipe_rasterizer_state
));
378 cso_save_rasterizer(ctx
->cso_context
);
379 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
380 dsa
.stencil
[0].valuemask
= ~0;
382 raster
.cull_face
= PIPE_FACE_BACK
;
383 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
384 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
385 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_INCR_WRAP
;
387 cso_set_blend(ctx
->cso_context
, &blend
);
388 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
389 cso_set_rasterizer(ctx
->cso_context
, &raster
);
390 draw_polygon(ctx
, poly
);
392 raster
.cull_face
= PIPE_FACE_FRONT
;
393 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
394 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
395 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_DECR_WRAP
;
396 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
397 cso_set_rasterizer(ctx
->cso_context
, &raster
);
398 draw_polygon(ctx
, poly
);
400 cso_restore_rasterizer(ctx
->cso_context
);
404 /* restore color writes */
405 cso_restore_blend(ctx
->cso_context
);
406 /* setup stencil ops */
407 dsa
.stencil
[0].func
= PIPE_FUNC_NOTEQUAL
;
408 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
409 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
410 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
411 dsa
.stencil
[0].valuemask
= dsa
.stencil
[0].writemask
;
412 dsa
.stencil
[1].enabled
= 0;
413 memcpy(&dsa
.depth
, &ctx
->state
.g3d
.dsa
.depth
,
414 sizeof(struct pipe_depth_state
));
415 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
417 /* render the quad to propagate the rendering from stencil */
418 renderer_draw_quad(ctx
->renderer
, min_x
, min_y
,
419 max_x
, max_y
, 0.0f
/*depth should be disabled*/);
421 cso_restore_depth_stencil_alpha(ctx
->cso_context
);
424 void polygon_array_fill(struct polygon_array
*polyarray
, struct vg_context
*ctx
)
426 struct array
*polys
= polyarray
->array
;
427 struct pipe_depth_stencil_alpha_state dsa
;
428 struct pipe_stencil_ref sr
;
429 struct pipe_blend_state blend
;
430 VGfloat min_x
= polyarray
->min_x
;
431 VGfloat min_y
= polyarray
->min_y
;
432 VGfloat max_x
= polyarray
->max_x
;
433 VGfloat max_y
= polyarray
->max_y
;
438 debug_printf("%s: Poly bounds are [%f, %f], [%f, %f]\n",
440 min_x
, min_y
, max_x
, max_y
);
443 set_blend_for_fill(&blend
);
445 memset(&dsa
, 0, sizeof(struct pipe_depth_stencil_alpha_state
));
446 memset(&sr
, 0, sizeof(struct pipe_stencil_ref
));
447 /* only need a fixed 0. Rely on default or move it out at least? */
448 cso_set_stencil_ref(ctx
->cso_context
, &sr
);
450 cso_save_blend(ctx
->cso_context
);
451 cso_save_depth_stencil_alpha(ctx
->cso_context
);
453 dsa
.stencil
[0].enabled
= 1;
454 if (ctx
->state
.vg
.fill_rule
== VG_EVEN_ODD
) {
455 dsa
.stencil
[0].writemask
= 1;
456 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
457 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
458 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_INVERT
;
459 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
460 dsa
.stencil
[0].valuemask
= ~0;
462 cso_set_blend(ctx
->cso_context
, &blend
);
463 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
464 for (i
= 0; i
< polys
->num_elements
; ++i
) {
465 struct polygon
*poly
= (((struct polygon
**)polys
->data
)[i
]);
466 draw_polygon(ctx
, poly
);
468 } else if (ctx
->state
.vg
.fill_rule
== VG_NON_ZERO
) {
469 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
471 if (screen
->get_param(screen
, PIPE_CAP_TWO_SIDED_STENCIL
)) {
473 dsa
.stencil
[0].writemask
= ~0;
474 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
475 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
476 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_INCR_WRAP
;
477 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
478 dsa
.stencil
[0].valuemask
= ~0;
481 dsa
.stencil
[1].enabled
= 1;
482 dsa
.stencil
[1].writemask
= ~0;
483 dsa
.stencil
[1].fail_op
= PIPE_STENCIL_OP_KEEP
;
484 dsa
.stencil
[1].zfail_op
= PIPE_STENCIL_OP_KEEP
;
485 dsa
.stencil
[1].zpass_op
= PIPE_STENCIL_OP_DECR_WRAP
;
486 dsa
.stencil
[1].func
= PIPE_FUNC_ALWAYS
;
487 dsa
.stencil
[1].valuemask
= ~0;
489 cso_set_blend(ctx
->cso_context
, &blend
);
490 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
491 for (i
= 0; i
< polys
->num_elements
; ++i
) {
492 struct polygon
*poly
= (((struct polygon
**)polys
->data
)[i
]);
493 draw_polygon(ctx
, poly
);
496 struct pipe_rasterizer_state raster
;
498 memcpy(&raster
, &ctx
->state
.g3d
.rasterizer
, sizeof(struct pipe_rasterizer_state
));
500 cso_save_rasterizer(ctx
->cso_context
);
501 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
502 dsa
.stencil
[0].valuemask
= ~0;
504 raster
.cull_face
= PIPE_FACE_BACK
;
505 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
506 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
507 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_INCR_WRAP
;
509 cso_set_blend(ctx
->cso_context
, &blend
);
510 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
511 cso_set_rasterizer(ctx
->cso_context
, &raster
);
512 for (i
= 0; i
< polys
->num_elements
; ++i
) {
513 struct polygon
*poly
= (((struct polygon
**)polys
->data
)[i
]);
514 draw_polygon(ctx
, poly
);
517 raster
.cull_face
= PIPE_FACE_FRONT
;
518 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_KEEP
;
519 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_KEEP
;
520 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_DECR_WRAP
;
521 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
522 cso_set_rasterizer(ctx
->cso_context
, &raster
);
523 for (i
= 0; i
< polys
->num_elements
; ++i
) {
524 struct polygon
*poly
= (((struct polygon
**)polys
->data
)[i
]);
525 draw_polygon(ctx
, poly
);
528 cso_restore_rasterizer(ctx
->cso_context
);
532 /* restore color writes */
533 cso_restore_blend(ctx
->cso_context
);
534 /* setup stencil ops */
535 dsa
.stencil
[0].func
= PIPE_FUNC_NOTEQUAL
;
536 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
537 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
538 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
539 dsa
.stencil
[0].valuemask
= dsa
.stencil
[0].writemask
;
540 dsa
.stencil
[1].enabled
= 0;
541 memcpy(&dsa
.depth
, &ctx
->state
.g3d
.dsa
.depth
,
542 sizeof(struct pipe_depth_state
));
543 cso_set_depth_stencil_alpha(ctx
->cso_context
, &dsa
);
545 /* render the quad to propagate the rendering from stencil */
546 renderer_draw_quad(ctx
->renderer
, min_x
, min_y
,
547 max_x
, max_y
, 0.0f
/*depth should be disabled*/);
549 cso_restore_depth_stencil_alpha(ctx
->cso_context
);