shader.c revision a19eaaa6c1956add5343295af7e9f682efa08d74
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 213 /* 1st stage: fill */ 214 if (!shader->drawing_image || 215 (shader->image_mode == VG_DRAW_IMAGE_MULTIPLY || shader->image_mode == VG_DRAW_IMAGE_STENCIL)) { 216 switch(paint_type(shader->paint)) { 217 case VG_PAINT_TYPE_COLOR: 218 shader_id |= VEGA_SOLID_FILL_SHADER; 219 break; 220 case VG_PAINT_TYPE_LINEAR_GRADIENT: 221 shader_id |= VEGA_LINEAR_GRADIENT_SHADER; 222 break; 223 case VG_PAINT_TYPE_RADIAL_GRADIENT: 224 shader_id |= VEGA_RADIAL_GRADIENT_SHADER; 225 break; 226 case VG_PAINT_TYPE_PATTERN: 227 shader_id |= VEGA_PATTERN_SHADER; 228 break; 229 230 default: 231 abort(); 232 } 233 234 if (paint_is_degenerate(shader->paint)) 235 shader_id = VEGA_PAINT_DEGENERATE_SHADER; 236 } 237 238 /* second stage image */ 239 if (shader->drawing_image) { 240 switch(shader->image_mode) { 241 case VG_DRAW_IMAGE_NORMAL: 242 shader_id |= VEGA_IMAGE_NORMAL_SHADER; 243 break; 244 case VG_DRAW_IMAGE_MULTIPLY: 245 shader_id |= VEGA_IMAGE_MULTIPLY_SHADER; 246 break; 247 case VG_DRAW_IMAGE_STENCIL: 248 shader_id |= VEGA_IMAGE_STENCIL_SHADER; 249 break; 250 default: 251 debug_printf("Unknown image mode!"); 252 } 253 } 254 255 if (shader->color_transform) 256 shader_id |= VEGA_COLOR_TRANSFORM_SHADER; 257 258 switch(blend_mode) { 259 case VG_BLEND_MULTIPLY: 260 shader_id |= VEGA_BLEND_MULTIPLY_SHADER; 261 break; 262 case VG_BLEND_SCREEN: 263 shader_id |= VEGA_BLEND_SCREEN_SHADER; 264 break; 265 case VG_BLEND_DARKEN: 266 shader_id |= VEGA_BLEND_DARKEN_SHADER; 267 break; 268 case VG_BLEND_LIGHTEN: 269 shader_id |= VEGA_BLEND_LIGHTEN_SHADER; 270 break; 271 default: 272 /* handled by pipe_blend_state */ 273 break; 274 } 275 276 if (shader->masking) 277 shader_id |= VEGA_MASK_SHADER; 278 279 if (black_white) 280 shader_id |= VEGA_BW_SHADER; 281 282 shader->fs = shaders_cache_fill(ctx->sc, shader_id); 283} 284 285 286void shader_bind(struct shader *shader) 287{ 288 struct vg_context *ctx = shader->context; 289 struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS]; 290 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS]; 291 VGint num_samplers, param_bytes; 292 293 /* first resolve the real paint type */ 294 paint_resolve_type(shader->paint); 295 296 num_samplers = setup_samplers(shader, samplers, sampler_views); 297 param_bytes = setup_constant_buffer(shader); 298 setup_shader_program(shader); 299 300 renderer_validate_for_shader(ctx->renderer, 301 (const struct pipe_sampler_state **) samplers, 302 sampler_views, num_samplers, 303 &shader->modelview, 304 shader->fs, (const void *) shader->constants, param_bytes); 305} 306 307void shader_set_image_mode(struct shader *shader, VGImageMode image_mode) 308{ 309 shader->image_mode = image_mode; 310} 311 312VGImageMode shader_image_mode(struct shader *shader) 313{ 314 return shader->image_mode; 315} 316 317void shader_set_drawing_image(struct shader *shader, VGboolean drawing_image) 318{ 319 shader->drawing_image = drawing_image; 320} 321 322VGboolean shader_drawing_image(struct shader *shader) 323{ 324 return shader->drawing_image; 325} 326 327void shader_set_image(struct shader *shader, struct vg_image *img) 328{ 329 shader->image = img; 330} 331 332/** 333 * Set the transformation to map a vertex to the surface coordinates. 334 */ 335void shader_set_surface_matrix(struct shader *shader, 336 const struct matrix *mat) 337{ 338 shader->modelview = *mat; 339} 340 341/** 342 * Set the transformation to map a pixel to the paint coordinates. 343 */ 344void shader_set_paint_matrix(struct shader *shader, const struct matrix *mat) 345{ 346 const struct st_framebuffer *stfb = shader->context->draw_buffer; 347 const VGfloat px_center_offset = 0.5f; 348 349 memcpy(&shader->paint_matrix, mat, sizeof(*mat)); 350 351 /* make it window-to-paint for the shaders */ 352 matrix_translate(&shader->paint_matrix, px_center_offset, 353 stfb->height - 1.0f + px_center_offset); 354 matrix_scale(&shader->paint_matrix, 1.0f, -1.0f); 355} 356