Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Dec 27, 2024
1 parent bec4948 commit 7d0f0a9
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 340 deletions.
8 changes: 4 additions & 4 deletions internal/Dx/ContextDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void Ray::Dx::Context::Destroy() {
DestroyDeferredResources(i);
}

default_memory_allocs_ = {};
default_mem_allocs_ = {};
staging_descr_alloc_ = {};

SAFE_RELEASE(command_queue_);
Expand Down Expand Up @@ -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<MemoryAllocators>("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<MemAllocators>("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<DescrMultiPoolAlloc<FreelistAllocAdapted>>(this, false, 16 * 1024);

for (int i = 0; i < MaxFramesInFlight; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions internal/Dx/ContextDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Buffer;
class BumpAlloc;
class FreelistAllocAdapted;
template <class Allocator> class DescrMultiPoolAlloc;
class MemoryAllocators;
class MemAllocators;

using CommandBuffer = ID3D12GraphicsCommandList *;

Expand Down Expand Up @@ -81,7 +81,7 @@ class Context {

uint32_t max_combined_image_samplers_ = 0, max_sampled_images_ = 0, max_samplers_ = 0;

std::unique_ptr<MemoryAllocators> default_memory_allocs_;
std::unique_ptr<MemAllocators> default_mem_allocs_;
std::unique_ptr<DescrMultiPoolAlloc<BumpAlloc>> default_descr_alloc_[MaxFramesInFlight];
std::unique_ptr<DescrMultiPoolAlloc<FreelistAllocAdapted>> staging_descr_alloc_;

Expand Down Expand Up @@ -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<BumpAlloc> *default_descr_alloc() { return default_descr_alloc_[backend_frame].get(); }
DescrMultiPoolAlloc<FreelistAllocAdapted> *staging_descr_alloc() { return staging_descr_alloc_.get(); }

Expand Down
26 changes: 7 additions & 19 deletions internal/Dx/MemoryAllocatorDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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)
46 changes: 21 additions & 25 deletions internal/Dx/MemoryAllocatorDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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_;
Expand All @@ -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_; }
Expand All @@ -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<MemoryAllocator, 4> allocators_;
SmallVector<MemAllocator, 4> 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;
Expand All @@ -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
Expand Down
70 changes: 8 additions & 62 deletions internal/Dx/TextureDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ 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);
}

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);
}
Expand All @@ -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();
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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 &params, MemoryAllocators *mem_allocs,
Ray::Dx::Texture3D::Texture3D(const char *name, Context *ctx, const Tex3DParams &params, MemAllocators *mem_allocs,
ILog *log)
: name_(name), ctx_(ctx) {
Init(params, mem_allocs, log);
Expand All @@ -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++;
Expand Down
Loading

0 comments on commit 7d0f0a9

Please sign in to comment.