Path: blob/main_old/src/tests/perf_tests/DispatchComputePerf.cpp
1693 views
//1// Copyright 2018 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// DispatchComputePerf:6// Performance tests for ANGLE DispatchCompute call overhead.7//89#include "ANGLEPerfTest.h"10#include "util/shader_utils.h"1112namespace13{14unsigned int kIterationsPerStep = 50;1516struct DispatchComputePerfParams final : public RenderTestParams17{18DispatchComputePerfParams()19{20iterationsPerStep = kIterationsPerStep;21majorVersion = 3;22minorVersion = 1;23}2425std::string story() const override;2627unsigned int localSizeX = 16;28unsigned int localSizeY = 16;29unsigned int textureWidth = 32;30unsigned int textureHeight = 32;31};3233std::string DispatchComputePerfParams::story() const34{35std::stringstream storyStr;36storyStr << RenderTestParams::story();3738if (eglParameters.deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE)39{40storyStr << "_null";41}42return storyStr.str();43}4445std::ostream &operator<<(std::ostream &os, const DispatchComputePerfParams ¶ms)46{47os << params.backendAndStory().substr(1);48return os;49}5051class DispatchComputePerfBenchmark : public ANGLERenderTest,52public ::testing::WithParamInterface<DispatchComputePerfParams>53{54public:55DispatchComputePerfBenchmark();5657void initializeBenchmark() override;58void destroyBenchmark() override;59void drawBenchmark() override;6061private:62void initComputeShader();63void initTextures();6465GLuint mProgram = 0;66GLuint mReadTexture = 0;67GLuint mWriteTexture = 0;68GLuint mDispatchX = 0;69GLuint mDispatchY = 0;70};7172DispatchComputePerfBenchmark::DispatchComputePerfBenchmark()73: ANGLERenderTest("DispatchComputePerf", GetParam())74{}7576void DispatchComputePerfBenchmark::initializeBenchmark()77{78const auto ¶ms = GetParam();7980initComputeShader();81initTextures();8283glUseProgram(mProgram);84glActiveTexture(GL_TEXTURE0);85glBindTexture(GL_TEXTURE_2D, mReadTexture);86glUniform1i(glGetUniformLocation(mProgram, "readTexture"), 0);87glBindImageTexture(4, mWriteTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);8889mDispatchX = params.textureWidth / params.localSizeX;90mDispatchY = params.textureHeight / params.localSizeY;91ASSERT_GL_NO_ERROR();92}9394void DispatchComputePerfBenchmark::initComputeShader()95{96constexpr char kCS[] = R"(#version 310 es97#define LOCAL_SIZE_X 1698#define LOCAL_SIZE_Y 1699layout(local_size_x=LOCAL_SIZE_X, local_size_y=LOCAL_SIZE_Y) in;100precision highp float;101uniform sampler2D readTexture;102layout(r32f, binding = 4) writeonly uniform highp image2D outImage;103104void main() {105float sum = 0.;106sum += texelFetch(readTexture, ivec2(gl_GlobalInvocationID.xy), 0).r;107imageStore(outImage, ivec2(gl_GlobalInvocationID.xy), vec4(sum));108})";109110mProgram = CompileComputeProgram(kCS, false);111ASSERT_NE(0u, mProgram);112}113114void DispatchComputePerfBenchmark::initTextures()115{116const auto ¶ms = GetParam();117118unsigned int textureDataSize = params.textureWidth * params.textureHeight;119std::vector<GLfloat> textureInputData(textureDataSize, 0.2f);120std::vector<GLfloat> textureOutputData(textureDataSize, 0.1f);121122glGenTextures(1, &mReadTexture);123glBindTexture(GL_TEXTURE_2D, mReadTexture);124glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, params.textureWidth, params.textureHeight, 0, GL_RED,125GL_FLOAT, textureInputData.data());126glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);127glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);128glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);129glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);130131glGenTextures(1, &mWriteTexture);132glBindTexture(GL_TEXTURE_2D, mWriteTexture);133glTexStorage2D(GL_TEXTURE_2D, 1, GL_R32F, params.textureWidth, params.textureHeight);134glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, params.textureWidth, params.textureHeight, GL_RED,135GL_FLOAT, textureOutputData.data());136ASSERT_GL_NO_ERROR();137}138139void DispatchComputePerfBenchmark::destroyBenchmark()140{141glDeleteProgram(mProgram);142glDeleteTextures(1, &mReadTexture);143glDeleteTextures(1, &mWriteTexture);144}145146void DispatchComputePerfBenchmark::drawBenchmark()147{148const auto ¶ms = GetParam();149for (unsigned int it = 0; it < params.iterationsPerStep; it++)150{151glDispatchCompute(mDispatchX, mDispatchY, 1);152glMemoryBarrier(GL_TEXTURE_FETCH_BARRIER_BIT);153}154ASSERT_GL_NO_ERROR();155}156157DispatchComputePerfParams DispatchComputePerfOpenGLOrGLESParams(bool useNullDevice)158{159DispatchComputePerfParams params;160params.eglParameters = useNullDevice ? angle::egl_platform::OPENGL_OR_GLES_NULL()161: angle::egl_platform::OPENGL_OR_GLES();162return params;163}164165TEST_P(DispatchComputePerfBenchmark, Run)166{167run();168}169170GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DispatchComputePerfBenchmark);171ANGLE_INSTANTIATE_TEST(DispatchComputePerfBenchmark,172DispatchComputePerfOpenGLOrGLESParams(true),173DispatchComputePerfOpenGLOrGLESParams(false));174175} // namespace176177178