blob: 8a61f1a2c6055dc90bb00e5aa77ab21d73acc4ba [file] [log] [blame]
bsalomon@google.com6918d482013-03-07 19:09:11 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrGLVertexArray_DEFINED
9#define GrGLVertexArray_DEFINED
10
11#include "GrResource.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000012#include "GrTypesPriv.h"
13#include "gl/GrGLDefines.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000014#include "gl/GrGLFunctions.h"
15
16#include "SkTArray.h"
17
18class GrGLVertexBuffer;
19class GrGLIndexBuffer;
20class GrGpuGL;
21
bsalomon@google.com31ec7982013-03-27 18:14:57 +000022struct GrGLAttribLayout {
23 GrGLint fCount;
24 GrGLenum fType;
25 GrGLboolean fNormalized;
26};
27
28static inline const GrGLAttribLayout& GrGLAttribTypeToLayout(GrVertexAttribType type) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000029 SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount);
bsalomon@google.com31ec7982013-03-27 18:14:57 +000030 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 {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType
36 };
37 GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType);
38 GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType);
39 GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType);
40 GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType);
41 GR_STATIC_ASSERT(4 == kVec4ub_GrVertexAttribType);
42 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayouts) == kGrVertexAttribTypeCount);
43 return kLayouts[type];
44}
45
bsalomon@google.com6918d482013-03-07 19:09:11 +000046/**
47 * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray
48 * (below) but is separate because it is also used to track the state of vertex array object 0.
49 */
50class GrGLAttribArrayState {
51public:
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000052 explicit GrGLAttribArrayState(int arrayCount = 0) {
53 this->resize(arrayCount);
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000054 }
bsalomon@google.com6918d482013-03-07 19:09:11 +000055
56 void resize(int newCount) {
57 fAttribArrayStates.resize_back(newCount);
58 for (int i = 0; i < newCount; ++i) {
59 fAttribArrayStates[i].invalidate();
60 }
61 }
62
63 /**
64 * This function enables and sets vertex attrib state for the specified attrib index. It is
65 * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex
66 * array object.
67 */
68 void set(const GrGpuGL*,
69 int index,
70 GrGLVertexBuffer*,
71 GrGLint size,
72 GrGLenum type,
73 GrGLboolean normalized,
74 GrGLsizei stride,
75 GrGLvoid* offset);
76
77 /**
78 * This function disables vertex attribs not present in the mask. It is assumed that the
79 * GrGLAttribArrayState is tracking the state of the currently bound vertex array object.
80 */
commit-bot@chromium.org6ebfbf92014-02-24 12:05:02 +000081 void disableUnusedArrays(const GrGpuGL*, uint64_t usedAttribArrayMask);
bsalomon@google.com6918d482013-03-07 19:09:11 +000082
83 void invalidate() {
84 int count = fAttribArrayStates.count();
85 for (int i = 0; i < count; ++i) {
86 fAttribArrayStates[i].invalidate();
87 }
88 }
89
90 void notifyVertexBufferDelete(GrGLuint id) {
91 int count = fAttribArrayStates.count();
92 for (int i = 0; i < count; ++i) {
robertphillips@google.com152336f2013-03-13 21:47:56 +000093 if (fAttribArrayStates[i].fAttribPointerIsValid &&
94 id == fAttribArrayStates[i].fVertexBufferID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000095 fAttribArrayStates[i].invalidate();
96 }
97 }
98 }
99
100 /**
101 * The number of attrib arrays that this object is configured to track.
102 */
103 int count() const { return fAttribArrayStates.count(); }
104
105private:
106 /**
107 * Tracks the state of glVertexAttribArray for an attribute index.
108 */
109 struct AttribArrayState {
110 void invalidate() {
111 fEnableIsValid = false;
112 fAttribPointerIsValid = false;
113 }
114
115 bool fEnableIsValid;
116 bool fAttribPointerIsValid;
117 bool fEnabled;
118 GrGLuint fVertexBufferID;
119 GrGLint fSize;
120 GrGLenum fType;
121 GrGLboolean fNormalized;
122 GrGLsizei fStride;
123 GrGLvoid* fOffset;
124 };
125
126 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000127};
128
129/**
130 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
131 * and is used to track the state of the vertex array to avoid redundant GL calls.
132 */
133class GrGLVertexArray : public GrResource {
134public:
135 GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount);
136
137 /**
138 * Binds this vertex array. If the ID has been deleted or abandoned then NULL is returned.
139 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
140 * returned.
141 */
142 GrGLAttribArrayState* bind();
143
144 /**
145 * This is a version of the above function that also binds an index buffer to the vertex
146 * array object.
147 */
148 GrGLAttribArrayState* bindWithIndexBuffer(const GrGLIndexBuffer* indexBuffer);
149
150 void notifyIndexBufferDelete(GrGLuint bufferID);
151
152 void notifyVertexBufferDelete(GrGLuint id) {
153 fAttribArrays.notifyVertexBufferDelete(id);
154 }
155
156 GrGLuint arrayID() const { return fID; }
157
158 void invalidateCachedState();
159
160 virtual size_t sizeInBytes() const SK_OVERRIDE { return 0; }
161
162protected:
163 virtual void onAbandon() SK_OVERRIDE;
164
165 virtual void onRelease() SK_OVERRIDE;
166
167private:
168 GrGLuint fID;
169 GrGLAttribArrayState fAttribArrays;
170 GrGLuint fIndexBufferID;
171 bool fIndexBufferIDIsValid;
172
173 typedef GrResource INHERITED;
174};
175
176#endif