尝试实现LINQ to HLSL提供程序。有没有好的HLSL教程?特别是那些专注于语法的人

时间:2010-12-19 23:40:51

标签: linq syntax hlsl

HLSL是否有任何优秀的教程可以对语法做出很好的解释?当我看到显示寄存器(s0)的样本和显示tex0的样本时,我傻眼了,它们总是一样吗? MSDN文档读起来像手册,不容易理解。

我是HLSL的绝对初学者,我正在尝试为HLSL(details)实现一个自定义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;
}

我现在遇到的麻烦主要是语义,尤其是值语义(我没有找到很多使用它们的例子)。真实世界的例子也会非常有用。我不确定那里使用了什么样的着色器。

1 个答案:

答案 0 :(得分:0)

来自DXSDK:

类型注册说明:

  • b:常量缓冲区
  • t:纹理和纹理缓冲区
  • c:缓冲区偏移量
  • s:采样器
  • u:无序访问视图

这是针对SM 5.0的。如果您的目标只是SM 4.0或更低,则您没有无人机。对于SM 3.0或更低版本,采样器和纹理之间没有清晰的分离。我的建议是始终使用采样器寄存器语法(register(s0)tex0相同)。