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 3 // found in the LICENSE file. 4 5 #include "cc/output/geometry_binding.h" 6 7 #include "cc/output/gl_renderer.h" // For the GLC() macro. 8 #include "gpu/command_buffer/client/gles2_interface.h" 9 #include "third_party/khronos/GLES2/gl2.h" 10 #include "ui/gfx/rect_f.h" 11 12 namespace cc { 13 14 GeometryBinding::GeometryBinding(gpu::gles2::GLES2Interface* gl, 15 const gfx::RectF& quad_vertex_rect) 16 : gl_(gl), quad_vertices_vbo_(0), quad_elements_vbo_(0) { 17 struct Vertex { 18 float a_position[3]; 19 float a_texCoord[2]; 20 // Index of the vertex, divide by 4 to have the matrix for this quad. 21 float a_index; 22 }; 23 struct Quad { 24 Vertex v0, v1, v2, v3; 25 }; 26 struct QuadIndex { 27 uint16 data[6]; 28 }; 29 30 COMPILE_ASSERT(sizeof(Quad) == 24 * sizeof(float), // NOLINT(runtime/sizeof) 31 struct_is_densely_packed); 32 COMPILE_ASSERT( 33 sizeof(QuadIndex) == 6 * sizeof(uint16_t), // NOLINT(runtime/sizeof) 34 struct_is_densely_packed); 35 36 Quad quad_list[8]; 37 QuadIndex quad_index_list[8]; 38 for (int i = 0; i < 8; i++) { 39 Vertex v0 = {{quad_vertex_rect.x(), quad_vertex_rect.bottom(), 0.0f, }, 40 {0.0f, 1.0f, }, i * 4.0f + 0.0f}; 41 Vertex v1 = {{quad_vertex_rect.x(), quad_vertex_rect.y(), 0.0f, }, 42 {0.0f, 0.0f, }, i * 4.0f + 1.0f}; 43 Vertex v2 = {{quad_vertex_rect.right(), quad_vertex_rect.y(), 0.0f, }, 44 {1.0f, .0f, }, i * 4.0f + 2.0f}; 45 Vertex v3 = {{quad_vertex_rect.right(), quad_vertex_rect.bottom(), 0.0f, }, 46 {1.0f, 1.0f, }, i * 4.0f + 3.0f}; 47 Quad x = {v0, v1, v2, v3}; 48 quad_list[i] = x; 49 QuadIndex y = { 50 {static_cast<uint16>(0 + 4 * i), static_cast<uint16>(1 + 4 * i), 51 static_cast<uint16>(2 + 4 * i), static_cast<uint16>(3 + 4 * i), 52 static_cast<uint16>(0 + 4 * i), static_cast<uint16>(2 + 4 * i)}}; 53 quad_index_list[i] = y; 54 } 55 56 gl_->GenBuffers(1, &quad_vertices_vbo_); 57 gl_->GenBuffers(1, &quad_elements_vbo_); 58 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); 59 GLC(gl_, 60 gl_->BufferData( 61 GL_ARRAY_BUFFER, sizeof(quad_list), quad_list, GL_STATIC_DRAW)); 62 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); 63 GLC(gl_, 64 gl_->BufferData(GL_ELEMENT_ARRAY_BUFFER, 65 sizeof(quad_index_list), 66 quad_index_list, 67 GL_STATIC_DRAW)); 68 } 69 70 GeometryBinding::~GeometryBinding() { 71 gl_->DeleteBuffers(1, &quad_vertices_vbo_); 72 gl_->DeleteBuffers(1, &quad_elements_vbo_); 73 } 74 75 void GeometryBinding::PrepareForDraw() { 76 GLC(gl_, gl_->BindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_elements_vbo_)); 77 78 GLC(gl_, gl_->BindBuffer(GL_ARRAY_BUFFER, quad_vertices_vbo_)); 79 // OpenGL defines the last parameter to VertexAttribPointer as type 80 // "const GLvoid*" even though it is actually an offset into the buffer 81 // object's data store and not a pointer to the client's address space. 82 const void* offsets[3] = { 83 0, reinterpret_cast<const void*>( 84 3 * sizeof(float)), // NOLINT(runtime/sizeof) 85 reinterpret_cast<const void*>(5 * 86 sizeof(float)), // NOLINT(runtime/sizeof) 87 }; 88 89 GLC(gl_, 90 gl_->VertexAttribPointer(PositionAttribLocation(), 91 3, 92 GL_FLOAT, 93 false, 94 6 * sizeof(float), // NOLINT(runtime/sizeof) 95 offsets[0])); 96 GLC(gl_, 97 gl_->VertexAttribPointer(TexCoordAttribLocation(), 98 2, 99 GL_FLOAT, 100 false, 101 6 * sizeof(float), // NOLINT(runtime/sizeof) 102 offsets[1])); 103 GLC(gl_, 104 gl_->VertexAttribPointer(TriangleIndexAttribLocation(), 105 1, 106 GL_FLOAT, 107 false, 108 6 * sizeof(float), // NOLINT(runtime/sizeof) 109 offsets[2])); 110 GLC(gl_, gl_->EnableVertexAttribArray(PositionAttribLocation())); 111 GLC(gl_, gl_->EnableVertexAttribArray(TexCoordAttribLocation())); 112 GLC(gl_, gl_->EnableVertexAttribArray(TriangleIndexAttribLocation())); 113 } 114 115 } // namespace cc 116