好的,所以我使用的是 CUDAfy.Net,我有以下 3 个结构:
[Cudafy]
public struct Collider
{
public int Index;
public int Type;
public Sphere Sphere;
public Plane Plane;
public Material Material;
}
[Cudafy]
public struct Material
{
public Color Color;
public Texture Texture;
public float Shininess;
}
[Cudafy]
public struct Texture
{
public int Width, Height;
public byte[ ] Data;
}
现在,只要我将一组 Collider 对象发送到 GPU,使用
CopyToDevice<GPU.Collider>( ColliderArray );
我收到以下错误:
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Object contains non-primitive or non-blittable data.
有没有人对 CUDAfy.Net 或 OpenCL 有任何经验(因为它基本上可以编译成 OpenCL ),知道我该如何做到这一点吗?整个问题在于 Texture 的字节数组,因为当我没有 Texture 结构时,一切都工作得很好,据我所知,该数组是不可 blittable 部分。我发现了几个关于同一问题的问题,他们使用固定大小的数组修复了它。但是,我无法做到这一点,因为这些是纹理,其大小可能有很大差异。
编辑:现在,我正在 CPU 上执行以下操作:
public unsafe static GPU.Texture CreateGPUTexture( Cudafy.Host.GPGPU _GPU, System.Drawing.Bitmap Image )
{
GPU.Texture T = new GPU.Texture( );
T.Width = Image.Width;
T.Height = Image.Height;
byte[ ] Data = new byte[ Image.Width * Image.Height * 3 ];
for ( int X = 0; X < Image.Width; X++ )
for ( int Y = 0; Y < Image.Height; Y++ )
{
System.Drawing.Color C = Image.GetPixel( X, Y );
int ID = ( X + Y * Image.Width ) * 3;
Data[ ID ] = C.R;
Data[ ID + 1 ] = C.G;
Data[ ID + 2 ] = C.B;
}
byte[ ] _Data = _GPU.CopyToDevice<byte>( Data );
IntPtr Pointer = _GPU.GetDeviceMemory( _Data ).Pointer;
T.Data = ( byte* )Pointer.ToPointer( );
return T;
}
然后我将此纹理结构附加到碰撞器,并将它们发送到 GPU。这一切都没有任何错误。但是,一旦我尝试在 GPU 上使用纹理,就像这样:
[Cudafy]
public static Color GetTextureColor( int X, int Y, Texture Tex )
{
int ID = ( X + Y * Tex.Width ) * 3;
unsafe
{
byte R = Tex.Data[ ID ];
byte G = Tex.Data[ ID + 1 ];
byte B = Tex.Data[ ID + 2 ];
return CreateColor( ( float )R / 255f, ( float )G / 255f, ( float )B / 255f );
}
}
我收到以下错误:
An unhandled exception of type 'Cloo.InvalidCommandQueueComputeException' occurred in Cudafy.NET.dll
Additional information: OpenCL error code detected: InvalidCommandQueue.
顺便说一下,Texture 结构看起来像这样:
[Cudafy]
public unsafe struct Texture
{
public int Width, Height;
public byte* Data;
}
我又完全不知所措了。。