Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
vulkan_material_resources.cpp
Go to the documentation of this file.
2#include "vulkan_renderer.hpp"
3#include "material.hpp"
4#include "utility.hpp"
5
6namespace rendering_engine
7{
8
10 :
11 mRenderer(renderer)
12{}
13
15{
16 Shutdown();
17
18 mDescriptorSetLayout = mRenderer->CreateDescriptorSetLayout(material);
19
20 std::string matName;
21 if (material->GetMaterialSettings().parentMaterialName.empty())
22 {
23 matName = material->GetMaterialSettings().materialName;
24 }
25 else
26 {
27 matName = material->GetMaterialSettings().parentMaterialName;
28 }
29
30 std::vector<char> spvVert;
31 std::vector<char> spvFrag;
32
34 {
35 const auto& entries = Utility::GetPackEntries();
36 std::string materialEntry = "Shaders/" + matName;
37
38 std::vector<uint8_t> binaryFileDataVert = Utility::ReadPackedFile(materialEntry + "/" + std::string(matName + "_vert.spv"));
39 std::vector<uint8_t> binaryFileDataFrag = Utility::ReadPackedFile(materialEntry + "/" + std::string(matName + "_frag.spv"));
40
41 spvVert.assign(binaryFileDataVert.begin(), binaryFileDataVert.end());
42 spvFrag.assign(binaryFileDataFrag.begin(), binaryFileDataFrag.end());
43 }
44 else
45 {
46 boost::filesystem::path matPath = Utility::GetShadersFolderPath() / matName;
47
48 spvVert = Utility::ReadShaderBinaryFile((matPath / std::string(matName + "_vert.spv")).string());
49 spvFrag = Utility::ReadShaderBinaryFile((matPath / std::string(matName + "_frag.spv")).string());
50 }
51
52 mPipelinePair = mRenderer->CreateGraphicsPipeline(material, mDescriptorSetLayout, spvVert, spvFrag);
53}
54
56{
57 if (!mRenderer)
58 return;
59
60 VkDevice device = mRenderer->GetLogicalDevice(); // or GetDevice()
61
62 // Pipeline first
63 if (mPipelinePair.second != VK_NULL_HANDLE)
64 {
65 vkDestroyPipeline(device, mPipelinePair.second, nullptr);
66 mPipelinePair.second = VK_NULL_HANDLE;
67 }
68
69 // Then pipeline layout
70 if (mPipelinePair.first != VK_NULL_HANDLE)
71 {
72 vkDestroyPipelineLayout(device, mPipelinePair.first, nullptr);
73 mPipelinePair.first = VK_NULL_HANDLE;
74 }
75
76 // Then descriptor set layout
77 if (mDescriptorSetLayout != VK_NULL_HANDLE)
78 {
79 vkDestroyDescriptorSetLayout(device, mDescriptorSetLayout, nullptr);
80 mDescriptorSetLayout = VK_NULL_HANDLE;
81 }
82}
83
85{
86 return mDescriptorSetLayout;
87}
88
90{
91 return mPipelinePair.first;
92}
93
95{
96 return mPipelinePair.second;
97}
98
99} // namespace rendering_engine
Represents a material instance with parameter values, texture bindings, and rendering configuration.
Definition: material.hpp:30
const MaterialSettings GetMaterialSettings() const
Returns the material's static settings (domain, blend mode, shading model, etc.).
Definition: material.cpp:22
static boost::filesystem::path GetShadersFolderPath()
Returns absolute path to Content/Shaders.
Definition: utility.cpp:259
static std::vector< char > ReadShaderBinaryFile(std::string const &filename)
Reads a binary shader file from disk.
Definition: utility.cpp:113
static const PackEntries & GetPackEntries()
Returns the manifest of packed files.
Definition: utility.cpp:281
static std::vector< uint8_t > ReadPackedFile(const std::string &entryPath)
Reads raw bytes of a file stored inside Pack.bin.
Definition: utility.cpp:322
static bool IsPackageProvided()
Checks whether packed assets (Pack.bin / Pack.json) exist.
Definition: utility.cpp:274
VkPipeline GetPipeline() const
Gets the Vulkan graphics pipeline used by the material.
void Shutdown() override
Releases all Vulkan GPU resources associated with this material.
void Initialize(Material *material) override
Initializes Vulkan-specific GPU resources for the material.
VkPipelineLayout GetPipelineLayout() const
Gets the Vulkan pipeline layout used by the material.
VulkanMaterialResources(VulkanRenderer *renderer)
Constructs a VulkanMaterialResources instance.
VkDescriptorSetLayout GetDescriptorSetLayout() const
Gets the Vulkan descriptor set layout for the material.
Vulkan-based implementation of the IRenderer interface.
VkDevice & GetLogicalDevice()
Returns reference to the logical Vulkan device.
VkDescriptorSetLayout CreateDescriptorSetLayout(Material *material)
Creates a descriptor set layout corresponding to a given material.
std::pair< VkPipelineLayout, VkPipeline > CreateGraphicsPipeline(Material *material, VkDescriptorSetLayout &descriptorSetLayout, std::vector< char > &spvVertShaderCode, std::vector< char > &spvFragShaderCode)
Creates a Vulkan graphics pipeline based on material and shader inputs.