1 //
2 // Copyright 2017 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 // draw_call_perf_utils.cpp:
7 // Common utilities for performance tests that need to do a large amount of draw calls.
8 //
9
10 #include "draw_call_perf_utils.h"
11
12 #include <vector>
13
14 #include "util/shader_utils.h"
15
16 namespace
17 {
18 constexpr char kSimpleScaleAndOffsetVS[] = R"(attribute vec2 vPosition;
19 uniform float uScale;
20 uniform float uOffset;
21 void main()
22 {
23 gl_Position = vec4(vPosition * vec2(uScale) + vec2(uOffset), 0, 1);
24 })";
25
26 constexpr char kSimpleDrawVS[] = R"(attribute vec2 vPosition;
27 const float scale = 0.5;
28 const float offset = -0.5;
29
30 void main()
31 {
32 gl_Position = vec4(vPosition * vec2(scale) + vec2(offset), 0, 1);
33 })";
34
35 constexpr char kSimpleTexCoordVS[] = R"(attribute vec2 vPosition;
36 varying vec2 texCoord;
37 void main()
38 {
39 gl_Position = vec4(vPosition, 0, 1);
40 texCoord = vPosition * 0.5 + vec2(0.5);
41 })";
42
43 constexpr char kSimpleFS[] = R"(precision mediump float;
44 void main()
45 {
46 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
47 })";
48
49 constexpr char kSimpleTextureFS[] = R"(precision mediump float;
50 varying vec2 texCoord;
51 uniform sampler2D tex;
52 void main()
53 {
54 gl_FragColor = texture2D(tex, texCoord);
55 })";
56
57 constexpr char kDoubleTextureFS[] = R"(precision mediump float;
58 varying vec2 texCoord;
59 uniform sampler2D tex1;
60 uniform sampler2D tex2;
61 void main()
62 {
63 gl_FragColor = texture2D(tex1, texCoord) + texture2D(tex2, texCoord);
64 })";
65
66 constexpr char kEightTextureFS[] = R"(precision mediump float;
67 varying vec2 texCoord;
68 uniform sampler2D tex1;
69 uniform sampler2D tex2;
70 uniform sampler2D tex3;
71 uniform sampler2D tex4;
72 uniform sampler2D tex5;
73 uniform sampler2D tex6;
74 uniform sampler2D tex7;
75 uniform sampler2D tex8;
76 void main()
77 {
78 gl_FragColor = texture2D(tex1, texCoord) + texture2D(tex2, texCoord) +
79 texture2D(tex3, texCoord) + texture2D(tex4, texCoord) +
80 texture2D(tex5, texCoord) + texture2D(tex6, texCoord) +
81 texture2D(tex7, texCoord) + texture2D(tex8, texCoord);
82 })";
83
Generate2DTriangleData(size_t numTris,std::vector<float> * floatData)84 void Generate2DTriangleData(size_t numTris, std::vector<float> *floatData)
85 {
86 for (size_t triIndex = 0; triIndex < numTris; ++triIndex)
87 {
88 floatData->push_back(1.0f);
89 floatData->push_back(2.0f);
90
91 floatData->push_back(0.0f);
92 floatData->push_back(0.0f);
93
94 floatData->push_back(2.0f);
95 floatData->push_back(0.0f);
96 }
97 }
98
99 } // anonymous namespace
100
SetupSimpleScaleAndOffsetProgram()101 GLuint SetupSimpleScaleAndOffsetProgram()
102 {
103 GLuint program = CompileProgram(kSimpleScaleAndOffsetVS, kSimpleFS);
104 if (program == 0u)
105 {
106 return program;
107 }
108
109 // Use the program object
110 glUseProgram(program);
111
112 GLfloat scale = 0.5f;
113 GLfloat offset = -0.5f;
114
115 glUniform1f(glGetUniformLocation(program, "uScale"), scale);
116 glUniform1f(glGetUniformLocation(program, "uOffset"), offset);
117 return program;
118 }
119
SetupSimpleDrawProgram()120 GLuint SetupSimpleDrawProgram()
121 {
122 GLuint program = CompileProgram(kSimpleDrawVS, kSimpleFS);
123 if (program == 0u)
124 {
125 return program;
126 }
127
128 // Use the program object
129 glUseProgram(program);
130
131 return program;
132 }
133
SetupSimpleTextureProgram()134 GLuint SetupSimpleTextureProgram()
135 {
136 GLuint program = CompileProgram(kSimpleTexCoordVS, kSimpleTextureFS);
137 if (program == 0u)
138 {
139 return program;
140 }
141
142 // Use the program object
143 glUseProgram(program);
144
145 return program;
146 }
147
SetupDoubleTextureProgram()148 GLuint SetupDoubleTextureProgram()
149 {
150 GLuint program = CompileProgram(kSimpleTexCoordVS, kDoubleTextureFS);
151 if (program == 0u)
152 {
153 return program;
154 }
155
156 // Use the program object
157 glUseProgram(program);
158
159 return program;
160 }
161
SetupEightTextureProgram()162 GLuint SetupEightTextureProgram()
163 {
164 GLuint program = CompileProgram(kSimpleTexCoordVS, kEightTextureFS);
165 if (program == 0u)
166 {
167 return program;
168 }
169
170 // Use the program object
171 glUseProgram(program);
172
173 return program;
174 }
175
Create2DTriangleBuffer(size_t numTris,GLenum usage)176 GLuint Create2DTriangleBuffer(size_t numTris, GLenum usage)
177 {
178 GLuint buffer = 0u;
179 glGenBuffers(1, &buffer);
180 glBindBuffer(GL_ARRAY_BUFFER, buffer);
181
182 std::vector<GLfloat> floatData;
183 Generate2DTriangleData(numTris, &floatData);
184
185 // To avoid generating GL errors when testing validation-only with zero triangles.
186 if (floatData.empty())
187 {
188 floatData.push_back(0.0f);
189 }
190
191 glBufferData(GL_ARRAY_BUFFER, floatData.size() * sizeof(GLfloat), &floatData[0], usage);
192
193 return buffer;
194 }
195
CreateColorFBO(GLsizei width,GLsizei height,GLuint * fbo,GLuint * texture)196 void CreateColorFBO(GLsizei width, GLsizei height, GLuint *fbo, GLuint *texture)
197 {
198 glGenFramebuffers(1, fbo);
199 glBindFramebuffer(GL_FRAMEBUFFER, *fbo);
200 glGenTextures(1, texture);
201 glBindTexture(GL_TEXTURE_2D, *texture);
202 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
203 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *texture, 0);
204 }
205