Sign in to follow this  
Plerion

Problem rendering Mesh

Recommended Posts

Hey everyone

Im currently having some problems rendering my meshes. Im coming from D3D9 and this is my first project with D3D11. I have my custom mesh class with the meshgeometry and vertex elements. Current i have this simple test:
[source lang="csharp"]
Rendering.Geometry.MeshGeometry<uint> geom = Rendering.Geometry.MeshGeometry<uint>.Create(device);
var elem = Rendering.Geometry.VertexElement.CreatePosition3D(device);
float[] vertices = new float[9]
{
0, 0, -3,
0, 0, 3,
0, 5, 0,
};

elem.DataStream.WriteRange(vertices);
geom.AddVertexElement(elem);

var triangle = geom.Triangles.AddFace();
triangle.P1 = 0;
triangle.P2 = 1;
triangle.P3 = 2;

geom.LoadBuffers();
Rendering.Scene.Mesh mesh = new Rendering.Scene.Mesh();
mesh.AttachGeometry(geom);

device.Scene.SceneRoot.AttachChild(mesh);
Rendering.Effects.Common.BasicEffect basicEffect = new Rendering.Effects.Common.BasicEffect(device);
basicEffect.WorldMatrix = SlimDX.Matrix.Identity;
basicEffect.ViewMatrix = SlimDX.Matrix.LookAtLH(new SlimDX.Vector3(50, 5, 0), new SlimDX.Vector3(0, 5, 0), new SlimDX.Vector3(0, 1, 0));
basicEffect.ProjectionMatrix = SlimDX.Matrix.PerspectiveFovLH((45.0f * (float)Math.PI) / 180.0f, ((float)f.ClientSize.Width / f.ClientSize.Height), 0.1f, 1000.0f);
[/source]

The basic effect looks like that:
Vertexshader:
[source lang="cpp"]
cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

struct VertexInputType
{
float4 position : POSITION;
};

struct PixelInputType
{
float4 position : SV_POSITION;
};

PixelInputType BasicEffectVS(VertexInputType input)
{
PixelInputType output = (PixelInputType)0;

input.position.w = 1.0f;
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);

return output;
}
[/source]

Pixelshader:
[source lang="cpp"]
struct PixelInputType
{
float4 position : SV_POSITION;
};
float4 BasicEffectPS(PixelInputType input) : SV_Target
{
return float4(1, 1, 1, 1);
}[/source]
Rendering is done the following way:
[source lang="csharp"] var ctx = mDevice.ImmediateContext;
ctx.InputAssembler.InputLayout = inpLayout;

shader.PerformRender((dev) =>
{
ctx.InputAssembler.SetVertexBuffers(0, mVBinding);
ctx.InputAssembler.SetIndexBuffer(mIndexBuffer, SlimDX.DXGI.Format.R32_UInt, 0);
ctx.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
ctx.DrawIndexed(Triangles.getTriangleCount() * 3, 0, 0);
}
);
[/source]
Now the problem is that nothing is rendered on the screen. Im using PIX to debug my shaders and draw calls.

The results:
As Pre-VS data i get the values from my triangle:
(Vertex, Index, Position)
0, 0, (0.0, 0.0, -3.0, 1.0)
1, 1, (0.0, 0.0, 3.0, 1.0)
2, 2, (0.0, 5.0, 0.0, 1.0)

But then at PostVS i have the following:
0, 0, (5.274, 0.0, -149.0, 0)
1, 1, (-5.274, 0.0, 151.0, 0)
2, 2, (0.0, 12.071, -24.0, 0)

While the first two position values make sense if i translate them on the screen the depth (id say z is depth) is weird. From my vertices depth should be equal for all the 3, or am i wrong? Also the images are not showing what i expect when i have a triangle aligned on the z-axis with a camera looking from the x-axis onto it:
[img]http://imagr.eu/up/50575518a8b682_PIX.jpg[/img]

Hopefully someone can help me and point me out what im doing wrong!

Thanks in advance
Plerion Edited by Plerion

Share this post


Link to post
Share on other sites
Here are some updates:
1. I forgot to transpose the matrices, i changed that!
2. I realized in PIX that my matrices are not the same as the values i set for the shader! They are completely different (1 instead of -1, -0.01 instead of 5, ...) but i have no clue why. Everything i was able to debug in the application has the correct matrix values until it comes to mapping and writing the subresource! For debugging purposes i added another variable of type uint to my VertexShaders MatrixBuffer and set its value to 200. The matrices have invalid values, the additional parameter has its correct value 200. If i add a matrix that was created "by hand" with all the values it also gets transmitted to the vertex shader without problems. Only if there is any Matrix.XXXX operation involved afterwards the matrix in the shader is completely messed up.

Here is the code i use to set a variable:
[source lang="csharp"]public void SetValue<T>(string name, T value)
{
if (mVariables.ContainsKey(name) == false)
#if DEBUG
throw new ArgumentException("Variable '" + name + "' not present in any constant buffer!");
#else
return;
#endif

var desc = mVariables[name];
var wrapper = mDevice.ImmediateContext.VertexShader;
var oldShader = wrapper.Get();
wrapper.Set(mShader);
var constBuffer = mConstantBuffers[desc.cBufferIndex];

GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned);
var pinPtr = handle.AddrOfPinnedObject();

byte[] tmpBuffer = new byte[Marshal.SizeOf(typeof(T))];
Marshal.Copy(pinPtr, tmpBuffer, 0, tmpBuffer.Length);
handle.Free();

var dataBox = mDevice.ImmediateContext.MapSubresource(constBuffer.buffer, MapMode.WriteDiscard, MapFlags.None);

// constBuffer.dataStream is a stream holding all the data in memory so you can read from it
// and you can overwrite its contents without losing the old content (not like in MapSubresource with
// MapMode.WriteDiscard)
var existingStream = constBuffer.dataStream;
existingStream.Position = desc.byteOffset;

existingStream.Write(tmpBuffer, 0, desc.byteSize);
existingStream.Position = 0;

// Write the complete content of the constant buffer to the databox of the subresource
dataBox.Data.WriteRange(existingStream.DataPointer, existingStream.Length);

mDevice.ImmediateContext.UnmapSubresource(constBuffer.buffer, 0);

wrapper.SetConstantBuffer(constBuffer.buffer, desc.cBufferIndex);
wrapper.Set(oldShader);
}[/source]

So my problem changed from strange calculations in the shader to the fact that my parameters are not correctly ported to the shader!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this