summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/vega/shader.c
blob: 6eef94ce7670e5384820cf28a5480d0f7eb7dba3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**************************************************************************
 *
 * Copyright 2009 VMware, Inc.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "shader.h"

#include "vg_context.h"
#include "shaders_cache.h"
#include "paint.h"
#include "mask.h"
#include "image.h"

#include "pipe/p_context.h"
#include "pipe/p_screen.h"
#include "pipe/p_state.h"
#include "util/u_inlines.h"
#include "util/u_memory.h"

#define MAX_CONSTANTS 20

struct shader {
   struct vg_context *context;

   VGboolean masking;
   struct vg_paint *paint;
   struct vg_image *image;

   VGboolean drawing_image;
   VGImageMode image_mode;

   float constants[MAX_CONSTANTS];
   struct pipe_resource *cbuf;
   struct pipe_shader_state fs_state;
   void *fs;
};

struct shader * shader_create(struct vg_context *ctx)
{
   struct shader *shader = 0;

   shader = CALLOC_STRUCT(shader);
   shader->context = ctx;

   return shader;
}

void shader_destroy(struct shader *shader)
{
   free(shader);
}

void shader_set_masking(struct shader *shader, VGboolean set)
{
   shader->masking = set;
}

VGboolean shader_is_masking(struct shader *shader)
{
   return shader->masking;
}

void shader_set_paint(struct shader *shader, struct vg_paint *paint)
{
   shader->paint = paint;
}

struct vg_paint * shader_paint(struct shader *shader)
{
   return shader->paint;
}


static void setup_constant_buffer(struct shader *shader)
{
   struct vg_context *ctx = shader->context;
   struct pipe_context *pipe = shader->context->pipe;
   struct pipe_resource **cbuf = &shader->cbuf;
   VGint param_bytes = paint_constant_buffer_size(shader->paint);
   float temp_buf[MAX_CONSTANTS];

   assert(param_bytes <= sizeof(temp_buf));
   paint_fill_constant_buffer(shader->paint, temp_buf);

   if (*cbuf == NULL ||
       memcmp(temp_buf, shader->constants, param_bytes) != 0)
   {
      pipe_resource_reference(cbuf, NULL);

      memcpy(shader->constants, temp_buf, param_bytes);
      *cbuf = pipe_user_buffer_create(pipe->screen,
                                      &shader->constants,
                                      sizeof(shader->constants),
				      PIPE_BIND_VERTEX_BUFFER);
   }

   ctx->pipe->set_constant_buffer(ctx->pipe, PIPE_SHADER_FRAGMENT, 0, *cbuf);
}

static VGint blend_bind_samplers(struct vg_context *ctx,
                                 struct pipe_sampler_state **samplers,
                                 struct pipe_sampler_view **sampler_views)
{
   VGBlendMode bmode = ctx->state.vg.blend_mode;

   if (bmode == VG_BLEND_MULTIPLY ||
       bmode == VG_BLEND_SCREEN ||
       bmode == VG_BLEND_DARKEN ||
       bmode == VG_BLEND_LIGHTEN) {
      struct st_framebuffer *stfb = ctx->draw_buffer;

      vg_prepare_blend_surface(ctx);

      samplers[2] = &ctx->blend_sampler;
      sampler_views[2] = stfb->blend_texture_view;

      if (!samplers[0] || !sampler_views[0]) {
         samplers[0] = samplers[2];
         sampler_views[0] = sampler_views[2];
      }
      if (!samplers[1] || !sampler_views[1]) {
         samplers[1] = samplers[0];
         sampler_views[1] = sampler_views[0];
      }

      return 1;
   }
   return 0;
}

static void setup_samplers(struct shader *shader)
{
   struct pipe_sampler_state *samplers[PIPE_MAX_SAMPLERS];
   struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
   struct vg_context *ctx = shader->context;
   /* a little wonky: we use the num as a boolean that just says
    * whether any sampler/textures have been set. the actual numbering
    * for samplers is always the same:
    * 0 - paint sampler/texture for gradient/pattern
    * 1 - mask sampler/texture
    * 2 - blend sampler/texture
    * 3 - image sampler/texture
    * */
   VGint num = 0;

   samplers[0] = NULL;
   samplers[1] = NULL;
   samplers[2] = NULL;
   samplers[3] = NULL;
   sampler_views[0] = NULL;
   sampler_views[1] = NULL;
   sampler_views[2] = NULL;
   sampler_views[3] = NULL;

   num += paint_bind_samplers(shader->paint, samplers, sampler_views);
   num += mask_bind_samplers(samplers, sampler_views);
   num += blend_bind_samplers(ctx, samplers, sampler_views);
   if (shader->drawing_image && shader->image)
      num += image_bind_samplers(shader->image, samplers, sampler_views);

   if (num) {
      cso_set_samplers(ctx->cso_context, 4, (const struct pipe_sampler_state **)samplers);
      cso_set_fragment_sampler_views(ctx->cso_context, 4, sampler_views);
   }
}

static INLINE VGboolean is_format_bw(struct shader *shader)
{
#if 0
   struct vg_context *ctx = shader->context;
   struct st_framebuffer *stfb = ctx->draw_buffer;
#endif

   if (shader->drawing_image && shader->image) {
      if (shader->image->format == VG_BW_1)
         return VG_TRUE;
   }

   return VG_FALSE;
}

static void setup_shader_program(struct shader *shader)
{
   struct vg_context *ctx = shader->context;
   VGint shader_id = 0;
   VGBlendMode blend_mode = ctx->state.vg.blend_mode;
   VGboolean black_white = is_format_bw(shader);

   /* 1st stage: fill */
   if (!shader->drawing_image ||
       (shader->image_mode == VG_DRAW_IMAGE_MULTIPLY || shader->image_mode == VG_DRAW_IMAGE_STENCIL)) {
      switch(paint_type(shader->paint)) {
      case VG_PAINT_TYPE_COLOR:
         shader_id |= VEGA_SOLID_FILL_SHADER;
         break;
      case VG_PAINT_TYPE_LINEAR_GRADIENT:
         shader_id |= VEGA_LINEAR_GRADIENT_SHADER;
         break;
      case VG_PAINT_TYPE_RADIAL_GRADIENT:
         shader_id |= VEGA_RADIAL_GRADIENT_SHADER;
         break;
      case VG_PAINT_TYPE_PATTERN:
         shader_id |= VEGA_PATTERN_SHADER;
         break;

      default:
         abort();
      }
   }

   /* second stage image */
   if (shader->drawing_image) {
      switch(shader->image_mode) {
      case VG_DRAW_IMAGE_NORMAL:
         shader_id |= VEGA_IMAGE_NORMAL_SHADER;
         break;
      case VG_DRAW_IMAGE_MULTIPLY:
         shader_id |= VEGA_IMAGE_MULTIPLY_SHADER;
         break;
      case VG_DRAW_IMAGE_STENCIL:
         shader_id |= VEGA_IMAGE_STENCIL_SHADER;
         break;
      default:
         debug_printf("Unknown image mode!");
      }
   }

   if (shader->masking)
      shader_id |= VEGA_MASK_SHADER;

   switch(blend_mode) {
   case VG_BLEND_MULTIPLY:
      shader_id |= VEGA_BLEND_MULTIPLY_SHADER;
      break;
   case VG_BLEND_SCREEN:
      shader_id |= VEGA_BLEND_SCREEN_SHADER;
      break;
   case VG_BLEND_DARKEN:
      shader_id |= VEGA_BLEND_DARKEN_SHADER;
      break;
   case VG_BLEND_LIGHTEN:
      shader_id |= VEGA_BLEND_LIGHTEN_SHADER;
      break;
   default:
      /* handled by pipe_blend_state */
      break;
   }

   if (black_white)
      shader_id |= VEGA_BW_SHADER;

   shader->fs = shaders_cache_fill(ctx->sc, shader_id);
   cso_set_fragment_shader_handle(ctx->cso_context, shader->fs);
}


void shader_bind(struct shader *shader)
{
   /* first resolve the real paint type */
   paint_resolve_type(shader->paint);

   setup_constant_buffer(shader);
   setup_samplers(shader);
   setup_shader_program(shader);
}

void shader_set_image_mode(struct shader *shader, VGImageMode image_mode)
{
   shader->image_mode = image_mode;
}

VGImageMode shader_image_mode(struct shader *shader)
{
   return shader->image_mode;
}

void shader_set_drawing_image(struct shader *shader, VGboolean drawing_image)
{
   shader->drawing_image = drawing_image;
}

VGboolean shader_drawing_image(struct shader *shader)
{
   return shader->drawing_image;
}

void shader_set_image(struct shader *shader, struct vg_image *img)
{
   shader->image = img;
}