Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / gallium / drivers / svga / svga_pipe_blend.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_bitmask.h"
31
32 #include "svga_context.h"
33 #include "svga_hw_reg.h"
34 #include "svga_cmd.h"
35
36
37 static inline unsigned
38 svga_translate_blend_factor(const struct svga_context *svga, unsigned factor)
39 {
40 switch (factor) {
41 case PIPE_BLENDFACTOR_ZERO: return SVGA3D_BLENDOP_ZERO;
42 case PIPE_BLENDFACTOR_SRC_ALPHA: return SVGA3D_BLENDOP_SRCALPHA;
43 case PIPE_BLENDFACTOR_ONE: return SVGA3D_BLENDOP_ONE;
44 case PIPE_BLENDFACTOR_SRC_COLOR: return SVGA3D_BLENDOP_SRCCOLOR;
45 case PIPE_BLENDFACTOR_INV_SRC_COLOR: return SVGA3D_BLENDOP_INVSRCCOLOR;
46 case PIPE_BLENDFACTOR_DST_COLOR: return SVGA3D_BLENDOP_DESTCOLOR;
47 case PIPE_BLENDFACTOR_INV_DST_COLOR: return SVGA3D_BLENDOP_INVDESTCOLOR;
48 case PIPE_BLENDFACTOR_INV_SRC_ALPHA: return SVGA3D_BLENDOP_INVSRCALPHA;
49 case PIPE_BLENDFACTOR_DST_ALPHA: return SVGA3D_BLENDOP_DESTALPHA;
50 case PIPE_BLENDFACTOR_INV_DST_ALPHA: return SVGA3D_BLENDOP_INVDESTALPHA;
51 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE: return SVGA3D_BLENDOP_SRCALPHASAT;
52 case PIPE_BLENDFACTOR_CONST_COLOR: return SVGA3D_BLENDOP_BLENDFACTOR;
53 case PIPE_BLENDFACTOR_INV_CONST_COLOR: return SVGA3D_BLENDOP_INVBLENDFACTOR;
54 case PIPE_BLENDFACTOR_CONST_ALPHA:
55 if (svga_have_vgpu10(svga))
56 return SVGA3D_BLENDOP_BLENDFACTORALPHA;
57 else
58 return SVGA3D_BLENDOP_BLENDFACTOR; /* as close as we can get */
59 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
60 if (svga_have_vgpu10(svga))
61 return SVGA3D_BLENDOP_INVBLENDFACTORALPHA;
62 else
63 return SVGA3D_BLENDOP_INVBLENDFACTOR; /* as close as we can get */
64 case PIPE_BLENDFACTOR_SRC1_COLOR: return SVGA3D_BLENDOP_SRC1COLOR;
65 case PIPE_BLENDFACTOR_INV_SRC1_COLOR: return SVGA3D_BLENDOP_INVSRC1COLOR;
66 case PIPE_BLENDFACTOR_SRC1_ALPHA: return SVGA3D_BLENDOP_SRC1ALPHA;
67 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: return SVGA3D_BLENDOP_INVSRC1ALPHA;
68 case 0: return SVGA3D_BLENDOP_ONE;
69 default:
70 assert(0);
71 return SVGA3D_BLENDOP_ZERO;
72 }
73 }
74
75 static inline unsigned
76 svga_translate_blend_func(unsigned mode)
77 {
78 switch (mode) {
79 case PIPE_BLEND_ADD: return SVGA3D_BLENDEQ_ADD;
80 case PIPE_BLEND_SUBTRACT: return SVGA3D_BLENDEQ_SUBTRACT;
81 case PIPE_BLEND_REVERSE_SUBTRACT: return SVGA3D_BLENDEQ_REVSUBTRACT;
82 case PIPE_BLEND_MIN: return SVGA3D_BLENDEQ_MINIMUM;
83 case PIPE_BLEND_MAX: return SVGA3D_BLENDEQ_MAXIMUM;
84 default:
85 assert(0);
86 return SVGA3D_BLENDEQ_ADD;
87 }
88 }
89
90
91 /**
92 * Define a vgpu10 blend state object for the given
93 * svga blend state.
94 */
95 static void
96 define_blend_state_object(struct svga_context *svga,
97 struct svga_blend_state *bs)
98 {
99 SVGA3dDXBlendStatePerRT perRT[SVGA3D_MAX_RENDER_TARGETS];
100 unsigned try;
101 int i;
102
103 assert(svga_have_vgpu10(svga));
104
105 bs->id = util_bitmask_add(svga->blend_object_id_bm);
106
107 for (i = 0; i < SVGA3D_DX_MAX_RENDER_TARGETS; i++) {
108 perRT[i].blendEnable = bs->rt[i].blend_enable;
109 perRT[i].srcBlend = bs->rt[i].srcblend;
110 perRT[i].destBlend = bs->rt[i].dstblend;
111 perRT[i].blendOp = bs->rt[i].blendeq;
112 perRT[i].srcBlendAlpha = bs->rt[i].srcblend_alpha;
113 perRT[i].destBlendAlpha = bs->rt[i].dstblend_alpha;
114 perRT[i].blendOpAlpha = bs->rt[i].blendeq_alpha;
115 perRT[i].renderTargetWriteMask = bs->rt[i].writemask;
116 perRT[i].logicOpEnable = 0;
117 perRT[i].logicOp = SVGA3D_LOGICOP_COPY;
118 assert(perRT[i].srcBlend == perRT[0].srcBlend);
119 }
120
121 /* Loop in case command buffer is full and we need to flush and retry */
122 for (try = 0; try < 2; try++) {
123 enum pipe_error ret;
124
125 ret = SVGA3D_vgpu10_DefineBlendState(svga->swc,
126 bs->id,
127 bs->alpha_to_coverage,
128 bs->independent_blend_enable,
129 perRT);
130 if (ret == PIPE_OK)
131 return;
132 svga_context_flush(svga, NULL);
133 }
134 }
135
136
137 static void *
138 svga_create_blend_state(struct pipe_context *pipe,
139 const struct pipe_blend_state *templ)
140 {
141 struct svga_context *svga = svga_context(pipe);
142 struct svga_blend_state *blend = CALLOC_STRUCT( svga_blend_state );
143 unsigned i;
144
145 /* Fill in the per-rendertarget blend state. We currently only
146 * support independent blend enable and colormask per render target.
147 */
148 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
149 /* No way to set this in SVGA3D, and no way to correctly implement it on
150 * top of D3D9 API. Instead we try to simulate with various blend modes.
151 */
152 if (templ->logicop_enable) {
153 switch (templ->logicop_func) {
154 case PIPE_LOGICOP_XOR:
155 case PIPE_LOGICOP_INVERT:
156 blend->need_white_fragments = TRUE;
157 blend->rt[i].blend_enable = TRUE;
158 blend->rt[i].srcblend = SVGA3D_BLENDOP_ONE;
159 blend->rt[i].dstblend = SVGA3D_BLENDOP_ONE;
160 blend->rt[i].blendeq = SVGA3D_BLENDEQ_SUBTRACT;
161 break;
162 case PIPE_LOGICOP_CLEAR:
163 blend->rt[i].blend_enable = TRUE;
164 blend->rt[i].srcblend = SVGA3D_BLENDOP_ZERO;
165 blend->rt[i].dstblend = SVGA3D_BLENDOP_ZERO;
166 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MINIMUM;
167 break;
168 case PIPE_LOGICOP_COPY:
169 blend->rt[i].blend_enable = FALSE;
170 blend->rt[i].srcblend = SVGA3D_BLENDOP_ONE;
171 blend->rt[i].dstblend = SVGA3D_BLENDOP_ZERO;
172 blend->rt[i].blendeq = SVGA3D_BLENDEQ_ADD;
173 break;
174 case PIPE_LOGICOP_COPY_INVERTED:
175 blend->rt[i].blend_enable = TRUE;
176 blend->rt[i].srcblend = SVGA3D_BLENDOP_INVSRCCOLOR;
177 blend->rt[i].dstblend = SVGA3D_BLENDOP_ZERO;
178 blend->rt[i].blendeq = SVGA3D_BLENDEQ_ADD;
179 break;
180 case PIPE_LOGICOP_NOOP:
181 blend->rt[i].blend_enable = TRUE;
182 blend->rt[i].srcblend = SVGA3D_BLENDOP_ZERO;
183 blend->rt[i].dstblend = SVGA3D_BLENDOP_DESTCOLOR;
184 blend->rt[i].blendeq = SVGA3D_BLENDEQ_ADD;
185 break;
186 case PIPE_LOGICOP_SET:
187 blend->rt[i].blend_enable = TRUE;
188 blend->rt[i].srcblend = SVGA3D_BLENDOP_ONE;
189 blend->rt[i].dstblend = SVGA3D_BLENDOP_ONE;
190 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MAXIMUM;
191 break;
192 case PIPE_LOGICOP_AND:
193 /* Approximate with minimum - works for the 0 & anything case: */
194 blend->rt[i].blend_enable = TRUE;
195 blend->rt[i].srcblend = SVGA3D_BLENDOP_SRCCOLOR;
196 blend->rt[i].dstblend = SVGA3D_BLENDOP_DESTCOLOR;
197 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MINIMUM;
198 break;
199 case PIPE_LOGICOP_AND_REVERSE:
200 blend->rt[i].blend_enable = TRUE;
201 blend->rt[i].srcblend = SVGA3D_BLENDOP_SRCCOLOR;
202 blend->rt[i].dstblend = SVGA3D_BLENDOP_INVDESTCOLOR;
203 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MINIMUM;
204 break;
205 case PIPE_LOGICOP_AND_INVERTED:
206 blend->rt[i].blend_enable = TRUE;
207 blend->rt[i].srcblend = SVGA3D_BLENDOP_INVSRCCOLOR;
208 blend->rt[i].dstblend = SVGA3D_BLENDOP_DESTCOLOR;
209 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MINIMUM;
210 break;
211 case PIPE_LOGICOP_OR:
212 /* Approximate with maximum - works for the 1 | anything case: */
213 blend->rt[i].blend_enable = TRUE;
214 blend->rt[i].srcblend = SVGA3D_BLENDOP_SRCCOLOR;
215 blend->rt[i].dstblend = SVGA3D_BLENDOP_DESTCOLOR;
216 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MAXIMUM;
217 break;
218 case PIPE_LOGICOP_OR_REVERSE:
219 blend->rt[i].blend_enable = TRUE;
220 blend->rt[i].srcblend = SVGA3D_BLENDOP_SRCCOLOR;
221 blend->rt[i].dstblend = SVGA3D_BLENDOP_INVDESTCOLOR;
222 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MAXIMUM;
223 break;
224 case PIPE_LOGICOP_OR_INVERTED:
225 blend->rt[i].blend_enable = TRUE;
226 blend->rt[i].srcblend = SVGA3D_BLENDOP_INVSRCCOLOR;
227 blend->rt[i].dstblend = SVGA3D_BLENDOP_DESTCOLOR;
228 blend->rt[i].blendeq = SVGA3D_BLENDEQ_MAXIMUM;
229 break;
230 case PIPE_LOGICOP_NAND:
231 case PIPE_LOGICOP_NOR:
232 case PIPE_LOGICOP_EQUIV:
233 /* Fill these in with plausible values */
234 blend->rt[i].blend_enable = FALSE;
235 blend->rt[i].srcblend = SVGA3D_BLENDOP_ONE;
236 blend->rt[i].dstblend = SVGA3D_BLENDOP_ZERO;
237 blend->rt[i].blendeq = SVGA3D_BLENDEQ_ADD;
238 break;
239 default:
240 assert(0);
241 break;
242 }
243 blend->rt[i].srcblend_alpha = blend->rt[i].srcblend;
244 blend->rt[i].dstblend_alpha = blend->rt[i].dstblend;
245 blend->rt[i].blendeq_alpha = blend->rt[i].blendeq;
246
247 if (templ->logicop_func == PIPE_LOGICOP_XOR) {
248 pipe_debug_message(&svga->debug.callback, CONFORMANCE,
249 "XOR logicop mode has limited support");
250 }
251 else if (templ->logicop_func != PIPE_LOGICOP_COPY) {
252 pipe_debug_message(&svga->debug.callback, CONFORMANCE,
253 "general logicops are not supported");
254 }
255 }
256 else {
257 /* Note: the vgpu10 device does not yet support independent
258 * blend terms per render target. Target[0] always specifies the
259 * blending terms.
260 */
261 if (templ->independent_blend_enable || templ->rt[0].blend_enable) {
262 /* always use the 0th target's blending terms for now */
263 blend->rt[i].srcblend =
264 svga_translate_blend_factor(svga, templ->rt[0].rgb_src_factor);
265 blend->rt[i].dstblend =
266 svga_translate_blend_factor(svga, templ->rt[0].rgb_dst_factor);
267 blend->rt[i].blendeq =
268 svga_translate_blend_func(templ->rt[0].rgb_func);
269 blend->rt[i].srcblend_alpha =
270 svga_translate_blend_factor(svga, templ->rt[0].alpha_src_factor);
271 blend->rt[i].dstblend_alpha =
272 svga_translate_blend_factor(svga, templ->rt[0].alpha_dst_factor);
273 blend->rt[i].blendeq_alpha =
274 svga_translate_blend_func(templ->rt[0].alpha_func);
275
276 if (blend->rt[i].srcblend_alpha != blend->rt[i].srcblend ||
277 blend->rt[i].dstblend_alpha != blend->rt[i].dstblend ||
278 blend->rt[i].blendeq_alpha != blend->rt[i].blendeq) {
279 blend->rt[i].separate_alpha_blend_enable = TRUE;
280 }
281 }
282 else {
283 /* disabled - default blend terms */
284 blend->rt[i].srcblend = SVGA3D_BLENDOP_ONE;
285 blend->rt[i].dstblend = SVGA3D_BLENDOP_ZERO;
286 blend->rt[i].blendeq = SVGA3D_BLENDEQ_ADD;
287 blend->rt[i].srcblend_alpha = SVGA3D_BLENDOP_ONE;
288 blend->rt[i].dstblend_alpha = SVGA3D_BLENDOP_ZERO;
289 blend->rt[i].blendeq_alpha = SVGA3D_BLENDEQ_ADD;
290 }
291
292 if (templ->independent_blend_enable) {
293 blend->rt[i].blend_enable = templ->rt[i].blend_enable;
294 }
295 else {
296 blend->rt[i].blend_enable = templ->rt[0].blend_enable;
297 }
298 }
299
300 /* Some GL blend modes are not supported by the VGPU9 device (there's
301 * no equivalent of PIPE_BLENDFACTOR_[INV_]CONST_ALPHA).
302 * When we set this flag, we copy the constant blend alpha value
303 * to the R, G, B components.
304 * This works as long as the src/dst RGB blend factors doesn't use
305 * PIPE_BLENDFACTOR_CONST_COLOR and PIPE_BLENDFACTOR_CONST_ALPHA
306 * at the same time. There's no work-around for that.
307 */
308 if (!svga_have_vgpu10(svga)) {
309 if (templ->rt[0].rgb_src_factor == PIPE_BLENDFACTOR_CONST_ALPHA ||
310 templ->rt[0].rgb_dst_factor == PIPE_BLENDFACTOR_CONST_ALPHA ||
311 templ->rt[0].rgb_src_factor == PIPE_BLENDFACTOR_INV_CONST_ALPHA ||
312 templ->rt[0].rgb_dst_factor == PIPE_BLENDFACTOR_INV_CONST_ALPHA) {
313 blend->blend_color_alpha = TRUE;
314 }
315 }
316
317 if (templ->independent_blend_enable) {
318 blend->rt[i].writemask = templ->rt[i].colormask;
319 }
320 else {
321 blend->rt[i].writemask = templ->rt[0].colormask;
322 }
323 }
324
325 blend->independent_blend_enable = templ->independent_blend_enable;
326
327 blend->alpha_to_coverage = templ->alpha_to_coverage;
328
329 if (svga_have_vgpu10(svga)) {
330 define_blend_state_object(svga, blend);
331 }
332
333 svga->hud.num_state_objects++;
334
335 return blend;
336 }
337
338
339 static void svga_bind_blend_state(struct pipe_context *pipe,
340 void *blend)
341 {
342 struct svga_context *svga = svga_context(pipe);
343
344 svga->curr.blend = (struct svga_blend_state*)blend;
345 svga->dirty |= SVGA_NEW_BLEND;
346 }
347
348 static void svga_delete_blend_state(struct pipe_context *pipe,
349 void *blend)
350 {
351 struct svga_context *svga = svga_context(pipe);
352 struct svga_blend_state *bs =
353 (struct svga_blend_state *) blend;
354
355 if (bs->id != SVGA3D_INVALID_ID) {
356 enum pipe_error ret;
357
358 ret = SVGA3D_vgpu10_DestroyBlendState(svga->swc, bs->id);
359 if (ret != PIPE_OK) {
360 svga_context_flush(svga, NULL);
361 ret = SVGA3D_vgpu10_DestroyBlendState(svga->swc, bs->id);
362 assert(ret == PIPE_OK);
363 }
364
365 if (bs->id == svga->state.hw_draw.blend_id)
366 svga->state.hw_draw.blend_id = SVGA3D_INVALID_ID;
367
368 util_bitmask_clear(svga->blend_object_id_bm, bs->id);
369 bs->id = SVGA3D_INVALID_ID;
370 }
371
372 FREE(blend);
373 svga->hud.num_state_objects--;
374 }
375
376 static void svga_set_blend_color( struct pipe_context *pipe,
377 const struct pipe_blend_color *blend_color )
378 {
379 struct svga_context *svga = svga_context(pipe);
380
381 svga->curr.blend_color = *blend_color;
382
383 svga->dirty |= SVGA_NEW_BLEND_COLOR;
384 }
385
386
387 void svga_init_blend_functions( struct svga_context *svga )
388 {
389 svga->pipe.create_blend_state = svga_create_blend_state;
390 svga->pipe.bind_blend_state = svga_bind_blend_state;
391 svga->pipe.delete_blend_state = svga_delete_blend_state;
392
393 svga->pipe.set_blend_color = svga_set_blend_color;
394 }