19 bHasCustomMaterialVariables(false)
33 mTextureCache = textureCache;
54 if (mDescriptorPool != VK_NULL_HANDLE)
60 mDescriptorPool = VK_NULL_HANDLE;
64 for (
size_t i = 0; i < mTransformationBuffers.size(); i++)
66 if (mTransformationBuffers[i] != VK_NULL_HANDLE)
70 tBuffer.
buffer = mTransformationBuffers[i];
74 if (i < mTransformationBuffersMemory.size() &&
75 mTransformationBuffersMemory[i] != VK_NULL_HANDLE)
80 tMem.
memory = mTransformationBuffersMemory[i];
83 mTransformationBuffers[i] = VK_NULL_HANDLE;
84 mTransformationBuffersMemory[i] = VK_NULL_HANDLE;
88 for(
size_t i = 0; i < mMaterialParametersBuffers.size(); ++i)
90 if (mMaterialParametersBuffers[i] != VK_NULL_HANDLE)
94 matBuffer.
buffer = mMaterialParametersBuffers[i];
98 if(i < mMaterialParametersMemory.size() &&
99 mMaterialParametersMemory[i] != VK_NULL_HANDLE)
103 matMem.
memory = mMaterialParametersMemory[i];
106 mMaterialParametersBuffers[i] = VK_NULL_HANDLE;
107 mMaterialParametersMemory[i] = VK_NULL_HANDLE;
111 mTransformationBuffers.clear();
112 mTransformationBuffersMemory.clear();
113 mMaterialParametersBuffers.clear();
114 mMaterialParametersMemory.clear();
135 mGraphicsPipeline = materialResources->
GetPipeline();
149 VkDeviceSize transformationBufferSize;
164 mRenderer->
CreateBuffer(transformationBufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
165 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
166 mTransformationBuffers[i], mTransformationBuffersMemory[i]);
170 if (materialParameters.buffer.empty())
175 VkDeviceSize matVarBufferSize = materialParameters.
buffer.size();
181 mRenderer->
CreateBuffer(matVarBufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
182 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
183 mMaterialParametersBuffers[i], mMaterialParametersMemory[i]);
190 std::vector<VkDescriptorPoolSize> poolSizes;
192 VkDescriptorPoolSize tranformationPool;
193 tranformationPool.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
194 tranformationPool.descriptorCount = maxFramesInFlight;
196 poolSizes.push_back(tranformationPool);
199 if (!materialParameters.buffer.empty())
201 VkDescriptorPoolSize materialParameterPool;
202 materialParameterPool.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
203 materialParameterPool.descriptorCount = maxFramesInFlight;
205 poolSizes.push_back(materialParameterPool);
208 for (
const auto& texture : mMaterial->
GetTextures())
211 VkDescriptorPoolSize textureSamplerPool;
212 textureSamplerPool.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
213 textureSamplerPool.descriptorCount = maxFramesInFlight;
215 poolSizes.push_back(textureSamplerPool);
218 VkDescriptorPoolCreateInfo poolInfo{};
219 poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
220 poolInfo.poolSizeCount =
static_cast<uint32_t
>(poolSizes.size());
221 poolInfo.pPoolSizes = poolSizes.data();
222 poolInfo.maxSets = maxFramesInFlight;
224 if (vkCreateDescriptorPool(mRenderer->
GetLogicalDevice(), &poolInfo,
nullptr, &mDescriptorPool) != VK_SUCCESS)
226 throw std::runtime_error(
"failed to create descriptor pool!");
243 std::vector<VkDescriptorSetLayout> layouts(maxFramesInFlight, descriptorSetLayout);
244 VkDescriptorSetAllocateInfo allocInfo{};
245 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
246 allocInfo.descriptorPool = mDescriptorPool;
247 allocInfo.descriptorSetCount = maxFramesInFlight;
248 allocInfo.pSetLayouts = layouts.data();
251 VkResult result = vkAllocateDescriptorSets(mRenderer->
GetLogicalDevice(), &allocInfo, mDescriptorSets.data());
252 if (result != VK_SUCCESS)
254 throw std::runtime_error(
"failed to allocate descriptor sets! Error: " + std::to_string(result));
259 unsigned int dstBinding = 0;
261 VkDescriptorBufferInfo matVarBufferInfo{};
262 VkWriteDescriptorSet matVarDescSet;
264 std::vector<VkWriteDescriptorSet> descriptorWrites;
267 VkDescriptorBufferInfo transformationBufferInfo{};
268 transformationBufferInfo.buffer = mTransformationBuffers[i];
269 transformationBufferInfo.offset = 0;
278 VkWriteDescriptorSet transformationsDescSet;
279 transformationsDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
280 transformationsDescSet.pNext =
nullptr;
281 transformationsDescSet.dstSet = mDescriptorSets[i];
282 transformationsDescSet.dstBinding = dstBinding;
283 transformationsDescSet.dstArrayElement = 0;
284 transformationsDescSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
285 transformationsDescSet.descriptorCount = 1;
286 transformationsDescSet.pBufferInfo = &transformationBufferInfo;
287 descriptorWrites.push_back(transformationsDescSet);
291 if (!materialParameters.buffer.empty())
295 matVarBufferInfo.
buffer = mMaterialParametersBuffers[i];
296 matVarBufferInfo.offset = 0;
297 matVarBufferInfo.range = materialParameters.buffer.size();
299 matVarDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
300 matVarDescSet.pNext =
nullptr;
301 matVarDescSet.dstSet = mDescriptorSets[i];
302 matVarDescSet.dstBinding = dstBinding;
303 matVarDescSet.dstArrayElement = 0;
304 matVarDescSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
305 matVarDescSet.descriptorCount = 1;
306 matVarDescSet.pBufferInfo = &matVarBufferInfo;
307 descriptorWrites.push_back(matVarDescSet);
311 for (
const auto& texture : mMaterial->
GetTextures())
316 VkDescriptorImageInfo imageInfo{};
317 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
321 VkWriteDescriptorSet imageDescSet;
322 imageDescSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
323 imageDescSet.pNext =
nullptr;
324 imageDescSet.dstSet = mDescriptorSets[i];
325 imageDescSet.dstBinding = dstBinding;
326 imageDescSet.dstArrayElement = 0;
327 imageDescSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
328 imageDescSet.descriptorCount = 1;
329 imageDescSet.pImageInfo = &imageInfo;
331 descriptorWrites.push_back(imageDescSet);
334 vkUpdateDescriptorSets(mRenderer->
GetLogicalDevice(),
static_cast<uint32_t
>(descriptorWrites.size()), descriptorWrites.data(), 0,
nullptr);
345 vkMapMemory(device, mTransformationBuffersMemory[frame], 0,
sizeof(
Transformations2D), 0, &dataTransform);
346 memcpy(dataTransform, &transformations,
sizeof(transformations));
347 vkUnmapMemory(device, mTransformationBuffersMemory[frame]);
356 transformations.
proj[1][1] *= -1;
359 vkMapMemory(device, mTransformationBuffersMemory[frame], 0,
sizeof(
Transformations3D), 0, &dataTransform);
360 memcpy(dataTransform, &transformations,
sizeof(transformations));
361 vkUnmapMemory(device, mTransformationBuffersMemory[frame]);
370 if (!materialParameters.
buffer.empty())
373 vkMapMemory(device, mMaterialParametersMemory[frame], 0, materialParameters.
buffer.size(), 0, &dataCustom);
374 memcpy(dataCustom, materialParameters.
buffer.data(), materialParameters.
buffer.size());
375 vkUnmapMemory(device, mMaterialParametersMemory[frame]);
384 VkBuffer vertexBuffer[] = { mVertexBuffer };
385 VkDeviceSize offsets[] = { 0 };
387 vkCmdBindPipeline(commandBuffers[frame], VK_PIPELINE_BIND_POINT_GRAPHICS, mGraphicsPipeline);
404 vkCmdBindVertexBuffers(commandBuffers[frame], 0, 1, vertexBuffer, offsets);
405 vkCmdBindIndexBuffer(commandBuffers[frame], mIndexBuffer, 0, VK_INDEX_TYPE_UINT32);
406 vkCmdBindDescriptorSets(commandBuffers[frame], VK_PIPELINE_BIND_POINT_GRAPHICS,
407 mPipelineLayout, 0, 1, &mDescriptorSets[frame], 0,
nullptr);
408 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