1/**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 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.
25 *
26 **************************************************************************/
27
28 /*
29  * Authors:
30  *   Keith Whitwell <keithw@vmware.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#include "main/samplerobj.h"
44#include "main/stencil.h"
45
46
47/**
48 * Convert GLenum stencil op tokens to pipe tokens.
49 */
50static GLuint
51gl_stencil_op_to_pipe(GLenum func)
52{
53   switch (func) {
54   case GL_KEEP:
55      return PIPE_STENCIL_OP_KEEP;
56   case GL_ZERO:
57      return PIPE_STENCIL_OP_ZERO;
58   case GL_REPLACE:
59      return PIPE_STENCIL_OP_REPLACE;
60   case GL_INCR:
61      return PIPE_STENCIL_OP_INCR;
62   case GL_DECR:
63      return PIPE_STENCIL_OP_DECR;
64   case GL_INCR_WRAP:
65      return PIPE_STENCIL_OP_INCR_WRAP;
66   case GL_DECR_WRAP:
67      return PIPE_STENCIL_OP_DECR_WRAP;
68   case GL_INVERT:
69      return PIPE_STENCIL_OP_INVERT;
70   default:
71      assert("invalid GL token in gl_stencil_op_to_pipe()" == NULL);
72      return 0;
73   }
74}
75
76void
77st_update_depth_stencil_alpha(struct st_context *st)
78{
79   struct pipe_depth_stencil_alpha_state *dsa = &st->state.depth_stencil;
80   struct pipe_stencil_ref sr;
81   struct gl_context *ctx = st->ctx;
82
83   memset(dsa, 0, sizeof(*dsa));
84   memset(&sr, 0, sizeof(sr));
85
86   if (ctx->DrawBuffer->Visual.depthBits > 0) {
87      if (ctx->Depth.Test) {
88         dsa->depth_enabled = 1;
89         dsa->depth_func = func_to_gallium(ctx->Depth.Func);
90         if (dsa->depth_func != PIPE_FUNC_EQUAL)
91            dsa->depth_writemask = ctx->Depth.Mask;
92      }
93      if (ctx->Depth.BoundsTest) {
94         dsa->depth_bounds_test = 1;
95         dsa->depth_bounds_min = ctx->Depth.BoundsMin;
96         dsa->depth_bounds_max = ctx->Depth.BoundsMax;
97      }
98   }
99
100   if (ctx->Stencil.Enabled && ctx->DrawBuffer->Visual.stencilBits > 0) {
101      dsa->stencil[0].enabled = 1;
102      dsa->stencil[0].func = func_to_gallium(ctx->Stencil.Function[0]);
103      dsa->stencil[0].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[0]);
104      dsa->stencil[0].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[0]);
105      dsa->stencil[0].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[0]);
106      dsa->stencil[0].valuemask = ctx->Stencil.ValueMask[0] & 0xff;
107      dsa->stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
108      sr.ref_value[0] = _mesa_get_stencil_ref(ctx, 0);
109
110      if (_mesa_stencil_is_two_sided(ctx)) {
111         const GLuint back = ctx->Stencil._BackFace;
112         dsa->stencil[1].enabled = 1;
113         dsa->stencil[1].func = func_to_gallium(ctx->Stencil.Function[back]);
114         dsa->stencil[1].fail_op = gl_stencil_op_to_pipe(ctx->Stencil.FailFunc[back]);
115         dsa->stencil[1].zfail_op = gl_stencil_op_to_pipe(ctx->Stencil.ZFailFunc[back]);
116         dsa->stencil[1].zpass_op = gl_stencil_op_to_pipe(ctx->Stencil.ZPassFunc[back]);
117         dsa->stencil[1].valuemask = ctx->Stencil.ValueMask[back] & 0xff;
118         dsa->stencil[1].writemask = ctx->Stencil.WriteMask[back] & 0xff;
119         sr.ref_value[1] = _mesa_get_stencil_ref(ctx, back);
120      }
121      else {
122         /* This should be unnecessary. Drivers must not expect this to
123          * contain valid data, except the enabled bit
124          */
125         dsa->stencil[1] = dsa->stencil[0];
126         dsa->stencil[1].enabled = 0;
127         sr.ref_value[1] = sr.ref_value[0];
128      }
129   }
130
131   if (ctx->Color.AlphaEnabled && !st->lower_alpha_test &&
132       !(ctx->DrawBuffer->_IntegerBuffers & 0x1)) {
133      dsa->alpha_enabled = 1;
134      dsa->alpha_func = func_to_gallium(ctx->Color.AlphaFunc);
135      dsa->alpha_ref_value = ctx->Color.AlphaRefUnclamped;
136   }
137
138   cso_set_depth_stencil_alpha(st->cso_context, dsa);
139   cso_set_stencil_ref(st->cso_context, sr);
140}
141