Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / state_trackers / vega / paint.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 *
25 **************************************************************************/
26
27 #include "paint.h"
28
29 #include "shaders_cache.h"
30 #include "matrix.h"
31 #include "image.h"
32 #include "st_inlines.h"
33
34 #include "pipe/p_compiler.h"
35 #include "pipe/p_inlines.h"
36
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
39
40 #include "cso_cache/cso_context.h"
41
42 struct vg_paint {
43 struct vg_object base;
44
45 VGPaintType type;
46
47 struct {
48 VGfloat color[4];
49 VGint colori[4];
50 } solid;
51
52 struct {
53 VGColorRampSpreadMode spread;
54 VGuint color_data[1024];
55 struct {
56 VGfloat coords[4];
57 VGint coordsi[4];
58 } linear;
59 struct {
60 VGfloat vals[5];
61 VGint valsi[5];
62 } radial;
63 struct pipe_texture *texture;
64 struct pipe_sampler_state sampler;
65
66 VGfloat *ramp_stops;
67 VGint *ramp_stopsi;
68 VGint num_stops;
69
70 VGboolean color_ramps_premultiplied;
71 } gradient;
72
73 struct {
74 struct pipe_texture *texture;
75 VGTilingMode tiling_mode;
76 struct pipe_sampler_state sampler;
77 } pattern;
78
79 struct pipe_constant_buffer cbuf;
80 struct pipe_shader_state fs_state;
81 void *fs;
82 };
83
84 static INLINE VGuint mix_pixels(VGuint p1, VGuint a, VGuint p2, VGuint b)
85 {
86 VGuint t = (p1 & 0xff00ff) * a + (p2 & 0xff00ff) * b;
87 t >>= 8; t &= 0xff00ff;
88
89 p1 = ((p1 >> 8) & 0xff00ff) * a + ((p2 >> 8) & 0xff00ff) * b;
90 p1 &= 0xff00ff00; p1 |= t;
91
92 return p1;
93 }
94
95 static INLINE VGuint float4_to_argb(const VGfloat *clr)
96 {
97 return float_to_ubyte(clr[3]) << 24 |
98 float_to_ubyte(clr[0]) << 16 |
99 float_to_ubyte(clr[1]) << 8 |
100 float_to_ubyte(clr[2]) << 0;
101 }
102
103 static INLINE void create_gradient_data(const VGfloat *ramp_stops,
104 VGint num,
105 VGuint *data,
106 VGint size)
107 {
108 VGint i;
109 VGint pos = 0;
110 VGfloat fpos = 0, incr = 1.f / size;
111 VGuint last_color;
112
113 while (fpos < ramp_stops[0]) {
114 data[pos] = float4_to_argb(ramp_stops + 1);
115 fpos += incr;
116 ++pos;
117 }
118
119 for (i = 0; i < num - 1; ++i) {
120 VGint rcur = 5 * i;
121 VGint rnext = 5 * (i + 1);
122 VGfloat delta = 1.f/(ramp_stops[rnext] - ramp_stops[rcur]);
123 while (fpos < ramp_stops[rnext] && pos < size) {
124 VGint dist = 256 * ((fpos - ramp_stops[rcur]) * delta);
125 VGint idist = 256 - dist;
126 VGuint current_color = float4_to_argb(ramp_stops + rcur + 1);
127 VGuint next_color = float4_to_argb(ramp_stops + rnext + 1);
128 data[pos] = mix_pixels(current_color, idist,
129 next_color, dist);
130 fpos += incr;
131 ++pos;
132 }
133 }
134
135 last_color = float4_to_argb(ramp_stops + ((num - 1) * 5 + 1));
136 while (pos < size) {
137 data[pos] = last_color;
138 ++pos;
139 }
140 data[size-1] = last_color;
141 }
142
143 static INLINE struct pipe_texture *create_gradient_texture(struct vg_paint *p)
144 {
145 struct pipe_context *pipe = p->base.ctx->pipe;
146 struct pipe_screen *screen = pipe->screen;
147 struct pipe_texture *tex = 0;
148 struct pipe_texture templ;
149
150 memset(&templ, 0, sizeof(templ));
151 templ.target = PIPE_TEXTURE_1D;
152 templ.format = PIPE_FORMAT_A8R8G8B8_UNORM;
153 templ.last_level = 0;
154 templ.width0 = 1024;
155 templ.height0 = 1;
156 templ.depth0 = 1;
157 templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
158
159 tex = screen->texture_create(screen, &templ);
160
161 { /* upload color_data */
162 struct pipe_transfer *transfer =
163 st_no_flush_get_tex_transfer(p->base.ctx, tex, 0, 0, 0,
164 PIPE_TRANSFER_WRITE, 0, 0, 1024, 1);
165 void *map = screen->transfer_map(screen, transfer);
166 memcpy(map, p->gradient.color_data, sizeof(VGint)*1024);
167 screen->transfer_unmap(screen, transfer);
168 screen->tex_transfer_destroy(transfer);
169 }
170
171 return tex;
172 }
173
174 struct vg_paint * paint_create(struct vg_context *ctx)
175 {
176 struct vg_paint *paint = CALLOC_STRUCT(vg_paint);
177 const VGfloat default_color[] = {0.0f, 0.0f, 0.0f, 1.0f};
178 const VGfloat def_ling[] = {0.0f, 0.0f, 1.0f, 0.0f};
179 const VGfloat def_radg[] = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
180 vg_init_object(&paint->base, ctx, VG_OBJECT_PAINT);
181 vg_context_add_object(ctx, VG_OBJECT_PAINT, paint);
182
183 paint->type = VG_PAINT_TYPE_COLOR;
184 memcpy(paint->solid.color, default_color,
185 4 * sizeof(VGfloat));
186 paint->gradient.spread = VG_COLOR_RAMP_SPREAD_PAD;
187 memcpy(paint->gradient.linear.coords, def_ling,
188 4 * sizeof(VGfloat));
189 memcpy(paint->gradient.radial.vals, def_radg,
190 5 * sizeof(VGfloat));
191
192 paint->gradient.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
193 paint->gradient.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
194 paint->gradient.sampler.min_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
195 paint->gradient.sampler.mag_img_filter = PIPE_TEX_MIPFILTER_NEAREST;
196 paint->gradient.sampler.normalized_coords = 1;
197
198 memcpy(&paint->pattern.sampler,
199 &paint->gradient.sampler,
200 sizeof(struct pipe_sampler_state));
201
202 return paint;
203 }
204
205 void paint_destroy(struct vg_paint *paint)
206 {
207 struct vg_context *ctx = paint->base.ctx;
208 if (paint->pattern.texture)
209 pipe_texture_reference(&paint->pattern.texture, NULL);
210 if (ctx)
211 vg_context_remove_object(ctx, VG_OBJECT_PAINT, paint);
212
213 free(paint->gradient.ramp_stopsi);
214 free(paint->gradient.ramp_stops);
215 free(paint);
216 }
217
218 void paint_set_color(struct vg_paint *paint,
219 const VGfloat *color)
220 {
221 paint->solid.color[0] = color[0];
222 paint->solid.color[1] = color[1];
223 paint->solid.color[2] = color[2];
224 paint->solid.color[3] = color[3];
225
226 paint->solid.colori[0] = FLT_TO_INT(color[0]);
227 paint->solid.colori[1] = FLT_TO_INT(color[1]);
228 paint->solid.colori[2] = FLT_TO_INT(color[2]);
229 paint->solid.colori[3] = FLT_TO_INT(color[3]);
230 }
231
232 static INLINE void paint_color_buffer(struct vg_paint *paint, void *buffer)
233 {
234 VGfloat *map = (VGfloat*)buffer;
235 memcpy(buffer, paint->solid.color, 4 * sizeof(VGfloat));
236 map[4] = 0.f;
237 map[5] = 1.f;
238 map[6] = 2.f;
239 map[7] = 4.f;
240 }
241
242 static INLINE void paint_linear_gradient_buffer(struct vg_paint *paint, void *buffer)
243 {
244 struct vg_context *ctx = paint->base.ctx;
245 VGfloat *map = (VGfloat*)buffer;
246
247 map[0] = paint->gradient.linear.coords[2] - paint->gradient.linear.coords[0];
248 map[1] = paint->gradient.linear.coords[3] - paint->gradient.linear.coords[1];
249 map[2] = 1.f / (map[0] * map[0] + map[1] * map[1]);
250 map[3] = 1.f;
251
252 map[4] = 0.f;
253 map[5] = 1.f;
254 map[6] = 2.f;
255 map[7] = 4.f;
256 {
257 struct matrix mat;
258 struct matrix inv;
259 matrix_load_identity(&mat);
260 matrix_translate(&mat, -paint->gradient.linear.coords[0], -paint->gradient.linear.coords[1]);
261 memcpy(&inv, &ctx->state.vg.fill_paint_to_user_matrix,
262 sizeof(struct matrix));
263 matrix_invert(&inv);
264 matrix_mult(&inv, &mat);
265 memcpy(&mat, &inv,
266 sizeof(struct matrix));
267
268 map[8] = mat.m[0]; map[9] = mat.m[3]; map[10] = mat.m[6]; map[11] = 0.f;
269 map[12] = mat.m[1]; map[13] = mat.m[4]; map[14] = mat.m[7]; map[15] = 0.f;
270 map[16] = mat.m[2]; map[17] = mat.m[5]; map[18] = mat.m[8]; map[19] = 0.f;
271 }
272 #if 0
273 debug_printf("Coords (%f, %f, %f, %f)\n",
274 map[0], map[1], map[2], map[3]);
275 #endif
276 }
277
278
279 static INLINE void paint_radial_gradient_buffer(struct vg_paint *paint, void *buffer)
280 {
281 VGfloat *radialCoords = paint->gradient.radial.vals;
282 struct vg_context *ctx = paint->base.ctx;
283
284 VGfloat *map = (VGfloat*)buffer;
285
286 map[0] = radialCoords[0] - radialCoords[2];
287 map[1] = radialCoords[1] - radialCoords[3];
288 map[2] = -map[0] * map[0] - map[1] * map[1] +
289 radialCoords[4] * radialCoords[4];
290 map[3] = 1.f;
291
292 map[4] = 0.f;
293 map[5] = 1.f;
294 map[6] = 2.f;
295 map[7] = 4.f;
296
297 {
298 struct matrix mat;
299 struct matrix inv;
300 matrix_load_identity(&mat);
301 matrix_translate(&mat, -radialCoords[2], -radialCoords[3]);
302 memcpy(&inv, &ctx->state.vg.fill_paint_to_user_matrix,
303 sizeof(struct matrix));
304 matrix_invert(&inv);
305 matrix_mult(&inv, &mat);
306 memcpy(&mat, &inv,
307 sizeof(struct matrix));
308
309 map[8] = mat.m[0]; map[9] = mat.m[3]; map[10] = mat.m[6]; map[11] = 0.f;
310 map[12] = mat.m[1]; map[13] = mat.m[4]; map[14] = mat.m[7]; map[15] = 0.f;
311 map[16] = mat.m[2]; map[17] = mat.m[5]; map[18] = mat.m[8]; map[19] = 0.f;
312 }
313
314 #if 0
315 debug_printf("Coords (%f, %f, %f, %f)\n",
316 map[0], map[1], map[2], map[3]);
317 #endif
318 }
319
320
321 static INLINE void paint_pattern_buffer(struct vg_paint *paint, void *buffer)
322 {
323 struct vg_context *ctx = paint->base.ctx;
324
325 VGfloat *map = (VGfloat *)buffer;
326 memcpy(map, paint->solid.color, 4 * sizeof(VGfloat));
327
328 map[4] = 0.f;
329 map[5] = 1.f;
330 map[6] = paint->pattern.texture->width0;
331 map[7] = paint->pattern.texture->height0;
332 {
333 struct matrix mat;
334 memcpy(&mat, &ctx->state.vg.fill_paint_to_user_matrix,
335 sizeof(struct matrix));
336 matrix_invert(&mat);
337 {
338 struct matrix pm;
339 memcpy(&pm, &ctx->state.vg.path_user_to_surface_matrix,
340 sizeof(struct matrix));
341 matrix_invert(&pm);
342 matrix_mult(&pm, &mat);
343 memcpy(&mat, &pm, sizeof(struct matrix));
344 }
345
346 map[8] = mat.m[0]; map[9] = mat.m[3]; map[10] = mat.m[6]; map[11] = 0.f;
347 map[12] = mat.m[1]; map[13] = mat.m[4]; map[14] = mat.m[7]; map[15] = 0.f;
348 map[16] = mat.m[2]; map[17] = mat.m[5]; map[18] = mat.m[8]; map[19] = 0.f;
349 }
350 }
351
352 void paint_set_type(struct vg_paint *paint, VGPaintType type)
353 {
354 paint->type = type;
355 }
356
357 void paint_set_ramp_stops(struct vg_paint *paint, const VGfloat *stops,
358 int num)
359 {
360 const VGfloat default_stops[] = {0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
361 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
362 VGint i;
363 const VGint num_stops = num / 5;
364 VGfloat last_coord;
365
366 paint->gradient.num_stops = num;
367 if (num) {
368 free(paint->gradient.ramp_stops);
369 paint->gradient.ramp_stops = malloc(sizeof(VGfloat)*num);
370 memcpy(paint->gradient.ramp_stops, stops, sizeof(VGfloat)*num);
371 } else
372 return;
373
374 /* stops must be in increasing order. the last stop is 1.0. if the
375 * first one is bigger than 1 then the whole sequence is invalid*/
376 if (stops[0] > 1) {
377 stops = default_stops;
378 num = 10;
379 }
380 last_coord = stops[0];
381 for (i = 1; i < num_stops; ++i) {
382 VGint idx = 5 * i;
383 VGfloat coord = stops[idx];
384 if (!floatsEqual(last_coord, coord) && coord < last_coord) {
385 stops = default_stops;
386 num = 10;
387 break;
388 }
389 last_coord = coord;
390 }
391
392 create_gradient_data(stops, num / 5, paint->gradient.color_data,
393 1024);
394
395 if (paint->gradient.texture) {
396 pipe_texture_reference(&paint->gradient.texture, NULL);
397 paint->gradient.texture = 0;
398 }
399
400 paint->gradient.texture = create_gradient_texture(paint);
401 }
402
403 void paint_set_colori(struct vg_paint *p,
404 VGuint rgba)
405 {
406 p->solid.color[0] = ((rgba >> 24) & 0xff) / 255.f;
407 p->solid.color[1] = ((rgba >> 16) & 0xff) / 255.f;
408 p->solid.color[2] = ((rgba >> 8) & 0xff) / 255.f;
409 p->solid.color[3] = ((rgba >> 0) & 0xff) / 255.f;
410 }
411
412 VGuint paint_colori(struct vg_paint *p)
413 {
414 #define F2B(f) (float_to_ubyte(f))
415
416 return ((F2B(p->solid.color[0]) << 24) |
417 (F2B(p->solid.color[1]) << 16) |
418 (F2B(p->solid.color[2]) << 8) |
419 (F2B(p->solid.color[3]) << 0));
420 #undef F2B
421 }
422
423 void paint_set_linear_gradient(struct vg_paint *paint,
424 const VGfloat *coords)
425 {
426 memcpy(paint->gradient.linear.coords, coords, sizeof(VGfloat) * 4);
427 }
428
429 void paint_set_spread_mode(struct vg_paint *paint,
430 VGint mode)
431 {
432 paint->gradient.spread = mode;
433 switch(mode) {
434 case VG_COLOR_RAMP_SPREAD_PAD:
435 paint->gradient.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
436 break;
437 case VG_COLOR_RAMP_SPREAD_REPEAT:
438 paint->gradient.sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
439 break;
440 case VG_COLOR_RAMP_SPREAD_REFLECT:
441 paint->gradient.sampler.wrap_s = PIPE_TEX_WRAP_MIRROR_REPEAT;
442 break;
443 }
444 }
445
446 VGColorRampSpreadMode paint_spread_mode(struct vg_paint *paint)
447 {
448 return paint->gradient.spread;
449 }
450
451 void paint_set_radial_gradient(struct vg_paint *paint,
452 const VGfloat *values)
453 {
454 memcpy(paint->gradient.radial.vals, values, sizeof(VGfloat) * 5);
455 }
456
457 void paint_set_pattern(struct vg_paint *paint,
458 struct vg_image *img)
459 {
460 if (paint->pattern.texture)
461 pipe_texture_reference(&paint->pattern.texture, NULL);
462
463 paint->pattern.texture = 0;
464 pipe_texture_reference(&paint->pattern.texture,
465 img->texture);
466 }
467
468 void paint_set_pattern_tiling(struct vg_paint *paint,
469 VGTilingMode mode)
470 {
471 paint->pattern.tiling_mode = mode;
472
473 switch(mode) {
474 case VG_TILE_FILL:
475 paint->pattern.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
476 paint->pattern.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_BORDER;
477 break;
478 case VG_TILE_PAD:
479 paint->pattern.sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
480 paint->pattern.sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
481 break;
482 case VG_TILE_REPEAT:
483 paint->pattern.sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
484 paint->pattern.sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
485 break;
486 case VG_TILE_REFLECT:
487 paint->pattern.sampler.wrap_s = PIPE_TEX_WRAP_MIRROR_REPEAT;
488 paint->pattern.sampler.wrap_t = PIPE_TEX_WRAP_MIRROR_REPEAT;
489 break;
490 default:
491 debug_assert("!Unknown tiling mode");
492 }
493 }
494
495 void paint_get_color(struct vg_paint *paint,
496 VGfloat *color)
497 {
498 color[0] = paint->solid.color[0];
499 color[1] = paint->solid.color[1];
500 color[2] = paint->solid.color[2];
501 color[3] = paint->solid.color[3];
502 }
503
504 void paint_ramp_stops(struct vg_paint *paint, VGfloat *stops,
505 int num)
506 {
507 memcpy(stops, paint->gradient.ramp_stops, sizeof(VGfloat)*num);
508 }
509
510 void paint_linear_gradient(struct vg_paint *paint,
511 VGfloat *coords)
512 {
513 memcpy(coords, paint->gradient.linear.coords, sizeof(VGfloat)*4);
514 }
515
516 void paint_radial_gradient(struct vg_paint *paint,
517 VGfloat *coords)
518 {
519 memcpy(coords, paint->gradient.radial.vals, sizeof(VGfloat)*5);
520 }
521
522 int paint_num_ramp_stops(struct vg_paint *paint)
523 {
524 return paint->gradient.num_stops;
525 }
526
527 VGPaintType paint_type(struct vg_paint *paint)
528 {
529 return paint->type;
530 }
531
532 void paint_set_coloriv(struct vg_paint *paint,
533 const VGint *color)
534 {
535 paint->solid.color[0] = color[0];
536 paint->solid.color[1] = color[1];
537 paint->solid.color[2] = color[2];
538 paint->solid.color[3] = color[3];
539
540 paint->solid.colori[0] = color[0];
541 paint->solid.colori[1] = color[1];
542 paint->solid.colori[2] = color[2];
543 paint->solid.colori[3] = color[3];
544 }
545
546 void paint_get_coloriv(struct vg_paint *paint,
547 VGint *color)
548 {
549 color[0] = paint->solid.colori[0];
550 color[1] = paint->solid.colori[1];
551 color[2] = paint->solid.colori[2];
552 color[3] = paint->solid.colori[3];
553 }
554
555 void paint_set_color_ramp_premultiplied(struct vg_paint *paint,
556 VGboolean set)
557 {
558 paint->gradient.color_ramps_premultiplied = set;
559 }
560
561 VGboolean paint_color_ramp_premultiplied(struct vg_paint *paint)
562 {
563 return paint->gradient.color_ramps_premultiplied;
564 }
565
566 void paint_set_ramp_stopsi(struct vg_paint *paint, const VGint *stops,
567 int num)
568 {
569 if (num) {
570 free(paint->gradient.ramp_stopsi);
571 paint->gradient.ramp_stopsi = malloc(sizeof(VGint)*num);
572 memcpy(paint->gradient.ramp_stopsi, stops, sizeof(VGint)*num);
573 }
574 }
575
576 void paint_ramp_stopsi(struct vg_paint *paint, VGint *stops,
577 int num)
578 {
579 memcpy(stops, paint->gradient.ramp_stopsi, sizeof(VGint)*num);
580 }
581
582 void paint_set_linear_gradienti(struct vg_paint *paint,
583 const VGint *coords)
584 {
585 memcpy(paint->gradient.linear.coordsi, coords, sizeof(VGint) * 4);
586 }
587
588 void paint_linear_gradienti(struct vg_paint *paint,
589 VGint *coords)
590 {
591 memcpy(coords, paint->gradient.linear.coordsi, sizeof(VGint)*4);
592 }
593
594 void paint_set_radial_gradienti(struct vg_paint *paint,
595 const VGint *values)
596 {
597 memcpy(paint->gradient.radial.valsi, values, sizeof(VGint) * 5);
598 }
599
600 void paint_radial_gradienti(struct vg_paint *paint,
601 VGint *coords)
602 {
603 memcpy(coords, paint->gradient.radial.valsi, sizeof(VGint)*5);
604 }
605
606 VGTilingMode paint_pattern_tiling(struct vg_paint *paint)
607 {
608 return paint->pattern.tiling_mode;
609 }
610
611 VGint paint_bind_samplers(struct vg_paint *paint, struct pipe_sampler_state **samplers,
612 struct pipe_texture **textures)
613 {
614 struct vg_context *ctx = vg_current_context();
615
616 switch(paint->type) {
617 case VG_PAINT_TYPE_LINEAR_GRADIENT:
618 case VG_PAINT_TYPE_RADIAL_GRADIENT: {
619 if (paint->gradient.texture) {
620 paint->gradient.sampler.min_img_filter = image_sampler_filter(ctx);
621 paint->gradient.sampler.mag_img_filter = image_sampler_filter(ctx);
622 samplers[0] = &paint->gradient.sampler;
623 textures[0] = paint->gradient.texture;
624 return 1;
625 }
626 }
627 break;
628 case VG_PAINT_TYPE_PATTERN: {
629 memcpy(paint->pattern.sampler.border_color,
630 ctx->state.vg.tile_fill_color,
631 sizeof(VGfloat) * 4);
632 paint->pattern.sampler.min_img_filter = image_sampler_filter(ctx);
633 paint->pattern.sampler.mag_img_filter = image_sampler_filter(ctx);
634 samplers[0] = &paint->pattern.sampler;
635 textures[0] = paint->pattern.texture;
636 return 1;
637 }
638 break;
639 default:
640 samplers[0] = &paint->pattern.sampler; /* dummy */
641 textures[0] = 0;
642 return 0;
643 break;
644 }
645 return 0;
646 }
647
648 void paint_resolve_type(struct vg_paint *paint)
649 {
650 if (paint->type == VG_PAINT_TYPE_PATTERN &&
651 !paint->pattern.texture) {
652 paint->type = VG_PAINT_TYPE_COLOR;
653 }
654 }
655
656 VGint paint_constant_buffer_size(struct vg_paint *paint)
657 {
658 switch(paint->type) {
659 case VG_PAINT_TYPE_COLOR:
660 return 8 * sizeof(VGfloat);/*4 color + 4 constants (0.f,1.f,2.f,4.f)*/
661 break;
662 case VG_PAINT_TYPE_LINEAR_GRADIENT:
663 return 20 * sizeof(VGfloat);
664 break;
665 case VG_PAINT_TYPE_RADIAL_GRADIENT:
666 return 20 * sizeof(VGfloat);
667 break;
668 case VG_PAINT_TYPE_PATTERN:
669 return 20 * sizeof(VGfloat);
670 break;
671 default:
672 debug_printf("Uknown paint type: %d\n", paint->type);
673 }
674
675 return 0;
676 }
677
678 void paint_fill_constant_buffer(struct vg_paint *paint,
679 void *buffer)
680 {
681 switch(paint->type) {
682 case VG_PAINT_TYPE_COLOR:
683 paint_color_buffer(paint, buffer);
684 break;
685 case VG_PAINT_TYPE_LINEAR_GRADIENT:
686 paint_linear_gradient_buffer(paint, buffer);
687 break;
688 case VG_PAINT_TYPE_RADIAL_GRADIENT:
689 paint_radial_gradient_buffer(paint, buffer);
690 break;
691 case VG_PAINT_TYPE_PATTERN:
692 paint_pattern_buffer(paint, buffer);
693 break;
694
695 default:
696 abort();
697 }
698 }