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 
31 #include "svga_context.h"
32 #include "svga_hw_reg.h"
33 
34 
35 static INLINE unsigned
svga_translate_compare_func(unsigned func)36 svga_translate_compare_func(unsigned func)
37 {
38    switch (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;
47    default:
48       assert(0);
49       return SVGA3D_CMP_ALWAYS;
50    }
51 }
52 
53 static INLINE unsigned
svga_translate_stencil_op(unsigned op)54 svga_translate_stencil_op(unsigned op)
55 {
56    switch (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_INCRSAT;
61    case PIPE_STENCIL_OP_DECR:      return SVGA3D_STENCILOP_DECRSAT;
62    case PIPE_STENCIL_OP_INCR_WRAP: return SVGA3D_STENCILOP_INCR;
63    case PIPE_STENCIL_OP_DECR_WRAP: return SVGA3D_STENCILOP_DECR;
64    case PIPE_STENCIL_OP_INVERT:    return SVGA3D_STENCILOP_INVERT;
65    default:
66       assert(0);
67       return SVGA3D_STENCILOP_KEEP;
68    }
69 }
70 
71 
72 static void *
svga_create_depth_stencil_state(struct pipe_context * pipe,const struct pipe_depth_stencil_alpha_state * templ)73 svga_create_depth_stencil_state(struct pipe_context *pipe,
74 				const struct pipe_depth_stencil_alpha_state *templ)
75 {
76    struct svga_depth_stencil_state *ds = CALLOC_STRUCT( svga_depth_stencil_state );
77 
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.
81     */
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);
88 
89       /* SVGA3D has one ref/mask/writemask triple shared between front &
90        * back face stencil.  We really need two:
91        */
92       ds->stencil_mask      = templ->stencil[0].valuemask & 0xff;
93       ds->stencil_writemask = templ->stencil[0].writemask & 0xff;
94    }
95 
96 
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);
103 
104       ds->stencil_mask      = templ->stencil[1].valuemask & 0xff;
105       ds->stencil_writemask = templ->stencil[1].writemask & 0xff;
106    }
107 
108 
109    ds->zenable = templ->depth.enabled;
110    if (ds->zenable) {
111       ds->zfunc = svga_translate_compare_func(templ->depth.func);
112       ds->zwriteenable = templ->depth.writemask;
113    }
114 
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;
119    }
120 
121    return ds;
122 }
123 
svga_bind_depth_stencil_state(struct pipe_context * pipe,void * depth_stencil)124 static void svga_bind_depth_stencil_state(struct pipe_context *pipe,
125                                           void *depth_stencil)
126 {
127    struct svga_context *svga = svga_context(pipe);
128 
129    svga->curr.depth = (const struct svga_depth_stencil_state *)depth_stencil;
130    svga->dirty |= SVGA_NEW_DEPTH_STENCIL;
131 }
132 
svga_delete_depth_stencil_state(struct pipe_context * pipe,void * depth_stencil)133 static void svga_delete_depth_stencil_state(struct pipe_context *pipe,
134                                             void *depth_stencil)
135 {
136    FREE(depth_stencil);
137 }
138 
139 
svga_set_stencil_ref(struct pipe_context * pipe,const struct pipe_stencil_ref * stencil_ref)140 static void svga_set_stencil_ref( struct pipe_context *pipe,
141                                   const struct pipe_stencil_ref *stencil_ref )
142 {
143    struct svga_context *svga = svga_context(pipe);
144 
145    svga->curr.stencil_ref = *stencil_ref;
146 
147    svga->dirty |= SVGA_NEW_STENCIL_REF;
148 }
149 
150 static void
svga_set_sample_mask(struct pipe_context * pipe,unsigned sample_mask)151 svga_set_sample_mask(struct pipe_context *pipe,
152                      unsigned sample_mask)
153 {
154 }
155 
156 
svga_init_depth_stencil_functions(struct svga_context * svga)157 void svga_init_depth_stencil_functions( struct svga_context *svga )
158 {
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;
162 
163    svga->pipe.set_stencil_ref = svga_set_stencil_ref;
164    svga->pipe.set_sample_mask = svga_set_sample_mask;
165 }
166 
167 
168 
169 
170