candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
RobotScene.h
Go to the documentation of this file.
1
4#pragma once
5
6#include "Multibody.h"
7
8#include "../core/Device.h"
12#include "../core/Texture.h"
13#include "../posteffects/SSAO.h"
14#include "../utils/MeshData.h"
15
16#include <magic_enum/magic_enum.hpp>
17#include <entt/entity/fwd.hpp>
18#include <coal/fwd.hh>
19#include <pinocchio/multibody/fwd.hpp>
20#include <set>
21
22namespace candlewick {
23enum class RenderMode;
24
25namespace multibody {
26
33 void updateRobotTransforms(entt::registry &registry,
34 const pin::GeometryModel &geom_model,
35 const pin::GeometryData &geom_data);
36
41 class RobotScene final {
42 [[nodiscard]] bool hasInternalPointers() const {
43 return m_geomModel && m_geomData;
44 }
45
46 void compositeTransparencyPass(CommandBuffer &command_buffer);
47
48 void renderPBRTriangleGeometry(CommandBuffer &command_buffer,
49 const Camera &camera, bool transparent);
50
51 void renderOtherGeometry(CommandBuffer &command_buffer,
52 const Camera &camera);
53
54 void initGBuffer();
55
56 void initCompositePipeline(const MeshLayout &layout);
57
58 public:
64 using enum PipelineType;
73
75 static PipelineType pinGeomToPipeline(const coal::CollisionGeometry &geom);
76
78 static constexpr SDL_GPUPrimitiveType
80 switch (type) {
82 return SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
84 return SDL_GPU_PRIMITIVETYPE_LINELIST;
86 return SDL_GPU_PRIMITIVETYPE_POINTLIST;
87 }
88 }
89
90 template <PipelineType T> using pipeline_tag = entt::integral_constant<T>;
91
93 // shader set
94 const char *vertex_shader_path;
96 SDL_GPUCullMode cull_mode = SDL_GPU_CULLMODE_BACK;
97 };
98 struct Config {
101 .vertex_shader_path = "PbrBasic.vert",
102 .fragment_shader_path = "PbrBasic.frag",
103 };
105 .vertex_shader_path = "PbrBasic.vert",
106 .fragment_shader_path = "PbrTransparent.frag",
107 .cull_mode = SDL_GPU_CULLMODE_NONE,
108 };
111 .vertex_shader_path = "Hud3dElement.vert",
112 .fragment_shader_path = "Hud3dElement.frag",
113 };
115 bool enable_shadows = true;
116 bool enable_ssao = true;
119 };
120
121 struct PipelineKey {
125
126 auto operator<=>(const PipelineKey &) const = default;
127 };
129 std::map<PipelineKey, GraphicsPipeline> m_store;
130
131 public:
132 PipelineManager() : m_store() {}
133
134 bool contains(const PipelineKey &key) const {
135 return m_store.contains(key);
136 }
137
141
143 auto it = m_store.find(key);
144 if (it != m_store.cend())
145 return &it->second;
146
147 return nullptr;
148 }
149
150 void set(const PipelineKey &key, GraphicsPipeline &&pipeline) {
151 m_store.emplace(key, std::move(pipeline));
152 }
153
154 void clear() { m_store.clear(); }
155 };
156
157 std::array<DirectionalLight, kNumLights> directionalLight;
159 struct GBuffer {
162
163 // WBOIT buffers
168 SDL_GPUSampler *sampler = nullptr; // composite pass
169
170 bool initialized() const {
171 return (sampler != nullptr) && normalMap && accumTexture &&
173 }
174
175 void release() noexcept {
176 auto *device = normalMap.device();
177 normalMap.destroy();
178 resolveNormalMap.destroy();
179 accumTexture.destroy();
180 revealTexture.destroy();
181 resolveAccumTexture.destroy();
182 resolveRevealTexture.destroy();
183 if (sampler) {
184 SDL_ReleaseGPUSampler(device, sampler);
185 sampler = nullptr;
186 }
187 }
188 ~GBuffer() noexcept { this->release(); }
191
193 RobotScene(entt::registry &registry, const RenderContext &renderer);
194
198 RobotScene(entt::registry &registry, const RenderContext &renderer,
199 const pin::GeometryModel &geom_model,
200 const pin::GeometryData &geom_data, Config config);
201
202 RobotScene(const RobotScene &) = delete;
203
204 void setConfig(const Config &config) {
205 if (m_initialized)
207 "Cannot call setConfig() after render system was initialized.");
208
209 m_config = config;
210 }
211
214 void loadModels(const pin::GeometryModel &geom_model,
215 const pin::GeometryData &geom_data);
216
218 void update();
219
221 const std::vector<OpaqueCastable> &castables() const { return m_castables; }
222
223 Uint32 numLights() const noexcept { return shadowPass.numLights(); }
224
225 entt::entity
228
229 entt::entity
230 addEnvironmentObject(MeshData &&data, const Eigen::Affine3f &T,
232 return addEnvironmentObject(std::move(data), T.matrix(), pipe_type);
233 }
234
240
242 createRenderPipeline(const PipelineKey &key, const MeshLayout &layout,
243 SDL_GPUTextureFormat render_target_format,
244 SDL_GPUTextureFormat depth_stencil_format);
245
248 void render(CommandBuffer &command_buffer, const Camera &camera);
249
250 void renderOpaque(CommandBuffer &command_buffer, const Camera &camera);
251
252 void renderTransparent(CommandBuffer &command_buffer, const Camera &camera);
253
255 void release();
256
257 Config &config() { return m_config; }
258 const Config &config() const { return m_config; }
259 inline bool pbrHasPrepass() const { return m_config.triangle_has_prepass; }
260 inline bool shadowsEnabled() const { return m_config.enable_shadows; }
261
262 using pipeline_req_t = std::tuple<MeshLayout, PipelineKey>;
265 void
266 ensurePipelinesExist(const std::set<pipeline_req_t> &required_pipelines);
267
269 const pin::GeometryModel &geomModel() const { return *m_geomModel; }
270
272 const pin::GeometryData &geomData() const { return *m_geomData; }
273
274 const Device &device() { return m_renderer.device; }
275 entt::registry &registry() { return m_registry; }
276 const entt::registry &registry() const { return m_registry; }
277
278 private:
279 entt::registry &m_registry;
280 const RenderContext &m_renderer;
281 Config m_config;
282 const pin::GeometryModel *m_geomModel;
283 const pin::GeometryData *m_geomData;
284 std::vector<OpaqueCastable> m_castables;
285 bool m_initialized;
286 PipelineManager m_pipelines;
287 GraphicsPipeline m_wboitComposite{NoInit};
288 };
289 static_assert(Scene<RobotScene>);
290
291} // namespace multibody
292} // namespace candlewick
Definition CommandBuffer.h:16
Class representing a graphics pipeline.
Definition GraphicsPipeline.h:15
A class to store type-erased vertex data and index data.
Definition MeshData.h:33
This class defines the layout of a mesh's vertices.
Definition MeshLayout.h:122
Class for defining the shadow maps (as an atlas) and rendering into it.
Definition DepthAndShadowPass.h:127
Definition Texture.h:9
PipelineManager & operator=(const PipelineManager &)=delete
bool contains(const PipelineKey &key) const
Definition RobotScene.h:134
void clear()
Definition RobotScene.h:154
GraphicsPipeline * get(const PipelineKey &key)
Definition RobotScene.h:142
PipelineManager(const PipelineManager &)=delete
void set(const PipelineKey &key, GraphicsPipeline &&pipeline)
Definition RobotScene.h:150
void clearEnvironment()
Destroy all entities with the EnvironmentTag component.
const entt::registry & registry() const
Definition RobotScene.h:276
void update()
Update the transform component of the GeometryObject entities.
RobotScene(const RobotScene &)=delete
static constexpr SDL_GPUPrimitiveType getPrimitiveTopologyForType(PipelineType type)
Map pipeline type to geometry primitive.
Definition RobotScene.h:79
const pin::GeometryModel & geomModel() const
Getter for the pinocchio GeometryModel object.
Definition RobotScene.h:269
bool shadowsEnabled() const
Definition RobotScene.h:260
GraphicsPipeline createRenderPipeline(const PipelineKey &key, const MeshLayout &layout, SDL_GPUTextureFormat render_target_format, SDL_GPUTextureFormat depth_stencil_format)
entt::integral_constant< T > pipeline_tag
Definition RobotScene.h:90
FragmentSamplerSlots
Definition RobotScene.h:72
@ SSAO_SLOT
Definition RobotScene.h:72
@ SHADOW_MAP_SLOT
Definition RobotScene.h:72
void clearRobotGeometries()
Destroy all entities with the PinGeomObjComponent component (Pinocchio geometry objects).
const pin::GeometryData & geomData() const
Getter for the pinocchio GeometryData object.
Definition RobotScene.h:272
void setConfig(const Config &config)
Definition RobotScene.h:204
ssao::SsaoPass ssaoPass
Definition RobotScene.h:158
const std::vector< OpaqueCastable > & castables() const
Definition RobotScene.h:221
const Device & device()
Definition RobotScene.h:274
void renderOpaque(CommandBuffer &command_buffer, const Camera &camera)
VertexUniformSlots
Definition RobotScene.h:65
@ LIGHT_MATRICES
Definition RobotScene.h:65
@ TRANSFORM
Definition RobotScene.h:65
void renderTransparent(CommandBuffer &command_buffer, const Camera &camera)
std::array< DirectionalLight, kNumLights > directionalLight
Definition RobotScene.h:157
entt::registry & registry()
Definition RobotScene.h:275
ShadowMapPass shadowPass
Definition RobotScene.h:190
RobotScene(entt::registry &registry, const RenderContext &renderer)
Non-initializing constructor.
entt::entity addEnvironmentObject(MeshData &&data, Mat4f placement, PipelineType pipe_type=PIPELINE_TRIANGLEMESH)
struct candlewick::multibody::RobotScene::GBuffer gBuffer
void loadModels(const pin::GeometryModel &geom_model, const pin::GeometryData &geom_data)
Set the internal geometry model and data pointers, and load the corresponding models.
RobotScene(entt::registry &registry, const RenderContext &renderer, const pin::GeometryModel &geom_model, const pin::GeometryData &geom_data, Config config)
Constructor which initializes the system.
void ensurePipelinesExist(const std::set< pipeline_req_t > &required_pipelines)
Ensure the render pipelines were properly created following the provided requirements.
entt::entity addEnvironmentObject(MeshData &&data, const Eigen::Affine3f &T, PipelineType pipe_type=PIPELINE_TRIANGLEMESH)
Definition RobotScene.h:230
FragmentUniformSlots
Definition RobotScene.h:66
@ MATERIAL
Definition RobotScene.h:67
@ SSAO_FLAG
Definition RobotScene.h:69
@ LIGHTING
Definition RobotScene.h:68
@ ATLAS_INFO
Definition RobotScene.h:70
Uint32 numLights() const noexcept
Definition RobotScene.h:223
void render(CommandBuffer &command_buffer, const Camera &camera)
Config & config()
Definition RobotScene.h:257
void release()
Release all resources.
const Config & config() const
Definition RobotScene.h:258
std::tuple< MeshLayout, PipelineKey > pipeline_req_t
Definition RobotScene.h:262
PipelineType
Definition RobotScene.h:59
@ PIPELINE_HEIGHTFIELD
Definition RobotScene.h:61
@ PIPELINE_POINTCLOUD
Definition RobotScene.h:62
@ PIPELINE_TRIANGLEMESH
Definition RobotScene.h:60
bool pbrHasPrepass() const
Definition RobotScene.h:259
static PipelineType pinGeomToPipeline(const coal::CollisionGeometry &geom)
Map hpp-fcl/coal collision geometry to desired pipeline type.
Support for the Pinocchio rigid-body algorithms library and the Coal collision detection library.
Definition LoadPinocchioGeometry.h:8
void updateRobotTransforms(entt::registry &registry, const pin::GeometryModel &geom_model, const pin::GeometryData &geom_data)
A system for updating the transform components for robot geometry entities.
Definition Camera.h:8
RenderMode
Definition Components.h:24
terminate_with_message(std::string_view, Ts &&...) -> terminate_with_message< Ts... >
Eigen::Matrix4f Mat4f
Definition math_types.h:11
constexpr NoInitT NoInit
Definition Tags.h:9
The main way of using a camera to render things.
Definition Camera.h:19
RAII wrapper for SDL_GPUDevice.
Definition Device.h:17
The RenderContext class provides a rendering context for a graphical application.
Definition RenderContext.h:36
Definition DepthAndShadowPass.h:110
PipelineConfig transparent
Definition RobotScene.h:104
ShadowPassConfig shadow_config
Definition RobotScene.h:118
struct candlewick::multibody::RobotScene::Config::TrianglePipelineConfig triangle_config
PipelineConfig pointcloud_config
Definition RobotScene.h:114
bool enable_shadows
Definition RobotScene.h:115
bool enable_ssao
Definition RobotScene.h:116
PipelineConfig heightfield_config
Definition RobotScene.h:110
bool triangle_has_prepass
Definition RobotScene.h:117
Texture accumTexture
Definition RobotScene.h:164
void release() noexcept
Definition RobotScene.h:175
bool initialized() const
Definition RobotScene.h:170
Texture revealTexture
Definition RobotScene.h:165
Texture resolveNormalMap
Definition RobotScene.h:161
~GBuffer() noexcept
Definition RobotScene.h:188
SDL_GPUSampler * sampler
Definition RobotScene.h:168
Texture resolveRevealTexture
Definition RobotScene.h:167
Texture resolveAccumTexture
Definition RobotScene.h:166
Texture normalMap
Definition RobotScene.h:160
const char * fragment_shader_path
Definition RobotScene.h:95
SDL_GPUCullMode cull_mode
Definition RobotScene.h:96
const char * vertex_shader_path
Definition RobotScene.h:94
RenderMode renderMode
Definition RobotScene.h:124
PipelineType type
Definition RobotScene.h:122
auto operator<=>(const PipelineKey &) const =default
bool transparent
Definition RobotScene.h:123
Definition SSAO.h:9