5#include "AccelerationStructure.h"
17 alignas(16) glm::vec3 position;
18 alignas(16) glm::vec3 normal;
19 alignas(16) glm::vec2 texcoord;
20 alignas(16) glm::vec3 tangent;
21 alignas(16) glm::vec3 binormal;
28 bool operator==(
const Vertex& other)
const
30 return position == other.position && normal == other.normal && texcoord == other.texcoord &&
31 tangent == other.tangent && binormal == other.binormal;
36 std::vector<Vertex> vertices;
37 std::vector<uint32_t> indices;
38 uint32_t materialIndex{};
41 VkDeviceSize deviceVerticesOffset{};
42 VkDeviceSize deviceIndicesOffset{};
45 struct alignas(16) MaterialParams {
49 float indexOfRefraction;
56 struct alignas(16) MaterialDevice {
57 MaterialParams params;
58 uint32_t diffuseTextureIndex;
59 uint32_t specularTextureIndex;
60 uint32_t ambientTextureIndex;
61 uint32_t emissionTextureIndex;
62 uint32_t normalTextureIndex;
66 MaterialParams params{};
67 MaterialParams* paramsDevice{};
68 VkDeviceSize paramsOffset{};
70 std::string diffuseTexturePath;
71 std::string specularTexturePath;
72 std::string ambientTexturePath;
73 std::string emissionTexturePath;
74 std::string normalTexturePath;
76 ptr<Descriptor> pDescriptor;
80 uint32_t verticesOffset;
81 uint32_t indicesOffset;
103 MANDRILL_API
~Node();
112 MANDRILL_API
void render(VkCommandBuffer cmd,
const ptr<Camera> pCamera,
const ptr<const Scene> pScene)
const;
121 mMeshIndices.push_back(meshIndex);
131 mpPipeline = pPipeline;
150 mTransform = transform;
184 ptr<Pipeline> mpPipeline;
186 std::vector<uint32_t> mMeshIndices;
188 glm::mat4 mTransform;
189 glm::mat4* mpTransformDevice;
190 ptr<Descriptor> pDescriptor;
191 std::vector<VkDescriptorSet> mDescriptors;
195 std::vector<Node*> mChildren;
201 class Scene :
public std::enable_shared_from_this<Scene>
204 MANDRILL_NON_COPYABLE(
Scene)
237 MANDRILL_API
Scene(ptr<Device> pDevice, ptr<Swapchain> pSwapchain,
bool supportRayTracing =
false);
251 MANDRILL_API
void render(VkCommandBuffer cmd,
const ptr<Camera> pCamera)
const;
257 MANDRILL_API ptr<Node>
addNode();
264 MANDRILL_API uint32_t
addMaterial(Material material);
273 MANDRILL_API uint32_t
addMesh(
const std::vector<Vertex> vertices,
const std::vector<uint32_t> indices,
274 uint32_t materialIndex);
283 MANDRILL_API std::vector<uint32_t>
addMeshFromFile(
const std::filesystem::path& path,
284 const std::filesystem::path& materialPath =
"");
313 VkBuildAccelerationStructureFlagsKHR flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR);
331 MANDRILL_API
void setSampler(
const ptr<Sampler> pSampler);
396 return count(mMeshes);
405 return count(mMaterials);
414 return count(mTextures);
424 return count(mMeshes[meshIndex].vertices);
434 return count(mMeshes[meshIndex].indices);
444 return mpVertexBuffer->getDeviceAddress() + mMeshes[meshIndex].deviceVerticesOffset;
454 return mpIndexBuffer->getDeviceAddress() + mMeshes[meshIndex].deviceIndicesOffset;
464 return mMeshes[meshIndex].materialIndex;
473 return mpAccelerationStructure;
483 mpEnvironmentMap = pTexture;
489 std::vector<uint32_t> loadFromOBJ(
const std::filesystem::path& path,
490 const std::filesystem::path& materialPath =
"");
491 std::vector<uint32_t> loadFromGLTF(
const std::filesystem::path& path,
492 const std::filesystem::path& materialPath =
"");
493 void addTexture(std::string texturePath);
494 void createDescriptors();
496 ptr<Device> mpDevice;
497 ptr<Swapchain> mpSwapchain;
499 std::vector<Mesh> mMeshes;
500 std::vector<Node> mNodes;
501 std::vector<Material> mMaterials;
502 std::unordered_map<std::string, ptr<Texture>> mTextures;
503 ptr<Texture> mpEnvironmentMap;
504 ptr<Descriptor> mpEnvironmentMapDescriptor;
506 ptr<Buffer> mpVertexBuffer;
507 ptr<Buffer> mpIndexBuffer;
508 ptr<Buffer> mpTransforms;
509 ptr<Buffer> mpMaterialParams;
511 ptr<Texture> mpMissingTexture;
513 bool mSupportRayTracing;
514 ptr<AccelerationStructure> mpAccelerationStructure;
515 ptr<Descriptor> mpRayTracingDescriptor;
516 ptr<Buffer> mpMaterialBuffer;
517 ptr<Buffer> mpInstanceDataBuffer;
519 uint32_t mVertexCount;
520 uint32_t mIndexCount;
Scene node class for managing a single node in a scene graph. This class can hold meshes and transfor...
Definition Scene.h:91
MANDRILL_API void addMesh(uint32_t meshIndex)
Add a mesh to the node.
Definition Scene.h:119
MANDRILL_API void render(VkCommandBuffer cmd, const ptr< Camera > pCamera, const ptr< const Scene > pScene) const
Render a node in the scene.
Definition Scene.cpp:32
MANDRILL_API glm::mat4 getTransform() const
Get the TRS transform of the node.
Definition Scene.h:138
MANDRILL_API void setTransform(glm::mat4 transform)
Set the TRS transform of the node.
Definition Scene.h:148
MANDRILL_API void setVisible(bool visible)
Set weather the node should be rendered or not.
Definition Scene.h:158
MANDRILL_API void setPipeline(ptr< Pipeline > pPipeline)
Set pipeline to use when rendering node.
Definition Scene.h:129
MANDRILL_API ~Node()
Destructor for scene node.
Definition Scene.cpp:28
MANDRILL_API Node()
Create a new scene node.
Definition Scene.cpp:21
MANDRILL_API bool getVisible() const
Get the visibility of the node.
Definition Scene.h:167
MANDRILL_API std::vector< uint32_t > & getMeshIndices()
Get the mesh indices.
Definition Scene.h:176
Scene class that manages a collection of nodes, materials, meshes, and rendering operations.
Definition Scene.h:202
MANDRILL_API ptr< AccelerationStructure > getAccelerationStructure() const
Get the acceleration structure of the scene.
Definition Scene.h:471
MANDRILL_API VkDeviceAddress getMeshIndexAddress(uint32_t meshIndex) const
Get the address of a mesh's indices buffer.
Definition Scene.h:452
MANDRILL_API void updateAccelerationStructure(VkBuildAccelerationStructureFlagsKHR flags=VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR)
Build and update acceleration structure of the scene. This is needed for ray tracing the scene....
Definition Scene.cpp:328
MANDRILL_API std::vector< uint32_t > addMeshFromFile(const std::filesystem::path &path, const std::filesystem::path &materialPath="")
Add several meshes to a scene by reading them from an OBJ-file.
Definition Scene.cpp:166
MANDRILL_API ~Scene()
Destructor for scene.
Definition Scene.cpp:84
MANDRILL_API uint32_t getTextureCount() const
Get the number of textures in the scene.
Definition Scene.h:412
MANDRILL_API void setEnvironmentMap(ptr< Texture > pTexture)
Set an environment map for the scene.
Definition Scene.h:481
MANDRILL_API std::vector< Node > & getNodes()
Get the list of all nodes in the scene.
Definition Scene.h:367
MANDRILL_API void compile()
Calculate sizes of buffers and allocate resources. Call this after all nodes have been added.
Definition Scene.cpp:191
MANDRILL_API uint32_t getMeshMaterialIndex(uint32_t meshIndex) const
Get the matieral index of a mesh.
Definition Scene.h:462
MANDRILL_API uint32_t getIndexCount() const
Get the number of indices in the scene.
Definition Scene.h:385
MANDRILL_API void setSampler(const ptr< Sampler > pSampler)
Set sampler to use for rendering materials.
Definition Scene.cpp:363
MANDRILL_API uint32_t getMeshVertexCount(uint32_t meshIndex) const
Get the number of vertices in a mesh.
Definition Scene.h:422
MANDRILL_API void bindRayTracingDescriptors(VkCommandBuffer cmd, ptr< Camera > pCamera, VkPipelineLayout layout)
Bind descriptors for ray tracing.
Definition Scene.cpp:349
MANDRILL_API ptr< Node > addNode()
Add a node to the scene.
Definition Scene.cpp:95
MANDRILL_API ptr< Layout > getLayout()
Get the layout used by the scene (as described in the comments for the Scene() constructor above).
Definition Scene.cpp:372
MANDRILL_API void syncToDevice()
Synchronize buffers to device.
Definition Scene.cpp:299
MANDRILL_API uint32_t addMaterial(Material material)
Add a material to the scene.
Definition Scene.cpp:104
MANDRILL_API uint32_t addMesh(const std::vector< Vertex > vertices, const std::vector< uint32_t > indices, uint32_t materialIndex)
Add a mesh to the scene.
Definition Scene.cpp:128
MANDRILL_API uint32_t getMeshIndexCount(uint32_t meshIndex) const
Get the number of indices in a mesh.
Definition Scene.h:432
MANDRILL_API void render(VkCommandBuffer cmd, const ptr< Camera > pCamera) const
Render all the nodes in the scene.
Definition Scene.cpp:88
MANDRILL_API uint32_t getMaterialCount() const
Get the number of materials in the scene.
Definition Scene.h:403
MANDRILL_API uint32_t getMeshCount()
Get the number of meshes in the scene.
Definition Scene.h:394
MANDRILL_API uint32_t getVertexCount() const
Get the number of vertices in the scene.
Definition Scene.h:376
MANDRILL_API VkDeviceAddress getMeshVertexAddress(uint32_t meshIndex) const
Get the address of a mesh's vertices buffer.
Definition Scene.h:442