WebGLVertexArrayFake.cpp (2187B)
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 #include "WebGLVertexArrayFake.h" 7 8 #include "GLContext.h" 9 #include "WebGLBuffer.h" 10 #include "WebGLContext.h" 11 12 namespace mozilla { 13 14 static void BindBuffer(gl::GLContext* const gl, const GLenum target, 15 WebGLBuffer* const buffer) { 16 gl->fBindBuffer(target, buffer ? buffer->mGLName : 0); 17 } 18 19 WebGLVertexArrayFake::WebGLVertexArrayFake(WebGLContext* webgl) 20 : WebGLVertexArray(webgl) {} 21 22 void WebGLVertexArray::DoVertexAttrib(const uint32_t index, 23 const uint32_t vertOffset) const { 24 const auto& gl = mContext->gl; 25 26 const bool useDivisor = 27 mContext->IsWebGL2() || 28 mContext->IsExtensionEnabled(WebGLExtensionID::ANGLE_instanced_arrays); 29 30 const auto& binding = mBindings.at(index); 31 const auto& desc = mDescs.at(index); 32 33 if (binding.layout.isArray) { 34 gl->fEnableVertexAttribArray(index); 35 } else { 36 gl->fDisableVertexAttribArray(index); 37 } 38 39 if (useDivisor) { 40 gl->fVertexAttribDivisor(index, binding.layout.divisor); 41 } 42 43 static_assert(IsBufferTargetLazilyBound(LOCAL_GL_ARRAY_BUFFER)); 44 BindBuffer(gl, LOCAL_GL_ARRAY_BUFFER, binding.buffer); 45 46 auto desc2 = desc; 47 if (!binding.layout.divisor) { 48 desc2.byteOffset += binding.layout.byteStride * vertOffset; 49 } 50 DoVertexAttribPointer(*gl, index, desc2); 51 52 BindBuffer(gl, LOCAL_GL_ARRAY_BUFFER, nullptr); 53 } 54 55 void WebGLVertexArrayFake::BindVertexArray() { 56 // Go through and re-bind all buffers and setup all 57 // vertex attribute pointers 58 const auto& gl = mContext->gl; 59 60 mContext->mBoundVertexArray = this; 61 62 static_assert(IsBufferTargetLazilyBound(LOCAL_GL_ARRAY_BUFFER)); 63 static_assert(!IsBufferTargetLazilyBound(LOCAL_GL_ELEMENT_ARRAY_BUFFER)); 64 65 BindBuffer(gl, LOCAL_GL_ELEMENT_ARRAY_BUFFER, mElementArrayBuffer); 66 67 for (const auto i : IntegerRange(mContext->MaxVertexAttribs())) { 68 DoVertexAttrib(i); 69 } 70 } 71 72 } // namespace mozilla