Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
PearCoding committed Feb 11, 2025
1 parent 7282617 commit f076fa0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
18 changes: 9 additions & 9 deletions src/device/UnifiedArray.cpp → src/device/DeviceArray.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#include "UnifiedArray.h"
#include "DeviceArray.h"

#include <anydsl_runtime.h>

namespace IG {
void* UnifiedArrayBase::allocateUnified(int dev, size_t sizeInBytes)
void* DeviceArrayBase::allocateUnified(int dev, size_t sizeInBytes)
{
return anydsl_alloc_unified(dev, (int64_t)sizeInBytes);
}

void* UnifiedArrayBase::allocateDevice(int dev, size_t sizeInBytes)
void* DeviceArrayBase::allocateDevice(int dev, size_t sizeInBytes)
{
return anydsl_alloc(dev, (int64_t)sizeInBytes);
}

void UnifiedArrayBase::deallocateUnified(int dev, void* ptr)
void DeviceArrayBase::deallocateUnified(int dev, void* ptr)
{
if (ptr)
anydsl_release(dev, ptr);
}

void UnifiedArrayBase::deallocateDevice(int dev, void* ptr)
void DeviceArrayBase::deallocateDevice(int dev, void* ptr)
{
if (ptr)
anydsl_release(dev, ptr);
}

void* UnifiedArrayBase::getDevicePtr(int dev, void* hostPtr)
void* DeviceArrayBase::getDevicePtr(int dev, void* hostPtr)
{
if (dev == 0)
return hostPtr;
Expand All @@ -35,21 +35,21 @@ void* UnifiedArrayBase::getDevicePtr(int dev, void* hostPtr)
return nullptr;
}

void UnifiedArrayBase::fillHostWithZero(int dev, void* ptr, size_t sizeInBytes)
void DeviceArrayBase::fillHostWithZero(int dev, void* ptr, size_t sizeInBytes)
{
IG_UNUSED(dev);
std::memset(ptr, 0, sizeInBytes);
}

void UnifiedArrayBase::copyToHost(int dev, const void* devPtr, void* hostPtr, size_t sizeInBytes)
void DeviceArrayBase::copyToHost(int dev, const void* devPtr, void* hostPtr, size_t sizeInBytes)
{
if (dev == 0 /*Host*/ && devPtr == hostPtr)
return;

anydsl_copy(dev, devPtr, 0, 0 /*Host*/, hostPtr, 0, (int64)sizeInBytes);
}

void UnifiedArrayBase::copyFromHost(int dev, void* devPtr, const void* hostPtr, size_t sizeInBytes)
void DeviceArrayBase::copyFromHost(int dev, void* devPtr, const void* hostPtr, size_t sizeInBytes)
{
if (dev == 0 /*Host*/ && devPtr == hostPtr)
return;
Expand Down
46 changes: 23 additions & 23 deletions src/device/UnifiedArray.h → src/device/DeviceArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "IG_Config.h"

namespace IG {
class UnifiedArrayBase {
class DeviceArrayBase {
protected:
[[nodiscard]] static void* allocateUnified(int dev, size_t sizeInBytes);
[[nodiscard]] static void* allocateDevice(int dev, size_t sizeInBytes);
Expand All @@ -16,8 +16,8 @@ class UnifiedArrayBase {
};

template <typename T>
class UnifiedArray : public UnifiedArrayBase {
IG_CLASS_NON_COPYABLE(UnifiedArray);
class DeviceArray : public DeviceArrayBase {
IG_CLASS_NON_COPYABLE(DeviceArray);

public:
enum class MemoryType {
Expand All @@ -39,7 +39,7 @@ class UnifiedArray : public UnifiedArrayBase {
MemoryType Type;
int32 StatusFlags;

inline UnifiedArray()
inline DeviceArray()
: DevicePtr(nullptr)
, HostPtr(nullptr)
, SizeInBytes(0)
Expand All @@ -49,7 +49,7 @@ class UnifiedArray : public UnifiedArrayBase {
{
}

inline UnifiedArray(UnifiedArray&& arr)
inline DeviceArray(DeviceArray&& arr)
: DevicePtr(arr.DevicePtr)
, HostPtr(arr.HostPtr)
, SizeInBytes(arr.SizeInBytes)
Expand All @@ -65,49 +65,49 @@ class UnifiedArray : public UnifiedArrayBase {
arr.StatusFlags = 0;
}

static inline UnifiedArray AllocateUnified(int dev, size_t size)
static inline DeviceArray AllocateUnified(int dev, size_t size)
{
void* ptr = allocateUnified(dev, size * sizeof(T));
return UnifiedArray(
return DeviceArray(
(T*)ptr,
(T*)ptr,
size * sizeof(T),
dev,
MemoryType::Unified);
}

static inline UnifiedArray AllocateDevice(int dev, size_t size)
static inline DeviceArray AllocateDevice(int dev, size_t size)
{
return UnifiedArray(
return DeviceArray(
nullptr,
(T*)allocateDevice(dev, size * sizeof(T)),
size * sizeof(T),
dev,
MemoryType::Device);
}

static inline UnifiedArray AllocateBuffered(int dev, size_t size)
static inline DeviceArray AllocateBuffered(int dev, size_t size)
{
void* devPtr = allocateDevice(dev, size * sizeof(T));
return UnifiedArray(
return DeviceArray(
dev != 0 ? (T*)allocateDevice(0 /*Host*/, size * sizeof(T)) : (T*)devPtr, // Only allocate buffer if the device != host
(T*)devPtr,
size * sizeof(T),
dev,
MemoryType::Buffered);
}

static inline UnifiedArray CreateExternal(int dev, T* ptr, size_t size)
static inline DeviceArray CreateExternal(int dev, T* ptr, size_t size)
{
return UnifiedArray(
return DeviceArray(
ptr,
ptr,
size * sizeof(T),
dev,
MemoryType::External);
}

static inline UnifiedArray CreateExternalOrAllocateUnified(int dev, T* ptr, size_t size, bool externalFlag)
static inline DeviceArray CreateExternalOrAllocateUnified(int dev, T* ptr, size_t size, bool externalFlag)
{
if (externalFlag) {
return CreateExternal(dev, ptr, size);
Expand All @@ -118,7 +118,7 @@ class UnifiedArray : public UnifiedArrayBase {
}
}

static inline UnifiedArray CreateExternalOrAllocateDevice(int dev, T* ptr, size_t size, bool externalFlag)
static inline DeviceArray CreateExternalOrAllocateDevice(int dev, T* ptr, size_t size, bool externalFlag)
{
if (externalFlag) {
return CreateExternal(dev, ptr, size);
Expand All @@ -129,12 +129,12 @@ class UnifiedArray : public UnifiedArrayBase {
}
}

inline ~UnifiedArray()
inline ~DeviceArray()
{
release();
}

inline UnifiedArray& operator=(UnifiedArray&& arr)
inline DeviceArray& operator=(DeviceArray&& arr)
{
release();

Expand All @@ -161,22 +161,22 @@ class UnifiedArray : public UnifiedArrayBase {

inline void fillHostWithZero()
{
UnifiedArrayBase::fillHostWithZero(Device, (void*)HostPtr, SizeInBytes);
DeviceArrayBase::fillHostWithZero(Device, (void*)HostPtr, SizeInBytes);
StatusFlags &= ~(int)Flags::DirtyDevice; // We do not care if the device is "dirty". Cleanup has priority
StatusFlags |= (int)Flags::DirtyHost;
}

inline void copyFromExternalHostToDevice(const T* hostPtr, size_t sizeInBytes)
{
UnifiedArrayBase::copyFromHost(Device, DevicePtr, hostPtr, sizeInBytes);
DeviceArrayBase::copyFromHost(Device, DevicePtr, hostPtr, sizeInBytes);
StatusFlags |= (int)Flags::DirtyDevice;
}

inline void copyFromExternalHostToDevice(const T* hostPtr) { copyFromExternalHostToDevice(hostPtr, SizeInBytes); }

inline void copyFromDeviceToExternalHost(T* hostPtr, size_t sizeInBytes)
{
UnifiedArrayBase::copyToHost(Device, DevicePtr, hostPtr, sizeInBytes);
DeviceArrayBase::copyToHost(Device, DevicePtr, hostPtr, sizeInBytes);
}

inline void copyFromDeviceToExternalHost(T* hostPtr) { copyFromDeviceToExternalHost(hostPtr, SizeInBytes); }
Expand Down Expand Up @@ -211,7 +211,7 @@ class UnifiedArray : public UnifiedArrayBase {

IG_ASSERT((StatusFlags & ((int)Flags::DirtyDevice | (int)Flags::DirtyHost)) != ((int)Flags::DirtyDevice | (int)Flags::DirtyHost), "Unified array can not deal with device and host buffer being out of sync at the same time!");

UnifiedArrayBase::copyFromHost(Device, DevicePtr, HostPtr, SizeInBytes);
DeviceArrayBase::copyFromHost(Device, DevicePtr, HostPtr, SizeInBytes);
}
}

Expand All @@ -226,7 +226,7 @@ class UnifiedArray : public UnifiedArrayBase {

IG_ASSERT((StatusFlags & ((int)Flags::DirtyDevice | (int)Flags::DirtyHost)) != ((int)Flags::DirtyDevice | (int)Flags::DirtyHost), "Unified array can not deal with device and host buffer being out of sync at the same time!");

UnifiedArrayBase::copyToHost(Device, DevicePtr, HostPtr, SizeInBytes);
DeviceArrayBase::copyToHost(Device, DevicePtr, HostPtr, SizeInBytes);
}
}

Expand Down Expand Up @@ -261,7 +261,7 @@ class UnifiedArray : public UnifiedArrayBase {
}

private:
inline UnifiedArray(T* hostPtr, T* devicePtr, size_t sizeInBytes, int device, MemoryType type)
inline DeviceArray(T* hostPtr, T* devicePtr, size_t sizeInBytes, int device, MemoryType type)
: DevicePtr(devicePtr)
, HostPtr(hostPtr)
, SizeInBytes(sizeInBytes)
Expand Down
44 changes: 22 additions & 22 deletions src/device/DeviceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ static inline size_t roundUp(size_t num, size_t multiple)
}

template <typename T>
static inline void resizeDeviceArray(int32_t dev, UnifiedArray<T>& array, size_t size, size_t multiplier)
static inline void resizeDeviceArray(int32_t dev, DeviceArray<T>& array, size_t size, size_t multiplier)
{
const auto capacity = roundUp(std::max<size_t>(1, size), 32);
const size_t n = capacity * multiplier;
if (array.SizeInBytes < n * sizeof(T)) {
array = UnifiedArray<T>::AllocateDevice(dev, n);
array = DeviceArray<T>::AllocateDevice(dev, n);
if (array.DevicePtr == nullptr)
IG_LOG(L_FATAL) << "Stream allocation resulted in out of memory!" << std::endl;
}
Expand Down Expand Up @@ -204,13 +204,13 @@ void DeviceInterface::resizeFramebuffer(size_t width, size_t height)
ensureFramebuffer();
}

static inline UnifiedArray<float> createAOVArray(int device, size_t size)
static inline DeviceArray<float> createAOVArray(int device, size_t size)
{
// Unified memory on Windows (Nvidia RTX 4070 Ti Super) has quite a penalty for our AOV access pattern. AOVs in general are easy to sync in access so it is not worth it.
#if 1
return UnifiedArray<float>::AllocateBuffered(device, size);
return DeviceArray<float>::AllocateBuffered(device, size);
#else
return UnifiedArray<float>::AllocateUnified(device, size);
return DeviceArray<float>::AllocateUnified(device, size);
#endif
}

Expand Down Expand Up @@ -474,7 +474,7 @@ void* DeviceInterface::loadRayList()

IG_ASSERT(mCurrentRenderSettings.rays != nullptr, "Expected list of rays to be available");

device.ray_list = UnifiedArray<StreamRay>::AllocateDevice(mDeviceID, count);
device.ray_list = DeviceArray<StreamRay>::AllocateDevice(mDeviceID, count);

std::vector<StreamRay> host_data(isGPU() ? count : 0);
StreamRay* ptr = isGPU() ? host_data.data() : device.ray_list.DevicePtr;
Expand Down Expand Up @@ -527,8 +527,8 @@ IDeviceInterface::DynTableProxy DeviceInterface::loadDynTable(const std::string&

DeviceDynTable entry = DeviceDynTable{
.EntryCount = tbl.entryCount(),
.LookupEntries = UnifiedArray<::LookupEntry>::AllocateDevice(mDeviceID, tbl.lookups().size()),
.Data = UnifiedArray<uint8_t>::AllocateDevice(mDeviceID, tbl.data().size())
.LookupEntries = DeviceArray<::LookupEntry>::AllocateDevice(mDeviceID, tbl.lookups().size()),
.Data = DeviceArray<uint8_t>::AllocateDevice(mDeviceID, tbl.data().size())
};

entry.LookupEntries.copyFromExternalHostToDevice((const ::LookupEntry*)tbl.lookups().data());
Expand Down Expand Up @@ -564,7 +564,7 @@ IDeviceInterface::FixTableProxy DeviceInterface::loadFixTable(const std::string&
// Special case: No entities in the scene
return FixTableProxy::Invalid();
} else {
DeviceBuffer buffer = DeviceBuffer{ .Data = UnifiedArray<uint8>::AllocateDevice(mDeviceID, fixtable.data().size()) };
DeviceBuffer buffer = DeviceBuffer{ .Data = DeviceArray<uint8>::AllocateDevice(mDeviceID, fixtable.data().size()) };
buffer.Data.copyFromExternalHostToDevice(fixtable.data().data());
return mapToProxyDevice(tables.try_emplace(name, std::move(buffer)).first->second);
}
Expand Down Expand Up @@ -599,22 +599,22 @@ void DeviceInterface::loadEntityBVH(BVHType type, const char* prim_type, void**
case BVHType::BVH2: {
const size_t node_count = bvh.Nodes.size() / sizeof(Node2);
device.bvh_ents[str] = IG::Bvh2Ent{
std::move(UnifiedArray<Node2>::CreateExternalOrAllocateDevice(mDeviceID, (Node2*)bvh.Nodes.data(), node_count, !isGPU())),
std::move(UnifiedArray<EntityLeaf1>::CreateExternalOrAllocateDevice(mDeviceID, (EntityLeaf1*)bvh.Leaves.data(), leaf_count, !isGPU()))
std::move(DeviceArray<Node2>::CreateExternalOrAllocateDevice(mDeviceID, (Node2*)bvh.Nodes.data(), node_count, !isGPU())),
std::move(DeviceArray<EntityLeaf1>::CreateExternalOrAllocateDevice(mDeviceID, (EntityLeaf1*)bvh.Leaves.data(), leaf_count, !isGPU()))
};
} break;
case BVHType::BVH4: {
const size_t node_count = bvh.Nodes.size() / sizeof(Node4);
device.bvh_ents[str] = IG::Bvh4Ent{
std::move(UnifiedArray<Node4>::CreateExternalOrAllocateDevice(mDeviceID, (Node4*)bvh.Nodes.data(), node_count, !isGPU())),
std::move(UnifiedArray<EntityLeaf1>::CreateExternalOrAllocateDevice(mDeviceID, (EntityLeaf1*)bvh.Leaves.data(), leaf_count, !isGPU()))
std::move(DeviceArray<Node4>::CreateExternalOrAllocateDevice(mDeviceID, (Node4*)bvh.Nodes.data(), node_count, !isGPU())),
std::move(DeviceArray<EntityLeaf1>::CreateExternalOrAllocateDevice(mDeviceID, (EntityLeaf1*)bvh.Leaves.data(), leaf_count, !isGPU()))
};
} break;
case BVHType::BVH8: {
const size_t node_count = bvh.Nodes.size() / sizeof(Node8);
device.bvh_ents[str] = IG::Bvh8Ent{
std::move(UnifiedArray<Node8>::CreateExternalOrAllocateDevice(mDeviceID, (Node8*)bvh.Nodes.data(), node_count, !isGPU())),
std::move(UnifiedArray<EntityLeaf1>::CreateExternalOrAllocateDevice(mDeviceID, (EntityLeaf1*)bvh.Leaves.data(), leaf_count, !isGPU()))
std::move(DeviceArray<Node8>::CreateExternalOrAllocateDevice(mDeviceID, (Node8*)bvh.Nodes.data(), node_count, !isGPU())),
std::move(DeviceArray<EntityLeaf1>::CreateExternalOrAllocateDevice(mDeviceID, (EntityLeaf1*)bvh.Leaves.data(), leaf_count, !isGPU()))
};
} break;
}
Expand Down Expand Up @@ -655,14 +655,14 @@ IDeviceInterface::DeviceImageProxy<float> DeviceInterface::loadImageFromFile(con
_SECTION(SectionType::ImageLoading);

IG_LOG(L_DEBUG) << "Loading image '" << filename << "' (C=" << expected_channels << ")" << std::endl;
UnifiedArray<float> arr;
DeviceArray<float> arr;
size_t width, height;
try {
Image image = Image::load(filename);
if (image.channels != (size_t)expected_channels)
image = image.castTo((size_t)expected_channels);

arr = UnifiedArray<float>::AllocateDevice(mDeviceID, image.width * image.height * image.channels);
arr = DeviceArray<float>::AllocateDevice(mDeviceID, image.width * image.height * image.channels);
width = image.width;
height = image.height;
arr.copyFromExternalHostToDevice(image.pixels.get());
Expand All @@ -675,12 +675,12 @@ IDeviceInterface::DeviceImageProxy<float> DeviceInterface::loadImageFromFile(con

if (MissingImage.channels != (size_t)expected_channels) {
Image image = MissingImage.castTo((size_t)expected_channels);
arr = UnifiedArray<float>::AllocateDevice(mDeviceID, image.width * image.height * image.channels);
arr = DeviceArray<float>::AllocateDevice(mDeviceID, image.width * image.height * image.channels);
width = image.width;
height = image.height;
arr.copyFromExternalHostToDevice(image.pixels.get());
} else {
arr = UnifiedArray<float>::AllocateDevice(mDeviceID, MissingImage.width * MissingImage.height * MissingImage.channels);
arr = DeviceArray<float>::AllocateDevice(mDeviceID, MissingImage.width * MissingImage.height * MissingImage.channels);
width = MissingImage.width;
height = MissingImage.height;
arr.copyFromExternalHostToDevice(MissingImage.pixels.get());
Expand Down Expand Up @@ -730,7 +730,7 @@ IDeviceInterface::DeviceImageProxy<uint8_t> DeviceInterface::loadPackedImageFrom
channels = MissingImage.channels;
}

auto arr = UnifiedArray<uint8>::AllocateDevice(mDeviceID, hostData.size());
auto arr = DeviceArray<uint8>::AllocateDevice(mDeviceID, hostData.size());
arr.copyFromExternalHostToDevice(hostData.data());

return mapToProxyDevice(images.try_emplace(filename,
Expand Down Expand Up @@ -774,7 +774,7 @@ IDeviceInterface::DeviceBufferProxy<uint8_t> DeviceInterface::loadBufferFromFile
if ((vec.size() % sizeof(int32_t)) != 0)
IG_LOG(L_WARNING) << "Buffer '" << filename << "' is not properly sized!" << std::endl;

auto arr = UnifiedArray<uint8>::AllocateDevice(mDeviceID, vec.size());
auto arr = DeviceArray<uint8>::AllocateDevice(mDeviceID, vec.size());
arr.copyFromExternalHostToDevice(vec.data());

return mapToProxyDevice(buffers.try_emplace(filename, DeviceBuffer{ .Data = std::move(arr) }).first->second);
Expand Down Expand Up @@ -809,7 +809,7 @@ IDeviceInterface::DeviceBufferProxy<uint8_t> DeviceInterface::requestBuffer(cons

IG_LOG(L_DEBUG) << "Requested buffer '" << name << "' with " << FormatMemory(size) << std::endl;

auto arr = UnifiedArray<uint8>::AllocateDevice(mDeviceID, size);
auto arr = DeviceArray<uint8>::AllocateDevice(mDeviceID, size);
return mapToProxyDevice(buffers.insert_or_assign(name, DeviceBuffer{ .Data = std::move(arr) }).first->second);
}

Expand Down
Loading

0 comments on commit f076fa0

Please sign in to comment.