Path: blob/main_old/src/tests/perf_tests/PointSprites.cpp
1693 views
//1// Copyright 2014 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// PointSpritesBenchmark:6// Performance test for ANGLE point sprites.7//8//9#include "ANGLEPerfTest.h"1011#include <iostream>12#include <sstream>1314#include "util/random_utils.h"15#include "util/shader_utils.h"1617using namespace angle;1819namespace20{21constexpr unsigned int kIterationsPerStep = 100;2223struct PointSpritesParams final : public RenderTestParams24{25PointSpritesParams()26{27iterationsPerStep = kIterationsPerStep;2829// Common default params30majorVersion = 2;31minorVersion = 0;32windowWidth = 1280;33windowHeight = 720;34count = 10;35size = 3.0f;36numVaryings = 3;37}3839std::string story() const override;4041unsigned int count;42float size;43unsigned int numVaryings;44};4546std::ostream &operator<<(std::ostream &os, const PointSpritesParams ¶ms)47{48os << params.backendAndStory().substr(1);49return os;50}5152class PointSpritesBenchmark : public ANGLERenderTest,53public ::testing::WithParamInterface<PointSpritesParams>54{55public:56PointSpritesBenchmark();5758void initializeBenchmark() override;59void destroyBenchmark() override;60void drawBenchmark() override;6162private:63GLuint mProgram;64GLuint mBuffer;65RNG mRNG;66};6768std::string PointSpritesParams::story() const69{70std::stringstream strstr;7172strstr << RenderTestParams::story() << "_" << count << "_" << size << "px"73<< "_" << numVaryings << "vars";7475return strstr.str();76}7778PointSpritesBenchmark::PointSpritesBenchmark()79: ANGLERenderTest("PointSprites", GetParam()), mRNG(1)80{}8182void PointSpritesBenchmark::initializeBenchmark()83{84const auto ¶ms = GetParam();8586std::stringstream vstrstr;8788// Verify "numVaryings" is within MAX_VARYINGS limit89GLint maxVaryings;90glGetIntegerv(GL_MAX_VARYING_VECTORS, &maxVaryings);9192if (params.numVaryings > static_cast<unsigned int>(maxVaryings))93{94FAIL() << "Varying count (" << params.numVaryings << ")"95<< " exceeds maximum varyings: " << maxVaryings << std::endl;96}9798vstrstr << "attribute vec2 vPosition;\n"99"uniform float uPointSize;\n";100101for (unsigned int varCount = 0; varCount < params.numVaryings; varCount++)102{103vstrstr << "varying vec4 v" << varCount << ";\n";104}105106vstrstr << "void main()\n"107"{\n";108109for (unsigned int varCount = 0; varCount < params.numVaryings; varCount++)110{111vstrstr << " v" << varCount << " = vec4(1.0);\n";112}113114vstrstr << " gl_Position = vec4(vPosition, 0, 1.0);\n"115" gl_PointSize = uPointSize;\n"116"}";117118std::stringstream fstrstr;119120fstrstr << "precision mediump float;\n";121122for (unsigned int varCount = 0; varCount < params.numVaryings; varCount++)123{124fstrstr << "varying vec4 v" << varCount << ";\n";125}126127fstrstr << "void main()\n"128"{\n"129" vec4 colorOut = vec4(1.0, 0.0, 0.0, 1.0);\n";130131for (unsigned int varCount = 0; varCount < params.numVaryings; varCount++)132{133fstrstr << " colorOut.r += v" << varCount << ".r;\n";134}135136fstrstr << " gl_FragColor = colorOut;\n"137"}\n";138139mProgram = CompileProgram(vstrstr.str().c_str(), fstrstr.str().c_str());140ASSERT_NE(0u, mProgram);141142// Use the program object143glUseProgram(mProgram);144145glClearColor(0.0f, 0.0f, 0.0f, 0.0f);146147std::vector<float> vertexPositions(params.count * 2);148for (size_t pointIndex = 0; pointIndex < vertexPositions.size(); ++pointIndex)149{150vertexPositions[pointIndex] = mRNG.randomNegativeOneToOne();151}152153glGenBuffers(1, &mBuffer);154glBindBuffer(GL_ARRAY_BUFFER, mBuffer);155glBufferData(GL_ARRAY_BUFFER, vertexPositions.size() * sizeof(float), &vertexPositions[0],156GL_STATIC_DRAW);157158GLint positionLocation = glGetAttribLocation(mProgram, "vPosition");159ASSERT_NE(-1, positionLocation);160161glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, nullptr);162glEnableVertexAttribArray(positionLocation);163164// Set the viewport165glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());166167GLint pointSizeLocation = glGetUniformLocation(mProgram, "uPointSize");168ASSERT_NE(-1, pointSizeLocation);169170glUniform1f(pointSizeLocation, params.size);171172ASSERT_GL_NO_ERROR();173}174175void PointSpritesBenchmark::destroyBenchmark()176{177glDeleteProgram(mProgram);178glDeleteBuffers(1, &mBuffer);179}180181void PointSpritesBenchmark::drawBenchmark()182{183glClear(GL_COLOR_BUFFER_BIT);184185const auto ¶ms = GetParam();186187for (unsigned int it = 0; it < params.iterationsPerStep; it++)188{189// TODO(jmadill): Indexed point rendering. ANGLE is bad at this.190glDrawArrays(GL_POINTS, 0, params.count);191}192193ASSERT_GL_NO_ERROR();194}195196PointSpritesParams D3D11Params()197{198PointSpritesParams params;199params.eglParameters = egl_platform::D3D11();200return params;201}202203PointSpritesParams OpenGLOrGLESParams()204{205PointSpritesParams params;206params.eglParameters = egl_platform::OPENGL_OR_GLES();207return params;208}209210PointSpritesParams VulkanParams()211{212PointSpritesParams params;213params.eglParameters = egl_platform::VULKAN();214return params;215}216217} // namespace218219TEST_P(PointSpritesBenchmark, Run)220{221run();222}223224ANGLE_INSTANTIATE_TEST(PointSpritesBenchmark, D3D11Params(), OpenGLOrGLESParams(), VulkanParams());225226227