Mandrill 2025.6.0
Loading...
Searching...
No Matches
Mandrill::Scene Class Reference

Scene class that manages a collection of nodes, materials, meshes, and rendering operations. More...

#include <Scene.h>

Inherits std::enable_shared_from_this< Scene >.

Public Member Functions

MANDRILL_API Scene (ptr< Device > pDevice, ptr< Swapchain > pSwapchain, bool supportRayTracing=false)
 Create a new scene.
 
MANDRILL_API ~Scene ()
 Destructor for scene.
 
MANDRILL_API void render (VkCommandBuffer cmd, const ptr< Camera > pCamera) const
 Render all the nodes in the scene.
 
MANDRILL_API ptr< NodeaddNode ()
 Add a node to the scene.
 
MANDRILL_API uint32_t addMaterial (Material material)
 Add a material to the scene.
 
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.
 
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.
 
MANDRILL_API void compile ()
 Calculate sizes of buffers and allocate resources. Call this after all nodes have been added.
 
MANDRILL_API void syncToDevice ()
 Synchronize buffers to device.
 
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. First time called, it will build a new acceleration structure. Subsequent calls will update the TLAS to account for updates in the instance transforms.
 
MANDRILL_API void bindRayTracingDescriptors (VkCommandBuffer cmd, ptr< Camera > pCamera, VkPipelineLayout layout)
 Bind descriptors for ray tracing.
 
MANDRILL_API void setSampler (const ptr< Sampler > pSampler)
 Set sampler to use for rendering materials.
 
MANDRILL_API ptr< LayoutgetLayout ()
 Get the layout used by the scene (as described in the comments for the Scene() constructor above).
 
MANDRILL_API std::vector< Node > & getNodes ()
 Get the list of all nodes in the scene.
 
MANDRILL_API uint32_t getVertexCount () const
 Get the number of vertices in the scene.
 
MANDRILL_API uint32_t getIndexCount () const
 Get the number of indices in the scene.
 
MANDRILL_API uint32_t getMeshCount ()
 Get the number of meshes in the scene.
 
MANDRILL_API uint32_t getMaterialCount () const
 Get the number of materials in the scene.
 
MANDRILL_API uint32_t getTextureCount () const
 Get the number of textures in the scene.
 
MANDRILL_API uint32_t getMeshVertexCount (uint32_t meshIndex) const
 Get the number of vertices in a mesh.
 
MANDRILL_API uint32_t getMeshIndexCount (uint32_t meshIndex) const
 Get the number of indices in a mesh.
 
MANDRILL_API VkDeviceAddress getMeshVertexAddress (uint32_t meshIndex) const
 Get the address of a mesh's vertices buffer.
 
MANDRILL_API VkDeviceAddress getMeshIndexAddress (uint32_t meshIndex) const
 Get the address of a mesh's indices buffer.
 
MANDRILL_API uint32_t getMeshMaterialIndex (uint32_t meshIndex) const
 Get the matieral index of a mesh.
 
MANDRILL_API ptr< AccelerationStructuregetAccelerationStructure () const
 Get the acceleration structure of the scene.
 
MANDRILL_API void setEnvironmentMap (ptr< Texture > pTexture)
 Set an environment map for the scene.
 

Detailed Description

Scene class that manages a collection of nodes, materials, meshes, and rendering operations.

Constructor & Destructor Documentation

◆ Scene()

Scene::Scene ( ptr< Device pDevice,
ptr< Swapchain pSwapchain,
bool  supportRayTracing = false 
)

Create a new scene.

The scene will use descriptor sets as follows:

Descriptor layout
Usage Set Binding Type
Camera matrix (struct CameraMatrices) 0 0 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
Model matrix (mat4) 1 0 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC
Material params (struct MaterialParams) 2 0 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
Material diffuse texture 2 1 VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
Material specular texture 2 2 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
Material ambient texture 2 3 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
Material emission texture 2 4 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
Material normal texture 2 5 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
Acceleration structure 3 0 VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR
Storage output image 3 1 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
Scene vertex buffer 3 2 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
Scene index buffer 3 3 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
Scene material buffer 3 4 VK_DESCRIPTOR_TYPE_STORAGE_BUFFER
Scene texture array 3 5 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
Output storage image 4 0 VK_DESCRIPTOR_TYPE_STORAGE_IMAGE

NB: Set 3 and 4 are only available when using ray tracing

Parameters
pDeviceDevice to use
pSwapchainSwapchain is used to determine how many descriptors will be used
supportRayTracingPrepare scene for handling acceleration sturctures
Returns

◆ ~Scene()

Scene::~Scene ( )

Destructor for scene.

Returns

Member Function Documentation

◆ addMaterial()

uint32_t Scene::addMaterial ( Material  material)

Add a material to the scene.

Parameters
materialMaterial struct defining the new material
Returns
Material index that can be used to create new meshes

◆ addMesh()

uint32_t Scene::addMesh ( const std::vector< Vertex >  vertices,
const std::vector< uint32_t >  indices,
uint32_t  materialIndex 
)

Add a mesh to the scene.

Parameters
verticesList of vertices that make up the mesh
indicesList of indices that describes how the vertices are connected
materialIndexWhich material should be used for the mesh
Returns
Mesh index that can be added to a node in the scene

◆ addMeshFromFile()

std::vector< uint32_t > Scene::addMeshFromFile ( const std::filesystem::path &  path,
const std::filesystem::path &  materialPath = "" 
)

Add several meshes to a scene by reading them from an OBJ-file.

Parameters
pathPath to the OBJ-file
materialPathPath to where the material files are stored (leave to default if the materials are in the same directory as the OBJ-file)
Returns
List of mesh indices that can be added to a node in the scene

◆ addNode()

ptr< Node > Scene::addNode ( )

Add a node to the scene.

Returns
A ptr to the created node

◆ bindRayTracingDescriptors()

void Scene::bindRayTracingDescriptors ( VkCommandBuffer  cmd,
ptr< Camera pCamera,
VkPipelineLayout  layout 
)

Bind descriptors for ray tracing.

This will bind the descriptor sets 0 (camera matrices) and 3 (acceleration structure and scene buffers). Descriptor set 4 should contain the storage image to render the ray-traced image to, and has to be bound elsewhere.

Returns

◆ compile()

void Scene::compile ( )

Calculate sizes of buffers and allocate resources. Call this after all nodes have been added.

Returns

◆ getAccelerationStructure()

MANDRILL_API ptr< AccelerationStructure > Mandrill::Scene::getAccelerationStructure ( ) const
inline

Get the acceleration structure of the scene.

Returns
Pointer to acceleration structure or nullptr

◆ getIndexCount()

MANDRILL_API uint32_t Mandrill::Scene::getIndexCount ( ) const
inline

Get the number of indices in the scene.

Returns
Number of indices

◆ getLayout()

ptr< Layout > Scene::getLayout ( )

Get the layout used by the scene (as described in the comments for the Scene() constructor above).

The layout is as follows:

Set.Binding: Descriptor type [usage]

0.0: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC [Camera matrices]
1.0: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC [Model matrix]
2.0: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER [Material parameters]
2.1: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER [Material diffuse texture]
2.2: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER [Material specular texture]
2.3: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER [Material ambient texture]
2.4: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER [Material emission texture]
2.5: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER [Material normal texture]

If ray-tracing support is requested, the following is also available:

3.0: VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR [Acceleration structure]
3.1: VK_DESCRIPTOR_TYPE_STORAGE_BUFFER [Scene vertex buffer]
3.2: VK_DESCRIPTOR_TYPE_STORAGE_BUFFER [Scene index buffer]
3.3: VK_DESCRIPTOR_TYPE_STORAGE_BUFFER [Scene material buffer]
3.4: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER (array) [Scene texture array]
3.5: VK_DESCRIPTOR_TYPE_STORAGE_BUFFER [Instance data buffer]
4.0: VK_DESCRIPTOR_TYPE_STORAGE_IMAGE [Output storage image]

Returns
Pointer to layout

◆ getMaterialCount()

MANDRILL_API uint32_t Mandrill::Scene::getMaterialCount ( ) const
inline

Get the number of materials in the scene.

Returns
Number of materials

◆ getMeshCount()

MANDRILL_API uint32_t Mandrill::Scene::getMeshCount ( )
inline

Get the number of meshes in the scene.

Returns
Number of meshes

◆ getMeshIndexAddress()

MANDRILL_API VkDeviceAddress Mandrill::Scene::getMeshIndexAddress ( uint32_t  meshIndex) const
inline

Get the address of a mesh's indices buffer.

Parameters
meshIndexIndex of the mesh to look up
Returns
Device address

◆ getMeshIndexCount()

MANDRILL_API uint32_t Mandrill::Scene::getMeshIndexCount ( uint32_t  meshIndex) const
inline

Get the number of indices in a mesh.

Parameters
meshIndexIndex of mesh to get the vertices from
Returns
Number of indices

◆ getMeshMaterialIndex()

MANDRILL_API uint32_t Mandrill::Scene::getMeshMaterialIndex ( uint32_t  meshIndex) const
inline

Get the matieral index of a mesh.

Parameters
meshIndexIndex of the mesh to look up
Returns

◆ getMeshVertexAddress()

MANDRILL_API VkDeviceAddress Mandrill::Scene::getMeshVertexAddress ( uint32_t  meshIndex) const
inline

Get the address of a mesh's vertices buffer.

Parameters
meshIndexIndex of mesh to look up
Returns
Device address

◆ getMeshVertexCount()

MANDRILL_API uint32_t Mandrill::Scene::getMeshVertexCount ( uint32_t  meshIndex) const
inline

Get the number of vertices in a mesh.

Parameters
meshIndexIndex of mesh to get the vertices from
Returns
Number of vertices

◆ getNodes()

MANDRILL_API std::vector< Node > & Mandrill::Scene::getNodes ( )
inline

Get the list of all nodes in the scene.

Returns
Vector of nodes

◆ getTextureCount()

MANDRILL_API uint32_t Mandrill::Scene::getTextureCount ( ) const
inline

Get the number of textures in the scene.

Returns
Number of textures

◆ getVertexCount()

MANDRILL_API uint32_t Mandrill::Scene::getVertexCount ( ) const
inline

Get the number of vertices in the scene.

Returns
Number of vertices

◆ render()

void Scene::render ( VkCommandBuffer  cmd,
const ptr< Camera pCamera 
) const

Render all the nodes in the scene.

Parameters
cmdCommand buffer to use for rendering
pCameraCamera that defines which camera matrices to use
Returns

◆ setEnvironmentMap()

MANDRILL_API void Mandrill::Scene::setEnvironmentMap ( ptr< Texture pTexture)
inline

Set an environment map for the scene.

Parameters
pTextureTexture to use as environment map
Returns

◆ setSampler()

void Scene::setSampler ( const ptr< Sampler pSampler)

Set sampler to use for rendering materials.

Parameters
pSamplerSampler to be used for all materials
Returns

◆ syncToDevice()

void Scene::syncToDevice ( )

Synchronize buffers to device.

Upload all buffers from host to device. This function should be called after updates have been done to the scene.

Node transforms and material parameters are kept host coherent and can be changed without requiring a new sync.

Returns

◆ updateAccelerationStructure()

void Scene::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. First time called, it will build a new acceleration structure. Subsequent calls will update the TLAS to account for updates in the instance transforms.

Parameters
flagsFlags used for building acceleration structure
Returns

The documentation for this class was generated from the following files: