@@ -108,41 +108,28 @@ public void Dispose()
108
108
/// If <paramref name="src" /> format is different from <see cref="format" />, it converts the format.
109
109
/// </summary>
110
110
/// <remarks>
111
- /// After calling it, pixel data can't be read from CPU safely.
111
+ /// After calling it, pixel data can't be read on CPU safely.
112
+ /// To read pixel data on CPU, use <see cref="ReadTextureAsync" />.
112
113
/// </remarks>
113
- public bool ReadTextureOnGPU ( Texture src )
114
+ public void ReadTextureOnGPU ( Texture src , bool flipHorizontally , bool flipVertically )
114
115
{
115
- if ( GetTextureFormat ( src ) != format )
116
- {
117
- return Graphics . ConvertTexture ( src , _texture ) ;
118
- }
119
- Graphics . CopyTexture ( src , _texture ) ;
120
- return true ;
121
- }
116
+ ReadTextureInternal ( src , flipHorizontally , flipVertically ) ;
122
117
123
- public AsyncGPUReadbackRequest ReadTextureAsync ( Texture src )
124
- {
125
- if ( ! ReadTextureOnGPU ( src ) )
126
- {
127
- throw new InvalidOperationException ( "Failed to read texture on GPU" ) ;
128
- }
129
-
130
- return AsyncGPUReadback . Request ( _texture , 0 , ( req ) =>
131
- {
132
- if ( _texture == null )
133
- {
134
- return ;
135
- }
136
- _texture . LoadRawTextureData ( req . GetData < byte > ( ) ) ;
137
- _texture . Apply ( ) ;
138
- } ) ;
118
+ Graphics . CopyTexture ( _tmpRenderTexture , _texture ) ;
119
+ RenderTexture . ReleaseTemporary ( _tmpRenderTexture ) ;
139
120
}
140
121
141
122
/// <remarks>
142
123
/// This method is not thread-safe.
143
124
/// Avoid calling this method again before the returned <see cref="AsyncGPUReadbackRequest.done"/> is <see langword="true"/>.
144
125
/// </remarks>
145
126
public AsyncGPUReadbackRequest ReadTextureAsync ( Texture src , bool flipHorizontally , bool flipVertically )
127
+ {
128
+ ReadTextureInternal ( src , flipHorizontally , flipVertically ) ;
129
+ return AsyncGPUReadback . Request ( _tmpRenderTexture , 0 , _onReadBackRenderTexture ) ;
130
+ }
131
+
132
+ private void ReadTextureInternal ( Texture src , bool flipHorizontally , bool flipVertically )
146
133
{
147
134
var graphicsFormat = GraphicsFormatUtility . GetGraphicsFormat ( format , true ) ;
148
135
_tmpRenderTexture = RenderTexture . GetTemporary ( src . width , src . height , 32 , graphicsFormat ) ;
@@ -163,8 +150,6 @@ public AsyncGPUReadbackRequest ReadTextureAsync(Texture src, bool flipHorizontal
163
150
}
164
151
Graphics . Blit ( src , _tmpRenderTexture , scale , offset ) ;
165
152
RenderTexture . active = currentRenderTexture ;
166
-
167
- return AsyncGPUReadback . Request ( _tmpRenderTexture , 0 , _onReadBackRenderTexture ) ;
168
153
}
169
154
170
155
private readonly Action < AsyncGPUReadbackRequest > _onReadBackRenderTexture ;
0 commit comments