1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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 TUNGSTEN GRAPHICS 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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 * Zack Rusin
33 */
34
35
36 #include <assert.h>
37
38 #include "st_context.h"
39 #include "st_atom.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "cso_cache/cso_context.h"
43
44
45 /**
46 * Convert an OpenGL compare mode to a pipe tokens.
47 */
48 GLuint
st_compare_func_to_pipe(GLenum func)49 st_compare_func_to_pipe(GLenum func)
50 {
51 /* Same values, just biased */
52 STATIC_ASSERT(PIPE_FUNC_NEVER == GL_NEVER - GL_NEVER);
53 STATIC_ASSERT(PIPE_FUNC_LESS == GL_LESS - GL_NEVER);
54 STATIC_ASSERT(PIPE_FUNC_EQUAL == GL_EQUAL - GL_NEVER);
55 STATIC_ASSERT(PIPE_FUNC_LEQUAL == GL_LEQUAL - GL_NEVER);
56 STATIC_ASSERT(PIPE_FUNC_GREATER == GL_GREATER - GL_NEVER);
57 STATIC_ASSERT(PIPE_FUNC_NOTEQUAL == GL_NOTEQUAL - GL_NEVER);
58 STATIC_ASSERT(PIPE_FUNC_GEQUAL == GL_GEQUAL - GL_NEVER);
59 STATIC_ASSERT(PIPE_FUNC_ALWAYS == GL_ALWAYS - GL_NEVER);
60 assert(func >= GL_NEVER);
61 assert(func <= GL_ALWAYS);
62 return func - GL_NEVER;
63 }
64
65
66 /**
67 * Convert GLenum stencil op tokens to pipe tokens.
68 */
69 static GLuint
gl_stencil_op_to_pipe(GLenum func)70 gl_stencil_op_to_pipe(GLenum func)
71 {
72 switch (func) {
73 case GL_KEEP:
74 return PIPE_STENCIL_OP_KEEP;
75 case GL_ZERO:
76 return PIPE_STENCIL_OP_ZERO;
77 case GL_REPLACE:
78 return PIPE_STENCIL_OP_REPLACE;
79 case GL_INCR:
80 return PIPE_STENCIL_OP_INCR;
81 case GL_DECR:
82 return PIPE_STENCIL_OP_DECR;
83 case GL_INCR_WRAP:
84 return PIPE_STENCIL_OP_INCR_WRAP;
85 case GL_DECR_WRAP:
86 return PIPE_STENCIL_OP_DECR_WRAP;
87 case GL_INVERT:
88 return PIPE_STENCIL_OP_INVERT;
89 default:
90 assert("invalid GL token in gl_stencil_op_to_pipe()" == NULL);
91 return 0;
92 }
93 }
94
95 static void
update_depth_stencil_alpha(struct st_context * st)96 update_depth_stencil_alpha(struct st_context *st)
97 {
98 struct pipe_depth_stencil_alpha_state *dsa = &st->state.depth_stencil;
99 struct pipe_stencil_ref sr;
100 struct gl_context *ctx = st->ctx;
101
102 memset(dsa, 0, sizeof(*dsa));
103 memset(&sr, 0, sizeof(sr));
104
105 if (ctx->Depth.Test && ctx->DrawBuffer->Visual.depthBits > 0) {
106 dsa->depth.enabled = 1;
107 dsa->depth.writemask = ctx->Depth.Mask;
108 dsa->depth.func = st_compare_func_to_pipe(ctx->Depth.Func);
109 }
110
111 if (ctx->Stencil.Enabled && ctx->DrawBuffer->Visual.stencilBits > 0) {
112 dsa->stencil[0].enabled = 1;
113 dsa->stencil[0].func = st_compare_func_to_pipe(ctx->Stencil.Function[0]);
114 dsa->stencil[0].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[0]);
115 dsa->stencil[0].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[0]);
116 dsa->stencil[0].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[0]);
117 dsa->stencil[0].valuemask = ctx->Stencil.ValueMask[0] & 0xff;
118 dsa->stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
119 sr.ref_value[0] = ctx->Stencil.Ref[0] & 0xff;
120
121 if (ctx->Stencil._TestTwoSide) {
122 const GLuint back = ctx->Stencil._BackFace;
123 dsa->stencil[1].enabled = 1;
124 dsa->stencil[1].func = st_compare_func_to_pipe(ctx->Stencil.Function[back]);
125 dsa->stencil[1].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[back]);
126 dsa->stencil[1].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[back]);
127 dsa->stencil[1].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[back]);
128 dsa->stencil[1].valuemask = ctx->Stencil.ValueMask[back] & 0xff;
129 dsa->stencil[1].writemask = ctx->Stencil.WriteMask[back] & 0xff;
130 sr.ref_value[1] = ctx->Stencil.Ref[back] & 0xff;
131 }
132 else {
133 /* This should be unnecessary. Drivers must not expect this to
134 * contain valid data, except the enabled bit
135 */
136 dsa->stencil[1] = dsa->stencil[0];
137 dsa->stencil[1].enabled = 0;
138 sr.ref_value[1] = sr.ref_value[0];
139 }
140 }
141
142 if (ctx->Color.AlphaEnabled) {
143 dsa->alpha.enabled = 1;
144 dsa->alpha.func = st_compare_func_to_pipe(ctx->Color.AlphaFunc);
145 dsa->alpha.ref_value = ctx->Color.AlphaRefUnclamped;
146 }
147
148 cso_set_depth_stencil_alpha(st->cso_context, dsa);
149 cso_set_stencil_ref(st->cso_context, &sr);
150 }
151
152
153 const struct st_tracked_state st_update_depth_stencil_alpha = {
154 "st_update_depth_stencil", /* name */
155 { /* dirty */
156 (_NEW_DEPTH|_NEW_STENCIL|_NEW_COLOR), /* mesa */
157 0, /* st */
158 },
159 update_depth_stencil_alpha /* update */
160 };
161