DeferredCombineEffect.cs 4.4 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 tainicom.Aether.Shaders
  22. {
  23. public class DeferredCombineEffect : Effect
  24. {
  25. #region Effect Parameters
  26. EffectParameter colorMapParam;
  27. EffectParameter lightMapParam;
  28. EffectParameter halfPixelParam;
  29. #endregion
  30. #region Fields
  31. #if ((MG && WINDOWS) || W8_1 || W10)
  32. static readonly String resourceName = "tainicom.Aether.Shaders.Resources.DeferredCombine.dx11.mgfxo";
  33. #else
  34. static readonly String resourceName = "tainicom.Aether.Shaders.Resources.DeferredCombine.xna.WinReach";
  35. #endif
  36. internal static byte[] LoadEffectResource(string name)
  37. {
  38. using (var stream = LoadEffectResourceStream(name))
  39. {
  40. var bytecode = new byte[stream.Length];
  41. stream.Read(bytecode, 0, (int)stream.Length);
  42. return bytecode;
  43. }
  44. }
  45. internal static Stream LoadEffectResourceStream(string name)
  46. {
  47. // Detect MG version
  48. var version = "";
  49. #if !XNA
  50. version = ".8";
  51. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  52. if (mgVersion.Major == 3)
  53. {
  54. if (mgVersion.Minor == 4)
  55. version = ".6";
  56. if (mgVersion.Minor == 5)
  57. version = ".7";
  58. if (mgVersion.Minor == 6)
  59. version = ".8";
  60. if (mgVersion.Minor == 7)
  61. version = ".8";
  62. }
  63. name = name + version;
  64. #endif
  65. Stream stream = GetAssembly(typeof(DeferredCombineEffect)).GetManifestResourceStream(name);
  66. return stream;
  67. }
  68. private static Assembly GetAssembly(Type type)
  69. {
  70. #if W8_1 || W10
  71. return type.GetTypeInfo().Assembly;
  72. #else
  73. return type.Assembly;
  74. #endif
  75. }
  76. #endregion
  77. #region Public Properties
  78. public RenderTarget2D ColorMap
  79. {
  80. get { return colorMapParam.GetValueTexture2D() as RenderTarget2D; }
  81. set { colorMapParam.SetValue(value); }
  82. }
  83. public RenderTarget2D LightMap
  84. {
  85. get { return lightMapParam.GetValueTexture2D() as RenderTarget2D; }
  86. set { lightMapParam.SetValue(value); }
  87. }
  88. public Vector2 HalfPixel
  89. {
  90. get { return halfPixelParam.GetValueVector2(); }
  91. set { halfPixelParam.SetValue(value); }
  92. }
  93. #endregion
  94. #region Methods
  95. public DeferredCombineEffect(GraphicsDevice graphicsDevice)
  96. : base(graphicsDevice,
  97. #if NETFX_CORE || WP8
  98. LoadEffectResourceStream(resourceName), true
  99. #else
  100. LoadEffectResource(resourceName)
  101. #endif
  102. )
  103. {
  104. CacheEffectParameters(null);
  105. }
  106. public DeferredCombineEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  107. {
  108. CacheEffectParameters(null);
  109. }
  110. protected DeferredCombineEffect(DeferredCombineEffect cloneSource)
  111. : base(cloneSource)
  112. {
  113. CacheEffectParameters(cloneSource);
  114. }
  115. public override Effect Clone()
  116. {
  117. return new DeferredCombineEffect(this);
  118. }
  119. void CacheEffectParameters(DeferredCombineEffect cloneSource)
  120. {
  121. colorMapParam = Parameters["colorMap"];
  122. lightMapParam = Parameters["lightMap"];
  123. halfPixelParam = Parameters["halfPixel"];
  124. }
  125. #endregion
  126. }
  127. }