-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLHelper.cs
More file actions
473 lines (415 loc) · 14.9 KB
/
GLHelper.cs
File metadata and controls
473 lines (415 loc) · 14.9 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
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
using Silk.NET.OpenGL;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace GraphicsApiBenchmark
{
public static unsafe class GLHelper
{
public static GL gl;
#region Vertex array
public static uint CreateVertexArray(
uint vertexBuffer,
uint stride = 3 * sizeof(float),
VertexAttribType vertexAttribType = VertexAttribType.Float,
int size = 3,
uint bindingIndex = 0,
uint attribIndex = 0,
uint offset = 0,
nint bufferOffset = 0,
bool normalized = false)
{
uint vao = gl.CreateVertexArray();
gl.VertexArrayVertexBuffer(vao, bindingIndex, vertexBuffer, bufferOffset, stride);
AssertGlCall();
gl.VertexArrayAttribBinding(vao, attribIndex, bindingIndex);
AssertGlCall();
gl.VertexArrayAttribFormat(vao, attribIndex, size, vertexAttribType, normalized, offset);
AssertGlCall();
gl.EnableVertexArrayAttrib(vao, attribIndex);
AssertGlCall();
return vao;
}
public static void BindVertexArray(uint vertexArray)
{
gl.BindVertexArray(vertexArray);
AssertGlCall();
}
public static void DeleteVertexArray(uint vertexArray)
{
gl.DeleteVertexArray(vertexArray);
AssertGlCall();
}
#endregion
#region Buffer
public static uint CreateStorageBuffer(uint size, BufferStorageMask flags = BufferStorageMask.None)
{
uint buffer = gl.CreateBuffer();
AssertGlCall();
gl.NamedBufferStorage(buffer, size, ReadOnlySpan<byte>.Empty, flags);
AssertGlCall();
return buffer;
}
public static uint CreateStorageBuffer<T>(ReadOnlySpan<T> data, BufferStorageMask flags = BufferStorageMask.None) where T : unmanaged
{
uint buffer = gl.CreateBuffer();
AssertGlCall();
gl.NamedBufferStorage(buffer, (uint)(data.Length * sizeof(T)), data, flags);
AssertGlCall();
return buffer;
}
public static uint CreateBuffer(uint size, VertexBufferObjectUsage flags)
{
uint buffer = gl.CreateBuffer();
AssertGlCall();
gl.NamedBufferData(buffer, size, ReadOnlySpan<byte>.Empty, flags);
AssertGlCall();
return buffer;
}
public static void BindBufferRange(uint buffer, uint index, nuint size, nint offset = 0, BufferTargetARB target = BufferTargetARB.UniformBuffer)
{
gl.BindBufferRange(target, index, buffer, offset, size);
AssertGlCall();
}
public static void BufferData<T>(uint buffer, ReadOnlySpan<T> data, VertexBufferObjectUsage flags) where T : unmanaged
{
gl.NamedBufferData(buffer, data, flags);
AssertGlCall();
}
public static void BufferSubData<T>(uint buffer, ReadOnlySpan<T> data, nint offset = 0) where T : unmanaged
{
gl.NamedBufferSubData(buffer, offset, data);
AssertGlCall();
}
public static void InvalidateBufferSubData(uint buffer, nuint length, nint offset = 0)
{
gl.InvalidateBufferSubData(buffer, offset, length);
AssertGlCall();
}
public static void FlushBufferRange(uint buffer, nuint length, nint offset = 0)
{
gl.FlushMappedNamedBufferRange(buffer, offset, length);
AssertGlCall();
}
public static unsafe void* MapBufferRange(uint buffer, MapBufferAccessMask flags, nuint length, nint offset = 0)
{
void* pointer = gl.MapNamedBufferRange(buffer, offset, length, flags);
AssertGlCall();
return pointer;
}
public static void UnmapBuffer(uint buffer)
{
gl.UnmapNamedBuffer(buffer);
AssertGlCall();
}
public static void DeleteBuffer(uint buffer)
{
gl.DeleteBuffer(buffer);
AssertGlCall();
}
public static int GetUniformBufferOffsetAlignment()
{
int offsetAlignment = gl.GetInteger(GetPName.UniformBufferOffsetAlignment);
AssertGlCall();
return offsetAlignment;
}
public static string GetFlagNames(this BufferStorageMask flags)
{
var result = "";
if ((flags & BufferStorageMask.MapPersistentBit) > 0)
{
result += " PERSISTENT";
}
if ((flags & BufferStorageMask.MapCoherentBit) > 0)
{
result += " COHERENT";
}
if ((flags & BufferStorageMask.MapWriteBit) > 0)
{
result += " MAP_WRITE";
}
if ((flags & BufferStorageMask.DynamicStorageBit) > 0)
{
result += " DYNAMIC_STORAGE";
}
if ((flags & BufferStorageMask.ClientStorageBit) > 0)
{
result += " CLIENT_STORAGE";
}
return result;
}
public static string GetFlagNames(this MapBufferAccessMask flags)
{
var result = "";
if ((flags & MapBufferAccessMask.PersistentBit) > 0)
{
result += " PERSISTENT";
}
if ((flags & MapBufferAccessMask.CoherentBit) > 0)
{
result += " COHERENT";
}
if ((flags & MapBufferAccessMask.WriteBit) > 0)
{
result += " WRITE";
}
if ((flags & MapBufferAccessMask.FlushExplicitBit) > 0)
{
result += " FLUSH_EXPLICIT";
}
if ((flags & MapBufferAccessMask.InvalidateBufferBit) > 0)
{
result += " INVALIDATE_BUFFER";
}
if ((flags & MapBufferAccessMask.InvalidateRangeBit) > 0)
{
result += " INVALIDATE_RANGE";
}
if ((flags & MapBufferAccessMask.UnsynchronizedBit) > 0)
{
result += " UNSYNCHRONIZED";
}
return result;
}
public static string GetFlagNames(this VertexBufferObjectUsage flags)
{
if (flags == VertexBufferObjectUsage.StaticCopy)
{
return " STATIC_COPY";
}
else if (flags == VertexBufferObjectUsage.StaticDraw)
{
return " STATIC_DRAW";
}
else if (flags == VertexBufferObjectUsage.StaticRead)
{
return " STATIC_READ";
}
else if (flags == VertexBufferObjectUsage.DynamicCopy)
{
return " DYNAMIC_COPY";
}
else if (flags == VertexBufferObjectUsage.DynamicDraw)
{
return " DYNAMIC_DRAW";
}
else if (flags == VertexBufferObjectUsage.DynamicRead)
{
return " DYNAMIC_READ";
}
else if (flags == VertexBufferObjectUsage.StreamCopy)
{
return " STREAM_COPY";
}
else if (flags == VertexBufferObjectUsage.StreamDraw)
{
return " STREAM_DRAW";
}
else if (flags == VertexBufferObjectUsage.StreamRead)
{
return " STREAM_READ";
}
return "";
}
#endregion
#region Program
public static uint CreateProgram(string vertexSource, string fragmentSource)
{
uint program = gl.CreateProgram();
AssertGlCall();
uint vertexShader = CreateShader(ShaderType.VertexShader, vertexSource);
gl.AttachShader(program, vertexShader);
uint fragmentShader = CreateShader(ShaderType.FragmentShader, fragmentSource);
gl.AttachShader(program, fragmentShader);
AssertGlCall();
gl.LinkProgram(program);
AssertProgram(program);
DeleteShader(vertexShader);
DeleteShader(fragmentShader);
return program;
}
public static void UseProgram(uint program)
{
gl.UseProgram(program);
AssertGlCall();
}
public static void DeleteProgram(uint program)
{
gl.DeleteBuffer(program);
AssertGlCall();
}
[Conditional("DEBUG")]
private static void AssertProgram(uint program)
{
int result = gl.GetProgram(program, ProgramPropertyARB.LinkStatus);
AssertGlCall();
if (result != (int)GLEnum.True)
{
var infoLog = gl.GetProgramInfoLog(program);
AssertGlCall();
Console.WriteLine(infoLog);
throw new Exception(infoLog);
}
gl.ValidateProgram(program);
AssertGlCall();
result = gl.GetProgram(program, ProgramPropertyARB.ValidateStatus);
AssertGlCall();
if (result != (int)GLEnum.True)
{
var infoLog = gl.GetProgramInfoLog(program);
AssertGlCall();
Console.WriteLine(infoLog);
throw new Exception(infoLog);
}
}
#endregion
#region Shader
public static uint CreateShader(ShaderType shaderType, string source)
{
uint shader = gl.CreateShader(shaderType);
AssertGlCall();
gl.ShaderSource(shader, source);
AssertGlCall();
gl.CompileShader(shader);
AssertShader(shader);
return shader;
}
public static void DeleteShader(uint shader)
{
gl.DeleteShader(shader);
AssertGlCall();
}
[Conditional("DEBUG")]
private static void AssertShader(uint shader)
{
int result = gl.GetShader(shader, ShaderParameterName.CompileStatus);
AssertGlCall();
if (result != (int)GLEnum.True)
{
var infoLog = gl.GetShaderInfoLog(shader);
AssertGlCall();
Console.WriteLine(infoLog);
throw new Exception(infoLog);
}
}
#endregion
#region Sync
public static nint CreateSync()
{
nint sync = gl.FenceSync(SyncCondition.SyncGpuCommandsComplete, SyncBehaviorFlags.None);
AssertGlCall();
return sync;
}
public static void CpuWaitSync(nint sync, ulong timeout)
{
gl.ClientWaitSync(sync, 0u, timeout);
AssertGlCall();
}
public static void DeleteSync(nint sync)
{
gl.DeleteSync(sync);
AssertGlCall();
}
#endregion
#region Logging
[Conditional("DEBUG")]
public static void InitializeLogging()
{
gl.Enable(EnableCap.DebugOutput);
AssertGlCall();
gl.Enable(EnableCap.DebugOutputSynchronous);
AssertGlCall();
gl.DebugMessageCallback((source, type, id, severity, length, message, param) =>
{
var severityEnum = (DebugSeverity)severity;
var res = $"OpenGL {GetMessageSeverity(severityEnum)} {GetMessageType((DebugType)type)} message from the {GetMessageSource((DebugSource)source)}: {Marshal.PtrToStringAnsi(message)}";
Console.WriteLine(res);
}, ReadOnlySpan<byte>.Empty);
AssertGlCall();
}
private static string GetMessageSeverity(DebugSeverity severity)
{
return severity switch
{
DebugSeverity.DebugSeverityNotification => "notification severity",
DebugSeverity.DebugSeverityLow => "low severity",
DebugSeverity.DebugSeverityMedium => "medium severity",
DebugSeverity.DebugSeverityHigh => "high severity",
_ => "invalid severity",
};
}
private static string GetMessageSource(DebugSource source)
{
return source switch
{
DebugSource.DebugSourceWindowSystem => "window system",
DebugSource.DebugSourceThirdParty => "third party",
DebugSource.DebugSourceShaderCompiler => "shader compiler",
DebugSource.DebugSourceOther => "other",
DebugSource.DebugSourceApplication => "application",
DebugSource.DebugSourceApi => "API",
_ => "invalid source",
};
}
private static string GetMessageType(DebugType type)
{
return type switch
{
DebugType.DebugTypeDeprecatedBehavior => "deprecated behavior",
DebugType.DebugTypeError => "error",
DebugType.DebugTypeMarker => "marker",
DebugType.DebugTypeOther => "other",
DebugType.DebugTypePerformance => "performance",
DebugType.DebugTypePopGroup => "pop group",
DebugType.DebugTypePortability => "portability",
DebugType.DebugTypePushGroup => "push group",
DebugType.DebugTypeUndefinedBehavior => "undefined behavior",
_ => "invalid type",
};
}
#endregion
#region Other
public static void EnableBackfaceCulling()
{
gl.Enable(EnableCap.CullFace);
AssertGlCall();
gl.CullFace(TriangleFace.Back);
AssertGlCall();
}
public static void ClearBackBuffer()
{
gl.Clear(ClearBufferMask.ColorBufferBit);
AssertGlCall();
}
public static void SetViewport(uint width, uint height, int x = 0, int y = 0)
{
gl.Viewport(x, y, width, height);
AssertGlCall();
}
public static void Draw(uint vertexCount, PrimitiveType primitive = PrimitiveType.Triangles, int first = 0)
{
gl.DrawArrays(primitive, first, vertexCount);
AssertGlCall();
}
public static string GetRenderer()
{
var renderer = gl.GetStringS(StringName.Renderer);
AssertGlCall();
return renderer;
}
public static uint AlignSize(uint size, int alignment)
{
return (uint)((size + alignment - 1) & ~(alignment - 1));
}
[Conditional("DEBUG")]
public static void AssertGlCall()
{
var result = gl.GetError();
if (result != GLEnum.NoError)
{
Console.WriteLine(result);
throw new Exception($"{result}");
}
}
#endregion
}
}