ffx_spd.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. //_____________________________________________________________/\_______________________________________________________________
  2. //==============================================================================================================================
  3. //
  4. // [FFX SPD] Single Pass Downsampler 2.0
  5. //
  6. //==============================================================================================================================
  7. // LICENSE
  8. // =======
  9. // Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved.
  10. // -------
  11. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  12. // documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  13. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to the following conditions:
  15. // -------
  16. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  17. // Software.
  18. // -------
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  21. // OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. //------------------------------------------------------------------------------------------------------------------------------
  25. // CHANGELIST v2.0
  26. // ===============
  27. // - Added support for cube and array textures. SpdDownsample and SpdDownsampleH shader functions now take index of
  28. // texture slice
  29. // as an additional parameter. For regular texture use 0.
  30. // - Added support for updating only sub-rectangle of the texture. Additional, optional parameter workGroupOffset added
  31. // to shader
  32. // functions SpdDownsample and SpdDownsampleH.
  33. // - Added C function SpdSetup that helps to setup constants to be passed as a constant buffer.
  34. // - The global atomic counter is automatically reset to 0 by the shader at the end, so you do not need to clear it
  35. // before every
  36. // use, just once after creation
  37. //
  38. //------------------------------------------------------------------------------------------------------------------------------
  39. // INTEGRATION SUMMARY FOR CPU
  40. // ===========================
  41. // // you need to provide as constants:
  42. // // number of mip levels to be computed (maximum is 12)
  43. // // number of total thread groups: ((widthInPixels+63)>>6) * ((heightInPixels+63)>>6)
  44. // // workGroupOffset -> by default 0, if you only downsample a rectancle within the source texture use SpdSetup
  45. // function to calculate correct offset
  46. // ...
  47. // // Dispatch the shader such that each thread group works on a 64x64 sub-tile of the source image
  48. // // for Cube Textures or Texture2DArray, use the z dimension
  49. // vkCmdDispatch(cmdBuf,(widthInPixels+63)>>6,(heightInPixels+63)>>6, slices);
  50. // // you can also use the SpdSetup function:
  51. // //on top of your cpp file:
  52. // #define A_CPU
  53. // #include "ffx_a.h"
  54. // #include "ffx_spd.h"
  55. // // before your dispatch call, use SpdSetup function to get your constants
  56. // varAU2(dispatchThreadGroupCountXY); // output variable
  57. // varAU2(workGroupOffset); // output variable, this constants are required if Left and Top are not 0,0
  58. // varAU2(numWorkGroupsAndMips); // output variable
  59. // // input information about your source texture:
  60. // // left and top of the rectancle within your texture you want to downsample
  61. // // width and height of the rectancle you want to downsample
  62. // // if complete source texture should get downsampled: left = 0, top = 0, width = sourceTexture.width, height =
  63. // sourceTexture.height varAU4(rectInfo) = initAU4(0, 0, m_Texture.GetWidth(), m_Texture.GetHeight()); // left, top,
  64. // width, height SpdSetup(dispatchThreadGroupCountXY, workGroupOffset, numWorkGroupsAndMips, rectInfo);
  65. // ...
  66. // // constants:
  67. // data.numWorkGroupsPerSlice = numWorkGroupsAndMips[0];
  68. // data.mips = numWorkGroupsAndMips[1];
  69. // data.workGroupOffset[0] = workGroupOffset[0];
  70. // data.workGroupOffset[1] = workGroupOffset[1];
  71. // ...
  72. // uint32_t dispatchX = dispatchThreadGroupCountXY[0];
  73. // uint32_t dispatchY = dispatchThreadGroupCountXY[1];
  74. // uint32_t dispatchZ = m_CubeTexture.GetArraySize(); // slices - for 2D Texture this is 1, for cube texture 6
  75. // vkCmdDispatch(cmd_buf, dispatchX, dispatchY, dispatchZ);
  76. //------------------------------------------------------------------------------------------------------------------------------
  77. // INTEGRATION SUMMARY FOR GPU
  78. // ===========================
  79. // [SAMPLER] - if you want to use a sampler with linear filtering for loading the source image
  80. // follow additionally the instructions marked with [SAMPLER]
  81. // add following define:
  82. // #define SPD_LINEAR_SAMPLER
  83. // this is recommended, as using one sample() with linear filter to reduce 2x2 is faster
  84. // than 4x load() plus manual averaging
  85. // // Setup layout. Example below for VK_FORMAT_R16G16B16A16_SFLOAT.
  86. // // Note: If you use SRGB format for UAV load() and store() (if it's supported), you need to convert to and from
  87. // linear space
  88. // // when using UAV load() and store()
  89. // // approximate conversion to linear (load function): x*x
  90. // // approximate conversion from linear (store function): sqrt()
  91. // // or use more accurate functions from ffx_a.h: AFromSrgbF1(value) and AToSrgbF1(value)
  92. // // Recommendation: use UNORM format instead of SRGB for UAV access, and SRGB for SRV access
  93. // // look in the sample app to see how it's done
  94. // // source image
  95. // // if cube texture use image2DArray / Texture2DArray and adapt your load/store/sample calls
  96. // GLSL: layout(set=0,binding=0,rgba16f)uniform image2D imgSrc;
  97. // [SAMPLER]: layout(set=0,binding=0)uniform texture2D imgSrc;
  98. // HLSL: [[vk::binding(0)]] Texture2D<float4> imgSrc :register(u0);
  99. // // destination -> 12 is the maximum number of mips supported by SPD
  100. // GLSL: layout(set=0,binding=1,rgba16f) uniform coherent image2D imgDst[12];
  101. // HLSL: [[vk::binding(1)]] globallycoherent RWTexture2D<float4> imgDst[12] :register(u1);
  102. // // global atomic counter - MUST be initialized to 0
  103. // // SPD resets the counter back after each run by calling SpdResetAtomicCounter(slice)
  104. // // if you have more than 1 slice (== if you downsample a cube texture or a texture2Darray)
  105. // // you have an array of counters: counter[6] -> if you have 6 slices for example
  106. // // GLSL:
  107. // layout(std430, set=0, binding=2) coherent buffer SpdGlobalAtomicBuffer
  108. // {
  109. // uint counter;
  110. // } spdGlobalAtomic;
  111. // // HLSL:
  112. // struct SpdGlobalAtomicBuffer
  113. // {
  114. // uint counter;
  115. // };
  116. // [[vk::binding(2)]] globallycoherent RWStructuredBuffer<SpdGlobalAtomicBuffer> spdGlobalAtomic;
  117. // // [SAMPLER] add sampler
  118. // GLSL: layout(set=0, binding=3) uniform sampler srcSampler;
  119. // HLSL: [[vk::binding(3)]] SamplerState srcSampler :register(s0);
  120. // // constants - either push constant or constant buffer
  121. // // or calculate within shader
  122. // // [SAMPLER] when using sampler add inverse source image size
  123. // // GLSL:
  124. // layout(push_constant) uniform SpdConstants {
  125. // uint mips; // needed to opt out earlier if mips are < 12
  126. // uint numWorkGroups; // number of total thread groups, so numWorkGroupsX * numWorkGroupsY * 1
  127. // // it is important to NOT take the number of slices (z dimension) into account here
  128. // // as each slice has its own counter!
  129. // vec2 workGroupOffset; // optional - use SpdSetup() function to calculate correct workgroup offset
  130. // } spdConstants;
  131. // // HLSL:
  132. // [[vk::push_constant]]
  133. // cbuffer spdConstants {
  134. // uint mips;
  135. // uint numWorkGroups;
  136. // float2 workGroupOffset; // optional
  137. // };
  138. // ...
  139. // // Setup pre-portability-header defines (sets up GLSL/HLSL path, etc)
  140. // #define A_GPU 1
  141. // #define A_GLSL 1 // or // #define A_HLSL 1
  142. // // if you want to use PACKED version
  143. // // recommended if bpc <= 16bit
  144. // #define A_HALF
  145. // ...
  146. // // Include the portability header (or copy it in without an include).
  147. // #include "ffx_a.h"
  148. // ...
  149. // // Define LDS variables
  150. // shared AF4 spdIntermediate[16][16]; // HLSL: groupshared
  151. // shared AU1 spdCounter; // HLSL: groupshared
  152. // // PACKED version
  153. // shared AH4 spdIntermediate[16][16]; // HLSL: groupshared
  154. // // Note: You can also use
  155. // shared AF1 spdIntermediateR[16][16];
  156. // shared AF1 spdIntermediateG[16][16];
  157. // shared AF1 spdIntermediateB[16][16];
  158. // shared AF1 spdIntermediateA[16][16];
  159. // // or for Packed version:
  160. // shared AH2 spdIntermediateRG[16][16];
  161. // shared AH2 spdIntermediateBA[16][16];
  162. // // This is potentially faster
  163. // // Adapt your load and store functions accordingly
  164. // // if subgroup operations are not supported / can't use SM6.0
  165. // #define SPD_NO_WAVE_OPERATIONS
  166. // // Define the fetch function(s) and the reduction function
  167. // // if non-power-of-2 textures, add border controls to the load and store functions
  168. // // to make sure the borders of the mip level look as you want it
  169. // // if you don't add border controls you'll read zeros past the border
  170. // // if you load with a sampler, this is obv. handled by your sampler :)
  171. // // this is also the place where you need to do color space transformation if needed
  172. // // E.g. if your texture format is SRGB/UNORM and you use the UAV load and store functions
  173. // // no automatic to/from linear conversions are happening
  174. // // there is to/from linear conversions when using a sampler and render target approach
  175. // // conversion to linear (load function): x*x
  176. // // conversion from linear (store function): sqrt()
  177. // AU1 slice parameter is for Cube textures and texture2DArray
  178. // if downsampling Texture2D you can ignore this parameter, otherwise use it to access correct slice
  179. // // Load from source image
  180. // GLSL: AF4 SpdLoadSourceImage(ASU2 p, AU1 slice){return imageLoad(imgSrc, p);}
  181. // HLSL: AF4 SpdLoadSourceImage(ASU2 tex, AU1 slice){return imgSrc[tex];}
  182. // [SAMPLER] don't forget to add the define #SPD_LINEAR_SAMPLER :)
  183. // GLSL:
  184. // AF4 SpdLoadSourceImage(ASU2 p, AU1 slice){
  185. // AF2 textureCoord = p * invInputSize + invInputSize;
  186. // return texture(sampler2D(imgSrc, srcSampler), textureCoord);
  187. // }
  188. // HLSL:
  189. // AF4 SpdLoadSourceImage(ASU2 p, AU1 slice){
  190. // AF2 textureCoord = p * invInputSize + invInputSize;
  191. // return imgSrc.SampleLevel(srcSampler, textureCoord, 0);
  192. // }
  193. // // SpdLoad() takes a 32-bit signed integer 2D coordinate and loads color.
  194. // // Loads the 5th mip level, each value is computed by a different thread group
  195. // // last thread group will access all its elements and compute the subsequent mips
  196. // // reminder: if non-power-of-2 textures, add border controls if you do not want to read zeros past the border
  197. // GLSL: AF4 SpdLoad(ASU2 p, AU1 slice){return imageLoad(imgDst[5],p);}
  198. // HLSL: AF4 SpdLoad(ASU2 tex, AU1 slice){return imgDst[5][tex];}
  199. // Define the store function
  200. // GLSL: void SpdStore(ASU2 p, AF4 value, AU1 mip, AU1 slice){imageStore(imgDst[mip], p, value);}
  201. // HLSL: void SpdStore(ASU2 pix, AF4 value, AU1 mip, AU1 slice){imgDst[mip][pix] = value;}
  202. // // Define the atomic counter increase function
  203. // // each slice only reads and stores to its specific slice counter
  204. // // so, if you have several slices it's
  205. // // InterlockedAdd(spdGlobalAtomic[0].counter[slice], 1, spdCounter);
  206. // // GLSL:
  207. // void SpdIncreaseAtomicCounter(AU1 slice){spdCounter = atomicAdd(spdGlobalAtomic.counter, 1);}
  208. // AU1 SpdGetAtomicCounter() {return spdCounter;}
  209. // void SpdResetAtomicCounter(AU1 slice){spdGlobalAtomic.counter[slice] = 0;}
  210. // // HLSL:
  211. // void SpdIncreaseAtomicCounter(AU1 slice){InterlockedAdd(spdGlobalAtomic[0].counter, 1, spdCounter);}
  212. // AU1 SpdGetAtomicCounter(){return spdCounter;}
  213. // void SpdResetAtomicCounter(AU1 slice){spdGlobalAtomic[0].counter[slice] = 0;}
  214. // // Define the LDS load and store functions
  215. // // GLSL:
  216. // AF4 SpdLoadIntermediate(AU1 x, AU1 y){return spdIntermediate[x][y];}
  217. // void SpdStoreIntermediate(AU1 x, AU1 y, AF4 value){spdIntermediate[x][y] = value;}
  218. // // HLSL:
  219. // AF4 SpdLoadIntermediate(AU1 x, AU1 y){return spdIntermediate[x][y];}
  220. // void SpdStoreIntermediate(AU1 x, AU1 y, AF4 value){spdIntermediate[x][y] = value;}
  221. // // Define your reduction function: takes as input the four 2x2 values and returns 1 output value
  222. // Example below: computes the average value
  223. // AF4 SpdReduce4(AF4 v0, AF4 v1, AF4 v2, AF4 v3){return (v0+v1+v2+v3)*0.25;}
  224. // // PACKED VERSION
  225. // Load from source image
  226. // GLSL: AH4 SpdLoadSourceImageH(ASU2 p, AU1 slice){return AH4(imageLoad(imgSrc, p));}
  227. // HLSL: AH4 SpdLoadSourceImageH(ASU2 tex, AU1 slice){return AH4(imgSrc[tex]);}
  228. // [SAMPLER]
  229. // GLSL:
  230. // AH4 SpdLoadSourceImageH(ASU2 p, AU1 slice){
  231. // AF2 textureCoord = p * invInputSize + invInputSize;
  232. // return AH4(texture(sampler2D(imgSrc, srcSampler), textureCoord));
  233. // }
  234. // HLSL:
  235. // AH4 SpdLoadSourceImageH(ASU2 p, AU1 slice){
  236. // AF2 textureCoord = p * invInputSize + invInputSize;
  237. // return AH4(imgSrc.SampleLevel(srcSampler, textureCoord, 0));
  238. // }
  239. // // SpdLoadH() takes a 32-bit signed integer 2D coordinate and loads color.
  240. // // Loads the 5th mip level, each value is computed by a different thread group
  241. // // last thread group will access all its elements and compute the subsequent mips
  242. // GLSL: AH4 SpdLoadH(ASU2 p, AU1 slice){return AH4(imageLoad(imgDst[5],p));}
  243. // HLSL: AH4 SpdLoadH(ASU2 tex, AU1 slice){return AH4(imgDst[5][tex]);}
  244. // Define the store function
  245. // GLSL: void SpdStoreH(ASU2 p, AH4 value, AU1 mip, AU1 slice){imageStore(imgDst[mip], p, AF4(value));}
  246. // HLSL: void SpdStoreH(ASU2 pix, AH4 value, AU1 index, AU1 slice){imgDst[index][pix] = AF4(value);}
  247. // // Define the atomic counter increase function
  248. // // GLSL:
  249. // void SpdIncreaseAtomicCounter(AU1 slice){spd_counter = atomicAdd(spdGlobalAtomic.counter, 1);}
  250. // AU1 SpdGetAtomicCounter() {return spdCounter;}
  251. // // HLSL:
  252. // void SpdIncreaseAtomicCounter(AU1 slice){InterlockedAdd(spdGlobalAtomic[0].counter, 1, spdCounter);}
  253. // AU1 SpdGetAtomicCounter(){return spdCounter;}
  254. // // Define the LDS load and store functions
  255. // // GLSL:
  256. // AH4 SpdLoadIntermediateH(AU1 x, AU1 y){return spdIntermediate[x][y];}
  257. // void SpdStoreIntermediateH(AU1 x, AU1 y, AH4 value){spdIntermediate[x][y] = value;}
  258. // // HLSL:
  259. // AH4 SpdLoadIntermediate(AU1 x, AU1 y){return spdIntermediate[x][y];}
  260. // void SpdStoreIntermediate(AU1 x, AU1 y, AH4 value){spdIntermediate[x][y] = value;}
  261. // // Define your reduction function: takes as input the four 2x2 values and returns 1 output value
  262. // Example below: computes the average value
  263. // AH4 SpdReduce4H(AH4 v0, AH4 v1, AH4 v2, AH4 v3){return (v0+v1+v2+v3)*AH1(0.25);}
  264. // //
  265. // // If you only use PACKED version
  266. // #define SPD_PACKED_ONLY
  267. // // Include this SPD (single pass downsampler) header file (or copy it in without an include).
  268. // #include "ffx_spd.h"
  269. // ...
  270. // // Example in shader integration
  271. // // GLSL:
  272. // layout(local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  273. // void main(){
  274. // // Call the downsampling function
  275. // // WorkGroupId.z should be 0 if you only downsample a Texture2D!
  276. // SpdDownsample(AU2(gl_WorkGroupID.xy), AU1(gl_LocalInvocationIndex),
  277. // AU1(spdConstants.mips), AU1(spdConstants.numWorkGroups), AU1(WorkGroupId.z));
  278. //
  279. // // PACKED:
  280. // SpdDownsampleH(AU2(gl_WorkGroupID.xy), AU1(gl_LocalInvocationIndex),
  281. // AU1(spdConstants.mips), AU1(spdConstants.numWorkGroups), AU1(WorkGroupId.z));
  282. // ...
  283. // // HLSL:
  284. // [numthreads(256,1,1)]
  285. // void main(uint3 WorkGroupId : SV_GroupID, uint LocalThreadIndex : SV_GroupIndex) {
  286. // SpdDownsample(AU2(WorkGroupId.xy), AU1(LocalThreadIndex),
  287. // AU1(mips), AU1(numWorkGroups), AU1(WorkGroupId.z));
  288. //
  289. // // PACKED:
  290. // SpdDownsampleH(AU2(WorkGroupId.xy), AU1(LocalThreadIndex),
  291. // AU1(mips), AU1(numWorkGroups), AU1(WorkGroupId.z));
  292. // ...
  293. //
  294. //------------------------------------------------------------------------------------------------------------------------------
  295. //==============================================================================================================================
  296. // SPD Setup
  297. //==============================================================================================================================
  298. #ifdef A_CPU
  299. A_STATIC void SpdSetup(outAU2 dispatchThreadGroupCountXY, // CPU side: dispatch thread group count xy
  300. outAU2 workGroupOffset, // GPU side: pass in as constant
  301. outAU2 numWorkGroupsAndMips, // GPU side: pass in as constant
  302. inAU4 rectInfo, // left, top, width, height
  303. ASU1 mips // optional: if -1, calculate based on rect width and height
  304. )
  305. {
  306. workGroupOffset[0] = rectInfo[0] / 64; // rectInfo[0] = left
  307. workGroupOffset[1] = rectInfo[1] / 64; // rectInfo[1] = top
  308. AU1 endIndexX = (rectInfo[0] + rectInfo[2] - 1) / 64; // rectInfo[0] = left, rectInfo[2] = width
  309. AU1 endIndexY = (rectInfo[1] + rectInfo[3] - 1) / 64; // rectInfo[1] = top, rectInfo[3] = height
  310. dispatchThreadGroupCountXY[0] = endIndexX + 1 - workGroupOffset[0];
  311. dispatchThreadGroupCountXY[1] = endIndexY + 1 - workGroupOffset[1];
  312. numWorkGroupsAndMips[0] = (dispatchThreadGroupCountXY[0]) * (dispatchThreadGroupCountXY[1]);
  313. if(mips >= 0)
  314. {
  315. numWorkGroupsAndMips[1] = AU1(mips);
  316. }
  317. else
  318. { // calculate based on rect width and height
  319. AU1 resolution = AMaxU1(rectInfo[2], rectInfo[3]);
  320. numWorkGroupsAndMips[1] = AU1((AMinF1(AFloorF1(ALog2F1(AF1(resolution))), AF1(12))));
  321. }
  322. }
  323. A_STATIC void SpdSetup(outAU2 dispatchThreadGroupCountXY, // CPU side: dispatch thread group count xy
  324. outAU2 workGroupOffset, // GPU side: pass in as constant
  325. outAU2 numWorkGroupsAndMips, // GPU side: pass in as constant
  326. inAU4 rectInfo // left, top, width, height
  327. )
  328. {
  329. SpdSetup(dispatchThreadGroupCountXY, workGroupOffset, numWorkGroupsAndMips, rectInfo, -1);
  330. }
  331. #endif // #ifdef A_CPU
  332. //==============================================================================================================================
  333. // NON-PACKED VERSION
  334. //==============================================================================================================================
  335. #ifdef A_GPU
  336. # ifdef SPD_PACKED_ONLY
  337. // Avoid compiler error
  338. AF4 SpdLoadSourceImage(ASU2 p, AU1 slice)
  339. {
  340. return AF4(0.0, 0.0, 0.0, 0.0);
  341. }
  342. AF4 SpdLoad(ASU2 p, AU1 slice)
  343. {
  344. return AF4(0.0, 0.0, 0.0, 0.0);
  345. }
  346. void SpdStore(ASU2 p, AF4 value, AU1 mip, AU1 slice)
  347. {
  348. }
  349. AF4 SpdLoadIntermediate(AU1 x, AU1 y)
  350. {
  351. return AF4(0.0, 0.0, 0.0, 0.0);
  352. }
  353. void SpdStoreIntermediate(AU1 x, AU1 y, AF4 value)
  354. {
  355. }
  356. AF4 SpdReduce4(AF4 v0, AF4 v1, AF4 v2, AF4 v3)
  357. {
  358. return AF4(0.0, 0.0, 0.0, 0.0);
  359. }
  360. # endif // #ifdef SPD_PACKED_ONLY
  361. //_____________________________________________________________/\_______________________________________________________________
  362. # if defined(A_GLSL) && !defined(SPD_NO_WAVE_OPERATIONS)
  363. # extension GL_KHR_shader_subgroup_quad : require
  364. # endif
  365. void SpdWorkgroupShuffleBarrier()
  366. {
  367. # ifdef A_GLSL
  368. barrier();
  369. # endif
  370. # ifdef A_HLSL
  371. GroupMemoryBarrierWithGroupSync();
  372. # endif
  373. }
  374. // Only last active workgroup should proceed
  375. bool SpdExitWorkgroup(AU1 numWorkGroups, AU1 localInvocationIndex, AU1 slice)
  376. {
  377. // global atomic counter
  378. if(localInvocationIndex == 0u)
  379. {
  380. SpdIncreaseAtomicCounter(slice);
  381. }
  382. SpdWorkgroupShuffleBarrier();
  383. return (SpdGetAtomicCounter() != (numWorkGroups - 1u));
  384. }
  385. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  386. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  387. // User defined: AF4 SpdReduce4(AF4 v0, AF4 v1, AF4 v2, AF4 v3);
  388. AF4 SpdReduceQuad(AF4 v)
  389. {
  390. # if defined(A_GLSL) && !defined(SPD_NO_WAVE_OPERATIONS)
  391. AF4 v0 = v;
  392. AF4 v1 = subgroupQuadSwapHorizontal(v);
  393. AF4 v2 = subgroupQuadSwapVertical(v);
  394. AF4 v3 = subgroupQuadSwapDiagonal(v);
  395. return SpdReduce4(v0, v1, v2, v3);
  396. # elif defined(A_HLSL) && !defined(SPD_NO_WAVE_OPERATIONS)
  397. // requires SM6.0
  398. AF4 v0 = v;
  399. AF4 v1 = QuadReadAcrossX(v);
  400. AF4 v2 = QuadReadAcrossY(v);
  401. AF4 v3 = QuadReadAcrossDiagonal(v);
  402. return SpdReduce4(v0, v1, v2, v3);
  403. /*
  404. // if SM6.0 is not available, you can use the AMD shader intrinsics
  405. // the AMD shader intrinsics are available in AMD GPU Services (AGS) library:
  406. // https://gpuopen.com/amd-gpu-services-ags-library/
  407. // works for DX11
  408. AF4 v0 = v;
  409. AF4 v1;
  410. v1.x = AmdExtD3DShaderIntrinsics_SwizzleF(v.x, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  411. v1.y = AmdExtD3DShaderIntrinsics_SwizzleF(v.y, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  412. v1.z = AmdExtD3DShaderIntrinsics_SwizzleF(v.z, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  413. v1.w = AmdExtD3DShaderIntrinsics_SwizzleF(v.w, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  414. AF4 v2;
  415. v2.x = AmdExtD3DShaderIntrinsics_SwizzleF(v.x, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  416. v2.y = AmdExtD3DShaderIntrinsics_SwizzleF(v.y, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  417. v2.z = AmdExtD3DShaderIntrinsics_SwizzleF(v.z, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  418. v2.w = AmdExtD3DShaderIntrinsics_SwizzleF(v.w, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  419. AF4 v3;
  420. v3.x = AmdExtD3DShaderIntrinsics_SwizzleF(v.x, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  421. v3.y = AmdExtD3DShaderIntrinsics_SwizzleF(v.y, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  422. v3.z = AmdExtD3DShaderIntrinsics_SwizzleF(v.z, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  423. v3.w = AmdExtD3DShaderIntrinsics_SwizzleF(v.w, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  424. return SpdReduce4(v0, v1, v2, v3);
  425. */
  426. # endif
  427. return v;
  428. }
  429. AF4 SpdReduceIntermediate(AU2 i0, AU2 i1, AU2 i2, AU2 i3)
  430. {
  431. AF4 v0 = SpdLoadIntermediate(i0.x, i0.y);
  432. AF4 v1 = SpdLoadIntermediate(i1.x, i1.y);
  433. AF4 v2 = SpdLoadIntermediate(i2.x, i2.y);
  434. AF4 v3 = SpdLoadIntermediate(i3.x, i3.y);
  435. return SpdReduce4(v0, v1, v2, v3);
  436. }
  437. AF4 SpdReduceLoad4(AU2 i0, AU2 i1, AU2 i2, AU2 i3, AU1 slice)
  438. {
  439. AF4 v0 = SpdLoad(i0, slice);
  440. AF4 v1 = SpdLoad(i1, slice);
  441. AF4 v2 = SpdLoad(i2, slice);
  442. AF4 v3 = SpdLoad(i3, slice);
  443. return SpdReduce4(v0, v1, v2, v3);
  444. }
  445. AF4 SpdReduceLoad4(AU2 base, AU1 slice)
  446. {
  447. return SpdReduceLoad4(AU2(base + AU2(0, 0)), AU2(base + AU2(0, 1)), AU2(base + AU2(1, 0)), AU2(base + AU2(1, 1)),
  448. slice);
  449. }
  450. AF4 SpdReduceLoadSourceImage4(AU2 i0, AU2 i1, AU2 i2, AU2 i3, AU1 slice)
  451. {
  452. AF4 v0 = SpdLoadSourceImage(i0, slice);
  453. AF4 v1 = SpdLoadSourceImage(i1, slice);
  454. AF4 v2 = SpdLoadSourceImage(i2, slice);
  455. AF4 v3 = SpdLoadSourceImage(i3, slice);
  456. return SpdReduce4(v0, v1, v2, v3);
  457. }
  458. AF4 SpdReduceLoadSourceImage(AU2 base, AU1 slice)
  459. {
  460. # ifdef SPD_LINEAR_SAMPLER
  461. return SpdLoadSourceImage(base, slice);
  462. # else
  463. return SpdReduceLoadSourceImage4(AU2(base + AU2(0, 0)), AU2(base + AU2(0, 1)), AU2(base + AU2(1, 0)),
  464. AU2(base + AU2(1, 1)), slice);
  465. # endif
  466. }
  467. void SpdDownsampleMips_0_1_Intrinsics(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  468. {
  469. AF4 v[4];
  470. AU2 tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u, y * 2u);
  471. AU2 pix = AU2(workGroupID.xy * 32u) + AU2(x, y);
  472. v[0] = SpdReduceLoadSourceImage(tex, slice);
  473. SpdStore(pix, v[0], 0u, slice);
  474. tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u + 32u, y * 2u);
  475. pix = AU2(workGroupID.xy * 32u) + AU2(x + 16u, y);
  476. v[1] = SpdReduceLoadSourceImage(tex, slice);
  477. SpdStore(pix, v[1], 0u, slice);
  478. tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u, y * 2u + 32u);
  479. pix = AU2(workGroupID.xy * 32u) + AU2(x, y + 16u);
  480. v[2] = SpdReduceLoadSourceImage(tex, slice);
  481. SpdStore(pix, v[2], 0u, slice);
  482. tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u + 32u, y * 2u + 32u);
  483. pix = AU2(workGroupID.xy * 32u) + AU2(x + 16u, y + 16u);
  484. v[3] = SpdReduceLoadSourceImage(tex, slice);
  485. SpdStore(pix, v[3], 0u, slice);
  486. if(mip <= 1u)
  487. return;
  488. v[0] = SpdReduceQuad(v[0]);
  489. v[1] = SpdReduceQuad(v[1]);
  490. v[2] = SpdReduceQuad(v[2]);
  491. v[3] = SpdReduceQuad(v[3]);
  492. if((localInvocationIndex % 4u) == 0u)
  493. {
  494. SpdStore(AU2(workGroupID.xy * 16u) + AU2(x / 2u, y / 2u), v[0], 1u, slice);
  495. SpdStoreIntermediate(x / 2u, y / 2u, v[0]);
  496. SpdStore(AU2(workGroupID.xy * 16u) + AU2(x / 2u + 8u, y / 2u), v[1], 1u, slice);
  497. SpdStoreIntermediate(x / 2u + 8u, y / 2u, v[1]);
  498. SpdStore(AU2(workGroupID.xy * 16u) + AU2(x / 2u, y / 2u + 8u), v[2], 1u, slice);
  499. SpdStoreIntermediate(x / 2u, y / 2u + 8u, v[2]);
  500. SpdStore(AU2(workGroupID.xy * 16u) + AU2(x / 2u + 8u, y / 2u + 8u), v[3], 1u, slice);
  501. SpdStoreIntermediate(x / 2u + 8u, y / 2u + 8u, v[3]);
  502. }
  503. }
  504. void SpdDownsampleMips_0_1_LDS(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  505. {
  506. AF4 v[4];
  507. AU2 tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u, y * 2u);
  508. AU2 pix = AU2(workGroupID.xy * 32u) + AU2(x, y);
  509. v[0] = SpdReduceLoadSourceImage(tex, slice);
  510. SpdStore(pix, v[0], 0u, slice);
  511. tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u + 32u, y * 2u);
  512. pix = AU2(workGroupID.xy * 32u) + AU2(x + 16u, y);
  513. v[1] = SpdReduceLoadSourceImage(tex, slice);
  514. SpdStore(pix, v[1], 0u, slice);
  515. tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u, y * 2u + 32u);
  516. pix = AU2(workGroupID.xy * 32u) + AU2(x, y + 16u);
  517. v[2] = SpdReduceLoadSourceImage(tex, slice);
  518. SpdStore(pix, v[2], 0u, slice);
  519. tex = AU2(workGroupID.xy * 64u) + AU2(x * 2u + 32u, y * 2u + 32u);
  520. pix = AU2(workGroupID.xy * 32u) + AU2(x + 16u, y + 16u);
  521. v[3] = SpdReduceLoadSourceImage(tex, slice);
  522. SpdStore(pix, v[3], 0u, slice);
  523. if(mip <= 1u)
  524. return;
  525. for(AU1 i = 0u; i < 4u; i++)
  526. {
  527. SpdStoreIntermediate(x, y, v[i]);
  528. SpdWorkgroupShuffleBarrier();
  529. if(localInvocationIndex < 64u)
  530. {
  531. v[i] = SpdReduceIntermediate(AU2(x * 2u + 0u, y * 2u + 0u), AU2(x * 2u + 1u, y * 2u + 0u),
  532. AU2(x * 2u + 0u, y * 2u + 1u), AU2(x * 2u + 1u, y * 2u + 1u));
  533. SpdStore(AU2(workGroupID.xy * 16u) + AU2(x + (i % 2u) * 8u, y + (i / 2u) * 8u), v[i], 1u, slice);
  534. }
  535. SpdWorkgroupShuffleBarrier();
  536. }
  537. if(localInvocationIndex < 64u)
  538. {
  539. SpdStoreIntermediate(x + 0u, y + 0u, v[0]);
  540. SpdStoreIntermediate(x + 8u, y + 0u, v[1]);
  541. SpdStoreIntermediate(x + 0u, y + 8u, v[2]);
  542. SpdStoreIntermediate(x + 8u, y + 8u, v[3]);
  543. }
  544. }
  545. void SpdDownsampleMips_0_1(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  546. {
  547. # ifdef SPD_NO_WAVE_OPERATIONS
  548. SpdDownsampleMips_0_1_LDS(x, y, workGroupID, localInvocationIndex, mip, slice);
  549. # else
  550. SpdDownsampleMips_0_1_Intrinsics(x, y, workGroupID, localInvocationIndex, mip, slice);
  551. # endif
  552. }
  553. void SpdDownsampleMip_2(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  554. {
  555. # ifdef SPD_NO_WAVE_OPERATIONS
  556. if(localInvocationIndex < 64u)
  557. {
  558. AF4 v = SpdReduceIntermediate(AU2(x * 2u + 0u, y * 2u + 0u), AU2(x * 2u + 1u, y * 2u + 0u),
  559. AU2(x * 2u + 0u, y * 2u + 1u), AU2(x * 2u + 1u, y * 2u + 1u));
  560. SpdStore(AU2(workGroupID.xy * 8u) + AU2(x, y), v, mip, slice);
  561. // store to LDS, try to reduce bank conflicts
  562. // x 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0
  563. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  564. // 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0 x
  565. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  566. // x 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0
  567. // ...
  568. // x 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0
  569. SpdStoreIntermediate(x * 2u + y % 2u, y * 2u, v);
  570. }
  571. # else
  572. AF4 v = SpdLoadIntermediate(x, y);
  573. v = SpdReduceQuad(v);
  574. // quad index 0 stores result
  575. if(localInvocationIndex % 4u == 0u)
  576. {
  577. SpdStore(AU2(workGroupID.xy * 8u) + AU2(x / 2u, y / 2u), v, mip, slice);
  578. SpdStoreIntermediate(x + (y / 2u) % 2u, y, v);
  579. }
  580. # endif
  581. }
  582. void SpdDownsampleMip_3(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  583. {
  584. # ifdef SPD_NO_WAVE_OPERATIONS
  585. if(localInvocationIndex < 16u)
  586. {
  587. // x 0 x 0
  588. // 0 0 0 0
  589. // 0 x 0 x
  590. // 0 0 0 0
  591. AF4 v = SpdReduceIntermediate(AU2(x * 4u + 0u + 0u, y * 4u + 0u), AU2(x * 4u + 2u + 0u, y * 4u + 0u),
  592. AU2(x * 4u + 0u + 1u, y * 4u + 2u), AU2(x * 4u + 2u + 1u, y * 4u + 2u));
  593. SpdStore(AU2(workGroupID.xy * 4u) + AU2(x, y), v, mip, slice);
  594. // store to LDS
  595. // x 0 0 0 x 0 0 0 x 0 0 0 x 0 0 0
  596. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  597. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  598. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  599. // 0 x 0 0 0 x 0 0 0 x 0 0 0 x 0 0
  600. // ...
  601. // 0 0 x 0 0 0 x 0 0 0 x 0 0 0 x 0
  602. // ...
  603. // 0 0 0 x 0 0 0 x 0 0 0 x 0 0 0 x
  604. // ...
  605. SpdStoreIntermediate(x * 4u + y, y * 4u, v);
  606. }
  607. # else
  608. if(localInvocationIndex < 64u)
  609. {
  610. AF4 v = SpdLoadIntermediate(x * 2u + y % 2u, y * 2u);
  611. v = SpdReduceQuad(v);
  612. // quad index 0 stores result
  613. if(localInvocationIndex % 4u == 0u)
  614. {
  615. SpdStore(AU2(workGroupID.xy * 4u) + AU2(x / 2u, y / 2u), v, mip, slice);
  616. SpdStoreIntermediate(x * 2u + y / 2u, y * 2u, v);
  617. }
  618. }
  619. # endif
  620. }
  621. void SpdDownsampleMip_4(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  622. {
  623. # ifdef SPD_NO_WAVE_OPERATIONS
  624. if(localInvocationIndex < 4u)
  625. {
  626. // x 0 0 0 x 0 0 0
  627. // ...
  628. // 0 x 0 0 0 x 0 0
  629. AF4 v = SpdReduceIntermediate(
  630. AU2(x * 8u + 0u + 0u + y * 2u, y * 8u + 0u), AU2(x * 8u + 4u + 0u + y * 2u, y * 8u + 0u),
  631. AU2(x * 8u + 0u + 1u + y * 2u, y * 8u + 4u), AU2(x * 8u + 4u + 1u + y * 2u, y * 8u + 4u));
  632. SpdStore(AU2(workGroupID.xy * 2u) + AU2(x, y), v, mip, slice);
  633. // store to LDS
  634. // x x x x 0 ...
  635. // 0 ...
  636. SpdStoreIntermediate(x + y * 2u, 0u, v);
  637. }
  638. # else
  639. if(localInvocationIndex < 16u)
  640. {
  641. AF4 v = SpdLoadIntermediate(x * 4u + y, y * 4u);
  642. v = SpdReduceQuad(v);
  643. // quad index 0 stores result
  644. if(localInvocationIndex % 4u == 0u)
  645. {
  646. SpdStore(AU2(workGroupID.xy * 2u) + AU2(x / 2u, y / 2u), v, mip, slice);
  647. SpdStoreIntermediate(x / 2u + y, 0u, v);
  648. }
  649. }
  650. # endif
  651. }
  652. void SpdDownsampleMip_5(AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  653. {
  654. # ifdef SPD_NO_WAVE_OPERATIONS
  655. if(localInvocationIndex < 1u)
  656. {
  657. // x x x x 0 ...
  658. // 0 ...
  659. AF4 v = SpdReduceIntermediate(AU2(0, 0), AU2(1, 0), AU2(2, 0), AU2(3, 0));
  660. SpdStore(AU2(workGroupID.xy), v, mip, slice);
  661. }
  662. # else
  663. if(localInvocationIndex < 4u)
  664. {
  665. AF4 v = SpdLoadIntermediate(localInvocationIndex, 0u);
  666. v = SpdReduceQuad(v);
  667. // quad index 0 stores result
  668. if(localInvocationIndex % 4u == 0u)
  669. {
  670. SpdStore(AU2(workGroupID.xy), v, mip, slice);
  671. }
  672. }
  673. # endif
  674. }
  675. void SpdDownsampleMips_6_7(AU1 x, AU1 y, AU1 mips, AU1 slice)
  676. {
  677. AU2 tex = AU2(x * 4u + 0u, y * 4u + 0u);
  678. AU2 pix = AU2(x * 2u + 0u, y * 2u + 0u);
  679. AF4 v0 = SpdReduceLoad4(tex, slice);
  680. SpdStore(pix, v0, 6u, slice);
  681. tex = AU2(x * 4u + 2u, y * 4u + 0u);
  682. pix = AU2(x * 2u + 1u, y * 2u + 0u);
  683. AF4 v1 = SpdReduceLoad4(tex, slice);
  684. SpdStore(pix, v1, 6u, slice);
  685. tex = AU2(x * 4u + 0u, y * 4u + 2u);
  686. pix = AU2(x * 2u + 0u, y * 2u + 1u);
  687. AF4 v2 = SpdReduceLoad4(tex, slice);
  688. SpdStore(pix, v2, 6u, slice);
  689. tex = AU2(x * 4u + 2u, y * 4u + 2u);
  690. pix = AU2(x * 2u + 1u, y * 2u + 1u);
  691. AF4 v3 = SpdReduceLoad4(tex, slice);
  692. SpdStore(pix, v3, 6u, slice);
  693. if(mips <= 7u)
  694. return;
  695. // no barrier needed, working on values only from the same thread
  696. AF4 v = SpdReduce4(v0, v1, v2, v3);
  697. SpdStore(AU2(x, y), v, 7u, slice);
  698. SpdStoreIntermediate(x, y, v);
  699. }
  700. void SpdDownsampleNextFour(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 baseMip, AU1 mips, AU1 slice)
  701. {
  702. if(mips <= baseMip)
  703. return;
  704. SpdWorkgroupShuffleBarrier();
  705. SpdDownsampleMip_2(x, y, workGroupID, localInvocationIndex, baseMip, slice);
  706. if(mips <= baseMip + 1u)
  707. return;
  708. SpdWorkgroupShuffleBarrier();
  709. SpdDownsampleMip_3(x, y, workGroupID, localInvocationIndex, baseMip + 1u, slice);
  710. if(mips <= baseMip + 2u)
  711. return;
  712. SpdWorkgroupShuffleBarrier();
  713. SpdDownsampleMip_4(x, y, workGroupID, localInvocationIndex, baseMip + 2u, slice);
  714. if(mips <= baseMip + 3u)
  715. return;
  716. SpdWorkgroupShuffleBarrier();
  717. SpdDownsampleMip_5(workGroupID, localInvocationIndex, baseMip + 3u, slice);
  718. }
  719. void SpdDownsample(AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 numWorkGroups, AU1 slice)
  720. {
  721. AU2 sub_xy = ARmpRed8x8(localInvocationIndex % 64u);
  722. AU1 x = sub_xy.x + 8u * ((localInvocationIndex >> 6u) % 2u);
  723. AU1 y = sub_xy.y + 8u * ((localInvocationIndex >> 7u));
  724. SpdDownsampleMips_0_1(x, y, workGroupID, localInvocationIndex, mips, slice);
  725. SpdDownsampleNextFour(x, y, workGroupID, localInvocationIndex, 2u, mips, slice);
  726. if(mips <= 6u)
  727. return;
  728. if(SpdExitWorkgroup(numWorkGroups, localInvocationIndex, slice))
  729. return;
  730. SpdResetAtomicCounter(slice);
  731. // After mip 6 there is only a single workgroup left that downsamples the remaining up to 64x64 texels.
  732. SpdDownsampleMips_6_7(x, y, mips, slice);
  733. SpdDownsampleNextFour(x, y, AU2(0, 0), localInvocationIndex, 8u, mips, slice);
  734. }
  735. void SpdDownsample(AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 numWorkGroups, AU1 slice,
  736. AU2 workGroupOffset)
  737. {
  738. SpdDownsample(workGroupID + workGroupOffset, localInvocationIndex, mips, numWorkGroups, slice);
  739. }
  740. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  741. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  742. //==============================================================================================================================
  743. // PACKED VERSION
  744. //==============================================================================================================================
  745. # ifdef A_HALF
  746. # ifdef A_GLSL
  747. # extension GL_EXT_shader_subgroup_extended_types_float16 : require
  748. # endif
  749. AH4 SpdReduceQuadH(AH4 v)
  750. {
  751. # if defined(A_GLSL) && !defined(SPD_NO_WAVE_OPERATIONS)
  752. AH4 v0 = v;
  753. AH4 v1 = subgroupQuadSwapHorizontal(v);
  754. AH4 v2 = subgroupQuadSwapVertical(v);
  755. AH4 v3 = subgroupQuadSwapDiagonal(v);
  756. return SpdReduce4H(v0, v1, v2, v3);
  757. # elif defined(A_HLSL) && !defined(SPD_NO_WAVE_OPERATIONS)
  758. // requires SM6.0
  759. AU1 quad = WaveGetLaneIndex() & (~0x3);
  760. AH4 v0 = v;
  761. AH4 v1 = WaveReadLaneAt(v, quad | 1);
  762. AH4 v2 = WaveReadLaneAt(v, quad | 2);
  763. AH4 v3 = WaveReadLaneAt(v, quad | 3);
  764. return SpdReduce4H(v0, v1, v2, v3);
  765. /*
  766. // if SM6.0 is not available, you can use the AMD shader intrinsics
  767. // the AMD shader intrinsics are available in AMD GPU Services (AGS) library:
  768. // https://gpuopen.com/amd-gpu-services-ags-library/
  769. // works for DX11
  770. AH4 v0 = v;
  771. AH4 v1;
  772. v1.x = AmdExtD3DShaderIntrinsics_SwizzleF(v.x, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  773. v1.y = AmdExtD3DShaderIntrinsics_SwizzleF(v.y, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  774. v1.z = AmdExtD3DShaderIntrinsics_SwizzleF(v.z, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  775. v1.w = AmdExtD3DShaderIntrinsics_SwizzleF(v.w, AmdExtD3DShaderIntrinsicsSwizzle_SwapX1);
  776. AH4 v2;
  777. v2.x = AmdExtD3DShaderIntrinsics_SwizzleF(v.x, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  778. v2.y = AmdExtD3DShaderIntrinsics_SwizzleF(v.y, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  779. v2.z = AmdExtD3DShaderIntrinsics_SwizzleF(v.z, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  780. v2.w = AmdExtD3DShaderIntrinsics_SwizzleF(v.w, AmdExtD3DShaderIntrinsicsSwizzle_SwapX2);
  781. AH4 v3;
  782. v3.x = AmdExtD3DShaderIntrinsics_SwizzleF(v.x, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  783. v3.y = AmdExtD3DShaderIntrinsics_SwizzleF(v.y, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  784. v3.z = AmdExtD3DShaderIntrinsics_SwizzleF(v.z, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  785. v3.w = AmdExtD3DShaderIntrinsics_SwizzleF(v.w, AmdExtD3DShaderIntrinsicsSwizzle_ReverseX4);
  786. return SpdReduce4H(v0, v1, v2, v3);
  787. */
  788. # endif
  789. return AH4(0.0, 0.0, 0.0, 0.0);
  790. }
  791. AH4 SpdReduceIntermediateH(AU2 i0, AU2 i1, AU2 i2, AU2 i3)
  792. {
  793. AH4 v0 = SpdLoadIntermediateH(i0.x, i0.y);
  794. AH4 v1 = SpdLoadIntermediateH(i1.x, i1.y);
  795. AH4 v2 = SpdLoadIntermediateH(i2.x, i2.y);
  796. AH4 v3 = SpdLoadIntermediateH(i3.x, i3.y);
  797. return SpdReduce4H(v0, v1, v2, v3);
  798. }
  799. AH4 SpdReduceLoad4H(AU2 i0, AU2 i1, AU2 i2, AU2 i3, AU1 slice)
  800. {
  801. AH4 v0 = SpdLoadH(ASU2(i0), slice);
  802. AH4 v1 = SpdLoadH(ASU2(i1), slice);
  803. AH4 v2 = SpdLoadH(ASU2(i2), slice);
  804. AH4 v3 = SpdLoadH(ASU2(i3), slice);
  805. return SpdReduce4H(v0, v1, v2, v3);
  806. }
  807. AH4 SpdReduceLoad4H(AU2 base, AU1 slice)
  808. {
  809. return SpdReduceLoad4H(AU2(base + AU2(0, 0)), AU2(base + AU2(0, 1)), AU2(base + AU2(1, 0)), AU2(base + AU2(1, 1)),
  810. slice);
  811. }
  812. AH4 SpdReduceLoadSourceImage4H(AU2 i0, AU2 i1, AU2 i2, AU2 i3, AU1 slice)
  813. {
  814. AH4 v0 = SpdLoadSourceImageH(ASU2(i0), slice);
  815. AH4 v1 = SpdLoadSourceImageH(ASU2(i1), slice);
  816. AH4 v2 = SpdLoadSourceImageH(ASU2(i2), slice);
  817. AH4 v3 = SpdLoadSourceImageH(ASU2(i3), slice);
  818. return SpdReduce4H(v0, v1, v2, v3);
  819. }
  820. AH4 SpdReduceLoadSourceImageH(AU2 base, AU1 slice)
  821. {
  822. # ifdef SPD_LINEAR_SAMPLER
  823. return SpdLoadSourceImageH(ASU2(base), slice);
  824. # else
  825. return SpdReduceLoadSourceImage4H(AU2(base + AU2(0, 0)), AU2(base + AU2(0, 1)), AU2(base + AU2(1, 0)),
  826. AU2(base + AU2(1, 1)), slice);
  827. # endif
  828. }
  829. void SpdDownsampleMips_0_1_IntrinsicsH(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 slice)
  830. {
  831. AH4 v[4];
  832. ASU2 tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2, y * 2);
  833. ASU2 pix = ASU2(workGroupID.xy * 32) + ASU2(x, y);
  834. v[0] = SpdReduceLoadSourceImageH(tex, slice);
  835. SpdStoreH(pix, v[0], 0, slice);
  836. tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2 + 32, y * 2);
  837. pix = ASU2(workGroupID.xy * 32) + ASU2(x + 16, y);
  838. v[1] = SpdReduceLoadSourceImageH(tex, slice);
  839. SpdStoreH(pix, v[1], 0, slice);
  840. tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2, y * 2 + 32);
  841. pix = ASU2(workGroupID.xy * 32) + ASU2(x, y + 16);
  842. v[2] = SpdReduceLoadSourceImageH(tex, slice);
  843. SpdStoreH(pix, v[2], 0, slice);
  844. tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2 + 32, y * 2 + 32);
  845. pix = ASU2(workGroupID.xy * 32) + ASU2(x + 16, y + 16);
  846. v[3] = SpdReduceLoadSourceImageH(tex, slice);
  847. SpdStoreH(pix, v[3], 0, slice);
  848. if(mips <= 1)
  849. return;
  850. v[0] = SpdReduceQuadH(v[0]);
  851. v[1] = SpdReduceQuadH(v[1]);
  852. v[2] = SpdReduceQuadH(v[2]);
  853. v[3] = SpdReduceQuadH(v[3]);
  854. if((localInvocationIndex % 4) == 0)
  855. {
  856. SpdStoreH(ASU2(workGroupID.xy * 16) + ASU2(x / 2, y / 2), v[0], 1, slice);
  857. SpdStoreIntermediateH(x / 2, y / 2, v[0]);
  858. SpdStoreH(ASU2(workGroupID.xy * 16) + ASU2(x / 2 + 8, y / 2), v[1], 1, slice);
  859. SpdStoreIntermediateH(x / 2 + 8, y / 2, v[1]);
  860. SpdStoreH(ASU2(workGroupID.xy * 16) + ASU2(x / 2, y / 2 + 8), v[2], 1, slice);
  861. SpdStoreIntermediateH(x / 2, y / 2 + 8, v[2]);
  862. SpdStoreH(ASU2(workGroupID.xy * 16) + ASU2(x / 2 + 8, y / 2 + 8), v[3], 1, slice);
  863. SpdStoreIntermediateH(x / 2 + 8, y / 2 + 8, v[3]);
  864. }
  865. }
  866. void SpdDownsampleMips_0_1_LDSH(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 slice)
  867. {
  868. AH4 v[4];
  869. ASU2 tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2, y * 2);
  870. ASU2 pix = ASU2(workGroupID.xy * 32) + ASU2(x, y);
  871. v[0] = SpdReduceLoadSourceImageH(tex, slice);
  872. SpdStoreH(pix, v[0], 0, slice);
  873. tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2 + 32, y * 2);
  874. pix = ASU2(workGroupID.xy * 32) + ASU2(x + 16, y);
  875. v[1] = SpdReduceLoadSourceImageH(tex, slice);
  876. SpdStoreH(pix, v[1], 0, slice);
  877. tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2, y * 2 + 32);
  878. pix = ASU2(workGroupID.xy * 32) + ASU2(x, y + 16);
  879. v[2] = SpdReduceLoadSourceImageH(tex, slice);
  880. SpdStoreH(pix, v[2], 0, slice);
  881. tex = ASU2(workGroupID.xy * 64) + ASU2(x * 2 + 32, y * 2 + 32);
  882. pix = ASU2(workGroupID.xy * 32) + ASU2(x + 16, y + 16);
  883. v[3] = SpdReduceLoadSourceImageH(tex, slice);
  884. SpdStoreH(pix, v[3], 0, slice);
  885. if(mips <= 1)
  886. return;
  887. for(int i = 0; i < 4; i++)
  888. {
  889. SpdStoreIntermediateH(x, y, v[i]);
  890. SpdWorkgroupShuffleBarrier();
  891. if(localInvocationIndex < 64u)
  892. {
  893. v[i] = SpdReduceIntermediateH(AU2(x * 2 + 0, y * 2 + 0), AU2(x * 2 + 1, y * 2 + 0),
  894. AU2(x * 2 + 0, y * 2 + 1), AU2(x * 2 + 1, y * 2 + 1));
  895. SpdStoreH(ASU2(workGroupID.xy * 16) + ASU2(x + (i % 2) * 8, y + (i / 2) * 8), v[i], 1, slice);
  896. }
  897. SpdWorkgroupShuffleBarrier();
  898. }
  899. if(localInvocationIndex < 64u)
  900. {
  901. SpdStoreIntermediateH(x + 0, y + 0, v[0]);
  902. SpdStoreIntermediateH(x + 8, y + 0, v[1]);
  903. SpdStoreIntermediateH(x + 0, y + 8, v[2]);
  904. SpdStoreIntermediateH(x + 8, y + 8, v[3]);
  905. }
  906. }
  907. void SpdDownsampleMips_0_1H(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 slice)
  908. {
  909. # ifdef SPD_NO_WAVE_OPERATIONS
  910. SpdDownsampleMips_0_1_LDSH(x, y, workGroupID, localInvocationIndex, mips, slice);
  911. # else
  912. SpdDownsampleMips_0_1_IntrinsicsH(x, y, workGroupID, localInvocationIndex, mips, slice);
  913. # endif
  914. }
  915. void SpdDownsampleMip_2H(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  916. {
  917. # ifdef SPD_NO_WAVE_OPERATIONS
  918. if(localInvocationIndex < 64u)
  919. {
  920. AH4 v = SpdReduceIntermediateH(AU2(x * 2 + 0, y * 2 + 0), AU2(x * 2 + 1, y * 2 + 0), AU2(x * 2 + 0, y * 2 + 1),
  921. AU2(x * 2 + 1, y * 2 + 1));
  922. SpdStoreH(ASU2(workGroupID.xy * 8) + ASU2(x, y), v, mip, slice);
  923. // store to LDS, try to reduce bank conflicts
  924. // x 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0
  925. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  926. // 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0 x
  927. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  928. // x 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0
  929. // ...
  930. // x 0 x 0 x 0 x 0 x 0 x 0 x 0 x 0
  931. SpdStoreIntermediateH(x * 2 + y % 2, y * 2, v);
  932. }
  933. # else
  934. AH4 v = SpdLoadIntermediateH(x, y);
  935. v = SpdReduceQuadH(v);
  936. // quad index 0 stores result
  937. if(localInvocationIndex % 4 == 0)
  938. {
  939. SpdStoreH(ASU2(workGroupID.xy * 8) + ASU2(x / 2, y / 2), v, mip, slice);
  940. SpdStoreIntermediateH(x + (y / 2) % 2, y, v);
  941. }
  942. # endif
  943. }
  944. void SpdDownsampleMip_3H(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  945. {
  946. # ifdef SPD_NO_WAVE_OPERATIONS
  947. if(localInvocationIndex < 16)
  948. {
  949. // x 0 x 0
  950. // 0 0 0 0
  951. // 0 x 0 x
  952. // 0 0 0 0
  953. AH4 v = SpdReduceIntermediateH(AU2(x * 4 + 0 + 0, y * 4 + 0), AU2(x * 4 + 2 + 0, y * 4 + 0),
  954. AU2(x * 4 + 0 + 1, y * 4 + 2), AU2(x * 4 + 2 + 1, y * 4 + 2));
  955. SpdStoreH(ASU2(workGroupID.xy * 4) + ASU2(x, y), v, mip, slice);
  956. // store to LDS
  957. // x 0 0 0 x 0 0 0 x 0 0 0 x 0 0 0
  958. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  959. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  960. // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  961. // 0 x 0 0 0 x 0 0 0 x 0 0 0 x 0 0
  962. // ...
  963. // 0 0 x 0 0 0 x 0 0 0 x 0 0 0 x 0
  964. // ...
  965. // 0 0 0 x 0 0 0 x 0 0 0 x 0 0 0 x
  966. // ...
  967. SpdStoreIntermediateH(x * 4 + y, y * 4, v);
  968. }
  969. # else
  970. if(localInvocationIndex < 64u)
  971. {
  972. AH4 v = SpdLoadIntermediateH(x * 2 + y % 2, y * 2);
  973. v = SpdReduceQuadH(v);
  974. // quad index 0 stores result
  975. if(localInvocationIndex % 4 == 0)
  976. {
  977. SpdStoreH(ASU2(workGroupID.xy * 4) + ASU2(x / 2, y / 2), v, mip, slice);
  978. SpdStoreIntermediateH(x * 2 + y / 2, y * 2, v);
  979. }
  980. }
  981. # endif
  982. }
  983. void SpdDownsampleMip_4H(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  984. {
  985. # ifdef SPD_NO_WAVE_OPERATIONS
  986. if(localInvocationIndex < 4)
  987. {
  988. // x 0 0 0 x 0 0 0
  989. // ...
  990. // 0 x 0 0 0 x 0 0
  991. AH4 v = SpdReduceIntermediateH(AU2(x * 8 + 0 + 0 + y * 2, y * 8 + 0), AU2(x * 8 + 4 + 0 + y * 2, y * 8 + 0),
  992. AU2(x * 8 + 0 + 1 + y * 2, y * 8 + 4), AU2(x * 8 + 4 + 1 + y * 2, y * 8 + 4));
  993. SpdStoreH(ASU2(workGroupID.xy * 2) + ASU2(x, y), v, mip, slice);
  994. // store to LDS
  995. // x x x x 0 ...
  996. // 0 ...
  997. SpdStoreIntermediateH(x + y * 2, 0, v);
  998. }
  999. # else
  1000. if(localInvocationIndex < 16)
  1001. {
  1002. AH4 v = SpdLoadIntermediateH(x * 4 + y, y * 4);
  1003. v = SpdReduceQuadH(v);
  1004. // quad index 0 stores result
  1005. if(localInvocationIndex % 4 == 0)
  1006. {
  1007. SpdStoreH(ASU2(workGroupID.xy * 2) + ASU2(x / 2, y / 2), v, mip, slice);
  1008. SpdStoreIntermediateH(x / 2 + y, 0, v);
  1009. }
  1010. }
  1011. # endif
  1012. }
  1013. void SpdDownsampleMip_5H(AU2 workGroupID, AU1 localInvocationIndex, AU1 mip, AU1 slice)
  1014. {
  1015. # ifdef SPD_NO_WAVE_OPERATIONS
  1016. if(localInvocationIndex < 1)
  1017. {
  1018. // x x x x 0 ...
  1019. // 0 ...
  1020. AH4 v = SpdReduceIntermediateH(AU2(0, 0), AU2(1, 0), AU2(2, 0), AU2(3, 0));
  1021. SpdStoreH(ASU2(workGroupID.xy), v, mip, slice);
  1022. }
  1023. # else
  1024. if(localInvocationIndex < 4)
  1025. {
  1026. AH4 v = SpdLoadIntermediateH(localInvocationIndex, 0);
  1027. v = SpdReduceQuadH(v);
  1028. // quad index 0 stores result
  1029. if(localInvocationIndex % 4 == 0)
  1030. {
  1031. SpdStoreH(ASU2(workGroupID.xy), v, mip, slice);
  1032. }
  1033. }
  1034. # endif
  1035. }
  1036. void SpdDownsampleMips_6_7H(AU1 x, AU1 y, AU1 mips, AU1 slice)
  1037. {
  1038. ASU2 tex = ASU2(x * 4 + 0, y * 4 + 0);
  1039. ASU2 pix = ASU2(x * 2 + 0, y * 2 + 0);
  1040. AH4 v0 = SpdReduceLoad4H(tex, slice);
  1041. SpdStoreH(pix, v0, 6, slice);
  1042. tex = ASU2(x * 4 + 2, y * 4 + 0);
  1043. pix = ASU2(x * 2 + 1, y * 2 + 0);
  1044. AH4 v1 = SpdReduceLoad4H(tex, slice);
  1045. SpdStoreH(pix, v1, 6, slice);
  1046. tex = ASU2(x * 4 + 0, y * 4 + 2);
  1047. pix = ASU2(x * 2 + 0, y * 2 + 1);
  1048. AH4 v2 = SpdReduceLoad4H(tex, slice);
  1049. SpdStoreH(pix, v2, 6, slice);
  1050. tex = ASU2(x * 4 + 2, y * 4 + 2);
  1051. pix = ASU2(x * 2 + 1, y * 2 + 1);
  1052. AH4 v3 = SpdReduceLoad4H(tex, slice);
  1053. SpdStoreH(pix, v3, 6, slice);
  1054. if(mips < 8)
  1055. return;
  1056. // no barrier needed, working on values only from the same thread
  1057. AH4 v = SpdReduce4H(v0, v1, v2, v3);
  1058. SpdStoreH(ASU2(x, y), v, 7, slice);
  1059. SpdStoreIntermediateH(x, y, v);
  1060. }
  1061. void SpdDownsampleNextFourH(AU1 x, AU1 y, AU2 workGroupID, AU1 localInvocationIndex, AU1 baseMip, AU1 mips, AU1 slice)
  1062. {
  1063. if(mips <= baseMip)
  1064. return;
  1065. SpdWorkgroupShuffleBarrier();
  1066. SpdDownsampleMip_2H(x, y, workGroupID, localInvocationIndex, baseMip, slice);
  1067. if(mips <= baseMip + 1)
  1068. return;
  1069. SpdWorkgroupShuffleBarrier();
  1070. SpdDownsampleMip_3H(x, y, workGroupID, localInvocationIndex, baseMip + 1, slice);
  1071. if(mips <= baseMip + 2)
  1072. return;
  1073. SpdWorkgroupShuffleBarrier();
  1074. SpdDownsampleMip_4H(x, y, workGroupID, localInvocationIndex, baseMip + 2, slice);
  1075. if(mips <= baseMip + 3)
  1076. return;
  1077. SpdWorkgroupShuffleBarrier();
  1078. SpdDownsampleMip_5H(workGroupID, localInvocationIndex, baseMip + 3, slice);
  1079. }
  1080. void SpdDownsampleH(AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 numWorkGroups, AU1 slice)
  1081. {
  1082. AU2 sub_xy = ARmpRed8x8(localInvocationIndex % 64);
  1083. AU1 x = sub_xy.x + 8 * ((localInvocationIndex >> 6) % 2);
  1084. AU1 y = sub_xy.y + 8 * ((localInvocationIndex >> 7));
  1085. SpdDownsampleMips_0_1H(x, y, workGroupID, localInvocationIndex, mips, slice);
  1086. SpdDownsampleNextFourH(x, y, workGroupID, localInvocationIndex, 2, mips, slice);
  1087. if(mips < 7)
  1088. return;
  1089. if(SpdExitWorkgroup(numWorkGroups, localInvocationIndex, slice))
  1090. return;
  1091. SpdResetAtomicCounter(slice);
  1092. // After mip 6 there is only a single workgroup left that downsamples the remaining up to 64x64 texels.
  1093. SpdDownsampleMips_6_7H(x, y, mips, slice);
  1094. SpdDownsampleNextFourH(x, y, AU2(0, 0), localInvocationIndex, 8, mips, slice);
  1095. }
  1096. void SpdDownsampleH(AU2 workGroupID, AU1 localInvocationIndex, AU1 mips, AU1 numWorkGroups, AU1 slice,
  1097. AU2 workGroupOffset)
  1098. {
  1099. SpdDownsampleH(workGroupID + workGroupOffset, localInvocationIndex, mips, numWorkGroups, slice);
  1100. }
  1101. # endif // #ifdef A_HALF
  1102. #endif // #ifdef A_GPU