[WIP] Fixed texture load corruption issue.

This commit is contained in:
Anish Bhobe 2025-04-02 00:27:36 +02:00
parent 3ab9d838fa
commit aa729610cf
6 changed files with 41 additions and 31 deletions

View File

@ -32,6 +32,10 @@ struct Sampler
{
const auto rc = --m_RefCount;
assert(rc < MaxValue<u32>);
if (rc == 0)
{
Destroy();
}
}
[[nodiscard]] bool

View File

@ -53,6 +53,7 @@ class Manager
m_Device = nullptr;
}
// TODO: Work on deletion!!
void
Sweep()
requires concepts::DeviceDestructible<Type>

View File

@ -14,9 +14,9 @@ BufferManager::CreateStorageBuffer(const usize size, const cstr name)
// TODO: Storage and Index buffer are set.
// This is hacky and should be improved.
constexpr vk::BufferUsageFlags usage = vk::BufferUsageFlagBits::eStorageBuffer |
vk::BufferUsageFlagBits::eIndexBuffer |
vk::BufferUsageFlagBits::eShaderDeviceAddress;
constexpr vk::BufferUsageFlags usage =
vk::BufferUsageFlagBits::eStorageBuffer | vk::BufferUsageFlagBits::eIndexBuffer |
vk::BufferUsageFlagBits::eShaderDeviceAddress | vk::BufferUsageFlagBits::eTransferDst;
constexpr VmaAllocationCreateFlags createFlags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
VMA_ALLOCATION_CREATE_HOST_ACCESS_ALLOW_TRANSFER_INSTEAD_BIT |
VMA_ALLOCATION_CREATE_MAPPED_BIT;

View File

@ -391,6 +391,7 @@ AssetLoader::LoadImageToGpu(tinygltf::Image *image, bool isSrgb) const
.m_IsMipMapped = true,
.m_IsStorage = false,
});
auto stagingBuffer = m_ResourceManager->Buffers().CreateStagingBuffer(byteSize);
stagingBuffer->Write(0, byteSize, image->image.data());
@ -434,7 +435,9 @@ AssetLoader::LoadImageToGpu(tinygltf::Image *image, bool isSrgb) const
vk::ImageMemoryBarrier2 postStagingBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eAllTransfer,
.srcAccessMask = vk::AccessFlagBits2::eTransferWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eAllTransfer,
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
.srcQueueFamilyIndex = m_TransferQueueIndex,
@ -550,7 +553,7 @@ AssetLoader::LoadModelToGpu(cstr path, cstr name)
if (!model.materials.empty())
{
// TODO("Something broken on load here.");
auto getTextureHandle = [this, &textureHandleMap, &model](i32 index,
auto getTextureHandle = [this, &textureHandleMap, &stagingBuffers, &model](i32 index,
const bool isSrgb) -> systems::ResId<TextureView> {
if (index < 0)
{
@ -564,6 +567,7 @@ AssetLoader::LoadModelToGpu(cstr path, cstr name)
auto *image = &model.images[index];
auto [handle, staging] = LoadImageToGpu(image, isSrgb);
textureHandleMap.emplace(index, handle);
stagingBuffers.emplace_back(std::move(staging));
return handle;
};

View File

@ -343,7 +343,7 @@ CreateCubeFromHdrEnv(AssetLoader *assetLoader, vk::Queue computeQueue, const u32
{
prefilterPushConstants.m_OutputTexture = tex;
prefilterPushConstants.m_CubeSide = mipSize;
prefilterPushConstants.m_Roughness = Cast<f32>(mipCount) / Cast<f32>(prefilterMipCountMax);
prefilterPushConstants.m_Roughness = Cast<f32>(mipCount) / Cast<f32>(prefilterMipCountMax - 1);
cmd.pushConstants(pipelineLayout, vk::ShaderStageFlagBits::eCompute, 0, sizeof prefilterPushConstants,
&prefilterPushConstants);
u32 groupCount = eastl::max(mipSize / 16u, 1u);
@ -356,6 +356,7 @@ CreateCubeFromHdrEnv(AssetLoader *assetLoader, vk::Queue computeQueue, const u32
cmd.pipelineBarrier2(&skyboxToSampleDependency);
cmd.pipelineBarrier2(&prefilterToSampleDependency);
TODO("BRDF LUT has a black line at x=0. Needs fixing");
cmd.bindPipeline(vk::PipelineBindPoint::eCompute, brdfLutPipeline);
cmd.pushConstants(pipelineLayout, vk::ShaderStageFlagBits::eCompute, 0, sizeof brdfLutPushConstants,
&brdfLutPushConstants);

View File

@ -179,7 +179,7 @@ main(int, char **)
Swapchain swapchain = {&surface, &device, window.GetSize(), "Primary Chain"};
systems::ResourceManager resourceManager = {&device, 1000, 1000, 10, 1000};
systems::CommitManager commitManager = {&device, 1000, 1000, 1000, resourceManager.Samplers().CreateSampler({})};
systems::CommitManager commitManager = {&device, 1000, 1000, 1000, resourceManager.Samplers().CreateSampler({.m_Name = "Default Sampler"})};
AssetLoader assetLoader = {&resourceManager, &commitManager, graphicsQueue, queueAllocation.m_Family,
queueAllocation.m_Family};
@ -304,14 +304,14 @@ main(int, char **)
};
vk::ImageMemoryBarrier2 preRenderBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
.srcAccessMask = vk::AccessFlagBits2::eNone,
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
.srcAccessMask = vk::AccessFlagBits2::eTransferRead,
.dstStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
.dstAccessMask = vk::AccessFlagBits2::eColorAttachmentWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
.srcQueueFamilyIndex = queueAllocation.m_Family,
.dstQueueFamilyIndex = queueAllocation.m_Family,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.subresourceRange = subresourceRange,
};
vk::DependencyInfo preRenderDependencies = {
@ -322,23 +322,23 @@ main(int, char **)
vk::ImageMemoryBarrier2 renderToBlitBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
.srcAccessMask = vk::AccessFlagBits2::eColorAttachmentWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eAllTransfer,
.dstStageMask = vk::PipelineStageFlagBits2::eBlit,
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
.srcQueueFamilyIndex = queueAllocation.m_Family,
.dstQueueFamilyIndex = queueAllocation.m_Family,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.subresourceRange = subresourceRange,
};
vk::ImageMemoryBarrier2 acquireToTransferDstBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eTopOfPipe,
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
.srcAccessMask = vk::AccessFlagBits2::eNone,
.dstStageMask = vk::PipelineStageFlagBits2::eAllTransfer,
.dstStageMask = vk::PipelineStageFlagBits2::eBlit,
.dstAccessMask = vk::AccessFlagBits2::eTransferWrite,
.oldLayout = vk::ImageLayout::eUndefined,
.newLayout = vk::ImageLayout::eTransferDstOptimal,
.srcQueueFamilyIndex = queueAllocation.m_Family,
.dstQueueFamilyIndex = queueAllocation.m_Family,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.subresourceRange = subresourceRange,
};
eastl::array postRenderBarriers = {
@ -351,15 +351,15 @@ main(int, char **)
};
vk::ImageMemoryBarrier2 transferDstToGuiRenderBarrier = {
.srcStageMask = vk::PipelineStageFlagBits2::eAllTransfer,
.srcAccessMask = vk::AccessFlagBits2::eTransferWrite,
.dstStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
.dstAccessMask = vk::AccessFlagBits2::eColorAttachmentWrite,
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
.srcQueueFamilyIndex = queueAllocation.m_Family,
.dstQueueFamilyIndex = queueAllocation.m_Family,
.subresourceRange = subresourceRange,
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
.srcAccessMask = vk::AccessFlagBits2::eTransferWrite | vk::AccessFlagBits2::eTransferRead,
.dstStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
.dstAccessMask = vk::AccessFlagBits2::eColorAttachmentRead,
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.subresourceRange = subresourceRange,
};
vk::DependencyInfo preGuiDependencies = {
.imageMemoryBarrierCount = 1,
@ -373,8 +373,8 @@ main(int, char **)
.dstAccessMask = vk::AccessFlagBits2::eNone,
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
.newLayout = vk::ImageLayout::ePresentSrcKHR,
.srcQueueFamilyIndex = queueAllocation.m_Family,
.dstQueueFamilyIndex = queueAllocation.m_Family,
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
.subresourceRange = subresourceRange,
};
vk::DependencyInfo prePresentDependencies = {
@ -523,8 +523,8 @@ main(int, char **)
vk::ImageView currentSwapchainImageView = swapchain.m_ImageViews[imageIndex];
vk::CommandBuffer cmd = currentFrame->m_CommandBuffer;
auto& currentDepthImage = depthImages[currentFrame->m_FrameIdx];
auto& currentAttachment = attachmentImages[currentFrame->m_FrameIdx];
auto &currentDepthImage = depthImages[currentFrame->m_FrameIdx];
auto &currentAttachment = attachmentImages[currentFrame->m_FrameIdx];
if (currentAttachment->m_Extent.width != internalResolution.width ||
currentAttachment->m_Extent.height != internalResolution.height)
@ -682,7 +682,7 @@ main(int, char **)
AbortIfFailed(cmd.end());
vk::PipelineStageFlags waitDstStage = vk::PipelineStageFlagBits::eColorAttachmentOutput;
vk::PipelineStageFlags waitDstStage = vk::PipelineStageFlagBits::eTransfer;
vk::SubmitInfo submitInfo = {
.waitSemaphoreCount = 1,
.pWaitSemaphores = &currentFrame->m_ImageAcquireSem,