1 using UnityEngine
.Assertions
;
2 using UnityEngine
.Rendering
;
4 namespace UnityEngine
.Experimental
.Rendering
6 public class EncodeBC6H
8 public static EncodeBC6H DefaultInstance
;
10 static readonly int _Source
= Shader
.PropertyToID("_Source");
11 static readonly int _Target
= Shader
.PropertyToID("_Target");
12 static readonly int _MipIndex
= Shader
.PropertyToID("_MipIndex");
13 static readonly int[] __Tmp_RT
=
15 Shader
.PropertyToID("__Tmp_RT0"),
16 Shader
.PropertyToID("__Tmp_RT1"),
17 Shader
.PropertyToID("__Tmp_RT2"),
18 Shader
.PropertyToID("__Tmp_RT3"),
19 Shader
.PropertyToID("__Tmp_RT4"),
20 Shader
.PropertyToID("__Tmp_RT5"),
21 Shader
.PropertyToID("__Tmp_RT6"),
22 Shader
.PropertyToID("__Tmp_RT7"),
23 Shader
.PropertyToID("__Tmp_RT8"),
24 Shader
.PropertyToID("__Tmp_RT9"),
25 Shader
.PropertyToID("__Tmp_RT10"),
26 Shader
.PropertyToID("__Tmp_RT11"),
27 Shader
.PropertyToID("__Tmp_RT12"),
28 Shader
.PropertyToID("__Tmp_RT13")
31 readonly ComputeShader m_Shader
;
32 readonly int m_KEncodeFastCubemapMip
;
34 public EncodeBC6H(ComputeShader shader
)
36 Assert
.IsNotNull(shader
);
39 m_KEncodeFastCubemapMip
= m_Shader
.FindKernel("KEncodeFastCubemapMip");
42 m_Shader
.GetKernelThreadGroupSizes(m_KEncodeFastCubemapMip
, out x
, out y
, out z
);
45 // Only use mode11 of BC6H encoding
47 /// Encode a Cubemap in BC6H.
49 /// It will encode all faces and selected mips of the Cubemap.
51 /// It uses only mode 11 of BC6H.
53 /// <param name="cmb">Command buffer for execution</param>
54 /// <param name="source">The source Cubemap</param>
55 /// <param name="sourceSize">The size of the source Cubemap</param>
56 /// <param name="target">The compressed texture.
57 /// It must be a BC6H Cubemap or Cubemap array with the same size as the source Cubemap</param>
58 /// <param name="fromMip">Starting mip to encode</param>
59 /// <param name="toMip">Last mip to encode</param>
60 /// <param name="targetArrayIndex">The index of the cubemap to store the compressed texture.
62 /// Only relevant when target is a CubemapArray</param>
63 public void EncodeFastCubemap(CommandBuffer cmb
, RenderTargetIdentifier source
, int sourceSize
, RenderTargetIdentifier target
, int fromMip
, int toMip
, int targetArrayIndex
= 0)
65 var maxMip
= Mathf
.Max(0, (int)(Mathf
.Log(sourceSize
) / Mathf
.Log(2)) - 2);
66 var actualFromMip
= (int)Mathf
.Clamp(fromMip
, 0, maxMip
);
67 var actualToMip
= (int)Mathf
.Min(maxMip
, Mathf
.Max(toMip
, actualFromMip
));
69 // Convert TextureCube source to Texture2DArray
70 var d
= new RenderTextureDescriptor
72 autoGenerateMips
= false,
74 colorFormat
= RenderTextureFormat
.ARGBInt
,
76 dimension
= TextureDimension
.Tex2DArray
,
77 enableRandomWrite
= true,
84 cmb
.SetComputeTextureParam(m_Shader
, m_KEncodeFastCubemapMip
, _Source
, source
);
86 for (var mip
= actualFromMip
; mip
<= actualToMip
; ++mip
)
88 var size
= (sourceSize
>> mip
) >> 2;
91 cmb
.GetTemporaryRT(__Tmp_RT
[mip
], d
);
94 for (var mip
= actualFromMip
; mip
<= actualToMip
; ++mip
)
96 var size
= (sourceSize
>> mip
) >> 2;
97 cmb
.SetComputeTextureParam(m_Shader
, m_KEncodeFastCubemapMip
, _Target
, __Tmp_RT
[mip
]);
98 cmb
.SetComputeIntParam(m_Shader
, _MipIndex
, mip
);
99 cmb
.DispatchCompute(m_Shader
, m_KEncodeFastCubemapMip
, size
, size
, 6);
102 var startSlice
= 6 * targetArrayIndex
;
103 for (var mip
= actualFromMip
; mip
<= actualToMip
; ++mip
)
105 var rtMip
= Mathf
.Clamp(mip
, actualFromMip
, actualToMip
);
106 for (var faceId
= 0; faceId
< 6; ++faceId
)
107 cmb
.CopyTexture(__Tmp_RT
[rtMip
], faceId
, 0, target
, startSlice
+ faceId
, mip
);
110 for (var mip
= actualFromMip
; mip
<= actualToMip
; ++mip
)
111 cmb
.ReleaseTemporaryRT(__Tmp_RT
[mip
]);
115 public static class BC6HExtensions
117 public static void BC6HEncodeFastCubemap(this CommandBuffer cmb
, RenderTargetIdentifier source
, int sourceSize
, RenderTargetIdentifier target
, int fromMip
, int toMip
, int targetArrayIndex
= 0)
119 EncodeBC6H
.DefaultInstance
.EncodeFastCubemap(cmb
, source
, sourceSize
, target
, fromMip
, toMip
, targetArrayIndex
);