14c9b48dcc510929951516f3cd17b98654ac93a9aBrian/**************************************************************************
24c9b48dcc510929951516f3cd17b98654ac93a9aBrian *
34c9b48dcc510929951516f3cd17b98654ac93a9aBrian * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
44c9b48dcc510929951516f3cd17b98654ac93a9aBrian * All Rights Reserved.
54c9b48dcc510929951516f3cd17b98654ac93a9aBrian *
64c9b48dcc510929951516f3cd17b98654ac93a9aBrian * Permission is hereby granted, free of charge, to any person obtaining a
74c9b48dcc510929951516f3cd17b98654ac93a9aBrian * copy of this software and associated documentation files (the
84c9b48dcc510929951516f3cd17b98654ac93a9aBrian * "Software"), to deal in the Software without restriction, including
94c9b48dcc510929951516f3cd17b98654ac93a9aBrian * without limitation the rights to use, copy, modify, merge, publish,
104c9b48dcc510929951516f3cd17b98654ac93a9aBrian * distribute, sub license, and/or sell copies of the Software, and to
114c9b48dcc510929951516f3cd17b98654ac93a9aBrian * permit persons to whom the Software is furnished to do so, subject to
124c9b48dcc510929951516f3cd17b98654ac93a9aBrian * the following conditions:
134c9b48dcc510929951516f3cd17b98654ac93a9aBrian *
144c9b48dcc510929951516f3cd17b98654ac93a9aBrian * The above copyright notice and this permission notice (including the
154c9b48dcc510929951516f3cd17b98654ac93a9aBrian * next paragraph) shall be included in all copies or substantial portions
164c9b48dcc510929951516f3cd17b98654ac93a9aBrian * of the Software.
174c9b48dcc510929951516f3cd17b98654ac93a9aBrian *
184c9b48dcc510929951516f3cd17b98654ac93a9aBrian * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
194c9b48dcc510929951516f3cd17b98654ac93a9aBrian * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
204c9b48dcc510929951516f3cd17b98654ac93a9aBrian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
214c9b48dcc510929951516f3cd17b98654ac93a9aBrian * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
224c9b48dcc510929951516f3cd17b98654ac93a9aBrian * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
234c9b48dcc510929951516f3cd17b98654ac93a9aBrian * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
244c9b48dcc510929951516f3cd17b98654ac93a9aBrian * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
254c9b48dcc510929951516f3cd17b98654ac93a9aBrian *
264c9b48dcc510929951516f3cd17b98654ac93a9aBrian **************************************************************************/
274c9b48dcc510929951516f3cd17b98654ac93a9aBrian
284c9b48dcc510929951516f3cd17b98654ac93a9aBrian /*
294c9b48dcc510929951516f3cd17b98654ac93a9aBrian  * Authors:
304c9b48dcc510929951516f3cd17b98654ac93a9aBrian  *   Keith Whitwell <keith@tungstengraphics.com>
314c9b48dcc510929951516f3cd17b98654ac93a9aBrian  *   Brian Paul
32d6ac959833a8e40a27907940969c622692f749b1Zack Rusin  *   Zack  Rusin
334c9b48dcc510929951516f3cd17b98654ac93a9aBrian  */
344c9b48dcc510929951516f3cd17b98654ac93a9aBrian
354c9b48dcc510929951516f3cd17b98654ac93a9aBrian
369846b0627149e221c9fbd7c3379e33fb68e68511Vinson Lee#include <assert.h>
379846b0627149e221c9fbd7c3379e33fb68e68511Vinson Lee
384c9b48dcc510929951516f3cd17b98654ac93a9aBrian#include "st_context.h"
394c9b48dcc510929951516f3cd17b98654ac93a9aBrian#include "st_atom.h"
40943964a1e5bad86bdceb0a06d60fb3b302ebce6aKeith Whitwell#include "pipe/p_context.h"
41943964a1e5bad86bdceb0a06d60fb3b302ebce6aKeith Whitwell#include "pipe/p_defines.h"
42339e7ec6805e6de8794514c0a935081b5d36d38fBrian#include "cso_cache/cso_context.h"
434c9b48dcc510929951516f3cd17b98654ac93a9aBrian
444c9b48dcc510929951516f3cd17b98654ac93a9aBrian
454c9b48dcc510929951516f3cd17b98654ac93a9aBrian/**
46b18763141d651ae1112e80bb7ef3e813c8ce1d18Brian * Convert an OpenGL compare mode to a pipe tokens.
47d6ac959833a8e40a27907940969c622692f749b1Zack Rusin */
48b18763141d651ae1112e80bb7ef3e813c8ce1d18BrianGLuint
49b18763141d651ae1112e80bb7ef3e813c8ce1d18Brianst_compare_func_to_pipe(GLenum func)
50d6ac959833a8e40a27907940969c622692f749b1Zack Rusin{
51d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   /* Same values, just biased */
52322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_NEVER == GL_NEVER - GL_NEVER);
53322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_LESS == GL_LESS - GL_NEVER);
54322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_EQUAL == GL_EQUAL - GL_NEVER);
55322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_LEQUAL == GL_LEQUAL - GL_NEVER);
56322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_GREATER == GL_GREATER - GL_NEVER);
57322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_NOTEQUAL == GL_NOTEQUAL - GL_NEVER);
58322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_GEQUAL == GL_GEQUAL - GL_NEVER);
59322a2938f320f8a39cb6d86f249a339625a8772aBrian Paul   STATIC_ASSERT(PIPE_FUNC_ALWAYS == GL_ALWAYS - GL_NEVER);
60f612e55e45402c14b2099238e8d41e911932fd26Brian Paul   assert(func >= GL_NEVER);
61f612e55e45402c14b2099238e8d41e911932fd26Brian Paul   assert(func <= GL_ALWAYS);
62d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   return func - GL_NEVER;
63d6ac959833a8e40a27907940969c622692f749b1Zack Rusin}
64d6ac959833a8e40a27907940969c622692f749b1Zack Rusin
65d6ac959833a8e40a27907940969c622692f749b1Zack Rusin
66d6ac959833a8e40a27907940969c622692f749b1Zack Rusin/**
67d6ac959833a8e40a27907940969c622692f749b1Zack Rusin * Convert GLenum stencil op tokens to pipe tokens.
68d6ac959833a8e40a27907940969c622692f749b1Zack Rusin */
69d6ac959833a8e40a27907940969c622692f749b1Zack Rusinstatic GLuint
70b18763141d651ae1112e80bb7ef3e813c8ce1d18Briangl_stencil_op_to_pipe(GLenum func)
71d6ac959833a8e40a27907940969c622692f749b1Zack Rusin{
72d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   switch (func) {
73d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_KEEP:
74d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_KEEP;
75d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_ZERO:
76d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_ZERO;
77d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_REPLACE:
78d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_REPLACE;
79d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_INCR:
80d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_INCR;
81d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_DECR:
82d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_DECR;
83d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_INCR_WRAP:
84d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_INCR_WRAP;
85d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_DECR_WRAP:
86d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_DECR_WRAP;
87d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   case GL_INVERT:
88d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return PIPE_STENCIL_OP_INVERT;
89d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   default:
90b18763141d651ae1112e80bb7ef3e813c8ce1d18Brian      assert("invalid GL token in gl_stencil_op_to_pipe()" == NULL);
91d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      return 0;
92d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   }
93d6ac959833a8e40a27907940969c622692f749b1Zack Rusin}
94d6ac959833a8e40a27907940969c622692f749b1Zack Rusin
95d6ac959833a8e40a27907940969c622692f749b1Zack Rusinstatic void
96bfe79babf99e6b9435195178d1ea64687c60d161Keith Whitwellupdate_depth_stencil_alpha(struct st_context *st)
974c9b48dcc510929951516f3cd17b98654ac93a9aBrian{
98339e7ec6805e6de8794514c0a935081b5d36d38fBrian   struct pipe_depth_stencil_alpha_state *dsa = &st->state.depth_stencil;
991a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger   struct pipe_stencil_ref sr;
100f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg   struct gl_context *ctx = st->ctx;
1014c9b48dcc510929951516f3cd17b98654ac93a9aBrian
102339e7ec6805e6de8794514c0a935081b5d36d38fBrian   memset(dsa, 0, sizeof(*dsa));
1031a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger   memset(&sr, 0, sizeof(sr));
1044c9b48dcc510929951516f3cd17b98654ac93a9aBrian
105f1a083d4b8c86e0ba335ab162f60b6f2b8391c31Brian Paul   if (ctx->Depth.Test && ctx->DrawBuffer->Visual.depthBits > 0) {
106f1a083d4b8c86e0ba335ab162f60b6f2b8391c31Brian Paul      dsa->depth.enabled = 1;
107f1a083d4b8c86e0ba335ab162f60b6f2b8391c31Brian Paul      dsa->depth.writemask = ctx->Depth.Mask;
108f1a083d4b8c86e0ba335ab162f60b6f2b8391c31Brian Paul      dsa->depth.func = st_compare_func_to_pipe(ctx->Depth.Func);
109f1a083d4b8c86e0ba335ab162f60b6f2b8391c31Brian Paul   }
1104c9b48dcc510929951516f3cd17b98654ac93a9aBrian
111026465b9b1aebab98b6d519b75fe96d0ca9f4f51Brian Paul   if (ctx->Stencil.Enabled && ctx->DrawBuffer->Visual.stencilBits > 0) {
112339e7ec6805e6de8794514c0a935081b5d36d38fBrian      dsa->stencil[0].enabled = 1;
1132b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->stencil[0].func = st_compare_func_to_pipe(ctx->Stencil.Function[0]);
1142b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->stencil[0].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[0]);
1152b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->stencil[0].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[0]);
1162b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->stencil[0].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[0]);
1172b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->stencil[0].valuemask = ctx->Stencil.ValueMask[0] & 0xff;
1182b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
1191a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger      sr.ref_value[0] = ctx->Stencil.Ref[0] & 0xff;
1202b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul
1212b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      if (ctx->Stencil._TestTwoSide) {
1222b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         const GLuint back = ctx->Stencil._BackFace;
123339e7ec6805e6de8794514c0a935081b5d36d38fBrian         dsa->stencil[1].enabled = 1;
1242b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         dsa->stencil[1].func = st_compare_func_to_pipe(ctx->Stencil.Function[back]);
1252b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         dsa->stencil[1].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[back]);
1262b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         dsa->stencil[1].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[back]);
1272b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         dsa->stencil[1].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[back]);
1282b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         dsa->stencil[1].valuemask = ctx->Stencil.ValueMask[back] & 0xff;
1292b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul         dsa->stencil[1].writemask = ctx->Stencil.WriteMask[back] & 0xff;
1301a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger         sr.ref_value[1] = ctx->Stencil.Ref[back] & 0xff;
131d6ac959833a8e40a27907940969c622692f749b1Zack Rusin      }
132d453042bc67cc94bb215d229b60e8cd70a80ff8dBrian Paul      else {
1331a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger         /* This should be unnecessary. Drivers must not expect this to
1341a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger          * contain valid data, except the enabled bit
1351a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger          */
136d453042bc67cc94bb215d229b60e8cd70a80ff8dBrian Paul         dsa->stencil[1] = dsa->stencil[0];
137cf9b07ea3474cd33e797eeb10b3fd73ad54ae8d5Roland Scheidegger         dsa->stencil[1].enabled = 0;
1381a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger         sr.ref_value[1] = sr.ref_value[0];
139d453042bc67cc94bb215d229b60e8cd70a80ff8dBrian Paul      }
140d6ac959833a8e40a27907940969c622692f749b1Zack Rusin   }
141563479552e2f491fb94e7fac5772f3c72cee962aBrian
1422b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul   if (ctx->Color.AlphaEnabled) {
143339e7ec6805e6de8794514c0a935081b5d36d38fBrian      dsa->alpha.enabled = 1;
1442b32c7dff51fa9853ba528744bd191400aa4b206Brian Paul      dsa->alpha.func = st_compare_func_to_pipe(ctx->Color.AlphaFunc);
14547e3896dfd89a26abbe4ca2469c2480f3982b204Luca Barbieri      dsa->alpha.ref_value = ctx->Color.AlphaRefUnclamped;
146bfe79babf99e6b9435195178d1ea64687c60d161Keith Whitwell   }
147bfe79babf99e6b9435195178d1ea64687c60d161Keith Whitwell
148339e7ec6805e6de8794514c0a935081b5d36d38fBrian   cso_set_depth_stencil_alpha(st->cso_context, dsa);
1491a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger   cso_set_stencil_ref(st->cso_context, &sr);
1504c9b48dcc510929951516f3cd17b98654ac93a9aBrian}
1514c9b48dcc510929951516f3cd17b98654ac93a9aBrian
1524c9b48dcc510929951516f3cd17b98654ac93a9aBrian
153bfe79babf99e6b9435195178d1ea64687c60d161Keith Whitwellconst struct st_tracked_state st_update_depth_stencil_alpha = {
15454507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   "st_update_depth_stencil",				/* name */
15554507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   {							/* dirty */
15654507125e735ffa595e252282eaabf38095c21e1Alan Hourihane      (_NEW_DEPTH|_NEW_STENCIL|_NEW_COLOR),		/* mesa */
15754507125e735ffa595e252282eaabf38095c21e1Alan Hourihane      0,						/* st */
1584c9b48dcc510929951516f3cd17b98654ac93a9aBrian   },
15954507125e735ffa595e252282eaabf38095c21e1Alan Hourihane   update_depth_stencil_alpha				/* update */
1604c9b48dcc510929951516f3cd17b98654ac93a9aBrian};
161