我正在尝试使用SharpDX(DX11)在屏幕上绘制一个三角形。无论出于何种原因,三角形似乎只是每隔一帧绘制一次。我的设备初始化代码如下所示:
public void Init()
{
renderForm = new RenderForm(Engine.GameTitle);
renderForm.ClientSize = new Size(Engine.Settings.Screen.Width, Engine.Settings.Screen.Height);
renderForm.MaximizeBox = false;
var desc = new SwapChainDescription()
{
BufferCount = 2,
ModeDescription = new ModeDescription(renderForm.ClientSize.Width, renderForm.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = renderForm.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Sequential,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain);
deviceContext = device.ImmediateContext;
var factory = swapChain.GetParent<Factory>();
factory.MakeWindowAssociation(renderForm.Handle, WindowAssociationFlags.IgnoreAll);
backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
renderView = new RenderTargetView(device, backBuffer);
backBuffer.Dispose();
deviceContext.OutputMerger.SetTargets(renderView);
deviceContext.Rasterizer.SetViewports(new Viewport(0, 0, renderForm.ClientSize.Width, renderForm.ClientSize.Height, 0.0f, 1.0f));
ProjectionMatrix = Matrix.PerspectiveFovLH(
(float)(Math.PI / 4),
(float)(renderForm.ClientSize.Width / renderForm.ClientSize.Height),
nearPlane,
farPlane);
WorldMatrix = Matrix.Identity;
renderForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width / 2 - Engine.Settings.Screen.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - Engine.Settings.Screen.Height / 2);
}
渲染三角形的代码如下所示:
public void Render()
{
DeviceContext context = D3DRenderer.Instance.GetDevice().ImmediateContext;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<Vertex>(), 0));
context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
}
public void RenderShader(int indexCount)
{
device.ImmediateContext.DrawIndexed(indexCount, 0, 0);
}
而在RenderShader()之前调用Render()。 除Direct3D警告外,任何函数都不会返回任何错误消息:
D3D11:警告:ID3D11DeviceContext :: DrawIndexed:顶点着色器单元第0槽位的常量缓冲区大小太小(提供64字节,至少192字节,预期)。
我的MatrixBuffer结构如下所示:
[StructLayout(LayoutKind.Sequential)]
internal struct MatrixBuffer
{
public Matrix world;
public Matrix view;
public Matrix projection;
}
我一直在使用不同的颜色清除每个其他帧的后备缓冲区,以确保它没有正确交换后备缓冲区的问题。这很好。
对于为什么现在不能正常工作,我感到非常困惑。我希望有人知道答案。
答案 0 :(得分:1)
女士们,先生们,今天要上课。永远不要复制和粘贴教程代码。永远。 原来问题出在着色器呈现代码中。我复制声明的教程(并没有在这里发布,否则你们可能已经非常明显了)将world / view / projection矩阵声明如下:
public Matrix WorldMatrix { get; private set; }
然后我试着这样做:
D3DRenderer.Instance.WorldMatrix.Transpose();
现在显而易见的原因不起作用。有趣的是,它确实似乎每隔一帧都有效。为什么我不知道。但是在将矩阵定义从私有集更改为set之后,现在一切正常。