What Is CUDA Texture Memory? Choosing Between 2D Sampling and Regular Loads
CUDA texture memory is a read-only path for sampling GPU data by coordinates. A texture object combines the resource to read with coordinate, filtering, and boundary settings; it can suit a warp that reads nearby values in 2D. A cache hit can reduce DRAM traffic, but it does not remove fetch latency, so texture reads are not automatically faster than ordinary global loads. [CUDA C++ Best Practices Guide — Texture Memory](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)
What Is CUDA Texture Memory? Choosing Between 2D Sampling and Regular Loads
CUDA texture memory is a read-only path for sampling GPU data by coordinates. A texture object combines the resource to read with coordinate, filtering, and boundary settings; it can suit a warp that reads nearby values in 2D. A cache hit can reduce DRAM traffic, but it does not remove fetch latency, so texture reads are not automatically faster than ordinary global loads. CUDA C++ Best Practices Guide — Texture Memory
What is texture memory, and how does it differ from a regular load?
A texture fetch reads data by coordinates through device functions such as tex1D, tex2D, and tex3D. A texture object pairs the data source (resource descriptor) with the sampling rules (texture descriptor). Depending on the supported resource type, that source can be a CUDA array, linear device memory, or pitched 2D memory. Think of it as a CUDA data-sampling path that uses hardware related to graphics texturing. CUDA Runtime API — Texture Object Management
| Choice | Texture fetch | Ordinary global load |
|---|---|---|
| Access | Reads 1D, 2D, or 3D texels using coordinates and sampler settings. | Reads an element through a pointer and index. |
| Writes | Texture reads are read-only; writing requires a separate path. | Can read and write. |
| Spatial sampling | Can use a texture cache suited to 2D locality, filtering, and address modes. | You design indexing around coalescing and ordinary cache behavior. |
| Coordinates and edges | Can configure normalized coordinates and modes such as clamp or wrap. | Coordinate conversion and boundary branches are explicit in your code. |
| Trade-off | Requires resource and sampler setup; same-kernel writes and texture reads are not coherent. | More direct, but interpolation and boundary handling are yours to implement. |
NVIDIA documents that the texture cache is optimized for 2D spatial locality: a warp whose threads read nearby 2D texture addresses is a good fit. A cache hit lowers DRAM bandwidth demand, not fetch latency. Global loads can also be reused by ordinary caches depending on the device and access pattern, so the texture path is most compelling when spatial-neighbor reuse or sampling features are actually useful. CUDA C++ Best Practices Guide — Texture Memory
How do you create an object to read 2D data?
First confirm that the data is on the device and that its dimensions, channel format, and layout are supported as a texture resource. cudaResourceDesc identifies the data; cudaTextureDesc sets choices such as coordinate normalization, filtering, and address modes. cudaCreateTextureObject combines them into a cudaTextureObject_t, which you pass to a kernel and read with tex2D. After all kernels using the object finish, destroy the object and release its backing resource. Check the Runtime API for device support and resource-specific pitch, alignment, and dimension limits. CUDA Runtime API — Texture Object Management
For example, suppose an input image is a CUDA 2D array of float texels and you want to linearly sample the input location corresponding to each output pixel center. The fragment below illustrates setup and coordinate convention; it is not a complete program. Array allocation, data transfer, and full error handling are omitted, so check every API result and match the setup to the actual input format and dimensions.
Existing CUDA 2D array
│ resource descriptor: cudaResourceDesc
▼
cudaCreateTextureObject ◄── sampler settings: cudaTextureDesc
│ cudaTextureObject_t
▼
kernel calls tex2D(u, v)
├─ warp reads nearby 2D coordinates ─► texture-cache locality may help
└─ filter/address settings ───────────► choose texels, interpolation, and edges// inputArray: an existing cudaArray_t containing float texels
__global__ void resize(cudaTextureObject_t tex, float* output,
int outWidth, int outHeight) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < outWidth && y < outHeight) {
float u = (x + 0.5f) / outWidth;
float v = (y + 0.5f) / outHeight;
output[y * outWidth + x] = tex2D<float>(tex, u, v);
}
}
void runResize(cudaArray_t inputArray, float* output,
int outWidth, int outHeight) {
cudaResourceDesc resource{};
resource.resType = cudaResourceTypeArray;
resource.res.array.array = inputArray;
cudaTextureDesc sampling{};
sampling.addressMode[0] = cudaAddressModeClamp;
sampling.addressMode[1] = cudaAddressModeClamp;
sampling.filterMode = cudaFilterModeLinear;
sampling.readMode = cudaReadModeElementType;
sampling.normalizedCoords = 1;
cudaTextureObject_t image = 0;
cudaCreateTextureObject(&image, &resource, &sampling, nullptr);
// Check API and kernel errors in a complete program.
dim3 block(16, 16);
dim3 grid((outWidth + block.x - 1) / block.x,
(outHeight + block.y - 1) / block.y);
resize<<<grid, block>>>(image, output, outWidth, outHeight);
cudaDeviceSynchronize(); // Wait before releasing the object in this example.
cudaDestroyTextureObject(image);
// Release inputArray only after its final use.
}With normalized coordinates, each axis is scaled by the texture dimension; (x + 0.5) / width addresses a texel center. The mapping from an output coordinate to an input coordinate depends on the transform convention, such as how pixel centers and boundaries are aligned. This is one sampling convention, not a universal resize rule. Confirm that cudaCreateTextureObject succeeds, and do not destroy the object or array while a kernel still uses it.
How do coordinates, filtering, and edge modes change the result?
cudaFilterModePoint selects a texel; cudaFilterModeLinear interpolates nearby texels. In 2D, linear filtering uses four nearby texels for bilinear interpolation. Linear filtering is valid only when the returned value is floating point, so check the data and read mode before using it with integer input. CUDA Runtime API — Texture Object Management
Without normalized coordinates, coordinates are expressed in texel units; normalized coordinates scale with each axis. To address a texel center in the example, use (x + 0.5) / width. This coordinate convention is convenient when dimensions vary, but it does not choose the geometric mapping you want for a transform. Verify the coordinate math against the resource dimensions.
cudaAddressModeClamp handles an out-of-range coordinate at the edge. cudaAddressModeWrap and cudaAddressModeMirror are supported only with normalized coordinates; if normalization is disabled, CUDA switches them to clamp. Wrapping to the opposite edge, reflecting, and clamping produce different image boundaries, so choose the mode that matches the intended edge rule. CUDA Runtime API — Texture Object Management
With cudaReadModeNormalizedFloat, 8-bit and 16-bit integer texels can be converted to floating point: unsigned values map to [0, 1] and signed values to [-1, 1]. The setting does not promote 32-bit integer texels. Because this changes the numerical range and possibly the meaning of image values, match the input type, signedness, and downstream calculations. Linear filtering and normalized reads are data and precision choices, not automatic “quality” upgrades. CUDA Runtime API — Texture Object Management
When should you use textures, and how do you compare them safely?
A texture object can fit a read-only workload such as a 2D image or grid when nearby coordinates are reused, or when hardware addressing and linear interpolation are useful. Ordinary global loads may be simpler when threads read regular values and you need writes or custom interpolation. Compare the whole workload, including object setup, cache reuse, address calculations, and interpolation accuracy.
Do not read through a texture an address that the same kernel invocation writes through global memory. NVIDIA warns that the texture cache is not kept coherent with those writes, so the fetch can return undefined data. A safe baseline is to finish a producer kernel, preserve the required stream order or synchronization, and read through the texture in a separate consumer kernel. CUDA C++ Best Practices Guide — Device Memory Spaces
Decide whether the data is read-only and whether its supported resource type and format are 1D, 2D, or 3D. Check pitch, alignment, and dimension limits for pitched pointers or CUDA arrays.
Choose texel or normalized coordinates. Substitute small pixel-center, output-scaling, and boundary examples to verify the mapping.
Check that cudaTextureDesc address modes, point/linear filtering, and read mode match the desired result and input type. Confirm the floating-point condition for linear filtering and the integer-width limits for conversion.
Check cudaCreateTextureObject’s return value and pass the handle to the kernel. Keep the object and resource alive until every using kernel finishes, and do not texture-read an address written in the same kernel.
Compare texture and global-load versions with identical input, output, boundary, and accuracy requirements. Measure the full path, including object setup, on representative 2D patterns and target GPUs.
If results differ from expectations, first check coordinate units and texel centers, address modes, and filter/read modes. If only edges differ, check clamp, wrap, or mirror and whether coordinates are normalized. If values appear stale or irregular, inspect producer/consumer kernel boundaries, stream ordering, and whether that address was written. If performance does not improve, examine texture-cache behavior and DRAM demand; do not infer lower latency from a cache hit alone.
Texture memory is not “fast because the data is an image.” Its read path can simplify work when nearby 2D coordinates, boundary handling, or interpolation fit the workload. Coordinates and input format affect the result, same-kernel writes must not be mixed with texture reads, and the final choice should come from a matched full-path comparison.
Sources
[NVIDIA CUDA C++ Best Practices Guide — Texture Memory](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)
[NVIDIA CUDA Runtime API — Texture Object Management](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__TEXTURE__OBJECT.html)