Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" | 5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" |
| 6 | 6 |
| 7 #include "chrome/browser/android/vr_shell/vr_util.h" | 7 #include "chrome/browser/android/vr_shell/vr_util.h" |
| 8 #include "ui/gl/gl_bindings.h" | 8 #include "ui/gl/gl_bindings.h" |
| 9 | 9 |
| 10 namespace vr_shell { | |
| 11 | |
| 12 namespace { | 10 namespace { |
| 13 | 11 |
| 14 const float kHalfHeight = 0.5f; | 12 static constexpr float kHalfHeight = 0.5f; |
| 15 const float kHalfWidth = 0.5f; | 13 static constexpr float kHalfWidth = 0.5f; |
| 16 const float kTextureQuadPosition[18] = { | 14 static constexpr float kTextureQuadPosition[18] = { |
| 17 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, | 15 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, |
| 18 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, | 16 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, |
| 19 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f}; | 17 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f}; |
| 20 const int kPositionDataSize = 3; | 18 static constexpr int kPositionDataSize = 3; |
| 21 // Number of vertices passed to glDrawArrays(). | 19 // Number of vertices passed to glDrawArrays(). |
| 22 const int kVerticesNumber = 6; | 20 static constexpr int kVerticesNumber = 6; |
| 23 | 21 |
| 24 const float kTexturedQuadTextureCoordinates[12] = { | 22 static constexpr float kTexturedQuadTextureCoordinates[12] = |
| 25 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f}; | 23 makeRectangularTextureBuffer(0.0f, 1.0f, 0.0f, 1.0f); |
| 26 const int kTextureCoordinateDataSize = 2; | 24 |
| 25 static constexpr int kTextureCoordinateDataSize = 2; | |
| 27 | 26 |
| 28 #define SHADER(Src) #Src | 27 #define SHADER(Src) #Src |
| 29 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src | 28 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src |
| 30 | 29 |
| 31 const char* GetShaderSource(ShaderID shader) { | 30 const char* GetShaderSource(vr_shell::ShaderID shader) { |
|
bshe
2016/09/09 14:42:10
if you dont move the vr_shell namespace definition
mthiesse
2016/09/09 15:16:38
Style preferences, anonymous namespaces are usuall
| |
| 32 switch (shader) { | 31 switch (shader) { |
| 33 case TEXTURE_QUAD_VERTEX_SHADER: | 32 case vr_shell::ShaderID::TEXTURE_QUAD_VERTEX_SHADER: |
| 34 return SHADER(uniform mat4 u_CombinedMatrix; attribute vec4 a_Position; | 33 return SHADER(uniform mat4 u_CombinedMatrix; |
| 34 attribute vec4 a_Position; | |
| 35 attribute vec2 a_TexCoordinate; | 35 attribute vec2 a_TexCoordinate; |
| 36 varying vec2 v_TexCoordinate; void main() { | 36 varying vec2 v_TexCoordinate; |
| 37 void main() { | |
| 37 v_TexCoordinate = a_TexCoordinate; | 38 v_TexCoordinate = a_TexCoordinate; |
| 38 gl_Position = u_CombinedMatrix * a_Position; | 39 gl_Position = u_CombinedMatrix * a_Position; |
| 39 }); | 40 }); |
| 40 case TEXTURE_QUAD_FRAGMENT_SHADER: | 41 case vr_shell::ShaderID::TEXTURE_QUAD_FRAGMENT_SHADER: |
| 41 return OEIE_SHADER( | 42 return OEIE_SHADER( |
| 42 precision highp float; uniform samplerExternalOES u_Texture; | 43 precision highp float; |
| 43 varying vec2 v_TexCoordinate; void main() { | 44 uniform samplerExternalOES u_Texture; |
| 44 vec4 texture = texture2D(u_Texture, v_TexCoordinate); | 45 uniform vec4 u_CopyRect; // rectangle |
| 45 gl_FragColor = vec4(texture.r, texture.g, texture.b, 1.0); | 46 varying vec2 v_TexCoordinate; |
| 47 void main() { | |
| 48 vec2 scaledTex = vec2( | |
| 49 u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], | |
| 50 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); | |
| 51 gl_FragColor = texture2D(u_Texture, scaledTex); | |
| 46 }); | 52 }); |
| 47 default: | 53 default: |
| 48 LOG(ERROR) << "Shader source requested for unknown shader"; | 54 LOG(ERROR) << "Shader source requested for unknown shader"; |
| 49 return ""; | 55 return ""; |
| 50 } | 56 } |
| 51 } | 57 } |
| 52 | 58 |
| 53 } // namespace | 59 } // namespace |
| 54 | 60 |
| 61 namespace vr_shell { | |
| 62 | |
| 55 TexturedQuadRenderer::TexturedQuadRenderer() { | 63 TexturedQuadRenderer::TexturedQuadRenderer() { |
| 56 std::string error; | 64 std::string error; |
| 57 vertex_shader_handle_ = CompileShader( | 65 vertex_shader_handle_ = CompileShader( |
| 58 GL_VERTEX_SHADER, GetShaderSource(TEXTURE_QUAD_VERTEX_SHADER), error); | 66 GL_VERTEX_SHADER, GetShaderSource(TEXTURE_QUAD_VERTEX_SHADER), error); |
| 59 if (vertex_shader_handle_ == 0) { | 67 if (vertex_shader_handle_ == 0) { |
| 60 LOG(ERROR) << error; | 68 LOG(ERROR) << error; |
| 61 exit(1); | 69 exit(1); |
| 62 } | 70 } |
| 63 fragment_shader_handle_ = CompileShader( | 71 fragment_shader_handle_ = CompileShader( |
| 64 GL_FRAGMENT_SHADER, GetShaderSource(TEXTURE_QUAD_FRAGMENT_SHADER), error); | 72 GL_FRAGMENT_SHADER, GetShaderSource(TEXTURE_QUAD_FRAGMENT_SHADER), error); |
| 65 if (fragment_shader_handle_ == 0) { | 73 if (fragment_shader_handle_ == 0) { |
| 66 LOG(ERROR) << error; | 74 LOG(ERROR) << error; |
| 67 exit(1); | 75 exit(1); |
| 68 } | 76 } |
| 69 | 77 |
| 70 program_handle_ = CreateAndLinkProgram( | 78 program_handle_ = CreateAndLinkProgram( |
| 71 vertex_shader_handle_, fragment_shader_handle_, 4, nullptr, error); | 79 vertex_shader_handle_, fragment_shader_handle_, 5, nullptr, error); |
| 72 if (program_handle_ == 0) { | 80 if (program_handle_ == 0) { |
| 73 LOG(ERROR) << error; | 81 LOG(ERROR) << error; |
| 74 exit(1); | 82 exit(1); |
| 75 } | 83 } |
| 76 combined_matrix_handle_ = | 84 combined_matrix_handle_ = |
| 77 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); | 85 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); |
| 78 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); | 86 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 87 copy_rect_uniform_handle_ = glGetUniformLocation(program_handle_, | |
| 88 "u_CopyRect"); | |
| 79 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); | 89 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); |
| 80 texture_coordinate_handle_ = | 90 texture_coordinate_handle_ = |
| 81 glGetAttribLocation(program_handle_, "a_TexCoordinate"); | 91 glGetAttribLocation(program_handle_, "a_TexCoordinate"); |
| 82 } | 92 } |
| 83 | 93 |
| 84 void TexturedQuadRenderer::Draw(int texture_data_handle, | 94 void TexturedQuadRenderer::Draw(int texture_data_handle, |
| 85 const gvr::Mat4f& combined_matrix) { | 95 const gvr::Mat4f& combined_matrix, |
| 96 const Rectf& copy_rect) { | |
| 86 glUseProgram(program_handle_); | 97 glUseProgram(program_handle_); |
| 87 | 98 |
| 88 // Pass in model view project matrix. | 99 // Pass in model view project matrix. |
| 89 glUniformMatrix4fv(combined_matrix_handle_, 1, false, | 100 glUniformMatrix4fv(combined_matrix_handle_, 1, false, |
| 90 MatrixToGLArray(combined_matrix).data()); | 101 MatrixToGLArray(combined_matrix).data()); |
| 91 | 102 |
| 92 // Pass in texture coordinate. | 103 // Pass in texture coordinate. |
| 93 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize, | 104 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize, |
| 94 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates); | 105 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates); |
| 95 glEnableVertexAttribArray(texture_coordinate_handle_); | 106 glEnableVertexAttribArray(texture_coordinate_handle_); |
| 96 | 107 |
| 97 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, | 108 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, |
| 98 kTextureQuadPosition); | 109 kTextureQuadPosition); |
| 99 glEnableVertexAttribArray(position_handle_); | 110 glEnableVertexAttribArray(position_handle_); |
| 100 | 111 |
| 112 glEnable(GL_BLEND); | |
| 113 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 114 | |
| 101 // Link texture data with texture unit. | 115 // Link texture data with texture unit. |
| 102 glActiveTexture(GL_TEXTURE0); | 116 glActiveTexture(GL_TEXTURE0); |
| 103 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); | 117 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); |
| 104 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 118 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 105 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 119 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 106 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 120 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 107 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 121 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 122 | |
| 108 glUniform1i(texture_uniform_handle_, 0); | 123 glUniform1i(texture_uniform_handle_, 0); |
| 124 glUniform4fv(copy_rect_uniform_handle_, 1, (float*)(©_rect)); | |
| 109 | 125 |
| 110 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); | 126 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
| 111 | 127 |
| 112 glDisableVertexAttribArray(position_handle_); | 128 glDisableVertexAttribArray(position_handle_); |
| 113 glDisableVertexAttribArray(texture_coordinate_handle_); | 129 glDisableVertexAttribArray(texture_coordinate_handle_); |
| 114 } | 130 } |
| 115 | 131 |
| 116 TexturedQuadRenderer::~TexturedQuadRenderer() { | 132 TexturedQuadRenderer::~TexturedQuadRenderer() = default; |
| 117 glDeleteShader(vertex_shader_handle_); | |
| 118 glDeleteShader(fragment_shader_handle_); | |
| 119 } | |
| 120 | 133 |
| 121 VrShellRenderer::VrShellRenderer() | 134 VrShellRenderer::VrShellRenderer() |
| 122 : textured_quad_renderer_(new TexturedQuadRenderer) {} | 135 : textured_quad_renderer_(new TexturedQuadRenderer) {} |
| 123 | 136 |
| 124 VrShellRenderer::~VrShellRenderer() {} | 137 VrShellRenderer::~VrShellRenderer() = default; |
| 125 | 138 |
| 126 } // namespace vr_shell | 139 } // namespace vr_shell |
| OLD | NEW |