
Cloo, C# to OpenCL struct transfer trouble
Posted Sunday, 16 December, 2012 - 05:58 by Lammar inAll compile and run normally, but it looks like the triangle data is corrupted.
In kernel I have struct:
typedef struct { float3 a, b, c; float3 ambient, diffuse, specular; } Triangle; ... __kernel void Main(__write_only image2d_t image, __global Triangle * triangles, const float trianglesCount) { Triangle tri = triangles[0]; ...
And in host code:
public struct RGB { public float R, G, B; public RGB(float n) : this(n, n, n) { } public RGB(float R, float G, float B) { this.R = R; this.G = G; this.B = B; } } public struct Triangle { public Vector3 a, b, c; public RGB ambient, diffuse, specular; public Triangle(Vector3 a, Vector3 b, Vector3 c) : this(a, b, c, new RGB(), new RGB(), new RGB()) { } public Triangle(Vector3 a, Vector3 b, Vector3 c, RGB ambient, RGB diffuse, RGB specular) { this.a = a; this.b = b; this.c = c; this.ambient = ambient; this.diffuse = diffuse; this.specular = specular; } }
Rendering loop:
triangles = new ComputeBuffer<CL.Triangle>(CL.context, ComputeMemoryFlags.ReadOnly, new CL.Triangle[] { new CL.Triangle(new Vector3(), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new CL.RGB(1, 0, 0), new CL.RGB(), new CL.RGB()) }); ... kernel.SetMemoryArgument(1, triangles);
What's wrong in my code?


Comments
Re: Cloo, C# to OpenCL struct transfer trouble
Looks like struct alignment problems. Try swapping float3's for float4's.
Re: Cloo, C# to OpenCL struct transfer trouble
Fixed!
I added ComputeMemoryFlags.UseHostPointer flag to buffer flags and offsets to struct:
Re: Cloo, C# to OpenCL struct transfer trouble
But then I try to transfer more than one triangle and have problems again. It looks as structures overlap each other.
Re: Cloo, C# to OpenCL struct transfer trouble
UseHostPointer is NOT safe under a compacting garbage collector environment (much less with a temporary array :). Use CopyHostPointer instead.
Re: Cloo, C# to OpenCL struct transfer trouble
You can avoid that mess of FieldOffsetAttributes by using the "Pack" property of StructLayoutAttribute:
[StructLayout(LayoutKind.Sequential, Pack = 1)]Will pack the struct on 1-byte boundaries instead of 4.
Re: Cloo, C# to OpenCL struct transfer trouble
Fxed last problem by specifying struct size