3D texture binding and Application

Source: Internet
Author: User

15:02:24 | category:

Cuda | Tag: 3D texture binding
| Font size
Subscription

As we all know, global memory does not have a cache, And the access speed is very slow. Shared memory access speed is very fast, but the capacity is very small. It is always a good choice to bind a large array to texture memory. Texture memory can be cached and has a large capacity.

In the current Cuda version, 3D linear memory cannot be directly bound to texture memory, which is one-dimensional. Therefore, you must first put the data into a 3D Cuda array, then, bind the 3D Cuda array to texture memory. when accessing the array elements, the texture function tex3d (Tex, x, y, z) can return the coordinates (x, y, z.

1. Create a Cuda 3D Array
In the previous Cuda version, extent. width is different from height and depth, and its Counter unit is bytes. Therefore, array_width * sizeof (float) must be used in the old version. The latest 3.1 is quietly modified. Cuda-enabled documents are always incorrect. In this document, width, height, and depth are all in bytes. In fact, you can use the number of elements when assigning values. If you do not assign values directly, you can also call the make_cudaextent (Extent, width, height, depth) function ),
The principle is similar.

Cudaarray * d_u
Cudachannelformatdesc channeldesc = cudacreatechanneldesc <float> ();

Cudaextent extent;

Extent. width = array_width;

Extent. Height = array_height;

Extent. Depth = array_depth;

Cudamalloc3darray (& d_u, & channeldesc, extent );

2. Copy Data to 3D Array
First, explain the working principle of the pitched pointer tool. If you access the array element U [x] [y] [Z], the access through pitched pointer is u_p [x + y * Pitch + z * Pitch * Height]. Obviously, Here pitch = width, so when creating a pitched pointer, we need to pass the width and height as parameters to the make_cudapitchedptr () function (). In particular, pitched
The array pointed by pointer is different from the traditional C language array storage method, the C language accesses element U [x] [y] [Z] through U [x * height * depth + y * depth + Z]. Therefore, to correctly read the required elements, we recommend that you create pitched pointer in reverse order:
Copyparams. srcptr = make_cudapitchedptr (void *) U, array_depth * sizeof (float), array_depth, array_height );
In this case, the array U [x] [y] [Z] is transposed. In cuda3d array, the corresponding element is U [Z] [y] [X]. this difference was not mentioned in the Cuda document and the guide. It had been a long time for me to figure out this problem. I hope that the SDK sample will cover this point of attention in the future.

Cudamemcpy3dparms copyparams = {0 };

Copyparams. srcptr = make_cudapitchedptr (void *) U, array_width * sizeof (float), array_width, array_height );
Copyparams. dstarray = d_u;
Copyparams. extent = extent;
Copyparams. Kind = cudamemcpyhosttodevice;
Cudamemcpy3d (& copyparams );

3. Bind a 3D array to texture memory

Normalized sets whether to normalize texture coordinates. If normalized is a non-zero value, the coordinates normalized to [0, 1) are used for addressing. Otherwise, the coordinates of the texture with the size of width, height, and depth are used, width-1], [0, height-1], [0, depth-1] addressing. For example, a texture with a size of 64 × 32 can be addressed through coordinates with a range of [0, 63] and a range of [0, 31] in the Y dimension. If the texture with a size of 64 × 32 is normalized, the coordinates in the X and Y dimensions are [0.0, 1.0 ). This ensures that the texture coordinates are irrelevant to the texture size.
Filtermode is used to set the texture filtering mode, that is, how to calculate the returned texture Value Based on coordinates. The filtering mode can be cudafiltermodepoint or cudafiltermodelinear. When the filtering mode is cudafiltermodepoint, the returned value is the pixel value closest to the coordinate. In cudafiltermodelinear mode, only floating-point textures can be returned. When this mode is enabled, pixels around texture coordinates are picked and calculated based on the distance between coordinates and these pixels. Linear filtering can be used for one-dimensional textures, and bilinear filtering can be used for two-dimensional textures. The returned value is the value obtained after interpolation of the two pixels closest to the texture coordinate (one-dimensional texture), four pixels (two-dimensional texture), or eight pixels (three-dimensional texture.

Texture <float, 3, cudareadmodeelementtype> tex_u;
Tex_u.filtermode = cudafiltermodepoint;
Tex_u.normalized = false;
Tex_u.channeldesc = channeldesc;
If (cudabindtexturetoarray (tex_u, d_u, channeldesc )! = (Unsigned INT) cuda_success ){
Printf ("[Error] cocould not bind texture U \ n ");
Return;
}

When normolized is true, addressmode specifies the addressing mode, that is, how to handle cross-border texture coordinates. addressmode is an array of 3 sizes, the first, second, and third elements respectively specify the addressing mode of the first, second, and third texture coordinates. The addressing mode can be equal to cudaaddressmodeclamp, at this time, the cross-border texture coordinates will be placed within the valid range, or equal to cudaaddressmodewrap. At this time, the cross-border texture coordinates will be placed within the valid range. cudaaddressmodewrap only supports normalized texture coordinates.

// Set texture parameters
Tex. Normalized = true; // access with normalized texture coordinates
Tex. filtermode = cudafiltermodelinear; // linear interpolation
Tex. addressmode [0] = cudaaddressmodeclamp; // clamp texture coordinates
Tex. addressmode [1] = cudaaddressmodeclamp;

// Bind array to 3D texture
Cutilsafecall (cudabindtexturetoarray (Tex, d_volumearray, channeldesc ));


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.