xref: /aosp_15_r20/external/mesa3d/src/mesa/main/stencil.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /**
2*61046927SAndroid Build Coastguard Worker  * \file stencil.h
3*61046927SAndroid Build Coastguard Worker  * Stencil operations.
4*61046927SAndroid Build Coastguard Worker  */
5*61046927SAndroid Build Coastguard Worker 
6*61046927SAndroid Build Coastguard Worker /*
7*61046927SAndroid Build Coastguard Worker  * Mesa 3-D graphics library
8*61046927SAndroid Build Coastguard Worker  *
9*61046927SAndroid Build Coastguard Worker  * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
10*61046927SAndroid Build Coastguard Worker  *
11*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
12*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
13*61046927SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
14*61046927SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15*61046927SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
16*61046927SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
17*61046927SAndroid Build Coastguard Worker  *
18*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice shall be included
19*61046927SAndroid Build Coastguard Worker  * in all copies or substantial portions of the Software.
20*61046927SAndroid Build Coastguard Worker  *
21*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22*61046927SAndroid Build Coastguard Worker  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24*61046927SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25*61046927SAndroid Build Coastguard Worker  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26*61046927SAndroid Build Coastguard Worker  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27*61046927SAndroid Build Coastguard Worker  * OTHER DEALINGS IN THE SOFTWARE.
28*61046927SAndroid Build Coastguard Worker  */
29*61046927SAndroid Build Coastguard Worker 
30*61046927SAndroid Build Coastguard Worker 
31*61046927SAndroid Build Coastguard Worker #ifndef STENCIL_H
32*61046927SAndroid Build Coastguard Worker #define STENCIL_H
33*61046927SAndroid Build Coastguard Worker 
34*61046927SAndroid Build Coastguard Worker 
35*61046927SAndroid Build Coastguard Worker #include "util/glheader.h"
36*61046927SAndroid Build Coastguard Worker #include "macros.h"
37*61046927SAndroid Build Coastguard Worker 
38*61046927SAndroid Build Coastguard Worker struct gl_context;
39*61046927SAndroid Build Coastguard Worker 
40*61046927SAndroid Build Coastguard Worker extern void
41*61046927SAndroid Build Coastguard Worker _mesa_init_stencil( struct gl_context * ctx );
42*61046927SAndroid Build Coastguard Worker 
43*61046927SAndroid Build Coastguard Worker /* From the GL 4.3 spec, 17.3.5:
44*61046927SAndroid Build Coastguard Worker  *    "Stencil comparison operations and queries of <ref> clamp its value
45*61046927SAndroid Build Coastguard Worker  *    to the range [0, 2^s-1], where <s> is the number of bits in the
46*61046927SAndroid Build Coastguard Worker  *    stencil buffer attached to the draw framebuffer."
47*61046927SAndroid Build Coastguard Worker  */
48*61046927SAndroid Build Coastguard Worker 
49*61046927SAndroid Build Coastguard Worker static inline GLint
_mesa_get_stencil_ref(struct gl_context const * ctx,int face)50*61046927SAndroid Build Coastguard Worker _mesa_get_stencil_ref(struct gl_context const *ctx, int face)
51*61046927SAndroid Build Coastguard Worker {
52*61046927SAndroid Build Coastguard Worker    GLint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
53*61046927SAndroid Build Coastguard Worker    GLint ref = ctx->Stencil.Ref[face];
54*61046927SAndroid Build Coastguard Worker    return CLAMP(ref, 0, stencilMax);
55*61046927SAndroid Build Coastguard Worker }
56*61046927SAndroid Build Coastguard Worker 
57*61046927SAndroid Build Coastguard Worker static inline bool
_mesa_stencil_is_enabled(const struct gl_context * ctx)58*61046927SAndroid Build Coastguard Worker _mesa_stencil_is_enabled(const struct gl_context *ctx)
59*61046927SAndroid Build Coastguard Worker {
60*61046927SAndroid Build Coastguard Worker    return ctx->Stencil.Enabled &&
61*61046927SAndroid Build Coastguard Worker           ctx->DrawBuffer->Visual.stencilBits > 0;
62*61046927SAndroid Build Coastguard Worker }
63*61046927SAndroid Build Coastguard Worker 
64*61046927SAndroid Build Coastguard Worker static inline bool
_mesa_stencil_is_two_sided(const struct gl_context * ctx)65*61046927SAndroid Build Coastguard Worker _mesa_stencil_is_two_sided(const struct gl_context *ctx)
66*61046927SAndroid Build Coastguard Worker {
67*61046927SAndroid Build Coastguard Worker    const int face = ctx->Stencil._BackFace;
68*61046927SAndroid Build Coastguard Worker 
69*61046927SAndroid Build Coastguard Worker    return _mesa_stencil_is_enabled(ctx) &&
70*61046927SAndroid Build Coastguard Worker           (ctx->Stencil.Function[0] != ctx->Stencil.Function[face] ||
71*61046927SAndroid Build Coastguard Worker            ctx->Stencil.FailFunc[0] != ctx->Stencil.FailFunc[face] ||
72*61046927SAndroid Build Coastguard Worker            ctx->Stencil.ZPassFunc[0] != ctx->Stencil.ZPassFunc[face] ||
73*61046927SAndroid Build Coastguard Worker            ctx->Stencil.ZFailFunc[0] != ctx->Stencil.ZFailFunc[face] ||
74*61046927SAndroid Build Coastguard Worker            ctx->Stencil.Ref[0] != ctx->Stencil.Ref[face] ||
75*61046927SAndroid Build Coastguard Worker            ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[face] ||
76*61046927SAndroid Build Coastguard Worker            ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[face]);
77*61046927SAndroid Build Coastguard Worker }
78*61046927SAndroid Build Coastguard Worker 
79*61046927SAndroid Build Coastguard Worker static inline bool
_mesa_stencil_is_write_enabled(const struct gl_context * ctx,bool is_two_sided)80*61046927SAndroid Build Coastguard Worker _mesa_stencil_is_write_enabled(const struct gl_context *ctx, bool is_two_sided)
81*61046927SAndroid Build Coastguard Worker {
82*61046927SAndroid Build Coastguard Worker    return _mesa_stencil_is_enabled(ctx) &&
83*61046927SAndroid Build Coastguard Worker           (ctx->Stencil.WriteMask[0] != 0 ||
84*61046927SAndroid Build Coastguard Worker            (is_two_sided &&
85*61046927SAndroid Build Coastguard Worker             ctx->Stencil.WriteMask[ctx->Stencil._BackFace] != 0));
86*61046927SAndroid Build Coastguard Worker }
87*61046927SAndroid Build Coastguard Worker 
88*61046927SAndroid Build Coastguard Worker #endif
89