From 37d8529a9bbfdfd9f0cb4708c6c72301423ca733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mirza=20Halil=C4=8Devi=C4=87?= <109971222+mirza-halilcevic@users.noreply.github.com> Date: Thu, 28 Dec 2023 14:22:01 +0100 Subject: [PATCH] EXSWHTEC-318 - Implement tests for 2D texture device functions #367 Change-Id: I5404eae219d23dc058aa6ef150a0764b06ab6de1 --- catch/unit/texture/CMakeLists.txt | 2 + catch/unit/texture/test_fixture.hh | 3 +- catch/unit/texture/tex2D.cc | 174 ++++++++++++++++++++++ catch/unit/texture/tex2DLayered.cc | 183 ++++++++++++++++++++++++ catch/unit/texture/texture_reference.hh | 11 +- 5 files changed, 363 insertions(+), 10 deletions(-) create mode 100644 catch/unit/texture/tex2D.cc create mode 100644 catch/unit/texture/tex2DLayered.cc diff --git a/catch/unit/texture/CMakeLists.txt b/catch/unit/texture/CMakeLists.txt index 73394005b6..14a74de8f5 100644 --- a/catch/unit/texture/CMakeLists.txt +++ b/catch/unit/texture/CMakeLists.txt @@ -42,6 +42,8 @@ set(TEST_SRC hipTextureObj3DCheckModes.cc hipTextureObj1DCheckSRGBModes.cc hipTextureObj2DCheckSRGBModes.cc + tex2D.cc + tex2DLayered.cc hipTexObjectTests.cc hipTextureObjectTests.cc hipBindTextureToMipmappedArray.cc diff --git a/catch/unit/texture/test_fixture.hh b/catch/unit/texture/test_fixture.hh index 47ac8b73bc..0572fd6fae 100644 --- a/catch/unit/texture/test_fixture.hh +++ b/catch/unit/texture/test_fixture.hh @@ -126,7 +126,8 @@ template struct TextureTestFix SetVec4(host_alloc.ptr()[i], i + test_value_offset); } - hipMemcpy3DParms memcpy_params = {}; + hipMemcpy3DParms memcpy_params; + memset(&memcpy_params, 0 sizeof(hipMemcpy3DParms)); memcpy_params.dstArray = tex_alloc_d.ptr(); memcpy_params.extent = params.LayeredExtent(); memcpy_params.extent.height = memcpy_params.extent.height ?: 1; diff --git a/catch/unit/texture/tex2D.cc b/catch/unit/texture/tex2D.cc new file mode 100644 index 0000000000..79d6055ede --- /dev/null +++ b/catch/unit/texture/tex2D.cc @@ -0,0 +1,174 @@ +/* +Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include + +#include "kernels.hh" +#include "test_fixture.hh" + +/** + * @addtogroup tex2D tex2D + * @{ + * @ingroup TextureTest + */ + +/** + * Test Description + * ------------------------ + * - Test texture fetching with `tex2D` and read mode set to `hipReadModeElementType`. The + * test is performed with: + * - normalized coordinates + * - non-normalized coordinates + * - Nearest-point sampling + * - Linear filtering + * - All combinations of different addressing modes. + * Test source + * ------------------------ + * - unit/texture/tex2D.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 5.2 + */ +TEMPLATE_TEST_CASE("Unit_tex2D_Positive_ReadModeElementType", "", char, unsigned char, short, + unsigned short, int, unsigned int, float) { + TextureTestParams params = {0}; + params.extent = make_hipExtent(16, 4, 0); + params.num_subdivisions = 4; + params.GenerateTextureDesc(); + + TextureTestFixture fixture{params}; + + const auto [num_threads_x, num_blocks_x] = GetLaunchConfig(32, params.NumItersX()); + const auto [num_threads_y, num_blocks_y] = GetLaunchConfig(32, params.NumItersY()); + + dim3 dim_grid; + dim_grid.x = num_blocks_x; + dim_grid.y = num_blocks_y; + + dim3 dim_block; + dim_block.x = num_threads_x; + dim_block.y = num_threads_y; + + tex2DKernel><<>>( + fixture.out_alloc_d.ptr(), params.NumItersX(), params.NumItersY(), fixture.tex.object(), + params.Width(), params.Height(), params.num_subdivisions, params.tex_desc.normalizedCoords); + HIP_CHECK(hipGetLastError()); + + fixture.LoadOutput(); + + for (auto j = 0u; j < params.NumItersY(); ++j) { + for (auto i = 0u; i < params.NumItersX(); ++i) { + float x = GetCoordinate(i, params.NumItersX(), params.Width(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + float y = GetCoordinate(j, params.NumItersY(), params.Height(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + + INFO("i: " << i); + INFO("j: " << j); + INFO("Normalized coordinates: " << std::boolalpha << params.tex_desc.normalizedCoords); + INFO("Address mode X: " << AddressModeToString(params.tex_desc.addressMode[0])); + INFO("Address mode Y: " << AddressModeToString(params.tex_desc.addressMode[1])); + INFO("x: " << std::fixed << std::setprecision(16) << x); + INFO("y: " << std::fixed << std::setprecision(16) << y); + + auto index = j * params.NumItersX() + i; + + const auto ref_val = fixture.tex_h.Tex2D(x, y, params.tex_desc); + REQUIRE(ref_val.x == fixture.out_alloc_h[index].x); + REQUIRE(ref_val.y == fixture.out_alloc_h[index].y); + REQUIRE(ref_val.z == fixture.out_alloc_h[index].z); + REQUIRE(ref_val.w == fixture.out_alloc_h[index].w); + } + } +} + +/** + * Test Description + * ------------------------ + * - Test texture fetching with `tex2D` and read mode set to `hipReadModeNormalizedFloat`. The + * test is performed with: + * - normalized coordinates + * - non-normalized coordinates + * - Nearest-point sampling + * - Linear filtering + * - All combinations of different addressing modes. + * Test source + * ------------------------ + * - unit/texture/tex2D.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 5.2 + */ +TEMPLATE_TEST_CASE("Unit_tex2D_Positive_ReadModeNormalizedFloat", "", char, unsigned char, short, + unsigned short) { + TextureTestParams params = {0}; + params.extent = make_hipExtent(16, 4, 0); + params.num_subdivisions = 4; + params.GenerateTextureDesc(hipReadModeNormalizedFloat); + + TextureTestFixture fixture{params}; + + const auto [num_threads_x, num_blocks_x] = GetLaunchConfig(32, params.NumItersX()); + const auto [num_threads_y, num_blocks_y] = GetLaunchConfig(32, params.NumItersY()); + + dim3 dim_grid; + dim_grid.x = num_blocks_x; + dim_grid.y = num_blocks_y; + + dim3 dim_block; + dim_block.x = num_threads_x; + dim_block.y = num_threads_y; + + tex2DKernel><<>>( + fixture.out_alloc_d.ptr(), params.NumItersX(), params.NumItersY(), fixture.tex.object(), + params.Width(), params.Height(), params.num_subdivisions, params.tex_desc.normalizedCoords); + HIP_CHECK(hipGetLastError()); + + fixture.LoadOutput(); + + for (auto j = 0u; j < params.NumItersY(); ++j) { + for (auto i = 0u; i < params.NumItersX(); ++i) { + float x = GetCoordinate(i, params.NumItersX(), params.Width(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + float y = GetCoordinate(j, params.NumItersY(), params.Height(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + + INFO("i: " << i); + INFO("j: " << j); + INFO("Filtering mode: " << FilteringModeToString(params.tex_desc.filterMode)); + INFO("Normalized coordinates: " << std::boolalpha << params.tex_desc.normalizedCoords); + INFO("Address mode X: " << AddressModeToString(params.tex_desc.addressMode[0])); + INFO("Address mode Y: " << AddressModeToString(params.tex_desc.addressMode[1])); + INFO("x: " << std::fixed << std::setprecision(16) << x); + INFO("y: " << std::fixed << std::setprecision(16) << y); + + auto index = j * params.NumItersX() + i; + + auto ref_val = + Vec4Map(fixture.tex_h.Tex2D(x, y, params.tex_desc), NormalizeInteger); + REQUIRE(ref_val.x == fixture.out_alloc_h[index].x); + REQUIRE(ref_val.y == fixture.out_alloc_h[index].y); + REQUIRE(ref_val.z == fixture.out_alloc_h[index].z); + REQUIRE(ref_val.w == fixture.out_alloc_h[index].w); + } + } +} \ No newline at end of file diff --git a/catch/unit/texture/tex2DLayered.cc b/catch/unit/texture/tex2DLayered.cc new file mode 100644 index 0000000000..b05a2e0a32 --- /dev/null +++ b/catch/unit/texture/tex2DLayered.cc @@ -0,0 +1,183 @@ +/* +Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include + +#include "kernels.hh" +#include "test_fixture.hh" + +/** + * @addtogroup tex2DLayered tex2DLayered + * @{ + * @ingroup TextureTest + */ + +/** + * Test Description + * ------------------------ + * - Test texture fetching with `tex2DLayered` and read mode set to `hipReadModeElementType`. The + * test is performed with: + * - normalized coordinates + * - non-normalized coordinates + * - Nearest-point sampling + * - Linear filtering + * - All combinations of different addressing modes. + * Test source + * ------------------------ + * - unit/texture/tex2DLayered.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 5.2 + */ +TEMPLATE_TEST_CASE("Unit_tex2DLayered_Positive_ReadModeElementType", "", char, unsigned char, short, + unsigned short, int, unsigned int, float) { + TextureTestParams params = {0}; + params.extent = make_hipExtent(16, 4, 0); + params.layers = 2; + params.num_subdivisions = 4; + params.GenerateTextureDesc(); + + TextureTestFixture fixture{params}; + + const auto [num_threads_x, num_blocks_x] = GetLaunchConfig(32, params.NumItersX()); + const auto [num_threads_y, num_blocks_y] = GetLaunchConfig(32, params.NumItersY()); + + dim3 dim_grid; + dim_grid.x = num_blocks_x; + dim_grid.y = num_blocks_y; + + dim3 dim_block; + dim_block.x = num_threads_x; + dim_block.y = num_threads_y; + + for (auto layer = 0u; layer < params.layers; ++layer) { + tex2DLayeredKernel> + <<>>(fixture.out_alloc_d.ptr(), params.NumItersX(), params.NumItersY(), + fixture.tex.object(), params.Width(), params.Height(), + params.num_subdivisions, params.tex_desc.normalizedCoords, layer); + HIP_CHECK(hipGetLastError()); + + fixture.LoadOutput(); + + for (auto j = 0u; j < params.NumItersY(); ++j) { + for (auto i = 0u; i < params.NumItersX(); ++i) { + float x = GetCoordinate(i, params.NumItersX(), params.Width(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + float y = GetCoordinate(j, params.NumItersY(), params.Height(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + + INFO("Layer: " << layer); + INFO("i: " << i); + INFO("j: " << j); + INFO("Normalized coordinates: " << std::boolalpha << params.tex_desc.normalizedCoords); + INFO("Address mode X: " << AddressModeToString(params.tex_desc.addressMode[0])); + INFO("Address mode Y: " << AddressModeToString(params.tex_desc.addressMode[1])); + INFO("x: " << std::fixed << std::setprecision(16) << x); + INFO("y: " << std::fixed << std::setprecision(16) << y); + + auto index = j * params.NumItersX() + i; + + const auto ref_val = fixture.tex_h.Tex2DLayered(x, y, layer, params.tex_desc); + REQUIRE(ref_val.x == fixture.out_alloc_h[index].x); + REQUIRE(ref_val.y == fixture.out_alloc_h[index].y); + REQUIRE(ref_val.z == fixture.out_alloc_h[index].z); + REQUIRE(ref_val.w == fixture.out_alloc_h[index].w); + } + } + } +} + +/** + * Test Description + * ------------------------ + * - Test texture fetching with `tex2DLayered` and read mode set to `hipReadModeNormalizedFloat`. + * The test is performed with: + * - normalized coordinates + * - non-normalized coordinates + * - Nearest-point sampling + * - Linear filtering + * - All combinations of different addressing modes. + * Test source + * ------------------------ + * - unit/texture/tex2DLayered.cc + * Test requirements + * ------------------------ + * - HIP_VERSION >= 5.2 + */ +TEMPLATE_TEST_CASE("Unit_tex2DLayered_Positive_ReadModeNormalizedFloat", "", char, unsigned char, + short, unsigned short) { + TextureTestParams params = {0}; + params.extent = make_hipExtent(16, 4, 0); + params.layers = 2; + params.num_subdivisions = 4; + params.GenerateTextureDesc(hipReadModeNormalizedFloat); + + TextureTestFixture fixture{params}; + + const auto [num_threads_x, num_blocks_x] = GetLaunchConfig(32, params.NumItersX()); + const auto [num_threads_y, num_blocks_y] = GetLaunchConfig(32, params.NumItersY()); + + dim3 dim_grid; + dim_grid.x = num_blocks_x; + dim_grid.y = num_blocks_y; + + dim3 dim_block; + dim_block.x = num_threads_x; + dim_block.y = num_threads_y; + + for (auto layer = 0u; layer < params.layers; ++layer) { + tex2DLayeredKernel> + <<>>(fixture.out_alloc_d.ptr(), params.NumItersX(), params.NumItersY(), + fixture.tex.object(), params.Width(), params.Height(), + params.num_subdivisions, params.tex_desc.normalizedCoords, layer); + HIP_CHECK(hipGetLastError()); + + fixture.LoadOutput(); + + for (auto j = 0u; j < params.NumItersY(); ++j) { + for (auto i = 0u; i < params.NumItersX(); ++i) { + float x = GetCoordinate(i, params.NumItersX(), params.Width(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + float y = GetCoordinate(j, params.NumItersY(), params.Height(), params.num_subdivisions, + params.tex_desc.normalizedCoords); + + INFO("Layer: " << layer); + INFO("i: " << i); + INFO("j: " << j); + INFO("Normalized coordinates: " << std::boolalpha << params.tex_desc.normalizedCoords); + INFO("Address mode X: " << AddressModeToString(params.tex_desc.addressMode[0])); + INFO("Address mode Y: " << AddressModeToString(params.tex_desc.addressMode[1])); + INFO("x: " << std::fixed << std::setprecision(16) << x); + INFO("y: " << std::fixed << std::setprecision(16) << y); + + auto index = j * params.NumItersX() + i; + + auto ref_val = Vec4Map(fixture.tex_h.Tex2DLayered(x, y, layer, params.tex_desc), + NormalizeInteger); + REQUIRE(ref_val.x == fixture.out_alloc_h[index].x); + REQUIRE(ref_val.y == fixture.out_alloc_h[index].y); + REQUIRE(ref_val.z == fixture.out_alloc_h[index].z); + REQUIRE(ref_val.w == fixture.out_alloc_h[index].w); + } + } + } +} \ No newline at end of file diff --git a/catch/unit/texture/texture_reference.hh b/catch/unit/texture/texture_reference.hh index d2ee9159e0..21e08abd0e 100644 --- a/catch/unit/texture/texture_reference.hh +++ b/catch/unit/texture/texture_reference.hh @@ -236,17 +236,10 @@ template class TextureReference { return coord; } - template float FloatToNBitFractional(float x) const { - constexpr size_t mult = 1 << N; - const auto x_trunc = std::trunc(x); - const auto x_frac = std::round((x - x_trunc) * mult) / mult; - return x_trunc + x_frac; - } - std::tuple GetLinearFilteringParams(float coord) const { - const auto coordB = FloatToNBitFractional<8>(coord - 0.5f); + const auto coordB = coord - 0.5f; const auto index = floorf(coordB); - const auto coeff = coordB - index; + const FixedPoint<8> coeff = coordB - index; return {index, coeff}; }