-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchRenderingGame.cs
213 lines (168 loc) · 7.02 KB
/
BatchRenderingGame.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
using System.Net;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
using OpenTKTesting.Rendering;
using OpenTKTesting.Utils;
namespace OpenTKTesting.Game;
public class BatchrenderingGame : GameWindow
{
#region FPS
private int _frameCount = 0;
private double _time = 0;
#endregion
private Camera _camera;
private Vector2? _previousMousePos;
private Plane _plane = new Plane();
private BatchRenderingMesh _mesh;
private BatchBillboard _billboard;
private ShaderProgram _computeProgram;
private float _viewDistance = 3f;
private ShaderStorageBufferObject _ssbo;
private ShaderStorageBufferObject _meshesData;
private ShaderStorageBufferObject _billboardData;
private ShaderStorageBufferObject _statsData;
public BatchrenderingGame(GameWindowSettings settings, NativeWindowSettings nativeWindowSettings)
: base(settings, nativeWindowSettings) { }
public BatchrenderingGame()
: base(GameWindowSettings.Default, new NativeWindowSettings() {Size = new(800, 600), NumberOfSamples = 16}) { }
private (Vector3[], Vector3[], float[], float[]) GenerateInstances(int count)
{
var pos = new Vector3[count];
var colors = new Vector3[count];
var scales = new float[count];
var rd = new float[count];
var rand = new Random();
for (int i = 0; i < count; i++)
{
pos[i] = new Vector3(rand.NextSingle(), 0f, rand.NextSingle()) * 40f - new Vector3(20f, 0f, 20f);
colors[i] = new Vector3(rand.NextSingle(), rand.NextSingle(), rand.NextSingle());
scales[i] = rand.NextSingle() * 0.3f + .7f;
rd[i] = rand.NextSingle();
}
return (pos, colors, scales, rd);
}
private float[] InstancesToFloatArray(int count, Vector3[] positions, Vector3[] colors, float[] scales, float[] randomValue)
{
return Enumerable.Range(0, count).SelectMany(e =>
{
return new float[]
{
positions[e].X, positions[e].Y, positions[e].Z,
colors[e].X, colors[e].Y, colors[e].Z,
scales[e], randomValue[e], 0
};
}).ToArray();
}
protected override void OnLoad()
{
_camera = new Camera(Vector3.Zero, new Vector3(5), Size);
_plane.Init();
_plane.Scale = 20f;
_plane.Color = Vector3.One * 0.6f;
const int nbOfInstances = 10000;
var (pos, colors, scales, rd) = GenerateInstances(nbOfInstances);
var data = InstancesToFloatArray(nbOfInstances, pos, colors, scales, rd);
// Input data of compute shader
_ssbo = new ShaderStorageBufferObject();
_ssbo.SetData(data);
// Output of compute shader -> per instance data used as VBO
var blankData = Enumerable.Range(0, sizeof(float) * 9 * nbOfInstances).Select(e => 0f).ToArray();
_meshesData = new ShaderStorageBufferObject();
_meshesData.SetData(blankData);
_billboardData = new ShaderStorageBufferObject();
_billboardData.SetData(blankData);
_statsData = new ShaderStorageBufferObject();
_statsData.SetData(new int[2] { 0, 0});
_computeProgram = new ShaderProgram(new Shader(ShaderType.ComputeShader, "../../../Shaders/batch_filtering.comp"));
OpenGLErrorChecker.CheckError();
_billboard = new BatchBillboard(nbOfInstances, new VertexBufferObject<float>(_billboardData.ID));
OpenGLErrorChecker.CheckError();
_billboard.Init();
_mesh = new BatchRenderingMesh(@"./Assets/tree2.gltf", nbOfInstances, new VertexBufferObject<float>(_meshesData.ID));
_mesh.Init();
VSync = VSyncMode.Off;
GL.Enable(EnableCap.DepthTest);
base.OnLoad();
}
protected override void OnRenderFrame(FrameEventArgs args)
{
// Compute
_computeProgram.Use();
_statsData.SetData(new int[2] { 0, 0});
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 2, _ssbo.ID);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 3, _billboardData.ID);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 4, _meshesData.ID);
GL.BindBufferBase(BufferRangeTarget.ShaderStorageBuffer, 5, _statsData.ID);
GL.Uniform3(1, _camera.Position.X, _camera.Position.Y, _camera.Position.Z);
GL.Uniform1(2, _viewDistance);
_computeProgram.DispatchCompute(10000);
GL.MemoryBarrier(MemoryBarrierFlags.AllBarrierBits);
var data = new int[2];
GL.GetNamedBufferSubData(_statsData.ID, IntPtr.Zero, sizeof(int) * 2, data);
_mesh.InstanceCount = data[0];
_billboard.InstanceCount = data[1];
// Draw
GL.ClearColor(0, 0, 0.1f, 0);
GL.ClearDepth(1);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
_plane.Render(_camera);
_mesh.Render(_camera);
_billboard.Render(_camera);
SwapBuffers();
base.OnRenderFrame(args);
}
protected override void OnResize(ResizeEventArgs e)
{
if (e.Width != 0 && e.Height != 0)
{
GL.Viewport(0, 0, e.Width, e.Height);
_camera.UpdateSize(e.Size);
}
base.OnResize(e);
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
_previousMousePos = null;
base.OnMouseUp(e);
}
protected override void OnUpdateFrame(FrameEventArgs args)
{
if (IsKeyPressed(Keys.Escape))
Close();
_plane.Update((float) args.Time);
_mesh.Update((float) args.Time);
_billboard.Update((float) args.Time);
_frameCount++;
_time += args.Time;
if (_time > 1)
{
Title = $"FPS: {_frameCount / _time:F2} - {(_time / _frameCount):F4}ms";
_time = 0;
_frameCount = 0;
}
if (IsMouseButtonDown(MouseButton.Left))
{
if (_previousMousePos.HasValue)
{
var delta = _previousMousePos.Value - MousePosition;
delta = new Vector2(delta.X / Size.X, delta.Y / Size.Y) * (float) Math.PI;
_camera.Position *= Matrix3.CreateRotationY(delta.X);
var rotationAxis = Vector3.Cross(_camera.Direction, Vector3.UnitY);
_camera.Position *= Matrix3.CreateFromAxisAngle(rotationAxis, delta.Y);
}
_previousMousePos = MousePosition;
}
if (IsKeyDown(Keys.W) || IsKeyDown(Keys.Z))
_camera.Position += _camera.Direction * (float) args.Time * 2f;
if (IsKeyDown(Keys.S))
_camera.Position -= _camera.Direction * (float) args.Time * 2f;
if (IsKeyDown(Keys.LeftShift))
_viewDistance += 1f * (float) args.Time;
else if (IsKeyDown(Keys.LeftControl))
_viewDistance = Math.Max(0, _viewDistance - 1f * (float) args.Time);
base.OnUpdateFrame(args);
}
}