candlewick 0.7.0-59-g23c6
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"
9#include "../core/Scene.h"
13#include "../core/Texture.h"
14#include "../posteffects/SSAO.h"
15#include "../utils/MeshData.h"
16
17#include <magic_enum/magic_enum.hpp>
18#include <entt/entity/fwd.hpp>
19#include <coal/fwd.hh>
20#include <pinocchio/multibody/fwd.hpp>
21#include <set>
22
23namespace candlewick {
24
26template <typename T>
27 requires std::is_enum_v<T>
28[[noreturn]] void
29invalid_enum(const char *msg, T type,
30 std::source_location location = std::source_location::current()) {
31 terminate_with_message(location, "Invalid enum: {:s} - {:s}", msg,
32 magic_enum::enum_name(type));
33}
34
35namespace multibody {
36
43 void updateRobotTransforms(entt::registry &registry,
44 const pin::GeometryModel &geom_model,
45 const pin::GeometryData &geom_data);
46
51 class RobotScene final {
52 [[nodiscard]] bool hasInternalPointers() const {
53 return m_geomModel && m_geomData;
54 }
55
56 void compositeTransparencyPass(CommandBuffer &command_buffer);
57
58 void renderPBRTriangleGeometry(CommandBuffer &command_buffer,
59 const Camera &camera, bool transparent);
60
61 void renderOtherGeometry(CommandBuffer &command_buffer,
62 const Camera &camera);
63
64 void initGBuffer();
65
66 void initCompositePipeline(const MeshLayout &layout);
67
68 public:
74 static constexpr size_t kNumPipelineTypes =
75 magic_enum::enum_count<PipelineType>();
84
86 static PipelineType pinGeomToPipeline(const coal::CollisionGeometry &geom);
87
89 static constexpr SDL_GPUPrimitiveType
91 switch (type) {
93 return SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
95 return SDL_GPU_PRIMITIVETYPE_LINELIST;
97 return SDL_GPU_PRIMITIVETYPE_POINTLIST;
98 }
99 }
100
101 template <PipelineType t> using pipeline_tag = entt::tag<t>;
102
104 // shader set
107 SDL_GPUCullMode cull_mode = SDL_GPU_CULLMODE_BACK;
108 SDL_GPUFillMode fill_mode = SDL_GPU_FILLMODE_FILL;
109 };
110 struct Config {
113 .vertex_shader_path = "PbrBasic.vert",
114 .fragment_shader_path = "PbrBasic.frag",
115 };
117 .vertex_shader_path = "PbrBasic.vert",
118 .fragment_shader_path = "PbrTransparent.frag",
119 .cull_mode = SDL_GPU_CULLMODE_NONE,
120 };
123 .vertex_shader_path = "Hud3dElement.vert",
124 .fragment_shader_path = "Hud3dElement.frag",
125 };
127 bool enable_msaa = false;
128 bool enable_shadows = true;
129 bool enable_ssao = true;
132 SDL_GPUSampleCount msaa_samples = SDL_GPU_SAMPLECOUNT_1;
134 };
135
136 std::array<DirectionalLight, kNumLights> directionalLight;
138 struct GBuffer {
140
141 // WBOIT buffers
144 SDL_GPUSampler *sampler = nullptr; // composite pass
145
146 bool initialized() const {
147 return (sampler != nullptr) && normalMap && accumTexture &&
149 }
152
154 RobotScene(entt::registry &registry, const RenderContext &renderer);
155
159 RobotScene(entt::registry &registry, const RenderContext &renderer,
160 const pin::GeometryModel &geom_model,
161 const pin::GeometryData &geom_data, Config config);
162
163 RobotScene(const RobotScene &) = delete;
164
165 void setConfig(const Config &config) {
167 !m_initialized,
168 "Cannot call setConfig() after render system was initialized.");
169 m_config = config;
170 }
171
174 void loadModels(const pin::GeometryModel &geom_model,
175 const pin::GeometryData &geom_data);
176
179
181 const std::vector<OpaqueCastable> &castables() const { return m_castables; }
182
183 Uint32 numLights() const noexcept { return shadowPass.numLights(); }
184
185 entt::entity
188
189 entt::entity
190 addEnvironmentObject(MeshData &&data, const Eigen::Affine3f &T,
192 return addEnvironmentObject(std::move(data), T.matrix(), pipe_type);
193 }
194
200
202 SDL_GPUTextureFormat render_target_format,
203 SDL_GPUTextureFormat depth_stencil_format,
204 PipelineType type, bool transparent);
205
208 void render(CommandBuffer &command_buffer, const Camera &camera);
209
210 void renderOpaque(CommandBuffer &command_buffer, const Camera &camera);
211
212 void renderTransparent(CommandBuffer &command_buffer, const Camera &camera);
213
215 void release();
216
217 Config &config() { return m_config; }
218 const Config &config() const { return m_config; }
219 inline bool pbrHasPrepass() const { return m_config.triangle_has_prepass; }
220 inline bool shadowsEnabled() const { return m_config.enable_shadows; }
221
225 const std::set<std::tuple<MeshLayout, PipelineType, bool>>
226 &required_pipelines);
227
229 const pin::GeometryModel &geomModel() const { return *m_geomModel; }
230
232 const pin::GeometryData &geomData() const { return *m_geomData; }
233
234 const Device &device() { return m_renderer.device; }
235 entt::registry &registry() { return m_registry; }
236 const entt::registry &registry() const { return m_registry; }
237
238 private:
239 entt::registry &m_registry;
240 const RenderContext &m_renderer;
241 Config m_config;
242 const pin::GeometryModel *m_geomModel;
243 const pin::GeometryData *m_geomData;
244 std::vector<OpaqueCastable> m_castables;
245 bool m_initialized;
246 struct {
247 SDL_GPUGraphicsPipeline *triangleMeshOpaque = nullptr;
248 SDL_GPUGraphicsPipeline *triangleMeshTransparent = nullptr;
249 SDL_GPUGraphicsPipeline *heightfield = nullptr;
250 SDL_GPUGraphicsPipeline *pointcloud = nullptr;
251 SDL_GPUGraphicsPipeline *wboitComposite = nullptr;
252 } m_pipelines;
253
254 SDL_GPUGraphicsPipeline *&routePipeline(PipelineType type,
255 bool transparent = false) {
256 switch (type) {
258 return transparent ? m_pipelines.triangleMeshTransparent
259 : m_pipelines.triangleMeshOpaque;
261 return m_pipelines.heightfield;
263 return m_pipelines.pointcloud;
264 }
265 }
266 };
267 static_assert(Scene<RobotScene>);
268
269} // namespace multibody
270} // namespace candlewick
#define CANDLEWICK_ASSERT(condition, msg)
Definition Core.h:6
Definition CommandBuffer.h:17
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:122
Definition Texture.h:9
void clearEnvironment()
Destroy all entities with the EnvironmentTag component.
const entt::registry & registry() const
Definition RobotScene.h:236
RobotScene(const RobotScene &)=delete
static constexpr SDL_GPUPrimitiveType getPrimitiveTopologyForType(PipelineType type)
Map pipeline type to geometry primitive.
Definition RobotScene.h:90
entt::tag< t > pipeline_tag
Definition RobotScene.h:101
const pin::GeometryModel & geomModel() const
Getter for the pinocchio GeometryModel object.
Definition RobotScene.h:229
bool shadowsEnabled() const
Definition RobotScene.h:220
SDL_GPUGraphicsPipeline * pointcloud
Definition RobotScene.h:250
FragmentSamplerSlots
Definition RobotScene.h:83
@ SSAO_SLOT
Definition RobotScene.h:83
@ SHADOW_MAP_SLOT
Definition RobotScene.h:83
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:232
void setConfig(const Config &config)
Definition RobotScene.h:165
ssao::SsaoPass ssaoPass
Definition RobotScene.h:137
const std::vector< OpaqueCastable > & castables() const
Definition RobotScene.h:181
const Device & device()
Definition RobotScene.h:234
void renderOpaque(CommandBuffer &command_buffer, const Camera &camera)
VertexUniformSlots
Definition RobotScene.h:76
@ LIGHT_MATRICES
Definition RobotScene.h:76
@ TRANSFORM
Definition RobotScene.h:76
void renderTransparent(CommandBuffer &command_buffer, const Camera &camera)
std::array< DirectionalLight, kNumLights > directionalLight
Definition RobotScene.h:136
entt::registry & registry()
Definition RobotScene.h:235
SDL_GPUGraphicsPipeline * triangleMeshOpaque
Definition RobotScene.h:247
static constexpr size_t kNumPipelineTypes
Definition RobotScene.h:74
ShadowMapPass shadowPass
Definition RobotScene.h:151
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
SDL_GPUGraphicsPipeline * triangleMeshTransparent
Definition RobotScene.h:248
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.
SDL_GPUGraphicsPipeline * heightfield
Definition RobotScene.h:249
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 createRenderPipeline(const MeshLayout &layout, SDL_GPUTextureFormat render_target_format, SDL_GPUTextureFormat depth_stencil_format, PipelineType type, bool transparent)
SDL_GPUGraphicsPipeline * wboitComposite
Definition RobotScene.h:251
entt::entity addEnvironmentObject(MeshData &&data, const Eigen::Affine3f &T, PipelineType pipe_type=PIPELINE_TRIANGLEMESH)
Definition RobotScene.h:190
void updateTransforms()
Update the transform component of the GeometryObject entities.
FragmentUniformSlots
Definition RobotScene.h:77
@ MATERIAL
Definition RobotScene.h:78
@ SSAO_FLAG
Definition RobotScene.h:80
@ LIGHTING
Definition RobotScene.h:79
@ ATLAS_INFO
Definition RobotScene.h:81
Uint32 numLights() const noexcept
Definition RobotScene.h:183
void render(CommandBuffer &command_buffer, const Camera &camera)
Config & config()
Definition RobotScene.h:217
void release()
Release all resources.
const Config & config() const
Definition RobotScene.h:218
void ensurePipelinesExist(const std::set< std::tuple< MeshLayout, PipelineType, bool > > &required_pipelines)
Ensure the render pipelines were properly created following the provided requirements.
PipelineType
Definition RobotScene.h:69
@ PIPELINE_HEIGHTFIELD
Definition RobotScene.h:71
@ PIPELINE_POINTCLOUD
Definition RobotScene.h:72
@ PIPELINE_TRIANGLEMESH
Definition RobotScene.h:70
bool pbrHasPrepass() const
Definition RobotScene.h:219
static PipelineType pinGeomToPipeline(const coal::CollisionGeometry &geom)
Map hpp-fcl/coal collision geometry to desired pipeline type.
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
void terminate_with_message(std::source_location location, std::string_view fmt, Ts &&...args)
Definition errors.h:51
void invalid_enum(const char *msg, T type, std::source_location location=std::source_location::current())
Terminate the application after encountering an invalid enum value.
Definition RobotScene.h:29
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:20
Definition DepthAndShadowPass.h:105
PipelineConfig transparent
Definition RobotScene.h:116
ShadowPassConfig shadow_config
Definition RobotScene.h:133
struct candlewick::multibody::RobotScene::Config::TrianglePipelineConfig triangle_config
bool enable_normal_target
Definition RobotScene.h:131
PipelineConfig pointcloud_config
Definition RobotScene.h:126
bool enable_shadows
Definition RobotScene.h:128
bool enable_ssao
Definition RobotScene.h:129
SDL_GPUSampleCount msaa_samples
Definition RobotScene.h:132
PipelineConfig heightfield_config
Definition RobotScene.h:122
bool enable_msaa
Definition RobotScene.h:127
bool triangle_has_prepass
Definition RobotScene.h:130
Texture accumTexture
Definition RobotScene.h:142
bool initialized() const
Definition RobotScene.h:146
Texture revealTexture
Definition RobotScene.h:143
SDL_GPUSampler * sampler
Definition RobotScene.h:144
Texture normalMap
Definition RobotScene.h:139
SDL_GPUFillMode fill_mode
Definition RobotScene.h:108
const char * fragment_shader_path
Definition RobotScene.h:106
SDL_GPUCullMode cull_mode
Definition RobotScene.h:107
const char * vertex_shader_path
Definition RobotScene.h:105
Definition SSAO.h:9