blob: 4c77d2b5824c2ffe787ac8e88101064acc6f55f1 [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);
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 */
bsalomon6df86402015-06-01 10:41:49 -070042 void set(GrGLGpu*,
43 int attribIndex,
csmartdalton485a1202016-07-13 10:16:32 -070044 const GrBuffer* vertexBuffer,
cdalton793dc262016-02-08 10:11:47 -080045 GrVertexAttribType type,
bsalomon@google.com6918d482013-03-07 19:09:11 +000046 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 */
bsalomon861e1032014-12-16 07:33:49 -080053 void disableUnusedArrays(const GrGLGpu*, uint64_t usedAttribArrayMask);
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 }
60 }
61
bsalomon@google.com6918d482013-03-07 19:09:11 +000062 /**
63 * The number of attrib arrays that this object is configured to track.
64 */
65 int count() const { return fAttribArrayStates.count(); }
66
67private:
68 /**
69 * Tracks the state of glVertexAttribArray for an attribute index.
70 */
71 struct AttribArrayState {
Robert Phillips294870f2016-11-11 12:38:40 -050072 void invalidate() {
73 fEnableIsValid = false;
74 fVertexBufferUniqueID.makeInvalid();
75 }
bsalomon@google.com6918d482013-03-07 19:09:11 +000076
Robert Phillips294870f2016-11-11 12:38:40 -050077 bool fEnableIsValid;
78 bool fEnabled;
79 GrGpuResource::UniqueID fVertexBufferUniqueID;
80 GrVertexAttribType fType;
81 GrGLsizei fStride;
82 GrGLvoid* fOffset;
bsalomon@google.com6918d482013-03-07 19:09:11 +000083 };
84
85 SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
bsalomon@google.com6918d482013-03-07 19:09:11 +000086};
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 */
bsalomon8780bc62015-05-13 09:56:37 -070092class GrGLVertexArray {
bsalomon@google.com6918d482013-03-07 19:09:11 +000093public:
bsalomon8780bc62015-05-13 09:56:37 -070094 GrGLVertexArray(GrGLint id, int attribCount);
bsalomon@google.com6918d482013-03-07 19:09:11 +000095
96 /**
halcanary96fcdcc2015-08-27 07:41:13 -070097 * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
bsalomon@google.com6918d482013-03-07 19:09:11 +000098 * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
99 * returned.
100 */
bsalomon8780bc62015-05-13 09:56:37 -0700101 GrGLAttribArrayState* bind(GrGLGpu*);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000102
103 /**
104 * This is a version of the above function that also binds an index buffer to the vertex
105 * array object.
106 */
csmartdalton485a1202016-07-13 10:16:32 -0700107 GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000108
109 GrGLuint arrayID() const { return fID; }
110
111 void invalidateCachedState();
112
bsalomon@google.com6918d482013-03-07 19:09:11 +0000113private:
Robert Phillips294870f2016-11-11 12:38:40 -0500114 GrGLuint fID;
115 GrGLAttribArrayState fAttribArrays;
116 GrGpuResource::UniqueID fIndexBufferUniqueID;
bsalomon@google.com6918d482013-03-07 19:09:11 +0000117};
118
119#endif