首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编译器“优化”Out对象初始化功能

编译器“优化”Out对象初始化功能
EN

Stack Overflow用户
提问于 2021-05-09 21:17:26
回答 1查看 88关注 0票数 0

在使用pipelineInfo之前,我需要填充一个对象。要填充对象,我使用一个名为createPipelineInfo.的函数当我使用visual studio编译调试构建时,这非常好,但是当我试图编译发行版构建时,编译器会对整个createPipelineInfo函数进行“优化”。

下面是初始化对象及其使用的调用:

代码语言:javascript
复制
VkGraphicsPipelineCreateInfo pipelineInfo = createPipelineInfo(shaderStages, vertexInputInfo, inputAssembly, viewportState, rasterizer,
    multisampling, colorBlending, pipelineLayout, renderPass);

if (vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
    throw std::runtime_error("failed to create graphics pipeline!");
}

以下是createPipelineInfo函数:

代码语言:javascript
复制
inline static VkGraphicsPipelineCreateInfo createPipelineInfo(
    const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages,
    const VkPipelineVertexInputStateCreateInfo& vertexInputInfo,
    const VkPipelineInputAssemblyStateCreateInfo& inputAssembly,
    const VkPipelineViewportStateCreateInfo& viewportState,
    const VkPipelineRasterizationStateCreateInfo& rasterizer,
    const VkPipelineMultisampleStateCreateInfo& multisampling,
    const VkPipelineColorBlendStateCreateInfo& colorBlending,
    const VkPipelineLayout& pipelineLayout,
    const VkRenderPass& renderPass) {

    VkGraphicsPipelineCreateInfo pipelineInfo{};

    //Shader Stage
    pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
    pipelineInfo.stageCount = 2;
    pipelineInfo.pStages = shaderStages.data();

    //Fixed Pipeline Stage
    pipelineInfo.pVertexInputState = &vertexInputInfo;
    pipelineInfo.pInputAssemblyState = &inputAssembly;
    pipelineInfo.pViewportState = &viewportState;
    pipelineInfo.pRasterizationState = &rasterizer;
    pipelineInfo.pMultisampleState = &multisampling;
    //pipelineInfo.pDepthStencilState = &depthStencil;
    pipelineInfo.pColorBlendState = &colorBlending;
    pipelineInfo.pDynamicState = nullptr; // Optional

    //Pipeline Layout
    pipelineInfo.layout = pipelineLayout;
    pipelineInfo.renderPass = renderPass;
    pipelineInfo.subpass = 0;
    pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;

    return pipelineInfo;
}

另一方面,如果我复制函数的主体并将其转储以代替函数调用,那么一切都很好。

代码语言:javascript
复制
VkGraphicsPipelineCreateInfo pipelineInfo{};

//Shader Stage
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages.data();

//Fixed Pipeline Stage
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
//pipelineInfo.pDepthStencilState = &depthStencil;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = nullptr; // Optional

//Pipeline Layout
pipelineInfo.layout = pipelineLayout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;


if (vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
    throw std::runtime_error("failed to create graphics pipeline!");
}

我试图弄清楚为什么编译器正在优化函数调用,而试图开发一个不涉及转储函数主体而不是每个函数调用的工作失败了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-09 21:26:14

猜测:此参数通过副本传递。

代码语言:javascript
复制
const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages

因此,当使用data方法调用获取其内容的地址时:

代码语言:javascript
复制
pipelineInfo.pStages = shaderStages.data();

你会调用未定义的行为。由于调用的复杂性,编译器不够聪明,不能警告您对临时引用进行引用;2)编译器不会在参数传递时自动执行复制省略。

Fix:通过引用传递它(注意所有其他参数都出于某种原因使用by引用模式)

代码语言:javascript
复制
const std::array<VkPipelineShaderStageCreateInfo, 2> &shaderStages
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67462396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档