Cg Programming/ProgrammableGraphics Pipeline

前端之家收集整理的这篇文章主要介绍了Cg Programming/ProgrammableGraphics Pipeline前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
前言:本文是Cg Wiki系列的一篇 该系从两个方面来---Shader背后的图形原理,一些常见的Shader事例,为unity中的Shader做出讲解,我会坚持翻译下去,和大家一起学习.
由于本人刚刚开始翻译所以如果有翻译错的地方,还请大家轻拍
========================================================
Cg Programming/ProgrammableGraphics Pipeline
-------------------------------------------------------------
@H_502_31@Cg @H_502_31@编程语言/可编程图形管线
Contents
本节内容
· 1Parallelismin Graphics Pipelines
· 1图形管线中的并行计算
· 2Programmableand Fixed-Function Stages
· 2可编程阶段和固定功能阶段
· 3Data Flow
· 3数据流
· 4FurtherReading
· 4拓展阅读
Theprogrammable graphics pipeline presented here is very similar to the OpenGL(ES) 2.0 pipeline,the WebGL pipeline,and the Direct3D 8.0 pipeline. As suchit is the lowest common denominator of programmable graphics pipelines of themajority of today's desktop PCs and mobile devices.
本节所讲到的的可编程的图形管线和OpenGL(ES)2.0管线,WebGL管线以及Direct3D 8.0管线都十分类似。它是当今主流桌面PC以及移动设备可编程图形管线中的最基本的标准。

Parallelism in Graphics Pipelines
图形管线中的并行计算
GPUsare highly parallel processors. This is the main reason for their performance.In fact,they implement two kinds of parallelism: vertical and horizontalparallelism:
出于性能方面的考虑,GPU(图形处理单元)一般都具有能进行高效并行计算的处理器。实际上,GPU能进行两种并行计算:垂直并行计算和水平并行计算

Ford assembly line,1913.(1913年,福特汽车的流水线)
· Vertical parallelism describes parallel processingat different stages of a pipeline. This concept was also crucial inthe development of the assembly line at Ford Motor Company: many workers canwork in parallel on rather simple tasks. This made mass production (andtherefore mass consumption) possible. In the context of processing units in GPUs,the simple tasks correspond to less complex processing units,which save costsand power consumption.
· 垂直并行计算指的是能同时处理处于 管线中的不同阶段的计算。这种理念对于福特汽车公司的装配件流水线作业来说也起着非常重要的作用:很多工人都只需要在流水线上做一些很简单的工作即可。这使得庞大的生产过程(以及消耗过程)变得可能。对于GPU中的处理单元而言,简单工作就是减少具有计算复杂运算能力的处理单元,这样不仅能减少花费也能节能。
·
Assembly plant of the Bell Aircraft Corporation withmultiple parallel assembly lines,ca. 1944.1994年,贝尔航空公司具有多条并行作业流水线的工厂
· Horizontal parallelism describes the possibility toprocess work in multiple pipelines. This allows for even moreparallelism than the vertical parallelism in a single pipeline. Again,theconcept was also employed at Ford Motor Company and in many other industries.In the context of GPUs,horizontal parallelism of the graphics pipeline was animportant feature to achieve the performance of modern GPUs.
· 水平并行计算指的是在 多条渲染管线上同时处理任务的可行性。这使得并行计算并不仅仅局限于在单一的一条管线上的不同阶段进行垂直并行计算。同样的,这个理念在福特汽车公司和许多工厂制造情境中也都得到了广泛的应用。对于GPU而言,拥有具有水平并行计算的图形渲染管线是当代GPU能达到合格性能的重要特征之一。

Thefollowing diagram shows an illustration of vertical parallelism (processing instages represented by Boxes) and horizontal parallelism (multiple processingunits for each stage represented by multiple arrows between Boxes).
这个图表展示出了垂直并行计算(用底色标签表示出了不同阶段的处理过程)和水平并行计算(用在两个底色标签间的大量箭头表示出了每个阶段间的大量处理单元)

Vertex Data(顶点数据)
e.g. trianglemeshes provided by 3D modeling tools. 3D 建模 件提供的数据,例如三角形网格
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
many verticesare processed in parallel
处于并行处理 程中的许多
Vertex Shader(顶点着色器)
a small programin Cg (or another shading language) is applied to each vertex
用在每个 点上的用Cg或者其他着色器 编写 的一小段程序
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
PrimitiveAssembly(图元装配)
setup ofprimitives,e.g. triangles,lines,and points
例如三角形,直 线 ,点 不同图元进行装配
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
many primitivesare processed in parallel
并行 算的 元装配
Rasterization(光栅化)
interpolation ofdata for all pixels covered by the primitive (e.g. triangle)
根据各种 元(比如三角形) 所有像素点 据的
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
many fragments(corresponding to pixels) are processed in parallel
并行 算的 多片段( 对应 着像素)处理过程
Fragment Shader(片段着色器)
a small programin Cg (or another shading language) is applied to each fragment (i.e. coveredpixel)
每个片段(也就是像素) 行的一小段用 Cg 或者其他着色器 编写 的程序
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Per-FragmentOperations(逐片段处理)
configurableoperations on each fragment (i.e. covered pixel)
每个片段(也就是像素) 行配置运算
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
results of manyfragments are written in parallel to the framebuffer
片段的计算 果被并行地 入到 帧缓 存中
Framebuffer(帧缓存)
array of pixelsin which the computed fragment colors are stored
保存通 过计 算得出后的片段 色的 数组

In the following diagrams,there is only one arrow between any two stages.However,it should be understood that GPUs usually implement the graphicspipeline with massive horizontal parallelism. Only software implementations ofthe graphics pipeline,e.g. Mesa 3D (see the Wikipedia entry),usually implement a single pipeline.
在上面的图示中,虽然这里只在两个阶段间表示出了一种箭头,但是应该能明白GPU通常在图形管线中使用了大量的水平并行计算。只有一些软件(比如Mesa3D)能够只在单一一条管线上进行渲染。

Programmable and Fixed-FunctionStage
可编程阶段和固定功能阶段

Thepipelines of OpenGL ES 1.x,core OpenGL 1.x,and Direct3D 7.x are configurablefixed-function pipelines,i.e. there is no possibility to include programs inthese pipelines. In OpenGL (ES) 2.0,WebGL,and Direct3D 8.0,two stages (thevertex shader and the fragment shader stage) of the pipeline are programmable,i.e. small programs (shaders) written in Cg (or another shading language) areapplied in these stages. In the following diagram,programmable stages arerepresented by green Boxes,fixed-function stages are represented by grayBoxes,and data is represented by blue Boxes.
OpenGLES 1系列,核心OpenGL1系列以及Direct3D 7系列的管线都是只包含一些可以设置参数的固定功能管线,也就是说,这些管线没有编程能力。在OpenGL(ES) 2.0,WebGL以及Direct3D8.0开始,处于两个阶段(顶点着色器以及片段着色器)的渲染过程都拥有了可编程能力,也就是说,一些用Cg或者其他着色器语言编写的小程序能够应用到这两个阶段中。在下面的图示中,可编程的阶段将用绿色底色标签表示出来,固定功能阶段用将用蓝色底色标签标出来。

Vertex Data(顶点数据)
e.g. trianglemeshes provided by 3D modeling tools
3D建模工具提供的数据,比如三角形
Vertex Shader(顶点着色器)
a small programin Cg is applied to each vertex
作用在每个点上的一小段Cg程序
PrimitiveAssembly(图元装配)
setup ofprimitives,and points
对图元例如三角形,线段,点行装配
Rasterization(光栅化)
interpolation ofdata (e.g. color) for all pixels covered by the primitive
对图元覆盖的每个像素据(比如色)
Fragment Shader(片段着色器)
a small programin Cg is applied to each fragment (i.e. covered pixel)
作用在每个片段(也就是像素)上的一小段Cg程序
Per-FragmentOperations(逐片段处理)
configurableoperations on each fragment (i.e. covered pixel)
每个像素配置运算
Framebuffer(帧缓存)
array of pixelsin which the computed fragment colors are stored
由片段中算出成的像素数组

The vertex shader and fragment shader stages are discussed in more detail inthe platform-specific tutorials. The rasterization stage is discussed in Section“Rasterization” and theper-fragment operations in Section “Per-FragmentOperations”.
在特定平台的教程中有对顶点着色器和片段着色器这两个阶段的更详细的阐述。光栅化将在『光栅化』章节中进行阐述,逐片段处理操作将在『逐片段处理操作』章节中进行阐述。

Theprimitive assembly stage mainly consists of clipping primitives to the viewfrustum (the part of space that is visible on the screen) and optional cullingof front-facing and/or back-facing primitives. These possibilities arediscussed in more detail in the platform-specific tutorials.
图元装配阶段主要包括了:根据视锥体(屏幕中的一部分可见区域)进行剪裁,以及根据选择,对朝向正面或者背面的图元进行剪裁。有关的具体过程将在特定平台的教程中进行阐述。

Data Flow
数据流

Inorder to program Cg vertex and fragment shaders,it is important to understandthe input and ouput of each shader. To this end,it is also useful tounderstand how data is communicated between all stages of the pipeline. This isillustrated in the next diagram:
为了对顶点和片段着色器进行编程,理解每个着色器的输入和输出是很重要的。在这之后,理解数据是如何在渲染管线的不同阶段间联系起来的也很有帮助。用下面的图表来进行说明:

Vertex Data(顶点数据)
vertex inputparameters with semantics (e.g. POSITION , COLOR , NORMAL , TEXCOORD0 , TEXCOORD1 ,etc.)
义(如 POSITION COLOR NORMAL TEXCOORD0 TEXCOORD1 等等) 定的 点输入参
Vertex Shader(顶点着色器)
uniformparameters; in some cases texture data (images)
uniform参数,在一些情况下还有图片数据
vertex outputparameters with semantics (in particular POSITION , SV_POSITION ,and PSIZE but also COLOR ,etc.)
义(如 POSITION SV_POSITION 以及PSIZE 同样也有 TEXCOORD0 TEXCOORD1 等等) 定的 输出
PrimitiveAssembly(图元装配)
vertex outputparameters
输出
Rasterization(光栅化)
fragment inputparameters (interpolated at pixels) with semantics (corresponding tosemantics of the vertex output parameters)
定了(和 输出 对应 的) 义的片段(是经 过内 算后的像素)输入参
Fragment Shader(片段着色器)
uniformparameters (constant for each primitive) and texture data
uniform参数(对于每个图元来说都是一个常量)以及图片数据
fragment outputparameters with semantics (in particular COLOR and DEPTH )
定了 义(特 是COLOR和DEPTH)的片段输出
Per-FragmentOperations(逐片段处理操作)
fragment colorand fragment depth 片段的 色和深度
Framebuffer(帧缓存)


Vertex input parameters are defined based on the vertex data. Foreach vertex input parameter a semantic has to be defined,which specifies how the parameter relates to data in the fixed-functionpipeline. Examples of semantics are POSITION,COLOR,NORMAL,TEXCOORD0,TEXCOORD1,etc. This makes it possible to use Cg programs even with APIs that wereoriginally designed for a fixed-function pipeline. For example,the vertexinput parameter for vertex positions should use the POSITION semanticsuch that all APIs can provide the appropriate data for this input parameter.Note that the vertex position is in object coordinates,i.e. this is theposition as specified in a 3D modeling tool.
顶点的输入参数是根据顶点数据而来的。对于每一个顶点输入参数而言,因为语义要用来约定在固定功能管线阶段参数是如何关联到数据的,所以参数都要定义语义。比如POSITION,COLOR,NORMAL,TEXCOORD0,TEXCOORD1等等这些语义。这样之后才能正确使用Cg程序甚至其他一些基于固定功能管线设置出来的API接口。比如,对于每个表示顶点的位置的顶点输入参数都要绑定POSITION语义,这样之后API才能给输入参数提供恰当的数据。注意顶点位置是相对物体坐标系而言的,也就是说,这个位置是在3D建模工具中定下来的。

Uniformparameters (or uniforms) have the same value for all vertexshaders and all fragment shaders that are executed when rendering a specificprimitive (e.g. a triangle). However,they can be changed for other primitives.Usually,they have the same value for a large set of primitives that make up amesh. Typically,vertex transformations,specifications of light sources andmaterials,etc. are specified as uniforms.
Uniform参数(或者说uniforms)在渲染同一个特定图元(比如三角形)时,对于所有起作用的顶点着色器和片段着色器都具有相同的值。然而,对于其他图元来说,他们可能会改变值。通常情况下,对于构成网格的大多数图元来说,他们的值都是相同的。典型的是关于顶点变换,光源描述以及材质等等情况中数据参数都被定义成了uniforms。

Vertexoutput parameters are computed by the vertexshader,i.e. there is one set of values of these parameters for each vertex. Asemantic has to be specified for each parameter,e.g. POSITION,SV_POSITION,etc. Usually,there has to be an output parameter with the semantic POSITION or SV_POSITION,which determines where a primitive is rendered on the screen (“SV” stands for“system value” and can have a special meaning). The size of point primitivescan be specified by an output parameter with the semantic PSIZE.Other parameters are interpolated (see Section“Rasterization”) for each pixelcovered by a primitive.
顶点输出参数是经过了顶点着色器计算之后的值,也就是说,每个顶点都有一组相关量的参数。每个参数必须和一个语义(比如,POSITION,SV_POSITION,COLOR,TEXCOORD0,TEXCOORD1等等)绑定。通常情况下,必须要有一个参数和用来决定图元渲染在屏幕的哪个位置,并且与POSITION语义或者SV_POSITION(『SV』是『system value』系统值的缩写有着特殊的意义)语义绑定在一起。点图元的大小可以根据绑定了PSIZE的输出参数来获取。在一个图元中的每个像素会内插计算其他的参数(参考『光栅化』章节)。

Fragmentinput parameters are interpolated from thevertex output parameters for each pixel covered by a primitive. Similar tovertex output parameters,a semantic has to be specified for each fragmentinput parameter. These semantics are used to match vertex output parameterswith fragment input parameters. Therefore,the names of correspondingparameters in the vertex shader and fragment shader can be different as long asthe semantics are the same.
片段输入参数是根据顶点输出参数内插计算之后,在被图元覆盖的每个像素得到的数据。和顶点输出参数类似,每个片段输入参数都要和一个语义绑定。这些语义使得片段输入参数和顶点输出参数相匹配上。所以只要顶点输出参数和片段输入参数语义相同,他们的参数名可以不同。

Fragmentoutput parameters are computed by fragmentshaders. A semantic has to be specified,which determines how the value is usedin the following fixed-function pipeline. Most fragment shaders specify anoutput parameter with the semantic COLOR.The fragment depth is computed implicitly even if no output parameter with thesemantic DEPTH is specified.
片段输出参数是经过片段着色器计算之后的值。因为要来说明这个值在接下来的固定功能管线阶段的用途,所以必须要绑定一个语义。大多数片段着色器都将输出参数和COLOR语义绑定在一起。即使没有绑定DEPTH语义的输出参数,片段的深度值也会被隐式计算出来。

Texturedata include a uniform sampler,which specifies thetexture sampling unit,which in turn specifies the texture image from whichcolors are fetched.
图片数据包括了一张用来定义如何根据图片采样单位来从图片文件中取得颜色的采样图。

Otherdata is described in the tutorials for specific platforms.
其他数据会在具体平台的教程中做阐述。

Further Reading
拓展阅读
Themodel of the programmable graphics pipeline used by Cg is described inthe first chapter of Nvidia's Cg Tutorial.
在Nvidia的Cg教程中的第一章阐述的有关Cg中的可编程图形管线模型。
原文链接:https://www.f2er.com/javaschema/284049.html

猜你在找的设计模式相关文章