Chapter 3 The Graphics Processing Unit
3.3 The Evolution of Programmable Shading
可编程着色语言的演化
The year 2002 saw the release of DirectX 9.0 including Shader Model 2.0 (and its extended version 2.X), which featured truly programmable vertex and pixel shaders.
HLSL 由微软和英伟达合作开发,HLSL 的一个跨平台变体被称为Cg
Fortunately, DirectX 9.0 also included a new shader programming language called HLSL (High Level Shading Language). HLSL was developed by Microsoft in collaboration with NVIDIA, which released a cross-platform variant called Cg [818].
与此同时,OpenGL 也推出了类似的语言,称为GLSL
Around the same time, the OpenGL ARB (Architecture Review Board) released a somewhat similar language for OpenGL, called GLSL [647, 1084] (also known as GLslang).
Shader Model 3.0 was introduced in 2004
Shader Model 4.0,2007,included in DirectX 10.0
The next large step in programmability came in 2007. Shader Model 4.0 (included in DirectX 10.0 [123] and also available in OpenGL via extensions), introduced several major features, such as the geometry shader and stream output.
Shader Model 4.0 仅支持高级着色语言,如HLSL 和GLSL
Shader Model 4.0 also is notable in that it supports only high-level language shaders (HLSL for DirectX and GLSL for OpenGL)—there is no user-writable assembly language interface,
such as found in previous models.
CUDA,这种新的编程模型已经开始至力于非图形应用程序的通用计算(general-purpose computations)。
Besides new versions of existing APIs, new programming models such as NVIDIA's CUD A [211]
and AMD's CTM [994] have been targeted at non-graphics applications.
3.3.1 Comparison of Shader Models
比较各种着色模型
"vertex shader" 简称 "VS"
"pixel shader" 简称 " PS "
Shader Model 4.0 开始引入几何着色(geometry shader)
Table 3.1 compares the capabilities of the various shader models. In the table, "VS" stands for "vertex shader" and "PS" for "pixel shader" (Shader Model 4.0 introduced the geometry shader, with capabilities similar to those of the vertex shader). If neither "VS" nor "PS" appears, the row applies to both vertex and pixel shaders. Since the virtual machine is 4-way SIMD, each register can store between one and four independent values.
3.4 The Vertex Shader
顶点着色器
DirectX 管顶点着色器叫input assembler
In what DirectX calls the input assembler
DirectX 10 的input assembler 用一个数字来标记每一个实例,如图元和顶点
The input assembler in DirectX 10 also tags each instance, primitive, and vertex with an identifier number that can be accessed by any of the shader stages that follow.
顶点着色特效
shadow volume creation(阴影体)、vertex blending(顶点溶合)、silhouette rendering(轮廓渲染)
Chapters that follow explain a number of vertex shader effects, such as shadow volume creation, vertex blending for animating joints, and silhouette rendering.
3.5 The Geometry Shader
The geometry shader was added to the hardware-accelerated graphics pipeline with the release of DirectX 10, in late 2006.
几何着色器必须为每个生成的顶点输出一个齐次空间位置
Similar to the vertex shader, the geometry shader must output a homogeneous clip space location for each vertex produced.
几何着色器保证图元的输出顺序和输入顺序一致。这会影响性能,因为如果一些着色器单元并行执行,其产生的结果必须被保存和保持顺序。作为性能和效率的一种妥协,Shader Model 4.0限制了每次执行可产生1024 32-bit 值的总数。
The geometry shader is guaranteed to output results from primitives in the same order as they are input. This affects performance, because if a number of shader units run in parallel, results must be saved and ordered. As a compromise between capability and efficiency, there is a limit in Shader Model 4.0 of a total of 1024 32-bit values that can be generated per execution.
所以,以一张叶子作为输入产生数千叶子画刷是不可行的,并且不推荐使用几何着色器来完成这个工作。Tessellation of simple surfaces into more elaborate triangle meshes is also not recommended ????
So, generating a thousand bush leaves given a single leaf as input is not feasible and is not the recommended use of the geometry shader. Tessellation of simple surfaces into more elaborate triangle meshes is also not recommended [123].
3.6 The Pixel Shader
在Shader Model 4.0 中,总共有16 个向量(含4分量) 可以由顶点着色器传递给像素着色器。如使用了几何着色器,几何着色器可以输出32 个向量给像素着色器。
A total of 16 vectors (4 values each) can be passed from the vertex shader to the pixel shader in Shader Model 4.0. When the geometry shader is used, it can output 32 vectors to the pixel shader [261].
Shader Model 3.0 还引入了一些额外输入,如一个标记表示三角形的哪一边是可见的
Additional inputs were added specifically for the pixel shader with the introduction of Shader Model 3.0. For example, which side of a triangle is visible was added as an input flag. This knowledge is important for rendering a different material on the front versus back of each triangle in a single pass. The screen position of the fragment is also available to the pixel shader.
像素着色器的限制
像素着色器不能将其执行结果直接传给相邻的像素。
The pixel shader's limitation is that it can influence only the fragment handed it. That is, when a pixel shader program executes, it cannot send its results directly to neighboring pixels.
不过倾斜度或斜率信息可以(也不是直接访问)
The one case in which the pixel shader can access information for adjacent pixels (albeit indirectly) is the computation of gradient or derivative information.
像素着色器有这种能力,给定任意值,并计算其沿着屏幕的x,y坐标轴变化了多少像素。
The pixel shader has the ability to take any value and compute the amount by which it changes per pixel along the x and y screen axes.
能够访问斜率信息是像素着色器独一无二的能力,是其它着色器所没有的。
The ability to access gradient information is a unique capability of the pixel shader, not shared by any of the other programmable shader stages.
3.7 The Merging Stage
the merging stage is where the depths and colors of the individual fragments (generated in the pixel shader) are combined with the frame buffer. This stage is where stencil-buffer and Z-buffer operations occur.
Another operation that takes place in this stage is color blending, which is most commonly used for transparency and compositing operations (see Section 5.7).
merging stage 不可编程,但其操作高度可配置
The merging stage occupies an interesting middle point between the fixed-function stages, such as clipping, and the fully programmable shader stages. Although it is not programmable, its operation is highly configurable.