Skip to content

Commit

Permalink
Merge branch 'master' into images_only
Browse files Browse the repository at this point in the history
Add support for creating images with uninitialised data too.
Add more tests for both Tensors and Images.
  • Loading branch information
robquill committed Aug 21, 2024
2 parents c0e2959 + 1748b82 commit d44aa42
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 43 deletions.
16 changes: 6 additions & 10 deletions src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ Image::init(void* data,
height,
Memory::toString(this->memoryType()));

if (width == 0 || height == 0 || numChannels == 0 || dataSize == 0 ||
data == nullptr) {
if (width == 0 || height == 0 || numChannels == 0) {
throw std::runtime_error(
"Kompute Image attempted to create a zero-sized image");
}

if (dataSize < width * height * numChannels) {
if (data != nullptr && dataSize < width * height * numChannels) {
throw std::runtime_error(
"Kompute Image data is smaller than the requested image size");
}
Expand All @@ -48,7 +47,8 @@ Image::init(void* data,
this->mTiling = tiling;
this->mSize = this->mWidth * this->mHeight * this->mNumChannels;

this->rebuild(data);
this->reserve();
this->updateRawData(data);
}

Image::~Image()
Expand All @@ -64,12 +64,9 @@ Image::~Image()
}

void
Image::rebuild(void* data)
Image::reserve()
{
KP_LOG_DEBUG("Kompute Image rebuilding with size {} x {} with {} channels",
this->mWidth,
this->mHeight,
this->mNumChannels);
KP_LOG_DEBUG("Reserving {} bytes for memory", this->mSize * this->mDataTypeMemorySize);

if (this->mPrimaryImage || this->mPrimaryMemory) {
KP_LOG_DEBUG(
Expand All @@ -78,7 +75,6 @@ Image::rebuild(void* data)
}

this->allocateMemoryCreateGPUResources();
this->updateRawData(data);
}

bool
Expand Down
2 changes: 1 addition & 1 deletion src/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Memory::unmapRawData()
void
Memory::updateRawData(void* data)
{
if (this->memoryType() != Memory::MemoryTypes::eStorage) {
if (this->memoryType() != Memory::MemoryTypes::eStorage && data != nullptr) {
this->mapRawData();
memcpy(this->mRawData, data, this->memorySize());
}
Expand Down
32 changes: 28 additions & 4 deletions src/Tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Tensor::Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
: Memory(physicalDevice, device, dataType, memoryType)
{
this->mSize = elementTotalCount;

// This is required if dataType is eCustom
this->mDataTypeMemorySize = elementMemorySize;

KP_LOG_DEBUG("Kompute Tensor constructor data length: {}, and type: {}",
Expand All @@ -23,7 +25,30 @@ Tensor::Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,

this->mDescriptorType = vk::DescriptorType::eStorageBuffer;

this->rebuild(data);
this->reserve();
this->updateRawData(data);
}

Tensor::Tensor(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t elementTotalCount,
uint32_t elementMemorySize,
const DataTypes& dataType,
const MemoryTypes& memoryType)
: Memory(physicalDevice, device, dataType, memoryType)
{
this->mSize = elementTotalCount;

// This is required if dataType is eCustom
this->mDataTypeMemorySize = elementMemorySize;

KP_LOG_DEBUG("Kompute Tensor constructor data length: {}, and type: {}",
elementTotalCount,
Memory::toString(memoryType));

this->mDescriptorType = vk::DescriptorType::eStorageBuffer;

this->reserve();
}

Tensor::~Tensor()
Expand All @@ -39,9 +64,9 @@ Tensor::~Tensor()
}

void
Tensor::rebuild(void* data)
Tensor::reserve()
{
KP_LOG_DEBUG("Kompute Tensor rebuilding with size {}", this->mSize);
KP_LOG_DEBUG("Reserving {} bytes for memory", this->mSize * this->mDataTypeMemorySize);

if (this->mPrimaryBuffer || this->mPrimaryMemory) {
KP_LOG_DEBUG(
Expand All @@ -50,7 +75,6 @@ Tensor::rebuild(void* data)
}

this->allocateMemoryCreateGPUResources();
this->updateRawData(data);
}

bool
Expand Down
124 changes: 114 additions & 10 deletions src/include/kompute/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ class Image : public Memory
init(data, dataSize, width, height, numChannels, tiling);
}

/**
* Constructor with no data provided.
*
* @param physicalDevice The physical device to use to fetch properties
* @param device The device to use to create the image and memory from
* @param width Width of the image in pixels
* @param height Height of the image in pixels
* @param dataType Data type for the image which is of type ImageDataTypes
* @param memoryType Type for the image which is of type MemoryTypes
* @param tiling Tiling mode to use for the image.
*/
Image(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t width,
uint32_t height,
uint32_t numChannels,
const DataTypes& dataType,
vk::ImageTiling tiling,
const MemoryTypes& memoryType = MemoryTypes::eDevice)
: Image(physicalDevice,
device,
nullptr,
0,
width,
height,
numChannels,
dataType,
tiling,
memoryType)
{
}

/**
* Constructor with data provided which would be used to create the
* respective vulkan image and memory. No tiling has been provided
Expand Down Expand Up @@ -92,18 +124,40 @@ class Image : public Memory
}

/**
* Destructor which is in charge of freeing vulkan resources unless they
* have been provided externally.
* Constructor with no data provided. No tiling has been provided
* so will be inferred from \p memoryType.
*
* @param physicalDevice The physical device to use to fetch properties
* @param device The device to use to create the image and memory from
* @param width Width of the image in pixels
* @param height Height of the image in pixels
* @param dataType Data type for the image which is of type ImageDataTypes
* @param memoryType Type for the image which is of type MemoryTypes
*/
virtual ~Image();
Image(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t width,
uint32_t height,
uint32_t numChannels,
const DataTypes& dataType,
const MemoryTypes& memoryType = MemoryTypes::eDevice)
: Image(physicalDevice,
device,
nullptr,
0,
width,
height,
numChannels,
dataType,
memoryType)
{
}

/**
* Function to trigger reinitialisation of the image and memory with
* new data .
*
* @param data Vector of data to use to initialise image from
* Destructor which is in charge of freeing vulkan resources unless they
* have been provided externally.
*/
void rebuild(void* data);
virtual ~Image();

/**
* Destroys and frees the GPU resources which include the image and memory.
Expand Down Expand Up @@ -282,6 +336,12 @@ class Image : public Memory
uint32_t height,
uint32_t numChannels,
vk::ImageTiling tiling);

/**
* Function to reserve memory on the image. This does not copy any data, it
* just reserves memory, similarly to std::vector reserve() method.
*/
void reserve();
};

template<typename T>
Expand All @@ -304,7 +364,7 @@ class ImageT : public Image
width,
height,
numChannels,
this->dataType<T>(),
Memory::dataType<T>(),
tiling,
imageType)
{
Expand All @@ -330,7 +390,7 @@ class ImageT : public Image
width,
height,
numChannels,
this->dataType<T>(),
Memory::dataType<T>(),
imageType)
{
KP_LOG_DEBUG("Kompute imageT constructor with data size {}, width {}, "
Expand All @@ -341,6 +401,50 @@ class ImageT : public Image
numChannels);
}

ImageT(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t width,
uint32_t height,
uint32_t numChannels,
vk::ImageTiling tiling,
const MemoryTypes& imageType = MemoryTypes::eDevice)
: Image(physicalDevice,
device,
width,
height,
numChannels,
Memory::dataType<T>(),
tiling,
imageType)
{
KP_LOG_DEBUG("Kompute imageT constructor with no data, width {}, "
"height {}, and num channels {}",
width,
height,
numChannels);
}

ImageT(std::shared_ptr<vk::PhysicalDevice> physicalDevice,
std::shared_ptr<vk::Device> device,
uint32_t width,
uint32_t height,
uint32_t numChannels,
const MemoryTypes& imageType = MemoryTypes::eDevice)
: Image(physicalDevice,
device,
width,
height,
numChannels,
Memory::dataType<T>(),
imageType)
{
KP_LOG_DEBUG("Kompute imageT constructor with no data, width {}, "
"height {}, and num channels {}",
width,
height,
numChannels);
}

~ImageT() { KP_LOG_DEBUG("Kompute imageT destructor"); }

std::vector<T> vector() { return Memory::vector<T>(); }
Expand Down
Loading

0 comments on commit d44aa42

Please sign in to comment.