Advertisement
Ameisen

Untitled

Jan 10th, 2022
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. try {
  2.     var dynMethod = new DynamicMethod(
  3.         string.Empty,
  4.         Texture2DType,
  5.         new Type[] { typeof(GraphicsDevice), typeof(int), typeof(int), typeof(SurfaceFormat), typeof(Enum) },
  6.         Texture2DType
  7.     );
  8.     var ilGen = dynMethod.GetILGenerator();
  9.     ilGen.Emit(OpCodes.Ldarg_0); // graphicsDevice
  10.     ilGen.Emit(OpCodes.Ldarg_1); // width
  11.     ilGen.Emit(OpCodes.Ldarg_2); // height
  12.     ilGen.Emit(OpCodes.Ldc_I4_0); // mipmap
  13.     ilGen.Emit(OpCodes.Ldarg_3); // format
  14.     ilGen.Emit(OpCodes.Ldarg_S, 4); // type
  15.     ilGen.Emit(OpCodes.Ldc_I4_0); // shared
  16.     ilGen.Emit(OpCodes.Ldc_I4_0); // arraySize
  17.     ilGen.Emit(OpCodes.Newobj, Texture2DConstructor);
  18.     ilGen.Emit(OpCodes.Ret);
  19.     CreateInstance = dynMethod.CreateDelegate<Func<GraphicsDevice, int, int, SurfaceFormat, Enum, Texture2D>>();
  20. }
  21. catch (Exception ex) {
  22.     throw;
  23. }
  24.  
  25. ...
  26.  
  27. try {
  28.     var p0 = System.Linq.Expressions.Expression.Parameter(typeof(GraphicsDevice));
  29.     var p1 = System.Linq.Expressions.Expression.Parameter(typeof(int));
  30.     var p2 = System.Linq.Expressions.Expression.Parameter(typeof(int));
  31.     var p3 = System.Linq.Expressions.Expression.Parameter(typeof(SurfaceFormat));
  32.     var p4 = System.Linq.Expressions.Expression.Parameter(typeof(Enum));
  33.  
  34.  
  35.     CreateInstance = System.Linq.Expressions.Expression.Lambda<Func<GraphicsDevice, int, int, SurfaceFormat, Enum, Texture2D>>(
  36.         System.Linq.Expressions.Expression.New(
  37.             Texture2DConstructor,
  38.             p0, // graphicsDevice
  39.             p1, // width
  40.             p2, // height
  41.             System.Linq.Expressions.Expression.Constant(false), // mipmap
  42.             p3, // format
  43.             System.Linq.Expressions.Expression.Convert(p4, SurfaceTypeType), // type
  44.             System.Linq.Expressions.Expression.Constant(false), // shared
  45.             System.Linq.Expressions.Expression.Constant(0)  // arraySize
  46.         ),
  47.         p0, p1, p2, p3, p4
  48.     ).Compile();
  49. }
  50. catch (Exception ex) {
  51.     throw;
  52. }
  53.  
  54. ...
  55.  
  56. var newObj = CreateInstance(DrawState.Device, size.Width, size.Height, SurfaceFormat.Dxt5, SwapChainRenderTarget);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement