Added st_update_framebuffer struct/object.
[mesa.git] / src / mesa / state_tracker / st_atom_stencil.c
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 */
33
34
35 #include "st_context.h"
36 #include "st_atom.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39
40
41 /**
42 * Convert GLenum stencil func tokens to pipe tokens.
43 */
44 static GLuint
45 gl_stencil_func_to_sp(GLenum func)
46 {
47 /* Same values, just biased */
48 assert(PIPE_STENCIL_FUNC_NEVER == GL_NEVER - GL_NEVER);
49 assert(PIPE_STENCIL_FUNC_LESS == GL_LESS - GL_NEVER);
50 assert(PIPE_STENCIL_FUNC_EQUAL == GL_EQUAL - GL_NEVER);
51 assert(PIPE_STENCIL_FUNC_LEQUAL == GL_LEQUAL - GL_NEVER);
52 assert(PIPE_STENCIL_FUNC_GREATER == GL_GREATER - GL_NEVER);
53 assert(PIPE_STENCIL_FUNC_NOTEQUAL == GL_NOTEQUAL - GL_NEVER);
54 assert(PIPE_STENCIL_FUNC_GEQUAL == GL_GEQUAL - GL_NEVER);
55 assert(PIPE_STENCIL_FUNC_ALWAYS == GL_ALWAYS - GL_NEVER);
56 assert(func >= GL_NEVER);
57 assert(func <= GL_ALWAYS);
58 return func - GL_NEVER;
59 }
60
61
62 /**
63 * Convert GLenum stencil op tokens to pipe tokens.
64 */
65 static GLuint
66 gl_stencil_op_to_sp(GLenum func)
67 {
68 switch (func) {
69 case GL_KEEP:
70 return PIPE_STENCIL_OP_KEEP;
71 case GL_ZERO:
72 return PIPE_STENCIL_OP_ZERO;
73 case GL_REPLACE:
74 return PIPE_STENCIL_OP_REPLACE;
75 case GL_INCR:
76 return PIPE_STENCIL_OP_INCR;
77 case GL_DECR:
78 return PIPE_STENCIL_OP_DECR;
79 case GL_INCR_WRAP:
80 return PIPE_STENCIL_OP_INCR_WRAP;
81 case GL_DECR_WRAP:
82 return PIPE_STENCIL_OP_DECR_WRAP;
83 case GL_INVERT:
84 return PIPE_STENCIL_OP_INVERT;
85 default:
86 assert("invalid GL token in gl_stencil_op_to_sp()" == NULL);
87 return 0;
88 }
89 }
90
91
92 static void
93 update_stencil( struct st_context *st )
94 {
95 struct pipe_stencil_state stencil;
96
97 memset(&stencil, 0, sizeof(stencil));
98
99 if (st->ctx->Stencil.Enabled) {
100 stencil.front_enabled = 1;
101 stencil.front_func = gl_stencil_func_to_sp(st->ctx->Stencil.Function[0]);
102 stencil.front_fail_op = gl_stencil_op_to_sp(st->ctx->Stencil.FailFunc[0]);
103 stencil.front_zfail_op = gl_stencil_op_to_sp(st->ctx->Stencil.ZFailFunc[0]);
104 stencil.front_zpass_op = gl_stencil_op_to_sp(st->ctx->Stencil.ZPassFunc[0]);
105 stencil.ref_value[0] = st->ctx->Stencil.Ref[0] & 0xff;
106 stencil.value_mask[0] = st->ctx->Stencil.ValueMask[0] & 0xff;
107 stencil.write_mask[0] = st->ctx->Stencil.WriteMask[0] & 0xff;
108 if (st->ctx->Stencil.TestTwoSide) {
109 stencil.back_enabled = 1;
110 stencil.back_func = gl_stencil_func_to_sp(st->ctx->Stencil.Function[1]);
111 stencil.back_fail_op = gl_stencil_op_to_sp(st->ctx->Stencil.FailFunc[1]);
112 stencil.back_zfail_op = gl_stencil_op_to_sp(st->ctx->Stencil.ZFailFunc[1]);
113 stencil.back_zpass_op = gl_stencil_op_to_sp(st->ctx->Stencil.ZPassFunc[1]);
114 stencil.ref_value[1] = st->ctx->Stencil.Ref[1] & 0xff;
115 stencil.value_mask[1] = st->ctx->Stencil.ValueMask[1] & 0xff;
116 stencil.write_mask[1] = st->ctx->Stencil.WriteMask[1] & 0xff;
117 }
118 stencil.clear_value = st->ctx->Stencil.Clear & 0xff;
119 }
120
121 if (memcmp(&stencil, &st->state.stencil, sizeof(stencil)) != 0) {
122 /* state has changed */
123 st->state.stencil = stencil; /* struct copy */
124 st->pipe->set_stencil_state(st->pipe, &stencil); /* set new state */
125 }
126 }
127
128
129 const struct st_tracked_state st_update_stencil = {
130 .dirty = {
131 .mesa = (_NEW_STENCIL),
132 .st = 0,
133 },
134 .update = update_stencil
135 };
136
137
138
139
140