Path: blob/main_old/src/tests/perf_tests/DynamicPromotionPerfTest.cpp
1693 views
//1// Copyright 2016 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// DynamicPromotionPerfTest:6// Tests that ANGLE will promote buffer specfied with DYNAMIC usage to static after a number of7// iterations without changing the data. It specifically affects the D3D back-end, which treats8// dynamic and static buffers quite differently.9//1011#include "ANGLEPerfTest.h"12#include "common/vector_utils.h"13#include "util/random_utils.h"14#include "util/shader_utils.h"1516using namespace angle;1718namespace19{20constexpr unsigned int kIterationsPerStep = 4;2122struct DynamicPromotionParams final : public RenderTestParams23{24DynamicPromotionParams() { iterationsPerStep = kIterationsPerStep; }2526std::string story() const override;2728size_t vertexCount = 1024;29};3031std::string DynamicPromotionParams::story() const32{33return RenderTestParams::story();34}3536std::ostream &operator<<(std::ostream &os, const DynamicPromotionParams ¶ms)37{38os << params.backendAndStory().substr(1);39return os;40}4142class DynamicPromotionPerfTest : public ANGLERenderTest,43public testing::WithParamInterface<DynamicPromotionParams>44{45public:46DynamicPromotionPerfTest();4748void initializeBenchmark() override;49void destroyBenchmark() override;50void drawBenchmark() override;5152private:53GLuint mProgram;54GLuint mElementArrayBuffer;55GLuint mArrayBuffer;56};5758DynamicPromotionPerfTest::DynamicPromotionPerfTest()59: ANGLERenderTest("DynamicPromotion", GetParam()),60mProgram(0),61mElementArrayBuffer(0),62mArrayBuffer(0)63{}6465void DynamicPromotionPerfTest::initializeBenchmark()66{67constexpr char kVertexShaderSource[] =68"attribute vec2 position;\n"69"attribute vec3 color;\n"70"varying vec3 vColor;\n"71"void main()\n"72"{\n"73" vColor = color;\n"74" gl_Position = vec4(position, 0, 1);\n"75"}";7677constexpr char kFragmentShaderSource[] =78"varying mediump vec3 vColor;\n"79"void main()\n"80"{\n"81" gl_FragColor = vec4(vColor, 1);\n"82"}";8384mProgram = CompileProgram(kVertexShaderSource, kFragmentShaderSource);85ASSERT_NE(0u, mProgram);8687const size_t vertexCount = GetParam().vertexCount;8889std::vector<GLushort> indexData;90std::vector<Vector2> positionData;91std::vector<Vector3> colorData;9293ASSERT_GE(static_cast<size_t>(std::numeric_limits<GLushort>::max()), vertexCount);9495RNG rng(1);9697for (size_t index = 0; index < vertexCount; ++index)98{99indexData.push_back(static_cast<GLushort>(index));100101Vector2 position(rng.randomNegativeOneToOne(), rng.randomNegativeOneToOne());102positionData.push_back(position);103104Vector3 color(rng.randomFloat(), rng.randomFloat(), rng.randomFloat());105colorData.push_back(color);106}107108glGenBuffers(1, &mElementArrayBuffer);109glGenBuffers(1, &mArrayBuffer);110111glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementArrayBuffer);112glBindBuffer(GL_ARRAY_BUFFER, mArrayBuffer);113114GLsizeiptr elementArraySize = sizeof(GLushort) * vertexCount;115GLsizeiptr positionArraySize = sizeof(Vector2) * vertexCount;116GLsizeiptr colorArraySize = sizeof(Vector3) * vertexCount;117118// The DYNAMIC_DRAW usage is the key to the test.119glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementArraySize, indexData.data(), GL_DYNAMIC_DRAW);120glBufferData(GL_ARRAY_BUFFER, positionArraySize + colorArraySize, nullptr, GL_DYNAMIC_DRAW);121glBufferSubData(GL_ARRAY_BUFFER, 0, positionArraySize, positionData.data());122glBufferSubData(GL_ARRAY_BUFFER, positionArraySize, colorArraySize, colorData.data());123124glUseProgram(mProgram);125GLint positionLocation = glGetAttribLocation(mProgram, "position");126ASSERT_NE(-1, positionLocation);127GLint colorLocation = glGetAttribLocation(mProgram, "color");128ASSERT_NE(-1, colorLocation);129130glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, nullptr);131glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0,132reinterpret_cast<const void *>(positionArraySize));133134glEnableVertexAttribArray(positionLocation);135glEnableVertexAttribArray(colorLocation);136137ASSERT_GL_NO_ERROR();138}139140void DynamicPromotionPerfTest::destroyBenchmark()141{142glDeleteProgram(mProgram);143glDeleteBuffers(1, &mElementArrayBuffer);144glDeleteBuffers(1, &mArrayBuffer);145}146147void DynamicPromotionPerfTest::drawBenchmark()148{149unsigned int iterations = GetParam().iterationsPerStep;150size_t vertexCount = GetParam().vertexCount;151152glClear(GL_COLOR_BUFFER_BIT);153for (unsigned int count = 0; count < iterations; ++count)154{155glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(vertexCount), GL_UNSIGNED_SHORT, nullptr);156}157158ASSERT_GL_NO_ERROR();159}160161DynamicPromotionParams DynamicPromotionD3D11Params()162{163DynamicPromotionParams params;164params.eglParameters = egl_platform::D3D11();165return params;166}167168TEST_P(DynamicPromotionPerfTest, Run)169{170run();171}172173ANGLE_INSTANTIATE_TEST(DynamicPromotionPerfTest, DynamicPromotionD3D11Params());174175// This test suite is not instantiated on some OSes.176GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DynamicPromotionPerfTest);177178} // anonymous namespace179180181