BaseVeldridRender.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Numerics;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Veldrid.Common.Tools;
  13. namespace Veldrid.Common.VeldridRender
  14. {
  15. internal abstract class BaseVeldridRender
  16. {
  17. protected Object _Locker = new Object();
  18. public Boolean IsDisposed { get; protected set; } = false;
  19. protected CommandList CommandList { get; }
  20. private ShaderManger ShaderManger { get; }
  21. protected Framebuffer MainSwapchainBuffer => GraphicsDevice.MainSwapchain.Framebuffer;
  22. internal Camera Camera { get; }
  23. public Boolean WindowSizeState { get; set; } = true;
  24. private Dictionary<string, bool> propertyChangedDictionary = new Dictionary<string, bool>();
  25. internal GraphicsDevice GraphicsDevice { get; }
  26. internal Vector2 WindowSize => new Vector2(GraphicsDevice.MainSwapchain.Framebuffer.Width, GraphicsDevice.MainSwapchain.Framebuffer.Height);
  27. private protected ResourceFactory ResourceFactory => GraphicsDevice.ResourceFactory;
  28. private bool TryGetPropertyState(string propertyName)
  29. {
  30. if (String.IsNullOrEmpty(propertyName))
  31. {
  32. return false;
  33. }
  34. if (propertyChangedDictionary.TryGetValue(propertyName, out var result))
  35. {
  36. return result;
  37. }
  38. propertyChangedDictionary[propertyName] = false;
  39. return false;
  40. }
  41. public virtual bool Contains(PointF point)
  42. {
  43. if (!Visibily) return false;
  44. RectangleF rectangle = new RectangleF(Local, new SizeF(VirtualSize.X, VirtualSize.Y));
  45. return rectangle.Contains(point);
  46. }
  47. /// <summary>
  48. /// 元素在虚拟坐标系中的绝对位置,
  49. /// 此参数的设置对波形绘制等无效,设置波形的位置请使用<see cref="Margin"/>
  50. /// </summary>
  51. public virtual PointF Local
  52. {
  53. get;
  54. set;
  55. }
  56. public virtual System.Numerics.Vector2 VirtualSize { get; }
  57. internal protected Boolean this[params string[] propertyNames]
  58. {
  59. get
  60. {
  61. if (propertyNames == null || propertyNames.Length == 0)
  62. {
  63. return false;
  64. }
  65. if (propertyNames.Length == 1)
  66. {
  67. return TryGetPropertyState(propertyNames[0]);
  68. }
  69. return propertyNames.Select(x => TryGetPropertyState(x)).Any(x => x);
  70. }
  71. set
  72. {
  73. if (propertyNames == null || propertyNames.Length == 0)
  74. {
  75. return;
  76. }
  77. foreach (var name in propertyNames.Where(x => !string.IsNullOrEmpty(x)))
  78. {
  79. propertyChangedDictionary[name] = value;
  80. }
  81. }
  82. }
  83. private Padding margin = new Padding();
  84. public Padding Margin
  85. {
  86. get { return margin; }
  87. set { Set(ref margin, value); }
  88. }
  89. public RectangleF Rectangle => new RectangleF(Margin.Left, Margin.Top, MainSwapchainBuffer.Width - Margin.Left - Margin.Right, MainSwapchainBuffer.Height - Margin.Top - Margin.Bottom);
  90. private LineRange range = new LineRange(0, 10000, -5000, 5000);
  91. public LineRange Range
  92. {
  93. get { return range; }
  94. set { Set(ref range, value); }
  95. }
  96. public BaseVeldridRender(IVeldridContent control)
  97. {
  98. GraphicsDevice = control.GraphicsManger.Device;
  99. CommandList = control.GraphicsManger.CommandList;
  100. Range = control.GraphicsManger.DefaultLineRange;
  101. Margin = control.GraphicsManger.DefaultPadding;
  102. Camera = control.GraphicsManger.Camera;
  103. ShaderManger = control.GraphicsManger.ShaderManger;
  104. }
  105. protected void Set<T>(ref T field, T value, [CallerMemberName] string propertyName = "")
  106. {
  107. if (object.Equals(field, value))
  108. {
  109. return;
  110. }
  111. field = value;
  112. this[propertyName] = true;
  113. Update(propertyName, value);
  114. }
  115. public virtual void CreateResources()
  116. {
  117. IsDisposed = false;
  118. }
  119. internal virtual void PreDraw()
  120. {
  121. }
  122. public void Draw()
  123. {
  124. lock (_Locker)
  125. {
  126. if (Visibily && !IsDisposed)
  127. {
  128. PreDraw();
  129. DrawData();
  130. PosDraw();
  131. }
  132. }
  133. }
  134. internal virtual void DrawData()
  135. {
  136. }
  137. internal virtual void PosDraw()
  138. {
  139. }
  140. public virtual void DisposeResources()
  141. {
  142. this.ClearEventHandle();
  143. IsDisposed = true;
  144. }
  145. public virtual void Update(string propertyName, object value)
  146. {
  147. }
  148. private protected DeviceBuffer CreateVertexBuffer<T>(T[] value) where T : unmanaged
  149. {
  150. var buffer = GraphicsDevice.ResourceFactory.CreateBuffer(new BufferDescription((uint)(value.Length * Unsafe.SizeOf<T>()), BufferUsage.VertexBuffer));
  151. GraphicsDevice.UpdateBuffer(buffer, 0, value);
  152. return buffer;
  153. }
  154. private protected DeviceBuffer CreateIndexBuffer(ushort[] index)
  155. {
  156. var buffer = ResourceFactory.CreateBuffer(new BufferDescription((uint)(index.Length * Unsafe.SizeOf<ushort>()), BufferUsage.IndexBuffer));
  157. GraphicsDevice.UpdateBuffer(buffer, 0, index);
  158. return buffer;
  159. }
  160. private protected DeviceBuffer CreateIndexBuffer(uint[] index)
  161. {
  162. var buffer = ResourceFactory.CreateBuffer(new BufferDescription((uint)(index.Length * Unsafe.SizeOf<uint>()), BufferUsage.IndexBuffer));
  163. GraphicsDevice.UpdateBuffer(buffer, 0, index);
  164. return buffer;
  165. }
  166. private protected Shader[] CreateShader(string name)
  167. {
  168. return ShaderManger.GetShaders(name);
  169. }
  170. private protected Shader GetLocalFileShader(string path, string entrypoint = "main", ShaderStages stages = ShaderStages.Vertex) => ShaderManger.GetLocalFileShader(path, entrypoint, stages);
  171. private protected Shader GetOtherShader(string name, ShaderStages stages = ShaderStages.Geometry, string entrypoints = "main")
  172. {
  173. return ShaderManger.GetOtherShader(name, stages, entrypoints);
  174. }
  175. private protected Pipeline CreatePipLine(PrimitiveTopology primitiveTopology,
  176. ResourceLayout layout,
  177. VertexLayoutDescription vertexLayout,
  178. Shader[] shaders,
  179. BlendStateDescription? blend = null,
  180. bool depthTestEnabled = true,
  181. OutputDescription? outputDescription = null,
  182. FrontFace frontFace = FrontFace.Clockwise)
  183. {
  184. return CreatePipLine(primitiveTopology, new ResourceLayout[] { layout }, new VertexLayoutDescription[] { vertexLayout }, shaders, blend, depthTestEnabled, outputDescription, frontFace);
  185. }
  186. private protected Pipeline CreatePipLine(PrimitiveTopology primitiveTopology,
  187. ResourceLayout[] layouts,
  188. VertexLayoutDescription[] vertexLayouts,
  189. Shader[] shaders,
  190. BlendStateDescription? blend = null,
  191. bool depthTestEnabled = true,
  192. OutputDescription? outputDescription = null,
  193. FrontFace frontFace = FrontFace.Clockwise)
  194. {
  195. GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription();
  196. pipelineDescription.BlendState = blend ?? BlendStateDescription.SingleAlphaBlend;
  197. pipelineDescription.DepthStencilState = new DepthStencilStateDescription(
  198. depthTestEnabled: depthTestEnabled,
  199. depthWriteEnabled: true,
  200. comparisonKind: ComparisonKind.LessEqual);
  201. pipelineDescription.RasterizerState = new RasterizerStateDescription(
  202. cullMode: FaceCullMode.Back,
  203. fillMode: PolygonFillMode.Solid,
  204. frontFace: frontFace,
  205. depthClipEnabled: true,
  206. scissorTestEnabled: false);
  207. pipelineDescription.PrimitiveTopology = primitiveTopology;
  208. pipelineDescription.ResourceLayouts = layouts;
  209. pipelineDescription.Outputs = outputDescription ?? GraphicsDevice.SwapchainFramebuffer.OutputDescription;
  210. pipelineDescription.ShaderSet = new ShaderSetDescription(
  211. vertexLayouts: vertexLayouts,
  212. shaders: shaders,
  213. specializations: new[] { new SpecializationConstant(0, GraphicsDevice.IsClipSpaceYInverted) });
  214. return ResourceFactory.CreateGraphicsPipeline(pipelineDescription);
  215. }
  216. public bool Visibily { get; set; } = true;
  217. private protected struct WindowsInfo
  218. {
  219. }
  220. }
  221. }