1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // Based on Stencil_Test.c from
8 // Book: OpenGL(R) ES 2.0 Programming Guide
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
10 // ISBN-10: 0321502795
11 // ISBN-13: 9780321502797
12 // Publisher: Addison-Wesley Professional
13 // URLs: http://safari.informit.com/9780321563835
14 // http://www.opengles-book.com
15
16 #include "SampleApplication.h"
17
18 #include "util/shader_utils.h"
19
20 class StencilOperationsSample : public SampleApplication
21 {
22 public:
StencilOperationsSample(int argc,char ** argv)23 StencilOperationsSample(int argc, char **argv)
24 : SampleApplication("StencilOperations", argc, argv)
25 {}
26
initialize()27 bool initialize() override
28 {
29 constexpr char kVS[] = R"(attribute vec4 a_position;
30 void main()
31 {
32 gl_Position = a_position;
33 })";
34
35 constexpr char kFS[] = R"(precision mediump float;
36 uniform vec4 u_color;
37 void main()
38 {
39 gl_FragColor = u_color;
40 })";
41
42 mProgram = CompileProgram(kVS, kFS);
43 if (!mProgram)
44 {
45 return false;
46 }
47
48 // Get the attribute locations
49 mPositionLoc = glGetAttribLocation(mProgram, "a_position");
50
51 // Get the sampler location
52 mColorLoc = glGetUniformLocation(mProgram, "u_color");
53
54 // Set the clear color
55 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
56
57 // Set the stencil clear value
58 glClearStencil(0x01);
59
60 // Set the depth clear value
61 glClearDepthf(0.75f);
62
63 // Enable the depth and stencil tests
64 glEnable(GL_DEPTH_TEST);
65 glEnable(GL_STENCIL_TEST);
66
67 return true;
68 }
69
destroy()70 void destroy() override { glDeleteProgram(mProgram); }
71
draw()72 void draw() override
73 {
74 GLfloat vertices[] = {
75 -0.75f, 0.25f, 0.50f, // Quad #0
76 -0.25f, 0.25f, 0.50f, -0.25f, 0.75f, 0.50f, -0.75f, 0.75f, 0.50f,
77 0.25f, 0.25f, 0.90f, // Quad #1
78 0.75f, 0.25f, 0.90f, 0.75f, 0.75f, 0.90f, 0.25f, 0.75f, 0.90f,
79 -0.75f, -0.75f, 0.50f, // Quad #2
80 -0.25f, -0.75f, 0.50f, -0.25f, -0.25f, 0.50f, -0.75f, -0.25f, 0.50f,
81 0.25f, -0.75f, 0.50f, // Quad #3
82 0.75f, -0.75f, 0.50f, 0.75f, -0.25f, 0.50f, 0.25f, -0.25f, 0.50f,
83 -1.00f, -1.00f, 0.00f, // Big Quad
84 1.00f, -1.00f, 0.00f, 1.00f, 1.00f, 0.00f, -1.00f, 1.00f, 0.00f,
85 };
86
87 GLubyte indices[][6] = {
88 {0, 1, 2, 0, 2, 3}, // Quad #0
89 {4, 5, 6, 4, 6, 7}, // Quad #1
90 {8, 9, 10, 8, 10, 11}, // Quad #2
91 {12, 13, 14, 12, 14, 15}, // Quad #3
92 {16, 17, 18, 16, 18, 19}, // Big Quad
93 };
94
95 static const size_t testCount = 4;
96 GLfloat colors[testCount][4] = {
97 {1.0f, 0.0f, 0.0f, 1.0f},
98 {0.0f, 1.0f, 0.0f, 1.0f},
99 {0.0f, 0.0f, 1.0f, 1.0f},
100 {1.0f, 1.0f, 0.0f, 0.0f},
101 };
102
103 GLuint stencilValues[testCount] = {
104 0x7, // Result of test 0
105 0x0, // Result of test 1
106 0x2, // Result of test 2
107 0xff // Result of test 3. We need to fill this value in at run-time
108 };
109
110 // Set the viewport
111 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
112
113 // Clear the color, depth, and stencil buffers. At this point, the stencil
114 // buffer will be 0x1 for all pixels
115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
116
117 // Use the program object
118 glUseProgram(mProgram);
119
120 // Load the vertex data
121 glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, vertices);
122 glEnableVertexAttribArray(mPositionLoc);
123
124 // Test 0:
125 //
126 // Initialize upper-left region. In this case, the stencil-buffer values will
127 // be replaced because the stencil test for the rendered pixels will fail the
128 // stencil test, which is
129 //
130 // ref mask stencil mask
131 // ( 0x7 & 0x3 ) < ( 0x1 & 0x7 )
132 //
133 // The value in the stencil buffer for these pixels will be 0x7.
134 glStencilFunc(GL_LESS, 0x7, 0x3);
135 glStencilOp(GL_REPLACE, GL_DECR, GL_DECR);
136 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[0]);
137
138 // Test 1:
139 //
140 // Initialize the upper-right region. Here, we'll decrement the stencil-buffer
141 // values where the stencil test passes but the depth test fails. The stencil test is
142 //
143 // ref mask stencil mask
144 // ( 0x3 & 0x3 ) > ( 0x1 & 0x3 )
145 //
146 // but where the geometry fails the depth test. The stencil values for these pixels
147 // will be 0x0.
148 glStencilFunc(GL_GREATER, 0x3, 0x3);
149 glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
150 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[1]);
151
152 // Test 2:
153 //
154 // Initialize the lower-left region. Here we'll increment (with saturation) the
155 // stencil value where both the stencil and depth tests pass. The stencil test for
156 // these pixels will be
157 //
158 // ref mask stencil mask
159 // ( 0x1 & 0x3 ) == ( 0x1 & 0x3 )
160 //
161 // The stencil values for these pixels will be 0x2.
162 glStencilFunc(GL_EQUAL, 0x1, 0x3);
163 glStencilOp(GL_KEEP, GL_INCR, GL_INCR);
164 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[2]);
165
166 // Test 3:
167 //
168 // Finally, initialize the lower-right region. We'll invert the stencil value
169 // where the stencil tests fails. The stencil test for these pixels will be
170 //
171 // ref mask stencil mask
172 // ( 0x2 & 0x1 ) == ( 0x1 & 0x1 )
173 //
174 // The stencil value here will be set to ~((2^s-1) & 0x1), (with the 0x1 being
175 // from the stencil clear value), where 's' is the number of bits in the stencil
176 // buffer
177 glStencilFunc(GL_EQUAL, 0x2, 0x1);
178 glStencilOp(GL_INVERT, GL_KEEP, GL_KEEP);
179 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[3]);
180
181 // Since we don't know at compile time how many stencil bits are present, we'll
182 // query, and update the value correct value in the stencilValues arrays for the
183 // fourth tests. We'll use this value later in rendering.
184 GLint stencilBitCount = 0;
185 glGetIntegerv(GL_STENCIL_BITS, &stencilBitCount);
186 stencilValues[3] = ~(((1 << stencilBitCount) - 1) & 0x1) & 0xff;
187
188 // Use the stencil buffer for controlling where rendering will occur. We disable
189 // writing to the stencil buffer so we can test against them without modifying
190 // the values we generated.
191 glStencilMask(0x0);
192
193 for (size_t i = 0; i < testCount; ++i)
194 {
195 glStencilFunc(GL_EQUAL, stencilValues[i], 0xff);
196 glUniform4fv(mColorLoc, 1, colors[i]);
197 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[4]);
198 }
199
200 // Reset the stencil mask
201 glStencilMask(0xFF);
202 }
203
204 private:
205 // Handle to a program object
206 GLuint mProgram;
207
208 // Attribute locations
209 GLint mPositionLoc;
210
211 // Uniform locations
212 GLint mColorLoc;
213 };
214
main(int argc,char ** argv)215 int main(int argc, char **argv)
216 {
217 StencilOperationsSample app(argc, argv);
218 return app.run();
219 }
220