GrGLVertexArray.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrGLVertexArray_DEFINED
  8. #define GrGLVertexArray_DEFINED
  9. #include "include/gpu/GrGpuResource.h"
  10. #include "include/gpu/gl/GrGLTypes.h"
  11. #include "include/private/GrTypesPriv.h"
  12. #include "include/private/SkTArray.h"
  13. #include "src/gpu/gl/GrGLDefines.h"
  14. class GrBuffer;
  15. class GrGLGpu;
  16. /**
  17. * This sets and tracks the vertex attribute array state. It is used internally by GrGLVertexArray
  18. * (below) but is separate because it is also used to track the state of vertex array object 0.
  19. */
  20. class GrGLAttribArrayState {
  21. public:
  22. explicit GrGLAttribArrayState(int arrayCount = 0) {
  23. this->resize(arrayCount);
  24. }
  25. void resize(int newCount) {
  26. fAttribArrayStates.resize_back(newCount);
  27. this->invalidate();
  28. }
  29. /**
  30. * This function enables and sets vertex attrib state for the specified attrib index. It is
  31. * assumed that the GrGLAttribArrayState is tracking the state of the currently bound vertex
  32. * array object.
  33. */
  34. void set(GrGLGpu*,
  35. int attribIndex,
  36. const GrBuffer* vertexBuffer,
  37. GrVertexAttribType cpuType,
  38. GrSLType gpuType,
  39. GrGLsizei stride,
  40. size_t offsetInBytes,
  41. int divisor = 0);
  42. /**
  43. * This function enables the first 'enabledCount' vertex arrays and disables the rest.
  44. */
  45. void enableVertexArrays(const GrGLGpu*, int enabledCount,
  46. GrPrimitiveRestart = GrPrimitiveRestart::kNo);
  47. void invalidate() {
  48. int count = fAttribArrayStates.count();
  49. for (int i = 0; i < count; ++i) {
  50. fAttribArrayStates[i].invalidate();
  51. }
  52. fEnableStateIsValid = false;
  53. }
  54. /**
  55. * The number of attrib arrays that this object is configured to track.
  56. */
  57. int count() const { return fAttribArrayStates.count(); }
  58. private:
  59. static constexpr int kInvalidDivisor = -1;
  60. /**
  61. * Tracks the state of glVertexAttribArray for an attribute index.
  62. */
  63. struct AttribArrayState {
  64. void invalidate() {
  65. fVertexBufferUniqueID.makeInvalid();
  66. fDivisor = kInvalidDivisor;
  67. fUsingCpuBuffer = false;
  68. }
  69. GrGpuResource::UniqueID fVertexBufferUniqueID;
  70. bool fUsingCpuBuffer;
  71. GrVertexAttribType fCPUType;
  72. GrSLType fGPUType;
  73. GrGLsizei fStride;
  74. const GrGLvoid* fOffset;
  75. int fDivisor;
  76. };
  77. SkSTArray<16, AttribArrayState, true> fAttribArrayStates;
  78. int fNumEnabledArrays;
  79. GrPrimitiveRestart fPrimitiveRestartEnabled;
  80. bool fEnableStateIsValid = false;
  81. };
  82. /**
  83. * This class represents an OpenGL vertex array object. It manages the lifetime of the vertex array
  84. * and is used to track the state of the vertex array to avoid redundant GL calls.
  85. */
  86. class GrGLVertexArray {
  87. public:
  88. GrGLVertexArray(GrGLint id, int attribCount);
  89. /**
  90. * Binds this vertex array. If the ID has been deleted or abandoned then nullptr is returned.
  91. * Otherwise, the GrGLAttribArrayState that is tracking this vertex array's attrib bindings is
  92. * returned.
  93. */
  94. GrGLAttribArrayState* bind(GrGLGpu*);
  95. /**
  96. * This is a version of the above function that also binds an index buffer to the vertex
  97. * array object.
  98. */
  99. GrGLAttribArrayState* bindWithIndexBuffer(GrGLGpu* gpu, const GrBuffer* indexBuffer);
  100. GrGLuint arrayID() const { return fID; }
  101. void invalidateCachedState();
  102. private:
  103. GrGLuint fID;
  104. GrGLAttribArrayState fAttribArrays;
  105. GrGpuResource::UniqueID fIndexBufferUniqueID;
  106. };
  107. #endif