1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/gl/GrGLVertexArray.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,176 @@ 1.4 +/* 1.5 + * Copyright 2013 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrGLVertexArray_DEFINED 1.12 +#define GrGLVertexArray_DEFINED 1.13 + 1.14 +#include "GrResource.h" 1.15 +#include "GrTypesPriv.h" 1.16 +#include "gl/GrGLDefines.h" 1.17 +#include "gl/GrGLFunctions.h" 1.18 + 1.19 +#include "SkTArray.h" 1.20 + 1.21 +class GrGLVertexBuffer; 1.22 +class GrGLIndexBuffer; 1.23 +class GrGpuGL; 1.24 + 1.25 +struct GrGLAttribLayout { 1.26 + GrGLint fCount; 1.27 + GrGLenum fType; 1.28 + GrGLboolean fNormalized; 1.29 +}; 1.30 + 1.31 +static inline const GrGLAttribLayout& GrGLAttribTypeToLayout(GrVertexAttribType type) { 1.32 + SkASSERT(type >= 0 && type < kGrVertexAttribTypeCount); 1.33 + static const GrGLAttribLayout kLayouts[kGrVertexAttribTypeCount] = { 1.34 + {1, GR_GL_FLOAT, false}, // kFloat_GrVertexAttribType 1.35 + {2, GR_GL_FLOAT, false}, // kVec2f_GrVertexAttribType 1.36 + {3, GR_GL_FLOAT, false}, // kVec3f_GrVertexAttribType 1.37 + {4, GR_GL_FLOAT, false}, // kVec4f_GrVertexAttribType 1.38 + {4, GR_GL_UNSIGNED_BYTE, true}, // kVec4ub_GrVertexAttribType 1.39 + }; 1.40 + GR_STATIC_ASSERT(0 == kFloat_GrVertexAttribType); 1.41 + GR_STATIC_ASSERT(1 == kVec2f_GrVertexAttribType); 1.42 + GR_STATIC_ASSERT(2 == kVec3f_GrVertexAttribType); 1.43 + GR_STATIC_ASSERT(3 == kVec4f_GrVertexAttribType); 1.44 + GR_STATIC_ASSERT(4 == kVec4ub_GrVertexAttribType); 1.45 + GR_STATIC_ASSERT(SK_ARRAY_COUNT(kLayouts) == kGrVertexAttribTypeCount); 1.46 + return kLayouts[type]; 1.47 +} 1.48 + 1.49 +/** 1.50 + * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray 1.51 + * (below) but is separate because it is also used to track the state of vertex array object 0. 1.52 + */ 1.53 +class GrGLAttribArrayState { 1.54 +public: 1.55 + explicit GrGLAttribArrayState(int arrayCount = 0) { 1.56 + this->resize(arrayCount); 1.57 + } 1.58 + 1.59 + void resize(int newCount) { 1.60 + fAttribArrayStates.resize_back(newCount); 1.61 + for (int i = 0; i < newCount; ++i) { 1.62 + fAttribArrayStates[i].invalidate(); 1.63 + } 1.64 + } 1.65 + 1.66 + /** 1.67 + * This function enables and sets vertex attrib state for the specified attrib index. It is 1.68 + * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex 1.69 + * array object. 1.70 + */ 1.71 + void set(const GrGpuGL*, 1.72 + int index, 1.73 + GrGLVertexBuffer*, 1.74 + GrGLint size, 1.75 + GrGLenum type, 1.76 + GrGLboolean normalized, 1.77 + GrGLsizei stride, 1.78 + GrGLvoid* offset); 1.79 + 1.80 + /** 1.81 + * This function disables vertex attribs not present in the mask. It is assumed that the 1.82 + * GrGLAttribArrayState is tracking the state of the currently bound vertex array object. 1.83 + */ 1.84 + void disableUnusedArrays(const GrGpuGL*, uint64_t usedAttribArrayMask); 1.85 + 1.86 + void invalidate() { 1.87 + int count = fAttribArrayStates.count(); 1.88 + for (int i = 0; i < count; ++i) { 1.89 + fAttribArrayStates[i].invalidate(); 1.90 + } 1.91 + } 1.92 + 1.93 + void notifyVertexBufferDelete(GrGLuint id) { 1.94 + int count = fAttribArrayStates.count(); 1.95 + for (int i = 0; i < count; ++i) { 1.96 + if (fAttribArrayStates[i].fAttribPointerIsValid && 1.97 + id == fAttribArrayStates[i].fVertexBufferID) { 1.98 + fAttribArrayStates[i].invalidate(); 1.99 + } 1.100 + } 1.101 + } 1.102 + 1.103 + /** 1.104 + * The number of attrib arrays that this object is configured to track. 1.105 + */ 1.106 + int count() const { return fAttribArrayStates.count(); } 1.107 + 1.108 +private: 1.109 + /** 1.110 + * Tracks the state of glVertexAttribArray for an attribute index. 1.111 + */ 1.112 + struct AttribArrayState { 1.113 + void invalidate() { 1.114 + fEnableIsValid = false; 1.115 + fAttribPointerIsValid = false; 1.116 + } 1.117 + 1.118 + bool fEnableIsValid; 1.119 + bool fAttribPointerIsValid; 1.120 + bool fEnabled; 1.121 + GrGLuint fVertexBufferID; 1.122 + GrGLint fSize; 1.123 + GrGLenum fType; 1.124 + GrGLboolean fNormalized; 1.125 + GrGLsizei fStride; 1.126 + GrGLvoid* fOffset; 1.127 + }; 1.128 + 1.129 + SkSTArray<16, AttribArrayState, true> fAttribArrayStates; 1.130 +}; 1.131 + 1.132 +/** 1.133 + * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array 1.134 + * and is used to track the state of the vertex array to avoid redundant GL calls. 1.135 + */ 1.136 +class GrGLVertexArray : public GrResource { 1.137 +public: 1.138 + GrGLVertexArray(GrGpuGL* gpu, GrGLint id, int attribCount); 1.139 + 1.140 + /** 1.141 + * Binds this vertex array. If the ID has been deleted or abandoned then NULL is returned. 1.142 + * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is 1.143 + * returned. 1.144 + */ 1.145 + GrGLAttribArrayState* bind(); 1.146 + 1.147 + /** 1.148 + * This is a version of the above function that also binds an index buffer to the vertex 1.149 + * array object. 1.150 + */ 1.151 + GrGLAttribArrayState* bindWithIndexBuffer(const GrGLIndexBuffer* indexBuffer); 1.152 + 1.153 + void notifyIndexBufferDelete(GrGLuint bufferID); 1.154 + 1.155 + void notifyVertexBufferDelete(GrGLuint id) { 1.156 + fAttribArrays.notifyVertexBufferDelete(id); 1.157 + } 1.158 + 1.159 + GrGLuint arrayID() const { return fID; } 1.160 + 1.161 + void invalidateCachedState(); 1.162 + 1.163 + virtual size_t sizeInBytes() const SK_OVERRIDE { return 0; } 1.164 + 1.165 +protected: 1.166 + virtual void onAbandon() SK_OVERRIDE; 1.167 + 1.168 + virtual void onRelease() SK_OVERRIDE; 1.169 + 1.170 +private: 1.171 + GrGLuint fID; 1.172 + GrGLAttribArrayState fAttribArrays; 1.173 + GrGLuint fIndexBufferID; 1.174 + bool fIndexBufferIDIsValid; 1.175 + 1.176 + typedef GrResource INHERITED; 1.177 +}; 1.178 + 1.179 +#endif