DeferredBasicEffect.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. case GraphicsBackend.GLES:
  57. case GraphicsBackend.WebGL:
  58. platformName = ".ogl.fxo";
  59. break;
  60. default:
  61. throw new NotSupportedException("platform");
  62. }
  63. // Detect version
  64. version = ".10";
  65. Version kniVersion = typeof(Effect).Assembly.GetName().Version;
  66. if (kniVersion.Major == 3)
  67. {
  68. if (kniVersion.Minor == 9)
  69. {
  70. version = ".10";
  71. }
  72. if (kniVersion.Minor == 10)
  73. {
  74. version = ".10";
  75. }
  76. }
  77. #endif
  78. return name + platformName + version;
  79. }
  80. #endregion
  81. #region Public Properties
  82. public Matrix Projection
  83. {
  84. get { return projectionParam.GetValueMatrix(); }
  85. set { projectionParam.SetValue(value); }
  86. }
  87. public Matrix View
  88. {
  89. get { return viewParam.GetValueMatrix(); }
  90. set { viewParam.SetValue(value); }
  91. }
  92. public Matrix World
  93. {
  94. get { return worldParam.GetValueMatrix(); }
  95. set { worldParam.SetValue(value); }
  96. }
  97. #endregion
  98. #region Methods
  99. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  100. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  101. {
  102. CacheEffectParameters(null);
  103. }
  104. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  105. {
  106. CacheEffectParameters(null);
  107. }
  108. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  109. : base(cloneSource)
  110. {
  111. CacheEffectParameters(cloneSource);
  112. }
  113. public override Effect Clone()
  114. {
  115. return new DeferredBasicEffect(this);
  116. }
  117. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  118. {
  119. // IEffectMatrices
  120. projectionParam = Parameters["Projection"];
  121. viewParam = Parameters["View"];
  122. worldParam = Parameters["World"];
  123. }
  124. #endregion
  125. }
  126. }