DeferredBasicEffect.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #region License
  2. // Copyright 2014-2016 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.IO;
  18. using System.Reflection;
  19. using Microsoft.Xna.Framework;
  20. using Microsoft.Xna.Framework.Graphics;
  21. namespace tainicom.Aether.Shaders
  22. {
  23. public class DeferredBasicEffect : Effect, IEffectMatrices
  24. {
  25. #region Effect Parameters
  26. // IEffectMatrices
  27. EffectParameter projectionParam;
  28. EffectParameter viewParam;
  29. EffectParameter worldParam;
  30. #endregion
  31. #region Fields
  32. #if ((MG && WINDOWS) || W8_1 || W10)
  33. static readonly String resourceName = "tainicom.Aether.Shaders.Resources.DeferredBasicEffect.dx11.mgfxo";
  34. #else
  35. static readonly String resourceName = "tainicom.Aether.Shaders.Resources.DeferredBasicEffect.xna.WinReach";
  36. #endif
  37. internal static byte[] LoadEffectResource(string name)
  38. {
  39. using (var stream = LoadEffectResourceStream(name))
  40. using (var ms = new MemoryStream())
  41. {
  42. stream.CopyTo(ms);
  43. return ms.ToArray();
  44. }
  45. }
  46. internal static Stream LoadEffectResourceStream(string name)
  47. {
  48. // Detect MG version
  49. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  50. if (mgVersion.Major == 3)
  51. {
  52. if (mgVersion.Minor == 4)
  53. name += ".6";
  54. if (mgVersion.Minor == 5)
  55. name += ".7";
  56. if (mgVersion.Minor == 6)
  57. name += ".8";
  58. }
  59. Stream stream = GetAssembly(typeof(DeferredBasicEffect)).GetManifestResourceStream(name);
  60. return stream;
  61. }
  62. private static Assembly GetAssembly(Type type)
  63. {
  64. #if W8_1 || W10
  65. return type.GetTypeInfo().Assembly;
  66. #else
  67. return type.Assembly;
  68. #endif
  69. }
  70. #endregion
  71. #region Public Properties
  72. public Matrix Projection
  73. {
  74. get { return projectionParam.GetValueMatrix(); }
  75. set { projectionParam.SetValue(value); }
  76. }
  77. public Matrix View
  78. {
  79. get { return viewParam.GetValueMatrix(); }
  80. set { viewParam.SetValue(value); }
  81. }
  82. public Matrix World
  83. {
  84. get { return worldParam.GetValueMatrix(); }
  85. set { worldParam.SetValue(value); }
  86. }
  87. #endregion
  88. #region Methods
  89. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  90. : base(graphicsDevice,
  91. #if NETFX_CORE || WP8
  92. LoadEffectResourceStream(resourceName), true
  93. #else
  94. LoadEffectResource(resourceName)
  95. #endif
  96. )
  97. {
  98. CacheEffectParameters(null);
  99. }
  100. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  101. {
  102. CacheEffectParameters(null);
  103. }
  104. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  105. : base(cloneSource)
  106. {
  107. CacheEffectParameters(cloneSource);
  108. }
  109. public override Effect Clone()
  110. {
  111. return new DeferredBasicEffect(this);
  112. }
  113. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  114. {
  115. // IEffectMatrices
  116. projectionParam = Parameters["Projection"];
  117. viewParam = Parameters["View"];
  118. worldParam = Parameters["World"];
  119. }
  120. #endregion
  121. }
  122. }