1 //
2 // Copyright 2018 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 // DispatchComputePerf:
7 //   Performance tests for ANGLE DispatchCompute call overhead.
8 //
9 
10 #include "ANGLEPerfTest.h"
11 #include "util/shader_utils.h"
12 
13 namespace
14 {
15 unsigned int kIterationsPerStep = 50;
16 
17 struct DispatchComputePerfParams final : public RenderTestParams
18 {
DispatchComputePerfParams__anon3803f1660111::DispatchComputePerfParams19     DispatchComputePerfParams()
20     {
21         iterationsPerStep = kIterationsPerStep;
22         majorVersion      = 3;
23         minorVersion      = 1;
24     }
25 
26     std::string story() const override;
27 
28     unsigned int localSizeX    = 16;
29     unsigned int localSizeY    = 16;
30     unsigned int textureWidth  = 32;
31     unsigned int textureHeight = 32;
32 };
33 
story() const34 std::string DispatchComputePerfParams::story() const
35 {
36     std::stringstream storyStr;
37     storyStr << RenderTestParams::story();
38 
39     if (eglParameters.deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE)
40     {
41         storyStr << "_null";
42     }
43     return storyStr.str();
44 }
45 
operator <<(std::ostream & os,const DispatchComputePerfParams & params)46 std::ostream &operator<<(std::ostream &os, const DispatchComputePerfParams &params)
47 {
48     os << params.backendAndStory().substr(1);
49     return os;
50 }
51 
52 class DispatchComputePerfBenchmark : public ANGLERenderTest,
53                                      public ::testing::WithParamInterface<DispatchComputePerfParams>
54 {
55   public:
56     DispatchComputePerfBenchmark();
57 
58     void initializeBenchmark() override;
59     void destroyBenchmark() override;
60     void drawBenchmark() override;
61 
62   private:
63     void initComputeShader();
64     void initTextures();
65 
66     GLuint mProgram      = 0;
67     GLuint mReadTexture  = 0;
68     GLuint mWriteTexture = 0;
69     GLuint mDispatchX    = 0;
70     GLuint mDispatchY    = 0;
71 };
72 
DispatchComputePerfBenchmark()73 DispatchComputePerfBenchmark::DispatchComputePerfBenchmark()
74     : ANGLERenderTest("DispatchComputePerf", GetParam())
75 {}
76 
initializeBenchmark()77 void DispatchComputePerfBenchmark::initializeBenchmark()
78 {
79     const auto &params = GetParam();
80 
81     initComputeShader();
82     initTextures();
83 
84     glUseProgram(mProgram);
85     glActiveTexture(GL_TEXTURE0);
86     glBindTexture(GL_TEXTURE_2D, mReadTexture);
87     glUniform1i(glGetUniformLocation(mProgram, "readTexture"), 0);
88     glBindImageTexture(4, mWriteTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);
89 
90     mDispatchX = params.textureWidth / params.localSizeX;
91     mDispatchY = params.textureHeight / params.localSizeY;
92     ASSERT_GL_NO_ERROR();
93 }
94 
initComputeShader()95 void DispatchComputePerfBenchmark::initComputeShader()
96 {
97     constexpr char kCS[] = R"(#version 310 es
98 #define LOCAL_SIZE_X 16
99 #define LOCAL_SIZE_Y 16
100 layout(local_size_x=LOCAL_SIZE_X, local_size_y=LOCAL_SIZE_Y) in;
101 precision highp float;
102 uniform sampler2D readTexture;
103 layout(r32f, binding = 4) writeonly uniform highp image2D  outImage;
104 
105 void main() {
106     float sum = 0.;
107     sum += texelFetch(readTexture, ivec2(gl_GlobalInvocationID.xy), 0).r;
108     imageStore(outImage, ivec2(gl_GlobalInvocationID.xy), vec4(sum));
109 })";
110 
111     mProgram = CompileComputeProgram(kCS, false);
112     ASSERT_NE(0u, mProgram);
113 }
114 
initTextures()115 void DispatchComputePerfBenchmark::initTextures()
116 {
117     const auto &params = GetParam();
118 
119     unsigned int textureDataSize = params.textureWidth * params.textureHeight;
120     std::vector<GLfloat> textureInputData(textureDataSize, 0.2f);
121     std::vector<GLfloat> textureOutputData(textureDataSize, 0.1f);
122 
123     glGenTextures(1, &mReadTexture);
124     glBindTexture(GL_TEXTURE_2D, mReadTexture);
125     glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, params.textureWidth, params.textureHeight, 0, GL_RED,
126                  GL_FLOAT, textureInputData.data());
127     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
128     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
129     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
130     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
131 
132     glGenTextures(1, &mWriteTexture);
133     glBindTexture(GL_TEXTURE_2D, mWriteTexture);
134     glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, params.textureWidth, params.textureHeight);
135     glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, params.textureWidth, params.textureHeight, GL_RED,
136                     GL_FLOAT, textureOutputData.data());
137     ASSERT_GL_NO_ERROR();
138 }
139 
destroyBenchmark()140 void DispatchComputePerfBenchmark::destroyBenchmark()
141 {
142     glDeleteProgram(mProgram);
143     glDeleteTextures(1, &mReadTexture);
144     glDeleteTextures(1, &mWriteTexture);
145 }
146 
drawBenchmark()147 void DispatchComputePerfBenchmark::drawBenchmark()
148 {
149     const auto &params = GetParam();
150     for (unsigned int it = 0; it < params.iterationsPerStep; it++)
151     {
152         glDispatchCompute(mDispatchX, mDispatchY, 1);
153         glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);
154     }
155     ASSERT_GL_NO_ERROR();
156 }
157 
DispatchComputePerfOpenGLOrGLESParams(bool useNullDevice)158 DispatchComputePerfParams DispatchComputePerfOpenGLOrGLESParams(bool useNullDevice)
159 {
160     DispatchComputePerfParams params;
161     params.eglParameters = useNullDevice ? angle::egl_platform::OPENGL_OR_GLES_NULL()
162                                          : angle::egl_platform::OPENGL_OR_GLES();
163     return params;
164 }
165 
TEST_P(DispatchComputePerfBenchmark,Run)166 TEST_P(DispatchComputePerfBenchmark, Run)
167 {
168     run();
169 }
170 
171 ANGLE_INSTANTIATE_TEST(DispatchComputePerfBenchmark,
172                        DispatchComputePerfOpenGLOrGLESParams(true),
173                        DispatchComputePerfOpenGLOrGLESParams(false));
174 
175 }  // namespace
176