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/GeometryRenderingStats.hpp>
5#include <Grindstone.Renderables.3D/include/Assets/Mesh3dAsset.hpp>
6#include <Grindstone.Renderables.3D/include/FrustumCulling.hpp>
7#include <Grindstone.Renderables.3D/include/Components/MeshRendererComponent.hpp>
8
9namespace Grindstone::Renderer {
11 glm::mat4 matrix;
12 uint32_t entityId;
13 };
14
15 template<typename MeshComponentType, typename RenderTask>
16 std::vector<RenderTask> GenerateTaskList(
18 entt::registry& registry,
20 const glm::mat4& viewMatrix,
21 Grindstone::HashedString renderQueueHash,
22 std::function<void(
23 std::vector<RenderTask>&,
26 const Mesh3dAsset*,
27 const MeshComponentType&,
29 )> submeshCallback
30 ) {
31 std::vector<RenderTask> renderTasks;
32 renderTasks.reserve(1000);
33
34 auto view = registry.view<const entt::entity, const TransformComponent, const MeshComponentType, MeshRendererComponent>();
35 view.each(
36 [&submeshCallback, &renderingStats, &registry, &renderTasks, &viewMatrix, frustum, renderQueueHash](
37 entt::entity entity,
38 const TransformComponent& transformComponent,
39 const MeshComponentType& meshComponent,
40 MeshRendererComponent& meshRenderComponent
41 ) {
42 const Mesh3dAsset* meshAsset = meshComponent.mesh.Get();
43
44 if (meshAsset == nullptr) {
45 return;
46 }
47
48 Math::Matrix4 transform = TransformComponent::GetWorldTransformMatrix(entity, registry);
49 glm::mat4 viewTransformTransform = viewMatrix * transform;
50
51 Grindstone::Renderer::AABB aabb{ meshAsset->boundingData.minAABB, meshAsset->boundingData.maxAABB };
52 if (!IsInFrustum(frustum, viewTransformTransform, aabb)) {
53 renderingStats.objectsCulled += 1;
54 return;
55 }
56
57 renderingStats.objectsRendered += 1;
58
59 RenderableBufferPair renderableData{
60 .matrix = transform,
61 .entityId = static_cast<uint32_t>(entity)
62 };
63 meshRenderComponent.perDrawUniformBuffer->UploadData(&renderableData);
64
65 std::vector<Grindstone::AssetReference<Grindstone::MaterialAsset>>& materials = meshRenderComponent.materials;
66 for (const Grindstone::Mesh3dAsset::Submesh& submesh : meshAsset->submeshes) {
67 submeshCallback(
68 renderTasks,
69 renderQueueHash,
70 submesh,
71 meshAsset,
72 meshComponent,
73 meshRenderComponent
74 );
75 }
76 }
77 );
78
79 return renderTasks;
80 }
81
82 template<typename RenderTask>
83 void RenderAllTasks(
84 Grindstone::Rendering::GeometryRenderStats& renderingStats,
85 GraphicsAPI::DescriptorSet* engineDescriptorSet,
86 GraphicsAPI::CommandBuffer* commandBuffer,
87 const std::vector<RenderTask>& renderTasks
88 ) {
89 const GraphicsAPI::PipelineLayout* pipelineLayout = nullptr;
90 const GraphicsAPI::GraphicsPipeline* graphicsPipeline = nullptr;
91 const GraphicsAPI::DescriptorSet* materialDescriptorSet = nullptr;
92
93 for (const RenderTask& renderTask : renderTasks) {
94 if (graphicsPipeline != renderTask.pipeline) {
95 graphicsPipeline = renderTask.pipeline;
96 pipelineLayout = graphicsPipeline->pipelineLayout;
97 commandBuffer->BindGraphicsPipeline(renderTask.pipeline);
98 renderingStats.pipelineBinds += 1;
99 }
100
101 commandBuffer->BindVertexArrayObject(renderTask.vertexArrayObject);
102
103 if (renderTask.materialDescriptorSet != materialDescriptorSet) {
104 std::array<GraphicsAPI::DescriptorSet*, 3> descriptors = {
105 engineDescriptorSet,
106 renderTask.materialDescriptorSet,
107 renderTask.perDrawDescriptorSet,
108 };
109 commandBuffer->BindGraphicsDescriptorSet(
110 pipelineLayout,
111 descriptors.data(),
112 0,
113 static_cast<uint32_t>(descriptors.size())
114 );
115 renderingStats.materialBinds += 1;
116 }
117
118 renderingStats.drawCalls += 1;
119 renderingStats.vertices += renderTask.indexCount;
120 renderingStats.triangles += renderTask.indexCount / 3;
121
122 commandBuffer->DrawIndices(
123 renderTask.baseIndex,
124 renderTask.indexCount,
125 0,
126 1,
127 renderTask.baseVertex
128 );
129 }
130 }
131}
Definition HashedString.hpp:9
Definition AttachmentInfo.hpp:15
Definition Mesh3dAsset.hpp:28
Definition Mesh3dAsset.hpp:26
Definition MeshRendererComponent.hpp:16
Definition FrustumCulling.hpp:14
Definition FrustumCulling.hpp:6
Definition RenderTasks.hpp:10
Definition GeometryRenderingStats.hpp:6
Definition TransformComponent.hpp:12