![]() |
Mandrill 2025.6.0
|
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< Node > | addNode () |
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< Layout > | getLayout () |
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< AccelerationStructure > | getAccelerationStructure () const |
Get the acceleration structure of the scene. | |
MANDRILL_API void | setEnvironmentMap (ptr< Texture > pTexture) |
Set an environment map for the scene. | |
Scene class that manages a collection of nodes, materials, meshes, and rendering operations.
Create a new scene.
The scene will use descriptor sets as follows:
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
pDevice | Device to use |
pSwapchain | Swapchain is used to determine how many descriptors will be used |
supportRayTracing | Prepare scene for handling acceleration sturctures |
Scene::~Scene | ( | ) |
Destructor for scene.
uint32_t Scene::addMaterial | ( | Material | material | ) |
Add a material to the scene.
material | Material struct defining the new material |
uint32_t Scene::addMesh | ( | const std::vector< Vertex > | vertices, |
const std::vector< uint32_t > | indices, | ||
uint32_t | materialIndex | ||
) |
Add a mesh to the scene.
vertices | List of vertices that make up the mesh |
indices | List of indices that describes how the vertices are connected |
materialIndex | Which material should be used for the mesh |
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.
path | Path to the OBJ-file |
materialPath | Path to where the material files are stored (leave to default if the materials are in the same directory as the OBJ-file) |
ptr< Node > Scene::addNode | ( | ) |
Add a node to the scene.
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.
void Scene::compile | ( | ) |
Calculate sizes of buffers and allocate resources. Call this after all nodes have been added.
|
inline |
Get the acceleration structure of the scene.
|
inline |
Get the number of indices in the scene.
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]
|
inline |
Get the number of materials in the scene.
|
inline |
Get the number of meshes in the scene.
|
inline |
Get the address of a mesh's indices buffer.
meshIndex | Index of the mesh to look up |
|
inline |
Get the number of indices in a mesh.
meshIndex | Index of mesh to get the vertices from |
|
inline |
Get the matieral index of a mesh.
meshIndex | Index of the mesh to look up |
|
inline |
Get the address of a mesh's vertices buffer.
meshIndex | Index of mesh to look up |
|
inline |
Get the number of vertices in a mesh.
meshIndex | Index of mesh to get the vertices from |
|
inline |
Get the list of all nodes in the scene.
|
inline |
Get the number of textures in the scene.
|
inline |
Get the number of vertices in the scene.
void Scene::render | ( | VkCommandBuffer | cmd, |
const ptr< Camera > | pCamera | ||
) | const |
Render all the nodes in the scene.
cmd | Command buffer to use for rendering |
pCamera | Camera that defines which camera matrices to use |
|
inline |
void Scene::setSampler | ( | const ptr< Sampler > | pSampler | ) |
Set sampler to use for rendering materials.
pSampler | Sampler to be used for all materials |
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.
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.
flags | Flags used for building acceleration structure |