blob: 20b21ae47b2a5821ddc04e136dfe6ae1a5143ad9 [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
Robert Phillips294870f2016-11-11 12:38:40 -050011#include "GrGpuResource.h"
bsalomon@google.com31ec7982013-03-27 18:14:57 +000012#include "GrTypesPriv.h"
13#include "gl/GrGLDefines.h"
bsalomon2fc11d32015-10-19 09:03:23 -070014#include "gl/GrGLTypes.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +000015#include "SkTArray.h"
16
csmartdalton485a1202016-07-13 10:16:32 -070017class GrBuffer;
bsalomon861e1032014-12-16 07:33:49 -080018class GrGLGpu;
bsalomon@google.com6918d482013-03-07 19:09:11 +000019
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 */
24class GrGLAttribArrayState {
25public:
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000026 explicit GrGLAttribArrayState(int arrayCount = 0) {
27 this->resize(arrayCount);
commit-bot@chromium.orgce6da4d2013-09-09 14:55:37 +000028 }
bsalomon@google.com6918d482013-03-07 19:09:11 +000029
30 void resize(int newCount) {
31 fAttribArrayStates.resize_back(newCount);
Chris Dalton8e45b4f2017-05-05 14:00:56 -040032 this->invalidate();
bsalomon@google.com6918d482013-03-07 19:09:11 +000033 }
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 */
bsalomon6df86402015-06-01 10:41:49 -070040 void set(GrGLGpu*,
41 int attribIndex,
csmartdalton485a1202016-07-13 10:16:32 -070042 const GrBuffer* vertexBuffer,
cdalton793dc262016-02-08 10:11:47 -080043 GrVertexAttribType type,
bsalomon@google.com6918d482013-03-07 19:09:11 +000044 GrGLsizei stride,
Chris Dalton1d616352017-05-31 12:51:23 -060045 size_t offsetInBytes,
46 int divisor = 0);
bsalomon@google.com6918d482013-03-07 19:09:11 +000047
Chris Dalton27059d32018-01-23 14:06:50 -070048 enum class EnablePrimitiveRestart : bool {
49 kYes = true,
50 kNo = false
51 };
52
bsalomon@google.com6918d482013-03-07 19:09:11 +000053 /**
Chris Dalton8e45b4f2017-05-05 14:00:56 -040054 * This function enables the first 'enabledCount' vertex arrays and disables the rest.
bsalomon@google.com6918d482013-03-07 19:09:11 +000055 */
Chris Dalton27059d32018-01-23 14:06:50 -070056 void enableVertexArrays(const GrGLGpu*, int enabledCount,
57 EnablePrimitiveRestart = EnablePrimitiveRestart::kNo);
bsalomon@google.com6918d482013-03-07 19:09:11 +000058
59 void invalidate() {
60 int count = fAttribArrayStates.count();
61 for (int i = 0; i < count; ++i) {
62 fAttribArrayStates[i].invalidate();
63 }
Chris Dalton27059d32018-01-23 14:06:50 -070064 fEnableStateIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +000065 }
66
bsalomon@google.com6918d482013-03-07 19:09:11 +000067 /**
68 * The number of attrib arrays that this object is configured to track.
69 */
70 int count() const { return fAttribArrayStates.count(); }
71
72private:
Chris Dalton1d616352017-05-31 12:51:23 -060073 static constexpr int kInvalidDivisor = -1;
74
bsalomon@google.com6918d482013-03-07 19:09:11 +000075 /**
76 * Tracks the state of glVertexAttribArray for an attribute index.
77 */
78 struct AttribArrayState {
Chris Dalton1d616352017-05-31 12:51:23 -060079 void invalidate() {
80 fVertexBufferUniqueID.makeInvalid();
81 fDivisor = kInvalidDivisor;
82 }
bsalomon@google.com6918d482013-03-07 19:09:11 +000083
Chris Dalton8e45b4f2017-05-05 14:00:56 -040084 GrGpuResource::UniqueID fVertexBufferUniqueID;
85 GrVertexAttribType fType;
86 GrGLsizei fStride;
87 size_t fOffset;
Chris Dalton1d616352017-05-31 12:51:23 -060088 int fDivisor;
bsalomon@google.com6918d482013-03-07 19:09:11 +000089 };
90
Chris Dalton27059d32018-01-23 14:06:50 -070091 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
92 int fNumEnabledArrays;
93 EnablePrimitiveRestart fPrimitiveRestartEnabled;
94 bool fEnableStateIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +000095};
96
97/**
98 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
Leon Scroggins III243ed372017-05-05 11:55:12 -040099 * and is used to track the state of the vertex array to avoid redundant GL calls.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000100 */
bsalomon8780bc62015-05-13 09:56:37 -0700101class GrGLVertexArray {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000102public:
bsalomon8780bc62015-05-13 09:56:37 -0700103 GrGLVertexArray(GrGLint id, int attribCount);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000104
105 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700106 * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000107 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
108 * returned.
109 */
bsalomon8780bc62015-05-13 09:56:37 -0700110 GrGLAttribArrayState* bind(GrGLGpu*);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000111
112 /**
113 * This is a version of the above function that also binds an index buffer to the vertex
114 * array object.
115 */
csmartdalton485a1202016-07-13 10:16:32 -0700116 GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000117
118 GrGLuint arrayID() const { return fID; }
119
120 void invalidateCachedState();
121
bsalomon@google.com6918d482013-03-07 19:09:11 +0000122private:
Robert Phillips294870f2016-11-11 12:38:40 -0500123 GrGLuint fID;
124 GrGLAttribArrayState fAttribArrays;
125 GrGpuResource::UniqueID fIndexBufferUniqueID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000126};
127
128#endif