draw_queue.h 11 KB

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