diff --git a/internal/Dx/ContextDX.cpp b/internal/Dx/ContextDX.cpp index 74c99b57..7ea1a176 100644 --- a/internal/Dx/ContextDX.cpp +++ b/internal/Dx/ContextDX.cpp @@ -74,7 +74,7 @@ void Ray::Dx::Context::Destroy() { DestroyDeferredResources(i); } - default_memory_allocs_ = {}; + default_mem_allocs_ = {}; staging_descr_alloc_ = {}; SAFE_RELEASE(command_queue_); @@ -356,9 +356,9 @@ bool Ray::Dx::Context::Init(ILog *log, const char *preferred_device, const int v subgroup_supported_ = (options1.WaveOps == TRUE); } - default_memory_allocs_ = - std::make_unique("Default Allocs", this, 32 * 1024 * 1024 /* initial_pool_size */, - 1.5f /* growth_factor */, 128 * 1024 * 1024 /* max_pool_size */); + default_mem_allocs_ = + std::make_unique("Default Allocs", this, 32 * 1024 * 1024 /* initial_pool_size */, + 1.5f /* growth_factor */, 128 * 1024 * 1024 /* max_pool_size */); staging_descr_alloc_ = std::make_unique>(this, false, 16 * 1024); for (int i = 0; i < MaxFramesInFlight; ++i) { diff --git a/internal/Dx/ContextDX.h b/internal/Dx/ContextDX.h index 9b472bb9..0fdf4053 100644 --- a/internal/Dx/ContextDX.h +++ b/internal/Dx/ContextDX.h @@ -34,7 +34,7 @@ class Buffer; class BumpAlloc; class FreelistAllocAdapted; template class DescrMultiPoolAlloc; -class MemoryAllocators; +class MemAllocators; using CommandBuffer = ID3D12GraphicsCommandList *; @@ -81,7 +81,7 @@ class Context { uint32_t max_combined_image_samplers_ = 0, max_sampled_images_ = 0, max_samplers_ = 0; - std::unique_ptr default_memory_allocs_; + std::unique_ptr default_mem_allocs_; std::unique_ptr> default_descr_alloc_[MaxFramesInFlight]; std::unique_ptr> staging_descr_alloc_; @@ -132,7 +132,7 @@ class Context { HANDLE fence_event() const { return fence_event_; } ID3D12QueryHeap *query_heap(const int i) const { return query_heaps_[i]; } - MemoryAllocators *default_memory_allocs() { return default_memory_allocs_.get(); } + MemAllocators *default_mem_allocs() { return default_mem_allocs_.get(); } DescrMultiPoolAlloc *default_descr_alloc() { return default_descr_alloc_[backend_frame].get(); } DescrMultiPoolAlloc *staging_descr_alloc() { return staging_descr_alloc_.get(); } diff --git a/internal/Dx/MemoryAllocatorDX.cpp b/internal/Dx/MemoryAllocatorDX.cpp index a34aace3..0754c69d 100644 --- a/internal/Dx/MemoryAllocatorDX.cpp +++ b/internal/Dx/MemoryAllocatorDX.cpp @@ -20,23 +20,20 @@ void Ray::Dx::MemAllocation::Release() { } } -Ray::Dx::MemoryAllocator::MemoryAllocator(const char name[32], Context *ctx, const uint32_t initial_pool_size, - D3D12_HEAP_TYPE heap_type, const float growth_factor, - const uint32_t max_pool_size) - : ctx_(ctx), growth_factor_(growth_factor), max_pool_size_(max_pool_size), heap_type_(heap_type) { - strcpy(name_, name); - +Ray::Dx::MemAllocator::MemAllocator(const char *name, Context *ctx, const uint32_t initial_pool_size, + D3D12_HEAP_TYPE heap_type, const float growth_factor, const uint32_t max_pool_size) + : name_(name), ctx_(ctx), growth_factor_(growth_factor), max_pool_size_(max_pool_size), heap_type_(heap_type) { assert(growth_factor_ > 1.0f); AllocateNewPool(initial_pool_size); } -Ray::Dx::MemoryAllocator::~MemoryAllocator() { +Ray::Dx::MemAllocator::~MemAllocator() { for (MemPool &pool : pools_) { pool.heap->Release(); } } -bool Ray::Dx::MemoryAllocator::AllocateNewPool(const uint32_t size) { +bool Ray::Dx::MemAllocator::AllocateNewPool(const uint32_t size) { D3D12_HEAP_DESC heap_desc = {}; heap_desc.SizeInBytes = size; heap_desc.Flags = D3D12_HEAP_FLAG_DENY_BUFFERS | D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES; @@ -61,7 +58,7 @@ bool Ray::Dx::MemoryAllocator::AllocateNewPool(const uint32_t size) { return SUCCEEDED(hr); } -Ray::Dx::MemAllocation Ray::Dx::MemoryAllocator::Allocate(const uint32_t alignment, const uint32_t size) { +Ray::Dx::MemAllocation Ray::Dx::MemAllocator::Allocate(const uint32_t alignment, const uint32_t size) { auto allocation = alloc_.Alloc(alignment, size); if (allocation.block == 0xffffffff) { @@ -87,18 +84,9 @@ Ray::Dx::MemAllocation Ray::Dx::MemoryAllocator::Allocate(const uint32_t alignme return new_alloc; } -void Ray::Dx::MemoryAllocator::Free(const uint32_t block) { +void Ray::Dx::MemAllocator::Free(const uint32_t block) { alloc_.Free(block); assert(alloc_.IntegrityCheck()); } -void Ray::Dx::MemoryAllocators::Print(ILog *log) { - /*log->Info("================================================================="); - log->Info("MemAllocs %s", name_); - for (const auto &alloc : allocators_) { - alloc.Print(log); - } - log->Info("=================================================================");*/ -} - #pragma warning(pop) \ No newline at end of file diff --git a/internal/Dx/MemoryAllocatorDX.h b/internal/Dx/MemoryAllocatorDX.h index 11d8d6d6..91157068 100644 --- a/internal/Dx/MemoryAllocatorDX.h +++ b/internal/Dx/MemoryAllocatorDX.h @@ -18,12 +18,12 @@ enum D3D12_HEAP_TYPE; namespace Ray { namespace Dx { class Buffer; -class MemoryAllocator; +class MemAllocator; struct MemAllocation { uint32_t offset = 0, block = 0; uint16_t pool = 0; - MemoryAllocator *owner = nullptr; + MemAllocator *owner = nullptr; MemAllocation() = default; MemAllocation(const MemAllocation &rhs) = delete; @@ -49,8 +49,8 @@ struct MemAllocation { void Release(); }; -class MemoryAllocator { - char name_[32] = {}; +class MemAllocator { + std::string name_; Context *ctx_ = nullptr; float growth_factor_; uint32_t max_pool_size_; @@ -67,15 +67,15 @@ class MemoryAllocator { bool AllocateNewPool(uint32_t size); public: - MemoryAllocator(const char name[32], Context *ctx, uint32_t initial_pool_size, D3D12_HEAP_TYPE heap_type, - float growth_factor, uint32_t max_pool_size); - ~MemoryAllocator(); + MemAllocator(const char *name, Context *ctx, uint32_t initial_pool_size, D3D12_HEAP_TYPE heap_type, + float growth_factor, uint32_t max_pool_size); + ~MemAllocator(); - MemoryAllocator(const MemoryAllocator &rhs) = delete; - MemoryAllocator(MemoryAllocator &&rhs) = default; + MemAllocator(const MemAllocator &rhs) = delete; + MemAllocator(MemAllocator &&rhs) = default; - MemoryAllocator &operator=(const MemoryAllocator &rhs) = delete; - MemoryAllocator &operator=(MemoryAllocator &&rhs) = default; + MemAllocator &operator=(const MemAllocator &rhs) = delete; + MemAllocator &operator=(MemAllocator &&rhs) = default; ID3D12Heap *heap(const int pool) const { return pools_[pool].heap; } D3D12_HEAP_TYPE heap_type() const { return heap_type_; } @@ -86,21 +86,19 @@ class MemoryAllocator { void Print(ILog *log) const {} }; -class MemoryAllocators { - char name_[16] = {}; +class MemAllocators { + std::string name_; Context *ctx_; uint32_t initial_pool_size_; float growth_factor_; uint32_t max_pool_size_; - SmallVector allocators_; + SmallVector allocators_; public: - MemoryAllocators(const char name[16], Context *ctx, const uint32_t initial_pool_size, const float growth_factor, - const uint32_t max_pool_size) - : ctx_(ctx), initial_pool_size_(initial_pool_size), growth_factor_(growth_factor), - max_pool_size_(max_pool_size) { - strcpy(name_, name); - } + MemAllocators(const char *name, Context *ctx, const uint32_t initial_pool_size, const float growth_factor, + const uint32_t max_pool_size) + : name_(name), ctx_(ctx), initial_pool_size_(initial_pool_size), growth_factor_(growth_factor), + max_pool_size_(max_pool_size) {} MemAllocation Allocate(const uint32_t alignment, const uint32_t size, const D3D12_HEAP_TYPE heap_type) { int alloc_index = -1; @@ -112,16 +110,14 @@ class MemoryAllocators { } if (alloc_index == -1) { - char name[32]; - snprintf(name, sizeof(name), "%s (type %i)", name_, int(heap_type)); + std::string name = name_; + name += " (type " + std::to_string(heap_type) + ")"; alloc_index = int(allocators_.size()); - allocators_.emplace_back(name, ctx_, initial_pool_size_, heap_type, growth_factor_, max_pool_size_); + allocators_.emplace_back(name.c_str(), ctx_, initial_pool_size_, heap_type, growth_factor_, max_pool_size_); } return allocators_[alloc_index].Allocate(alignment, size); } - - void Print(ILog *log); }; } // namespace Dx } // namespace Ray diff --git a/internal/Dx/TextureDX.cpp b/internal/Dx/TextureDX.cpp index 3cab4568..8727ae24 100644 --- a/internal/Dx/TextureDX.cpp +++ b/internal/Dx/TextureDX.cpp @@ -109,7 +109,7 @@ bool EndsWith(const std::string &str1, const char *str2); Ray::eTexUsage Ray::Dx::TexUsageFromState(const eResState state) { return g_tex_usage_per_state[int(state)]; } -Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams &p, MemoryAllocators *mem_allocs, +Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams &p, MemAllocators *mem_allocs, ILog *log) : ctx_(ctx), name_(name) { Init(p, mem_allocs, log); @@ -117,7 +117,7 @@ Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams Ray::Dx::Texture2D::Texture2D(const char *name, Context *ctx, const void *data, const uint32_t size, const Tex2DParams &p, Buffer &stage_buf, ID3D12GraphicsCommandList *cmd_buf, - MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log) + MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log) : ctx_(ctx), name_(name) { Init(data, size, p, stage_buf, cmd_buf, mem_allocs, load_status, log); } @@ -144,13 +144,13 @@ Ray::Dx::Texture2D &Ray::Dx::Texture2D::operator=(Texture2D &&rhs) noexcept { return (*this); } -void Ray::Dx::Texture2D::Init(const Tex2DParams &p, MemoryAllocators *mem_allocs, ILog *log) { +void Ray::Dx::Texture2D::Init(const Tex2DParams &p, MemAllocators *mem_allocs, ILog *log) { InitFromRAWData(nullptr, 0, nullptr, mem_allocs, p, log); ready_ = true; } void Ray::Dx::Texture2D::Init(const void *data, const uint32_t size, const Tex2DParams &p, Buffer &sbuf, - ID3D12GraphicsCommandList *cmd_buf, MemoryAllocators *mem_allocs, + ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log) { if (!data) { uint8_t *stage_data = sbuf.Map(); @@ -195,7 +195,7 @@ void Ray::Dx::Texture2D::Free() { bool Ray::Dx::Texture2D::Realloc(const int w, const int h, int mip_count, const int samples, const eTexFormat format, const eTexBlock block, const bool is_srgb, ID3D12GraphicsCommandList *cmd_buf, - MemoryAllocators *mem_allocs, ILog *log) { + MemAllocators *mem_allocs, ILog *log) { ID3D12Resource *new_image = nullptr; // VkImageView new_image_view = VK_NULL_HANDLE; MemAllocation new_alloc = {}; @@ -439,7 +439,7 @@ bool Ray::Dx::Texture2D::Realloc(const int w, const int h, int mip_count, const } void Ray::Dx::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, ID3D12GraphicsCommandList *cmd_buf, - MemoryAllocators *mem_allocs, const Tex2DParams &p, ILog *log) { + MemAllocators *mem_allocs, const Tex2DParams &p, ILog *log) { Free(); handle_.generation = TextureHandleCounter++; @@ -903,63 +903,9 @@ void Ray::Dx::_ClearColorImage(Texture2D &tex, const void *rgba, ID3D12GraphicsC ctx->descriptor_heaps_to_release[ctx->backend_frame].push_back(temp_gpu_descriptor_heap); } -//////////////////////////////////////////////////////////////////////////////////////// -#if 0 -Ray::Vk::Texture1D::Texture1D(const char *name, Buffer *buf, const eTexFormat format, const uint32_t offset, - const uint32_t size, ILog *log) - : name_(name) { - Init(buf, format, offset, size, log); -} - -Ray::Vk::Texture1D::~Texture1D() { Free(); } - -Ray::Vk::Texture1D &Ray::Vk::Texture1D::operator=(Texture1D &&rhs) noexcept { - if (this == &rhs) { - return (*this); - } - - Free(); - - buf_ = std::exchange(rhs.buf_, nullptr); - params_ = std::exchange(rhs.params_, {}); - name_ = std::move(rhs.name_); - buf_view_ = std::exchange(rhs.buf_view_, {}); - - return (*this); -} - -void Ray::Vk::Texture1D::Init(Buffer *buf, const eTexFormat format, const uint32_t offset, const uint32_t size, - ILog *log) { - Free(); - - VkBufferViewCreateInfo view_info = {VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO}; - view_info.buffer = buf->vk_handle(); - view_info.format = g_vk_formats[size_t(format)]; - view_info.offset = VkDeviceSize(offset); - view_info.range = VkDeviceSize(size); - - const VkResult res = vkCreateBufferView(buf->ctx()->device(), &view_info, nullptr, &buf_view_); - if (res != VK_SUCCESS) { - buf_->ctx()->log()->Error("Failed to create buffer view!"); - } - - buf_ = buf; - params_.offset = offset; - params_.size = size; - params_.format = format; -} - -void Ray::Vk::Texture1D::Free() { - if (buf_) { - buf_->ctx()->buf_views_to_destroy[buf_->ctx()->backend_frame].push_back(buf_view_); - buf_view_ = {}; - buf_ = {}; - } -} -#endif //////////////////////////////////////////////////////////////////////////////////////// -Ray::Dx::Texture3D::Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemoryAllocators *mem_allocs, +Ray::Dx::Texture3D::Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemAllocators *mem_allocs, ILog *log) : name_(name), ctx_(ctx) { Init(params, mem_allocs, log); @@ -985,7 +931,7 @@ Ray::Dx::Texture3D &Ray::Dx::Texture3D::operator=(Texture3D &&rhs) noexcept { return (*this); } -void Ray::Dx::Texture3D::Init(const Tex3DParams &p, MemoryAllocators *mem_allocs, ILog *log) { +void Ray::Dx::Texture3D::Init(const Tex3DParams &p, MemAllocators *mem_allocs, ILog *log) { Free(); handle_.generation = TextureHandleCounter++; diff --git a/internal/Dx/TextureDX.h b/internal/Dx/TextureDX.h index 0df280be..b159b890 100644 --- a/internal/Dx/TextureDX.h +++ b/internal/Dx/TextureDX.h @@ -20,7 +20,7 @@ class ILog; namespace Dx { eTexUsage TexUsageFromState(eResState state); -class MemoryAllocators; +class MemAllocators; const int TextureDataPitchAlignment = 256; @@ -73,7 +73,7 @@ class Texture2D { void Free(); - void InitFromRAWData(Buffer *sbuf, int data_off, ID3D12GraphicsCommandList *cmd_buf, MemoryAllocators *mem_allocs, + void InitFromRAWData(Buffer *sbuf, int data_off, ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, const Tex2DParams &p, ILog *log); public: @@ -83,13 +83,13 @@ class Texture2D { mutable eResState resource_state = eResState::Undefined; Texture2D() = default; - Texture2D(const char *name, Context *ctx, const Tex2DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + Texture2D(const char *name, Context *ctx, const Tex2DParams ¶ms, MemAllocators *mem_allocs, ILog *log); Texture2D(const char *name, Context *ctx, ID3D12Resource *img, // const VkImageView view, const VkSampler sampler, const Tex2DParams &_params, ILog *log) : handle_{img, /*view, VK_NULL_HANDLE, sampler,*/ 0}, ready_(true), name_(name), params(_params) {} Texture2D(const char *name, Context *ctx, const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf, - ID3D12GraphicsCommandList *cmd_buf, MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); + ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); Texture2D(const Texture2D &rhs) = delete; Texture2D(Texture2D &&rhs) noexcept { (*this) = std::move(rhs); } ~Texture2D(); @@ -97,7 +97,7 @@ class Texture2D { Texture2D &operator=(const Texture2D &rhs) = delete; Texture2D &operator=(Texture2D &&rhs) noexcept; - void Init(const Tex2DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + void Init(const Tex2DParams ¶ms, MemAllocators *mem_allocs, ILog *log); void Init(ID3D12Resource *img, /*const VkImageView view, const VkSampler sampler,*/ const Tex2DParams &_params, ILog *log) { handle_ = {img, /*view, VK_NULL_HANDLE, sampler,*/ 0}; @@ -105,10 +105,10 @@ class Texture2D { ready_ = true; } void Init(const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf, - ID3D12GraphicsCommandList *cmd_buf, MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); + ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); bool Realloc(int w, int h, int mip_count, int samples, eTexFormat format, eTexBlock block, bool is_srgb, - ID3D12GraphicsCommandList *cmd_buf, MemoryAllocators *mem_allocs, ILog *log); + ID3D12GraphicsCommandList *cmd_buf, MemAllocators *mem_allocs, ILog *log); Context *ctx() { return ctx_; } const TexHandle &handle() const { return handle_; } @@ -154,33 +154,6 @@ inline void ClearColorImage(Texture2D &tex, const uint32_t rgba[4], ID3D12Graphi _ClearColorImage(tex, rgba, cmd_buf); } -/*class Texture1D { - Buffer *buf_ = nullptr; - Texture1DParams params_; - std::string name_; - - VkBufferView buf_view_ = VK_NULL_HANDLE; - - void Free(); - - public: - Texture1D(const char *name, Buffer *buf, eTexFormat format, uint32_t offset, uint32_t size, ILog *log); - Texture1D(const Texture1D &rhs) = delete; - Texture1D(Texture1D &&rhs) noexcept { (*this) = std::move(rhs); } - ~Texture1D(); - - Texture1D &operator=(const Texture1D &rhs) = delete; - Texture1D &operator=(Texture1D &&rhs) noexcept; - - const VkBufferView &view() const { return buf_view_; } - - const Texture1DParams ¶ms() const { return params_; } - - const std::string &name() const { return name_; } - - void Init(Buffer *buf, eTexFormat format, uint32_t offset, uint32_t size, ILog *log); -};*/ - class Texture3D { std::string name_; Context *ctx_ = nullptr; @@ -194,7 +167,7 @@ class Texture3D { mutable eResState resource_state = eResState::Undefined; Texture3D() = default; - Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemAllocators *mem_allocs, ILog *log); Texture3D(const Texture3D &rhs) = delete; Texture3D(Texture3D &&rhs) noexcept { (*this) = std::move(rhs); } ~Texture3D(); @@ -208,7 +181,7 @@ class Texture3D { ID3D12Resource *dx_resource() const { return handle_.img; } PoolRef sampler_ref() const { return handle_.sampler_ref; } - void Init(const Tex3DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + void Init(const Tex3DParams ¶ms, MemAllocators *mem_allocs, ILog *log); void SetSubImage(int offsetx, int offsety, int offsetz, int sizex, int sizey, int sizez, eTexFormat format, const Buffer &sbuf, ID3D12GraphicsCommandList *cmd_buf, int data_off, int data_len); diff --git a/internal/RendererDX.cpp b/internal/RendererDX.cpp index c02844af..b2429042 100644 --- a/internal/RendererDX.cpp +++ b/internal/RendererDX.cpp @@ -244,7 +244,7 @@ Ray::Dx::Renderer::Renderer(const settings_t &s, ILog *log, params.sampling.filter = eTexFilter::Bilinear; params.sampling.wrap = eTexWrap::ClampToEdge; - tonemap_lut_ = Texture3D{"Tonemap LUT", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; + tonemap_lut_ = Texture3D{"Tonemap LUT", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; } Renderer::Resize(s.w, s.h); diff --git a/internal/RendererGPU.h b/internal/RendererGPU.h index b1114ae2..03300e6d 100644 --- a/internal/RendererGPU.h +++ b/internal/RendererGPU.h @@ -396,23 +396,22 @@ inline void Ray::NS::Renderer::Resize(const int w, const int h) { params.usage = eTexUsageBits::Sampled | eTexUsageBits::Storage | eTexUsageBits::Transfer; params.sampling.wrap = eTexWrap::ClampToEdge; - temp_buf0_ = Texture2D{"Temp Image 0", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; - temp_buf1_ = Texture2D{"Temp Image 1", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; - full_buf_ = Texture2D{"Full Image", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; - half_buf_ = Texture2D{"Half Image [1]", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; - base_color_buf_ = Texture2D{"Base Color Image", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; + temp_buf0_ = Texture2D{"Temp Image 0", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; + temp_buf1_ = Texture2D{"Temp Image 1", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; + full_buf_ = Texture2D{"Full Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; + half_buf_ = Texture2D{"Half Image [1]", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; + base_color_buf_ = Texture2D{"Base Color Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; temp_depth_normals_buf_ = - Texture2D{"Temp Depth-Normals Image", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; - depth_normals_buf_ = - Texture2D{"Depth-Normals Image", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; - final_buf_ = Texture2D{"Final Image", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; + Texture2D{"Temp Depth-Normals Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; + depth_normals_buf_ = Texture2D{"Depth-Normals Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; + final_buf_ = Texture2D{"Final Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; raw_filtered_buf_ = - Texture2D{"Raw Filtered Final Image", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; + Texture2D{"Raw Filtered Final Image", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; { // Texture that holds required sample count per pixel Tex2DParams uparams = params; uparams.format = eTexFormat::R16UI; required_samples_buf_ = - Texture2D{"Required samples Image", ctx_.get(), uparams, ctx_->default_memory_allocs(), ctx_->log()}; + Texture2D{"Required samples Image", ctx_.get(), uparams, ctx_->default_mem_allocs(), ctx_->log()}; } { // Sampler with black border diff --git a/internal/RendererVK.cpp b/internal/RendererVK.cpp index dc110626..2534131a 100644 --- a/internal/RendererVK.cpp +++ b/internal/RendererVK.cpp @@ -289,7 +289,7 @@ Ray::Vk::Renderer::Renderer(const settings_t &s, ILog *log, params.sampling.filter = eTexFilter::Bilinear; params.sampling.wrap = eTexWrap::ClampToEdge; - tonemap_lut_ = Texture3D{"Tonemap LUT", ctx_.get(), params, ctx_->default_memory_allocs(), ctx_->log()}; + tonemap_lut_ = Texture3D{"Tonemap LUT", ctx_.get(), params, ctx_->default_mem_allocs(), ctx_->log()}; } Renderer::Resize(s.w, s.h); diff --git a/internal/SceneGPU.h b/internal/SceneGPU.h index 890dbd4a..692fdf29 100644 --- a/internal/SceneGPU.h +++ b/internal/SceneGPU.h @@ -718,7 +718,7 @@ inline Ray::TextureHandle Ray::NS::Scene::AddBindlessTexture_nolock(const tex_de p.sampling.filter = eTexFilter::Nearest; std::pair ret = - bindless_textures_.emplace(_t.name ? _t.name : "Bindless Tex", ctx_, p, ctx_->default_memory_allocs(), log_); + bindless_textures_.emplace(_t.name ? _t.name : "Bindless Tex", ctx_, p, ctx_->default_mem_allocs(), log_); { // Submit GPU commands CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool()); @@ -1528,7 +1528,7 @@ inline void Ray::NS::Scene::Finalize(const std::functiondefault_memory_allocs(), log_); + env_map_qtree_.tex = Texture2D("Env map qtree", ctx_, p, ctx_->default_mem_allocs(), log_); } { // add env light source light_t l = {}; @@ -1553,7 +1553,7 @@ inline void Ray::NS::Scene::Finalize(const std::functiondefault_memory_allocs(), log_); + env_map_qtree_.tex = Texture2D("Env map qtree", ctx_, p, ctx_->default_mem_allocs(), log_); } if (use_bindless_ && env_.env_map != InvalidTextureHandle._index) { @@ -1653,7 +1653,7 @@ inline std::vector Ray::NS::Scene::CalcSkyEnvTexture(const a p.h = res[1]; p.format = eTexFormat::RGBA32F; p.usage = eTexUsageBits::Storage | eTexUsageBits::Transfer; - Texture2D temp_img = Texture2D{"Temp Sky Tex", ctx_, p, ctx_->default_memory_allocs(), log_}; + Texture2D temp_img = Texture2D{"Temp Sky Tex", ctx_, p, ctx_->default_mem_allocs(), log_}; { // Write sky image CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool()); @@ -1800,7 +1800,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::functiondefault_memory_allocs(), log_}; + sky_moon_tex_ = Texture2D{"Moon Tex", ctx_, params, ctx_->default_mem_allocs(), log_}; Buffer stage_buf = Buffer("Temp stage buf", ctx_, eBufType::Upload, 4 * MOON_TEX_W * MOON_TEX_H); uint8_t *mapped_ptr = stage_buf.Map(); @@ -1828,7 +1828,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::functiondefault_memory_allocs(), log_}; + sky_weather_tex_ = Texture2D{"Weather Tex", ctx_, params, ctx_->default_mem_allocs(), log_}; Buffer stage_buf = Buffer("Temp stage buf", ctx_, eBufType::Upload, 4 * WEATHER_TEX_RES * WEATHER_TEX_RES); uint8_t *mapped_ptr = stage_buf.Map(); @@ -1857,7 +1857,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::functiondefault_memory_allocs(), log_}; + sky_cirrus_tex_ = Texture2D{"Cirrus Tex", ctx_, params, ctx_->default_mem_allocs(), log_}; Buffer stage_buf = Buffer("Temp stage buf", ctx_, eBufType::Upload, 2 * CIRRUS_TEX_RES * CIRRUS_TEX_RES); uint8_t *mapped_ptr = stage_buf.Map(); @@ -1881,7 +1881,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::functiondefault_memory_allocs(), log_}; + sky_curl_tex_ = Texture2D{"Curl Tex", ctx_, params, ctx_->default_mem_allocs(), log_}; Buffer stage_buf = Buffer("Temp stage buf", ctx_, eBufType::Upload, 4 * CURL_TEX_RES * CURL_TEX_RES); uint8_t *mapped_ptr = stage_buf.Map(); @@ -1909,7 +1909,7 @@ Ray::NS::Scene::PrepareSkyEnvMap_nolock(const std::functiondefault_memory_allocs(), log_}; + sky_noise3d_tex_ = Texture3D{"Noise 3d Tex", ctx_, params, ctx_->default_mem_allocs(), log_}; const uint32_t data_len = NOISE_3D_RES * NOISE_3D_RES * round_up(NOISE_3D_RES, TextureDataPitchAlignment); Buffer stage_buf = Buffer("Temp stage buf", ctx_, eBufType::Upload, data_len); @@ -2193,7 +2193,7 @@ inline void Ray::NS::Scene::PrepareEnvMapQTree_nolock() { p.mip_count = env_.qtree_levels; p.usage = eTexUsageBits::Sampled | eTexUsageBits::Transfer; - env_map_qtree_.tex = Texture2D("Env map qtree", ctx_, p, ctx_->default_memory_allocs(), log_); + env_map_qtree_.tex = Texture2D("Env map qtree", ctx_, p, ctx_->default_mem_allocs(), log_); CommandBuffer cmd_buf = BegSingleTimeCommands(ctx_->api(), ctx_->device(), ctx_->temp_command_pool()); @@ -2534,7 +2534,7 @@ inline void Ray::NS::Scene::SetEnvironment(const environment_desc_t &env) { params.usage = eTexUsageBits::Sampled | eTexUsageBits::Transfer; sky_transmittance_lut_tex_ = - Texture2D{"Sky Transmittance LUT", ctx_, params, ctx_->default_memory_allocs(), log_}; + Texture2D{"Sky Transmittance LUT", ctx_, params, ctx_->default_mem_allocs(), log_}; } if (!sky_multiscatter_lut_tex_.ready()) { Tex2DParams params; @@ -2545,7 +2545,7 @@ inline void Ray::NS::Scene::SetEnvironment(const environment_desc_t &env) { params.usage = eTexUsageBits::Sampled | eTexUsageBits::Transfer; sky_multiscatter_lut_tex_ = - Texture2D{"Sky Multiscatter LUT", ctx_, params, ctx_->default_memory_allocs(), log_}; + Texture2D{"Sky Multiscatter LUT", ctx_, params, ctx_->default_mem_allocs(), log_}; } // Upload textures diff --git a/internal/Vk/ContextVK.cpp b/internal/Vk/ContextVK.cpp index 862ad26a..e0daef93 100644 --- a/internal/Vk/ContextVK.cpp +++ b/internal/Vk/ContextVK.cpp @@ -60,7 +60,7 @@ void Ray::Vk::Context::Destroy() { api_.vkDestroyQueryPool(device_, query_pools_[i], nullptr); } - default_memory_allocs_ = {}; + default_mem_allocs_ = {}; api_.vkFreeCommandBuffers(device_, command_pool_, MaxFramesInFlight, draw_cmd_bufs_); @@ -302,9 +302,9 @@ bool Ray::Vk::Context::Init(ILog *log, const VulkanDevice &vk_device, const Vulk subgroup_supported_ &= (subgroup_props.supportedOperations & VK_SUBGROUP_FEATURE_BASIC_BIT) != 0; } - default_memory_allocs_ = - std::make_unique("Default Allocs", this, 32 * 1024 * 1024 /* initial_pool_size */, - 1.5f /* growth_factor */, 128 * 1024 * 1024 /* max_pool_size */); + default_mem_allocs_ = + std::make_unique("Default Allocs", this, 32 * 1024 * 1024 /* initial_pool_size */, + 1.5f /* growth_factor */, 128 * 1024 * 1024 /* max_pool_size */); for (int i = 0; i < MaxFramesInFlight; ++i) { const int PoolStep = 8; diff --git a/internal/Vk/ContextVK.h b/internal/Vk/ContextVK.h index bd0a5449..11a58e21 100644 --- a/internal/Vk/ContextVK.h +++ b/internal/Vk/ContextVK.h @@ -65,7 +65,7 @@ class Context { uint32_t max_combined_image_samplers_ = 0, max_sampled_images_ = 0, max_samplers_ = 0; - std::unique_ptr default_memory_allocs_; + std::unique_ptr default_mem_allocs_; std::unique_ptr default_descr_alloc_[MaxFramesInFlight]; public: @@ -113,7 +113,7 @@ class Context { const VkFence &in_flight_fence(const int i) const { return in_flight_fences_[i]; } VkQueryPool query_pool(const int i) const { return query_pools_[i]; } - MemoryAllocators *default_memory_allocs() { return default_memory_allocs_.get(); } + MemAllocators *default_mem_allocs() { return default_mem_allocs_.get(); } DescrMultiPoolAlloc *default_descr_alloc() const { return default_descr_alloc_[backend_frame].get(); } int WriteTimestamp(VkCommandBuffer cmd_buf, bool start); diff --git a/internal/Vk/DrawCallVK.cpp b/internal/Vk/DrawCallVK.cpp index aeb3516f..02ee09a5 100644 --- a/internal/Vk/DrawCallVK.cpp +++ b/internal/Vk/DrawCallVK.cpp @@ -92,16 +92,6 @@ VkDescriptorSet Ray::Vk::PrepareDescriptorSet(Context *ctx, VkDescriptorSetLayou new_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; new_write.descriptorCount = 1; new_write.pBufferInfo = &ubuf; - } else if (b.trg == eBindTarget::TBuf) { - ++descr_sizes.tbuf_count; - - auto &new_write = descr_writes.emplace_back(); - new_write = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}; - new_write.dstBinding = b.loc; - new_write.dstArrayElement = 0; - new_write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; - new_write.descriptorCount = 1; - new_write.pTexelBufferView = &b.handle.tex_buf->view(); } else if (b.trg == eBindTarget::SBufRO || b.trg == eBindTarget::SBufRW) { auto &sbuf = sbuf_infos[descr_sizes.sbuf_count++]; sbuf.buffer = b.handle.buf->vk_handle(); diff --git a/internal/Vk/DrawCallVK.h b/internal/Vk/DrawCallVK.h index 6b03080b..dbd6ad1c 100644 --- a/internal/Vk/DrawCallVK.h +++ b/internal/Vk/DrawCallVK.h @@ -39,7 +39,6 @@ enum class eBindTarget : uint16_t { TexCubeArray, Tex3D, Tex3DSampled, - TBuf, UBuf, SBufRO, SBufRW, diff --git a/internal/Vk/MemoryAllocatorVK.cpp b/internal/Vk/MemoryAllocatorVK.cpp index 794c22d0..0b5701ce 100644 --- a/internal/Vk/MemoryAllocatorVK.cpp +++ b/internal/Vk/MemoryAllocatorVK.cpp @@ -34,23 +34,21 @@ void Ray::Vk::MemAllocation::Release() { } } -Ray::Vk::MemoryAllocator::MemoryAllocator(const char name[32], Context *ctx, const uint32_t initial_pool_size, - uint32_t mem_type_index, const float growth_factor, - const uint32_t max_pool_size) - : ctx_(ctx), growth_factor_(growth_factor), max_pool_size_(max_pool_size), mem_type_index_(mem_type_index) { - strcpy(name_, name); - +Ray::Vk::MemAllocator::MemAllocator(const char *name, Context *ctx, const uint32_t initial_pool_size, + uint32_t mem_type_index, const float growth_factor, const uint32_t max_pool_size) + : name_(name), ctx_(ctx), growth_factor_(growth_factor), max_pool_size_(max_pool_size), + mem_type_index_(mem_type_index) { assert(growth_factor_ > 1.0f); AllocateNewPool(initial_pool_size); } -Ray::Vk::MemoryAllocator::~MemoryAllocator() { +Ray::Vk::MemAllocator::~MemAllocator() { for (MemPool &pool : pools_) { ctx_->api().vkFreeMemory(ctx_->device(), pool.mem, nullptr); } } -bool Ray::Vk::MemoryAllocator::AllocateNewPool(const uint32_t size) { +bool Ray::Vk::MemAllocator::AllocateNewPool(const uint32_t size) { VkMemoryAllocateInfo buf_alloc_info = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO}; buf_alloc_info.allocationSize = VkDeviceSize(size); buf_alloc_info.memoryTypeIndex = mem_type_index_; @@ -70,7 +68,7 @@ bool Ray::Vk::MemoryAllocator::AllocateNewPool(const uint32_t size) { return res == VK_SUCCESS; } -Ray::Vk::MemAllocation Ray::Vk::MemoryAllocator::Allocate(const uint32_t alignment, const uint32_t size) { +Ray::Vk::MemAllocation Ray::Vk::MemAllocator::Allocate(const uint32_t alignment, const uint32_t size) { auto allocation = alloc_.Alloc(alignment, size); if (allocation.block == 0xffffffff) { @@ -96,13 +94,13 @@ Ray::Vk::MemAllocation Ray::Vk::MemoryAllocator::Allocate(const uint32_t alignme return new_alloc; } -void Ray::Vk::MemoryAllocator::Free(const uint32_t block) { +void Ray::Vk::MemAllocator::Free(const uint32_t block) { alloc_.Free(block); assert(alloc_.IntegrityCheck()); } -Ray::Vk::MemAllocation Ray::Vk::MemoryAllocators::Allocate(const uint32_t alignment, const uint32_t size, - const uint32_t mem_type_index) { +Ray::Vk::MemAllocation Ray::Vk::MemAllocators::Allocate(const uint32_t alignment, const uint32_t size, + const uint32_t mem_type_index) { if (mem_type_index == 0xffffffff) { return {}; } @@ -116,17 +114,18 @@ Ray::Vk::MemAllocation Ray::Vk::MemoryAllocators::Allocate(const uint32_t alignm } if (alloc_index == -1) { - char name[32]; - snprintf(name, sizeof(name), "%s (type %i)", name_, int(mem_type_index)); + std::string name = name_; + name += " (type " + std::to_string(mem_type_index) + ")"; alloc_index = int(allocators_.size()); - allocators_.emplace_back(name, ctx_, initial_pool_size_, mem_type_index, growth_factor_, max_pool_size_); + allocators_.emplace_back(name.c_str(), ctx_, initial_pool_size_, mem_type_index, growth_factor_, + max_pool_size_); } return allocators_[alloc_index].Allocate(alignment, size); } -Ray::Vk::MemAllocation Ray::Vk::MemoryAllocators::Allocate(const VkMemoryRequirements &mem_req, - const VkMemoryPropertyFlags desired_mem_flags) { +Ray::Vk::MemAllocation Ray::Vk::MemAllocators::Allocate(const VkMemoryRequirements &mem_req, + const VkMemoryPropertyFlags desired_mem_flags) { uint32_t mem_type_index = FindMemoryType(0, &ctx_->mem_properties(), mem_req.memoryTypeBits, desired_mem_flags, uint32_t(mem_req.size)); while (mem_type_index != 0xffffffff) { @@ -140,13 +139,4 @@ Ray::Vk::MemAllocation Ray::Vk::MemoryAllocators::Allocate(const VkMemoryRequire return {}; } -void Ray::Vk::MemoryAllocators::Print(ILog *log) { - /*log->Info("================================================================="); - log->Info("MemAllocs %s", name_); - for (const auto &alloc : allocators_) { - alloc.Print(log); - } - log->Info("=================================================================");*/ -} - #pragma warning(pop) \ No newline at end of file diff --git a/internal/Vk/MemoryAllocatorVK.h b/internal/Vk/MemoryAllocatorVK.h index 6886eadf..dab04748 100644 --- a/internal/Vk/MemoryAllocatorVK.h +++ b/internal/Vk/MemoryAllocatorVK.h @@ -14,12 +14,12 @@ namespace Ray { namespace Vk { class Buffer; -class MemoryAllocator; +class MemAllocator; struct MemAllocation { uint32_t offset = 0, block = 0; uint16_t pool = 0; - MemoryAllocator *owner = nullptr; + MemAllocator *owner = nullptr; MemAllocation() = default; MemAllocation(const MemAllocation &rhs) = delete; @@ -45,8 +45,8 @@ struct MemAllocation { void Release(); }; -class MemoryAllocator { - char name_[32] = {}; +class MemAllocator { + std::string name_; Context *ctx_ = nullptr; float growth_factor_; uint32_t max_pool_size_; @@ -63,15 +63,15 @@ class MemoryAllocator { bool AllocateNewPool(uint32_t size); public: - MemoryAllocator(const char name[32], Context *ctx, uint32_t initial_pool_size, uint32_t mem_type_index, - float growth_factor, uint32_t max_pool_size); - ~MemoryAllocator(); + MemAllocator(const char *name, Context *ctx, uint32_t initial_pool_size, uint32_t mem_type_index, + float growth_factor, uint32_t max_pool_size); + ~MemAllocator(); - MemoryAllocator(const MemoryAllocator &rhs) = delete; - MemoryAllocator(MemoryAllocator &&rhs) = default; + MemAllocator(const MemAllocator &rhs) = delete; + MemAllocator(MemAllocator &&rhs) = default; - MemoryAllocator &operator=(const MemoryAllocator &rhs) = delete; - MemoryAllocator &operator=(MemoryAllocator &&rhs) = default; + MemAllocator &operator=(const MemAllocator &rhs) = delete; + MemAllocator &operator=(MemAllocator &&rhs) = default; VkDeviceMemory mem(const int pool) const { return pools_[pool].mem; } uint32_t mem_type_index() const { return mem_type_index_; } @@ -82,27 +82,22 @@ class MemoryAllocator { void Print(ILog *log) const {} }; -class MemoryAllocators { - char name_[16] = {}; +class MemAllocators { + std::string name_; Context *ctx_; uint32_t initial_pool_size_; float growth_factor_; uint32_t max_pool_size_; - SmallVector allocators_; + SmallVector allocators_; public: - MemoryAllocators(const char name[16], Context *ctx, const uint32_t initial_pool_size, const float growth_factor, - const uint32_t max_pool_size) - : ctx_(ctx), initial_pool_size_(initial_pool_size), growth_factor_(growth_factor), - max_pool_size_(max_pool_size) { - strcpy(name_, name); - } + MemAllocators(const char *name, Context *ctx, const uint32_t initial_pool_size, const float growth_factor, + const uint32_t max_pool_size) + : name_(name), ctx_(ctx), initial_pool_size_(initial_pool_size), growth_factor_(growth_factor), + max_pool_size_(max_pool_size) {} MemAllocation Allocate(uint32_t alignment, uint32_t size, uint32_t mem_type_index); - MemAllocation Allocate(const VkMemoryRequirements &mem_req, VkMemoryPropertyFlags desired_mem_flags); - - void Print(ILog *log); }; inline VkDeviceSize AlignTo(VkDeviceSize size, VkDeviceSize alignment) { diff --git a/internal/Vk/TextureVK.cpp b/internal/Vk/TextureVK.cpp index 0874357f..6da169d1 100644 --- a/internal/Vk/TextureVK.cpp +++ b/internal/Vk/TextureVK.cpp @@ -143,7 +143,7 @@ bool EndsWith(const std::string &str1, const char *str2); Ray::eTexUsage Ray::Vk::TexUsageFromState(const eResState state) { return g_tex_usage_per_state[int(state)]; } -Ray::Vk::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams &p, MemoryAllocators *mem_allocs, +Ray::Vk::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams &p, MemAllocators *mem_allocs, ILog *log) : ctx_(ctx), name_(name) { Init(p, mem_allocs, log); @@ -151,7 +151,7 @@ Ray::Vk::Texture2D::Texture2D(const char *name, Context *ctx, const Tex2DParams Ray::Vk::Texture2D::Texture2D(const char *name, Context *ctx, const void *data, const uint32_t size, const Tex2DParams &p, Buffer &stage_buf, VkCommandBuffer cmd_buf, - MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log) + MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log) : ctx_(ctx), name_(name) { Init(data, size, p, stage_buf, cmd_buf, mem_allocs, load_status, log); } @@ -178,13 +178,13 @@ Ray::Vk::Texture2D &Ray::Vk::Texture2D::operator=(Texture2D &&rhs) noexcept { return (*this); } -void Ray::Vk::Texture2D::Init(const Tex2DParams &p, MemoryAllocators *mem_allocs, ILog *log) { +void Ray::Vk::Texture2D::Init(const Tex2DParams &p, MemAllocators *mem_allocs, ILog *log) { InitFromRAWData(nullptr, 0, nullptr, mem_allocs, p, log); ready_ = true; } void Ray::Vk::Texture2D::Init(const void *data, const uint32_t size, const Tex2DParams &p, Buffer &sbuf, - VkCommandBuffer cmd_buf, MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, + VkCommandBuffer cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log) { if (!data) { uint8_t *stage_data = sbuf.Map(); @@ -231,7 +231,7 @@ void Ray::Vk::Texture2D::Free() { bool Ray::Vk::Texture2D::Realloc(const int w, const int h, int mip_count, const int samples, const eTexFormat format, const eTexBlock block, const bool is_srgb, VkCommandBuffer cmd_buf, - MemoryAllocators *mem_allocs, ILog *log) { + MemAllocators *mem_allocs, ILog *log) { VkImage new_image = VK_NULL_HANDLE; VkImageView new_image_view = VK_NULL_HANDLE; MemAllocation new_alloc = {}; @@ -470,8 +470,8 @@ bool Ray::Vk::Texture2D::Realloc(const int w, const int h, int mip_count, const return true; } -void Ray::Vk::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, VkCommandBuffer cmd_buf, - MemoryAllocators *mem_allocs, const Tex2DParams &p, ILog *log) { +void Ray::Vk::Texture2D::InitFromRAWData(Buffer *sbuf, int data_off, VkCommandBuffer cmd_buf, MemAllocators *mem_allocs, + const Tex2DParams &p, ILog *log) { Free(); handle_.generation = TextureHandleCounter++; @@ -944,61 +944,7 @@ void Ray::Vk::ClearColorImage(Texture2D &tex, const uint32_t rgba[4], VkCommandB //////////////////////////////////////////////////////////////////////////////////////// -Ray::Vk::Texture1D::Texture1D(const char *name, Buffer *buf, const eTexFormat format, const uint32_t offset, - const uint32_t size, ILog *log) - : name_(name) { - Init(buf, format, offset, size, log); -} - -Ray::Vk::Texture1D::~Texture1D() { Free(); } - -Ray::Vk::Texture1D &Ray::Vk::Texture1D::operator=(Texture1D &&rhs) noexcept { - if (this == &rhs) { - return (*this); - } - - Free(); - - buf_ = std::exchange(rhs.buf_, nullptr); - params_ = std::exchange(rhs.params_, {}); - name_ = std::move(rhs.name_); - buf_view_ = std::exchange(rhs.buf_view_, {}); - - return (*this); -} - -void Ray::Vk::Texture1D::Init(Buffer *buf, const eTexFormat format, const uint32_t offset, const uint32_t size, - ILog *log) { - Free(); - - VkBufferViewCreateInfo view_info = {VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO}; - view_info.buffer = buf->vk_handle(); - view_info.format = g_vk_formats[size_t(format)]; - view_info.offset = VkDeviceSize(offset); - view_info.range = VkDeviceSize(size); - - const VkResult res = buf->ctx()->api().vkCreateBufferView(buf->ctx()->device(), &view_info, nullptr, &buf_view_); - if (res != VK_SUCCESS) { - buf_->ctx()->log()->Error("Failed to create buffer view!"); - } - - buf_ = buf; - params_.offset = offset; - params_.size = size; - params_.format = format; -} - -void Ray::Vk::Texture1D::Free() { - if (buf_) { - buf_->ctx()->buf_views_to_destroy[buf_->ctx()->backend_frame].push_back(buf_view_); - buf_view_ = {}; - buf_ = {}; - } -} - -//////////////////////////////////////////////////////////////////////////////////////// - -Ray::Vk::Texture3D::Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemoryAllocators *mem_allocs, +Ray::Vk::Texture3D::Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemAllocators *mem_allocs, ILog *log) : name_(name), ctx_(ctx) { Init(params, mem_allocs, log); @@ -1024,7 +970,7 @@ Ray::Vk::Texture3D &Ray::Vk::Texture3D::operator=(Texture3D &&rhs) noexcept { return (*this); } -void Ray::Vk::Texture3D::Init(const Tex3DParams &p, MemoryAllocators *mem_allocs, ILog *log) { +void Ray::Vk::Texture3D::Init(const Tex3DParams &p, MemAllocators *mem_allocs, ILog *log) { Free(); handle_.generation = TextureHandleCounter++; diff --git a/internal/Vk/TextureVK.h b/internal/Vk/TextureVK.h index ae8e20b3..d59a25da 100644 --- a/internal/Vk/TextureVK.h +++ b/internal/Vk/TextureVK.h @@ -17,7 +17,7 @@ class ILog; namespace Vk { eTexUsage TexUsageFromState(eResState state); -class MemoryAllocators; +class MemAllocators; const int TextureDataPitchAlignment = 1; @@ -69,7 +69,7 @@ class Texture2D { void Free(); - void InitFromRAWData(Buffer *sbuf, int data_off, VkCommandBuffer cmd_buf, MemoryAllocators *mem_allocs, + void InitFromRAWData(Buffer *sbuf, int data_off, VkCommandBuffer cmd_buf, MemAllocators *mem_allocs, const Tex2DParams &p, ILog *log); public: @@ -79,12 +79,12 @@ class Texture2D { mutable eResState resource_state = eResState::Undefined; Texture2D() = default; - Texture2D(const char *name, Context *ctx, const Tex2DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + Texture2D(const char *name, Context *ctx, const Tex2DParams ¶ms, MemAllocators *mem_allocs, ILog *log); Texture2D(const char *name, Context *ctx, const VkImage img, const VkImageView view, const VkSampler sampler, const Tex2DParams &_params, ILog *log) : handle_{img, view, VK_NULL_HANDLE, sampler, 0}, ready_(true), name_(name), params(_params) {} Texture2D(const char *name, Context *ctx, const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf, - VkCommandBuffer cmd_buf, MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); + VkCommandBuffer cmd_buf, MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); Texture2D(const Texture2D &rhs) = delete; Texture2D(Texture2D &&rhs) noexcept { (*this) = std::move(rhs); } ~Texture2D(); @@ -92,7 +92,7 @@ class Texture2D { Texture2D &operator=(const Texture2D &rhs) = delete; Texture2D &operator=(Texture2D &&rhs) noexcept; - void Init(const Tex2DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + void Init(const Tex2DParams ¶ms, MemAllocators *mem_allocs, ILog *log); void Init(const VkImage img, const VkImageView view, const VkSampler sampler, const Tex2DParams &_params, ILog *log) { handle_ = {img, view, VK_NULL_HANDLE, sampler, 0}; @@ -100,12 +100,12 @@ class Texture2D { ready_ = true; } void Init(const void *data, uint32_t size, const Tex2DParams &p, Buffer &stage_buf, VkCommandBuffer cmd_buf, - MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); + MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); void Init(const void *data[6], const int size[6], const Tex2DParams &p, Buffer &stage_buf, VkCommandBuffer cmd_buf, - MemoryAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); + MemAllocators *mem_allocs, eTexLoadStatus *load_status, ILog *log); bool Realloc(int w, int h, int mip_count, int samples, eTexFormat format, eTexBlock block, bool is_srgb, - VkCommandBuffer cmd_buf, MemoryAllocators *mem_allocs, ILog *log); + VkCommandBuffer cmd_buf, MemAllocators *mem_allocs, ILog *log); Context *ctx() const { return ctx_; } const TexHandle &handle() const { return handle_; } @@ -145,33 +145,6 @@ void CopyImageToBuffer(const Texture2D &src_tex, int level, int x, int y, int w, void ClearColorImage(Texture2D &tex, const float rgba[4], VkCommandBuffer cmd_buf); void ClearColorImage(Texture2D &tex, const uint32_t rgba[4], VkCommandBuffer cmd_buf); -class Texture1D { - Buffer *buf_ = nullptr; - Texture1DParams params_; - std::string name_; - - VkBufferView buf_view_ = VK_NULL_HANDLE; - - void Free(); - - public: - Texture1D(const char *name, Buffer *buf, eTexFormat format, uint32_t offset, uint32_t size, ILog *log); - Texture1D(const Texture1D &rhs) = delete; - Texture1D(Texture1D &&rhs) noexcept { (*this) = std::move(rhs); } - ~Texture1D(); - - Texture1D &operator=(const Texture1D &rhs) = delete; - Texture1D &operator=(Texture1D &&rhs) noexcept; - - const VkBufferView &view() const { return buf_view_; } - - const Texture1DParams ¶ms() const { return params_; } - - const std::string &name() const { return name_; } - - void Init(Buffer *buf, eTexFormat format, uint32_t offset, uint32_t size, ILog *log); -}; - class Texture3D { std::string name_; Context *ctx_ = nullptr; @@ -185,7 +158,7 @@ class Texture3D { mutable eResState resource_state = eResState::Undefined; Texture3D() = default; - Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + Texture3D(const char *name, Context *ctx, const Tex3DParams ¶ms, MemAllocators *mem_allocs, ILog *log); Texture3D(const Texture3D &rhs) = delete; Texture3D(Texture3D &&rhs) noexcept { (*this) = std::move(rhs); } ~Texture3D(); @@ -199,7 +172,7 @@ class Texture3D { TexHandle &handle() { return handle_; } VkSampler vk_sampler() const { return handle_.sampler; } - void Init(const Tex3DParams ¶ms, MemoryAllocators *mem_allocs, ILog *log); + void Init(const Tex3DParams ¶ms, MemAllocators *mem_allocs, ILog *log); void SetSubImage(int offsetx, int offsety, int offsetz, int sizex, int sizey, int sizez, eTexFormat format, const Buffer &sbuf, VkCommandBuffer cmd_buf, int data_off, int data_len);