2
0

draw_queue_soa.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include <cstddef>
  10. #include <cstdint>
  11. #include <vector>
  12. namespace Render::GL {
  13. class Mesh;
  14. class Texture;
  15. class Buffer;
  16. } // namespace Render::GL
  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 *instance_buffer = nullptr;
  45. std::size_t instance_count = 0;
  46. GrassBatchParams params;
  47. };
  48. struct StoneBatchCmd {
  49. Buffer *instance_buffer = nullptr;
  50. std::size_t instance_count = 0;
  51. StoneBatchParams params;
  52. };
  53. struct PlantBatchCmd {
  54. Buffer *instance_buffer = nullptr;
  55. std::size_t instance_count = 0;
  56. PlantBatchParams params;
  57. };
  58. struct TerrainChunkCmd {
  59. Mesh *mesh = nullptr;
  60. QMatrix4x4 model;
  61. TerrainChunkParams params;
  62. std::uint16_t sort_key = 0x8000u;
  63. bool depth_write = true;
  64. float depth_bias = 0.0F;
  65. };
  66. struct GridCmd {
  67. QMatrix4x4 model;
  68. QMatrix4x4 mvp;
  69. QVector3D color{0.2F, 0.25F, 0.2F};
  70. float cell_size = 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 alpha_inner = 0.6F;
  79. float alpha_outer = 0.25F;
  80. };
  81. struct SelectionSmokeCmd {
  82. QMatrix4x4 model;
  83. QMatrix4x4 mvp;
  84. QVector3D color{1, 1, 1};
  85. float base_alpha = 0.15F;
  86. };
  87. class DrawQueueSoA {
  88. public:
  89. void clear() {
  90. m_grid_cmds.clear();
  91. m_selection_ring_cmds.clear();
  92. m_selection_smoke_cmds.clear();
  93. m_cylinder_cmds.clear();
  94. m_mesh_cmds.clear();
  95. m_fog_batch_cmds.clear();
  96. m_grass_batch_cmds.clear();
  97. m_stone_batch_cmds.clear();
  98. m_plant_batch_cmds.clear();
  99. m_terrain_chunk_cmds.clear();
  100. }
  101. void submit(const GridCmd &cmd) { m_grid_cmds.push_back(cmd); }
  102. void submit(const SelectionRingCmd &cmd) {
  103. m_selection_ring_cmds.push_back(cmd);
  104. }
  105. void submit(const SelectionSmokeCmd &cmd) {
  106. m_selection_smoke_cmds.push_back(cmd);
  107. }
  108. void submit(const CylinderCmd &cmd) { m_cylinder_cmds.push_back(cmd); }
  109. void submit(const MeshCmd &cmd) { m_mesh_cmds.push_back(cmd); }
  110. void submit(const FogBatchCmd &cmd) { m_fog_batch_cmds.push_back(cmd); }
  111. void submit(const GrassBatchCmd &cmd) { m_grass_batch_cmds.push_back(cmd); }
  112. void submit(const StoneBatchCmd &cmd) { m_stone_batch_cmds.push_back(cmd); }
  113. void submit(const PlantBatchCmd &cmd) { m_plant_batch_cmds.push_back(cmd); }
  114. void submit(const TerrainChunkCmd &cmd) {
  115. m_terrain_chunk_cmds.push_back(cmd);
  116. }
  117. bool empty() const {
  118. return m_grid_cmds.empty() && m_selection_ring_cmds.empty() &&
  119. m_selection_smoke_cmds.empty() && m_cylinder_cmds.empty() &&
  120. m_mesh_cmds.empty() && m_fog_batch_cmds.empty() &&
  121. m_grass_batch_cmds.empty() && m_stone_batch_cmds.empty() &&
  122. m_plant_batch_cmds.empty() && m_terrain_chunk_cmds.empty();
  123. }
  124. void sort_for_batching() {
  125. std::sort(m_mesh_cmds.begin(), m_mesh_cmds.end(),
  126. [](const MeshCmd &a, const MeshCmd &b) {
  127. return reinterpret_cast<uintptr_t>(a.texture) <
  128. reinterpret_cast<uintptr_t>(b.texture);
  129. });
  130. std::sort(m_terrain_chunk_cmds.begin(), m_terrain_chunk_cmds.end(),
  131. [](const TerrainChunkCmd &a, const TerrainChunkCmd &b) {
  132. return a.sort_key < b.sort_key;
  133. });
  134. }
  135. const std::vector<GridCmd> &grid_cmds() const { return m_grid_cmds; }
  136. const std::vector<SelectionRingCmd> &selection_ring_cmds() const {
  137. return m_selection_ring_cmds;
  138. }
  139. const std::vector<SelectionSmokeCmd> &selection_smoke_cmds() const {
  140. return m_selection_smoke_cmds;
  141. }
  142. const std::vector<CylinderCmd> &cylinder_cmds() const {
  143. return m_cylinder_cmds;
  144. }
  145. const std::vector<MeshCmd> &mesh_cmds() const { return m_mesh_cmds; }
  146. const std::vector<FogBatchCmd> &fog_batch_cmds() const {
  147. return m_fog_batch_cmds;
  148. }
  149. const std::vector<GrassBatchCmd> &grass_batch_cmds() const {
  150. return m_grass_batch_cmds;
  151. }
  152. const std::vector<StoneBatchCmd> &stone_batch_cmds() const {
  153. return m_stone_batch_cmds;
  154. }
  155. const std::vector<PlantBatchCmd> &plant_batch_cmds() const {
  156. return m_plant_batch_cmds;
  157. }
  158. const std::vector<TerrainChunkCmd> &terrain_chunk_cmds() const {
  159. return m_terrain_chunk_cmds;
  160. }
  161. private:
  162. std::vector<GridCmd> m_grid_cmds;
  163. std::vector<SelectionRingCmd> m_selection_ring_cmds;
  164. std::vector<SelectionSmokeCmd> m_selection_smoke_cmds;
  165. std::vector<CylinderCmd> m_cylinder_cmds;
  166. std::vector<MeshCmd> m_mesh_cmds;
  167. std::vector<FogBatchCmd> m_fog_batch_cmds;
  168. std::vector<GrassBatchCmd> m_grass_batch_cmds;
  169. std::vector<StoneBatchCmd> m_stone_batch_cmds;
  170. std::vector<PlantBatchCmd> m_plant_batch_cmds;
  171. std::vector<TerrainChunkCmd> m_terrain_chunk_cmds;
  172. };
  173. } // namespace Render::GL