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