You might have seen my previous post about my textured quad not drawing. This time, I wanted to just slightly modify the SlimDX SimpleTriangle example to see If I could get a simple textured triangle to draw. This time, the application compiles and runs, but produces a black triangle instead of one with a texture on it. Any Ideas on what's wrong?
Here's the C# code:
namespace SimpleTriangle
{
[StructLayout(LayoutKind.Explicit, Size = 20)]
public struct Vertex
{
[FieldOffset(0)]
public Vector3 Position;
[FieldOffset(12)]
public Vector2 TexCoord;
}
static class Program
{
static void Main()
{
Device device;
SwapChain swapChain;
ShaderSignature inputSignature;
VertexShader vertexShader;
PixelShader pixelShader;
var form = new RenderForm("Tutorial 3: Simple Triangle");
var description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
RenderTargetView renderTarget;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
// setting a viewport is required if you want to actually see anything
var context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
// load and compile the vertex shader
using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
inputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}
// load and compile the pixel shader
using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
//load texture and pass it's ResourceView to the pixel shader
String file = "C:/Users/Jared/Documents/Visual Studio 2010/Projects/Dx11Test/Dx11Test/Texture.bmp";
Texture2D texture = Texture2D.FromFile(device, file);
ShaderResourceView textureView = new ShaderResourceView(device, texture);
// create test vertex data, making sure to rewind the stream afterward
var vertices = new DataStream(Marshal.SizeOf(typeof(Vertex)) * 3, true, true);
vertices.Write(new Vertex() { Position = new Vector3(0.0f, 0.5f, 0.5f), TexCoord = new Vector2(0.5f, 0.0f) });
vertices.Write(new Vertex() { Position = new Vector3(0.5f, -0.5f, 0.5f), TexCoord = new Vector2(1.0f, 1.0f) });
vertices.Write(new Vertex() { Position = new Vector3(-0.5f, -0.5f, 0.5f), TexCoord = new Vector2(0.0f, 1.0f) });
vertices.Position = 0;
// create the vertex layout and buffer
var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 0) };
var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new SlimDX.Direct3D11.Buffer(device, vertices, Marshal.SizeOf(typeof(Vertex)) * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 20, 0));
// set the shaders/shader resources
context.VertexShader.Set(vertexShader);
context.PixelShader.Set(pixelShader);
context.VertexShader.SetShaderResource(textureView, 1);
// set the sampler
SamplerDescription sampleDescr = new SamplerDescription();
sampleDescr.Filter = Filter.MinMagMipLinear;
sampleDescr.AddressU = TextureAddressMode.Wrap;
sampleDescr.AddressV = TextureAddressMode.Wrap;
sampleDescr.AddressW = TextureAddressMode.Wrap;
sampleDescr.MipLodBias = 0.0f;
sampleDescr.MaximumAnisotropy = 1;
sampleDescr.ComparisonFunction = Comparison.Always;
sampleDescr.BorderColor = System.Drawing.Color.White;
sampleDescr.MinimumLod = 0;
sampleDescr.MaximumLod = float.MaxValue;
SamplerState samplerState = SamplerState.FromDescription(device, sampleDescr);
context.PixelShader.SetSampler(samplerState, 0);
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
form.KeyDown += (o, e) =>
{
if (e.Alt && e.KeyCode == Keys.Enter)
swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
form.UserResized += (o, e) =>
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
};
MessagePump.Run(form, () =>
{
// clear the render target to a soothing blue
context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));
// draw the triangle
context.Draw(3, 0);
swapChain.Present(0, PresentFlags.None);
});
// clean up all resources
// anything we missed will show up in the debug output
vertices.Close();
vertexBuffer.Dispose();
layout.Dispose();
inputSignature.Dispose();
vertexShader.Dispose();
pixelShader.Dispose();
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
}
}
}
Here's my shader code:
SamplerState SamplerType;
Texture2D Texture;
struct VShaderInput
{
float4 position : POSITION;
float2 TexCoord : TEXCOORD0;
};
struct PShaderInput
{
float4 position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
};
PShaderInput VShader(VShaderInput input)
{
PShaderInput output = (PShaderInput)0;
output.position = input.position;
output.TexCoord = input.TexCoord;
return output;
}
float4 PShader(PShaderInput input) : SV_Target
{
float4 textureSample = Texture.Sample(SamplerType, input.TexCoord);
return textureSample;
}