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 <math.h> | 7 #include <math.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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::TEXTURE_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::SKIA_QUAD_VERTEX_SHADER: | |
| 78 return SHADER( | 79 return SHADER( |
| 79 /* clang-format off */ | 80 /* clang-format off */ |
| 80 uniform mat4 u_ModelViewProjMatrix; | 81 uniform mat4 u_ModelViewProjMatrix; |
| 81 attribute vec4 a_Position; | 82 attribute vec4 a_Position; |
| 82 attribute vec2 a_TexCoordinate; | 83 attribute vec2 a_TexCoordinate; |
| 83 varying vec2 v_TexCoordinate; | 84 varying vec2 v_TexCoordinate; |
| 84 | 85 |
| 85 void main() { | 86 void main() { |
| 86 v_TexCoordinate = a_TexCoordinate; | 87 v_TexCoordinate = a_TexCoordinate; |
| 87 gl_Position = u_ModelViewProjMatrix * a_Position; | 88 gl_Position = u_ModelViewProjMatrix * a_Position; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 112 uniform mediump float opacity; | 113 uniform mediump float opacity; |
| 113 | 114 |
| 114 void main() { | 115 void main() { |
| 115 vec2 scaledTex = | 116 vec2 scaledTex = |
| 116 vec2(u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], | 117 vec2(u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], |
| 117 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); | 118 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); |
| 118 lowp vec4 color = texture2D(u_Texture, scaledTex); | 119 lowp vec4 color = texture2D(u_Texture, scaledTex); |
| 119 gl_FragColor = vec4(color.xyz, color.w * opacity); | 120 gl_FragColor = vec4(color.xyz, color.w * opacity); |
| 120 } | 121 } |
| 121 /* clang-format on */); | 122 /* clang-format on */); |
| 123 case vr_shell::ShaderID::SKIA_QUAD_FRAGMENT_SHADER: | |
| 124 return OEIE_SHADER( | |
| 125 /* clang-format off */ | |
| 126 precision highp float; | |
| 127 uniform sampler2D u_Texture; | |
| 128 uniform vec4 u_CopyRect; // rectangle | |
| 129 varying vec2 v_TexCoordinate; | |
| 130 uniform lowp vec4 color; | |
| 131 uniform mediump float opacity; | |
| 132 | |
| 133 void main() { | |
| 134 vec2 scaledTex = | |
| 135 vec2(u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2], | |
| 136 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]); | |
| 137 lowp vec4 color = texture2D(u_Texture, scaledTex); | |
| 138 gl_FragColor = vec4(color.xyz, color.w * opacity); | |
| 139 } | |
| 140 /* clang-format on */); | |
| 122 case vr_shell::ShaderID::WEBVR_VERTEX_SHADER: | 141 case vr_shell::ShaderID::WEBVR_VERTEX_SHADER: |
| 123 return SHADER( | 142 return SHADER( |
| 124 /* clang-format off */ | 143 /* clang-format off */ |
| 125 attribute vec3 a_Position; | 144 attribute vec3 a_Position; |
| 126 attribute vec2 a_TexCoordinate; | 145 attribute vec2 a_TexCoordinate; |
| 127 varying vec2 v_TexCoordinate; | 146 varying vec2 v_TexCoordinate; |
| 128 | 147 |
| 129 void main() { | 148 void main() { |
| 130 v_TexCoordinate = a_TexCoordinate; | 149 v_TexCoordinate = a_TexCoordinate; |
| 131 gl_Position = vec4(a_Position * 2.0, 1.0); | 150 gl_Position = vec4(a_Position * 2.0, 1.0); |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 | 410 |
| 392 quad_queue_.pop(); | 411 quad_queue_.pop(); |
| 393 } | 412 } |
| 394 | 413 |
| 395 glDisableVertexAttribArray(position_handle_); | 414 glDisableVertexAttribArray(position_handle_); |
| 396 glDisableVertexAttribArray(tex_coord_handle_); | 415 glDisableVertexAttribArray(tex_coord_handle_); |
| 397 } | 416 } |
| 398 | 417 |
| 399 TexturedQuadRenderer::~TexturedQuadRenderer() = default; | 418 TexturedQuadRenderer::~TexturedQuadRenderer() = default; |
| 400 | 419 |
| 420 SkiaQuadRenderer::SkiaQuadRenderer() | |
| 421 : BaseQuadRenderer(SKIA_QUAD_VERTEX_SHADER, | |
| 422 SKIA_QUAD_FRAGMENT_SHADER) { | |
| 423 model_view_proj_matrix_handle_ = | |
| 424 glGetUniformLocation(program_handle_, "u_ModelViewProjMatrix"); | |
| 425 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); | |
| 426 copy_rect_uniform_handle_ = | |
| 427 glGetUniformLocation(program_handle_, "u_CopyRect"); | |
| 428 opacity_handle_ = glGetUniformLocation(program_handle_, "opacity"); | |
| 429 } | |
| 430 | |
| 431 void SkiaQuadRenderer::Draw(int texture_data_handle, | |
|
cjgrant
2017/04/19 16:24:52
This is Flush(), refactored into a queue-less equi
| |
| 432 const vr::Mat4f& view_proj_matrix, | |
| 433 const gfx::RectF& copy_rect, | |
| 434 float opacity) { | |
| 435 TexturedQuad quad; | |
| 436 quad.texture_data_handle = texture_data_handle; | |
| 437 quad.view_proj_matrix = view_proj_matrix; | |
| 438 quad.copy_rect = {copy_rect.x(), copy_rect.y(), copy_rect.width(), | |
| 439 copy_rect.height()}; | |
| 440 quad.opacity = opacity; | |
| 441 | |
| 442 // Set up GL state that doesn't change between draw calls. | |
| 443 glUseProgram(program_handle_); | |
| 444 | |
| 445 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 446 | |
| 447 // Set up position attribute. | |
| 448 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, | |
| 449 kTextureQuadDataStride, | |
| 450 VOID_OFFSET(kPositionDataOffset)); | |
| 451 glEnableVertexAttribArray(position_handle_); | |
| 452 | |
| 453 // Set up texture coordinate attribute. | |
| 454 glVertexAttribPointer(tex_coord_handle_, kTextureCoordinateDataSize, GL_FLOAT, | |
| 455 false, kTextureQuadDataStride, | |
| 456 VOID_OFFSET(kTextureCoordinateDataOffset)); | |
| 457 glEnableVertexAttribArray(tex_coord_handle_); | |
| 458 | |
| 459 glEnable(GL_BLEND); | |
| 460 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| 461 | |
| 462 // Link texture data with texture unit. | |
| 463 glActiveTexture(GL_TEXTURE0); | |
| 464 glUniform1i(tex_uniform_handle_, 0); | |
| 465 | |
| 466 glBindTexture(GL_TEXTURE_EXTERNAL_OES, quad.texture_data_handle); | |
| 467 glUniform1f(opacity_handle_, quad.opacity); | |
| 468 | |
| 469 // Pass in model view project matrix. | |
| 470 glUniformMatrix4fv(model_view_proj_matrix_handle_, 1, false, | |
| 471 MatrixToGLArray(quad.view_proj_matrix).data()); | |
| 472 | |
| 473 // Pass in the copy rect. | |
| 474 glUniform4fv(copy_rect_uniform_handle_, 1, | |
| 475 reinterpret_cast<const float*>(&quad.copy_rect)); | |
| 476 | |
| 477 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); | |
| 478 | |
| 479 glDisableVertexAttribArray(position_handle_); | |
| 480 glDisableVertexAttribArray(tex_coord_handle_); | |
| 481 } | |
| 482 | |
| 483 SkiaQuadRenderer::~SkiaQuadRenderer() = default; | |
| 484 | |
| 401 WebVrRenderer::WebVrRenderer() | 485 WebVrRenderer::WebVrRenderer() |
| 402 : BaseQuadRenderer(WEBVR_VERTEX_SHADER, WEBVR_FRAGMENT_SHADER) { | 486 : BaseQuadRenderer(WEBVR_VERTEX_SHADER, WEBVR_FRAGMENT_SHADER) { |
| 403 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); | 487 tex_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); |
| 404 } | 488 } |
| 405 | 489 |
| 406 // Draw the stereo WebVR frame | 490 // Draw the stereo WebVR frame |
| 407 void WebVrRenderer::Draw(int texture_handle) { | 491 void WebVrRenderer::Draw(int texture_handle) { |
| 408 glUseProgram(program_handle_); | 492 glUseProgram(program_handle_); |
| 409 | 493 |
| 410 // Bind vertex attributes | 494 // Bind vertex attributes |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 735 laser_renderer_(base::MakeUnique<LaserRenderer>()), | 819 laser_renderer_(base::MakeUnique<LaserRenderer>()), |
| 736 controller_renderer_(base::MakeUnique<ControllerRenderer>()), | 820 controller_renderer_(base::MakeUnique<ControllerRenderer>()), |
| 737 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), | 821 gradient_quad_renderer_(base::MakeUnique<GradientQuadRenderer>()), |
| 738 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { | 822 gradient_grid_renderer_(base::MakeUnique<GradientGridRenderer>()) { |
| 739 BaseQuadRenderer::SetVertexBuffer(); | 823 BaseQuadRenderer::SetVertexBuffer(); |
| 740 } | 824 } |
| 741 | 825 |
| 742 VrShellRenderer::~VrShellRenderer() = default; | 826 VrShellRenderer::~VrShellRenderer() = default; |
| 743 | 827 |
| 744 } // namespace vr_shell | 828 } // namespace vr_shell |
| OLD | NEW |