Hello everyone! I’m new to the forums, so please redirect me if this is not the place to ask.
I’m playing around with CommandBuffers and deferred rendering, and to get a feel for it I wanted to try and just do a basic test. Basically, render a simple mesh with CommandBuffers in deferred rendering, and have it look the same as it would if you rendered it with a MeshRenderer. I’ve hit a road-block. This is my tiny test scene:
The mesh on the right is rendered with a MeshRenderer, the mesh on the left is rendered with CommandBuffers. The material is just a simple material with the Standard shader and a slightly yellow albedo (no textures).
So, I’ve been digging into the Frame Debugger, and I think I know what’s going on: everything goes just fine until I get to the “RenderDeferred.Lighting” stage, at which point it seems to totally ignore the mesh drawn with CommandBuffers. These are the passes in the Frame Debugger for the MeshRenderer and CommandBuffer meshes (I turned off both shadows and deferred reflections to simplify the rendering):
As far as I can tell, the only difference in rendering these is that the “normally rendered” mesh enables writing to the stencil buffer. It makes sense that this is the problem: the reason my mesh is ignored during the Lighting pass because it’s outside of the stencil. Testing this problem, I figured I would “trick” the stencil buffer by putting the correctly rendered mesh behind my mesh, so that at least some of it would be inside the stencil:
It’s a bit tricky to see, but it works: the parts of my CommandBuffer mesh that is directly in front of the other bird is rendered correctly, the rest is not. It seems clear that the stencil buffer is at fault.
Clearly, CommandBuffer.DrawMesh does not set whatever stencil settings is needed for deferred rendering. I’ve been digging through the docs and googling around, but I haven’t found any answers to this. Is this a bug, or is there some way I can tell the command buffer to write to the stencil buffer as part of rendering the G-buffer?
Here’s my code for setting up the CommandBuffer, btw:
buf = new CommandBuffer();
buf.name = "Draw meshes";
// This is the only material keyword the MeshRenderer
// sets that isn't set when using the command buffer,
// so set it here.
Material.EnableKeyword("SHADOWS_SHADOWMASK");
// Simplest possible DrawMesh. The last argumenet (the
// 3) is the Deferred pass in the shader. Mesh and material
// are provided as fields set in the editor.
buf.DrawMesh(mesh, Matrix4x4.identity, mat, 0, 3);
cam = GetComponent<Camera>();
cam.AddCommandBuffer(CameraEvent.AfterGBuffer, buf);
I’ve tried a bunch of variants (different camera events, etc.) but I haven’t found anything that works. Help would be much appreciated!