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; | |
| 15 const float kHalfWidth = 0.5f; | |
| 16 const float kTextureQuadPosition[18] = { | |
| 17 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, | |
| 18 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, | |
| 19 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f}; | |
| 20 const int kPositionDataSize = 3; | |
| 21 // Number of vertices passed to glDrawArrays(). | |
| 22 const int kVerticesNumber = 6; | |
| 23 | |
| 24 const 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}; | |
| 26 const int kTextureCoordinateDataSize = 2; | |
| 27 | |
| 28 #define SHADER(Src) #Src | 12 #define SHADER(Src) #Src |
| 29 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src | 13 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src |
| 30 | 14 |
| 31 const char* GetShaderSource(ShaderID shader) { | 15 const char* GetShaderSource(vr_shell::ShaderID shader) { |
| 32 switch (shader) { | 16 switch (shader) { |
| 33 case TEXTURE_QUAD_VERTEX_SHADER: | 17 case vr_shell::ShaderID::TEXTURE_QUAD_VERTEX_SHADER: |
| 34 return SHADER(uniform mat4 u_CombinedMatrix; attribute vec4 a_Position; | 18 return SHADER(uniform mat4 u_CombinedMatrix; |
| 19 attribute vec4 a_Position; | |
| 35 attribute vec2 a_TexCoordinate; | 20 attribute vec2 a_TexCoordinate; |
| 36 varying vec2 v_TexCoordinate; void main() { | 21 varying vec2 v_TexCoordinate; |
| 22 void main() { | |
| 37 v_TexCoordinate = a_TexCoordinate; | 23 v_TexCoordinate = a_TexCoordinate; |
| 38 gl_Position = u_CombinedMatrix * a_Position; | 24 gl_Position = u_CombinedMatrix * a_Position; |
| 39 }); | 25 }); |
| 40 case TEXTURE_QUAD_FRAGMENT_SHADER: | 26 case vr_shell::ShaderID::TEXTURE_QUAD_FRAGMENT_SHADER: |
| 41 return OEIE_SHADER( | 27 return OEIE_SHADER( |
| 42 precision highp float; uniform samplerExternalOES u_Texture; | 28 precision highp float; |
| 43 varying vec2 v_TexCoordinate; void main() { | 29 uniform samplerExternalOES u_Texture; |
| 44 vec4 texture = texture2D(u_Texture, v_TexCoordinate); | 30 uniform vec4 u_CopyRect; // rectangle |
| 45 gl_FragColor = vec4(texture.r, texture.g, texture.b, 1.0); | 31 varying vec2 v_TexCoordinate; |
| 32 void main() { | |
| 33 vec2 scaledTex = vec2( | |
| 34 u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], | |
| 35 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); | |
| 36 gl_FragColor = texture2D(u_Texture, scaledTex); | |
| 46 }); | 37 }); |
| 47 default: | 38 default: |
| 48 LOG(ERROR) << "Shader source requested for unknown shader"; | 39 LOG(ERROR) << "Shader source requested for unknown shader"; |
| 49 return ""; | 40 return ""; |
| 50 } | 41 } |
| 51 } | 42 } |
| 52 | 43 |
| 53 } // namespace | 44 } // namespace |
| 54 | 45 |
| 46 namespace vr_shell { | |
| 47 | |
| 48 constexpr float TexturedQuadRenderer::kTextureQuadPosition[]; | |
| 49 constexpr float TexturedQuadRenderer::kTexturedQuadTextureCoordinates[]; | |
| 50 | |
| 55 TexturedQuadRenderer::TexturedQuadRenderer() { | 51 TexturedQuadRenderer::TexturedQuadRenderer() { |
| 56 std::string error; | 52 std::string error; |
| 57 vertex_shader_handle_ = CompileShader( | 53 vertex_shader_handle_ = CompileShader( |
| 58 GL_VERTEX_SHADER, GetShaderSource(TEXTURE_QUAD_VERTEX_SHADER), error); | 54 GL_VERTEX_SHADER, GetShaderSource(TEXTURE_QUAD_VERTEX_SHADER), error); |
| 59 if (vertex_shader_handle_ == 0) { | 55 if (vertex_shader_handle_ == 0) { |
| 60 LOG(ERROR) << error; | 56 LOG(ERROR) << error; |
| 61 exit(1); | 57 exit(1); |
| 62 } | 58 } |
| 63 fragment_shader_handle_ = CompileShader( | 59 fragment_shader_handle_ = CompileShader( |
| 64 GL_FRAGMENT_SHADER, GetShaderSource(TEXTURE_QUAD_FRAGMENT_SHADER), error); | 60 GL_FRAGMENT_SHADER, GetShaderSource(TEXTURE_QUAD_FRAGMENT_SHADER), error); |
| 65 if (fragment_shader_handle_ == 0) { | 61 if (fragment_shader_handle_ == 0) { |
| 66 LOG(ERROR) << error; | 62 LOG(ERROR) << error; |
| 67 exit(1); | 63 exit(1); |
| 68 } | 64 } |
| 69 | 65 |
| 70 program_handle_ = CreateAndLinkProgram( | 66 program_handle_ = CreateAndLinkProgram( |
| 71 vertex_shader_handle_, fragment_shader_handle_, 4, nullptr, error); | 67 vertex_shader_handle_, fragment_shader_handle_, 5, nullptr, error); |
| 72 if (program_handle_ == 0) { | 68 if (program_handle_ == 0) { |
| 73 LOG(ERROR) << error; | 69 LOG(ERROR) << error; |
| 74 exit(1); | 70 exit(1); |
| 75 } | 71 } |
| 76 combined_matrix_handle_ = | 72 combined_matrix_handle_ = |
| 77 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); | 73 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); |
| 78 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); | 74 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 75 copy_rect_uniform_handle_ = glGetUniformLocation(program_handle_, | |
| 76 "u_CopyRect"); | |
| 79 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); | 77 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); |
| 80 texture_coordinate_handle_ = | 78 texture_coordinate_handle_ = |
| 81 glGetAttribLocation(program_handle_, "a_TexCoordinate"); | 79 glGetAttribLocation(program_handle_, "a_TexCoordinate"); |
| 82 } | 80 } |
| 83 | 81 |
| 84 void TexturedQuadRenderer::Draw(int texture_data_handle, | 82 void TexturedQuadRenderer::Draw(int texture_data_handle, |
| 85 const gvr::Mat4f& combined_matrix) { | 83 const gvr::Mat4f& combined_matrix, |
| 84 const Rectf& copy_rect) { | |
| 86 glUseProgram(program_handle_); | 85 glUseProgram(program_handle_); |
| 87 | 86 |
| 88 // Pass in model view project matrix. | 87 // Pass in model view project matrix. |
| 89 glUniformMatrix4fv(combined_matrix_handle_, 1, false, | 88 glUniformMatrix4fv(combined_matrix_handle_, 1, false, |
| 90 MatrixToGLArray(combined_matrix).data()); | 89 MatrixToGLArray(combined_matrix).data()); |
| 91 | 90 |
| 92 // Pass in texture coordinate. | 91 // Pass in texture coordinate. |
| 93 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize, | 92 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize, |
| 94 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates); | 93 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates); |
| 95 glEnableVertexAttribArray(texture_coordinate_handle_); | 94 glEnableVertexAttribArray(texture_coordinate_handle_); |
| 96 | 95 |
| 97 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, | 96 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, |
| 98 kTextureQuadPosition); | 97 kTextureQuadPosition); |
| 99 glEnableVertexAttribArray(position_handle_); | 98 glEnableVertexAttribArray(position_handle_); |
| 100 | 99 |
| 100 glEnable(GL_BLEND); | |
| 101 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 102 | |
| 101 // Link texture data with texture unit. | 103 // Link texture data with texture unit. |
| 102 glActiveTexture(GL_TEXTURE0); | 104 glActiveTexture(GL_TEXTURE0); |
| 103 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); | 105 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); |
| 104 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 106 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); | 107 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); | 108 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 107 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 109 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 110 | |
| 108 glUniform1i(texture_uniform_handle_, 0); | 111 glUniform1i(texture_uniform_handle_, 0); |
| 112 glUniform4fv(copy_rect_uniform_handle_, 1, (float*)(©_rect)); | |
| 109 | 113 |
| 110 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); | 114 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
| 111 | 115 |
| 112 glDisableVertexAttribArray(position_handle_); | 116 glDisableVertexAttribArray(position_handle_); |
| 113 glDisableVertexAttribArray(texture_coordinate_handle_); | 117 glDisableVertexAttribArray(texture_coordinate_handle_); |
| 114 } | 118 } |
| 115 | 119 |
| 116 TexturedQuadRenderer::~TexturedQuadRenderer() { | 120 TexturedQuadRenderer::~TexturedQuadRenderer() {} |
|
David Trainor- moved to gerrit
2016/09/08 05:49:30
= default? Same with other destructor
mthiesse
2016/09/08 17:44:41
Done.
| |
| 117 glDeleteShader(vertex_shader_handle_); | |
| 118 glDeleteShader(fragment_shader_handle_); | |
| 119 } | |
| 120 | 121 |
| 121 VrShellRenderer::VrShellRenderer() | 122 VrShellRenderer::VrShellRenderer() |
| 122 : textured_quad_renderer_(new TexturedQuadRenderer) {} | 123 : textured_quad_renderer_(new TexturedQuadRenderer) {} |
| 123 | 124 |
| 124 VrShellRenderer::~VrShellRenderer() {} | 125 VrShellRenderer::~VrShellRenderer() {} |
| 125 | 126 |
| 126 } // namespace vr_shell | 127 } // namespace vr_shell |
| OLD | NEW |