xref: /aosp_15_r20/external/angle/samples/post_sub_buffer/PostSubBuffer.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 Simple_VertexShader.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 #include "texture_utils.h"
18 #include "util/Matrix.h"
19 #include "util/geometry_utils.h"
20 #include "util/shader_utils.h"
21 
22 #include <cmath>
23 #include <iostream>
24 
25 class PostSubBufferSample : public SampleApplication
26 {
27   public:
PostSubBufferSample(int argc,char ** argv)28     PostSubBufferSample(int argc, char **argv) : SampleApplication("PostSubBuffer", argc, argv) {}
29 
initialize()30     bool initialize() override
31     {
32         constexpr char kVS[] = R"(uniform mat4 u_mvpMatrix;
33 attribute vec4 a_position;
34 attribute vec2 a_texcoord;
35 varying vec2 v_texcoord;
36 void main()
37 {
38     gl_Position = u_mvpMatrix * a_position;
39     v_texcoord = a_texcoord;
40 })";
41 
42         constexpr char kFS[] = R"(precision mediump float;
43 varying vec2 v_texcoord;
44 void main()
45 {
46     gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);
47 })";
48 
49         mProgram = CompileProgram(kVS, kFS);
50         if (!mProgram)
51         {
52             return false;
53         }
54 
55         // Get the attribute locations
56         mPositionLoc = glGetAttribLocation(mProgram, "a_position");
57         mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");
58 
59         // Get the uniform locations
60         mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");
61 
62         // Generate the geometry data
63         GenerateCubeGeometry(0.5f, &mCube);
64 
65         // Set an initial rotation
66         mRotation = 45.0f;
67 
68         // Clear the whole window surface to blue.
69         glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
70         glClear(GL_COLOR_BUFFER_BIT);
71         SampleApplication::swap();
72 
73         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
74         glCullFace(GL_BACK);
75         glEnable(GL_CULL_FACE);
76 
77         return true;
78     }
79 
destroy()80     void destroy() override { glDeleteProgram(mProgram); }
81 
step(float dt,double totalTime)82     void step(float dt, double totalTime) override
83     {
84         mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);
85 
86         Matrix4 perspectiveMatrix = Matrix4::perspective(
87             60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(), 1.0f, 20.0f);
88 
89         Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0.0f, 0.0f, -2.0f)) *
90                               Matrix4::rotate(mRotation, angle::Vector3(1.0f, 0.0f, 1.0f));
91 
92         Matrix4 viewMatrix = Matrix4::identity();
93 
94         Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;
95 
96         // Load the matrices
97         glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
98     }
99 
draw()100     void draw() override
101     {
102         // Set the viewport
103         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
104 
105         // Clear the color buffer
106         glClear(GL_COLOR_BUFFER_BIT);
107 
108         // Use the program object
109         glUseProgram(mProgram);
110 
111         // Load the vertex position
112         glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());
113         glEnableVertexAttribArray(mPositionLoc);
114 
115         // Load the texcoord data
116         glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());
117         glEnableVertexAttribArray(mTexcoordLoc);
118 
119         // Draw the cube
120         glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mCube.indices.size()), GL_UNSIGNED_SHORT,
121                        mCube.indices.data());
122     }
123 
swap()124     void swap() override
125     {
126         // Instead of letting the application call eglSwapBuffers, call eglPostSubBufferNV here
127         // instead
128         EGLint windowWidth  = static_cast<EGLint>(getWindow()->getWidth());
129         EGLint windowHeight = static_cast<EGLint>(getWindow()->getHeight());
130         EGLDisplay display  = getDisplay();
131         EGLSurface surface  = getSurface();
132         eglPostSubBufferNV(display, surface, 60, 60, windowWidth - 120, windowHeight - 120);
133     }
134 
135   private:
136     // Handle to a program object
137     GLuint mProgram;
138 
139     // Attribute locations
140     GLint mPositionLoc;
141     GLint mTexcoordLoc;
142 
143     // Uniform locations
144     GLuint mMVPMatrixLoc;
145 
146     // Current rotation
147     float mRotation;
148 
149     // Geometry data
150     CubeGeometry mCube;
151 };
152 
main(int argc,char ** argv)153 int main(int argc, char **argv)
154 {
155     PostSubBufferSample app(argc, argv);
156     return app.run();
157 }
158