1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Keith Whitwell <keithw@vmware.com>
38 #include "st_context.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "cso_cache/cso_context.h"
44 #include "main/core.h"
45 #include "main/stencil.h"
49 * Convert an OpenGL compare mode to a pipe tokens.
52 st_compare_func_to_pipe(GLenum func
)
54 /* Same values, just biased */
55 STATIC_ASSERT(PIPE_FUNC_NEVER
== GL_NEVER
- GL_NEVER
);
56 STATIC_ASSERT(PIPE_FUNC_LESS
== GL_LESS
- GL_NEVER
);
57 STATIC_ASSERT(PIPE_FUNC_EQUAL
== GL_EQUAL
- GL_NEVER
);
58 STATIC_ASSERT(PIPE_FUNC_LEQUAL
== GL_LEQUAL
- GL_NEVER
);
59 STATIC_ASSERT(PIPE_FUNC_GREATER
== GL_GREATER
- GL_NEVER
);
60 STATIC_ASSERT(PIPE_FUNC_NOTEQUAL
== GL_NOTEQUAL
- GL_NEVER
);
61 STATIC_ASSERT(PIPE_FUNC_GEQUAL
== GL_GEQUAL
- GL_NEVER
);
62 STATIC_ASSERT(PIPE_FUNC_ALWAYS
== GL_ALWAYS
- GL_NEVER
);
63 assert(func
>= GL_NEVER
);
64 assert(func
<= GL_ALWAYS
);
65 return func
- GL_NEVER
;
70 * Convert GLenum stencil op tokens to pipe tokens.
73 gl_stencil_op_to_pipe(GLenum func
)
77 return PIPE_STENCIL_OP_KEEP
;
79 return PIPE_STENCIL_OP_ZERO
;
81 return PIPE_STENCIL_OP_REPLACE
;
83 return PIPE_STENCIL_OP_INCR
;
85 return PIPE_STENCIL_OP_DECR
;
87 return PIPE_STENCIL_OP_INCR_WRAP
;
89 return PIPE_STENCIL_OP_DECR_WRAP
;
91 return PIPE_STENCIL_OP_INVERT
;
93 assert("invalid GL token in gl_stencil_op_to_pipe()" == NULL
);
99 update_depth_stencil_alpha(struct st_context
*st
)
101 struct pipe_depth_stencil_alpha_state
*dsa
= &st
->state
.depth_stencil
;
102 struct pipe_stencil_ref sr
;
103 struct gl_context
*ctx
= st
->ctx
;
105 memset(dsa
, 0, sizeof(*dsa
));
106 memset(&sr
, 0, sizeof(sr
));
108 if (ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
109 if (ctx
->Depth
.Test
) {
110 dsa
->depth
.enabled
= 1;
111 dsa
->depth
.writemask
= ctx
->Depth
.Mask
;
112 dsa
->depth
.func
= st_compare_func_to_pipe(ctx
->Depth
.Func
);
114 if (ctx
->Depth
.BoundsTest
) {
115 dsa
->depth
.bounds_test
= 1;
116 dsa
->depth
.bounds_min
= ctx
->Depth
.BoundsMin
;
117 dsa
->depth
.bounds_max
= ctx
->Depth
.BoundsMax
;
121 if (ctx
->Stencil
.Enabled
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0) {
122 dsa
->stencil
[0].enabled
= 1;
123 dsa
->stencil
[0].func
= st_compare_func_to_pipe(ctx
->Stencil
.Function
[0]);
124 dsa
->stencil
[0].fail_op
= gl_stencil_op_to_pipe(ctx
->Stencil
.FailFunc
[0]);
125 dsa
->stencil
[0].zfail_op
= gl_stencil_op_to_pipe(ctx
->Stencil
.ZFailFunc
[0]);
126 dsa
->stencil
[0].zpass_op
= gl_stencil_op_to_pipe(ctx
->Stencil
.ZPassFunc
[0]);
127 dsa
->stencil
[0].valuemask
= ctx
->Stencil
.ValueMask
[0] & 0xff;
128 dsa
->stencil
[0].writemask
= ctx
->Stencil
.WriteMask
[0] & 0xff;
129 sr
.ref_value
[0] = _mesa_get_stencil_ref(ctx
, 0);
131 if (ctx
->Stencil
._TestTwoSide
) {
132 const GLuint back
= ctx
->Stencil
._BackFace
;
133 dsa
->stencil
[1].enabled
= 1;
134 dsa
->stencil
[1].func
= st_compare_func_to_pipe(ctx
->Stencil
.Function
[back
]);
135 dsa
->stencil
[1].fail_op
= gl_stencil_op_to_pipe(ctx
->Stencil
.FailFunc
[back
]);
136 dsa
->stencil
[1].zfail_op
= gl_stencil_op_to_pipe(ctx
->Stencil
.ZFailFunc
[back
]);
137 dsa
->stencil
[1].zpass_op
= gl_stencil_op_to_pipe(ctx
->Stencil
.ZPassFunc
[back
]);
138 dsa
->stencil
[1].valuemask
= ctx
->Stencil
.ValueMask
[back
] & 0xff;
139 dsa
->stencil
[1].writemask
= ctx
->Stencil
.WriteMask
[back
] & 0xff;
140 sr
.ref_value
[1] = _mesa_get_stencil_ref(ctx
, back
);
143 /* This should be unnecessary. Drivers must not expect this to
144 * contain valid data, except the enabled bit
146 dsa
->stencil
[1] = dsa
->stencil
[0];
147 dsa
->stencil
[1].enabled
= 0;
148 sr
.ref_value
[1] = sr
.ref_value
[0];
152 if (ctx
->Color
.AlphaEnabled
) {
153 dsa
->alpha
.enabled
= 1;
154 dsa
->alpha
.func
= st_compare_func_to_pipe(ctx
->Color
.AlphaFunc
);
155 dsa
->alpha
.ref_value
= ctx
->Color
.AlphaRefUnclamped
;
158 cso_set_depth_stencil_alpha(st
->cso_context
, dsa
);
159 cso_set_stencil_ref(st
->cso_context
, &sr
);
163 const struct st_tracked_state st_update_depth_stencil_alpha
= {
164 "st_update_depth_stencil", /* name */
166 (_NEW_DEPTH
|_NEW_STENCIL
|_NEW_COLOR
|_NEW_BUFFERS
),/* mesa */
169 update_depth_stencil_alpha
/* update */