/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ #pragma clang diagnostic ignored "-Wunused-parameter" #include /** * @addtogroup hipCreateTextureObject hipCreateTextureObject * @{ * @ingroup TextureTest */ __global__ void tex2DKernel(float* outputData, hipTextureObject_t textureObject, int width) { #if !__HIP_NO_IMAGE_SUPPORT int x = blockIdx.x * blockDim.x + threadIdx.x; int y = blockIdx.y * blockDim.y + threadIdx.y; outputData[y * width + x] = tex2D(textureObject, x, y); #endif } /** * Test Description * ------------------------ * - Basic test where the resource type is a 2D array. * Test source * ------------------------ * - unit/texture/hipTextureObj2D.cc * Test requirements * ------------------------ * - Textures supported on device * - HIP_VERSION >= 5.2 */ HIP_TEST_CASE(Unit_hipTextureObj2D_Check) { CHECK_IMAGE_SUPPORT constexpr int SIZE = 256; constexpr unsigned int width = SIZE; constexpr unsigned int height = SIZE; constexpr unsigned int size = width * height * sizeof(float); unsigned int i, j; float* dData = nullptr; HIP_CHECK(hipMalloc(&dData, size)); REQUIRE(dData != nullptr); float* hOutputData = reinterpret_cast(malloc(size)); REQUIRE(hOutputData != nullptr); memset(hOutputData, 0, size); float* hData = reinterpret_cast(malloc(size)); REQUIRE(hData != nullptr); memset(hData, 0, size); for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { hData[i * width + j] = i * width + j; } } hipChannelFormatDesc channelDesc = hipCreateChannelDesc(32, 0, 0, 0, hipChannelFormatKindFloat); hipArray_t hipArray; HIP_CHECK(hipMallocArray(&hipArray, &channelDesc, width, height)); HIP_CHECK(hipMemcpyToArray(hipArray, 0, 0, hData, size, hipMemcpyHostToDevice)); hipResourceDesc resDesc; memset(&resDesc, 0, sizeof(resDesc)); resDesc.resType = hipResourceTypeArray; resDesc.res.array.array = hipArray; // Specify texture object parameters hipTextureDesc texDesc; memset(&texDesc, 0, sizeof(texDesc)); texDesc.addressMode[0] = hipAddressModeWrap; texDesc.addressMode[1] = hipAddressModeWrap; texDesc.filterMode = hipFilterModePoint; texDesc.readMode = hipReadModeElementType; texDesc.normalizedCoords = 0; // Create texture object hipTextureObject_t textureObject = 0; HIP_CHECK(hipCreateTextureObject(&textureObject, &resDesc, &texDesc, nullptr)); dim3 dimBlock(16, 16, 1); dim3 dimGrid(width / dimBlock.x, height / dimBlock.y, 1); hipLaunchKernelGGL(tex2DKernel, dim3(dimGrid), dim3(dimBlock), 0, 0, dData, textureObject, width); HIP_CHECK(hipGetLastError()); HIP_CHECK(hipDeviceSynchronize()); HIP_CHECK(hipMemcpy(hOutputData, dData, size, hipMemcpyDeviceToHost)); for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { if (hData[i * width + j] != hOutputData[i * width + j]) { INFO("Difference found at [ " << i << j << " ]: " << hData[i * width + j] << hOutputData[i * width + j]); REQUIRE(false); } } } HIP_CHECK(hipDestroyTextureObject(textureObject)); HIP_CHECK(hipFree(dData)); HIP_CHECK(hipFreeArray(hipArray)); free(hData); } /** * End doxygen group TextureTest. * @} */