Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "WebGLVertexAttribData.h"
7 :
8 : #include "GLContext.h"
9 : #include "WebGLBuffer.h"
10 :
11 : namespace mozilla {
12 :
13 : static uint8_t
14 0 : CalcBytesPerVertex(GLenum type, uint8_t size)
15 : {
16 : uint8_t bytesPerType;
17 0 : switch (type) {
18 : case LOCAL_GL_INT_2_10_10_10_REV:
19 : case LOCAL_GL_UNSIGNED_INT_2_10_10_10_REV:
20 0 : return 4;
21 :
22 : case LOCAL_GL_BYTE:
23 : case LOCAL_GL_UNSIGNED_BYTE:
24 0 : bytesPerType = 1;
25 0 : break;
26 :
27 : case LOCAL_GL_HALF_FLOAT:
28 : case LOCAL_GL_SHORT:
29 : case LOCAL_GL_UNSIGNED_SHORT:
30 0 : bytesPerType = 2;
31 0 : break;
32 :
33 : case LOCAL_GL_FIXED: // GLES 3.0.4 p9: 32-bit signed, with 16 fractional bits.
34 : case LOCAL_GL_FLOAT:
35 : case LOCAL_GL_INT:
36 : case LOCAL_GL_UNSIGNED_INT:
37 0 : bytesPerType = 4;
38 0 : break;
39 :
40 : default:
41 0 : MOZ_CRASH("Bad `type`.");
42 : }
43 :
44 0 : return bytesPerType * size;
45 : }
46 :
47 : static GLenum
48 0 : AttribPointerBaseType(bool integerFunc, GLenum type)
49 : {
50 0 : if (!integerFunc)
51 0 : return LOCAL_GL_FLOAT;
52 :
53 0 : switch (type) {
54 : case LOCAL_GL_BYTE:
55 : case LOCAL_GL_SHORT:
56 : case LOCAL_GL_INT:
57 0 : return LOCAL_GL_INT;
58 :
59 : case LOCAL_GL_UNSIGNED_BYTE:
60 : case LOCAL_GL_UNSIGNED_SHORT:
61 : case LOCAL_GL_UNSIGNED_INT:
62 0 : return LOCAL_GL_UNSIGNED_INT;
63 :
64 : default:
65 0 : MOZ_CRASH();
66 : }
67 : }
68 :
69 : void
70 0 : WebGLVertexAttribData::VertexAttribPointer(bool integerFunc, WebGLBuffer* buf,
71 : uint8_t size, GLenum type, bool normalized,
72 : uint32_t stride, uint64_t byteOffset)
73 : {
74 0 : mIntegerFunc = integerFunc;
75 0 : WebGLBuffer::SetSlot(0, buf, &mBuf);
76 0 : mType = type;
77 0 : mBaseType = AttribPointerBaseType(integerFunc, type);
78 0 : mSize = size;
79 0 : mBytesPerVertex = CalcBytesPerVertex(mType, mSize);
80 0 : mNormalized = normalized;
81 0 : mStride = stride;
82 0 : mExplicitStride = (mStride ? mStride : mBytesPerVertex);
83 0 : mByteOffset = byteOffset;
84 0 : }
85 :
86 : void
87 0 : WebGLVertexAttribData::DoVertexAttribPointer(gl::GLContext* gl, GLuint index) const
88 : {
89 0 : if (mIntegerFunc) {
90 0 : gl->fVertexAttribIPointer(index, mSize, mType, mStride,
91 0 : (const void*)mByteOffset);
92 : } else {
93 0 : gl->fVertexAttribPointer(index, mSize, mType, mNormalized, mStride,
94 0 : (const void*)mByteOffset);
95 : }
96 0 : }
97 :
98 : } // namespace mozilla
|