-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGraphicsDrawBaseMono.cs
257 lines (167 loc) · 5.92 KB
/
GraphicsDrawBaseMono.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
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
using Unity.Collections;
using UnityEngine;
using static GraphicsFactory;
[ExecuteInEditMode]
[DefaultExecutionOrder(1000)]
public class GraphicsDrawBaseMono : MonoBehaviour
{
public virtual Bounds CalculateBounds() => throw new System.NotImplementedException();
public virtual void CalculateMatricies() => throw new System.NotImplementedException();
public virtual bool allowExecInEditMode => true;
bool canExecNow => Application.isPlaying || allowExecInEditMode;
protected Bounds _bounds;
protected ComputeBuffer _instancesBuffer;
protected ComputeBuffer _argsBuffer;
internal NativeArray<InstanceMatrix> _matricies;
internal NativeArray<InstanceMatrix> _matriciesCulled;
protected MaterialPropertyBlock _matPropBlock;
[Tooltip("Will run the transform calcualtions every frame, use this if you plan to move or rotate this drawer")]
public bool alwaysUpdate = true;
public int count = 1000;
public DebugParams debugParams;
public int drawCount { get; protected set; } = 1000;
public TransformData transformation => new TransformData
{
position = transform.position,
rotation = transform.rotation,
scale = transform.lossyScale
};
public DrawParamsII drawParams;
public virtual bool ParamsAreValid()
{
if( drawParams == null || ! drawParams.IsValid() ) return false;
return true;
}
public virtual bool DataIsValid()
{
if ( ! _matricies.IsCreated ) return false;
return true;
}
protected bool initialized = false;
protected bool isEnabled = false;
public bool active => isActiveAndEnabled && initialized && isEnabled && ParamsAreValid();
public virtual void OnEnable()
{
if( ! canExecNow ) return;
GraphicsDrawStats.Add( this );
isEnabled = true;
if( debugParams == null ) debugParams = new DebugParams();
if( typeof( GraphicsDrawBaseMono ) == this.GetType() )
{
Debug.LogWarning("GraphicsDrawBaseMono is a base class and cannot be used on its own");
if( Application.isPlaying ) Destroy( this );
else DestroyImmediate( this );
return;
}
if ( debugParams.showLogs ) Debug.Log("enabled");
SetParams( drawParams );
}
public void SetParams( DrawParamsII value )
{
this.drawParams = value;
BufferInit();
BufferUpdate();
}
// for example if count is 888 and value is 256 , then set new count to 1024 to have some extra space
static int bufferExtendValue = 256;
void BufferInit()
{
if ( ! ParamsAreValid() ) return;
DataPreInit();
var t = Time.realtimeSinceStartup;
int resized = ( 1 + count / bufferExtendValue ) * bufferExtendValue;
if ( _matricies.IsCreated ) _matricies.Dispose();
_matricies = new NativeArray<InstanceMatrix>( resized , Allocator.Persistent );
if( _matriciesCulled.IsCreated ) _matriciesCulled.Dispose();
_matriciesCulled = new NativeArray<InstanceMatrix>( resized , Allocator.Persistent );
if (_instancesBuffer != null) _instancesBuffer.Release();
_instancesBuffer = new ComputeBuffer( resized , InstanceMatrix.Size() );
if( _matPropBlock == null ) _matPropBlock = new MaterialPropertyBlock();
_matPropBlock.SetBuffer( "_PerInstanceMatrix", _instancesBuffer );
DataInit( resized );
initialized = true;
debugParams.execTime = ( ( Time.realtimeSinceStartup - t ) * 1000f );
debugParams.benchmark = debugParams.execTime.ToString("N3") + " ms";
if ( debugParams.showLogs ) Debug.Log( "buffer init " + debugParams.benchmark );
}
void BufferUpdate() // OnValidate , Update , OnEnable
{
if ( ! initialized || ! ParamsAreValid() || ! DataIsValid() ) return;
var t = Time.realtimeSinceStartup;
_bounds = CalculateBounds();
CalculateMatricies();
GraphicsBurst.FillNaNs.Run( _matricies , drawCount = count );
if( drawParams.cullingEnabled ) drawCount = GraphicsBurst.Cull.Run( this );
OnPostCull();
// drawCount = PostCull( drawParams.cullingEnabled ? _matriciesCulled : _matricies , drawCount );
SetBuffer( drawParams.mesh, drawCount, ref _argsBuffer );
_instancesBuffer.SetData( drawParams.cullingEnabled ? _matriciesCulled : _matricies );
DataUpdate();
debugParams.benchmark = ( ( Time.realtimeSinceStartup - t ) * 1000f ).ToString("N3") + " ms";
}
protected virtual void DataPreInit() { }
protected virtual void DataInit(int size) { }
protected virtual void DataUpdate() { }
protected virtual void DataDispose() { }
protected virtual void OnPostCull() { }
// Override this method to do additional culling
protected virtual int PostCull( NativeArray<InstanceMatrix> array, int count ) => count;
protected virtual void OnValidate()
{
if( ! isEnabled || ! isActiveAndEnabled ) return;
if ( ! ParamsAreValid() ) return;
drawParams.Validate();
if( ! canExecNow ) return;
Validate();
}
internal void Validate( bool update = true )
{
if ( count < 0 ) count = 0;
drawCount = count;
if ( count > _matricies.Length || ! initialized )
BufferInit();
if( update ) BufferUpdate();
}
internal bool manualUpdate = false;
public virtual void Update()
{
if( ! canExecNow ) return;
if ( ! initialized || ! ParamsAreValid( ) ) return;
if( manualUpdate ) return;
ManualUpdate();
}
public void ManualUpdate()
{
if ( alwaysUpdate ) BufferUpdate();
if( drawParams.drawMode == GraphicsDrawMode.InstancedIndirect )
drawParams.DrawMeshInstancedIndirect( _bounds, _argsBuffer, _matPropBlock );
if( drawParams.drawMode == GraphicsDrawMode.RenderMeshPrimitives )
drawParams.RenderMeshPrimitives( _bounds, _matPropBlock, drawCount );
}
public virtual void OnDisable()
{
if ( ! canExecNow ) return;
GraphicsDrawStats.Remove( this );
isEnabled = false;
if (_instancesBuffer != null)
{
_instancesBuffer.Release();
_instancesBuffer = null;
}
if (_argsBuffer != null)
{
_argsBuffer.Release();
_argsBuffer = null;
}
if (_matricies.IsCreated )
{
_matricies.Dispose();
}
if( _matriciesCulled.IsCreated )
{
_matriciesCulled.Dispose();
}
DataDispose();
initialized = false;
}
}