| 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 <math.h> | 7 #include <math.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 "\xcd\xcd\xcd\x24\xce\xce\xce\x1f\xce\xce\xce\x1a\xce\xce\xce\x15\xd2\xd2" | 64 "\xcd\xcd\xcd\x24\xce\xce\xce\x1f\xce\xce\xce\x1a\xce\xce\xce\x15\xd2\xd2" |
| 65 "\xd2\x11\xd8\xd8\xd8\x0d\xcc\xcc\xcc\x0a\xdb\xdb\xdb\x07\xcc\xcc\xcc\x05" | 65 "\xd2\x11\xd8\xd8\xd8\x0d\xcc\xcc\xcc\x0a\xdb\xdb\xdb\x07\xcc\xcc\xcc\x05" |
| 66 "\xbf\xbf\xbf\x04\xff\xff\xff\x02\xff\xff\xff\x01"; | 66 "\xbf\xbf\xbf\x04\xff\xff\xff\x02\xff\xff\xff\x01"; |
| 67 | 67 |
| 68 #define SHADER(Src) #Src | 68 #define SHADER(Src) #Src |
| 69 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src | 69 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src |
| 70 #define VOID_OFFSET(x) reinterpret_cast<void*>(x) | 70 #define VOID_OFFSET(x) reinterpret_cast<void*>(x) |
| 71 | 71 |
| 72 const char* GetShaderSource(vr_shell::ShaderID shader) { | 72 const char* GetShaderSource(vr_shell::ShaderID shader) { |
| 73 switch (shader) { | 73 switch (shader) { |
| 74 case vr_shell::ShaderID::TEXTURE_QUAD_VERTEX_SHADER: | 74 case vr_shell::ShaderID::EXTERNAL_TEXTURED_QUAD_VERTEX_SHADER: |
| 75 case vr_shell::ShaderID::RETICLE_VERTEX_SHADER: | 75 case vr_shell::ShaderID::RETICLE_VERTEX_SHADER: |
| 76 case vr_shell::ShaderID::LASER_VERTEX_SHADER: | 76 case vr_shell::ShaderID::LASER_VERTEX_SHADER: |
| 77 case vr_shell::ShaderID::CONTROLLER_VERTEX_SHADER: | 77 case vr_shell::ShaderID::CONTROLLER_VERTEX_SHADER: |
| 78 case vr_shell::ShaderID::TEXTURED_QUAD_VERTEX_SHADER: |
| 78 return SHADER( | 79 return SHADER( |
| 79 /* clang-format off */ | 80 /* clang-format off */ |
| 80 precision mediump float; | 81 precision mediump float; |
| 81 uniform mat4 u_ModelViewProjMatrix; | 82 uniform mat4 u_ModelViewProjMatrix; |
| 82 attribute vec4 a_Position; | 83 attribute vec4 a_Position; |
| 83 attribute vec2 a_TexCoordinate; | 84 attribute vec2 a_TexCoordinate; |
| 84 varying vec2 v_TexCoordinate; | 85 varying vec2 v_TexCoordinate; |
| 85 | 86 |
| 86 void main() { | 87 void main() { |
| 87 v_TexCoordinate = a_TexCoordinate; | 88 v_TexCoordinate = a_TexCoordinate; |
| 88 gl_Position = u_ModelViewProjMatrix * a_Position; | 89 gl_Position = u_ModelViewProjMatrix * a_Position; |
| 89 } | 90 } |
| 90 /* clang-format on */); | 91 /* clang-format on */); |
| 91 case vr_shell::ShaderID::GRADIENT_QUAD_VERTEX_SHADER: | 92 case vr_shell::ShaderID::GRADIENT_QUAD_VERTEX_SHADER: |
| 92 case vr_shell::ShaderID::GRADIENT_GRID_VERTEX_SHADER: | 93 case vr_shell::ShaderID::GRADIENT_GRID_VERTEX_SHADER: |
| 93 return SHADER( | 94 return SHADER( |
| 94 /* clang-format off */ | 95 /* clang-format off */ |
| 95 precision mediump float; | 96 precision mediump float; |
| 96 uniform mat4 u_ModelViewProjMatrix; | 97 uniform mat4 u_ModelViewProjMatrix; |
| 97 uniform float u_SceneRadius; | 98 uniform float u_SceneRadius; |
| 98 attribute vec4 a_Position; | 99 attribute vec4 a_Position; |
| 99 varying vec2 v_GridPosition; | 100 varying vec2 v_GridPosition; |
| 100 | 101 |
| 101 void main() { | 102 void main() { |
| 102 v_GridPosition = a_Position.xy / u_SceneRadius; | 103 v_GridPosition = a_Position.xy / u_SceneRadius; |
| 103 gl_Position = u_ModelViewProjMatrix * a_Position; | 104 gl_Position = u_ModelViewProjMatrix * a_Position; |
| 104 } | 105 } |
| 105 /* clang-format on */); | 106 /* clang-format on */); |
| 106 case vr_shell::ShaderID::TEXTURE_QUAD_FRAGMENT_SHADER: | 107 case vr_shell::ShaderID::EXTERNAL_TEXTURED_QUAD_FRAGMENT_SHADER: |
| 107 return OEIE_SHADER( | 108 return OEIE_SHADER( |
| 108 /* clang-format off */ | 109 /* clang-format off */ |
| 109 precision highp float; | 110 precision highp float; |
| 110 uniform samplerExternalOES u_Texture; | 111 uniform samplerExternalOES u_Texture; |
| 111 uniform vec4 u_CopyRect; // rectangle | 112 uniform vec4 u_CopyRect; // rectangle |
| 112 varying vec2 v_TexCoordinate; | 113 varying vec2 v_TexCoordinate; |
| 113 uniform lowp vec4 color; | 114 uniform lowp vec4 color; |
| 114 uniform mediump float opacity; | 115 uniform mediump float opacity; |
| 115 | 116 |
| 116 void main() { | 117 void main() { |
| 117 vec2 scaledTex = | 118 vec2 scaledTex = |
| 118 vec2(u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], | 119 vec2(u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], |
| 119 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); | 120 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); |
| 120 lowp vec4 color = texture2D(u_Texture, scaledTex); | 121 lowp vec4 color = texture2D(u_Texture, scaledTex); |
| 121 gl_FragColor = vec4(color.xyz, color.w * opacity); | 122 gl_FragColor = vec4(color.xyz, color.w * opacity); |
| 122 } | 123 } |
| 123 /* clang-format on */); | 124 /* clang-format on */); |
| 125 case vr_shell::ShaderID::TEXTURED_QUAD_FRAGMENT_SHADER: |
| 126 return SHADER( |
| 127 /* clang-format off */ |
| 128 precision highp float; |
| 129 uniform sampler2D u_Texture; |
| 130 uniform vec4 u_CopyRect; // rectangle |
| 131 varying vec2 v_TexCoordinate; |
| 132 uniform lowp vec4 color; |
| 133 uniform mediump float opacity; |
| 134 |
| 135 void main() { |
| 136 vec2 scaledTex = |
| 137 vec2(u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], |
| 138 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); |
| 139 lowp vec4 color = texture2D(u_Texture, scaledTex); |
| 140 gl_FragColor = vec4(color.xyz, color.w * opacity); |
| 141 } |
| 142 /* clang-format on */); |
| 124 case vr_shell::ShaderID::WEBVR_VERTEX_SHADER: | 143 case vr_shell::ShaderID::WEBVR_VERTEX_SHADER: |
| 125 return SHADER( | 144 return SHADER( |
| 126 /* clang-format off */ | 145 /* clang-format off */ |
| 127 precision mediump float; | 146 precision mediump float; |
| 128 attribute vec3 a_Position; | 147 attribute vec3 a_Position; |
| 129 attribute vec2 a_TexCoordinate; | 148 attribute vec2 a_TexCoordinate; |
| 130 varying highp vec2 v_TexCoordinate; | 149 varying highp vec2 v_TexCoordinate; |
| 131 | 150 |
| 132 void main() { | 151 void main() { |
| 133 v_TexCoordinate = a_TexCoordinate; | 152 v_TexCoordinate = a_TexCoordinate; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 316 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 298 } | 317 } |
| 299 | 318 |
| 300 void BaseQuadRenderer::SetVertexBuffer() { | 319 void BaseQuadRenderer::SetVertexBuffer() { |
| 301 glGenBuffersARB(1, &vertex_buffer_); | 320 glGenBuffersARB(1, &vertex_buffer_); |
| 302 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 321 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 303 glBufferData(GL_ARRAY_BUFFER, kTextureQuadVerticesSize, kTextureQuadVertices, | 322 glBufferData(GL_ARRAY_BUFFER, kTextureQuadVerticesSize, kTextureQuadVertices, |
| 304 GL_STATIC_DRAW); | 323 GL_STATIC_DRAW); |
| 305 } | 324 } |
| 306 | 325 |
| 307 TexturedQuadRenderer::TexturedQuadRenderer() | 326 ExternalTexturedQuadRenderer::ExternalTexturedQuadRenderer() |
| 308 : BaseQuadRenderer(TEXTURE_QUAD_VERTEX_SHADER, | 327 : BaseQuadRenderer(EXTERNAL_TEXTURED_QUAD_VERTEX_SHADER, |
| 309 TEXTURE_QUAD_FRAGMENT_SHADER) { | 328 EXTERNAL_TEXTURED_QUAD_FRAGMENT_SHADER) { |
| 310 model_view_proj_matrix_handle_ = | 329 model_view_proj_matrix_handle_ = |
| 311 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); | 330 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); |
| 312 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); | 331 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 332 copy_rect_uniform_handle_ = |
| 333 glGetUniformLocation(program_handle_, "u_CopyRect"); |
| 334 opacity_handle_ = glGetUniformLocation(program_handle_, "opacity"); |
| 335 } |
| 336 |
| 337 void ExternalTexturedQuadRenderer::Draw(int texture_data_handle, |
| 338 const vr::Mat4f& view_proj_matrix, |
| 339 const gfx::RectF& copy_rect, |
| 340 float opacity) { |
| 341 PrepareToDraw(model_view_proj_matrix_handle_, view_proj_matrix); |
| 342 |
| 343 // Link texture data with texture unit. |
| 344 glActiveTexture(GL_TEXTURE0); |
| 345 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); |
| 346 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 347 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 348 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 349 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 350 |
| 351 glUniform1i(tex_uniform_handle_, 0); |
| 352 glUniform4fv(copy_rect_uniform_handle_, 1, |
| 353 reinterpret_cast<const float*>(©_rect)); |
| 354 glUniform1f(opacity_handle_, opacity); |
| 355 |
| 356 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); |
| 357 |
| 358 glDisableVertexAttribArray(position_handle_); |
| 359 glDisableVertexAttribArray(tex_coord_handle_); |
| 360 } |
| 361 |
| 362 ExternalTexturedQuadRenderer::~ExternalTexturedQuadRenderer() = default; |
| 363 |
| 364 TexturedQuadRenderer::TexturedQuadRenderer() |
| 365 : BaseQuadRenderer(TEXTURED_QUAD_VERTEX_SHADER, |
| 366 TEXTURED_QUAD_FRAGMENT_SHADER) { |
| 367 model_view_proj_matrix_handle_ = |
| 368 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); |
| 369 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 313 copy_rect_uniform_handle_ = | 370 copy_rect_uniform_handle_ = |
| 314 glGetUniformLocation(program_handle_, "u_CopyRect"); | 371 glGetUniformLocation(program_handle_, "u_CopyRect"); |
| 315 opacity_handle_ = glGetUniformLocation(program_handle_, "opacity"); | 372 opacity_handle_ = glGetUniformLocation(program_handle_, "opacity"); |
| 316 } | 373 } |
| 317 | 374 |
| 318 void TexturedQuadRenderer::AddQuad(int texture_data_handle, | 375 void TexturedQuadRenderer::AddQuad(int texture_data_handle, |
| 319 const vr::Mat4f& view_proj_matrix, | 376 const vr::Mat4f& view_proj_matrix, |
| 320 const gfx::RectF& copy_rect, | 377 const gfx::RectF& copy_rect, |
| 321 float opacity) { | 378 float opacity) { |
| 322 TexturedQuad quad; | 379 SkiaQuad quad; |
| 323 quad.texture_data_handle = texture_data_handle; | 380 quad.texture_data_handle = texture_data_handle; |
| 324 quad.view_proj_matrix = view_proj_matrix; | 381 quad.view_proj_matrix = view_proj_matrix; |
| 325 quad.copy_rect = {copy_rect.x(), copy_rect.y(), copy_rect.width(), | 382 quad.copy_rect = {copy_rect.x(), copy_rect.y(), copy_rect.width(), |
| 326 copy_rect.height()}; | 383 copy_rect.height()}; |
| 327 quad.opacity = opacity; | 384 quad.opacity = opacity; |
| 328 quad_queue_.push(quad); | 385 quad_queue_.push(quad); |
| 329 } | 386 } |
| 330 | 387 |
| 331 void TexturedQuadRenderer::Flush() { | 388 void TexturedQuadRenderer::Flush() { |
| 332 if (quad_queue_.empty()) | 389 if (quad_queue_.empty()) |
| (...skipping 23 matching lines...) Expand all Loading... |
| 356 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 413 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 357 | 414 |
| 358 // Link texture data with texture unit. | 415 // Link texture data with texture unit. |
| 359 glActiveTexture(GL_TEXTURE0); | 416 glActiveTexture(GL_TEXTURE0); |
| 360 glUniform1i(tex_uniform_handle_, 0); | 417 glUniform1i(tex_uniform_handle_, 0); |
| 361 | 418 |
| 362 // TODO(bajones): This should eventually be changed to use instancing so that | 419 // TODO(bajones): This should eventually be changed to use instancing so that |
| 363 // the entire queue can be processed in one draw call. For now this still | 420 // the entire queue can be processed in one draw call. For now this still |
| 364 // significantly reduces the amount of state changes made per draw. | 421 // significantly reduces the amount of state changes made per draw. |
| 365 while (!quad_queue_.empty()) { | 422 while (!quad_queue_.empty()) { |
| 366 const TexturedQuad& quad = quad_queue_.front(); | 423 const SkiaQuad& quad = quad_queue_.front(); |
| 367 | 424 |
| 368 // Only change texture ID or opacity when they differ between quads. | 425 // Only change texture ID or opacity when they differ between quads. |
| 369 if (last_texture != quad.texture_data_handle) { | 426 if (last_texture != quad.texture_data_handle) { |
| 370 last_texture = quad.texture_data_handle; | 427 last_texture = quad.texture_data_handle; |
| 371 glBindTexture(GL_TEXTURE_EXTERNAL_OES, last_texture); | 428 glBindTexture(GL_TEXTURE_2D, last_texture); |
| 372 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, | |
| 373 GL_CLAMP_TO_EDGE); | |
| 374 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, | |
| 375 GL_CLAMP_TO_EDGE); | |
| 376 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, | |
| 377 GL_LINEAR); | |
| 378 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, | |
| 379 GL_NEAREST); | |
| 380 } | 429 } |
| 381 | 430 |
| 382 if (last_opacity != quad.opacity) { | 431 if (last_opacity != quad.opacity) { |
| 383 last_opacity = quad.opacity; | 432 last_opacity = quad.opacity; |
| 384 glUniform1f(opacity_handle_, last_opacity); | 433 glUniform1f(opacity_handle_, last_opacity); |
| 385 } | 434 } |
| 386 | 435 |
| 387 // Pass in model view project matrix. | 436 // Pass in model view project matrix. |
| 388 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false, | 437 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false, |
| 389 MatrixToGLArray(quad.view_proj_matrix).data()); | 438 MatrixToGLArray(quad.view_proj_matrix).data()); |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 727 | 776 |
| 728 size_t vertex_buffer_size = sizeof(Line3d) * linesNumber; | 777 size_t vertex_buffer_size = sizeof(Line3d) * linesNumber; |
| 729 | 778 |
| 730 glGenBuffersARB(1, &vertex_buffer_); | 779 glGenBuffersARB(1, &vertex_buffer_); |
| 731 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | 780 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 732 glBufferData(GL_ARRAY_BUFFER, vertex_buffer_size, grid_lines_.data(), | 781 glBufferData(GL_ARRAY_BUFFER, vertex_buffer_size, grid_lines_.data(), |
| 733 GL_STATIC_DRAW); | 782 GL_STATIC_DRAW); |
| 734 } | 783 } |
| 735 | 784 |
| 736 VrShellRenderer::VrShellRenderer() | 785 VrShellRenderer::VrShellRenderer() |
| 737 : textured_quad_renderer_(base::MakeUnique<TexturedQuadRenderer>()), | 786 : external_textured_quad_renderer_( |
| 787 base::MakeUnique<ExternalTexturedQuadRenderer>()), |
| 788 textured_quad_renderer_(base::MakeUnique<TexturedQuadRenderer>()), |
| 738 webvr_renderer_(base::MakeUnique<WebVrRenderer>()), | 789 webvr_renderer_(base::MakeUnique<WebVrRenderer>()), |
| 739 reticle_renderer_(base::MakeUnique<ReticleRenderer>()), | 790 reticle_renderer_(base::MakeUnique<ReticleRenderer>()), |
| 740 laser_renderer_(base::MakeUnique<LaserRenderer>()), | 791 laser_renderer_(base::MakeUnique<LaserRenderer>()), |
| 741 controller_renderer_(base::MakeUnique<ControllerRenderer>()), | 792 controller_renderer_(base::MakeUnique<ControllerRenderer>()), |
| 742 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), | 793 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), |
| 743 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { | 794 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { |
| 744 BaseQuadRenderer::SetVertexBuffer(); | 795 BaseQuadRenderer::SetVertexBuffer(); |
| 745 } | 796 } |
| 746 | 797 |
| 747 VrShellRenderer::~VrShellRenderer() = default; | 798 VrShellRenderer::~VrShellRenderer() = default; |
| 748 | 799 |
| 800 ExternalTexturedQuadRenderer* |
| 801 VrShellRenderer::GetExternalTexturedQuadRenderer() { |
| 802 Flush(); |
| 803 return external_textured_quad_renderer_.get(); |
| 804 } |
| 805 |
| 806 TexturedQuadRenderer* VrShellRenderer::GetTexturedQuadRenderer() { |
| 807 return textured_quad_renderer_.get(); |
| 808 } |
| 809 |
| 810 WebVrRenderer* VrShellRenderer::GetWebVrRenderer() { |
| 811 Flush(); |
| 812 return webvr_renderer_.get(); |
| 813 } |
| 814 |
| 815 ReticleRenderer* VrShellRenderer::GetReticleRenderer() { |
| 816 Flush(); |
| 817 return reticle_renderer_.get(); |
| 818 } |
| 819 |
| 820 LaserRenderer* VrShellRenderer::GetLaserRenderer() { |
| 821 Flush(); |
| 822 return laser_renderer_.get(); |
| 823 } |
| 824 |
| 825 ControllerRenderer* VrShellRenderer::GetControllerRenderer() { |
| 826 Flush(); |
| 827 return controller_renderer_.get(); |
| 828 } |
| 829 |
| 830 GradientQuadRenderer* VrShellRenderer::GetGradientQuadRenderer() { |
| 831 Flush(); |
| 832 return gradient_quad_renderer_.get(); |
| 833 } |
| 834 |
| 835 GradientGridRenderer* VrShellRenderer::GetGradientGridRenderer() { |
| 836 Flush(); |
| 837 return gradient_grid_renderer_.get(); |
| 838 } |
| 839 |
| 840 void VrShellRenderer::Flush() { |
| 841 textured_quad_renderer_->Flush(); |
| 842 } |
| 843 |
| 749 } // namespace vr_shell | 844 } // namespace vr_shell |
| OLD | NEW |