DeferredCombineEffect.cs 4.7 KB

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