No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Blitter.cs 47KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Experimental.Rendering;
  4. using UnityEngine.Assertions;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine.Rendering.RenderGraphModule.Util;
  7. #if UNITY_EDITOR
  8. using UnityEditor;
  9. #endif
  10. namespace UnityEngine.Rendering
  11. {
  12. /// <summary>
  13. /// Various blit (texture copy) utilities for the Scriptable Render Pipelines.
  14. /// </summary>
  15. public static class Blitter
  16. {
  17. static Material s_Copy;
  18. static Material s_Blit;
  19. static Material s_BlitTexArray;
  20. static Material s_BlitTexArraySingleSlice;
  21. static Material s_BlitColorAndDepth;
  22. static MaterialPropertyBlock s_PropertyBlock = new MaterialPropertyBlock();
  23. static Mesh s_TriangleMesh;
  24. static Mesh s_QuadMesh;
  25. static LocalKeyword s_DecodeHdrKeyword;
  26. static class BlitShaderIDs
  27. {
  28. public static readonly int _BlitTexture = Shader.PropertyToID("_BlitTexture");
  29. public static readonly int _BlitCubeTexture = Shader.PropertyToID("_BlitCubeTexture");
  30. public static readonly int _BlitScaleBias = Shader.PropertyToID("_BlitScaleBias");
  31. public static readonly int _BlitScaleBiasRt = Shader.PropertyToID("_BlitScaleBiasRt");
  32. public static readonly int _BlitMipLevel = Shader.PropertyToID("_BlitMipLevel");
  33. public static readonly int _BlitTexArraySlice = Shader.PropertyToID("_BlitTexArraySlice");
  34. public static readonly int _BlitTextureSize = Shader.PropertyToID("_BlitTextureSize");
  35. public static readonly int _BlitPaddingSize = Shader.PropertyToID("_BlitPaddingSize");
  36. public static readonly int _BlitDecodeInstructions = Shader.PropertyToID("_BlitDecodeInstructions");
  37. public static readonly int _InputDepth = Shader.PropertyToID("_InputDepthTexture");
  38. }
  39. // This enum needs to be in sync with the shader pass names and indices of the Blit.shader in every pipeline.
  40. enum BlitShaderPassNames
  41. {
  42. Nearest = 0,
  43. Bilinear = 1,
  44. NearestQuad = 2,
  45. BilinearQuad = 3,
  46. NearestQuadPadding = 4,
  47. BilinearQuadPadding = 5,
  48. NearestQuadPaddingRepeat = 6,
  49. BilinearQuadPaddingRepeat = 7,
  50. BilinearQuadPaddingOctahedral = 8,
  51. NearestQuadPaddingAlphaBlend = 9,
  52. BilinearQuadPaddingAlphaBlend = 10,
  53. NearestQuadPaddingAlphaBlendRepeat = 11,
  54. BilinearQuadPaddingAlphaBlendRepeat = 12,
  55. BilinearQuadPaddingAlphaBlendOctahedral = 13,
  56. CubeToOctahedral = 14,
  57. CubeToOctahedralLuminance = 15,
  58. CubeToOctahedralAlpha = 16,
  59. CubeToOctahedralRed = 17,
  60. BilinearQuadLuminance = 18,
  61. BilinearQuadAlpha = 19,
  62. BilinearQuadRed = 20,
  63. NearestCubeToOctahedralPadding = 21,
  64. BilinearCubeToOctahedralPadding = 22,
  65. }
  66. enum BlitColorAndDepthPassNames
  67. {
  68. ColorOnly = 0,
  69. ColorAndDepth = 1,
  70. }
  71. // This maps the requested shader indices to actual existing shader indices. When running in a build, it's possible
  72. // that some shader pass are stripped or removed, causing a shift in all shader pass index. In this case, hardcoded
  73. // shader passes become invalid. This array prevent this error from happening.
  74. static int[] s_BlitShaderPassIndicesMap;
  75. static int[] s_BlitColorAndDepthShaderPassIndicesMap;
  76. /// <summary>
  77. /// Initialize Blitter resources. Must be called once before any use
  78. /// </summary>
  79. /// <param name="blitPS">Blit shader</param>
  80. /// <param name="blitColorAndDepthPS">Blit shader</param>
  81. public static void Initialize(Shader blitPS, Shader blitColorAndDepthPS)
  82. {
  83. if (s_Blit != null)
  84. {
  85. throw new Exception("Blitter is already initialized. Please only initialize the blitter once or you will leak engine resources. If you need to re-initialize the blitter with different shaders destroy & recreate it.");
  86. }
  87. // NOTE NOTE NOTE NOTE NOTE NOTE
  88. // If you create something here you must also destroy it in Cleanup()
  89. // or it will leak during enter/leave play mode cycles
  90. // NOTE NOTE NOTE NOTE NOTE NOTE
  91. s_Copy = CoreUtils.CreateEngineMaterial(GraphicsSettings.GetRenderPipelineSettings<RenderGraphUtilsResources>().coreCopyPS);
  92. s_Blit = CoreUtils.CreateEngineMaterial(blitPS);
  93. s_BlitColorAndDepth = CoreUtils.CreateEngineMaterial(blitColorAndDepthPS);
  94. s_DecodeHdrKeyword = new LocalKeyword(blitPS, "BLIT_DECODE_HDR");
  95. // With texture array enabled, we still need the normal blit version for other systems like atlas
  96. if (TextureXR.useTexArray)
  97. {
  98. s_Blit.EnableKeyword("DISABLE_TEXTURE2D_X_ARRAY");
  99. s_BlitTexArray = CoreUtils.CreateEngineMaterial(blitPS);
  100. s_BlitTexArraySingleSlice = CoreUtils.CreateEngineMaterial(blitPS);
  101. s_BlitTexArraySingleSlice.EnableKeyword("BLIT_SINGLE_SLICE");
  102. }
  103. /*UNITY_NEAR_CLIP_VALUE*/
  104. float nearClipZ = -1;
  105. if (SystemInfo.usesReversedZBuffer)
  106. nearClipZ = 1;
  107. if (SystemInfo.graphicsShaderLevel < 30)
  108. {
  109. if (!s_TriangleMesh)
  110. {
  111. s_TriangleMesh = new Mesh();
  112. s_TriangleMesh.vertices = GetFullScreenTriangleVertexPosition(nearClipZ);
  113. s_TriangleMesh.uv = GetFullScreenTriangleTexCoord();
  114. s_TriangleMesh.triangles = new int[3] { 0, 1, 2 };
  115. }
  116. }
  117. if (!s_QuadMesh)
  118. {
  119. s_QuadMesh = new Mesh();
  120. s_QuadMesh.vertices = GetQuadVertexPosition(nearClipZ);
  121. s_QuadMesh.uv = GetQuadTexCoord();
  122. s_QuadMesh.triangles = new int[6] { 0, 1, 2, 0, 2, 3 };
  123. }
  124. // Should match Common.hlsl
  125. static Vector3[] GetFullScreenTriangleVertexPosition(float z /*= UNITY_NEAR_CLIP_VALUE*/)
  126. {
  127. var r = new Vector3[3];
  128. for (int i = 0; i < 3; i++)
  129. {
  130. Vector2 uv = new Vector2((i << 1) & 2, i & 2);
  131. r[i] = new Vector3(uv.x * 2.0f - 1.0f, uv.y * 2.0f - 1.0f, z);
  132. }
  133. return r;
  134. }
  135. // Should match Common.hlsl
  136. static Vector2[] GetFullScreenTriangleTexCoord()
  137. {
  138. var r = new Vector2[3];
  139. for (int i = 0; i < 3; i++)
  140. {
  141. if (SystemInfo.graphicsUVStartsAtTop)
  142. r[i] = new Vector2((i << 1) & 2, 1.0f - (i & 2));
  143. else
  144. r[i] = new Vector2((i << 1) & 2, i & 2);
  145. }
  146. return r;
  147. }
  148. // Should match Common.hlsl
  149. static Vector3[] GetQuadVertexPosition(float z /*= UNITY_NEAR_CLIP_VALUE*/)
  150. {
  151. var r = new Vector3[4];
  152. for (uint i = 0; i < 4; i++)
  153. {
  154. uint topBit = i >> 1;
  155. uint botBit = (i & 1);
  156. float x = topBit;
  157. float y = 1 - (topBit + botBit) & 1; // produces 1 for indices 0,3 and 0 for 1,2
  158. r[i] = new Vector3(x, y, z);
  159. }
  160. return r;
  161. }
  162. // Should match Common.hlsl
  163. static Vector2[] GetQuadTexCoord()
  164. {
  165. var r = new Vector2[4];
  166. for (uint i = 0; i < 4; i++)
  167. {
  168. uint topBit = i >> 1;
  169. uint botBit = (i & 1);
  170. float u = topBit;
  171. float v = (topBit + botBit) & 1; // produces 0 for indices 0,3 and 1 for 1,2
  172. if (SystemInfo.graphicsUVStartsAtTop)
  173. v = 1.0f - v;
  174. r[i] = new Vector2(u, v);
  175. }
  176. return r;
  177. }
  178. // Build shader pass map:
  179. var passNames = Enum.GetNames(typeof(BlitShaderPassNames));
  180. s_BlitShaderPassIndicesMap = new int[passNames.Length];
  181. for (int i = 0; i < passNames.Length; i++)
  182. s_BlitShaderPassIndicesMap[i] = s_Blit.FindPass(passNames[i]);
  183. passNames = Enum.GetNames(typeof(BlitColorAndDepthPassNames));
  184. s_BlitColorAndDepthShaderPassIndicesMap = new int[passNames.Length];
  185. for (int i = 0; i < passNames.Length; i++)
  186. s_BlitColorAndDepthShaderPassIndicesMap[i] = s_BlitColorAndDepth.FindPass(passNames[i]);
  187. }
  188. /// <summary>
  189. /// Release Blitter resources.
  190. /// </summary>
  191. public static void Cleanup()
  192. {
  193. CoreUtils.Destroy(s_Copy);
  194. s_Copy = null;
  195. CoreUtils.Destroy(s_Blit);
  196. s_Blit = null;
  197. CoreUtils.Destroy(s_BlitColorAndDepth);
  198. s_BlitColorAndDepth = null;
  199. CoreUtils.Destroy(s_BlitTexArray);
  200. s_BlitTexArray = null;
  201. CoreUtils.Destroy(s_BlitTexArraySingleSlice);
  202. s_BlitTexArraySingleSlice = null;
  203. CoreUtils.Destroy(s_TriangleMesh);
  204. s_TriangleMesh = null;
  205. CoreUtils.Destroy(s_QuadMesh);
  206. s_QuadMesh = null;
  207. }
  208. /// <summary>
  209. /// Returns the default blit material.
  210. /// </summary>
  211. /// <param name="dimension">Dimension of the texture to blit, either 2D or 2D Array.</param>
  212. /// <param name="singleSlice">Blit only a single slice of the array if applicable.</param>
  213. /// <returns>The default blit material for specified arguments.</returns>
  214. static public Material GetBlitMaterial(TextureDimension dimension, bool singleSlice = false)
  215. {
  216. bool useTexArray = dimension == TextureDimension.Tex2DArray;
  217. return useTexArray ? (singleSlice ? s_BlitTexArraySingleSlice : s_BlitTexArray) : s_Blit;
  218. }
  219. static internal void DrawTriangle(RasterCommandBuffer cmd, Material material, int shaderPass)
  220. {
  221. DrawTriangle(cmd.m_WrappedCommandBuffer, material, shaderPass);
  222. }
  223. static internal void DrawTriangle(CommandBuffer cmd, Material material, int shaderPass)
  224. {
  225. DrawTriangle(cmd, material, shaderPass, s_PropertyBlock);
  226. }
  227. static internal void DrawTriangle(CommandBuffer cmd, Material material, int shaderPass, MaterialPropertyBlock propertyBlock)
  228. {
  229. if (SystemInfo.graphicsShaderLevel < 30)
  230. cmd.DrawMesh(s_TriangleMesh, Matrix4x4.identity, material, 0, shaderPass, propertyBlock);
  231. else
  232. cmd.DrawProcedural(Matrix4x4.identity, material, shaderPass, MeshTopology.Triangles, 3, 1, propertyBlock);
  233. }
  234. static internal void DrawQuadMesh(CommandBuffer cmd, Material material, int shaderPass, MaterialPropertyBlock propertyBlock)
  235. {
  236. cmd.DrawMesh(s_QuadMesh, Matrix4x4.identity, material, 0, shaderPass, propertyBlock);
  237. }
  238. static internal void DrawQuad(RasterCommandBuffer cmd, Material material, int shaderPass, MaterialPropertyBlock propertyBlock)
  239. {
  240. DrawQuad(cmd.m_WrappedCommandBuffer, material, shaderPass, propertyBlock);
  241. }
  242. static internal void DrawQuad(CommandBuffer cmd, Material material, int shaderPass)
  243. {
  244. DrawQuad(cmd, material, shaderPass, s_PropertyBlock);
  245. }
  246. static internal void DrawQuad(CommandBuffer cmd, Material material, int shaderPass, MaterialPropertyBlock propertyBlock)
  247. {
  248. if (SystemInfo.graphicsShaderLevel < 30)
  249. cmd.DrawMesh(s_QuadMesh, Matrix4x4.identity, material, 0, shaderPass, propertyBlock);
  250. else
  251. cmd.DrawProcedural(Matrix4x4.identity, material, shaderPass, MeshTopology.Quads, 4, 1, propertyBlock);
  252. }
  253. internal static bool CanCopyMSAA()
  254. {
  255. return s_Copy.passCount == 2;
  256. }
  257. /// <summary>
  258. /// Copy a texture to another texture using framebuffer fetch.
  259. /// </summary>
  260. /// <param name="cmd">Command Buffer used for rendering.</param>
  261. internal static void CopyTexture(RasterCommandBuffer cmd, bool isMSAA)
  262. {
  263. DrawTriangle(cmd, s_Copy, isMSAA ? 1 : 0);
  264. }
  265. /// <summary>
  266. /// Blit a RTHandle texture.
  267. /// </summary>
  268. /// <param name="cmd">Command Buffer used for rendering.</param>
  269. /// <param name="source">Source RTHandle.</param>
  270. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  271. /// <param name="sourceMipLevel">Mip level to blit from source.</param>
  272. /// <param name="sourceDepthSlice">Source texture slice index.</param>
  273. /// <param name="bilinear">Enable bilinear filtering.</param>
  274. internal static void BlitTexture(CommandBuffer cmd, RTHandle source, Vector4 scaleBias, float sourceMipLevel, int sourceDepthSlice, bool bilinear)
  275. {
  276. BlitTexture(cmd, source, scaleBias, GetBlitMaterial(TextureDimension.Tex2D), s_BlitShaderPassIndicesMap[bilinear ? 1 : 0], sourceMipLevel, sourceDepthSlice);
  277. }
  278. /// <summary>
  279. /// Blit a RTHandle texture.
  280. /// </summary>
  281. /// <param name="cmd">Command Buffer used for rendering.</param>
  282. /// <param name="source">Source RTHandle.</param>
  283. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  284. /// <param name="sourceMipLevel">Mip level to blit from source.</param>
  285. /// <param name="sourceDepthSlice">Source texture slice index.</param>
  286. internal static void BlitTexture(CommandBuffer cmd, RTHandle source, Vector4 scaleBias, Material material, int pass, float sourceMipLevel, int sourceDepthSlice)
  287. {
  288. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, sourceMipLevel);
  289. s_PropertyBlock.SetInt(BlitShaderIDs._BlitTexArraySlice, sourceDepthSlice);
  290. BlitTexture(cmd, source, scaleBias, material, pass);
  291. }
  292. /// <summary>
  293. /// Blit a RTHandle texture.
  294. /// </summary>
  295. /// <param name="cmd">Command Buffer used for rendering.</param>
  296. /// <param name="source">Source RTHandle.</param>
  297. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  298. /// <param name="mipLevel">Mip level to blit.</param>
  299. /// <param name="bilinear">Enable bilinear filtering.</param>
  300. public static void BlitTexture(RasterCommandBuffer cmd, RTHandle source, Vector4 scaleBias, float mipLevel, bool bilinear)
  301. {
  302. BlitTexture(cmd.m_WrappedCommandBuffer, source, scaleBias, mipLevel, bilinear);
  303. }
  304. /// <summary>
  305. /// Blit a RTHandle texture.
  306. /// </summary>
  307. /// <param name="cmd">Command Buffer used for rendering.</param>
  308. /// <param name="source">Source RTHandle.</param>
  309. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  310. /// <param name="mipLevel">Mip level to blit.</param>
  311. /// <param name="bilinear">Enable bilinear filtering.</param>
  312. public static void BlitTexture(CommandBuffer cmd, RTHandle source, Vector4 scaleBias, float mipLevel, bool bilinear)
  313. {
  314. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevel);
  315. BlitTexture(cmd, source, scaleBias, GetBlitMaterial(TextureXR.dimension), s_BlitShaderPassIndicesMap[bilinear ? 1 : 0]);
  316. }
  317. /// <summary>
  318. /// Blit a RTHandle texture 2D.
  319. /// </summary>
  320. /// <param name="cmd">Command Buffer used for rendering.</param>
  321. /// <param name="source">Source RTHandle.</param>
  322. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  323. /// <param name="mipLevel">Mip level to blit.</param>
  324. /// <param name="bilinear">Enable bilinear filtering.</param>
  325. public static void BlitTexture2D(RasterCommandBuffer cmd, RTHandle source, Vector4 scaleBias, float mipLevel, bool bilinear)
  326. {
  327. BlitTexture2D(cmd.m_WrappedCommandBuffer, source, scaleBias, mipLevel, bilinear);
  328. }
  329. /// <summary>
  330. /// Blit a RTHandle texture 2D.
  331. /// </summary>
  332. /// <param name="cmd">Command Buffer used for rendering.</param>
  333. /// <param name="source">Source RTHandle.</param>
  334. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  335. /// <param name="mipLevel">Mip level to blit.</param>
  336. /// <param name="bilinear">Enable bilinear filtering.</param>
  337. public static void BlitTexture2D(CommandBuffer cmd, RTHandle source, Vector4 scaleBias, float mipLevel, bool bilinear)
  338. {
  339. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevel);
  340. BlitTexture(cmd, source, scaleBias, GetBlitMaterial(TextureDimension.Tex2D), s_BlitShaderPassIndicesMap[bilinear ? 1 : 0]);
  341. }
  342. /// <summary>
  343. /// Blit a 2D texture and depth buffer.
  344. /// </summary>
  345. /// <param name="cmd">Command Buffer used for rendering.</param>
  346. /// <param name="sourceColor">Source Texture for color.</param>
  347. /// <param name="sourceDepth">Source RenderTexture for depth.</param>
  348. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  349. /// <param name="mipLevel">Mip level to blit.</param>
  350. /// <param name="blitDepth">Enable depth blit.</param>
  351. public static void BlitColorAndDepth(RasterCommandBuffer cmd, Texture sourceColor, RenderTexture sourceDepth, Vector4 scaleBias, float mipLevel, bool blitDepth)
  352. {
  353. BlitColorAndDepth(cmd.m_WrappedCommandBuffer, sourceColor, sourceDepth, scaleBias, mipLevel, blitDepth);
  354. }
  355. /// <summary>
  356. /// Blit a 2D texture and depth buffer.
  357. /// </summary>
  358. /// <param name="cmd">Command Buffer used for rendering.</param>
  359. /// <param name="sourceColor">Source Texture for color.</param>
  360. /// <param name="sourceDepth">Source RenderTexture for depth.</param>
  361. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  362. /// <param name="mipLevel">Mip level to blit.</param>
  363. /// <param name="blitDepth">Enable depth blit.</param>
  364. public static void BlitColorAndDepth(CommandBuffer cmd, Texture sourceColor, RenderTexture sourceDepth, Vector4 scaleBias, float mipLevel, bool blitDepth)
  365. {
  366. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevel);
  367. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBias);
  368. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, sourceColor);
  369. if (blitDepth)
  370. s_PropertyBlock.SetTexture(BlitShaderIDs._InputDepth, sourceDepth, RenderTextureSubElement.Depth);
  371. DrawTriangle(cmd, s_BlitColorAndDepth, s_BlitColorAndDepthShaderPassIndicesMap[blitDepth ? 1 : 0]);
  372. }
  373. /// <summary>
  374. /// Blit a RTHandle texture
  375. /// </summary>
  376. /// <param name="cmd">Command Buffer used for rendering.</param>
  377. /// <param name="source">Source RTHandle.</param>
  378. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  379. /// <param name="material">Material to invoke when blitting.</param>
  380. /// <param name="pass">Pass idx within the material to invoke.</param>
  381. public static void BlitTexture(RasterCommandBuffer cmd, RTHandle source, Vector4 scaleBias, Material material, int pass)
  382. {
  383. BlitTexture(cmd.m_WrappedCommandBuffer, source, scaleBias, material, pass);
  384. }
  385. /// <summary>
  386. /// Blit a RTHandle texture
  387. /// </summary>
  388. /// <param name="cmd">Command Buffer used for rendering.</param>
  389. /// <param name="source">Source RTHandle.</param>
  390. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  391. /// <param name="material">Material to invoke when blitting.</param>
  392. /// <param name="pass">Pass idx within the material to invoke.</param>
  393. public static void BlitTexture(CommandBuffer cmd, RTHandle source, Vector4 scaleBias, Material material, int pass)
  394. {
  395. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBias);
  396. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  397. DrawTriangle(cmd, material, pass);
  398. }
  399. /// <summary>
  400. /// Blit a RTHandle texture
  401. /// </summary>
  402. /// <param name="cmd">Command Buffer used for rendering.</param>
  403. /// <param name="source">Source render target.</param>
  404. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  405. /// <param name="material">Material to invoke when blitting.</param>
  406. /// <param name="pass">Pass idx within the material to invoke.</param>
  407. public static void BlitTexture(RasterCommandBuffer cmd, RenderTargetIdentifier source, Vector4 scaleBias, Material material, int pass)
  408. {
  409. BlitTexture(cmd.m_WrappedCommandBuffer, source, scaleBias, material, pass);
  410. }
  411. /// <summary>
  412. /// Blit a RTHandle texture
  413. /// </summary>
  414. /// <param name="cmd">Command Buffer used for rendering.</param>
  415. /// <param name="source">Source render target.</param>
  416. /// <param name="scaleBias">Scale and bias for sampling the input texture.</param>
  417. /// <param name="material">Material to invoke when blitting.</param>
  418. /// <param name="pass">Pass idx within the material to invoke.</param>
  419. public static void BlitTexture(CommandBuffer cmd, RenderTargetIdentifier source, Vector4 scaleBias, Material material, int pass)
  420. {
  421. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBias);
  422. // Unfortunately there is no function bind a RenderTargetIdentifier with a property block so we have to bind it globally.
  423. cmd.SetGlobalTexture(BlitShaderIDs._BlitTexture, source);
  424. DrawTriangle(cmd, material, pass);
  425. }
  426. /// <summary>
  427. /// Blit a Texture with a specified material. The reference name "_BlitTexture" will be used to bind the input texture.
  428. /// </summary>
  429. /// <param name="cmd">Command Buffer used for rendering.</param>
  430. /// <param name="source">Source render target.</param>
  431. /// <param name="destination">Destination render target.</param>
  432. /// <param name="material">Material to invoke when blitting.</param>
  433. /// <param name="pass">Pass idx within the material to invoke.</param>
  434. public static void BlitTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material material, int pass)
  435. {
  436. // Unfortunately there is no function bind a RenderTargetIdentifier with a property block so we have to bind it globally.
  437. cmd.SetGlobalTexture(BlitShaderIDs._BlitTexture, source);
  438. cmd.SetRenderTarget(destination);
  439. DrawTriangle(cmd, material, pass);
  440. }
  441. /// <summary>
  442. /// Blit a Texture with a specified material. The reference name "_BlitTexture" will be used to bind the input texture.
  443. /// </summary>
  444. /// <param name="cmd">Command Buffer used for rendering.</param>
  445. /// <param name="source">Source render target.</param>
  446. /// <param name="destination">Destination render target.</param>
  447. /// <param name="loadAction">Load action.</param>
  448. /// <param name="storeAction">Store action.</param>
  449. /// <param name="material">Material to invoke when blitting.</param>
  450. /// <param name="pass">Pass idx within the material to invoke.</param>
  451. public static void BlitTexture(CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, Material material, int pass)
  452. {
  453. // Unfortunately there is no function bind a RenderTargetIdentifier with a property block so we have to bind it globally.
  454. cmd.SetGlobalTexture(BlitShaderIDs._BlitTexture, source);
  455. cmd.SetRenderTarget(destination, loadAction, storeAction);
  456. DrawTriangle(cmd, material, pass);
  457. }
  458. /// <summary>
  459. /// Blit a quad with a given Material. Set the destination parameter before using this method.
  460. /// </summary>
  461. /// <param name="cmd">Command Buffer used for rendering.</param>
  462. /// <param name="scaleBias">Scale and bias values for sampling the input texture.</param>
  463. /// <param name="material">Material to invoke when blitting.</param>
  464. /// <param name="pass">Pass index within the Material to invoke.</param>
  465. public static void BlitTexture(CommandBuffer cmd, Vector4 scaleBias, Material material, int pass)
  466. {
  467. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBias);
  468. DrawTriangle(cmd, material, pass);
  469. }
  470. /// <summary>
  471. /// Blit a quad with a given Material. Set the destination parameter before using this method.
  472. /// </summary>
  473. /// <param name="cmd">Command Buffer used for rendering.</param>
  474. /// <param name="scaleBias">Scale and bias values for sampling the input texture.</param>
  475. /// <param name="material">Material to invoke when blitting.</param>
  476. /// <param name="pass">Pass index within the Material to invoke.</param>
  477. public static void BlitTexture(RasterCommandBuffer cmd, Vector4 scaleBias, Material material, int pass)
  478. {
  479. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBias);
  480. DrawTriangle(cmd, material, pass);
  481. }
  482. /// <summary>
  483. /// Blit a RTHandle to another RTHandle.
  484. /// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
  485. /// </summary>
  486. /// <param name="cmd">Command Buffer used for rendering.</param>
  487. /// <param name="source">Source RTHandle.</param>
  488. /// <param name="destination">Destination RTHandle.</param>
  489. /// <param name="mipLevel">Mip level to blit.</param>
  490. /// <param name="bilinear">Enable bilinear filtering.</param>
  491. public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, float mipLevel = 0.0f, bool bilinear = false)
  492. {
  493. Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
  494. // Will set the correct camera viewport as well.
  495. CoreUtils.SetRenderTarget(cmd, destination);
  496. BlitTexture(cmd, source, viewportScale, mipLevel, bilinear);
  497. }
  498. /// <summary>
  499. /// Blit a RThandle Texture2D RTHandle to another RTHandle.
  500. /// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
  501. /// </summary>
  502. /// <param name="cmd">Command Buffer used for rendering.</param>
  503. /// <param name="source">Source RTHandle.</param>
  504. /// <param name="destination">Destination RTHandle.</param>
  505. /// <param name="mipLevel">Mip level to blit.</param>
  506. /// <param name="bilinear">Enable bilinear filtering.</param>
  507. public static void BlitCameraTexture2D(CommandBuffer cmd, RTHandle source, RTHandle destination, float mipLevel = 0.0f, bool bilinear = false)
  508. {
  509. Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
  510. // Will set the correct camera viewport as well.
  511. CoreUtils.SetRenderTarget(cmd, destination);
  512. BlitTexture2D(cmd, source, viewportScale, mipLevel, bilinear);
  513. }
  514. /// <summary>
  515. /// Blit a RTHandle to another RTHandle.
  516. /// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
  517. /// This overloads allows the user to override the default blit shader
  518. /// </summary>
  519. /// <param name="cmd">Command Buffer used for rendering.</param>
  520. /// <param name="source">Source RTHandle.</param>
  521. /// <param name="destination">Destination RTHandle.</param>
  522. /// <param name="material">The material to use when blitting</param>
  523. /// <param name="pass">pass to use of the provided material</param>
  524. public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, Material material, int pass)
  525. {
  526. Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
  527. // Will set the correct camera viewport as well.
  528. CoreUtils.SetRenderTarget(cmd, destination);
  529. BlitTexture(cmd, source, viewportScale, material, pass);
  530. }
  531. /// <summary>
  532. /// Blit a RTHandle to another RTHandle.
  533. /// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
  534. /// This overloads allows the user to override the default blit shader
  535. /// </summary>
  536. /// <param name="cmd">Command Buffer used for rendering.</param>
  537. /// <param name="source">Source RTHandle.</param>
  538. /// <param name="destination">Destination RTHandle.</param>
  539. /// <param name="loadAction">Load action.</param>
  540. /// <param name="storeAction">Store action.</param>
  541. /// <param name="material">The material to use when blitting</param>
  542. /// <param name="pass">pass to use of the provided material</param>
  543. public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, RenderBufferLoadAction loadAction, RenderBufferStoreAction storeAction, Material material, int pass)
  544. {
  545. Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
  546. // Will set the correct camera viewport as well.
  547. CoreUtils.SetRenderTarget(cmd, destination, loadAction, storeAction, ClearFlag.None, Color.clear);
  548. BlitTexture(cmd, source, viewportScale, material, pass);
  549. }
  550. /// <summary>
  551. /// Blit a RTHandle to another RTHandle.
  552. /// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
  553. /// This overload allows user to override the scale and bias used when sampling the input RTHandle.
  554. /// </summary>
  555. /// <param name="cmd">Command Buffer used for rendering.</param>
  556. /// <param name="source">Source RTHandle.</param>
  557. /// <param name="destination">Destination RTHandle.</param>
  558. /// <param name="scaleBias">Scale and bias used to sample the input RTHandle.</param>
  559. /// <param name="mipLevel">Mip level to blit.</param>
  560. /// <param name="bilinear">Enable bilinear filtering.</param>
  561. public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, Vector4 scaleBias, float mipLevel = 0.0f, bool bilinear = false)
  562. {
  563. // Will set the correct camera viewport as well.
  564. CoreUtils.SetRenderTarget(cmd, destination);
  565. BlitTexture(cmd, source, scaleBias, mipLevel, bilinear);
  566. }
  567. /// <summary>
  568. /// Blit a RTHandle to another RTHandle.
  569. /// This will properly account for partial usage (in term of resolution) of the texture for the current viewport.
  570. /// This overload allows user to override the viewport of the destination RTHandle.
  571. /// </summary>
  572. /// <param name="cmd">Command Buffer used for rendering.</param>
  573. /// <param name="source">Source RTHandle.</param>
  574. /// <param name="destination">Destination RTHandle.</param>
  575. /// <param name="destViewport">Viewport of the destination RTHandle.</param>
  576. /// <param name="mipLevel">Mip level to blit.</param>
  577. /// <param name="bilinear">Enable bilinear filtering.</param>
  578. public static void BlitCameraTexture(CommandBuffer cmd, RTHandle source, RTHandle destination, Rect destViewport, float mipLevel = 0.0f, bool bilinear = false)
  579. {
  580. Vector2 viewportScale = source.useScaling ? new Vector2(source.rtHandleProperties.rtHandleScale.x, source.rtHandleProperties.rtHandleScale.y) : Vector2.one;
  581. CoreUtils.SetRenderTarget(cmd, destination);
  582. cmd.SetViewport(destViewport);
  583. BlitTexture(cmd, source, viewportScale, mipLevel, bilinear);
  584. }
  585. /// <summary>
  586. /// Blit a texture using a quad in the current render target.
  587. /// </summary>
  588. /// <param name="cmd">Command buffer used for rendering.</param>
  589. /// <param name="source">Source texture.</param>
  590. /// <param name="scaleBiasTex">Scale and bias for the input texture.</param>
  591. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  592. /// <param name="mipLevelTex">Mip level to blit.</param>
  593. /// <param name="bilinear">Enable bilinear filtering.</param>
  594. public static void BlitQuad(CommandBuffer cmd, Texture source, Vector4 scaleBiasTex, Vector4 scaleBiasRT, int mipLevelTex, bool bilinear)
  595. {
  596. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  597. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBiasTex);
  598. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  599. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  600. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[bilinear ? 3 : 2]);
  601. }
  602. /// <summary>
  603. /// Blit a texture using a quad in the current render target.
  604. /// </summary>
  605. /// <param name="cmd">Command buffer used for rendering.</param>
  606. /// <param name="source">Source texture.</param>
  607. /// <param name="textureSize">Source texture size.</param>
  608. /// <param name="scaleBiasTex">Scale and bias for sampling the input texture.</param>
  609. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  610. /// <param name="mipLevelTex">Mip level to blit.</param>
  611. /// <param name="bilinear">Enable bilinear filtering.</param>
  612. /// <param name="paddingInPixels">Padding in pixels.</param>
  613. public static void BlitQuadWithPadding(CommandBuffer cmd, Texture source, Vector2 textureSize, Vector4 scaleBiasTex, Vector4 scaleBiasRT, int mipLevelTex, bool bilinear, int paddingInPixels)
  614. {
  615. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  616. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBiasTex);
  617. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  618. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  619. s_PropertyBlock.SetVector(BlitShaderIDs._BlitTextureSize, textureSize);
  620. s_PropertyBlock.SetInt(BlitShaderIDs._BlitPaddingSize, paddingInPixels);
  621. if (source.wrapMode == TextureWrapMode.Repeat)
  622. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[bilinear ? 7 : 6]);
  623. else
  624. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[bilinear ? 5 : 4]);
  625. }
  626. /// <summary>
  627. /// Blit a texture using a quad in the current render target, by performing an alpha blend with the existing content on the render target.
  628. /// </summary>
  629. /// <param name="cmd">Command buffer used for rendering.</param>
  630. /// <param name="source">Source texture.</param>
  631. /// <param name="textureSize">Source texture size.</param>
  632. /// <param name="scaleBiasTex">Scale and bias for sampling the input texture.</param>
  633. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  634. /// <param name="mipLevelTex">Mip level to blit.</param>
  635. /// <param name="bilinear">Enable bilinear filtering.</param>
  636. /// <param name="paddingInPixels">Padding in pixels.</param>
  637. public static void BlitQuadWithPaddingMultiply(CommandBuffer cmd, Texture source, Vector2 textureSize, Vector4 scaleBiasTex, Vector4 scaleBiasRT, int mipLevelTex, bool bilinear, int paddingInPixels)
  638. {
  639. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  640. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBiasTex);
  641. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  642. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  643. s_PropertyBlock.SetVector(BlitShaderIDs._BlitTextureSize, textureSize);
  644. s_PropertyBlock.SetInt(BlitShaderIDs._BlitPaddingSize, paddingInPixels);
  645. if (source.wrapMode == TextureWrapMode.Repeat)
  646. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[bilinear ? 12 : 11]);
  647. else
  648. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[bilinear ? 10 : 9]);
  649. }
  650. /// <summary>
  651. /// Blit a texture (which is a Octahedral projection) using a quad in the current render target.
  652. /// </summary>
  653. /// <param name="cmd">Command buffer used for rendering.</param>
  654. /// <param name="source">Source texture.</param>
  655. /// <param name="textureSize">Source texture size.</param>
  656. /// <param name="scaleBiasTex">Scale and bias for sampling the input texture.</param>
  657. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  658. /// <param name="mipLevelTex">Mip level to blit.</param>
  659. /// <param name="bilinear">Enable bilinear filtering.</param>
  660. /// <param name="paddingInPixels">Padding in pixels.</param>
  661. public static void BlitOctahedralWithPadding(CommandBuffer cmd, Texture source, Vector2 textureSize, Vector4 scaleBiasTex, Vector4 scaleBiasRT, int mipLevelTex, bool bilinear, int paddingInPixels)
  662. {
  663. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  664. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBiasTex);
  665. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  666. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  667. s_PropertyBlock.SetVector(BlitShaderIDs._BlitTextureSize, textureSize);
  668. s_PropertyBlock.SetInt(BlitShaderIDs._BlitPaddingSize, paddingInPixels);
  669. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[8]);
  670. }
  671. /// <summary>
  672. /// Blit a texture (which is a Octahedral projection) using a quad in the current render target, by performing an alpha blend with the existing content on the render target.
  673. /// </summary>
  674. /// <param name="cmd">Command buffer used for rendering.</param>
  675. /// <param name="source">Source texture.</param>
  676. /// <param name="textureSize">Source texture size.</param>
  677. /// <param name="scaleBiasTex">Scale and bias for sampling the input texture.</param>
  678. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  679. /// <param name="mipLevelTex">Mip level to blit.</param>
  680. /// <param name="bilinear">Enable bilinear filtering.</param>
  681. /// <param name="paddingInPixels">Padding in pixels.</param>
  682. public static void BlitOctahedralWithPaddingMultiply(CommandBuffer cmd, Texture source, Vector2 textureSize, Vector4 scaleBiasTex, Vector4 scaleBiasRT, int mipLevelTex, bool bilinear, int paddingInPixels)
  683. {
  684. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  685. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBiasTex);
  686. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  687. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  688. s_PropertyBlock.SetVector(BlitShaderIDs._BlitTextureSize, textureSize);
  689. s_PropertyBlock.SetInt(BlitShaderIDs._BlitPaddingSize, paddingInPixels);
  690. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[13]);
  691. }
  692. /// <summary>
  693. /// Blit a cube texture into 2d texture as octahedral quad. (projection)
  694. /// </summary>
  695. /// <param name="cmd">Command buffer used for rendering.</param>
  696. /// <param name="source">Source cube texture.</param>
  697. /// <param name="mipLevelTex">Mip level to sample.</param>
  698. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  699. public static void BlitCubeToOctahedral2DQuad(CommandBuffer cmd, Texture source, Vector4 scaleBiasRT, int mipLevelTex)
  700. {
  701. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitCubeTexture, source);
  702. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  703. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, new Vector4(1, 1, 0, 0));
  704. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  705. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[14]);
  706. }
  707. /// <summary>
  708. /// Blit a cube texture into 2d texture as octahedral quad with padding. (projection)
  709. /// </summary>
  710. /// <param name="cmd">Command buffer used for rendering.</param>
  711. /// <param name="source">Source cube texture.</param>
  712. /// <param name="textureSize">Source texture size.</param>
  713. /// <param name="mipLevelTex">Mip level to sample.</param>
  714. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  715. /// <param name="bilinear">Enable bilinear filtering.</param>
  716. /// <param name="paddingInPixels">Padding in pixels.</param>
  717. /// <param name="decodeInstructions">The purpose of this parameter is to blit HDR-encoded values to a non HDR texture. Use values from API that produce HDR-encoded values, for example <see cref="ReflectionProbe.textureHDRDecodeValues"/>. If this parameter is null, HDR decoding is disabled.</param>
  718. public static void BlitCubeToOctahedral2DQuadWithPadding(CommandBuffer cmd, Texture source, Vector2 textureSize, Vector4 scaleBiasRT, int mipLevelTex, bool bilinear, int paddingInPixels, Vector4? decodeInstructions = null)
  719. {
  720. var material = GetBlitMaterial(source.dimension);
  721. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitCubeTexture, source);
  722. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  723. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, new Vector4(1, 1, 0, 0));
  724. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  725. s_PropertyBlock.SetVector(BlitShaderIDs._BlitTextureSize, textureSize);
  726. s_PropertyBlock.SetInt(BlitShaderIDs._BlitPaddingSize, paddingInPixels);
  727. cmd.SetKeyword(material, s_DecodeHdrKeyword, decodeInstructions.HasValue);
  728. if (decodeInstructions.HasValue)
  729. {
  730. s_PropertyBlock.SetVector(BlitShaderIDs._BlitDecodeInstructions, decodeInstructions.Value);
  731. }
  732. DrawQuad(cmd, material, s_BlitShaderPassIndicesMap[bilinear ? 22 : 21]);
  733. cmd.SetKeyword(material, s_DecodeHdrKeyword, false);
  734. }
  735. /// <summary>
  736. /// Blit a cube texture into 2d texture as octahedral quad. (projection)
  737. /// Conversion between single and multi channel formats.
  738. /// RGB(A) to YYYY (luminance).
  739. /// R to RRRR.
  740. /// A to AAAA.
  741. /// </summary>
  742. /// <param name="cmd">Command buffer used for rendering.</param>
  743. /// <param name="source">Source texture.</param>
  744. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  745. /// <param name="mipLevelTex">Mip level to blit.</param>
  746. public static void BlitCubeToOctahedral2DQuadSingleChannel(CommandBuffer cmd, Texture source, Vector4 scaleBiasRT, int mipLevelTex)
  747. {
  748. int pass = 15;
  749. uint sourceChnCount = GraphicsFormatUtility.GetComponentCount(source.graphicsFormat);
  750. if (sourceChnCount == 1)
  751. {
  752. if (GraphicsFormatUtility.IsAlphaOnlyFormat(source.graphicsFormat))
  753. pass = 16;
  754. if (GraphicsFormatUtility.GetSwizzleR(source.graphicsFormat) == FormatSwizzle.FormatSwizzleR)
  755. pass = 17;
  756. }
  757. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitCubeTexture, source);
  758. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  759. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, new Vector4(1, 1, 0, 0));
  760. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  761. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[pass]);
  762. }
  763. /// <summary>
  764. /// Bilinear Blit a texture using a quad in the current render target.
  765. /// Conversion between single and multi channel formats.
  766. /// RGB(A) to YYYY (luminance).
  767. /// R to RRRR.
  768. /// A to AAAA.
  769. /// </summary>
  770. /// <param name="cmd">Command buffer used for rendering.</param>
  771. /// <param name="source">Source texture.</param>
  772. /// <param name="scaleBiasTex">Scale and bias for the input texture.</param>
  773. /// <param name="scaleBiasRT">Scale and bias for the output texture.</param>
  774. /// <param name="mipLevelTex">Mip level to blit.</param>
  775. public static void BlitQuadSingleChannel(CommandBuffer cmd, Texture source, Vector4 scaleBiasTex, Vector4 scaleBiasRT, int mipLevelTex)
  776. {
  777. int pass = 18;
  778. uint sourceChnCount = GraphicsFormatUtility.GetComponentCount(source.graphicsFormat);
  779. if (sourceChnCount == 1)
  780. {
  781. if (GraphicsFormatUtility.IsAlphaOnlyFormat(source.graphicsFormat))
  782. pass = 19;
  783. if (GraphicsFormatUtility.GetSwizzleR(source.graphicsFormat) == FormatSwizzle.FormatSwizzleR)
  784. pass = 20;
  785. }
  786. s_PropertyBlock.SetTexture(BlitShaderIDs._BlitTexture, source);
  787. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBias, scaleBiasTex);
  788. s_PropertyBlock.SetVector(BlitShaderIDs._BlitScaleBiasRt, scaleBiasRT);
  789. s_PropertyBlock.SetFloat(BlitShaderIDs._BlitMipLevel, mipLevelTex);
  790. DrawQuad(cmd, GetBlitMaterial(source.dimension), s_BlitShaderPassIndicesMap[pass]);
  791. }
  792. }
  793. }