WebGLVertexArray.h (3685B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef WEBGL_VERTEX_ARRAY_H_ 7 #define WEBGL_VERTEX_ARRAY_H_ 8 9 #include <array> 10 #include <bitset> 11 12 #include "CacheInvalidator.h" 13 #include "WebGLObjectModel.h" 14 #include "WebGLStrongTypes.h" 15 #include "mozilla/IntegerRange.h" 16 17 namespace mozilla { 18 19 class WebGLBuffer; 20 class WebGLVertexArrayFake; 21 22 namespace webgl { 23 struct LinkedProgramInfo; 24 25 struct VertAttribLayoutData final { 26 // Keep this packed tightly! 27 uint32_t divisor = 0; 28 bool isArray = false; 29 uint8_t byteSize = 0; 30 uint8_t byteStride = 1; // Non-zero, at-most 255 31 webgl::AttribBaseType baseType = webgl::AttribBaseType::Float; 32 uint64_t byteOffset = 0; 33 }; 34 35 struct VertAttribBindingData final { 36 VertAttribLayoutData layout; 37 RefPtr<WebGLBuffer> buffer; 38 }; 39 40 } // namespace webgl 41 42 void DoVertexAttribPointer(gl::GLContext&, uint32_t index, 43 const webgl::VertAttribPointerDesc&); 44 45 class WebGLVertexArray : public WebGLContextBoundObject { 46 friend class ScopedDrawHelper; 47 friend class WebGLContext; 48 friend class WebGLVertexArrayFake; 49 friend class WebGL2Context; 50 friend struct webgl::LinkedProgramInfo; 51 52 static constexpr size_t kMaxAttribs = 32; // gpuinfo.org says so 53 54 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(WebGLVertexArray, override) 55 56 RefPtr<WebGLBuffer> mElementArrayBuffer; 57 58 std::array<webgl::VertAttribBindingData, kMaxAttribs> mBindings; // Hot data. 59 std::array<webgl::VertAttribPointerDesc, kMaxAttribs> 60 mDescs; // cold data, parallel to mBindings. 61 62 std::bitset<kMaxAttribs> mAttribIsArrayWithNullBuffer; 63 bool mHasBeenBound = false; 64 65 public: 66 static WebGLVertexArray* Create(WebGLContext* webgl); 67 68 protected: 69 explicit WebGLVertexArray(WebGLContext* webgl); 70 ~WebGLVertexArray(); 71 72 public: 73 virtual void BindVertexArray() = 0; 74 75 void SetAttribIsArray(const uint32_t index, const bool val) { 76 auto& binding = mBindings.at(index); 77 binding.layout.isArray = val; 78 mAttribIsArrayWithNullBuffer[index] = 79 binding.layout.isArray && !binding.buffer; 80 } 81 82 void AttribDivisor(const uint32_t index, const uint32_t val) { 83 auto& binding = mBindings.at(index); 84 binding.layout.divisor = val; 85 } 86 87 void AttribPointer(const uint32_t index, WebGLBuffer* const buffer, 88 const webgl::VertAttribPointerDesc& desc, 89 const webgl::VertAttribPointerCalculated& calc) { 90 mDescs[index] = desc; 91 92 auto& binding = mBindings.at(index); 93 binding.buffer = buffer; 94 binding.layout.byteSize = calc.byteSize; 95 binding.layout.byteStride = calc.byteStride; 96 binding.layout.baseType = calc.baseType; 97 binding.layout.byteOffset = desc.byteOffset; 98 99 mAttribIsArrayWithNullBuffer[index] = 100 binding.layout.isArray && !binding.buffer; 101 } 102 103 const auto& AttribBinding(const uint32_t index) const { 104 return mBindings.at(index); 105 } 106 const auto& AttribDesc(const uint32_t index) const { 107 return mDescs.at(index); 108 } 109 110 Maybe<uint32_t> GetAttribIsArrayWithNullBuffer() const { 111 const auto& bitset = mAttribIsArrayWithNullBuffer; 112 if (MOZ_LIKELY(bitset.none())) return {}; 113 for (const auto i : IntegerRange(bitset.size())) { 114 if (bitset[i]) return Some(i); 115 } 116 MOZ_CRASH(); 117 } 118 119 Maybe<double> GetVertexAttrib(uint32_t index, GLenum pname) const; 120 void DoVertexAttrib(uint32_t index, uint32_t vertOffset = 0) const; 121 }; 122 123 } // namespace mozilla 124 125 #endif // WEBGL_VERTEX_ARRAY_H_