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