blob: 0a5dea6a1ebf22830b3d4e7bcf0fab090ad08947 [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
bsalomon@google.com31ec7982013-03-27 18:14:57 +000011#include "GrTypesPriv.h"
12#include "gl/GrGLDefines.h"
bsalomon2fc11d32015-10-19 09:03:23 -070013#include "gl/GrGLTypes.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000014#include "SkTArray.h"
15
16class GrGLVertexBuffer;
17class GrGLIndexBuffer;
bsalomon861e1032014-12-16 07:33:49 -080018class GrGLGpu;
bsalomon@google.com6918d482013-03-07 19:09:11 +000019
bsalomon@google.com31ec7982013-03-27 18:14:57 +000020struct GrGLAttribLayout {
21 GrGLint fCount;
22 GrGLenum fType;
23 GrGLboolean fNormalized;
24};
25
26static inline const GrGLAttribLayout& GrGLAttribTypeToLayout(GrVertexAttribType type) {
bsalomon@google.com31ec7982013-03-27 18:14:57 +000027 static const GrGLAttribLayout kLayouts[kGrVertexAttribTypeCount] = {
28 {1, GR_GL_FLOAT, false}, // kFloat_GrVertexAttribType
29 {2, GR_GL_FLOAT, false}, // kVec2f_GrVertexAttribType
30 {3, GR_GL_FLOAT, false}, // kVec3f_GrVertexAttribType
31 {4, GR_GL_FLOAT, false}, // kVec4f_GrVertexAttribType
egdaniel37b4d862014-11-03 10:07:07 -080032 {1, GR_GL_UNSIGNED_BYTE, true}, // kUByte_GrVertexAttribType
bsalomon@google.com31ec7982013-03-27 18:14:57 +000033 {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType
jvanverth5a105ff2015-02-18 11:36:35 -080034 {2, GR_GL_SHORT, false}, // kVec2s_GrVertexAttribType
ethannicholas22793252016-01-30 09:59:10 -080035 {4, GR_GL_INT, false}, // kInt_GrVertexAttribType
bsalomon@google.com31ec7982013-03-27 18:14:57 +000036 };
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);
egdaniel37b4d862014-11-03 10:07:07 -080041 GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType);
42 GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType);
jvanverth5a105ff2015-02-18 11:36:35 -080043 GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType);
ethannicholas22793252016-01-30 09:59:10 -080044 GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType);
bsalomon@google.com31ec7982013-03-27 18:14:57 +000045 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayouts) == kGrVertexAttribTypeCount);
46 return kLayouts[type];
47}
48
bsalomon@google.com6918d482013-03-07 19:09:11 +000049/**
50 * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray
51 * (below) but is separate because it is also used to track the state of vertex array object 0.
52 */
53class GrGLAttribArrayState {
54public:
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000055 explicit GrGLAttribArrayState(int arrayCount = 0) {
56 this->resize(arrayCount);
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000057 }
bsalomon@google.com6918d482013-03-07 19:09:11 +000058
59 void resize(int newCount) {
60 fAttribArrayStates.resize_back(newCount);
61 for (int i = 0; i < newCount; ++i) {
62 fAttribArrayStates[i].invalidate();
63 }
64 }
65
66 /**
67 * This function enables and sets vertex attrib state for the specified attrib index. It is
68 * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex
69 * array object.
70 */
bsalomon6df86402015-06-01 10:41:49 -070071 void set(GrGLGpu*,
72 int attribIndex,
73 GrGLuint vertexBufferID,
bsalomon@google.com6918d482013-03-07 19:09:11 +000074 GrGLint size,
75 GrGLenum type,
76 GrGLboolean normalized,
77 GrGLsizei stride,
78 GrGLvoid* offset);
79
80 /**
81 * This function disables vertex attribs not present in the mask. It is assumed that the
82 * GrGLAttribArrayState is tracking the state of the currently bound vertex array object.
83 */
bsalomon861e1032014-12-16 07:33:49 -080084 void disableUnusedArrays(const GrGLGpu*, uint64_t usedAttribArrayMask);
bsalomon@google.com6918d482013-03-07 19:09:11 +000085
86 void invalidate() {
87 int count = fAttribArrayStates.count();
88 for (int i = 0; i < count; ++i) {
89 fAttribArrayStates[i].invalidate();
90 }
91 }
92
93 void notifyVertexBufferDelete(GrGLuint id) {
94 int count = fAttribArrayStates.count();
95 for (int i = 0; i < count; ++i) {
robertphillips@google.com152336f2013-03-13 21:47:56 +000096 if (fAttribArrayStates[i].fAttribPointerIsValid &&
97 id == fAttribArrayStates[i].fVertexBufferID) {
bsalomon@google.com6918d482013-03-07 19:09:11 +000098 fAttribArrayStates[i].invalidate();
99 }
100 }
101 }
102
103 /**
104 * The number of attrib arrays that this object is configured to track.
105 */
106 int count() const { return fAttribArrayStates.count(); }
107
108private:
109 /**
110 * Tracks the state of glVertexAttribArray for an attribute index.
111 */
112 struct AttribArrayState {
113 void invalidate() {
114 fEnableIsValid = false;
115 fAttribPointerIsValid = false;
116 }
117
118 bool fEnableIsValid;
119 bool fAttribPointerIsValid;
120 bool fEnabled;
121 GrGLuint fVertexBufferID;
122 GrGLint fSize;
123 GrGLenum fType;
124 GrGLboolean fNormalized;
125 GrGLsizei fStride;
126 GrGLvoid* fOffset;
127 };
128
129 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000130};
131
132/**
133 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
134 * and is used to track the state of the vertex array to avoid redundant GL calls.
135 */
bsalomon8780bc62015-05-13 09:56:37 -0700136class GrGLVertexArray {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000137public:
bsalomon8780bc62015-05-13 09:56:37 -0700138 GrGLVertexArray(GrGLint id, int attribCount);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000139
140 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700141 * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000142 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
143 * returned.
144 */
bsalomon8780bc62015-05-13 09:56:37 -0700145 GrGLAttribArrayState* bind(GrGLGpu*);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000146
147 /**
148 * This is a version of the above function that also binds an index buffer to the vertex
149 * array object.
150 */
bsalomon6df86402015-06-01 10:41:49 -0700151 GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, GrGLuint indexBufferID);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000152
153 void notifyIndexBufferDelete(GrGLuint bufferID);
154
155 void notifyVertexBufferDelete(GrGLuint id) {
156 fAttribArrays.notifyVertexBufferDelete(id);
157 }
158
159 GrGLuint arrayID() const { return fID; }
160
161 void invalidateCachedState();
162
bsalomon@google.com6918d482013-03-07 19:09:11 +0000163private:
164 GrGLuint fID;
165 GrGLAttribArrayState fAttribArrays;
166 GrGLuint fIndexBufferID;
167 bool fIndexBufferIDIsValid;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000168};
169
170#endif