32 mTextureCache = textureCache;
53 if (mDescriptorPool != VK_NULL_HANDLE)
59 mDescriptorPool = VK_NULL_HANDLE;
63 for (
size_t i = 0; i < mTransformationBuffers.size(); i++)
65 if (mTransformationBuffers[i] != VK_NULL_HANDLE)
69 tBuffer.
buffer = mTransformationBuffers[i];
73 if (i < mTransformationBuffersMemory.size() &&
74 mTransformationBuffersMemory[i] != VK_NULL_HANDLE)
79 tMem.
memory = mTransformationBuffersMemory[i];
82 mTransformationBuffers[i] = VK_NULL_HANDLE;
83 mTransformationBuffersMemory[i] = VK_NULL_HANDLE;
87 for(
size_t i = 0; i < mMaterialParametersBuffers.size(); ++i)
89 if (mMaterialParametersBuffers[i] != VK_NULL_HANDLE)
93 matBuffer.
buffer = mMaterialParametersBuffers[i];
97 if(i < mMaterialParametersMemory.size() &&
98 mMaterialParametersMemory[i] != VK_NULL_HANDLE)
102 matMem.
memory = mMaterialParametersMemory[i];
105 mMaterialParametersBuffers[i] = VK_NULL_HANDLE;
106 mMaterialParametersMemory[i] = VK_NULL_HANDLE;
110 mTransformationBuffers.clear();
111 mTransformationBuffersMemory.clear();
112 mMaterialParametersBuffers.clear();
113 mMaterialParametersMemory.clear();
134 mGraphicsPipeline = materialResources->
GetPipeline();
148 VkDeviceSize transformationBufferSize = 0;
161 throw std::runtime_error(
"Unsupported material domain.");
169 mRenderer->
CreateBuffer(transformationBufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
170 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
171 mTransformationBuffers[i], mTransformationBuffersMemory[i]);
175 if (materialParameters.buffer.empty())
180 VkDeviceSize matVarBufferSize = materialParameters.
buffer.size();
186 mRenderer->
CreateBuffer(matVarBufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
187 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
188 mMaterialParametersBuffers[i], mMaterialParametersMemory[i]);
195 std::vector<VkDescriptorPoolSize> poolSizes;
197 VkDescriptorPoolSize tranformationPool;
198 tranformationPool.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
199 tranformationPool.descriptorCount = maxFramesInFlight;
201 poolSizes.push_back(tranformationPool);
204 if (!materialParameters.buffer.empty())
206 VkDescriptorPoolSize materialParameterPool;
207 materialParameterPool.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
208 materialParameterPool.descriptorCount = maxFramesInFlight;
210 poolSizes.push_back(materialParameterPool);
213 for (
const auto& texture : mMaterial->
GetTextures())
216 VkDescriptorPoolSize textureSamplerPool;
217 textureSamplerPool.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
218 textureSamplerPool.descriptorCount = maxFramesInFlight;
220 poolSizes.push_back(textureSamplerPool);
223 VkDescriptorPoolCreateInfo poolInfo{};
224 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
225 poolInfo.poolSizeCount =
static_cast<uint32_t
>(poolSizes.size());
226 poolInfo.pPoolSizes = poolSizes.data();
227 poolInfo.maxSets = maxFramesInFlight;
229 if (vkCreateDescriptorPool(mRenderer->
GetLogicalDevice(), &poolInfo,
nullptr, &mDescriptorPool) != VK_SUCCESS)
231 throw std::runtime_error(
"failed to create descriptor pool!");
248 std::vector<VkDescriptorSetLayout> layouts(maxFramesInFlight, descriptorSetLayout);
249 VkDescriptorSetAllocateInfo allocInfo{};
250 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
251 allocInfo.descriptorPool = mDescriptorPool;
252 allocInfo.descriptorSetCount = maxFramesInFlight;
253 allocInfo.pSetLayouts = layouts.data();
256 VkResult result = vkAllocateDescriptorSets(mRenderer->
GetLogicalDevice(), &allocInfo, mDescriptorSets.data());
257 if (result != VK_SUCCESS)
259 throw std::runtime_error(
"failed to allocate descriptor sets! Error: " + std::to_string(result));
264 unsigned int dstBinding = 0;
266 VkDescriptorBufferInfo matVarBufferInfo{};
267 VkWriteDescriptorSet matVarDescSet;
269 std::vector<VkWriteDescriptorSet> descriptorWrites;
272 VkDescriptorBufferInfo transformationBufferInfo{};
273 transformationBufferInfo.buffer = mTransformationBuffers[i];
274 transformationBufferInfo.offset = 0;
283 VkWriteDescriptorSet transformationsDescSet;
284 transformationsDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
285 transformationsDescSet.pNext =
nullptr;
286 transformationsDescSet.dstSet = mDescriptorSets[i];
287 transformationsDescSet.dstBinding = dstBinding;
288 transformationsDescSet.dstArrayElement = 0;
289 transformationsDescSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
290 transformationsDescSet.descriptorCount = 1;
291 transformationsDescSet.pBufferInfo = &transformationBufferInfo;
292 descriptorWrites.push_back(transformationsDescSet);
296 if (!materialParameters.buffer.empty())
300 matVarBufferInfo.
buffer = mMaterialParametersBuffers[i];
301 matVarBufferInfo.offset = 0;
302 matVarBufferInfo.range = materialParameters.buffer.size();
304 matVarDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
305 matVarDescSet.pNext =
nullptr;
306 matVarDescSet.dstSet = mDescriptorSets[i];
307 matVarDescSet.dstBinding = dstBinding;
308 matVarDescSet.dstArrayElement = 0;
309 matVarDescSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
310 matVarDescSet.descriptorCount = 1;
311 matVarDescSet.pBufferInfo = &matVarBufferInfo;
312 descriptorWrites.push_back(matVarDescSet);
316 for (
const auto& texture : mMaterial->
GetTextures())
321 VkDescriptorImageInfo imageInfo{};
322 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
326 VkWriteDescriptorSet imageDescSet;
327 imageDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
328 imageDescSet.pNext =
nullptr;
329 imageDescSet.dstSet = mDescriptorSets[i];
330 imageDescSet.dstBinding = dstBinding;
331 imageDescSet.dstArrayElement = 0;
332 imageDescSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
333 imageDescSet.descriptorCount = 1;
334 imageDescSet.pImageInfo = &imageInfo;
336 descriptorWrites.push_back(imageDescSet);
339 vkUpdateDescriptorSets(mRenderer->
GetLogicalDevice(),
static_cast<uint32_t
>(descriptorWrites.size()), descriptorWrites.data(), 0,
nullptr);
350 vkMapMemory(device, mTransformationBuffersMemory[frame], 0,
sizeof(
Transformations2D), 0, &dataTransform);
351 memcpy(dataTransform, &transformations,
sizeof(transformations));
352 vkUnmapMemory(device, mTransformationBuffersMemory[frame]);
361 transformations.
proj[1][1] *= -1;
364 vkMapMemory(device, mTransformationBuffersMemory[frame], 0,
sizeof(
Transformations3D), 0, &dataTransform);
365 memcpy(dataTransform, &transformations,
sizeof(transformations));
366 vkUnmapMemory(device, mTransformationBuffersMemory[frame]);
375 if (!materialParameters.
buffer.empty())
378 vkMapMemory(device, mMaterialParametersMemory[frame], 0, materialParameters.
buffer.size(), 0, &dataCustom);
379 memcpy(dataCustom, materialParameters.
buffer.data(), materialParameters.
buffer.size());
380 vkUnmapMemory(device, mMaterialParametersMemory[frame]);
389 VkBuffer vertexBuffer[] = { mVertexBuffer };
390 VkDeviceSize offsets[] = { 0 };
392 vkCmdBindPipeline(commandBuffers[frame], VK_PIPELINE_BIND_POINT_GRAPHICS, mGraphicsPipeline);
409 vkCmdBindVertexBuffers(commandBuffers[frame], 0, 1, vertexBuffer, offsets);
410 vkCmdBindIndexBuffer(commandBuffers[frame], mIndexBuffer, 0, VK_INDEX_TYPE_UINT32);
411 vkCmdBindDescriptorSets(commandBuffers[frame], VK_PIPELINE_BIND_POINT_GRAPHICS,
412 mPipelineLayout, 0, 1, &mDescriptorSets[frame], 0,
nullptr);
413 vkCmdDrawIndexed(commandBuffers[frame],
static_cast<uint32_t
>(mMeshData->
GetIndices().size()), 1, 0, 0, 0);
Represents a material instance with parameter values, texture bindings, and rendering configuration.
std::vector< std::string > GetTextures() const
Returns the list of texture names used by this material.
PackedMaterialData PackMaterialParameters()
Packs the current float/vector parameters into a binary buffer and layout metadata.
const MaterialSettings GetMaterialSettings() const
Returns the material's static settings (domain, blend mode, shading model, etc.).
IMaterialRenderResources * GetMaterialRenderResources() const
Returns the backend-specific GPU handle of the material.
Manages mesh data in RAM and GPU, including upload and release operations.
const std::vector< uint32_t > & GetIndices() const
Returns a constant reference to the mesh indices.
IMeshRenderResources * GetMeshRenderResources()
Get the interface for mesh GPU resources (Vulkan or other backend).
Manages texture loading, GPU uploading, and caching for reuse.
std::shared_ptr< ImageDataGpu > GetTextureResources(std::string filename)
Retrieves the full texture resource wrapper from cache.
Vulkan-specific implementation of material render resources.
VkPipeline GetPipeline() const
Gets the Vulkan graphics pipeline used by the material.
VkPipelineLayout GetPipelineLayout() const
Gets the Vulkan pipeline layout used by the material.
VkDescriptorSetLayout GetDescriptorSetLayout() const
Gets the Vulkan descriptor set layout for the material.
Vulkan implementation of the mesh GPU resource interface.
VkBuffer GetIndexBuffer() const
Get the Vulkan index buffer handle.
VkBuffer GetVertexBuffer() const
Get the Vulkan vertex buffer handle.
void AcquireResources()
Allocates and initializes all GPU buffers, descriptor sets, and pipelines for this drawable.
~VulkanRenderResources()
Destructor.
void OnRenderResourcesRebuild() override
Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).
void OnRenderResourcesRelease() override
Renderer callback: release all GPU resources (used during device loss/reset).
void DrawIndexed()
Issues a Vulkan draw command for the currently bound indexed mesh.
void CreateDescriptorPool()
VulkanRenderResources(VulkanRenderer *renderer)
Constructor.
void Shutdown() override
Releases all allocated GPU resources for this object.
void UpdateTransformations(Transformations2D &transformations)
void Initialize(Material *material, MeshDataGpu *meshData, TextureCache *textureCache) override
Initializes GPU-side resources using provided material, mesh, and texture cache.
void SubmitResources(Transformations2D &transformations, const PackedMaterialData &materialParameters) override
Updates GPU resources and issues a draw call for a 2D object.
void CreateDescriptorSet()
void CreateUniformBuffers()
void UpdateMaterialParameters(const PackedMaterialData &materialParameters)
Vulkan-based implementation of the IRenderer interface.
VkDevice & GetLogicalDevice()
Returns reference to the logical Vulkan device.
void CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory)
Creates a new Vulkan buffer with the specified usage and memory properties.
void UnregisterObserver(IRendererObserver *notifier) override
Unregisters a previously registered observer.
void AddDeferredDestroy(DeferredItem deferredItem)
size_t GetCurrentFrame() const
Returns the index of the currently active frame in flight.
void RegisterObserver(IRendererObserver *notifier) override
Registers an observer for rendering events.
std::vector< VkCommandBuffer > GetComandBuffers()
Returns the collection of command buffers used for rendering.
Vulkan-specific implementation of ITextureRenderResources.
VkImageView GetVkImageView() const
Returns the Vulkan image view associated with this texture resource.
VkSampler GetVkSampler() const
Returns the Vulkan sampler associated with this texture resource.
const int MAX_FRAMES_IN_FLIGHT
Number of frames that can be processed simultaneously (double buffering).
VkDescriptorPool descriptorPool
MaterialDomain materialDomain
Contains the raw buffer data and layout metadata of packed material parameters.
std::vector< uint8_t > buffer