HLSL 3可以单独声明像素着色器吗? [英] HLSL 3 Can a Pixel Shader be declared alone?
问题描述
我被要求将以下问题分为多个问题:
I've been asked to split the question below into multiple questions:
这是第一个问题,我可以在HLSL 3中运行没有顶点着色器的像素着色器吗?在HLSL 2中,我注意到您可以,但似乎无法在3中找到方法?
This is asking the first question, can I in HLSL 3 run a pixel shader without a vertex shader. In HLSL 2 I notice you can but I can't seem to find a way in 3?
着色器可以很好地编译,但是当我调用SpriteBatch Draw()时,我将从Visual Studio中得到此错误.
The shader will compile fine, I will then however get this error from Visual Studio when calling SpriteBatch Draw().
不能将着色器模型3.0与早期的着色器模型混合使用.如果将顶点着色器或像素着色器中的任何一个编译为3.0,则必须两者都使用."
"Cannot mix shader model 3.0 with earlier shader models. If either the vertex shader or pixel shader is compiled as 3.0, they must both be."
我不相信我已经在着色器中定义了任何东西来早于3.所以我有些困惑.任何帮助将不胜感激.
I don't believe I've defined anything in the shader to use anything earlier then 3. So I'm left a bit confused. Any help would be appreciated.
推荐答案
问题是内置的SpriteBatch
着色器为2.0.如果仅指定像素着色器,则SpriteBatch
仍将使用其内置的顶点着色器.因此版本不匹配.
The problem is that the built-in SpriteBatch
shader is 2.0. If you specify a pixel shader only, SpriteBatch
still uses its built-in vertex shader. Hence the version mismatch.
那么,解决方案就是自己指定一个顶点着色器.幸运的是,Microsoft为XNA的内置着色器提供了来源 .它所涉及的只是一个矩阵变换.这是经过修改的代码,因此您可以直接使用它:
The solution, then, is to also specify a vertex shader yourself. Fortunately Microsoft provides the source to XNA's built-in shaders. All it involves is a matrix transformation. Here's the code, modified so you can use it directly:
float4x4 MatrixTransform;
void SpriteVertexShader(inout float4 color : COLOR0,
inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position)
{
position = mul(position, MatrixTransform);
}
然后-因为SpriteBatch
不会为您设置-正确设置效果的MatrixTransform
.这是对客户端"空间的简单投影(来自
And then - because SpriteBatch
won't set it for you - setting your effect's MatrixTransform
correctly. It's a simple projection of "client" space (source from this blog post). Here's the code:
Matrix projection = Matrix.CreateOrthographicOffCenter(0,
GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
effect.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
这篇关于HLSL 3可以单独声明像素着色器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!