Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve asserted_cast #95

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ void App::drawProfiling(
size_t longestNameLength = 0;
for (const auto &t : profilerDatas)
if (t.name.size() > longestNameLength)
longestNameLength = asserted_cast<size_t>(t.name.size());
longestNameLength = t.name.size();

String tmp{scopeAlloc};
tmp.resize(longestNameLength * 2);
Expand Down Expand Up @@ -859,9 +859,7 @@ void App::drawMemory(uint32_t scopeHighWatermark) const
TlsfAllocator::Stats const &allocStats = gAllocators.general.stats();

ImGui::Text("High watermarks:\n");
ImGui::Text(
" ctors : %uKB\n",
asserted_cast<uint32_t>(m_ctorScratchHighWatermark) / 1024);
ImGui::Text(" ctors : %uKB\n", m_ctorScratchHighWatermark / 1024);
ImGui::Text(
" deferred general: %uMB\n",
asserted_cast<uint32_t>(
Expand Down
4 changes: 2 additions & 2 deletions src/render/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,10 @@ ImageHandle Renderer::blitColorToFinalComposite(
},
vk::Offset3D{
std::min(
asserted_cast<int32_t>(dstOffset.x + dstSize.x),
dstOffset.x + dstSize.x,
asserted_cast<int32_t>(swapImageExtent.width)),
std::min(
asserted_cast<int32_t>(dstOffset.y + dstSize.y),
dstOffset.y + dstSize.y,
asserted_cast<int32_t>(swapImageExtent.height)),
1,
},
Expand Down
16 changes: 8 additions & 8 deletions src/scene/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ void Texture2D::init(
WHEELS_ASSERT(!dds.data.empty());

const vk::Extent2D extent{
asserted_cast<uint32_t>(dds.width),
asserted_cast<uint32_t>(dds.height),
dds.width,
dds.height,
};

WHEELS_ASSERT(stagingBuffer.mapped != nullptr);
Expand Down Expand Up @@ -461,7 +461,7 @@ void Texture2D::init(
.imageSubresource =
vk::ImageSubresourceLayers{
.aspectMask = vk::ImageAspectFlagBits::eColor,
.mipLevel = asserted_cast<uint32_t>(i),
.mipLevel = i,
.baseArrayLayer = 0,
.layerCount = 1,
},
Expand Down Expand Up @@ -500,9 +500,9 @@ void Texture3D::init(
WHEELS_ASSERT(!dds.data.empty());

const vk::Extent3D extent{
asserted_cast<uint32_t>(dds.width),
asserted_cast<uint32_t>(dds.height),
asserted_cast<uint32_t>(dds.depth),
dds.width,
dds.height,
dds.depth,
};

// Just create the staging here as Texture3D are only loaded in during load
Expand Down Expand Up @@ -600,8 +600,8 @@ void TextureCubemap::init(
.desc =
ImageDescription{
.format = cube.format,
.width = asserted_cast<uint32_t>(cube.width),
.height = asserted_cast<uint32_t>(cube.height),
.width = cube.width,
.height = cube.height,
.mipCount = cube.mipLevelCount,
.layerCount = cube.faceCount,
.createFlags = vk::ImageCreateFlagBits::eCubeCompatible,
Expand Down
4 changes: 1 addition & 3 deletions src/scene/WorldData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,9 +1405,7 @@ void WorldData::gatherScene(
{
const uint32_t childIndex = sceneNode.firstChild + i;
scene.nodes[childIndex].parent = indices.sceneNode;
nodeStack.emplace_back(
asserted_cast<uint32_t>(tmpNode.children[i]),
asserted_cast<uint32_t>(childIndex));
nodeStack.emplace_back(tmpNode.children[i], childIndex);

scene.fullNodeNames.emplace_back(
gAllocators.general, sceneNode.fullName);
Expand Down
53 changes: 24 additions & 29 deletions src/utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,41 @@ const size_t sMaxMsVertices = 64;
const size_t sMaxMsTriangles = 124;

// Statically casts a into T, asserts that the value fits in T if T is integral
template <typename T, typename V> constexpr T asserted_cast(V a)
template <typename T, typename V>
constexpr T asserted_cast(V a)
requires(!std::is_same_v<T, V> && std::is_integral_v<T>)
{
#ifndef NDEBUG
static_assert(
!std::is_floating_point_v<T> &&
"No assertions for floating point target type");

if constexpr (!std::is_same_v<T, V> && std::is_integral_v<T>)
if (a >= 0)
{
if (a >= 0)
if constexpr (std::is_integral_v<V>)
{
if constexpr (std::is_integral_v<V>)
{
if constexpr ((sizeof(T) < sizeof(V) ||
(sizeof(T) == sizeof(V) && std::is_signed_v<T> &&
!std::is_signed_v<V>)))
{
if (a > static_cast<V>(std::numeric_limits<T>::max()))
WHEELS_ASSERT(!"overflow");
}
}
else
if constexpr ((sizeof(T) < sizeof(V) ||
(sizeof(T) == sizeof(V) && std::is_signed_v<T> &&
!std::is_signed_v<V>)))
{
// NOTE: Some edge cases with large target types will be weird
// here because of precision and truncation
if (a > static_cast<V>(std::numeric_limits<T>::max()))
WHEELS_ASSERT(!"overflow");
if (a < static_cast<V>(std::numeric_limits<T>::min()))
WHEELS_ASSERT(!"underflow");
}
}
else if constexpr (!std::numeric_limits<T>::is_signed)
WHEELS_ASSERT(!"Trying to cast negative into unsigned");
else
{
if constexpr (sizeof(T) < sizeof(V))
{
if (a < static_cast<V>(std::numeric_limits<T>::min()))
WHEELS_ASSERT(!"underflow");
}
// NOTE: Some edge cases with large target types will be weird
// here because of precision and truncation
if (a > static_cast<V>(std::numeric_limits<T>::max()))
WHEELS_ASSERT(!"overflow");
if (a < static_cast<V>(std::numeric_limits<T>::min()))
WHEELS_ASSERT(!"underflow");
}
}
else if constexpr (!std::numeric_limits<T>::is_signed)
WHEELS_ASSERT(!"Trying to cast negative into unsigned");
else
{
if constexpr (sizeof(T) < sizeof(V))
{
if (a < static_cast<V>(std::numeric_limits<T>::min()))
WHEELS_ASSERT(!"underflow");
}
}
#endif // NDEBUG
Expand Down
Loading