着色器相关

一、Vertex Shader

1. 顶点着色器处理数据流成图

image

 

2.Vertex Shader Virtual Machine(顶点着色器虚拟机)

   (1)它是一个能够认识顶点着色器是怎样工作的概念模型。模型空间->投影空间;使用指令来驱动ALU。

image

image

 

    (2)VS Shader Layout

     A.指令分类:和其他程序一样,某些指令必须在其它指令调用之前

 

    image

     B.例子:

 

image

C.说明

version instruction:它必须是所有shader中的第一条指令。

constants instruction:使用def来定义,该指令定义的常量只能在该着色器里访问(只读),在外面是无法访

                                问的,和SetVertexShaderConstantX是不同的

VS一般写到一个postion register中,要不然会出错;除非你事先在应用程序中调用ProcessVerteices,接受vs的处理数据,从而导致vs的结果写入顶点缓冲区中。

 

   (3).registers

   Input registers 为ALU提供数据,运行时顶点缓冲区的数据输入到input registers。

  image

 

  out register

  image

 

(4).Instructions

     寄存器为ALU提供处理的数据,而指令则规定了ALU的操作。指令的功能主要有两个:指定数据什么时候送到

    ALU;以及ALU中的计算方法。

    A. VS的指令主要分为以下几类:

    ■ Setup instructions
    ■ Arithmetic instructions
    ■ Macro-op instructions
    ■ Texture instructions
    ■ Flow-control instructions

    着色器规定了程序中使用指令槽(instruction slot)的最大数量(包括静态和动态循环的)

    image

大部分vs指令会占用一个指令槽,而macro-op会占用多个指令槽。所以,尽量设计使用较少指令的shader。

通常,使用并行操作,或使用modifers来扩展指令集,而不增加指令和寄存器数量。

 

(5).Modifiers Extend the Virtual Machine

     Modifers extend通过修改指令的结果、目的结果、源数据。主要包括:

     ■ Instruction modifiers
     ■ Destination register modifiers
     ■ Source register modifiers

     image

 

二、Pixel Shader

       像素着色器会处理每个绘制顶点所作用的像素,所以像素处理比顶点处理更费时间。

1. Pixel Processing

image

 

(1)Primitive Processing主要包括如下几个处理步骤:

image

 

(2)像素处理主要包括两个阶段:

       阶段1:把插值后的顶点数据(如、diffuse color,specular color,texturecoordinates)转换成一个或

                 多个像素颜色。可以在PS中处理。

       阶段2:把阶段1生成的像素颜色转换为成屏幕像素颜色(Frame-buffer blending)。在固定管线中处理。

       image

 

 

2. Pixel Shader Virtual Machine

    image

 

   (1) 像素着色器主要包括如下几类寄存器:

        ■ Input registers provide the interpolated per-vertex data outputs from
           primitive processing. Some specialty registers have been added
           (such as vFace and vPos in ps_3_0).
        ■ Constant registers provide constants to the ALU.
        ■ Temporary registers are like temporary shader variables.
        ■ Output registers contain the shader results.
        ■ Flow control registers control the order that shader instructions are
           executed.
        ■ The texture sampler (as we saw in the last section) uses texture coordinates
            from the shader to sample textures. Then texture samples are
            returned to the shader.

 

 

3. Shader Layout

image

       ■ A version instruction.
       ■ Setup instructions for defining constants. Later versions also require
           inputs and outputs to be declared.
       ■ The rest of the instruction types: arithmetic, texture, macro-ops, flow
          control, and modifiers.

 

       注:可以使使用def来定义本地常量(只能在该着色器中使用),而使用SetPiexelShaderConstantX定义

            的是全局常量。如同名,则本地常量覆盖全局常量。

4. Registers

5. Instructions

     指令集的作用:A、决定哪些数据从寄存器转移到ALU中;B、ALU上的数学操作。

   (1)指令的类型:

          ■ Setup instructions
          ■ Arithmetic instructions
          ■ Macro-op instructions
          ■ Texture instructions
          ■ Flow-control instructions

   (2)和VS一样,PS也有指令数量的限制

        image

   

6.Modifiers Extend the Virtual Machine

    Modifers可以在ALU操作之前修改从寄存器读取的数据(不修改源寄存器里的值),同时也可以修改ALU的操作结果。

image

posted on 2011-06-11 12:36  arun  阅读(1281)  评论(0编辑  收藏  举报

导航