DeferredBasicEffect.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 nkast.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) || W10)
  33. static readonly String resourceName = "nkast.Aether.Shaders.Resources.DeferredBasicEffect.dx11.mgfxo";
  34. #else
  35. static readonly String resourceName = "nkast.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 = ".9";
  52. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  53. if (mgVersion.Major == 3)
  54. {
  55. if (mgVersion.Minor == 6)
  56. version = ".8";
  57. if (mgVersion.Minor == 7)
  58. version = ".8";
  59. if (mgVersion.Minor == 8)
  60. version = ".9";
  61. }
  62. name = name + version;
  63. #endif
  64. Stream stream = GetAssembly(typeof(DeferredBasicEffect)).GetManifestResourceStream(name);
  65. return stream;
  66. }
  67. private static Assembly GetAssembly(Type type)
  68. {
  69. #if W10
  70. return type.GetTypeInfo().Assembly;
  71. #else
  72. return type.Assembly;
  73. #endif
  74. }
  75. #endregion
  76. #region Public Properties
  77. public Matrix Projection
  78. {
  79. get { return projectionParam.GetValueMatrix(); }
  80. set { projectionParam.SetValue(value); }
  81. }
  82. public Matrix View
  83. {
  84. get { return viewParam.GetValueMatrix(); }
  85. set { viewParam.SetValue(value); }
  86. }
  87. public Matrix World
  88. {
  89. get { return worldParam.GetValueMatrix(); }
  90. set { worldParam.SetValue(value); }
  91. }
  92. #endregion
  93. #region Methods
  94. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  95. : base(graphicsDevice,
  96. #if NETFX_CORE
  97. LoadEffectResourceStream(resourceName), true
  98. #else
  99. LoadEffectResource(resourceName)
  100. #endif
  101. )
  102. {
  103. CacheEffectParameters(null);
  104. }
  105. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  106. {
  107. CacheEffectParameters(null);
  108. }
  109. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  110. : base(cloneSource)
  111. {
  112. CacheEffectParameters(cloneSource);
  113. }
  114. public override Effect Clone()
  115. {
  116. return new DeferredBasicEffect(this);
  117. }
  118. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  119. {
  120. // IEffectMatrices
  121. projectionParam = Parameters["Projection"];
  122. viewParam = Parameters["View"];
  123. worldParam = Parameters["World"];
  124. }
  125. #endregion
  126. }
  127. }