• Create Account

## Beginner DirectX 10 problems

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1DwarvesH  Members

510
Like
0Likes
Like

Posted 25 February 2014 - 03:36 AM

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

// Layout from VertexShader input signature
{
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.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));

But then I tried using an Effect:

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 signature
var 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.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));

The rendering code remained the same, except for adding an Apply statement:

pass.Apply();

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:

pass.Apply();
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!

### #2gfxCahd  Members

230
Like
0Likes
Like

Posted 26 February 2014 - 07:12 AM

Yeah, you need to set any used buffer (vertex-instance-constant etc...) or shaderResource, topology,  as well as blend-depth-raster state every time before you call Draw.

Edited by gfxCahd, 26 February 2014 - 07:15 AM.

### #3DwarvesH  Members

510
Like
0Likes
Like

Posted 27 February 2014 - 07:36 AM

Thanks! I guess the pass.Apply() clobbers the buffer binding or something.

The learning curve for DirectX 10 is pretty steep. I'm facing often the problem of things working 99%, but not 100%.

Currently I'm struggling with the problem of MSAA not working. I'm setting:

SampleDescription = new SampleDescription(2, 0)


to both the swap chain descriptor and the depth buffer I create, yet AA is not active. In the debugger the descriptions have the correct value. I managed to get it to work in a small hello world app, but in my real app it is not working and I have no idea why.

### #4DwarvesH  Members

510
Like
0Likes
Like

Posted 27 February 2014 - 08:21 AM

I figured it out: it was the pass.Apply yet again!

            RasterizerStateDescription r = new RasterizerStateDescription();
r.IsAntialiasedLineEnabled = true;
r.IsMultisampleEnabled = true;
r.CullMode = CullMode.Back;
r.FillMode = FillMode.Solid;
rs = new RasterizerState(device, r);

...

for (int i = 0; i < technique.Description.PassCount; ++i) {
pass.Apply();
Device.Rasterizer.State = rs;

device.Draw(36, 0);
}


Under DirectX 9 the effect framework was pretty good. I see that under 10 it seems to do more harm than good! The behavior is different enough at least to make it a sore point of the porting effort.

### #5spazzarama  GDNet+

1630
Like
0Likes
Like

Posted 19 March 2014 - 05:01 AM

Also keep in mind that the effect framework isn't available with the SDK as of DirectX 11...

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#), Afterglow, C#raft

@spazzarama

### #6DwarvesH  Members

510
Like
0Likes
Like

Posted 19 March 2014 - 11:17 AM

Also keep in mind that the effect framework isn't available with the SDK as of DirectX 11...

Thanks! I have moved past this phase. And I'm using less and less of the FX framework as the days go by, eventually phasing it out.

But for other reading this, I now have a clear explanation why this was happening. The FX framework assigns a hidden constant buffer (under DX10+) to all the constant in the FX file. This is completely independent from the constant buffer you create manually in code and set to be used.

Calling Apply will set in use this constant buffer, overriding my previous assignment. So I needed to set it back after the Apply call.

The moral of the story: when in DirectX 10, use either the FX framework and set FX variables or use constant buffers without the FX framework. Don't use both at the same time!

There is still one strange thing I have no explanation for. Calling Apply also wiped my flag that said that the primitive should be drawn with MSAA. I had to set that flag manually.

But I did not manage to do the jump to DirectX 10. I'm stuck with 9...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.