在使用pipelineInfo之前,我需要填充一个对象。要填充对象,我使用一个名为createPipelineInfo.的函数当我使用visual studio编译调试构建时,这非常好,但是当我试图编译发行版构建时,编译器会对整个createPipelineInfo函数进行“优化”。
下面是初始化对象及其使用的调用:
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函数:
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;
}另一方面,如果我复制函数的主体并将其转储以代替函数调用,那么一切都很好。
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!");
}我试图弄清楚为什么编译器正在优化函数调用,而试图开发一个不涉及转储函数主体而不是每个函数调用的工作失败了。
发布于 2021-05-09 21:26:14
猜测:此参数通过副本传递。
const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages因此,当使用data方法调用获取其内容的地址时:
pipelineInfo.pStages = shaderStages.data();你会调用未定义的行为。由于调用的复杂性,编译器不够聪明,不能警告您对临时引用进行引用;2)编译器不会在参数传递时自动执行复制省略。
Fix:通过引用传递它(注意所有其他参数都出于某种原因使用by引用模式)
const std::array<VkPipelineShaderStageCreateInfo, 2> &shaderStageshttps://stackoverflow.com/questions/67462396
复制相似问题