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 94b4c9d commit 8fcee0d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Source/DFPSR/api/textureAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ namespace dsr {
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(const TextureRgbaU8 &texture, U x, U y, const U &mipLevel) {
U texture_getPixelOffset(const TextureRgbaU8 &texture, const U &x, const U &y, const U &mipLevel) {
// 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);
Expand All @@ -182,17 +182,19 @@ namespace dsr {
if (!HIGHEST_RESOLUTION) {
log2PixelStride = log2PixelStride - mipLevel;
}
U tiledX = x;
U tiledY = y;
if (!XY_INSIDE) {
x = x & tileMaskX;
tiledX = tiledX & tileMaskX;
if (SQUARE) {
// Apply the same mask to both for square images, so that the other mask can be optimized away.
y = y & tileMaskX;
tiledY = tiledY & tileMaskX;
} else {
// Apply a separate mask for Y coordinates when the texture might not be square.
y = y & tileMaskY;
tiledY = tiledY & tileMaskY;
}
}
U coordinateOffset = ((y << log2PixelStride) | x);
U coordinateOffset = ((tiledY << log2PixelStride) | tiledX);
#ifndef NDEBUG
// In debug mode, wrong use of optimization arguments will throw errors.
if (SQUARE) {
Expand Down
112 changes: 112 additions & 0 deletions Source/test/tests/TextureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,108 @@
#define ASSERT_EQUAL_SIMD(A, B) ASSERT_COMP(A, B, allLanesEqual, "==")
#define ASSERT_NOTEQUAL_SIMD(A, B) ASSERT_COMP(A, B, !allLanesEqual, "!=")

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_getSomething_test(const TextureRgbaU8 &texture, const U &x, const U &y, const U &mipLevel) {
stateName = U"Getting something A.\n"; // Crashes here on the server!
// 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 );
stateName = U"Getting something B.\n"; // Crashes here on the server!
return tileMaskX;
}

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, const U &x, const U &y, const U &mipLevel) {
stateName = U"Getting eight pixel offsets A.\n"; // Crashes here on the server!
// 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"Getting eight pixel offsets A.\n";
if (!HIGHEST_RESOLUTION) {
tileMaskX = tileMaskX >> mipLevel;
tileMaskY = tileMaskY >> mipLevel;
}
stateName = U"Getting eight pixel offsets A.\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"Getting eight pixel offsets A.\n";
U log2PixelStride = U(texture.impl_log2width);
if (!HIGHEST_RESOLUTION) {
stateName = U"Getting eight pixel offsets A.\n";
log2PixelStride = log2PixelStride - mipLevel;
}
stateName = U"Getting untiled coordinates.\n";
U tiledX = x;
U tiledY = y;
stateName = U"Getting eight pixel offsets A.\n";
if (!XY_INSIDE) {
tiledX = tiledX & tileMaskX;
stateName = U"Getting eight pixel offsets A.\n";
if (SQUARE) {
// Apply the same mask to both for square images, so that the other mask can be optimized away.
tiledY = tiledY & tileMaskX;
} else {
// Apply a separate mask for Y coordinates when the texture might not be square.
tiledY = tiledY & tileMaskY;
}
}
stateName = U"Getting eight pixel offsets A.\n";
U coordinateOffset = ((tiledY << log2PixelStride) | tiledX);
stateName = U"Getting eight pixel offsets A.\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
if (SINGLE_LAYER) {
return coordinateOffset;
} else {
U startOffset = texture_getPixelOffsetToLayer<HIGHEST_RESOLUTION, U>(texture, mipLevel);
return startOffset + coordinateOffset;
}
}

START_TEST(Texture)
{
// Linear blending of colors using unsigned integers.
Expand All @@ -20,12 +122,22 @@ START_TEST(Texture)
{
// 1x1, 2x2, 4x4, 8x8, 16x16
TextureRgbaU8 texture = TextureRgbaU8(4, 4);
{
stateName = U"Getting something TEST.\n";
U32x8 something = texture_getSomething_test(texture, U32x8(0u, 1u, 2u, 3u, 0u, 1u, 2u, 3u), U32x8(0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u), U32x8(0u));
stateName = U"Getting eight pixel offsets TEST.\n";
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));
stateName = U"Comparing eight pixel offsets.\n";
ASSERT_EQUAL_SIMD(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_EQUAL_SIMD(pixelOffsets, U32x8(85u, 86u, 87u, 88u, 101u, 102u, 103u, 104u));
}
*/
ASSERT(texture_hasPyramid(texture));
ASSERT_EQUAL(texture_getMaxWidth(texture), 16);
ASSERT_EQUAL(texture_getMaxHeight(texture), 16);
Expand Down

0 comments on commit 8fcee0d

Please sign in to comment.