0
\$\begingroup\$

I'm trying to learn DirectX and SlimDX but I'm having a terrible time getting started.

I'm taking the Triangle tutorial and trying to add color and transformations.

Starting with controlling color, I've updated the .fx file to the following:

cbuffer cbPerObject
{
    float4x4 gWVP;
};

struct vsInput 
{
    float4 positionLS   : POSITION;
    float4 color        : COLOR;
};

struct vsOutput
{
    float4 positionCS   : POSITION;
    float4 color        : COLOR;
};

struct psInput 
{
    float4 positionCS   : POSITION;
    float4 color        : COLOR;
};

vsOutput VShader(vsInput input)
{
    vsOutput output;

    output.positionCS = input.positionLS; //mul(position, gWVP);
    output.color = input.color;

    return output;
}

float4 PShader(psInput input) : SV_TARGET
{
    return input.color;
}

And the code updates limited to the following lines:

// create test vertex data, making sure to rewind the stream afterward
var vertices = new DataStream(16 * 6, true, true);
vertices.Write(new Vector4(0.0f, 0.5f, 0.5f, 1f));  // What is W (of POSITION)
vertices.Write(new Color4(.8f, 1f, 0f, 0f));        // What should it's value be?
vertices.Write(new Vector4(0.5f, -0.5f, 0.5f, 1f));
vertices.Write(new Color4(.8f, 0f, 1f, 0f));
vertices.Write(new Vector4(-0.5f, -0.5f, 0.5f, 1f));
vertices.Write(new Color4(.8f, 0f, 0f, 1f));
vertices.Position = 0;

// create the vertex layout and buffer
// Thanks to Mārtiņš Možeiko for fixing these next lines
var elements = new[] { 
    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0) 
};

var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new SlimDX.Direct3D11.Buffer(device, 
    vertices, 
    (int)vertices.Length, 
    ResourceUsage.Default, 
    BindFlags.VertexBuffer, 
    CpuAccessFlags.None, 
    ResourceOptionFlags.None, 
    0);

After a few other attempts at using SV_POSITION, COLOR, and SV_TARGET in the wrong spots, it finally executes without error; however, the triangle does not appear.

  1. Please help me get the triangle working again with this updated shader.
  2. Sample code for SetConstantBuffer() for populating gWVP for standard perspective.

Thanks!

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

elements array sould look like this:

// create the vertex layout and buffer
var elements = new[] { 
    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) 
};

or

// create the vertex layout and buffer
var elements = new[] { 
    new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
    new InputElement("COLOR", 0, Format.R32G32B32A32_Float, InputElement.AppendAligned, 0) 
};

The last element is not an index of element. It is offset in bytes from beginning of vertex.

\$\endgroup\$
4
  • \$\begingroup\$ Oh, sorry, I edited my post to correct code. See the MiniTri11 example code in SlimDX for full example: code.google.com/p/slimdx/source/browse/trunk/samples/Direct3D11/… \$\endgroup\$ Feb 8, 2012 at 22:40
  • \$\begingroup\$ Ok, I've incorporated that fix, but the triangle still doesn't appear. Any other ideas? Thanks. \$\endgroup\$ Feb 8, 2012 at 23:19
  • \$\begingroup\$ Then the problem is somewhere else and not in InputLayout creation. \$\endgroup\$ Feb 8, 2012 at 23:44
  • \$\begingroup\$ Yeah, so I'm still back to the original situation. Given that those are the only changes I'm making to the sample, what could be causing the triangle to not be appearing with this augmented shader? \$\endgroup\$ Feb 9, 2012 at 18:01
1
\$\begingroup\$

What helped me was changing the InputElement format of the Position.

From

Format.R32G32B32A32_Float

to

Format.R32G32B32_Float
\$\endgroup\$
0
\$\begingroup\$

I had same problem and it was caused by a bad stride between vertex elements.

Check your vertexBufferBinding:

device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new 
    VertexBufferBinding(vertexBuffer, 32, 0));
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .