-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cs
More file actions
323 lines (272 loc) · 9.95 KB
/
Graph.cs
File metadata and controls
323 lines (272 loc) · 9.95 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace TextureFlow
{
public class Graph
{
internal Shader _shader;
internal int _pass;
internal MaterialPropertyBlock _propertyBlock = new MaterialPropertyBlock();
internal Dictionary<string, Graph> _inputs = new Dictionary<string, Graph>();
internal int _downsample = 1;
internal Mesh _mesh;
internal RenderTextureDescriptor? _createTexture = null;
internal RenderTexture _textureReference;
private int _outputReferences = 0;
private RenderTexture _output;
#region Operations
public static Graph Downsample(Graph graph, PowOf2 pot = PowOf2._2)
{
Shader downsampling16 = Shader.Find("Hidden/TextureFlow/Downsampling16X");
Graph newGraph;
int di = (int)pot;
for (; di > 2; di >>= 2)
{
newGraph = new Graph()
{
_shader = downsampling16,
_downsample = 4,
};
((Property)graph).Apply(newGraph);
new Vec("_PixelOffset", 1, 1, true).Apply(newGraph);
graph = newGraph;
}
if (di == 2)
{
newGraph = new Graph()
{
_downsample = 2,
};
((Property)graph).Apply(newGraph);
graph = newGraph;
}
return graph;
}
public static Graph GaussianBlur(Graph graph, float pixels)
{
Vector4[] samples = TextureFlow.GaussianBlur.GetSamples(pixels);
int pass = Mathf.Clamp(samples.Length - 1, 0, 6);
Shader gaussianBlur = Shader.Find("Hidden/TextureFlow/GaussianBlur");
Graph newGraph;
string ss = "";
for (int i = 0; i < samples.Length; i++)
ss += samples[i].x.ToString("0.000") + "\t";
ss += "\n";
for (int i = 0; i < samples.Length; i++)
ss += samples[i].y.ToString("0.000") + "\t";
Debug.Log(ss);
newGraph = new Graph()
{
_shader = gaussianBlur,
_pass = pass
};
((Property)graph).Apply(newGraph);
new Vec("_Pixel", 1, 0, true).Apply(newGraph);
for (int i = 0; i < samples.Length; i++)
new Vec("_Sample" + i, samples[i]).Apply(newGraph);
graph = newGraph;
newGraph = new Graph()
{
_shader = gaussianBlur,
_pass = pass
};
((Property)graph).Apply(newGraph);
new Vec("_Pixel", 0, 1, true).Apply(newGraph);
for (int i = 0; i < samples.Length; i++)
new Vec("_Sample" + i, samples[i]).Apply(newGraph);
return newGraph;
}
public static Graph Create(int width,int height)
{
return Create(new RenderTextureDescriptor(width, height));
}
public static Graph Create(RenderTextureDescriptor descriptor)
{
return new Graph()
{
_createTexture = descriptor
};
}
public static Graph Use(RenderTexture texture)
{
return new Graph()
{
_textureReference = texture
};
}
public static Graph Process(Shader shader, int pass, params Property[] properties)
{
Graph graph = new Graph()
{
_shader = shader,
_pass = pass,
};
for (int i = 0; i < properties.Length; i++)
properties[i].Apply(graph);
return graph;
}
public static Graph Process(Shader shader, params Property[] properties)
{
return Process(shader, 0, properties);
}
public static Graph Clear(Graph graph, bool color, bool depth, bool stencil)
{
int pass = 0;
pass += color ? 1 : 0;
pass += depth ? 2 : 0;
pass += stencil ? 4 : 0;
Graph newGraph = new Graph()
{
_shader = Shader.Find("Hidden/InternalClear"),
_pass = pass,
};
((Property)graph).Apply(newGraph);
return newGraph;
}
#endregion
#region GraphAnalysis
private void FillQueue(List<Graph> queue)
{
++_outputReferences;
if (_outputReferences > 1)
return;
foreach (var pair in _inputs)
pair.Value.FillQueue(queue);
queue.Add(this);
}
private static void FillTextures(List<Graph> queue, TexturePool pool, bool drawOnScreen, out RenderTexture result)
{
for (int i = 0; i < queue.Count; i++)
{
Graph graph = queue[i];
if (graph._createTexture != null)
{
graph._output = new RenderTexture(graph._createTexture.Value);
continue;
}
if (graph._textureReference != null)
{
graph._output = graph._textureReference;
continue;
}
if (graph._inputs.Count == 0) throw new System.Exception("there's no inputs in the node and it doesn't create a texture");
int width = 0, height = 0;
foreach (var pair in graph._inputs)
{
width = pair.Value._output.width / graph._downsample;
height = pair.Value._output.height / graph._downsample;
break;
}
bool theLastOne = i == (queue.Count - 1);
graph._output = theLastOne && drawOnScreen ? null : pool.GetOrAdd(width, height);
foreach (var pair in graph._inputs)
{
graph._propertyBlock.SetTexture(pair.Key, pair.Value._output);
pair.Value._outputReferences--;
if (pair.Value._outputReferences == 0)
pool.Release(pair.Value._output);
}
}
result = queue[queue.Count - 1]._output;
}
private static void BuildCommandBuffer(List<Graph> queue, CommandBuffer cb)
{
RenderTexture previousTarget = null;
for (int i = 0; i < queue.Count; i++)
{
Graph graph = queue[i];
if (graph._createTexture != null || graph._textureReference != null)
continue;
if (previousTarget != graph._output)
cb.SetRenderTarget(previousTarget = graph._output);
Shader shader = graph._shader != null ? graph._shader : Shader.Find("Hidden/BlitCopy");
Mesh mesh = graph._mesh != null ? graph._mesh : MeshCreator.Quad;
cb.DrawMesh(mesh, Matrix4x4.identity, new Material(shader), 0, graph._pass, graph._propertyBlock);
}
}
private RenderTexture FillCommandBuffer(CommandBuffer commandBuffer, bool drawOnScreen)
{
List<Graph> queue = new List<Graph>();
FillQueue(queue);
RenderTexture result;
FillTextures(queue, new TexturePool(), drawOnScreen, out result);
commandBuffer.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.Ortho(0, 1, 0, 1, 0, 1));
BuildCommandBuffer(queue, commandBuffer);
return result;
}
public void FillCommandBuffer(CommandBuffer commandBuffer)
{
FillCommandBuffer(commandBuffer, true);
}
public void FillCommandBuffer(CommandBuffer commandBuffer, out RenderTexture result)
{
result = FillCommandBuffer(commandBuffer, false);
}
public CommandBuffer ToCommandBuffer()
{
CommandBuffer commandBuffer = new CommandBuffer();
FillCommandBuffer(commandBuffer, true);
return commandBuffer;
}
public CommandBuffer ToCommandBuffer(out RenderTexture result)
{
CommandBuffer commandBuffer = new CommandBuffer();
result = FillCommandBuffer(commandBuffer, false);
return commandBuffer;
}
#endregion
#region SyntacticSugar
public static implicit operator Property(Graph graph)
{
return new Input(graph);
}
public static implicit operator Graph(RenderTexture texture)
{
return new Graph()
{
_textureReference = texture
};
}
public Property As(string property)
{
return new Input(property, this);
}
public Graph Proces(Shader shader, int pass, params Property[] properties)
{
System.Array.Resize(ref properties, properties.Length + 1);
properties[properties.Length - 1] = this;
return Graph.Process(shader, pass, properties);
}
public Graph Proces(Shader shader, params Property[] properties)
{
return Proces(shader, 0, properties);
}
public Graph Downsample(PowOf2 pot = PowOf2._2)
{
return Graph.Downsample(this, pot);
}
public Graph Clear(bool color, bool depth, bool stencil)
{
return Graph.Clear(this, color, depth, stencil);
}
#endregion
}
public enum PowOf2
{
_2 = 2,
_4 = 4,
_8 = 8,
_16 = 16,
_32 = 32,
_64 = 64,
_128 = 128,
_256 = 256,
_512 = 512,
_1024 = 1024,
_2048 = 2048,
_4096 = 4096,
_8192 = 8192,
_16384 = 16384,
}
}