是否有任何针对 HLSL 的好的教程,特别是对语法给出很好解释的教程?当我看到显示寄存器(s0)和显示tex0的样本时,我傻眼了,这些总是一样的吗?MSDN 文档读起来像一本手册,并不容易理解。
我是 HLSL 的绝对初学者,我正在尝试为 HLSL(详细信息)实现一个自定义 LINQ 提供程序,到目前为止,它可以工作,但我确信它在大多数情况下都会失败。
我的 Linq 提供程序应该将 LINQ 查询转换为 HLSL 并编译它。给定这个查询(这个着色器从三个图像中创建一个 HDR 图像):
Texture texSampler1 = GraphicsContext.Textures[0];
Texture texSampler2 = GraphicsContext.Textures[1];
Texture texSampler3 = GraphicsContext.Textures[2];
float threshold = 0.33f;
var shader = from input in GraphicsContext.Pixel
let pos = input.GetMember<float2>("pos")
let color1 = HlslMethods.tex2D(texSampler1, pos)
let color2 = HlslMethods.tex2D(texSampler2, pos)
let color3 = HlslMethods.tex2D(texSampler3, pos)
let avg1 = (color1.r + color1.g + color1.b) / 3
let avg2 = (color2.r + color2.g + color2.b) / 3
let avg3 = (color3.r + color3.g + color3.b) / 3
let thresholdMultiplicand = (1 / (HlslMethods.max(1 - threshold, threshold)))
let diff1 = Math.Abs(avg1 - threshold) * thresholdMultiplicand
let diff2 = Math.Abs(avg2 - threshold) * thresholdMultiplicand
let diff3 = Math.Abs(avg3 - threshold) * thresholdMultiplicand
select new
{
Color = ( color1 * (1f - diff1 + diff2 + diff3) +
color2 * (1f - diff1 - diff2 + diff3) +
color3 * (1f + diff1 + diff2 - diff3)) / 3
};
应该导致这个 HLSL 着色器:
sampler2D texSampler1 : register(s0);
sampler2D texSampler2 : register(s1);
sampler2D texSampler3 : register(s2);
struct MyPixelShader2Input
{
float2 pos : TEXCOORD0;
};
float4 MyPixelShader2(MyPixelShader2Input input) : COLOR
{
float2 pos = input.pos;
float4 color1 = tex2D(texSampler1, pos);
float4 color2 = tex2D(texSampler2, pos);
float4 color3 = tex2D(texSampler3, pos);
float avg1 = (((color1.r + color1.g) + color1.b) / 3);
float avg2 = (((color2.r + color2.g) + color2.b) / 3);
float avg3 = (((color3.r + color3.g) + color3.b) / 3);
float thresholdMultiplicand = (1 / max((1 - 0.33), 0.33));
float diff1 = (abs((avg1 - 0.33)) * thresholdMultiplicand);
float diff2 = (abs((avg2 - 0.33)) * thresholdMultiplicand);
float diff3 = (abs((avg3 - 0.33)) * thresholdMultiplicand);
float4 output = ((((color1 * (((1 - diff1) + diff2) + diff3)) + (color2 * (((1 - diff1) - diff2) + diff3))) + (color3 * (((1 + diff1) + diff2) - diff3))) / 3);
return output;
}
我现在遇到的问题主要是语义,尤其是价值语义(我没有找到很多使用它们的例子)。现实世界的例子也会很有帮助。我不确定那里使用了哪种着色器。