ResourceFactoryExtensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System.Text;
  2. namespace Veldrid.SPIRV
  3. {
  4. /// <summary>
  5. /// Contains extension methods for loading <see cref="Shader"/> modules from SPIR-V bytecode.
  6. /// </summary>
  7. public static class ResourceFactoryExtensions
  8. {
  9. /// <summary>
  10. /// Creates a vertex and fragment shader pair from the given <see cref="ShaderDescription"/> pair containing SPIR-V
  11. /// bytecode or GLSL source code.
  12. /// </summary>
  13. /// <param name="factory">The <see cref="ResourceFactory"/> used to compile the translated shader code.</param>
  14. /// <param name="vertexShaderDescription">The vertex shader's description. <see cref="ShaderDescription.ShaderBytes"/>
  15. /// should contain SPIR-V bytecode or Vulkan-style GLSL source code which can be compiled to SPIR-V.</param>
  16. /// <param name="fragmentShaderDescription">The fragment shader's description.
  17. /// <see cref="ShaderDescription.ShaderBytes"/> should contain SPIR-V bytecode or Vulkan-style GLSL source code which
  18. /// can be compiled to SPIR-V.</param>
  19. /// <returns>A two-element array, containing the vertex shader (element 0) and the fragment shader (element 1).</returns>
  20. public static Shader[] CreateFromSpirv(
  21. this ResourceFactory factory,
  22. ShaderDescription vertexShaderDescription,
  23. ShaderDescription fragmentShaderDescription)
  24. {
  25. return CreateFromSpirv(factory, vertexShaderDescription, fragmentShaderDescription, new CrossCompileOptions());
  26. }
  27. /// <summary>
  28. /// Creates a vertex and fragment shader pair from the given <see cref="ShaderDescription"/> pair containing SPIR-V
  29. /// bytecode or GLSL source code.
  30. /// </summary>
  31. /// <param name="factory">The <see cref="ResourceFactory"/> used to compile the translated shader code.</param>
  32. /// <param name="vertexShaderDescription">The vertex shader's description. <see cref="ShaderDescription.ShaderBytes"/>
  33. /// should contain SPIR-V bytecode or Vulkan-style GLSL source code which can be compiled to SPIR-V.</param>
  34. /// <param name="fragmentShaderDescription">The fragment shader's description.
  35. /// <see cref="ShaderDescription.ShaderBytes"/> should contain SPIR-V bytecode or Vulkan-style GLSL source code which
  36. /// can be compiled to SPIR-V.</param>
  37. /// <param name="options">The <see cref="CrossCompileOptions"/> which will control the parameters used to translate the
  38. /// shaders from SPIR-V to the target language.</param>
  39. /// <returns>A two-element array, containing the vertex shader (element 0) and the fragment shader (element 1).</returns>
  40. public static Shader[] CreateFromSpirv(
  41. this ResourceFactory factory,
  42. ShaderDescription vertexShaderDescription,
  43. ShaderDescription fragmentShaderDescription,
  44. CrossCompileOptions options)
  45. {
  46. GraphicsBackend backend = factory.BackendType;
  47. if (backend == GraphicsBackend.Vulkan)
  48. {
  49. vertexShaderDescription.ShaderBytes = EnsureSpirv(vertexShaderDescription);
  50. fragmentShaderDescription.ShaderBytes = EnsureSpirv(fragmentShaderDescription);
  51. return new Shader[]
  52. {
  53. factory.CreateShader(ref vertexShaderDescription),
  54. factory.CreateShader(ref fragmentShaderDescription)
  55. };
  56. }
  57. CrossCompileTarget target = GetCompilationTarget(factory.BackendType);
  58. VertexFragmentCompilationResult compilationResult = SpirvCompilation.CompileVertexFragment(
  59. vertexShaderDescription.ShaderBytes,
  60. fragmentShaderDescription.ShaderBytes,
  61. target,
  62. options);
  63. string vertexEntryPoint = (backend == GraphicsBackend.Metal && vertexShaderDescription.EntryPoint == "main")
  64. ? "main0"
  65. : vertexShaderDescription.EntryPoint;
  66. byte[] vertexBytes = GetBytes(backend, compilationResult.VertexShader);
  67. Shader vertexShader = factory.CreateShader(new ShaderDescription(
  68. vertexShaderDescription.Stage,
  69. vertexBytes,
  70. vertexEntryPoint));
  71. string fragmentEntryPoint = (backend == GraphicsBackend.Metal && fragmentShaderDescription.EntryPoint == "main")
  72. ? "main0"
  73. : fragmentShaderDescription.EntryPoint;
  74. byte[] fragmentBytes = GetBytes(backend, compilationResult.FragmentShader);
  75. Shader fragmentShader = factory.CreateShader(new ShaderDescription(
  76. fragmentShaderDescription.Stage,
  77. fragmentBytes,
  78. fragmentEntryPoint));
  79. return new Shader[] { vertexShader, fragmentShader };
  80. }
  81. /// <summary>
  82. ///
  83. /// </summary>
  84. /// <param name="vertex"></param>
  85. /// <param name="fragment"></param>
  86. /// <param name="backend"></param>
  87. public static void CompileFromSpirv(ref ShaderDescription vertex,ref ShaderDescription fragment,GraphicsBackend backend = GraphicsBackend.Vulkan)
  88. {
  89. CompileFromSpirv(ref vertex, ref fragment, new CrossCompileOptions(), backend);
  90. }
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. /// <param name="vertex"></param>
  95. /// <param name="fragment"></param>
  96. /// <param name="options"></param>
  97. /// <param name="backend"></param>
  98. public static void CompileFromSpirv(ref ShaderDescription vertex, ref ShaderDescription fragment,CrossCompileOptions options, GraphicsBackend backend = GraphicsBackend.Vulkan)
  99. {
  100. if (backend == GraphicsBackend.Vulkan)
  101. {
  102. vertex.ShaderBytes = EnsureSpirv(vertex);
  103. fragment.ShaderBytes = EnsureSpirv(fragment);
  104. return;
  105. }
  106. CrossCompileTarget target = GetCompilationTarget(backend);
  107. VertexFragmentCompilationResult compilationResult = SpirvCompilation.CompileVertexFragment(
  108. vertex.ShaderBytes,
  109. fragment.ShaderBytes,
  110. target,
  111. options);
  112. vertex.EntryPoint = (backend == GraphicsBackend.Metal && vertex.EntryPoint == "main")
  113. ? "main0"
  114. : vertex.EntryPoint;
  115. vertex.ShaderBytes = GetBytes(backend, compilationResult.VertexShader);
  116. fragment.EntryPoint = (backend == GraphicsBackend.Metal && fragment.EntryPoint == "main")
  117. ? "main0"
  118. : fragment.EntryPoint;
  119. fragment.ShaderBytes = GetBytes(backend, compilationResult.FragmentShader);
  120. }
  121. /// <summary>
  122. /// Creates a compute shader from the given <see cref="ShaderDescription"/> containing SPIR-V bytecode or GLSL source
  123. /// code.
  124. /// </summary>
  125. /// <param name="factory">The <see cref="ResourceFactory"/> used to compile the translated shader code.</param>
  126. /// <param name="computeShaderDescription">The compute shader's description.
  127. /// <see cref="ShaderDescription.ShaderBytes"/> should contain SPIR-V bytecode or Vulkan-style GLSL source code which
  128. /// can be compiled to SPIR-V.</param>
  129. /// <returns>The compiled compute <see cref="Shader"/>.</returns>
  130. public static Shader CreateFromSpirv(
  131. this ResourceFactory factory,
  132. ShaderDescription computeShaderDescription)
  133. {
  134. return CreateFromSpirv(factory, computeShaderDescription, new CrossCompileOptions());
  135. }
  136. /// <summary>
  137. /// Creates a compute shader from the given <see cref="ShaderDescription"/> containing SPIR-V bytecode or GLSL source
  138. /// code.
  139. /// </summary>
  140. /// <param name="factory">The <see cref="ResourceFactory"/> used to compile the translated shader code.</param>
  141. /// <param name="computeShaderDescription">The compute shader's description.
  142. /// <see cref="ShaderDescription.ShaderBytes"/> should contain SPIR-V bytecode or Vulkan-style GLSL source code which
  143. /// can be compiled to SPIR-V.</param>
  144. /// <param name="options">The <see cref="CrossCompileOptions"/> which will control the parameters used to translate the
  145. /// shaders from SPIR-V to the target language.</param>
  146. /// <returns>The compiled compute <see cref="Shader"/>.</returns>
  147. public static Shader CreateFromSpirv(
  148. this ResourceFactory factory,
  149. ShaderDescription computeShaderDescription,
  150. CrossCompileOptions options)
  151. {
  152. GraphicsBackend backend = factory.BackendType;
  153. if (backend == GraphicsBackend.Vulkan)
  154. {
  155. computeShaderDescription.ShaderBytes = EnsureSpirv(computeShaderDescription);
  156. return factory.CreateShader(ref computeShaderDescription);
  157. }
  158. CrossCompileTarget target = GetCompilationTarget(factory.BackendType);
  159. ComputeCompilationResult compilationResult = SpirvCompilation.CompileCompute(
  160. computeShaderDescription.ShaderBytes,
  161. target,
  162. options);
  163. string computeEntryPoint = (backend == GraphicsBackend.Metal && computeShaderDescription.EntryPoint == "main")
  164. ? "main0"
  165. : computeShaderDescription.EntryPoint;
  166. byte[] computeBytes = GetBytes(backend, compilationResult.ComputeShader);
  167. return factory.CreateShader(new ShaderDescription(
  168. computeShaderDescription.Stage,
  169. computeBytes,
  170. computeEntryPoint));
  171. }
  172. private static unsafe byte[] EnsureSpirv(ShaderDescription description)
  173. {
  174. if (Util.HasSpirvHeader(description.ShaderBytes))
  175. {
  176. return description.ShaderBytes;
  177. }
  178. else
  179. {
  180. fixed (byte* sourceAsciiPtr = description.ShaderBytes)
  181. {
  182. SpirvCompilationResult glslCompileResult = SpirvCompilation.CompileGlslToSpirv(
  183. (uint)description.ShaderBytes.Length,
  184. sourceAsciiPtr,
  185. null,
  186. description.Stage,
  187. description.Debug,
  188. 0,
  189. null);
  190. return glslCompileResult.SpirvBytes;
  191. }
  192. }
  193. }
  194. private static byte[] GetBytes(GraphicsBackend backend, string code)
  195. {
  196. switch (backend)
  197. {
  198. case GraphicsBackend.Direct3D11:
  199. case GraphicsBackend.OpenGL:
  200. case GraphicsBackend.OpenGLES:
  201. return Encoding.ASCII.GetBytes(code);
  202. case GraphicsBackend.Metal:
  203. return Encoding.UTF8.GetBytes(code);
  204. default:
  205. throw new SpirvCompilationException($"Invalid GraphicsBackend: {backend}");
  206. }
  207. }
  208. private static CrossCompileTarget GetCompilationTarget(GraphicsBackend backend)
  209. {
  210. switch (backend)
  211. {
  212. case GraphicsBackend.Direct3D11:
  213. return CrossCompileTarget.HLSL;
  214. case GraphicsBackend.OpenGL:
  215. return CrossCompileTarget.GLSL;
  216. case GraphicsBackend.Metal:
  217. return CrossCompileTarget.MSL;
  218. case GraphicsBackend.OpenGLES:
  219. return CrossCompileTarget.ESSL;
  220. default:
  221. throw new SpirvCompilationException($"Invalid GraphicsBackend: {backend}");
  222. }
  223. }
  224. }
  225. }