DeferredCombineEffect.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. using (var ms = new MemoryStream())
  40. {
  41. stream.CopyTo(ms);
  42. return ms.ToArray();
  43. }
  44. }
  45. internal static Stream LoadEffectResourceStream(string name)
  46. {
  47. // Detect MG version
  48. var mgVersion = GetAssembly(typeof(Effect)).GetName().Version;
  49. if (mgVersion.Major == 3)
  50. {
  51. if (mgVersion.Minor == 4)
  52. name += ".6";
  53. if (mgVersion.Minor == 5)
  54. name += ".7";
  55. if (mgVersion.Minor == 6)
  56. name += ".8";
  57. }
  58. Stream stream = GetAssembly(typeof(DeferredCombineEffect)).GetManifestResourceStream(name);
  59. return stream;
  60. }
  61. private static Assembly GetAssembly(Type type)
  62. {
  63. #if W8_1 || W10
  64. return type.GetTypeInfo().Assembly;
  65. #else
  66. return type.Assembly;
  67. #endif
  68. }
  69. #endregion
  70. #region Public Properties
  71. public RenderTarget2D ColorMap
  72. {
  73. get { return colorMapParam.GetValueTexture2D() as RenderTarget2D; }
  74. set { colorMapParam.SetValue(value); }
  75. }
  76. public RenderTarget2D LightMap
  77. {
  78. get { return lightMapParam.GetValueTexture2D() as RenderTarget2D; }
  79. set { lightMapParam.SetValue(value); }
  80. }
  81. public Vector2 HalfPixel
  82. {
  83. get { return halfPixelParam.GetValueVector2(); }
  84. set { halfPixelParam.SetValue(value); }
  85. }
  86. #endregion
  87. #region Methods
  88. public DeferredCombineEffect(GraphicsDevice graphicsDevice)
  89. : base(graphicsDevice,
  90. #if NETFX_CORE || WP8
  91. LoadEffectResourceStream(resourceName), true
  92. #else
  93. LoadEffectResource(resourceName)
  94. #endif
  95. )
  96. {
  97. CacheEffectParameters(null);
  98. }
  99. public DeferredCombineEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  100. {
  101. CacheEffectParameters(null);
  102. }
  103. protected DeferredCombineEffect(DeferredCombineEffect cloneSource)
  104. : base(cloneSource)
  105. {
  106. CacheEffectParameters(cloneSource);
  107. }
  108. public override Effect Clone()
  109. {
  110. return new DeferredCombineEffect(this);
  111. }
  112. void CacheEffectParameters(DeferredCombineEffect cloneSource)
  113. {
  114. colorMapParam = Parameters["colorMap"];
  115. lightMapParam = Parameters["lightMap"];
  116. halfPixelParam = Parameters["halfPixel"];
  117. }
  118. #endregion
  119. }
  120. }