/* * Copyright 1993-2015 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and its licensors retain all intellectual property and * proprietary rights in and to this software and related documentation. * Any use, reproduction, disclosure, or distribution of this software * and related documentation without an express license agreement from * NVIDIA Corporation is strictly prohibited. * * Please refer to the applicable NVIDIA end user license agreement (EULA) * associated with this source code for terms and conditions that govern * your use of this NVIDIA software. * */ #include "common.h" /////////////////////////////////////////////////////////////////////////////// /// \brief downscale image /// /// CUDA kernel, relies heavily on texture unit /// \param[in] width image width /// \param[in] height image height /// \param[in] stride image stride /// \param[out] out result /////////////////////////////////////////////////////////////////////////////// __global__ void DownscaleKernel(int width, int height, int stride, float *out, cudaTextureObject_t texFine) { const int ix = threadIdx.x + blockIdx.x * blockDim.x; const int iy = threadIdx.y + blockIdx.y * blockDim.y; if (ix >= width || iy >= height) { return; } float dx = 1.0f/(float)width; float dy = 1.0f/(float)height; float x = ((float)ix + 0.5f) * dx; float y = ((float)iy + 0.5f) * dy; out[ix + iy * stride] = 0.25f * (tex2D(texFine, x - dx * 0.25f, y) + tex2D(texFine, x + dx * 0.25f, y) + tex2D(texFine, x, y - dy * 0.25f) + tex2D(texFine, x, y + dy * 0.25f)); } /////////////////////////////////////////////////////////////////////////////// /// \brief downscale image /// /// \param[in] src image to downscale /// \param[in] width image width /// \param[in] height image height /// \param[in] stride image stride /// \param[out] out result /////////////////////////////////////////////////////////////////////////////// static void Downscale(const float *src, int width, int height, int stride, int newWidth, int newHeight, int newStride, float *out) { dim3 threads(32, 8); dim3 blocks(iDivUp(newWidth, threads.x), iDivUp(newHeight, threads.y)); cudaTextureObject_t texFine; cudaResourceDesc texRes; memset(&texRes,0,sizeof(cudaResourceDesc)); texRes.resType = cudaResourceTypePitch2D; texRes.res.pitch2D.devPtr = (void*)src; texRes.res.pitch2D.desc = cudaCreateChannelDesc(); texRes.res.pitch2D.width = width; texRes.res.pitch2D.height = height; texRes.res.pitch2D.pitchInBytes = stride * sizeof(float); cudaTextureDesc texDescr; memset(&texDescr,0,sizeof(cudaTextureDesc)); texDescr.normalizedCoords = true; texDescr.filterMode = cudaFilterModeLinear; texDescr.addressMode[0] = cudaAddressModeMirror; texDescr.addressMode[1] = cudaAddressModeMirror; texDescr.readMode = cudaReadModeElementType; checkCudaErrors(cudaCreateTextureObject(&texFine, &texRes, &texDescr, NULL)); DownscaleKernel<<>>(newWidth, newHeight, newStride, out, texFine); }