-
Notifications
You must be signed in to change notification settings - Fork 4
/
CombinedMeshes.cs
486 lines (377 loc) · 15.3 KB
/
CombinedMeshes.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
using System;
using Xenko.Core;
using Xenko.Core.Mathematics;
using Xenko.Core.Collections;
using Xenko.Engine;
using Xenko.Graphics;
using Xenko.Graphics.GeometricPrimitives;
using Xenko.Rendering;
using Xenko.Extensions;
using System.Runtime.InteropServices;
using VertexType = XenkoByteSized.ProceduralMesh.VertexPositionNormalTextureColor;
namespace XenkoByteSized.ProceduralMesh
{
class PoorMansMultiMesh : SyncScript
{
const int INITIAL_INSTANCE_COUNT = 16;
private TrackingDictionary<Guid, SomeObjectInSpace> objects = new TrackingDictionary<Guid, SomeObjectInSpace>();
private FastList<Matrix> matrices = new FastList<Matrix>(INITIAL_INSTANCE_COUNT);
private FastList<Color4> colors = new FastList<Color4>(INITIAL_INSTANCE_COUNT);
/* gpu pipeline stuff */
private PipelineState pipelineState;
/* gpu side data, our ModelComponent we use to render, no index buffer data so there is some... waste yes */
private ModelComponent modelComponent;
private VertexBufferBinding streamOutBufferBinding;
private Buffer<Matrix> transformBuffer;
private Buffer<Color4> colorBuffer;
private EffectInstance streamShader;
private Mesh streamBufferedMesh;
private Mesh renderedMesh;
public Mesh Mesh
{
get
{
return renderedMesh;
}
set
{
renderedMesh = value;
}
}
public Material Material { get; set; }
public System.Guid AddInstance(SomeObjectInSpace someObject)
{
objects[someObject.Id] = someObject;
return someObject.Id;
}
public bool RemoveInstance(SomeObjectInSpace someObject)
{
return objects.Remove(someObject.Id);
}
private void CreateDeviceObjects()
{
var commandList = Game.GraphicsContext.CommandList;
var shader = new EffectInstance(EffectSystem.LoadEffect("MultiMeshShader").WaitForResult());
shader.UpdateEffect(GraphicsDevice);
streamShader = shader;
var outputDesc = new RenderOutputDescription(GraphicsDevice.Presenter.BackBuffer.Format);
outputDesc.CaptureState(commandList);
var pipeline = new PipelineStateDescription()
{
/* TODO: do we need all these? */
BlendState = BlendStates.Default,
RasterizerState = RasterizerStateDescription.Default,
DepthStencilState = DepthStencilStates.None,
Output = outputDesc,
PrimitiveType = PrimitiveType.TriangleList,
InputElements = VertexType.Layout.CreateInputElements(),
EffectBytecode = shader.Effect.Bytecode,
RootSignature = shader.RootSignature,
};
var newPipelineState = PipelineState.New(GraphicsDevice, ref pipeline);
pipelineState = newPipelineState;
var streamBuffer = Xenko.Graphics.Buffer.New<VertexType>(
GraphicsDevice,
INITIAL_INSTANCE_COUNT,
BufferFlags.VertexBuffer | BufferFlags.StreamOutput
);
streamOutBufferBinding = new VertexBufferBinding(streamBuffer, VertexType.Layout, streamBuffer.ElementCount);
transformBuffer = Xenko.Graphics.Buffer.Structured.New<Matrix>(
GraphicsDevice,
INITIAL_INSTANCE_COUNT,
isUnorderedAccess: true
);
colorBuffer = Xenko.Graphics.Buffer.Structured.New<Color4>(
GraphicsDevice,
INITIAL_INSTANCE_COUNT,
isUnorderedAccess: true
);
}
private void CheckBuffers()
{
var device = GraphicsDevice;
var commandList = Game.GraphicsContext.CommandList;
var totalIndices = renderedMesh.Draw.IndexBuffer.Count;
int neededStreamBufferSize = objects.Count * totalIndices;
if (neededStreamBufferSize > streamOutBufferBinding.Count)
{
streamOutBufferBinding.Buffer.Dispose(); // dispose the old buffer first
var streamBuffer = Xenko.Graphics.Buffer.New<VertexType>(
device,
neededStreamBufferSize,
BufferFlags.VertexBuffer | BufferFlags.StreamOutput
);
streamOutBufferBinding = new VertexBufferBinding(streamBuffer, VertexType.Layout, streamBuffer.ElementCount);
}
int neededTransformBufferSize = objects.Count;
if (neededTransformBufferSize > transformBuffer.ElementCount)
{
transformBuffer.Dispose(); // dispose old buffer first
transformBuffer = Xenko.Graphics.Buffer.Structured.New(
device,
matrices.Items,
isUnorderedAccess: true
);
colorBuffer.Dispose(); // dispose old buffer first
colorBuffer = Xenko.Graphics.Buffer.Structured.New(
device,
colors.Items,
isUnorderedAccess: true
);
}
else
{
transformBuffer.SetData(commandList, matrices.Items);
colorBuffer.SetData(commandList, colors.Items);
}
}
private void PerformStreamOut()
{
var commandList = Game.GraphicsContext.CommandList;
commandList.SetPipelineState(pipelineState);
var meshDraw = renderedMesh.Draw;
/* vertex buffer(s) */
for (int i = 0; i < meshDraw.VertexBuffers.Length; i++)
{
var vertexBufferBinding = meshDraw.VertexBuffers[i];
commandList.SetVertexBuffer(i, vertexBufferBinding.Buffer, meshDraw.StartLocation, vertexBufferBinding.Declaration.VertexStride);
}
var indexBuffer = meshDraw.IndexBuffer.Buffer;
commandList.SetIndexBuffer(indexBuffer, 0, meshDraw.IndexBuffer.Is32Bit);
commandList.SetStreamTargets(streamOutBufferBinding.Buffer);
streamShader.Parameters.Set(MultiMeshShaderKeys.ModelTransforms, transformBuffer);
streamShader.Parameters.Set(MultiMeshShaderKeys.ModelColors, colorBuffer);
streamShader.Apply(Game.GraphicsContext);
/* finally write to our streamout buffer */
commandList.DrawIndexedInstanced(indexBuffer.ElementCount, objects.Count);
commandList.SetStreamTargets(null);
}
static private Mesh CreateMesh(VertexBufferBinding bufferBinding)
{
var newMesh = new Mesh()
{
Draw = new MeshDraw()
{
PrimitiveType = PrimitiveType.TriangleList,
VertexBuffers = new[] {
bufferBinding,
},
DrawCount = bufferBinding.Count
}
};
return newMesh;
}
public override void Start()
{
CreateDeviceObjects();
streamBufferedMesh = CreateMesh(streamOutBufferBinding);
streamBufferedMesh.MaterialIndex = 0;
var newModel = new Model();
newModel.Meshes.Add(streamBufferedMesh);
newModel.Materials.Add(Material); // FIXME: can be null at this point if not set
modelComponent = new ModelComponent()
{
Model = newModel
};
Entity.Add(modelComponent);
}
private void UpdateBuffers()
{
CheckBuffers();
/* update our MeshDraw to reflect current state */
var vertexCount = streamOutBufferBinding.Count;
streamBufferedMesh.Draw.VertexBuffers[0] = streamOutBufferBinding;
streamBufferedMesh.Draw.DrawCount = vertexCount;
}
private void UpdateInstanceData()
{
/* FIXME: this is all somewhat stupid.. fix this once it at least runs */
matrices.Clear();
colors.Clear();
foreach (var kv in objects)
{
matrices.Add(kv.Value.Entity.Transform.WorldMatrix);
colors.Add(kv.Value.Color);
}
}
public override void Update()
{
UpdateInstanceData();
UpdateBuffers();
PerformStreamOut();
}
public override void Cancel()
{
streamOutBufferBinding.Buffer.Dispose();
transformBuffer.Dispose();
colorBuffer.Dispose();
streamShader.Dispose();
}
}
class SomeObjectInSpace : SyncScript
{
public Vector3 Velocity;
public Vector3 RotVelocity;
public Color4 Color;
public override void Update()
{
var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds;
if (Entity.Transform.Position.X > 64.0f || Entity.Transform.Position.X < -64.0f)
{
Velocity.X = -Velocity.X;
}
if (Entity.Transform.Position.Y > 64.0f || Entity.Transform.Position.Y < -64.0f)
{
Velocity.Y = -Velocity.Y;
}
if (Entity.Transform.Position.Z > 64.0f || Entity.Transform.Position.Z < -64.0f)
{
Velocity.Z = -Velocity.Z;
}
Entity.Transform.Rotation *=
Quaternion.RotationX(RotVelocity.X * dt) *
Quaternion.RotationY(RotVelocity.Y * dt) *
Quaternion.RotationZ(RotVelocity.Z * dt);
Entity.Transform.Position += Velocity * dt;
}
}
public class CombinedMeshes : SyncScript
{
/* GPU side data */
private PoorMansMultiMesh multiMesh;
[Display("Material")]
public Material meshMaterial;
public override void Start()
{
var ourPrimitive = GeometricPrimitive.Cube.New(GraphicsDevice, 1.0f);
var primitiveMeshDraw = ourPrimitive.ToMeshDraw();
var newMultiMesh = new PoorMansMultiMesh()
{
Mesh = new Mesh()
{
Draw = primitiveMeshDraw
},
Material = meshMaterial
};
var numInstances = 2048;
var random = new Random();
for (int i = 0; i < numInstances; ++i)
{
var randX = random.Next(-64, 64);
var randY = random.Next(-64, 64);
var randZ = random.Next(-64, 64);
var velX = random.NextDouble() * 4.0;
var velY = random.NextDouble() * 4.0;
var velZ = random.NextDouble() * 4.0;
var vel = new Vector3((float)velX, (float)velY, (float)velZ);
var rotVelX = random.NextDouble();
var rotVelY = random.NextDouble();
var rotVelZ = random.NextDouble();
var rotVel = new Vector3((float)rotVelX, (float)rotVelY, (float)rotVelZ);
var r = random.NextDouble();
var g = random.NextDouble();
var b = random.NextDouble();
var col = new Color4((float)r, (float)g, (float)b);
var newEntity = new Entity(new Vector3(randX, randY, randZ));
var newObjectInSpace = new SomeObjectInSpace() { Velocity = vel, RotVelocity = rotVel, Color = col };
newEntity.Add(newObjectInSpace);
newMultiMesh.AddInstance(newObjectInSpace);
Entity.AddChild(newEntity);
}
Entity.Add(newMultiMesh);
multiMesh = newMultiMesh;
}
public override void Update()
{
}
}
/// <summary>
/// Describes a custom vertex format structure that contains position and color information.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct VertexPositionNormalTextureColor : IEquatable<VertexPositionNormalTextureColor>, IVertex
{
/// <summary>
/// Initializes a new <see cref="VertexPositionNormalTextureColor"/> instance.
/// </summary>
/// <param name="position">The position of this vertex.</param>
/// <param name="color">The color of this vertex.</param>
/// <param name="textureCoordinate">UV texture coordinates.</param>
public VertexPositionNormalTextureColor(Vector3 position, Vector3 normal, Vector2 textureCoordinate, Color4 color)
: this()
{
Position = position;
Normal = normal;
TextureCoordinate = textureCoordinate;
Color = color;
}
/// <summary>
/// XYZ position.
/// </summary>
public Vector3 Position;
/// <summary>
/// XYZ position.
/// </summary>
public Vector3 Normal;
/// <summary>
/// UV texture coordinates.
/// </summary>
public Vector2 TextureCoordinate;
/// <summary>
/// The vertex color.
/// </summary>
public Color4 Color;
/// <summary>
/// Defines structure byte size.
/// </summary>
public static readonly int Size = 48;
/// <summary>
/// The vertex layout of this struct.
/// </summary>
public static readonly VertexDeclaration Layout = new VertexDeclaration(
VertexElement.Position<Vector3>(),
VertexElement.Normal<Vector3>(),
VertexElement.TextureCoordinate<Vector2>(),
VertexElement.Color<Color4>());
public bool Equals(VertexPositionNormalTextureColor other)
{
return Position.Equals(other.Position) && Normal.Equals(other.Normal) && Color.Equals(other.Color) && TextureCoordinate.Equals(other.TextureCoordinate);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is VertexPositionNormalTextureColor && Equals((VertexPositionNormalTextureColor)obj);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = Position.GetHashCode();
hashCode = (hashCode * 397) ^ Normal.GetHashCode();
hashCode = (hashCode * 397) ^ TextureCoordinate.GetHashCode();
hashCode = (hashCode * 397) ^ Color.GetHashCode();
return hashCode;
}
}
public static bool operator ==(VertexPositionNormalTextureColor left, VertexPositionNormalTextureColor right)
{
return left.Equals(right);
}
public static bool operator !=(VertexPositionNormalTextureColor left, VertexPositionNormalTextureColor right)
{
return !left.Equals(right);
}
public override string ToString()
{
return string.Format("Position: {0}, Normal {1} Color: {2}, Texcoord: {3}", Position, Normal, TextureCoordinate, Color);
}
public VertexDeclaration GetLayout()
{
return Layout;
}
public void FlipWinding()
{
TextureCoordinate.X = (1.0f - TextureCoordinate.X);
}
}
}