bc2_encode_kernel.hlsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //=====================================================================
  2. // Copyright (c) 2020 Advanced Micro Devices, Inc. All rights reserved.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files(the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions :
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. // File: BC1Encode.hlsl
  23. //--------------------------------------------------------------------------------------
  24. // Copyright (c) Microsoft Corporation. All rights reserved.
  25. // Licensed under the MIT License.
  26. //--------------------------------------------------------------------------------------
  27. #ifndef ASPM_HLSL
  28. #define ASPM_HLSL
  29. #endif
  30. cbuffer cbCS : register( b0 )
  31. {
  32. uint g_tex_width;
  33. uint g_num_block_x;
  34. uint g_format;
  35. uint g_mode_id;
  36. uint g_start_block_id;
  37. uint g_num_total_blocks;
  38. float g_alpha_weight;
  39. float g_quality;
  40. };
  41. #include "bcn_common_kernel.h"
  42. // Source Data
  43. Texture2D g_Input : register( t0 );
  44. StructuredBuffer<uint4> g_InBuff : register( t1 );
  45. // Compressed Output Data
  46. RWStructuredBuffer<uint4> g_OutBuff : register( u0 );
  47. // Processing multiple blocks at a time
  48. #define MAX_USED_THREAD 16 // pixels in a BC (block compressed) block
  49. #define BLOCK_IN_GROUP 4 // the number of BC blocks a thread group processes = 64 / 16 = 4
  50. #define THREAD_GROUP_SIZE 64 // 4 blocks where a block is (BLOCK_SIZE_X x BLOCK_SIZE_Y)
  51. #define BLOCK_SIZE_Y 4
  52. #define BLOCK_SIZE_X 4
  53. groupshared float4 shared_temp[THREAD_GROUP_SIZE];
  54. [numthreads( THREAD_GROUP_SIZE, 1, 1 )]
  55. void EncodeBlocks(uint GI : SV_GroupIndex, uint3 groupID : SV_GroupID)
  56. {
  57. // we process 4 BC blocks per thread group
  58. uint blockInGroup = GI / MAX_USED_THREAD; // what BC block this thread is on within this thread group
  59. uint blockID = g_start_block_id + groupID.x * BLOCK_IN_GROUP + blockInGroup; // what global BC block this thread is on
  60. uint pixelBase = blockInGroup * MAX_USED_THREAD; // the first id of the pixel in this BC block in this thread group
  61. uint pixelInBlock = GI - pixelBase; // id of the pixel in this BC block
  62. uint block_y = blockID / g_num_block_x;
  63. uint block_x = blockID - block_y * g_num_block_x;
  64. uint base_x = block_x * BLOCK_SIZE_X;
  65. uint base_y = block_y * BLOCK_SIZE_Y;
  66. // Load up the pixels
  67. if (pixelInBlock < 16)
  68. {
  69. // load pixels (0..1)
  70. shared_temp[GI] = float4(g_Input.Load( uint3( base_x + pixelInBlock % 4, base_y + pixelInBlock / 4, 0 ) ));
  71. }
  72. GroupMemoryBarrierWithGroupSync();
  73. // Process and save s
  74. if (pixelInBlock == 0)
  75. {
  76. float3 blockRGB[16];
  77. float blockA[16];
  78. for (int i = 0; i < 16; i++ )
  79. {
  80. blockRGB[i].x = shared_temp[pixelBase + i].x;
  81. blockRGB[i].y = shared_temp[pixelBase + i].y;
  82. blockRGB[i].z = shared_temp[pixelBase + i].z;
  83. blockA[i] = shared_temp[pixelBase + i].w;
  84. }
  85. g_OutBuff[blockID] = CompressBlockBC2_UNORM(blockRGB,blockA,g_quality,false);
  86. }
  87. }