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/openvg.h"
29 #include "vg_context.h"
32 #include "shaders_cache.h"
33 #include "st_inlines.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_inlines.h"
38 #include "pipe/p_screen.h"
39 #include "pipe/p_shader_tokens.h"
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
45 #include "asm_filters.h"
51 struct vg_shader
* (*setup_shader
)(struct vg_context
*, void *);
53 const void *const_buffer
;
54 VGint const_buffer_len
;
55 VGTilingMode tiling_mode
;
56 struct pipe_texture
*extra_texture
;
59 static INLINE
struct pipe_texture
*create_texture_1d(struct vg_context
*ctx
,
60 const VGuint
*color_data
,
61 const VGint color_data_len
)
63 struct pipe_context
*pipe
= ctx
->pipe
;
64 struct pipe_screen
*screen
= pipe
->screen
;
65 struct pipe_texture
*tex
= 0;
66 struct pipe_texture templ
;
68 memset(&templ
, 0, sizeof(templ
));
69 templ
.target
= PIPE_TEXTURE_1D
;
70 templ
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
72 templ
.width0
= color_data_len
;
75 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
77 tex
= screen
->texture_create(screen
, &templ
);
79 { /* upload color_data */
80 struct pipe_transfer
*transfer
=
81 screen
->get_tex_transfer(screen
, tex
,
83 PIPE_TRANSFER_READ_WRITE
,
84 0, 0, tex
->width0
, tex
->height0
);
85 void *map
= screen
->transfer_map(screen
, transfer
);
86 memcpy(map
, color_data
, sizeof(VGint
)*color_data_len
);
87 screen
->transfer_unmap(screen
, transfer
);
88 screen
->tex_transfer_destroy(transfer
);
94 static INLINE
struct pipe_surface
* setup_framebuffer(struct vg_image
*dst
)
96 struct vg_context
*ctx
= vg_current_context();
97 struct pipe_context
*pipe
= ctx
->pipe
;
98 struct pipe_framebuffer_state fb
;
99 struct pipe_surface
*dst_surf
= pipe
->screen
->get_tex_surface(
100 pipe
->screen
, dst
->texture
, 0, 0, 0,
101 PIPE_BUFFER_USAGE_GPU_WRITE
);
104 memset(&fb
, 0, sizeof(fb
));
105 fb
.width
= dst
->x
+ dst_surf
->width
;
106 fb
.height
= dst
->y
+ dst_surf
->height
;
108 fb
.cbufs
[0] = dst_surf
;
111 for (i
= 1; i
< PIPE_MAX_COLOR_BUFS
; ++i
)
114 cso_set_framebuffer(ctx
->cso_context
, &fb
);
119 static void setup_viewport(struct vg_image
*dst
)
121 struct vg_context
*ctx
= vg_current_context();
122 vg_set_viewport(ctx
, VEGA_Y0_TOP
);
125 static void setup_blend()
127 struct vg_context
*ctx
= vg_current_context();
128 struct pipe_blend_state blend
;
129 memset(&blend
, 0, sizeof(blend
));
130 blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
131 blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
132 blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
133 blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
134 if (ctx
->state
.vg
.filter_channel_mask
& VG_RED
)
135 blend
.colormask
|= PIPE_MASK_R
;
136 if (ctx
->state
.vg
.filter_channel_mask
& VG_GREEN
)
137 blend
.colormask
|= PIPE_MASK_G
;
138 if (ctx
->state
.vg
.filter_channel_mask
& VG_BLUE
)
139 blend
.colormask
|= PIPE_MASK_B
;
140 if (ctx
->state
.vg
.filter_channel_mask
& VG_ALPHA
)
141 blend
.colormask
|= PIPE_MASK_A
;
142 blend
.blend_enable
= 1;
143 cso_set_blend(ctx
->cso_context
, &blend
);
146 static void setup_constant_buffer(struct vg_context
*ctx
, const void *buffer
,
149 struct pipe_context
*pipe
= ctx
->pipe
;
150 struct pipe_constant_buffer
*cbuf
= &ctx
->filter
.buffer
;
152 /* We always need to get a new buffer, to keep the drivers simple and
153 * avoid gratuitous rendering synchronization. */
154 pipe_buffer_reference(&cbuf
->buffer
, NULL
);
156 cbuf
->buffer
= pipe_buffer_create(pipe
->screen
, 16,
157 PIPE_BUFFER_USAGE_CONSTANT
,
161 st_no_flush_pipe_buffer_write(ctx
, cbuf
->buffer
,
162 0, param_bytes
, buffer
);
165 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, cbuf
);
168 static void setup_samplers(struct vg_context
*ctx
, struct filter_info
*info
)
170 struct pipe_sampler_state
*samplers
[PIPE_MAX_SAMPLERS
];
171 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
172 struct pipe_sampler_state sampler
[3];
173 int num_samplers
= 0;
174 int num_textures
= 0;
185 memset(&sampler
[0], 0, sizeof(struct pipe_sampler_state
));
186 sampler
[0].wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
187 sampler
[0].wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
188 sampler
[0].wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
189 sampler
[0].min_img_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
190 sampler
[0].mag_img_filter
= PIPE_TEX_MIPFILTER_LINEAR
;
191 sampler
[0].normalized_coords
= 1;
193 switch(info
->tiling_mode
) {
195 sampler
[0].wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
196 sampler
[0].wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
197 memcpy(sampler
[0].border_color
,
198 ctx
->state
.vg
.tile_fill_color
,
199 sizeof(VGfloat
) * 4);
202 sampler
[0].wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
203 sampler
[0].wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
206 sampler
[0].wrap_s
= PIPE_TEX_WRAP_REPEAT
;
207 sampler
[0].wrap_t
= PIPE_TEX_WRAP_REPEAT
;
209 case VG_TILE_REFLECT
:
210 sampler
[0].wrap_s
= PIPE_TEX_WRAP_MIRROR_REPEAT
;
211 sampler
[0].wrap_t
= PIPE_TEX_WRAP_MIRROR_REPEAT
;
214 debug_assert(!"Unknown tiling mode");
217 samplers
[0] = &sampler
[0];
218 textures
[0] = info
->src
->texture
;
222 if (info
->extra_texture
) {
223 memcpy(&sampler
[1], &sampler
[0], sizeof(struct pipe_sampler_state
));
224 samplers
[1] = &sampler
[1];
225 textures
[1] = info
->extra_texture
;
231 cso_set_samplers(ctx
->cso_context
, num_samplers
, (const struct pipe_sampler_state
**)samplers
);
232 cso_set_sampler_textures(ctx
->cso_context
, num_textures
, textures
);
235 static struct vg_shader
* setup_color_matrix(struct vg_context
*ctx
, void *user_data
)
237 struct vg_shader
*shader
=
238 shader_create_from_text(ctx
->pipe
, color_matrix_asm
, 200,
239 PIPE_SHADER_FRAGMENT
);
240 cso_set_fragment_shader_handle(ctx
->cso_context
, shader
->driver
);
244 static struct vg_shader
* setup_convolution(struct vg_context
*ctx
, void *user_data
)
247 VGint num_consts
= (VGint
)(long)(user_data
);
248 struct vg_shader
*shader
;
250 snprintf(buffer
, 1023, convolution_asm
, num_consts
, num_consts
/ 2 + 1);
252 shader
= shader_create_from_text(ctx
->pipe
, buffer
, 200,
253 PIPE_SHADER_FRAGMENT
);
255 cso_set_fragment_shader_handle(ctx
->cso_context
, shader
->driver
);
259 static struct vg_shader
* setup_lookup(struct vg_context
*ctx
, void *user_data
)
261 struct vg_shader
*shader
=
262 shader_create_from_text(ctx
->pipe
, lookup_asm
,
263 200, PIPE_SHADER_FRAGMENT
);
265 cso_set_fragment_shader_handle(ctx
->cso_context
, shader
->driver
);
270 static struct vg_shader
* setup_lookup_single(struct vg_context
*ctx
, void *user_data
)
273 VGImageChannel channel
= (VGImageChannel
)(user_data
);
274 struct vg_shader
*shader
;
278 snprintf(buffer
, 1023, lookup_single_asm
, "xxxx");
281 snprintf(buffer
, 1023, lookup_single_asm
, "yyyy");
284 snprintf(buffer
, 1023, lookup_single_asm
, "zzzz");
287 snprintf(buffer
, 1023, lookup_single_asm
, "wwww");
290 debug_assert(!"Unknown color channel");
293 shader
= shader_create_from_text(ctx
->pipe
, buffer
, 200,
294 PIPE_SHADER_FRAGMENT
);
296 cso_set_fragment_shader_handle(ctx
->cso_context
, shader
->driver
);
300 static void execute_filter(struct vg_context
*ctx
,
301 struct filter_info
*info
)
303 struct pipe_surface
*dst_surf
;
304 struct vg_shader
*shader
;
306 cso_save_framebuffer(ctx
->cso_context
);
307 cso_save_fragment_shader(ctx
->cso_context
);
308 cso_save_viewport(ctx
->cso_context
);
309 cso_save_blend(ctx
->cso_context
);
310 cso_save_samplers(ctx
->cso_context
);
311 cso_save_sampler_textures(ctx
->cso_context
);
313 dst_surf
= setup_framebuffer(info
->dst
);
314 setup_viewport(info
->dst
);
316 setup_constant_buffer(ctx
, info
->const_buffer
, info
->const_buffer_len
);
317 shader
= info
->setup_shader(ctx
, info
->user_data
);
318 setup_samplers(ctx
, info
);
320 renderer_draw_texture(ctx
->renderer
,
322 info
->dst
->x
, info
->dst
->y
,
323 info
->dst
->x
+ info
->dst
->width
,
324 info
->dst
->y
+ info
->dst
->height
,
325 info
->dst
->x
, info
->dst
->y
,
326 info
->dst
->x
+ info
->dst
->width
,
327 info
->dst
->y
+ info
->dst
->height
);
329 cso_restore_framebuffer(ctx
->cso_context
);
330 cso_restore_fragment_shader(ctx
->cso_context
);
331 cso_restore_viewport(ctx
->cso_context
);
332 cso_restore_blend(ctx
->cso_context
);
333 cso_restore_samplers(ctx
->cso_context
);
334 cso_restore_sampler_textures(ctx
->cso_context
);
336 vg_shader_destroy(ctx
, shader
);
338 pipe_surface_reference(&dst_surf
, NULL
);
341 void vgColorMatrix(VGImage dst
, VGImage src
,
342 const VGfloat
* matrix
)
344 struct vg_context
*ctx
= vg_current_context();
345 struct vg_image
*d
, *s
;
346 struct filter_info info
;
348 if (dst
== VG_INVALID_HANDLE
|| src
== VG_INVALID_HANDLE
) {
349 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
352 if (!matrix
|| !is_aligned(matrix
)) {
353 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
357 d
= (struct vg_image
*)dst
;
358 s
= (struct vg_image
*)src
;
360 if (vg_image_overlaps(d
, s
)) {
361 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
367 info
.setup_shader
= &setup_color_matrix
;
368 info
.user_data
= NULL
;
369 info
.const_buffer
= matrix
;
370 info
.const_buffer_len
= 20 * sizeof(VGfloat
);
371 info
.tiling_mode
= VG_TILE_PAD
;
372 info
.extra_texture
= 0;
373 execute_filter(ctx
, &info
);
376 static VGfloat
texture_offset(VGfloat width
, VGint kernelSize
, VGint current
, VGint shift
)
378 VGfloat diff
= current
- shift
;
383 void vgConvolve(VGImage dst
, VGImage src
,
384 VGint kernelWidth
, VGint kernelHeight
,
385 VGint shiftX
, VGint shiftY
,
386 const VGshort
* kernel
,
389 VGTilingMode tilingMode
)
391 struct vg_context
*ctx
= vg_current_context();
396 struct vg_image
*d
, *s
;
397 VGint kernel_size
= kernelWidth
* kernelHeight
;
398 struct filter_info info
;
399 const VGint max_kernel_size
= vgGeti(VG_MAX_KERNEL_SIZE
);
401 if (dst
== VG_INVALID_HANDLE
|| src
== VG_INVALID_HANDLE
) {
402 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
406 if (kernelWidth
<= 0 || kernelHeight
<= 0 ||
407 kernelWidth
> max_kernel_size
|| kernelHeight
> max_kernel_size
) {
408 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
412 if (!kernel
|| !is_aligned_to(kernel
, 2)) {
413 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
417 if (tilingMode
< VG_TILE_FILL
||
418 tilingMode
> VG_TILE_REFLECT
) {
419 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
423 d
= (struct vg_image
*)dst
;
424 s
= (struct vg_image
*)src
;
426 if (vg_image_overlaps(d
, s
)) {
427 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
431 vg_validate_state(ctx
);
433 buffer_len
= 8 + 2 * 4 * kernel_size
;
434 buffer
= (VGfloat
*)malloc(buffer_len
* sizeof(VGfloat
));
438 buffer
[2] = 2.f
; /*unused*/
439 buffer
[3] = 4.f
; /*unused*/
441 buffer
[4] = kernelWidth
* kernelHeight
;
447 for (j
= 0; j
< kernelHeight
; ++j
) {
448 for (i
= 0; i
< kernelWidth
; ++i
) {
449 VGint index
= j
* kernelWidth
+ i
;
452 x
= texture_offset(s
->width
, kernelWidth
, i
, shiftX
);
453 y
= texture_offset(s
->height
, kernelHeight
, j
, shiftY
);
455 buffer
[idx
+ index
*4 + 0] = x
;
456 buffer
[idx
+ index
*4 + 1] = y
;
457 buffer
[idx
+ index
*4 + 2] = 0.f
;
458 buffer
[idx
+ index
*4 + 3] = 0.f
;
461 idx
+= kernel_size
* 4;
463 for (j
= 0; j
< kernelHeight
; ++j
) {
464 for (i
= 0; i
< kernelWidth
; ++i
) {
465 /* transpose the kernel */
466 VGint index
= j
* kernelWidth
+ i
;
467 VGint kindex
= (kernelWidth
- i
- 1) * kernelHeight
+ (kernelHeight
- j
- 1);
468 buffer
[idx
+ index
*4 + 0] = kernel
[kindex
];
469 buffer
[idx
+ index
*4 + 1] = kernel
[kindex
];
470 buffer
[idx
+ index
*4 + 2] = kernel
[kindex
];
471 buffer
[idx
+ index
*4 + 3] = kernel
[kindex
];
477 info
.setup_shader
= &setup_convolution
;
478 info
.user_data
= (void*)(long)(buffer_len
/4);
479 info
.const_buffer
= buffer
;
480 info
.const_buffer_len
= buffer_len
* sizeof(VGfloat
);
481 info
.tiling_mode
= tilingMode
;
482 info
.extra_texture
= 0;
483 execute_filter(ctx
, &info
);
488 void vgSeparableConvolve(VGImage dst
, VGImage src
,
491 VGint shiftX
, VGint shiftY
,
492 const VGshort
* kernelX
,
493 const VGshort
* kernelY
,
496 VGTilingMode tilingMode
)
498 struct vg_context
*ctx
= vg_current_context();
501 const VGint max_kernel_size
= vgGeti(VG_MAX_SEPARABLE_KERNEL_SIZE
);
503 if (dst
== VG_INVALID_HANDLE
|| src
== VG_INVALID_HANDLE
) {
504 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
508 if (kernelWidth
<= 0 || kernelHeight
<= 0 ||
509 kernelWidth
> max_kernel_size
|| kernelHeight
> max_kernel_size
) {
510 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
514 if (!kernelX
|| !kernelY
||
515 !is_aligned_to(kernelX
, 2) || !is_aligned_to(kernelY
, 2)) {
516 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
519 if (tilingMode
< VG_TILE_FILL
||
520 tilingMode
> VG_TILE_REFLECT
) {
521 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
524 kernel
= malloc(sizeof(VGshort
)*kernelWidth
*kernelHeight
);
525 for (i
= 0; i
< kernelWidth
; ++i
) {
526 for (j
= 0; j
< kernelHeight
; ++j
) {
527 kernel
[idx
] = kernelX
[i
] * kernelY
[j
];
531 vgConvolve(dst
, src
, kernelWidth
, kernelHeight
, shiftX
, shiftY
,
532 kernel
, scale
, bias
, tilingMode
);
536 static INLINE VGfloat
compute_gaussian_componenet(VGfloat x
, VGfloat y
,
537 VGfloat stdDeviationX
,
538 VGfloat stdDeviationY
)
540 VGfloat mult
= 1 / ( 2 * M_PI
* stdDeviationX
* stdDeviationY
);
541 VGfloat e
= exp( - ( pow(x
, 2)/(2*pow(stdDeviationX
, 2)) +
542 pow(y
, 2)/(2*pow(stdDeviationY
, 2)) ) );
546 static INLINE VGint
compute_kernel_size(VGfloat deviation
)
548 VGint size
= ceil(2.146 * deviation
);
554 static void compute_gaussian_kernel(VGfloat
*kernel
,
555 VGint width
, VGint height
,
556 VGfloat stdDeviationX
,
557 VGfloat stdDeviationY
)
560 VGfloat scale
= 0.0f
;
562 for (j
= 0; j
< height
; ++j
) {
563 for (i
= 0; i
< width
; ++i
) {
564 VGint idx
= (height
- j
-1) * width
+ (width
- i
-1);
565 kernel
[idx
] = compute_gaussian_componenet(i
-(ceil(width
/2))-1,
567 stdDeviationX
, stdDeviationY
);
568 scale
+= kernel
[idx
];
572 for (j
= 0; j
< height
; ++j
) {
573 for (i
= 0; i
< width
; ++i
) {
574 VGint idx
= j
* width
+ i
;
575 kernel
[idx
] /= scale
;
580 void vgGaussianBlur(VGImage dst
, VGImage src
,
581 VGfloat stdDeviationX
,
582 VGfloat stdDeviationY
,
583 VGTilingMode tilingMode
)
585 struct vg_context
*ctx
= vg_current_context();
586 struct vg_image
*d
, *s
;
587 VGfloat
*buffer
, *kernel
;
588 VGint kernel_width
, kernel_height
, kernel_size
;
591 struct filter_info info
;
593 if (dst
== VG_INVALID_HANDLE
|| src
== VG_INVALID_HANDLE
) {
594 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
597 if (stdDeviationX
<= 0 || stdDeviationY
<= 0) {
598 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
602 if (tilingMode
< VG_TILE_FILL
||
603 tilingMode
> VG_TILE_REFLECT
) {
604 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
608 d
= (struct vg_image
*)dst
;
609 s
= (struct vg_image
*)src
;
611 if (vg_image_overlaps(d
, s
)) {
612 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
616 kernel_width
= compute_kernel_size(stdDeviationX
);
617 kernel_height
= compute_kernel_size(stdDeviationY
);
618 kernel_size
= kernel_width
* kernel_height
;
619 kernel
= malloc(sizeof(VGfloat
)*kernel_size
);
620 compute_gaussian_kernel(kernel
, kernel_width
, kernel_height
,
621 stdDeviationX
, stdDeviationY
);
623 buffer_len
= 8 + 2 * 4 * kernel_size
;
624 buffer
= (VGfloat
*)malloc(buffer_len
* sizeof(VGfloat
));
628 buffer
[2] = 2.f
; /*unused*/
629 buffer
[3] = 4.f
; /*unused*/
631 buffer
[4] = kernel_width
* kernel_height
;
632 buffer
[5] = 1.f
;/*scale*/
633 buffer
[6] = 0.f
;/*bias*/
637 for (j
= 0; j
< kernel_height
; ++j
) {
638 for (i
= 0; i
< kernel_width
; ++i
) {
639 VGint index
= j
* kernel_width
+ i
;
642 x
= texture_offset(s
->width
, kernel_width
, i
, kernel_width
/2);
643 y
= texture_offset(s
->height
, kernel_height
, j
, kernel_height
/2);
645 buffer
[idx
+ index
*4 + 0] = x
;
646 buffer
[idx
+ index
*4 + 1] = y
;
647 buffer
[idx
+ index
*4 + 2] = 0.f
;
648 buffer
[idx
+ index
*4 + 3] = 0.f
;
651 idx
+= kernel_size
* 4;
653 for (j
= 0; j
< kernel_height
; ++j
) {
654 for (i
= 0; i
< kernel_width
; ++i
) {
655 /* transpose the kernel */
656 VGint index
= j
* kernel_width
+ i
;
657 VGint kindex
= (kernel_width
- i
- 1) * kernel_height
+ (kernel_height
- j
- 1);
658 buffer
[idx
+ index
*4 + 0] = kernel
[kindex
];
659 buffer
[idx
+ index
*4 + 1] = kernel
[kindex
];
660 buffer
[idx
+ index
*4 + 2] = kernel
[kindex
];
661 buffer
[idx
+ index
*4 + 3] = kernel
[kindex
];
667 info
.setup_shader
= &setup_convolution
;
668 info
.user_data
= (void*)(long)(buffer_len
/4);
669 info
.const_buffer
= buffer
;
670 info
.const_buffer_len
= buffer_len
* sizeof(VGfloat
);
671 info
.tiling_mode
= tilingMode
;
672 info
.extra_texture
= 0;
673 execute_filter(ctx
, &info
);
679 void vgLookup(VGImage dst
, VGImage src
,
680 const VGubyte
* redLUT
,
681 const VGubyte
* greenLUT
,
682 const VGubyte
* blueLUT
,
683 const VGubyte
* alphaLUT
,
684 VGboolean outputLinear
,
685 VGboolean outputPremultiplied
)
687 struct vg_context
*ctx
= vg_current_context();
688 struct vg_image
*d
, *s
;
689 VGuint color_data
[256];
691 struct pipe_texture
*lut_texture
;
693 struct filter_info info
;
695 if (dst
== VG_INVALID_HANDLE
|| src
== VG_INVALID_HANDLE
) {
696 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
700 if (!redLUT
|| !greenLUT
|| !blueLUT
|| !alphaLUT
) {
701 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
705 d
= (struct vg_image
*)dst
;
706 s
= (struct vg_image
*)src
;
708 if (vg_image_overlaps(d
, s
)) {
709 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
713 for (i
= 0; i
< 256; ++i
) {
714 color_data
[i
] = blueLUT
[i
] << 24 | greenLUT
[i
] << 16 |
715 redLUT
[i
] << 8 | alphaLUT
[i
];
717 lut_texture
= create_texture_1d(ctx
, color_data
, 255);
726 info
.setup_shader
= &setup_lookup
;
727 info
.user_data
= NULL
;
728 info
.const_buffer
= buffer
;
729 info
.const_buffer_len
= 4 * sizeof(VGfloat
);
730 info
.tiling_mode
= VG_TILE_PAD
;
731 info
.extra_texture
= lut_texture
;
733 execute_filter(ctx
, &info
);
735 pipe_texture_reference(&lut_texture
, NULL
);
738 void vgLookupSingle(VGImage dst
, VGImage src
,
739 const VGuint
* lookupTable
,
740 VGImageChannel sourceChannel
,
741 VGboolean outputLinear
,
742 VGboolean outputPremultiplied
)
744 struct vg_context
*ctx
= vg_current_context();
745 struct vg_image
*d
, *s
;
746 struct pipe_texture
*lut_texture
;
748 struct filter_info info
;
749 VGuint color_data
[256];
752 if (dst
== VG_INVALID_HANDLE
|| src
== VG_INVALID_HANDLE
) {
753 vg_set_error(ctx
, VG_BAD_HANDLE_ERROR
);
757 if (!lookupTable
|| !is_aligned(lookupTable
)) {
758 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
762 if (sourceChannel
!= VG_RED
&& sourceChannel
!= VG_GREEN
&&
763 sourceChannel
!= VG_BLUE
&& sourceChannel
!= VG_ALPHA
) {
764 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
768 d
= (struct vg_image
*)dst
;
769 s
= (struct vg_image
*)src
;
771 if (vg_image_overlaps(d
, s
)) {
772 vg_set_error(ctx
, VG_ILLEGAL_ARGUMENT_ERROR
);
776 for (i
= 0; i
< 256; ++i
) {
777 VGuint rgba
= lookupTable
[i
];
778 VGubyte blue
, green
, red
, alpha
;
779 red
= (rgba
& 0xff000000)>>24;
780 green
= (rgba
& 0x00ff0000)>>16;
781 blue
= (rgba
& 0x0000ff00)>> 8;
782 alpha
= (rgba
& 0x000000ff)>> 0;
783 color_data
[i
] = blue
<< 24 | green
<< 16 |
786 lut_texture
= create_texture_1d(ctx
, color_data
, 256);
795 info
.setup_shader
= &setup_lookup_single
;
796 info
.user_data
= (void*)sourceChannel
;
797 info
.const_buffer
= buffer
;
798 info
.const_buffer_len
= 4 * sizeof(VGfloat
);
799 info
.tiling_mode
= VG_TILE_PAD
;
800 info
.extra_texture
= lut_texture
;
802 execute_filter(ctx
, &info
);
804 pipe_texture_reference(&lut_texture
, NULL
);