From 38d53a0900a41d013879168236278b8ba9cff99b Mon Sep 17 00:00:00 2001 From: Willem Jan Palenstijn Date: Mon, 22 Nov 2021 13:12:35 +0100 Subject: Replace texref by texobj in fan_fp --- cuda/2d/fan_fp.cu | 56 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'cuda/2d') diff --git a/cuda/2d/fan_fp.cu b/cuda/2d/fan_fp.cu index 342ca4c..0801097 100644 --- a/cuda/2d/fan_fp.cu +++ b/cuda/2d/fan_fp.cu @@ -33,12 +33,6 @@ along with the ASTRA Toolbox. If not, see . #include #include - -typedef texture texture2D; - -static texture2D gT_FanVolumeTexture; - - namespace astraCUDA { static const unsigned g_MaxAngles = 2560; @@ -55,31 +49,39 @@ static const unsigned int g_anglesPerBlock = 16; static const unsigned int g_detBlockSize = 32; static const unsigned int g_blockSlices = 64; -static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, unsigned int pitch, unsigned int width, unsigned int height) +static bool bindVolumeDataTexture(float* data, cudaArray*& dataArray, cudaTextureObject_t& texObj, unsigned int pitch, unsigned int width, unsigned int height) { - cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(); + // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) + // not using an array is more efficient. + + cudaChannelFormatDesc channelDesc = + cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat); + dataArray = 0; cudaMallocArray(&dataArray, &channelDesc, width, height); cudaMemcpy2DToArray(dataArray, 0, 0, data, pitch*sizeof(float), width*sizeof(float), height, cudaMemcpyDeviceToDevice); - gT_FanVolumeTexture.addressMode[0] = cudaAddressModeBorder; - gT_FanVolumeTexture.addressMode[1] = cudaAddressModeBorder; - gT_FanVolumeTexture.filterMode = cudaFilterModeLinear; - gT_FanVolumeTexture.normalized = false; + cudaResourceDesc resDesc; + memset(&resDesc, 0, sizeof(resDesc)); + resDesc.resType = cudaResourceTypeArray; + resDesc.res.array.array = dataArray; - // TODO: For very small sizes (roughly <=512x128) with few angles (<=180) - // not using an array is more efficient. - //cudaBindTexture2D(0, gT_FanVolumeTexture, (const void*)data, channelDesc, width, height, sizeof(float)*pitch); - cudaBindTextureToArray(gT_FanVolumeTexture, dataArray, channelDesc); + cudaTextureDesc texDesc; + memset(&texDesc, 0, sizeof(texDesc)); + texDesc.addressMode[0] = cudaAddressModeBorder; + texDesc.addressMode[1] = cudaAddressModeBorder; + texDesc.filterMode = cudaFilterModeLinear; + texDesc.readMode = cudaReadModeElementType; + texDesc.normalizedCoords = 0; - // TODO: error value? + texObj = 0; - return true; + return checkCuda(cudaCreateTextureObject(&texObj, &resDesc, &texDesc, NULL), "fan_fp texture"); } // projection for angles that are roughly horizontal // (detector roughly vertical) -__global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) +__global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) { float* projData = (float*)D_projData; const int relDet = threadIdx.x; @@ -134,7 +136,7 @@ __global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, unsig float fV = 0.0f; for (int slice = startSlice; slice < endSlice; ++slice) { - fV += tex2D(gT_FanVolumeTexture, fX, fY); + fV += tex2D(tex, fX, fY); fY -= alpha; fX += 1.0f; } @@ -149,7 +151,7 @@ __global__ void FanFPhorizontal(float* D_projData, unsigned int projPitch, unsig // projection for angles that are roughly vertical // (detector roughly horizontal) -__global__ void FanFPvertical(float* D_projData, unsigned int projPitch, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) +__global__ void FanFPvertical(float* D_projData, unsigned int projPitch, cudaTextureObject_t tex, unsigned int startSlice, unsigned int startAngle, unsigned int endAngle, const SDimensions dims, float outputScale) { const int relDet = threadIdx.x; const int relAngle = threadIdx.y; @@ -207,7 +209,7 @@ __global__ void FanFPvertical(float* D_projData, unsigned int projPitch, unsigne for (int slice = startSlice; slice < endSlice; ++slice) { - fV += tex2D(gT_FanVolumeTexture, fX, fY); + fV += tex2D(tex, fX, fY); fX -= alpha; fY += 1.0f; } @@ -227,7 +229,9 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, assert(dims.iProjAngles <= g_MaxAngles); cudaArray* D_dataArray; - bindVolumeDataTexture(D_volumeData, D_dataArray, volumePitch, dims.iVolWidth, dims.iVolHeight); + cudaTextureObject_t D_texObj; + + bindVolumeDataTexture(D_volumeData, D_dataArray, D_texObj, volumePitch, dims.iVolWidth, dims.iVolHeight); // transfer angles to constant memory float* tmp = new float[dims.iProjAngles]; @@ -260,13 +264,13 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, cudaStreamCreate(&stream1); streams.push_back(stream1); for (unsigned int i = 0; i < dims.iVolWidth; i += g_blockSlices) - FanFPhorizontal<<>>(D_projData, projPitch, i, blockStart, blockEnd, dims, outputScale); + FanFPhorizontal<<>>(D_projData, projPitch, D_texObj, i, blockStart, blockEnd, dims, outputScale); cudaStream_t stream2; cudaStreamCreate(&stream2); streams.push_back(stream2); for (unsigned int i = 0; i < dims.iVolHeight; i += g_blockSlices) - FanFPvertical<<>>(D_projData, projPitch, i, blockStart, blockEnd, dims, outputScale); + FanFPvertical<<>>(D_projData, projPitch, D_texObj, i, blockStart, blockEnd, dims, outputScale); bool ok = true; @@ -278,6 +282,8 @@ bool FanFP_internal(float* D_volumeData, unsigned int volumePitch, cudaFreeArray(D_dataArray); + cudaDestroyTextureObject(D_texObj); + return ok; } -- cgit v1.2.3