This project is read-only.

Hi All need Help to translate this c++ to managedcuda

Apr 2, 2013 at 2:13 PM
Hi All,

I need some help in translate this c++ code cuda, to managed cuda texture3d i look at sample FluidsForms but i wasn't able to translate in this.
 void createNoiseTexture(int w, int h, int d)
    {
        cudaExtent size = make_cudaExtent(w, h, d);
        uint elements = (uint) size.width*size.height*size.depth;

        float *volumeData = (float *)malloc(elements*4*sizeof(float));
        float *ptr = volumeData;

        for (uint i=0; i<elements; i++)
        {
            *ptr++ = frand()*2.0f-1.0f;
            *ptr++ = frand()*2.0f-1.0f;
            *ptr++ = frand()*2.0f-1.0f;
            *ptr++ = frand()*2.0f-1.0f;
        }


        cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float4>();
        checkCudaErrors(cudaMalloc3DArray(&noiseArray, &channelDesc, size));

        cudaMemcpy3DParms copyParams = { 0 };
        copyParams.srcPtr   = make_cudaPitchedPtr((void *)volumeData, size.width*sizeof(float4), size.width, size.height);
        copyParams.dstArray = noiseArray;
        copyParams.extent   = size;
        copyParams.kind     = cudaMemcpyHostToDevice;
        checkCudaErrors(cudaMemcpy3D(&copyParams));

        free(volumeData);

        // set texture parameters
        noiseTex.normalized = true;                      // access with normalized texture coordinates
        noiseTex.filterMode = cudaFilterModeLinear;      // linear interpolation
        noiseTex.addressMode[0] = cudaAddressModeWrap;   // wrap texture coordinates
        noiseTex.addressMode[1] = cudaAddressModeWrap;
        noiseTex.addressMode[2] = cudaAddressModeWrap;

        // bind array to 3D texture
        checkCudaErrors(cudaBindTextureToArray(noiseTex, noiseArray, channelDesc));
}
P.S the delcaration in kernel code of texture is:
texture<float4, 3, cudaReadModeElementType> noiseTex;
and the function that mange texture:
__device__ float3 noise3D(float3 p)
{
    float4 n = tex3D(noiseTex, p.x, p.y, p.z);
    return make_float3(n.x, n.y, n.z);
}
Apr 4, 2013 at 7:43 PM
Edited Apr 4, 2013 at 8:52 PM
Hi,
without any warranty or testing:
CudaArray3D noiseArray;

void createNoiseTexture(int w, int h, int d)
{
    Random r = new Random();
    int elements = w * h * d;
    float4[] volumeData = new float4[elements];

    for (int i = 0; i < elements; i++)
    {
        volumeData[i] = new float4(
            (float)r.NextDouble() * 2.0f - 1.0f, 
            (float)r.NextDouble() * 2.0f - 1.0f, 
            (float)r.NextDouble() * 2.0f - 1.0f, 
            (float)r.NextDouble() * 2.0f - 1.0f);
    }

    noiseArray = new CudaArray3D(CUArrayFormat.Float, w, h, d, 
        CudaArray3DNumChannels.Four, CUDAArray3DFlags.None);

    noiseArray.CopyFromHostToThis<float4>(volumeData);

    //Bind array to tex and bind tex to kernel
    CudaTexture.BindTexture(kernel, "noiseTex", 
        CUAddressMode.Clamp,               // wrap texture coordinates
        CUFilterMode.Linear,                    // linear interpolation
        CUTexRefSetFlags.NormalizedCoordinates, // access with normalized texture coordinates
        noiseArray);

}
Apr 4, 2013 at 11:17 PM
Great it's works!!

many thanks