Hello everybody!
I'm trying to add DirectX 10 support using SharpDX to my app but I'm having problems using the Effect frameworks.
So I jumped over to the sample applications and tried to do some changes there. The "MiniCube " sample compiles a vertex shader and a pixel shader:
// Compile Vertex and Pixel shaders
var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "VS", "vs_4_0");var vertexShader = new VertexShader(device, vertexShaderByteCode);var pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "PS", "ps_4_0");var pixelShader = new PixelShader(device, pixelShaderByteCode);// Layout from VertexShader input signaturevar layout = new InputLayout(device, ShaderSignature.GetInputSignature(vertexShaderByteCode), new[]{new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)});
Then it sets these values and the cube is rendering fine:
// Prepare All the stages
context.InputAssembler.InputLayout = layout2;context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Utilities.SizeOf<Vector4>() * 2, 0));context.VertexShader.SetConstantBuffer(0, contantBuffer);context.VertexShader.Set(vertexShader);context.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));context.PixelShader.Set(pixelShader);
But then I tried using an Effect:
var effectByteCode = ShaderBytecode.CompileFromFile("MiniCube2.fx", "fx_4_0", ShaderFlags.None, EffectFlags.None);
var effect10 = new Effect(device, effectByteCode);var technique = effect10.GetTechniqueByIndex(0);var pass = technique.GetPassByIndex(0);var passSignature = pass.Description.Signature;// Layout from VertexShader input signaturevar layout2 = new InputLayout(device, passSignature, new[]{new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)});
context.InputAssembler.InputLayout = layout2;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Utilities.SizeOf<Vector4>() * 2, 0));context.VertexShader.SetConstantBuffer(0, contantBuffer);//context.VertexShader.Set(vertexShader);context.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));//context.PixelShader.Set(pixelShader);
The rendering code remained the same, except for adding an Apply statement:
var a = context.VertexShader.GetConstantBuffers(0, 1);
pass.Apply();var b = context.VertexShader.GetConstantBuffers(0, 1);context.Draw(36, 0);
The a and b variables are only there for some sanity checks. Yet this code does not render. If I change it to set the constant buffer after every apply it works:
var a = context.VertexShader.GetConstantBuffers(0, 1);
pass.Apply();var b = context.VertexShader.GetConstantBuffers(0, 1);context.VertexShader.SetConstantBuffer(0, contantBuffer);context.Draw(36, 0);
Is this the correct way to use Effects or am I doing something else wrong? What are the rules here?
I need to get this sorted out and then move to my real app which does not work with this fix.
Thank you!