TestImageRender.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Numerics;
  6. using System.Text;
  7. namespace Veldrid.Common
  8. {
  9. internal class TestImageRender : BaseVeldridRender
  10. {
  11. private VeldridSpriteBatch spriteBatch;
  12. private Texture texture;
  13. private Shader[] shaders;
  14. public TestImageRender(VeldridContent device) : base(device)
  15. {
  16. shaders = CreateShader("ImageRender");
  17. spriteBatch = new VeldridSpriteBatch(GraphicsDevice, GraphicsDevice.MainSwapchain.Framebuffer.OutputDescription, shaders, GraphicsDevice.Aniso4xSampler);
  18. }
  19. public BitmapData Bitmap
  20. {
  21. get => bitmap;
  22. set
  23. {
  24. if (bitmap != value)
  25. {
  26. this[nameof(Bitmap)] = true;
  27. bitmap = value;
  28. InitTexture();
  29. }
  30. }
  31. }
  32. private void InitTexture()
  33. {
  34. if (texture == null)
  35. {
  36. texture = ResourceFactory.CreateTexture(new TextureDescription((uint)Bitmap.Width, (uint)Bitmap.Height, 1, 1, 1, PixelFormat.B8_G8_R8_A8_UNorm_SRgb, TextureUsage.Sampled, TextureType.Texture2D));
  37. }
  38. else if (texture.Height != Bitmap.Height || texture.Width != Bitmap.Width)
  39. {
  40. texture?.Dispose();
  41. texture = ResourceFactory.CreateTexture(new TextureDescription((uint)Bitmap.Width, (uint)Bitmap.Height, 1, 1, 1, PixelFormat.B8_G8_R8_A8_UNorm_SRgb, TextureUsage.Sampled, TextureType.Texture2D));
  42. }
  43. GraphicsDevice.UpdateTexture(texture, Bitmap.Data, 0, 0, 0, texture.Width, texture.Height, 1, 0, 0);
  44. }
  45. internal override void CreateResources()
  46. {
  47. base.CreateResources();
  48. if (Bitmap.Height != 0 && Bitmap.Width != 0)
  49. {
  50. InitTexture();
  51. }
  52. }
  53. internal override void PreDraw()
  54. {
  55. if (texture == null)
  56. {
  57. return;
  58. }
  59. base.PreDraw();
  60. if (this[nameof(Bitmap), nameof(Range), nameof(Local), nameof(Row), nameof(Column)] || WindowSizeState)
  61. {
  62. spriteBatch.Begin();
  63. for (int i = 0; i < 100; i++)
  64. {
  65. PointF pointF = new System.Drawing.PointF(Range.XLenght / 20 * (i % 20) + Range.MinX, Range.MinY + Range.YLenght / 10 * (i / 20));
  66. ImagePoint = GetImagePoint(pointF);
  67. spriteBatch.ViewMatrix = Matrix4x4.CreateOrthographic(MainSwapchainBuffer.Width, MainSwapchainBuffer.Height, 0.01f, -100f);
  68. spriteBatch.Draw(texture, ImagePoint, new RectangleF(3, (i%40) * 30 + 3, 24, 24), Color.Empty, 0,Vector2.Zero,1,1);
  69. }
  70. spriteBatch.End();
  71. this[nameof(Bitmap), nameof(Range), nameof(Local), nameof(Row), nameof(Column)] = false;
  72. WindowSizeState = false;
  73. }
  74. }
  75. public int Row { get => row; set =>Set(ref row,value); }
  76. public int Column { get => column; set =>Set(ref column,value); }
  77. public override Vector2 VirtualSize => new Vector2(Bitmap.Width / ContentSize.Width * (Range.MaxX - Range.MinX), bitmap.Height / ContentSize.Height * (Range.MaxY - Range.MinY));
  78. private PointF local;
  79. private BitmapData bitmap = new BitmapData();
  80. private int row;
  81. private int column;
  82. public override PointF Local
  83. {
  84. get => local;
  85. set => Set(ref local, value);
  86. }
  87. public PointF ImagePoint { get; private set; }
  88. private PointF GetImagePoint(PointF point)
  89. {
  90. float x = ContentSize.Width / (Range.MaxX - Range.MinX) * (point.X - Range.MinX) + Margin.Left - 0.5f * GraphicsDevice.MainSwapchain.Framebuffer.Width;
  91. float y = ContentSize.Height / (Range.MaxY - Range.MinY) * (point.Y - Range.MinY) + Margin.Top - 0.5f * GraphicsDevice.MainSwapchain.Framebuffer.Height;
  92. switch (VerticalAlignment)
  93. {
  94. case VerticalAlignment.Top:
  95. default:
  96. break;
  97. case VerticalAlignment.Bottom:
  98. y -= bitmap.Height;
  99. break;
  100. case VerticalAlignment.Center:
  101. y -= bitmap.Height / 2;
  102. break;
  103. }
  104. switch (HorizontalAlignment)
  105. {
  106. case HorizontalAlignment.Left:
  107. default:
  108. break;
  109. case HorizontalAlignment.Center:
  110. x -= bitmap.Width / 2;
  111. break;
  112. case HorizontalAlignment.Right:
  113. x -= bitmap.Width;
  114. break;
  115. }
  116. return new PointF(x, y);
  117. }
  118. public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Top;
  119. public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Left;
  120. internal override void DrawData()
  121. {
  122. if (texture == null) return;
  123. spriteBatch.DrawBatch(CommandList);
  124. }
  125. internal override void DisposeResources()
  126. {
  127. base.DisposeResources();
  128. texture?.Dispose();
  129. spriteBatch?.Dispose();
  130. Bitmap?.Dispose();
  131. }
  132. }
  133. }