I’ve been diving into shadow mapping and PCF (Percentage Closer Filtering) these past few days, and I stumbled upon something a bit perplexing that I hope someone here could help clarify. I’m working with two different sampling methods for this 3×3 PCF blur: `Texture.SampleCmp` (which I’ve pulled from a CascadedShadow demo in DX11) and `Texture.Sample` (using DX11 SM5).
Let me break down what I’m doing. I’ve got a projected UV and Z from the shadow light matrix that I’m using to sample a shadow map. The Shadow value I’m calculating is the result of comparing the sample results against a bias (which I set to `txProj.z – 0.0005f`).
So, here’s the tricky part: with `Sample`, I need to check if the value is less than the bias, and based on that, I determine whether to add `0` or `1` to the Shadow total. On the flip side, `SampleCmp` gives me a direct result where it returns `1` when the comparison is successful and `0` otherwise. Both methods should theoretically yield the same shadow result, right? However, the visual outcome is strikingly different.
I’ve noticed that when I visualize the results, the upper image (from `Sample`) looks quite different from the lower image (from `SampleCmp`). The Shadow value seems to be the same in terms of numerical calculations, but the actual visual representation tells a different story.
One possible explanation I considered is related to the texture formats: I’m using a regular depth buffer (D16_UNORM) with `SampleCmp`, while `Sample` is operating on a color texture (R16_UNORM) filled with transformed position inputs. I’m left wondering if this difference in texture types is influencing the end result.
Here’s where I need your insights: what do you all think might be causing these visual discrepancies between the two sampling methods despite them theoretically producing similar shadow values? Any thoughts on how the depth and color textures might affect the outcomes? Would love to hear your thoughts!
It sounds like you’re running into quite a classic issue when working with shadow mapping and the differences in sampling methods. You’re right that both `Texture.SampleCmp` and `Texture.Sample` should provide similar shadowing results in theory, but the implementation and context can lead to noticeable differences in the final visuals.
One likely reason for the disparity you see could indeed be tied to the types of textures you’re using. The `D16_UNORM` depth buffer is designed specifically for depth values and is structured to handle depth comparisons effectively, whereas your `R16_UNORM` color texture is likely not optimized in the same way for depth comparisons. Because of this, the filtering behavior and precision may vary significantly between the two.
Another factor to consider is how the shadow bias is applied. With `Sample`, you’re manually comparing each shadow sample against the bias value which might lead to subtle differences in the final shadow calculation—especially if there’s any noise or slight variations in the sampled values. Meanwhile, `SampleCmp` streamlines this process by directly returning a comparison result.
Also, remember that the filtering done by `Sample` can be influenced by the format and characteristics of the texture, including how mipmapping and interpolation are handled. You could be seeing some artifacts or blurring effects that look different based on these differences.
As for fixing the issue, you might want to experiment with consistently using the same texture formats, and modifying the way you’re implementing the shadow bias might help, too. It might also be helpful to visualize or log the values being sampled at different stages to spot where the discrepancy occurs. Don’t hesitate to provide more details for further troubleshooting!
The differences you’re observing likely stem from subtle yet significant behavioral distinctions between
Texture.SampleCmp
andTexture.Sample
combined with manual depth comparison. When usingSampleCmp
, GPU hardware inherently handles depth comparisons optimized for shadow mapping. The depth buffer (D16_UNORM) stores depth values tailored explicitly for fast hardware-assisted depth comparisons. This provides consistent precision and accurate interpolation when performing shadow lookups directly, often resulting in smoother and more visually coherent shadows with PCF.On the other hand, manually replicating this comparison through
Sample
on an R16_UNORM color texture introduces subtle conversions and possibly lower fidelity interpolation. Color textures aren’t optimized for depth comparisons, and sampling depth values manually from color render targets can yield slight discrepancies due to linearization, rounding errors, or filtering anomalies. Moreover, manually handling bias and precision differences in shader code directly may amplify such issues, resulting in visual artifacts or discrepancies when compared with hardware-computed shadows throughSampleCmp
. To address this, consider consistently using hardware depth buffers andSampleCmp
for accurate and high-quality shadow resolutions, or alternatively ensuring the sampled values in color textures closely match the precision and biases of their depth counterparts.