zoukankan      html  css  js  c++  java
  • HLSL学习笔记(一):基础

    前言

    五一在家无事,于是学习了一下HLSL,基于XAN4.0的。学习完了也就总结一下,纯粹是新手学习的经验之谈,纰漏之处还望见谅,不喜勿喷。

    HLSL是一种使用GPU渲染出图像的技术,不仅可以改变最终呈现的颜色,还可以物体的大小、胖瘦和位置等。例如物体的碎裂效果就可以使用HLSL来渲染得到。

    数据类型

    数据类型有值类型、向量、矩阵、采样器、和结构体。

    1.值类型

      bool 布尔变量

      half 16为整形

      int 32位整形

      float 单精度浮点数

      double 双精度浮点数

      声明方式:float f;

      赋值方式:f = 1;

    2.向量

      声明方式:float4 f;

      赋值方式:f = {1,2,3,4};

      取值方式:float3 ff = f.rgb;

      
      说明:向可以通过xyzw或者rgba访问向量中的指定字段,x或者r就是代表0号字段
    。不仅可以单独操作一个字段,还可以对多个字段同时操作,例如3*f.xyz,就是将f中的xyz都乘以个3。

    3.矩阵

      声明方式:float2x4 f;   先行后列。

      赋值方式:f = {1,1,2,2,3,3,4,4};

      取值方式:float ff = f[0][0];

      说明:如果要对矩阵做乘法运算,请使用mul函数,另外,mul(ff,f)与mul(f,ff)的结果是不一样的。

    4.采样器

      声明方式:

        texture Texture; //纹理变量
        sampler TextureSampler = sampler_state //纹理采样器
        {
            Texture = <Texture>; //纹理采样器使用的纹理对象
            MinFilter = Linear; //缩小图形使用线性滤波
            MagFilter = Linear; //放大图形使用线性滤波
            MipFilter = Linear; //Mipmap使用线性滤波
            AddressU = Wrap; //U方向上的纹理寻址模式采用Wrap方式
            AddressV = Wrap; //V方向上的纹理寻址模式采用Wrap方式
        };

      赋值方式:在C#中对Texture赋值,effect.Parameters["Texture"].SetValue(Game.Content.Load<Texture2D>("*"));

      取值方式:tex2D(TextureSampler, TEXCOORD0);

      说明:MinFilter、MagFilter、MipFilter、AddressU、AddressV是可选项,如果不写将会使用默认值,也就是上面赋予的值。

    5.结构体

      声明方式:

        struct VertexShaderInput
        {
          float4 Position : POSITION;
          float2 TextureCoordinates : TEXCOORD0;
          float3 Normal: NORMAL;
        };

        VertexShaderInput input;

        此处与C#语法有些区别,直接这么写,不需要再写个new什么的。

      赋值方式:与C#语法一致。

      取值方式:与C#语法一致。

      说明:其它的地方都好理解,关键是每个字段后面还有个“小尾巴”,这是什么玩意啊?这个我也不太清楚,自己想当然感觉吧,应该就是显存里面有一些特殊的空间,它们有着自己名字,比如TEXCOORD0什么的,就像C#中的Dictionary。

      所以TextureCoordinates这行的意思是 创建一个float2类型的向量,它的名字是TextureCoordinates,值存储在TEXCOORD0这个内存空间。

      Position的意思对所有POSITION(POSITION0-POSITION9,貌似是这样。)内存空间的引用。

      这也就说明了入口函数的参数为什么可以千变万化,其实只是换了个名字,数据该在哪永远都在哪。

      

    语法

      语法什么的,与C#相差不大,不过还是有点区别的,至于区别的地方,我所知道的上面都写出来了。

    函数列表

    本表来自网络,我对说明做了些修改。

    Name Syntax Description
    abs abs(x) 返回x的绝对值。对x的每个元素都会独立计算一次。Absolute value (per component).
    acos acos(x) 返回x的反余弦值。对x的每个元素都会独立计算一次。Returns the arccosine of each component of x.
    all all(x) 检测x的所有元数的值是否为0.Test if all components of x are nonzero.
    any any(x) 检测x是否有某个元数的值为0.Test if any component of x is nonzero.
    asfloat asfloat(x) 将x转换为float类型。Convert the input type to a float. 
    asin asin(x) 返回x的反正弦值。对x的每个元素都会独立计算一次。
    asint asint(x) 将x转换为int类型。Convert the input type to an integer.
    asuint asuint(x) 将x转换为uint类型。
    atan atan(x) 返回x的反正切值。
    atan2 atan2(y, x) 返回y、x的反正切值。
    ceil ceil(x) 返回大于或等于x的最小整数。
    clamp clamp(x, min, max) 将x截取在[min, max]范围内。
    clip clip(x) 如果x中存在值小于0的参数,则丢弃当前像素。
    cos cos(x) 返回x的余弦值。
    cosh cosh(x) 返回x的双曲余弦值。
    cross cross(x, y) 返回x、y的叉积。 
    D3DCOLORtoUBYTE4 D3DCOLORtoUBYTE4(x) 混合和缩放4D向量x用于补偿一些对UBYTE4支持的硬件。Swizzles and scales components of the 4D vector x to compensate for the lack of UBYTE4 support in some hardware. 
    ddx ddx(x) 返回关于屏幕坐标x轴的偏导数。 
    ddy ddy(x) 返回关于屏幕坐标y轴的偏导数。
    degrees degrees(x) 将x(弧度)转换到角度。
    determinant determinant(m) 返回的正方形矩阵m的行列式。
    distance distance(x, y) 返回x、y之间的距离。
    dot dot(x, y) 返回x、y的点积。
    exp exp(x) 返回以e为底数,x为指数的指数函数值。
    exp2 exp2(x) 返回以2为底数,x为指数的指数函数值。对x的每个字段都会计算一次。
    faceforward faceforward(n, i, ng) 检测多边形是否位于正面。-n * sign(•(i, ng))。
    floor floor(x) 返回小于等于x的最大整数。
    fmod fmod(x, y) 返回x/y的浮点余数。
    frac frac(x) 返回x的小数部分。
    frexp frexp(x, exp) 返回x的尾数和指数。
    fwidth fwidth(x) 返回 abs(ddx(x)) + abs(ddy(x)),
    GetRenderTargetSampleCount GetRenderTargetSampleCount() 返回渲染目标采样器的个数。Returns the number of render-target samples. 
    GetRenderTargetSamplePosition GetRenderTargetSamplePosition(x) 返回关于给定采样器的一个采样点(x,y)。Returns a sample position (x,y) for a given sample index. 
    isfinite isfinite(x) 如果x为有限值则返回true,否则返回false。
    isinf isinf(x) 如果x为无限值则返回true,否则返回false。
    isnan isnan(x) 如果x为NAN或QNAN则返回true,否则返回false。
    ldexp ldexp(x, exp) frexp的逆运算,返回 x * 2 ^ exp。
    length length(v) 返回v向量的长度。
    lerp lerp(x, y, s) 对x、y进行插值计算。Returns x + s(y - x)。
    lit lit(n • l, n • h, m) 返回光照向量(环境光,漫反射光,镜面高光,1)。
    log log(x) 返回以e为底的对数。
    log10 log10(x) 返回以10为底的对数。
    log2 log2(x) 返回以2为底的对数。
    max max(x, y) 返回x、y中较大值。
    min min(x, y) 返回x、y中较小值。
    modf modf(x, out ip) 把x分割为整数和小数部分。
    mul mul(x, y) 返回x、y矩阵相乘的积。
    noise noise(x) Generates a random value using the Perlin-noise algorithm. 
    normalize normalize(x) 返回单位化向量,定义为 x / length(x)。
    pow pow(x, y) 返回x^y。
    radians radians(x) 将x(角度)转换到弧度。
    reflect reflect(i, n) 返回入射光线i对表面法线n的反射光线。
    refract refract(i, n, R) 返回在入射光线i,表面法线n,折射率为R下的折射光线。
    round round(x) 返回最接近x的整数。
    rsqrt rsqrt(x) 返回x平方根的倒数。 1 / sqrt(x) 。
    saturate saturate(x) 把x截取在[0, 1]之间。
    sign sign(x) 返回x的符号。
    sin sin(x) 返回x的正弦值。
    sincos sincos(x, out s, out c) 返回x的正弦值和余弦值。
    sinh sinh(x) 返回x的双曲正弦值。
    smoothstep smoothstep(min, max, x) 如果x的范围是[min, max],则返回一个介于0和1之间的Hermite插值。
    sqrt sqrt(x) 返回x的平方根,对x的每个字段都会计算一次。
    step step(a, x) 返回 (x >= a) ? 1 : 0 。
    tan tan(x) 返回x的正切值。
    tanh tanh(x) 返回x的双曲正切值。
    tex1D tex1D(s, t) 返回纹理s在t位置的颜色。1D texture lookup. 
    tex1Dbias tex1Dbias(s, t) 使用bias返回纹理s在t位置的颜色。1D texture lookup with bias. 
    tex1Dgrad tex1Dgrad(s, t, ddx, ddy) 1D texture lookup with a gradient. 
    tex1Dlod tex1Dlod(s, t) 使用LOD返回纹理s在t位置的颜色。1D texture lookup with LOD. 
    tex1Dproj tex1Dproj(s, t) 使用透视分离返回纹理s在t位置的颜色。1D texture lookup with projective divide. 
    tex2D tex2D(s, t) 返回纹理s在t位置的颜色。
    tex2Dbias tex2Dbias(s, t) 2D texture lookup with bias. 
    tex2Dgrad tex2Dgrad(s, t, ddx, ddy) 2D texture lookup with a gradient. 
    tex2Dlod tex2Dlod(s, t) 2D texture lookup with LOD. 
    tex2Dproj tex2Dproj(s, t) 2D texture lookup with projective divide. 
    tex3D tex3D(s, t) 3D texture lookup. 
    tex3Dbias tex3Dbias(s, t) 3D texture lookup with bias. 
    tex3Dgrad tex3Dgrad(s, t, ddx, ddy) 3D texture lookup with a gradient. 
    tex3Dlod tex3Dlod(s, t) 3D texture lookup with LOD. 
    tex3Dproj tex3Dproj(s, t) 3D texture lookup with projective divide. 
    texCUBE texCUBE(s, t) Cube texture lookup. 
    texCUBEbias texCUBEbias(s, t) Cube texture lookup with bias. 
    texCUBEgrad texCUBEgrad(s, t, ddx, ddy) Cube texture lookup with a gradient. 
    texCUBElod tex3Dlod(s, t) Cube texture lookup with LOD. 
    texCUBEproj texCUBEproj(s, t) Cube texture lookup with projective divide. 
    transpose transpose(m) 返回m的转置矩阵。
    trunc trunc(x) 将x的所有元素从浮点值截断到整数值。
  • 相关阅读:
    hdu 1425 sort 解题报告
    codeforces B. Jeff and Periods 解题报告
    codeforces A. Jeff and Digits 解题报告
    codeforces B. Xenia and Spies 解题报告
    Python 条件判断的使用
    Python Apache日志处理脚本(初稿)
    Python函数定义
    Python多条件配合使用
    Python循环的使用(2)
    NavigationController导航栏中添加多个UIBarButtonItem
  • 原文地址:https://www.cnblogs.com/rainstorm/p/3057444.html
Copyright © 2011-2022 走看看