Index: gpu/command_buffer/tests/gl_test_utils.cc |
diff --git a/gpu/command_buffer/tests/gl_test_utils.cc b/gpu/command_buffer/tests/gl_test_utils.cc |
index 4878b1489c0783b9038087c5b8fb24bb04054202..d9a30ce9b96b10b6a12d4387597c237375b6c285 100644 |
--- a/gpu/command_buffer/tests/gl_test_utils.cc |
+++ b/gpu/command_buffer/tests/gl_test_utils.cc |
@@ -4,19 +4,71 @@ |
#include "gpu/command_buffer/tests/gl_test_utils.h" |
+#include <GLES2/gl2extchromium.h> |
#include <stdint.h> |
#include <stdio.h> |
#include <string> |
#include "base/memory/scoped_ptr.h" |
+#include "base/strings/stringize_macros.h" |
+#include "base/strings/stringprintf.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/geometry/size.h" |
// GCC requires these declarations, but MSVC requires they not be present. |
#ifndef COMPILER_MSVC |
const uint8_t GLTestHelper::kCheckClearValue; |
#endif |
+// Compiles a fragment shader for sampling out of a texture of |size| bound to |
+// |target| and checks for compilation errors. |
+GLuint LoadFragmentShader(unsigned target, const gfx::Size& size) { |
+ // clang-format off |
+ const char kFragmentShader[] = STRINGIZE( |
+ uniform SamplerType a_texture; |
+ varying vec2 v_texCoord; |
+ void main() { |
+ gl_FragColor = TextureLookup(a_texture, v_texCoord * TextureScale); |
+ } |
+ ); |
+ const char kShaderFloatPrecision[] = STRINGIZE( |
+ precision mediump float; |
+ ); |
+ // clang-format on |
+ |
+ switch (target) { |
+ case GL_TEXTURE_2D: |
+ return GLTestHelper::LoadShader( |
+ GL_FRAGMENT_SHADER, |
+ base::StringPrintf("%s\n" |
+ "#define SamplerType sampler2D\n" |
+ "#define TextureLookup texture2D\n" |
+ "#define TextureScale vec2(1.0, 1.0)\n" |
+ "%s", |
+ kShaderFloatPrecision, |
+ kFragmentShader) |
+ .c_str()); |
+ case GL_TEXTURE_RECTANGLE_ARB: |
+ return GLTestHelper::LoadShader( |
+ GL_FRAGMENT_SHADER, |
+ base::StringPrintf("%s\n" |
+ "#extension GL_ARB_texture_rectangle : require\n" |
+ "#define SamplerType sampler2DRect\n" |
+ "#define TextureLookup texture2DRect\n" |
+ "#define TextureScale vec2(%f, %f)\n" |
+ "%s", |
+ kShaderFloatPrecision, |
+ static_cast<double>(size.width()), |
+ static_cast<double>(size.height()), |
+ kFragmentShader) |
+ .c_str()); |
+ default: |
+ NOTREACHED(); |
+ return 0; |
+ } |
+} |
+ |
bool GLTestHelper::HasExtension(const char* extension) { |
std::string extensions( |
reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS))); |
@@ -259,3 +311,35 @@ bool GLTestHelper::SaveBackbufferAsBMP( |
fclose(fp); |
return true; |
} |
+ |
+void GLTestHelper::DrawTextureQuad(GLenum target, const gfx::Size& size) { |
+ // clang-format off |
+ const char kVertexShader[] = STRINGIZE( |
+ attribute vec2 a_position; |
+ varying vec2 v_texCoord; |
+ void main() { |
+ gl_Position = vec4(a_position.x, a_position.y, 0.0, 1.0); |
+ v_texCoord = (a_position + vec2(1.0, 1.0)) * 0.5; |
+ } |
+ ); |
+ // clang-format on |
+ |
+ // Setup program. |
+ GLuint vertex_shader = |
+ GLTestHelper::LoadShader(GL_VERTEX_SHADER, kVertexShader); |
+ GLuint fragment_shader = LoadFragmentShader(target, size); |
+ GLuint program = GLTestHelper::SetupProgram(vertex_shader, fragment_shader); |
+ EXPECT_NE(program, 0u); |
+ glUseProgram(program); |
+ |
+ GLint sampler_location = glGetUniformLocation(program, "a_texture"); |
+ ASSERT_NE(sampler_location, -1); |
+ |
+ GLuint vertex_buffer = GLTestHelper::SetupUnitQuad(sampler_location); |
+ glDrawArrays(GL_TRIANGLES, 0, 6); |
+ |
+ glDeleteShader(vertex_shader); |
+ glDeleteShader(fragment_shader); |
+ glDeleteProgram(program); |
+ glDeleteBuffers(1, &vertex_buffer); |
+} |