Skip to content

Commit

Permalink
Add asserts for custom data types not being support in Images
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 2, 2024
1 parent 4417e0d commit 921577a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 64 deletions.
63 changes: 0 additions & 63 deletions src/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,67 +324,4 @@ Memory::destroy(void)
}
}

template<>
Memory::DataTypes
Memory::dataType<bool>()
{
return DataTypes::eBool;
}

template<>
Memory::DataTypes
Memory::dataType<int8_t>()
{
return DataTypes::eChar;
}

template<>
Memory::DataTypes
Memory::dataType<uint8_t>()
{
return DataTypes::eUnsignedChar;
}

template<>
Memory::DataTypes
Memory::dataType<int16_t>()
{
return DataTypes::eShort;
}

template<>
Memory::DataTypes
Memory::dataType<uint16_t>()
{
return DataTypes::eUnsignedShort;
}

template<>
Memory::DataTypes
Memory::dataType<int32_t>()
{
return DataTypes::eInt;
}

template<>
Memory::DataTypes
Memory::dataType<uint32_t>()
{
return DataTypes::eUnsignedInt;
}

template<>
Memory::DataTypes
Memory::dataType<float>()
{
return DataTypes::eFloat;
}

template<>
Memory::DataTypes
Memory::dataType<double>()
{
return DataTypes::eDouble;
}

} // end namespace kp
22 changes: 22 additions & 0 deletions src/include/kompute/Image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class Image : public Memory
const MemoryTypes& memoryType = MemoryTypes::eDevice)
: Memory(physicalDevice, device, dataType, memoryType, x, y)
{
if (dataType == DataTypes::eCustom)
{
throw std::runtime_error("Custom data types are not supported for Kompute Images");
}

init(data, dataSize, numChannels, tiling);
}

Expand Down Expand Up @@ -110,6 +115,11 @@ class Image : public Memory
{
vk::ImageTiling tiling;

if (dataType == DataTypes::eCustom)
{
throw std::runtime_error("Custom data types are not supported for Kompute Images");
}

if (memoryType == MemoryTypes::eHost) {
// Host-accessible memory must be linear-tiled.
tiling = vk::ImageTiling::eLinear;
Expand Down Expand Up @@ -393,6 +403,9 @@ class ImageT : public Image
tiling,
imageType)
{
// Images cannot be created with custom types
static_assert(Memory::dataType<T>() != DataTypes::eCustom, "Custom data types are not supported for Kompute Images");

KP_LOG_DEBUG("Kompute imageT constructor with data size {}, x {}, "
"y {}, and num channels {}",
data.size(),
Expand All @@ -418,6 +431,9 @@ class ImageT : public Image
Memory::dataType<T>(),
imageType)
{
// Images cannot be created with custom types
static_assert(Memory::dataType<T>() != DataTypes::eCustom, "Custom data types are not supported for Kompute Images");

KP_LOG_DEBUG("Kompute imageT constructor with data size {}, x {}, "
"y {}, and num channels {}",
data.size(),
Expand All @@ -442,6 +458,9 @@ class ImageT : public Image
tiling,
imageType)
{
// Images cannot be created with custom types
static_assert(Memory::dataType<T>() != DataTypes::eCustom, "Custom data types are not supported for Kompute Images");

KP_LOG_DEBUG("Kompute imageT constructor with no data, x {}, "
"y {}, and num channels {}",
x,
Expand All @@ -463,6 +482,9 @@ class ImageT : public Image
Memory::dataType<T>(),
imageType)
{
// Images cannot be created with custom types
static_assert(Memory::dataType<T>() != DataTypes::eCustom, "Custom data types are not supported for Kompute Images");

KP_LOG_DEBUG("Kompute imageT constructor with no data, x {}, "
"y {}, and num channels {}",
x,
Expand Down
65 changes: 64 additions & 1 deletion src/include/kompute/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Memory
* @return Data type of T of type kp::Memory::DataTypes
*/
template<typename T>
static DataTypes dataType();
static constexpr DataTypes dataType();

/**
* Retrieve the data type of the tensor (int, float, unsigned short etc.)
Expand Down Expand Up @@ -332,4 +332,67 @@ class Memory
std::shared_ptr<Image> copyFromMemory) = 0;
};

template<>
constexpr Memory::DataTypes
Memory::dataType<bool>()
{
return DataTypes::eBool;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<int8_t>()
{
return DataTypes::eChar;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<uint8_t>()
{
return DataTypes::eUnsignedChar;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<int16_t>()
{
return DataTypes::eShort;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<uint16_t>()
{
return DataTypes::eUnsignedShort;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<int32_t>()
{
return DataTypes::eInt;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<uint32_t>()
{
return DataTypes::eUnsignedInt;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<float>()
{
return DataTypes::eFloat;
}

template<>
constexpr Memory::DataTypes
Memory::dataType<double>()
{
return DataTypes::eDouble;
}

} // End namespace kp
9 changes: 9 additions & 0 deletions test/TestImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,12 @@ TEST(TestImage, GetNumChannels)
// Check the number of channels matches
EXPECT_EQ(image->getNumChannels(), 1);
}

TEST(TestImage, InvalidDataType)
{
kp::Manager mgr;

// Custom data types are not supported for images.
EXPECT_THROW(mgr.image(3, 3, 1, kp::Memory::DataTypes::eCustom), std::runtime_error);
EXPECT_THROW(mgr.image(3, 3, 1, kp::Memory::DataTypes::eCustom, vk::ImageTiling::eOptimal), std::runtime_error);
}

0 comments on commit 921577a

Please sign in to comment.