1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2007 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci /*
29bf215546Sopenharmony_ci  * Authors:
30bf215546Sopenharmony_ci  *   Keith Whitwell <keithw@vmware.com>
31bf215546Sopenharmony_ci  *   Brian Paul
32bf215546Sopenharmony_ci  *   Zack  Rusin
33bf215546Sopenharmony_ci  */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#include <assert.h>
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci#include "st_context.h"
39bf215546Sopenharmony_ci#include "st_atom.h"
40bf215546Sopenharmony_ci#include "pipe/p_context.h"
41bf215546Sopenharmony_ci#include "pipe/p_defines.h"
42bf215546Sopenharmony_ci#include "cso_cache/cso_context.h"
43bf215546Sopenharmony_ci#include "main/samplerobj.h"
44bf215546Sopenharmony_ci#include "main/stencil.h"
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci/**
48bf215546Sopenharmony_ci * Convert GLenum stencil op tokens to pipe tokens.
49bf215546Sopenharmony_ci */
50bf215546Sopenharmony_cistatic GLuint
51bf215546Sopenharmony_cigl_stencil_op_to_pipe(GLenum func)
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci   switch (func) {
54bf215546Sopenharmony_ci   case GL_KEEP:
55bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_KEEP;
56bf215546Sopenharmony_ci   case GL_ZERO:
57bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_ZERO;
58bf215546Sopenharmony_ci   case GL_REPLACE:
59bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_REPLACE;
60bf215546Sopenharmony_ci   case GL_INCR:
61bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_INCR;
62bf215546Sopenharmony_ci   case GL_DECR:
63bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_DECR;
64bf215546Sopenharmony_ci   case GL_INCR_WRAP:
65bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_INCR_WRAP;
66bf215546Sopenharmony_ci   case GL_DECR_WRAP:
67bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_DECR_WRAP;
68bf215546Sopenharmony_ci   case GL_INVERT:
69bf215546Sopenharmony_ci      return PIPE_STENCIL_OP_INVERT;
70bf215546Sopenharmony_ci   default:
71bf215546Sopenharmony_ci      assert("invalid GL token in gl_stencil_op_to_pipe()" == NULL);
72bf215546Sopenharmony_ci      return 0;
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_civoid
77bf215546Sopenharmony_cist_update_depth_stencil_alpha(struct st_context *st)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   struct pipe_depth_stencil_alpha_state *dsa = &st->state.depth_stencil;
80bf215546Sopenharmony_ci   struct pipe_stencil_ref sr;
81bf215546Sopenharmony_ci   struct gl_context *ctx = st->ctx;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   memset(dsa, 0, sizeof(*dsa));
84bf215546Sopenharmony_ci   memset(&sr, 0, sizeof(sr));
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   if (ctx->DrawBuffer->Visual.depthBits > 0) {
87bf215546Sopenharmony_ci      if (ctx->Depth.Test) {
88bf215546Sopenharmony_ci         dsa->depth_enabled = 1;
89bf215546Sopenharmony_ci         dsa->depth_func = func_to_gallium(ctx->Depth.Func);
90bf215546Sopenharmony_ci         if (dsa->depth_func != PIPE_FUNC_EQUAL)
91bf215546Sopenharmony_ci            dsa->depth_writemask = ctx->Depth.Mask;
92bf215546Sopenharmony_ci      }
93bf215546Sopenharmony_ci      if (ctx->Depth.BoundsTest) {
94bf215546Sopenharmony_ci         dsa->depth_bounds_test = 1;
95bf215546Sopenharmony_ci         dsa->depth_bounds_min = ctx->Depth.BoundsMin;
96bf215546Sopenharmony_ci         dsa->depth_bounds_max = ctx->Depth.BoundsMax;
97bf215546Sopenharmony_ci      }
98bf215546Sopenharmony_ci   }
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   if (ctx->Stencil.Enabled && ctx->DrawBuffer->Visual.stencilBits > 0) {
101bf215546Sopenharmony_ci      dsa->stencil[0].enabled = 1;
102bf215546Sopenharmony_ci      dsa->stencil[0].func = func_to_gallium(ctx->Stencil.Function[0]);
103bf215546Sopenharmony_ci      dsa->stencil[0].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[0]);
104bf215546Sopenharmony_ci      dsa->stencil[0].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[0]);
105bf215546Sopenharmony_ci      dsa->stencil[0].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[0]);
106bf215546Sopenharmony_ci      dsa->stencil[0].valuemask = ctx->Stencil.ValueMask[0] & 0xff;
107bf215546Sopenharmony_ci      dsa->stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
108bf215546Sopenharmony_ci      sr.ref_value[0] = _mesa_get_stencil_ref(ctx, 0);
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci      if (_mesa_stencil_is_two_sided(ctx)) {
111bf215546Sopenharmony_ci         const GLuint back = ctx->Stencil._BackFace;
112bf215546Sopenharmony_ci         dsa->stencil[1].enabled = 1;
113bf215546Sopenharmony_ci         dsa->stencil[1].func = func_to_gallium(ctx->Stencil.Function[back]);
114bf215546Sopenharmony_ci         dsa->stencil[1].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[back]);
115bf215546Sopenharmony_ci         dsa->stencil[1].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[back]);
116bf215546Sopenharmony_ci         dsa->stencil[1].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[back]);
117bf215546Sopenharmony_ci         dsa->stencil[1].valuemask = ctx->Stencil.ValueMask[back] & 0xff;
118bf215546Sopenharmony_ci         dsa->stencil[1].writemask = ctx->Stencil.WriteMask[back] & 0xff;
119bf215546Sopenharmony_ci         sr.ref_value[1] = _mesa_get_stencil_ref(ctx, back);
120bf215546Sopenharmony_ci      }
121bf215546Sopenharmony_ci      else {
122bf215546Sopenharmony_ci         /* This should be unnecessary. Drivers must not expect this to
123bf215546Sopenharmony_ci          * contain valid data, except the enabled bit
124bf215546Sopenharmony_ci          */
125bf215546Sopenharmony_ci         dsa->stencil[1] = dsa->stencil[0];
126bf215546Sopenharmony_ci         dsa->stencil[1].enabled = 0;
127bf215546Sopenharmony_ci         sr.ref_value[1] = sr.ref_value[0];
128bf215546Sopenharmony_ci      }
129bf215546Sopenharmony_ci   }
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   if (ctx->Color.AlphaEnabled && !st->lower_alpha_test &&
132bf215546Sopenharmony_ci       !(ctx->DrawBuffer->_IntegerBuffers & 0x1)) {
133bf215546Sopenharmony_ci      dsa->alpha_enabled = 1;
134bf215546Sopenharmony_ci      dsa->alpha_func = func_to_gallium(ctx->Color.AlphaFunc);
135bf215546Sopenharmony_ci      dsa->alpha_ref_value = ctx->Color.AlphaRefUnclamped;
136bf215546Sopenharmony_ci   }
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci   cso_set_depth_stencil_alpha(st->cso_context, dsa);
139bf215546Sopenharmony_ci   cso_set_stencil_ref(st->cso_context, sr);
140bf215546Sopenharmony_ci}
141