【问题标题】:Compiling GLSL to corresponding Spir V for use in a Vulkan Application将 GLSL 编译为相应的 Spir V 以在 Vulkan 应用程序中使用
【发布时间】:2019-09-19 15:23:17
【问题描述】:

我正在使用来自 github 的 Jonno Robson 的 Vulkan 代码库:Vulkan-Terrain-Generator 作为指南和/或学习参考,以更好地理解 Vulkan。我对源代码本身没有任何问题,但我的问题或担忧与将 GLSL 着色器代码编译成 Spir V 代码有关。我是 SpirV 的编译器和工具包的新手。我尝试使用以下两种方法:glslangValidator.exe 和 glslc.exe 将着色器文件转换为 Spir V 文件。

在 Jonno 的代码库中,他将每个 GLSL 着色器文件转换为相应的 spirv 文件。我尝试使用他在批处理文件中使用的标志选项,唯一的区别是我用我自己的目录替换了指向他的 glslangValidator.exe 的目录。

我正在尝试实现相同的效果,它将批处理文件目录中的所有着色器文件从 GLSL 编译到 Spir V,它将在每个文件的末尾附加 .spv在将其从 GLSL 转换为受尊重的 Spir V 字节码后,它将在该目录中生成的新 SpirV 文件。

这是我的批处理文件的样子:

compile.bat

C:\VulkanSDK\Bin\glslangValidator.exe -V %1 -o %1.spv
pause

但是在我双击批处理文件后它对我不起作用。它打开并运行,但没有生成预期的 shader_filename.vert.spv ... shader_filename.frag.spv 文件。

我不知道他们在什么平台上运行,但我运行的是 Windows 7,我不知道这是否会影响批处理命令中提供的命令参数或标志。我不知道他们是否使用了 Vulkan SDK 中的任何其他工具包或一些外部库或工具,或者没有。

我希望能够使用这个批处理文件做的是使用最简单的批处理命令将所有着色器文件转换为适当的 Spir V 文件。我不想为每个着色器文件一遍又一遍地编写相同的命令,因为这个目录中有超过 20 个着色器。

如何实现这一点,或者 glslangValidator 或 glslc 生成所需 SpirV 文件的正确命令参数是什么?

我已阅读此处找到的文档:SPIR-V Toolchain,但我仍然不确定如何正确生成所需的批处理文件。

【问题讨论】:

  • @GerhardBarnard 最后一个命令是带引号还是不带引号?我只是用引号试了一下,仍然没有生成文件...
  • @GerhardBarnard C:\Users\skilz99\source\repos\Vulkan Terrain Generator\res\shaders>pause Press any key to continue . . . 与着色器文件所在的目录相同。
  • @GerhardBarnard 我想知道是否因为projectsolution 目录的名称中有一个空格会产生影响......我认为它不应该,但它可以。 ..
  • @GerhardBarnard 我熟悉 C/C++ 中的 OpenGL 和 GLSL,但我正在尝试自学 Vulkan,它需要 Spir-V ...所以现在我想弄清楚了解如何使用工具链从 GLSL 或 HLSL 转换为 Spir-V...可能!
  • 这里只是清理了一些cmets。

标签: batch-file command command-line-arguments vulkan spir-v


【解决方案1】:

您需要确保为每个文件提供输入名称,该示例纯粹使用 %1,然后从命令行作为参数发出,例如:

mybatch.bat inputfile.frag

如果您打算只双击它,我们需要更改它。它将允许您遍历要执行此操作的每个文件:

@echo off
for %%i in (*.vert *.frag) do "C:\VulkanSDK\Bin\glslangValidator.exe" -V "%%~i" -o "%%~i.spv"

它所做的是获取每个.vert.frag 并将其分配给元变量%%i,然后我们只需为每个发出命令,直到我们遍历每个文件。

您可以在cmd.exefor /? 中阅读有关元变量以及如何扩展它们的更多信息

这是摘录。

您现在可以使用以下可选语法:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

修饰符可以组合得到复合结果:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

【讨论】:

  • 优秀的答案和解释。我现在可以在一行批处理代码中轻松地将 X 数量的 GLSLHLSL 着色器文件生成到它们对应的 Spir V 文件中,前提是我要将它们作为单独的着色器文件加载。这也可作为 Windows cmd 批处理文件中基本循环的极好参考。感谢您花时间和精力提供完美的工作解决方案!
  • 在此之前;我能够编译和构建我的 Vulkan APP,但在所有必要的着色器文件可用之前无法调试它。现在我可以开始调试应用程序,寻找任何 C++ - Vulkan 源代码错误!
猜你喜欢
  • 2020-12-01
  • 2022-11-09
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-08
  • 1970-01-01
相关资源
最近更新 更多