VertexBufferUpdater.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using VkBuffer = Silk.NET.Vulkan.Buffer;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. internal class VertexBufferUpdater : IDisposable
  6. {
  7. private readonly VulkanRenderer _gd;
  8. private uint _baseBinding;
  9. private uint _count;
  10. private readonly NativeArray<VkBuffer> _buffers;
  11. private readonly NativeArray<ulong> _offsets;
  12. private readonly NativeArray<ulong> _sizes;
  13. private readonly NativeArray<ulong> _strides;
  14. public VertexBufferUpdater(VulkanRenderer gd)
  15. {
  16. _gd = gd;
  17. _buffers = new NativeArray<VkBuffer>(Constants.MaxVertexBuffers);
  18. _offsets = new NativeArray<ulong>(Constants.MaxVertexBuffers);
  19. _sizes = new NativeArray<ulong>(Constants.MaxVertexBuffers);
  20. _strides = new NativeArray<ulong>(Constants.MaxVertexBuffers);
  21. }
  22. public void BindVertexBuffer(CommandBufferScoped cbs, uint binding, VkBuffer buffer, ulong offset, ulong size, ulong stride)
  23. {
  24. if (_count == 0)
  25. {
  26. _baseBinding = binding;
  27. }
  28. else if (_baseBinding + _count != binding)
  29. {
  30. Commit(cbs);
  31. _baseBinding = binding;
  32. }
  33. int index = (int)_count;
  34. _buffers[index] = buffer;
  35. _offsets[index] = offset;
  36. _sizes[index] = size;
  37. _strides[index] = stride;
  38. _count++;
  39. }
  40. public unsafe void Commit(CommandBufferScoped cbs)
  41. {
  42. if (_count != 0)
  43. {
  44. if (_gd.Capabilities.SupportsExtendedDynamicState)
  45. {
  46. _gd.ExtendedDynamicStateApi.CmdBindVertexBuffers2(
  47. cbs.CommandBuffer,
  48. _baseBinding,
  49. _count,
  50. _buffers.Pointer,
  51. _offsets.Pointer,
  52. _sizes.Pointer,
  53. _strides.Pointer);
  54. }
  55. else
  56. {
  57. _gd.Api.CmdBindVertexBuffers(cbs.CommandBuffer, _baseBinding, _count, _buffers.Pointer, _offsets.Pointer);
  58. }
  59. _count = 0;
  60. }
  61. }
  62. public void Dispose()
  63. {
  64. _buffers.Dispose();
  65. _offsets.Dispose();
  66. _sizes.Dispose();
  67. _strides.Dispose();
  68. }
  69. }
  70. }