blob: 4e28e627a9ff482ec401f511755056e3b423d34c [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,
Brian Osman4a3f5c82018-09-18 16:16:38 -040043 GrVertexAttribType cpuType,
44 GrSLType gpuType,
bsalomon@google.com6918d482013-03-07 19:09:11 +000045 GrGLsizei stride,
Chris Dalton1d616352017-05-31 12:51:23 -060046 size_t offsetInBytes,
47 int divisor = 0);
bsalomon@google.com6918d482013-03-07 19:09:11 +000048
49 /**
Chris Dalton8e45b4f2017-05-05 14:00:56 -040050 * This function enables the first 'enabledCount' vertex arrays and disables the rest.
bsalomon@google.com6918d482013-03-07 19:09:11 +000051 */
Chris Dalton27059d32018-01-23 14:06:50 -070052 void enableVertexArrays(const GrGLGpu*, int enabledCount,
Brian Salomon802cb312018-06-08 18:05:20 -040053 GrPrimitiveRestart = GrPrimitiveRestart::kNo);
bsalomon@google.com6918d482013-03-07 19:09:11 +000054
55 void invalidate() {
56 int count = fAttribArrayStates.count();
57 for (int i = 0; i < count; ++i) {
58 fAttribArrayStates[i].invalidate();
59 }
Chris Dalton27059d32018-01-23 14:06:50 -070060 fEnableStateIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +000061 }
62
bsalomon@google.com6918d482013-03-07 19:09:11 +000063 /**
64 * The number of attrib arrays that this object is configured to track.
65 */
66 int count() const { return fAttribArrayStates.count(); }
67
68private:
Chris Dalton1d616352017-05-31 12:51:23 -060069 static constexpr int kInvalidDivisor = -1;
70
bsalomon@google.com6918d482013-03-07 19:09:11 +000071 /**
72 * Tracks the state of glVertexAttribArray for an attribute index.
73 */
74 struct AttribArrayState {
Chris Dalton1d616352017-05-31 12:51:23 -060075 void invalidate() {
76 fVertexBufferUniqueID.makeInvalid();
77 fDivisor = kInvalidDivisor;
Brian Salomondbf70722019-02-07 11:31:24 -050078 fUsingCpuBuffer = false;
Chris Dalton1d616352017-05-31 12:51:23 -060079 }
bsalomon@google.com6918d482013-03-07 19:09:11 +000080
Chris Dalton8e45b4f2017-05-05 14:00:56 -040081 GrGpuResource::UniqueID fVertexBufferUniqueID;
Brian Salomondbf70722019-02-07 11:31:24 -050082 bool fUsingCpuBuffer;
Brian Osman4a3f5c82018-09-18 16:16:38 -040083 GrVertexAttribType fCPUType;
84 GrSLType fGPUType;
Chris Dalton8e45b4f2017-05-05 14:00:56 -040085 GrGLsizei fStride;
Brian Salomondbf70722019-02-07 11:31:24 -050086 const GrGLvoid* fOffset;
Chris Dalton1d616352017-05-31 12:51:23 -060087 int fDivisor;
bsalomon@google.com6918d482013-03-07 19:09:11 +000088 };
89
Chris Dalton27059d32018-01-23 14:06:50 -070090 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
91 int fNumEnabledArrays;
Brian Salomon802cb312018-06-08 18:05:20 -040092 GrPrimitiveRestart fPrimitiveRestartEnabled;
Chris Dalton27059d32018-01-23 14:06:50 -070093 bool fEnableStateIsValid = false;
bsalomon@google.com6918d482013-03-07 19:09:11 +000094};
95
96/**
97 * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
Leon Scroggins III243ed372017-05-05 11:55:12 -040098 * and is used to track the state of the vertex array to avoid redundant GL calls.
bsalomon@google.com6918d482013-03-07 19:09:11 +000099 */
bsalomon8780bc62015-05-13 09:56:37 -0700100class GrGLVertexArray {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000101public:
bsalomon8780bc62015-05-13 09:56:37 -0700102 GrGLVertexArray(GrGLint id, int attribCount);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000103
104 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700105 * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
bsalomon@google.com6918d482013-03-07 19:09:11 +0000106 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
107 * returned.
108 */
bsalomon8780bc62015-05-13 09:56:37 -0700109 GrGLAttribArrayState* bind(GrGLGpu*);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000110
111 /**
112 * This is a version of the above function that also binds an index buffer to the vertex
113 * array object.
114 */
csmartdalton485a1202016-07-13 10:16:32 -0700115 GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000116
117 GrGLuint arrayID() const { return fID; }
118
119 void invalidateCachedState();
120
bsalomon@google.com6918d482013-03-07 19:09:11 +0000121private:
Robert Phillips294870f2016-11-11 12:38:40 -0500122 GrGLuint fID;
123 GrGLAttribArrayState fAttribArrays;
124 GrGpuResource::UniqueID fIndexBufferUniqueID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000125};
126
127#endif