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 "util/u_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"
43 #include "util/u_sampler.h"
46 #include "asm_filters.h"
52 struct vg_shader * (*setup_shader)(struct vg_context *, void *);
54 const void *const_buffer;
55 VGint const_buffer_len;
56 VGTilingMode tiling_mode;
57 struct pipe_sampler_view *extra_texture_view;
60 static INLINE struct pipe_resource *create_texture_1d(struct vg_context *ctx,
61 const VGuint *color_data,
62 const VGint color_data_len)
64 struct pipe_context *pipe = ctx->pipe;
65 struct pipe_screen *screen = pipe->screen;
66 struct pipe_resource *tex = 0;
67 struct pipe_resource templ;
69 memset(&templ, 0, sizeof(templ));
70 templ.target = PIPE_TEXTURE_1D;
71 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
73 templ.width0 = color_data_len;
76 templ.bind = PIPE_BIND_SAMPLER_VIEW;
78 tex = screen->resource_create(screen, &templ);
80 { /* upload color_data */
81 struct pipe_transfer *transfer =
82 pipe_get_transfer(pipe, tex,
84 PIPE_TRANSFER_READ_WRITE ,
85 0, 0, tex->width0, tex->height0);
86 void *map = pipe->transfer_map(pipe, transfer);
87 memcpy(map, color_data, sizeof(VGint)*color_data_len);
88 pipe->transfer_unmap(pipe, transfer);
89 pipe->transfer_destroy(pipe, transfer);
95 static INLINE struct pipe_sampler_view *create_texture_1d_view(struct vg_context *ctx,
96 const VGuint *color_data,
97 const VGint color_data_len)
99 struct pipe_context *pipe = ctx->pipe;
100 struct pipe_resource *texture;
101 struct pipe_sampler_view view_templ;
102 struct pipe_sampler_view *view;
104 texture = create_texture_1d(ctx, color_data, color_data_len);
109 u_sampler_view_default_template(&view_templ, texture, texture->format);
110 view = pipe->create_sampler_view(pipe, texture, &view_templ);
111 /* want the texture to go away if the view is freed */
112 pipe_resource_reference(&texture, NULL);
117 static INLINE struct pipe_surface * setup_framebuffer(struct vg_image *dst)
119 struct vg_context *ctx = vg_current_context();
120 struct pipe_context *pipe = ctx->pipe;
121 struct pipe_framebuffer_state fb;
122 struct pipe_surface *dst_surf = pipe->screen->get_tex_surface(
123 pipe->screen, dst->sampler_view->texture, 0, 0, 0,
124 PIPE_BIND_RENDER_TARGET);
127 memset(&fb, 0, sizeof(fb));
128 fb.width = dst->x + dst_surf->width;
129 fb.height = dst->y + dst_surf->height;
131 fb.cbufs[0] = dst_surf;
134 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
137 cso_set_framebuffer(ctx->cso_context, &fb);
142 static void setup_viewport(struct vg_image *dst)
144 struct vg_context *ctx = vg_current_context();
145 vg_set_viewport(ctx, VEGA_Y0_TOP);
148 static void setup_blend()
150 struct vg_context *ctx = vg_current_context();
151 struct pipe_blend_state blend;
152 memset(&blend, 0, sizeof(blend));
153 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
154 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
155 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
156 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
157 if (ctx->state.vg.filter_channel_mask & VG_RED)
158 blend.rt[0].colormask |= PIPE_MASK_R;
159 if (ctx->state.vg.filter_channel_mask & VG_GREEN)
160 blend.rt[0].colormask |= PIPE_MASK_G;
161 if (ctx->state.vg.filter_channel_mask & VG_BLUE)
162 blend.rt[0].colormask |= PIPE_MASK_B;
163 if (ctx->state.vg.filter_channel_mask & VG_ALPHA)
164 blend.rt[0].colormask |= PIPE_MASK_A;
165 blend.rt[0].blend_enable = 0;
166 cso_set_blend(ctx->cso_context, &blend);
169 static void setup_constant_buffer(struct vg_context *ctx, const void *buffer,
172 struct pipe_context *pipe = ctx->pipe;
173 struct pipe_resource **cbuf = &ctx->filter.buffer;
175 /* We always need to get a new buffer, to keep the drivers simple and
176 * avoid gratuitous rendering synchronization. */
177 pipe_resource_reference(cbuf, NULL);
179 *cbuf = pipe_buffer_create(pipe->screen,
180 PIPE_BIND_CONSTANT_BUFFER,
184 st_no_flush_pipe_buffer_write(ctx, *cbuf,
185 0, param_bytes, buffer);
188 ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, *cbuf);
191 static void setup_samplers(struct vg_context *ctx, struct filter_info *info)
193 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
194 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
195 struct pipe_sampler_state sampler[3];
196 int num_samplers = 0;
197 int num_textures = 0;
203 sampler_views[0] = NULL;
204 sampler_views[1] = NULL;
205 sampler_views[2] = NULL;
206 sampler_views[3] = NULL;
208 memset(&sampler[0], 0, sizeof(struct pipe_sampler_state));
209 sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
210 sampler[0].wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
211 sampler[0].wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
212 sampler[0].min_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
213 sampler[0].mag_img_filter = PIPE_TEX_MIPFILTER_LINEAR;
214 sampler[0].normalized_coords = 1;
216 switch(info->tiling_mode) {
218 sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
219 sampler[0].wrap_t = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
220 memcpy(sampler[0].border_color,
221 ctx->state.vg.tile_fill_color,
222 sizeof(VGfloat) * 4);
225 sampler[0].wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
226 sampler[0].wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
229 sampler[0].wrap_s = PIPE_TEX_WRAP_REPEAT;
230 sampler[0].wrap_t = PIPE_TEX_WRAP_REPEAT;
232 case VG_TILE_REFLECT:
233 sampler[0].wrap_s = PIPE_TEX_WRAP_MIRROR_REPEAT;
234 sampler[0].wrap_t = PIPE_TEX_WRAP_MIRROR_REPEAT;
237 debug_assert(!"Unknown tiling mode");
240 samplers[0] = &sampler[0];
241 sampler_views[0] = info->src->sampler_view;
245 if (info->extra_texture_view) {
246 memcpy(&sampler[1], &sampler[0], sizeof(struct pipe_sampler_state));
247 samplers[1] = &sampler[1];
248 sampler_views[1] = info->extra_texture_view;
254 cso_set_samplers(ctx->cso_context, num_samplers, (const struct pipe_sampler_state **)samplers);
255 cso_set_fragment_sampler_views(ctx->cso_context, num_textures, sampler_views);
258 static struct vg_shader * setup_color_matrix(struct vg_context *ctx, void *user_data)
260 struct vg_shader *shader =
261 shader_create_from_text(ctx->pipe, color_matrix_asm, 200,
262 PIPE_SHADER_FRAGMENT);
263 cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
267 static struct vg_shader * setup_convolution(struct vg_context *ctx, void *user_data)
270 VGint num_consts = (VGint)(long)(user_data);
271 struct vg_shader *shader;
273 snprintf(buffer, 1023, convolution_asm, num_consts, num_consts / 2 + 1);
275 shader = shader_create_from_text(ctx->pipe, buffer, 200,
276 PIPE_SHADER_FRAGMENT);
278 cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
282 static struct vg_shader * setup_lookup(struct vg_context *ctx, void *user_data)
284 struct vg_shader *shader =
285 shader_create_from_text(ctx->pipe, lookup_asm,
286 200, PIPE_SHADER_FRAGMENT);
288 cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
293 static struct vg_shader * setup_lookup_single(struct vg_context *ctx, void *user_data)
296 VGImageChannel channel = (VGImageChannel)(user_data);
297 struct vg_shader *shader;
301 snprintf(buffer, 1023, lookup_single_asm, "xxxx");
304 snprintf(buffer, 1023, lookup_single_asm, "yyyy");
307 snprintf(buffer, 1023, lookup_single_asm, "zzzz");
310 snprintf(buffer, 1023, lookup_single_asm, "wwww");
313 debug_assert(!"Unknown color channel");
316 shader = shader_create_from_text(ctx->pipe, buffer, 200,
317 PIPE_SHADER_FRAGMENT);
319 cso_set_fragment_shader_handle(ctx->cso_context, shader->driver);
323 static void execute_filter(struct vg_context *ctx,
324 struct filter_info *info)
326 struct pipe_surface *dst_surf;
327 struct vg_shader *shader;
329 cso_save_framebuffer(ctx->cso_context);
330 cso_save_fragment_shader(ctx->cso_context);
331 cso_save_viewport(ctx->cso_context);
332 cso_save_blend(ctx->cso_context);
333 cso_save_samplers(ctx->cso_context);
334 cso_save_fragment_sampler_views(ctx->cso_context);
336 dst_surf = setup_framebuffer(info->dst);
337 setup_viewport(info->dst);
339 setup_constant_buffer(ctx, info->const_buffer, info->const_buffer_len);
340 shader = info->setup_shader(ctx, info->user_data);
341 setup_samplers(ctx, info);
343 renderer_draw_texture(ctx->renderer,
344 info->src->sampler_view->texture,
345 info->dst->x, info->dst->y,
346 info->dst->x + info->dst->width,
347 info->dst->y + info->dst->height,
348 info->dst->x, info->dst->y,
349 info->dst->x + info->dst->width,
350 info->dst->y + info->dst->height);
352 cso_restore_framebuffer(ctx->cso_context);
353 cso_restore_fragment_shader(ctx->cso_context);
354 cso_restore_viewport(ctx->cso_context);
355 cso_restore_blend(ctx->cso_context);
356 cso_restore_samplers(ctx->cso_context);
357 cso_restore_fragment_sampler_views(ctx->cso_context);
359 vg_shader_destroy(ctx, shader);
361 pipe_surface_reference(&dst_surf, NULL);
364 void vgColorMatrix(VGImage dst, VGImage src,
365 const VGfloat * matrix)
367 struct vg_context *ctx = vg_current_context();
368 struct vg_image *d, *s;
369 struct filter_info info;
371 if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
372 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
375 if (!matrix || !is_aligned(matrix)) {
376 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
380 d = (struct vg_image*)dst;
381 s = (struct vg_image*)src;
383 if (vg_image_overlaps(d, s)) {
384 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
390 info.setup_shader = &setup_color_matrix;
391 info.user_data = NULL;
392 info.const_buffer = matrix;
393 info.const_buffer_len = 20 * sizeof(VGfloat);
394 info.tiling_mode = VG_TILE_PAD;
395 info.extra_texture_view = NULL;
396 execute_filter(ctx, &info);
399 static VGfloat texture_offset(VGfloat width, VGint kernelSize, VGint current, VGint shift)
401 VGfloat diff = current - shift;
406 void vgConvolve(VGImage dst, VGImage src,
407 VGint kernelWidth, VGint kernelHeight,
408 VGint shiftX, VGint shiftY,
409 const VGshort * kernel,
412 VGTilingMode tilingMode)
414 struct vg_context *ctx = vg_current_context();
419 struct vg_image *d, *s;
420 VGint kernel_size = kernelWidth * kernelHeight;
421 struct filter_info info;
422 const VGint max_kernel_size = vgGeti(VG_MAX_KERNEL_SIZE);
424 if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
425 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
429 if (kernelWidth <= 0 || kernelHeight <= 0 ||
430 kernelWidth > max_kernel_size || kernelHeight > max_kernel_size) {
431 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
435 if (!kernel || !is_aligned_to(kernel, 2)) {
436 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
440 if (tilingMode < VG_TILE_FILL ||
441 tilingMode > VG_TILE_REFLECT) {
442 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
446 d = (struct vg_image*)dst;
447 s = (struct vg_image*)src;
449 if (vg_image_overlaps(d, s)) {
450 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
454 vg_validate_state(ctx);
456 buffer_len = 8 + 2 * 4 * kernel_size;
457 buffer = (VGfloat*)malloc(buffer_len * sizeof(VGfloat));
461 buffer[2] = 2.f; /*unused*/
462 buffer[3] = 4.f; /*unused*/
464 buffer[4] = kernelWidth * kernelHeight;
470 for (j = 0; j < kernelHeight; ++j) {
471 for (i = 0; i < kernelWidth; ++i) {
472 VGint index = j * kernelWidth + i;
475 x = texture_offset(s->width, kernelWidth, i, shiftX);
476 y = texture_offset(s->height, kernelHeight, j, shiftY);
478 buffer[idx + index*4 + 0] = x;
479 buffer[idx + index*4 + 1] = y;
480 buffer[idx + index*4 + 2] = 0.f;
481 buffer[idx + index*4 + 3] = 0.f;
484 idx += kernel_size * 4;
486 for (j = 0; j < kernelHeight; ++j) {
487 for (i = 0; i < kernelWidth; ++i) {
488 /* transpose the kernel */
489 VGint index = j * kernelWidth + i;
490 VGint kindex = (kernelWidth - i - 1) * kernelHeight + (kernelHeight - j - 1);
491 buffer[idx + index*4 + 0] = kernel[kindex];
492 buffer[idx + index*4 + 1] = kernel[kindex];
493 buffer[idx + index*4 + 2] = kernel[kindex];
494 buffer[idx + index*4 + 3] = kernel[kindex];
500 info.setup_shader = &setup_convolution;
501 info.user_data = (void*)(long)(buffer_len/4);
502 info.const_buffer = buffer;
503 info.const_buffer_len = buffer_len * sizeof(VGfloat);
504 info.tiling_mode = tilingMode;
505 info.extra_texture_view = NULL;
506 execute_filter(ctx, &info);
511 void vgSeparableConvolve(VGImage dst, VGImage src,
514 VGint shiftX, VGint shiftY,
515 const VGshort * kernelX,
516 const VGshort * kernelY,
519 VGTilingMode tilingMode)
521 struct vg_context *ctx = vg_current_context();
524 const VGint max_kernel_size = vgGeti(VG_MAX_SEPARABLE_KERNEL_SIZE);
526 if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
527 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
531 if (kernelWidth <= 0 || kernelHeight <= 0 ||
532 kernelWidth > max_kernel_size || kernelHeight > max_kernel_size) {
533 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
537 if (!kernelX || !kernelY ||
538 !is_aligned_to(kernelX, 2) || !is_aligned_to(kernelY, 2)) {
539 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
542 if (tilingMode < VG_TILE_FILL ||
543 tilingMode > VG_TILE_REFLECT) {
544 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
547 kernel = malloc(sizeof(VGshort)*kernelWidth*kernelHeight);
548 for (i = 0; i < kernelWidth; ++i) {
549 for (j = 0; j < kernelHeight; ++j) {
550 kernel[idx] = kernelX[i] * kernelY[j];
554 vgConvolve(dst, src, kernelWidth, kernelHeight, shiftX, shiftY,
555 kernel, scale, bias, tilingMode);
559 static INLINE VGfloat compute_gaussian_componenet(VGfloat x, VGfloat y,
560 VGfloat stdDeviationX,
561 VGfloat stdDeviationY)
563 VGfloat mult = 1 / ( 2 * M_PI * stdDeviationX * stdDeviationY);
564 VGfloat e = exp( - ( pow(x, 2)/(2*pow(stdDeviationX, 2)) +
565 pow(y, 2)/(2*pow(stdDeviationY, 2)) ) );
569 static INLINE VGint compute_kernel_size(VGfloat deviation)
571 VGint size = ceil(2.146 * deviation);
577 static void compute_gaussian_kernel(VGfloat *kernel,
578 VGint width, VGint height,
579 VGfloat stdDeviationX,
580 VGfloat stdDeviationY)
583 VGfloat scale = 0.0f;
585 for (j = 0; j < height; ++j) {
586 for (i = 0; i < width; ++i) {
587 VGint idx = (height - j -1) * width + (width - i -1);
588 kernel[idx] = compute_gaussian_componenet(i-(ceil(width/2))-1,
590 stdDeviationX, stdDeviationY);
591 scale += kernel[idx];
595 for (j = 0; j < height; ++j) {
596 for (i = 0; i < width; ++i) {
597 VGint idx = j * width + i;
598 kernel[idx] /= scale;
603 void vgGaussianBlur(VGImage dst, VGImage src,
604 VGfloat stdDeviationX,
605 VGfloat stdDeviationY,
606 VGTilingMode tilingMode)
608 struct vg_context *ctx = vg_current_context();
609 struct vg_image *d, *s;
610 VGfloat *buffer, *kernel;
611 VGint kernel_width, kernel_height, kernel_size;
614 struct filter_info info;
616 if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
617 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
620 if (stdDeviationX <= 0 || stdDeviationY <= 0) {
621 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
625 if (tilingMode < VG_TILE_FILL ||
626 tilingMode > VG_TILE_REFLECT) {
627 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
631 d = (struct vg_image*)dst;
632 s = (struct vg_image*)src;
634 if (vg_image_overlaps(d, s)) {
635 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
639 kernel_width = compute_kernel_size(stdDeviationX);
640 kernel_height = compute_kernel_size(stdDeviationY);
641 kernel_size = kernel_width * kernel_height;
642 kernel = malloc(sizeof(VGfloat)*kernel_size);
643 compute_gaussian_kernel(kernel, kernel_width, kernel_height,
644 stdDeviationX, stdDeviationY);
646 buffer_len = 8 + 2 * 4 * kernel_size;
647 buffer = (VGfloat*)malloc(buffer_len * sizeof(VGfloat));
651 buffer[2] = 2.f; /*unused*/
652 buffer[3] = 4.f; /*unused*/
654 buffer[4] = kernel_width * kernel_height;
655 buffer[5] = 1.f;/*scale*/
656 buffer[6] = 0.f;/*bias*/
660 for (j = 0; j < kernel_height; ++j) {
661 for (i = 0; i < kernel_width; ++i) {
662 VGint index = j * kernel_width + i;
665 x = texture_offset(s->width, kernel_width, i, kernel_width/2);
666 y = texture_offset(s->height, kernel_height, j, kernel_height/2);
668 buffer[idx + index*4 + 0] = x;
669 buffer[idx + index*4 + 1] = y;
670 buffer[idx + index*4 + 2] = 0.f;
671 buffer[idx + index*4 + 3] = 0.f;
674 idx += kernel_size * 4;
676 for (j = 0; j < kernel_height; ++j) {
677 for (i = 0; i < kernel_width; ++i) {
678 /* transpose the kernel */
679 VGint index = j * kernel_width + i;
680 VGint kindex = (kernel_width - i - 1) * kernel_height + (kernel_height - j - 1);
681 buffer[idx + index*4 + 0] = kernel[kindex];
682 buffer[idx + index*4 + 1] = kernel[kindex];
683 buffer[idx + index*4 + 2] = kernel[kindex];
684 buffer[idx + index*4 + 3] = kernel[kindex];
690 info.setup_shader = &setup_convolution;
691 info.user_data = (void*)(long)(buffer_len/4);
692 info.const_buffer = buffer;
693 info.const_buffer_len = buffer_len * sizeof(VGfloat);
694 info.tiling_mode = tilingMode;
695 info.extra_texture_view = NULL;
696 execute_filter(ctx, &info);
702 void vgLookup(VGImage dst, VGImage src,
703 const VGubyte * redLUT,
704 const VGubyte * greenLUT,
705 const VGubyte * blueLUT,
706 const VGubyte * alphaLUT,
707 VGboolean outputLinear,
708 VGboolean outputPremultiplied)
710 struct vg_context *ctx = vg_current_context();
711 struct vg_image *d, *s;
712 VGuint color_data[256];
714 struct pipe_sampler_view *lut_texture_view;
716 struct filter_info info;
718 if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
719 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
723 if (!redLUT || !greenLUT || !blueLUT || !alphaLUT) {
724 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
728 d = (struct vg_image*)dst;
729 s = (struct vg_image*)src;
731 if (vg_image_overlaps(d, s)) {
732 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
736 for (i = 0; i < 256; ++i) {
737 color_data[i] = blueLUT[i] << 24 | greenLUT[i] << 16 |
738 redLUT[i] << 8 | alphaLUT[i];
740 lut_texture_view = create_texture_1d_view(ctx, color_data, 255);
749 info.setup_shader = &setup_lookup;
750 info.user_data = NULL;
751 info.const_buffer = buffer;
752 info.const_buffer_len = 4 * sizeof(VGfloat);
753 info.tiling_mode = VG_TILE_PAD;
754 info.extra_texture_view = lut_texture_view;
756 execute_filter(ctx, &info);
758 pipe_sampler_view_reference(&lut_texture_view, NULL);
761 void vgLookupSingle(VGImage dst, VGImage src,
762 const VGuint * lookupTable,
763 VGImageChannel sourceChannel,
764 VGboolean outputLinear,
765 VGboolean outputPremultiplied)
767 struct vg_context *ctx = vg_current_context();
768 struct vg_image *d, *s;
769 struct pipe_sampler_view *lut_texture_view;
771 struct filter_info info;
772 VGuint color_data[256];
775 if (dst == VG_INVALID_HANDLE || src == VG_INVALID_HANDLE) {
776 vg_set_error(ctx, VG_BAD_HANDLE_ERROR);
780 if (!lookupTable || !is_aligned(lookupTable)) {
781 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
785 if (sourceChannel != VG_RED && sourceChannel != VG_GREEN &&
786 sourceChannel != VG_BLUE && sourceChannel != VG_ALPHA) {
787 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
791 d = (struct vg_image*)dst;
792 s = (struct vg_image*)src;
794 if (vg_image_overlaps(d, s)) {
795 vg_set_error(ctx, VG_ILLEGAL_ARGUMENT_ERROR);
799 for (i = 0; i < 256; ++i) {
800 VGuint rgba = lookupTable[i];
801 VGubyte blue, green, red, alpha;
802 red = (rgba & 0xff000000)>>24;
803 green = (rgba & 0x00ff0000)>>16;
804 blue = (rgba & 0x0000ff00)>> 8;
805 alpha = (rgba & 0x000000ff)>> 0;
806 color_data[i] = blue << 24 | green << 16 |
809 lut_texture_view = create_texture_1d_view(ctx, color_data, 256);
818 info.setup_shader = &setup_lookup_single;
819 info.user_data = (void*)sourceChannel;
820 info.const_buffer = buffer;
821 info.const_buffer_len = 4 * sizeof(VGfloat);
822 info.tiling_mode = VG_TILE_PAD;
823 info.extra_texture_view = lut_texture_view;
825 execute_filter(ctx, &info);
827 pipe_sampler_view_reference(&lut_texture_view, NULL);