42

Deferred shading is only a technique to "defer" the actual shading operation for later stages, this can be great to reduce the number of passes needed (for example) to render 10 lights which needs 10 passes. My point is regardless of the rendering technique you are using there are certain possible rendering optimizations that reduce the number of objects (...


23

A semi-common approach is to make what I call shader components, similar to what I think you're calling modules. The idea is similar to a post-processing graph. You write chunks of shader code that includes both the necessary inputs, the generated outputs, and then the code to actually work on them. You have a list which denotes which shaders to apply in ...


11

In deferred shading all the material properties are rendered into the G-buffer, e.g. albedo, normals, roughness, metalness, etc. that are needed for BRDF evaluation. After this step shading is performed for pixels within light volumes using light and material properties as input to the BRDF. The problem with deferred shading is that more complex BRDF's (e.g. ...


9

I understand that having the same "size" each attachment can be aligned better, but practically speaking is it better to waste channels (or reserve them for future use) and having all the RTs of the same size or I should use just what needed? Having a unified 32bit aligned Render Targets is better, even if it means "wasting" some memory. This will be ...


8

The attenuation equation you posted, strictly speaking, does not allow for any finite light radius, since the attenuation will not go to zero at any finite distance. However, in practical terms there will be some distance at which the light becomes too dim to see. If you define a luminance threshold, you can plug this into the attenuation formula and solve ...


7

The skybox should be drawn after all lighting has been done. It's done exactly the same way as in a forward-shaded renderer; it shouldn't go into the G-buffer at all, and the lighting shaders shouldn't need to know anything about it. I don't think there's any need to fiddle around with the viewport depth range or stencil either. If you use a projection ...


6

You can easily use the Z-Buffer to render only the pixels that matter during skybox/light passes. I render the skybox last (after all lights). When rendering the skybox, make sure your depth buffer is still intact and use GL_GEQUAL for the depth function. Then in the vertext shader of the skybox, set the z value of gl_Position to its w value. This makes ...


6

Final answer, solved the performance problem! Changed my culling loop to this instead (based on the one used by Dice in BF3) uint threadCount = WORK_GROUP_SIZE * WORK_GROUP_SIZE; uint passCount = (numActiveLights + threadCount - 1) /threadCount; for (uint passIt = 0; passIt < passCount; ++passIt) { uint lightIndex = passIt * threadCount + ...


6

Your problem is not related to deferred shading whatsoever, you need to implement the basic core elements of a renderer before you try to speed up some specific part. When you have finished with what concept3d has explained, if you actually find that you need to optimize the deferred shader itself (as opposed to the whole rasterization pass) you can ...


6

Update for Unity 5.5 See @TWickz's answer. TL;DR Add #define UNITY_BRDF_PBS BRDF2_Unity_PBS right before #pragma's and #define's in the shader. Finding the solution was time-consuming, but I gained useful knowledge and so will you if you follow my “investigation log” below. I hope it's suitable answer format in this case. Step 1 Tinkering with light....


5

Anti-aliasing in a deferred shader is a complex topic. WikiPedia lists a number of techniques for doing anti-aliasing in a deferred-compatible way. Typically you'll need to do it after lighting, otherwise you can end up with lighting artifacts. Most approaches I know of do another pass on the scene after the entire deferred pipeline is complete. If you ...


5

Solution I used good algorithm for depth linearization but I didn't convert depth to NDC first: const float near = 0.1; // projection matrix's near plane const float far = 90.0; // projection matrix's far plane float LinearizeDepth(float depth) { float z = depth * 2.0 - 1.0; // back to NDC return (2.0 * near * far) / (far + near - z * (far - near))...


5

I found a solution that works well. I don't think it gets any faster than this. What I did first is, I did the matrix-vector multiply by manually and skipped all the matrix components which are zero. I used an inverse projection matrix for this (to transform to view space). That left me with this code: vec3 calculate_view_position(vec2 texture_coordinate,...


4

The result sampled from gbuffer_texture[2] will be in the [0, 1] range, but in OpenGL, NDC space ranges from -1 to 1 along all three axes. (This is different from D3D, where the NDC space ranges from 0 to 1 along the z axis.) So, you need to multiply the depth result by 2 and subtract 1 to convert the range to [-1, 1], just as you're doing already for the ...


4

The draw order often implied by tutorials, where you do something like this: for each object: for each pass: apply pass state draw object is actually backwards from how it makes sense to do it in a "real game" context. Rather, you'd be more likely to do something like: for each pass: apply pass state for each object (grouped ...


4

To me it looks very much like your issue here is that you are mixing world and viewspace or something similiar. Now your GBuffer normals look like they might be in viewspace in that picture, but the code in your geometry pass definitely doesn't transform them from world to view space. If they are in world space then your NBT matrix is basically oriented ...


4

Note that some of the functions used here have been deprecated in Unity 5.5, but I have tested it to work. In Unity 5.5, many changes have been made. The most significant, being that the dot product is now calculated on the fly, and the light.ndotl field is no longer taken into consideration anywhere in code. I spoke to the original author of the blog ...


3

Have solved the issue, partially. This is the new culling code, which works for everything but the far and near plane. Performance is still pretty bad so if anyone can see what might cause that it would be appreciated. ivec2 pixel = ivec2(gl_GlobalInvocationID.xy); vec4 normalColor = imageLoad(normalDepth, pixel); float d = normalColor.w; ...


3

I'd say 2-3 passes is pretty normal. I suspect 4 is not uncommon. For vertex-lit games, 1-2. Usually just the albedo pass then some post effect or another. For pixel lit, 3+ is going to be the norm. 1st pass is g-buffer, z-buffer, surface normals and diffuse/albedo info as you describe it. Building blocks. 2nd pass we try to fit all other ops, but in ...


3

Usually each post processing effect has it own pass unless the effects are "organically" coupled and/or are simple. For example, to simulate an old film camera, you could put the grayscale, sepia, noise, scratch and vignetting effects all inside the same shader, since each effect is a simple part of the goal effect. On the other hand, some other effects have ...


3

Branching incurs overhead on GPU if the threads take different branches, which leads to serialisation. Thus, if all of your pixels take the same conditional branch, the overhead is negligible. In addition, if the condition is complex, it can increase the number of registers used per thread which can impact performance depending on how much is this increment. ...


3

Your positions are converted from WCS ( World Coordinate System) to NDC (Normalized Device Coordinates) in order to by saved inside the depth buffer. This is achieved by multiplying your coordinates with the view matrix to convert them to ECS (Eye Coordinate System) and then with the projection matrix. And at last they are divided by W component in order to ...


3

It's going to depend on the hardware and app, of course, but on current generation GPUs (NVIDIA Kepler, AMD GCN), I believe there is no penalty for using different render target formats at once. On older GPUs you simply couldn't do it; all render targets had to be the same format. Writing out to multiple render targets is usually memory-bound, so you can ...


3

I've already found some time and solution to my problems and I want share it with you. Maybe it will help someone: float distFromCentroid=ACamera.far(); camera.setLookAt(frustum_centroid+dir->direction*distFromCentroid,frustum_centroid); for(int i=0;i<8;i++){ point[i]=ACamera._point[i]*camera.matrix; } min=point[0]; max=point[0]; for(int i=0;i&...


3

You can use any shader to fill the g-buffer, it doesn't have to be generic. That's not the point of deferred, the point of deferred is when you come to do the lighting pass you combine the various render targets in the g-buffer with your lights; your lighting pass is confined to pixels on-screen (rather than shading pixels which may be occluded). But it's ...


3

One idea I came up with but which isn't very satisfying is to have two image textures and automatically use them alternating. Also keeping track of which is the newer one to finally display it on the screen. This is actually the way it's commonly done, and the technique even has a name: "ping-ponging". There's some discussion on the technique here (look ...


3

I ended up using the stencil buffers. Basically, this are the steps my application goes through every frame. Draw geometry except skydome and set stencil buffer to 1. Disable depth mask to not distort depth buffer. Draw skydome where stencil is still 0. Use stencil test to only compute lighting and other shader computations where stencil is 1 and therefore ...


3

Copy the depth information to the la-buffer and then render normally to the la-buffer using forward shading. All you need to do to copy over the depth information is add this to the light accumulation fragment shader: gl_FragDepth = texture(depthBuffer, texCoord).r;


3

So after much working I found out this is a bug with Unity. It happens in their Deferred Lighting -> RenderingToTexture pipeline. What's happening is they are cutting it to only show the first pass. I'll be filing a bug report to Unity, and hopefully this gets fixed soon. As a simple work around I'm just setting the portal camera to forward rendering path, ...


3

Well, it's not new and it's a proper method. I think a common term for this "color picking", at least that's what i call it. As long as you are fine with picking only one object at a time, and that object being the one on top, there are no critical shortcomings. Even multi-select clicks are possible to a certain extend by mixing/adding colors/ids. However,...


Only top voted, non community-wiki answers of a minimum length are eligible