question about deferred rendering

Started by
11 comments, last by Yours3!f 12 years, 6 months ago
Hi,

I'm trying to implement deferred rendering in a modern way using OGL 4.x core. For this goal I've read several articles pdfs, tutorials etc. about this topic, however I still have some questions.
First of all, when I create a G-buffer what do I really do?
I mean I surely want to create a FBO and an RBO and attach the RBO to the FBO so that I can render to a texture which I attach to the FBO.
But then how do I attach other stuff to the FBO?
As I've read I definitely will need at least an albedo a depth and a normal buffer. I suppose these will all be textures. However there are many formats to choose from, and it depends on the article (or rather game engine) which is used.
Which are the most common, or rather the most efficient, and nicest ones?
(to add I definitely want to go with the full HDR pipeline)
Another question is: how do I fill these buffers?
Especially when there are object in my scene which doesn't have textures or normals. And there is the cube mapping which should be completely untouched, since I only need the colors of it.
Finally, I ran into an article from Intel in which the whole shading part was solved within a compute shader. So how can I connect OpenCL and OpenGL so that I can get the same results with open source APIs?

Best regards,
Yours3!f
Advertisement

Hi,

I'm trying to implement deferred rendering in a modern way using OGL 4.x core. For this goal I've read several articles pdfs, tutorials etc. about this topic, however I still have some questions.
First of all, when I create a G-buffer what do I really do?
I mean I surely want to create a FBO and an RBO and attach the RBO to the FBO so that I can render to a texture which I attach to the FBO.
But then how do I attach other stuff to the FBO?
As I've read I definitely will need at least an albedo a depth and a normal buffer. I suppose these will all be textures. However there are many formats to choose from, and it depends on the article (or rather game engine) which is used.
Which are the most common, or rather the most efficient, and nicest ones?
(to add I definitely want to go with the full HDR pipeline)
Another question is: how do I fill these buffers?
Especially when there are object in my scene which doesn't have textures or normals. And there is the cube mapping which should be completely untouched, since I only need the colors of it.
Finally, I ran into an article from Intel in which the whole shading part was solved within a compute shader. So how can I connect OpenCL and OpenGL so that I can get the same results with open source APIs?

Best regards,
Yours3!f


Well I am trying to work on the same thing and I can only attempt to try and answer one of your questions, "How do I fill these buffers?". And I believe to fill them you bind them before drawing and then your shader should output its final data to your buffer. I am not 100% sure if that is right, but I think it is.
something like this?


glActiveTexture(GL_TEXTURE0);
texture0.bind();
glActiveTexture(GL_TEXTURE0 + 1);
albedo_texture.bind();
glActiveTexture(GL_TEXTURE0 + 2);
normal_texture.bind();
glActiveTexture(GL_TEXTURE0 + 3);
depth_texture.bind();
glUniform1i(texture0_location, GL_TEXTURE0);
glUniform1i(albedo_location, GL_TEXTURE0 + 1);
glUniform1i(normal_location, GL_TEXTURE0 + 2);
glUniform1i(depth_location, GL_TEXTURE0 + 3);
//render stuff...

(pixel shader)
#version 410
uniform sampler2D texture0;
uniform sampler2D albedo;
uniform sampler2D normal;
uniform sampler2D depth;

in vec3 normals;
in vec4 position;
smooth in vec2 texture_coordinates;

out vec4 fragment_color;

void main()
{
depth = position.z;
normal = vec4(normals, 1.0);
fragment_color = albedo = texture(texture0, texture_coordinates);
}
About the texture formats:

I've looked at the possible formats found here:
http://www.opengl.or...ki/Image_Format
and I've read about them a lot.

I've found an nvidia HDR sample that has an FPS and a timer as well with it, which features some of these formats:

From best performance to worst:

HDR format RT format FPS time (ms)
RGB9_E5 R11F_G11F_B10F 750 1.31
R11F_G11F_B10F R11F_G11F_B10F 740 1.32
RGBA16F R11F_G11F_B10F 725 1.40
RGBA32F R11F_G11F_B10F 610 1.63
RGB9_E5 RGBA16F 523 1.91
R11F_G11F_B10F RGBA16F 523 1.91
RGBA16F RGBA16F 512 1.95
RGBA32F RGBA16F 470 2.12
RGB9_E5 RGBA32F 292 3.43
R11F_G11F_B10F RGBA32F 291 3.43
RGBA16F RGBA32F 288 3.46
RGBA32F RGBA32F 277 3.6

These were achieved with a Core i3 540, a HD 5770 with 1GB GDDR5, and on 1440x900.

I didn't notice any visual difference only when using a RGBA32F HDR format. However this might be because of some bug.
To add when using high performance formats like RGB9_E5 some visual glitches might appear if we combine this with other effects.
Another thing to notice is that if we want to make the game go with 30 FPS then in the worst case HDR rendering took 10% of the rendering time, in the best it only took 4.36%. Using an easily implementable format like the RGBA16F + RGBA16F would be the best way in my opinion because it takes 6.5% of the rendering time which is nice, and we still have enough precision.

On the other hand I've read the deferred rendering tutorial on Catalina's XNA blog, in which several combinations are mentioned for the G-buffer, from which the best seemed to be using a RGBA16F for albedo, R16G16F (I don't know if there's such format in OGL) for normal data (or maybe RGB10_A2), and R32F for position data. This would use about 33 MB of memory and would leave 1 channel of 16 bit data free to use for other purposes such as storing material ID. I think there would be need for another G-buffer component for other stuff like specular intensity and exponent material ID etc. For this a RGBA, or RGB format could be used.
Did you already understand how it works? Add multiple out variables to the shader, and use glBindFragDataLocation before linking shader. And when rendering, call glDrawBuffers to tell what buffers you are using.


out vec4 depthFrag;
out vec4 normalFrag;
// ... whatever you want

void main() {
depthFrag = vec4(...);
normalFrag = vec4(...);
}



// Create shader program and attach shaders here

// Bind frag data locations
glBindFragDataLocation(shaderProgram, 0, "depthFrag");
glBindFragDataLocation(shaderProgram, 1, "normalFrag");

glLinkShader(shaderProgram);

// And when rendering, tell what buffers we will be using

GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, buffers);


Hope this helps.

Derp

No actually I didn't understand it until this point :) I just thought about it on a theoretical basis, but yeah that's what I was looking for, thank you.
Ok, I tried to implement it, but in the final lighting stage I seem to be getting no normals or depth, and I also think that depth isn't calculated properly. I do get the albedo, which is great, but due to the lack of the other two components I can't do lighting.

So here's my initialization:

void deferred::init()
{
//load the lighting shader
objs::get()->shader_loader.load_shader_control_file ( "../shaders/deferred/fs_quad.sc", &fs_quad );

//load the full-screen quad
objs::get()->obj.load_obj_file ( "../resources/fs_quad.obj", &quad, &fs_quad );

//set texture parameters

float w = objs::get()->conf.SCREEN_WIDTH;
float h = objs::get()->conf.SCREEN_HEIGHT;

fbo.create(); //generate a fbo
fbo.bind(); //bind it

GLenum modes[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
//set which draw buffers will it use
glDrawBuffers ( 3, modes );

//create render buffers
rb_albedo.create();
rb_normal.create();
rb_position.create();
rb_depth.create(); //this is not a g-buffer component it is just a depth attachment

//bind render buffers, set the storage format, and attach them to the fbo
rb_albedo.bind();
rb_albedo.set_storage_format ( GL_RGBA16F, w, h );
rb_albedo.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, &fbo );
rb_normal.bind();
rb_normal.set_storage_format ( GL_RG16F, w, h );
rb_normal.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, &fbo );
rb_position.bind();
rb_position.set_storage_format ( GL_R32F, w, h );
rb_position.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, &fbo );
rb_depth.bind();
rb_depth.set_storage_format ( GL_DEPTH_COMPONENT32, w, h );
rb_depth.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, &fbo );

//create textures
albedo.create();
normals.create();
position.create();

//set the albedo as the 5th (4) texture
glActiveTexture ( GL_TEXTURE4 );
albedo.bind(); //bind it
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
//use rgba16f format
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGBA16F, w, h, 0, GL_RGBA, GL_FLOAT, 0 );
albedo.set_dimensions ( w, h ); //store the size of the texture for future use

glActiveTexture ( GL_TEXTURE5 );
normals.bind();
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RG16F, w, h, 0, GL_RGBA, GL_FLOAT, 0 );
normals.set_dimensions ( w, h );

glActiveTexture ( GL_TEXTURE6 );
position.bind();
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D ( GL_TEXTURE_2D, 0, GL_R32F, w, h, 0, GL_RGBA, GL_FLOAT, 0 );
position.set_dimensions ( w, h );

//reset the active texture
glActiveTexture ( GL_TEXTURE0 );

//attach textures to fbo
albedo.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, &fbo );
normals.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, &fbo );
position.attach_to_frame_buffer ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, &fbo );

fbo.unbind();
}


here's the rendering code

void deferred::render()
{
//set orthographic view w, h, near, far
event::get()->get_resize()->set_orthographic ( 1.0f, 1.0f, -1.0f, 1.0f );
//disable depth testing
glDisable(GL_DEPTH_TEST);
//bind the lighting shader
fs_quad.bind();
//pass the matrices and scene information
fs_quad.pass_m4x4 ( objs::get()->ppl.get_projection_matrix(), "m4_p" );
fs_quad.pass_m4x4 ( objs::get()->ppl.get_model_view_matrix(), "m4_mv" );
//reset perspective mode
event::get()->get_resize()->set_perspective ();
//inverse projection matrix for depth to position reconstruction
fs_quad.pass_m4x4 ( objs::get()->ppl.get_projection_matrix().invert(), "inv_proj" );
//pass camera position
fs_quad.pass_vec4 ( mymath::vec4f ( objs::get()->cam.pos[0], objs::get()->cam.pos[1], objs::get()->cam.pos[2], 1.0f ), "cam_pos" );
//pass the textures
fs_quad.pass_int ( 4, "texture4" );
fs_quad.pass_int ( 5, "texture5" );
fs_quad.pass_int ( 6, "texture6" );
//draw full screen quad
quad.render();
//unbind the lighting shader
fs_quad.unbind();
//enable depth testing
glEnable(GL_DEPTH_TEST);
}


the shader that fills the G-buffer

//vertex shader
#version 410

//projection, modelview matrices
uniform mat4 m4_p, m4_mv;
uniform mat3 m3_n;

//the vertex position
in vec4 v4_vertex;
//the texture coordinates
in vec2 v2_texture;
in vec3 v3_normal;

smooth out vec2 v2_texture_coords;
out vec2 normal;
out float depth;

vec2 encode_normal_x_y_reconstruct_z(vec3 in_normal)
{
return vec2(in_normal.xy * 0.5 + 0.5);
}

void main()
{
normal = encode_normal_x_y_reconstruct_z(m3_n * v3_normal);
v2_texture_coords = v2_texture;

gl_Position = m4_p * m4_mv * v4_vertex;
depth = gl_Position.z / gl_Position.w;
}

//////////////////////////////////////////-------------------------------------------------------------

//pixel shader
#version 410

uniform sampler2D texture0;

smooth in vec2 v2_texture_coords;
in vec2 normal;
in float depth;

out vec4 v4_color; //color attachment0
out vec4 v4_normal; //c. a. 1
out vec4 v4_depth; //c. a. 2

void main()
{
v4_normal.xy = normal;
v4_depth.x = depth;
v4_color = texture(texture0, v2_texture_coords);
}


Using this shader the normals here seem to be good when I draw them: v4_color = vec4(v2_normal, 0.0, 1.0);
however when I draw the depth the mesh get all black, instead of that grayscale depth look. v4_color = vec4(vec3(depth), 1.0);
I also use the glBindFragDataLocation before linking as you suggested, although I don't know if it was bound

the lighting shader:


//vertex shader
#version 410

//projection, modelview matrices
uniform mat4 m4_p, m4_mv;

//the vertex position
in vec4 v4_vertex;
//the texture coordinates
in vec2 v2_texture;

smooth out vec2 v2_texture_coords;

void main()
{
v2_texture_coords = v2_texture;
gl_Position = m4_p * m4_mv * v4_vertex;
}


///////////////////////////////////---------------------------------------------------------------------------

//pixel shader
#version 410

uniform sampler2D texture4; //albedo RGBA16F
uniform sampler2D texture5; //normal RG16F
uniform sampler2D texture6; //depth R32F

smooth in vec2 v2_texture_coords; //texture coordinates for the G-buffer
uniform mat4 inv_proj; //inverse projection matrix
uniform vec4 cam_pos; //camera position

out vec4 v4_color; //the outgoing color

vec3 decode_normal_x_y_reconstruct_z(vec2 in_normal)
{
vec3 out_normal;
out_normal.xy = in_normal * 2 - 1; //convert from range [0.0, 1.0] to [-1.0, 1.0]
out_normal.z = sqrt(1 - dot(out_normal.xy, out_normal.xy)); //since it is perpendicular to x and y we can calculate it
return out_normal;
}

void main()
{
vec4 albedo = texture(texture4, v2_texture_coords);
vec3 normal = decode_normal_x_y_reconstruct_z(texture(texture5, v2_texture_coords).xy);
float depth = texture(texture6, v2_texture_coords).x;

//get texture coords from [0, 1] to [-1, 1], add them as x and y, add depth as z, and multiply by inverse projection matrix
vec4 position = vec4(v2_texture_coords.x * 2.0 - 1.0, -(v2_texture_coords.y * 2.0 - 1.0), depth, 1.0) * inv_proj;
position /= position.w;

//blinn lighting with a light placed at [0, 5, -2]
vec3 light = vec3( 0.0, 5.0, -2.0);
vec3 light_dir = normalize(light - position.xyz);
vec3 eye_dir = normalize(cam_pos.xyz - position.xyz);
vec3 half_vec = normalize(light_dir + eye_dir);
v4_color = max(dot(normal, light_dir), 0.0) * albedo + pow(max(dot(normal, half_vec), 0.0), 9.0) * 10.0;
}


When I try to draw the normals with v4_color = vec4(normal, 1.0); I get a black screen, this also happens with depth. Please help I'm struggling to get this working.

Best regards,
Yours3!f
I think you have to call glDrawBuffers every time you want to render something on the buffers. It's probably using only the first buffer now (which is albedo), because you don't tell it what to use when rendering.

Derp


I think you have to call glDrawBuffers every time you want to render something on the buffers. It's probably using only the first buffer now (which is albedo), because you don't tell it what to use when rendering.


Thanks, but you don't have to do this every frame. I know on Codinglabs in the tutorial it is done every frame, but you simply don't have to, because it only sets up the frame buffer object, so that it actually recieves the color input.

I updated the code (see the post above), and now I do get the color information, depth and normals as well.

Now I suppose the lighting equation or the decoding function is wrong, since I have all the data.
ok so I tried to figure out why my ligthing doesn't work, so I tried some things.

first of all if in the lighting shader I gather the normal and depth and convert them the conversion works.

void main()
{
vec3 normal = decode_normal_x_y_reconstruct_z(texture(texture5, v2_texture_coords).xy);
float depth = texture(texture6, v2_texture_coords).x;

//get texture coords from [0, 1] to [-1, 1], add them as x and y, add depth as z, and multiply by inverse projection matrix
vec4 position = vec4(v2_texture_coords.x * 2.0 - 1.0, -(v2_texture_coords.y * 2.0 - 1.0), depth, 1.0) * inv_proj;
position /= position.w;

v4_color = vec4(normal, 1.0); //gives me a normal looking scene
//v4_color = vec4(vec3(depth), 1.0); //gives me a depth looking scene
//v4_color = position; //gives me a position scene (well it is red green and blue :) )
}


However when I do this I get a black screen:

void main()
{
v4_color = texture(texture4, v2_texture_coords);
vec3 normal = decode_normal_x_y_reconstruct_z(texture(texture5, v2_texture_coords).xy);

v4_color = vec4(normal, 1.0); //this should give me a normal looking scene
}


I have no idea why that happens, and I guess this is why my lighting doesn't work too...

This topic is closed to new replies.

Advertisement