Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IndexBuffer.cs
Go to the documentation of this file.
1using System;
5
7
9{
10 private protected uint _usage;
11
12 private protected uint _indexCount;
13
14 private protected uint _indexSize;
15
16 private protected uint _bufferSize;
17
18 private protected uint _pool;
19
20 private unsafe void* pBufferData;
21
22 internal unsafe IDirect3DIndexBuffer9* pComPtr;
23
24 internal bool IsWriteOnly
25 {
26 [return: MarshalAs(UnmanagedType.U1)]
27 get
28 {
29 return (_usage & 8) == 8;
30 }
31 }
32
33 public int IndexCount => (int)_indexCount;
34
36
37 public BufferUsage BufferUsage => _003CModule_003E.ConvertDxBufferUsageToXna(_usage);
38
39 private unsafe IndexBuffer(IDirect3DIndexBuffer9* 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 public unsafe IndexBuffer(GraphicsDevice graphicsDevice, Type indexType, int indexCount, BufferUsage usage)
57 {
58 try
59 {
60 if (indexCount <= 0)
61 {
63 }
64 _parent = graphicsDevice;
65 uint indexSize = (uint)Marshal.SizeOf(indexType);
66 CreateBuffer((uint)indexCount, indexSize, _003CModule_003E.ConvertXnaBufferUsageToDx(usage), (_D3DPOOL)1);
68 return;
69 }
70 catch
71 {
72 //try-fault
73 base.Dispose(true);
74 throw;
75 }
76 }
77
78 public unsafe IndexBuffer(GraphicsDevice graphicsDevice, IndexElementSize indexElementSize, int indexCount, BufferUsage usage)
79 {
80 try
81 {
82 if (indexCount <= 0)
83 {
85 }
86 _parent = graphicsDevice;
87 int indexSize = ((indexElementSize == IndexElementSize.SixteenBits) ? 2 : 4);
88 CreateBuffer((uint)indexCount, (uint)indexSize, _003CModule_003E.ConvertXnaBufferUsageToDx(usage), (_D3DPOOL)1);
90 return;
91 }
92 catch
93 {
94 //try-fault
95 base.Dispose(true);
96 throw;
97 }
98 }
99
100 private protected IndexBuffer()
101 {
102 }
103
104 public void SetData<T>(T[] data) where T : struct
105 {
106 int elementCount = ((data != null) ? data.Length : 0);
107 SetData(0, data, 0, elementCount);
108 }
109
110 public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
111 {
112 SetData(0, data, startIndex, elementCount);
113 }
114
115 public void SetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct
116 {
117 CopyData(offsetInBytes, data, startIndex, elementCount, 0u, isSetting: true);
118 }
119
120 public void GetData<T>(T[] data) where T : struct
121 {
122 int elementCount = ((data != null) ? data.Length : 0);
123 GetData(0, data, 0, elementCount);
124 }
125
126 public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
127 {
128 GetData(0, data, startIndex, elementCount);
129 }
130
131 public void GetData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount) where T : struct
132 {
133 CopyData(offsetInBytes, data, startIndex, elementCount, 16u, isSetting: false);
134 }
135
136 private protected unsafe void CopyData<T>(int offsetInBytes, T[] data, int startIndex, int elementCount, uint options, [MarshalAs(UnmanagedType.U1)] bool isSetting) where T : struct
137 {
138 IntPtr intPtr = (IntPtr)pComPtr;
139 Helpers.CheckDisposed(this, intPtr);
140 if (data != null && data.Length != 0)
141 {
142 int num = -2147467259;
143 if ((options & 0x3010) == 0 && _parent.Indices == this)
144 {
145 throw GraphicsHelpers.GetExceptionFromResult(2147500036u);
146 }
147 if (!isSetting && (_usage & 8) == 8)
148 {
150 }
151 Helpers.ValidateCopyParameters(data.Length, startIndex, elementCount);
152 try
153 {
154 uint num2 = (uint)(System.Runtime.CompilerServices.Unsafe.SizeOf<T>() * elementCount);
155 if ((uint)((int)num2 + offsetInBytes) > _bufferSize)
156 {
158 }
160 {
161 if (isSetting)
162 {
163 return;
164 }
165 fixed (T* ptr = &data[startIndex])
166 {
167 try
168 {
169 // IL initblk instruction
171 }
172 catch
173 {
174 //try-fault
175 ptr = null;
176 throw;
177 }
178 }
179 return;
180 }
181 void* ptr2 = null;
182 byte* ptr3 = (byte*)pBufferData;
183 if (IsWriteOnly && _pool == 1)
184 {
185 ptr3 = offsetInBytes + ptr3;
186 }
187 IDirect3DIndexBuffer9* ptr4 = pComPtr;
188 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, void**, uint, int>)(int)(*(uint*)(*(int*)ptr4 + 44)))((nint)ptr4, (uint)offsetInBytes, num2, &ptr2, options);
189 if (num >= 0)
190 {
191 fixed (T* ptr5 = &data[startIndex])
192 {
193 try
194 {
195 if (isSetting)
196 {
197 _003CModule_003E.memcpy_s(ptr2, _bufferSize - (uint)offsetInBytes, ptr5, num2);
198 if (IsWriteOnly && _pool == 1)
199 {
200 _003CModule_003E.memcpy_s(ptr3, _bufferSize - (uint)offsetInBytes, ptr5, num2);
201 }
202 }
203 else
204 {
205 _003CModule_003E.memcpy_s(ptr5, num2, ptr2, num2);
206 }
207 }
208 catch
209 {
210 //try-fault
211 ptr5 = null;
212 throw;
213 }
214 }
215 }
216 }
217 finally
218 {
219 if (num >= 0)
220 {
221 IDirect3DIndexBuffer9* intPtr2 = pComPtr;
222 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr2 + 48)))((nint)intPtr2);
223 }
224 }
225 if (num < 0)
226 {
228 }
229 if (isSetting && this is IDynamicGraphicsResource dynamicGraphicsResource)
230 {
231 dynamicGraphicsResource.SetContentLost(isContentLost: false);
232 }
233 return;
234 }
236 }
237
238 private protected unsafe void CreateBuffer(uint indexCount, uint indexSize, uint usage, _D3DPOOL pool)
239 {
240 isDisposed = false;
241 _D3DFORMAT d3DFORMAT;
242 if (indexSize == 4)
243 {
244 d3DFORMAT = (_D3DFORMAT)102;
246 {
248 }
249 }
250 else
251 {
252 if (indexSize != 2)
253 {
255 }
256 d3DFORMAT = (_D3DFORMAT)101;
257 }
258 uint num = (_bufferSize = indexCount * indexSize);
259 int maxIndexBufferSize = _parent._profileCapabilities.MaxIndexBufferSize;
260 if (num > (uint)maxIndexBufferSize)
261 {
263 }
264 fixed (IDirect3DIndexBuffer9** ptr = &pComPtr)
265 {
266 int num2 = *(int*)_parent.pComPtr + 108;
267 int num3 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, _D3DFORMAT, _D3DPOOL, IDirect3DIndexBuffer9**, void**, int>)(int)(*(uint*)num2))((nint)_parent.pComPtr, _bufferSize, _parent.GetBufferUsage(usage), d3DFORMAT, pool, ptr, null);
268 if (num3 < 0)
269 {
270 throw GraphicsHelpers.GetExceptionFromResult((uint)num3);
271 }
272 _usage = usage;
273 _indexCount = indexCount;
274 _indexSize = indexSize;
275 _pool = (uint)pool;
276 if ((usage & 8) == 8 && pool == (_D3DPOOL)1 && (pBufferData = _003CModule_003E.new_005B_005D(_bufferSize)) == null)
277 {
278 throw new InsufficientMemoryException();
279 }
280 }
281 }
282
283 internal unsafe virtual int SaveDataForRecreation()
284 {
285 if (pComPtr == null)
286 {
287 return 0;
288 }
289 int num = 0;
290 if ((_usage & 8) == 8)
291 {
292 goto IL_0088;
293 }
294 void* ptr = null;
295 if ((pBufferData = _003CModule_003E.new_005B_005D(_bufferSize)) == null)
296 {
297 return -2147024882;
298 }
299 IDirect3DIndexBuffer9* ptr2 = pComPtr;
300 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, void**, uint, int>)(int)(*(uint*)(*(int*)ptr2 + 44)))((nint)ptr2, 0u, 0u, &ptr, 0u);
301 if (num >= 0)
302 {
303 uint bufferSize = _bufferSize;
304 _003CModule_003E.memcpy_s(pBufferData, bufferSize, ptr, bufferSize);
305 ptr2 = pComPtr;
306 IDirect3DIndexBuffer9* intPtr = ptr2;
307 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr + 48)))((nint)intPtr);
308 if (num >= 0)
309 {
310 goto IL_0088;
311 }
312 }
313 void* ptr3 = pBufferData;
314 if (ptr3 != null)
315 {
316 _003CModule_003E.delete_005B_005D(ptr3);
317 pBufferData = null;
318 }
319 goto IL_00a8;
320 IL_0088:
321 ReleaseNativeObject(disposeManagedResource: false);
322 goto IL_00a8;
323 IL_00a8:
324 return num;
325 }
326
328 {
329 //ILSpy generated this explicit interface implementation from .override directive in SaveDataForRecreation
330 return this.SaveDataForRecreation();
331 }
332
333 internal unsafe virtual int RecreateAndPopulateObject()
334 {
335 if (pComPtr != null)
336 {
337 return -2147467259;
338 }
339 if (pBufferData == null && _bufferSize != 0)
340 {
341 return -2147467259;
342 }
343 fixed (IDirect3DIndexBuffer9** ptr2 = &pComPtr)
344 {
345 _D3DFORMAT d3DFORMAT = ((_indexSize == 4) ? ((_D3DFORMAT)102) : ((_D3DFORMAT)101));
346 GraphicsDevice parent = _parent;
347 IDirect3DDevice9* ptr = parent.pComPtr;
348 GraphicsDevice graphicsDevice = parent;
349 int num = *(int*)ptr + 108;
350 int num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, _D3DFORMAT, _D3DPOOL, IDirect3DIndexBuffer9**, void**, int>)(int)(*(uint*)num))((nint)ptr, _bufferSize, graphicsDevice.GetBufferUsage(_usage), d3DFORMAT, (_D3DPOOL)1, ptr2, null);
351 if (num2 >= 0)
352 {
353 void* ptr3 = null;
354 IDirect3DIndexBuffer9* ptr4 = pComPtr;
355 num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, void**, uint, int>)(int)(*(uint*)(*(int*)ptr4 + 44)))((nint)ptr4, 0u, 0u, &ptr3, 0u);
356 if (num2 >= 0)
357 {
358 uint bufferSize = _bufferSize;
359 _003CModule_003E.memcpy_s(ptr3, bufferSize, pBufferData, bufferSize);
360 ptr4 = pComPtr;
361 IDirect3DIndexBuffer9* intPtr = ptr4;
362 num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr + 48)))((nint)intPtr);
363 }
364 if (!((_usage & 8) == 8))
365 {
366 void* ptr5 = pBufferData;
367 if (ptr5 != null)
368 {
369 _003CModule_003E.delete_005B_005D(ptr5);
370 pBufferData = null;
371 }
372 }
373 }
375 return num2;
376 }
377 }
378
380 {
381 //ILSpy generated this explicit interface implementation from .override directive in RecreateAndPopulateObject
382 return this.RecreateAndPopulateObject();
383 }
384
385 internal unsafe virtual void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
386 {
387 GraphicsDevice parent = _parent;
388 if (parent != null && pComPtr != null)
389 {
390 parent.Resources.ReleaseAllReferences(_internalHandle, disposeManagedResource);
391 }
392 pComPtr = null;
393 }
394
395 void IGraphicsResource.ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
396 {
397 //ILSpy generated this explicit interface implementation from .override directive in ReleaseNativeObject
398 this.ReleaseNativeObject(disposeManagedResource);
399 }
400
401 internal unsafe static IndexBuffer GetManagedObject(IDirect3DIndexBuffer9* pInterface, GraphicsDevice pDevice, uint pool)
402 {
403 IndexBuffer indexBuffer = pDevice.Resources.GetCachedObject(pInterface) as IndexBuffer;
404 if (indexBuffer != null)
405 {
406 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)pInterface + 8)))((nint)pInterface);
407 indexBuffer.isDisposed = false;
408 GC.ReRegisterForFinalize(indexBuffer);
409 }
410 else
411 {
412 indexBuffer = new IndexBuffer(pInterface, pDevice);
413 pDevice.Resources.AddTrackedObject(indexBuffer, pInterface, pool, 0uL, ref indexBuffer._internalHandle);
414 }
415 return indexBuffer;
416 }
417
418 private void OnObjectCreation()
419 {
420 }
421
422 private unsafe void _0021IndexBuffer()
423 {
424 if (!isDisposed)
425 {
426 ReleaseNativeObject(disposeManagedResource: true);
427 void* ptr = pBufferData;
428 if (ptr != null)
429 {
430 _003CModule_003E.delete_005B_005D(ptr);
431 pBufferData = null;
432 }
433 }
434 }
435
436 private void _007EIndexBuffer()
437 {
439 }
440
441 [HandleProcessCorruptedStateExceptions]
442 protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
443 {
444 if (P_0)
445 {
446 try
447 {
449 return;
450 }
451 finally
452 {
453 base.Dispose(true);
454 }
455 }
456 try
457 {
459 }
460 finally
461 {
462 base.Dispose(false);
463 }
464 }
465}
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 void CreateBuffer(uint indexCount, uint indexSize, uint usage, _D3DPOOL pool)
unsafe IndexBuffer(GraphicsDevice graphicsDevice, IndexElementSize indexElementSize, int indexCount, BufferUsage usage)
unsafe void CopyData< T >(int offsetInBytes, T[] data, int startIndex, int elementCount, uint options, [MarshalAs(UnmanagedType.U1)] bool isSetting)
virtual unsafe void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
unsafe IndexBuffer(GraphicsDevice graphicsDevice, Type indexType, int indexCount, BufferUsage usage)
static unsafe IndexBuffer GetManagedObject(IDirect3DIndexBuffer9 *pInterface, GraphicsDevice pDevice, uint pool)
unsafe IDirect3DIndexBuffer9 * pComPtr
unsafe IndexBuffer(IDirect3DIndexBuffer9 *pInterface, GraphicsDevice pDevice)
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
static int SizeOf(object structure)
Definition Marshal.cs:697
void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)