Jump to content
  • Advertisement
Sign in to follow this  

Problem rendering Mesh

This topic is 2280 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

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,


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

Rendering.Scene.Mesh mesh = new Rendering.Scene.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);

The basic effect looks like that:
[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 lang="cpp"]
struct PixelInputType
float4 position : SV_POSITION;
float4 BasicEffectPS(PixelInputType input) : SV_Target
return float4(1, 1, 1, 1);
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);
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:

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)
throw new ArgumentException("Variable '" + name + "' not present in any constant buffer!");

var desc = mVariables[name];
var wrapper = mDevice.ImmediateContext.VertexShader;
var oldShader = wrapper.Get();
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);

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);

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
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!