I implemented a simple deferred shading renderer. I use 3 G-Buffer for storing position (R32F), normal (G16R16F) and albedo (ARGB8). I use sphere map algorithm to store normals in world space. Currently I use inverse of view * projection matrix to calculate the position of each pixel from stored depth value.

  1. First I want to avoid per pixel matrix multiplication for calculating the position. Is there another way to store and calculate position in G-Buffer without the need of matrix multiplication
  2. Store the normal in view space

Every lighting in my engine is in world space and I want do the lighting in view space to speed up my lighting pass.

I want an optimized lighting pass for my deferred engine.

  • \$\begingroup\$ I don't actually see a question here... store the positions and normals in view space and you're done? If you store the position in a separate buffer, I don't see why you would need a matrix multiplication? You can save a lot of memory band width by reconstructing the position from fragment coords and the depth buffer (at least if you use OpenGL, not sure what version of DirectX you need to be able to read the depth buffer as a texture). \$\endgroup\$
    – ccxvii
    Commented Nov 19, 2012 at 15:18

1 Answer 1


For pixel positions, as mentioned in the comments, it's generally considered best to use the depth buffer and the pixel's 2D position on screen to calculate its view space or world space position. If you're using an older D3D/OpenGL API version you may need to store an extra copy of the depth in an R32F buffer (it's unclear to me if this is what you're doing now); but in more recent APIs you can read the actual depth buffer directly, saving yourself one G-buffer slot.

In any case, most of the position reconstruction math can be moved into the vertex shader or the CPU, with only a reciprocal and a madd in the fragment shader (the reciprocal is not needed if you're using a linear depth buffer). The details of how to do this are in this blog post.

  • \$\begingroup\$ Thanks for the link finally I store the camera ray distant in my depth map and every thing works fine now. \$\endgroup\$
    – kochol
    Commented Dec 18, 2012 at 12:54
  • \$\begingroup\$ Could you give me a keyword to find out how to read the depth buffer directly in newer OpenGL? I want to do that but couldn't find the needed functions. \$\endgroup\$
    – danijar
    Commented May 14, 2013 at 8:53
  • \$\begingroup\$ I'm not an OpenGL expert, but I think you might need to create an FBO and attach a texture created using one of the depth formats. \$\endgroup\$ Commented May 14, 2013 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .