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