DeferredBasicEffect.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = GetAssembly(typeof(DeferredBasicEffect)).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 = GetAssembly(typeof(Effect)).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. private static Assembly GetAssembly(Type type)
  81. {
  82. #if W10
  83. return type.GetTypeInfo().Assembly;
  84. #else
  85. return type.Assembly;
  86. #endif
  87. }
  88. #endregion
  89. #region Public Properties
  90. public Matrix Projection
  91. {
  92. get { return projectionParam.GetValueMatrix(); }
  93. set { projectionParam.SetValue(value); }
  94. }
  95. public Matrix View
  96. {
  97. get { return viewParam.GetValueMatrix(); }
  98. set { viewParam.SetValue(value); }
  99. }
  100. public Matrix World
  101. {
  102. get { return worldParam.GetValueMatrix(); }
  103. set { worldParam.SetValue(value); }
  104. }
  105. #endregion
  106. #region Methods
  107. public DeferredBasicEffect(GraphicsDevice graphicsDevice)
  108. : base(graphicsDevice, LoadEffectResource(graphicsDevice, ResourceName))
  109. {
  110. CacheEffectParameters(null);
  111. }
  112. public DeferredBasicEffect(GraphicsDevice graphicsDevice, byte[] Bytecode): base(graphicsDevice, Bytecode)
  113. {
  114. CacheEffectParameters(null);
  115. }
  116. protected DeferredBasicEffect(DeferredBasicEffect cloneSource)
  117. : base(cloneSource)
  118. {
  119. CacheEffectParameters(cloneSource);
  120. }
  121. public override Effect Clone()
  122. {
  123. return new DeferredBasicEffect(this);
  124. }
  125. void CacheEffectParameters(DeferredBasicEffect cloneSource)
  126. {
  127. // IEffectMatrices
  128. projectionParam = Parameters["Projection"];
  129. viewParam = Parameters["View"];
  130. worldParam = Parameters["World"];
  131. }
  132. #endregion
  133. }
  134. }