/***************************************************************************
 *                         Zanoza Modeler v3.3.x                           *
 *     This is unpublished proprietary source code of Zanoza Software.     *
 *      The copyright notice does not evidence any actual or intended      *
 *                    publication of such source code.                     *
 *                                                                         *
 *                 Copyright (c) 2002-2024 Zanoza Software.                *
 *                                                                         *
 *                        All rights reserved.                             *
 ***************************************************************************
 *--------------------------------------------------------------------------
 * @author:   Oleg M.
 *--------------------------------------------------------------------------
 * @purpose:  ZModeler 3.3.x Compute shader for texture downsampling.
 *--------------------------------------------------------------------------
 * @history: 12.05.2024. Created
 * @Description:
   Computer Shader for texture 2x downsampling.
   Insipred by downsample shader implementation by @aaronscherzinger 
      [www.github.com/aaronscherzinger/directx11_bloom/]
<[[
UserName = "2x Downsample"
Version  = 1.0.0
Options  = COMPUTE
Entry = Downsample2x
Engine = 11
]]>
*/

Texture2D<float4> 	inputTexture  : register(t0);
RWTexture2D<float4> 	outputTexture : register(u0);

#ifdef COMPUTE_SHADER

[numthreads(8, 8, 1)]
void Downsample2x(uint3 groupID : SV_GroupID, 
		uint3 groupThreadID : SV_GroupThreadID, 
		uint groupIndex : SV_GroupIndex, 
		uint3 dispatchID : SV_DispatchThreadID)
{
    // output pixel in half resolution
    uint2 pixel = uint2(dispatchID.x, dispatchID.y);

    // bilinear interpolation for downsampling
    uint2 inPixel = pixel * 2;
    // source texture pixels for current downsampled pixel are:
    // [A] [B]
    // [C] [D]
    // Interpolate A and B
    float4 vColorAB = lerp(inputTexture[inPixel], inputTexture[inPixel + uint2(1, 0)], 0.5);
    // Interpolate C and D
    float4 vColorCD = lerp(inputTexture[inPixel + uint2(0, 1)], inputTexture[inPixel + uint2(1, 1)], 0.5);
    // Output interpolation of two above:
    outputTexture[pixel] = lerp(vColorAB, vColorCD, 0.5);
}
#endif //COMPUTE_SHADER

