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); |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 32 | this->invalidate(); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /** |
| 36 | * This function enables and sets vertex attrib state for the specified attrib index. It is |
| 37 | * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex |
| 38 | * array object. |
| 39 | */ |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 40 | void set(GrGLGpu*, |
| 41 | int attribIndex, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 42 | const GrBuffer* vertexBuffer, |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 43 | GrVertexAttribType cpuType, |
| 44 | GrSLType gpuType, |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 45 | GrGLsizei stride, |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 46 | size_t offsetInBytes, |
| 47 | int divisor = 0); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 48 | |
| 49 | /** |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 50 | * This function enables the first 'enabledCount' vertex arrays and disables the rest. |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 51 | */ |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 52 | void enableVertexArrays(const GrGLGpu*, int enabledCount, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 53 | GrPrimitiveRestart = GrPrimitiveRestart::kNo); |
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 | } |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 60 | fEnableStateIsValid = false; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 61 | } |
| 62 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 63 | /** |
| 64 | * The number of attrib arrays that this object is configured to track. |
| 65 | */ |
| 66 | int count() const { return fAttribArrayStates.count(); } |
| 67 | |
| 68 | private: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 69 | static constexpr int kInvalidDivisor = -1; |
| 70 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Tracks the state of glVertexAttribArray for an attribute index. |
| 73 | */ |
| 74 | struct AttribArrayState { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 75 | void invalidate() { |
| 76 | fVertexBufferUniqueID.makeInvalid(); |
| 77 | fDivisor = kInvalidDivisor; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 78 | fUsingCpuBuffer = false; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 79 | } |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 80 | |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 81 | GrGpuResource::UniqueID fVertexBufferUniqueID; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 82 | bool fUsingCpuBuffer; |
Brian Osman | 4a3f5c8 | 2018-09-18 16:16:38 -0400 | [diff] [blame] | 83 | GrVertexAttribType fCPUType; |
| 84 | GrSLType fGPUType; |
Chris Dalton | 8e45b4f | 2017-05-05 14:00:56 -0400 | [diff] [blame] | 85 | GrGLsizei fStride; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 86 | const GrGLvoid* fOffset; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 87 | int fDivisor; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 90 | SkSTArray<16, AttribArrayState, true> fAttribArrayStates; |
| 91 | int fNumEnabledArrays; |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 92 | GrPrimitiveRestart fPrimitiveRestartEnabled; |
Chris Dalton | 27059d3 | 2018-01-23 14:06:50 -0700 | [diff] [blame] | 93 | bool fEnableStateIsValid = false; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array |
Leon Scroggins III | 243ed37 | 2017-05-05 11:55:12 -0400 | [diff] [blame] | 98 | * and is used to track the state of the vertex array to avoid redundant GL calls. |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 99 | */ |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 100 | class GrGLVertexArray { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 101 | public: |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 102 | GrGLVertexArray(GrGLint id, int attribCount); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 103 | |
| 104 | /** |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | * 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] | 106 | * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is |
| 107 | * returned. |
| 108 | */ |
bsalomon | 8780bc6 | 2015-05-13 09:56:37 -0700 | [diff] [blame] | 109 | GrGLAttribArrayState* bind(GrGLGpu*); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * This is a version of the above function that also binds an index buffer to the vertex |
| 113 | * array object. |
| 114 | */ |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 115 | GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 116 | |
| 117 | GrGLuint arrayID() const { return fID; } |
| 118 | |
| 119 | void invalidateCachedState(); |
| 120 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 121 | private: |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 122 | GrGLuint fID; |
| 123 | GrGLAttribArrayState fAttribArrays; |
| 124 | GrGpuResource::UniqueID fIndexBufferUniqueID; |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | #endif |