bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 11 | #include "GrTypesPriv.h" |
| 12 | #include "gl/GrGLDefines.h" |
bsalomon | 2fc11d3 | 2015-10-19 09:03:23 -0700 | [diff] [blame] | 13 | #include "gl/GrGLTypes.h" |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 14 | #include "SkTArray.h" |
| 15 | |
| 16 | class GrGLVertexBuffer; |
| 17 | class GrGLIndexBuffer; |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 18 | class GrGLGpu; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 20 | struct GrGLAttribLayout { |
| 21 | GrGLint fCount; |
| 22 | GrGLenum fType; |
| 23 | GrGLboolean fNormalized; |
| 24 | }; |
| 25 | |
| 26 | static inline const GrGLAttribLayout& GrGLAttribTypeToLayout(GrVertexAttribType type) { |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 27 | 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 |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 32 | {1, GR_GL_UNSIGNED_BYTE, true}, // kUByte_GrVertexAttribType |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 33 | {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 34 | {2, GR_GL_SHORT, false}, // kVec2s_GrVertexAttribType |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 35 | {4, GR_GL_INT, false}, // kInt_GrVertexAttribType |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 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); |
egdaniel | 37b4d86 | 2014-11-03 10:07:07 -0800 | [diff] [blame] | 41 | GR_STATIC_ASSERT(4 == kUByte_GrVertexAttribType); |
| 42 | GR_STATIC_ASSERT(5 == kVec4ub_GrVertexAttribType); |
jvanverth | 5a105ff | 2015-02-18 11:36:35 -0800 | [diff] [blame] | 43 | GR_STATIC_ASSERT(6 == kVec2s_GrVertexAttribType); |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 44 | GR_STATIC_ASSERT(7 == kInt_GrVertexAttribType); |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 45 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayouts) == kGrVertexAttribTypeCount); |
| 46 | return kLayouts[type]; |
| 47 | } |
| 48 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 49 | /** |
| 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 | */ |
| 53 | class GrGLAttribArrayState { |
| 54 | public: |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 55 | explicit GrGLAttribArrayState(int arrayCount = 0) { |
| 56 | this->resize(arrayCount); |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 57 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 58 | |
| 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 | */ |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 71 | void set(GrGLGpu*, |
| 72 | int attribIndex, |
| 73 | GrGLuint vertexBufferID, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 74 | 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 | */ |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 84 | void disableUnusedArrays(const GrGLGpu*, uint64_t usedAttribArrayMask); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 85 | |
| 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.com | 152336f | 2013-03-13 21:47:56 +0000 | [diff] [blame] | 96 | if (fAttribArrayStates[i].fAttribPointerIsValid && |
| 97 | id == fAttribArrayStates[i].fVertexBufferID) { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 98 | 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 | |
| 108 | private: |
| 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.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 130 | }; |
| 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 | */ |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 136 | class GrGLVertexArray { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 137 | public: |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 138 | GrGLVertexArray(GrGLint id, int attribCount); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 141 | * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned. |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 142 | * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is |
| 143 | * returned. |
| 144 | */ |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 145 | GrGLAttribArrayState* bind(GrGLGpu*); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * This is a version of the above function that also binds an index buffer to the vertex |
| 149 | * array object. |
| 150 | */ |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 151 | GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, GrGLuint indexBufferID); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 152 | |
| 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.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 163 | private: |
| 164 | GrGLuint fID; |
| 165 | GrGLAttribArrayState fAttribArrays; |
| 166 | GrGLuint fIndexBufferID; |
| 167 | bool fIndexBufferIDIsValid; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | #endif |