shader.c revision b06de80843e7d096bed4ae03ddc5e2842f1876af
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 paint_matrix; 54 55 VGboolean drawing_image; 56 VGImageMode image_mode; 57 58 float constants[MAX_CONSTANTS]; 59 struct pipe_resource *cbuf; 60 struct pipe_shader_state fs_state; 61 void *fs; 62}; 63 64struct shader * shader_create(struct vg_context *ctx) 65{ 66 struct shader *shader = 0; 67 68 shader = CALLOC_STRUCT(shader); 69 shader->context = ctx; 70 71 return shader; 72} 73 74void shader_destroy(struct shader *shader) 75{ 76 FREE(shader); 77} 78 79void shader_set_color_transform(struct shader *shader, VGboolean set) 80{ 81 shader->color_transform = set; 82} 83 84void shader_set_masking(struct shader *shader, VGboolean set) 85{ 86 shader->masking = set; 87} 88 89VGboolean shader_is_masking(struct shader *shader) 90{ 91 return shader->masking; 92} 93 94void shader_set_paint(struct shader *shader, struct vg_paint *paint) 95{ 96 shader->paint = paint; 97} 98 99struct vg_paint * shader_paint(struct shader *shader) 100{ 101 return shader->paint; 102} 103 104static VGint setup_constant_buffer(struct shader *shader) 105{ 106 const struct vg_state *state = &shader->context->state.vg; 107 VGint param_bytes = paint_constant_buffer_size(shader->paint); 108 VGint i; 109 110 param_bytes += sizeof(VGfloat) * 8; 111 assert(param_bytes <= sizeof(shader->constants)); 112 113 if (state->color_transform) { 114 for (i = 0; i < 8; i++) { 115 VGfloat val = (i < 4) ? 127.0f : 1.0f; 116 shader->constants[i] = 117 CLAMP(state->color_transform_values[i], -val, val); 118 } 119 } 120 else { 121 memset(shader->constants, 0, sizeof(VGfloat) * 8); 122 } 123 124 paint_fill_constant_buffer(shader->paint, 125 &shader->paint_matrix, shader->constants + 8); 126 127 return param_bytes; 128} 129 130static VGint blend_bind_samplers(struct vg_context *ctx, 131 struct pipe_sampler_state **samplers, 132 struct pipe_sampler_view **sampler_views) 133{ 134 VGBlendMode bmode = ctx->state.vg.blend_mode; 135 136 if (bmode == VG_BLEND_MULTIPLY || 137 bmode == VG_BLEND_SCREEN || 138 bmode == VG_BLEND_DARKEN || 139 bmode == VG_BLEND_LIGHTEN) { 140 struct st_framebuffer *stfb = ctx->draw_buffer; 141 142 vg_prepare_blend_surface(ctx); 143 144 samplers[2] = &ctx->blend_sampler; 145 sampler_views[2] = stfb->blend_texture_view; 146 147 if (!samplers[0] || !sampler_views[0]) { 148 samplers[0] = samplers[2]; 149 sampler_views[0] = sampler_views[2]; 150 } 151 if (!samplers[1] || !sampler_views[1]) { 152 samplers[1] = samplers[0]; 153 sampler_views[1] = sampler_views[0]; 154 } 155 156 return 1; 157 } 158 return 0; 159} 160 161static VGint setup_samplers(struct shader *shader, 162 struct pipe_sampler_state **samplers, 163 struct pipe_sampler_view **sampler_views) 164{ 165 struct vg_context *ctx = shader->context; 166 /* a little wonky: we use the num as a boolean that just says 167 * whether any sampler/textures have been set. the actual numbering 168 * for samplers is always the same: 169 * 0 - paint sampler/texture for gradient/pattern 170 * 1 - mask sampler/texture 171 * 2 - blend sampler/texture 172 * 3 - image sampler/texture 173 * */ 174 VGint num = 0; 175 176 samplers[0] = NULL; 177 samplers[1] = NULL; 178 samplers[2] = NULL; 179 samplers[3] = NULL; 180 sampler_views[0] = NULL; 181 sampler_views[1] = NULL; 182 sampler_views[2] = NULL; 183 sampler_views[3] = NULL; 184 185 num += paint_bind_samplers(shader->paint, samplers, sampler_views); 186 num += mask_bind_samplers(samplers, sampler_views); 187 num += blend_bind_samplers(ctx, samplers, sampler_views); 188 if (shader->drawing_image && shader->image) 189 num += image_bind_samplers(shader->image, samplers, sampler_views); 190 191 return (num) ? 4 : 0; 192} 193 194static INLINE VGboolean is_format_bw(struct shader *shader) 195{ 196#if 0 197 struct vg_context *ctx = shader->context; 198 struct st_framebuffer *stfb = ctx->draw_buffer; 199#endif 200 201 if (shader->drawing_image && shader->image) { 202 if (shader->image->format == VG_BW_1) 203 return VG_TRUE; 204 } 205 206 return VG_FALSE; 207} 208 209static void setup_shader_program(struct shader *shader) 210{ 211 struct vg_context *ctx = shader->context; 212 VGint shader_id = 0; 213 VGBlendMode blend_mode = ctx->state.vg.blend_mode; 214 VGboolean black_white = is_format_bw(shader); 215 216 /* 1st stage: fill */ 217 if (!shader->drawing_image || 218 (shader->image_mode == VG_DRAW_IMAGE_MULTIPLY || shader->image_mode == VG_DRAW_IMAGE_STENCIL)) { 219 switch(paint_type(shader->paint)) { 220 case VG_PAINT_TYPE_COLOR: 221 shader_id |= VEGA_SOLID_FILL_SHADER; 222 break; 223 case VG_PAINT_TYPE_LINEAR_GRADIENT: 224 shader_id |= VEGA_LINEAR_GRADIENT_SHADER; 225 break; 226 case VG_PAINT_TYPE_RADIAL_GRADIENT: 227 shader_id |= VEGA_RADIAL_GRADIENT_SHADER; 228 break; 229 case VG_PAINT_TYPE_PATTERN: 230 shader_id |= VEGA_PATTERN_SHADER; 231 break; 232 233 default: 234 abort(); 235 } 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 if (shader->masking) 259 shader_id |= VEGA_MASK_SHADER; 260 261 switch(blend_mode) { 262 case VG_BLEND_MULTIPLY: 263 shader_id |= VEGA_BLEND_MULTIPLY_SHADER; 264 break; 265 case VG_BLEND_SCREEN: 266 shader_id |= VEGA_BLEND_SCREEN_SHADER; 267 break; 268 case VG_BLEND_DARKEN: 269 shader_id |= VEGA_BLEND_DARKEN_SHADER; 270 break; 271 case VG_BLEND_LIGHTEN: 272 shader_id |= VEGA_BLEND_LIGHTEN_SHADER; 273 break; 274 default: 275 /* handled by pipe_blend_state */ 276 break; 277 } 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->fs, (const void *) shader->constants, param_bytes); 304} 305 306void shader_set_image_mode(struct shader *shader, VGImageMode image_mode) 307{ 308 shader->image_mode = image_mode; 309} 310 311VGImageMode shader_image_mode(struct shader *shader) 312{ 313 return shader->image_mode; 314} 315 316void shader_set_drawing_image(struct shader *shader, VGboolean drawing_image) 317{ 318 shader->drawing_image = drawing_image; 319} 320 321VGboolean shader_drawing_image(struct shader *shader) 322{ 323 return shader->drawing_image; 324} 325 326void shader_set_image(struct shader *shader, struct vg_image *img) 327{ 328 shader->image = img; 329} 330 331/** 332 * Set the transformation to map a pixel to the paint coordinates. 333 */ 334void shader_set_paint_matrix(struct shader *shader, const struct matrix *mat) 335{ 336 const struct st_framebuffer *stfb = shader->context->draw_buffer; 337 const VGfloat px_center_offset = 0.5f; 338 339 memcpy(&shader->paint_matrix, mat, sizeof(*mat)); 340 341 /* make it window-to-paint for the shaders */ 342 matrix_translate(&shader->paint_matrix, px_center_offset, 343 stfb->height - 1.0f + px_center_offset); 344 matrix_scale(&shader->paint_matrix, 1.0f, -1.0f); 345} 346