DeferredBasicEffect.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static readonly String ResourceName = "nkast.Aether.Shaders.Resources.DeferredBasicEffect";
  33. internal static byte[] LoadEffectResource(string name)
  34. {
  35. name = GetResourceName(name);
  36. using (var stream = GetAssembly(typeof(DeferredBasicEffect)).GetManifestResourceStream(name))
  37. {
  38. var bytecode = new byte[stream.Length];
  39. stream.Read(bytecode, 0, (int)stream.Length);
  40. return bytecode;
  41. }
  42. }
  43. private static string GetResourceName(string name)
  44. {
  45. String platformName = "";
  46. var version = "";
  47. #if XNA
  48. platformName = ".xna.WinReach";
  49. #else
  50. platformName = ".dx11.fxo";
  51. // Detect version
  52. version = ".10";
  53. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  54. if (mgVersion.Major == 3)
  55. {
  56. if (mgVersion.Minor == 6)
  57. version = ".8";
  58. if (mgVersion.Minor == 7)
  59. version = ".8";
  60. if (mgVersion.Minor == 8)
  61. {
  62. version = ".9";
  63. if (mgVersion.Build == 1 || mgVersion.Build >= 9100)
  64. version = ".10";
  65. }
  66. }
  67. #endif
  68. return name + platformName + version;
  69. }
  70. private static Assembly GetAssembly(Type type)
  71. {
  72. #if W10
  73. return type.GetTypeInfo().Assembly;
  74. #else
  75. return type.Assembly;
  76. #endif
  77. }
  78. #endregion
  79. #region Public Properties
  80. public Matrix Projection
  81. {
  82. get { return projectionParam.GetValueMatrix(); }
  83. set { projectionParam.SetValue(value); }
  84. }
  85. public Matrix View
  86. {
  87. get { return viewParam.GetValueMatrix(); }
  88. set { viewParam.SetValue(value); }
  89. }
  90. public Matrix World
  91. {
  92. get { return worldParam.GetValueMatrix(); }
  93. set { worldParam.SetValue(value); }
  94. }
  95. #endregion
  96. #region Methods
  97. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  98. : base(graphicsDevice, LoadEffectResource(ResourceName))
  99. {
  100. CacheEffectParameters(null);
  101. }
  102. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  103. {
  104. CacheEffectParameters(null);
  105. }
  106. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  107. : base(cloneSource)
  108. {
  109. CacheEffectParameters(cloneSource);
  110. }
  111. public override Effect Clone()
  112. {
  113. return new DeferredBasicEffect(this);
  114. }
  115. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  116. {
  117. // IEffectMatrices
  118. projectionParam = Parameters["Projection"];
  119. viewParam = Parameters["View"];
  120. worldParam = Parameters["World"];
  121. }
  122. #endregion
  123. }
  124. }