From 0f6dabde868bc7d1d11f993b1e5b2eadef8861ff Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Thu, 18 Jan 2024 18:39:44 +0100 Subject: [PATCH] Fixing more macos things --- src/Engine/Asset/AssetRegistry.cpp | 2 +- src/Engine/Graphics/VertexData.cpp | 8 +++++--- src/Engine/Graphics/Vulkan/Buffer.cpp | 6 +++--- src/Engine/Graphics/Vulkan/Texture.cpp | 6 ++++-- src/Engine/Material/MaterialInstance.cpp | 3 ++- src/Engine/Material/MaterialInstance.h | 1 + 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index b82ec90..ff9c906 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -257,7 +257,7 @@ void AssetRegistry::loadAsset(ArchiveBuffer& buffer) Serialization::load(buffer, folderPath); AssetFolder* folder = getOrCreateFolder(folderPath); - + PAsset asset; switch (identifier) { diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index c951c53..9abcecc 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -30,10 +30,11 @@ void VertexData::resetMeshData() void VertexData::updateMesh(PMesh mesh, Component::Transform& transform) { std::unique_lock l(materialDataLock); - PMaterial mat = mesh->referencedMaterial->getHandle()->getBaseMaterial(); + PMaterialInstance referencedInstance = mesh->referencedMaterial->getHandle(); + PMaterial mat = referencedInstance->getBaseMaterial(); MaterialData& matData = materialData[mat->getName()]; matData.material = mat; - MaterialInstanceData& matInstanceData = matData.instances[mesh->referencedMaterial->getHandle()->getId()]; + MaterialInstanceData& matInstanceData = matData.instances[referencedInstance->getId()]; for (const auto& data : meshData[mesh->id]) { matInstanceData.meshes.add(MeshInstanceData{ @@ -43,7 +44,8 @@ void VertexData::updateMesh(PMesh mesh, Component::Transform& transform) .data = data, }); } - matInstanceData.materialInstance = mesh->referencedMaterial->getHandle(); + matInstanceData.materialInstance = referencedInstance; + referencedInstance->updateDescriptor(); } void VertexData::createDescriptors() diff --git a/src/Engine/Graphics/Vulkan/Buffer.cpp b/src/Engine/Graphics/Vulkan/Buffer.cpp index ff529c0..e7f7449 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.cpp +++ b/src/Engine/Graphics/Vulkan/Buffer.cpp @@ -10,7 +10,6 @@ struct PendingBuffer uint64 size; VkBuffer stagingBuffer; VmaAllocation allocation; - void *mappedMemory; Gfx::QueueType prevQueue; bool writeOnly; }; @@ -163,7 +162,7 @@ void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize, bool writeOnly) { if (buffers[currentBuffer].properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) { - VK_CHECK(vmaMapMemory(graphics->getAllocator(), buffers[currentBuffer].allocation, &pending.mappedMemory)); + VK_CHECK(vmaMapMemory(graphics->getAllocator(), buffers[currentBuffer].allocation, &data)); } else { @@ -177,9 +176,10 @@ void *Buffer::mapRegion(uint64 regionOffset, uint64 regionSize, bool writeOnly) }; VmaAllocationCreateInfo allocInfo = { .usage = VMA_MEMORY_USAGE_AUTO, + .requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, }; VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &allocInfo, &pending.stagingBuffer, &pending.allocation, nullptr)); - vmaMapMemory(graphics->getAllocator(), pending.allocation, &pending.mappedMemory); + vmaMapMemory(graphics->getAllocator(), pending.allocation, &data); } } else diff --git a/src/Engine/Graphics/Vulkan/Texture.cpp b/src/Engine/Graphics/Vulkan/Texture.cpp index 7725a27..1a5d302 100644 --- a/src/Engine/Graphics/Vulkan/Texture.cpp +++ b/src/Engine/Graphics/Vulkan/Texture.cpp @@ -247,7 +247,7 @@ void TextureBase::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra void* data; VkBuffer stagingBuffer = VK_NULL_HANDLE; VmaAllocation stagingAlloc; - + // always create a staging buffer since we do buffer -> image copy and the image may be in any tiling format VkBufferCreateInfo stagingInfo = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .pNext = nullptr, @@ -258,6 +258,7 @@ void TextureBase::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra }; VmaAllocationCreateInfo alloc = { .usage = VMA_MEMORY_USAGE_AUTO, + .flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT, }; VK_CHECK(vmaCreateBuffer(graphics->getAllocator(), &stagingInfo, &alloc, &stagingBuffer, &stagingAlloc, nullptr)); @@ -286,10 +287,11 @@ void TextureBase::download(uint32 mipLevel, uint32 arrayLayer, uint32 face, Arra }; vkCmdCopyImageToBuffer(cmdBuffer->getHandle(), image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, stagingBuffer, 1, ®ion); changeLayout(prevLayout); + VK_CHECK(vmaMapMemory(graphics->getAllocator(), stagingAlloc, &data)); buffer.resize(imageSize); std::memcpy(buffer.data(), data, buffer.size()); vmaUnmapMemory(graphics->getAllocator(), stagingAlloc); - vmaDestroyBuffer(graphics->getAllocator(), stagingBuffer, stagingAlloc); + graphics->getDestructionManager()->queueBuffer(cmdBuffer, stagingBuffer, stagingAlloc); } void TextureBase::executeOwnershipBarrier(Gfx::QueueType newOwner) diff --git a/src/Engine/Material/MaterialInstance.cpp b/src/Engine/Material/MaterialInstance.cpp index 04287f6..e184d87 100644 --- a/src/Engine/Material/MaterialInstance.cpp +++ b/src/Engine/Material/MaterialInstance.cpp @@ -50,6 +50,8 @@ MaterialInstance::~MaterialInstance() void MaterialInstance::updateDescriptor() { + if(!dirty) + return; Gfx::PDescriptorLayout layout = baseMaterial->getMaterial()->getDescriptorLayout(); descriptor = layout->allocateDescriptorSet(); for (auto& param : parameters) @@ -85,7 +87,6 @@ void MaterialInstance::setBaseMaterial(PMaterialAsset asset) ); } baseMaterial = asset; - updateDescriptor(); } void MaterialInstance::save(ArchiveBuffer& buffer) const diff --git a/src/Engine/Material/MaterialInstance.h b/src/Engine/Material/MaterialInstance.h index 578ce6e..3904c82 100644 --- a/src/Engine/Material/MaterialInstance.h +++ b/src/Engine/Material/MaterialInstance.h @@ -35,6 +35,7 @@ private: Gfx::PDescriptorSet descriptor; PMaterialAsset baseMaterial; uint64 id; + bool dirty = true; }; DEFINE_REF(MaterialInstance) } // namespace Seele \ No newline at end of file