blob: 207a8316e8c18be5e8af2712939b8b784881eb85 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.comd302f142011-03-03 13:54:13 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.comd302f142011-03-03 13:54:13 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.comd302f142011-03-03 13:54:13 +000010#ifndef GrStencil_DEFINED
11#define GrStencil_DEFINED
12
13#include "GrTypes.h"
14/**
15 * Gr uses the stencil buffer to implement complex clipping inside the
16 * GrDrawTarget class. The GrDrawTarget makes a subset of the stencil buffer
17 * bits available for other uses by external code (clients). Client code can
18 * modify these bits. GrDrawTarget will ignore ref, mask, and writemask bits
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +000019 * provided by clients that overlap the bits used to implement clipping.
bsalomon@google.comd302f142011-03-03 13:54:13 +000020 *
21 * When code outside the GrDrawTarget class uses the stencil buffer the contract
22 * is as follows:
23 *
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +000024 * > Normal stencil funcs allow the client to pass / fail regardless of the
25 * reserved clip bits.
26 * > Additional functions allow a test against the clip along with a limited
27 * set of tests against the client bits.
bsalomon@google.comd302f142011-03-03 13:54:13 +000028 * > Client can assume all client bits are zero initially.
29 * > Client must ensure that after all its passes are finished it has only
30 * written to the color buffer in the region inside the clip. Furthermore, it
31 * must zero all client bits that were modifed (both inside and outside the
32 * clip).
33 */
34
35/**
36 * Determines which pixels pass / fail the stencil test.
37 * Stencil test passes if (ref & mask) FUNC (stencil & mask) is true
38 */
39enum GrStencilFunc {
40 kAlways_StencilFunc = 0,
41 kNever_StencilFunc,
42 kGreater_StencilFunc,
43 kGEqual_StencilFunc,
44 kLess_StencilFunc,
45 kLEqual_StencilFunc,
46 kEqual_StencilFunc,
47 kNotEqual_StencilFunc,
48
49 // Gr stores the current clip in the
50 // stencil buffer in the high bits that
51 // are not directly accessible modifiable
52 // via the GrDrawTarget interface. The below
53 // stencil funcs test against the current
54 // clip in addition to the GrDrawTarget
55 // client's stencil bits.
56
57 // pass if inside the clip
58 kAlwaysIfInClip_StencilFunc,
59 kEqualIfInClip_StencilFunc,
60 kLessIfInClip_StencilFunc,
61 kLEqualIfInClip_StencilFunc,
62 kNonZeroIfInClip_StencilFunc, // this one forces the ref to be 0
63
64 // counts
65 kStencilFuncCount,
66 kClipStencilFuncCount = kNonZeroIfInClip_StencilFunc -
67 kAlwaysIfInClip_StencilFunc + 1,
68 kBasicStencilFuncCount = kStencilFuncCount - kClipStencilFuncCount
69};
70
71/**
72 * Operations to perform based on whether stencil test passed failed.
73 */
74enum GrStencilOp {
75 kKeep_StencilOp = 0, // preserve existing stencil value
76 kReplace_StencilOp, // replace with reference value from stencl test
77 kIncWrap_StencilOp, // increment and wrap at max
78 kIncClamp_StencilOp, // increment and clamp at max
79 kDecWrap_StencilOp, // decrement and wrap at 0
80 kDecClamp_StencilOp, // decrement and clamp at 0
81 kZero_StencilOp, // zero stencil bits
82 kInvert_StencilOp, // invert stencil bits
83
84 kStencilOpCount
85};
86
87/**
88 * Struct representing stencil state.
89 */
90struct GrStencilSettings {
tomhudson@google.com62b09682011-11-09 16:39:17 +000091 GrStencilOp fFrontPassOp : 8; // op to perform when front faces pass
92 GrStencilOp fBackPassOp : 8; // op to perform when back faces pass
93 GrStencilOp fFrontFailOp : 8; // op to perform when front faces fail
94 GrStencilOp fBackFailOp : 8; // op to perform when back faces fail
95 GrStencilFunc fFrontFunc : 8; // test function for front faces
96 GrStencilFunc fBackFunc : 8; // test function for back faces
97 unsigned short fFrontFuncMask; // mask for front face test
98 unsigned short fBackFuncMask; // mask for back face test
99 unsigned short fFrontFuncRef; // reference value for front face test
100 unsigned short fBackFuncRef; // reference value for back face test
101 unsigned short fFrontWriteMask; // stencil write mask for front faces
102 unsigned short fBackWriteMask; // stencil write mask for back faces
bsalomon@google.comd302f142011-03-03 13:54:13 +0000103
104 bool operator == (const GrStencilSettings& s) const {
tomhudson@google.com62b09682011-11-09 16:39:17 +0000105 // make sure this is tightly packed (< 4B padding).
106 GR_STATIC_ASSERT(sizeof(GrStencilSettings) / 4 ==
107 (4*sizeof(uint8_t) +
108 2*sizeof(uint8_t) +
109 6*sizeof(unsigned short) + 3) / 4);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000110 return 0 == memcmp(this, &s, sizeof(GrStencilSettings));
111 }
112
113 bool operator != (const GrStencilSettings& s) const {
114 return !(*this == s);
115 }
116
117 GrStencilSettings& operator =(const GrStencilSettings& s) {
118 memcpy(this, &s, sizeof(GrStencilSettings));
119 return *this;
120 }
121
122 void setSame(GrStencilOp passOp,
123 GrStencilOp failOp,
124 GrStencilFunc func,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000125 unsigned short funcMask,
126 unsigned short funcRef,
127 unsigned short writeMask) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000128 fFrontPassOp = passOp;
129 fBackPassOp = passOp;
130 fFrontFailOp = failOp;
131 fBackFailOp = failOp;
132 fFrontFunc = func;
133 fBackFunc = func;
134 fFrontFuncMask = funcMask;
135 fBackFuncMask = funcMask;
136 fFrontFuncRef = funcRef;
137 fBackFuncRef = funcRef;
138 fFrontWriteMask = writeMask;
139 fBackWriteMask = writeMask;
140 }
141
142 // canonical value for disabled stenciling
143 static const GrStencilSettings gDisabled;
144 void setDisabled() {
145 *this = gDisabled;
146 }
147 bool isDisabled() const {
148 return kKeep_StencilOp == fFrontPassOp &&
149 kKeep_StencilOp == fBackPassOp &&
150 kKeep_StencilOp == fFrontFailOp &&
bsalomon@google.comd7beab42011-05-27 16:42:30 +0000151 kKeep_StencilOp == fBackFailOp &&
bsalomon@google.comd302f142011-03-03 13:54:13 +0000152 kAlways_StencilFunc == fFrontFunc &&
153 kAlways_StencilFunc == fBackFunc;
154 }
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000155 bool doesWrite() const {
156 return !((kNever_StencilFunc == fFrontFunc ||
157 kKeep_StencilOp == fFrontPassOp) &&
158 (kNever_StencilFunc == fBackFunc ||
159 kKeep_StencilOp == fBackPassOp) &&
160 (kAlways_StencilFunc == fFrontFunc ||
161 kKeep_StencilOp == fFrontFailOp) &&
162 (kAlways_StencilFunc == fBackFunc ||
163 kKeep_StencilOp == fBackFailOp));
164 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000165 void invalidate() {
166 // just write an illegal value to the first member
tomhudson@google.com62b09682011-11-09 16:39:17 +0000167 fFrontPassOp = (GrStencilOp)(uint8_t)-1;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000168 }
169
170private:
171 friend class GrGpu;
172
173 enum {
174 kMaxStencilClipPasses = 2 // maximum number of passes to add a clip
175 // element to the stencil buffer.
176 };
177
178 /**
179 * Given a thing to draw into the stencil clip, a fill type, and a set op
180 * this function determines:
181 * 1. Whether the thing can be draw directly to the stencil clip or
182 * needs to be drawn to the client portion of the stencil first.
183 * 2. How many passes are needed.
184 * 3. What those passes are.
185 * 4. The fill rule that should actually be used to render (will
186 * always be non-inverted).
187 *
188 * @param op the set op to combine this element with the
189 * existing clip
190 * @param stencilClipMask mask with just the stencil bit used for clipping
191 * enabled.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000192 * @param invertedFill is this path inverted
bsalomon@google.comd302f142011-03-03 13:54:13 +0000193 * @param numPasses out: the number of passes needed to add the
194 * element to the clip.
195 * @param settings out: the stencil settings to use for each pass
196 *
197 * @return true if the clip element's geometry can be drawn directly to the
198 * stencil clip bit. Will only be true if canBeDirect is true.
199 * numPasses will be 1 if return value is true.
200 */
201 static bool GetClipPasses(GrSetOp op,
202 bool canBeDirect,
203 unsigned int stencilClipMask,
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000204 bool invertedFill,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000205 int* numPasses,
206 GrStencilSettings settings[kMaxStencilClipPasses]);
207};
208
209#endif