@@ -21,10 +21,12 @@ namespace UnityCustomTextureRenderer
2121 /// </summary>
2222 public sealed class NonBlockingCustomTextureRenderer : IDisposable
2323 {
24- static readonly string RenderShaderName = "NonBlockingCustomTextureRenderer" ;
25- static readonly string RawTextureDataPropertyName = "_RawTextureData" ;
26- static readonly string TextureWidthPropertyName = "_TextureWidth" ;
27- static readonly string TextureHeightPropertyName = "_TextureHeight" ;
24+ static readonly string ComputeShaderName = "NonBlockingCustomTextureRenderer" ;
25+ static readonly string KernelName = "LoadRawTextureDataRGBA32" ;
26+ static readonly string RawTextureDataPropertyName = "RawTextureData" ;
27+ static readonly string OutputTexturePropertyName = "OutputTexture" ;
28+ static readonly string TextureWidthPropertyName = "Width" ;
29+ static readonly string TextureHeightPropertyName = "Height" ;
2830
2931 UpdateRawTextureDataFunction _updateRawTextureDataFunction ;
3032
@@ -48,9 +50,12 @@ public sealed class NonBlockingCustomTextureRenderer : IDisposable
4850 readonly int _targetFrameTimeMilliseconds ;
4951 static readonly double TimestampsToTicks = TimeSpan . TicksPerSecond / ( double ) Stopwatch . Frequency ;
5052
51- readonly Material _renderMaterial ;
53+ readonly ComputeShader _computeShader ;
54+ readonly int _kernelIndex ;
55+ readonly Vector3Int _kernelThreads ;
5256
5357 readonly int _rawTextureDataPropertyId ;
58+ readonly int _outputTexturePropertyId ;
5459 readonly int _textureWidthPropertyId ;
5560 readonly int _textureHeightPropertyId ;
5661
@@ -87,14 +92,13 @@ public NonBlockingCustomTextureRenderer(UpdateRawTextureDataFunction updateRawTe
8792 return ;
8893 }
8994
90- var renderShader = Resources . Load < Shader > ( RenderShaderName ) ;
91- if ( renderShader is null )
95+ _computeShader = UnityEngine . Object . Instantiate ( Resources . Load < ComputeShader > ( ComputeShaderName ) ) ;
96+ if ( _computeShader is null )
9297 {
9398 _disposed = true ;
94- DebugLogError ( $ "[{ nameof ( NonBlockingCustomTextureRenderer ) } ] The shader '{ RenderShaderName } ' could not be found in 'Resources'.") ;
99+ DebugLogError ( $ "[{ nameof ( NonBlockingCustomTextureRenderer ) } ] The compute shader '{ ComputeShaderName } ' could not be found in 'Resources'.") ;
95100 return ;
96101 }
97- _renderMaterial = new Material ( renderShader ) ;
98102
99103 if ( targetTexture . format != RenderTextureFormat . ARGB32 )
100104 {
@@ -127,7 +131,15 @@ public NonBlockingCustomTextureRenderer(UpdateRawTextureDataFunction updateRawTe
127131 DebugLog ( $ "[{ nameof ( NonBlockingCustomTextureRenderer ) } ] Texture size: { _targetTexture . width } x{ _targetTexture . height } ") ;
128132 DebugLog ( $ "[{ nameof ( NonBlockingCustomTextureRenderer ) } ] Texture buffer size: { _targetTexture . width * _targetTexture . height * _bytesPerPixel } [Bytes]") ;
129133
134+ _kernelIndex = _computeShader . FindKernel ( KernelName ) ;
135+
136+ _computeShader . GetKernelThreadGroupSizes ( _kernelIndex , out uint numThreadsX , out uint numThreadsY , out uint numThreadsZ ) ;
137+ _kernelThreads . x = ( int ) numThreadsX ;
138+ _kernelThreads . y = ( int ) numThreadsY ;
139+ _kernelThreads . z = ( int ) numThreadsZ ;
140+
130141 _rawTextureDataPropertyId = Shader . PropertyToID ( RawTextureDataPropertyName ) ;
142+ _outputTexturePropertyId = Shader . PropertyToID ( OutputTexturePropertyName ) ;
131143 _textureWidthPropertyId = Shader . PropertyToID ( TextureWidthPropertyName ) ;
132144 _textureHeightPropertyId = Shader . PropertyToID ( TextureHeightPropertyName ) ;
133145
@@ -201,10 +213,16 @@ void Render()
201213 if ( _asyncGPUUploadFrame == _asyncGPUUploadCount )
202214#endif
203215 {
204- _renderMaterial . SetInt ( _textureWidthPropertyId , _textureWidth ) ;
205- _renderMaterial . SetInt ( _textureHeightPropertyId , _textureHeight ) ;
206- _renderMaterial . SetBuffer ( _rawTextureDataPropertyId , _rawTextureDataComputeBuffer ) ;
207- Graphics . Blit ( null , _targetTexture , _renderMaterial ) ;
216+ _computeShader . SetInt ( _textureWidthPropertyId , _textureWidth ) ;
217+ _computeShader . SetInt ( _textureHeightPropertyId , _textureHeight ) ;
218+ _computeShader . SetBuffer ( _kernelIndex , _rawTextureDataPropertyId , _rawTextureDataComputeBuffer ) ;
219+ _computeShader . SetTexture ( _kernelIndex , _outputTexturePropertyId , _targetTexture ) ;
220+
221+ int threadGroupsX = Mathf . CeilToInt ( ( float ) _textureWidth / _kernelThreads . x ) ;
222+ int threadGroupsY = Mathf . CeilToInt ( ( float ) _textureHeight / _kernelThreads . y ) ;
223+ _computeShader . Dispatch ( _kernelIndex , threadGroupsX , threadGroupsY , 1 ) ;
224+
225+ // DebugLog($"[{nameof(NonBlockingCustomTextureRenderer)}.Render] Dispatch the compute shader. Async GPU Upload Frame: {_asyncGPUUploadFrame}");
208226 }
209227 }
210228
0 commit comments