EffectTexture.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Microsoft.Xna.Framework.Graphics
  5. {
  6. public abstract class EffectTexture2D
  7. {
  8. #region debug
  9. protected virtual string DebuggerDisplay()
  10. {
  11. var txt = string.Empty;
  12. if (_Texture != null) txt += $"UVSet{_TextureSetIndex}-Tex({_Texture.Width},{_Texture.Height})";
  13. return txt;
  14. }
  15. #endregion
  16. #region lifecycle
  17. static EffectTexture2D()
  18. {
  19. _IsOpenGL = Resources.GetShaderProfile() == "ogl";
  20. }
  21. private static readonly bool _IsOpenGL;
  22. internal EffectTexture2D(GraphicsDevice gd, EffectParameterCollection parameters, string name, int samplerIdx)
  23. {
  24. _Device = gd;
  25. var texName = name + "Texture";
  26. if (_IsOpenGL) texName = texName + "Sampler+" + texName;
  27. _TextureMap = parameters[texName];
  28. _SamplerIndex = samplerIdx;
  29. _TextureScale = parameters[name + "Scale"];
  30. _TextureSet = parameters[name + "TextureIdx"];
  31. _TextureTransformU = parameters[name + "TransformU"];
  32. _TextureTransformV = parameters[name + "TransformV"];
  33. }
  34. #endregion
  35. #region data
  36. private GraphicsDevice _Device;
  37. private EffectParameter _TextureMap;
  38. internal EffectParameter _TextureScale;
  39. internal EffectParameter _TextureSet;
  40. internal EffectParameter _TextureTransformU;
  41. internal EffectParameter _TextureTransformV;
  42. private Texture2D _Texture;
  43. private int _SamplerIndex;
  44. private SamplerState _Sampler = SamplerState.LinearWrap;
  45. private Int32 _TextureSetIndex;
  46. // this should be defined as a Matrix3x2, but the type is missing in monogame, so...
  47. private Vector3 _TransformU = Vector3.UnitX;
  48. private Vector3 _TransformV = Vector3.UnitY;
  49. #endregion
  50. #region public
  51. public Texture2D Texture
  52. {
  53. get => _Texture;
  54. set => _Texture = value;
  55. }
  56. public SamplerState Sampler
  57. {
  58. get => _Sampler;
  59. set => _Sampler = value;
  60. }
  61. public int SetIndex
  62. {
  63. get => _TextureSetIndex;
  64. set => _TextureSetIndex = value;
  65. }
  66. // this should be defined as a Matrix3x2, but the type is missing in monogame, so...
  67. public (Vector3 U, Vector3 V) Transform
  68. {
  69. get => (_TransformU,_TransformV);
  70. set { _TransformU = value.U; _TransformV = value.V; }
  71. }
  72. #endregion
  73. #region API
  74. internal static int GetMinimumVertexUVSets(EffectTexture2D a, EffectTexture2D b, EffectTexture2D c)
  75. {
  76. int count = 0;
  77. count = Math.Max(count, a.Texture == null ? 0 : a.SetIndex +1);
  78. count = Math.Max(count, b.Texture == null ? 0 : b.SetIndex + 1);
  79. count = Math.Max(count, c.Texture == null ? 0 : c.SetIndex + 1);
  80. return count;
  81. }
  82. internal static int GetMinimumVertexUVSets(EffectTexture2D a, EffectTexture2D b, EffectTexture2D c, EffectTexture2D d, EffectTexture2D e)
  83. {
  84. int count = 0;
  85. count = Math.Max(count, a.Texture == null ? 0 : a.SetIndex + 1);
  86. count = Math.Max(count, b.Texture == null ? 0 : b.SetIndex + 1);
  87. count = Math.Max(count, c.Texture == null ? 0 : c.SetIndex + 1);
  88. count = Math.Max(count, d.Texture == null ? 0 : d.SetIndex + 1);
  89. count = Math.Max(count, e.Texture == null ? 0 : e.SetIndex + 1);
  90. return count;
  91. }
  92. internal virtual void Apply()
  93. {
  94. if (_TextureMap == null) return;
  95. _TextureMap.SetValue(_Texture);
  96. if (_Sampler != null) _Device.SamplerStates[_SamplerIndex] = _Sampler;
  97. _TextureSet.SetValue(_TextureSetIndex);
  98. _TextureTransformU.SetValue(_TransformU);
  99. _TextureTransformV.SetValue(_TransformV);
  100. }
  101. #endregion
  102. #region nested
  103. [System.Diagnostics.DebuggerDisplay("{DebuggerDisplay(),nq}")]
  104. public sealed class Scalar1 : EffectTexture2D
  105. {
  106. protected override string DebuggerDisplay() { return base.DebuggerDisplay() + $" x {Scale}"; }
  107. internal Scalar1(GraphicsDevice gd, EffectParameterCollection parameters, string name, int samplerIdx) : base(gd, parameters, name, samplerIdx) { }
  108. public float Scale { get; set; }
  109. internal override void Apply() { base.Apply(); _TextureScale.SetValue(Scale); }
  110. }
  111. [System.Diagnostics.DebuggerDisplay("{DebuggerDisplay(),nq}")]
  112. public sealed class Scalar2 : EffectTexture2D
  113. {
  114. protected override string DebuggerDisplay() { return base.DebuggerDisplay() + $" x <{Scale.X},{Scale.Y}>"; }
  115. internal Scalar2(GraphicsDevice gd, EffectParameterCollection parameters, string name, int samplerIdx) : base(gd, parameters, name, samplerIdx) { }
  116. public Vector2 Scale { get; set; }
  117. internal override void Apply() { base.Apply(); _TextureScale.SetValue(Scale); }
  118. }
  119. [System.Diagnostics.DebuggerDisplay("{DebuggerDisplay(),nq}")]
  120. public sealed class Scalar3 : EffectTexture2D
  121. {
  122. protected override string DebuggerDisplay() { return base.DebuggerDisplay() + $" x <{Scale.X},{Scale.Y},{Scale.Z}>"; }
  123. internal Scalar3(GraphicsDevice gd, EffectParameterCollection parameters, string name, int samplerIdx) : base(gd, parameters, name, samplerIdx) { }
  124. public Vector3 Scale { get; set; }
  125. internal override void Apply() { base.Apply(); _TextureScale.SetValue(Scale); }
  126. }
  127. [System.Diagnostics.DebuggerDisplay("{DebuggerDisplay(),nq}")]
  128. public sealed class Scalar4 : EffectTexture2D
  129. {
  130. protected override string DebuggerDisplay() { return base.DebuggerDisplay() + $" x <{Scale.X},{Scale.Y},{Scale.Z},{Scale.W}>"; }
  131. internal Scalar4(GraphicsDevice gd, EffectParameterCollection parameters, string name, int samplerIdx) : base(gd, parameters, name, samplerIdx) { }
  132. public Vector4 Scale { get; set; }
  133. internal override void Apply() { base.Apply(); _TextureScale.SetValue(Scale); }
  134. }
  135. #endregion
  136. }
  137. }