draw_queue_soa.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #pragma once
  2. #include "ground/grass_gpu.h"
  3. #include "ground/plant_gpu.h"
  4. #include "ground/stone_gpu.h"
  5. #include "ground/terrain_gpu.h"
  6. #include <QMatrix4x4>
  7. #include <QVector3D>
  8. #include <algorithm>
  9. <parameter name = "cstddef">
  10. #include <cstdint>
  11. #include <vector>
  12. namespace Render::GL {
  13. class Mesh;
  14. class Texture;
  15. class Buffer;
  16. }
  17. namespace Render::GL {
  18. struct MeshCmd {
  19. Mesh *mesh = nullptr;
  20. Texture *texture = nullptr;
  21. QMatrix4x4 model;
  22. QMatrix4x4 mvp;
  23. QVector3D color{1, 1, 1};
  24. float alpha = 1.0f;
  25. };
  26. struct CylinderCmd {
  27. QVector3D start{0.0f, -0.5f, 0.0f};
  28. QVector3D end{0.0f, 0.5f, 0.0f};
  29. QVector3D color{1.0f, 1.0f, 1.0f};
  30. float radius = 1.0f;
  31. float alpha = 1.0f;
  32. };
  33. struct FogInstanceData {
  34. QVector3D center{0.0f, 0.25f, 0.0f};
  35. QVector3D color{0.05f, 0.05f, 0.05f};
  36. float alpha = 1.0f;
  37. float size = 1.0f;
  38. };
  39. struct FogBatchCmd {
  40. const FogInstanceData *instances = nullptr;
  41. std::size_t count = 0;
  42. };
  43. struct GrassBatchCmd {
  44. Buffer *instanceBuffer = nullptr;
  45. std::size_t instanceCount = 0;
  46. GrassBatchParams params;
  47. };
  48. struct StoneBatchCmd {
  49. Buffer *instanceBuffer = nullptr;
  50. std::size_t instanceCount = 0;
  51. StoneBatchParams params;
  52. };
  53. struct PlantBatchCmd {
  54. Buffer *instanceBuffer = nullptr;
  55. std::size_t instanceCount = 0;
  56. PlantBatchParams params;
  57. };
  58. struct TerrainChunkCmd {
  59. Mesh *mesh = nullptr;
  60. QMatrix4x4 model;
  61. TerrainChunkParams params;
  62. std::uint16_t sortKey = 0x8000u;
  63. bool depthWrite = true;
  64. float depthBias = 0.0f;
  65. };
  66. struct GridCmd {
  67. QMatrix4x4 model;
  68. QMatrix4x4 mvp;
  69. QVector3D color{0.2f, 0.25f, 0.2f};
  70. float cellSize = 1.0f;
  71. float thickness = 0.06f;
  72. float extent = 50.0f;
  73. };
  74. struct SelectionRingCmd {
  75. QMatrix4x4 model;
  76. QMatrix4x4 mvp;
  77. QVector3D color{0, 0, 0};
  78. float alphaInner = 0.6f;
  79. float alphaOuter = 0.25f;
  80. };
  81. struct SelectionSmokeCmd {
  82. QMatrix4x4 model;
  83. QMatrix4x4 mvp;
  84. QVector3D color{1, 1, 1};
  85. float baseAlpha = 0.15f;
  86. };
  87. class DrawQueueSoA {
  88. public:
  89. void clear() {
  90. m_gridCmds.clear();
  91. m_selectionRingCmds.clear();
  92. m_selectionSmokeCmds.clear();
  93. m_cylinderCmds.clear();
  94. m_meshCmds.clear();
  95. m_fogBatchCmds.clear();
  96. m_grassBatchCmds.clear();
  97. m_stoneBatchCmds.clear();
  98. m_plantBatchCmds.clear();
  99. m_terrainChunkCmds.clear();
  100. }
  101. void submit(const GridCmd &cmd) { m_gridCmds.push_back(cmd); }
  102. void submit(const SelectionRingCmd &cmd) {
  103. m_selectionRingCmds.push_back(cmd);
  104. }
  105. void submit(const SelectionSmokeCmd &cmd) {
  106. m_selectionSmokeCmds.push_back(cmd);
  107. }
  108. void submit(const CylinderCmd &cmd) { m_cylinderCmds.push_back(cmd); }
  109. void submit(const MeshCmd &cmd) { m_meshCmds.push_back(cmd); }
  110. void submit(const FogBatchCmd &cmd) { m_fogBatchCmds.push_back(cmd); }
  111. void submit(const GrassBatchCmd &cmd) { m_grassBatchCmds.push_back(cmd); }
  112. void submit(const StoneBatchCmd &cmd) { m_stoneBatchCmds.push_back(cmd); }
  113. void submit(const PlantBatchCmd &cmd) { m_plantBatchCmds.push_back(cmd); }
  114. void submit(const TerrainChunkCmd &cmd) { m_terrainChunkCmds.push_back(cmd); }
  115. bool empty() const {
  116. return m_gridCmds.empty() && m_selectionRingCmds.empty() &&
  117. m_selectionSmokeCmds.empty() && m_cylinderCmds.empty() &&
  118. m_meshCmds.empty() && m_fogBatchCmds.empty() &&
  119. m_grassBatchCmds.empty() && m_stoneBatchCmds.empty() &&
  120. m_plantBatchCmds.empty() && m_terrainChunkCmds.empty();
  121. }
  122. void sortForBatching() {
  123. std::sort(m_meshCmds.begin(), m_meshCmds.end(),
  124. [](const MeshCmd &a, const MeshCmd &b) {
  125. return reinterpret_cast<uintptr_t>(a.texture) <
  126. reinterpret_cast<uintptr_t>(b.texture);
  127. });
  128. std::sort(m_terrainChunkCmds.begin(), m_terrainChunkCmds.end(),
  129. [](const TerrainChunkCmd &a, const TerrainChunkCmd &b) {
  130. return a.sortKey < b.sortKey;
  131. });
  132. }
  133. const std::vector<GridCmd> &gridCmds() const { return m_gridCmds; }
  134. const std::vector<SelectionRingCmd> &selectionRingCmds() const {
  135. return m_selectionRingCmds;
  136. }
  137. const std::vector<SelectionSmokeCmd> &selectionSmokeCmds() const {
  138. return m_selectionSmokeCmds;
  139. }
  140. const std::vector<CylinderCmd> &cylinderCmds() const {
  141. return m_cylinderCmds;
  142. }
  143. const std::vector<MeshCmd> &meshCmds() const { return m_meshCmds; }
  144. const std::vector<FogBatchCmd> &fogBatchCmds() const {
  145. return m_fogBatchCmds;
  146. }
  147. const std::vector<GrassBatchCmd> &grassBatchCmds() const {
  148. return m_grassBatchCmds;
  149. }
  150. const std::vector<StoneBatchCmd> &stoneBatchCmds() const {
  151. return m_stoneBatchCmds;
  152. }
  153. const std::vector<PlantBatchCmd> &plantBatchCmds() const {
  154. return m_plantBatchCmds;
  155. }
  156. const std::vector<TerrainChunkCmd> &terrainChunkCmds() const {
  157. return m_terrainChunkCmds;
  158. }
  159. private:
  160. std::vector<GridCmd> m_gridCmds;
  161. std::vector<SelectionRingCmd> m_selectionRingCmds;
  162. std::vector<SelectionSmokeCmd> m_selectionSmokeCmds;
  163. std::vector<CylinderCmd> m_cylinderCmds;
  164. std::vector<MeshCmd> m_meshCmds;
  165. std::vector<FogBatchCmd> m_fogBatchCmds;
  166. std::vector<GrassBatchCmd> m_grassBatchCmds;
  167. std::vector<StoneBatchCmd> m_stoneBatchCmds;
  168. std::vector<PlantBatchCmd> m_plantBatchCmds;
  169. std::vector<TerrainChunkCmd> m_terrainChunkCmds;
  170. };
  171. } // namespace Render::GL