Chromium Code Reviews| Index: chrome/browser/android/vr_shell/vr_shell_gpu_renderer.cc |
| diff --git a/chrome/browser/android/vr_shell/vr_shell_gpu_renderer.cc b/chrome/browser/android/vr_shell/vr_shell_gpu_renderer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9bdabaec603c029f525723cab032f1961d540831 |
| --- /dev/null |
| +++ b/chrome/browser/android/vr_shell/vr_shell_gpu_renderer.cc |
| @@ -0,0 +1,193 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/vr_shell/vr_shell_gpu_renderer.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| + |
| +#define VOID_OFFSET(x) reinterpret_cast<void*>(x) |
| +#define SHADER(Src) #Src |
| + |
| +namespace vr_shell { |
| + |
| +namespace { |
| + |
| +const char* kQuadCopyVertex = |
| + SHADER(attribute vec4 a_Position; attribute vec2 a_TexCoordinate; |
| + varying vec2 v_TexCoordinate; |
| + void main() { |
| + v_TexCoordinate = a_TexCoordinate; |
| + gl_Position = a_Position; |
| + }); |
| + |
| +const char* kQuadCopyFragment = SHADER( |
| + precision highp float; uniform sampler2D u_Texture; |
| + varying vec2 v_TexCoordinate; |
| + void main() { gl_FragColor = texture2D(u_Texture, v_TexCoordinate); }); |
| + |
| +GLuint CompileShader(gpu::gles2::GLES2Interface* gl, |
| + GLenum shaderType, |
| + const GLchar* shaderSource, |
| + std::string& error) { |
| + GLuint shaderHandle = gl->CreateShader(shaderType); |
| + if (shaderHandle != 0) { |
| + // Pass in the shader source. |
| + int len = strlen(shaderSource); |
| + gl->ShaderSource(shaderHandle, 1, &shaderSource, &len); |
| + // Compile the shader. |
| + gl->CompileShader(shaderHandle); |
| + // Get the compilation status. |
| + GLint status = 0; |
| + gl->GetShaderiv(shaderHandle, GL_COMPILE_STATUS, &status); |
| + if (status == GL_FALSE) { |
| + GLint infoLogLength = 0; |
| + gl->GetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLogLength); |
| + GLchar* strInfoLog = new GLchar[infoLogLength + 1]; |
| + gl->GetShaderInfoLog(shaderHandle, infoLogLength, nullptr, strInfoLog); |
| + error = "Error compiling shader: "; |
| + error += strInfoLog; |
| + delete[] strInfoLog; |
| + gl->DeleteShader(shaderHandle); |
| + shaderHandle = 0; |
| + } |
| + } |
| + |
| + return shaderHandle; |
| +} |
| + |
| +GLuint CreateAndLinkProgram(gpu::gles2::GLES2Interface* gl, |
| + GLuint vertextShaderHandle, |
| + GLuint fragmentShaderHandle, |
| + std::string& error) { |
| + GLuint programHandle = gl->CreateProgram(); |
| + |
| + if (programHandle != 0) { |
| + // Bind the vertex shader to the program. |
| + gl->AttachShader(programHandle, vertextShaderHandle); |
| + |
| + // Bind the fragment shader to the program. |
| + gl->AttachShader(programHandle, fragmentShaderHandle); |
| + |
| + // Link the two shaders together into a program. |
| + gl->LinkProgram(programHandle); |
| + |
| + // Get the link status. |
| + GLint linkStatus = 0; |
| + gl->GetProgramiv(programHandle, GL_LINK_STATUS, &linkStatus); |
| + |
| + // If the link failed, delete the program. |
| + if (linkStatus == GL_FALSE) { |
| + GLint infoLogLength; |
| + gl->GetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &infoLogLength); |
| + |
| + GLchar* strInfoLog = new GLchar[infoLogLength + 1]; |
| + gl->GetProgramInfoLog(programHandle, infoLogLength, nullptr, strInfoLog); |
| + error = "Error compiling program: "; |
| + error += strInfoLog; |
| + delete[] strInfoLog; |
| + gl->DeleteProgram(programHandle); |
| + programHandle = 0; |
| + } |
| + } |
| + |
| + return programHandle; |
| +} |
| + |
| +static constexpr float kQuadVertices[16] = { |
| + // x y u, v |
| + -1.f, 1.f, 0.f, 1.f, -1.f, -1.f, 0.f, 0.f, |
| + 1.f, -1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f}; |
| +static constexpr int kQuadVerticesSize = sizeof(float) * 16; |
| + |
| +} // namespace |
| + |
| +GpuRenderer::GpuRenderer(gpu::gles2::GLES2Interface* gl) { |
| + std::string error; |
| + GLuint vertexShaderHandle = |
| + CompileShader(gl, GL_VERTEX_SHADER, kQuadCopyVertex, error); |
| + CHECK(vertexShaderHandle) << error; |
| + |
| + GLuint fragmentShaderHandle = |
| + CompileShader(gl, GL_FRAGMENT_SHADER, kQuadCopyFragment, error); |
| + CHECK(fragmentShaderHandle) << error; |
| + |
| + m_programHandle = |
| + CreateAndLinkProgram(gl, vertexShaderHandle, fragmentShaderHandle, error); |
| + CHECK(m_programHandle) << error; |
| + |
| + // Once the program is linked the shader objects are no longer needed |
| + gl->DeleteShader(vertexShaderHandle); |
| + gl->DeleteShader(fragmentShaderHandle); |
| + |
| + m_positionHandle = gl->GetAttribLocation(m_programHandle, "a_Position"); |
| + m_texCoordHandle = gl->GetAttribLocation(m_programHandle, "a_TexCoordinate"); |
| + m_texUniformHandle = gl->GetUniformLocation(m_programHandle, "u_Texture"); |
| + |
| + m_vertexBuffer = 0; |
| + gl->GenBuffers(1, &m_vertexBuffer); |
| + gl->BindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); |
| + gl->BufferData(GL_ARRAY_BUFFER, kQuadVerticesSize, kQuadVertices, |
| + GL_STATIC_DRAW); |
| + |
| + // Set state once only, we assume that nobody else modifies GL state in a way |
| + // that would interfere with our operations. |
| + gl->Disable(GL_CULL_FACE); |
| + gl->DepthMask(GL_FALSE); |
| + gl->Disable(GL_DEPTH_TEST); |
| + gl->Disable(GL_SCISSOR_TEST); |
| + gl->Disable(GL_BLEND); |
| + gl->Disable(GL_POLYGON_OFFSET_FILL); |
| + |
| + // Not using gl->Viewport, we assume that it defaults to the whole |
| + // surface and gets updated by ResizeSurface externally as |
| + // appropriate. |
| + |
| + gl->UseProgram(m_programHandle); |
| + |
| + // Bind vertex attributes |
| + gl->BindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); |
| + |
| + gl->EnableVertexAttribArray(m_positionHandle); |
| + gl->EnableVertexAttribArray(m_texCoordHandle); |
| + |
| + static constexpr size_t VERTEX_STRIDE = sizeof(float) * 4; |
| + static constexpr size_t POSITION_ELEMENTS = 2; |
| + static constexpr size_t TEXCOORD_ELEMENTS = 2; |
| + static constexpr size_t POSITION_OFFSET = 0; |
| + static constexpr size_t TEXCOORD_OFFSET = sizeof(float) * 2; |
| + |
| + gl->VertexAttribPointer(m_positionHandle, POSITION_ELEMENTS, GL_FLOAT, false, |
| + VERTEX_STRIDE, VOID_OFFSET(POSITION_OFFSET)); |
| + gl->VertexAttribPointer(m_texCoordHandle, TEXCOORD_ELEMENTS, GL_FLOAT, false, |
| + VERTEX_STRIDE, VOID_OFFSET(TEXCOORD_OFFSET)); |
| + |
| + // Configure texture. This is a 1:1 pixel copy since the surface |
| + // size is resized to match the source canvas, so we can use |
| + // GL_NEAREST. |
| + gl->ActiveTexture(GL_TEXTURE0); |
| + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| + gl->Uniform1i(m_texUniformHandle, 0); |
| +} |
| + |
| +GpuRenderer::~GpuRenderer() = default; |
| + |
| +void GpuRenderer::DrawQuad(gpu::gles2::GLES2Interface* gl, |
| + GLuint textureHandle) { |
| + // Clearing shouldn't be necessary since we're redrawing on top |
| + // of the entire viewport, but allegedly it's friendlier for tiling |
| + // GPUs. TODO(klausw): compare performance with and without it? |
|
bajones
2017/03/07 00:48:07
Nit: discardFramebufferEXT is the "right" thing to
klausw
2017/03/07 02:55:55
Done, though simplified to just use GL_COLOR_ATTAC
|
| + gl->Clear(GL_COLOR_BUFFER_BIT); |
| + |
| + gl->BindTexture(GL_TEXTURE_2D, textureHandle); |
| + gl->DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| + |
| + gl->Flush(); |
| +} |
| + |
| +} // namespace vr_shell |