Skip to content

Commit e05f6c6

Browse files
committed
Merge branch 'main' into develop
2 parents 2a653bd + 7d9bbaa commit e05f6c6

File tree

8 files changed

+123
-55
lines changed

8 files changed

+123
-55
lines changed

Assets/CustomTextureRenderer.Samples/Scripts/Test.cs

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Runtime.InteropServices;
34
using UnityEngine;
45

@@ -36,16 +37,26 @@ enum PluginType
3637
[SerializeField] TextureSize _textureSize;
3738
[SerializeField] PluginType _pluginType;
3839

39-
public event Action<(int TextureWidth, int TextureHeight)> OnInitialized;
40+
public event Action<(int TextureWidth, int TextureHeight)> OnUpdateTexture;
4041

41-
uint _frame;
42+
MaterialPropertyBlock _prop;
43+
Renderer _renderer;
44+
45+
SynchronizationContext _unityMainThreadContext;
4246

43-
Texture2D _texture;
47+
uint _frame;
48+
int _rendererId;
49+
int _currentTextureSize;
4450
PluginTextureRenderer _pluginTextureRenderer;
4551

4652
void Start()
4753
{
48-
var size = _textureSize switch
54+
_prop = new MaterialPropertyBlock();
55+
_renderer = GetComponent<Renderer>();
56+
57+
_unityMainThreadContext = SynchronizationContext.Current;
58+
59+
_currentTextureSize = _textureSize switch
4960
{
5061
TextureSize._64x64 => 64,
5162
TextureSize._128x128 => 128,
@@ -57,39 +68,42 @@ void Start()
5768
_ => 64,
5869
};
5970

60-
_texture = new Texture2D(size, size, TextureFormat.RGBA32, false);
61-
_texture.wrapMode = TextureWrapMode.Clamp;
62-
6371
if (_pluginType is PluginType.Type1)
6472
{
6573
var callback = Marshal.GetDelegateForFunctionPointer<IssuePluginCustomTextureUpdateCallback>(GetTextureUpdateCallback());
66-
_pluginTextureRenderer = new PluginTextureRenderer(callback, _texture);
67-
CustomTextureRenderSystem.Instance.AddRenderer(_pluginTextureRenderer);
74+
_pluginTextureRenderer = new PluginTextureRenderer(callback, _currentTextureSize, _currentTextureSize);
75+
_rendererId = CustomTextureRenderSystem.Instance.AddRenderer(_pluginTextureRenderer);
6876
}
6977
else if (_pluginType is PluginType.Type2)
7078
{
71-
_pluginTextureRenderer = new PluginTextureRenderer(UpdateRawTextureDataCallback, _texture);
72-
CustomTextureRenderSystem.Instance.AddRenderer(_pluginTextureRenderer);
79+
_pluginTextureRenderer = new PluginTextureRenderer(UpdateRawTextureDataCallback, _currentTextureSize, _currentTextureSize);
80+
_rendererId = CustomTextureRenderSystem.Instance.AddRenderer(_pluginTextureRenderer);
7381
}
7482

7583
// Set the texture to the renderer with using a property block.
76-
var prop = new MaterialPropertyBlock();
77-
prop.SetTexture("_MainTex", _texture);
78-
GetComponent<Renderer>().SetPropertyBlock(prop);
79-
80-
OnInitialized?.Invoke((size, size));
81-
}
84+
_prop.SetTexture("_MainTex", _pluginTextureRenderer.TargetTexture);
85+
_renderer.SetPropertyBlock(_prop);
8286

83-
void OnDestroy()
84-
{
85-
Destroy(_texture);
87+
OnUpdateTexture?.Invoke((_currentTextureSize, _currentTextureSize));
8688
}
8789

8890
void Update()
8991
{
9092
_frame = (uint)(Time.time * 60);
9193
_pluginTextureRenderer.SetUserData(_frame);
9294

95+
_currentTextureSize = _textureSize switch
96+
{
97+
TextureSize._64x64 => 64,
98+
TextureSize._128x128 => 128,
99+
TextureSize._256x256 => 256,
100+
TextureSize._512x512 => 512,
101+
TextureSize._1024x1024 => 1024,
102+
TextureSize._2048x2048 => 2048,
103+
TextureSize._4096x4096 => 4096,
104+
_ => 64,
105+
};
106+
93107
// Rotation
94108
transform.eulerAngles = new Vector3(10, 20, 30) * Time.time;
95109
}
@@ -103,6 +117,27 @@ void Update()
103117
/// <param name="frameCount"></param>
104118
void UpdateRawTextureDataCallback(IntPtr data, int width, int height, int bytesPerPixel)
105119
{
120+
if (_pluginType is PluginType.Type2
121+
&& (_currentTextureSize != width || _currentTextureSize != height))
122+
{
123+
_unityMainThreadContext.Send(_ =>
124+
{
125+
CustomTextureRenderSystem.Instance.RemoveRenderer((ushort)_rendererId);
126+
127+
data = _pluginTextureRenderer.CreateTextureBuffer(_currentTextureSize, _currentTextureSize);
128+
width = _currentTextureSize;
129+
height = _currentTextureSize;
130+
131+
_rendererId = CustomTextureRenderSystem.Instance.AddRenderer(_pluginTextureRenderer);
132+
133+
// Set the texture to the renderer with using a property block.
134+
_prop.SetTexture("_MainTex", _pluginTextureRenderer.TargetTexture);
135+
_renderer.SetPropertyBlock(_prop);
136+
137+
OnUpdateTexture?.Invoke((_currentTextureSize, _currentTextureSize));
138+
}, null);
139+
}
140+
106141
UpdateRawTextureData(data, width, height, _frame);
107142
}
108143
}

Assets/CustomTextureRenderer.Samples/Scripts/TestPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void Awake()
1313
{
1414
_uiView.SetGraphicsAPI(SystemInfo.graphicsDeviceType.ToString());
1515

16-
_test.OnInitialized += (values) =>
16+
_test.OnUpdateTexture += (values) =>
1717
{
1818
var title = nameof(UnityCustomTextureRenderer.CustomTextureRenderSystem);
1919
_uiView.SetTitle(title);

Assets/CustomTextureRenderer.Samples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jp.sotanmochi.unitycustomtexturerenderer.samples",
3-
"version": "1.1.1",
3+
"version": "1.3.0",
44
"displayName": "CustomTextureRenderer.Samples",
55
"description": "A graphics utility to update textures from native plugins.",
66
"license": "MIT",

Assets/CustomTextureRenderer/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ The function for updating textures runs on another thread.
1414
// manifest.json
1515
{
1616
"dependencies": {
17-
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.2.0",
18-
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.2.0",
17+
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.3.0 ",
18+
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.3.0",
1919
...
2020
}
2121
}

Assets/CustomTextureRenderer/Runtime/CustomTextureRenderSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public int AddRenderer(PluginTextureRenderer renderer)
130130
public void RemoveRenderer(ushort rendererId)
131131
{
132132
s_TextureRenderers.TryRemove(rendererId, out PluginTextureRenderer renderer);
133+
s_TextureBufferPtrs.TryRemove(rendererId, out IntPtr bufferPtr);
133134
}
134135

135136
private void SystemUpdate()

Assets/CustomTextureRenderer/Runtime/PluginTextureRenderer.cs

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public sealed class PluginTextureRenderer : IDisposable
2525
private IntPtr _textureBufferPtr;
2626
private bool _updated;
2727

28-
private readonly int _textureWidth;
29-
private readonly int _textureHeight;
28+
private int _textureWidth;
29+
private int _textureHeight;
3030
private readonly int _bytesPerPixel = 4; // RGBA32. 1 byte (8 bits) per channel.
3131

3232
private readonly Thread _pluginRenderThread;
@@ -48,60 +48,50 @@ public sealed class PluginTextureRenderer : IDisposable
4848
private CustomSampler _textureUpdateLoopSampler;
4949
#endif
5050

51-
public PluginTextureRenderer(RawTextureDataUpdateCallback callback, Texture2D targetTexture, bool autoDispose = true)
51+
public PluginTextureRenderer(RawTextureDataUpdateCallback callback, int textureWidth, int textureHeight,
52+
int targetFrameRateOfPluginRenderThread = 60, bool autoDispose = true)
5253
{
5354
#if DEVELOPMENT_BUILD || UNITY_EDITOR
5455
_textureUpdateLoopSampler = CustomSampler.Create("RawTextureDataUpdateFunction");
5556
#endif
5657

5758
_loopAction = RawTextureDataUpdate;
5859
_rawTextureDataUpdateCallback = callback;
60+
DebugLog($"[{nameof(PluginTextureRenderer)}] The RawTextureDataUpdateCallback is \n'{_rawTextureDataUpdateCallback.Target}.{_rawTextureDataUpdateCallback.Method.Name}'.");
5961

60-
_textureBuffer = new uint[targetTexture.width * targetTexture.height];
61-
_textureBufferHandle = GCHandle.Alloc(_textureBuffer, GCHandleType.Pinned);
62-
_textureBufferPtr = _textureBufferHandle.AddrOfPinnedObject();
63-
64-
if (targetTexture.format != TextureFormat.RGBA32)
65-
{
66-
_disposed = true;
67-
DebugLogError($"[{nameof(PluginTextureRenderer)}] Unsupported texture format: {targetTexture.format}");
68-
return;
69-
}
62+
CreateTextureBuffer(textureWidth, textureHeight);
7063

7164
if (autoDispose){ UnityEngine.Application.quitting += Dispose; }
7265

73-
_targetTexture = targetTexture;
74-
_textureWidth = targetTexture.width;
75-
_textureHeight = targetTexture.height;
66+
_targetFrameTimeMilliseconds = (int)(1000.0f / targetFrameRateOfPluginRenderThread);
67+
DebugLog($"[{nameof(PluginTextureRenderer)}] Target frame rate: {targetFrameRateOfPluginRenderThread}");
68+
DebugLog($"[{nameof(PluginTextureRenderer)}] Target frame time milliseconds: {_targetFrameTimeMilliseconds}");
7669

7770
_cts = new CancellationTokenSource();
7871
_pluginRenderThread = new Thread(PluginRenderThread);
7972
_pluginRenderThread.Start();
8073
}
8174

82-
public PluginTextureRenderer(IssuePluginCustomTextureUpdateCallback callback, Texture2D targetTexture, bool autoDispose = true)
75+
public PluginTextureRenderer(IssuePluginCustomTextureUpdateCallback callback, int textureWidth, int textureHeight,
76+
int targetFrameRateOfPluginRenderThread = 60, bool autoDispose = true)
8377
{
8478
#if DEVELOPMENT_BUILD || UNITY_EDITOR
8579
_textureUpdateLoopSampler = CustomSampler.Create("CustomTextureUpdateFunction");
8680
#endif
8781

8882
_loopAction = IssuePluginCustomTextureUpdate;
8983
_customTextureUpdateCallback = callback;
84+
DebugLog($"[{nameof(PluginTextureRenderer)}] The CustomTextureUpdateCallback is \n'{_customTextureUpdateCallback.Target}.{_customTextureUpdateCallback.Method.Name}'.");
9085

9186
_textureUpdateParamsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_textureUpdateParams));
9287

93-
if (targetTexture.format != TextureFormat.RGBA32)
94-
{
95-
_disposed = true;
96-
DebugLogError($"[{nameof(PluginTextureRenderer)}] Unsupported texture format: {targetTexture.format}");
97-
return;
98-
}
88+
CreateTextureBuffer(textureWidth, textureHeight);
9989

10090
if (autoDispose){ UnityEngine.Application.quitting += Dispose; }
10191

102-
_targetTexture = targetTexture;
103-
_textureWidth = targetTexture.width;
104-
_textureHeight = targetTexture.height;
92+
_targetFrameTimeMilliseconds = (int)(1000.0f / targetFrameRateOfPluginRenderThread);
93+
DebugLog($"[{nameof(PluginTextureRenderer)}] Target frame rate: {targetFrameRateOfPluginRenderThread}");
94+
DebugLog($"[{nameof(PluginTextureRenderer)}] Target frame time milliseconds: {_targetFrameTimeMilliseconds}");
10595

10696
_cts = new CancellationTokenSource();
10797
_pluginRenderThread = new Thread(PluginRenderThread);
@@ -136,9 +126,46 @@ public void Dispose()
136126
DebugLog($"[{nameof(PluginTextureRenderer)}] Disposed");
137127
}
138128

139-
public void SetUserData(uint userData)
129+
/// <summary>
130+
/// Runs on Unity main thread.
131+
/// </summary>
132+
/// <param name="width"></param>
133+
/// <param name="height"></param>
134+
/// <returns></returns>
135+
public IntPtr CreateTextureBuffer(int width, int height)
140136
{
141-
_userData = userData;
137+
_updated = false;
138+
139+
_textureBufferPtr = IntPtr.Zero;
140+
Marshal.FreeHGlobal(_textureUpdateParamsPtr);
141+
142+
if (_textureBuffer != null)
143+
{
144+
_textureBuffer = null;
145+
_textureBufferHandle.Free();
146+
}
147+
148+
UnityEngine.Object.Destroy(_targetTexture);
149+
150+
_targetTexture = new Texture2D(width, height, TextureFormat.RGBA32, false);
151+
_textureWidth = width;
152+
_textureHeight = height;
153+
154+
if (_rawTextureDataUpdateCallback != null)
155+
{
156+
_textureBuffer = new uint[_targetTexture.width * _targetTexture.height];
157+
_textureBufferHandle = GCHandle.Alloc(_textureBuffer, GCHandleType.Pinned);
158+
_textureBufferPtr = _textureBufferHandle.AddrOfPinnedObject();
159+
}
160+
161+
if (_customTextureUpdateCallback != null)
162+
{
163+
_textureUpdateParamsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_textureUpdateParams));
164+
}
165+
166+
DebugLog($"[{nameof(PluginTextureRenderer)}] Create texture: {_textureWidth}x{_textureHeight} [pixels]");
167+
168+
return _textureBufferPtr;
142169
}
143170

144171
public IntPtr GetTextureBufferPtr()
@@ -154,6 +181,11 @@ public IntPtr GetTextureBufferPtr()
154181
}
155182
}
156183

184+
public void SetUserData(uint userData)
185+
{
186+
_userData = userData;
187+
}
188+
157189
/// <summary>
158190
/// Runs on another thread.
159191
/// </summary>

Assets/CustomTextureRenderer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jp.sotanmochi.unitycustomtexturerenderer",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"displayName": "CustomTextureRenderer",
55
"description": "A graphics utility to update textures from native plugins.",
66
"license": "MIT",

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ The function for updating textures runs on another thread.
1414
// manifest.json
1515
{
1616
"dependencies": {
17-
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.2.0",
18-
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.2.0",
17+
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.3.0",
18+
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.3.0",
1919
...
2020
}
2121
}

0 commit comments

Comments
 (0)