Skip to content

Commit

Permalink
Clang format
Browse files Browse the repository at this point in the history
Signed-off-by: Robert Quill <robert.quill@imgtec.com>
  • Loading branch information
robquill committed Sep 3, 2024
1 parent f7f0020 commit b23fb2f
Show file tree
Hide file tree
Showing 23 changed files with 258 additions and 225 deletions.
29 changes: 12 additions & 17 deletions src/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,31 +134,26 @@ Algorithm::createParameters()
KP_LOG_DEBUG("Kompute Algorithm createParameters started");

for (const std::shared_ptr<Memory>& mem : this->mMemObjects) {
if (mem->type() == Memory::Type::eImage) {
numImages++;
}
else {
numTensors++;
}
if (mem->type() == Memory::Type::eImage) {
numImages++;
} else {
numTensors++;
}
}

std::vector<vk::DescriptorPoolSize> descriptorPoolSizes;

if (numTensors > 0)
{
descriptorPoolSizes.push_back(
vk::DescriptorPoolSize(

if (numTensors > 0) {
descriptorPoolSizes.push_back(vk::DescriptorPoolSize(
vk::DescriptorType::eStorageBuffer,
static_cast<uint32_t>(numTensors) // Descriptor count
));
}

if (numImages > 0)
{
descriptorPoolSizes.push_back(
vk::DescriptorPoolSize(
vk::DescriptorType::eStorageImage,
static_cast<uint32_t>(numImages) // Descriptor count
if (numImages > 0) {
descriptorPoolSizes.push_back(vk::DescriptorPoolSize(
vk::DescriptorType::eStorageImage,
static_cast<uint32_t>(numImages) // Descriptor count
));
};

Expand Down
114 changes: 60 additions & 54 deletions src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ Image::init(void* data,
"Kompute Image attempted to create an image with no channels");
}

if (data != nullptr && dataSize < this->getX() * this->getY() * numChannels) {
if (data != nullptr &&
dataSize < this->getX() * this->getY() * numChannels) {
throw std::runtime_error(
"Kompute Image data is smaller than the requested image size");
}

if (numChannels > 4 || numChannels == 3) {
// We don't support 3-channel images because they are not supported by Metal or Mesa (llvmpipe)
// See comment here: https://github.com/KomputeProject/kompute/pull/388#discussion_r1720959531
// We don't support 3-channel images because they are not supported by
// Metal or Mesa (llvmpipe) See comment here:
// https://github.com/KomputeProject/kompute/pull/388#discussion_r1720959531
throw std::runtime_error(
"Kompute Images can only have up to 1, 2 or 4 channels");
}
Expand Down Expand Up @@ -92,9 +94,9 @@ Image::recordCopyFrom(const vk::CommandBuffer& commandBuffer,
vk::Offset3D offset = { 0, 0, 0 };

if (this->getX() != copyFromImage->getX() ||
this->getY() != copyFromImage->getY())
{
throw std::runtime_error("Kompute Image recordCopyFrom image sizes do not match");
this->getY() != copyFromImage->getY()) {
throw std::runtime_error(
"Kompute Image recordCopyFrom image sizes do not match");
}

vk::Extent3D size = { this->getX(), this->getY(), 1 };
Expand All @@ -105,12 +107,12 @@ Image::recordCopyFrom(const vk::CommandBuffer& commandBuffer,
"Kompute Image recordCopyFrom size {},{}.", size.width, size.height);

copyFromImage->recordPrimaryImageBarrier(
commandBuffer,
vk::AccessFlagBits::eMemoryRead,
vk::AccessFlagBits::eMemoryWrite,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferSrcOptimal);
commandBuffer,
vk::AccessFlagBits::eMemoryRead,
vk::AccessFlagBits::eMemoryWrite,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferSrcOptimal);

this->recordPrimaryImageBarrier(commandBuffer,
vk::AccessFlagBits::eMemoryRead,
Expand Down Expand Up @@ -172,21 +174,25 @@ Image::recordCopyFromStagingToDevice(const vk::CommandBuffer& commandBuffer)
KP_LOG_DEBUG("Kompute Image copying size {},{}.", size.width, size.height);

this->recordStagingImageBarrier(commandBuffer,
vk::AccessFlagBits::eMemoryRead,
vk::AccessFlagBits::eMemoryWrite,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferSrcOptimal);
vk::AccessFlagBits::eMemoryRead,
vk::AccessFlagBits::eMemoryWrite,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferSrcOptimal);

this->recordPrimaryImageBarrier(commandBuffer,
vk::AccessFlagBits::eMemoryRead,
vk::AccessFlagBits::eMemoryWrite,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferDstOptimal);

this->recordCopyImage(
commandBuffer, this->mStagingImage, this->mPrimaryImage, this->mStagingImageLayout, this->mPrimaryImageLayout, copyRegion);
vk::AccessFlagBits::eMemoryRead,
vk::AccessFlagBits::eMemoryWrite,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferDstOptimal);

this->recordCopyImage(commandBuffer,
this->mStagingImage,
this->mPrimaryImage,
this->mStagingImageLayout,
this->mPrimaryImageLayout,
copyRegion);
}

void
Expand Down Expand Up @@ -217,8 +223,12 @@ Image::recordCopyFromDeviceToStaging(const vk::CommandBuffer& commandBuffer)
vk::PipelineStageFlagBits::eTransfer,
vk::ImageLayout::eTransferDstOptimal);

this->recordCopyImage(
commandBuffer, this->mPrimaryImage, this->mStagingImage, this->mPrimaryImageLayout, this->mStagingImageLayout, copyRegion);
this->recordCopyImage(commandBuffer,
this->mPrimaryImage,
this->mStagingImage,
this->mPrimaryImageLayout,
this->mStagingImageLayout,
copyRegion);
}

void
Expand All @@ -229,12 +239,8 @@ Image::recordCopyImage(const vk::CommandBuffer& commandBuffer,
vk::ImageLayout dstLayout,
vk::ImageCopy copyRegion)
{
commandBuffer.copyImage(*srcImage,
srcLayout,
*dstImage,
dstLayout,
1,
&copyRegion);
commandBuffer.copyImage(
*srcImage, srcLayout, *dstImage, dstLayout, 1, &copyRegion);
}

void
Expand All @@ -254,7 +260,7 @@ Image::recordPrimaryMemoryBarrier(const vk::CommandBuffer& commandBuffer,
vk::AccessFlagBits dstAccessMask,
vk::PipelineStageFlagBits srcStageMask,
vk::PipelineStageFlagBits dstStageMask)
{
{
vk::ImageLayout dstImageLayout;

// Ideally the image would be set to eGeneral as soon as it was created
Expand All @@ -267,11 +273,11 @@ Image::recordPrimaryMemoryBarrier(const vk::CommandBuffer& commandBuffer,
dstImageLayout = this->mPrimaryImageLayout;

this->recordPrimaryImageBarrier(commandBuffer,
srcAccessMask,
dstAccessMask,
srcStageMask,
dstStageMask,
dstImageLayout);
srcAccessMask,
dstAccessMask,
srcStageMask,
dstStageMask,
dstImageLayout);
}

void
Expand All @@ -293,20 +299,20 @@ Image::recordStagingMemoryBarrier(const vk::CommandBuffer& commandBuffer,
dstImageLayout = this->mStagingImageLayout;

this->recordStagingImageBarrier(commandBuffer,
srcAccessMask,
dstAccessMask,
srcStageMask,
dstStageMask,
dstImageLayout);
srcAccessMask,
dstAccessMask,
srcStageMask,
dstStageMask,
dstImageLayout);
}

void
Image::recordPrimaryImageBarrier(const vk::CommandBuffer& commandBuffer,
vk::AccessFlagBits srcAccessMask,
vk::AccessFlagBits dstAccessMask,
vk::PipelineStageFlagBits srcStageMask,
vk::PipelineStageFlagBits dstStageMask,
vk::ImageLayout dstLayout)
vk::AccessFlagBits srcAccessMask,
vk::AccessFlagBits dstAccessMask,
vk::PipelineStageFlagBits srcStageMask,
vk::PipelineStageFlagBits dstStageMask,
vk::ImageLayout dstLayout)
{
KP_LOG_DEBUG("Kompute Image recording PRIMARY image memory barrier");

Expand All @@ -324,11 +330,11 @@ Image::recordPrimaryImageBarrier(const vk::CommandBuffer& commandBuffer,

void
Image::recordStagingImageBarrier(const vk::CommandBuffer& commandBuffer,
vk::AccessFlagBits srcAccessMask,
vk::AccessFlagBits dstAccessMask,
vk::PipelineStageFlagBits srcStageMask,
vk::PipelineStageFlagBits dstStageMask,
vk::ImageLayout dstLayout)
vk::AccessFlagBits srcAccessMask,
vk::AccessFlagBits dstAccessMask,
vk::PipelineStageFlagBits srcStageMask,
vk::PipelineStageFlagBits dstStageMask,
vk::ImageLayout dstLayout)
{
KP_LOG_DEBUG("Kompute Image recording STAGING image memory barrier");

Expand Down
3 changes: 2 additions & 1 deletion src/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ Manager::destroy()

if (this->mManageResources && this->mManagedMemObjects.size()) {
KP_LOG_DEBUG("Kompute Manager explicitly freeing memory objects");
for (const std::weak_ptr<Memory>& weakMemory : this->mManagedMemObjects) {
for (const std::weak_ptr<Memory>& weakMemory :
this->mManagedMemObjects) {
if (std::shared_ptr<Memory> memory = weakMemory.lock()) {
memory->destroy();
}
Expand Down
31 changes: 15 additions & 16 deletions src/Memory.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

#include <fmt/core.h>
#include "kompute/Memory.hpp"
#include "kompute/Tensor.hpp"
#include "kompute/Image.hpp"
#include "kompute/Tensor.hpp"
#include <fmt/core.h>

namespace kp {

Expand Down Expand Up @@ -249,30 +249,29 @@ Memory::getStagingMemoryPropertyFlags()

void
Memory::recordCopyFrom(const vk::CommandBuffer& commandBuffer,
std::shared_ptr<Memory> copyFromMemory)
std::shared_ptr<Memory> copyFromMemory)
{
if (copyFromMemory->dataType() != this->dataType()) {
throw std::runtime_error(fmt::format(
"Attempting to copy memory of different types from {} to {}",
Memory::toString(copyFromMemory->dataType()),
Memory::toString(this->dataType())));
"Attempting to copy memory of different types from {} to {}",
Memory::toString(copyFromMemory->dataType()),
Memory::toString(this->dataType())));
}

if (copyFromMemory->size() != this->size()) {
throw std::runtime_error(fmt::format(
"Attempting to copy tensors of different sizes from {} to {}",
copyFromMemory->size(),
this->size()));
"Attempting to copy tensors of different sizes from {} to {}",
copyFromMemory->size(),
this->size()));
}

if (copyFromMemory->type() == Memory::Type::eTensor) {
this->recordCopyFrom(commandBuffer, std::static_pointer_cast<Tensor>(copyFromMemory));
}
else if (copyFromMemory->type() == Memory::Type::eImage) {
this->recordCopyFrom(commandBuffer, std::static_pointer_cast<Image>(copyFromMemory));
}
else
{
this->recordCopyFrom(commandBuffer,
std::static_pointer_cast<Tensor>(copyFromMemory));
} else if (copyFromMemory->type() == Memory::Type::eImage) {
this->recordCopyFrom(commandBuffer,
std::static_pointer_cast<Image>(copyFromMemory));
} else {
throw std::runtime_error("Kompute Memory unsupported memory type");
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/OpAlgoDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ OpAlgoDispatch::record(const vk::CommandBuffer& commandBuffer)
for (const std::shared_ptr<Memory>& mem :
this->mAlgorithm->getMemObjects()) {

// For images the image layout needs to be set to eGeneral before using it for imageLoad/imageStore in a shader.
// For images the image layout needs to be set to eGeneral before using
// it for imageLoad/imageStore in a shader.
if (mem->type() == Memory::Type::eImage) {
std::shared_ptr<Image> image = std::static_pointer_cast<Image>(mem);

image->recordPrimaryImageBarrier(
commandBuffer,
vk::AccessFlagBits::eTransferWrite,
vk::AccessFlagBits::eShaderRead,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eComputeShader,
vk::ImageLayout::eGeneral);
}
else {
std::shared_ptr<Image> image = std::static_pointer_cast<Image>(mem);

image->recordPrimaryImageBarrier(
commandBuffer,
vk::AccessFlagBits::eTransferWrite,
vk::AccessFlagBits::eShaderRead,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eComputeShader,
vk::ImageLayout::eGeneral);
} else {
mem->recordPrimaryMemoryBarrier(
commandBuffer,
vk::AccessFlagBits::eTransferWrite,
vk::AccessFlagBits::eShaderRead,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eComputeShader);
commandBuffer,
vk::AccessFlagBits::eTransferWrite,
vk::AccessFlagBits::eShaderRead,
vk::PipelineStageFlagBits::eTransfer,
vk::PipelineStageFlagBits::eComputeShader);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/OpCopy.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0

#include "kompute/operations/OpCopy.hpp"
#include "kompute/Tensor.hpp"
#include "kompute/Image.hpp"
#include "kompute/Tensor.hpp"

namespace kp {

Expand Down Expand Up @@ -30,7 +30,8 @@ OpCopy::record(const vk::CommandBuffer& commandBuffer)

// We iterate from the second memory object onwards and record a copy to all
for (size_t i = 1; i < this->mMemObjects.size(); i++) {
this->mMemObjects[i]->recordCopyFrom(commandBuffer, this->mMemObjects[0]);
this->mMemObjects[i]->recordCopyFrom(commandBuffer,
this->mMemObjects[0]);
}
}

Expand All @@ -46,7 +47,8 @@ OpCopy::postEval(const vk::CommandBuffer& /*commandBuffer*/)
KP_LOG_DEBUG("Kompute OpCopy postEval called");

// Do not copy on CPU side if source is storage memory
if (this->mMemObjects[0]->memoryType() == kp::Memory::MemoryTypes::eStorage) {
if (this->mMemObjects[0]->memoryType() ==
kp::Memory::MemoryTypes::eStorage) {
KP_LOG_DEBUG("Kompute OpCopy not copying tensor source given "
"it's of eStorage type");
return;
Expand All @@ -60,7 +62,8 @@ OpCopy::postEval(const vk::CommandBuffer& /*commandBuffer*/)
"given it's of eStorage type");
continue;
}
this->mMemObjects[i]->setData(this->mMemObjects[0]->rawData(),this->mMemObjects[0]->memorySize());
this->mMemObjects[i]->setData(this->mMemObjects[0]->rawData(),
this->mMemObjects[0]->memorySize());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/OpSyncDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ OpSyncDevice::record(const vk::CommandBuffer& commandBuffer)
KP_LOG_DEBUG("Kompute OpSyncDevice record called");

for (size_t i = 0; i < this->mMemObjects.size(); i++) {
if (this->mMemObjects[i]->memoryType() == Tensor::MemoryTypes::eDevice) {
if (this->mMemObjects[i]->memoryType() ==
Tensor::MemoryTypes::eDevice) {
this->mMemObjects[i]->recordCopyFromStagingToDevice(commandBuffer);
}
}
Expand Down
Loading

0 comments on commit b23fb2f

Please sign in to comment.