Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
VertexBuffer.cs
浏览该文件的文档.
1#if DIRECT3D11
2using SharpDX;
3using SharpDX.Direct3D11;
4#else
5using Silk.NET.OpenGLES;
6#endif
7using System.Runtime.InteropServices;
8
9namespace Engine.Graphics {
11#if DIRECT3D11
12 public SharpDX.Direct3D11.Buffer m_buffer;
13#else
14 public int m_buffer;
15#endif
16
17 public string DebugName {
18 get {
19#if DIRECT3D11
20 return m_buffer.DebugName;
21#else
22 return string.Empty;
23#endif
24 }
25 // ReSharper disable ValueParameterNotUsed
26 set
27 // ReSharper restore ValueParameterNotUsed
28 {
29#if DIRECT3D11
30 m_buffer.DebugName = value;
31#endif
32 }
33 }
34
36
37 public int VerticesCount { get; set; }
38
39 public object Tag { get; set; }
40
41 public VertexBuffer(VertexDeclaration vertexDeclaration, int verticesCount) {
42 InitializeVertexBuffer(vertexDeclaration, verticesCount);
44 }
45
46 public override void Dispose() {
47 base.Dispose();
49 }
50
51 public void SetData<T>(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex = 0) where T : unmanaged {
52 VerifyParametersSetData(source, sourceStartIndex, sourceCount, targetStartIndex);
53 GCHandle gCHandle = GCHandle.Alloc(source, GCHandleType.Pinned);
54 try {
55 int num = Utilities.SizeOf<T>();
56 int vertexStride = VertexDeclaration.VertexStride;
57#if DIRECT3D11
58 DataBox dataBox = new(gCHandle.AddrOfPinnedObject() + sourceStartIndex * num, 1, 0);
59 ResourceRegion resourceRegion = new(
60 targetStartIndex * VertexDeclaration.VertexStride,
61 0,
62 0,
63 targetStartIndex * VertexDeclaration.VertexStride + sourceCount * num,
64 1,
65 1
66 );
67 DXWrapper.Context.UpdateSubresource(dataBox, m_buffer, 0, resourceRegion);
68#else
69 unsafe {
70 GLWrapper.BindBuffer(BufferTargetARB.ArrayBuffer, m_buffer);
71 GLWrapper.GL.BufferSubData(
72 BufferTargetARB.ArrayBuffer,
73 new IntPtr(targetStartIndex * vertexStride),
74 new UIntPtr((uint)(num * sourceCount)),
75 (gCHandle.AddrOfPinnedObject() + sourceStartIndex * num).ToPointer()
76 );
77 }
78#endif
79 }
80 finally {
81 gCHandle.Free();
82 }
83 }
84
85 public override void HandleDeviceLost() {
87 }
88
89 public override void HandleDeviceReset() {
91 }
92
93 public void AllocateBuffer() {
94#if DIRECT3D11
95 m_buffer = new SharpDX.Direct3D11.Buffer(
97 VertexDeclaration.VertexStride * VerticesCount,
98 ResourceUsage.Default,
99 BindFlags.VertexBuffer,
100 CpuAccessFlags.None,
101 ResourceOptionFlags.None,
102 0
103 );
104#else
105 unsafe {
106 GLWrapper.GL.GenBuffers(1, out uint buffer);
107 m_buffer = (int)buffer;
108 GLWrapper.BindBuffer(BufferTargetARB.ArrayBuffer, m_buffer);
109 GLWrapper.GL.BufferData(
110 BufferTargetARB.ArrayBuffer,
111 new UIntPtr((uint)(VertexDeclaration.VertexStride * VerticesCount)),
112 null,
113 BufferUsageARB.StaticDraw
114 );
115 }
116#endif
117 }
118
119 public void DeleteBuffer() {
120#if DIRECT3D11
121 Utilities.Dispose(ref m_buffer);
122#else
123 if (m_buffer != 0) {
124 GLWrapper.DeleteBuffer(BufferTargetARB.ArrayBuffer, m_buffer);
125 m_buffer = 0;
126 }
127#endif
128 }
129
131
132 public void InitializeVertexBuffer(VertexDeclaration vertexDeclaration, int verticesCount) {
133 ArgumentNullException.ThrowIfNull(vertexDeclaration);
134 if (verticesCount <= 0) {
135 throw new ArgumentException("verticesCount must be greater than 0.");
136 }
137 VertexDeclaration = vertexDeclaration;
138 VerticesCount = verticesCount;
139 }
140
141 public void VerifyParametersSetData<T>(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex = 0) where T : unmanaged {
143 int num = Utilities.SizeOf<T>();
144 int vertexStride = VertexDeclaration.VertexStride;
145 ArgumentNullException.ThrowIfNull(source);
146 if (sourceStartIndex < 0
147 || sourceCount < 0
148 || sourceStartIndex + sourceCount > source.Length) {
149 throw new ArgumentException("Range is out of source bounds.");
150 }
151 if (targetStartIndex < 0
152 || targetStartIndex * vertexStride + sourceCount * num > VerticesCount * vertexStride) {
153 throw new ArgumentException("Range is out of target bounds.");
154 }
155 }
156 }
157}
unsafe
定义 Main.cs:15
static DeviceContext2 Context
static void DeleteBuffer(BufferTargetARB target, int buffer)
static void BindBuffer(BufferTargetARB target, int buffer)
void VerifyParametersSetData< T >(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex=0)
void SetData< T >(T[] source, int sourceStartIndex, int sourceCount, int targetStartIndex=0)
VertexBuffer(VertexDeclaration vertexDeclaration, int verticesCount)
void InitializeVertexBuffer(VertexDeclaration vertexDeclaration, int verticesCount)