EffectResource.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Reflection;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace MonoGame.Extended.Graphics.Effects
  8. {
  9. /// <summary>
  10. /// Reperesents the bytecode of an <see cref="Effect" /> that is encapsulated inside a compiled assembly.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// Files that are encapsulated inside a compiled assembly are commonly known as Manifiest or embedded resources.
  15. /// Since embedded resources are added to the assembly at compiled time, they can not be accidentally deleted or
  16. /// misplaced. However, if the file needs to be changed, the assembly will need to be re-compiled with the changed
  17. /// file.
  18. /// </para>
  19. /// <para>
  20. /// To add an embedded resource file to an assembly, first add it to the project and then change the Build Action
  21. /// in the Properties of the file to <code>Embedded Resource</code>. The next time the project is compiled, the
  22. /// compiler will add the file to the assembly as an embedded resource. The compiler adds namespace(s) to the
  23. /// embedded resource so it matches with the path of where the file was added to the project.
  24. /// </para>
  25. /// </remarks>
  26. public class EffectResource
  27. {
  28. private static EffectResource _defaultEffect;
  29. private static string _shaderExtension;
  30. /// <summary>
  31. /// Gets the <see cref="Effects.DefaultEffect" /> embedded into the MonoGame.Extended.Graphics library.
  32. /// </summary>
  33. public static EffectResource DefaultEffect => _defaultEffect ?? (_defaultEffect = new EffectResource($"MonoGame.Extended.Graphics.Effects.Resources.DefaultEffect.{_shaderExtension}.mgfxo"));
  34. static EffectResource()
  35. {
  36. DetermineShaderExtension();
  37. }
  38. private static void DetermineShaderExtension()
  39. {
  40. // use reflection to figure out if Shader.Profile is OpenGL (0) or DirectX (1),
  41. // may need to be changed / fixed for future shader profiles
  42. var assembly = typeof(Game).GetTypeInfo().Assembly;
  43. Debug.Assert(assembly != null);
  44. var shaderType = assembly.GetType("Microsoft.Xna.Framework.Graphics.Shader");
  45. Debug.Assert(shaderType != null);
  46. var shaderTypeInfo = shaderType.GetTypeInfo();
  47. Debug.Assert(shaderTypeInfo != null);
  48. // https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Shader/Shader.cs#L47
  49. var profileProperty = shaderTypeInfo.GetDeclaredProperty("Profile");
  50. var value = (int)profileProperty.GetValue(null);
  51. switch (value)
  52. {
  53. case 0:
  54. {
  55. // OpenGL
  56. _shaderExtension = "ogl";
  57. break;
  58. }
  59. case 1:
  60. {
  61. // DirectX 11
  62. _shaderExtension = "dx11";
  63. break;
  64. }
  65. case 2:
  66. {
  67. // DirectX 12
  68. // Note: ShaderProfile/PlatformProfile for Xbox One and Xbox Series X/S is 21, not 2
  69. _shaderExtension = "dx12";
  70. break;
  71. }
  72. case 80:
  73. {
  74. // Vulkan
  75. _shaderExtension = "vlkn";
  76. break;
  77. }
  78. default:
  79. {
  80. throw new InvalidOperationException("Unknown shader profile.");
  81. }
  82. }
  83. }
  84. private readonly string _resourceName;
  85. private volatile byte[] _bytecode;
  86. private readonly Assembly _assembly;
  87. /// <summary>
  88. /// Gets the bytecode of the <see cref="Effect" /> file.
  89. /// </summary>
  90. /// <value>
  91. /// The bytecode of the <see cref="Effect" /> file.
  92. /// </value>
  93. public byte[] Bytecode
  94. {
  95. get
  96. {
  97. if (_bytecode != null)
  98. return _bytecode;
  99. lock (this)
  100. {
  101. if (_bytecode != null)
  102. return _bytecode;
  103. var stream = _assembly.GetManifestResourceStream(_resourceName);
  104. using (var memoryStream = new MemoryStream())
  105. {
  106. stream.CopyTo(memoryStream);
  107. _bytecode = memoryStream.ToArray();
  108. }
  109. }
  110. return _bytecode;
  111. }
  112. }
  113. /// <summary>
  114. /// Initializes a new instance of the <see cref="EffectResource" /> class.
  115. /// </summary>
  116. /// <param name="resourceName">The name of the embedded resource. This must include the namespace(s).</param>
  117. /// <param name="assembly">The assembly which the embedded resource is apart of.</param>
  118. public EffectResource(string resourceName, Assembly assembly = null)
  119. {
  120. _resourceName = resourceName;
  121. _assembly = assembly ?? typeof(EffectResource).GetTypeInfo().Assembly;
  122. }
  123. }
  124. }