Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Texture2D.cs
Go to the documentation of this file.
1using System;
2using System.IO;
6using Microsoft.Xna.Framework.Graphics.PackedVector;
7using Microsoft.Xna.Framework.Media;
8using SharedConstants;
9
11
13{
14 private protected int _width;
15
16 private protected int _height;
17
18 private protected bool _shouldNotRecreate;
19
20 private IntPtr[] pFaceData;
21
22 internal unsafe IDirect3DTexture9* pComPtr;
23
24 private protected override bool MustClamp
25 {
26 [return: MarshalAs(UnmanagedType.U1)]
27 get
28 {
30 {
31 return false;
32 }
34 {
35 return false;
36 }
37 return true;
38 }
39 }
40
41 public Rectangle Bounds => new Rectangle(0, 0, _width, _height);
42
43 public int Height => _height;
44
45 public int Width => _width;
46
47 private unsafe Texture2D(IDirect3DTexture9* pInterface, GraphicsDevice pDevice)
48 {
49 pComPtr = pInterface;
50 ((object)this)._002Ector();
51 try
52 {
53 _parent = pDevice;
55 return;
56 }
57 catch
58 {
59 //try-fault
60 base.Dispose(true);
61 throw;
62 }
63 }
64
65 private protected Texture2D()
66 {
67 }
68
69 public Texture2D(GraphicsDevice graphicsDevice, int width, int height)
70 {
71 try
72 {
73 CreateTexture(graphicsDevice, width, height, mipMap: false, 0u, (_D3DPOOL)1, SurfaceFormat.Color);
74 return;
75 }
76 catch
77 {
78 //try-fault
79 base.Dispose(true);
80 throw;
81 }
82 }
83
84 public Texture2D(GraphicsDevice graphicsDevice, int width, int height, [MarshalAs(UnmanagedType.U1)] bool mipMap, SurfaceFormat format)
85 {
86 try
87 {
88 CreateTexture(graphicsDevice, width, height, mipMap, 0u, (_D3DPOOL)1, format);
89 return;
90 }
91 catch
92 {
93 //try-fault
94 base.Dispose(true);
95 throw;
96 }
97 }
98
99 internal Texture2D(GraphicsDevice graphicsDevice, int width, int height, [MarshalAs(UnmanagedType.U1)] bool mipMap, SurfaceFormat format, [MarshalAs(UnmanagedType.U1)] bool __unnamed005)
100 {
101 try
102 {
103 CreateTexture(graphicsDevice, width, height, mipMap, 0u, (_D3DPOOL)1, format);
104 return;
105 }
106 catch
107 {
108 //try-fault
109 base.Dispose(true);
110 throw;
111 }
112 }
113
114 internal unsafe Texture2D(GraphicsDevice graphicsDevice, Stream stream, int width, int height, SharedConstants.XnaImageOperation operation)
115 {
116 try
117 {
118 if (graphicsDevice == null)
119 {
121 }
122 if (stream == null)
123 {
125 }
126 if (!stream.CanSeek)
127 {
129 }
130 ValidateCreationParameters(graphicsDevice._profileCapabilities, width, height, SurfaceFormat.Color, mipMap: false);
131 ImageStream imageStream = stream as ImageStream;
132 try
133 {
134 if (imageStream == null)
135 {
136 imageStream = ImageStream.FromStream(stream);
137 }
138 void* ptr = null;
139 Helpers.ThrowExceptionFromErrorCode(Microsoft.Xna.Framework.Media.UnsafeNativeMethods.DecodeStreamToTexture(graphicsDevice.pComPtr, imageStream.Handle, &width, &height, (XnaImageOperation)operation, graphicsDevice._profileCapabilities.MaxTextureAspectRatio, &ptr));
140 pComPtr = (IDirect3DTexture9*)ptr;
141 }
142 finally
143 {
144 if (imageStream != null && imageStream != stream)
145 {
146 ((IDisposable)imageStream).Dispose();
147 }
148 }
149 isDisposed = false;
150 _parent = graphicsDevice;
153 graphicsDevice.Resources.AddTrackedObject(this, pComPtr, 1u, _internalHandle, ref _internalHandle);
154 return;
155 }
156 catch
157 {
158 //try-fault
159 base.Dispose(true);
160 throw;
161 }
162 }
163
164 public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream, int width, int height, [MarshalAs(UnmanagedType.U1)] bool zoom)
165 {
166 SharedConstants.XnaImageOperation operation = ((!zoom) ? ((SharedConstants.XnaImageOperation)1) : ((SharedConstants.XnaImageOperation)3));
167 return new Texture2D(graphicsDevice, stream, width, height, operation);
168 }
169
170 public static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
171 {
172 int maxTextureSize = graphicsDevice._profileCapabilities.MaxTextureSize;
173 return new Texture2D(graphicsDevice, stream, maxTextureSize, maxTextureSize, (SharedConstants.XnaImageOperation)0);
174 }
175
176 public void SaveAsJpeg(Stream stream, int width, int height)
177 {
178 SaveAsImage(stream, (SharedConstants.XnaImageFormat)0, width, height);
179 }
180
181 public void SaveAsPng(Stream stream, int width, int height)
182 {
183 SaveAsImage(stream, (SharedConstants.XnaImageFormat)2, width, height);
184 }
185
186 public void SetData<T>(T[] data) where T : struct
187 {
188 Rectangle? rect = null;
189 int elementCount = ((data != null) ? data.Length : 0);
190 SetData(0, rect, data, 0, elementCount);
191 }
192
193 public void SetData<T>(T[] data, int startIndex, int elementCount) where T : struct
194 {
195 SetData(0, null, data, startIndex, elementCount);
196 }
197
198 public void SetData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
199 {
200 CopyData(level, rect, data, startIndex, elementCount, 0u, isSetting: true);
201 }
202
203 public void GetData<T>(T[] data) where T : struct
204 {
205 Rectangle? rect = null;
206 int elementCount = ((data != null) ? data.Length : 0);
207 GetData(0, rect, data, 0, elementCount);
208 }
209
210 public void GetData<T>(T[] data, int startIndex, int elementCount) where T : struct
211 {
212 GetData(0, null, data, startIndex, elementCount);
213 }
214
215 public void GetData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount) where T : struct
216 {
217 CopyData(level, rect, data, startIndex, elementCount, 16u, isSetting: false);
218 }
219
220 private unsafe void CopyData<T>(int level, Rectangle? rect, T[] data, int startIndex, int elementCount, uint options, [MarshalAs(UnmanagedType.U1)] bool isSetting) where T : struct
221 {
222 IntPtr intPtr = (IntPtr)pComPtr;
223 Helpers.CheckDisposed(this, intPtr);
224 if (data != null && data.Length != 0)
225 {
227 {
229 }
230 if (isSetting)
231 {
232 int num = 0;
234 {
235 do
236 {
237 if (_parent.Textures[num] != this)
238 {
239 num++;
240 continue;
241 }
242 throw GraphicsHelpers.GetExceptionFromResult(2147500036u);
243 }
244 while (num < _parent.Textures._maxTextures);
245 }
246 int num2 = 0;
248 {
249 do
250 {
251 if (_parent.VertexTextures[num2] != this)
252 {
253 num2++;
254 continue;
255 }
256 throw GraphicsHelpers.GetExceptionFromResult(2147500036u);
257 }
258 while (num2 < _parent.VertexTextures._maxTextures);
259 }
260 }
261 System.Runtime.CompilerServices.Unsafe.SkipInit(out _D3DSURFACE_DESC d3DSURFACE_DESC);
262 // IL initblk instruction
263 System.Runtime.CompilerServices.Unsafe.InitBlock(ref d3DSURFACE_DESC, 0, 32);
264 IDirect3DTexture9* ptr = pComPtr;
265 int num3 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, _D3DSURFACE_DESC*, int>)(int)(*(uint*)(*(int*)ptr + 68)))((nint)ptr, (uint)level, &d3DSURFACE_DESC);
266 if (num3 < 0)
267 {
268 throw GraphicsHelpers.GetExceptionFromResult((uint)num3);
269 }
270 Helpers.ValidateCopyParameters(data.Length, startIndex, elementCount);
271 System.Runtime.CompilerServices.Unsafe.SkipInit(out uint dwFormatSize);
272 System.Runtime.CompilerServices.Unsafe.SkipInit(out uint dwElementSize);
273 Texture.GetAndValidateSizes<T>(&d3DSURFACE_DESC, &dwFormatSize, &dwElementSize);
274 System.Runtime.CompilerServices.Unsafe.SkipInit(out uint dwLockWidth);
275 System.Runtime.CompilerServices.Unsafe.SkipInit(out uint dwLockHeight);
276 Texture.GetAndValidateRect(&d3DSURFACE_DESC, &dwLockWidth, &dwLockHeight, rect);
277 Texture.ValidateTotalSize(&d3DSURFACE_DESC, dwLockWidth, dwLockHeight, dwFormatSize, dwElementSize, (uint)elementCount);
278 tagRECT* ptr2 = null;
279 Rectangle rectangle = default(Rectangle);
280 if (rect.HasValue)
281 {
282 rectangle = rect.Value;
283 ptr2 = (tagRECT*)(int)(ref rectangle);
284 if (ptr2 != null)
285 {
286 *(int*)((byte*)ptr2 + 8) += *(int*)ptr2;
287 *(int*)((byte*)ptr2 + 12) += *(int*)((byte*)ptr2 + 4);
288 }
289 }
290 int num5;
291 System.Runtime.CompilerServices.Unsafe.SkipInit(out _D3DLOCKED_RECT d3DLOCKED_RECT);
292 if (System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, int>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 12)) == 0)
293 {
294 if (!isSetting)
295 {
296 GraphicsDevice parent = _parent;
297 GraphicsDevice graphicsDevice = parent;
298 GraphicsDevice graphicsDevice2 = parent;
299 GraphicsDevice graphicsDevice3 = parent;
300 int num4 = *(int*)GraphicsAdapter.pComPtr + 40;
301 if (((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, _D3DDEVTYPE, _D3DFORMAT, uint, _D3DRESOURCETYPE, _D3DFORMAT, int>)(int)(*(uint*)num4))((nint)GraphicsAdapter.pComPtr, graphicsDevice3.Adapter.adapter, graphicsDevice2._deviceType, _003CModule_003E.ConvertXnaFormatToWindowsAdapterFormat(graphicsDevice.Adapter.CurrentDisplayMode.Format), 1u, (_D3DRESOURCETYPE)3, *(_D3DFORMAT*)(&d3DSURFACE_DESC)) < 0)
302 {
304 }
305 }
306 IDirect3DSurface9* ptr3 = null;
307 IDirect3DTexture9* ptr4 = null;
308 IDirect3DSurface9* ptr5 = null;
309 IDirect3DDevice9* ptr6 = _parent.pComPtr;
310 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, uint, uint, _D3DFORMAT, _D3DPOOL, IDirect3DTexture9**, void**, int>)(int)(*(uint*)(*(int*)ptr6 + 92)))((nint)ptr6, System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 24)), System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 28)), 1u, 0u, *(_D3DFORMAT*)(&d3DSURFACE_DESC), (_D3DPOOL)2, &ptr4, null);
311 if (num5 >= 0)
312 {
313 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, IDirect3DSurface9**, int>)(int)(*(uint*)(*(int*)ptr4 + 72)))((nint)ptr4, 0u, &ptr3);
314 if (num5 >= 0)
315 {
316 ptr = pComPtr;
317 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, IDirect3DSurface9**, int>)(int)(*(uint*)(*(int*)ptr + 72)))((nint)ptr, (uint)level, &ptr5);
318 if (num5 >= 0)
319 {
320 if (isSetting)
321 {
322 if (ptr2 != null && (*(int*)ptr2 != 0 || *(int*)((byte*)ptr2 + 4) != 0 || *(int*)((byte*)ptr2 + 8) != System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, int>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 24)) || *(int*)((byte*)ptr2 + 12) != System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, int>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 28))))
323 {
324 num5 = _003CModule_003E.D3DXLoadSurfaceFromSurface(ptr3, null, null, ptr5, null, null, 1u, 0u);
325 }
326 if (num5 >= 0)
327 {
328 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, _D3DLOCKED_RECT*, tagRECT*, uint, int>)(int)(*(uint*)(*(int*)ptr3 + 52)))((nint)ptr3, &d3DLOCKED_RECT, ptr2, 0u);
329 if (num5 >= 0)
330 {
331 try
332 {
333 Texture.CopyData((void*)(int)System.Runtime.CompilerServices.Unsafe.As<_D3DLOCKED_RECT, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DLOCKED_RECT, 4)), *(int*)(&d3DLOCKED_RECT), data, startIndex, elementCount, &d3DSURFACE_DESC, dwLockWidth, dwLockHeight, isSetting);
334 }
335 finally
336 {
337 IDirect3DSurface9* intPtr2 = ptr3;
338 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr2 + 56)))((nint)intPtr2);
339 }
340 if (num5 >= 0)
341 {
342 num5 = _003CModule_003E.D3DXLoadSurfaceFromSurface(ptr5, null, ptr2, ptr3, null, ptr2, 1u, 0u);
343 }
344 }
345 }
346 }
347 else
348 {
349 num5 = _003CModule_003E.D3DXLoadSurfaceFromSurface(ptr3, null, ptr2, ptr5, null, ptr2, 1u, 0u);
350 if (num5 >= 0)
351 {
352 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, _D3DLOCKED_RECT*, tagRECT*, uint, int>)(int)(*(uint*)(*(int*)ptr3 + 52)))((nint)ptr3, &d3DLOCKED_RECT, ptr2, 0u);
353 if (num5 >= 0)
354 {
355 try
356 {
357 Texture.CopyData((void*)(int)System.Runtime.CompilerServices.Unsafe.As<_D3DLOCKED_RECT, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DLOCKED_RECT, 4)), *(int*)(&d3DLOCKED_RECT), data, startIndex, elementCount, &d3DSURFACE_DESC, dwLockWidth, dwLockHeight, isSetting: false);
358 }
359 finally
360 {
361 IDirect3DSurface9* intPtr3 = ptr3;
362 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, int>)(int)(*(uint*)(*(int*)intPtr3 + 56)))((nint)intPtr3);
363 }
364 }
365 }
366 }
367 }
368 }
369 }
370 if (ptr5 != null)
371 {
372 IDirect3DSurface9* intPtr4 = ptr5;
373 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)intPtr4 + 8)))((nint)intPtr4);
374 ptr5 = null;
375 }
376 if (ptr3 != null)
377 {
378 IDirect3DSurface9* intPtr5 = ptr3;
379 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)intPtr5 + 8)))((nint)intPtr5);
380 ptr3 = null;
381 }
382 if (ptr4 != null)
383 {
384 IDirect3DTexture9* intPtr6 = ptr4;
385 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)intPtr6 + 8)))((nint)intPtr6);
386 ptr4 = null;
387 }
388 }
389 else
390 {
391 ptr = pComPtr;
392 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, _D3DLOCKED_RECT*, tagRECT*, uint, int>)(int)(*(uint*)(*(int*)ptr + 76)))((nint)ptr, (uint)level, &d3DLOCKED_RECT, ptr2, options);
393 if (num5 >= 0)
394 {
395 try
396 {
397 Texture.CopyData((void*)(int)System.Runtime.CompilerServices.Unsafe.As<_D3DLOCKED_RECT, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DLOCKED_RECT, 4)), *(int*)(&d3DLOCKED_RECT), data, startIndex, elementCount, &d3DSURFACE_DESC, dwLockWidth, dwLockHeight, isSetting);
398 }
399 finally
400 {
401 ptr = pComPtr;
402 num5 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, int>)(int)(*(uint*)(*(int*)ptr + 80)))((nint)ptr, (uint)level);
403 }
404 }
405 }
407 {
408 if (isSetting)
409 {
410 return;
411 }
412 fixed (T* ptr7 = &data[startIndex])
413 {
414 try
415 {
416 uint num6 = (uint)(System.Runtime.CompilerServices.Unsafe.SizeOf<T>() * elementCount);
417 // IL initblk instruction
419 }
420 catch
421 {
422 //try-fault
423 ptr7 = null;
424 throw;
425 }
426 }
427 return;
428 }
429 if (num5 < 0)
430 {
431 throw GraphicsHelpers.GetExceptionFromResult((uint)num5);
432 }
433 if (isSetting)
434 {
435 if (this is IDynamicGraphicsResource dynamicGraphicsResource)
436 {
437 dynamicGraphicsResource.SetContentLost(isContentLost: false);
438 }
440 }
441 return;
442 }
444 }
445
446 private void SaveAsImage(Stream stream, SharedConstants.XnaImageFormat __unnamed001, int width, int height)
447 {
448 if (stream == null)
449 {
451 }
452 if (!stream.CanWrite)
453 {
454 throw new ArgumentException("stream");
455 }
456 if (__unnamed001 != 0 && __unnamed001 != (SharedConstants.XnaImageFormat)2)
457 {
458 throw new ArgumentException("format");
459 }
461 Color[] array2;
462 int num;
463 int height3;
464 int width3;
465 switch (format)
466 {
467 case SurfaceFormat.Color:
468 {
469 int width2 = _width;
470 array2 = new Color[_height * width2];
471 GetData(array2);
472 goto IL_01da;
473 }
474 case SurfaceFormat.Bgr565:
475 array2 = GetDataAsColor<Bgr565>();
476 goto IL_01da;
477 case SurfaceFormat.Bgra5551:
478 array2 = GetDataAsColor<Bgra5551>();
479 goto IL_01da;
480 case SurfaceFormat.Bgra4444:
481 array2 = GetDataAsColor<Bgra4444>();
482 goto IL_01da;
483 case SurfaceFormat.NormalizedByte2:
484 array2 = GetDataAsColor<NormalizedByte2>();
485 goto IL_01da;
486 case SurfaceFormat.NormalizedByte4:
487 array2 = GetDataAsColor<NormalizedByte4>();
488 goto IL_01da;
489 case SurfaceFormat.Rgba1010102:
490 array2 = GetDataAsColor<Rgba1010102>();
491 goto IL_01da;
492 case SurfaceFormat.Rg32:
493 array2 = GetDataAsColor<Rg32>();
494 goto IL_01da;
495 case SurfaceFormat.Rgba64:
496 array2 = GetDataAsColor<Rgba64>();
497 goto IL_01da;
498 case SurfaceFormat.Alpha8:
499 array2 = GetDataAsColor<Alpha8>();
500 goto IL_01da;
501 case SurfaceFormat.HalfSingle:
502 array2 = GetDataAsColor<HalfSingle>();
503 goto IL_01da;
504 case SurfaceFormat.HalfVector2:
505 array2 = GetDataAsColor<HalfVector2>();
506 goto IL_01da;
507 case SurfaceFormat.HalfVector4:
508 array2 = GetDataAsColor<HalfVector4>();
509 goto IL_01da;
510 case SurfaceFormat.HdrBlendable:
511 array2 = GetDataAsColor<HalfVector4>();
512 goto IL_01da;
513 case SurfaceFormat.Single:
514 array2 = GetDataAsColor<float>(_003CModule_003E._003FA0x8419bba8_002ESingleToColor);
515 goto IL_01da;
516 case SurfaceFormat.Vector2:
517 array2 = GetDataAsColor<Vector2>(_003CModule_003E._003FA0x8419bba8_002EVector2ToColor);
518 goto IL_01da;
519 case SurfaceFormat.Vector4:
520 array2 = GetDataAsColor<Vector4>(_003CModule_003E._003FA0x8419bba8_002EVector4ToColor);
521 goto IL_01da;
522 case SurfaceFormat.Dxt1:
523 case SurfaceFormat.Dxt3:
524 case SurfaceFormat.Dxt5:
525 {
526 SurfaceFormat format2 = format;
527 int height2 = _height;
528 DxtDecoder dxtDecoder = new DxtDecoder(_width, height2, format2);
529 byte[] array = new byte[dxtDecoder.PackedDataSize];
530 GetData(array);
531 array2 = dxtDecoder.Decode(array);
532 goto IL_01da;
533 }
534 default:
535 {
536 throw new InvalidOperationException();
537 }
538 IL_01da:
539 num = 0;
540 if (0 < (nint)array2.LongLength)
541 {
542 do
543 {
544 if (array2[num].A == 0)
545 {
546 Color transparent = Color.Transparent;
547 array2[num] = transparent;
548 }
549 num++;
550 }
551 while (num < (nint)array2.LongLength);
552 }
553 height3 = _height;
554 width3 = _width;
555 using (ImageStream imageStream = ImageStream.FromColors(array2, width3, height3, (XnaImageFormat)__unnamed001, width, height))
556 {
557 byte[] array3 = new BinaryReader(imageStream).ReadBytes((int)imageStream.Length);
558 stream.Write(array3, 0, array3.Length);
559 break;
560 }
561 }
562 }
563
564 private Color[] GetDataAsColor<T>(Converter<T, Color> toColor) where T : struct
565 {
566 int num = _height * _width;
567 T[] array = new T[num];
568 GetData(array);
569 Color[] array2 = new Color[num];
570 int num2 = 0;
571 if (0 < num)
572 {
573 do
574 {
575 Color color = toColor(array[num2]);
576 array2[num2] = color;
577 num2++;
578 }
579 while (num2 < num);
580 }
581 return array2;
582 }
583
584 private Color[] GetDataAsColor<T>() where T : struct, IPackedVector
585 {
586 return GetDataAsColor<T>(_003CModule_003E._003FA0x8419bba8_002EPackedVectorToColor);
587 }
588
589 private protected unsafe void InitializeDescription(SurfaceFormat? format)
590 {
591 IDirect3DTexture9* ptr = pComPtr;
592 System.Runtime.CompilerServices.Unsafe.SkipInit(out _D3DSURFACE_DESC d3DSURFACE_DESC);
593 int num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, _D3DSURFACE_DESC*, int>)(int)(*(uint*)(*(int*)ptr + 68)))((nint)ptr, 0u, &d3DSURFACE_DESC);
594 if (num < 0)
595 {
597 }
598 if (!format.HasValue)
599 {
600 format = _003CModule_003E.ConvertWindowsFormatToXna(*(_D3DFORMAT*)(&d3DSURFACE_DESC));
601 }
602 _width = System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, int>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 24));
603 _height = System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, int>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 28));
604 int shouldNotRecreate = ((System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, int>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 12)) == 0) ? 1 : 0);
605 _shouldNotRecreate = (byte)shouldNotRecreate != 0;
606 base.InitializeDescription(format.Value);
607 }
608
609 private protected unsafe void CreateTexture(GraphicsDevice graphicsDevice, int width, int height, [MarshalAs(UnmanagedType.U1)] bool mipMap, uint usage, _D3DPOOL pool, SurfaceFormat format)
610 {
611 if (graphicsDevice == null)
612 {
614 }
615 ValidateCreationParameters(graphicsDevice._profileCapabilities, width, height, format, mipMap);
616 int num = ((!mipMap) ? 1 : 0);
617 fixed (IDirect3DTexture9** ptr = &pComPtr)
618 {
619 int num2 = *(int*)graphicsDevice.pComPtr + 92;
620 int num3 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, uint, uint, _D3DFORMAT, _D3DPOOL, IDirect3DTexture9**, void**, int>)(int)(*(uint*)num2))((nint)graphicsDevice.pComPtr, (uint)width, (uint)height, (uint)num, usage, _003CModule_003E.ConvertXnaFormatToWindows(format), pool, ptr, null);
621 if (num3 < 0)
622 {
623 throw GraphicsHelpers.GetExceptionFromResult((uint)num3);
624 }
625 isDisposed = false;
626 _parent = graphicsDevice;
627 SurfaceFormat? format2 = format;
628 InitializeDescription(format2);
629 graphicsDevice.Resources.AddTrackedObject(this, pComPtr, (uint)pool, _internalHandle, ref _internalHandle);
630 }
631 }
632
633 private protected static void ValidateCreationParameters(ProfileCapabilities profile, int width, int height, SurfaceFormat format, [MarshalAs(UnmanagedType.U1)] bool mipMap)
634 {
635 if (width <= 0)
636 {
638 }
639 if (height <= 0)
640 {
642 }
643 bool flag = Texture.CheckCompressedTexture(_003CModule_003E.ConvertXnaFormatToWindows(format));
644 if (!profile.ValidTextureFormats.Contains(format))
645 {
647 }
648 int maxTextureSize = profile.MaxTextureSize;
649 if (width > maxTextureSize || height > maxTextureSize)
650 {
652 }
653 int num = Math.Max(width, height);
654 int num2 = Math.Min(width, height);
655 int num3 = (num + num2 - 1) / num2;
656 int maxTextureAspectRatio = profile.MaxTextureAspectRatio;
657 if (num3 > maxTextureAspectRatio)
658 {
659 profile.ThrowNotSupportedException(FrameworkResources.ProfileAspectRatio, typeof(Texture2D).Name, maxTextureAspectRatio);
660 }
661 if (!profile.NonPow2Unconditional && (!Texture.IsPowerOfTwo((uint)width) || !Texture.IsPowerOfTwo((uint)height)))
662 {
663 if (mipMap)
664 {
666 }
667 if (!flag)
668 {
669 return;
670 }
672 }
673 if (flag && (((uint)width & 3u) != 0 || ((uint)height & 3u) != 0))
674 {
676 }
677 }
678
679 internal unsafe virtual int SaveDataForRecreation()
680 {
681 if (pComPtr == null)
682 {
683 return 0;
684 }
686 int num = CopyOrRestoreData(isStoring: true);
687 if (num >= 0)
688 {
689 ReleaseNativeObject(disposeManagedResource: false);
690 }
691 else
692 {
694 }
695 alreadyRecreated = false;
696 return num;
697 }
698
700 {
701 //ILSpy generated this explicit interface implementation from .override directive in SaveDataForRecreation
702 return this.SaveDataForRecreation();
703 }
704
705 internal unsafe virtual int RecreateAndPopulateObject()
706 {
708 {
709 return 0;
710 }
712 {
713 return 0;
714 }
715 if (pComPtr != null)
716 {
717 return -2147467259;
718 }
719 fixed (IDirect3DTexture9** ptr2 = &pComPtr)
720 {
721 IDirect3DDevice9* ptr = _parent.pComPtr;
722 int num = *(int*)ptr + 92;
723 int num2 = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, uint, uint, uint, _D3DFORMAT, _D3DPOOL, IDirect3DTexture9**, void**, int>)(int)(*(uint*)num))((nint)ptr, (uint)_width, (uint)_height, (uint)_levelCount, 0u, _003CModule_003E.ConvertXnaFormatToWindows(_format), (_D3DPOOL)1, ptr2, null);
724 if (num2 >= 0)
725 {
726 num2 = CreateStateWrapper();
727 if (num2 >= 0)
728 {
729 num2 = CopyOrRestoreData(isStoring: false);
730 if (num2 >= 0)
731 {
733 }
734 }
736 }
737 alreadyRecreated = true;
738 return num2;
739 }
740 }
741
743 {
744 //ILSpy generated this explicit interface implementation from .override directive in RecreateAndPopulateObject
745 return this.RecreateAndPopulateObject();
746 }
747
748 internal unsafe virtual void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
749 {
751 if (ptr != null)
752 {
753 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)ptr + 8)))((nint)ptr);
754 pStateTracker = null;
755 }
756 GraphicsDevice parent = _parent;
757 if (parent != null && pComPtr != null)
758 {
759 parent.Resources.ReleaseAllReferences(_internalHandle, disposeManagedResource);
760 }
761 pComPtr = null;
762 }
763
764 void IGraphicsResource.ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
765 {
766 //ILSpy generated this explicit interface implementation from .override directive in ReleaseNativeObject
767 this.ReleaseNativeObject(disposeManagedResource);
768 }
769
770 internal unsafe int CopyOrRestoreData([MarshalAs(UnmanagedType.U1)] bool isStoring)
771 {
772 IDirect3DTexture9* ptr = pComPtr;
773 if (ptr == null)
774 {
775 return 0;
776 }
777 System.Runtime.CompilerServices.Unsafe.SkipInit(out _D3DSURFACE_DESC d3DSURFACE_DESC);
778 int num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, _D3DSURFACE_DESC*, int>)(int)(*(uint*)(*(int*)ptr + 68)))((nint)ptr, 0u, &d3DSURFACE_DESC);
779 if (num >= 0)
780 {
781 if (isStoring)
782 {
784 }
785 uint num2 = System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 24));
786 uint num3 = System.Runtime.CompilerServices.Unsafe.As<_D3DSURFACE_DESC, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DSURFACE_DESC, 28));
787 bool flag = Texture.CheckCompressedTexture(*(_D3DFORMAT*)(&d3DSURFACE_DESC));
788 byte b = ((!flag) ? Texture.GetExpectedByteSizeFromFormat(*(_D3DFORMAT*)(&d3DSURFACE_DESC)) : ((byte)((*(int*)(&d3DSURFACE_DESC) == 827611204) ? 8u : 16u)));
789 int num4 = 0;
790 int num5 = 0;
791 if (0 < _levelCount)
792 {
793 System.Runtime.CompilerServices.Unsafe.SkipInit(out _D3DLOCKED_RECT d3DLOCKED_RECT);
794 do
795 {
796 byte* ptr2 = null;
797 int num6;
798 if (!isStoring)
799 {
800 ref IntPtr reference = ref pFaceData[num4];
801 num4++;
802 ptr2 = (byte*)reference.ToPointer();
803 num6 = 0;
804 }
805 else
806 {
807 num6 = 16;
808 }
809 ptr = pComPtr;
810 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, _D3DLOCKED_RECT*, tagRECT*, uint, int>)(int)(*(uint*)(*(int*)ptr + 76)))((nint)ptr, (uint)num5, &d3DLOCKED_RECT, null, (uint)num6);
811 if (num < 0)
812 {
813 break;
814 }
815 uint num7 = num2;
816 uint num8 = num3;
817 if (flag)
818 {
819 num7 = num2 + 3 >> 2;
820 num8 = num3 + 3 >> 2;
821 }
822 uint num9 = b * num7;
823 byte* ptr3 = (byte*)(int)System.Runtime.CompilerServices.Unsafe.As<_D3DLOCKED_RECT, uint>(ref System.Runtime.CompilerServices.Unsafe.AddByteOffset(ref d3DLOCKED_RECT, 4));
824 if (isStoring)
825 {
826 ptr2 = (byte*)_003CModule_003E.new_005B_005D(num9 * num8);
827 }
828 byte* ptr4 = ptr2;
829 if (0 < num8)
830 {
831 uint num10 = num8;
832 do
833 {
834 if (isStoring)
835 {
836 _003CModule_003E.memcpy_s(ptr4, num9 * num8, ptr3, num9);
837 }
838 else
839 {
840 _003CModule_003E.memcpy_s(ptr3, *(uint*)(&d3DLOCKED_RECT), ptr4, num9);
841 }
842 ptr3 = *(int*)(&d3DLOCKED_RECT) + ptr3;
843 ptr4 = (int)num9 + ptr4;
844 num10--;
845 }
846 while (num10 != 0);
847 }
848 ptr = pComPtr;
849 num = ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint, int>)(int)(*(uint*)(*(int*)ptr + 80)))((nint)ptr, (uint)num5);
850 if (num < 0)
851 {
852 break;
853 }
854 if (isStoring)
855 {
856 IntPtr intPtr = (IntPtr)ptr2;
857 ref IntPtr reference2 = ref pFaceData[num4];
858 num4++;
859 reference2 = intPtr;
860 }
861 num2 = ((num2 <= 1) ? 1u : (num2 >> 1));
862 num3 = ((num3 <= 1) ? 1u : (num3 >> 1));
863 num5++;
864 }
865 while (num5 < _levelCount);
866 }
867 }
868 return num;
869 }
870
871 internal unsafe void CleanupSavedData()
872 {
874 if (array == null)
875 {
876 return;
877 }
878 int num = 0;
879 if (0 < (nint)array.LongLength)
880 {
881 do
882 {
883 void* ptr = pFaceData[num].ToPointer();
884 if (ptr != null)
885 {
886 _003CModule_003E.delete_005B_005D(ptr);
887 }
888 ref IntPtr reference = ref pFaceData[num];
889 reference = IntPtr.Zero;
890 num++;
891 }
892 while (num < (nint)pFaceData.LongLength);
893 }
894 pFaceData = null;
895 }
896
897 internal unsafe override IDirect3DBaseTexture9* GetComPtr()
898 {
899 return (IDirect3DBaseTexture9*)pComPtr;
900 }
901
902 internal unsafe static Texture2D GetManagedObject(IDirect3DTexture9* pInterface, GraphicsDevice pDevice, uint pool)
903 {
904 Texture2D texture2D = pDevice.Resources.GetCachedObject(pInterface) as Texture2D;
905 if (texture2D != null)
906 {
907 ((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)pInterface + 8)))((nint)pInterface);
908 texture2D.isDisposed = false;
909 GC.ReRegisterForFinalize(texture2D);
910 }
911 else
912 {
913 texture2D = new Texture2D(pInterface, pDevice);
914 pDevice.Resources.AddTrackedObject(texture2D, pInterface, pool, 0uL, ref texture2D._internalHandle);
915 }
916 return texture2D;
917 }
918
919 private void OnObjectCreation()
920 {
922 }
923
924 private void _0021Texture2D()
925 {
926 if (!isDisposed)
927 {
928 ReleaseNativeObject(disposeManagedResource: true);
930 }
931 }
932
933 private void _007ETexture2D()
934 {
936 }
937
938 [HandleProcessCorruptedStateExceptions]
939 protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
940 {
941 if (P_0)
942 {
943 try
944 {
946 return;
947 }
948 finally
949 {
950 base.Dispose(true);
951 }
952 }
953 try
954 {
956 }
957 finally
958 {
959 base.Dispose(false);
960 }
961 }
962}
unsafe void AddTrackedObject(object managedObject, void *pComPtr, uint resourceManagementMode, ulong handle, ref ulong updatedHandle)
unsafe void ReleaseAllReferences(ulong handle, [MarshalAs(UnmanagedType.U1)] bool dispose)
static Exception GetExceptionFromResult(uint result)
unsafe int CopyOrRestoreData([MarshalAs(UnmanagedType.U1)] bool isStoring)
Definition Texture2D.cs:770
static void ValidateCreationParameters(ProfileCapabilities profile, int width, int height, SurfaceFormat format, [MarshalAs(UnmanagedType.U1)] bool mipMap)
Definition Texture2D.cs:633
static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream, int width, int height, [MarshalAs(UnmanagedType.U1)] bool zoom)
Definition Texture2D.cs:164
void SaveAsImage(Stream stream, SharedConstants.XnaImageFormat __unnamed001, int width, int height)
Definition Texture2D.cs:446
void SaveAsPng(Stream stream, int width, int height)
Definition Texture2D.cs:181
static unsafe Texture2D GetManagedObject(IDirect3DTexture9 *pInterface, GraphicsDevice pDevice, uint pool)
Definition Texture2D.cs:902
virtual unsafe int RecreateAndPopulateObject()
Definition Texture2D.cs:705
unsafe IDirect3DTexture9 * pComPtr
Definition Texture2D.cs:22
override void Dispose([MarshalAs(UnmanagedType.U1)] bool P_0)
Definition Texture2D.cs:939
Texture2D(GraphicsDevice graphicsDevice, int width, int height)
Definition Texture2D.cs:69
unsafe void CreateTexture(GraphicsDevice graphicsDevice, int width, int height, [MarshalAs(UnmanagedType.U1)] bool mipMap, uint usage, _D3DPOOL pool, SurfaceFormat format)
Definition Texture2D.cs:609
static Texture2D FromStream(GraphicsDevice graphicsDevice, Stream stream)
Definition Texture2D.cs:170
Texture2D(GraphicsDevice graphicsDevice, int width, int height, [MarshalAs(UnmanagedType.U1)] bool mipMap, SurfaceFormat format, [MarshalAs(UnmanagedType.U1)] bool __unnamed005)
Definition Texture2D.cs:99
unsafe void InitializeDescription(SurfaceFormat? format)
Definition Texture2D.cs:589
unsafe Texture2D(IDirect3DTexture9 *pInterface, GraphicsDevice pDevice)
Definition Texture2D.cs:47
unsafe override IDirect3DBaseTexture9 * GetComPtr()
Definition Texture2D.cs:897
void SaveAsJpeg(Stream stream, int width, int height)
Definition Texture2D.cs:176
Texture2D(GraphicsDevice graphicsDevice, int width, int height, [MarshalAs(UnmanagedType.U1)] bool mipMap, SurfaceFormat format)
Definition Texture2D.cs:84
unsafe void CopyData< T >(int level, Rectangle? rect, T[] data, int startIndex, int elementCount, uint options, [MarshalAs(UnmanagedType.U1)] bool isSetting)
Definition Texture2D.cs:220
virtual unsafe void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
Definition Texture2D.cs:748
unsafe Texture2D(GraphicsDevice graphicsDevice, Stream stream, int width, int height, SharedConstants.XnaImageOperation operation)
Definition Texture2D.cs:114
static unsafe void GetAndValidateRect(_D3DSURFACE_DESC *__unnamed000, uint *pdwLockWidth, uint *pdwLockHeight, Rectangle? rect)
Definition Texture.cs:221
static bool IsPowerOfTwo(uint dwNumber)
Definition Texture.cs:353
unsafe StateTrackerTexture * pStateTracker
Definition Texture.cs:20
static unsafe void ValidateTotalSize(_D3DSURFACE_DESC *__unnamed000, uint dwLockWidth, uint dwLockHeight, uint dwFormatSize, uint dwElementSize, uint elementCount)
Definition Texture.cs:253
static byte GetExpectedByteSizeFromFormat(_D3DFORMAT format)
Definition Texture.cs:72
static bool CheckCompressedTexture(_D3DFORMAT format)
Definition Texture.cs:272
static void CheckDisposed(object obj, IntPtr pComPtr)
Definition Helpers.cs:188
static void ThrowExceptionFromErrorCode(ErrorCodes error)
Definition Helpers.cs:87
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
virtual byte[] ReadBytes(int count)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static unsafe void InitBlock(void *startAddress, byte value, uint byteCount)
Definition Unsafe.cs:117
void ReleaseNativeObject([MarshalAs(UnmanagedType.U1)] bool disposeManagedResource)
static Color Transparent
Definition Color.cs:76
Color(uint packedValue)
Definition Color.cs:358
static readonly IntPtr Zero
Definition IntPtr.cs:18
unsafe void * ToPointer()
Definition IntPtr.cs:210