Skip to content

Commit 885fb26

Browse files
Map3DTo1D, Map1DTo3D, Map2DTo1D, Map1DTo2D, Map3DTo2D, Map2DTo3D
1 parent 0809039 commit 885fb26

File tree

2 files changed

+308
-0
lines changed

2 files changed

+308
-0
lines changed

DimensionConverter.compute

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#pragma kernel CSMain1
2+
#pragma kernel CSMain2
3+
#pragma kernel CSMain3
4+
#pragma kernel CSMain4
5+
#pragma kernel CSMain5
6+
#pragma kernel CSMain6
7+
8+
RWStructuredBuffer<uint> _ComputeBuffer1;
9+
RWStructuredBuffer<uint2> _ComputeBuffer2;
10+
RWStructuredBuffer<uint3> _ComputeBuffer3;
11+
uint _Size2, _Size3;
12+
13+
uint Map3DTo1D(uint3 coords, uint size3)
14+
{
15+
return uint(coords.z * size3 * size3 + coords.y * size3 + coords.x);
16+
}
17+
18+
uint3 Map1DTo3D(uint coords, uint size3)
19+
{
20+
return uint3(coords % size3, (coords / size3) % size3, coords / (size3 * size3));
21+
}
22+
23+
uint Map2DTo1D(uint2 coords, uint size2)
24+
{
25+
return uint(size2 * coords.y + coords.x);
26+
}
27+
28+
uint2 Map1DTo2D(uint coords, uint size2)
29+
{
30+
return uint2(coords % size2, coords / size2);
31+
}
32+
33+
uint2 Map3DTo2D(uint3 coords, uint size3, uint size2)
34+
{
35+
uint index = uint(coords.z * size3 * size3 + coords.y * size3 + coords.x);
36+
return uint2(index % size2, index / size2);
37+
}
38+
39+
uint3 Map2DTo3D(uint2 coords, uint size2, uint size3)
40+
{
41+
uint index = uint(size2 * coords.y + coords.x);
42+
return uint3(index % size3, (index / size3) % size3, index / (size3 * size3));
43+
}
44+
45+
[numthreads(8,1,1)]
46+
void CSMain1 (uint3 id : SV_DispatchThreadID)
47+
{
48+
uint coords = Map3DTo1D(_ComputeBuffer3[id.x], _Size3);
49+
_ComputeBuffer1[id.x] = coords;
50+
_ComputeBuffer3[id.x] = Map1DTo3D(coords, _Size3);
51+
}
52+
53+
[numthreads(8,1,1)]
54+
void CSMain2 (uint3 id : SV_DispatchThreadID)
55+
{
56+
uint3 coords = Map1DTo3D(_ComputeBuffer1[id.x], _Size3);
57+
_ComputeBuffer3[id.x] = coords;
58+
_ComputeBuffer1[id.x] = Map3DTo1D(coords, _Size3);
59+
}
60+
61+
[numthreads(8,1,1)]
62+
void CSMain3 (uint3 id : SV_DispatchThreadID)
63+
{
64+
uint coords = Map2DTo1D(_ComputeBuffer2[id.x], _Size2);
65+
_ComputeBuffer1[id.x] = coords;
66+
_ComputeBuffer2[id.x] = Map1DTo2D(coords, _Size2);
67+
}
68+
69+
[numthreads(8,1,1)]
70+
void CSMain4 (uint3 id : SV_DispatchThreadID)
71+
{
72+
uint2 coords = Map1DTo2D(_ComputeBuffer1[id.x], _Size2);
73+
_ComputeBuffer2[id.x] = coords;
74+
_ComputeBuffer1[id.x] = Map2DTo1D(coords, _Size2);
75+
}
76+
77+
[numthreads(8,1,1)]
78+
void CSMain5 (uint3 id : SV_DispatchThreadID)
79+
{
80+
uint2 coords = Map3DTo2D(_ComputeBuffer3[id.x], _Size3, _Size2);
81+
_ComputeBuffer2[id.x] = coords;
82+
_ComputeBuffer3[id.x] = Map2DTo3D(coords, _Size2, _Size3);
83+
}
84+
85+
[numthreads(8,1,1)]
86+
void CSMain6 (uint3 id : SV_DispatchThreadID)
87+
{
88+
uint3 coords = Map2DTo3D(_ComputeBuffer2[id.x], _Size2, _Size3);
89+
_ComputeBuffer3[id.x] = coords;
90+
_ComputeBuffer2[id.x] = Map3DTo2D(coords, _Size3, _Size2);
91+
}

DimensionConverter.cs

+217
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class DimensionConverter : MonoBehaviour
6+
{
7+
public ComputeShader CSShader;
8+
[Range(1, 6)]
9+
[Header(
10+
"4096x1 = 64x64 = 16x16x16 \n" +
11+
"1 - Test: Map3DTo1D => Map1DTo3D \n" +
12+
"2 - Test: Map1DTo3D => Map3DTo1D \n" +
13+
"3 - Test: Map2DTo1D => Map1DTo2D \n" +
14+
"4 - Test: Map1DTo2D => Map2DTo1D \n" +
15+
"5 - Test: Map3DTo2D => Map2DTo3D \n" +
16+
"6 - Test: Map2DTo3D => Map3DTo2D"
17+
)]
18+
public int Option = 1;
19+
20+
private int _Kernel1, _Kernel2, _Kernel3, _Kernel4, _Kernel5, _Kernel6;
21+
private const int _Count = 4096; // might be also 16, 46656 or 262144
22+
private int _Size2, _Size3;
23+
24+
void Test1D(int kernel, int result)
25+
{
26+
uint[] input = new uint[_Count];
27+
for (uint n = 0; n < _Count; n++) input[n] = n;
28+
ComputeBuffer buffer1 = new ComputeBuffer(_Count, 4 * 1);
29+
buffer1.SetData(input);
30+
CSShader.SetBuffer(kernel, "_ComputeBuffer1", buffer1);
31+
ComputeBuffer buffer2 = new ComputeBuffer(_Count, 4 * 2);
32+
CSShader.SetBuffer(kernel, "_ComputeBuffer2", buffer2);
33+
ComputeBuffer buffer3 = new ComputeBuffer(_Count, 4 * 3);
34+
CSShader.SetBuffer(kernel, "_ComputeBuffer3", buffer3);
35+
CSShader.Dispatch(kernel, _Count / 8, 1, 1);
36+
uint[] output = new uint[_Count];
37+
buffer1.GetData(output);
38+
Vector2Int[] result2 = new Vector2Int[_Count];
39+
buffer2.GetData(result2);
40+
Vector3Int[] result3 = new Vector3Int[_Count];
41+
buffer3.GetData(result3);
42+
buffer1.Release();
43+
buffer2.Release();
44+
buffer3.Release();
45+
for (uint i = 0; i < input.Length; i++)
46+
{
47+
string chars = (result == 2) ? result2[i].ToString() : result3[i].ToString();
48+
if (Mathf.Approximately(input[i], output[i]))
49+
{
50+
Debug.Log(input[i].ToString() + " => " + chars + " ### " + output[i].ToString());
51+
}
52+
else
53+
{
54+
Debug.LogError(input[i].ToString() + " => " + chars + " ### " + output[i].ToString());
55+
}
56+
}
57+
}
58+
59+
void Test2D(int kernel, int result)
60+
{
61+
Vector2Int[] input = new Vector2Int[_Count];
62+
int n = 0;
63+
for (int y = 0; y < _Size2; y++)
64+
{
65+
for (int x = 0; x < _Size2; x++)
66+
{
67+
input[n] = new Vector2Int(x, y);
68+
n++;
69+
}
70+
}
71+
ComputeBuffer buffer2 = new ComputeBuffer(_Count, 4 * 2);
72+
buffer2.SetData(input);
73+
CSShader.SetBuffer(kernel, "_ComputeBuffer2", buffer2);
74+
ComputeBuffer buffer1 = new ComputeBuffer(_Count, 4 * 1);
75+
CSShader.SetBuffer(kernel, "_ComputeBuffer1", buffer1);
76+
ComputeBuffer buffer3 = new ComputeBuffer(_Count, 4 * 3);
77+
CSShader.SetBuffer(kernel, "_ComputeBuffer3", buffer3);
78+
CSShader.Dispatch(kernel, _Count / 8, 1, 1);
79+
Vector2Int[] output = new Vector2Int[_Count];
80+
buffer2.GetData(output);
81+
uint[] result1 = new uint[_Count];
82+
buffer1.GetData(result1);
83+
Vector3Int[] result3 = new Vector3Int[_Count];
84+
buffer3.GetData(result3);
85+
buffer1.Release();
86+
buffer2.Release();
87+
buffer3.Release();
88+
for (uint i = 0; i < input.Length; i++)
89+
{
90+
bool a = Mathf.Approximately(input[i].x, output[i].x);
91+
bool b = Mathf.Approximately(input[i].y, output[i].y);
92+
string chars = (result == 1) ? result1[i].ToString() : result3[i].ToString();
93+
if (a && b)
94+
{
95+
Debug.Log(input[i].ToString() + " => " + chars + " ### " + output[i].ToString());
96+
}
97+
else
98+
{
99+
Debug.LogError(input[i].ToString() + " => " + chars + " ### " + output[i].ToString());
100+
}
101+
}
102+
}
103+
104+
void Test3D(int kernel, int result)
105+
{
106+
Vector3Int[] input = new Vector3Int[_Count];
107+
int n = 0;
108+
for (int z = 0; z < _Size3; z++)
109+
{
110+
for (int y = 0; y < _Size3; y++)
111+
{
112+
for (int x = 0; x < _Size3; x++)
113+
{
114+
input[n] = new Vector3Int(x, y, z);
115+
n++;
116+
}
117+
}
118+
}
119+
ComputeBuffer buffer3 = new ComputeBuffer(_Count, 4 * 3);
120+
buffer3.SetData(input);
121+
CSShader.SetBuffer(kernel, "_ComputeBuffer3", buffer3);
122+
ComputeBuffer buffer1 = new ComputeBuffer(_Count, 4 * 1);
123+
CSShader.SetBuffer(kernel, "_ComputeBuffer1", buffer1);
124+
ComputeBuffer buffer2 = new ComputeBuffer(_Count, 4 * 2);
125+
CSShader.SetBuffer(kernel, "_ComputeBuffer2", buffer2);
126+
CSShader.Dispatch(kernel, _Count / 8, 1, 1);
127+
Vector3Int[] output = new Vector3Int[_Count];
128+
buffer3.GetData(output);
129+
uint[] result1 = new uint[_Count];
130+
buffer1.GetData(result1);
131+
Vector2Int[] result2 = new Vector2Int[_Count];
132+
buffer2.GetData(result2);
133+
buffer1.Release();
134+
buffer2.Release();
135+
buffer3.Release();
136+
for (uint i = 0; i < input.Length; i++)
137+
{
138+
bool a = Mathf.Approximately(input[i].x, output[i].x);
139+
bool b = Mathf.Approximately(input[i].y, output[i].y);
140+
bool c = Mathf.Approximately(input[i].z, output[i].z);
141+
string chars = (result == 1) ? result1[i].ToString() : result2[i].ToString();
142+
if (a && b && c)
143+
{
144+
Debug.Log(input[i].ToString() + " => " + chars + " ### " + output[i].ToString());
145+
}
146+
else
147+
{
148+
Debug.LogError(input[i].ToString() + " => " + chars + " ### " + output[i].ToString());
149+
}
150+
}
151+
}
152+
153+
void Option1()
154+
{
155+
Test3D(_Kernel1, 1);
156+
}
157+
158+
void Option2()
159+
{
160+
Test1D(_Kernel2, 3);
161+
}
162+
163+
void Option3()
164+
{
165+
Test2D(_Kernel3, 1);
166+
}
167+
168+
void Option4()
169+
{
170+
Test1D(_Kernel4, 2);
171+
}
172+
173+
void Option5()
174+
{
175+
Test3D(_Kernel5, 2);
176+
}
177+
178+
void Option6()
179+
{
180+
Test2D(_Kernel6, 3);
181+
}
182+
183+
void Start()
184+
{
185+
_Size2 = Mathf.RoundToInt(Mathf.Pow((float)_Count, 1.0f / 2.0f));
186+
_Size3 = Mathf.RoundToInt(Mathf.Pow((float)_Count, 1.0f / 3.0f));
187+
CSShader.SetInt("_Size2", _Size2);
188+
CSShader.SetInt("_Size3", _Size3);
189+
_Kernel1 = CSShader.FindKernel("CSMain1");
190+
_Kernel2 = CSShader.FindKernel("CSMain2");
191+
_Kernel3 = CSShader.FindKernel("CSMain3");
192+
_Kernel4 = CSShader.FindKernel("CSMain4");
193+
_Kernel5 = CSShader.FindKernel("CSMain5");
194+
_Kernel6 = CSShader.FindKernel("CSMain6");
195+
switch (Option)
196+
{
197+
case 1:
198+
Option1();
199+
break;
200+
case 2:
201+
Option2();
202+
break;
203+
case 3:
204+
Option3();
205+
break;
206+
case 4:
207+
Option4();
208+
break;
209+
case 5:
210+
Option5();
211+
break;
212+
case 6:
213+
Option6();
214+
break;
215+
}
216+
}
217+
}

0 commit comments

Comments
 (0)