shader.c revision e8ff3931f801dffdfd54832c298351e933688235
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 "shader.h" 28 29#include "vg_context.h" 30#include "shaders_cache.h" 31#include "paint.h" 32#include "mask.h" 33#include "image.h" 34#include "renderer.h" 35 36#include "pipe/p_context.h" 37#include "pipe/p_screen.h" 38#include "pipe/p_state.h" 39#include "util/u_inlines.h" 40#include "util/u_memory.h" 41#include "util/u_math.h" 42 43#define MAX_CONSTANTS 28 44 45struct shader { 46 struct vg_context *context; 47 48 VGboolean color_transform; 49 VGboolean masking; 50 struct vg_paint *paint; 51 struct vg_image *image; 52 53 struct matrix modelview; 54 struct matrix paint_matrix; 55 56 VGboolean drawing_image; 57 VGImageMode image_mode; 58 59 float constants[MAX_CONSTANTS]; 60 struct pipe_resource *cbuf; 61 struct pipe_shader_state fs_state; 62 void *fs; 63}; 64 65struct shader * shader_create(struct vg_context *ctx) 66{ 67 struct shader *shader = 0; 68 69 shader = CALLOC_STRUCT(shader); 70 shader->context = ctx; 71 72 return shader; 73} 74 75void shader_destroy(struct shader *shader) 76{ 77 FREE(shader); 78} 79 80void shader_set_color_transform(struct shader *shader, VGboolean set) 81{ 82 shader->color_transform = set; 83} 84 85void shader_set_masking(struct shader *shader, VGboolean set) 86{ 87 shader->masking = set; 88} 89 90VGboolean shader_is_masking(struct shader *shader) 91{ 92 return shader->masking; 93} 94 95void shader_set_paint(struct shader *shader, struct vg_paint *paint) 96{ 97 shader->paint = paint; 98} 99 100struct vg_paint * shader_paint(struct shader *shader) 101{ 102 return shader->paint; 103} 104 105static VGint setup_constant_buffer(struct shader *shader) 106{ 107 const struct vg_state *state = &shader->context->state.vg; 108 VGint param_bytes = paint_constant_buffer_size(shader->paint); 109 VGint i; 110 111 param_bytes += sizeof(VGfloat) * 8; 112 assert(param_bytes <= sizeof(shader->constants)); 113 114 if (state->color_transform) { 115 for (i = 0; i < 8; i++) { 116 VGfloat val = (i < 4) ? 127.0f : 1.0f; 117 shader->constants[i] = 118 CLAMP(state->color_transform_values[i], -val, val); 119 } 120 } 121 else { 122 memset(shader->constants, 0, sizeof(VGfloat) * 8); 123 } 124 125 paint_fill_constant_buffer(shader->paint, 126 &shader->paint_matrix, shader->constants + 8); 127 128 return param_bytes; 129} 130 131static VGint blend_bind_samplers(struct vg_context *ctx, 132 struct pipe_sampler_state **samplers, 133 struct pipe_sampler_view **sampler_views) 134{ 135 VGBlendMode bmode = ctx->state.vg.blend_mode; 136 137 if (bmode == VG_BLEND_MULTIPLY || 138 bmode == VG_BLEND_SCREEN || 139 bmode == VG_BLEND_DARKEN || 140 bmode == VG_BLEND_LIGHTEN) { 141 samplers[2] = &ctx->blend_sampler; 142 sampler_views[2] = vg_prepare_blend_surface(ctx); 143 144 if (!samplers[0] || !sampler_views[0]) { 145 samplers[0] = samplers[2]; 146 sampler_views[0] = sampler_views[2]; 147 } 148 if (!samplers[1] || !sampler_views[1]) { 149 samplers[1] = samplers[0]; 150 sampler_views[1] = sampler_views[0]; 151 } 152 153 return 1; 154 } 155 return 0; 156} 157 158static VGint setup_samplers(struct shader *shader, 159 struct pipe_sampler_state **samplers, 160 struct pipe_sampler_view **sampler_views) 161{ 162 struct vg_context *ctx = shader->context; 163 /* a little wonky: we use the num as a boolean that just says 164 * whether any sampler/textures have been set. the actual numbering 165 * for samplers is always the same: 166 * 0 - paint sampler/texture for gradient/pattern 167 * 1 - mask sampler/texture 168 * 2 - blend sampler/texture 169 * 3 - image sampler/texture 170 * */ 171 VGint num = 0; 172 173 samplers[0] = NULL; 174 samplers[1] = NULL; 175 samplers[2] = NULL; 176 samplers[3] = NULL; 177 sampler_views[0] = NULL; 178 sampler_views[1] = NULL; 179 sampler_views[2] = NULL; 180 sampler_views[3] = NULL; 181 182 num += paint_bind_samplers(shader->paint, samplers, sampler_views); 183 num += mask_bind_samplers(samplers, sampler_views); 184 num += blend_bind_samplers(ctx, samplers, sampler_views); 185 if (shader->drawing_image && shader->image) 186 num += image_bind_samplers(shader->image, samplers, sampler_views); 187 188 return (num) ? 4 : 0; 189} 190 191static INLINE VGboolean is_format_bw(struct shader *shader) 192{ 193#if 0 194 struct vg_context *ctx = shader->context; 195 struct st_framebuffer *stfb = ctx->draw_buffer; 196#endif 197 198 if (shader->drawing_image && shader->image) { 199 if (shader->image->format == VG_BW_1) 200 return VG_TRUE; 201 } 202 203 return VG_FALSE; 204} 205 206static void setup_shader_program(struct shader *shader) 207{ 208 struct vg_context *ctx = shader->context; 209 VGint shader_id = 0; 210 VGBlendMode blend_mode = ctx->state.vg.blend_mode; 211 VGboolean black_white = is_format_bw(shader); 212 VGboolean advanced_blend; 213 214 /* 1st stage: fill */ 215 if (!shader->drawing_image || 216 (shader->image_mode == VG_DRAW_IMAGE_MULTIPLY || shader->image_mode == VG_DRAW_IMAGE_STENCIL)) { 217 switch(paint_type(shader->paint)) { 218 case VG_PAINT_TYPE_COLOR: 219 shader_id |= VEGA_SOLID_FILL_SHADER; 220 break; 221 case VG_PAINT_TYPE_LINEAR_GRADIENT: 222 shader_id |= VEGA_LINEAR_GRADIENT_SHADER; 223 break; 224 case VG_PAINT_TYPE_RADIAL_GRADIENT: 225 shader_id |= VEGA_RADIAL_GRADIENT_SHADER; 226 break; 227 case VG_PAINT_TYPE_PATTERN: 228 shader_id |= VEGA_PATTERN_SHADER; 229 break; 230 231 default: 232 abort(); 233 } 234 235 if (paint_is_degenerate(shader->paint)) 236 shader_id = VEGA_PAINT_DEGENERATE_SHADER; 237 } 238 239 /* second stage image */ 240 if (shader->drawing_image) { 241 switch(shader->image_mode) { 242 case VG_DRAW_IMAGE_NORMAL: 243 shader_id |= VEGA_IMAGE_NORMAL_SHADER; 244 break; 245 case VG_DRAW_IMAGE_MULTIPLY: 246 shader_id |= VEGA_IMAGE_MULTIPLY_SHADER; 247 break; 248 case VG_DRAW_IMAGE_STENCIL: 249 shader_id |= VEGA_IMAGE_STENCIL_SHADER; 250 break; 251 default: 252 debug_printf("Unknown image mode!"); 253 } 254 } 255 256 if (shader->color_transform) 257 shader_id |= VEGA_COLOR_TRANSFORM_SHADER; 258 259 switch(blend_mode) { 260 case VG_BLEND_MULTIPLY: 261 case VG_BLEND_SCREEN: 262 case VG_BLEND_DARKEN: 263 case VG_BLEND_LIGHTEN: 264 advanced_blend = VG_TRUE; 265 break; 266 default: 267 /* handled by pipe_blend_state */ 268 advanced_blend = VG_FALSE; 269 break; 270 } 271 272 if (advanced_blend) { 273 if (shader->drawing_image && shader->image_mode == VG_DRAW_IMAGE_STENCIL) 274 shader_id |= VEGA_ALPHA_PER_CHANNEL_SHADER; 275 else 276 shader_id |= VEGA_ALPHA_NORMAL_SHADER; 277 278 switch(blend_mode) { 279 case VG_BLEND_MULTIPLY: 280 shader_id |= VEGA_BLEND_MULTIPLY_SHADER; 281 break; 282 case VG_BLEND_SCREEN: 283 shader_id |= VEGA_BLEND_SCREEN_SHADER; 284 break; 285 case VG_BLEND_DARKEN: 286 shader_id |= VEGA_BLEND_DARKEN_SHADER; 287 break; 288 case VG_BLEND_LIGHTEN: 289 shader_id |= VEGA_BLEND_LIGHTEN_SHADER; 290 break; 291 default: 292 assert(0); 293 break; 294 } 295 } 296 else { 297 /* update alpha of the source */ 298 if (shader->drawing_image && shader->image_mode == VG_DRAW_IMAGE_STENCIL) 299 shader_id |= VEGA_ALPHA_PER_CHANNEL_SHADER; 300 } 301 302 if (shader->masking) 303 shader_id |= VEGA_MASK_SHADER; 304 305 if (black_white) 306 shader_id |= VEGA_BW_SHADER; 307 308 shader->fs = shaders_cache_fill(ctx->sc, shader_id); 309} 310 311 312void shader_bind(struct shader *shader) 313{ 314 struct vg_context *ctx = shader->context; 315 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS]; 316 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS]; 317 VGint num_samplers, param_bytes; 318 319 /* first resolve the real paint type */ 320 paint_resolve_type(shader->paint); 321 322 num_samplers = setup_samplers(shader, samplers, sampler_views); 323 param_bytes = setup_constant_buffer(shader); 324 setup_shader_program(shader); 325 326 renderer_validate_for_shader(ctx->renderer, 327 (const struct pipe_sampler_state **) samplers, 328 sampler_views, num_samplers, 329 &shader->modelview, 330 shader->fs, (const void *) shader->constants, param_bytes); 331} 332 333void shader_set_image_mode(struct shader *shader, VGImageMode image_mode) 334{ 335 shader->image_mode = image_mode; 336} 337 338VGImageMode shader_image_mode(struct shader *shader) 339{ 340 return shader->image_mode; 341} 342 343void shader_set_drawing_image(struct shader *shader, VGboolean drawing_image) 344{ 345 shader->drawing_image = drawing_image; 346} 347 348VGboolean shader_drawing_image(struct shader *shader) 349{ 350 return shader->drawing_image; 351} 352 353void shader_set_image(struct shader *shader, struct vg_image *img) 354{ 355 shader->image = img; 356} 357 358/** 359 * Set the transformation to map a vertex to the surface coordinates. 360 */ 361void shader_set_surface_matrix(struct shader *shader, 362 const struct matrix *mat) 363{ 364 shader->modelview = *mat; 365} 366 367/** 368 * Set the transformation to map a pixel to the paint coordinates. 369 */ 370void shader_set_paint_matrix(struct shader *shader, const struct matrix *mat) 371{ 372 const struct st_framebuffer *stfb = shader->context->draw_buffer; 373 const VGfloat px_center_offset = 0.5f; 374 375 memcpy(&shader->paint_matrix, mat, sizeof(*mat)); 376 377 /* make it window-to-paint for the shaders */ 378 matrix_translate(&shader->paint_matrix, px_center_offset, 379 stfb->height - 1.0f + px_center_offset); 380 matrix_scale(&shader->paint_matrix, 1.0f, -1.0f); 381} 382