Skip to content

Commit

Permalink
Debugging...
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawoodoz committed Feb 9, 2025
1 parent 35af2ed commit b701601
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 125 deletions.
34 changes: 17 additions & 17 deletions Source/DFPSR/base/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@
#define CREATE_METHOD_PRINT(VECTOR_TYPE, ELEMENT_TYPE, LANE_COUNT) \
inline dsr::String& string_toStreamIndented(dsr::String& target, const VECTOR_TYPE& source, const dsr::ReadableString& indentation) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
source.writeAlignedUnsafe(a); \
source.writeAlignedUnsafe(&(a[0])); \
dsr::string_append(target, indentation, a[0]); \
for (int i = 1; i < LANE_COUNT; i++) { \
string_append(target, U", ", a[i]); \
Expand All @@ -1937,8 +1937,8 @@
inline bool allLanesEqual(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (a[i] != b[i]) return false; \
} \
Expand All @@ -1947,8 +1947,8 @@
inline bool allLanesNotEqual(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (a[i] == b[i]) return false; \
} \
Expand All @@ -1963,8 +1963,8 @@
inline bool allLanesEqual(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (fabs(a[i] - b[i]) >= 0.0001f) return false; \
} \
Expand All @@ -1973,8 +1973,8 @@
inline bool allLanesNotEqual(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (fabs(a[i] - b[i]) < 0.0001f) return false; \
} \
Expand All @@ -1988,8 +1988,8 @@
inline bool allLanesGreater(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (a[i] <= b[i]) return false; \
} \
Expand All @@ -1998,8 +1998,8 @@
inline bool allLanesGreaterOrEqual(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (a[i] < b[i]) return false; \
} \
Expand All @@ -2008,8 +2008,8 @@
inline bool allLanesLesser(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (a[i] >= b[i]) return false; \
} \
Expand All @@ -2018,8 +2018,8 @@
inline bool allLanesLesserOrEqual(const VECTOR_TYPE& left, const VECTOR_TYPE& right) { \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE a[LANE_COUNT]; \
ALIGN_BYTES(sizeof(VECTOR_TYPE)) ELEMENT_TYPE b[LANE_COUNT]; \
left.writeAlignedUnsafe(a); \
right.writeAlignedUnsafe(b); \
left.writeAlignedUnsafe(&(a[0])); \
right.writeAlignedUnsafe(&(b[0])); \
for (int i = 0; i < LANE_COUNT; i++) { \
if (a[i] > b[i]) return false; \
} \
Expand Down
128 changes: 20 additions & 108 deletions Source/test/tests/TextureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,109 +6,20 @@
#define ASSERT_EQUAL_SIMD(A, B) ASSERT_COMP(A, B, allLanesEqual, "==")
#define ASSERT_NOTEQUAL_SIMD(A, B) ASSERT_COMP(A, B, !allLanesEqual, "!=")

inline U32x8 shiftRight_test(const U32x8& left, const U32x8 &bitOffsets) {
stateName = U"shiftRight_test: Checking left alignment.\n";
assert((uintptr_t(&left) & 31u) == 0);
stateName = U"shiftRight_test: Checking bitOffsets alignment.\n";
assert((uintptr_t(&bitOffsets) & 31u) == 0);
#ifdef SAFE_POINTER_CHECKS
stateName = U"shiftRight_test: Checking offsets.\n";
if(!allLanesLesser(bitOffsets, U32x8(32u))) {
stateName = U"shiftRight_test: Throwing error.\n";
throwError(U"Tried to shift ", left, U" by bit offsets ", bitOffsets, U", which is non-deterministic from being out of bound 0..31!\n");
}
#endif
#if defined(USE_AVX2)
stateName = U"shiftRight_test: Scalar fallback bit shifting.\n";
IMPL_SCALAR_FALLBACK_INFIX_8_LANES(left, bitOffsets, U32x8, uint32_t, >>)
#else
stateName = U"shiftRight_test: Scalar reference bit shifting.\n";
IMPL_SCALAR_REFERENCE_INFIX_8_LANES(left, bitOffsets, U32x8, uint32_t, >>)
#endif
}

template<
bool SQUARE = false, // Width and height must be the same.
bool SINGLE_LAYER = false, // Demanding that the texture only has a single layer.
bool XY_INSIDE = false, // No pixels may be sampled outside.
bool MIP_INSIDE = false, // Mip level may not go outside of existing layer indices.
bool HIGHEST_RESOLUTION = false, // Ignoring any lower layers.
typename U, // uint32_t, U32x4, U32x8, U32xX
DSR_ENABLE_IF(DSR_CHECK_PROPERTY(DsrTrait_Any_U32, U))>
U texture_getPixelOffset_test(const TextureRgbaU8 &texture, U x, U y, const U &mipLevel) {
stateName = U"texture_getPixelOffset_test: Getting masks for the highest resolution.\n";
// Clamp the mip-level using bitwise operations in a logarithmic scale, by masking out excess bits with zeroes and filling missing bits with ones.
U tileMaskX = U(texture.impl_maxWidthAndMask );
U tileMaskY = U(texture.impl_maxHeightAndMask);
stateName = U"texture_getPixelOffset_test: Shifting masks.\n";
if (!HIGHEST_RESOLUTION) {
//tileMaskX = tileMaskX >> mipLevel;
//tileMaskY = tileMaskY >> mipLevel;
tileMaskX = shiftRight_test(tileMaskX, mipLevel);
tileMaskY = shiftRight_test(tileMaskY, mipLevel);
}
stateName = U"texture_getPixelOffset_test: Limiting mask for the lowest resolution.\n";
if (!MIP_INSIDE) {
// If the mip level index might be higher than what is used in the texture, make sure that the tile masks have at least enough bits for the lowest texture resolution.
tileMaskX = tileMaskX | texture.impl_minWidthOrMask;
if (!SQUARE) {
tileMaskY = tileMaskY | texture.impl_minHeightOrMask;
}
}
stateName = U"texture_getPixelOffset_test: Getting logarithmic width.\n";
U log2PixelStride = U(texture.impl_log2width);
if (!HIGHEST_RESOLUTION) {
log2PixelStride = log2PixelStride - mipLevel;
}
stateName = U"texture_getPixelOffset_test: Tiling texel coordinates.\n";
if (!XY_INSIDE) {
x = x & tileMaskX;
if (SQUARE) {
// Apply the same mask to both for square images, so that the other mask can be optimized away.
y = y & tileMaskX;
} else {
// Apply a separate mask for Y coordinates when the texture might not be square.
y = y & tileMaskY;
}
}
stateName = U"texture_getPixelOffset_test: Calculating offset.\n";
U coordinateOffset = ((y << log2PixelStride) | x);
stateName = U"texture_getPixelOffset_test: Safety checks.\n";
#ifndef NDEBUG
// In debug mode, wrong use of optimization arguments will throw errors.
if (SQUARE) {
if (texture.impl_log2width != texture.impl_log2height) {
throwError(U"texture_getPixelOffset was told that the texture would have square dimensions using SQUARE, but ", texture_getMaxWidth(texture), U"x", texture_getMaxHeight(texture), U" is not square!\n");
}
}
if (SINGLE_LAYER) {
if (texture_getSmallestMipLevel(texture) > 0) {
throwError(U"texture_getPixelOffset was told that the texture would only have a single layer using SINGLE_LAYER, but it has ", texture_getSmallestMipLevel(texture) + 1, U" layers!\n");
}
}
if (XY_INSIDE) {
if (!(allLanesEqual(x & ~tileMaskX, U(0)) && allLanesEqual(y & ~tileMaskY, U(0)))) {
throwError(U"texture_getPixelOffset was told that the pixel coordinates would stay inside using XY_INSIDE, but the coordinate (", x, U", ", y, U") is not within", texture_getMaxWidth(texture), U"x", texture_getMaxHeight(texture), U" pixels!\n");
}
}
if (!HIGHEST_RESOLUTION) {
if (!allLanesLesserOrEqual(mipLevel, U(15u))) {
throwError(U"texture_getPixelOffset got mip level ", mipLevel, U", which is not within the fixed range of 0..15!\n");
}
if (MIP_INSIDE) {
if (!allLanesLesserOrEqual(mipLevel, U(texture_getSmallestMipLevel(texture)))) {
throwError(U"texture_getPixelOffset was told that the mip level would stay within valid indices using MIP_INSIDE, but mip level ", mipLevel, U" is not within 0..", texture_getSmallestMipLevel(texture), U"!\n");
}
}
}
#endif
stateName = U"texture_getPixelOffset_test: Calculating final result.\n";
if (SINGLE_LAYER) {
return coordinateOffset;
} else {
U startOffset = texture_getPixelOffsetToLayer<HIGHEST_RESOLUTION, U>(texture, mipLevel);
return startOffset + coordinateOffset;
static bool allLanesEqual_test(const U32x4& left, const U32x4& right) {
ALIGN_BYTES(sizeof(U32x4)) uint32_t a[4];
ALIGN_BYTES(sizeof(U32x4)) uint32_t b[4];
stateName = U"allLanesEqual_test: Writing left side to array.\n";
left.writeAlignedUnsafe(&(a[0]));
stateName = U"allLanesEqual_test: Writing right side to array.\n";
right.writeAlignedUnsafe(&(b[0]));
for (int i = 0; i < 4; i++) {
stateName = U"allLanesEqual_test: Comparing elements.\n";
if (a[i] != b[i]) return false;
}
stateName = U"allLanesEqual_test: Passed comparison.\n";
return true;
}

START_TEST(Texture)
Expand All @@ -120,20 +31,21 @@ START_TEST(Texture)
U32x4( 0, 128, 256, 256)
);
U32x4 expectedColor = packOrder_packBytes(U32x4(255, 195, 62, 127), U32x4(255, 123, 152, 93), U32x4( 0, 162, 62, 200), U32x4( 0, 144, 180, 124));
ASSERT_EQUAL_SIMD(mixedColor, expectedColor);

stateName = U"allLanesEqual_test: Start.\n";
bool result = allLanesEqual_test(mixedColor, expectedColor);
stateName = U"allLanesEqual_test: End.\n";
ASSERT(result);

//ASSERT_EQUAL_SIMD(mixedColor, expectedColor);
}
{
// 1x1, 2x2, 4x4, 8x8, 16x16
TextureRgbaU8 texture = TextureRgbaU8(4, 4);
{
U32x8 pixelOffsets = texture_getPixelOffset_test(texture, U32x8(0u, 1u, 2u, 3u, 0u, 1u, 2u, 3u), U32x8(0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u), U32x8(0u));
ASSERT(allLanesEqual((pixelOffsets), U32x8(85u, 86u, 87u, 88u, 101u, 102u, 103u, 104u)));
}
{
stateName = U"Getting eight pixel offsets.\n";
U32x8 pixelOffsets = texture_getPixelOffset(texture, U32x8(0u, 1u, 2u, 3u, 0u, 1u, 2u, 3u), U32x8(0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u), U32x8(0u));
stateName = U"Comparing eight pixel offsets.\n";
ASSERT(allLanesEqual((pixelOffsets), U32x8(85u, 86u, 87u, 88u, 101u, 102u, 103u, 104u)));
ASSERT_EQUAL_SIMD(pixelOffsets, U32x8(85u, 86u, 87u, 88u, 101u, 102u, 103u, 104u));
}
ASSERT(texture_hasPyramid(texture));
ASSERT_EQUAL(texture_getMaxWidth(texture), 16);
Expand Down

0 comments on commit b701601

Please sign in to comment.