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 void SetupGLContext(gpu::gles2::GLES2Interface* gl, |
14 const gfx::RectF& quad_vertex_rect) | 14 GLuint quad_elements_vbo, |
15 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) { | 15 GLuint quad_vertices_vbo) { |
16 struct Vertex { | 16 GLC(gl, gl->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo)); |
17 float a_position[3]; | |
18 float a_texCoord[2]; | |
19 // Index of the vertex, divide by 4 to have the matrix for this quad. | |
20 float a_index; | |
21 }; | |
22 struct Quad { | |
23 Vertex v0, v1, v2, v3; | |
24 }; | |
25 struct QuadIndex { | |
26 uint16 data[6]; | |
27 }; | |
28 | 17 |
29 static_assert(sizeof(Quad) == 24 * sizeof(float), | 18 GLC(gl, gl->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo)); |
30 "struct Quad should be densely packed"); | |
31 static_assert(sizeof(QuadIndex) == 6 * sizeof(uint16_t), | |
32 "struct QuadIndex should be densely packed"); | |
33 | |
34 Quad quad_list[8]; | |
35 QuadIndex quad_index_list[8]; | |
36 for (int i = 0; i < 8; i++) { | |
37 Vertex v0 = {{quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f, }, | |
38 {0.0f, 1.0f, }, i * 4.0f + 0.0f}; | |
39 Vertex v1 = {{quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f, }, | |
40 {0.0f, 0.0f, }, i * 4.0f + 1.0f}; | |
41 Vertex v2 = {{quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f, }, | |
42 {1.0f, .0f, }, i * 4.0f + 2.0f}; | |
43 Vertex v3 = {{quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f, }, | |
44 {1.0f, 1.0f, }, i * 4.0f + 3.0f}; | |
45 Quad x = {v0, v1, v2, v3}; | |
46 quad_list[i] = x; | |
47 QuadIndex y = { | |
48 {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), | |
50 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}}; | |
51 quad_index_list[i] = y; | |
52 } | |
53 | |
54 gl_->GenBuffers(1, &quad_vertices_vbo_); | |
55 gl_->GenBuffers(1, &quad_elements_vbo_); | |
56 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); | |
57 GLC(gl_, | |
58 gl_->BufferData( | |
59 GL_ARRAY_BUFFER, sizeof(quad_list), quad_list, GL_STATIC_DRAW)); | |
60 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); | |
61 GLC(gl_, | |
62 gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, | |
63 sizeof(quad_index_list), | |
64 quad_index_list, | |
65 GL_STATIC_DRAW)); | |
66 } | |
67 | |
68 GeometryBinding::~GeometryBinding() { | |
69 gl_->DeleteBuffers(1, &quad_vertices_vbo_); | |
70 gl_->DeleteBuffers(1, &quad_elements_vbo_); | |
71 } | |
72 | |
73 void GeometryBinding::PrepareForDraw() { | |
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 | 19 // OpenGL defines the last parameter to VertexAttribPointer as type |
78 // "const GLvoid*" even though it is actually an offset into the buffer | 20 // "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. | 21 // object's data store and not a pointer to the client's address space. |
80 const void* offsets[3] = { | 22 const void* offsets[3] = { |
81 0, | 23 0, |
82 reinterpret_cast<const void*>(3 * sizeof(float)), | 24 reinterpret_cast<const void*>(3 * sizeof(float)), |
83 reinterpret_cast<const void*>(5 * sizeof(float)), | 25 reinterpret_cast<const void*>(5 * sizeof(float)), |
84 }; | 26 }; |
85 | 27 |
86 GLC(gl_, gl_->VertexAttribPointer(PositionAttribLocation(), 3, GL_FLOAT, | 28 GLC(gl, |
87 false, 6 * sizeof(float), offsets[0])); | 29 gl->VertexAttribPointer(GeometryBinding::PositionAttribLocation(), 3, |
88 GLC(gl_, gl_->VertexAttribPointer(TexCoordAttribLocation(), 2, GL_FLOAT, | 30 GL_FLOAT, false, 6 * sizeof(float), offsets[0])); |
89 false, 6 * sizeof(float), offsets[1])); | 31 GLC(gl, |
90 GLC(gl_, gl_->VertexAttribPointer(TriangleIndexAttribLocation(), 1, GL_FLOAT, | 32 gl->VertexAttribPointer(GeometryBinding::TexCoordAttribLocation(), 2, |
91 false, 6 * sizeof(float), offsets[2])); | 33 GL_FLOAT, false, 6 * sizeof(float), offsets[1])); |
92 GLC(gl_, gl_->EnableVertexAttribArray(PositionAttribLocation())); | 34 GLC(gl, |
93 GLC(gl_, gl_->EnableVertexAttribArray(TexCoordAttribLocation())); | 35 gl->VertexAttribPointer(GeometryBinding::TriangleIndexAttribLocation(), 1, |
94 GLC(gl_, gl_->EnableVertexAttribArray(TriangleIndexAttribLocation())); | 36 GL_FLOAT, false, 6 * sizeof(float), offsets[2])); |
| 37 GLC(gl, |
| 38 gl->EnableVertexAttribArray(GeometryBinding::PositionAttribLocation())); |
| 39 GLC(gl, |
| 40 gl->EnableVertexAttribArray(GeometryBinding::TexCoordAttribLocation())); |
| 41 GLC(gl, gl->EnableVertexAttribArray( |
| 42 GeometryBinding::TriangleIndexAttribLocation())); |
| 43 } |
| 44 |
| 45 GeometryBindingQuad::GeometryBindingQuad() { |
| 46 v0 = {{0, 0, 0}, {0, 0}, 0}; |
| 47 v1 = {{0, 0, 0}, {0, 0}, 0}; |
| 48 v2 = {{0, 0, 0}, {0, 0}, 0}; |
| 49 v3 = {{0, 0, 0}, {0, 0}, 0}; |
| 50 } |
| 51 |
| 52 GeometryBindingQuad::GeometryBindingQuad(const GeometryBindingVertex& vert0, |
| 53 const GeometryBindingVertex& vert1, |
| 54 const GeometryBindingVertex& vert2, |
| 55 const GeometryBindingVertex& vert3) { |
| 56 v0 = vert0; |
| 57 v1 = vert1; |
| 58 v2 = vert2; |
| 59 v3 = vert3; |
| 60 } |
| 61 |
| 62 GeometryBindingQuadIndex::GeometryBindingQuadIndex() { |
| 63 memset(data, 0x0, sizeof(data)); |
| 64 } |
| 65 |
| 66 GeometryBindingQuadIndex::GeometryBindingQuadIndex(uint16 index0, |
| 67 uint16 index1, |
| 68 uint16 index2, |
| 69 uint16 index3, |
| 70 uint16 index4, |
| 71 uint16 index5) { |
| 72 data[0] = index0; |
| 73 data[1] = index1; |
| 74 data[2] = index2; |
| 75 data[3] = index3; |
| 76 data[4] = index4; |
| 77 data[5] = index5; |
95 } | 78 } |
96 | 79 |
97 } // namespace cc | 80 } // namespace cc |
OLD | NEW |