| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 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 "cc/output/geometry_binding.h" | 5 #include "cc/output/geometry_binding.h" |
| 6 | 6 |
| 7 #include "cc/output/gl_renderer.h" // For the GLC() macro. | 7 #include "cc/output/gl_renderer.h" // For the GLC() macro. |
| 8 #include "gpu/command_buffer/client/gles2_interface.h" | 8 #include "gpu/command_buffer/client/gles2_interface.h" |
| 9 #include "ui/gfx/geometry/rect_f.h" | 9 #include "ui/gfx/geometry/rect_f.h" |
| 10 | 10 |
| 11 namespace cc { | 11 namespace cc { |
| 12 | 12 |
| 13 GeometryBinding::GeometryBinding(gpu::gles2::GLES2Interface* gl, | 13 struct Vertex { |
| 14 const gfx::RectF& quad_vertex_rect) | |
| 15 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) { | |
| 16 struct Vertex { | |
| 17 float a_position[3]; | 14 float a_position[3]; |
| 18 float a_texCoord[2]; | 15 float a_texCoord[2]; |
| 19 // Index of the vertex, divide by 4 to have the matrix for this quad. | 16 // Index of the vertex, divide by 4 to have the matrix for this quad. |
| 20 float a_index; | 17 float a_index; |
| 21 }; | 18 }; |
| 22 struct Quad { | 19 |
| 23 Vertex v0, v1, v2, v3; | 20 struct Quad { |
| 24 }; | 21 Vertex v0, v1, v2, v3; |
| 25 struct QuadIndex { | 22 }; |
| 26 uint16 data[6]; | 23 |
| 27 }; | 24 struct QuadIndex { |
| 25 uint16 data[6]; |
| 26 }; |
| 27 |
| 28 StaticGeometryBinding::StaticGeometryBinding(gpu::gles2::GLES2Interface* gl, |
| 29 const gfx::RectF& quad_vertex_rect) |
| 30 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) { |
| 31 Quad quads[8]; |
| 32 QuadIndex quad_indices[8]; |
| 28 | 33 |
| 29 static_assert(sizeof(Quad) == 24 * sizeof(float), | 34 static_assert(sizeof(Quad) == 24 * sizeof(float), |
| 30 "struct Quad should be densely packed"); | 35 "struct Quad should be densely packed"); |
| 31 static_assert(sizeof(QuadIndex) == 6 * sizeof(uint16_t), | 36 static_assert(sizeof(QuadIndex) == 6 * sizeof(uint16_t), |
| 32 "struct QuadIndex should be densely packed"); | 37 "struct QuadIndex should be densely packed"); |
| 33 | 38 |
| 34 Quad quad_list[8]; | 39 for (size_t i = 0; i < 8; i++) { |
| 35 QuadIndex quad_index_list[8]; | 40 Vertex v0 = {{quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f}, |
| 36 for (int i = 0; i < 8; i++) { | 41 {0.0f, 1.0f}, |
| 37 Vertex v0 = {{quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f, }, | 42 i * 4.0f + 0.0f}; |
| 38 {0.0f, 1.0f, }, i * 4.0f + 0.0f}; | 43 Vertex v1 = {{quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f}, |
| 39 Vertex v1 = {{quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f, }, | 44 {0.0f, 0.0f}, |
| 40 {0.0f, 0.0f, }, i * 4.0f + 1.0f}; | 45 i * 4.0f + 1.0f}; |
| 41 Vertex v2 = {{quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f, }, | 46 Vertex v2 = {{quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f}, |
| 42 {1.0f, .0f, }, i * 4.0f + 2.0f}; | 47 {1.0f, 0.0f}, |
| 43 Vertex v3 = {{quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f, }, | 48 i * 4.0f + 2.0f}; |
| 44 {1.0f, 1.0f, }, i * 4.0f + 3.0f}; | 49 Vertex v3 = {{quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f}, |
| 50 {1.0f, 1.0f}, |
| 51 i * 4.0f + 3.0f}; |
| 45 Quad x = {v0, v1, v2, v3}; | 52 Quad x = {v0, v1, v2, v3}; |
| 46 quad_list[i] = x; | 53 quads[i] = x; |
| 47 QuadIndex y = { | 54 QuadIndex y = { |
| 48 {static_cast<uint16>(0 + 4 * i), static_cast<uint16>(1 + 4 * i), | 55 {static_cast<uint16>(0 + 4 * i), static_cast<uint16>(1 + 4 * i), |
| 49 static_cast<uint16>(2 + 4 * i), static_cast<uint16>(3 + 4 * i), | 56 static_cast<uint16>(2 + 4 * i), static_cast<uint16>(3 + 4 * i), |
| 50 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}}; | 57 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}}; |
| 51 quad_index_list[i] = y; | 58 quad_indices[i] = y; |
| 52 } | 59 } |
| 53 | 60 |
| 54 gl_->GenBuffers(1, &quad_vertices_vbo_); | 61 GLC(gl_, gl_->GenBuffers(1, &quad_vertices_vbo_)); |
| 55 gl_->GenBuffers(1, &quad_elements_vbo_); | 62 GLC(gl_, gl_->GenBuffers(1, &quad_elements_vbo_)); |
| 63 |
| 56 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); | 64 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); |
| 57 GLC(gl_, | 65 GLC(gl_, gl_->BufferData(GL_ARRAY_BUFFER, sizeof(Quad) * 8, quads, |
| 58 gl_->BufferData( | 66 GL_STATIC_DRAW)); |
| 59 GL_ARRAY_BUFFER, sizeof(quad_list), quad_list, GL_STATIC_DRAW)); | 67 |
| 60 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); | 68 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); |
| 61 GLC(gl_, | 69 GLC(gl_, gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(QuadIndex) * 8, |
| 62 gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, | 70 &quad_indices, GL_STATIC_DRAW)); |
| 63 sizeof(quad_index_list), | |
| 64 quad_index_list, | |
| 65 GL_STATIC_DRAW)); | |
| 66 } | 71 } |
| 67 | 72 |
| 68 GeometryBinding::~GeometryBinding() { | 73 void SetupGLContext(gpu::gles2::GLES2Interface* gl, |
| 69 gl_->DeleteBuffers(1, &quad_vertices_vbo_); | 74 GLuint quad_elements_vbo, |
| 70 gl_->DeleteBuffers(1, &quad_elements_vbo_); | 75 GLuint quad_vertices_vbo) { |
| 71 } | 76 GLC(gl, gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo)); |
| 72 | 77 |
| 73 void GeometryBinding::PrepareForDraw() { | 78 GLC(gl, gl->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo)); |
| 74 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); | |
| 75 | |
| 76 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); | |
| 77 // OpenGL defines the last parameter to VertexAttribPointer as type | 79 // OpenGL defines the last parameter to VertexAttribPointer as type |
| 78 // "const GLvoid*" even though it is actually an offset into the buffer | 80 // "const GLvoid*" even though it is actually an offset into the buffer |
| 79 // object's data store and not a pointer to the client's address space. | 81 // object's data store and not a pointer to the client's address space. |
| 80 const void* offsets[3] = { | 82 const void* offsets[3] = { |
| 81 0, | 83 0, |
| 82 reinterpret_cast<const void*>(3 * sizeof(float)), | 84 reinterpret_cast<const void*>(3 * sizeof(float)), |
| 83 reinterpret_cast<const void*>(5 * sizeof(float)), | 85 reinterpret_cast<const void*>(5 * sizeof(float)), |
| 84 }; | 86 }; |
| 85 | 87 |
| 86 GLC(gl_, gl_->VertexAttribPointer(PositionAttribLocation(), 3, GL_FLOAT, | 88 GLC(gl, |
| 87 false, 6 * sizeof(float), offsets[0])); | 89 gl->VertexAttribPointer(GeometryBinding::PositionAttribLocation(), 3, |
| 88 GLC(gl_, gl_->VertexAttribPointer(TexCoordAttribLocation(), 2, GL_FLOAT, | 90 GL_FLOAT, false, 6 * sizeof(float), offsets[0])); |
| 89 false, 6 * sizeof(float), offsets[1])); | 91 GLC(gl, |
| 90 GLC(gl_, gl_->VertexAttribPointer(TriangleIndexAttribLocation(), 1, GL_FLOAT, | 92 gl->VertexAttribPointer(GeometryBinding::TexCoordAttribLocation(), 2, |
| 91 false, 6 * sizeof(float), offsets[2])); | 93 GL_FLOAT, false, 6 * sizeof(float), offsets[1])); |
| 92 GLC(gl_, gl_->EnableVertexAttribArray(PositionAttribLocation())); | 94 GLC(gl, |
| 93 GLC(gl_, gl_->EnableVertexAttribArray(TexCoordAttribLocation())); | 95 gl->VertexAttribPointer(GeometryBinding::TriangleIndexAttribLocation(), 1, |
| 94 GLC(gl_, gl_->EnableVertexAttribArray(TriangleIndexAttribLocation())); | 96 GL_FLOAT, false, 6 * sizeof(float), offsets[2])); |
| 97 GLC(gl, |
| 98 gl->EnableVertexAttribArray(GeometryBinding::PositionAttribLocation())); |
| 99 GLC(gl, |
| 100 gl->EnableVertexAttribArray(GeometryBinding::TexCoordAttribLocation())); |
| 101 GLC(gl, gl->EnableVertexAttribArray( |
| 102 GeometryBinding::TriangleIndexAttribLocation())); |
| 103 } |
| 104 |
| 105 StaticGeometryBinding::~StaticGeometryBinding() { |
| 106 gl_->DeleteBuffers(1, &quad_vertices_vbo_); |
| 107 gl_->DeleteBuffers(1, &quad_elements_vbo_); |
| 108 } |
| 109 |
| 110 void StaticGeometryBinding::PrepareForDraw() { |
| 111 SetupGLContext(gl_, quad_elements_vbo_, quad_vertices_vbo_); |
| 112 } |
| 113 |
| 114 DynamicGeometryBinding::DynamicGeometryBinding(gpu::gles2::GLES2Interface* gl) |
| 115 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) { |
| 116 Quad quads[8]; |
| 117 QuadIndex quad_indices[8]; |
| 118 |
| 119 static_assert(sizeof(Quad) == 24 * sizeof(float), |
| 120 "struct Quad should be densely packed"); |
| 121 static_assert(sizeof(QuadIndex) == 6 * sizeof(uint16_t), |
| 122 "struct QuadIndex should be densely packed"); |
| 123 |
| 124 GLC(gl_, gl_->GenBuffers(1, &quad_vertices_vbo_)); |
| 125 GLC(gl_, gl_->GenBuffers(1, &quad_elements_vbo_)); |
| 126 |
| 127 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); |
| 128 GLC(gl_, gl_->BufferData(GL_ARRAY_BUFFER, sizeof(Quad) * 1, quads, |
| 129 GL_DYNAMIC_DRAW)); |
| 130 |
| 131 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); |
| 132 GLC(gl_, gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(QuadIndex) * 1, |
| 133 &quad_indices, GL_DYNAMIC_DRAW)); |
| 134 } |
| 135 |
| 136 void DynamicGeometryBinding::InitializeCustomQuad(const gfx::QuadF& quad) { |
| 137 float uv[] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}; |
| 138 InitializeCustomQuadWithUVs(quad, uv); |
| 139 } |
| 140 |
| 141 void DynamicGeometryBinding::InitializeCustomQuadWithUVs(const gfx::QuadF& quad, |
| 142 const float uv[8]) { |
| 143 Vertex v0 = {{quad.p1().x(), quad.p1().y(), 0.0f}, {uv[0], uv[1]}, 0.0f}; |
| 144 Vertex v1 = {{quad.p2().x(), quad.p2().y(), 0.0f}, {uv[2], uv[3]}, 1.0f}; |
| 145 Vertex v2 = {{quad.p3().x(), quad.p3().y(), 0.0f}, {uv[4], uv[5]}, 2.0f}; |
| 146 Vertex v3 = {{quad.p4().x(), quad.p4().y(), 0.0f}, {uv[6], uv[7]}, 3.0f}; |
| 147 |
| 148 Quad local_quad = {v0, v1, v2, v3}; |
| 149 QuadIndex quad_index = {{static_cast<uint16>(0), |
| 150 static_cast<uint16>(1), |
| 151 static_cast<uint16>(2), |
| 152 static_cast<uint16>(3), |
| 153 static_cast<uint16>(0), |
| 154 static_cast<uint16>(2)}}; |
| 155 |
| 156 GLC(gl_, gl_->BufferSubData(GL_ARRAY_BUFFER, 0, sizeof(Quad), &local_quad)); |
| 157 GLC(gl_, gl_->BufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(QuadIndex), |
| 158 &quad_index)); |
| 159 } |
| 160 |
| 161 void DynamicGeometryBinding::PrepareForDraw() { |
| 162 SetupGLContext(gl_, quad_elements_vbo_, quad_vertices_vbo_); |
| 95 } | 163 } |
| 96 | 164 |
| 97 } // namespace cc | 165 } // namespace cc |
| OLD | NEW |