mesh_instancing_pipeline.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 = 512;
  11. constexpr std::size_t k_max_instances_per_batch = 8192;
  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. GLsizeiptr const upload_size =
  144. static_cast<GLsizeiptr>(count * sizeof(MeshInstanceGpu));
  145. void *mapped =
  146. glMapBufferRange(GL_ARRAY_BUFFER, 0, upload_size,
  147. GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
  148. if (mapped != nullptr) {
  149. std::memcpy(mapped, m_instances.data(),
  150. static_cast<std::size_t>(upload_size));
  151. glUnmapBuffer(GL_ARRAY_BUFFER);
  152. } else {
  153. glBufferSubData(GL_ARRAY_BUFFER, 0, upload_size, m_instances.data());
  154. }
  155. if (!m_currentMesh->bind_vao()) {
  156. m_instances.clear();
  157. return;
  158. }
  159. if (!m_currentMesh->bind_vao()) {
  160. m_instances.clear();
  161. return;
  162. }
  163. setup_instance_attributes();
  164. if (m_currentTexture != nullptr) {
  165. m_currentTexture->bind(0);
  166. }
  167. m_currentMesh->draw_instanced_raw(count);
  168. glVertexAttribDivisor(k_instance_model_col0_loc, 0);
  169. glVertexAttribDivisor(k_instance_model_col1_loc, 0);
  170. glVertexAttribDivisor(k_instance_model_col2_loc, 0);
  171. glVertexAttribDivisor(k_instance_color_alpha_loc, 0);
  172. glDisableVertexAttribArray(k_instance_model_col0_loc);
  173. glDisableVertexAttribArray(k_instance_model_col1_loc);
  174. glDisableVertexAttribArray(k_instance_model_col2_loc);
  175. glDisableVertexAttribArray(k_instance_color_alpha_loc);
  176. m_currentMesh->unbind_vao();
  177. glBindBuffer(GL_ARRAY_BUFFER, 0);
  178. m_instances.clear();
  179. }
  180. auto MeshInstancingPipeline::instance_count() const -> std::size_t {
  181. return m_instances.size();
  182. }
  183. auto MeshInstancingPipeline::has_pending() const -> bool {
  184. return !m_instances.empty();
  185. }
  186. void MeshInstancingPipeline::setup_instance_attributes() {
  187. glBindBuffer(GL_ARRAY_BUFFER, m_instanceBuffer);
  188. const auto stride = static_cast<GLsizei>(sizeof(MeshInstanceGpu));
  189. glEnableVertexAttribArray(k_instance_model_col0_loc);
  190. glVertexAttribPointer(
  191. k_instance_model_col0_loc, 4, GL_FLOAT, GL_FALSE, stride,
  192. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, model_col0)));
  193. glVertexAttribDivisor(k_instance_model_col0_loc, 1);
  194. glEnableVertexAttribArray(k_instance_model_col1_loc);
  195. glVertexAttribPointer(
  196. k_instance_model_col1_loc, 4, GL_FLOAT, GL_FALSE, stride,
  197. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, model_col1)));
  198. glVertexAttribDivisor(k_instance_model_col1_loc, 1);
  199. glEnableVertexAttribArray(k_instance_model_col2_loc);
  200. glVertexAttribPointer(
  201. k_instance_model_col2_loc, 4, GL_FLOAT, GL_FALSE, stride,
  202. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, model_col2)));
  203. glVertexAttribDivisor(k_instance_model_col2_loc, 1);
  204. glEnableVertexAttribArray(k_instance_color_alpha_loc);
  205. glVertexAttribPointer(
  206. k_instance_color_alpha_loc, 4, GL_FLOAT, GL_FALSE, stride,
  207. reinterpret_cast<void *>(offsetof(MeshInstanceGpu, color_alpha)));
  208. glVertexAttribDivisor(k_instance_color_alpha_loc, 1);
  209. }
  210. } // namespace Render::GL::BackendPipelines