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.
5 #include "cc/geometry_binding.h"
7 #include "cc/gl_renderer.h" // For the GLC() macro.
8 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h"
9 #include "third_party/khronos/GLES2/gl2.h"
10 #include "ui/gfx/rect_f.h"
14 GeometryBinding::GeometryBinding(WebKit::WebGraphicsContext3D
* context
, const gfx::RectF
& quadVertexRect
)
16 , m_quadVerticesVbo(0)
17 , m_quadElementsVbo(0)
18 , m_initialized(false)
20 float vertices
[] = { quadVertexRect
.x(), quadVertexRect
.bottom(), 0.0f
, 0.0f
, 1.0f
,
21 quadVertexRect
.x(), quadVertexRect
.y(), 0.0f
, 0.0f
, 0.0f
,
22 quadVertexRect
.right(), quadVertexRect
.y(), 0.0f
, 1.0f
, 0.0f
,
23 quadVertexRect
.right(), quadVertexRect
.bottom(), 0.0f
, 1.0f
, 1.0f
};
28 float a_index
; // index of the vertex, divide by 4 to have the matrix for this quad
30 struct Quad
{ Vertex v0
, v1
, v2
, v3
; };
31 struct QuadIndex
{ uint16_t data
[6]; };
33 COMPILE_ASSERT(sizeof(Quad
) == 24 * sizeof(float), struct_is_densely_packed
);
34 COMPILE_ASSERT(sizeof(QuadIndex
) == 6 * sizeof(uint16_t), struct_is_densely_packed
);
37 QuadIndex quad_index_list
[8];
38 for (int i
= 0; i
< 8; i
++) {
39 Vertex v0
= { quadVertexRect
.x() , quadVertexRect
.bottom(), 0.0f
, 0.0f
, 1.0f
, i
* 4.0f
+ 0.0f
};
40 Vertex v1
= { quadVertexRect
.x() , quadVertexRect
.y() , 0.0f
, 0.0f
, 0.0f
, i
* 4.0f
+ 1.0f
};
41 Vertex v2
= { quadVertexRect
.right(), quadVertexRect
.y() , 0.0f
, 1.0f
, 0.0f
, i
* 4.0f
+ 2.0f
};
42 Vertex v3
= { quadVertexRect
.right(), quadVertexRect
.bottom(), 0.0f
, 1.0f
, 1.0f
, i
* 4.0f
+ 3.0f
};
43 Quad x
= { v0
, v1
, v2
, v3
};
45 QuadIndex y
= { 0 + 4 * i
, 1 + 4 * i
, 2 + 4 * i
, 3 + 4 * i
, 0 + 4 * i
, 2 + 4 * i
};
46 quad_index_list
[i
] = y
;
49 GLC(m_context
, m_quadVerticesVbo
= m_context
->createBuffer());
50 GLC(m_context
, m_quadElementsVbo
= m_context
->createBuffer());
51 GLC(m_context
, m_quadListVerticesVbo
= m_context
->createBuffer());
52 GLC(m_context
, m_context
->bindBuffer(GL_ARRAY_BUFFER
, m_quadVerticesVbo
));
53 GLC(m_context
, m_context
->bufferData(GL_ARRAY_BUFFER
, sizeof(quad_list
), quad_list
, GL_STATIC_DRAW
));
54 GLC(m_context
, m_context
->bindBuffer(GL_ELEMENT_ARRAY_BUFFER
, m_quadElementsVbo
));
55 GLC(m_context
, m_context
->bufferData(GL_ELEMENT_ARRAY_BUFFER
, sizeof(quad_index_list
), quad_index_list
, GL_STATIC_DRAW
));
60 GeometryBinding::~GeometryBinding()
62 GLC(m_context
, m_context
->deleteBuffer(m_quadVerticesVbo
));
63 GLC(m_context
, m_context
->deleteBuffer(m_quadElementsVbo
));
66 void GeometryBinding::prepareForDraw()
68 GLC(m_context
, m_context
->bindBuffer(GL_ELEMENT_ARRAY_BUFFER
, quadElementsVbo()));
70 GLC(m_context
, m_context
->bindBuffer(GL_ARRAY_BUFFER
, quadVerticesVbo()));
71 GLC(m_context
, m_context
->vertexAttribPointer(positionAttribLocation(), 3, GL_FLOAT
, false, 6 * sizeof(float), 0));
72 GLC(m_context
, m_context
->vertexAttribPointer(texCoordAttribLocation(), 2, GL_FLOAT
, false, 6 * sizeof(float), 3 * sizeof(float)));
73 GLC(m_context
, m_context
->vertexAttribPointer(triangleIndexAttribLocation(), 1, GL_FLOAT
, false, 6 * sizeof(float), 5 * sizeof(float)));
74 GLC(m_context
, m_context
->enableVertexAttribArray(positionAttribLocation()));
75 GLC(m_context
, m_context
->enableVertexAttribArray(texCoordAttribLocation()));
76 GLC(m_context
, m_context
->enableVertexAttribArray(triangleIndexAttribLocation()));