Skip to content

Commit d1703d2

Browse files
committed
Revert "Merge branch 'develop'"
This reverts commit 179c18f, reversing changes made to 7324651.
1 parent 179c18f commit d1703d2

File tree

8 files changed

+74
-67
lines changed

8 files changed

+74
-67
lines changed

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.2.0",
3+
"version": "1.0.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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ UnityCustomTextureRenderer.Samples.Test.Update () (at <0000000000000000000000000
3838
// manifest.json
3939
{
4040
"dependencies": {
41-
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.2.0",
42-
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.2.0",
41+
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.1.0",
42+
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.1.0",
4343
...
4444
}
4545
}
4646
```
4747

4848
## References
49-
- [Unity Shader Programming Vol.06 - 第4章 Compute/Structured Buffer](https://xjine.booth.pm/items/2813518)
50-
- [UnityShaderProgramming_06_Sample/04_StructuredBuffer](https://github.com/XJINE/UnityShaderProgramming_06_Sample/tree/main/Assets/04_StructuredBuffer)
5149
- [UnityグラフィックスAPI総点検!〜最近こんなの増えてました〜 - Unityステーション](https://youtu.be/7tjycAEMJNg?t=3197)
5250
- https://github.com/keijiro/TextureUpdateExample
5351

Assets/CustomTextureRenderer/Runtime/NonBlockingCustomTextureRenderer.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma kernel LoadRawTextureDataRGBA32
2+
3+
#define NUM_THREADS_X 16
4+
#define NUM_THREADS_Y 16
5+
6+
StructuredBuffer<uint> RawTextureData;
7+
RWTexture2D<float4> OutputTexture;
8+
9+
cbuffer params
10+
{
11+
uint Width;
12+
uint Height;
13+
};
14+
15+
bool IsValidPixelPosition(uint2 p, uint width, uint height)
16+
{
17+
return (p.x >= 0 && p.x < width && p.y >= 0 && p.y < height);
18+
}
19+
20+
[numthreads(NUM_THREADS_X, NUM_THREADS_Y, 1)]
21+
void LoadRawTextureDataRGBA32 (uint2 id : SV_DispatchThreadID)
22+
{
23+
if (!IsValidPixelPosition(id, Width, Height)) { return; }
24+
25+
int index = id.y * Width + id.x;
26+
int pixel = RawTextureData[index];
27+
28+
float red = (pixel & 0x000000ff) / 255.0;
29+
float green = ((pixel & 0x0000ff00) >> 8) / 255.0;
30+
float blue = ((pixel & 0x00ff0000) >> 16) / 255.0;
31+
float alpha = ((pixel & 0xff000000) >> 24) / 255.0;
32+
33+
OutputTexture[id.xy] = float4(red, green, blue, alpha);
34+
}
Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CustomTextureRenderer/Runtime/Resources/NonBlockingCustomTextureRenderer.shader

Lines changed: 0 additions & 40 deletions
This file was deleted.

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.0.0",
44
"displayName": "CustomTextureRenderer",
55
"description": "A graphics utility to update textures from native plugins.",
66
"license": "MIT",

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ UnityCustomTextureRenderer.Samples.Test.Update () (at <0000000000000000000000000
3838
// manifest.json
3939
{
4040
"dependencies": {
41-
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.2.0",
42-
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.2.0",
41+
"jp.sotanmochi.unitycustomtexturerenderer": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer#v1.1.0",
42+
"jp.sotanmochi.unitycustomtexturerenderer.samples": "https://github.com/sotanmochi/UnityCustomTextureRenderer.git?path=Assets/CustomTextureRenderer.Samples#v1.1.0",
4343
...
4444
}
4545
}
4646
```
4747

4848
## References
49-
- [Unity Shader Programming Vol.06 - 第4章 Compute/Structured Buffer](https://xjine.booth.pm/items/2813518)
50-
- [UnityShaderProgramming_06_Sample/04_StructuredBuffer](https://github.com/XJINE/UnityShaderProgramming_06_Sample/tree/main/Assets/04_StructuredBuffer)
5149
- [UnityグラフィックスAPI総点検!〜最近こんなの増えてました〜 - Unityステーション](https://youtu.be/7tjycAEMJNg?t=3197)
5250
- https://github.com/keijiro/TextureUpdateExample
5351

0 commit comments

Comments
 (0)