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
)
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
;
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
;
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
;
71 return SVGA3D_BLENDOP_ZERO
;
75 static inline unsigned
76 svga_translate_blend_func(unsigned 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
;
86 return SVGA3D_BLENDEQ_ADD
;
92 * Define a vgpu10 blend state object for the given
96 define_blend_state_object(struct svga_context
*svga
,
97 struct svga_blend_state
*bs
)
99 SVGA3dDXBlendStatePerRT perRT
[SVGA3D_MAX_RENDER_TARGETS
];
103 assert(svga_have_vgpu10(svga
));
105 bs
->id
= util_bitmask_add(svga
->blend_object_id_bm
);
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
);
121 /* Loop in case command buffer is full and we need to flush and retry */
122 for (try = 0; try < 2; try++) {
125 ret
= SVGA3D_vgpu10_DefineBlendState(svga
->swc
,
127 bs
->alpha_to_coverage
,
128 bs
->independent_blend_enable
,
132 svga_context_flush(svga
, NULL
);
138 svga_create_blend_state(struct pipe_context
*pipe
,
139 const struct pipe_blend_state
*templ
)
141 struct svga_context
*svga
= svga_context(pipe
);
142 struct svga_blend_state
*blend
= CALLOC_STRUCT( svga_blend_state
);
145 /* Fill in the per-rendertarget blend state. We currently only
146 * support independent blend enable and colormask per render target.
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.
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
248 /* Note: the vgpu10 device does not yet support independent
249 * blend terms per render target. Target[0] always specifies the
252 if (templ
->independent_blend_enable
|| templ
->rt
[0].blend_enable
) {
253 /* always use the 0th target's blending terms for now */
254 blend
->rt
[i
].srcblend
=
255 svga_translate_blend_factor(svga
, templ
->rt
[0].rgb_src_factor
);
256 blend
->rt
[i
].dstblend
=
257 svga_translate_blend_factor(svga
, templ
->rt
[0].rgb_dst_factor
);
258 blend
->rt
[i
].blendeq
=
259 svga_translate_blend_func(templ
->rt
[0].rgb_func
);
260 blend
->rt
[i
].srcblend_alpha
=
261 svga_translate_blend_factor(svga
, templ
->rt
[0].alpha_src_factor
);
262 blend
->rt
[i
].dstblend_alpha
=
263 svga_translate_blend_factor(svga
, templ
->rt
[0].alpha_dst_factor
);
264 blend
->rt
[i
].blendeq_alpha
=
265 svga_translate_blend_func(templ
->rt
[0].alpha_func
);
267 if (blend
->rt
[i
].srcblend_alpha
!= blend
->rt
[i
].srcblend
||
268 blend
->rt
[i
].dstblend_alpha
!= blend
->rt
[i
].dstblend
||
269 blend
->rt
[i
].blendeq_alpha
!= blend
->rt
[i
].blendeq
) {
270 blend
->rt
[i
].separate_alpha_blend_enable
= TRUE
;
274 /* disabled - default blend terms */
275 blend
->rt
[i
].srcblend
= SVGA3D_BLENDOP_ONE
;
276 blend
->rt
[i
].dstblend
= SVGA3D_BLENDOP_ZERO
;
277 blend
->rt
[i
].blendeq
= SVGA3D_BLENDEQ_ADD
;
278 blend
->rt
[i
].srcblend_alpha
= SVGA3D_BLENDOP_ONE
;
279 blend
->rt
[i
].dstblend_alpha
= SVGA3D_BLENDOP_ZERO
;
280 blend
->rt
[i
].blendeq_alpha
= SVGA3D_BLENDEQ_ADD
;
283 if (templ
->independent_blend_enable
) {
284 blend
->rt
[i
].blend_enable
= templ
->rt
[i
].blend_enable
;
287 blend
->rt
[i
].blend_enable
= templ
->rt
[0].blend_enable
;
291 /* Some GL blend modes are not supported by the VGPU9 device (there's
292 * no equivalent of PIPE_BLENDFACTOR_[INV_]CONST_ALPHA).
293 * When we set this flag, we copy the constant blend alpha value
294 * to the R, G, B components.
295 * This works as long as the src/dst RGB blend factors doesn't use
296 * PIPE_BLENDFACTOR_CONST_COLOR and PIPE_BLENDFACTOR_CONST_ALPHA
297 * at the same time. There's no work-around for that.
299 if (!svga_have_vgpu10(svga
)) {
300 if (templ
->rt
[0].rgb_src_factor
== PIPE_BLENDFACTOR_CONST_ALPHA
||
301 templ
->rt
[0].rgb_dst_factor
== PIPE_BLENDFACTOR_CONST_ALPHA
||
302 templ
->rt
[0].rgb_src_factor
== PIPE_BLENDFACTOR_INV_CONST_ALPHA
||
303 templ
->rt
[0].rgb_dst_factor
== PIPE_BLENDFACTOR_INV_CONST_ALPHA
) {
304 blend
->blend_color_alpha
= TRUE
;
308 if (templ
->independent_blend_enable
) {
309 blend
->rt
[i
].writemask
= templ
->rt
[i
].colormask
;
312 blend
->rt
[i
].writemask
= templ
->rt
[0].colormask
;
316 blend
->independent_blend_enable
= templ
->independent_blend_enable
;
318 blend
->alpha_to_coverage
= templ
->alpha_to_coverage
;
320 if (svga_have_vgpu10(svga
)) {
321 define_blend_state_object(svga
, blend
);
328 static void svga_bind_blend_state(struct pipe_context
*pipe
,
331 struct svga_context
*svga
= svga_context(pipe
);
333 svga
->curr
.blend
= (struct svga_blend_state
*)blend
;
334 svga
->dirty
|= SVGA_NEW_BLEND
;
337 static void svga_delete_blend_state(struct pipe_context
*pipe
,
340 struct svga_context
*svga
= svga_context(pipe
);
341 struct svga_blend_state
*bs
=
342 (struct svga_blend_state
*) blend
;
344 if (bs
->id
!= SVGA3D_INVALID_ID
) {
347 ret
= SVGA3D_vgpu10_DestroyBlendState(svga
->swc
, bs
->id
);
348 if (ret
!= PIPE_OK
) {
349 svga_context_flush(svga
, NULL
);
350 ret
= SVGA3D_vgpu10_DestroyBlendState(svga
->swc
, bs
->id
);
351 assert(ret
== PIPE_OK
);
354 if (bs
->id
== svga
->state
.hw_draw
.blend_id
)
355 svga
->state
.hw_draw
.blend_id
= SVGA3D_INVALID_ID
;
357 util_bitmask_clear(svga
->blend_object_id_bm
, bs
->id
);
358 bs
->id
= SVGA3D_INVALID_ID
;
364 static void svga_set_blend_color( struct pipe_context
*pipe
,
365 const struct pipe_blend_color
*blend_color
)
367 struct svga_context
*svga
= svga_context(pipe
);
369 svga
->curr
.blend_color
= *blend_color
;
371 svga
->dirty
|= SVGA_NEW_BLEND_COLOR
;
375 void svga_init_blend_functions( struct svga_context
*svga
)
377 svga
->pipe
.create_blend_state
= svga_create_blend_state
;
378 svga
->pipe
.bind_blend_state
= svga_bind_blend_state
;
379 svga
->pipe
.delete_blend_state
= svga_delete_blend_state
;
381 svga
->pipe
.set_blend_color
= svga_set_blend_color
;