candlewick 0.6.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#include "../core/Device.h"
8#include "../core/Scene.h"
11#include "../core/Collision.h"
13#include "../core/Texture.h"
14#include "../posteffects/SSAO.h"
15#include "../utils/MeshData.h"
16#include <magic_enum/magic_enum.hpp>
17
18#include <entt/entity/fwd.hpp>
19#include <coal/fwd.hh>
20#include <pinocchio/multibody/fwd.hpp>
21
22namespace candlewick {
23
25template <typename T>
26 requires std::is_enum_v<T>
27[[noreturn]] void
28invalid_enum(const char *msg, T type,
29 std::source_location location = std::source_location::current()) {
30 terminate_with_message(location, "Invalid enum: {:s} - {:s}", msg,
31 magic_enum::enum_name(type));
32}
33
34namespace multibody {
35
42 void updateRobotTransforms(entt::registry &registry,
43 const pin::GeometryModel &geom_model,
44 const pin::GeometryData &geom_data);
45
50 class RobotScene final {
51 [[nodiscard]] bool hasInternalPointers() const {
52 return m_geomModel && m_geomData;
53 }
54
55 void compositeTransparencyPass(CommandBuffer &command_buffer);
56
57 void renderPBRTriangleGeometry(CommandBuffer &command_buffer,
58 const Camera &camera, bool transparent);
59
60 void renderOtherGeometry(CommandBuffer &command_buffer,
61 const Camera &camera);
62
63 void initGBuffer();
64
65 void initCompositePipeline(const MeshLayout &layout);
66
67 public:
73 static constexpr size_t kNumPipelineTypes =
74 magic_enum::enum_count<PipelineType>();
83
85 static PipelineType pinGeomToPipeline(const coal::CollisionGeometry &geom);
86
88 static constexpr SDL_GPUPrimitiveType
90 switch (type) {
92 return SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
94 return SDL_GPU_PRIMITIVETYPE_LINELIST;
96 return SDL_GPU_PRIMITIVETYPE_POINTLIST;
97 }
98 }
99
100 template <PipelineType t> using pipeline_tag = entt::tag<t>;
101
103 // shader set
106 SDL_GPUCullMode cull_mode = SDL_GPU_CULLMODE_BACK;
107 SDL_GPUFillMode fill_mode = SDL_GPU_FILLMODE_FILL;
108 };
109 struct Config {
112 .vertex_shader_path = "PbrBasic.vert",
113 .fragment_shader_path = "PbrBasic.frag",
114 };
116 .vertex_shader_path = "PbrBasic.vert",
117 .fragment_shader_path = "PbrTransparent.frag",
118 .cull_mode = SDL_GPU_CULLMODE_NONE,
119 };
122 .vertex_shader_path = "Hud3dElement.vert",
123 .fragment_shader_path = "Hud3dElement.frag",
124 };
126 bool enable_msaa = false;
127 bool enable_shadows = true;
128 bool enable_ssao = true;
131 SDL_GPUSampleCount msaa_samples = SDL_GPU_SAMPLECOUNT_1;
133 };
134
135 struct Pipelines {
136 struct {
137 SDL_GPUGraphicsPipeline *opaque = nullptr;
138 SDL_GPUGraphicsPipeline *transparent = nullptr;
140 SDL_GPUGraphicsPipeline *heightfield = nullptr;
141 SDL_GPUGraphicsPipeline *pointcloud = nullptr;
142 SDL_GPUGraphicsPipeline *wboitComposite = nullptr;
144
145 std::array<DirectionalLight, kNumLights> directionalLight;
147 struct GBuffer {
149
150 // WBOIT buffers
153 SDL_GPUSampler *sampler = nullptr; // composite pass
156
158 RobotScene(entt::registry &registry, const RenderContext &renderer);
159
163 RobotScene(entt::registry &registry, const RenderContext &renderer,
164 const pin::GeometryModel &geom_model,
165 const pin::GeometryData &geom_data, Config config);
166
167 RobotScene(const RobotScene &) = delete;
168
169 void setConfig(const Config &config) {
171 !m_initialized,
172 "Cannot call setConfig() after render system was initialized.");
173 m_config = config;
174 }
175
178 void loadModels(const pin::GeometryModel &geom_model,
179 const pin::GeometryData &geom_data);
180
183
185 const std::vector<OpaqueCastable> &castables() const { return m_castables; }
186
187 Uint32 numLights() const noexcept { return shadowPass.numLights(); }
188
189 entt::entity
192
193 entt::entity
194 addEnvironmentObject(MeshData &&data, const Eigen::Affine3f &T,
196 return addEnvironmentObject(std::move(data), T.matrix(), pipe_type);
197 }
198
204
205 void createPipeline(const MeshLayout &layout,
206 SDL_GPUTextureFormat render_target_format,
207 SDL_GPUTextureFormat depth_stencil_format,
208 PipelineType type, bool transparent);
209
212 void render(CommandBuffer &command_buffer, const Camera &camera);
213
214 void renderOpaque(CommandBuffer &command_buffer, const Camera &camera);
215
216 void renderTransparent(CommandBuffer &command_buffer, const Camera &camera);
217
219 void release();
220
221 Config &config() { return m_config; }
222 const Config &config() const { return m_config; }
223 inline bool pbrHasPrepass() const { return m_config.triangle_has_prepass; }
224 inline bool shadowsEnabled() const { return m_config.enable_shadows; }
225
227 const pin::GeometryModel &geomModel() const { return *m_geomModel; }
228
230 const pin::GeometryData &geomData() const { return *m_geomData; }
231
232 const entt::registry &registry() const { return m_registry; }
233
234 const Device &device() { return m_renderer.device; }
235
236 SDL_GPUGraphicsPipeline *getPipeline(PipelineType type,
237 bool transparent = false) {
238 return *routePipeline(type, transparent);
239 }
240
241 private:
242 entt::registry &m_registry;
243 const RenderContext &m_renderer;
244 Config m_config;
245 const pin::GeometryModel *m_geomModel;
246 const pin::GeometryData *m_geomData;
247 std::vector<OpaqueCastable> m_castables;
248 bool m_initialized;
249
250 SDL_GPUGraphicsPipeline **routePipeline(PipelineType type,
251 bool transparent) {
252 switch (type) {
254 return transparent ? &pipelines.triangleMesh.transparent
257 return &pipelines.heightfield;
259 return &pipelines.pointcloud;
260 }
261 }
262 };
263 static_assert(Scene<RobotScene>);
264
265} // namespace multibody
266} // 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:98
Class for defining the shadow maps (as an atlas) and rendering into it.
Definition DepthAndShadowPass.h:118
Definition Texture.h:9
void clearEnvironment()
Destroy all entities with the EnvironmentTag component.
const entt::registry & registry() const
Definition RobotScene.h:232
RobotScene(const RobotScene &)=delete
static constexpr SDL_GPUPrimitiveType getPrimitiveTopologyForType(PipelineType type)
Map pipeline type to geometry primitive.
Definition RobotScene.h:89
entt::tag< t > pipeline_tag
Definition RobotScene.h:100
const pin::GeometryModel & geomModel() const
Getter for the pinocchio GeometryModel object.
Definition RobotScene.h:227
bool shadowsEnabled() const
Definition RobotScene.h:224
FragmentSamplerSlots
Definition RobotScene.h:82
@ SSAO_SLOT
Definition RobotScene.h:82
@ SHADOW_MAP_SLOT
Definition RobotScene.h:82
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:230
void setConfig(const Config &config)
Definition RobotScene.h:169
ssao::SsaoPass ssaoPass
Definition RobotScene.h:146
const std::vector< OpaqueCastable > & castables() const
Definition RobotScene.h:185
const Device & device()
Definition RobotScene.h:234
void renderOpaque(CommandBuffer &command_buffer, const Camera &camera)
VertexUniformSlots
Definition RobotScene.h:75
@ LIGHT_MATRICES
Definition RobotScene.h:75
@ TRANSFORM
Definition RobotScene.h:75
void renderTransparent(CommandBuffer &command_buffer, const Camera &camera)
std::array< DirectionalLight, kNumLights > directionalLight
Definition RobotScene.h:145
void createPipeline(const MeshLayout &layout, SDL_GPUTextureFormat render_target_format, SDL_GPUTextureFormat depth_stencil_format, PipelineType type, bool transparent)
static constexpr size_t kNumPipelineTypes
Definition RobotScene.h:73
ShadowMapPass shadowPass
Definition RobotScene.h:155
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.
entt::entity addEnvironmentObject(MeshData &&data, const Eigen::Affine3f &T, PipelineType pipe_type=PIPELINE_TRIANGLEMESH)
Definition RobotScene.h:194
void updateTransforms()
Update the transform component of the GeometryObject entities.
FragmentUniformSlots
Definition RobotScene.h:76
@ MATERIAL
Definition RobotScene.h:77
@ SSAO_FLAG
Definition RobotScene.h:79
@ LIGHTING
Definition RobotScene.h:78
@ ATLAS_INFO
Definition RobotScene.h:80
Uint32 numLights() const noexcept
Definition RobotScene.h:187
void render(CommandBuffer &command_buffer, const Camera &camera)
Config & config()
Definition RobotScene.h:221
void release()
Release all resources.
const Config & config() const
Definition RobotScene.h:222
SDL_GPUGraphicsPipeline * getPipeline(PipelineType type, bool transparent=false)
Definition RobotScene.h:236
PipelineType
Definition RobotScene.h:68
@ PIPELINE_HEIGHTFIELD
Definition RobotScene.h:70
@ PIPELINE_POINTCLOUD
Definition RobotScene.h:71
@ PIPELINE_TRIANGLEMESH
Definition RobotScene.h:69
bool pbrHasPrepass() const
Definition RobotScene.h:223
static PipelineType pinGeomToPipeline(const coal::CollisionGeometry &geom)
Map hpp-fcl/coal collision geometry to desired pipeline type.
struct candlewick::multibody::RobotScene::Pipelines pipelines
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:28
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:101
PipelineConfig transparent
Definition RobotScene.h:115
ShadowPassConfig shadow_config
Definition RobotScene.h:132
struct candlewick::multibody::RobotScene::Config::TrianglePipelineConfig triangle_config
bool enable_normal_target
Definition RobotScene.h:130
PipelineConfig pointcloud_config
Definition RobotScene.h:125
bool enable_shadows
Definition RobotScene.h:127
bool enable_ssao
Definition RobotScene.h:128
SDL_GPUSampleCount msaa_samples
Definition RobotScene.h:131
PipelineConfig heightfield_config
Definition RobotScene.h:121
bool enable_msaa
Definition RobotScene.h:126
bool triangle_has_prepass
Definition RobotScene.h:129
Texture accumTexture
Definition RobotScene.h:151
Texture revealTexture
Definition RobotScene.h:152
SDL_GPUSampler * sampler
Definition RobotScene.h:153
Texture normalMap
Definition RobotScene.h:148
SDL_GPUFillMode fill_mode
Definition RobotScene.h:107
const char * fragment_shader_path
Definition RobotScene.h:105
SDL_GPUCullMode cull_mode
Definition RobotScene.h:106
const char * vertex_shader_path
Definition RobotScene.h:104
SDL_GPUGraphicsPipeline * transparent
Definition RobotScene.h:138
SDL_GPUGraphicsPipeline * opaque
Definition RobotScene.h:137
SDL_GPUGraphicsPipeline * heightfield
Definition RobotScene.h:140
SDL_GPUGraphicsPipeline * pointcloud
Definition RobotScene.h:141
struct candlewick::multibody::RobotScene::Pipelines::@173370065110064371102147354360310021261035041233 triangleMesh
SDL_GPUGraphicsPipeline * wboitComposite
Definition RobotScene.h:142
Definition SSAO.h:9