Path: blob/main_old/src/tests/perf_tests/FramebufferAttachmentPerfTest.cpp
1693 views
//1// Copyright 2021 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// FramebufferAttachPerfTest:6// Performance test for attaching and detaching resources to a Framebuffer.7//89#include "ANGLEPerfTest.h"10#include "test_utils/gl_raii.h"1112#include <iostream>13#include <random>14#include <sstream>1516namespace angle17{18constexpr unsigned int kIterationsPerStep = 256;19constexpr unsigned int kTextureSize = 256;20constexpr std::size_t kTextureCount = 4;21constexpr std::size_t kFboCount = kTextureCount;22constexpr std::size_t kAdditionalFboCount = kFboCount * kFboCount;2324struct FramebufferAttachmentParams final : public RenderTestParams25{26FramebufferAttachmentParams()27{28iterationsPerStep = kIterationsPerStep;2930// Common default params31majorVersion = 3;32minorVersion = 0;33windowWidth = kTextureSize;34windowHeight = kTextureSize;35}3637std::string story() const override;38};3940std::ostream &operator<<(std::ostream &os, const FramebufferAttachmentParams ¶ms)41{42os << params.backendAndStory().substr(1);43return os;44}4546std::string FramebufferAttachmentParams::story() const47{48std::stringstream strstr;4950strstr << RenderTestParams::story();5152return strstr.str();53}5455class FramebufferAttachmentBenchmark56: public ANGLERenderTest,57public ::testing::WithParamInterface<FramebufferAttachmentParams>58{59public:60FramebufferAttachmentBenchmark() : ANGLERenderTest("Framebuffers", GetParam()) {}61void initializeBenchmark() override;62void drawBenchmark() override;6364protected:65void initTextures();6667std::array<GLTexture, kTextureCount> mTextures;68std::array<GLFramebuffer, kFboCount> mFbo;69};7071void FramebufferAttachmentBenchmark::initializeBenchmark()72{73initTextures();7475glClearColor(0.0f, 0.0f, 0.0f, 0.0f);76glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());7778ASSERT_GL_NO_ERROR();7980GLint maxAttachmentCount;81glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttachmentCount);82if (mTextures.size() > static_cast<size_t>(maxAttachmentCount))83{84// Texture count exceeds maximum attachment unit count, skip the test85mSkipTest = true;86}87}8889void FramebufferAttachmentBenchmark::initTextures()90{91std::vector<GLubyte> textureData(kTextureSize * kTextureSize * 4);92for (auto &byte : textureData)93{94byte = rand() % 255u;95}9697for (GLTexture &texture : mTextures)98{99glBindTexture(GL_TEXTURE_2D, texture);100glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTextureSize, kTextureSize, 0, GL_RGBA,101GL_UNSIGNED_BYTE, textureData.data());102glBindTexture(GL_TEXTURE_2D, 0);103}104}105106void FramebufferAttachmentBenchmark::drawBenchmark()107{108const auto ¶ms = GetParam();109110size_t fboCount = mFbo.size();111size_t textureCount = mTextures.size();112113for (size_t it = 0; it < params.iterationsPerStep; ++it)114{115// Attach116for (size_t fboIndex = 0; fboIndex < fboCount; fboIndex++)117{118glBindFramebuffer(GL_FRAMEBUFFER, mFbo[fboIndex]);119for (size_t textureIndex = 0; textureIndex < textureCount; textureIndex++)120{121glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + textureIndex,122GL_TEXTURE_2D, mTextures[textureIndex], 0);123}124glBindFramebuffer(GL_FRAMEBUFFER, 0);125}126127// Detach128for (size_t fboIndex = 0; fboIndex < fboCount; fboIndex++)129{130glBindFramebuffer(GL_FRAMEBUFFER, mFbo[fboIndex]);131for (size_t index = 0; index < textureCount; index++)132{133size_t textureIndex = mTextures.size() - (index + 1);134glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + textureIndex,135GL_TEXTURE_2D, 0, 0);136}137glBindFramebuffer(GL_FRAMEBUFFER, 0);138}139}140141ASSERT_GL_NO_ERROR();142}143144class FramebufferAttachmentStateUpdateBenchmark : public FramebufferAttachmentBenchmark145{146public:147FramebufferAttachmentStateUpdateBenchmark() : FramebufferAttachmentBenchmark() {}148void initializeBenchmark() override;149void destroyBenchmark() override;150void drawBenchmark() override;151152private:153std::array<GLFramebuffer, kAdditionalFboCount> mAdditionalFbo;154};155156void FramebufferAttachmentStateUpdateBenchmark::initializeBenchmark()157{158FramebufferAttachmentBenchmark::initializeBenchmark();159160// Attach161for (size_t fboIndex = 0; fboIndex < mAdditionalFbo.size(); fboIndex++)162{163glBindFramebuffer(GL_FRAMEBUFFER, mAdditionalFbo[fboIndex]);164for (size_t textureIndex = 0; textureIndex < mTextures.size(); textureIndex++)165{166glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + textureIndex,167GL_TEXTURE_2D, mTextures[textureIndex], 0);168}169glBindFramebuffer(GL_FRAMEBUFFER, 0);170}171}172173void FramebufferAttachmentStateUpdateBenchmark::destroyBenchmark()174{175// Detach176for (size_t fboIndex = 0; fboIndex < mAdditionalFbo.size(); fboIndex++)177{178glBindFramebuffer(GL_FRAMEBUFFER, mAdditionalFbo[fboIndex]);179for (size_t index = 0; index < mTextures.size(); index++)180{181glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_2D, 0,1820);183}184glBindFramebuffer(GL_FRAMEBUFFER, 0);185}186187FramebufferAttachmentBenchmark::destroyBenchmark();188}189190void FramebufferAttachmentStateUpdateBenchmark::drawBenchmark()191{192const auto ¶ms = GetParam();193194size_t textureCount = mTextures.size();195GLenum nearestFilter = GL_NEAREST;196GLenum linearFilter = GL_LINEAR;197for (size_t it = 0; it < params.iterationsPerStep; ++it)198{199for (size_t textureIndex = 0; textureIndex < textureCount; textureIndex++)200{201glBindTexture(GL_TEXTURE_2D, mTextures[textureIndex]);202glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,203(it % 2) ? linearFilter : nearestFilter);204glBindTexture(GL_TEXTURE_2D, 0);205}206}207208ASSERT_GL_NO_ERROR();209}210211FramebufferAttachmentParams VulkanParams()212{213FramebufferAttachmentParams params;214params.eglParameters = egl_platform::VULKAN_NULL();215216return params;217}218219TEST_P(FramebufferAttachmentBenchmark, Run)220{221run();222}223224TEST_P(FramebufferAttachmentStateUpdateBenchmark, Run)225{226run();227}228229GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FramebufferAttachmentBenchmark);230ANGLE_INSTANTIATE_TEST(FramebufferAttachmentBenchmark, VulkanParams());231232GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(FramebufferAttachmentStateUpdateBenchmark);233ANGLE_INSTANTIATE_TEST(FramebufferAttachmentStateUpdateBenchmark, VulkanParams());234} // namespace angle235236237