Reducing memory usage
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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 <fmt/core.h>
|
||||
#include <random>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -195,18 +195,21 @@ void AssetRegistry::initialize(const std::filesystem::path& _rootFolder, Gfx::PG
|
||||
}
|
||||
|
||||
void AssetRegistry::loadRegistryInternal() {
|
||||
List<Pair<PAsset, ArchiveBuffer>> peeked;
|
||||
Array<Pair<PAsset, ArchiveBuffer>> 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<Pair<PAsset, ArchiveBuffer>> AssetRegistry::peekFolder(AssetFolder* folder) {
|
||||
List<Pair<PAsset, ArchiveBuffer>> peeked;
|
||||
Array<Pair<PAsset, ArchiveBuffer>> AssetRegistry::peekFolder(AssetFolder* folder) {
|
||||
Array<Pair<PAsset, ArchiveBuffer>> 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<Pair<PAsset, ArchiveBuffer>> 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<PAsset, ArchiveBuffer> AssetRegistry::peekAsset(ArchiveBuffer& buffer) {
|
||||
default:
|
||||
throw new std::logic_error("Unknown Identifier");
|
||||
}
|
||||
return {asset, buffer};
|
||||
return {asset, std::move(buffer)};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ class AssetRegistry {
|
||||
private:
|
||||
void initialize(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics);
|
||||
void loadRegistryInternal();
|
||||
List<Pair<PAsset, ArchiveBuffer>> peekFolder(AssetFolder* folder);
|
||||
Array<Pair<PAsset, ArchiveBuffer>> peekFolder(AssetFolder* folder);
|
||||
Pair<PAsset, ArchiveBuffer> peekAsset(ArchiveBuffer& buffer);
|
||||
void saveRegistryInternal();
|
||||
void saveFolder(const std::filesystem::path& folderPath, AssetFolder* folder);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -28,4 +28,6 @@ void MaterialInstanceAsset::load(ArchiveBuffer& buffer) {
|
||||
material->load(buffer);
|
||||
baseMaterial = AssetRegistry::findMaterial(folder, id);
|
||||
material->setBaseMaterial(baseMaterial);
|
||||
|
||||
byteSize = material->getCPUSize();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ void TextureAsset::load(ArchiveBuffer& buffer) {
|
||||
}
|
||||
|
||||
ktxTexture_Destroy(ktxTexture(ktxHandle));
|
||||
|
||||
byteSize = sizeof(TextureAsset) + ktxData.size();
|
||||
}
|
||||
|
||||
uint32 TextureAsset::getWidth() { return texture->getWidth(); }
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Graphics.h"
|
||||
#include "Graphics/Enums.h"
|
||||
#include "Mesh.h"
|
||||
#include <fstream>
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
@@ -17,36 +18,54 @@ StaticMeshVertexData* StaticMeshVertexData::getInstance() {
|
||||
}
|
||||
|
||||
void StaticMeshVertexData::loadPositions(uint64 offset, const Array<Vector4>& 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<Vector2>& 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<Vector4>& 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<Vector4>& 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<Vector4>& 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<Vector4>& 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<Vector4> nor;
|
||||
Array<Vector4> 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;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class StaticMeshVertexData : public VertexData {
|
||||
void loadBiTangents(uint64 offset, const Array<Vector4>& data);
|
||||
void loadColors(uint64 offset, const Array<Vector4>& 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<Vector4> positionData;
|
||||
Gfx::OShaderBuffer texCoords[MAX_TEXCOORDS];
|
||||
@@ -44,6 +47,7 @@ class StaticMeshVertexData : public VertexData {
|
||||
Array<Vector4> biTangentData;
|
||||
Gfx::OShaderBuffer colors;
|
||||
Array<Vector4> colorData;
|
||||
bool swappedOut = false;
|
||||
Gfx::ODescriptorLayout descriptorLayout;
|
||||
Gfx::PDescriptorSet descriptorSet;
|
||||
};
|
||||
|
||||
@@ -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<uint32> loadedIndices, Array<Meshlet> 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<Meshlet> in;
|
||||
Array<uint32> 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<VertexData*> vertexDataList;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -5,12 +5,17 @@
|
||||
#include <vma/vk_mem_alloc.h>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -464,6 +464,7 @@ CommandPool::CommandPool(PGraphics graphics, PQueue queue) : graphics(graphics),
|
||||
}
|
||||
|
||||
CommandPool::~CommandPool() {
|
||||
submitCommands();
|
||||
vkDeviceWaitIdle(graphics->getDevice());
|
||||
for (auto& cmd : allocatedBuffers) {
|
||||
cmd->checkFence();
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "stdlib.h"
|
||||
#include <fmt/core.h>
|
||||
|
||||
|
||||
using namespace Seele;
|
||||
using namespace Seele::Vulkan;
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "Graphics/Initializer.h"
|
||||
#include <math.h>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
#include <fmt/format.h>
|
||||
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ class Material {
|
||||
|
||||
void compile();
|
||||
|
||||
uint64 getCPUSize() const;
|
||||
uint64 getGPUSize() const;
|
||||
|
||||
private:
|
||||
Gfx::PGraphics graphics;
|
||||
uint32 numTextures;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<OShaderParameter> parameters;
|
||||
|
||||
@@ -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<std::string, std::string>& 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<std::string, std::string>& 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<std::string, std::string>& 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<std::string, std::string>& 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<std::string, std::string>& 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<std::string, std::string>& 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<std::string, std::string>& 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;
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user