Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
VertexBuffer.cs
Go to the documentation of this file.
1using System;
5
7
9{
10 private protected uint _usage;
11
12 private protected uint _size;
13
14 private protected uint _pool;
15
16 internal uint _vertexCount;
17
19
20 private unsafe void* pBufferData;
21
22 internal unsafe IDirect3DVertexBuffer9* pComPtr;
23
24 internal bool IsWriteOnly
25 {
26 [return: MarshalAs(UnmanagedType.U1)]
27 get
28 {
29 return (_usage & 8) == 8;
30 }
31 }
32
34
35 public int VertexCount => (int)_vertexCount;
36
37 public BufferUsage BufferUsage => _003CModule_003E.ConvertDxBufferUsageToXna(_usage);
38
39 private unsafe VertexBuffer(IDirect3DVertexBuffer9* pInterface, GraphicsDevice pDevice)
40 {
41 pComPtr = pInterface;
42 ((object)this)._002Ector();
43 try
44 {
45 _parent = pDevice;
46 return;
47 }
48 catch
49 {
50 //try-fault
51 base.Dispose(true);
52 throw;
53 }
54 }
55
56 private protected VertexBuffer()
57 {
58 }
59
60 public unsafe VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsage usage)
61 {
62 try
63 {
64 VertexDeclaration vertexDeclaration = VertexDeclaration.FromType(vertexType);
65 if (vertexCount <= 0)
66 {
68 }
69 _parent = graphicsDevice;
70 CreateBuffer(vertexDeclaration, (uint)vertexCount, _003CModule_003E.ConvertXnaBufferUsageToDx(usage), (_D3DPOOL)1);
72 return;
73 }
74 catch
75 {
76 //try-fault
77 base.Dispose(true);
78 throw;
79 }
80 }
81
82 public unsafe VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
83 {
84 try
85 {
86 if (vertexDeclaration == null)
87 {
88 throw new ArgumentNullException("vertexDeclaration", FrameworkResources.NullNotAllowed);
89 }
90 if (vertexCount <= 0)
91 {
93 }
94 _parent = graphicsDevice;
95 CreateBuffer(vertexDeclaration, (uint)vertexCount, _003CModule_003E.ConvertXnaBufferUsageToDx(usage), (_D3DPOOL)1);
97 return;
98 }
99 catch
100 {
101 //try-fault
102 base.Dispose(true);
103 throw;
104 }
105 }
106
107 public void SetData<T>(T[] data) where T : struct
108 {
109 int elementCount = ((data != null) ? data.Length : 0);
110 SetData(0, data, 0, elementCount, 0);
111 }
112
113 public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
114 {
115 SetData(0, data, startIndex, elementCount, 0);
116 }
117
118 public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct
119 {
120 CopyData(offsetInBytes, data, startIndex, elementCount, vertexStride, 0u, isSetting: true);
121 }
122
123 public void GetData<T>(T[] data) where T : struct
124 {
125 int elementCount = ((data != null) ? data.Length : 0);
126 GetData(0, data, 0, elementCount, 0);
127 }
128
129 public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
130 {
131 GetData(0, data, startIndex, elementCount, 0);
132 }
133
134 public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride) where T : struct
135 {
136 CopyData(offsetInBytes, data, startIndex, elementCount, vertexStride, 16u, isSetting: false);
137 }
138
139 internal unsafe void CopyData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride, uint options, [MarshalAs(UnmanagedType.U1)] bool isSetting) where T : struct
140 {
141 IntPtr intPtr = (IntPtr)pComPtr;
142 Helpers.CheckDisposed(this, intPtr);
143 if (data != null)
144 {
145 int num = data.Length;
146 if (num != 0)
147 {
148 int num2 = -2147467259;
149 if ((options & 0x3010) == 0)
150 {
151 int num3 = 0;
152 GraphicsDevice parent = _parent;
153 int currentVertexBufferCount = parent.currentVertexBufferCount;
154 if (0 < currentVertexBufferCount)
155 {
156 VertexBufferBinding[] currentVertexBuffers = parent.currentVertexBuffers;
157 do
158 {
159 if (currentVertexBuffers[num3]._vertexBuffer != this)
160 {
161 num3++;
162 continue;
163 }
164 throw GraphicsHelpers.GetExceptionFromResult(2147500036u);
165 }
166 while (num3 < currentVertexBufferCount);
167 }
168 }
169 if (!isSetting && (_usage & 8) == 8)
170 {
172 }
173 Helpers.ValidateCopyParameters(num, startIndex, elementCount);
174 try
175 {
176 uint num4 = (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<T>();
177 uint num5 = num4 * (uint)elementCount;
178 int num6;
179 if (vertexStride != 0)
180 {
181 num6 = vertexStride - (int)num4;
182 if (num6 < 0)
183 {
185 }
186 if (elementCount > 1)
187 {
188 num5 = (uint)((elementCount - 1) * num6) + num5;
189 }
190 }
191 else
192 {
193 num6 = 0;
194 }
195 if ((uint)((int)num5 + offsetInBytes) > _size)
196 {
198 }
200 {
201 if (isSetting)
202 {
203 return;
204 }
205 fixed (T* ptr = &data[startIndex])
206 {
207 try
208 {
209 // IL initblk instruction
211 }
212 catch
213 {
214 //try-fault
215 ptr = null;
216 throw;
217 }
218 }
219 return;
220 }
221 void* ptr2 = null;
222 byte* ptr3 = (byte*)pBufferData;
223 if (IsWriteOnly && _pool == 1)
224 {
225 ptr3 = offsetInBytes + ptr3;
226 }
227 IDirect3DVertexBuffer9* ptr4 = pComPtr;
228 num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, void**, uint, int>)(int)(*(uint*)(*(int*)ptr4 + 44)))((nint)ptr4, (uint)offsetInBytes, num5, &ptr2, options);
229 if (num2 >= 0)
230 {
231 if (num6 == 0)
232 {
233 fixed (T* ptr5 = &data[startIndex])
234 {
235 try
236 {
237 if (isSetting)
238 {
239 _003CModule_003E.memcpy_s(ptr2, _size - (uint)offsetInBytes, ptr5, num5);
240 if (IsWriteOnly && _pool == 1)
241 {
242 _003CModule_003E.memcpy_s(ptr3, _size - (uint)offsetInBytes, ptr5, num5);
243 }
244 }
245 else
246 {
247 _003CModule_003E.memcpy_s(ptr5, num5, ptr2, num5);
248 }
249 }
250 catch
251 {
252 //try-fault
253 ptr5 = null;
254 throw;
255 }
256 }
257 }
258 else
259 {
260 byte* ptr6 = (byte*)ptr2;
261 byte* ptr7 = ptr3;
262 for (int i = 0; i < elementCount; i++)
263 {
264 fixed (T* ptr8 = &data[i + startIndex])
265 {
266 try
267 {
268 if (isSetting)
269 {
270 _003CModule_003E.memcpy_s(ptr6, _size - (uint)offsetInBytes, ptr8, (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<T>());
271 if (IsWriteOnly && _pool == 1)
272 {
273 _003CModule_003E.memcpy_s(ptr7, _size - (uint)offsetInBytes, ptr8, (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<T>());
274 }
275 }
276 else
277 {
278 _003CModule_003E.memcpy_s(ptr8, num5, ptr6, (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<T>());
279 }
280 ptr6 = vertexStride + ptr6;
281 if (IsWriteOnly && _pool == 1)
282 {
283 ptr7 = vertexStride + ptr7;
284 }
285 }
286 catch
287 {
288 //try-fault
289 ptr8 = null;
290 throw;
291 }
292 }
293 }
294 }
295 }
296 }
297 finally
298 {
299 if (num2 >= 0)
300 {
301 IDirect3DVertexBuffer9* intPtr2 = pComPtr;
302 num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr2 + 48)))((nint)intPtr2);
303 }
304 }
305 if (num2 < 0)
306 {
307 throw GraphicsHelpers.GetExceptionFromResult((uint)num2);
308 }
309 if (isSetting && this is IDynamicGraphicsResource dynamicGraphicsResource)
310 {
311 dynamicGraphicsResource.SetContentLost(isContentLost: false);
312 }
313 return;
314 }
315 }
317 }
318
319 internal unsafe virtual int SaveDataForRecreation()
320 {
321 if (pComPtr == null)
322 {
323 return 0;
324 }
325 int num = 0;
326 if ((_usage & 8) == 8)
327 {
328 goto IL_0088;
329 }
330 void* ptr = null;
331 if ((pBufferData = _003CModule_003E.new_005B_005D(_size)) == null)
332 {
333 return -2147024882;
334 }
335 IDirect3DVertexBuffer9* ptr2 = pComPtr;
336 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, void**, uint, int>)(int)(*(uint*)(*(int*)ptr2 + 44)))((nint)ptr2, 0u, 0u, &ptr, 0u);
337 if (num >= 0)
338 {
339 uint size = _size;
340 _003CModule_003E.memcpy_s(pBufferData, size, ptr, size);
341 ptr2 = pComPtr;
342 IDirect3DVertexBuffer9* intPtr = ptr2;
343 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr + 48)))((nint)intPtr);
344 if (num >= 0)
345 {
346 goto IL_0088;
347 }
348 }
349 void* ptr3 = pBufferData;
350 if (ptr3 != null)
351 {
352 _003CModule_003E.delete_005B_005D(ptr3);
353 pBufferData = null;
354 }
355 goto IL_00a8;
356 IL_0088:
357 ReleaseNativeObject(disposeManagedResource: false);
358 goto IL_00a8;
359 IL_00a8:
360 return num;
361 }
362
364 {
365 //ILSpy generated this explicit interface implementation from .override directive in SaveDataForRecreation
366 return this.SaveDataForRecreation();
367 }
368
369 internal unsafe virtual int RecreateAndPopulateObject()
370 {
371 if (pComPtr != null)
372 {
373 return -2147467259;
374 }
375 if (pBufferData == null && _size != 0)
376 {
377 return -2147467259;
378 }
379 fixed (IDirect3DVertexBuffer9** ptr2 = &pComPtr)
380 {
381 GraphicsDevice parent = _parent;
382 IDirect3DDevice9* ptr = parent.pComPtr;
383 GraphicsDevice graphicsDevice = parent;
384 int num = *(int*)ptr + 104;
385 int num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, uint, _D3DPOOL, IDirect3DVertexBuffer9**, void**, int>)(int)(*(uint*)num))((nint)ptr, _size, graphicsDevice.GetBufferUsage(_usage), 0u, (_D3DPOOL)1, ptr2, null);
386 if (num2 >= 0)
387 {
388 void* ptr3 = null;
389 IDirect3DVertexBuffer9* ptr4 = pComPtr;
390 num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, void**, uint, int>)(int)(*(uint*)(*(int*)ptr4 + 44)))((nint)ptr4, 0u, 0u, &ptr3, 0u);
391 if (num2 >= 0)
392 {
393 uint size = _size;
394 _003CModule_003E.memcpy_s(ptr3, size, pBufferData, size);
395 ptr4 = pComPtr;
396 IDirect3DVertexBuffer9* intPtr = ptr4;
397 num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr + 48)))((nint)intPtr);
398 }
399 if (!((_usage & 8) == 8))
400 {
401 void* ptr5 = pBufferData;
402 if (ptr5 != null)
403 {
404 _003CModule_003E.delete_005B_005D(ptr5);
405 pBufferData = null;
406 }
407 }
408 }
410 return num2;
411 }
412 }
413
415 {
416 //ILSpy generated this explicit interface implementation from .override directive in RecreateAndPopulateObject
417 return this.RecreateAndPopulateObject();
418 }
419
420 internal unsafe virtual void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
421 {
422 GraphicsDevice parent = _parent;
423 if (parent != null && pComPtr != null)
424 {
425 parent.Resources.ReleaseAllReferences(_internalHandle, disposeManagedResource);
426 }
427 pComPtr = null;
428 }
429
430 void IGraphicsResource.ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
431 {
432 //ILSpy generated this explicit interface implementation from .override directive in ReleaseNativeObject
433 this.ReleaseNativeObject(disposeManagedResource);
434 }
435
436 private protected unsafe void CreateBuffer(VertexDeclaration vertexDeclaration, uint dwVertexCount, uint usage, _D3DPOOL pool)
437 {
438 isDisposed = false;
439 if (vertexDeclaration.isDisposed)
440 {
442 }
443 GraphicsDevice parent = ((GraphicsResource)vertexDeclaration)._parent;
444 GraphicsDevice parent2 = _parent;
445 if (parent != parent2)
446 {
447 vertexDeclaration.Bind(parent2);
448 }
449 uint num = (uint)vertexDeclaration._vertexStride * dwVertexCount;
450 int maxVertexBufferSize = _parent._profileCapabilities.MaxVertexBufferSize;
451 if (num > (uint)maxVertexBufferSize)
452 {
454 }
455 fixed (IDirect3DVertexBuffer9** ptr = &pComPtr)
456 {
457 int num2 = *(int*)_parent.pComPtr + 104;
458 int num3 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, uint, _D3DPOOL, IDirect3DVertexBuffer9**, void**, int>)(int)(*(uint*)num2))((nint)_parent.pComPtr, num, _parent.GetBufferUsage(usage), 0u, pool, ptr, null);
459 if (num3 < 0)
460 {
461 throw GraphicsHelpers.GetExceptionFromResult((uint)num3);
462 }
463 _usage = usage;
464 _size = num;
465 _pool = (uint)pool;
466 _vertexCount = dwVertexCount;
467 _vertexDeclaration = vertexDeclaration;
468 if ((usage & 8) == 8 && pool == (_D3DPOOL)1 && (pBufferData = _003CModule_003E.new_005B_005D(num)) == null)
469 {
470 throw new InsufficientMemoryException();
471 }
472 }
473 }
474
475 internal unsafe static VertexBuffer GetManagedObject(IDirect3DVertexBuffer9* pInterface, GraphicsDevice pDevice, uint pool)
476 {
477 VertexBuffer vertexBuffer = pDevice.Resources.GetCachedObject(pInterface) as VertexBuffer;
478 if (vertexBuffer != null)
479 {
480 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)pInterface + 8)))((nint)pInterface);
481 vertexBuffer.isDisposed = false;
482 GC.ReRegisterForFinalize(vertexBuffer);
483 }
484 else
485 {
486 vertexBuffer = new VertexBuffer(pInterface, pDevice);
487 pDevice.Resources.AddTrackedObject(vertexBuffer, pInterface, pool, 0uL, ref vertexBuffer._internalHandle);
488 }
489 return vertexBuffer;
490 }
491
492 private void OnObjectCreation()
493 {
494 }
495
496 private unsafe void _0021VertexBuffer()
497 {
498 if (!isDisposed)
499 {
500 ReleaseNativeObject(disposeManagedResource: true);
501 void* ptr = pBufferData;
502 if (ptr != null)
503 {
504 _003CModule_003E.delete_005B_005D(ptr);
505 pBufferData = null;
506 }
507 }
508 }
509
510 private void _007EVertexBuffer()
511 {
513 }
514
515 [HandleProcessCorruptedStateExceptions]
516 protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
517 {
518 if (P_0)
519 {
520 try
521 {
523 return;
524 }
525 finally
526 {
527 base.Dispose(true);
528 }
529 }
530 try
531 {
533 }
534 finally
535 {
536 base.Dispose(false);
537 }
538 }
539}
unsafe void AddTrackedObject(object managedObject, void *pComPtr, uint resourceManagementMode, ulong handle, ref ulong updatedHandle)
unsafe void ReleaseAllReferences(ulong handle, [MarshalAs(UnmanagedType.U1)] bool dispose)
virtual void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
static Exception GetExceptionFromResult(uint result)
unsafe VertexBuffer(GraphicsDevice graphicsDevice, VertexDeclaration vertexDeclaration, int vertexCount, BufferUsage usage)
unsafe VertexBuffer(IDirect3DVertexBuffer9 *pInterface, GraphicsDevice pDevice)
virtual unsafe void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
static unsafe VertexBuffer GetManagedObject(IDirect3DVertexBuffer9 *pInterface, GraphicsDevice pDevice, uint pool)
unsafe VertexBuffer(GraphicsDevice graphicsDevice, Type vertexType, int vertexCount, BufferUsage usage)
override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
unsafe void CopyData< T >(int offsetInBytes, T[] data, int startIndex, int elementCount, int vertexStride, uint options, [MarshalAs(UnmanagedType.U1)] bool isSetting)
unsafe IDirect3DVertexBuffer9 * pComPtr
unsafe void CreateBuffer(VertexDeclaration vertexDeclaration, uint dwVertexCount, uint usage, _D3DPOOL pool)
static VertexDeclaration FromType(Type vertexType)
static void CheckDisposed(object obj, IntPtr pComPtr)
Definition Helpers.cs:188
static void ValidateCopyParameters(int dataLength, int dataIndex, int elementCount)
Definition Helpers.cs:66
static void ReRegisterForFinalize(object obj)
Definition GC.cs:214
Definition GC.cs:8
static unsafe void InitBlock(void *startAddress, byte value, uint byteCount)
Definition Unsafe.cs:117
void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)