为什么在 D3D 上将顶点数据传递到片段着色器会失败,而在 OpenGL 上却不会?
我正在使用一个 Cg 着色器,它应该将一些预先计算的值从顶点着色器传递到片段着色器,以提高性能,但似乎只有 OpenGL 能够正确接收数据。我可以传递给 D3D 中片段着色器的唯一变量似乎是 TEXCOORD0 语义,这很糟糕,因为我需要传递 6-7 个不同的 float4 参数。我只使用一种纹理,它位于 TEXUNIT0 中。
该代码看起来像这样:
/* Vertex shader */
struct tex_coords
{
float4 c00_01;
float4 c02_10;
float2 c11;
float4 c12_20;
float4 c21_22;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
void main_vertex
(
float4 position : POSITION,
out float4 oPosition : POSITION,
uniform float4x4 modelViewProj,
float4 color : COLOR,
out float4 oColor : COLOR,
float2 tex : TEXCOORD,
uniform input IN,
out tex_coords coords
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
float2 texsize = IN.texture_size;
float2 delta = 0.5 / texsize;
float dx = delta.x;
float dy = delta.y;
coords = tex_coords (
float4(tex + float2(-dx, -dy), tex + float2(-dx, 0)),
float4(tex + float2(-dx, dy), tex + float2(0, -dy)),
tex + float2(0, 0),
float4(tex + float2(0, dy), tex + float2(dx, -dy)),
float4(tex + float2(dx, 0), tex + float2(dx, dy))
);
}
/* Fragment shader */
float4 main_fragment (in tex_coords co, uniform input IN, uniform sampler2D s0 : TEXUNIT0) : COLOR
{
// Use texture lookups on the coordinates found in co.
}
这段代码应该失败有什么原因吗?我尝试将结构中的所有变量绑定到 TEXCOORD1-through-5 语义,但在 D3D 上失败。不过在 OpenGL 上运行良好。
I'm working with a Cg shader that is supposed to pass some precalculated values from the vertex shader to the fragment shader in order to improve performance, but only OpenGL seems to be able to receive the data correctly. The only variable I can pass to the fragment shader in D3D seems to be the TEXCOORD0 semantic, which is bad, since I need to pass 6-7 different float4 params. I only use one texture, which is in TEXUNIT0.
The code looks something like this:
/* Vertex shader */
struct tex_coords
{
float4 c00_01;
float4 c02_10;
float2 c11;
float4 c12_20;
float4 c21_22;
};
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
};
void main_vertex
(
float4 position : POSITION,
out float4 oPosition : POSITION,
uniform float4x4 modelViewProj,
float4 color : COLOR,
out float4 oColor : COLOR,
float2 tex : TEXCOORD,
uniform input IN,
out tex_coords coords
)
{
oPosition = mul(modelViewProj, position);
oColor = color;
float2 texsize = IN.texture_size;
float2 delta = 0.5 / texsize;
float dx = delta.x;
float dy = delta.y;
coords = tex_coords (
float4(tex + float2(-dx, -dy), tex + float2(-dx, 0)),
float4(tex + float2(-dx, dy), tex + float2(0, -dy)),
tex + float2(0, 0),
float4(tex + float2(0, dy), tex + float2(dx, -dy)),
float4(tex + float2(dx, 0), tex + float2(dx, dy))
);
}
/* Fragment shader */
float4 main_fragment (in tex_coords co, uniform input IN, uniform sampler2D s0 : TEXUNIT0) : COLOR
{
// Use texture lookups on the coordinates found in co.
}
Is there any reason this code should fail? I've tried binding all the variables in the struct to TEXCOORD1-through-5 semantics, but it fails on D3D. Works fine on OpenGL though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Direct3D 在使用预变换顶点时出现的问题,从而完全绕过顶点着色器,这显然会导致片段着色器出现问题。强制数据通过顶点着色器应该可以解决问题。
This is a problem with Direct3D when using pre-transformed vertexes, thus bypassing the vertex shader entirely, which obviously causes problems in the fragment shader. Forcing the data to go through vertex shader should fix the problem.