DeferredBasicEffect.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 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(GraphicsDevice graphicsDevice, string name)
  34. {
  35. name = GetResourceName(graphicsDevice, name);
  36. using (Stream stream = typeof(DeferredBasicEffect).Assembly.GetManifestResourceStream(name))
  37. {
  38. byte[] bytecode = new byte[stream.Length];
  39. stream.Read(bytecode, 0, (int)stream.Length);
  40. return bytecode;
  41. }
  42. }
  43. private static string GetResourceName(GraphicsDevice graphicsDevice, string name)
  44. {
  45. string platformName = "";
  46. string version = "";
  47. #if XNA
  48. platformName = ".xna.WinReach";
  49. #else
  50. switch (graphicsDevice.Adapter.Backend)
  51. {
  52. case GraphicsBackend.DirectX11:
  53. platformName = ".dx11.fxo";
  54. break;
  55. case GraphicsBackend.OpenGL:
  56. platformName = ".ogl.fxo";
  57. break;
  58. case GraphicsBackend.GLES:
  59. case GraphicsBackend.WebGL:
  60. platformName = ".gles.fxo";
  61. break;
  62. default:
  63. throw new NotSupportedException("Backend");
  64. }
  65. // Detect version
  66. version = ".10";
  67. Version kniVersion = typeof(Effect).Assembly.GetName().Version;
  68. {
  69. if (kniVersion.Minor == 9
  70. || kniVersion.Minor == 10
  71. || kniVersion.Minor == 11
  72. || kniVersion.Minor == 12
  73. || kniVersion.Minor == 13
  74. || kniVersion.Minor == 14)
  75. version = ".10";
  76. }
  77. if (kniVersion.Major == 4)
  78. {
  79. if (kniVersion.Minor == 0
  80. || kniVersion.Minor == 1)
  81. version = ".10";
  82. }
  83. #endif
  84. return name + platformName + version;
  85. }
  86. #endregion
  87. #region Public Properties
  88. public Matrix Projection
  89. {
  90. get { return projectionParam.GetValueMatrix(); }
  91. set { projectionParam.SetValue(value); }
  92. }
  93. public Matrix View
  94. {
  95. get { return viewParam.GetValueMatrix(); }
  96. set { viewParam.SetValue(value); }
  97. }
  98. public Matrix World
  99. {
  100. get { return worldParam.GetValueMatrix(); }
  101. set { worldParam.SetValue(value); }
  102. }
  103. #endregion
  104. #region Methods
  105. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  106. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  107. {
  108. CacheEffectParameters(null);
  109. }
  110. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  111. {
  112. CacheEffectParameters(null);
  113. }
  114. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  115. : base(cloneSource)
  116. {
  117. CacheEffectParameters(cloneSource);
  118. }
  119. public override Effect Clone()
  120. {
  121. return new DeferredBasicEffect(this);
  122. }
  123. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  124. {
  125. // IEffectMatrices
  126. projectionParam = Parameters["Projection"];
  127. viewParam = Parameters["View"];
  128. worldParam = Parameters["World"];
  129. }
  130. #endregion
  131. }
  132. }