|
libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
|
Convenience wrapper that owns an MXModel plus its Vulkan textures, UBOs, and descriptor sets. More...
#include <vk_abstract_model.hpp>
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 |
Convenience wrapper that owns an MXModel plus its Vulkan textures, UBOs, and descriptor sets.
Typical usage:
Definition at line 34 of file vk_abstract_model.hpp.
|
default |
Default constructor.
Referenced by loadInstance(), operator=(), operator=(), VKAbstractModel(), and VKAbstractModel().
|
delete |
References VKAbstractModel().
|
delete |
References VKAbstractModel().
| void mx::VKAbstractModel::cleanup | ( | VKWindow * | win | ) |
Destroy all Vulkan resources owned by this model.
| win | Owning 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.
|
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().
|
private |
Create the Vulkan descriptor pool sized for this model's textures.
| win | Owning 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().
|
private |
Allocate and write descriptor sets binding UBOs and textures.
| win | Owning 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().
|
private |
Create a per-model graphics pipeline from stored shader paths.
| win | Owning 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().
|
inline |
Get the per-model pipeline, or VK_NULL_HANDLE if using the window default.
Definition at line 108 of file vk_abstract_model.hpp.
References modelPipeline.
|
inline |
Get the per-model wireframe pipeline, or VK_NULL_HANDLE if not available.
Definition at line 114 of file vk_abstract_model.hpp.
References modelPipelineWireframe.
|
inline |
Check whether this model has custom shaders set.
true if setShaders() was called. Definition at line 131 of file vk_abstract_model.hpp.
References vertShaderPath_.
|
inline |
Check whether this is a lightweight instance (shared geometry).
Definition at line 134 of file vk_abstract_model.hpp.
References isInstance_.
| 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.
| win | Owning Vulkan window (provides device handles). |
| model | Path to the mesh file (.obj / .mxmod / .mxmod.z). |
| text | Path to a .tex file listing texture image filenames. |
| path | Directory prefix prepended to each texture filename. |
| scale | Uniform 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.
| 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.
| win | Owning Vulkan window. |
| source | Fully-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.
|
private |
Load texture images listed in a .tex file and upload to GPU.
| win | Owning Vulkan window. |
| tex | Path to the .tex manifest file. |
| path | Directory prefix for image files. |
Definition at line 119 of file vk_abstract_model.cpp.
References mx::VKWindow::copyBufferToImage(), mx::VKWindow::createBuffer(), mx::VKWindow::createImage(), mx::VKWindow::createImageView(), mx::VKWindow::getDevice(), mx::VKAbstractModel::TexEntry::h, mx::VKAbstractModel::TexEntry::image, png::LoadPNG(), mx::VKAbstractModel::TexEntry::memory, modelTextures, mx::VKWindow::transitionImageLayout(), mx::VKAbstractModel::TexEntry::view, VKWindow, and mx::VKAbstractModel::TexEntry::w.
Referenced by load().
|
private |
Load textures using map_Kd paths from parsed MTL materials.
| win | Owning Vulkan window. |
| path | Directory prefix for image files. |
Definition at line 241 of file vk_abstract_model.cpp.
References mx::VKWindow::copyBufferToImage(), mx::VKWindow::createBuffer(), mx::VKWindow::createImage(), mx::VKWindow::createImageView(), mx::VKWindow::getDevice(), mx::VKAbstractModel::TexEntry::h, mx::VKAbstractModel::TexEntry::image, png::LoadPNG(), mx::VKAbstractModel::TexEntry::memory, modelTextures, mx::VKWindow::transitionImageLayout(), mx::VKAbstractModel::TexEntry::view, VKWindow, and mx::VKAbstractModel::TexEntry::w.
|
delete |
References VKAbstractModel().
|
delete |
References VKAbstractModel(), and VKWindow.
| void mx::VKAbstractModel::render | ( | int | cmd | ) |
Record rendering commands (currently a placeholder).
| cmd | Command buffer index. |
Definition at line 110 of file vk_abstract_model.cpp.
| void mx::VKAbstractModel::resize | ( | VKWindow * | win | ) |
Recreate UBOs and descriptor sets after a swapchain resize.
| win | Owning Vulkan window. |
Definition at line 321 of file vk_abstract_model.cpp.
References mx::VKWindow::createBuffer(), createModelDescriptorPool(), createModelDescriptorSets(), createModelPipeline(), mx::VKWindow::getDevice(), modelDescriptorPool, modelDescriptorSets, modelUniformBufferMemory, modelUniformBuffers, modelUniformBuffersMapped, mx::VKWindow::swapChainImages, vertShaderPath_, and VKWindow.
| 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.
| win | Owning Vulkan window. |
| vert | Path to the vertex shader SPIR-V file. |
| frag | Path 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.
| void mx::VKAbstractModel::updateUBO | ( | VKWindow * | win, |
| uint32_t | imageIndex, | ||
| const mx::UniformBufferObject & | ubo ) |
Copy a UBO into the mapped buffer for a given frame.
| win | Owning Vulkan window. |
| imageIndex | Swapchain image index for this frame. |
| ubo | Uniform data to upload. |
Definition at line 114 of file vk_abstract_model.cpp.
References modelUniformBuffersMapped, and VKWindow.
|
friend |
Definition at line 145 of file vk_abstract_model.hpp.
References VKWindow.
Referenced by cleanup(), cleanupInstanceResources(), createModelDescriptorPool(), createModelDescriptorSets(), createModelPipeline(), load(), loadInstance(), loadModelTextures(), loadModelTexturesFromMTL(), operator=(), resize(), setShaders(), updateUBO(), and VKWindow.
|
private |
Fragment shader SPIR-V path.
Definition at line 141 of file vk_abstract_model.hpp.
Referenced by createModelPipeline(), and setShaders().
|
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().
| 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().
|
private |
Descriptor pool for this model.
Definition at line 137 of file vk_abstract_model.hpp.
Referenced by cleanup(), cleanupInstanceResources(), createModelDescriptorPool(), createModelDescriptorSets(), and resize().
| 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().
|
private |
Per-model graphics pipeline (fill).
Definition at line 138 of file vk_abstract_model.hpp.
Referenced by cleanup(), createModelPipeline(), getModelPipeline(), and setShaders().
|
private |
Per-model wireframe pipeline.
Definition at line 139 of file vk_abstract_model.hpp.
Referenced by cleanup(), createModelPipeline(), getModelPipelineWireframe(), and setShaders().
| float mx::VKAbstractModel::modelRenderScale |
Scale factor normalising model extent.
Definition at line 102 of file vk_abstract_model.hpp.
Referenced by load(), and loadInstance().
| std::vector<TexEntry> mx::VKAbstractModel::modelTextures |
Per-sub-mesh texture entries.
Definition at line 99 of file vk_abstract_model.hpp.
Referenced by cleanup(), cleanupInstanceResources(), createModelDescriptorPool(), createModelDescriptorSets(), loadInstance(), loadModelTextures(), and loadModelTexturesFromMTL().
|
private |
Memory for per-frame UBOs.
Definition at line 143 of file vk_abstract_model.hpp.
Referenced by cleanup(), cleanupInstanceResources(), load(), loadInstance(), and resize().
|
private |
Per-frame uniform buffers.
Definition at line 142 of file vk_abstract_model.hpp.
Referenced by cleanup(), cleanupInstanceResources(), createModelDescriptorSets(), load(), loadInstance(), and resize().
|
private |
Persistently mapped UBO pointers.
Definition at line 144 of file vk_abstract_model.hpp.
Referenced by cleanup(), cleanupInstanceResources(), load(), loadInstance(), resize(), and updateUBO().
| mx::MXModel mx::VKAbstractModel::obj |
Underlying mesh data and GPU buffers.
Definition at line 98 of file vk_abstract_model.hpp.
|
private |
Vertex shader SPIR-V path.
Definition at line 140 of file vk_abstract_model.hpp.
Referenced by createModelPipeline(), hasCustomShaders(), resize(), and setShaders().