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"
31 #include "svga_context.h"
32 #include "svga_hw_reg.h"
35 static INLINE
unsigned
36 svga_translate_compare_func(unsigned func
)
39 case PIPE_FUNC_NEVER
: return SVGA3D_CMP_NEVER
;
40 case PIPE_FUNC_LESS
: return SVGA3D_CMP_LESS
;
41 case PIPE_FUNC_LEQUAL
: return SVGA3D_CMP_LESSEQUAL
;
42 case PIPE_FUNC_GREATER
: return SVGA3D_CMP_GREATER
;
43 case PIPE_FUNC_GEQUAL
: return SVGA3D_CMP_GREATEREQUAL
;
44 case PIPE_FUNC_NOTEQUAL
: return SVGA3D_CMP_NOTEQUAL
;
45 case PIPE_FUNC_EQUAL
: return SVGA3D_CMP_EQUAL
;
46 case PIPE_FUNC_ALWAYS
: return SVGA3D_CMP_ALWAYS
;
49 return SVGA3D_CMP_ALWAYS
;
53 static INLINE
unsigned
54 svga_translate_stencil_op(unsigned op
)
57 case PIPE_STENCIL_OP_KEEP
: return SVGA3D_STENCILOP_KEEP
;
58 case PIPE_STENCIL_OP_ZERO
: return SVGA3D_STENCILOP_ZERO
;
59 case PIPE_STENCIL_OP_REPLACE
: return SVGA3D_STENCILOP_REPLACE
;
60 case PIPE_STENCIL_OP_INCR
: return SVGA3D_STENCILOP_INCR
;
61 case PIPE_STENCIL_OP_DECR
: return SVGA3D_STENCILOP_DECR
;
62 case PIPE_STENCIL_OP_INCR_WRAP
: return SVGA3D_STENCILOP_INCRSAT
; /* incorrect? */
63 case PIPE_STENCIL_OP_DECR_WRAP
: return SVGA3D_STENCILOP_DECRSAT
; /* incorrect? */
64 case PIPE_STENCIL_OP_INVERT
: return SVGA3D_STENCILOP_INVERT
;
67 return SVGA3D_STENCILOP_KEEP
;
73 svga_create_depth_stencil_state(struct pipe_context
*pipe
,
74 const struct pipe_depth_stencil_alpha_state
*templ
)
76 struct svga_depth_stencil_state
*ds
= CALLOC_STRUCT( svga_depth_stencil_state
);
78 /* Don't try to figure out CW/CCW correspondence with
79 * stencil[0]/[1] at this point. Presumably this can change as
80 * back/front face are modified.
82 ds
->stencil
[0].enabled
= templ
->stencil
[0].enabled
;
83 if (ds
->stencil
[0].enabled
) {
84 ds
->stencil
[0].func
= svga_translate_compare_func(templ
->stencil
[0].func
);
85 ds
->stencil
[0].fail
= svga_translate_stencil_op(templ
->stencil
[0].fail_op
);
86 ds
->stencil
[0].zfail
= svga_translate_stencil_op(templ
->stencil
[0].zfail_op
);
87 ds
->stencil
[0].pass
= svga_translate_stencil_op(templ
->stencil
[0].zpass_op
);
89 /* SVGA3D has one ref/mask/writemask triple shared between front &
90 * back face stencil. We really need two:
92 ds
->stencil_mask
= templ
->stencil
[0].valuemask
& 0xff;
93 ds
->stencil_writemask
= templ
->stencil
[0].writemask
& 0xff;
97 ds
->stencil
[1].enabled
= templ
->stencil
[1].enabled
;
98 if (templ
->stencil
[1].enabled
) {
99 ds
->stencil
[1].func
= svga_translate_compare_func(templ
->stencil
[1].func
);
100 ds
->stencil
[1].fail
= svga_translate_stencil_op(templ
->stencil
[1].fail_op
);
101 ds
->stencil
[1].zfail
= svga_translate_stencil_op(templ
->stencil
[1].zfail_op
);
102 ds
->stencil
[1].pass
= svga_translate_stencil_op(templ
->stencil
[1].zpass_op
);
104 ds
->stencil_mask
= templ
->stencil
[1].valuemask
& 0xff;
105 ds
->stencil_writemask
= templ
->stencil
[1].writemask
& 0xff;
109 ds
->zenable
= templ
->depth
.enabled
;
111 ds
->zfunc
= svga_translate_compare_func(templ
->depth
.func
);
112 ds
->zwriteenable
= templ
->depth
.writemask
;
115 ds
->alphatestenable
= templ
->alpha
.enabled
;
116 if (ds
->alphatestenable
) {
117 ds
->alphafunc
= svga_translate_compare_func(templ
->alpha
.func
);
118 ds
->alpharef
= templ
->alpha
.ref_value
;
124 static void svga_bind_depth_stencil_state(struct pipe_context
*pipe
,
127 struct svga_context
*svga
= svga_context(pipe
);
129 svga
->curr
.depth
= (const struct svga_depth_stencil_state
*)depth_stencil
;
130 svga
->dirty
|= SVGA_NEW_DEPTH_STENCIL
;
133 static void svga_delete_depth_stencil_state(struct pipe_context
*pipe
,
140 static void svga_set_stencil_ref( struct pipe_context
*pipe
,
141 const struct pipe_stencil_ref
*stencil_ref
)
143 struct svga_context
*svga
= svga_context(pipe
);
145 svga
->curr
.stencil_ref
= *stencil_ref
;
147 svga
->dirty
|= SVGA_NEW_STENCIL_REF
;
151 svga_set_sample_mask(struct pipe_context
*pipe
,
152 unsigned sample_mask
)
157 void svga_init_depth_stencil_functions( struct svga_context
*svga
)
159 svga
->pipe
.create_depth_stencil_alpha_state
= svga_create_depth_stencil_state
;
160 svga
->pipe
.bind_depth_stencil_alpha_state
= svga_bind_depth_stencil_state
;
161 svga
->pipe
.delete_depth_stencil_alpha_state
= svga_delete_depth_stencil_state
;
163 svga
->pipe
.set_stencil_ref
= svga_set_stencil_ref
;
164 svga
->pipe
.set_sample_mask
= svga_set_sample_mask
;