WebGLVertexAttribData.cpp 2.6 KB

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