EffectResource.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // OpenGL
  55. _shaderExtension = "ogl";
  56. break;
  57. case 1:
  58. // DirectX
  59. _shaderExtension = "dx11";
  60. break;
  61. default:
  62. throw new InvalidOperationException("Unknown shader profile.");
  63. }
  64. }
  65. private readonly string _resourceName;
  66. private volatile byte[] _bytecode;
  67. private readonly Assembly _assembly;
  68. /// <summary>
  69. /// Gets the bytecode of the <see cref="Effect" /> file.
  70. /// </summary>
  71. /// <value>
  72. /// The bytecode of the <see cref="Effect" /> file.
  73. /// </value>
  74. public byte[] Bytecode
  75. {
  76. get
  77. {
  78. if (_bytecode != null)
  79. return _bytecode;
  80. lock (this)
  81. {
  82. if (_bytecode != null)
  83. return _bytecode;
  84. var stream = _assembly.GetManifestResourceStream(_resourceName);
  85. using (var memoryStream = new MemoryStream())
  86. {
  87. stream.CopyTo(memoryStream);
  88. _bytecode = memoryStream.ToArray();
  89. }
  90. }
  91. return _bytecode;
  92. }
  93. }
  94. /// <summary>
  95. /// Initializes a new instance of the <see cref="EffectResource" /> class.
  96. /// </summary>
  97. /// <param name="resourceName">The name of the embedded resource. This must include the namespace(s).</param>
  98. /// <param name="assembly">The assembly which the embedded resource is apart of.</param>
  99. public EffectResource(string resourceName, Assembly assembly = null)
  100. {
  101. _resourceName = resourceName;
  102. _assembly = assembly ?? typeof(EffectResource).GetTypeInfo().Assembly;
  103. }
  104. }
  105. }