我刚刚开始学习SlimDX框架,并尝试使用C#绘制一个非常简单的带有点的示例。我编写了所有代码,但是尽管ClearRenderTargetView()可以正常工作,但它没有画出点。
如果有人可以帮助我并告诉我我哪里做错了,我将不胜感激。预先谢谢你。
这是我的代码:
PointList类:
class PointList
{
IntPtr formHandle;
SwapChain swapChain;
Device device;
Buffer vertexBuffer;
RenderTargetView renderTargetView;
Viewport viewport;
VertexShader vertexShader;
PixelShader pixelShader;
ShaderSignature shaderSignature;
InputLayout inputLayout;
public PointList(IntPtr fHandle)
{
formHandle = fHandle;
swapChain = null;
device = null;
vertexBuffer = null;
renderTargetView = null;
viewport = null;
vertexShader = null;
pixelShader = null;
shaderSignature = null;
inputLayout = null;
}
public bool Init(int width, int height)
{
var description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
IsWindowed = true,
OutputHandle = formHandle,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.None,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
var context = device.ImmediateContext;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTargetView = new RenderTargetView(device, resource);
viewport = new Viewport(0, 0, width, height);
context.OutputMerger.SetTargets(renderTargetView);
context.Rasterizer.SetViewports(viewport);
using (var bytecode = ShaderBytecode.CompileFromFile("point_list.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
vertexShader = new VertexShader(device, bytecode);
shaderSignature = ShaderSignature.GetInputSignature(bytecode);
}
using (var bytecode = ShaderBytecode.CompileFromFile("point_list.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
var vertices = new DataStream(12 * 5, true, true);
vertices.Write(new Vector3(0.0f, 0.0f, 0.0f));
vertices.Write(new Vector3(0.5f, 1.0f, 0.0f));
vertices.Write(new Vector3(1.0f, 0.1f, 0.0f));
vertices.Write(new Vector3(1.5f, 0.9f, 0.0f));
vertices.Write(new Vector3(2.0f, 0.2f, 0.0f));
vertices.Position = 0;
var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
inputLayout = new InputLayout(device, elements, shaderSignature);
vertexBuffer = new Buffer(device, vertices, 12 * 5, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
context.InputAssembler.InputLayout = inputLayout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
context.VertexShader.Set(vertexShader);
context.PixelShader.Set(pixelShader);
return true;
}
public void Render()
{
var context = device.ImmediateContext;
context.ClearRenderTargetView(renderTargetView, Color.White);
context.Draw(5, 0);
swapChain.Present(0, PresentFlags.None);
}
public void Dispose()
{
device.Dispose();
swapChain.Dispose();
vertexBuffer.Dispose();
vertexShader.Dispose();
pixelShader.Dispose();
inputLayout.Dispose();
shaderSignature.Dispose();
renderTargetView.Dispose();
}
主要:
static void Main(string[] args)
{
var form = new RenderForm("Point List");
PointList pl = new PointList(form.Handle);
if(!pl.Init(form.ClientSize.Width, form.ClientSize.Height))
{
return;
}
MessagePump.Run(form, () =>
{
pl.Render();
});
pl.Dispose();
}