2 * Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
5 * with this source code for terms and conditions that govern your use of
6 * this software. Any use, reproduction, disclosure, or distribution of
7 * this software and related documentation outside the terms of the EULA
8 * is strictly prohibited.
13 * This sample demonstrates how to use texture fetches from layered 2D textures in CUDA C
15 * This sample first generates a 3D input data array for the layered texture
16 * and the expected output. Then it starts CUDA C kernels, one for each layer,
17 * which fetch their layer's texture data (using normalized texture coordinates)
18 * transform it to the expected output, and write it to a 3D output data array.
28 #include <cuda_runtime.h>
30 // helper functions and utilities to work with CUDA
31 #include <helper_functions.h>
32 #include <helper_cuda.h>
34 static char *sSDKname = "simpleCubemapTexture";
37 // declare texture reference for layered 2D float texture
38 // Note: The "dim" field in the texture reference template is now deprecated.
39 // Instead, please use a texture type macro such as cudaTextureType1D, etc.
41 texture<float, cudaTextureTypeCubemap> tex;
43 ////////////////////////////////////////////////////////////////////////////////
44 //! Transform a cubemap face of a linear buffe using cubemap texture lookups
45 //! @param g_odata output data in global memory
46 ////////////////////////////////////////////////////////////////////////////////
48 transformKernel(float *g_odata, int width)
50 // calculate this thread's data point
51 unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
52 unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
54 // 0.5f offset and division are necessary to access the original data points
55 // in the texture (such that bilinear interpolation will not be activated).
56 // For details, see also CUDA Programming Guide, Appendix D
58 float u = ((x+0.5f) / (float) width) * 2.f - 1.f;
59 float v = ((y+0.5f) / (float) width) * 2.f - 1.f;
63 for (unsigned int face = 0; face < 6; face ++)
65 //Layer 0 is positive X face
72 //Layer 1 is negative X face
79 //Layer 2 is positive Y face
86 //Layer 3 is negative Y face
93 //Layer 4 is positive Z face
100 //Layer 4 is negative Z face
108 // read from texture, do expected transformation and write to global memory
109 g_odata[face*width*width + y*width + x] = -texCubemap(tex, cx, cy, cz);
115 ////////////////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////////////////
119 main(int argc, char **argv)
121 // use command-line specified CUDA device, otherwise use device with highest Gflops/s
122 int devID = findCudaDevice(argc, (const char **)argv);
126 // get number of SMs on this GPU
127 cudaDeviceProp deviceProps;
129 checkCudaErrors(cudaGetDeviceProperties(&deviceProps, devID));
130 printf("CUDA device [%s] has %d Multi-Processors ", deviceProps.name, deviceProps.multiProcessorCount);
131 printf("SM %d.%d\n", deviceProps.major, deviceProps.minor);
133 if (deviceProps.major < 2)
135 printf("%s requires SM 2.0 or higher for support of Texture Arrays. Test will exit... \n", sSDKname);
140 // generate input data for layered texture
141 unsigned int width=64, num_faces = 6, num_layers = 1;
142 unsigned int cubemap_size = width * width * num_faces;
143 unsigned int size = cubemap_size * num_layers * sizeof(float);
144 float *h_data = (float *) malloc(size);
146 for (int i = 0; i < (int)(cubemap_size * num_layers); i++)
148 h_data[i] = (float)i;
153 // this is the expected transformation of the input data (the expected output)
154 float *h_data_ref = (float *) malloc(size);
156 for (unsigned int layer = 0; layer < num_layers; layer++)
158 for (int i = 0; i < (int)(cubemap_size); i++)
160 h_data_ref[layer*cubemap_size + i] = -h_data[layer*cubemap_size + i] + layer;
164 // allocate device memory for result
165 float *d_data = NULL;
166 checkCudaErrors(cudaMalloc((void **) &d_data, size));
168 // allocate array and copy image data
169 cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
170 cudaArray *cu_3darray;
171 // checkCudaErrors(cudaMalloc3DArray( &cu_3darray, &channelDesc, make_cudaExtent(width, height, num_layers), cudaArrayLayered ));
172 checkCudaErrors(cudaMalloc3DArray(&cu_3darray, &channelDesc, make_cudaExtent(width, width, num_faces), cudaArrayCubemap));
173 cudaMemcpy3DParms myparms = {0};
174 myparms.srcPos = make_cudaPos(0,0,0);
175 myparms.dstPos = make_cudaPos(0,0,0);
176 myparms.srcPtr = make_cudaPitchedPtr(h_data, width * sizeof(float), width, width);
177 myparms.dstArray = cu_3darray;
178 myparms.extent = make_cudaExtent(width, width, num_faces);
179 myparms.kind = cudaMemcpyHostToDevice;
180 checkCudaErrors(cudaMemcpy3D(&myparms));
182 // set texture parameters
183 tex.addressMode[0] = cudaAddressModeWrap;
184 tex.addressMode[1] = cudaAddressModeWrap;
185 tex.filterMode = cudaFilterModeLinear;
186 tex.normalized = true; // access with normalized texture coordinates
188 // Bind the array to the texture
189 checkCudaErrors(cudaBindTextureToArray(tex, cu_3darray, channelDesc));
191 dim3 dimBlock(8, 8, 1);
192 dim3 dimGrid(width / dimBlock.x, width / dimBlock.y, 1);
194 printf("Covering Cubemap data array of %d~3 x %d: Grid size is %d x %d, each block has 8 x 8 threads\n",
195 width, num_layers, dimGrid.x, dimGrid.y);
197 transformKernel<<< dimGrid, dimBlock >>>(d_data, width); // warmup (for better timing)
199 // check if kernel execution generated an error
200 getLastCudaError("warmup Kernel execution failed");
202 checkCudaErrors(cudaDeviceSynchronize());
204 StopWatchInterface *timer = NULL;
205 sdkCreateTimer(&timer);
206 sdkStartTimer(&timer);
208 // execute the kernel
209 transformKernel<<< dimGrid, dimBlock, 0 >>>(d_data, width);
211 // check if kernel execution generated an error
212 getLastCudaError("Kernel execution failed");
214 checkCudaErrors(cudaDeviceSynchronize());
215 sdkStopTimer(&timer);
216 printf("Processing time: %.3f msec\n", sdkGetTimerValue(&timer));
217 printf("%.2f Mtexlookups/sec\n", (cubemap_size / (sdkGetTimerValue(&timer) / 1000.0f) / 1e6));
218 sdkDeleteTimer(&timer);
220 // allocate mem for the result on host side
221 float *h_odata = (float *) malloc(size);
222 // copy result from device to host
223 checkCudaErrors(cudaMemcpy(h_odata, d_data, size, cudaMemcpyDeviceToHost));
225 // write regression file if necessary
226 if (checkCmdLineFlag(argc, (const char **)argv, "regression"))
228 // write file for regression test
229 sdkWriteFile<float>("./data/regression.dat", h_odata, width*width, 0.0f, false);
233 printf("Comparing kernel output to expected data\n");
235 #define MIN_EPSILON_ERROR 5e-3f
236 bResult = compareData(h_odata, h_data_ref, cubemap_size, MIN_EPSILON_ERROR, 0.0f);
244 checkCudaErrors(cudaFree(d_data));
245 checkCudaErrors(cudaFreeArray(cu_3darray));
248 exit(bResult ? EXIT_SUCCESS : EXIT_FAILURE);