c++ - SPIR-V 模块无效 : Codesize must be a multiple of 4 but is 191

标签 c++ vulkan vertex-shader

我一直在尝试在运行时将我的 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;
    }

最佳答案

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

关于c++ - SPIR-V 模块无效 : Codesize must be a multiple of 4 but is 191,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830897/

相关文章:

c++ - 类型检查模板类参数

c++ - Visual C++ parallel_for + vector 访问冲突

c++ - 从一个类到另一个类直接访问存储在 vector 中的数据

vulkan - 什么是 DirectX 12 相当于 Vulkan 的 "transient attachment"?

java - 关于顶点着色器中骨骼动画的困惑

opengl-es - GLSL集成功能

c++ - Boost 原子 128 位原子类型 x86_64

Vulkan:VK_PRESENT_MODE_MAILBOX_KHR 有两个图像相当于VK_PRESENT_MODE_FIFO_KHR?

c++ - 如何将多个结构加载到单个 UBO 中?

opengl - 我的 OpenGL Go 矩阵函数(Perspective/Frustum & Lookat 矩阵)有什么问题?