Path: blob/main_old/src/tests/perf_tests/InterleavedAttributeData.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// InterleavedAttributeData:6// Performance test for draws using interleaved attribute data in vertex buffers.7//89#include <sstream>1011#include "ANGLEPerfTest.h"12#include "util/shader_utils.h"1314using namespace angle;1516namespace17{1819struct InterleavedAttributeDataParams final : public RenderTestParams20{21InterleavedAttributeDataParams()22{23iterationsPerStep = 1;2425// Common default values26majorVersion = 2;27minorVersion = 0;28windowWidth = 512;29windowHeight = 512;30numSprites = 3000;31}3233// static parameters34unsigned int numSprites;35};3637std::ostream &operator<<(std::ostream &os, const InterleavedAttributeDataParams ¶ms)38{39os << params.backendAndStory().substr(1);4041if (params.eglParameters.majorVersion != EGL_DONT_CARE)42{43os << "_" << params.eglParameters.majorVersion << "_" << params.eglParameters.minorVersion;44}4546return os;47}4849class InterleavedAttributeDataBenchmark50: public ANGLERenderTest,51public ::testing::WithParamInterface<InterleavedAttributeDataParams>52{53public:54InterleavedAttributeDataBenchmark();5556void initializeBenchmark() override;57void destroyBenchmark() override;58void drawBenchmark() override;5960private:61GLuint mPointSpriteProgram;62GLuint mPositionColorBuffer[2];6364// The buffers contain two floats and 3 unsigned bytes per point sprite65// Has to be aligned for float access on arm66const size_t mBytesPerSpriteUnaligned = 2 * sizeof(float) + 3;67const size_t mBytesPerSprite =68((mBytesPerSpriteUnaligned + sizeof(float) - 1) / sizeof(float)) * sizeof(float);69};7071InterleavedAttributeDataBenchmark::InterleavedAttributeDataBenchmark()72: ANGLERenderTest("InterleavedAttributeData", GetParam()), mPointSpriteProgram(0)73{74// Timing out on Intel. http://crbug.com/92100475if (GetParam().eglParameters.renderer == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)76{77mSkipTest = true;78}79}8081void InterleavedAttributeDataBenchmark::initializeBenchmark()82{83const auto ¶ms = GetParam();8485// Compile point sprite shaders86constexpr char kVS[] =87"attribute vec4 aPosition;"88"attribute vec4 aColor;"89"varying vec4 vColor;"90"void main()"91"{"92" gl_PointSize = 25.0;"93" gl_Position = aPosition;"94" vColor = aColor;"95"}";9697constexpr char kFS[] =98"precision mediump float;"99"varying vec4 vColor;"100"void main()"101"{"102" gl_FragColor = vColor;"103"}";104105mPointSpriteProgram = CompileProgram(kVS, kFS);106ASSERT_NE(0u, mPointSpriteProgram);107108glClearColor(0.0f, 1.0f, 0.0f, 1.0f);109110for (size_t i = 0; i < ArraySize(mPositionColorBuffer); i++)111{112// Set up initial data for pointsprite positions and colors113std::vector<uint8_t> positionColorData(mBytesPerSprite * params.numSprites);114for (unsigned int j = 0; j < params.numSprites; j++)115{116float pointSpriteX =117(static_cast<float>(rand() % getWindow()->getWidth()) / getWindow()->getWidth()) *1182.0f -1191.0f;120float pointSpriteY =121(static_cast<float>(rand() % getWindow()->getHeight()) / getWindow()->getHeight()) *1222.0f -1231.0f;124GLubyte pointSpriteRed = static_cast<GLubyte>(rand() % 255);125GLubyte pointSpriteGreen = static_cast<GLubyte>(rand() % 255);126GLubyte pointSpriteBlue = static_cast<GLubyte>(rand() % 255);127128// Add position data for the pointsprite129*reinterpret_cast<float *>(130&(positionColorData[j * mBytesPerSprite + 0 * sizeof(float) + 0])) =131pointSpriteX; // X132*reinterpret_cast<float *>(133&(positionColorData[j * mBytesPerSprite + 1 * sizeof(float) + 0])) =134pointSpriteY; // Y135136// Add color data for the pointsprite137positionColorData[j * mBytesPerSprite + 2 * sizeof(float) + 0] = pointSpriteRed; // R138positionColorData[j * mBytesPerSprite + 2 * sizeof(float) + 1] = pointSpriteGreen; // G139positionColorData[j * mBytesPerSprite + 2 * sizeof(float) + 2] = pointSpriteBlue; // B140}141142// Generate the GL buffer with the position/color data143glGenBuffers(1, &mPositionColorBuffer[i]);144glBindBuffer(GL_ARRAY_BUFFER, mPositionColorBuffer[i]);145glBufferData(GL_ARRAY_BUFFER, params.numSprites * mBytesPerSprite, &(positionColorData[0]),146GL_STATIC_DRAW);147}148149ASSERT_GL_NO_ERROR();150}151152void InterleavedAttributeDataBenchmark::destroyBenchmark()153{154glDeleteProgram(mPointSpriteProgram);155156for (size_t i = 0; i < ArraySize(mPositionColorBuffer); i++)157{158glDeleteBuffers(1, &mPositionColorBuffer[i]);159}160}161162void InterleavedAttributeDataBenchmark::drawBenchmark()163{164glClear(GL_COLOR_BUFFER_BIT);165166for (size_t k = 0; k < 20; k++)167{168for (size_t i = 0; i < ArraySize(mPositionColorBuffer); i++)169{170// Firstly get the attribute locations for the program171glUseProgram(mPointSpriteProgram);172GLint positionLocation = glGetAttribLocation(mPointSpriteProgram, "aPosition");173ASSERT_NE(positionLocation, -1);174GLint colorLocation = glGetAttribLocation(mPointSpriteProgram, "aColor");175ASSERT_NE(colorLocation, -1);176177// Bind the position data from one buffer178glBindBuffer(GL_ARRAY_BUFFER, mPositionColorBuffer[i]);179glEnableVertexAttribArray(positionLocation);180glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE,181static_cast<GLsizei>(mBytesPerSprite), 0);182183// But bind the color data from the other buffer.184glBindBuffer(GL_ARRAY_BUFFER,185mPositionColorBuffer[(i + 1) % ArraySize(mPositionColorBuffer)]);186glEnableVertexAttribArray(colorLocation);187glVertexAttribPointer(colorLocation, 3, GL_UNSIGNED_BYTE, GL_TRUE,188static_cast<GLsizei>(mBytesPerSprite),189reinterpret_cast<void *>(2 * sizeof(float)));190191// Then draw the colored pointsprites192glDrawArrays(GL_POINTS, 0, GetParam().numSprites);193194glDisableVertexAttribArray(positionLocation);195glDisableVertexAttribArray(colorLocation);196}197}198199ASSERT_GL_NO_ERROR();200}201202TEST_P(InterleavedAttributeDataBenchmark, Run)203{204run();205}206207InterleavedAttributeDataParams D3D11Params()208{209InterleavedAttributeDataParams params;210params.eglParameters = egl_platform::D3D11();211return params;212}213214InterleavedAttributeDataParams OpenGLOrGLESParams()215{216InterleavedAttributeDataParams params;217params.eglParameters = egl_platform::OPENGL_OR_GLES();218return params;219}220221InterleavedAttributeDataParams VulkanParams()222{223InterleavedAttributeDataParams params;224params.eglParameters = egl_platform::VULKAN();225return params;226}227228ANGLE_INSTANTIATE_TEST(InterleavedAttributeDataBenchmark,229D3D11Params(),230OpenGLOrGLESParams(),231VulkanParams());232233} // anonymous namespace234235236