IndexBufferState.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 bufferSize);
  45. if (_offset >= bufferSize)
  46. {
  47. autoBuffer = null;
  48. }
  49. offset = _offset;
  50. size = _size;
  51. }
  52. _buffer = autoBuffer;
  53. if (autoBuffer != null)
  54. {
  55. gd.Api.CmdBindIndexBuffer(cbs.CommandBuffer, autoBuffer.Get(cbs, offset, size).Value, (ulong)offset, type);
  56. }
  57. }
  58. public void BindConvertedIndexBuffer(VulkanRenderer gd, CommandBufferScoped cbs, int firstIndex, int indexCount, int convertedCount, IndexBufferPattern pattern)
  59. {
  60. Auto<DisposableBuffer> autoBuffer;
  61. // Convert the index buffer using the given pattern.
  62. int indexSize = _type switch
  63. {
  64. IndexType.Uint32 => 4,
  65. IndexType.Uint16 => 2,
  66. _ => 1,
  67. };
  68. int firstIndexOffset = firstIndex * indexSize;
  69. autoBuffer = gd.BufferManager.GetBufferTopologyConversion(cbs, _handle, _offset + firstIndexOffset, indexCount * indexSize, pattern, indexSize);
  70. int size = convertedCount * 4;
  71. _buffer = autoBuffer;
  72. if (autoBuffer != null)
  73. {
  74. gd.Api.CmdBindIndexBuffer(cbs.CommandBuffer, autoBuffer.Get(cbs, 0, size).Value, 0, IndexType.Uint32);
  75. }
  76. }
  77. public bool BoundEquals(Auto<DisposableBuffer> buffer)
  78. {
  79. return _buffer == buffer;
  80. }
  81. }
  82. }