From 64a26bfd57a35736c2f2e887872f2baf0a5489df Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Wed, 7 Aug 2024 21:19:33 +0200 Subject: [PATCH] Reducing memory usage --- res/shaders/BasePass.slang | 6 +- res/shaders/WaterMesh.slang | 0 res/shaders/WaterPass.slang | 0 res/shaders/WaterTask.slang | 0 src/Editor/Asset/TextureLoader.cpp | 4 +- src/Editor/main.cpp | 1 + src/Engine/Asset/Asset.h | 2 + src/Engine/Asset/AssetRegistry.cpp | 15 ++-- src/Engine/Asset/AssetRegistry.h | 2 +- src/Engine/Asset/MaterialAsset.cpp | 1 + src/Engine/Asset/MaterialInstanceAsset.cpp | 2 + src/Engine/Asset/MeshAsset.cpp | 7 +- src/Engine/Asset/TextureAsset.cpp | 2 + src/Engine/Graphics/Mesh.cpp | 10 ++- src/Engine/Graphics/Mesh.h | 4 + src/Engine/Graphics/RenderPass/BasePass.cpp | 3 - .../Graphics/RenderPass/CachedDepthPass.cpp | 2 +- .../Graphics/RenderPass/DepthCullingPass.cpp | 2 +- src/Engine/Graphics/StaticMeshVertexData.cpp | 76 ++++++++++++++++++- src/Engine/Graphics/StaticMeshVertexData.h | 6 +- src/Engine/Graphics/VertexData.cpp | 10 +-- src/Engine/Graphics/VertexData.h | 2 +- src/Engine/Graphics/Vulkan/Buffer.cpp | 13 +++- src/Engine/Graphics/Vulkan/Buffer.h | 4 +- src/Engine/Graphics/Vulkan/Command.cpp | 1 + src/Engine/Graphics/Vulkan/Descriptor.cpp | 4 +- src/Engine/Graphics/Vulkan/Graphics.cpp | 3 +- src/Engine/Graphics/Vulkan/Resources.cpp | 2 +- src/Engine/Graphics/Vulkan/Resources.h | 3 +- src/Engine/Graphics/Vulkan/Shader.cpp | 1 - src/Engine/Graphics/Vulkan/Texture.cpp | 5 +- src/Engine/Material/Material.cpp | 21 +++++ src/Engine/Material/Material.h | 3 + src/Engine/Material/MaterialInstance.cpp | 21 ++++- src/Engine/Material/MaterialInstance.h | 3 + src/Engine/Material/ShaderExpression.h | 14 ++++ src/Engine/Serialization/ArchiveBuffer.h | 5 ++ 37 files changed, 216 insertions(+), 44 deletions(-) create mode 100644 res/shaders/WaterMesh.slang create mode 100644 res/shaders/WaterPass.slang create mode 100644 res/shaders/WaterTask.slang diff --git a/res/shaders/BasePass.slang b/res/shaders/BasePass.slang index 219ab0c..4e7e73b 100644 --- a/res/shaders/BasePass.slang +++ b/res/shaders/BasePass.slang @@ -23,17 +23,17 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target float3 result = float3(0, 0, 0); for(int i = 0; i < pLightEnv.numDirectionalLights; ++i) { - result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); + //result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); } for(uint i = 0; i < pLightEnv.numPointLights; ++i) { //uint lightIndex = pLightCullingData.lightIndexList[startOffset + i]; - result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf); + //result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf); } result += brdf.evaluateAmbient(); // gamma correction //result = result / (result + float3(1.0)); //result = pow(result, float3(1.0/2.2)); - return float4(result, brdf.getAlpha()); + return float4(result, 1.0f); } \ No newline at end of file diff --git a/res/shaders/WaterMesh.slang b/res/shaders/WaterMesh.slang new file mode 100644 index 0000000..e69de29 diff --git a/res/shaders/WaterPass.slang b/res/shaders/WaterPass.slang new file mode 100644 index 0000000..e69de29 diff --git a/res/shaders/WaterTask.slang b/res/shaders/WaterTask.slang new file mode 100644 index 0000000..e69de29 diff --git a/src/Editor/Asset/TextureLoader.cpp b/src/Editor/Asset/TextureLoader.cpp index f233adc..80fef86 100644 --- a/src/Editor/Asset/TextureLoader.cpp +++ b/src/Editor/Asset/TextureLoader.cpp @@ -115,8 +115,8 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) { .uastcFlags = KTX_PACK_UASTC_LEVEL_DEFAULT, .uastcRDO = true, }; - //KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams)); - //KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20)); + KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams)); + KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 20)); char writer[100]; snprintf(writer, sizeof(writer), "%s version %s", "SeeleEngine", "0.0.1"); diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 789ec04..e90649b 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -13,6 +13,7 @@ #include "Graphics/StaticMeshVertexData.h" #include "Graphics/Vulkan/Graphics.h" #include "Window/PlayView.h" +#include "Graphics/Vulkan/Buffer.h" #include "Window/WindowManager.h" #include #include diff --git a/src/Engine/Asset/Asset.h b/src/Engine/Asset/Asset.h index bfa2108..81688ae 100644 --- a/src/Engine/Asset/Asset.h +++ b/src/Engine/Asset/Asset.h @@ -16,6 +16,8 @@ class Asset { virtual void save(ArchiveBuffer& buffer) const = 0; virtual void load(ArchiveBuffer& buffer) = 0; + constexpr uint64 getSize() const { return byteSize; } + bool isModified() const; // returns the assets name std::string getName() const; diff --git a/src/Engine/Asset/AssetRegistry.cpp b/src/Engine/Asset/AssetRegistry.cpp index b5aee9a..0aa02bf 100644 --- a/src/Engine/Asset/AssetRegistry.cpp +++ b/src/Engine/Asset/AssetRegistry.cpp @@ -195,18 +195,21 @@ void AssetRegistry::initialize(const std::filesystem::path& _rootFolder, Gfx::PG } void AssetRegistry::loadRegistryInternal() { - List> peeked; + Array> peeked; { std::unique_lock l(get().assetLock); peeked = peekFolder(assetRoot); } + uint64 assetSize = 0; for (auto& [asset, buffer] : peeked) { asset->load(buffer); + assetSize += asset->getSize(); } + std::cout << "Done loading " << assetSize << std::endl; } -List> AssetRegistry::peekFolder(AssetFolder* folder) { - List> peeked; +Array> AssetRegistry::peekFolder(AssetFolder* folder) { + Array> peeked; for (const auto& entry : std::filesystem::directory_iterator(rootFolder / folder->folderPath)) { const auto& stem = entry.path().stem().string(); if (entry.is_directory()) { @@ -216,8 +219,8 @@ List> AssetRegistry::peekFolder(AssetFolder* folder) folder->children[stem] = new AssetFolder(folder->folderPath + "/" + stem); } auto temp = peekFolder(folder->children[stem]); - for (auto t : temp) { - peeked.add(t); + for (auto& t : temp) { + peeked.add(std::move(t)); } continue; } @@ -273,7 +276,7 @@ Pair AssetRegistry::peekAsset(ArchiveBuffer& buffer) { default: throw new std::logic_error("Unknown Identifier"); } - return {asset, buffer}; + return {asset, std::move(buffer)}; } diff --git a/src/Engine/Asset/AssetRegistry.h b/src/Engine/Asset/AssetRegistry.h index 88fcd73..c6bea59 100644 --- a/src/Engine/Asset/AssetRegistry.h +++ b/src/Engine/Asset/AssetRegistry.h @@ -62,7 +62,7 @@ class AssetRegistry { private: void initialize(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics); void loadRegistryInternal(); - List> peekFolder(AssetFolder* folder); + Array> peekFolder(AssetFolder* folder); Pair peekAsset(ArchiveBuffer& buffer); void saveRegistryInternal(); void saveFolder(const std::filesystem::path& folderPath, AssetFolder* folder); diff --git a/src/Engine/Asset/MaterialAsset.cpp b/src/Engine/Asset/MaterialAsset.cpp index d19dc81..9a058a7 100644 --- a/src/Engine/Asset/MaterialAsset.cpp +++ b/src/Engine/Asset/MaterialAsset.cpp @@ -19,6 +19,7 @@ void MaterialAsset::load(ArchiveBuffer& buffer) { material = new Material(); material->load(buffer); material->compile(); + byteSize = material->getCPUSize(); } PMaterialInstanceAsset MaterialAsset::instantiate(const InstantiationParameter& params) { diff --git a/src/Engine/Asset/MaterialInstanceAsset.cpp b/src/Engine/Asset/MaterialInstanceAsset.cpp index 4c95f87..0bd8000 100644 --- a/src/Engine/Asset/MaterialInstanceAsset.cpp +++ b/src/Engine/Asset/MaterialInstanceAsset.cpp @@ -28,4 +28,6 @@ void MaterialInstanceAsset::load(ArchiveBuffer& buffer) { material->load(buffer); baseMaterial = AssetRegistry::findMaterial(folder, id); material->setBaseMaterial(baseMaterial); + + byteSize = material->getCPUSize(); } diff --git a/src/Engine/Asset/MeshAsset.cpp b/src/Engine/Asset/MeshAsset.cpp index d7a98e1..b290566 100644 --- a/src/Engine/Asset/MeshAsset.cpp +++ b/src/Engine/Asset/MeshAsset.cpp @@ -14,4 +14,9 @@ MeshAsset::~MeshAsset() {} void MeshAsset::save(ArchiveBuffer& buffer) const { Serialization::save(buffer, meshes); } -void MeshAsset::load(ArchiveBuffer& buffer) { Serialization::load(buffer, meshes); } +void MeshAsset::load(ArchiveBuffer& buffer) { Serialization::load(buffer, meshes); + byteSize = 0; + for (const auto& mesh : meshes) { + byteSize += mesh->getCPUSize(); + } +} diff --git a/src/Engine/Asset/TextureAsset.cpp b/src/Engine/Asset/TextureAsset.cpp index 3c92f57..492acc0 100644 --- a/src/Engine/Asset/TextureAsset.cpp +++ b/src/Engine/Asset/TextureAsset.cpp @@ -62,6 +62,8 @@ void TextureAsset::load(ArchiveBuffer& buffer) { } ktxTexture_Destroy(ktxTexture(ktxHandle)); + + byteSize = sizeof(TextureAsset) + ktxData.size(); } uint32 TextureAsset::getWidth() { return texture->getWidth(); } diff --git a/src/Engine/Graphics/Mesh.cpp b/src/Engine/Graphics/Mesh.cpp index 329c3a5..f564c47 100644 --- a/src/Engine/Graphics/Mesh.cpp +++ b/src/Engine/Graphics/Mesh.cpp @@ -29,9 +29,17 @@ void Mesh::load(ArchiveBuffer& buffer) { Serialization::load(buffer, refId); referencedMaterial = AssetRegistry::findMaterialInstance(refFolder, refId); id = vertexData->allocateVertexData(vertexCount); - vertexData->deserializeMesh(id, buffer); + byteSize = vertexData->deserializeMesh(id, buffer); blas = buffer.getGraphics()->createBottomLevelAccelerationStructure(Gfx::BottomLevelASCreateInfo{ .mesh = this, }); vertexData->registerBottomLevelAccelerationStructure(blas); } + +uint64 Mesh::getCPUSize() const { + uint64 result = sizeof(Mesh); + result += byteSize; + return result; +} + +uint64 Mesh::getGPUSize() const { return byteSize; } diff --git a/src/Engine/Graphics/Mesh.h b/src/Engine/Graphics/Mesh.h index 6ecbc6e..111d4e6 100644 --- a/src/Engine/Graphics/Mesh.h +++ b/src/Engine/Graphics/Mesh.h @@ -15,11 +15,15 @@ class Mesh { VertexData* vertexData; MeshId id; uint64 vertexCount; + uint64 byteSize; PMaterialInstanceAsset referencedMaterial; Gfx::OBottomLevelAS blas; void save(ArchiveBuffer& buffer) const; void load(ArchiveBuffer& buffer); + uint64 getCPUSize() const; + uint64 getGPUSize() const; + private: }; DEFINE_REF(Mesh) diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index d728784..4e844a6 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -242,9 +242,6 @@ void BasePass::render() { } } - graphics->executeCommands(std::move(commands)); - - commands.clear(); Gfx::ORenderCommand skyboxCommand = graphics->createRenderCommand("SkyboxRender"); skyboxCommand->setViewport(viewport); skyboxCommand->bindPipeline(pipeline); diff --git a/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp b/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp index 6fe28d4..75ee601 100644 --- a/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp +++ b/src/Engine/Graphics/RenderPass/CachedDepthPass.cpp @@ -194,5 +194,5 @@ void CachedDepthPass::createRenderPass() { Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, }, }; - renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport); + renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport, "CachedDepthPass"); } diff --git a/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp b/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp index ed887f9..fc91f06 100644 --- a/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp @@ -294,5 +294,5 @@ void DepthCullingPass::createRenderPass() { Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, }, }; - renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport); + renderPass = graphics->createRenderPass(std::move(layout), std::move(dependency), viewport, "DepthCullingPass"); } diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index 704519c..76c37b3 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -2,6 +2,7 @@ #include "Graphics.h" #include "Graphics/Enums.h" #include "Mesh.h" +#include using namespace Seele; @@ -17,36 +18,54 @@ StaticMeshVertexData* StaticMeshVertexData::getInstance() { } void StaticMeshVertexData::loadPositions(uint64 offset, const Array& data) { + if (swappedOut) { + swapIn(); + } assert(offset + data.size() <= head); std::memcpy(positionData.data() + offset, data.data(), data.size() * sizeof(Vector4)); dirty = true; } void StaticMeshVertexData::loadTexCoords(uint64 offset, uint64 index, const Array& data) { + if (swappedOut) { + swapIn(); + } assert(offset + data.size() <= head); std::memcpy(texCoordsData[index].data() + offset, data.data(), data.size() * sizeof(Vector2)); dirty = true; } void StaticMeshVertexData::loadNormals(uint64 offset, const Array& data) { + if (swappedOut) { + swapIn(); + } assert(offset + data.size() <= head); std::memcpy(normalData.data() + offset, data.data(), data.size() * sizeof(Vector4)); dirty = true; } void StaticMeshVertexData::loadTangents(uint64 offset, const Array& data) { + if (swappedOut) { + swapIn(); + } assert(offset + data.size() <= head); std::memcpy(tangentData.data() + offset, data.data(), data.size() * sizeof(Vector4)); dirty = true; } void StaticMeshVertexData::loadBiTangents(uint64 offset, const Array& data) { + if (swappedOut) { + swapIn(); + } assert(offset + data.size() <= head); std::memcpy(biTangentData.data() + offset, data.data(), data.size() * sizeof(Vector4)); dirty = true; } void Seele::StaticMeshVertexData::loadColors(uint64 offset, const Array& data) { + if (swappedOut) { + swapIn(); + } assert(offset + data.size() <= head); std::memcpy(colorData.data() + offset, data.data(), data.size() * sizeof(Vector4)); dirty = true; @@ -82,8 +101,8 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB Serialization::save(buffer, col); } -void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { - VertexData::deserializeMesh(id, buffer); +uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { + uint64 result = VertexData::deserializeMesh(id, buffer); uint64 offset; { std::unique_lock l(vertexDataLock); @@ -94,6 +113,7 @@ void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { Serialization::load(buffer, tex[i]); loadTexCoords(offset, i, tex[i]); + result += tex[i].size() * sizeof(Vector2); } Array nor; Array tan; @@ -109,6 +129,12 @@ void StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { loadTangents(offset, tan); loadBiTangents(offset, bit); loadColors(offset, col); + result += pos.size() * sizeof(Vector4); + result += nor.size() * sizeof(Vector4); + result += tan.size() * sizeof(Vector4); + result += bit.size() * sizeof(Vector4); + result += col.size() * sizeof(Vector4); + return result; } void StaticMeshVertexData::init(Gfx::PGraphics _graphics) { @@ -119,8 +145,11 @@ void StaticMeshVertexData::init(Gfx::PGraphics _graphics) { descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{.binding = 4, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER}); - descriptorLayout->addDescriptorBinding( - Gfx::DescriptorBinding{.binding = 5, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = MAX_TEXCOORDS,}); + descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 5, + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, + .descriptorCount = MAX_TEXCOORDS, + }); descriptorLayout->create(); descriptorSet = descriptorLayout->allocateDescriptorSet(); } @@ -135,6 +164,7 @@ void StaticMeshVertexData::destroy() { tangents = nullptr; biTangents = nullptr; colors = nullptr; + descriptorSet = nullptr; descriptorLayout = nullptr; } @@ -231,6 +261,8 @@ void StaticMeshVertexData::updateBuffers() { }, .numElements = colorData.size(), }); + // we just updated the GPU buffers, might not change that for a while + swapOut(); descriptorLayout->reset(); descriptorSet = descriptorLayout->allocateDescriptorSet(); descriptorSet->updateBuffer(0, positions); @@ -243,3 +275,39 @@ void StaticMeshVertexData::updateBuffers() { } descriptorSet->writeChanges(); } + +void StaticMeshVertexData::swapOut() { + ArchiveBuffer buf; + Serialization::save(buf, positionData); + positionData.clear(); + Serialization::save(buf, normalData); + normalData.clear(); + Serialization::save(buf, tangentData); + tangentData.clear(); + Serialization::save(buf, biTangentData); + biTangentData.clear(); + Serialization::save(buf, colorData); + colorData.clear(); + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { + Serialization::save(buf, texCoordsData[i]); + texCoordsData[i].clear(); + } + std::ofstream str("vertex", std::ios::binary); + buf.writeToStream(str); + swappedOut = true; +} + +void StaticMeshVertexData::swapIn() { + ArchiveBuffer buf; + std::ifstream str("vertex", std::ios::binary); + buf.readFromStream(str); + Serialization::load(buf, positionData); + Serialization::load(buf, normalData); + Serialization::load(buf, tangentData); + Serialization::load(buf, biTangentData); + Serialization::load(buf, colorData); + for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { + Serialization::load(buf, texCoordsData[i]); + } + swappedOut = false; +} diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index f43a69f..9d40686 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -19,7 +19,7 @@ class StaticMeshVertexData : public VertexData { void loadBiTangents(uint64 offset, const Array& data); void loadColors(uint64 offset, const Array& data); virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override; - virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer) override; + virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer) override; virtual void init(Gfx::PGraphics graphics) override; virtual void destroy() override; virtual void bindBuffers(Gfx::PRenderCommand command) override; @@ -32,6 +32,9 @@ class StaticMeshVertexData : public VertexData { virtual void resizeBuffers() override; virtual void updateBuffers() override; + void swapOut(); + void swapIn(); + Gfx::OShaderBuffer positions; Array positionData; Gfx::OShaderBuffer texCoords[MAX_TEXCOORDS]; @@ -44,6 +47,7 @@ class StaticMeshVertexData : public VertexData { Array biTangentData; Gfx::OShaderBuffer colors; Array colorData; + bool swappedOut = false; Gfx::ODescriptorLayout descriptorLayout; Gfx::PDescriptorSet descriptorSet; }; diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 8d80181..6acd271 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -51,7 +51,7 @@ void VertexData::updateMesh(entt::entity id, uint32 meshIndex, PMesh mesh, Compo auto [instanceId, meshletOffset] = getCullingMapping(id, meshIndex, data.numMeshlets); referencedInstance->updateDescriptor(); - if (mat->hasTransparency()) { + if (false && mat->hasTransparency()) { auto params = referencedInstance->getMaterialOffsets(); transparentData.add(TransparentDraw{ .matInst = referencedInstance, @@ -204,9 +204,6 @@ void VertexData::createDescriptors() { void VertexData::loadMesh(MeshId id, Array loadedIndices, Array loadedMeshlets) { std::unique_lock l(vertexDataLock); - meshlets.reserve(meshlets.size() + loadedMeshlets.size()); - vertexIndices.reserve(vertexIndices.size() + loadedMeshlets.size() * Gfx::numVerticesPerMeshlet); - primitiveIndices.reserve(primitiveIndices.size() + loadedMeshlets.size() * Gfx::numPrimitivesPerMeshlet * 3); uint32 meshletOffset = meshlets.size(); AABB meshAABB; for (uint32 i = 0; i < loadedMeshlets.size(); ++i) { @@ -321,12 +318,15 @@ void VertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buf Serialization::save(buffer, ind); } -void VertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { +uint64 VertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { Array in; Array ind; Serialization::load(buffer, in); Serialization::load(buffer, ind); loadMesh(id, ind, in); + uint64 result = in.size() * sizeof(MeshletDescription); + result += ind.size() * sizeof(uint32); + return result; } List vertexDataList; diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index 181eb81..f267d44 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -63,7 +63,7 @@ class VertexData { uint64 getMeshOffset(MeshId id) const { return meshOffsets[id]; } uint64 getMeshVertexCount(MeshId id) { return meshVertexCounts[id]; } virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer); - virtual void deserializeMesh(MeshId id, ArchiveBuffer& buffer); + virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer); virtual void bindBuffers(Gfx::PRenderCommand command) = 0; virtual Gfx::PDescriptorLayout getVertexDataLayout() = 0; virtual Gfx::PDescriptorSet getVertexDataSet() = 0; diff --git a/src/Engine/Graphics/Vulkan/Buffer.cpp b/src/Engine/Graphics/Vulkan/Buffer.cpp index 19c67a0..89b0adf 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.cpp +++ b/src/Engine/Graphics/Vulkan/Buffer.cpp @@ -5,12 +5,17 @@ #include #include +uint64 bufferSize = 0; + +uint64 getBufferSize() +{ return bufferSize; } + using namespace Seele; using namespace Seele::Vulkan; BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name, VkBufferCreateInfo bufferInfo, VmaAllocationCreateInfo allocInfo, Gfx::QueueType owner, uint64 alignment) - : CommandBoundResource(graphics), size(bufferInfo.size), name(name), owner(owner) { + : CommandBoundResource(graphics, name), size(bufferInfo.size), owner(owner) { VK_CHECK(vmaCreateBufferWithAlignment(graphics->getAllocator(), &bufferInfo, &allocInfo, alignment, &buffer, &allocation, &info)); vmaGetAllocationMemoryProperties(graphics->getAllocator(), allocation, &properties); VkDebugUtilsObjectNameInfoEXT nameInfo = { @@ -20,6 +25,9 @@ BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name, .objectHandle = (uint64)buffer, .pObjectName = name.c_str(), }; + if (!(properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) { + bufferSize += size; + } assert(!name.empty()); vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo); if (bufferInfo.usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) { @@ -34,6 +42,9 @@ BufferAllocation::BufferAllocation(PGraphics graphics, const std::string& name, BufferAllocation::~BufferAllocation() { if (buffer != VK_NULL_HANDLE) { + if (!(properties & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) { + bufferSize -= size; + } vmaDestroyBuffer(graphics->getAllocator(), buffer, allocation); } } diff --git a/src/Engine/Graphics/Vulkan/Buffer.h b/src/Engine/Graphics/Vulkan/Buffer.h index 6e2feab..3137b23 100644 --- a/src/Engine/Graphics/Vulkan/Buffer.h +++ b/src/Engine/Graphics/Vulkan/Buffer.h @@ -4,10 +4,13 @@ #include "Graphics/Enums.h" #include "Resources.h" +uint64 getBufferSize(); + namespace Seele { namespace Vulkan { DECLARE_REF(Command) DECLARE_REF(Fence) + class BufferAllocation : public CommandBoundResource { public: BufferAllocation(PGraphics graphics, const std::string& name, VkBufferCreateInfo bufferInfo, VmaAllocationCreateInfo allocInfo, @@ -24,7 +27,6 @@ class BufferAllocation : public CommandBoundResource { VmaAllocationInfo info = VmaAllocationInfo(); VkMemoryPropertyFlags properties = 0; uint64 size = 0; - std::string name; VkDeviceAddress deviceAddress; Gfx::QueueType owner; }; diff --git a/src/Engine/Graphics/Vulkan/Command.cpp b/src/Engine/Graphics/Vulkan/Command.cpp index bb85746..0b55cf7 100644 --- a/src/Engine/Graphics/Vulkan/Command.cpp +++ b/src/Engine/Graphics/Vulkan/Command.cpp @@ -464,6 +464,7 @@ CommandPool::CommandPool(PGraphics graphics, PQueue queue) : graphics(graphics), } CommandPool::~CommandPool() { + submitCommands(); vkDeviceWaitIdle(graphics->getDevice()); for (auto& cmd : allocatedBuffers) { cmd->checkFence(); diff --git a/src/Engine/Graphics/Vulkan/Descriptor.cpp b/src/Engine/Graphics/Vulkan/Descriptor.cpp index 8f71274..6e858cc 100644 --- a/src/Engine/Graphics/Vulkan/Descriptor.cpp +++ b/src/Engine/Graphics/Vulkan/Descriptor.cpp @@ -57,7 +57,7 @@ void DescriptorLayout::create() { } DescriptorPool::DescriptorPool(PGraphics graphics, PDescriptorLayout layout) - : CommandBoundResource(graphics), graphics(graphics), layout(layout) { + : CommandBoundResource(graphics, layout->getName()), graphics(graphics), layout(layout) { for (uint32 i = 0; i < cachedHandles.size(); ++i) { cachedHandles[i] = nullptr; } @@ -159,7 +159,7 @@ void DescriptorPool::reset() { } DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner) - : Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner){ + : Gfx::DescriptorSet(owner->getLayout()), CommandBoundResource(graphics, owner->getLayout()->getName()), setHandle(VK_NULL_HANDLE), graphics(graphics), owner(owner){ boundResources.resize(owner->getLayout()->getBindings().size()); for (uint32 i = 0; i < boundResources.size(); ++i) { diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index 7535afa..8972e4e 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -165,7 +165,8 @@ void Graphics::beginRenderPass(Gfx::PRenderPass renderPass) { void Graphics::endRenderPass() { getGraphicsCommands()->getCommands()->endRenderPass(); } -void Graphics::waitDeviceIdle() { +void Graphics::waitDeviceIdle() { + getGraphicsCommands()->submitCommands(); vkDeviceWaitIdle(handle); getGraphicsCommands()->refreshCommands(); } diff --git a/src/Engine/Graphics/Vulkan/Resources.cpp b/src/Engine/Graphics/Vulkan/Resources.cpp index 51526f8..82aaf56 100644 --- a/src/Engine/Graphics/Vulkan/Resources.cpp +++ b/src/Engine/Graphics/Vulkan/Resources.cpp @@ -92,7 +92,7 @@ void DestructionManager::notifyCommandComplete() { } } -SamplerHandle::SamplerHandle(PGraphics graphics, VkSamplerCreateInfo createInfo) : CommandBoundResource(graphics) { +SamplerHandle::SamplerHandle(PGraphics graphics, VkSamplerCreateInfo createInfo) : CommandBoundResource(graphics, "Sampler") { vkCreateSampler(graphics->getDevice(), &createInfo, nullptr, &sampler); } diff --git a/src/Engine/Graphics/Vulkan/Resources.h b/src/Engine/Graphics/Vulkan/Resources.h index c8523d3..b1ca748 100644 --- a/src/Engine/Graphics/Vulkan/Resources.h +++ b/src/Engine/Graphics/Vulkan/Resources.h @@ -61,7 +61,7 @@ DEFINE_REF(DestructionManager) class CommandBoundResource { public: - CommandBoundResource(PGraphics graphics) : graphics(graphics) {} + CommandBoundResource(PGraphics graphics, const std::string& name) : graphics(graphics), name(name) {} virtual ~CommandBoundResource() { if (isCurrentlyBound()) abort(); @@ -72,6 +72,7 @@ class CommandBoundResource { protected: PGraphics graphics; + std::string name; uint64 bindCount = 0; }; DEFINE_REF(CommandBoundResource) diff --git a/src/Engine/Graphics/Vulkan/Shader.cpp b/src/Engine/Graphics/Vulkan/Shader.cpp index 2315228..91213cd 100644 --- a/src/Engine/Graphics/Vulkan/Shader.cpp +++ b/src/Engine/Graphics/Vulkan/Shader.cpp @@ -6,7 +6,6 @@ #include "stdlib.h" #include - using namespace Seele; using namespace Seele::Vulkan; diff --git a/src/Engine/Graphics/Vulkan/Texture.cpp b/src/Engine/Graphics/Vulkan/Texture.cpp index 88223e9..4847c55 100644 --- a/src/Engine/Graphics/Vulkan/Texture.cpp +++ b/src/Engine/Graphics/Vulkan/Texture.cpp @@ -5,6 +5,7 @@ #include "Graphics/Initializer.h" #include #include +#include using namespace Seele; using namespace Seele::Vulkan; @@ -28,7 +29,7 @@ VkImageAspectFlags getAspectFromFormat(Gfx::SeFormat format) { } TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const TextureCreateInfo& createInfo, VkImage existingImage) - : CommandBoundResource(graphics), image(existingImage), width(createInfo.width), height(createInfo.height), depth(createInfo.depth), + : CommandBoundResource(graphics, createInfo.name), image(existingImage), width(createInfo.width), height(createInfo.height), depth(createInfo.depth), arrayCount(createInfo.elements), layerCount(createInfo.layers), mipLevels(createInfo.mipLevels), samples(createInfo.samples), format(createInfo.format), usage(createInfo.usage), layout(Gfx::SE_IMAGE_LAYOUT_UNDEFINED), aspect(getAspectFromFormat(createInfo.format)), ownsImage(false), owner(createInfo.sourceData.owner) { @@ -102,7 +103,7 @@ TextureHandle::TextureHandle(PGraphics graphics, VkImageViewType viewType, const .flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT, .usage = VMA_MEMORY_USAGE_AUTO, }; - OBufferAllocation stagingAlloc = new BufferAllocation(graphics, createInfo.name, stagingInfo, alloc, Gfx::QueueType::TRANSFER); + OBufferAllocation stagingAlloc = new BufferAllocation(graphics, fmt::format("{}Staging", createInfo.name), stagingInfo, alloc, Gfx::QueueType::TRANSFER); vmaMapMemory(graphics->getAllocator(), stagingAlloc->allocation, &data); std::memcpy(data, sourceData.data, sourceData.size); vmaUnmapMemory(graphics->getAllocator(), stagingAlloc->allocation); diff --git a/src/Engine/Material/Material.cpp b/src/Engine/Material/Material.cpp index 5ed5eed..bbf6191 100644 --- a/src/Engine/Material/Material.cpp +++ b/src/Engine/Material/Material.cpp @@ -181,3 +181,24 @@ void Material::compile() { codeStream << "};\n"; graphics->getShaderCompiler()->registerMaterial(this); } + +uint64 Material::getCPUSize() const { + uint64 result = sizeof(Material); + for (size_t i = 0; i < parameters.size(); ++i) { + result += parameters[i].size(); + } + for (const auto& expr : codeExpressions) + { + result += expr->getCPUSize(); + } + return result; +} + +uint64 Material::getGPUSize() const +{ + uint64 result = 0; + for (const auto& expr : codeExpressions) { + result += expr->getGPUSize(); + } + return result; +} diff --git a/src/Engine/Material/Material.h b/src/Engine/Material/Material.h index 175b4e2..f15a942 100644 --- a/src/Engine/Material/Material.h +++ b/src/Engine/Material/Material.h @@ -40,6 +40,9 @@ class Material { void compile(); + uint64 getCPUSize() const; + uint64 getGPUSize() const; + private: Gfx::PGraphics graphics; uint32 numTextures; diff --git a/src/Engine/Material/MaterialInstance.cpp b/src/Engine/Material/MaterialInstance.cpp index 5a2052e..b690aa0 100644 --- a/src/Engine/Material/MaterialInstance.cpp +++ b/src/Engine/Material/MaterialInstance.cpp @@ -2,7 +2,6 @@ #include "Graphics/Graphics.h" #include "Material.h" - using namespace Seele; MaterialInstance::MaterialInstance() {} @@ -34,9 +33,7 @@ void MaterialInstance::updateDescriptor() { } } -void MaterialInstance::setBaseMaterial(PMaterialAsset asset) { - baseMaterial = asset; -} +void MaterialInstance::setBaseMaterial(PMaterialAsset asset) { baseMaterial = asset; } void MaterialInstance::save(ArchiveBuffer& buffer) const { Serialization::save(buffer, numTextures); @@ -56,3 +53,19 @@ void MaterialInstance::load(ArchiveBuffer& buffer) { samplersOffset = Material::addSamplers(numSamplers); floatBufferOffset = Material::addFloats(numFloats); } + +uint64 MaterialInstance::getCPUSize() const { + uint64 result = sizeof(MaterialInstance); + for (size_t i = 0; i < parameters.size(); ++i) { + result += parameters[i]->getCPUSize(); + } + return result; +} + +uint64 MaterialInstance::getGPUSize() const { + uint64 result = 0; + for (size_t i = 0; i < parameters.size(); ++i) { + result += parameters[i]->getGPUSize(); + } + return result; +} diff --git a/src/Engine/Material/MaterialInstance.h b/src/Engine/Material/MaterialInstance.h index e7093a7..e33b8aa 100644 --- a/src/Engine/Material/MaterialInstance.h +++ b/src/Engine/Material/MaterialInstance.h @@ -31,6 +31,9 @@ class MaterialInstance { void save(ArchiveBuffer& buffer) const; void load(ArchiveBuffer& buffer); + uint64 getCPUSize() const; + uint64 getGPUSize() const; + private: Gfx::PGraphics graphics; Array parameters; diff --git a/src/Engine/Material/ShaderExpression.h b/src/Engine/Material/ShaderExpression.h index 2547284..bcfdae4 100644 --- a/src/Engine/Material/ShaderExpression.h +++ b/src/Engine/Material/ShaderExpression.h @@ -37,6 +37,8 @@ class ShaderExpression { ShaderExpression(std::string key); virtual ~ShaderExpression(); virtual uint64 getIdentifier() const = 0; + virtual uint64 getCPUSize() const { return 0; }; + virtual uint64 getGPUSize() const { return 0; }; virtual std::string evaluate(Map& varState) const = 0; virtual void save(ArchiveBuffer& buffer) const; virtual void load(ArchiveBuffer& buffer); @@ -50,6 +52,8 @@ struct ShaderParameter : public ShaderExpression { virtual ~ShaderParameter(); virtual void updateDescriptorSet(uint32 textureOffset, uint32 samplerOffset, uint32 floatOffset) = 0; virtual uint64 getIdentifier() const override = 0; + virtual uint64 getCPUSize() const override = 0; + virtual uint64 getGPUSize() const override = 0; virtual std::string evaluate(Map& varState) const = 0; virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; @@ -64,6 +68,8 @@ struct FloatParameter : public ShaderParameter { virtual void updateDescriptorSet(uint32 textureOffset, uint32 samplerOffset, uint32 floatOffset) override; virtual std::string evaluate(Map& varState) const override; virtual uint64 getIdentifier() const override { return IDENTIFIER; } + virtual uint64 getCPUSize() const { return sizeof(FloatParameter); } + virtual uint64 getGPUSize() const { return sizeof(float); } virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; }; @@ -77,6 +83,8 @@ struct VectorParameter : public ShaderParameter { virtual void updateDescriptorSet(uint32 textureOffset, uint32 samplerOffset, uint32 floatOffset) override; virtual std::string evaluate(Map& varState) const override; virtual uint64 getIdentifier() const override { return IDENTIFIER; } + virtual uint64 getCPUSize() const { return sizeof(VectorParameter); } + virtual uint64 getGPUSize() const { return sizeof(Vector); } virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; }; @@ -90,6 +98,8 @@ struct TextureParameter : public ShaderParameter { virtual void updateDescriptorSet(uint32 textureOffset, uint32 samplerOffset, uint32 floatOffset) override; virtual std::string evaluate(Map& varState) const override; virtual uint64 getIdentifier() const override { return IDENTIFIER; } + virtual uint64 getCPUSize() const { return sizeof(TextureParameter); } + virtual uint64 getGPUSize() const { return sizeof(void*); } // TODO: technically this is just a ref, but idk virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; }; @@ -104,6 +114,8 @@ struct SamplerParameter : public ShaderParameter { virtual void updateDescriptorSet(uint32 textureOffset, uint32 samplerOffset, uint32 floatOffset) override; virtual std::string evaluate(Map& varState) const override; virtual uint64 getIdentifier() const override { return IDENTIFIER; } + virtual uint64 getCPUSize() const { return sizeof(SamplerParameter); } + virtual uint64 getGPUSize() const { return sizeof(void*); } virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; }; @@ -118,6 +130,8 @@ struct CombinedTextureParameter : public ShaderParameter { virtual void updateDescriptorSet(uint32 textureOffset, uint32 samplerOffset, uint32 floatOffset) override; virtual std::string evaluate(Map& varState) const override; virtual uint64 getIdentifier() const override { return IDENTIFIER; } + virtual uint64 getCPUSize() const { return sizeof(CombinedTextureParameter); } + virtual uint64 getGPUSize() const { return sizeof(void*); } virtual void save(ArchiveBuffer& buffer) const override; virtual void load(ArchiveBuffer& buffer) override; }; diff --git a/src/Engine/Serialization/ArchiveBuffer.h b/src/Engine/Serialization/ArchiveBuffer.h index 9406aa0..962ac91 100644 --- a/src/Engine/Serialization/ArchiveBuffer.h +++ b/src/Engine/Serialization/ArchiveBuffer.h @@ -11,6 +11,11 @@ class ArchiveBuffer { public: ArchiveBuffer(); ArchiveBuffer(Gfx::PGraphics graphics); + ArchiveBuffer(const ArchiveBuffer& other) = delete; + ArchiveBuffer(ArchiveBuffer&& other) = default; + ArchiveBuffer& operator=(const ArchiveBuffer& other) = delete; + ArchiveBuffer& operator=(ArchiveBuffer&& other) = default; + ~ArchiveBuffer() {} void writeBytes(const void* data, uint64 size); void readBytes(void* dest, uint64 size); void writeToStream(std::ostream& stream);