【问题标题】:SPIR-V module not valid: Codesize must be a multiple of 4 but is 191SPIR-V 模块无效:代码大小必须是 4 的倍数,但为 191
【发布时间】:2019-11-13 05:36:04
【问题描述】:

我在尝试将我的 glsl 着色器编译为运行时 spirv 时遇到此错误。在这一点上,我完全被难住了,我不知道是什么导致了错误,也无法在网上找到任何东西。

该错误仅发生在我的顶点着色器中,我的片段着色器编译没有问题(尽管它们都“编译”得很好,但在创建 vulkan 模块而不使用 shaderc 编译时会发生错误)。此外,当我从命令行编译并读取那些已编译的文件时,着色器模块的创建没有问题。

这是我的顶点着色器:

# version 450
# extension GL_ARB_separate_shader_objects : enable

layout(location = 0) in vec2 aPos;
layout(location = 1) in vec3 aColor;

layout(location = 0) out vec3 fragColor; 

void main()
{
    gl_Position = vec4(aPos, 1.0, 1.0);
    fragColor = aColor;
}

这是我用来读取和编译 .vert 文件的代码

std::vector<char> readFile(const std::string& filename)
{
    std::ifstream file(filename, std::ios::ate | std::ios::binary);

    if (!file.is_open())
    {
        throw std::runtime_error("failed to open file!");
    }

    size_t fileSize = (size_t) file.tellg();
    std::vector<char> buffer(fileSize);

    file.seekg(0);
    file.read(buffer.data(), fileSize);

    file.close();

    return buffer; 
}

std::vector<uint32_t> compileToSPIRV(const char* sPath, const shaderc_shader_kind kind)
{
    auto shaderVect = readFile(sPath);
    std::string shaderText(shaderVect.begin(), shaderVect.end());

    shaderc::Compiler compiler;
    shaderc::CompileOptions options;

    options.AddMacroDefinition("MY_DEFINE", "1");
    options.SetOptimizationLevel(shaderc_optimization_level_size);

    auto assembly = compiler.CompileGlslToSpvAssembly(shaderText, kind, sPath, options);
    std::string ass(assembly.cbegin(), assembly.cend());
    auto compile = compiler.AssembleToSpv(ass.c_str(), ass.size());
    std::vector<uint32_t> comp(compile.cbegin(), compile.cend());
    return comp;
}

感谢您的帮助。如果您希望我包含任何其他代码,请告诉我。

编辑:创建着色器模块:

VkShaderModule createShaderModule(const std::vector<uint32_t>& code)
    {
        VkShaderModuleCreateInfo moduleInfo = {};
        moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
        moduleInfo.codeSize = code.size();
        moduleInfo.pCode = code.data();

        VkShaderModule shaderModule;
        if (vkCreateShaderModule(device, &moduleInfo, nullptr, &shaderModule) != VK_SUCCESS)
        {
            throw std::runtime_error("Failed to create shader module");
        }

        return shaderModule;
    }

【问题讨论】:

  • 我没有看到您向 Vulkan 提供数据的部分。不过我怀疑它的样子。
  • 我将其编辑到问题中

标签: c++ vulkan vertex-shader


【解决方案1】:

VkShaderModuleCreateInfo::codeSize 以字节为单位,而不是uint32_ts。所以应该是4*code.size()

【讨论】:

  • 嗨,我正在尝试使用 CompileGlslToSpvAssembly 编译 Vulkan 着色器,第一个参数是“shader_source”,然后第三个参数是“input_file_name”。如果我们将着色器源作为第一个参数传递,那么“input_file_name”是什么?
  • @Zebrafish 似乎您不小心做了一个随机答案评论而不是一个新问题。
猜你喜欢
  • 1970-01-01
  • 2016-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 2012-02-28
  • 2019-05-15
  • 2011-12-02
相关资源
最近更新 更多