Reworks texture staging and fixes ID being at zero for certian constructors

This commit is contained in:
2024-06-23 09:14:56 -04:00
parent 8e2fa623dd
commit 4c0dc79a4a
2 changed files with 46 additions and 9 deletions

View File

@@ -9,21 +9,26 @@
#include "engine/FrameInfo.hpp"
#include "engine/buffers/BufferSuballocation.hpp"
#include "engine/descriptors/DescriptorSet.hpp"
#include "engine/image/Image.hpp"
#include "engine/image/ImageView.hpp"
#include "engine/logging/logging.hpp"
#include "engine/math/noise/perlin/generator.hpp"
#include "objectloaders/stb_image.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wconversion"
#include "engine/math/noise/perlin/generator.hpp"
#include "imgui/backends/imgui_impl_vulkan.h"
#pragma GCC diagnostic pop
#include "engine/logging/logging.hpp"
namespace fgl::engine
{
std::uint64_t getNextID()
{
static std::uint64_t id { 0 };
return id++;
}
std::tuple< std::vector< std::byte >, int, int, vk::Format >
loadTexture( const std::filesystem::path& path, const vk::Format format )
@@ -53,6 +58,12 @@ namespace fgl::engine
void Texture::drawImGui( vk::Extent2D extent )
{
if ( !isReady() )
{
log::debug( "Unable to draw Image {}. Image not ready", this->getID() );
return;
}
if ( m_imgui_set == VK_NULL_HANDLE ) createImGuiSet();
if ( extent == vk::Extent2D() )
@@ -95,10 +106,10 @@ namespace fgl::engine
{}
Texture::Texture( const std::vector< std::byte >& data, const vk::Extent2D extent, const vk::Format format ) :
m_extent( extent )
m_extent( extent ),
m_texture_id( getNextID() )
{
ZoneScoped;
static TextureID tex_counter { 0 };
auto image = std::make_shared< Image >(
extent,
@@ -109,8 +120,6 @@ namespace fgl::engine
m_image_view = image->getView();
m_texture_id = tex_counter++;
m_staging = std::make_unique< BufferSuballocation >( getGlobalStagingBuffer(), data.size() );
//Copy data info buffer
std::memcpy( reinterpret_cast< unsigned char* >( m_staging->ptr() ), data.data(), data.size() );
@@ -125,6 +134,18 @@ namespace fgl::engine
if ( m_imgui_set != VK_NULL_HANDLE ) ImGui_ImplVulkan_RemoveTexture( m_imgui_set );
}
void Texture::stage()
{
auto command_buffer { Device::getInstance().beginSingleTimeCommands() };
stage( command_buffer );
Device::getInstance().endSingleTimeCommands( command_buffer );
setReady();
m_staging.reset();
}
void Texture::stage( vk::raii::CommandBuffer& cmd )
{
ZoneScoped;
@@ -200,8 +221,7 @@ namespace fgl::engine
void Texture::dropStaging()
{
assert( m_staging );
m_staging.reset();
if ( m_staging ) m_staging.reset();
}
vk::DescriptorImageInfo Texture::getDescriptor() const
@@ -222,7 +242,14 @@ namespace fgl::engine
void Texture::createImGuiSet()
{
if ( !this->isReady() )
{
log::debug( "Unable to create ImGui set. Texture was not ready" );
return;
}
#if ENABLE_IMGUI
log::debug( "Created ImGui set for image ID {}", this->getID() );
if ( m_imgui_set != VK_NULL_HANDLE ) return;
auto& view { m_image_view };
@@ -245,6 +272,12 @@ namespace fgl::engine
return m_imgui_set;
}
Texture::Texture( Image& image, Sampler sampler ) : m_image_view( image.getView() ), m_texture_id( getNextID() )
{
m_image_view->getSampler() = std::move( sampler );
setReady();
}
TextureID Texture::getID() const
{
assert( !m_staging );

View File

@@ -59,12 +59,16 @@ namespace fgl::engine
~Texture();
void stage();
Texture( const Texture& ) = delete;
Texture& operator=( const Texture& ) = delete;
Texture( Texture&& other ) = delete;
Texture& operator=( Texture&& ) = delete;
Texture( Image& image, Sampler sampler = Sampler() );
[[nodiscard]] TextureID getID() const;
[[nodiscard]] vk::DescriptorImageInfo getDescriptor() const;