在我的应用程序中,我将世界,视图和投影矩阵传递给着色器时出现问题。我建立了一个小引擎来执行这些任务,并且我正在使用PIX和Visual Studio来调试我作为输出获取的内容。矩阵未正确转换为着色器
首先我发布,涉及到的顶点和索引代码:
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;
的几何形状似乎是正确的,因为当我调试我的绘制调用在PIX我得到了正确的值的顶点(0/0/-3)/(0/0/3)/(0/5/0),所以我认为索引缓冲区,顶点缓冲区,输入布局和多边形拓扑都正确设置。
现在在PIX中,我有那个有趣的Pre-VS,Post-VS视图。前VS,我告诉一切看起来很好,顶点是正确的顺序是正确的。当我去Post-VS并调试一个顶点时,我最终会在我的着色器中查看指令。
现在什么是不正确的矩阵传递给它与恒定的缓冲区。这里是我的着色器:
cbuffer MatrixBuffer
{
float4x4 worldMatrix;
float4x4 viewMatrix;
float4x4 projectionMatrix;
};
struct VertexInputType
{
float4 position : POSITION;
};
struct PixelInputType
{
float4 position : SV_POSITION;
};
PixelInputType BasicEffectVS(VertexInputType input)
{
PixelInputType output = (PixelInputType)0;
float4x4 worldViewProj = worldMatrix * viewMatrix * projectionMatrix;
output.position = mul(input.position, worldViewProj);
output.position.w = 1.0f;
return output;
}
当我在PIX看看三个矩阵我看到,除了worldMatrix他们已经完全错误的值(甚至NaN被包含),用于viewMatrix和projectionMatrix。我在我的应用程序中设置矩阵的方式如下:
basicEffect.WorldMatrix = SlimDX.Matrix.Identity;
basicEffect.ViewMatrix = SlimDX.Matrix.Transpose(SlimDX.Matrix.LookAtLH(new SlimDX.Vector3(20, 5, 0), new SlimDX.Vector3(0, 5, 0), new SlimDX.Vector3(0, 1, 0)));
basicEffect.ProjectionMatrix = SlimDX.Matrix.Transpose(SlimDX.Matrix.PerspectiveFovLH((float)Math.PI/4, ((float)f.ClientSize.Width/f.ClientSize.Height), 1.0f, 100.0f));
在VS中调试它们会给我正确的值。然后我按照着色器上的SetValue调用,直到实际写入字节。那里一切都很好!
缓冲区创建方式如下:
holder.buffer = new SlimDX.Direct3D11.Buffer(mShader.Device, new BufferDescription()
{
BindFlags = BindFlags.ConstantBuffer,
SizeInBytes = buffer.Description.Size,
Usage = ResourceUsage.Dynamic,
CpuAccessFlags = CpuAccessFlags.Write
});
更糟糕的是: 如果我另一个矩阵参数添加到我的着色器,我设置了硬编码矩阵该值,如:
Matrix mat = new Matrix()
{
M11 = 1,
M12 = 2,
...
};
PIX中的
我得到了我期望的值。所以我的着色器的函数设置值必须正确。
任何人有一个想法,这是从哪里来的?
你可以发布你的常量缓冲区构造代码吗? – Goz
当然,我已将它编辑到我的帖子中! – Muepe
TBH,我的意思是所有与常量缓冲区和那些矩阵有关的代码...... – Goz