延迟渲染(Deferred Shading)技术详解

来源:互联网 发布:淘宝淘金币官网 编辑:程序博客网 时间:2024/04/19 14:58

一、Deferred shading技术简介

Deferred shading是这样一种技术:将光照/渲染计算推迟到第二步进行计算。我们这样做的目的是为了避免多次(超过1次)渲染同一个像素。

基本思想如下:

1、在第一步中,我们渲染场景,但是与通常情况下应用反射模型计算片断颜色不同的是,我们只是简单的将几何信息(位置坐标,法线向量,纹理坐标,反射系数等等)存储在中间缓冲区中,这样的缓冲区我们称之为g-buffer(g是几何geometry的缩写)。

2、在第二步,我们从g-buffer中读取信息,应用反射模型,计算出每个像素的最终颜色。

 

Deferred shading技术的应用使得我们避免了应用反射模型于最终不可见的片断上。例如,考虑这样的像素,它位于两个多边形重叠的区域。通常的片断着色器会读对每个多边形分别计算那个像素一次;然而,两次执行的结果最终只有一个成为该像素的最终颜色(这里基于的一个假设是:混合已被禁用)。这样,其中的一次计算就是无用的。有了Deferred shading技术,反射模型的计算会推迟到所有几何体被处理之后,那时候每个像素位置几何体的可见性也是已知的。这样,对于屏幕上的每个像素,反射模型的计算只会发生一次。

 

Deferred shading容易懂而且便于使用。它能够帮助实施很复杂的光照/反射模型。

 

二、结合例子来说明Deferred shading技术

下面的例子采用Deferred shading技术渲染了一个包含一个茶壶和一个圆环的场景。效果如下:

图一 场景渲染效果图

在这个例子中,我们将位置坐标、法线以及漫反射因子存储在g-buffer里。在第二步的时候,我们使用g-buffer里面的数据来进行漫反射光照模型的计算。

g-buffer包含3个纹理:分别用来存储位置坐标、法线以及漫反射因子。对应的采用了3个uniform变量:PositionTex、NormalTex、ColorTex。

他们均被关联到一个FBO上。关于FBO使用见:FBO。

 

下面是创建包含g-buffer的FBO的代码:

 

  1. GLuint depthBuf, posTex, normTex, colorTex;  
  2.   
  3.     // Create and bind the FBO   
  4.     glGenFramebuffers(1, &deferredFBO);  
  5.     glBindFramebuffer(GL_FRAMEBUFFER, deferredFBO);  
  6.   
  7.     // The depth buffer   
  8.     glGenRenderbuffers(1, &depthBuf);  
  9.     glBindRenderbuffer(GL_RENDERBUFFER, depthBuf);  
  10.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);  
  11.   
  12.     // The position buffer   
  13.     glActiveTexture(GL_TEXTURE0);   // Use texture unit 0   
  14.     glGenTextures(1, &posTex);  
  15.     glBindTexture(GL_TEXTURE_2D, posTex);  
  16.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  
  17.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  18.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  19.   
  20.     // The normal buffer   
  21.     glActiveTexture(GL_TEXTURE1);  
  22.     glGenTextures(1, &normTex);  
  23.     glBindTexture(GL_TEXTURE_2D, normTex);  
  24.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  
  25.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  26.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  27.   
  28.     // The color buffer   
  29.     glActiveTexture(GL_TEXTURE2);  
  30.     glGenTextures(1, &colorTex);  
  31.     glBindTexture(GL_TEXTURE_2D, colorTex);  
  32.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  
  33.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  34.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  35.   
  36.     // Attach the images to the framebuffer   
  37.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuf);  
  38.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, posTex, 0);  
  39.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normTex, 0);  
  40.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, colorTex, 0);  
  41.   
  42.     GLenum drawBuffers[] = {GL_NONE, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1,  
  43.                         GL_COLOR_ATTACHMENT2};  
  44.     glDrawBuffers(4, drawBuffers);  
  45.   
  46.     glBindFramebuffer(GL_FRAMEBUFFER, 0);  
  1. GLuint depthBuf, posTex, normTex, colorTex;  
  2.   
  3.     // Create and bind the FBO  
  4.     glGenFramebuffers(1, &deferredFBO);  
  5.     glBindFramebuffer(GL_FRAMEBUFFER, deferredFBO);  
  6.   
  7.     // The depth buffer  
  8.     glGenRenderbuffers(1, &depthBuf);  
  9.     glBindRenderbuffer(GL_RENDERBUFFER, depthBuf);  
  10.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);  
  11.   
  12.     // The position buffer  
  13.     glActiveTexture(GL_TEXTURE0);   // Use texture unit 0  
  14.     glGenTextures(1, &posTex);  
  15.     glBindTexture(GL_TEXTURE_2D, posTex);  
  16.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  
  17.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  18.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  19.   
  20.     // The normal buffer  
  21.     glActiveTexture(GL_TEXTURE1);  
  22.     glGenTextures(1, &normTex);  
  23.     glBindTexture(GL_TEXTURE_2D, normTex);  
  24.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  
  25.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  26.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  27.   
  28.     // The color buffer  
  29.     glActiveTexture(GL_TEXTURE2);  
  30.     glGenTextures(1, &colorTex);  
  31.     glBindTexture(GL_TEXTURE_2D, colorTex);  
  32.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);  
  33.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  
  34.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  
  35.   
  36.     // Attach the images to the framebuffer  
  37.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuf);  
  38.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, posTex, 0);  
  39.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normTex, 0);  
  40.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, colorTex, 0);  
  41.   
  42.     GLenum drawBuffers[] = {GL_NONE, GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1,  
  43.                         GL_COLOR_ATTACHMENT2};  
  44.     glDrawBuffers(4, drawBuffers);  
  45.   
  46.     glBindFramebuffer(GL_FRAMEBUFFER, 0);  

注意:三个纹理分别使用函数glFramebufferTexture2D()关联到FBO的颜色关联点0、1、2上面。接着调用函数glDrawBuffers把它们和片断着色器的输出变量联系起来。

 

函数glDrawBuffer指示了FBO成员和片断着色器输出变量之间的联系。FBO中的第i个成员对应片断着色器中的索引为i的输出变量。这样,片断着色器(下面列出了完整代码)中相对应的输出变量分别是PosiutionData,NormalData和ColorData。

 

顶点着色器实现了一个很简单的功能:将位置坐标和法线转化到eye sapce中,然后传递到片断着色器中。而纹理坐标则没有发生变化。

 

片断着色器如下:

  1. #version 400   
  2.   
  3. struct LightInfo {  
  4.   vec4 Position;  // Light position in eye coords.   
  5.   vec3 Intensity; // A,D,S intensity   
  6. };  
  7. uniform LightInfo Light;  
  8.   
  9. struct MaterialInfo {  
  10.   vec3 Kd;            // Diffuse reflectivity   
  11. };  
  12. uniform MaterialInfo Material;  
  13.   
  14. subroutine void RenderPassType();  
  15. subroutine uniform RenderPassType RenderPass;  
  16.   
  17. uniform sampler2D PositionTex, NormalTex, ColorTex;  
  18.   
  19. in vec3 Position;  
  20. in vec3 Normal;  
  21. in vec2 TexCoord;  
  22.   
  23. layout (location = 0) out vec4 FragColor;  
  24. layout (location = 1) out vec3 PositionData;  
  25. layout (location = 2) out vec3 NormalData;  
  26. layout (location = 3) out vec3 ColorData;  
  27.   
  28. vec3 diffuseModel( vec3 pos, vec3 norm, vec3 diff )  
  29. {  
  30.     vec3 s = normalize(vec3(Light.Position) - pos);  
  31.     float sDotN = max( dot(s,norm), 0.0 );  
  32.     vec3 diffuse = Light.Intensity * diff * sDotN;  
  33.   
  34.     return diffuse;  
  35. }  
  36.   
  37. subroutine (RenderPassType)  
  38. void pass1()  
  39. {  
  40.     // Store position, normal, and diffuse color in textures   
  41.     PositionData = Position;  
  42.     NormalData = Normal;  
  43.     ColorData = Material.Kd;  
  44. }  
  45.   
  46. subroutine(RenderPassType)  
  47. void pass2()  
  48. {  
  49.     // Retrieve position and normal information from textures   
  50.     vec3 pos = vec3( texture( PositionTex, TexCoord ) );  
  51.     vec3 norm = vec3( texture( NormalTex, TexCoord ) );  
  52.     vec3 diffColor = vec3( texture(ColorTex, TexCoord) );  
  53.   
  54.     FragColor = vec4( diffuseModel(pos,norm,diffColor), 1.0 );  
  55. }  
  56.   
  57. void main() {  
  58.     // This will call either pass1 or pass2   
  59.     RenderPass();  
  60. }  
  1. #version 400  
  2.   
  3. struct LightInfo {  
  4.   vec4 Position;  // Light position in eye coords.  
  5.   vec3 Intensity; // A,D,S intensity  
  6. };  
  7. uniform LightInfo Light;  
  8.   
  9. struct MaterialInfo {  
  10.   vec3 Kd;            // Diffuse reflectivity  
  11. };  
  12. uniform MaterialInfo Material;  
  13.   
  14. subroutine void RenderPassType();  
  15. subroutine uniform RenderPassType RenderPass;  
  16.   
  17. uniform sampler2D PositionTex, NormalTex, ColorTex;  
  18.   
  19. in vec3 Position;  
  20. in vec3 Normal;  
  21. in vec2 TexCoord;  
  22.   
  23. layout (location = 0) out vec4 FragColor;  
  24. layout (location = 1) out vec3 PositionData;  
  25. layout (location = 2) out vec3 NormalData;  
  26. layout (location = 3) out vec3 ColorData;  
  27.   
  28. vec3 diffuseModel( vec3 pos, vec3 norm, vec3 diff )  
  29. {  
  30.     vec3 s = normalize(vec3(Light.Position) - pos);  
  31.     float sDotN = max( dot(s,norm), 0.0 );  
  32.     vec3 diffuse = Light.Intensity * diff * sDotN;  
  33.   
  34.     return diffuse;  
  35. }  
  36.   
  37. subroutine (RenderPassType)  
  38. void pass1()  
  39. {  
  40.     // Store position, normal, and diffuse color in textures  
  41.     PositionData = Position;  
  42.     NormalData = Normal;  
  43.     ColorData = Material.Kd;  
  44. }  
  45.   
  46. subroutine(RenderPassType)  
  47. void pass2()  
  48. {  
  49.     // Retrieve position and normal information from textures  
  50.     vec3 pos = vec3( texture( PositionTex, TexCoord ) );  
  51.     vec3 norm = vec3( texture( NormalTex, TexCoord ) );  
  52.     vec3 diffColor = vec3( texture(ColorTex, TexCoord) );  
  53.   
  54.     FragColor = vec4( diffuseModel(pos,norm,diffColor), 1.0 );  
  55. }  
  56.   
  57. void main() {  
  58.     // This will call either pass1 or pass2  
  59.     RenderPass();  
  60. }  

 

片断着色器则包含了关于光源、材料的一些信息,都是uniform变量,以用于光照计算。

片断着色器里面使用了subroutine技术,实现了两个函数pass1和pass2,分别包含了第一步和第二步的操作。我们在OpenGL应用程序中通过设置uniform变量的值可以选择使用相应的功能。

 

在OpenGL应用程序里面,

实施第一步的步骤如下:

1、绑定FBO;

2、情况颜色以及深度缓冲区,选择pass1 subroutine函数,启用深度测试;

3、渲染场景。

 

实施第二步的步骤是:

1、去除FBO绑定(将其绑定到0),目的是能够渲染场景到默认缓冲区,而不是FBO里面,它就能显示到屏幕上;

2、清除颜色缓冲去对象。禁用深度测试;

3、选择pass2 subroutine函数,渲染一个充满屏幕的四边形,带有纹理坐标,每个方向的纹理坐标的范围都是从0到1.计算光照模型,得出最后的片断颜色。

 

三、如何选择使用Deferred shading技术

在图形学领域,关于Deferred shading技术的优点和缺陷备受争议。这种技术并不适用所有的场合,它取决于你的应用程序的需求。因此在觉得是否采用这个技术之前一定要权衡它带来的优点和缺陷。

Deferred shading技术带来一个很重要的缺点就是不能使用基于硬件实现的多重采样抗锯齿功能。因为渲染过程发生在第二步,所以我们在第二步需要多个样本。但是,在第二步我们只有每一个像素的一个样本。

另外一个缺点就是不能使用混合技术。

1 0
原创粉丝点击