Jump to content
  • Advertisement
Sign in to follow this  
XBuilder

DirectX 10 problem with normals in C#

This topic is 2133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there,

 

I'm trying to learn how to render a simple scene in DirectX 10 with SharpDX's low level wrappers. I have already created a simple

application with a single triangle on the screen and a camera. Now i am trying to add a single directed light to the scene (like in the DX10 Example 6).

 

 It works fine with hard-coded normals in the Effect-File (active line in the PS shader), but if I try to pass normals from my vector buffer (commented out line below) I don't see anything.

 

What i don't understand is why the type of the position vector should be float3 in the element definition and why is it float4 in the shader file (copied from DX10 example). And how maps the shader this two structures?

 

My setup for the test triangle (like in the DX10 Example) and my effect file follows:

 

thanks in advance!

  ShaderBytecode shaderBytes = ShaderBytecode.CompileFromFile(_ShaderFile, "fx_4_0", ShaderFlags.None, EffectFlags.None, null, null);
  _SimpleEffect = new Effect(device, shaderBytes);

  EffectTechnique technique = _SimpleEffect.GetTechniqueByName("Render");
  EffectPass pass = technique.GetPassByIndex(0);
            
  var elements = new[] {
    new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 0),
    new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 12)
  };

  _LightDir = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  _LightDir.Normalize();
  _LightColor = new Vector4( 0.5f, 0.5f, 0.5f, 1.0f );

  _VertexLayout = new InputLayout(device, pass.Description.Signature, elements);

  _VertexStream = new DataStream(3 * 24, true, true);
  _VertexStream.Write(new Vector3(0.0f, 0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
  _VertexStream.Write(new Vector3(0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
  _VertexStream.Write(new Vector3(-0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
  _VertexStream.Position = 0;

  _Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
  {
    BindFlags = BindFlags.VertexBuffer,
    CpuAccessFlags = CpuAccessFlags.None,
    OptionFlags = ResourceOptionFlags.None,
    SizeInBytes = 3 * 24,
    Usage = ResourceUsage.Default
  });

  _Camera.Position = new Vector3(0.0f, 0.0f, -20.0f);
matrix view;
matrix projection;
float4 vLightDir;
float4 vLightColor;

struct VS_IN
{
	float4 pos : POSITION;
	float3 norm : NORMAL;
};

struct PS_IN
{
	float4 pos : SV_POSITION;
	float3 norm : TEXCOORD0;
};

PS_IN VS( VS_IN input )
{
	PS_IN output = (PS_IN)0;
	
	output.pos = mul(input.pos, view);
	output.pos = mul(output.pos, projection); 
	output.norm = input.norm;
	
	return output;
}

float4 PS( PS_IN input ) : SV_Target
{
	float4 finalColor = 0;
	//finalColor = saturate( dot((float3)vLightDir, input.norm) * vLightColor);
	finalColor = saturate(dot((float3)vLightDir, float3(0,0,1)) * vLightColor);
	finalColor.a = 1;
	return finalColor;
}

float4 PSSolid( PS_IN input) : SV_Target
{
	return float4(1,0,0,1);
}

technique10 Render
{
	pass P0
	{
		SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetGeometryShader( 0 );
		SetPixelShader( CompileShader( ps_4_0, PS() ) );
	}
}

technique10 RenderLight
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( 0 );
        SetPixelShader( CompileShader( ps_4_0, PSSolid() ) );
    }
}

Share this post


Link to post
Share on other sites
Advertisement

In the shader you use a float4 for position for transform reasons - to get the vert into projection space.  Try the following in your VS()

 

current: output.pos = mul(input.pos, view);

proposed: output.pos = mul(float4(input.pos,1), view);

 

EDIT:  I realized this isn't going to affect anything since you are defining your structure in the shader file as a float4 already.  Disregard!! 

 

Do you have a struct in your code to define what a vertex looks like to the CPU?  It looks like you are sending just simple Vector3s to the input assembler.  You should be sending a struct that has two elements, one for position and one for normal.  I apologize because i'm not familiar with that framework.

Edited by LyGuy

Share this post


Link to post
Share on other sites

Hello LyGuy,

 

thanks for your answer! I have tryed to write all data at once, but it doesn't changed anything. As i understand, the mapping is done by the constructor of the InputLayout class. It extracts the register information directly from the shader. But it seems that the vertex shader can't access normals from the buffer.

 

I have tried to hard-code the normals as the output of the vertex shader (line 25) and it works, but if i try to access the normal from the input (commented out line 24) it's doesn't render anything more.

PS_IN VS( VS_IN input )
{
	PS_IN output = (PS_IN)0;
	
	output.pos = mul(input.pos, view);
	output.pos = mul(output.pos, projection); 
	//output.norm = input.norm;
	output.norm = float3(0,0,1)

	return output;
}
 
float4 PS( PS_IN input ) : SV_Target
{
	float4 finalColor = 0;
	finalColor = saturate( dot((float3)vLightDir, input.norm) * vLightColor);
	finalColor.a = 1;
	return finalColor;
}
Edited by XBuilder

Share this post


Link to post
Share on other sites

_VertexStream = new DataStream(3 * 24, true, true);
_VertexStream.Write(new Vector3(0.0f, 0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Write(new Vector3(0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Write(new Vector3(-0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Position = 0;
 
_Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 3 * 24,
Usage = ResourceUsage.Default
});

 

From this code it looks like your vertex buffer is being filled with vertex elements of type Vector3.  The buffer should be filled with elements that match the signature in your vertex buffer. 

 

struct SimpleVertex

{

       Vector4 Position;

       Vector3 Normal;
};

 

This of course changes your buffer description as well.  Specifically the SizeInBytes field/property will be the size of a SimpleVertex * NumberOfVerts

Edited by LyGuy

Share this post


Link to post
Share on other sites

I have tried it already. If I try to change the elements array as following, I get the exception "HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: Invalid Argument." on InputLayout creation. The official DirectX 10 example using the Declaration with Vector3. As i have understood the mapping and the conversion is done by the system.

 

New elements array:

var elements = new[] {
  new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32A32_Float, 0, 0),
  new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 16)
};

D3D10 Example 6:

// Define the input layout
D3D10_INPUT_ELEMENT_DESC layout[] =
{
  { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
  { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};

//...

SimpleVertex vertices[] =
{
  { D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) },
   ...
}

Share this post


Link to post
Share on other sites


New elements array:
var elements = new[] {
new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 16)
};
D3D10 Example 6:
// Define the input layout
D3D10_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};

//...

SimpleVertex vertices[] =
{
{ D3DXVECTOR3( -1.0f, 1.0f, -1.0f ), D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) },
...
}

 

In the example they are using a SimpleVertex array which uses two 3 component structs for the Position and Normal respectively.  Their input layout DGXI_FORMAT matches this construct in that they are using DXGI_FORMAT_R32G32B32_FLOAT.  According to Microsoft, this format handles 3 components.

 

If you are using a SimpleVertex structure in the same way that they are, change your input layout to match as well by using the DXGI_FORMAT_R32G32B32_FLOAT format.

 

DXGI_FORMAT_R32G32B32_FLOAT

A three-component, 96-bit floating-point format that supports 32 bits per color channel.

 

Share this post


Link to post
Share on other sites

It the same situation as at start - nothing to see. I have now added a SimpleVertex struct to my code, but this have no effect too.

 

Code now:



    internal struct SimpleVertex
    {
        Vector3 Position;
        Vector3 Normal;

        public SimpleVertex(Vector3 pos, Vector3 norm)
        {
            Position = pos;
            Normal = norm;
        }
    };
const int VERTEX_SIZE = 24;

var elements = new[] {
  new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 0),
  new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 12)
};

_VertexLayout = new InputLayout(device, pass.Description.Signature, elements);

_VertexStream = new DataStream(3 * VERTEX_SIZE, true, true);
_VertexStream.Write(new SimpleVertex(new Vector3(0.0f, 0.5f, 0.5f),  new Vector3(0.0f, 0.0f, 1.0f)));
_VertexStream.Write(new SimpleVertex(new Vector3(0.5f, -0.5f, 0.5f), new Vector3(0.0f, 0.0f, 1.0f)));
_VertexStream.Write(new SimpleVertex(new Vector3(-0.5f, -0.5f, 0.5f), new Vector3(0.0f, 0.0f, 1.0f)));
_VertexStream.Position = 0;

_Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
{
  BindFlags = BindFlags.VertexBuffer,
  CpuAccessFlags = CpuAccessFlags.None,
  OptionFlags = ResourceOptionFlags.None,
  SizeInBytes = 3 * VERTEX_SIZE,
  Usage = ResourceUsage.Default
});
Edited by XBuilder

Share this post


Link to post
Share on other sites

Where do you (or the framework) bind the buffer to the IA?  Does your debugger support graphics debugging?

 

EDIT:  If you are sure that the buffer is being bound properly to the InputAssembler then the data is likely available in the vertex shader.  Perhaps something unexpected is happening with your transforms?

Edited by LyGuy

Share this post


Link to post
Share on other sites

Hallo LyGuy,

 

I am using VisualStudio 2012 with SharpDX Wrapper for C# (only low level without toolkit). I compile the effect file at the runtime. I have found a graphic debugger window. I will try it out. 

 

I bind the buffer as follows (setup before rendering):

device.InputAssembler.InputLayout = _VertexLayout;
device.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_Vertices, VERTEX_SIZE, 0));

Render code:

//Draw schene
for (int i = 0; i < technique.Description.PassCount; ++i)
{
  pass.Apply();
  device.Draw(3, 0);
}

Share this post


Link to post
Share on other sites

Hello there,

 

I have tried to use graphics debugger, but it is no support for WPF hosted Direct3D in the debugger. As i have read, third party debuggers have no support for this scenario too.

 

So i should continue error searching without debugger sad.png 

 

Any other idea?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!