1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
32 #include "svga_context.h"
33 #include "svga_hw_reg.h"
37 static inline unsigned
38 svga_translate_blend_factor(const struct svga_context
*svga
, unsigned factor
)
40 /* Note: there is no SVGA3D_BLENDOP_[INV]BLENDFACTORALPHA so
41 * we can't translate PIPE_BLENDFACTOR_[INV_]CONST_ALPHA properly.
44 case PIPE_BLENDFACTOR_ZERO
: return SVGA3D_BLENDOP_ZERO
;
45 case PIPE_BLENDFACTOR_SRC_ALPHA
: return SVGA3D_BLENDOP_SRCALPHA
;
46 case PIPE_BLENDFACTOR_ONE
: return SVGA3D_BLENDOP_ONE
;
47 case PIPE_BLENDFACTOR_SRC_COLOR
: return SVGA3D_BLENDOP_SRCCOLOR
;
48 case PIPE_BLENDFACTOR_INV_SRC_COLOR
: return SVGA3D_BLENDOP_INVSRCCOLOR
;
49 case PIPE_BLENDFACTOR_DST_COLOR
: return SVGA3D_BLENDOP_DESTCOLOR
;
50 case PIPE_BLENDFACTOR_INV_DST_COLOR
: return SVGA3D_BLENDOP_INVDESTCOLOR
;
51 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
: return SVGA3D_BLENDOP_INVSRCALPHA
;
52 case PIPE_BLENDFACTOR_DST_ALPHA
: return SVGA3D_BLENDOP_DESTALPHA
;
53 case PIPE_BLENDFACTOR_INV_DST_ALPHA
: return SVGA3D_BLENDOP_INVDESTALPHA
;
54 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
: return SVGA3D_BLENDOP_SRCALPHASAT
;
55 case PIPE_BLENDFACTOR_CONST_COLOR
: return SVGA3D_BLENDOP_BLENDFACTOR
;
56 case PIPE_BLENDFACTOR_INV_CONST_COLOR
: return SVGA3D_BLENDOP_INVBLENDFACTOR
;
57 case PIPE_BLENDFACTOR_CONST_ALPHA
:
58 if (svga_have_vgpu10(svga
))
59 return SVGA3D_BLENDOP_BLENDFACTORALPHA
;
61 return SVGA3D_BLENDOP_BLENDFACTOR
; /* as close as we can get */
62 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
63 if (svga_have_vgpu10(svga
))
64 return SVGA3D_BLENDOP_INVBLENDFACTORALPHA
;
66 return SVGA3D_BLENDOP_INVBLENDFACTOR
; /* as close as we can get */
67 case PIPE_BLENDFACTOR_SRC1_COLOR
: return SVGA3D_BLENDOP_SRC1COLOR
;
68 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
: return SVGA3D_BLENDOP_INVSRC1COLOR
;
69 case PIPE_BLENDFACTOR_SRC1_ALPHA
: return SVGA3D_BLENDOP_SRC1ALPHA
;
70 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
: return SVGA3D_BLENDOP_INVSRC1ALPHA
;
71 case 0: return SVGA3D_BLENDOP_ONE
;
74 return SVGA3D_BLENDOP_ZERO
;
78 static inline unsigned
79 svga_translate_blend_func(unsigned mode
)
82 case PIPE_BLEND_ADD
: return SVGA3D_BLENDEQ_ADD
;
83 case PIPE_BLEND_SUBTRACT
: return SVGA3D_BLENDEQ_SUBTRACT
;
84 case PIPE_BLEND_REVERSE_SUBTRACT
: return SVGA3D_BLENDEQ_REVSUBTRACT
;
85 case PIPE_BLEND_MIN
: return SVGA3D_BLENDEQ_MINIMUM
;
86 case PIPE_BLEND_MAX
: return SVGA3D_BLENDEQ_MAXIMUM
;
89 return SVGA3D_BLENDEQ_ADD
;
95 * Define a vgpu10 blend state object for the given
99 define_blend_state_object(struct svga_context
*svga
,
100 struct svga_blend_state
*bs
)
102 SVGA3dDXBlendStatePerRT perRT
[SVGA3D_MAX_RENDER_TARGETS
];
106 assert(svga_have_vgpu10(svga
));
108 bs
->id
= util_bitmask_add(svga
->blend_object_id_bm
);
110 for (i
= 0; i
< SVGA3D_DX_MAX_RENDER_TARGETS
; i
++) {
111 perRT
[i
].blendEnable
= bs
->rt
[i
].blend_enable
;
112 perRT
[i
].srcBlend
= bs
->rt
[i
].srcblend
;
113 perRT
[i
].destBlend
= bs
->rt
[i
].dstblend
;
114 perRT
[i
].blendOp
= bs
->rt
[i
].blendeq
;
115 perRT
[i
].srcBlendAlpha
= bs
->rt
[i
].srcblend_alpha
;
116 perRT
[i
].destBlendAlpha
= bs
->rt
[i
].dstblend_alpha
;
117 perRT
[i
].blendOpAlpha
= bs
->rt
[i
].blendeq_alpha
;
118 perRT
[i
].renderTargetWriteMask
= bs
->rt
[i
].writemask
;
119 perRT
[i
].logicOpEnable
= 0;
120 perRT
[i
].logicOp
= SVGA3D_LOGICOP_COPY
;
121 assert(perRT
[i
].srcBlend
== perRT
[0].srcBlend
);
124 /* Loop in case command buffer is full and we need to flush and retry */
125 for (try = 0; try < 2; try++) {
128 ret
= SVGA3D_vgpu10_DefineBlendState(svga
->swc
,
130 bs
->alpha_to_coverage
,
131 bs
->independent_blend_enable
,
135 svga_context_flush(svga
, NULL
);
141 svga_create_blend_state(struct pipe_context
*pipe
,
142 const struct pipe_blend_state
*templ
)
144 struct svga_context
*svga
= svga_context(pipe
);
145 struct svga_blend_state
*blend
= CALLOC_STRUCT( svga_blend_state
);
151 /* Find index of first target with blending enabled. -1 means blending
152 * is not enabled at all.
154 int first_enabled
= -1;
155 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
156 if (templ
->rt
[i
].blend_enable
) {
162 /* Fill in the per-rendertarget blend state. We currently only
163 * support independent blend enable and colormask per render target.
165 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
166 /* No way to set this in SVGA3D, and no way to correctly implement it on
167 * top of D3D9 API. Instead we try to simulate with various blend modes.
169 if (templ
->logicop_enable
) {
170 switch (templ
->logicop_func
) {
171 case PIPE_LOGICOP_XOR
:
172 case PIPE_LOGICOP_INVERT
:
173 blend
->need_white_fragments
= TRUE
;
174 blend
->rt
[i
].blend_enable
= TRUE
;
175 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ONE
;
176 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ONE
;
177 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_SUBTRACT
;
179 case PIPE_LOGICOP_CLEAR
:
180 blend
->rt
[i
].blend_enable
= TRUE
;
181 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ZERO
;
182 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ZERO
;
183 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MINIMUM
;
185 case PIPE_LOGICOP_COPY
:
186 blend
->rt
[i
].blend_enable
= FALSE
;
187 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ONE
;
188 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ZERO
;
189 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_ADD
;
191 case PIPE_LOGICOP_COPY_INVERTED
:
192 blend
->rt
[i
].blend_enable
= TRUE
;
193 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_INVSRCCOLOR
;
194 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ZERO
;
195 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_ADD
;
197 case PIPE_LOGICOP_NOOP
:
198 blend
->rt
[i
].blend_enable
= TRUE
;
199 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ZERO
;
200 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_DESTCOLOR
;
201 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_ADD
;
203 case PIPE_LOGICOP_SET
:
204 blend
->rt
[i
].blend_enable
= TRUE
;
205 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ONE
;
206 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ONE
;
207 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MAXIMUM
;
209 case PIPE_LOGICOP_AND
:
210 /* Approximate with minimum - works for the 0 & anything case: */
211 blend
->rt
[i
].blend_enable
= TRUE
;
212 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_SRCCOLOR
;
213 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_DESTCOLOR
;
214 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MINIMUM
;
216 case PIPE_LOGICOP_AND_REVERSE
:
217 blend
->rt
[i
].blend_enable
= TRUE
;
218 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_SRCCOLOR
;
219 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_INVDESTCOLOR
;
220 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MINIMUM
;
222 case PIPE_LOGICOP_AND_INVERTED
:
223 blend
->rt
[i
].blend_enable
= TRUE
;
224 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_INVSRCCOLOR
;
225 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_DESTCOLOR
;
226 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MINIMUM
;
228 case PIPE_LOGICOP_OR
:
229 /* Approximate with maximum - works for the 1 | anything case: */
230 blend
->rt
[i
].blend_enable
= TRUE
;
231 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_SRCCOLOR
;
232 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_DESTCOLOR
;
233 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MAXIMUM
;
235 case PIPE_LOGICOP_OR_REVERSE
:
236 blend
->rt
[i
].blend_enable
= TRUE
;
237 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_SRCCOLOR
;
238 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_INVDESTCOLOR
;
239 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MAXIMUM
;
241 case PIPE_LOGICOP_OR_INVERTED
:
242 blend
->rt
[i
].blend_enable
= TRUE
;
243 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_INVSRCCOLOR
;
244 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_DESTCOLOR
;
245 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_MAXIMUM
;
247 case PIPE_LOGICOP_NAND
:
248 case PIPE_LOGICOP_NOR
:
249 case PIPE_LOGICOP_EQUIV
:
250 /* Fill these in with plausible values */
251 blend
->rt
[i
].blend_enable
= FALSE
;
252 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ONE
;
253 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ZERO
;
254 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_ADD
;
260 blend
->rt
[i
].srcblend_alpha
= blend
->rt
[i
].srcblend
;
261 blend
->rt
[i
].dstblend_alpha
= blend
->rt
[i
].dstblend
;
262 blend
->rt
[i
].blendeq_alpha
= blend
->rt
[i
].blendeq
;
264 if (templ
->logicop_func
== PIPE_LOGICOP_XOR
) {
265 pipe_debug_message(&svga
->debug
.callback
, CONFORMANCE
,
266 "XOR logicop mode has limited support");
268 else if (templ
->logicop_func
!= PIPE_LOGICOP_COPY
) {
269 pipe_debug_message(&svga
->debug
.callback
, CONFORMANCE
,
270 "general logicops are not supported");
274 /* Note: the vgpu10 device does not yet support independent blend
275 * terms per render target. When blending is enabled, the blend
276 * terms must match for all targets.
278 if (first_enabled
>= 0) {
279 /* use first enabled target's blending terms */
280 const struct pipe_rt_blend_state
*rt
= &templ
->rt
[first_enabled
];
282 blend
->rt
[i
].srcblend
=
283 svga_translate_blend_factor(svga
, rt
->rgb_src_factor
);
284 blend
->rt
[i
].dstblend
=
285 svga_translate_blend_factor(svga
, rt
->rgb_dst_factor
);
286 blend
->rt
[i
].blendeq
=
287 svga_translate_blend_func(rt
->rgb_func
);
288 blend
->rt
[i
].srcblend_alpha
=
289 svga_translate_blend_factor(svga
, rt
->alpha_src_factor
);
290 blend
->rt
[i
].dstblend_alpha
=
291 svga_translate_blend_factor(svga
, rt
->alpha_dst_factor
);
292 blend
->rt
[i
].blendeq_alpha
=
293 svga_translate_blend_func(rt
->alpha_func
);
295 if (blend
->rt
[i
].srcblend_alpha
!= blend
->rt
[i
].srcblend
||
296 blend
->rt
[i
].dstblend_alpha
!= blend
->rt
[i
].dstblend
||
297 blend
->rt
[i
].blendeq_alpha
!= blend
->rt
[i
].blendeq
) {
298 blend
->rt
[i
].separate_alpha_blend_enable
= TRUE
;
302 /* disabled - default blend terms */
303 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ONE
;
304 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ZERO
;
305 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_ADD
;
306 blend
->rt
[i
].srcblend_alpha
= SVGA3D_BLENDOP_ONE
;
307 blend
->rt
[i
].dstblend_alpha
= SVGA3D_BLENDOP_ZERO
;
308 blend
->rt
[i
].blendeq_alpha
= SVGA3D_BLENDEQ_ADD
;
311 if (templ
->independent_blend_enable
) {
312 blend
->rt
[i
].blend_enable
= templ
->rt
[i
].blend_enable
;
315 blend
->rt
[i
].blend_enable
= templ
->rt
[0].blend_enable
;
319 /* Some GL blend modes are not supported by the VGPU9 device (there's
320 * no equivalent of PIPE_BLENDFACTOR_[INV_]CONST_ALPHA).
321 * When we set this flag, we copy the constant blend alpha value
322 * to the R, G, B components.
323 * This works as long as the src/dst RGB blend factors doesn't use
324 * PIPE_BLENDFACTOR_CONST_COLOR and PIPE_BLENDFACTOR_CONST_ALPHA
325 * at the same time. There's no work-around for that.
327 if (!svga_have_vgpu10(svga
)) {
328 if (templ
->rt
[0].rgb_src_factor
== PIPE_BLENDFACTOR_CONST_ALPHA
||
329 templ
->rt
[0].rgb_dst_factor
== PIPE_BLENDFACTOR_CONST_ALPHA
||
330 templ
->rt
[0].rgb_src_factor
== PIPE_BLENDFACTOR_INV_CONST_ALPHA
||
331 templ
->rt
[0].rgb_dst_factor
== PIPE_BLENDFACTOR_INV_CONST_ALPHA
) {
332 blend
->blend_color_alpha
= TRUE
;
336 if (templ
->independent_blend_enable
) {
337 blend
->rt
[i
].writemask
= templ
->rt
[i
].colormask
;
340 blend
->rt
[i
].writemask
= templ
->rt
[0].colormask
;
344 blend
->independent_blend_enable
= templ
->independent_blend_enable
;
346 blend
->alpha_to_coverage
= templ
->alpha_to_coverage
;
347 blend
->alpha_to_one
= templ
->alpha_to_one
;
349 if (svga_have_vgpu10(svga
)) {
350 define_blend_state_object(svga
, blend
);
353 svga
->hud
.num_blend_objects
++;
354 SVGA_STATS_COUNT_INC(svga_screen(svga
->pipe
.screen
)->sws
,
355 SVGA_STATS_COUNT_BLENDSTATE
);
361 static void svga_bind_blend_state(struct pipe_context
*pipe
,
364 struct svga_context
*svga
= svga_context(pipe
);
366 svga
->curr
.blend
= (struct svga_blend_state
*)blend
;
367 svga
->dirty
|= SVGA_NEW_BLEND
;
370 static void svga_delete_blend_state(struct pipe_context
*pipe
,
373 struct svga_context
*svga
= svga_context(pipe
);
374 struct svga_blend_state
*bs
=
375 (struct svga_blend_state
*) blend
;
377 if (svga_have_vgpu10(svga
) && bs
->id
!= SVGA3D_INVALID_ID
) {
380 ret
= SVGA3D_vgpu10_DestroyBlendState(svga
->swc
, bs
->id
);
381 if (ret
!= PIPE_OK
) {
382 svga_context_flush(svga
, NULL
);
383 ret
= SVGA3D_vgpu10_DestroyBlendState(svga
->swc
, bs
->id
);
384 assert(ret
== PIPE_OK
);
387 if (bs
->id
== svga
->state
.hw_draw
.blend_id
)
388 svga
->state
.hw_draw
.blend_id
= SVGA3D_INVALID_ID
;
390 util_bitmask_clear(svga
->blend_object_id_bm
, bs
->id
);
391 bs
->id
= SVGA3D_INVALID_ID
;
395 svga
->hud
.num_blend_objects
--;
398 static void svga_set_blend_color( struct pipe_context
*pipe
,
399 const struct pipe_blend_color
*blend_color
)
401 struct svga_context
*svga
= svga_context(pipe
);
403 svga
->curr
.blend_color
= *blend_color
;
405 svga
->dirty
|= SVGA_NEW_BLEND_COLOR
;
409 void svga_init_blend_functions( struct svga_context
*svga
)
411 svga
->pipe
.create_blend_state
= svga_create_blend_state
;
412 svga
->pipe
.bind_blend_state
= svga_bind_blend_state
;
413 svga
->pipe
.delete_blend_state
= svga_delete_blend_state
;
415 svga
->pipe
.set_blend_color
= svga_set_blend_color
;