3

定数バッファーをインスタンス化し、ピクセル シェーダーにバインドする次のコードがあります。ConstantBuffers.EveryFrame は、フレームごとに更新する必要があるすべてのデータを保持する ConstantBuffers クラスの構造体です (現在は単一の Color3 オブジェクトのみ)。

everyFrame は、定数バッファーとして使用する Direct3D11 Buffer オブジェクトです。コンテキストは私の D3DDevice.ImmediateContext です

int sizeInBytes;
ConstantBuffers.EveryFrame cb1 = new ConstantBuffers.EveryFrame();
cb1.Color = new Color3(0, 0, 0);
sizeInBytes = Marshal.SizeOf(typeof(ConstantBuffers.EveryFrame));
using (DataStream data = new DataStream(sizeInBytes, true, true))
{
    data.Write(cb1);
    data.Position = 0;
    everyFrame = new D3D.Buffer(device, data, new BufferDescription
    {
        Usage = ResourceUsage.Default,
        SizeInBytes = sizeInBytes,
        BindFlags = BindFlags.ConstantBuffer
    });
    context.PixelShader.SetConstantBuffer(everyFrame, 0);
}

このコードを実行すると、次のエラーが発生します。

SlimDX.Direct3D11.Direct3D11Exception was unhandled
  Message=E_INVALIDARG: An invalid parameter was passed to the returning function (-2147024809)
  Source=SlimDX
  StackTrace:
   at SlimDX.Result.Throw[T](Object dataKey, Object dataValue)
   at SlimDX.Result.Record[T](Int32 hr, Boolean failed, Object dataKey, Object dataValue)
   at SlimDX.Direct3D11.Buffer.Build(Device device, DataStream data, Int32 sizeInBytes, ResourceUsage usage, BindFlags bindFlags, CpuAccessFlags accessFlags, ResourceOptionFlags optionFlags, Int32 structureByteStride)
   at SlimDX.Direct3D11.Buffer..ctor(Device device, DataStream data, BufferDescription description)
   at VoxelGame.Form1.SetupConstantBuffers() in d:\files\my documents\visual studio 2010\Projects\VoxelGame\VoxelGame\Form1.cs:line 119
   at VoxelGame.Form1..ctor() in d:\files\my documents\visual studio 2010\Projects\VoxelGame\VoxelGame\Form1.cs:line 91
   at VoxelGame.Program.Main() in d:\files\my documents\visual studio 2010\Projects\VoxelGame\VoxelGame\Program.cs:line 21
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

ただし、この次のコードを実行すると (上記のコードの前に数回呼び出すだけ)、問題なく動作します。このコードは、上記のコードが使用する変数のいずれにも触れず、ほとんどが別のクラスで動作します (デバイスはクラスのコンストラクターを介して渡され、constantBuffer はクラスの変数に格納された Direct3D11 バッファーです)。

ConstantBuffers.EveryMotion cb2 = new ConstantBuffers.EveryMotion();
int sizeInBytes = Marshal.SizeOf(typeof(ConstantBuffers.EveryMotion));
using (DataStream data = new DataStream(sizeInBytes, true, true))
{
    data.Write(cb2);
    data.Position = 0;
    constantBuffer = new D3D.Buffer(device, data, new BufferDescription
    {
        Usage = ResourceUsage.Default,
        SizeInBytes = sizeInBytes,
        BindFlags = BindFlags.ConstantBuffer
    });
    device.ImmediateContext.VertexShader.SetConstantBuffer(constantBuffer, 0);
}

参考までに、ConstantBuffer 構造体は次のとおりです。

[StructLayout(LayoutKind.Sequential)]
public struct EveryMotion
{
    public Matrix WorldViewProjection;
}

[StructLayout(LayoutKind.Sequential)]
public struct EveryFrame
{
    public Color3 Color;
}
4

1 に答える 1

6

定数バッファーでは、データが適切に配置されている必要があります。SizeInBytes プロパティは、Matrix (4*4*4) の場合は 16 の倍数である必要がありますが、Color3 (4*3) の場合はそうではありません。

于 2011-03-01T18:49:33.813 に答える