Path: blob/main_old/src/tests/test_utils/draw_call_perf_utils.cpp
1693 views
//1// Copyright 2017 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// draw_call_perf_utils.cpp:6// Common utilities for performance tests that need to do a large amount of draw calls.7//89#include "draw_call_perf_utils.h"1011#include <vector>1213#include "util/shader_utils.h"1415namespace16{17constexpr char kSimpleScaleAndOffsetVS[] = R"(attribute vec2 vPosition;18uniform float uScale;19uniform float uOffset;20void main()21{22gl_Position = vec4(vPosition * vec2(uScale) + vec2(uOffset), 0, 1);23})";2425constexpr char kSimpleDrawVS[] = R"(attribute vec2 vPosition;26const float scale = 0.5;27const float offset = -0.5;2829void main()30{31gl_Position = vec4(vPosition * vec2(scale) + vec2(offset), 0, 1);32})";3334constexpr char kSimpleTexCoordVS[] = R"(attribute vec2 vPosition;35varying vec2 texCoord;36void main()37{38gl_Position = vec4(vPosition, 0, 1);39texCoord = vPosition * 0.5 + vec2(0.5);40})";4142constexpr char kSimpleFS[] = R"(precision mediump float;43void main()44{45gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);46})";4748constexpr char kSimpleTextureFS[] = R"(precision mediump float;49varying vec2 texCoord;50uniform sampler2D tex;51void main()52{53gl_FragColor = texture2D(tex, texCoord);54})";5556constexpr char kDoubleTextureFS[] = R"(precision mediump float;57varying vec2 texCoord;58uniform sampler2D tex1;59uniform sampler2D tex2;60void main()61{62gl_FragColor = texture2D(tex1, texCoord) + texture2D(tex2, texCoord);63})";6465void Generate2DTriangleData(size_t numTris, std::vector<float> *floatData)66{67for (size_t triIndex = 0; triIndex < numTris; ++triIndex)68{69floatData->push_back(1.0f);70floatData->push_back(2.0f);7172floatData->push_back(0.0f);73floatData->push_back(0.0f);7475floatData->push_back(2.0f);76floatData->push_back(0.0f);77}78}7980} // anonymous namespace8182GLuint SetupSimpleScaleAndOffsetProgram()83{84GLuint program = CompileProgram(kSimpleScaleAndOffsetVS, kSimpleFS);85if (program == 0u)86{87return program;88}8990// Use the program object91glUseProgram(program);9293GLfloat scale = 0.5f;94GLfloat offset = -0.5f;9596glUniform1f(glGetUniformLocation(program, "uScale"), scale);97glUniform1f(glGetUniformLocation(program, "uOffset"), offset);98return program;99}100101GLuint SetupSimpleDrawProgram()102{103GLuint program = CompileProgram(kSimpleDrawVS, kSimpleFS);104if (program == 0u)105{106return program;107}108109// Use the program object110glUseProgram(program);111112return program;113}114115GLuint SetupSimpleTextureProgram()116{117GLuint program = CompileProgram(kSimpleTexCoordVS, kSimpleTextureFS);118if (program == 0u)119{120return program;121}122123// Use the program object124glUseProgram(program);125126return program;127}128129GLuint SetupDoubleTextureProgram()130{131GLuint program = CompileProgram(kSimpleTexCoordVS, kDoubleTextureFS);132if (program == 0u)133{134return program;135}136137// Use the program object138glUseProgram(program);139140return program;141}142143GLuint Create2DTriangleBuffer(size_t numTris, GLenum usage)144{145GLuint buffer = 0u;146glGenBuffers(1, &buffer);147glBindBuffer(GL_ARRAY_BUFFER, buffer);148149std::vector<GLfloat> floatData;150Generate2DTriangleData(numTris, &floatData);151152// To avoid generating GL errors when testing validation-only with zero triangles.153if (floatData.empty())154{155floatData.push_back(0.0f);156}157158glBufferData(GL_ARRAY_BUFFER, floatData.size() * sizeof(GLfloat), &floatData[0], usage);159160return buffer;161}162163void CreateColorFBO(GLsizei width, GLsizei height, GLuint *fbo, GLuint *texture)164{165glGenFramebuffers(1, fbo);166glBindFramebuffer(GL_FRAMEBUFFER, *fbo);167glGenTextures(1, texture);168glBindTexture(GL_TEXTURE_2D, *texture);169glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);170glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *texture, 0);171}172173174