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 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [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 | 2fc11d3 | 2015-10-19 09:03:23 -0700 | [diff] [blame] | 14 | #include "gl/GrGLTypes.h" |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 15 | #include "SkTArray.h" |
| 16 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 17 | class GrBuffer; |
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 | |
| 20 | /** |
| 21 | * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray |
| 22 | * (below) but is separate because it is also used to track the state of vertex array object 0. |
| 23 | */ |
| 24 | class GrGLAttribArrayState { |
| 25 | public: |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 26 | explicit GrGLAttribArrayState(int arrayCount = 0) { |
| 27 | this->resize(arrayCount); |
commit-bot@chromium.org | ce6da4d | 2013-09-09 14:55:37 +0000 | [diff] [blame] | 28 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 29 | |
| 30 | void resize(int newCount) { |
| 31 | fAttribArrayStates.resize_back(newCount); |
| 32 | for (int i = 0; i < newCount; ++i) { |
| 33 | fAttribArrayStates[i].invalidate(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * This function enables and sets vertex attrib state for the specified attrib index. It is |
| 39 | * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex |
| 40 | * array object. |
| 41 | */ |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 42 | void set(GrGLGpu*, |
| 43 | int attribIndex, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 44 | const GrBuffer* vertexBuffer, |
cdalton | 793dc26 | 2016-02-08 10:11:47 -0800 | [diff] [blame] | 45 | GrVertexAttribType type, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 46 | GrGLsizei stride, |
| 47 | GrGLvoid* offset); |
| 48 | |
| 49 | /** |
| 50 | * This function disables vertex attribs not present in the mask. It is assumed that the |
| 51 | * GrGLAttribArrayState is tracking the state of the currently bound vertex array object. |
| 52 | */ |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 53 | void disableUnusedArrays(const GrGLGpu*, uint64_t usedAttribArrayMask); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 54 | |
| 55 | void invalidate() { |
| 56 | int count = fAttribArrayStates.count(); |
| 57 | for (int i = 0; i < count; ++i) { |
| 58 | fAttribArrayStates[i].invalidate(); |
| 59 | } |
| 60 | } |
| 61 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 62 | /** |
| 63 | * The number of attrib arrays that this object is configured to track. |
| 64 | */ |
| 65 | int count() const { return fAttribArrayStates.count(); } |
| 66 | |
| 67 | private: |
| 68 | /** |
| 69 | * Tracks the state of glVertexAttribArray for an attribute index. |
| 70 | */ |
| 71 | struct AttribArrayState { |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 72 | void invalidate() { |
| 73 | fEnableIsValid = false; |
| 74 | fVertexBufferUniqueID.makeInvalid(); |
| 75 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 76 | |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 77 | bool fEnableIsValid; |
| 78 | bool fEnabled; |
| 79 | GrGpuResource::UniqueID fVertexBufferUniqueID; |
| 80 | GrVertexAttribType fType; |
| 81 | GrGLsizei fStride; |
| 82 | GrGLvoid* fOffset; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | SkSTArray<16, AttribArrayState, true> fAttribArrayStates; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array |
| 90 | * and is used to track the state of the vertex array to avoid redundant GL calls. |
| 91 | */ |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 92 | class GrGLVertexArray { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 93 | public: |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 94 | GrGLVertexArray(GrGLint id, int attribCount); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 95 | |
| 96 | /** |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | * 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] | 98 | * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is |
| 99 | * returned. |
| 100 | */ |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 101 | GrGLAttribArrayState* bind(GrGLGpu*); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * This is a version of the above function that also binds an index buffer to the vertex |
| 105 | * array object. |
| 106 | */ |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 107 | GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 108 | |
| 109 | GrGLuint arrayID() const { return fID; } |
| 110 | |
| 111 | void invalidateCachedState(); |
| 112 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 113 | private: |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 114 | GrGLuint fID; |
| 115 | GrGLAttribArrayState fAttribArrays; |
| 116 | GrGpuResource::UniqueID fIndexBufferUniqueID; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #endif |