DeferredBasicEffect.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. {
  41. var bytecode = new byte[stream.Length];
  42. stream.Read(bytecode, 0, (int)stream.Length);
  43. return bytecode;
  44. }
  45. }
  46. internal static Stream LoadEffectResourceStream(string name)
  47. {
  48. // Detect MG version
  49. var version = "";
  50. #if !XNA
  51. version = ".8";
  52. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  53. if (mgVersion.Major == 3)
  54. {
  55. if (mgVersion.Minor == 4)
  56. version = ".6";
  57. if (mgVersion.Minor == 5)
  58. version = ".7";
  59. if (mgVersion.Minor == 6)
  60. version = ".8";
  61. if (mgVersion.Minor == 7)
  62. version = ".8";
  63. }
  64. name = name + version;
  65. #endif
  66. Stream stream = GetAssembly(typeof(DeferredBasicEffect)).GetManifestResourceStream(name);
  67. return stream;
  68. }
  69. private static Assembly GetAssembly(Type type)
  70. {
  71. #if W8_1 || W10
  72. return type.GetTypeInfo().Assembly;
  73. #else
  74. return type.Assembly;
  75. #endif
  76. }
  77. #endregion
  78. #region Public Properties
  79. public Matrix Projection
  80. {
  81. get { return projectionParam.GetValueMatrix(); }
  82. set { projectionParam.SetValue(value); }
  83. }
  84. public Matrix View
  85. {
  86. get { return viewParam.GetValueMatrix(); }
  87. set { viewParam.SetValue(value); }
  88. }
  89. public Matrix World
  90. {
  91. get { return worldParam.GetValueMatrix(); }
  92. set { worldParam.SetValue(value); }
  93. }
  94. #endregion
  95. #region Methods
  96. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  97. : base(graphicsDevice,
  98. #if NETFX_CORE || WP8
  99. LoadEffectResourceStream(resourceName), true
  100. #else
  101. LoadEffectResource(resourceName)
  102. #endif
  103. )
  104. {
  105. CacheEffectParameters(null);
  106. }
  107. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  108. {
  109. CacheEffectParameters(null);
  110. }
  111. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  112. : base(cloneSource)
  113. {
  114. CacheEffectParameters(cloneSource);
  115. }
  116. public override Effect Clone()
  117. {
  118. return new DeferredBasicEffect(this);
  119. }
  120. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  121. {
  122. // IEffectMatrices
  123. projectionParam = Parameters["Projection"];
  124. viewParam = Parameters["View"];
  125. worldParam = Parameters["World"];
  126. }
  127. #endregion
  128. }
  129. }