it is possible to index textures in CUDA 2.0 ?


It is possible to index textures in cuda 2.0 ?
something like this produces syntax error :/

texture <float4, 1, cudaReadModeElementType> TexturesArray[64];

global void Kernel()
// … calculate index
float4 t = tex2D(TexturesArray[index], U, V);

is there any ‘smart’ way to do this kind of indexing
i do not want to end with code like this:

case 0: t = tex2D(TextureArray_0, U, V); break;
case 1: t = tex2D(TextureArray_1, U, V); break;
case 2: t = tex2D(TextureArray_2, U, V); break;


i couldn’t use 3d texture because TextureArray* is d3d9 texture resource,
and for the rest of program & HLSL shaders it need to stay as 2d texture
(it contains mipmaps).

BTW. Is there any way to sample 2d texture with filtering & mipmaps in cuda ?
something like tex2Dlod from HLSL, the devide is able to do this, so there should be
a way to do this :)

Nope, dynamically indexing a texture array is not possible. The solution, if you can’t use 3D textures, is the really ugly switch statement you posted. Sorry.

Improving texturing support (LOD and other things) are under consideration for a future release.

Note that if only want to change textures between kernel calls, you can allocate several cudaArrays at startup, and then rebind the texture reference to the array you want (from the host code) before calling your kernel.

However, if you need to select different textures from inside the kernel based on other data, then the big switch is probably the best approach.

And you can do filtering with 2D textures, see the programming guide on the various addressing and filtering modes. It is only MIP maps that aren’t supported yet.

I know about filtering … the other thing wonders me now:

i have textures in d3d9 (512x512 format = A8R8G8B8) with full mipchain,

now i want to bind it as a cuda 2d texture

(i need only the 1st level available in cuda)

texture <float4, 2, cudaReadModeNormalizedFloat> MAT_TextureRef_00;

texture <float4, 2, cudaReadModeNormalizedFloat> MAT_TextureRef_01;

texture <float4, 2, cudaReadModeNormalizedFloat> MAT_TextureRef_02;

texture <float4, 2, cudaReadModeNormalizedFloat> MAT_TextureRef_03;


void BindToCuda()


 cudaChannelFormatDesc desc;

desc.x = 8;

 desc.y = 8;

 desc.z = 8;

 desc.w = 8;

 desc.f =  cudaChannelFormatKindUnsigned;

char tbf[128];

for (unsigned int i = 0; i < MAX_DIFFERENT_MATERIALS; i++)


if (cudaD3D9RegisterResource(CPU_MAT_Texture[i], cudaD3D9RegisterFlagsNone) != cudaSuccess)

 Â  freaked_error("Cuda failed to register !");

if (cudaD3D9ResourceSetMapFlags(CPU_MAT_Texture[i], cudaD3D9MapFlagsReadOnly) != cudaSuccess)

 Â  freaked_error("Cuda failed to set flags !");

if (cudaD3D9MapResources(1, &CPU_MAT_Texture[i]) != cudaSuccess)

freaked_error("Cuda failed to map !");

void  *TextureData;

size_t TextureSize;

if (cudaD3D9ResourceGetMappedPointer(&TextureData, CPU_MAT_Texture[i], 0, 0) != cudaSuccess)

freaked_error("Cuda failed to get !");

if (cudaD3D9ResourceGetMappedSize(&TextureSize, CPU_MAT_Texture[i], 0, 0) != cudaSuccess)

freaked_error("Cuda failed to get !");

sprintf(tbf, "MAT_TextureRef_%02i", i);

textureReference *texRef = NULL;

if (cudaGetTextureReference((const textureReference **)&texRef, tbf) != cudaSuccess)

freaked_error("Cuda failed to get refrence !");

texRef->addressMode[0] = cudaAddressModeWrap;

texRef->addressMode[1] = cudaAddressModeWrap;

texRef->filterMode   Â  = cudaFilterModeLinear;

texRef->normalized   Â  = true;


// Fails here!


if (cudaBindTexture(NULL, texRef, TextureData, &desc, TextureSize)!= cudaSuccess)

freaked_error("Cuda failed to bind !");



but the code fails at ‘cudaBindTexture’ with ‘cudaErrorInvalidTexture’ and i dont have any idea WHY ? (texRef is not NULL and seems to be correct pointer, so cudaGetTextureReference seems to work as expected)

it is possible to bind d3d texture as CUDA texture ?

there is no example about this in sdk :/

cudaBindTexture returned error on 8600 mobile, on my desktop 8800 GT all is fine, but another problem is now hunting me :) [so probably drivers / system issue]