//
// This is a modified version of the SSAO renderer from Microsoft's MiniEngine
// library. The copyright notice from the original version is included below.
//
// The original source code of MiniEngine is available on GitHub.
// https://github.com/Microsoft/DirectX-Graphics-Samples
//

//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
// Developed by Minigraph
//
// Author:  James Stanard
//

#pragma warning(disable : 3568)
#pragma exclude_renderers gles gles3 d3d11_9x

#pragma kernel MultiScaleVODownsample2              main=MultiScaleVODownsample2
#pragma kernel MultiScaleVODownsample2_MSAA         main=MultiScaleVODownsample2_MSAA       MSAA

#include "../StdLib.hlsl"

#ifdef MSAA
Texture2D<float2> DS4x;
RWTexture2D<float2> DS8x;
RWTexture2DArray<float2> DS8xAtlas;
RWTexture2D<float2> DS16x;
RWTexture2DArray<float2> DS16xAtlas;
#else
Texture2D<float> DS4x;
RWTexture2D<float> DS8x;
RWTexture2DArray<float> DS8xAtlas;
RWTexture2D<float> DS16x;
RWTexture2DArray<float> DS16xAtlas;
#endif


#ifdef DISABLE_COMPUTE_SHADERS

TRIVIAL_COMPUTE_KERNEL(main)

#else

[numthreads(8, 8, 1)]
void main(uint3 Gid : SV_GroupID, uint GI : SV_GroupIndex, uint3 GTid : SV_GroupThreadID, uint3 DTid : SV_DispatchThreadID)
{
    #ifdef MSAA
    float2 m1 = DS4x[DTid.xy << 1];
    #else
    float m1 = DS4x[DTid.xy << 1];
    #endif

    uint2 st = DTid.xy;
    uint2 stAtlas = st >> 2;
    uint stSlice = ((st.x & 3) | (st.y << 2)) & 15;
    DS8x[st] = m1;
    DS8xAtlas[uint3(stAtlas, stSlice)] = m1;

    if ((GI & 011) == 0)
    {
        uint2 st = DTid.xy >> 1;
        uint2 stAtlas = st >> 2;
        uint stSlice = ((st.x & 3) | (st.y << 2)) & 15;
        DS16x[st] = m1;
        DS16xAtlas[uint3(stAtlas, stSlice)] = m1;
    }
}

#endif // DISABLE_COMPUTE_SHADERS