microsoft / DirectX-Graphics-Samples

This repo contains the DirectX Graphics samples that demonstrate how to build graphics intensive applications on Windows.
MIT License
6k stars 2.02k forks source link

[Question] Depth of field in MiniEngine #734

Closed zznewclear13 closed 3 years ago

zznewclear13 commented 3 years ago

In DoFPass1CS.hlsl, line 40 to 53:

https://github.com/microsoft/DirectX-Graphics-Samples/blob/145b4dd499e5593f733682067e19861cfc5dd395/MiniEngine/Core/Shaders/DoFPass1CS.hlsl#L40-L53

we first write to group shared memory, and do max/min comparison later. I just wonder does this really give us the correct max/min value?

In my opinion, we should have something like this:

    // Write and sync
    gs_ClosestDepthSearch[GI] = TileMinDepth;
    gs_FarthestDepthSearch[GI] = TileMaxDepth;
    gs_MaximumCoC[GI] = TileMaxCoC;
    GroupMemoryBarrierWithGroupSync();

    for (uint i = 32; i > 0; i >>= 1)
    {
        // Read and sync
        if (GI < i)
        {
            gs_ClosestDepthSearch[i] = min(gs_ClosestDepthSearch[i], gs_ClosestDepthSearch[GI + i]);
            gs_FarthestDepthSearch[i] = max(gs_FarthestDepthSearch[i], gs_FarthestDepthSearch[GI + i]);
            gs_MaximumCoC[i] = max(gs_MaximumCoC[i], gs_MaximumCoC[GI + i]);
        }
        GroupMemoryBarrierWithGroupSync();
    }

    if (GI == 0)
        TileClass[Gid.xy] = float3(gs_MaximumCoC[0], gs_FarthestDepthSearch[0], gs_FarthestDepthSearch[0]);
stanard commented 3 years ago

Sure. That's nice too. They both work, but I think I like yours better. Want to create a pull request?

zznewclear13 commented 3 years ago

I'd love to! But there are some minor differences between those two codes, and the result is no better than the original one. So I think it may need more investigation.

By the way, I am trying to implement a similar depth of field effect in my project. I found Jorge Jimenez's presentation, but it lacks implementation details. Do you know where can I find some clearer references? Thanks!