2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLVertexArray_DEFINED
9 #define GrGLVertexArray_DEFINED
11 #include "GrGpuResource.h"
12 #include "GrTypesPriv.h"
13 #include "gl/GrGLDefines.h"
14 #include "gl/GrGLFunctions.h"
18 class GrGLVertexBuffer;
19 class GrGLIndexBuffer;
22 struct GrGLAttribLayout {
25 GrGLboolean fNormalized;
28 static inline const GrGLAttribLayout& GrGLAttribTypeToLayout(GrVertexAttribType type) {
29 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
30 static const GrGLAttribLayout kLayouts[kGrVertexAttribTypeCount] = {
31 {1, GR_GL_FLOAT, false}, // kFloat_GrVertexAttribType
32 {2, GR_GL_FLOAT, false}, // kVec2f_GrVertexAttribType
33 {3, GR_GL_FLOAT, false}, // kVec3f_GrVertexAttribType
34 {4, GR_GL_FLOAT, false}, // kVec4f_GrVertexAttribType
35 {1, GR_GL_UNSIGNED_BYTE, true}, // kUByte_GrVertexAttribType
36 {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType
37 {2, GR_GL_SHORT, false}, // kVec2s_GrVertexAttribType
39 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
40 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
41 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
42 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
43 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
44 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
45 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
46 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayouts) == kGrVertexAttribTypeCount);
47 return kLayouts[type];
51 * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray
52 * (below) but is separate because it is also used to track the state of vertex array object 0.
54 class GrGLAttribArrayState {
56 explicit GrGLAttribArrayState(int arrayCount = 0) {
57 this->resize(arrayCount);
60 void resize(int newCount) {
61 fAttribArrayStates.resize_back(newCount);
62 for (int i = 0; i < newCount; ++i) {
63 fAttribArrayStates[i].invalidate();
68 * This function enables and sets vertex attrib state for the specified attrib index. It is
69 * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex
72 void set(const GrGLGpu*,
77 GrGLboolean normalized,
82 * This function disables vertex attribs not present in the mask. It is assumed that the
83 * GrGLAttribArrayState is tracking the state of the currently bound vertex array object.
85 void disableUnusedArrays(const GrGLGpu*, uint64_t usedAttribArrayMask);
88 int count = fAttribArrayStates.count();
89 for (int i = 0; i < count; ++i) {
90 fAttribArrayStates[i].invalidate();
94 void notifyVertexBufferDelete(GrGLuint id) {
95 int count = fAttribArrayStates.count();
96 for (int i = 0; i < count; ++i) {
97 if (fAttribArrayStates[i].fAttribPointerIsValid &&
98 id == fAttribArrayStates[i].fVertexBufferID) {
99 fAttribArrayStates[i].invalidate();
105 * The number of attrib arrays that this object is configured to track.
107 int count() const { return fAttribArrayStates.count(); }
111 * Tracks the state of glVertexAttribArray for an attribute index.
113 struct AttribArrayState {
115 fEnableIsValid = false;
116 fAttribPointerIsValid = false;
120 bool fAttribPointerIsValid;
122 GrGLuint fVertexBufferID;
125 GrGLboolean fNormalized;
130 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
134 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
135 * and is used to track the state of the vertex array to avoid redundant GL calls.
\13
137 class GrGLVertexArray : public GrGpuResource {
139 GrGLVertexArray(GrGLGpu* gpu, GrGLint id, int attribCount);
142 * Binds this vertex array. If the ID has been deleted or abandoned then NULL is returned.
143 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
146 GrGLAttribArrayState* bind();
149 * This is a version of the above function that also binds an index buffer to the vertex
152 GrGLAttribArrayState* bindWithIndexBuffer(const GrGLIndexBuffer* indexBuffer);
154 void notifyIndexBufferDelete(GrGLuint bufferID);
156 void notifyVertexBufferDelete(GrGLuint id) {
157 fAttribArrays.notifyVertexBufferDelete(id);
160 GrGLuint arrayID() const { return fID; }
162 void invalidateCachedState();
165 size_t onGpuMemorySize() const SK_OVERRIDE { return 0; }
167 void onAbandon() SK_OVERRIDE;
169 void onRelease() SK_OVERRIDE;
173 GrGLAttribArrayState fAttribArrays;
174 GrGLuint fIndexBufferID;
175 bool fIndexBufferIDIsValid;
177 typedef GrGpuResource INHERITED;