VertexBufferState.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using BufferHandle = Ryujinx.Graphics.GAL.BufferHandle;
  2. namespace Ryujinx.Graphics.Vulkan
  3. {
  4. internal struct VertexBufferState
  5. {
  6. public static VertexBufferState Null => new VertexBufferState(null, 0, 0, 0);
  7. private readonly int _offset;
  8. private readonly int _size;
  9. private readonly int _stride;
  10. private readonly BufferHandle _handle;
  11. private Auto<DisposableBuffer> _buffer;
  12. internal readonly int DescriptorIndex;
  13. internal int AttributeScalarAlignment;
  14. public VertexBufferState(Auto<DisposableBuffer> buffer, int descriptorIndex, int offset, int size, int stride = 0)
  15. {
  16. _buffer = buffer;
  17. _handle = BufferHandle.Null;
  18. _offset = offset;
  19. _size = size;
  20. _stride = stride;
  21. DescriptorIndex = descriptorIndex;
  22. AttributeScalarAlignment = 1;
  23. buffer?.IncrementReferenceCount();
  24. }
  25. public VertexBufferState(BufferHandle handle, int descriptorIndex, int offset, int size, int stride = 0)
  26. {
  27. // This buffer state may be rewritten at bind time, so it must be retrieved on bind.
  28. _buffer = null;
  29. _handle = handle;
  30. _offset = offset;
  31. _size = size;
  32. _stride = stride;
  33. DescriptorIndex = descriptorIndex;
  34. AttributeScalarAlignment = 1;
  35. }
  36. public void BindVertexBuffer(VulkanRenderer gd, CommandBufferScoped cbs, uint binding, ref PipelineState state)
  37. {
  38. var autoBuffer = _buffer;
  39. if (_handle != BufferHandle.Null)
  40. {
  41. // May need to restride the vertex buffer.
  42. if (gd.NeedsVertexBufferAlignment(AttributeScalarAlignment, out int alignment) && (_stride % alignment) != 0)
  43. {
  44. autoBuffer = gd.BufferManager.GetAlignedVertexBuffer(cbs, _handle, _offset, _size, _stride, alignment);
  45. int stride = (_stride + (alignment - 1)) & -alignment;
  46. var buffer = autoBuffer.Get(cbs, _offset, _size).Value;
  47. if (gd.Capabilities.SupportsExtendedDynamicState)
  48. {
  49. gd.ExtendedDynamicStateApi.CmdBindVertexBuffers2(
  50. cbs.CommandBuffer,
  51. binding,
  52. 1,
  53. buffer,
  54. 0,
  55. (ulong)(_size / _stride) * (ulong)stride,
  56. (ulong)stride);
  57. }
  58. else
  59. {
  60. gd.Api.CmdBindVertexBuffers(cbs.CommandBuffer, binding, 1, buffer, 0);
  61. }
  62. _buffer = autoBuffer;
  63. state.Internal.VertexBindingDescriptions[DescriptorIndex].Stride = (uint)stride;
  64. return;
  65. }
  66. else
  67. {
  68. autoBuffer = gd.BufferManager.GetBuffer(cbs.CommandBuffer, _handle, false, out int _);
  69. // The original stride must be reapplied in case it was rewritten.
  70. state.Internal.VertexBindingDescriptions[DescriptorIndex].Stride = (uint)_stride;
  71. }
  72. }
  73. if (autoBuffer != null)
  74. {
  75. var buffer = autoBuffer.Get(cbs, _offset, _size).Value;
  76. if (gd.Capabilities.SupportsExtendedDynamicState)
  77. {
  78. gd.ExtendedDynamicStateApi.CmdBindVertexBuffers2(
  79. cbs.CommandBuffer,
  80. binding,
  81. 1,
  82. buffer,
  83. (ulong)_offset,
  84. (ulong)_size,
  85. (ulong)_stride);
  86. }
  87. else
  88. {
  89. gd.Api.CmdBindVertexBuffers(cbs.CommandBuffer, binding, 1, buffer, (ulong)_offset);
  90. }
  91. }
  92. }
  93. public bool BoundEquals(Auto<DisposableBuffer> buffer)
  94. {
  95. return _buffer == buffer;
  96. }
  97. public void Dispose()
  98. {
  99. // Only dispose if this buffer is not refetched on each bind.
  100. if (_handle == BufferHandle.Null)
  101. {
  102. _buffer?.DecrementReferenceCount();
  103. }
  104. }
  105. }
  106. }