summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/svga/svga_pipe_blend.c
blob: b5557d31f4401b815be1651d4193cd6292503bfe (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**********************************************************
 * Copyright 2008-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, sublicense, 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 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
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * 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 "util/u_inlines.h"
#include "pipe/p_defines.h"
#include "util/u_math.h"
#include "util/u_memory.h"
#include "util/u_bitmask.h"

#include "svga_context.h"
#include "svga_hw_reg.h"
#include "svga_cmd.h"


static inline unsigned
svga_translate_blend_factor(const struct svga_context *svga, unsigned factor)
{
   /* Note: there is no SVGA3D_BLENDOP_[INV]BLENDFACTORALPHA so
    * we can't translate PIPE_BLENDFACTOR_[INV_]CONST_ALPHA properly.
    */
   switch (factor) {
   case PIPE_BLENDFACTOR_ZERO:            return SVGA3D_BLENDOP_ZERO;
   case PIPE_BLENDFACTOR_SRC_ALPHA:       return SVGA3D_BLENDOP_SRCALPHA;
   case PIPE_BLENDFACTOR_ONE:             return SVGA3D_BLENDOP_ONE;
   case PIPE_BLENDFACTOR_SRC_COLOR:       return SVGA3D_BLENDOP_SRCCOLOR;
   case PIPE_BLENDFACTOR_INV_SRC_COLOR:   return SVGA3D_BLENDOP_INVSRCCOLOR;
   case PIPE_BLENDFACTOR_DST_COLOR:       return SVGA3D_BLENDOP_DESTCOLOR;
   case PIPE_BLENDFACTOR_INV_DST_COLOR:   return SVGA3D_BLENDOP_INVDESTCOLOR;
   case PIPE_BLENDFACTOR_INV_SRC_ALPHA:   return SVGA3D_BLENDOP_INVSRCALPHA;
   case PIPE_BLENDFACTOR_DST_ALPHA:       return SVGA3D_BLENDOP_DESTALPHA;
   case PIPE_BLENDFACTOR_INV_DST_ALPHA:   return SVGA3D_BLENDOP_INVDESTALPHA;
   case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE: return SVGA3D_BLENDOP_SRCALPHASAT;
   case PIPE_BLENDFACTOR_CONST_COLOR:     return SVGA3D_BLENDOP_BLENDFACTOR;
   case PIPE_BLENDFACTOR_INV_CONST_COLOR: return SVGA3D_BLENDOP_INVBLENDFACTOR;
   case PIPE_BLENDFACTOR_CONST_ALPHA:
      if (svga_have_vgpu10(svga))
         return SVGA3D_BLENDOP_BLENDFACTORALPHA;
      else
         return SVGA3D_BLENDOP_BLENDFACTOR; /* as close as we can get */
   case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
      if (svga_have_vgpu10(svga))
         return SVGA3D_BLENDOP_INVBLENDFACTORALPHA;
      else
         return SVGA3D_BLENDOP_INVBLENDFACTOR; /* as close as we can get */
   case PIPE_BLENDFACTOR_SRC1_COLOR:      return SVGA3D_BLENDOP_SRC1COLOR;
   case PIPE_BLENDFACTOR_INV_SRC1_COLOR:  return SVGA3D_BLENDOP_INVSRC1COLOR;
   case PIPE_BLENDFACTOR_SRC1_ALPHA:      return SVGA3D_BLENDOP_SRC1ALPHA;
   case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:  return SVGA3D_BLENDOP_INVSRC1ALPHA;
   case 0:                                return SVGA3D_BLENDOP_ONE;
   default:
      assert(0);
      return SVGA3D_BLENDOP_ZERO;
   }
}

static inline unsigned
svga_translate_blend_func(unsigned mode)
{
   switch (mode) {
   case PIPE_BLEND_ADD:              return SVGA3D_BLENDEQ_ADD;
   case PIPE_BLEND_SUBTRACT:         return SVGA3D_BLENDEQ_SUBTRACT;
   case PIPE_BLEND_REVERSE_SUBTRACT: return SVGA3D_BLENDEQ_REVSUBTRACT;
   case PIPE_BLEND_MIN:              return SVGA3D_BLENDEQ_MINIMUM;
   case PIPE_BLEND_MAX:              return SVGA3D_BLENDEQ_MAXIMUM;
   default:
      assert(0);
      return SVGA3D_BLENDEQ_ADD;
   }
}


/**
 * Define a vgpu10 blend state object for the given
 * svga blend state.
 */
static void
define_blend_state_object(struct svga_context *svga,
                          struct svga_blend_state *bs)
{
   SVGA3dDXBlendStatePerRT perRT[SVGA3D_MAX_RENDER_TARGETS];
   unsigned try;
   int i;

   assert(svga_have_vgpu10(svga));

   bs->id = util_bitmask_add(svga->blend_object_id_bm);

   for (i = 0; i < SVGA3D_DX_MAX_RENDER_TARGETS; i++) {
      perRT[i].blendEnable = bs->rt[i].blend_enable;
      perRT[i].srcBlend = bs->rt[i].srcblend;
      perRT[i].destBlend = bs->rt[i].dstblend;
      perRT[i].blendOp = bs->rt[i].blendeq;
      perRT[i].srcBlendAlpha = bs->rt[i].srcblend_alpha;
      perRT[i].destBlendAlpha = bs->rt[i].dstblend_alpha;
      perRT[i].blendOpAlpha = bs->rt[i].blendeq_alpha;
      perRT[i].renderTargetWriteMask = bs->rt[i].writemask;
      perRT[i].logicOpEnable = 0;
      perRT[i].logicOp = SVGA3D_LOGICOP_COPY;
   }

   /* Loop in case command buffer is full and we need to flush and retry */
   for (try = 0; try < 2; try++) {
      enum pipe_error ret;

      ret = SVGA3D_vgpu10_DefineBlendState(svga->swc,
                                           bs->id,
                                           bs->alpha_to_coverage,
                                           bs->independent_blend_enable,
                                           perRT);
      if (ret == PIPE_OK)
         return;
      svga_context_flush(svga, NULL);
   }
}


static void *
svga_create_blend_state(struct pipe_context *pipe,
                        const struct pipe_blend_state *templ)
{
   struct svga_context *svga = svga_context(pipe);
   struct svga_blend_state *blend = CALLOC_STRUCT( svga_blend_state );
   unsigned i;

   if (!blend)
      return NULL;

   /* Find index of first target with blending enabled.  If no blending is
    * enabled at all, first_enabled will be zero.
    */
   unsigned first_enabled = 0;
   for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
      if (templ->rt[i].blend_enable) {
         first_enabled = i;
         break;
      }
   }

   /* Fill in the per-rendertarget blend state.  We currently only
    * support independent blend enable and colormask per render target.
    */
   for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
      /* No way to set this in SVGA3D, and no way to correctly implement it on
       * top of D3D9 API.  Instead we try to simulate with various blend modes.
       */
      if (templ->logicop_enable) {
         switch (templ->logicop_func) {
         case PIPE_LOGICOP_XOR:
         case PIPE_LOGICOP_INVERT:
            blend->need_white_fragments = TRUE;
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_ONE;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_ONE;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_SUBTRACT;
            break;
         case PIPE_LOGICOP_CLEAR:
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MINIMUM;
            break;
         case PIPE_LOGICOP_COPY:
            blend->rt[i].blend_enable = FALSE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_ONE;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_ADD;
            break;
         case PIPE_LOGICOP_COPY_INVERTED:
            blend->rt[i].blend_enable   = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_INVSRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_ADD;
            break;
         case PIPE_LOGICOP_NOOP:
            blend->rt[i].blend_enable   = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_DESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_ADD;
            break;
         case PIPE_LOGICOP_SET:
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_ONE;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_ONE;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MAXIMUM;
            break;
         case PIPE_LOGICOP_AND:
            /* Approximate with minimum - works for the 0 & anything case: */
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_SRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_DESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MINIMUM;
            break;
         case PIPE_LOGICOP_AND_REVERSE:
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_SRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_INVDESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MINIMUM;
            break;
         case PIPE_LOGICOP_AND_INVERTED:
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_INVSRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_DESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MINIMUM;
            break;
         case PIPE_LOGICOP_OR:
            /* Approximate with maximum - works for the 1 | anything case: */
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_SRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_DESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MAXIMUM;
            break;
         case PIPE_LOGICOP_OR_REVERSE:
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_SRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_INVDESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MAXIMUM;
            break;
         case PIPE_LOGICOP_OR_INVERTED:
            blend->rt[i].blend_enable = TRUE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_INVSRCCOLOR;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_DESTCOLOR;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_MAXIMUM;
            break;
         case PIPE_LOGICOP_NAND:
         case PIPE_LOGICOP_NOR:
         case PIPE_LOGICOP_EQUIV:
            /* Fill these in with plausible values */
            blend->rt[i].blend_enable = FALSE;
            blend->rt[i].srcblend       = SVGA3D_BLENDOP_ONE;
            blend->rt[i].dstblend       = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].blendeq        = SVGA3D_BLENDEQ_ADD;
            break;
         default:
            assert(0);
            break;
         }
         blend->rt[i].srcblend_alpha = blend->rt[i].srcblend;
         blend->rt[i].dstblend_alpha = blend->rt[i].dstblend;
         blend->rt[i].blendeq_alpha = blend->rt[i].blendeq;

         if (templ->logicop_func == PIPE_LOGICOP_XOR) {
            pipe_debug_message(&svga->debug.callback, CONFORMANCE,
                               "XOR logicop mode has limited support");
         }
         else if (templ->logicop_func != PIPE_LOGICOP_COPY) {
            pipe_debug_message(&svga->debug.callback, CONFORMANCE,
                               "general logicops are not supported");
         }
      }
      else {
         /* Note: per-target blend terms are only supported for sm4_1
          * device. For vgpu10 device, the blending terms must be identical
          * for all targets (this is why we need the first_enabled index).
          */
         const unsigned j =
            svga_have_sm4_1(svga) && templ->independent_blend_enable
            ? i : first_enabled;
         if (templ->independent_blend_enable || templ->rt[j].blend_enable) {
            blend->rt[i].srcblend =
               svga_translate_blend_factor(svga, templ->rt[j].rgb_src_factor);
            blend->rt[i].dstblend =
               svga_translate_blend_factor(svga, templ->rt[j].rgb_dst_factor);
            blend->rt[i].blendeq =
               svga_translate_blend_func(templ->rt[j].rgb_func);
            blend->rt[i].srcblend_alpha =
               svga_translate_blend_factor(svga, templ->rt[j].alpha_src_factor);
            blend->rt[i].dstblend_alpha =
               svga_translate_blend_factor(svga, templ->rt[j].alpha_dst_factor);
            blend->rt[i].blendeq_alpha =
               svga_translate_blend_func(templ->rt[j].alpha_func);

            if (blend->rt[i].srcblend_alpha != blend->rt[i].srcblend ||
                blend->rt[i].dstblend_alpha != blend->rt[i].dstblend ||
                blend->rt[i].blendeq_alpha  != blend->rt[i].blendeq) {
               blend->rt[i].separate_alpha_blend_enable = TRUE;
            }
         }
         else {
            /* disabled - default blend terms */
            blend->rt[i].srcblend = SVGA3D_BLENDOP_ONE;
            blend->rt[i].dstblend = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].blendeq = SVGA3D_BLENDEQ_ADD;
            blend->rt[i].srcblend_alpha = SVGA3D_BLENDOP_ONE;
            blend->rt[i].dstblend_alpha = SVGA3D_BLENDOP_ZERO;
            blend->rt[i].blendeq_alpha = SVGA3D_BLENDEQ_ADD;
         }

         if (templ->independent_blend_enable) {
            blend->rt[i].blend_enable = templ->rt[i].blend_enable;
         }
         else {
            blend->rt[i].blend_enable = templ->rt[0].blend_enable;
         }
      }

      /* Some GL blend modes are not supported by the VGPU9 device (there's
       * no equivalent of PIPE_BLENDFACTOR_[INV_]CONST_ALPHA).
       * When we set this flag, we copy the constant blend alpha value
       * to the R, G, B components.
       * This works as long as the src/dst RGB blend factors doesn't use
       * PIPE_BLENDFACTOR_CONST_COLOR and PIPE_BLENDFACTOR_CONST_ALPHA
       * at the same time.  There's no work-around for that.
       */
      if (!svga_have_vgpu10(svga)) {
         if (templ->rt[0].rgb_src_factor == PIPE_BLENDFACTOR_CONST_ALPHA ||
             templ->rt[0].rgb_dst_factor == PIPE_BLENDFACTOR_CONST_ALPHA ||
             templ->rt[0].rgb_src_factor == PIPE_BLENDFACTOR_INV_CONST_ALPHA ||
             templ->rt[0].rgb_dst_factor == PIPE_BLENDFACTOR_INV_CONST_ALPHA) {
            blend->blend_color_alpha = TRUE;
         }
      }

      if (templ->independent_blend_enable) {
         blend->rt[i].writemask = templ->rt[i].colormask;
      }
      else {
         blend->rt[i].writemask = templ->rt[0].colormask;
      }
   }

   blend->independent_blend_enable = templ->independent_blend_enable;

   blend->alpha_to_coverage = templ->alpha_to_coverage;
   blend->alpha_to_one = templ->alpha_to_one;

   if (svga_have_vgpu10(svga)) {
      define_blend_state_object(svga, blend);
   }

   svga->hud.num_blend_objects++;
   SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
                        SVGA_STATS_COUNT_BLENDSTATE);

   return blend;
}


static void svga_bind_blend_state(struct pipe_context *pipe,
                                  void *blend)
{
   struct svga_context *svga = svga_context(pipe);

   svga->curr.blend = (struct svga_blend_state*)blend;
   svga->dirty |= SVGA_NEW_BLEND;
}

static void svga_delete_blend_state(struct pipe_context *pipe,
                                    void *blend)
{
   struct svga_context *svga = svga_context(pipe);
   struct svga_blend_state *bs =
      (struct svga_blend_state *) blend;

   if (svga_have_vgpu10(svga) && bs->id != SVGA3D_INVALID_ID) {
      enum pipe_error ret;

      ret = SVGA3D_vgpu10_DestroyBlendState(svga->swc, bs->id);
      if (ret != PIPE_OK) {
         svga_context_flush(svga, NULL);
         ret = SVGA3D_vgpu10_DestroyBlendState(svga->swc, bs->id);
         assert(ret == PIPE_OK);
      }

      if (bs->id == svga->state.hw_draw.blend_id)
         svga->state.hw_draw.blend_id = SVGA3D_INVALID_ID;

      util_bitmask_clear(svga->blend_object_id_bm, bs->id);
      bs->id = SVGA3D_INVALID_ID;
   }

   FREE(blend);
   svga->hud.num_blend_objects--;
}

static void svga_set_blend_color( struct pipe_context *pipe,
                                  const struct pipe_blend_color *blend_color )
{
   struct svga_context *svga = svga_context(pipe);

   svga->curr.blend_color = *blend_color;

   svga->dirty |= SVGA_NEW_BLEND_COLOR;
}


void svga_init_blend_functions( struct svga_context *svga )
{
   svga->pipe.create_blend_state = svga_create_blend_state;
   svga->pipe.bind_blend_state = svga_bind_blend_state;
   svga->pipe.delete_blend_state = svga_delete_blend_state;

   svga->pipe.set_blend_color = svga_set_blend_color;
}