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 "shaders_cache.h"
32 #include "st_inlines.h"
34 #include "pipe/p_compiler.h"
35 #include "pipe/p_inlines.h"
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
40 #include "cso_cache/cso_context.h"
43 struct vg_object base
;
53 VGColorRampSpreadMode spread
;
54 VGuint color_data
[1024];
63 struct pipe_texture
*texture
;
64 struct pipe_sampler_state sampler
;
70 VGboolean color_ramps_premultiplied
;
74 struct pipe_texture
*texture
;
75 VGTilingMode tiling_mode
;
76 struct pipe_sampler_state sampler
;
79 struct pipe_constant_buffer cbuf
;
80 struct pipe_shader_state fs_state
;
84 static INLINE VGuint
mix_pixels(VGuint p1
, VGuint a
, VGuint p2
, VGuint b
)
86 VGuint t
= (p1
& 0xff00ff) * a
+ (p2
& 0xff00ff) * b
;
87 t
>>= 8; t
&= 0xff00ff;
89 p1
= ((p1
>> 8) & 0xff00ff) * a
+ ((p2
>> 8) & 0xff00ff) * b
;
90 p1
&= 0xff00ff00; p1
|= t
;
95 static INLINE VGuint
float4_to_argb(const VGfloat
*clr
)
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;
103 static INLINE
void create_gradient_data(const VGfloat
*ramp_stops
,
110 VGfloat fpos
= 0, incr
= 1.f
/ size
;
113 while (fpos
< ramp_stops
[0]) {
114 data
[pos
] = float4_to_argb(ramp_stops
+ 1);
119 for (i
= 0; i
< num
- 1; ++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
,
135 last_color
= float4_to_argb(ramp_stops
+ ((num
- 1) * 5 + 1));
137 data
[pos
] = last_color
;
140 data
[size
-1] = last_color
;
143 static INLINE
struct pipe_texture
*create_gradient_texture(struct vg_paint
*p
)
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
;
150 memset(&templ
, 0, sizeof(templ
));
151 templ
.target
= PIPE_TEXTURE_1D
;
152 templ
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
153 templ
.last_level
= 0;
157 templ
.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
;
159 tex
= screen
->texture_create(screen
, &templ
);
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
);
174 struct vg_paint
* paint_create(struct vg_context
*ctx
)
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
);
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
));
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;
198 memcpy(&paint
->pattern
.sampler
,
199 &paint
->gradient
.sampler
,
200 sizeof(struct pipe_sampler_state
));
205 void paint_destroy(struct vg_paint
*paint
)
207 struct vg_context
*ctx
= paint
->base
.ctx
;
208 if (paint
->pattern
.texture
)
209 pipe_texture_reference(&paint
->pattern
.texture
, NULL
);
211 vg_context_remove_object(ctx
, VG_OBJECT_PAINT
, paint
);
213 free(paint
->gradient
.ramp_stopsi
);
214 free(paint
->gradient
.ramp_stops
);
218 void paint_set_color(struct vg_paint
*paint
,
219 const VGfloat
*color
)
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];
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]);
232 static INLINE
void paint_color_buffer(struct vg_paint
*paint
, void *buffer
)
234 VGfloat
*map
= (VGfloat
*)buffer
;
235 memcpy(buffer
, paint
->solid
.color
, 4 * sizeof(VGfloat
));
242 static INLINE
void paint_linear_gradient_buffer(struct vg_paint
*paint
, void *buffer
)
244 struct vg_context
*ctx
= paint
->base
.ctx
;
245 VGfloat
*map
= (VGfloat
*)buffer
;
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]);
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
));
264 matrix_mult(&inv
, &mat
);
266 sizeof(struct matrix
));
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
;
273 debug_printf("Coords (%f, %f, %f, %f)\n",
274 map
[0], map
[1], map
[2], map
[3]);
279 static INLINE
void paint_radial_gradient_buffer(struct vg_paint
*paint
, void *buffer
)
281 VGfloat
*radialCoords
= paint
->gradient
.radial
.vals
;
282 struct vg_context
*ctx
= paint
->base
.ctx
;
284 VGfloat
*map
= (VGfloat
*)buffer
;
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];
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
));
305 matrix_mult(&inv
, &mat
);
307 sizeof(struct matrix
));
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
;
315 debug_printf("Coords (%f, %f, %f, %f)\n",
316 map
[0], map
[1], map
[2], map
[3]);
321 static INLINE
void paint_pattern_buffer(struct vg_paint
*paint
, void *buffer
)
323 struct vg_context
*ctx
= paint
->base
.ctx
;
325 VGfloat
*map
= (VGfloat
*)buffer
;
326 memcpy(map
, paint
->solid
.color
, 4 * sizeof(VGfloat
));
330 map
[6] = paint
->pattern
.texture
->width0
;
331 map
[7] = paint
->pattern
.texture
->height0
;
334 memcpy(&mat
, &ctx
->state
.vg
.fill_paint_to_user_matrix
,
335 sizeof(struct matrix
));
339 memcpy(&pm
, &ctx
->state
.vg
.path_user_to_surface_matrix
,
340 sizeof(struct matrix
));
342 matrix_mult(&pm
, &mat
);
343 memcpy(&mat
, &pm
, sizeof(struct matrix
));
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
;
352 void paint_set_type(struct vg_paint
*paint
, VGPaintType type
)
357 void paint_set_ramp_stops(struct vg_paint
*paint
, const VGfloat
*stops
,
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
};
363 const VGint num_stops
= num
/ 5;
366 paint
->gradient
.num_stops
= 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
);
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*/
377 stops
= default_stops
;
380 last_coord
= stops
[0];
381 for (i
= 1; i
< num_stops
; ++i
) {
383 VGfloat coord
= stops
[idx
];
384 if (!floatsEqual(last_coord
, coord
) && coord
< last_coord
) {
385 stops
= default_stops
;
392 create_gradient_data(stops
, num
/ 5, paint
->gradient
.color_data
,
395 if (paint
->gradient
.texture
) {
396 pipe_texture_reference(&paint
->gradient
.texture
, NULL
);
397 paint
->gradient
.texture
= 0;
400 paint
->gradient
.texture
= create_gradient_texture(paint
);
403 void paint_set_colori(struct vg_paint
*p
,
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
;
412 VGuint
paint_colori(struct vg_paint
*p
)
414 #define F2B(f) (float_to_ubyte(f))
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));
423 void paint_set_linear_gradient(struct vg_paint
*paint
,
424 const VGfloat
*coords
)
426 memcpy(paint
->gradient
.linear
.coords
, coords
, sizeof(VGfloat
) * 4);
429 void paint_set_spread_mode(struct vg_paint
*paint
,
432 paint
->gradient
.spread
= mode
;
434 case VG_COLOR_RAMP_SPREAD_PAD
:
435 paint
->gradient
.sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
437 case VG_COLOR_RAMP_SPREAD_REPEAT
:
438 paint
->gradient
.sampler
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
440 case VG_COLOR_RAMP_SPREAD_REFLECT
:
441 paint
->gradient
.sampler
.wrap_s
= PIPE_TEX_WRAP_MIRROR_REPEAT
;
446 VGColorRampSpreadMode
paint_spread_mode(struct vg_paint
*paint
)
448 return paint
->gradient
.spread
;
451 void paint_set_radial_gradient(struct vg_paint
*paint
,
452 const VGfloat
*values
)
454 memcpy(paint
->gradient
.radial
.vals
, values
, sizeof(VGfloat
) * 5);
457 void paint_set_pattern(struct vg_paint
*paint
,
458 struct vg_image
*img
)
460 if (paint
->pattern
.texture
)
461 pipe_texture_reference(&paint
->pattern
.texture
, NULL
);
463 paint
->pattern
.texture
= 0;
464 pipe_texture_reference(&paint
->pattern
.texture
,
468 void paint_set_pattern_tiling(struct vg_paint
*paint
,
471 paint
->pattern
.tiling_mode
= mode
;
475 paint
->pattern
.sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
476 paint
->pattern
.sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_BORDER
;
479 paint
->pattern
.sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
480 paint
->pattern
.sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
483 paint
->pattern
.sampler
.wrap_s
= PIPE_TEX_WRAP_REPEAT
;
484 paint
->pattern
.sampler
.wrap_t
= PIPE_TEX_WRAP_REPEAT
;
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
;
491 debug_assert("!Unknown tiling mode");
495 void paint_get_color(struct vg_paint
*paint
,
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];
504 void paint_ramp_stops(struct vg_paint
*paint
, VGfloat
*stops
,
507 memcpy(stops
, paint
->gradient
.ramp_stops
, sizeof(VGfloat
)*num
);
510 void paint_linear_gradient(struct vg_paint
*paint
,
513 memcpy(coords
, paint
->gradient
.linear
.coords
, sizeof(VGfloat
)*4);
516 void paint_radial_gradient(struct vg_paint
*paint
,
519 memcpy(coords
, paint
->gradient
.radial
.vals
, sizeof(VGfloat
)*5);
522 int paint_num_ramp_stops(struct vg_paint
*paint
)
524 return paint
->gradient
.num_stops
;
527 VGPaintType
paint_type(struct vg_paint
*paint
)
532 void paint_set_coloriv(struct vg_paint
*paint
,
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];
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];
546 void paint_get_coloriv(struct vg_paint
*paint
,
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];
555 void paint_set_color_ramp_premultiplied(struct vg_paint
*paint
,
558 paint
->gradient
.color_ramps_premultiplied
= set
;
561 VGboolean
paint_color_ramp_premultiplied(struct vg_paint
*paint
)
563 return paint
->gradient
.color_ramps_premultiplied
;
566 void paint_set_ramp_stopsi(struct vg_paint
*paint
, const VGint
*stops
,
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
);
576 void paint_ramp_stopsi(struct vg_paint
*paint
, VGint
*stops
,
579 memcpy(stops
, paint
->gradient
.ramp_stopsi
, sizeof(VGint
)*num
);
582 void paint_set_linear_gradienti(struct vg_paint
*paint
,
585 memcpy(paint
->gradient
.linear
.coordsi
, coords
, sizeof(VGint
) * 4);
588 void paint_linear_gradienti(struct vg_paint
*paint
,
591 memcpy(coords
, paint
->gradient
.linear
.coordsi
, sizeof(VGint
)*4);
594 void paint_set_radial_gradienti(struct vg_paint
*paint
,
597 memcpy(paint
->gradient
.radial
.valsi
, values
, sizeof(VGint
) * 5);
600 void paint_radial_gradienti(struct vg_paint
*paint
,
603 memcpy(coords
, paint
->gradient
.radial
.valsi
, sizeof(VGint
)*5);
606 VGTilingMode
paint_pattern_tiling(struct vg_paint
*paint
)
608 return paint
->pattern
.tiling_mode
;
611 VGint
paint_bind_samplers(struct vg_paint
*paint
, struct pipe_sampler_state
**samplers
,
612 struct pipe_texture
**textures
)
614 struct vg_context
*ctx
= vg_current_context();
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
;
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
;
640 samplers
[0] = &paint
->pattern
.sampler
; /* dummy */
648 void paint_resolve_type(struct vg_paint
*paint
)
650 if (paint
->type
== VG_PAINT_TYPE_PATTERN
&&
651 !paint
->pattern
.texture
) {
652 paint
->type
= VG_PAINT_TYPE_COLOR
;
656 VGint
paint_constant_buffer_size(struct vg_paint
*paint
)
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)*/
662 case VG_PAINT_TYPE_LINEAR_GRADIENT
:
663 return 20 * sizeof(VGfloat
);
665 case VG_PAINT_TYPE_RADIAL_GRADIENT
:
666 return 20 * sizeof(VGfloat
);
668 case VG_PAINT_TYPE_PATTERN
:
669 return 20 * sizeof(VGfloat
);
672 debug_printf("Uknown paint type: %d\n", paint
->type
);
678 void paint_fill_constant_buffer(struct vg_paint
*paint
,
681 switch(paint
->type
) {
682 case VG_PAINT_TYPE_COLOR
:
683 paint_color_buffer(paint
, buffer
);
685 case VG_PAINT_TYPE_LINEAR_GRADIENT
:
686 paint_linear_gradient_buffer(paint
, buffer
);
688 case VG_PAINT_TYPE_RADIAL_GRADIENT
:
689 paint_radial_gradient_buffer(paint
, buffer
);
691 case VG_PAINT_TYPE_PATTERN
:
692 paint_pattern_buffer(paint
, buffer
);