【发布时间】:2014-09-28 22:03:55
【问题描述】:
我有一个不再正确绘制的着色器。它在 XNA 中工作,但必须为 DX11 和 SharpDX 重写,现在使用着色器模型 5。没有例外,着色器效果编译良好,除了不相关的(?)默认采样器外,没有来自设备的调试层消息州投诉。它是一个普通的全屏四边形,执行最简单的纹理 blit。 (使用随机噪声纹理进行测试)
我正在为顶点着色器提供一个VertexPositionTexutre,这是一个包含以下内容的 SharpDX 对象:
[VertexElement("SV_Position")]
public Vector3 Position;
[VertexElement("TEXCOORD0")]
public Vector2 TextureCoordinate;
我怀疑签名之间存在不匹配。 VertexPositionTexture 有一个为“SV_Position”定义的 Vector3 位置组件。
D3D11 defines "SV_Position" 作为 float4。它是具有具体本机类型的系统值常量。我担心着色器会在Vector3 的 float3 末尾吃掉额外的浮点数。这也可以解释为什么我的 UV 在几个顶点上被破坏了。
这是我的顶点着色器:
struct V2P
{
float4 vPosition : SV_Position;
float2 vTexcoord : TEXCOORD0;
};
V2P VSCopy(float4 InPos : SV_Position,
float2 InTex : TEXCOORD0)
{
V2P Out = (V2P)0;
// transform the position to the screen
Out.vPosition = float4(InPos);
Out.vTexcoord = InTex;
return Out;
}
我尝试将VSCopy() 的输入从float4 更改为float3,但结果没有变化。没有重新编译 SharpDX VertexPositionTexture,我不知道如何解决这个问题,但 SharpDX 完全支持 DirectX11,所以我一定做错了。
我正在定义我的顶点并将它们设置如下:
//using SharpDX.Toolkit.Graphics
[...]//In the initialization
verts = new VertexPositionTexture[]
{
new VertexPositionTexture(
new Vector3(1.0f,-1.0f,0),
new Vector2(1,1)),
new VertexPositionTexture(
new Vector3(-1.0f,-1.0f,0),
new Vector2(0,1)),
new VertexPositionTexture(
new Vector3(-1.0f,1.0f,0),
new Vector2(0,0)),
new VertexPositionTexture(
new Vector3(1.0f,1.0f,0),
new Vector2(1,0))
}
//vb is a Buffer<VertexPositionTexture>
short[] indeces = new short[] { 0, 1, 2, 2, 3, 0 };
vb = SharpDX.Toolkit.Graphics.Buffer.Vertex.New(Game.Instance.GraphicsDevice, verts, SharpDX.Direct3D11.ResourceUsage.Dynamic);
ib = SharpDX.Toolkit.Graphics.Buffer.Index.New(Game.Instance.GraphicsDevice, indeces, dx11.ResourceUsage.Dynamic);
[...] //In the Render method, called every frame to draw the quad
Game.Instance.GraphicsDevice.SetVertexBuffer<VertexPositionTexture>(vb, 0);
Game.Instance.GraphicsDevice.SetIndexBuffer(ib, false);
Game.Instance.GraphicsDevice.DrawIndexed(PrimitiveType.TriangleList, 6);
【问题讨论】:
-
您是如何创建顶点缓冲区和输入布局的?
-
我已经更新了这个问题,确切地说是如何设置顶点和索引缓冲区
-
您是否正在创建
VertexInputLayout并同时调用SetVertexInputLayout? -
我曾尝试过,但我发现我做错了,试图直接使用 D3D11 设备对象,却没有意识到 Toolkit 有一个方法
GraphicsDevice.SetVertexInputLayout();可以精确地做到这一点。这解决了我的问题。我会生成一个答案。
标签: hlsl directx-11 vertex-shader sharpdx