Skip to content
Open
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
13 changes: 13 additions & 0 deletions momentum/rasterizer/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ using IntrinsicsModel = IntrinsicsModelT<float>;
constexpr size_t kSimdPacketSize = drjit::DefaultSize;
constexpr size_t kSimdAlignment = 4 * kSimdPacketSize;

// Compile-time sanity check: Ensure SIMD packet size is reasonable (typically 4, 8, 16, or 32)
// This helps catch misconfigured builds where DefaultSize might be incorrect
static_assert(
kSimdPacketSize >= 1 && kSimdPacketSize <= 64,
"kSimdPacketSize must be between 1 and 64. If this assertion fails, "
"drjit::DefaultSize may be misconfigured in your build environment.");

// Additional check: packet size should be a power of 2 for most SIMD implementations
static_assert(
(kSimdPacketSize & (kSimdPacketSize - 1)) == 0,
"kSimdPacketSize should be a power of 2 (1, 2, 4, 8, 16, 32, 64). "
"If this fails, check your drjit configuration.");

using IntP = drjit::Packet<int32_t, kSimdPacketSize>;
using UintP = drjit::Packet<uint32_t, kSimdPacketSize>;
using Uint8P = drjit::Packet<uint8_t, kSimdPacketSize>;
Expand Down
38 changes: 37 additions & 1 deletion momentum/rasterizer/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <mdspan/mdspan.hpp>
#include <momentum/common/aligned.h>
#include <momentum/common/exception.h>
#include <momentum/rasterizer/fwd.h>
#include <algorithm>
#include <array>
Expand Down Expand Up @@ -40,7 +41,31 @@ class Tensor {
explicit Tensor(const extents_t& extents, const T& defaultValue = T{}) : _extents(extents) {
auto size = calculateSize(extents);
if (size > 0) {
_data.resize(size, defaultValue);
// Sanity check: if size is unreasonably large (e.g. > 1GB for floats/ints),
// throw a more descriptive error than std::bad_alloc
constexpr index_t kMaxReasonableElements =
256 * 1024 * 1024; // 256M elements (~1GB for floats)
MT_THROW_IF(
size > kMaxReasonableElements,
"Tensor allocation size is unreasonably large: {} elements. "
"This may indicate a configuration error with SIMD packet size. "
"Expected extents: [{}]",
size,
formatExtents(extents));
try {
_data.resize(size, defaultValue);
} catch (const std::bad_alloc& e) {
MT_THROW(
"Failed to allocate tensor of size {} elements ({} bytes). "
"Extents: [{}]. "
"This may indicate misconfigured SIMD packet size (kSimdPacketSize={}). "
"Original error: {}",
size,
size * sizeof(T),
formatExtents(extents),
kSimdPacketSize,
e.what());
}
}
}

Expand Down Expand Up @@ -131,6 +156,17 @@ class Tensor {
}
return size;
}

static std::string formatExtents(const extents_t& extents) {
std::string result;
for (size_t i = 0; i < Rank; ++i) {
if (i > 0) {
result += ", ";
}
result += std::to_string(extents[i]);
}
return result;
}
};

using Tensor2f = Tensor<float, 2>;
Expand Down
6 changes: 6 additions & 0 deletions momentum/test/rasterizer/test_software_rasterizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ TEST(SoftwareRasterizer, OneQuad) {
const int width = 20;
const int height = 10;

// Add diagnostic information to help debug allocation issues
// This can help identify when SIMD packet size is misconfigured
SCOPED_TRACE(
"SIMD Configuration: kSimdPacketSize=" + std::to_string(kSimdPacketSize) +
", kSimdAlignment=" + std::to_string(kSimdAlignment));

// Create OpenCV intrinsics with no distortion
OpenCVDistortionParametersT<float> distortionParams; // All zeros by default
auto intrinsics = std::make_shared<OpenCVIntrinsicsModel>(
Expand Down