SpirvReflection.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Converters;
  3. using System;
  4. using System.IO;
  5. namespace Veldrid.SPIRV
  6. {
  7. /// <summary>
  8. /// Contains information about the vertex attributes and resource types, and their binding slots, for a compiled
  9. /// set of shaders. This information can be used to construct <see cref="ResourceLayout"/> and
  10. /// <see cref="Pipeline"/> objects.
  11. /// </summary>
  12. public class SpirvReflection
  13. {
  14. private static readonly Lazy<JsonSerializer> s_serializer = new Lazy<JsonSerializer>(CreateSerializer);
  15. /// <summary>
  16. /// An array containing a description of each vertex element that is used by the compiled shader set.
  17. /// This array will be empty for compute shaders.
  18. /// </summary>
  19. public VertexElementDescription[] VertexElements { get; }
  20. /// <summary>
  21. /// An array containing a description of each set of resources used by the compiled shader set.
  22. /// </summary>
  23. public ResourceLayoutDescription[] ResourceLayouts { get; }
  24. /// <summary>
  25. /// Constructs a new <see cref="SpirvReflection"/> instance.
  26. /// </summary>
  27. /// <param name="vertexElements">/// An array containing a description of each vertex element that is used by
  28. /// the compiled shader set.</param>
  29. /// <param name="resourceLayouts">An array containing a description of each set of resources used by the
  30. /// compiled shader set.</param>
  31. public SpirvReflection(
  32. VertexElementDescription[] vertexElements,
  33. ResourceLayoutDescription[] resourceLayouts)
  34. {
  35. VertexElements = vertexElements;
  36. ResourceLayouts = resourceLayouts;
  37. }
  38. /// <summary>
  39. /// Loads a <see cref="SpirvReflection"/> object from a serialized JSON file at the given path.
  40. /// </summary>
  41. /// <param name="jsonPath">The path to the JSON file.</param>
  42. /// <returns>A new <see cref="SpirvReflection"/> object, deserialized from the file.</returns>
  43. public static SpirvReflection LoadFromJson(string jsonPath)
  44. {
  45. using (FileStream jsonStream = File.OpenRead(jsonPath))
  46. {
  47. return LoadFromJson(jsonStream);
  48. }
  49. }
  50. /// <summary>
  51. /// Loads a <see cref="SpirvReflection"/> object from a serialized JSON stream.
  52. /// </summary>
  53. /// <param name="jsonStream">The stream of serialized JSON text.</param>
  54. /// <returns>A new <see cref="SpirvReflection"/> object, deserialized from the stream.</returns>
  55. public static SpirvReflection LoadFromJson(Stream jsonStream)
  56. {
  57. using (StreamReader sr = new StreamReader(jsonStream))
  58. using (JsonTextReader jtr = new JsonTextReader(sr))
  59. {
  60. return s_serializer.Value.Deserialize<SpirvReflection>(jtr);
  61. }
  62. }
  63. private static JsonSerializer CreateSerializer()
  64. {
  65. JsonSerializer serializer = new JsonSerializer();
  66. serializer.Formatting = Formatting.Indented;
  67. StringEnumConverter enumConverter = new StringEnumConverter();
  68. serializer.Converters.Add(enumConverter);
  69. return serializer;
  70. }
  71. }
  72. }