Here's the class
[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector4 PositionRhw;
public Vector4 Color;
public Vector2 UV;
}
public static class TextureDrawer
{
public static Device TextureDevice;
}
public class T2D
{
public Texture2D Texture;
Effect effect;
EffectTechnique technique;
EffectPass pass;
InputLayout layout;
Buffer vertices;
List<Vector4> vertexPositions = new List<Vector4>();
List<Vector2> textureCoords = new List<Vector2>();
List<Vector4> vertexColors = new List<Vector4>();
public T2D(string FileName, SlimDX.DXGI.SwapChain swapChain)
{
Texture = Texture2D.FromFile(TextureDrawer.TextureDevice, FileName);
/*
// Setup the spriteInstances
spriteInstances = new SpriteInstance[1];
spriteInstances[0] = new SpriteInstance(new ShaderResourceView(TextureDrawer.TextureDevice, Texture), new Vector2(10, 10), new Vector2(10, 10));
*/
effect = Effect.FromFile(TextureDrawer.TextureDevice, "SimpleTexturedQuad.fx", "fx_4_0", SlimDX.D3DCompiler.ShaderFlags.None, SlimDX.D3DCompiler.EffectFlags.None);
technique = effect.GetTechniqueByIndex(0);
pass = technique.GetPassByIndex(0);
EffectResourceVariable shaderTetxure = effect.GetVariableByName("texture2d").AsResource();
ShaderResourceView textureView = new ShaderResourceView(TextureDrawer.TextureDevice, Texture);
shaderTetxure.SetResource(textureView);
InputElement[] inputElements = new InputElement[]
{
new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 16, 0),
new InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 32, 0)
};
layout = new InputLayout(TextureDrawer.TextureDevice, inputElements, pass.Description.Signature);
vertexPositions.Add(new Vector4(-0.5f, -0.5f, 0.5f, 1.0f));
vertexPositions.Add(new Vector4(-0.5f, 0.5f, 0.5f, 1.0f));
vertexPositions.Add(new Vector4(0.5f, -0.5f, 0.5f, 1.0f));
vertexPositions.Add(new Vector4(0.5f, 0.5f, 0.5f, 1.0f));
textureCoords.Add(new Vector2(0.0f, 1.0f));
textureCoords.Add(new Vector2(0.0f, 0.0f));
textureCoords.Add(new Vector2(1.0f, 1.0f));
textureCoords.Add(new Vector2(1.0f, 0.0f));
vertexColors.Add(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
vertexColors.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
vertexColors.Add(new Vector4(0.0f, 0.0f, 1.0f, 1.0f));
vertexColors.Add(new Vector4(1.0f, 0.0f, 1.0f, 1.0f));
DataStream stream = new DataStream(vertexPositions.Count * Marshal.SizeOf(typeof(Vertex)), true, true);
for (int i = 0; i < vertexPositions.Count; i++)
{
stream.Write(vertexPositions);
stream.Write(vertexColors);
stream.Write(textureCoords);
}
// Important: when specifying initial buffer data like this, the buffer will
// read from the current DataStream position; we must rewind the stream to
// the start of the data we just wrote.
stream.Position = 0;
BufferDescription bufferDescription = new BufferDescription();
bufferDescription.BindFlags = BindFlags.VertexBuffer;
bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
bufferDescription.OptionFlags = ResourceOptionFlags.None;
bufferDescription.SizeInBytes = vertexPositions.Count * Marshal.SizeOf(typeof(Vertex));
bufferDescription.Usage = ResourceUsage.Default;
vertices = new Buffer(TextureDrawer.TextureDevice, stream, bufferDescription);
stream.Close();
}
public void Draw()
{
TextureDrawer.TextureDevice.InputAssembler.SetInputLayout(layout);
TextureDrawer.TextureDevice.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
TextureDrawer.TextureDevice.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Marshal.SizeOf(typeof(Vertex)), 0));
for (int p = 0; p < technique.Description.PassCount; p++)
{
pass.Apply();
TextureDrawer.TextureDevice.Draw(vertexPositions.Count, 0);
}
}
}
This is my second day working with SlimDX so I don't know as much as I'd like and it's probably something simple.
Shader code
Texture2D texture2d;
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
SamplerState linearSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};
struct VS_IN
{
float4 position : POSITION;
float4 color : COLOR;
float2 UV: TEXCOORD0;
};
struct PS_IN
{
float4 position : SV_POSITION;
float4 color : COLOR;
float2 UV: TEXCOORD0;
};
PS_IN VS( VS_IN vertexShaderIn )
{
PS_IN vertexShaderOut = (PS_IN)0;
vertexShaderOut.position = vertexShaderIn.position;
vertexShaderOut.color = vertexShaderIn.color;
vertexShaderOut.UV = vertexShaderIn.UV;
vertexShaderOut.position = mul(vertexShaderIn.position, worldMatrix);
vertexShaderOut.position = mul(vertexShaderOut.position, viewMatrix);
vertexShaderOut.position = mul(vertexShaderOut.position, projectionMatrix);
return vertexShaderOut;
}
float4 PS( PS_IN pixelShaderIn ) : SV_Target
{
float4 finalColor = texture2d.Sample( linearSampler, pixelShaderIn.UV );
finalColor = float4(1,1,1,1);
return finalColor;
}
technique10 Render
{
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}