2.6 Through the Pipeline 通过整条管线
Points, lines, and triangles are the rendering primitives from which a model or an object is built. Imagine that the application is an interactive computer aided design (CAD) application, and that the user is examining a design for a waffle maker. Here we will follow this model through the entire graphics rendering pipeline, consisting of the four major stages: application, geometry, rasterization, and pixel processing. The scene is rendered with perspective into a window on the screen. In this simple example, the waffle maker model includes both lines (to show the edges of parts) and triangles (to show the surfaces). The waffle maker has a lid that can be opened. Some of the triangles are textured by a two-dimensional image with the manufacturer’s logo. For this example, surface shading is computed completely in the geometry stage, except for application of the texture, which occurs in the rasterization stage.
点、线和三角形是构建模型或对象的渲染图元。 假设该应用程序是一个交互式计算机辅助设计 (CAD) 应用程序,并且用户正在检查华夫饼机的设计。 在这里,我们将在整个图形渲染管道中遵循该模型,包括四个主要阶段:应用程序、几何、光栅化和像素处理。 场景以透视方式渲染到屏幕上的窗口中。 在这个简单的示例中,华夫饼机模型包括直线(显示零件的边缘)和三角形(显示表面)。 华夫饼机有一个可以打开的盖子。 一些三角形由带有制造商徽标的二维图像构成。 对于这个例子,表面着色完全在几何阶段计算,除了纹理的应用,它发生在光栅化阶段。
Application阶段
CAD applications allow the user to select and move parts of the model. For example, the user might select the lid and then move the mouse to open it. The application stage must translate the mouse move to a corresponding rotation matrix, then see to it that this matrix is properly applied to the lid when it is rendered. Another example: An animation is played that moves the camera along a predefined path to show the waffle maker from different views. The camera parameters, such as position and view direction, must then be updated by the application, dependent upon time. For each frame to be rendered, the application stage feeds the camera position, lighting, and primitives of the model to the next major stage in the pipeline—the geometry stage.
CAD 应用程序允许用户选择和移动模型的各个部分。 例如,用户可能会选择盖子,然后移动鼠标将其打开。 应用程序阶段必须将鼠标移动转换为相应的旋转矩阵,然后确保该矩阵在渲染时正确应用于盖子。 另一个示例:播放的动画沿着预定义的路径移动相机,以从不同的角度显示华夫饼机。 然后,应用程序必须根据时间更新摄像机参数,例如位置和视角方向。 对于要渲染的每一帧,应用程序阶段将模型的相机位置、照明和图元提供给管道中的下一个主要阶段——几何阶段。
Geometry Processing 几何处理阶段
For perspective viewing, we assume here that the application has supplied a projection matrix. Also, for each object, the application has computed a matrix that describes both the view transform and the location and orientation of the object in itself. In our example, the waffle maker’s base would have one matrix, the lid another. In the geometry stage the vertices and normals of the object are transformed with this matrix, putting the object into view space. Then shading or other calculations at the vertices may be computed, using material and light source properties. Projection is then performed using a separate user-supplied projection matrix, transforming the object into a unit cube’s space that represents what the eye sees. All primitives outside the cube are discarded. All primitives intersecting this unit cube are clipped against the cube in order to obtain a set of primitives that lies entirely inside the unit cube. The vertices then are mapped into the window on the screen. After all these pertriangle and per-vertex operations have been performed, the resulting data are passed on to the rasterization stage.
对于透视图,我们假设应用程序提供了一个投影矩阵。 此外,对于每个对象,应用程序都计算了一个矩阵,该矩阵描述了视图变换以及对象本身的位置和方向。 在我们的例子中,华夫饼机的底座有一个矩阵,盖子有另一个。 在几何阶段,对象的顶点和法线用这个矩阵进行变换,将对象放入视图空间。 然后可以使用材料和光源属性计算顶点处的着色或其他计算。 然后使用单独的用户提供的投影矩阵执行投影,将对象转换为表示眼睛所见内容的单位立方体空间。 立方体之外的所有图元都被丢弃。 与该单位立方体相交的所有图元都针对立方体进行裁剪,以获得一组完全位于单位立方体内部的图元。 然后将顶点映射到屏幕上的窗口中。 在执行完所有这些按三角形和按顶点的操作后,生成的数据将传递到光栅化阶段。
Rasterization阶段
All the primitives that survive clipping in the previous stage are then rasterized, which means that all pixels that are inside a primitive are found and sent further down the pipeline to pixel processing.
所有在前一阶段裁剪后幸存下来的图元都将被光栅化,这意味着图元内的所有像素都会被找到并进一步发送到管线中进行像素处理。
Pixel Processing
The goal here is to compute the color of each pixel of each visible primitive. Those triangles that have been associated with any textures (images) are rendered with these images applied to them as desired. Visibility is resolved via the z-buffer algorithm, along with optional discard and stencil tests. Each object is processed in turn, and the final image is then displayed on the screen.
这里的目标是计算每个可见图元的每个像素的颜色。 与任何纹理(图像)相关联的那些三角形根据需要使用应用于它们的这些图像进行渲染。 可见性通过 z-buffer 算法以及可选的丢弃和模板测试来解决。 依次处理每个对象,然后将最终图像显示在屏幕上。
Conclusion总结
This pipeline resulted from decades of API and graphics hardware evolution targeted to real-time rendering applications. It is important to note that this is not the only possible rendering pipeline; offline rendering pipelines have undergone different evolutionary paths. Rendering for film production was often done with micropolygon pipelines [289, 1734], but ray tracing and path tracing have taken over lately. These techniques, covered in Section 11.2.2, may also be used in architectural and design previsualization.
该管线源于数十年来针对实时渲染应用程序的 API 和图形硬件的发展。 重要的是要注意这不是唯一可能的渲染管道; 离线渲染管线经历了不同的进化路径。 电影制作的渲染通常使用微多边形管线 [289、1734] 完成,但光线追踪和路径追踪最近已经接管了。 11.2.2 节中介绍的这些技术也可用于架构和设计预可视化(previsualization)。
For many years, the only way for application developers to use the process described here was through a fixed-function pipeline defined by the graphics API in use. The fixed-function pipeline is so named because the graphics hardware that implements it consists of elements that cannot be programmed in a flexible way. The last example of a major fixed-function machine is Nintendo’s Wii, introduced in 2006. Programmable GPUs, on the other hand, make it possible to determine exactly what operations are applied in various sub-stages throughout the pipeline. For the fourth edition of the book, we assume that all development is done using programmable GPUs.
多年来,应用程序开发人员使用此处描述的过程的唯一方法是通过使用中的图形 API 定义的固定功能管线。 固定功能管线之所以如此命名,是因为实现它的图形硬件由无法以灵活方式编程的元素组成。 主要固定功能机器的最后一个例子是任天堂的 Wii,它于 2006 年推出。另一方面,可编程 GPU 可以准确确定在整个管线的各个子阶段应用哪些操作。 对于本书的第四版,我们假设所有开发都是使用可编程 GPU 完成的。
Further Reading and Resources拓展阅读
Blinn’s book A Trip Down the Graphics Pipeline [165] is an older book about writing a software renderer from scratch. It is a good resource for learning about some of the subtleties of implementing a rendering pipeline, explaining key algorithms such as clipping and perspective interpolation. The venerable (yet frequently updated) OpenGL Programming Guide (a.k.a. the “Red Book”) [885] provides a thorough description of the graphics pipeline and algorithms related to its use. Our book’s website, realtimerendering.com, gives links to a variety of pipeline diagrams, rendering engine implementations, and more.
Blinn 的书 A Trip Down the Graphics Pipeline [165] 是一本关于从头编写软件渲染器的旧书。 它是了解实现渲染管线的一些微妙之处的好资源,解释了裁剪和透视插值等关键算法。 久负盛名(但经常更新)的 OpenGL 编程指南(又名“红皮书”)[885] 提供了图形管线及其使用相关算法的详尽描述。 我们本书的网站 realtimerendering.com 提供了指向各种管线图、渲染引擎实现等的链接。