mesh_instancing_pipeline.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "mesh_instancing_pipeline.h"
  2. #include "../backend.h"
  3. #include "../mesh.h"
  4. #include "../texture.h"
  5. #include <QDebug>
  6. #include <QOpenGLContext>
  7. #include <cstring>
  8. namespace Render::GL::BackendPipelines {
  9. namespace {
  10. constexpr std::size_t k_initial_capacity = 256;
  11. constexpr std::size_t k_max_instances_per_batch = 4096;
  12. constexpr GLuint k_instance_model_col0_loc = 3;
  13. constexpr GLuint k_instance_model_col1_loc = 4;
  14. constexpr GLuint k_instance_model_col2_loc = 5;
  15. constexpr GLuint k_instance_color_alpha_loc = 6;
  16. } // namespace
  17. MeshInstancingPipeline::MeshInstancingPipeline(GL::Backend *backend,
  18. GL::ShaderCache *shader_cache)
  19. : m_backend(backend), m_shaderCache(shader_cache) {
  20. m_instances.reserve(k_initial_capacity);
  21. }
  22. MeshInstancingPipeline::~MeshInstancingPipeline() { shutdown(); }
  23. auto MeshInstancingPipeline::initialize() -> bool {
  24. if (m_initialized) {
  25. return true;
  26. }
  27. if (QOpenGLContext::currentContext() == nullptr) {
  28. qWarning() << "MeshInstancingPipeline::initialize called without GL "
  29. "context";
  30. return false;
  31. }
  32. initializeOpenGLFunctions();
  33. glGenBuffers(1, &m_instanceBuffer);
  34. if (m_instanceBuffer == 0) {
  35. qWarning() << "MeshInstancingPipeline: failed to create instance buffer";
  36. return false;
  37. }
  38. m_instanceCapacity = k_initial_capacity;
  39. glBindBuffer(GL_ARRAY_BUFFER, m_instanceBuffer);
  40. glBufferData(
  41. GL_ARRAY_BUFFER,
  42. static_cast<GLsizeiptr>(m_instanceCapacity * sizeof(MeshInstanceGpu)),
  43. nullptr, GL_DYNAMIC_DRAW);
  44. glBindBuffer(GL_ARRAY_BUFFER, 0);
  45. m_initialized = true;
  46. qInfo() << "MeshInstancingPipeline initialized with capacity"
  47. << m_instanceCapacity;
  48. return true;
  49. }
  50. void MeshInstancingPipeline::shutdown() {
  51. if (!m_initialized) {
  52. return;
  53. }
  54. if (QOpenGLContext::currentContext() != nullptr) {
  55. if (m_instanceBuffer != 0) {
  56. glDeleteBuffers(1, &m_instanceBuffer);
  57. m_instanceBuffer = 0;
  58. }
  59. }
  60. m_instances.clear();
  61. m_currentMesh = nullptr;
  62. m_currentShader = nullptr;
  63. m_currentTexture = nullptr;
  64. m_initialized = false;
  65. }
  66. void MeshInstancingPipeline::cache_uniforms() {}
  67. auto MeshInstancingPipeline::is_initialized() const -> bool {
  68. return m_initialized;
  69. }
  70. void MeshInstancingPipeline::begin_frame() {
  71. m_instances.clear();
  72. m_currentMesh = nullptr;
  73. m_currentShader = nullptr;
  74. m_currentTexture = nullptr;
  75. }
  76. auto MeshInstancingPipeline::can_batch(Mesh *mesh, Shader *shader,
  77. Texture *texture) const -> bool {
  78. if (m_instances.empty()) {
  79. return true;
  80. }
  81. if (m_instances.size() >= k_max_instances_per_batch) {
  82. return false;
  83. }
  84. return mesh == m_currentMesh && shader == m_currentShader &&
  85. texture == m_currentTexture;
  86. }
  87. void MeshInstancingPipeline::accumulate(const QMatrix4x4 &model,
  88. const QVector3D &color, float alpha,
  89. int) {
  90. MeshInstanceGpu inst{};
  91. const float *data = model.constData();
  92. inst.model_col0[0] = data[0];
  93. inst.model_col0[1] = data[1];
  94. inst.model_col0[2] = data[2];
  95. inst.model_col0[3] = data[12];
  96. inst.model_col1[0] = data[4];
  97. inst.model_col1[1] = data[5];
  98. inst.model_col1[2] = data[6];
  99. inst.model_col1[3] = data[13];
  100. inst.model_col2[0] = data[8];
  101. inst.model_col2[1] = data[9];
  102. inst.model_col2[2] = data[10];
  103. inst.model_col2[3] = data[14];
  104. inst.color_alpha[0] = color.x();
  105. inst.color_alpha[1] = color.y();
  106. inst.color_alpha[2] = color.z();
  107. inst.color_alpha[3] = alpha;
  108. m_instances.push_back(inst);
  109. }
  110. void MeshInstancingPipeline::begin_batch(Mesh *mesh, Shader *shader,
  111. Texture *texture) {
  112. m_currentMesh = mesh;
  113. m_currentShader = shader;
  114. m_currentTexture = texture;
  115. }
  116. void MeshInstancingPipeline::flush() {
  117. if (m_instances.empty()) {
  118. return;
  119. }
  120. if (m_currentMesh == nullptr || m_currentShader == nullptr ||
  121. !m_initialized) {
  122. qWarning() << "MeshInstancingPipeline::flush called with invalid state:"
  123. << "mesh=" << m_currentMesh << "shader=" << m_currentShader
  124. << "initialized=" << m_initialized
  125. << "instances=" << m_instances.size();
  126. m_instances.clear();
  127. return;
  128. }
  129. const std::size_t count = m_instances.size();
  130. if (count > m_instanceCapacity) {
  131. std::size_t newCapacity = m_instanceCapacity;
  132. while (newCapacity < count) {
  133. newCapacity *= 2;
  134. }
  135. newCapacity = std::min(newCapacity, k_max_instances_per_batch);
  136. glBindBuffer(GL_ARRAY_BUFFER, m_instanceBuffer);
  137. glBufferData(GL_ARRAY_BUFFER,
  138. static_cast<GLsizeiptr>(newCapacity * sizeof(MeshInstanceGpu)),
  139. nullptr, GL_DYNAMIC_DRAW);
  140. m_instanceCapacity = newCapacity;
  141. }
  142. glBindBuffer(GL_ARRAY_BUFFER, m_instanceBuffer);
  143. glBufferSubData(GL_ARRAY_BUFFER, 0,
  144. static_cast<GLsizeiptr>(count * sizeof(MeshInstanceGpu)),
  145. m_instances.data());
  146. setup_instance_attributes();
  147. if (m_currentTexture != nullptr) {
  148. m_currentTexture->bind(0);
  149. }
  150. m_currentMesh->draw_instanced(count);
  151. glBindBuffer(GL_ARRAY_BUFFER, 0);
  152. glDisableVertexAttribArray(k_instance_model_col0_loc);
  153. glDisableVertexAttribArray(k_instance_model_col1_loc);
  154. glDisableVertexAttribArray(k_instance_model_col2_loc);
  155. glDisableVertexAttribArray(k_instance_color_alpha_loc);
  156. m_instances.clear();
  157. }
  158. auto MeshInstancingPipeline::instance_count() const -> std::size_t {
  159. return m_instances.size();
  160. }
  161. auto MeshInstancingPipeline::has_pending() const -> bool {
  162. return !m_instances.empty();
  163. }
  164. void MeshInstancingPipeline::setup_instance_attributes() {
  165. glBindBuffer(GL_ARRAY_BUFFER, m_instanceBuffer);
  166. const auto stride = static_cast<GLsizei>(sizeof(MeshInstanceGpu));
  167. glEnableVertexAttribArray(k_instance_model_col0_loc);
  168. glVertexAttribPointer(
  169. k_instance_model_col0_loc, 4, GL_FLOAT, GL_FALSE, stride,
  170. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, model_col0)));
  171. glVertexAttribDivisor(k_instance_model_col0_loc, 1);
  172. glEnableVertexAttribArray(k_instance_model_col1_loc);
  173. glVertexAttribPointer(
  174. k_instance_model_col1_loc, 4, GL_FLOAT, GL_FALSE, stride,
  175. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, model_col1)));
  176. glVertexAttribDivisor(k_instance_model_col1_loc, 1);
  177. glEnableVertexAttribArray(k_instance_model_col2_loc);
  178. glVertexAttribPointer(
  179. k_instance_model_col2_loc, 4, GL_FLOAT, GL_FALSE, stride,
  180. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, model_col2)));
  181. glVertexAttribDivisor(k_instance_model_col2_loc, 1);
  182. glEnableVertexAttribArray(k_instance_color_alpha_loc);
  183. glVertexAttribPointer(
  184. k_instance_color_alpha_loc, 4, GL_FLOAT, GL_FALSE, stride,
  185. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, color_alpha)));
  186. glVertexAttribDivisor(k_instance_color_alpha_loc, 1);
  187. }
  188. } // namespace Render::GL::BackendPipelines