libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::VKAbstractModel Class Reference

Convenience wrapper that owns an MXModel plus its Vulkan textures, UBOs, and descriptor sets. More...

#include <vk_abstract_model.hpp>

Collaboration diagram for mx::VKAbstractModel:
[legend]

Classes

struct  TexEntry
 Vulkan image, memory, and view for one loaded texture. More...

Public Member Functions

 VKAbstractModel ()=default
 Default constructor.
 VKAbstractModel (const VKAbstractModel &)=delete
 VKAbstractModel (VKAbstractModel &&)=delete
VKAbstractModel & operator= (const VKAbstractModel &)=delete
VKAbstractModel & operator= (VKAbstractModel &&)=delete
void load (VKWindow *win, const std::string &model, const std::string &text, const std::string &path, float scale)
 Load a model, its textures, and create all GPU resources.
void setShaders (VKWindow *win, const std::string &vert, const std::string &frag)
 Set custom vertex and fragment shaders and create a per-model pipeline.
void render (int cmd)
 Record rendering commands (currently a placeholder).
void cleanup (VKWindow *win)
 Destroy all Vulkan resources owned by this model.
void resize (VKWindow *win)
 Recreate UBOs and descriptor sets after a swapchain resize.
void updateUBO (VKWindow *win, uint32_t imageIndex, const mx::UniformBufferObject &ubo)
 Copy a UBO into the mapped buffer for a given frame.
VkPipeline getModelPipeline () const
 Get the per-model pipeline, or VK_NULL_HANDLE if using the window default.
VkPipeline getModelPipelineWireframe () const
 Get the per-model wireframe pipeline, or VK_NULL_HANDLE if not available.
void loadInstance (VKWindow *win, VKAbstractModel &source)
 Create a lightweight instance sharing geometry & textures from a source model.
bool hasCustomShaders () const
 Check whether this model has custom shaders set.
bool isInstance () const
 Check whether this is a lightweight instance (shared geometry).

Public Attributes

mx::MXModel obj
 Underlying mesh data and GPU buffers.
std::vector< TexEntry > modelTextures
 Per-sub-mesh texture entries.
std::vector< VkDescriptorSet > modelDescriptorSets
 Descriptor sets (frames x textures).
glm::vec3 modelCenterOffset
 Translation to centre the bounding box at origin.
float modelRenderScale
 Scale factor normalising model extent.

Private Member Functions

void cleanupInstanceResources (VKWindow *win)
 Clean up only UBO and descriptor resources (used by instances).
void loadModelTextures (VKWindow *win, const std::string &tex, const std::string &path)
 Load texture images listed in a .tex file and upload to GPU.
void loadModelTexturesFromMTL (VKWindow *win, const std::string &path)
 Load textures using map_Kd paths from parsed MTL materials.
void createModelDescriptorPool (VKWindow *win)
 Create the Vulkan descriptor pool sized for this model's textures.
void createModelDescriptorSets (VKWindow *win)
 Allocate and write descriptor sets binding UBOs and textures.
void createModelPipeline (VKWindow *win)
 Create a per-model graphics pipeline from stored shader paths.

Private Attributes

bool isInstance_ = false
 True if geometry/textures are shared from another model.
VkDescriptorPool modelDescriptorPool = VK_NULL_HANDLE
 Descriptor pool for this model.
VkPipeline modelPipeline = VK_NULL_HANDLE
 Per-model graphics pipeline (fill).
VkPipeline modelPipelineWireframe = VK_NULL_HANDLE
 Per-model wireframe pipeline.
std::string vertShaderPath_
 Vertex shader SPIR-V path.
std::string fragShaderPath_
 Fragment shader SPIR-V path.
std::vector< VkBuffer > modelUniformBuffers
 Per-frame uniform buffers.
std::vector< VkDeviceMemory > modelUniformBufferMemory
 Memory for per-frame UBOs.
std::vector< void * > modelUniformBuffersMapped
 Persistently mapped UBO pointers.

Friends

class VKWindow

Detailed Description

Convenience wrapper that owns an MXModel plus its Vulkan textures, UBOs, and descriptor sets.

Typical usage:

mdl.load(win, "mesh.obj", "textures.tex", "assets", 1.0f);
// each frame:
mdl.updateUBO(win, imageIndex, ubo);
// on resize:
mdl.resize(win);
// on shutdown:
mdl.cleanup(win);
void resize(VKWindow *win)
Recreate UBOs and descriptor sets after a swapchain resize.
void cleanup(VKWindow *win)
Destroy all Vulkan resources owned by this model.
void load(VKWindow *win, const std::string &model, const std::string &text, const std::string &path, float scale)
Load a model, its textures, and create all GPU resources.
VKAbstractModel()=default
Default constructor.
void updateUBO(VKWindow *win, uint32_t imageIndex, const mx::UniformBufferObject &ubo)
Copy a UBO into the mapped buffer for a given frame.

Definition at line 34 of file vk_abstract_model.hpp.

Constructor & Destructor Documentation

◆ VKAbstractModel() [1/3]

mx::VKAbstractModel::VKAbstractModel ( )
default

Default constructor.

Referenced by loadInstance(), operator=(), operator=(), VKAbstractModel(), and VKAbstractModel().

◆ VKAbstractModel() [2/3]

mx::VKAbstractModel::VKAbstractModel ( const VKAbstractModel & )
delete

References VKAbstractModel().

◆ VKAbstractModel() [3/3]

mx::VKAbstractModel::VKAbstractModel ( VKAbstractModel && )
delete

References VKAbstractModel().

Member Function Documentation

◆ cleanup()

void mx::VKAbstractModel::cleanup ( VKWindow * win)

Destroy all Vulkan resources owned by this model.

Parameters
winOwning Vulkan window.

Definition at line 422 of file vk_abstract_model.cpp.

References cleanupInstanceResources(), mx::VKWindow::getDevice(), isInstance_, modelDescriptorPool, modelPipeline, modelPipelineWireframe, modelTextures, modelUniformBufferMemory, modelUniformBuffers, modelUniformBuffersMapped, and VKWindow.

◆ cleanupInstanceResources()

void mx::VKAbstractModel::cleanupInstanceResources ( VKWindow * win)
private

Clean up only UBO and descriptor resources (used by instances).

Definition at line 74 of file vk_abstract_model.cpp.

References mx::VKWindow::getDevice(), modelDescriptorPool, modelDescriptorSets, modelTextures, modelUniformBufferMemory, modelUniformBuffers, modelUniformBuffersMapped, and VKWindow.

Referenced by cleanup().

◆ createModelDescriptorPool()

void mx::VKAbstractModel::createModelDescriptorPool ( VKWindow * win)
private

Create the Vulkan descriptor pool sized for this model's textures.

Parameters
winOwning Vulkan window.

Definition at line 353 of file vk_abstract_model.cpp.

References mx::VKWindow::getDevice(), modelDescriptorPool, modelTextures, mx::VKWindow::swapChainImages, and VKWindow.

Referenced by load(), loadInstance(), and resize().

◆ createModelDescriptorSets()

void mx::VKAbstractModel::createModelDescriptorSets ( VKWindow * win)
private

Allocate and write descriptor sets binding UBOs and textures.

Parameters
winOwning Vulkan window.

Definition at line 371 of file vk_abstract_model.cpp.

References mx::VKWindow::descriptorSetLayout, mx::VKWindow::getDevice(), modelDescriptorPool, modelDescriptorSets, modelTextures, modelUniformBuffers, mx::VKWindow::swapChainImages, mx::VKWindow::textureSampler, and VKWindow.

Referenced by load(), loadInstance(), and resize().

◆ createModelPipeline()

void mx::VKAbstractModel::createModelPipeline ( VKWindow * win)
private

Create a per-model graphics pipeline from stored shader paths.

Parameters
winOwning Vulkan window.

Definition at line 468 of file vk_abstract_model.cpp.

References mx::VKWindow::createShaderModule(), fragShaderPath_, mx::VKWindow::getDevice(), modelPipeline, modelPipelineWireframe, mx::VKWindow::pipelineLayout, mx::readFile(), mx::VKWindow::renderPass, vertShaderPath_, and VKWindow.

Referenced by resize(), and setShaders().

◆ getModelPipeline()

VkPipeline mx::VKAbstractModel::getModelPipeline ( ) const
inline

Get the per-model pipeline, or VK_NULL_HANDLE if using the window default.

Returns
Pipeline handle (fill mode).

Definition at line 108 of file vk_abstract_model.hpp.

References modelPipeline.

◆ getModelPipelineWireframe()

VkPipeline mx::VKAbstractModel::getModelPipelineWireframe ( ) const
inline

Get the per-model wireframe pipeline, or VK_NULL_HANDLE if not available.

Returns
Pipeline handle (wireframe mode).

Definition at line 114 of file vk_abstract_model.hpp.

References modelPipelineWireframe.

◆ hasCustomShaders()

bool mx::VKAbstractModel::hasCustomShaders ( ) const
inline

Check whether this model has custom shaders set.

Returns
true if setShaders() was called.

Definition at line 131 of file vk_abstract_model.hpp.

References vertShaderPath_.

◆ isInstance()

bool mx::VKAbstractModel::isInstance ( ) const
inline

Check whether this is a lightweight instance (shared geometry).

Definition at line 134 of file vk_abstract_model.hpp.

References isInstance_.

◆ load()

void mx::VKAbstractModel::load ( VKWindow * win,
const std::string & model,
const std::string & text,
const std::string & path,
float scale )

Load a model, its textures, and create all GPU resources.

Parameters
winOwning Vulkan window (provides device handles).
modelPath to the mesh file (.obj / .mxmod / .mxmod.z).
textPath to a .tex file listing texture image filenames.
pathDirectory prefix prepended to each texture filename.
scaleUniform scale applied to all vertex positions.

Definition at line 6 of file vk_abstract_model.cpp.

References mx::VKWindow::createBuffer(), createModelDescriptorPool(), createModelDescriptorSets(), mx::VKWindow::getCommandPool(), mx::VKWindow::getDevice(), mx::VKWindow::getGraphicsQueue(), mx::VKWindow::getPhysicalDevice(), loadModelTextures(), modelCenterOffset, modelRenderScale, modelUniformBufferMemory, modelUniformBuffers, modelUniformBuffersMapped, mx::VKWindow::swapChainImages, and VKWindow.

◆ loadInstance()

void mx::VKAbstractModel::loadInstance ( VKWindow * win,
VKAbstractModel & source )

Create a lightweight instance sharing geometry & textures from a source model.

Only creates its own UBOs and descriptor sets. The source model must outlive this instance and be cleaned up after all instances.

Parameters
winOwning Vulkan window.
sourceFully-loaded model to share geometry/textures from.

Definition at line 49 of file vk_abstract_model.cpp.

References mx::VKWindow::createBuffer(), createModelDescriptorPool(), createModelDescriptorSets(), mx::VKWindow::getDevice(), isInstance_, modelCenterOffset, modelRenderScale, modelTextures, modelUniformBufferMemory, modelUniformBuffers, modelUniformBuffersMapped, mx::VKWindow::swapChainImages, VKAbstractModel(), and VKWindow.

◆ loadModelTextures()

void mx::VKAbstractModel::loadModelTextures ( VKWindow * win,
const std::string & tex,
const std::string & path )
private

◆ loadModelTexturesFromMTL()

void mx::VKAbstractModel::loadModelTexturesFromMTL ( VKWindow * win,
const std::string & path )
private

◆ operator=() [1/2]

VKAbstractModel & mx::VKAbstractModel::operator= ( const VKAbstractModel & )
delete

References VKAbstractModel().

◆ operator=() [2/2]

VKAbstractModel & mx::VKAbstractModel::operator= ( VKAbstractModel && )
delete

References VKAbstractModel(), and VKWindow.

◆ render()

void mx::VKAbstractModel::render ( int cmd)

Record rendering commands (currently a placeholder).

Parameters
cmdCommand buffer index.

Definition at line 110 of file vk_abstract_model.cpp.

◆ resize()

◆ setShaders()

void mx::VKAbstractModel::setShaders ( VKWindow * win,
const std::string & vert,
const std::string & frag )

Set custom vertex and fragment shaders and create a per-model pipeline.

Parameters
winOwning Vulkan window.
vertPath to the vertex shader SPIR-V file.
fragPath to the fragment shader SPIR-V file.

Definition at line 95 of file vk_abstract_model.cpp.

References createModelPipeline(), fragShaderPath_, mx::VKWindow::getDevice(), modelPipeline, modelPipelineWireframe, vertShaderPath_, and VKWindow.

◆ updateUBO()

void mx::VKAbstractModel::updateUBO ( VKWindow * win,
uint32_t imageIndex,
const mx::UniformBufferObject & ubo )

Copy a UBO into the mapped buffer for a given frame.

Parameters
winOwning Vulkan window.
imageIndexSwapchain image index for this frame.
uboUniform data to upload.

Definition at line 114 of file vk_abstract_model.cpp.

References modelUniformBuffersMapped, and VKWindow.

◆ VKWindow

Member Data Documentation

◆ fragShaderPath_

std::string mx::VKAbstractModel::fragShaderPath_
private

Fragment shader SPIR-V path.

Definition at line 141 of file vk_abstract_model.hpp.

Referenced by createModelPipeline(), and setShaders().

◆ isInstance_

bool mx::VKAbstractModel::isInstance_ = false
private

True if geometry/textures are shared from another model.

Definition at line 136 of file vk_abstract_model.hpp.

Referenced by cleanup(), isInstance(), and loadInstance().

◆ modelCenterOffset

glm::vec3 mx::VKAbstractModel::modelCenterOffset

Translation to centre the bounding box at origin.

Definition at line 101 of file vk_abstract_model.hpp.

Referenced by load(), and loadInstance().

◆ modelDescriptorPool

VkDescriptorPool mx::VKAbstractModel::modelDescriptorPool = VK_NULL_HANDLE
private

Descriptor pool for this model.

Definition at line 137 of file vk_abstract_model.hpp.

Referenced by cleanup(), cleanupInstanceResources(), createModelDescriptorPool(), createModelDescriptorSets(), and resize().

◆ modelDescriptorSets

std::vector<VkDescriptorSet> mx::VKAbstractModel::modelDescriptorSets

Descriptor sets (frames x textures).

Definition at line 100 of file vk_abstract_model.hpp.

Referenced by cleanupInstanceResources(), createModelDescriptorSets(), and resize().

◆ modelPipeline

VkPipeline mx::VKAbstractModel::modelPipeline = VK_NULL_HANDLE
private

Per-model graphics pipeline (fill).

Definition at line 138 of file vk_abstract_model.hpp.

Referenced by cleanup(), createModelPipeline(), getModelPipeline(), and setShaders().

◆ modelPipelineWireframe

VkPipeline mx::VKAbstractModel::modelPipelineWireframe = VK_NULL_HANDLE
private

Per-model wireframe pipeline.

Definition at line 139 of file vk_abstract_model.hpp.

Referenced by cleanup(), createModelPipeline(), getModelPipelineWireframe(), and setShaders().

◆ modelRenderScale

float mx::VKAbstractModel::modelRenderScale

Scale factor normalising model extent.

Definition at line 102 of file vk_abstract_model.hpp.

Referenced by load(), and loadInstance().

◆ modelTextures

std::vector<TexEntry> mx::VKAbstractModel::modelTextures

◆ modelUniformBufferMemory

std::vector<VkDeviceMemory> mx::VKAbstractModel::modelUniformBufferMemory
private

Memory for per-frame UBOs.

Definition at line 143 of file vk_abstract_model.hpp.

Referenced by cleanup(), cleanupInstanceResources(), load(), loadInstance(), and resize().

◆ modelUniformBuffers

std::vector<VkBuffer> mx::VKAbstractModel::modelUniformBuffers
private

Per-frame uniform buffers.

Definition at line 142 of file vk_abstract_model.hpp.

Referenced by cleanup(), cleanupInstanceResources(), createModelDescriptorSets(), load(), loadInstance(), and resize().

◆ modelUniformBuffersMapped

std::vector<void*> mx::VKAbstractModel::modelUniformBuffersMapped
private

Persistently mapped UBO pointers.

Definition at line 144 of file vk_abstract_model.hpp.

Referenced by cleanup(), cleanupInstanceResources(), load(), loadInstance(), resize(), and updateUBO().

◆ obj

mx::MXModel mx::VKAbstractModel::obj

Underlying mesh data and GPU buffers.

Definition at line 98 of file vk_abstract_model.hpp.

◆ vertShaderPath_

std::string mx::VKAbstractModel::vertShaderPath_
private

Vertex shader SPIR-V path.

Definition at line 140 of file vk_abstract_model.hpp.

Referenced by createModelPipeline(), hasCustomShaders(), resize(), and setShaders().


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