I am trying to implement reflection on a mirror in DirectX12. Assume that all the objects except the mirror in the scene are opaque. The steps that I am taking to implement this are as follows:
-
Configure a PSO for all the opaque items in the scene. Then draw them.
-
Configure a PSO for the rendering of the the mirror to the stencil buffer only. Then draw the mirror to the stencil buffer.
-
When an object is reflected across a plane the winding order of the vertices change since the normal vectors to the surface change. So configure a PSO that sets RasterizeState.FrontCounterClockwise to true. Then draw the object that is to be reflected on the mirror.
-
Finally, we should draw the mirror. For this we need a PSO that has its blend state properly configured. Then draw the mirror.
Unfortunately, after running the program everything is rendered fine except the reflection of the object in the scene on the supposed mirror. Nothing is reflected on the mirror when standing in front of it. However, it is worth noting that the object is reflected on the back side of the mirror. The relevant PSO configuration of step 2 is the following:
D3D12_GRAPHICS_PIPELINE_STATE_DESC stencilMirrorPsoDesc = opaquePsoDesc;
stencilMirrorPsoDesc.BlendState.RenderTarget[0].RenderTargetWriteMask = 0;
stencilMirrorPsoDesc.DepthStencilState.DepthEnable = true;
stencilMirrorPsoDesc.DepthStencilState.FrontFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
stencilMirrorPsoDesc.DepthStencilState.FrontFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
stencilMirrorPsoDesc.DepthStencilState.FrontFace.StencilPassOp = D3D12_STENCIL_OP_REPLACE;
stencilMirrorPsoDesc.DepthStencilState.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
stencilMirrorPsoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
stencilMirrorPsoDesc.DepthStencilState.StencilEnable = true;
stencilMirrorPsoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
The relevant PSO configuration of steps 3 is also as follows:
D3D12_GRAPHICS_PIPELINE_STATE_DESC reflectionPsoDesc = opaquePsoDesc;
reflectionPsoDesc.DepthStencilState.StencilEnable = true;
reflectionPsoDesc.DepthStencilState.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_EQUAL;
reflectionPsoDesc.DepthStencilState.FrontFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
reflectionPsoDesc.DepthStencilState.FrontFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
reflectionPsoDesc.DepthStencilState.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
reflectionPsoDesc.DepthStencilState.BackFace.StencilDepthFailOp = D3D12_STENCIL_OP_KEEP;
reflectionPsoDesc.DepthStencilState.BackFace.StencilFailOp = D3D12_STENCIL_OP_KEEP;
reflectionPsoDesc.DepthStencilState.BackFace.StencilPassOp = D3D12_STENCIL_OP_KEEP;
reflectionPsoDesc.DepthStencilState.BackFace.StencilFunc = D3D12_COMPARISON_FUNC_EQUAL;
reflectionPsoDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
reflectionPsoDesc.DepthStencilState.DepthEnable = true;
reflectionPsoDesc.RasterizerState.FrontCounterClockwise = true;
reflectionPsoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
I tried to debug it with RenderDoc and it seems that everything is rendered fine except when it is time to render the reflection of the object in step 3. Nothing is drawn and it goes straight to drawing the transparent mirror. I made sure that the root signature along with root parameters of the object are correctly configured. So I have narrowed it down to how the PSOs are setup. I would appreciate it if someone could point out the possible mistake in my code. Thanks.
For reference this is the image of the backside of the mirror: