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"
30 #include "svga_context.h"
31 #include "svga_state.h"
37 SVGA3dRenderState rs
[SVGA3D_RS_MAX
];
41 #define EMIT_RS(svga, value, token, fail) \
43 if (svga->state.hw_draw.rs[SVGA3D_RS_##token] != value) { \
44 svga_queue_rs( &queue, SVGA3D_RS_##token, value ); \
45 svga->state.hw_draw.rs[SVGA3D_RS_##token] = value; \
49 #define EMIT_RS_FLOAT(svga, fvalue, token, fail) \
51 unsigned value = fui(fvalue); \
52 if (svga->state.hw_draw.rs[SVGA3D_RS_##token] != value) { \
53 svga_queue_rs( &queue, SVGA3D_RS_##token, value ); \
54 svga->state.hw_draw.rs[SVGA3D_RS_##token] = value; \
60 svga_queue_rs( struct rs_queue
*q
,
64 q
->rs
[q
->rs_count
].state
= rss
;
65 q
->rs
[q
->rs_count
].uintValue
= value
;
70 /* Compare old and new render states and emit differences between them
71 * to hardware. Simplest implementation would be to emit the whole of
74 static int emit_rss( struct svga_context
*svga
,
77 struct rs_queue queue
;
81 if (dirty
& SVGA_NEW_BLEND
) {
82 const struct svga_blend_state
*curr
= svga
->curr
.blend
;
84 EMIT_RS( svga
, curr
->rt
[0].writemask
, COLORWRITEENABLE
, fail
);
85 EMIT_RS( svga
, curr
->rt
[0].blend_enable
, BLENDENABLE
, fail
);
87 if (curr
->rt
[0].blend_enable
) {
88 EMIT_RS( svga
, curr
->rt
[0].srcblend
, SRCBLEND
, fail
);
89 EMIT_RS( svga
, curr
->rt
[0].dstblend
, DSTBLEND
, fail
);
90 EMIT_RS( svga
, curr
->rt
[0].blendeq
, BLENDEQUATION
, fail
);
92 EMIT_RS( svga
, curr
->rt
[0].separate_alpha_blend_enable
,
93 SEPARATEALPHABLENDENABLE
, fail
);
95 if (curr
->rt
[0].separate_alpha_blend_enable
) {
96 EMIT_RS( svga
, curr
->rt
[0].srcblend_alpha
, SRCBLENDALPHA
, fail
);
97 EMIT_RS( svga
, curr
->rt
[0].dstblend_alpha
, DSTBLENDALPHA
, fail
);
98 EMIT_RS( svga
, curr
->rt
[0].blendeq_alpha
, BLENDEQUATIONALPHA
, fail
);
104 if (dirty
& (SVGA_NEW_DEPTH_STENCIL
| SVGA_NEW_RAST
)) {
105 const struct svga_depth_stencil_state
*curr
= svga
->curr
.depth
;
106 const struct svga_rasterizer_state
*rast
= svga
->curr
.rast
;
108 if (!curr
->stencil
[0].enabled
)
112 EMIT_RS( svga
, FALSE
, STENCILENABLE
, fail
);
113 EMIT_RS( svga
, FALSE
, STENCILENABLE2SIDED
, fail
);
115 else if (curr
->stencil
[0].enabled
&& !curr
->stencil
[1].enabled
)
119 EMIT_RS( svga
, TRUE
, STENCILENABLE
, fail
);
120 EMIT_RS( svga
, FALSE
, STENCILENABLE2SIDED
, fail
);
122 EMIT_RS( svga
, curr
->stencil
[0].func
, STENCILFUNC
, fail
);
123 EMIT_RS( svga
, curr
->stencil
[0].fail
, STENCILFAIL
, fail
);
124 EMIT_RS( svga
, curr
->stencil
[0].zfail
, STENCILZFAIL
, fail
);
125 EMIT_RS( svga
, curr
->stencil
[0].pass
, STENCILPASS
, fail
);
127 EMIT_RS( svga
, curr
->stencil_ref
, STENCILREF
, fail
);
128 EMIT_RS( svga
, curr
->stencil_mask
, STENCILMASK
, fail
);
129 EMIT_RS( svga
, curr
->stencil_writemask
, STENCILWRITEMASK
, fail
);
135 /* Hardware frontwinding is always CW, so if ours is also CW,
136 * then our definition of front face agrees with hardware.
137 * Otherwise need to flip.
139 if (rast
->templ
.front_winding
== PIPE_WINDING_CW
) {
150 EMIT_RS( svga
, TRUE
, STENCILENABLE
, fail
);
151 EMIT_RS( svga
, TRUE
, STENCILENABLE2SIDED
, fail
);
153 EMIT_RS( svga
, curr
->stencil
[cw
].func
, STENCILFUNC
, fail
);
154 EMIT_RS( svga
, curr
->stencil
[cw
].fail
, STENCILFAIL
, fail
);
155 EMIT_RS( svga
, curr
->stencil
[cw
].zfail
, STENCILZFAIL
, fail
);
156 EMIT_RS( svga
, curr
->stencil
[cw
].pass
, STENCILPASS
, fail
);
158 EMIT_RS( svga
, curr
->stencil
[ccw
].func
, CCWSTENCILFUNC
, fail
);
159 EMIT_RS( svga
, curr
->stencil
[ccw
].fail
, CCWSTENCILFAIL
, fail
);
160 EMIT_RS( svga
, curr
->stencil
[ccw
].zfail
, CCWSTENCILZFAIL
, fail
);
161 EMIT_RS( svga
, curr
->stencil
[ccw
].pass
, CCWSTENCILPASS
, fail
);
163 EMIT_RS( svga
, curr
->stencil_ref
, STENCILREF
, fail
);
164 EMIT_RS( svga
, curr
->stencil_mask
, STENCILMASK
, fail
);
165 EMIT_RS( svga
, curr
->stencil_writemask
, STENCILWRITEMASK
, fail
);
168 EMIT_RS( svga
, curr
->zenable
, ZENABLE
, fail
);
170 EMIT_RS( svga
, curr
->zfunc
, ZFUNC
, fail
);
171 EMIT_RS( svga
, curr
->zwriteenable
, ZWRITEENABLE
, fail
);
174 EMIT_RS( svga
, curr
->alphatestenable
, ALPHATESTENABLE
, fail
);
175 if (curr
->alphatestenable
) {
176 EMIT_RS( svga
, curr
->alphafunc
, ALPHAFUNC
, fail
);
177 EMIT_RS_FLOAT( svga
, curr
->alpharef
, ALPHAREF
, fail
);
182 if (dirty
& SVGA_NEW_RAST
)
184 const struct svga_rasterizer_state
*curr
= svga
->curr
.rast
;
186 /* Shademode: still need to rearrange index list to move
187 * flat-shading PV first vertex.
189 EMIT_RS( svga
, curr
->shademode
, SHADEMODE
, fail
);
190 EMIT_RS( svga
, curr
->cullmode
, CULLMODE
, fail
);
191 EMIT_RS( svga
, curr
->scissortestenable
, SCISSORTESTENABLE
, fail
);
192 EMIT_RS( svga
, curr
->multisampleantialias
, MULTISAMPLEANTIALIAS
, fail
);
193 EMIT_RS( svga
, curr
->lastpixel
, LASTPIXEL
, fail
);
194 EMIT_RS( svga
, curr
->linepattern
, LINEPATTERN
, fail
);
195 EMIT_RS_FLOAT( svga
, curr
->pointsize
, POINTSIZE
, fail
);
196 /* XXX still need to set this? */
197 EMIT_RS_FLOAT( svga
, 0.0, POINTSIZEMIN
, fail
);
198 EMIT_RS_FLOAT( svga
, SVGA_MAX_POINTSIZE
, POINTSIZEMAX
, fail
);
201 if (dirty
& (SVGA_NEW_RAST
| SVGA_NEW_FRAME_BUFFER
| SVGA_NEW_NEED_PIPELINE
))
203 const struct svga_rasterizer_state
*curr
= svga
->curr
.rast
;
207 /* Need to modify depth bias according to bound depthbuffer
208 * format. Don't do hardware depthbias while the software
209 * pipeline is active.
211 if (!svga
->state
.sw
.need_pipeline
&&
212 svga
->curr
.framebuffer
.zsbuf
)
214 slope
= curr
->slopescaledepthbias
;
215 bias
= svga
->curr
.depthscale
* curr
->depthbias
;
218 EMIT_RS_FLOAT( svga
, slope
, SLOPESCALEDEPTHBIAS
, fail
);
219 EMIT_RS_FLOAT( svga
, bias
, DEPTHBIAS
, fail
);
223 if (queue
.rs_count
) {
224 SVGA3dRenderState
*rs
;
226 if (SVGA3D_BeginSetRenderState( svga
->swc
,
228 queue
.rs_count
) != PIPE_OK
)
233 queue
.rs_count
* sizeof queue
.rs
[0]);
235 SVGA_FIFOCommitAll( svga
->swc
);
244 /* XXX: need to poison cached hardware state on failure to ensure
245 * dirty state gets re-emitted. Fix this by re-instating partial
246 * FIFOCommit command and only updating cached hw state once the
247 * initial allocation has succeeded.
249 memset(svga
->state
.hw_draw
.rs
, 0xcd, sizeof(svga
->state
.hw_draw
.rs
));
251 return PIPE_ERROR_OUT_OF_MEMORY
;
255 struct svga_tracked_state svga_hw_rss
=
260 SVGA_NEW_DEPTH_STENCIL
|
262 SVGA_NEW_FRAME_BUFFER
|
263 SVGA_NEW_NEED_PIPELINE
),