forked from google/angle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureSampling.cpp
355 lines (295 loc) · 10.3 KB
/
TextureSampling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// TextureSamplingBenchmark:
// Performance test for texture sampling. The test generates a texture containing random data
// and then blurs it in a fragment shader using nearest neighbor sampling. The test is
// specifically designed to test overhead of GLSL's builtin texture*() functions that may result
// from how ANGLE translates them on each backend.
//
#include "ANGLEPerfTest.h"
#include <iostream>
#include <random>
#include <sstream>
#include "util/shader_utils.h"
using namespace angle;
namespace
{
constexpr unsigned int kIterationsPerStep = 4;
struct TextureSamplingParams final : public RenderTestParams
{
TextureSamplingParams()
{
iterationsPerStep = kIterationsPerStep;
// Common default params
majorVersion = 3;
minorVersion = 0;
windowWidth = 720;
windowHeight = 720;
numSamplers = 2;
textureSize = 32;
kernelSize = 3;
}
std::string story() const override;
unsigned int numSamplers;
unsigned int textureSize;
unsigned int kernelSize;
};
std::ostream &operator<<(std::ostream &os, const TextureSamplingParams ¶ms)
{
os << params.backendAndStory().substr(1);
return os;
}
std::string TextureSamplingParams::story() const
{
std::stringstream strstr;
strstr << RenderTestParams::story() << "_" << numSamplers << "samplers";
return strstr.str();
}
class TextureSamplingBenchmark : public ANGLERenderTest,
public ::testing::WithParamInterface<TextureSamplingParams>
{
public:
TextureSamplingBenchmark();
void initializeBenchmark() override;
void destroyBenchmark() override;
void drawBenchmark() override;
protected:
void initShaders();
void initVertexBuffer();
void initTextures();
GLuint mProgram;
GLuint mBuffer;
std::vector<GLuint> mTextures;
};
TextureSamplingBenchmark::TextureSamplingBenchmark()
: ANGLERenderTest("TextureSampling", GetParam()), mProgram(0u), mBuffer(0u)
{}
void TextureSamplingBenchmark::initializeBenchmark()
{
const auto ¶ms = GetParam();
// Verify "numSamplers" is within MAX_TEXTURE_IMAGE_UNITS limit
GLint maxTextureImageUnits;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextureImageUnits);
if (params.numSamplers > static_cast<unsigned int>(maxTextureImageUnits))
{
FAIL() << "Sampler count (" << params.numSamplers << ")"
<< " exceeds maximum texture count: " << maxTextureImageUnits << std::endl;
}
initShaders();
initVertexBuffer();
initTextures();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
ASSERT_GL_NO_ERROR();
}
void TextureSamplingBenchmark::initShaders()
{
const auto ¶ms = GetParam();
std::stringstream vstrstr;
vstrstr << "attribute vec2 aPosition;\n"
"varying vec2 vTextureCoordinates;\n"
"void main()\n"
"{\n"
" vTextureCoordinates = (aPosition + vec2(1.0)) * 0.5;\n"
" gl_Position = vec4(aPosition, 0, 1.0);\n"
"}";
std::stringstream fstrstr;
fstrstr << "precision mediump float;\n"
"varying vec2 vTextureCoordinates;\n";
for (unsigned int count = 0; count < params.numSamplers; count++)
{
fstrstr << "uniform sampler2D uSampler" << count << ";\n";
}
fstrstr << "void main()\n"
"{\n"
" const float inverseTextureSize = 1.0 / "
<< params.textureSize
<< ".0;\n"
" vec4 colorOut = vec4(0.0, 0.0, 0.0, 1.0);\n";
for (unsigned int count = 0; count < params.numSamplers; count++)
{
fstrstr << " for (int x = 0; x < " << params.kernelSize
<< "; ++x)\n"
" {\n"
" for (int y = 0; y < "
<< params.kernelSize
<< "; ++y)\n"
" {\n"
" colorOut += texture2D(uSampler"
<< count
<< ", vTextureCoordinates + vec2(x, y) * inverseTextureSize) * 0.1;\n"
" }\n"
" }\n";
}
fstrstr << " gl_FragColor = colorOut;\n"
"}\n";
mProgram = CompileProgram(vstrstr.str().c_str(), fstrstr.str().c_str());
ASSERT_NE(0u, mProgram);
// Use the program object
glUseProgram(mProgram);
}
void TextureSamplingBenchmark::initVertexBuffer()
{
std::vector<float> vertexPositions(12);
{
// Bottom left triangle
vertexPositions[0] = -1.0f;
vertexPositions[1] = -1.0f;
vertexPositions[2] = 1.0f;
vertexPositions[3] = -1.0f;
vertexPositions[4] = -1.0f;
vertexPositions[5] = 1.0f;
// Top right triangle
vertexPositions[6] = -1.0f;
vertexPositions[7] = 1.0f;
vertexPositions[8] = 1.0f;
vertexPositions[9] = -1.0f;
vertexPositions[10] = 1.0f;
vertexPositions[11] = 1.0f;
}
glGenBuffers(1, &mBuffer);
glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
glBufferData(GL_ARRAY_BUFFER, vertexPositions.size() * sizeof(float), &vertexPositions[0],
GL_STATIC_DRAW);
GLint positionLocation = glGetAttribLocation(mProgram, "aPosition");
ASSERT_NE(-1, positionLocation);
glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(positionLocation);
}
void TextureSamplingBenchmark::initTextures()
{
const auto ¶ms = GetParam();
unsigned int dataSize = params.textureSize * params.textureSize;
std::vector<unsigned int> randomTextureData;
randomTextureData.resize(dataSize);
unsigned int pseudoRandom = 1u;
for (unsigned int i = 0; i < dataSize; ++i)
{
pseudoRandom = pseudoRandom * 1664525u + 1013904223u;
randomTextureData[i] = pseudoRandom;
}
mTextures.resize(params.numSamplers);
glGenTextures(params.numSamplers, mTextures.data());
for (unsigned int i = 0; i < params.numSamplers; ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, mTextures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, params.textureSize, params.textureSize, 0, GL_RGBA,
GL_UNSIGNED_BYTE, randomTextureData.data());
}
for (unsigned int count = 0; count < params.numSamplers; count++)
{
std::stringstream samplerstrstr;
samplerstrstr << "uSampler" << count;
GLint samplerLocation = glGetUniformLocation(mProgram, samplerstrstr.str().c_str());
ASSERT_NE(-1, samplerLocation);
glUniform1i(samplerLocation, count);
}
}
void TextureSamplingBenchmark::destroyBenchmark()
{
const auto ¶ms = GetParam();
glDeleteProgram(mProgram);
glDeleteBuffers(1, &mBuffer);
if (!mTextures.empty())
{
glDeleteTextures(params.numSamplers, mTextures.data());
}
}
void TextureSamplingBenchmark::drawBenchmark()
{
glClear(GL_COLOR_BUFFER_BIT);
const auto ¶ms = GetParam();
for (unsigned int it = 0; it < params.iterationsPerStep; ++it)
{
glDrawArrays(GL_TRIANGLES, 0, 6);
}
ASSERT_GL_NO_ERROR();
}
// Identical to TextureSamplingBenchmark, but enables and then disables
// EXT_texture_format_sRGB_override during initialization. This should force the internal texture
// representation to use VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, which is expected to carry a
// performance penalty. This penalty can be quantified by comparing the results of this test with
// the results from TextureSamplingBenchmark
class TextureSamplingMutableFormatBenchmark : public TextureSamplingBenchmark
{
public:
void initializeBenchmark() override;
protected:
void initTextures();
};
void TextureSamplingMutableFormatBenchmark::initializeBenchmark()
{
if (IsGLExtensionEnabled("GL_EXT_texture_format_sRGB_override"))
{
TextureSamplingBenchmark::initializeBenchmark();
initTextures();
}
}
void TextureSamplingMutableFormatBenchmark::initTextures()
{
TextureSamplingBenchmark::initTextures();
for (unsigned int i = 0; i < mTextures.size(); ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, mTextures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_FORMAT_SRGB_OVERRIDE_EXT, GL_SRGB);
}
// Force a state update
drawBenchmark();
for (unsigned int i = 0; i < mTextures.size(); ++i)
{
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, mTextures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_FORMAT_SRGB_OVERRIDE_EXT, GL_NONE);
}
}
TextureSamplingParams D3D11Params()
{
TextureSamplingParams params;
params.eglParameters = egl_platform::D3D11();
return params;
}
TextureSamplingParams MetalParams()
{
TextureSamplingParams params;
params.eglParameters = egl_platform::METAL();
return params;
}
TextureSamplingParams OpenGLOrGLESParams()
{
TextureSamplingParams params;
params.eglParameters = egl_platform::OPENGL_OR_GLES();
return params;
}
TextureSamplingParams VulkanParams()
{
TextureSamplingParams params;
params.eglParameters = egl_platform::VULKAN();
return params;
}
} // anonymous namespace
TEST_P(TextureSamplingBenchmark, Run)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_texture_format_sRGB_override"));
run();
}
TEST_P(TextureSamplingMutableFormatBenchmark, Run)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_texture_format_sRGB_override"));
run();
}
ANGLE_INSTANTIATE_TEST(TextureSamplingBenchmark,
D3D11Params(),
MetalParams(),
OpenGLOrGLESParams(),
VulkanParams());
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(TextureSamplingMutableFormatBenchmark);
ANGLE_INSTANTIATE_TEST(TextureSamplingMutableFormatBenchmark, VulkanParams());