Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/vr_shell/vr_shell_gpu_renderer.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 #define VOID_OFFSET(x) reinterpret_cast<void*>(x) | |
| 12 #define SHADER(Src) #Src | |
| 13 | |
| 14 namespace vr_shell { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char* kQuadCopyVertex = | |
| 19 SHADER(attribute vec4 a_Position; attribute vec2 a_TexCoordinate; | |
| 20 varying vec2 v_TexCoordinate; | |
| 21 void main() { | |
| 22 v_TexCoordinate = a_TexCoordinate; | |
| 23 gl_Position = a_Position; | |
| 24 }); | |
| 25 | |
| 26 const char* kQuadCopyFragment = SHADER( | |
| 27 precision highp float; uniform sampler2D u_Texture; | |
| 28 varying vec2 v_TexCoordinate; | |
| 29 void main() { gl_FragColor = texture2D(u_Texture, v_TexCoordinate); }); | |
| 30 | |
| 31 GLuint CompileShader(gpu::gles2::GLES2Interface* gl, | |
| 32 GLenum shaderType, | |
| 33 const GLchar* shaderSource, | |
| 34 std::string& error) { | |
| 35 GLuint shaderHandle = gl->CreateShader(shaderType); | |
| 36 if (shaderHandle != 0) { | |
| 37 // Pass in the shader source. | |
| 38 int len = strlen(shaderSource); | |
| 39 gl->ShaderSource(shaderHandle, 1, &shaderSource, &len); | |
| 40 // Compile the shader. | |
| 41 gl->CompileShader(shaderHandle); | |
| 42 // Get the compilation status. | |
| 43 GLint status = 0; | |
| 44 gl->GetShaderiv(shaderHandle, GL_COMPILE_STATUS, &status); | |
| 45 if (status == GL_FALSE) { | |
| 46 GLint infoLogLength = 0; | |
| 47 gl->GetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLogLength); | |
| 48 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; | |
| 49 gl->GetShaderInfoLog(shaderHandle, infoLogLength, nullptr, strInfoLog); | |
| 50 error = "Error compiling shader: "; | |
| 51 error += strInfoLog; | |
| 52 delete[] strInfoLog; | |
| 53 gl->DeleteShader(shaderHandle); | |
| 54 shaderHandle = 0; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 return shaderHandle; | |
| 59 } | |
| 60 | |
| 61 GLuint CreateAndLinkProgram(gpu::gles2::GLES2Interface* gl, | |
| 62 GLuint vertextShaderHandle, | |
| 63 GLuint fragmentShaderHandle, | |
| 64 std::string& error) { | |
| 65 GLuint programHandle = gl->CreateProgram(); | |
| 66 | |
| 67 if (programHandle != 0) { | |
| 68 // Bind the vertex shader to the program. | |
| 69 gl->AttachShader(programHandle, vertextShaderHandle); | |
| 70 | |
| 71 // Bind the fragment shader to the program. | |
| 72 gl->AttachShader(programHandle, fragmentShaderHandle); | |
| 73 | |
| 74 // Link the two shaders together into a program. | |
| 75 gl->LinkProgram(programHandle); | |
| 76 | |
| 77 // Get the link status. | |
| 78 GLint linkStatus = 0; | |
| 79 gl->GetProgramiv(programHandle, GL_LINK_STATUS, &linkStatus); | |
| 80 | |
| 81 // If the link failed, delete the program. | |
| 82 if (linkStatus == GL_FALSE) { | |
| 83 GLint infoLogLength; | |
| 84 gl->GetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &infoLogLength); | |
| 85 | |
| 86 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; | |
| 87 gl->GetProgramInfoLog(programHandle, infoLogLength, nullptr, strInfoLog); | |
| 88 error = "Error compiling program: "; | |
| 89 error += strInfoLog; | |
| 90 delete[] strInfoLog; | |
| 91 gl->DeleteProgram(programHandle); | |
| 92 programHandle = 0; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 return programHandle; | |
| 97 } | |
| 98 | |
| 99 static constexpr float kQuadVertices[16] = { | |
| 100 // x y u, v | |
| 101 -1.f, 1.f, 0.f, 1.f, -1.f, -1.f, 0.f, 0.f, | |
| 102 1.f, -1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f}; | |
| 103 static constexpr int kQuadVerticesSize = sizeof(float) * 16; | |
| 104 | |
| 105 } // namespace | |
| 106 | |
| 107 GpuRenderer::GpuRenderer(gpu::gles2::GLES2Interface* gl) { | |
| 108 std::string error; | |
| 109 GLuint vertexShaderHandle = | |
| 110 CompileShader(gl, GL_VERTEX_SHADER, kQuadCopyVertex, error); | |
| 111 CHECK(vertexShaderHandle) << error; | |
| 112 | |
| 113 GLuint fragmentShaderHandle = | |
| 114 CompileShader(gl, GL_FRAGMENT_SHADER, kQuadCopyFragment, error); | |
| 115 CHECK(fragmentShaderHandle) << error; | |
| 116 | |
| 117 m_programHandle = | |
| 118 CreateAndLinkProgram(gl, vertexShaderHandle, fragmentShaderHandle, error); | |
| 119 CHECK(m_programHandle) << error; | |
| 120 | |
| 121 // Once the program is linked the shader objects are no longer needed | |
| 122 gl->DeleteShader(vertexShaderHandle); | |
| 123 gl->DeleteShader(fragmentShaderHandle); | |
| 124 | |
| 125 m_positionHandle = gl->GetAttribLocation(m_programHandle, "a_Position"); | |
| 126 m_texCoordHandle = gl->GetAttribLocation(m_programHandle, "a_TexCoordinate"); | |
| 127 m_texUniformHandle = gl->GetUniformLocation(m_programHandle, "u_Texture"); | |
| 128 | |
| 129 m_vertexBuffer = 0; | |
| 130 gl->GenBuffers(1, &m_vertexBuffer); | |
| 131 gl->BindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); | |
| 132 gl->BufferData(GL_ARRAY_BUFFER, kQuadVerticesSize, kQuadVertices, | |
| 133 GL_STATIC_DRAW); | |
| 134 | |
| 135 // Set state once only, we assume that nobody else modifies GL state in a way | |
| 136 // that would interfere with our operations. | |
| 137 gl->Disable(GL_CULL_FACE); | |
| 138 gl->DepthMask(GL_FALSE); | |
| 139 gl->Disable(GL_DEPTH_TEST); | |
| 140 gl->Disable(GL_SCISSOR_TEST); | |
| 141 gl->Disable(GL_BLEND); | |
| 142 gl->Disable(GL_POLYGON_OFFSET_FILL); | |
| 143 | |
| 144 // Not using gl->Viewport, we assume that it defaults to the whole | |
| 145 // surface and gets updated by ResizeSurface externally as | |
| 146 // appropriate. | |
| 147 | |
| 148 gl->UseProgram(m_programHandle); | |
| 149 | |
| 150 // Bind vertex attributes | |
| 151 gl->BindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); | |
| 152 | |
| 153 gl->EnableVertexAttribArray(m_positionHandle); | |
| 154 gl->EnableVertexAttribArray(m_texCoordHandle); | |
| 155 | |
| 156 static constexpr size_t VERTEX_STRIDE = sizeof(float) * 4; | |
| 157 static constexpr size_t POSITION_ELEMENTS = 2; | |
| 158 static constexpr size_t TEXCOORD_ELEMENTS = 2; | |
| 159 static constexpr size_t POSITION_OFFSET = 0; | |
| 160 static constexpr size_t TEXCOORD_OFFSET = sizeof(float) * 2; | |
| 161 | |
| 162 gl->VertexAttribPointer(m_positionHandle, POSITION_ELEMENTS, GL_FLOAT, false, | |
| 163 VERTEX_STRIDE, VOID_OFFSET(POSITION_OFFSET)); | |
| 164 gl->VertexAttribPointer(m_texCoordHandle, TEXCOORD_ELEMENTS, GL_FLOAT, false, | |
| 165 VERTEX_STRIDE, VOID_OFFSET(TEXCOORD_OFFSET)); | |
| 166 | |
| 167 // Configure texture. This is a 1:1 pixel copy since the surface | |
| 168 // size is resized to match the source canvas, so we can use | |
| 169 // GL_NEAREST. | |
| 170 gl->ActiveTexture(GL_TEXTURE0); | |
| 171 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 172 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 173 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 174 gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 175 gl->Uniform1i(m_texUniformHandle, 0); | |
| 176 } | |
| 177 | |
| 178 GpuRenderer::~GpuRenderer() = default; | |
| 179 | |
| 180 void GpuRenderer::DrawQuad(gpu::gles2::GLES2Interface* gl, | |
| 181 GLuint textureHandle) { | |
| 182 // Clearing shouldn't be necessary since we're redrawing on top | |
| 183 // of the entire viewport, but allegedly it's friendlier for tiling | |
| 184 // 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
| |
| 185 gl->Clear(GL_COLOR_BUFFER_BIT); | |
| 186 | |
| 187 gl->BindTexture(GL_TEXTURE_2D, textureHandle); | |
| 188 gl->DrawArrays(GL_TRIANGLE_FAN, 0, 4); | |
| 189 | |
| 190 gl->Flush(); | |
| 191 } | |
| 192 | |
| 193 } // namespace vr_shell | |
| OLD | NEW |