升级了 MonoGame,现在我无法编译我的着色器
Upgraded MonoGame and now I can't get my shader to compile
我将我的单人游戏升级到了最新版本。最初在管线工具中构建着色器时,出现错误 Vertex shader 'SpriteVertexShader' must be SM 4.0 level 9.1 or higher!所以我将 ps_2_0 更改为 ps_4_0_level_9_1 但现在出现错误:
错误 X3004:未声明的标识符 'SecondPassTextureSampler'
有人知道如何解决这个问题吗?
#include "Macros.fxh"
texture Bitmap;
sampler2D FirstPassTexture = sampler_state{
Texture = (Bitmap);
MagFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
sampler2D SecondPassTexture = sampler_state{
Texture = (Bitmap);
MagFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
#define KERNEL_RADIUS 7
#define KERNEL_WIDTH (2*KERNEL_RADIUS + 1)
// Uniforms
BEGIN_CONSTANTS
float kernel[KERNEL_WIDTH];
float2 offsets[KERNEL_WIDTH];
MATRIX_CONSTANTS
float4x4 MatrixTransform _vs(c0) _cb(c0);
END_CONSTANTS
struct VSOutput
{
float4 position : SV_Position;
float4 color : COLOR0;
float2 texCoord : TEXCOORD0;
};
VSOutput SpriteVertexShader( float4 position : SV_Position,
float4 color : COLOR0,
float2 texCoord : TEXCOORD0)
{
VSOutput output;
output.position = mul(position, MatrixTransform);
output.color = color;
output.texCoord = texCoord;
return output;
}
float4 gaussH(VSOutput input): SV_Target0
{
float4 color = float4(0,0,0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(offsets[i].x, 0.0) )) * kernel[i];
return color * input.color;
}
float4 gaussV(VSOutput input): SV_Target0
{
float4 color = float4(0.0,0.0,0.0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) * kernel[i];
return color * input.color;
}
float4 gaussVGlow(VSOutput input): SV_Target0
{
float4 color = float4(1.0,1.0,1.0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
{
float alpha = SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )).a * kernel[i];
color.a += alpha;
}
// This will make stripes on top of the glow
/*
float m = smoothstep(0.45, 0.55, 0.5*cos(25.0*atan2(input.texCoord.y-0.5, input.texCoord.x-0.5))+0.5) * 0.5 + 0.5;
color.a *= m;
*/
color.a = pow(color.a, 0.5);
color.rgb *= color.a; // Yeah, you have to pre multiply your alpha -- either that or render with premultiply option
return color * input.color;
}
technique Blur {
pass p0 {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussH();
}
pass p1 {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussV();
}
pass p1Glow {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussVGlow();
}
}
Macros.fxh 是:http://pastebin.com/y51kFfii
来自 Monogame 论坛:
你得到一个错误,因为你使用宏 SAMPLE_TEXTURE 进行采样,但不使用宏 DECLARE_TEXTURE 来声明纹理。
如果要使用 Macros.fxh 中的宏,则纹理需要命名为 SecondPassTexture,采样器需要命名为 SecondPassTextureSampler。 (不要更改调用 SAMPLE_TEXTURE 的行。)
如果您查看宏(_Macro.fxh,第 31 行),代码
SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) ))
扩展为
SecondPassTexture.Sample(SecondPassTextureSampler, (input.texCoord + float2(0.0, offsets[i].y) ))
但是你的情况(原文post)应该是
Bitmap.Sample(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) ))
这是一个更简单的解决方案,您可以尝试:
只需将所有 SAMPLE_TEXTURE 替换为 tex2D。 (保留原始 post 中的采样器。)
我将我的单人游戏升级到了最新版本。最初在管线工具中构建着色器时,出现错误 Vertex shader 'SpriteVertexShader' must be SM 4.0 level 9.1 or higher!所以我将 ps_2_0 更改为 ps_4_0_level_9_1 但现在出现错误:
错误 X3004:未声明的标识符 'SecondPassTextureSampler'
有人知道如何解决这个问题吗?
#include "Macros.fxh"
texture Bitmap;
sampler2D FirstPassTexture = sampler_state{
Texture = (Bitmap);
MagFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
sampler2D SecondPassTexture = sampler_state{
Texture = (Bitmap);
MagFilter = Linear;
MinFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};
#define KERNEL_RADIUS 7
#define KERNEL_WIDTH (2*KERNEL_RADIUS + 1)
// Uniforms
BEGIN_CONSTANTS
float kernel[KERNEL_WIDTH];
float2 offsets[KERNEL_WIDTH];
MATRIX_CONSTANTS
float4x4 MatrixTransform _vs(c0) _cb(c0);
END_CONSTANTS
struct VSOutput
{
float4 position : SV_Position;
float4 color : COLOR0;
float2 texCoord : TEXCOORD0;
};
VSOutput SpriteVertexShader( float4 position : SV_Position,
float4 color : COLOR0,
float2 texCoord : TEXCOORD0)
{
VSOutput output;
output.position = mul(position, MatrixTransform);
output.color = color;
output.texCoord = texCoord;
return output;
}
float4 gaussH(VSOutput input): SV_Target0
{
float4 color = float4(0,0,0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(offsets[i].x, 0.0) )) * kernel[i];
return color * input.color;
}
float4 gaussV(VSOutput input): SV_Target0
{
float4 color = float4(0.0,0.0,0.0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
color += SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) * kernel[i];
return color * input.color;
}
float4 gaussVGlow(VSOutput input): SV_Target0
{
float4 color = float4(1.0,1.0,1.0,0);
for(int i = 0; i < KERNEL_WIDTH; ++i)
{
float alpha = SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )).a * kernel[i];
color.a += alpha;
}
// This will make stripes on top of the glow
/*
float m = smoothstep(0.45, 0.55, 0.5*cos(25.0*atan2(input.texCoord.y-0.5, input.texCoord.x-0.5))+0.5) * 0.5 + 0.5;
color.a *= m;
*/
color.a = pow(color.a, 0.5);
color.rgb *= color.a; // Yeah, you have to pre multiply your alpha -- either that or render with premultiply option
return color * input.color;
}
technique Blur {
pass p0 {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussH();
}
pass p1 {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussV();
}
pass p1Glow {
VertexShader = compile vs_4_0_level_9_1 SpriteVertexShader();
PixelShader = compile ps_4_0_level_9_1 gaussVGlow();
}
}
Macros.fxh 是:http://pastebin.com/y51kFfii
来自 Monogame 论坛: 你得到一个错误,因为你使用宏 SAMPLE_TEXTURE 进行采样,但不使用宏 DECLARE_TEXTURE 来声明纹理。 如果要使用 Macros.fxh 中的宏,则纹理需要命名为 SecondPassTexture,采样器需要命名为 SecondPassTextureSampler。 (不要更改调用 SAMPLE_TEXTURE 的行。)
如果您查看宏(_Macro.fxh,第 31 行),代码
SAMPLE_TEXTURE(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) 扩展为
SecondPassTexture.Sample(SecondPassTextureSampler, (input.texCoord + float2(0.0, offsets[i].y) )) 但是你的情况(原文post)应该是
Bitmap.Sample(SecondPassTexture, (input.texCoord + float2(0.0, offsets[i].y) )) 这是一个更简单的解决方案,您可以尝试: 只需将所有 SAMPLE_TEXTURE 替换为 tex2D。 (保留原始 post 中的采样器。)