Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Texture.cs
Go to the documentation of this file.
1using System;
4using std;
5
7
8public abstract class Texture : GraphicsResource
9{
10 private protected SurfaceFormat _format;
11
12 private protected int _levelCount;
13
14 internal bool alreadyRecreated;
15
16 internal bool isActiveRenderTarget;
17
19
21
22 public int LevelCount => _levelCount;
23
25
26 private protected virtual bool MustClamp
27 {
28 [return: MarshalAs(UnmanagedType.U1)]
29 get
30 {
31 return false;
32 }
33 }
34
35 internal Texture()
36 {
37 }
38
39 private protected unsafe void InitializeDescription(SurfaceFormat format)
40 {
42 IDirect3DBaseTexture9* comPtr = GetComPtr();
43 _levelCount = (int)((delegate* unmanaged[Stdcall, Stdcall]<IntPtr, uint>)(int)(*(uint*)(*(int*)comPtr + 52)))((nint)comPtr);
44 int num = CreateStateWrapper();
45 if (num < 0)
46 {
48 }
49 }
50
51 private protected unsafe int CreateStateWrapper()
52 {
54 bool flag = profileCapabilities.ValidVertexTextureFormats.Contains(_format);
55 bool flag2 = profileCapabilities.InvalidFilterFormats.Contains(_format);
56 StateTrackerTexture* ptr = (StateTrackerTexture*)_003CModule_003E.@new(28u, (nothrow_t*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref _003CModule_003E.std_002Enothrow));
58 try
59 {
60 ptr2 = ((ptr == null) ? null : _003CModule_003E.Microsoft_002EXna_002EFramework_002EGraphics_002EStateTrackerTexture_002E_007Bctor_007D(ptr, GetComPtr(), (uint)_format, flag, flag2, MustClamp));
61 }
62 catch
63 {
64 //try-fault
65 _003CModule_003E.delete(ptr, (nothrow_t*)System.Runtime.CompilerServices.Unsafe.AsPointer(ref _003CModule_003E.std_002Enothrow));
66 throw;
67 }
68 pStateTracker = ptr2;
69 return (ptr2 == null) ? (-2147024882) : 0;
70 }
71
72 internal static byte GetExpectedByteSizeFromFormat(_D3DFORMAT format)
73 {
74 if (format <= (_D3DFORMAT)827606349)
75 {
76 if (format == (_D3DFORMAT)827606349)
77 {
78 return 4;
79 }
80 switch (format - 20)
81 {
82 case (_D3DFORMAT)0:
83 return 3;
84 case (_D3DFORMAT)1:
85 return 4;
86 case (_D3DFORMAT)2:
87 return 4;
88 case (_D3DFORMAT)15:
89 return 4;
90 case (_D3DFORMAT)12:
91 return 4;
92 case (_D3DFORMAT)13:
93 return 4;
94 case (_D3DFORMAT)11:
95 return 4;
96 case (_D3DFORMAT)14:
97 return 4;
98 case (_D3DFORMAT)16:
99 return 8;
100 case (_D3DFORMAT)3:
101 return 2;
102 case (_D3DFORMAT)5:
103 return 2;
104 case (_D3DFORMAT)4:
105 return 2;
106 case (_D3DFORMAT)6:
107 return 2;
108 case (_D3DFORMAT)10:
109 return 2;
110 case (_D3DFORMAT)9:
111 return 2;
112 case (_D3DFORMAT)8:
113 return 1;
114 case (_D3DFORMAT)7:
115 return 1;
116 case (_D3DFORMAT)40:
117 return 2;
118 case (_D3DFORMAT)43:
119 return 4;
120 case (_D3DFORMAT)44:
121 return 4;
122 case (_D3DFORMAT)90:
123 return 8;
124 case (_D3DFORMAT)94:
125 return 4;
126 case (_D3DFORMAT)95:
127 return 8;
128 case (_D3DFORMAT)96:
129 return 16;
130 case (_D3DFORMAT)91:
131 return 2;
132 case (_D3DFORMAT)92:
133 return 4;
134 case (_D3DFORMAT)93:
135 return 8;
136 case (_D3DFORMAT)30:
137 return 1;
138 case (_D3DFORMAT)61:
139 return 2;
140 case (_D3DFORMAT)32:
141 return 1;
142 case (_D3DFORMAT)31:
143 return 2;
144 case (_D3DFORMAT)21:
145 return 1;
146 case (_D3DFORMAT)20:
147 return 2;
148 case (_D3DFORMAT)41:
149 return 2;
150 case (_D3DFORMAT)42:
151 return 4;
152 case (_D3DFORMAT)47:
153 return 4;
154 case (_D3DFORMAT)97:
155 return 2;
156 case (_D3DFORMAT)55:
157 return 4;
158 case (_D3DFORMAT)63:
159 return 4;
160 case (_D3DFORMAT)59:
161 return 4;
162 case (_D3DFORMAT)57:
163 return 4;
164 case (_D3DFORMAT)51:
165 return 4;
166 case (_D3DFORMAT)60:
167 return 2;
168 case (_D3DFORMAT)53:
169 return 2;
170 }
171 }
172 else
173 {
174 switch (format)
175 {
176 case (_D3DFORMAT)844388420:
177 return 1;
178 case (_D3DFORMAT)827611204:
179 return 1;
180 case (_D3DFORMAT)844715353:
181 return 2;
182 case (_D3DFORMAT)861165636:
183 return 1;
184 case (_D3DFORMAT)877942852:
185 return 1;
186 case (_D3DFORMAT)1111970375:
187 return 2;
188 case (_D3DFORMAT)894720068:
189 return 1;
190 case (_D3DFORMAT)1195525970:
191 return 2;
192 case (_D3DFORMAT)1498831189:
193 return 2;
194 }
195 }
196 throw new InvalidOperationException();
197 }
198
199 internal unsafe static void GetAndValidateSizes<T>(_D3DSURFACE_DESC* pSurface, uint* pdwFormatSize, uint* pdwElementSize) where T : struct
200 {
201 if (pSurface == null || pdwFormatSize == null || pdwElementSize == null)
202 {
203 return;
204 }
205 *pdwFormatSize = GetExpectedByteSizeFromFormat(*(_D3DFORMAT*)pSurface);
206 uint num = (*pdwElementSize = (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<T>());
207 uint num2 = *pdwFormatSize;
208 if (num != num2)
209 {
210 if (num2 <= num)
211 {
213 }
214 if (num2 % num != 0)
215 {
217 }
218 }
219 }
220
221 internal unsafe static void GetAndValidateRect(_D3DSURFACE_DESC* __unnamed000, uint* pdwLockWidth, uint* pdwLockHeight, Rectangle? rect)
222 {
223 if (__unnamed000 == null || pdwLockHeight == null || pdwLockWidth == null)
224 {
225 return;
226 }
227 *pdwLockWidth = *(uint*)((byte*)__unnamed000 + 24);
228 *pdwLockHeight = *(uint*)((byte*)__unnamed000 + 28);
229 if (!rect.HasValue)
230 {
231 return;
232 }
233 if (rect.Value.X >= 0 && rect.Value.Width > 0 && rect.Value.Y >= 0 && rect.Value.Height > 0)
234 {
235 Rectangle value = rect.Value;
236 Rectangle value2 = rect.Value;
237 if ((uint)(value.Left + value2.Width) <= (uint)(*(int*)((byte*)__unnamed000 + 24)))
238 {
239 Rectangle value3 = rect.Value;
240 Rectangle value4 = rect.Value;
241 if ((uint)(value3.Top + value4.Height) <= (uint)(*(int*)((byte*)__unnamed000 + 28)))
242 {
243 *pdwLockWidth = (uint)rect.Value.Width;
244 *pdwLockHeight = (uint)rect.Value.Height;
245 return;
246 }
247 }
249 }
251 }
252
253 internal unsafe static void ValidateTotalSize(_D3DSURFACE_DESC* __unnamed000, uint dwLockWidth, uint dwLockHeight, uint dwFormatSize, uint dwElementSize, uint elementCount)
254 {
255 int num = *(int*)__unnamed000;
256 _D3DFORMAT d3DFORMAT = (_D3DFORMAT)num;
257 int num2 = ((d3DFORMAT == (_D3DFORMAT)827611204 || d3DFORMAT == (_D3DFORMAT)844388420 || d3DFORMAT == (_D3DFORMAT)861165636 || d3DFORMAT == (_D3DFORMAT)877942852 || d3DFORMAT == (_D3DFORMAT)894720068) ? 1 : 0);
258 if ((byte)num2 != 0)
259 {
260 dwLockWidth = dwLockWidth + 3 >> 2;
261 dwLockHeight = dwLockHeight + 3 >> 2;
262 dwFormatSize = ((num == 827611204) ? 8u : 16u);
263 }
264 uint num3 = dwLockWidth * dwLockHeight * dwFormatSize;
265 if (dwElementSize * elementCount != num3)
266 {
268 }
269 }
270
271 [return: MarshalAs(UnmanagedType.U1)]
272 internal static bool CheckCompressedTexture(_D3DFORMAT format)
273 {
274 int num = ((format == (_D3DFORMAT)827611204 || format == (_D3DFORMAT)844388420 || format == (_D3DFORMAT)861165636 || format == (_D3DFORMAT)877942852 || format == (_D3DFORMAT)894720068) ? 1 : 0);
275 return (byte)num != 0;
276 }
277
278 internal unsafe static void CopyData<T>(void* pData, int pitch, T[] data, int dataIndex, int elementCount, _D3DSURFACE_DESC* pSurface, uint dwLockWidth, uint dwLockHeight, [MarshalAs(UnmanagedType.U1)] bool isSetting) where T : struct
279 {
280 if (pSurface == null)
281 {
282 return;
283 }
284 fixed (T* ptr2 = &data[dataIndex])
285 {
286 byte* ptr = (byte*)pData;
287 byte* ptr3 = (byte*)ptr2;
288 uint num = (uint)System.Runtime.CompilerServices.Unsafe.SizeOf<T>();
289 uint num2 = GetExpectedByteSizeFromFormat(*(_D3DFORMAT*)pSurface) / num;
290 int num3 = *(int*)pSurface;
291 _D3DFORMAT d3DFORMAT = (_D3DFORMAT)num3;
292 int num4 = ((d3DFORMAT == (_D3DFORMAT)827611204 || d3DFORMAT == (_D3DFORMAT)844388420 || d3DFORMAT == (_D3DFORMAT)861165636 || d3DFORMAT == (_D3DFORMAT)877942852 || d3DFORMAT == (_D3DFORMAT)894720068) ? 1 : 0);
293 if ((byte)num4 != 0)
294 {
295 dwLockWidth = dwLockWidth + 3 >> 2;
296 dwLockHeight = dwLockHeight + 3 >> 2;
297 num2 = ((num3 == 827611204) ? 8u : 16u);
298 }
299 uint num5 = num2 * num * dwLockWidth;
300 if (0 >= dwLockHeight)
301 {
302 return;
303 }
304 uint num6 = dwLockHeight;
305 do
306 {
307 if (*(int*)pSurface == 21)
308 {
309 if (isSetting)
310 {
311 SwapBgr(ptr, ptr3, num5);
312 }
313 else
314 {
315 SwapBgr(ptr3, ptr, num5);
316 }
317 }
318 else if (isSetting)
319 {
320 _003CModule_003E.memcpy_s(ptr, num5, ptr3, num5);
321 }
322 else
323 {
324 _003CModule_003E.memcpy_s(ptr3, num5, ptr, num5);
325 }
326 ptr = pitch + ptr;
327 ptr3 = (int)num5 + ptr3;
328 num6--;
329 }
330 while (num6 != 0);
331 }
332 }
333
334 internal unsafe static void SwapBgr(void* pDest, void* pSrc, uint dwSize)
335 {
336 uint* ptr = (uint*)pDest;
337 if (dwSize >= 4)
338 {
339 int num = (int)((byte*)pSrc - (nuint)pDest);
340 uint num2 = dwSize >> 2;
341 do
342 {
343 uint num3 = *(uint*)(num + (byte*)ptr);
344 *ptr = ((num3 >> 16) & 0xFFu) | ((num3 & 0xFF) << 16) | (num3 & 0xFF00FF00u);
345 ptr++;
346 num2--;
347 }
348 while (num2 != 0);
349 }
350 }
351
352 [return: MarshalAs(UnmanagedType.U1)]
353 internal static bool IsPowerOfTwo(uint dwNumber)
354 {
355 if (dwNumber != 0 && ((dwNumber - 1) & dwNumber) == 0)
356 {
357 return true;
358 }
359 return false;
360 }
361
362 internal unsafe abstract IDirect3DBaseTexture9* GetComPtr();
363
364 internal unsafe int CompareTo(Texture other)
365 {
366 IDirect3DBaseTexture9* comPtr = GetComPtr();
367 IDirect3DBaseTexture9* comPtr2 = other.GetComPtr();
368 if (comPtr > comPtr2)
369 {
370 return -1;
371 }
372 return (comPtr < comPtr2) ? 1 : 0;
373 }
374}
static Exception GetExceptionFromResult(uint result)
unsafe IDirect3DBaseTexture9 * GetComPtr()
static unsafe void GetAndValidateRect(_D3DSURFACE_DESC *__unnamed000, uint *pdwLockWidth, uint *pdwLockHeight, Rectangle? rect)
Definition Texture.cs:221
static unsafe void CopyData< T >(void *pData, int pitch, T[] data, int dataIndex, int elementCount, _D3DSURFACE_DESC *pSurface, uint dwLockWidth, uint dwLockHeight, [MarshalAs(UnmanagedType.U1)] bool isSetting)
Definition Texture.cs:278
static unsafe void GetAndValidateSizes< T >(_D3DSURFACE_DESC *pSurface, uint *pdwFormatSize, uint *pdwElementSize)
Definition Texture.cs:199
static bool IsPowerOfTwo(uint dwNumber)
Definition Texture.cs:353
unsafe StateTrackerTexture * pStateTracker
Definition Texture.cs:20
static unsafe void SwapBgr(void *pDest, void *pSrc, uint dwSize)
Definition Texture.cs:334
static unsafe void ValidateTotalSize(_D3DSURFACE_DESC *__unnamed000, uint dwLockWidth, uint dwLockHeight, uint dwFormatSize, uint dwElementSize, uint elementCount)
Definition Texture.cs:253
unsafe int CompareTo(Texture other)
Definition Texture.cs:364
static byte GetExpectedByteSizeFromFormat(_D3DFORMAT format)
Definition Texture.cs:72
unsafe void InitializeDescription(SurfaceFormat format)
Definition Texture.cs:39
static bool CheckCompressedTexture(_D3DFORMAT format)
Definition Texture.cs:272