draw_queue.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #pragma once
  2. #include "ground/grass_gpu.h"
  3. #include "ground/pine_gpu.h"
  4. #include "ground/plant_gpu.h"
  5. #include "ground/stone_gpu.h"
  6. #include "ground/terrain_gpu.h"
  7. #include <QMatrix4x4>
  8. #include <QVector3D>
  9. #include <algorithm>
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include <variant>
  13. #include <vector>
  14. namespace Render::GL {
  15. class Mesh;
  16. class Texture;
  17. class Buffer;
  18. class Shader;
  19. } // namespace Render::GL
  20. namespace Render::GL {
  21. struct MeshCmd {
  22. Mesh *mesh = nullptr;
  23. Texture *texture = nullptr;
  24. QMatrix4x4 model;
  25. QMatrix4x4 mvp;
  26. QVector3D color{1, 1, 1};
  27. float alpha = 1.0f;
  28. class Shader *shader = nullptr;
  29. };
  30. struct CylinderCmd {
  31. QVector3D start{0.0f, -0.5f, 0.0f};
  32. QVector3D end{0.0f, 0.5f, 0.0f};
  33. QVector3D color{1.0f, 1.0f, 1.0f};
  34. float radius = 1.0f;
  35. float alpha = 1.0f;
  36. };
  37. struct FogInstanceData {
  38. QVector3D center{0.0f, 0.25f, 0.0f};
  39. QVector3D color{0.05f, 0.05f, 0.05f};
  40. float alpha = 1.0f;
  41. float size = 1.0f;
  42. };
  43. struct FogBatchCmd {
  44. const FogInstanceData *instances = nullptr;
  45. std::size_t count = 0;
  46. };
  47. struct GrassBatchCmd {
  48. Buffer *instanceBuffer = nullptr;
  49. std::size_t instanceCount = 0;
  50. GrassBatchParams params;
  51. };
  52. struct StoneBatchCmd {
  53. Buffer *instanceBuffer = nullptr;
  54. std::size_t instanceCount = 0;
  55. StoneBatchParams params;
  56. };
  57. struct PlantBatchCmd {
  58. Buffer *instanceBuffer = nullptr;
  59. std::size_t instanceCount = 0;
  60. PlantBatchParams params;
  61. };
  62. struct PineBatchCmd {
  63. Buffer *instanceBuffer = nullptr;
  64. std::size_t instanceCount = 0;
  65. PineBatchParams params;
  66. };
  67. struct TerrainChunkCmd {
  68. Mesh *mesh = nullptr;
  69. QMatrix4x4 model;
  70. TerrainChunkParams params;
  71. std::uint16_t sortKey = 0x8000u;
  72. bool depthWrite = true;
  73. float depthBias = 0.0f;
  74. };
  75. struct GridCmd {
  76. QMatrix4x4 model;
  77. QMatrix4x4 mvp;
  78. QVector3D color{0.2f, 0.25f, 0.2f};
  79. float cellSize = 1.0f;
  80. float thickness = 0.06f;
  81. float extent = 50.0f;
  82. };
  83. struct SelectionRingCmd {
  84. QMatrix4x4 model;
  85. QMatrix4x4 mvp;
  86. QVector3D color{0, 0, 0};
  87. float alphaInner = 0.6f;
  88. float alphaOuter = 0.25f;
  89. };
  90. struct SelectionSmokeCmd {
  91. QMatrix4x4 model;
  92. QMatrix4x4 mvp;
  93. QVector3D color{1, 1, 1};
  94. float baseAlpha = 0.15f;
  95. };
  96. using DrawCmd =
  97. std::variant<GridCmd, SelectionRingCmd, SelectionSmokeCmd, CylinderCmd,
  98. MeshCmd, FogBatchCmd, GrassBatchCmd, StoneBatchCmd,
  99. PlantBatchCmd, PineBatchCmd, TerrainChunkCmd>;
  100. enum class DrawCmdType : std::uint8_t {
  101. Grid = 0,
  102. SelectionRing = 1,
  103. SelectionSmoke = 2,
  104. Cylinder = 3,
  105. Mesh = 4,
  106. FogBatch = 5,
  107. GrassBatch = 6,
  108. StoneBatch = 7,
  109. PlantBatch = 8,
  110. PineBatch = 9,
  111. TerrainChunk = 10
  112. };
  113. constexpr std::size_t MeshCmdIndex =
  114. static_cast<std::size_t>(DrawCmdType::Mesh);
  115. constexpr std::size_t GridCmdIndex =
  116. static_cast<std::size_t>(DrawCmdType::Grid);
  117. constexpr std::size_t SelectionRingCmdIndex =
  118. static_cast<std::size_t>(DrawCmdType::SelectionRing);
  119. constexpr std::size_t SelectionSmokeCmdIndex =
  120. static_cast<std::size_t>(DrawCmdType::SelectionSmoke);
  121. constexpr std::size_t CylinderCmdIndex =
  122. static_cast<std::size_t>(DrawCmdType::Cylinder);
  123. constexpr std::size_t FogBatchCmdIndex =
  124. static_cast<std::size_t>(DrawCmdType::FogBatch);
  125. constexpr std::size_t GrassBatchCmdIndex =
  126. static_cast<std::size_t>(DrawCmdType::GrassBatch);
  127. constexpr std::size_t StoneBatchCmdIndex =
  128. static_cast<std::size_t>(DrawCmdType::StoneBatch);
  129. constexpr std::size_t PlantBatchCmdIndex =
  130. static_cast<std::size_t>(DrawCmdType::PlantBatch);
  131. constexpr std::size_t PineBatchCmdIndex =
  132. static_cast<std::size_t>(DrawCmdType::PineBatch);
  133. constexpr std::size_t TerrainChunkCmdIndex =
  134. static_cast<std::size_t>(DrawCmdType::TerrainChunk);
  135. inline DrawCmdType drawCmdType(const DrawCmd &cmd) {
  136. return static_cast<DrawCmdType>(cmd.index());
  137. }
  138. class DrawQueue {
  139. public:
  140. void clear() { m_items.clear(); }
  141. void submit(const MeshCmd &c) { m_items.emplace_back(c); }
  142. void submit(const GridCmd &c) { m_items.emplace_back(c); }
  143. void submit(const SelectionRingCmd &c) { m_items.emplace_back(c); }
  144. void submit(const SelectionSmokeCmd &c) { m_items.emplace_back(c); }
  145. void submit(const CylinderCmd &c) { m_items.emplace_back(c); }
  146. void submit(const FogBatchCmd &c) { m_items.emplace_back(c); }
  147. void submit(const GrassBatchCmd &c) { m_items.emplace_back(c); }
  148. void submit(const StoneBatchCmd &c) { m_items.emplace_back(c); }
  149. void submit(const PlantBatchCmd &c) { m_items.emplace_back(c); }
  150. void submit(const PineBatchCmd &c) { m_items.emplace_back(c); }
  151. void submit(const TerrainChunkCmd &c) { m_items.emplace_back(c); }
  152. bool empty() const { return m_items.empty(); }
  153. std::size_t size() const { return m_items.size(); }
  154. const DrawCmd &getSorted(std::size_t i) const {
  155. return m_items[m_sortIndices[i]];
  156. }
  157. const std::vector<DrawCmd> &items() const { return m_items; }
  158. void sortForBatching() {
  159. const std::size_t count = m_items.size();
  160. m_sortKeys.resize(count);
  161. m_sortIndices.resize(count);
  162. for (std::size_t i = 0; i < count; ++i) {
  163. m_sortIndices[i] = static_cast<uint32_t>(i);
  164. m_sortKeys[i] = computeSortKey(m_items[i]);
  165. }
  166. if (count >= 2) {
  167. radixSortTwoPass(count);
  168. }
  169. }
  170. private:
  171. void radixSortTwoPass(std::size_t count) {
  172. constexpr int BUCKETS = 256;
  173. m_tempIndices.resize(count);
  174. {
  175. int histogram[BUCKETS] = {0};
  176. for (std::size_t i = 0; i < count; ++i) {
  177. uint8_t bucket = static_cast<uint8_t>(m_sortKeys[i] >> 56);
  178. ++histogram[bucket];
  179. }
  180. int offsets[BUCKETS];
  181. offsets[0] = 0;
  182. for (int i = 1; i < BUCKETS; ++i) {
  183. offsets[i] = offsets[i - 1] + histogram[i - 1];
  184. }
  185. for (std::size_t i = 0; i < count; ++i) {
  186. uint8_t bucket =
  187. static_cast<uint8_t>(m_sortKeys[m_sortIndices[i]] >> 56);
  188. m_tempIndices[offsets[bucket]++] = m_sortIndices[i];
  189. }
  190. }
  191. {
  192. int histogram[BUCKETS] = {0};
  193. for (std::size_t i = 0; i < count; ++i) {
  194. uint8_t bucket =
  195. static_cast<uint8_t>(m_sortKeys[m_tempIndices[i]] >> 48) & 0xFF;
  196. ++histogram[bucket];
  197. }
  198. int offsets[BUCKETS];
  199. offsets[0] = 0;
  200. for (int i = 1; i < BUCKETS; ++i) {
  201. offsets[i] = offsets[i - 1] + histogram[i - 1];
  202. }
  203. for (std::size_t i = 0; i < count; ++i) {
  204. uint8_t bucket =
  205. static_cast<uint8_t>(m_sortKeys[m_tempIndices[i]] >> 48) & 0xFF;
  206. m_sortIndices[offsets[bucket]++] = m_tempIndices[i];
  207. }
  208. }
  209. }
  210. uint64_t computeSortKey(const DrawCmd &cmd) const {
  211. enum class RenderOrder : uint8_t {
  212. TerrainChunk = 0,
  213. GrassBatch = 1,
  214. StoneBatch = 2,
  215. PlantBatch = 3,
  216. PineBatch = 4,
  217. SelectionRing = 5,
  218. FogBatch = 6,
  219. Mesh = 7,
  220. Cylinder = 8,
  221. SelectionSmoke = 9,
  222. Grid = 10
  223. };
  224. static constexpr uint8_t kTypeOrder[] = {
  225. static_cast<uint8_t>(RenderOrder::Grid),
  226. static_cast<uint8_t>(RenderOrder::SelectionRing),
  227. static_cast<uint8_t>(RenderOrder::SelectionSmoke),
  228. static_cast<uint8_t>(RenderOrder::Cylinder),
  229. static_cast<uint8_t>(RenderOrder::Mesh),
  230. static_cast<uint8_t>(RenderOrder::FogBatch),
  231. static_cast<uint8_t>(RenderOrder::GrassBatch),
  232. static_cast<uint8_t>(RenderOrder::StoneBatch),
  233. static_cast<uint8_t>(RenderOrder::PlantBatch),
  234. static_cast<uint8_t>(RenderOrder::PineBatch),
  235. static_cast<uint8_t>(RenderOrder::TerrainChunk)};
  236. const std::size_t typeIndex = cmd.index();
  237. constexpr std::size_t typeCount =
  238. sizeof(kTypeOrder) / sizeof(kTypeOrder[0]);
  239. const uint8_t typeOrder = typeIndex < typeCount
  240. ? kTypeOrder[typeIndex]
  241. : static_cast<uint8_t>(typeIndex);
  242. uint64_t key = static_cast<uint64_t>(typeOrder) << 56;
  243. if (cmd.index() == MeshCmdIndex) {
  244. const auto &mesh = std::get<MeshCmdIndex>(cmd);
  245. uint64_t texPtr =
  246. reinterpret_cast<uintptr_t>(mesh.texture) & 0x0000FFFFFFFFFFFF;
  247. key |= texPtr;
  248. } else if (cmd.index() == GrassBatchCmdIndex) {
  249. const auto &grass = std::get<GrassBatchCmdIndex>(cmd);
  250. uint64_t bufferPtr = reinterpret_cast<uintptr_t>(grass.instanceBuffer) &
  251. 0x0000FFFFFFFFFFFF;
  252. key |= bufferPtr;
  253. } else if (cmd.index() == StoneBatchCmdIndex) {
  254. const auto &stone = std::get<StoneBatchCmdIndex>(cmd);
  255. uint64_t bufferPtr = reinterpret_cast<uintptr_t>(stone.instanceBuffer) &
  256. 0x0000FFFFFFFFFFFF;
  257. key |= bufferPtr;
  258. } else if (cmd.index() == PlantBatchCmdIndex) {
  259. const auto &plant = std::get<PlantBatchCmdIndex>(cmd);
  260. uint64_t bufferPtr = reinterpret_cast<uintptr_t>(plant.instanceBuffer) &
  261. 0x0000FFFFFFFFFFFF;
  262. key |= bufferPtr;
  263. } else if (cmd.index() == PineBatchCmdIndex) {
  264. const auto &pine = std::get<PineBatchCmdIndex>(cmd);
  265. uint64_t bufferPtr =
  266. reinterpret_cast<uintptr_t>(pine.instanceBuffer) & 0x0000FFFFFFFFFFFF;
  267. key |= bufferPtr;
  268. } else if (cmd.index() == TerrainChunkCmdIndex) {
  269. const auto &terrain = std::get<TerrainChunkCmdIndex>(cmd);
  270. uint64_t sortByte = static_cast<uint64_t>((terrain.sortKey >> 8) & 0xFFu);
  271. key |= sortByte << 48;
  272. uint64_t meshPtr =
  273. reinterpret_cast<uintptr_t>(terrain.mesh) & 0x0000FFFFFFFFFFFFu;
  274. key |= meshPtr;
  275. }
  276. return key;
  277. }
  278. std::vector<DrawCmd> m_items;
  279. std::vector<uint32_t> m_sortIndices;
  280. std::vector<uint64_t> m_sortKeys;
  281. std::vector<uint32_t> m_tempIndices;
  282. };
  283. } // namespace Render::GL