0

I'd like to write an image from a kernel to a specified place in device memory which I define by an IntPtr.

Although it's not directly related to this problem, it's the RenderTexture from Unity which I want to change from inside the kernel in order to pass it to a shader which will visualize my algorithm on the GPU.

So far I tried this:

CLkernel.SetArgument (0, (IntPtr)(width*height*4*sizeof(float)), pointerToRT);

Which threw InvalidArgumentSize as I cannot specify it as an image, and this:

renderTexture = new ComputeImage2D (CLcontext, ComputeMemoryFlags.WriteOnly | ComputeMemoryFlags.UseHostPointer, 
                                    new ComputeImageFormat (ComputeImageChannelOrder.Rgba, ComputeImageChannelType.Float), 
                                    width, height, 0, pointerToRT);
CLkernel.SetMemoryArgument (0, renderTexture);

which resulted in an InvalidHostPointer Exception, as the pointer points to a place already in the device memory.

This is the Kernel code:

kernel void WriteToRenderTexture (
write_only image2d_t bdsRT,
global float* terrainHeight )
{
int width = get_global_size (0);
int x = get_global_id (0);
int y = get_global_id (1);
int pixel = x + y * width;

int2 coords = { x, y };
float4 value = { 0, terrainHeight [pixel * 3], 0.5, 0 };

write_imagef (bdsRT, coords, value);
}

Any ideas how I'm able to do so with Cloo?

Seneral
  • 327
  • 4
  • 12
  • What is `pointerToRT`, and is the first argument of your kernel an `image2d_t` or a `global float*`? – jprice May 04 '15 at 18:39
  • as the name suggests, its the IntPtr pointer to the underlying texture of the RenderTexture. So it's a normal texture. I'll update the question with the kernel code, and yes, the first argument is an image2d_t – Seneral May 04 '15 at 18:44
  • If your target texture object is already on the device but created with a different API, you'll need to retrieve an OpenGL or Direct3D handle to that texture which you can then use to create an OpenCL image with the CL/GL or CL/D3D interop APIs. – jprice May 04 '15 at 19:41
  • @jprice Thanks, that's a great idea. Drew my attention on this (from Unitys RenderTarget Docs describing the function to get the IntPtr to the underlying texture): 'On Direct3D-like devices this returns a pointer to the base texture type (IDirect3DBaseTexture9 on D3D9, ID3D11Resource on D3D11). On OpenGL-like devices the texture "name" is returned; cast the pointer to integer type to get it. On platforms that do not support native code plugins, this function always returns NULL.' Didn't got the time to looking into that "interop" so far (I'm new to OpenCL). Thanks for the tip! – Seneral May 04 '15 at 19:50

0 Answers0