MojoShader:HLSL 到 GLSL 转换
MojoShader: HLSL to GLSL conversion
我最近开始使用 Monogame 开发应用程序,并进入了着色器和着色语言的世界。目标平台是 GL。这意味着我必须在 HLSL 中编写我的着色器,随后由 MojoShader 将其转换为 GLSL。
我显然错过了一些非常明显的东西,我想知道是否有人可以帮助我。这是应该以统一的深灰色绘制场景中每个对象的代码。
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;
struct VertexShaderInput
{
float4 Position : POSITION0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return AmbientColor * AmbientIntensity;
}
technique Ambient
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
不幸的是,它只是给我一个空白屏幕。但事情是这样的,当我将像素着色器 return AmbientColor * AmbientIntensity;
中的一行代码更改为 return float4(1,1,1,1) * float(1);
时,它给了我正确的结果,我实际上可以在屏幕上看到对象。就好像当我使用全局变量时它们被设置为零。
从 HLSL 转换为 GLSL 时会不会出现问题,我在编写 HLSL 代码时必须牢记某些特殊性?
如果您能帮我回答这个问题或指出正确的方向,我将不胜感激。谢谢。
DirectX 中的 FX 系统允许您使用值初始化统一变量 AmbientColor
和 AmbientIntensity
,但 GLSL 不允许您这样做。就此而言,HLSL 也没有——只有建立在 HLSL 之上的 FX 系统才允许它。
因此这些制服将默认为零(或者可能是其他一些未初始化的值)。
在使用GLSL着色器之前,您需要在程序代码中设置统一常量。在 OpenGL 中,这通常通过调用 glUniform1f
和 glUniform4f
.
来完成
我最近开始使用 Monogame 开发应用程序,并进入了着色器和着色语言的世界。目标平台是 GL。这意味着我必须在 HLSL 中编写我的着色器,随后由 MojoShader 将其转换为 GLSL。
我显然错过了一些非常明显的东西,我想知道是否有人可以帮助我。这是应该以统一的深灰色绘制场景中每个对象的代码。
float4x4 World;
float4x4 View;
float4x4 Projection;
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;
struct VertexShaderInput
{
float4 Position : POSITION0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
return AmbientColor * AmbientIntensity;
}
technique Ambient
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
不幸的是,它只是给我一个空白屏幕。但事情是这样的,当我将像素着色器 return AmbientColor * AmbientIntensity;
中的一行代码更改为 return float4(1,1,1,1) * float(1);
时,它给了我正确的结果,我实际上可以在屏幕上看到对象。就好像当我使用全局变量时它们被设置为零。
从 HLSL 转换为 GLSL 时会不会出现问题,我在编写 HLSL 代码时必须牢记某些特殊性?
如果您能帮我回答这个问题或指出正确的方向,我将不胜感激。谢谢。
DirectX 中的 FX 系统允许您使用值初始化统一变量 AmbientColor
和 AmbientIntensity
,但 GLSL 不允许您这样做。就此而言,HLSL 也没有——只有建立在 HLSL 之上的 FX 系统才允许它。
因此这些制服将默认为零(或者可能是其他一些未初始化的值)。
在使用GLSL着色器之前,您需要在程序代码中设置统一常量。在 OpenGL 中,这通常通过调用 glUniform1f
和 glUniform4f
.