首页>
技术资讯>
详情

在游戏制作中的着色器编程介绍

2016-05-29 来源:CloudBest 阅读量: 198
关键词: 程序设计


  前言(Introduction)
  
  纵观3D加速卡的发展历史,世界各大显卡制造公司的工程师们一直都在致力于增强3D芯片的功能,提高其运行速度。第一块3D加速卡只能够光栅化三角形,只支持最简单的几种色彩混合模式。剧烈的竞争导致了这个领域的科技得到了快速的发展。今天的3D加速卡已经学会了对三角形进行变换和光照处理,并且支持多重纹理混合,执行着色器程序(用于处理顶点和象素的小程序)等复杂操作。
  Throughout the history of 3D-accelerators, engineers of the world's leading manufacturing companies increased the functionality of 3D-chips along with raising their speed. The first 3D-accelerators were only capable of rasterizing triangles and supported the simplest modes of color blending. Intense competition caused rapid development in this field of science. Today, 3D-accelerators have learned to transform and light triangles, perform complicated operations of blending several textures and execute shaders - short programs operating with vertices and pixels.
  
  着色器技术已经在3D渲染程序中广泛使用了(比如Renderman),它象征着一种强大而灵活的面描绘方式。现在,这种技术正被引入3D加速卡。然而,我们也必须看到Renderman和OpenGL/DirectX所使用的着色器的不同之处。首先,我们应该知道Renderman所使用的着色器是用于进行相对较慢的渲染的,而后者纯粹就是针对实时渲染的。
  The technology of shaders is widely used in 3D-rendering programs (like Renderman) and represents a powerful and flexible means of surfaces description. Today, this technology is being introduced into 3D-accelerators. However, in order to get the picture we have to see the difference between the Renderman shaders and those used in DirectX/OpenGL. First of all, we should remember that the former are meant for relatively slow rendering, while the latter are purely aimed at real-time rendering.
  

 

  在着色器出现之前,人们使用T&L(转换与光照)引擎来提供对顶点的几何处理,多重纹理(MT)引擎从属于光栅化过程。所有硬件上支持着色器的新显卡都同时支持T&L和多重纹理(MT)引擎,因此游戏开发者们在渲染的时候可以选择使用新方式或者老方式(有时也被称作可编程方式和固定函数方式)。
  Before the shaders appeared, it was the T&L (transformation and lighting) engine that provided geometrical processing of vertices, while the multitexturing (MT) engine was in charge of rasterization. Vertex and pixel shaders have emerged as ideological successors to these engines. All new 3D-accelerators with hardware shaders support will also include the T&L and MT engines, thus allowing game developers to choose between the new and the old methods of rendering (also called programmable and fixed-function, respectively).
  
 

  当着色器规范不断进化的时候,一系列版本的象素和顶点着色器被引入到DirectX中。本文主要是基于GeForce3所对应的着色器版本(顶点着色器v1.1,象素着色器v1.1)撰写的。
  As the specification for shaders keeps evolving, tracking of versions of pixel and vertex shaders was introduced in DirectX. This article is mostly based on materials about GeForce3 which in terms of versions corresponds to Vertex Shader 1.1 and Pixel Shaders 1.1.
  DirectX 8.0           DirectX 8.1
  Vertex Shaders 1.0 (obsolete)
  Vertex Shaders 1.1
  Pixel Shaders 0.5 (obsolete)  Pixel Shaders 1.2
  Pixel Shaders 1.0 (obsolete)  Pixel Shaders 1.3
  Pixel Shaders 1.1       Pixel Shaders 1.4
  
  顶点着色器(Vertex Shaders)
  
  顶点着色器包含两个部分:着色器函数和着色器声明。着色器函数定义了单个顶点上可以执行的操作。从顶点数据流中读出顶点后,就由着色器函数来对其进行处理。顶点数据既可以由数据缓冲区提供,也可以由图元镶嵌引擎提供。原始数据(顶点坐标,法线,等等)将被载入到输入寄存器v0-v15中。一开始,输入寄存器的编号和其功能之间没有严格的对应关系,比如某个顶点的坐标既可以被载入v0,也可以被载入v15。事实上,不使用硬件镶嵌的时候,着色器根本就不知道输入寄存器中载入的是什么数据。为了让图像加速卡能够知道哪些输入寄存器应该载入哪些数据,就需要使用着色器声明。另外,为了尽可能高效的利用显存的带宽和AGP总线带宽,应该极力避免载入冗余数据。
  A vertex shader consists of two parts: shader function and shader declaration. Shader function defines what operations must be executed on a single vertex. Vertices are read from the vertex data stream and are sequentially processed by the shader function. Data can be supplied either by the vertex buffer or by the primitive tessellation engine. Initial data (vertex coordinates, normal, etc.) are loaded into the input registers v0..v15. From the outset, the specification sets no strict correspondence between the register number and its function, for example a vertex coordinates can be loaded in either v0 or v15 registers. As a matter of fact, when no hardware tessellation is used, the shader is unaware of what data are loaded in the input registers. In order that the accelerator could know what data should be loaded in each input register, shader declaration is

热门推荐 查看更多