Skip to content

Commit d3114f0

Browse files
committed
Update
1 parent ba9ba34 commit d3114f0

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

Assets/CustomTextureRenderer/Runtime/PluginTextureRenderer.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public sealed class PluginTextureRenderer : IDisposable
2929

3030
private int _textureWidth;
3131
private int _textureHeight;
32+
private TextureFormat _textureFormat = TextureFormat.RGBA32;
3233
private readonly int _bytesPerPixel = 4; // RGBA32. 1 byte (8 bits) per channel.
34+
private readonly ColorSpace _activeColorSpace;
3335

3436
private readonly Thread _pluginRenderThread;
3537
private readonly Action _loopAction;
@@ -51,18 +53,19 @@ public sealed class PluginTextureRenderer : IDisposable
5153
#endif
5254

5355
public PluginTextureRenderer(RawTextureDataUpdateCallback callback,
54-
int textureWidth, int textureHeight, int bufferSize = 0,
56+
int textureWidth, int textureHeight, int bufferSize = 0, TextureFormat format = TextureFormat.RGBA32,
5557
int targetFrameRateOfPluginRenderThread = 60, bool autoDispose = true)
5658
{
5759
#if DEVELOPMENT_BUILD || UNITY_EDITOR
5860
_textureUpdateLoopSampler = CustomSampler.Create("RawTextureDataUpdateFunction");
5961
#endif
62+
_activeColorSpace = UnityEngine.QualitySettings.activeColorSpace;
6063

6164
_loopAction = RawTextureDataUpdate;
6265
_rawTextureDataUpdateCallback = callback;
6366
DebugLog($"[{nameof(PluginTextureRenderer)}] The RawTextureDataUpdateCallback is \n'{_rawTextureDataUpdateCallback.Target}.{_rawTextureDataUpdateCallback.Method.Name}'.");
6467

65-
CreateTextureBuffer(textureWidth, textureHeight, bufferSize);
68+
CreateTextureBuffer(textureWidth, textureHeight, bufferSize, format);
6669

6770
if (autoDispose){ UnityEngine.Application.quitting += Dispose; }
6871

@@ -75,20 +78,22 @@ public PluginTextureRenderer(RawTextureDataUpdateCallback callback,
7578
_pluginRenderThread.Start();
7679
}
7780

78-
public PluginTextureRenderer(IssuePluginCustomTextureUpdateCallback callback, int textureWidth, int textureHeight,
81+
public PluginTextureRenderer(IssuePluginCustomTextureUpdateCallback callback,
82+
int textureWidth, int textureHeight, int bufferSize = 0, TextureFormat format = TextureFormat.RGBA32,
7983
int targetFrameRateOfPluginRenderThread = 60, bool autoDispose = true)
8084
{
8185
#if DEVELOPMENT_BUILD || UNITY_EDITOR
8286
_textureUpdateLoopSampler = CustomSampler.Create("CustomTextureUpdateFunction");
8387
#endif
88+
_activeColorSpace = UnityEngine.QualitySettings.activeColorSpace;
8489

8590
_loopAction = IssuePluginCustomTextureUpdate;
8691
_customTextureUpdateCallback = callback;
8792
DebugLog($"[{nameof(PluginTextureRenderer)}] The CustomTextureUpdateCallback is \n'{_customTextureUpdateCallback.Target}.{_customTextureUpdateCallback.Method.Name}'.");
8893

8994
_textureUpdateParamsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_textureUpdateParams));
9095

91-
CreateTextureBuffer(textureWidth, textureHeight);
96+
CreateTextureBuffer(textureWidth, textureHeight, bufferSize, format);
9297

9398
if (autoDispose){ UnityEngine.Application.quitting += Dispose; }
9499

@@ -135,17 +140,18 @@ public void Dispose()
135140
/// <param name="width"></param>
136141
/// <param name="height"></param>
137142
/// <returns></returns>
138-
public IntPtr CreateTextureBuffer(int width, int height, int bufferSize = 0)
143+
public IntPtr CreateTextureBuffer(int width, int height, int bufferSize = 0, TextureFormat format = TextureFormat.RGBA32)
139144
{
140145
_updated = false;
141146

142147
if (_textureWidth != width || _textureHeight != height)
143148
{
144149
UnityEngine.Object.Destroy(_targetTexture);
145150

146-
_targetTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
151+
_targetTexture = new Texture2D(width, height, format, false);
147152
_textureWidth = width;
148153
_textureHeight = height;
154+
_textureFormat = format;
149155
}
150156

151157
if (_rawTextureDataUpdateCallback != null)
@@ -214,6 +220,25 @@ unsafe void IssuePluginCustomTextureUpdate()
214220
_textureUpdateParams.bpp = (uint)_bytesPerPixel;
215221
_textureUpdateParams.userData = _userData;
216222

223+
if (_activeColorSpace is ColorSpace.Linear)
224+
{
225+
_textureUpdateParams.format = _textureFormat switch
226+
{
227+
TextureFormat.RGBA32 => UnityRenderingExtTextureFormat.kUnityRenderingExtFormatR8G8B8A8_SRGB,
228+
TextureFormat.BGRA32 => UnityRenderingExtTextureFormat.kUnityRenderingExtFormatB8G8R8A8_SRGB,
229+
_ => UnityRenderingExtTextureFormat.kUnityRenderingExtFormatR8G8B8A8_SRGB,
230+
};
231+
}
232+
else
233+
{
234+
_textureUpdateParams.format = _textureFormat switch
235+
{
236+
TextureFormat.RGBA32 => UnityRenderingExtTextureFormat.kUnityRenderingExtFormatR8G8B8A8_UNorm,
237+
TextureFormat.BGRA32 => UnityRenderingExtTextureFormat.kUnityRenderingExtFormatB8G8R8A8_UNorm,
238+
_ => UnityRenderingExtTextureFormat.kUnityRenderingExtFormatR8G8B8A8_UNorm,
239+
};
240+
}
241+
217242
var eventId = (int)UnityRenderingExtEventType.kUnityRenderingExtEventUpdateTextureBeginV2;
218243
Marshal.StructureToPtr(_textureUpdateParams, _textureUpdateParamsPtr, false);
219244

0 commit comments

Comments
 (0)