IndexBufferState.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Silk.NET.Vulkan;
  2. using System;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. internal struct IndexBufferState
  6. {
  7. public static IndexBufferState Null => new IndexBufferState(GAL.BufferHandle.Null, 0, 0);
  8. private readonly int _offset;
  9. private readonly int _size;
  10. private readonly IndexType _type;
  11. private readonly GAL.BufferHandle _handle;
  12. private Auto<DisposableBuffer> _buffer;
  13. public IndexBufferState(GAL.BufferHandle handle, int offset, int size, IndexType type)
  14. {
  15. _handle = handle;
  16. _offset = offset;
  17. _size = size;
  18. _type = type;
  19. _buffer = null;
  20. }
  21. public IndexBufferState(GAL.BufferHandle handle, int offset, int size)
  22. {
  23. _handle = handle;
  24. _offset = offset;
  25. _size = size;
  26. _type = IndexType.Uint16;
  27. _buffer = null;
  28. }
  29. public void BindIndexBuffer(VulkanRenderer gd, CommandBufferScoped cbs)
  30. {
  31. Auto<DisposableBuffer> autoBuffer;
  32. int offset, size;
  33. IndexType type = _type;
  34. if (_type == IndexType.Uint8Ext && !gd.Capabilities.SupportsIndexTypeUint8)
  35. {
  36. // Index type is not supported. Convert to I16.
  37. autoBuffer = gd.BufferManager.GetBufferI8ToI16(cbs, _handle, _offset, _size);
  38. type = IndexType.Uint16;
  39. offset = 0;
  40. size = _size * 2;
  41. }
  42. else
  43. {
  44. autoBuffer = gd.BufferManager.GetBuffer(cbs.CommandBuffer, _handle, false, out int _);
  45. offset = _offset;
  46. size = _size;
  47. }
  48. _buffer = autoBuffer;
  49. if (autoBuffer != null)
  50. {
  51. gd.Api.CmdBindIndexBuffer(cbs.CommandBuffer, autoBuffer.Get(cbs, offset, size).Value, (ulong)offset, type);
  52. }
  53. }
  54. public void BindConvertedIndexBuffer(VulkanRenderer gd, CommandBufferScoped cbs, int firstIndex, int indexCount, int convertedCount, IndexBufferPattern pattern)
  55. {
  56. Auto<DisposableBuffer> autoBuffer;
  57. // Convert the index buffer using the given pattern.
  58. int indexSize = _type switch
  59. {
  60. IndexType.Uint32 => 4,
  61. IndexType.Uint16 => 2,
  62. _ => 1,
  63. };
  64. int firstIndexOffset = firstIndex * indexSize;
  65. autoBuffer = gd.BufferManager.GetBufferTopologyConversion(cbs, _handle, _offset + firstIndexOffset, indexCount * indexSize, pattern, indexSize);
  66. int size = convertedCount * 4;
  67. _buffer = autoBuffer;
  68. if (autoBuffer != null)
  69. {
  70. gd.Api.CmdBindIndexBuffer(cbs.CommandBuffer, autoBuffer.Get(cbs, 0, size).Value, 0, IndexType.Uint32);
  71. }
  72. }
  73. public bool BoundEquals(Auto<DisposableBuffer> buffer)
  74. {
  75. return _buffer == buffer;
  76. }
  77. }
  78. }