Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
RenderTasks.hpp
1#pragma once
2
3#include <Common/HashedString.hpp>
4#include <Common/Rendering/AssetRendererFilter.hpp>
5#include <Common/Rendering/GeometryRenderingStats.hpp>
6#include <Grindstone.Renderables.3D/include/Assets/Mesh3dAsset.hpp>
7#include <Grindstone.Renderables.3D/include/FrustumCulling.hpp>
8#include <Grindstone.Renderables.3D/include/Components/MeshRendererComponent.hpp>
9
10namespace Grindstone::Renderer {
12 glm::mat4 matrix;
13 uint32_t entityId;
14 };
15
16 template<typename MeshComponentType, typename RenderTask>
17 std::vector<RenderTask> GenerateTaskList(
19 Grindstone::HashedString renderQueueHash,
20 entt::registry& registry,
23 const glm::mat4& viewMatrix,
24 std::function<void(
25 std::vector<RenderTask>&,
28 const Mesh3dAsset*,
29 const MeshComponentType&,
31 )> submeshCallback
32 ) {
33 std::vector<RenderTask> renderTasks;
34 renderTasks.reserve(1000);
35
36 if (filter.useWhitelist) {
37 GS_ASSERT_ENGINE_WITH_MESSAGE(filter.blacklist.GetSize() == 0, "You can only use the blacklist if the whitelist is empty");
38
39 for (entt::entity& entity : filter.whitelist) {
40 auto [transformComponent, meshComponent, meshRendererComponent] = registry.try_get<const TransformComponent, const MeshComponentType, MeshRendererComponent>(entity);
41 if (transformComponent == nullptr || meshComponent == nullptr || meshRendererComponent == nullptr) {
42 continue;
43 }
44
45 const Mesh3dAsset* meshAsset = meshComponent->mesh.Get();
46
47 if (meshAsset == nullptr) {
48 continue;
49 }
50
51 Math::Matrix4 transform = TransformComponent::GetWorldTransformMatrix(entity, registry);
52 glm::mat4 viewTransformTransform = viewMatrix * transform;
53
54 Grindstone::Renderer::AABB aabb{ meshAsset->boundingData.minAABB, meshAsset->boundingData.maxAABB };
55 if (!IsInFrustum(frustum, viewTransformTransform, aabb)) {
56 renderingStats.objectsCulled += 1;
57 continue;
58 }
59
60 renderingStats.objectsRendered += 1;
61
62 RenderableBufferPair renderableData{
63 .matrix = transform,
64 .entityId = static_cast<uint32_t>(entity)
65 };
66 meshRendererComponent->perDrawUniformBuffer->UploadData(&renderableData);
67
68 std::vector<Grindstone::AssetReference<Grindstone::MaterialAsset>>& materials = meshRendererComponent->materials;
69 for (const Grindstone::Mesh3dAsset::Submesh& submesh : meshAsset->submeshes) {
70 submeshCallback(
71 renderTasks,
72 renderQueueHash,
73 submesh,
74 meshAsset,
75 *meshComponent,
76 *meshRendererComponent
77 );
78 }
79 }
80 }
81 else {
82 GS_ASSERT_ENGINE_WITH_MESSAGE(filter.blacklist.GetSize() == 0, "Blacklist support not implemented yet!");
83 GS_ASSERT_ENGINE_WITH_MESSAGE(filter.whitelist.GetSize() == 0, "You can only use the whitelist if useWhitelist is true");
84
85 auto view = registry.view<const entt::entity, const TransformComponent, const MeshComponentType, MeshRendererComponent>();
86 view.each(
87 [&submeshCallback, &renderingStats, &registry, &renderTasks, &viewMatrix, frustum, renderQueueHash](
88 entt::entity entity,
89 const TransformComponent& transformComponent,
90 const MeshComponentType& meshComponent,
91 MeshRendererComponent& meshRenderComponent
92 ) {
93 const Mesh3dAsset* meshAsset = meshComponent.mesh.Get();
94
95 if (meshAsset == nullptr) {
96 return;
97 }
98
99 Math::Matrix4 transform = TransformComponent::GetWorldTransformMatrix(entity, registry);
100 glm::mat4 viewTransformTransform = viewMatrix * transform;
101
102 Grindstone::Renderer::AABB aabb{ meshAsset->boundingData.minAABB, meshAsset->boundingData.maxAABB };
103 if (!IsInFrustum(frustum, viewTransformTransform, aabb)) {
104 renderingStats.objectsCulled += 1;
105 return;
106 }
107
108 renderingStats.objectsRendered += 1;
109
110 RenderableBufferPair renderableData{
111 .matrix = transform,
112 .entityId = static_cast<uint32_t>(entity)
113 };
114 meshRenderComponent.perDrawUniformBuffer->UploadData(&renderableData);
115
116 std::vector<Grindstone::AssetReference<Grindstone::MaterialAsset>>& materials = meshRenderComponent.materials;
117 for (const Grindstone::Mesh3dAsset::Submesh& submesh : meshAsset->submeshes) {
118 submeshCallback(
119 renderTasks,
120 renderQueueHash,
121 submesh,
122 meshAsset,
123 meshComponent,
124 meshRenderComponent
125 );
126 }
127 }
128 );
129 }
130
131 return renderTasks;
132 }
133
134 template<typename RenderTask>
135 void RenderAllTasks(
136 Grindstone::Rendering::GeometryRenderStats& renderingStats,
137 GraphicsAPI::DescriptorSet* engineDescriptorSet,
138 GraphicsAPI::CommandBuffer* commandBuffer,
139 const std::vector<RenderTask>& renderTasks
140 ) {
141 const GraphicsAPI::PipelineLayout* pipelineLayout = nullptr;
142 const GraphicsAPI::GraphicsPipeline* graphicsPipeline = nullptr;
143 const GraphicsAPI::DescriptorSet* materialDescriptorSet = nullptr;
144
145 for (const RenderTask& renderTask : renderTasks) {
146 if (graphicsPipeline != renderTask.pipeline) {
147 graphicsPipeline = renderTask.pipeline;
148 pipelineLayout = graphicsPipeline->pipelineLayout;
149 commandBuffer->BindGraphicsPipeline(renderTask.pipeline);
150 renderingStats.pipelineBinds += 1;
151 }
152
153 commandBuffer->BindVertexArrayObject(renderTask.vertexArrayObject);
154
155 if (renderTask.materialDescriptorSet != materialDescriptorSet) {
156 std::array<GraphicsAPI::DescriptorSet*, 3> descriptors = {
157 engineDescriptorSet,
158 renderTask.materialDescriptorSet,
159 renderTask.perDrawDescriptorSet,
160 };
161 commandBuffer->BindGraphicsDescriptorSet(
162 pipelineLayout,
163 descriptors.data(),
164 0,
165 static_cast<uint32_t>(descriptors.size())
166 );
167 renderingStats.materialBinds += 1;
168 }
169
170 renderingStats.drawCalls += 1;
171 renderingStats.vertices += renderTask.indexCount;
172 renderingStats.triangles += renderTask.indexCount / 3;
173
174 commandBuffer->DrawIndices(
175 renderTask.baseIndex,
176 renderTask.indexCount,
177 0,
178 1,
179 renderTask.baseVertex
180 );
181 }
182 }
183}
Definition HashedString.hpp:10
Definition AttachmentInfo.hpp:17
Definition Mesh3dAsset.hpp:28
Definition Mesh3dAsset.hpp:26
Definition MeshRendererComponent.hpp:16
Definition FrustumCulling.hpp:14
Definition FrustumCulling.hpp:6
Definition RenderTasks.hpp:11
Definition AssetRendererFilter.hpp:8
Definition GeometryRenderingStats.hpp:6
Definition TransformComponent.hpp:12