forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVertexBuffer.cs
113 lines (101 loc) · 3.03 KB
/
VertexBuffer.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
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Runtime.InteropServices;
namespace OpenRA.Platforms.Default
{
sealed class VertexBuffer<T> : ThreadAffine, IVertexBuffer<T>
where T : struct
{
static readonly int VertexSize = Marshal.SizeOf(typeof(T));
uint buffer;
bool disposed;
public VertexBuffer(int size)
{
OpenGL.glGenBuffers(1, out buffer);
OpenGL.CheckGLError();
Bind();
// Generates a buffer with uninitialized memory.
OpenGL.glBufferData(OpenGL.GL_ARRAY_BUFFER,
new IntPtr(VertexSize * size),
IntPtr.Zero,
OpenGL.GL_DYNAMIC_DRAW);
OpenGL.CheckGLError();
// We need to zero all the memory. Let's generate a smallish array and copy that over the whole buffer.
var zeroedArrayElementSize = Math.Min(size, 2048);
var ptr = GCHandle.Alloc(new T[zeroedArrayElementSize], GCHandleType.Pinned);
try
{
for (var offset = 0; offset < size; offset += zeroedArrayElementSize)
{
var length = Math.Min(zeroedArrayElementSize, size - offset);
OpenGL.glBufferSubData(OpenGL.GL_ARRAY_BUFFER,
new IntPtr(VertexSize * offset),
new IntPtr(VertexSize * length),
ptr.AddrOfPinnedObject());
OpenGL.CheckGLError();
}
}
finally
{
ptr.Free();
}
}
public void SetData(T[] data, int length)
{
SetData(data, 0, 0, length);
}
public void SetData(T[] data, int offset, int start, int length)
{
Bind();
var ptr = GCHandle.Alloc(data, GCHandleType.Pinned);
try
{
OpenGL.glBufferSubData(OpenGL.GL_ARRAY_BUFFER,
new IntPtr(VertexSize * start),
new IntPtr(VertexSize * length),
ptr.AddrOfPinnedObject() + VertexSize * offset);
}
finally
{
ptr.Free();
}
OpenGL.CheckGLError();
}
public void Bind()
{
VerifyThreadAffinity();
OpenGL.glBindBuffer(OpenGL.GL_ARRAY_BUFFER, buffer);
OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.VertexPosAttributeIndex, 3, OpenGL.GL_FLOAT, false, VertexSize, IntPtr.Zero);
OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TexCoordAttributeIndex, 4, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(12));
OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TexMetadataAttributeIndex, 2, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(28));
OpenGL.CheckGLError();
OpenGL.glVertexAttribPointer(Shader.TintAttributeIndex, 4, OpenGL.GL_FLOAT, false, VertexSize, new IntPtr(36));
OpenGL.CheckGLError();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposed)
return;
disposed = true;
OpenGL.glDeleteBuffers(1, ref buffer);
OpenGL.CheckGLError();
}
}
}