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 **************************************************************************/
27 #include "vg_context.h"
31 #include "shaders_cache.h"
34 #include "st_inlines.h"
36 #include "pipe/p_context.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
40 #include "cso_cache/cso_context.h"
42 #include "util/u_simple_shaders.h"
43 #include "util/u_memory.h"
44 #include "util/u_blit.h"
46 struct vg_context
*_vg_context
= 0;
48 struct vg_context
* vg_current_context(void)
53 static void init_clear(struct vg_context
*st
)
55 struct pipe_context
*pipe
= st
->pipe
;
57 /* rasterizer state: bypass clipping */
58 memset(&st
->clear
.raster
, 0, sizeof(st
->clear
.raster
));
59 st
->clear
.raster
.gl_rasterization_rules
= 1;
61 /* fragment shader state: color pass-through program */
63 util_make_fragment_passthrough_shader(pipe
);
65 void vg_set_current_context(struct vg_context
*ctx
)
70 struct vg_context
* vg_create_context(struct pipe_context
*pipe
,
72 struct vg_context
*share
)
74 struct vg_context
*ctx
;
77 ctx
= CALLOC_STRUCT(vg_context
);
81 vg_init_state(&ctx
->state
.vg
);
82 ctx
->state
.dirty
= ALL_DIRTY
;
84 ctx
->cso_context
= cso_create_context(pipe
);
88 ctx
->default_paint
= paint_create(ctx
);
89 ctx
->state
.vg
.stroke_paint
= ctx
->default_paint
;
90 ctx
->state
.vg
.fill_paint
= ctx
->default_paint
;
93 ctx
->mask
.sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
94 ctx
->mask
.sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
95 ctx
->mask
.sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
96 ctx
->mask
.sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
97 ctx
->mask
.sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
98 ctx
->mask
.sampler
.normalized_coords
= 0;
100 ctx
->blend_sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
101 ctx
->blend_sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
102 ctx
->blend_sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
103 ctx
->blend_sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
104 ctx
->blend_sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
105 ctx
->blend_sampler
.normalized_coords
= 0;
107 for (i
= 0; i
< 2; i
++) {
108 ctx
->velems
[i
].src_offset
= i
* 4 * sizeof(float);
109 ctx
->velems
[i
].instance_divisor
= 0;
110 ctx
->velems
[i
].vertex_buffer_index
= 0;
111 ctx
->velems
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
114 vg_set_error(ctx
, VG_NO_ERROR
);
116 ctx
->owned_objects
[VG_OBJECT_PAINT
] = cso_hash_create();
117 ctx
->owned_objects
[VG_OBJECT_IMAGE
] = cso_hash_create();
118 ctx
->owned_objects
[VG_OBJECT_MASK
] = cso_hash_create();
119 ctx
->owned_objects
[VG_OBJECT_FONT
] = cso_hash_create();
120 ctx
->owned_objects
[VG_OBJECT_PATH
] = cso_hash_create();
122 ctx
->renderer
= renderer_create(ctx
);
123 ctx
->sc
= shaders_cache_create(ctx
);
124 ctx
->shader
= shader_create(ctx
);
126 ctx
->blit
= util_create_blit(ctx
->pipe
, ctx
->cso_context
);
131 void vg_destroy_context(struct vg_context
*ctx
)
133 struct pipe_buffer
**cbuf
= &ctx
->mask
.cbuf
;
134 struct pipe_buffer
**vsbuf
= &ctx
->vs_const_buffer
;
136 util_destroy_blit(ctx
->blit
);
137 renderer_destroy(ctx
->renderer
);
138 shaders_cache_destroy(ctx
->sc
);
139 shader_destroy(ctx
->shader
);
140 paint_destroy(ctx
->default_paint
);
143 pipe_buffer_reference(cbuf
, NULL
);
146 pipe_buffer_reference(vsbuf
, NULL
);
149 cso_delete_fragment_shader(ctx
->cso_context
, ctx
->clear
.fs
);
150 ctx
->clear
.fs
= NULL
;
154 vg_shader_destroy(ctx
, ctx
->plain_vs
);
155 ctx
->plain_vs
= NULL
;
158 vg_shader_destroy(ctx
, ctx
->clear_vs
);
159 ctx
->clear_vs
= NULL
;
161 if (ctx
->texture_vs
) {
162 vg_shader_destroy(ctx
, ctx
->texture_vs
);
163 ctx
->texture_vs
= NULL
;
166 if (ctx
->pass_through_depth_fs
)
167 vg_shader_destroy(ctx
, ctx
->pass_through_depth_fs
);
168 if (ctx
->mask
.union_fs
)
169 vg_shader_destroy(ctx
, ctx
->mask
.union_fs
);
170 if (ctx
->mask
.intersect_fs
)
171 vg_shader_destroy(ctx
, ctx
->mask
.intersect_fs
);
172 if (ctx
->mask
.subtract_fs
)
173 vg_shader_destroy(ctx
, ctx
->mask
.subtract_fs
);
174 if (ctx
->mask
.set_fs
)
175 vg_shader_destroy(ctx
, ctx
->mask
.set_fs
);
177 cso_release_all(ctx
->cso_context
);
178 cso_destroy_context(ctx
->cso_context
);
180 cso_hash_delete(ctx
->owned_objects
[VG_OBJECT_PAINT
]);
181 cso_hash_delete(ctx
->owned_objects
[VG_OBJECT_IMAGE
]);
182 cso_hash_delete(ctx
->owned_objects
[VG_OBJECT_MASK
]);
183 cso_hash_delete(ctx
->owned_objects
[VG_OBJECT_FONT
]);
184 cso_hash_delete(ctx
->owned_objects
[VG_OBJECT_PATH
]);
189 void vg_init_object(struct vg_object
*obj
, struct vg_context
*ctx
, enum vg_object_type type
)
195 VGboolean
vg_context_is_object_valid(struct vg_context
*ctx
,
196 enum vg_object_type type
,
200 struct cso_hash
*hash
= ctx
->owned_objects
[type
];
203 return cso_hash_contains(hash
, (unsigned)(long)ptr
);
208 void vg_context_add_object(struct vg_context
*ctx
,
209 enum vg_object_type type
,
213 struct cso_hash
*hash
= ctx
->owned_objects
[type
];
216 cso_hash_insert(hash
, (unsigned)(long)ptr
, ptr
);
220 void vg_context_remove_object(struct vg_context
*ctx
,
221 enum vg_object_type type
,
225 struct cso_hash
*hash
= ctx
->owned_objects
[type
];
228 cso_hash_take(hash
, (unsigned)(long)ptr
);
232 static void update_clip_state(struct vg_context
*ctx
)
234 struct pipe_depth_stencil_alpha_state
*dsa
= &ctx
->state
.g3d
.dsa
;
235 struct vg_state
*state
= &ctx
->state
.vg
;
237 memset(dsa
, 0, sizeof(struct pipe_depth_stencil_alpha_state
));
239 if (state
->scissoring
) {
240 struct pipe_blend_state
*blend
= &ctx
->state
.g3d
.blend
;
241 struct pipe_framebuffer_state
*fb
= &ctx
->state
.g3d
.fb
;
244 dsa
->depth
.writemask
= 1;/*glDepthMask(TRUE);*/
245 dsa
->depth
.func
= PIPE_FUNC_ALWAYS
;
246 dsa
->depth
.enabled
= 1;
248 cso_save_blend(ctx
->cso_context
);
249 cso_save_fragment_shader(ctx
->cso_context
);
250 /* set a passthrough shader */
251 if (!ctx
->pass_through_depth_fs
)
252 ctx
->pass_through_depth_fs
= shader_create_from_text(ctx
->pipe
,
253 pass_through_depth_asm
,
255 PIPE_SHADER_FRAGMENT
);
256 cso_set_fragment_shader_handle(ctx
->cso_context
,
257 ctx
->pass_through_depth_fs
->driver
);
258 cso_set_depth_stencil_alpha(ctx
->cso_context
, dsa
);
260 ctx
->pipe
->clear(ctx
->pipe
, PIPE_CLEAR_DEPTHSTENCIL
, NULL
, 1.0, 0);
262 /* disable color writes */
263 blend
->rt
[0].colormask
= 0; /*disable colorwrites*/
264 cso_set_blend(ctx
->cso_context
, blend
);
266 /* enable scissoring */
267 for (i
= 0; i
< state
->scissor_rects_num
; ++i
) {
268 const float x
= state
->scissor_rects
[i
* 4 + 0].f
;
269 const float y
= state
->scissor_rects
[i
* 4 + 1].f
;
270 const float width
= state
->scissor_rects
[i
* 4 + 2].f
;
271 const float height
= state
->scissor_rects
[i
* 4 + 3].f
;
272 VGfloat minx
, miny
, maxx
, maxy
;
284 if (x
+ width
< maxx
)
286 if (y
+ height
< maxy
)
289 /* check for null space */
290 if (minx
>= maxx
|| miny
>= maxy
)
291 minx
= miny
= maxx
= maxy
= 0;
293 /*glClear(GL_DEPTH_BUFFER_BIT);*/
294 renderer_draw_quad(ctx
->renderer
, minx
, miny
, maxx
, maxy
, 0.0f
);
297 cso_restore_blend(ctx
->cso_context
);
298 cso_restore_fragment_shader(ctx
->cso_context
);
300 dsa
->depth
.enabled
= 1; /* glEnable(GL_DEPTH_TEST); */
301 dsa
->depth
.writemask
= 0;/*glDepthMask(FALSE);*/
302 dsa
->depth
.func
= PIPE_FUNC_GEQUAL
;
306 void vg_validate_state(struct vg_context
*ctx
)
308 if ((ctx
->state
.dirty
& BLEND_DIRTY
)) {
309 struct pipe_blend_state
*blend
= &ctx
->state
.g3d
.blend
;
310 memset(blend
, 0, sizeof(struct pipe_blend_state
));
311 blend
->rt
[0].blend_enable
= 1;
312 blend
->rt
[0].colormask
= PIPE_MASK_RGBA
;
314 switch (ctx
->state
.vg
.blend_mode
) {
316 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
317 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
318 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
319 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
320 blend
->rt
[0].blend_enable
= 0;
322 case VG_BLEND_SRC_OVER
:
323 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
324 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
325 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
326 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
328 case VG_BLEND_DST_OVER
:
329 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_INV_DST_ALPHA
;
330 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_INV_DST_ALPHA
;
331 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_DST_ALPHA
;
332 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_DST_ALPHA
;
334 case VG_BLEND_SRC_IN
:
335 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_DST_ALPHA
;
336 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_DST_ALPHA
;
337 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
338 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
340 case VG_BLEND_DST_IN
:
341 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ZERO
;
342 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ZERO
;
343 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
344 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
346 case VG_BLEND_MULTIPLY
:
347 case VG_BLEND_SCREEN
:
348 case VG_BLEND_DARKEN
:
349 case VG_BLEND_LIGHTEN
:
350 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
351 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
352 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
353 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
354 blend
->rt
[0].blend_enable
= 0;
356 case VG_BLEND_ADDITIVE
:
357 blend
->rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
358 blend
->rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
359 blend
->rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_ONE
;
360 blend
->rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_ONE
;
363 assert(!"not implemented blend mode");
365 cso_set_blend(ctx
->cso_context
, &ctx
->state
.g3d
.blend
);
367 if ((ctx
->state
.dirty
& RASTERIZER_DIRTY
)) {
368 struct pipe_rasterizer_state
*raster
= &ctx
->state
.g3d
.rasterizer
;
369 memset(raster
, 0, sizeof(struct pipe_rasterizer_state
));
370 raster
->gl_rasterization_rules
= 1;
371 cso_set_rasterizer(ctx
->cso_context
, &ctx
->state
.g3d
.rasterizer
);
373 if ((ctx
->state
.dirty
& VIEWPORT_DIRTY
)) {
374 struct pipe_framebuffer_state
*fb
= &ctx
->state
.g3d
.fb
;
375 const VGint param_bytes
= 8 * sizeof(VGfloat
);
376 VGfloat vs_consts
[8] = {
377 2.f
/fb
->width
, 2.f
/fb
->height
, 1, 1,
380 struct pipe_buffer
**cbuf
= &ctx
->vs_const_buffer
;
382 vg_set_viewport(ctx
, VEGA_Y0_BOTTOM
);
384 pipe_buffer_reference(cbuf
, NULL
);
385 *cbuf
= pipe_buffer_create(ctx
->pipe
->screen
, 16,
386 PIPE_BUFFER_USAGE_CONSTANT
,
390 st_no_flush_pipe_buffer_write(ctx
, *cbuf
,
391 0, param_bytes
, vs_consts
);
393 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_VERTEX
, 0, *cbuf
);
395 if ((ctx
->state
.dirty
& VS_DIRTY
)) {
396 cso_set_vertex_shader_handle(ctx
->cso_context
,
400 /* must be last because it renders to the depth buffer*/
401 if ((ctx
->state
.dirty
& DEPTH_STENCIL_DIRTY
)) {
402 update_clip_state(ctx
);
403 cso_set_depth_stencil_alpha(ctx
->cso_context
, &ctx
->state
.g3d
.dsa
);
406 shader_set_masking(ctx
->shader
, ctx
->state
.vg
.masking
);
407 shader_set_image_mode(ctx
->shader
, ctx
->state
.vg
.image_mode
);
409 ctx
->state
.dirty
= NONE_DIRTY
;
412 VGboolean
vg_object_is_valid(void *ptr
, enum vg_object_type type
)
414 struct vg_object
*obj
= ptr
;
415 if (ptr
&& is_aligned(obj
) && obj
->type
== type
)
421 void vg_set_error(struct vg_context
*ctx
,
424 /*vgGetError returns the oldest error code provided by
425 * an API call on the current context since the previous
426 * call to vgGetError on that context (or since the creation
428 if (ctx
->_error
== VG_NO_ERROR
)
432 void vg_prepare_blend_surface(struct vg_context
*ctx
)
434 struct pipe_surface
*dest_surface
= NULL
;
435 struct pipe_context
*pipe
= ctx
->pipe
;
436 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
437 struct st_renderbuffer
*strb
= stfb
->strb
;
439 /* first finish all pending rendering */
442 dest_surface
= pipe
->screen
->get_tex_surface(pipe
->screen
,
445 PIPE_BUFFER_USAGE_GPU_WRITE
);
446 /* flip it, because we want to use it as a sampler */
447 util_blit_pixels_tex(ctx
->blit
,
453 strb
->width
, strb
->height
,
454 0.0, PIPE_TEX_MIPFILTER_NEAREST
);
457 pipe_surface_reference(&dest_surface
, NULL
);
459 /* make sure it's complete */
464 void vg_prepare_blend_surface_from_mask(struct vg_context
*ctx
)
466 struct pipe_surface
*dest_surface
= NULL
;
467 struct pipe_context
*pipe
= ctx
->pipe
;
468 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
469 struct st_renderbuffer
*strb
= stfb
->strb
;
471 vg_validate_state(ctx
);
473 /* first finish all pending rendering */
476 dest_surface
= pipe
->screen
->get_tex_surface(pipe
->screen
,
479 PIPE_BUFFER_USAGE_GPU_WRITE
);
481 /* flip it, because we want to use it as a sampler */
482 util_blit_pixels_tex(ctx
->blit
,
488 strb
->width
, strb
->height
,
489 0.0, PIPE_TEX_MIPFILTER_NEAREST
);
491 /* make sure it's complete */
495 pipe_surface_reference(&dest_surface
, NULL
);
498 void * vg_plain_vs(struct vg_context
*ctx
)
500 if (!ctx
->plain_vs
) {
501 ctx
->plain_vs
= shader_create_from_text(ctx
->pipe
,
507 return ctx
->plain_vs
->driver
;
511 void * vg_clear_vs(struct vg_context
*ctx
)
513 if (!ctx
->clear_vs
) {
514 ctx
->clear_vs
= shader_create_from_text(ctx
->pipe
,
520 return ctx
->clear_vs
->driver
;
523 void * vg_texture_vs(struct vg_context
*ctx
)
525 if (!ctx
->texture_vs
) {
526 ctx
->texture_vs
= shader_create_from_text(ctx
->pipe
,
532 return ctx
->texture_vs
->driver
;
535 void vg_set_viewport(struct vg_context
*ctx
, VegaOrientation orientation
)
537 struct pipe_viewport_state viewport
;
538 struct pipe_framebuffer_state
*fb
= &ctx
->state
.g3d
.fb
;
539 VGfloat y_scale
= (orientation
== VEGA_Y0_BOTTOM
) ? -2.f
: 2.f
;
541 viewport
.scale
[0] = fb
->width
/ 2.f
;
542 viewport
.scale
[1] = fb
->height
/ y_scale
;
543 viewport
.scale
[2] = 1.0;
544 viewport
.scale
[3] = 1.0;
545 viewport
.translate
[0] = fb
->width
/ 2.f
;
546 viewport
.translate
[1] = fb
->height
/ 2.f
;
547 viewport
.translate
[2] = 0.0;
548 viewport
.translate
[3] = 0.0;
550 cso_set_viewport(ctx
->cso_context
, &viewport
);