C++ vkCreatePipeline失败和验证层段错误
我想画些东西给HTC和Vulkan的生活 我启用了验证层,程序在C++ vkCreatePipeline失败和验证层段错误,c++,vulkan,C++,Vulkan,我想画些东西给HTC和Vulkan的生活 我启用了验证层,程序在vkCreateGraphicsPipeline中给了我段错误。段错误发生在VkLayer\u core\u validation.dll中。如果这还不够奇怪,那么发生段故障的函数是vkemurateinnstanceextensions。因此,我在没有验证层的情况下进行了测试,然后vkCreateGraphicsPipeline失败,结果是VK\u错误\u验证\u失败\u EXT 现在,我已经阅读了教程中的管道部分,我已经遵循了好
vkCreateGraphicsPipeline
中给了我段错误。段错误发生在VkLayer\u core\u validation.dll
中。如果这还不够奇怪,那么发生段故障的函数是vkemurateinnstanceextensions
。因此,我在没有验证层的情况下进行了测试,然后vkCreateGraphicsPipeline
失败,结果是VK\u错误\u验证\u失败\u EXT
现在,我已经阅读了教程中的管道部分,我已经遵循了好几次,我没有发现任何错误。我还尝试了Vulkan SDK的旧版本,但唯一的区别是该段错误发生在vkCreateGraphicsPipeline
内部的vkGetInstanceProcAddr
中
static int loadShader(VrDevice *device,VkShaderModule *module,char *filename){
// load the shader
.
.
.
// Create the VkShaderModule
VkShaderModuleCreateInfo shadermodule;
shadermodule.sType=VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
shadermodule.flags=0;
shadermodule.pNext=NULL;
shadermodule.codeSize=buffersize;
shadermodule.pCode=(const uint32_t *)buffer;
VkResult result=vkCreateShaderModule(device->logicaldevice,&shadermodule,NULL,module);
// Check for vulkan error and free allocated memory before exiting
delete[] buffer;
if(result==VK_SUCCESS) return 1;
else return 0;
}
int renderingInit(VrDevice *device,char *appname){
.
.
.
VkApplicationInfo appinfo;
appinfo.sType=VK_STRUCTURE_TYPE_APPLICATION_INFO;
appinfo.pNext=NULL;
appinfo.apiVersion=VK_MAKE_VERSION(1,0,0);
appinfo.pApplicationName=appname;
appinfo.applicationVersion=1;
appinfo.pEngineName=appname;
appinfo.engineVersion=1;
VkInstanceCreateInfo instanceinfo;
instanceinfo.sType=VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceinfo.pNext=NULL;
instanceinfo.flags=0;
instanceinfo.pApplicationInfo=&appinfo;
instanceinfo.enabledExtensionCount=numextension;
instanceinfo.ppEnabledExtensionNames=extensions;
instanceinfo.enabledLayerCount=layercount;
instanceinfo.ppEnabledLayerNames=debuglayers;
if(vkCreateInstance(&instanceinfo,NULL,&device->instance)!=VK_SUCCESS) return 0;
.
.
.
// Graphics card related matters
uint32_t devicecount=1;
result=vkEnumeratePhysicalDevices(device->instance,&devicecount,&device->physicaldevice);
if(result==VK_SUCCESS || result==VK_INCOMPLETE){
vkGetPhysicalDeviceProperties(device->physicaldevice,&device->physicaldeviceprop);
vkGetPhysicalDeviceMemoryProperties(device->physicaldevice,&device->physicaldevicememprop);
vkGetPhysicalDeviceFeatures(device->physicaldevice,&device->physicaldevicefeatures);
}
else return 0
uint32_t queuecount;
vkGetPhysicalDeviceQueueFamilyProperties(device->physicaldevice,&queuecount,NULL);
if(queuecount>0){
VkQueueFamilyProperties *queues=new VkQueueFamilyProperties[queuecount];
vkGetPhysicalDeviceQueueFamilyProperties(device->physicaldevice,&queuecount,queues);
uint32_t queue;
for(queue=0;queue<queuecount;queue++){
if(queues[queue].queueFlags&VK_QUEUE_GRAPHICS_BIT) break;
}
delete[] queues;
device->queuefamily=queue;
}
else return 0;
.
.
.
// Make logical device
VkDeviceQueueCreateInfo queueinfo;
queueinfo.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueinfo.pNext=NULL;
queueinfo.flags=0;
queueinfo.queueCount=1;
queueinfo.queueFamilyIndex=device->queuefamily;
float priority=1.0f;
queueinfo.pQueuePriorities=&priority;
VkDeviceCreateInfo createinfo;
createinfo.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createinfo.pNext=NULL;
createinfo.flags=0;
createinfo.pQueueCreateInfos=&queueinfo;
createinfo.queueCreateInfoCount=1;
createinfo.ppEnabledExtensionNames=extensions;
createinfo.enabledExtensionCount=numextensions;
createinfo.ppEnabledLayerNames=NULL;
createinfo.enabledLayerCount=0;
createinfo.pEnabledFeatures=&device->physicaldevicefeatures;
if(vkCreateDevice(device->physicaldevice,&createinfo,NULL,&device->logicaldevice)!=VK_SUCCESS) return 0;
vkGetDeviceQueue(device->logicaldevice,device->queuefamily,0,&device->queue);
// Create the frame image for the Vive
.
.
.
// Create renderpass
VkAttachmentDescription colorattachment;
colorattachment.format = VK_FORMAT_R8G8B8A8_SRGB;
colorattachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorattachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorattachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorattachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorattachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorattachment.initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
colorattachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference attachreferences;
attachreferences.attachment=0;
attachreferences.layout=VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass;
subpass.flags=0;
subpass.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.inputAttachmentCount=0;
subpass.pInputAttachments=NULL;
subpass.colorAttachmentCount=1;
subpass.pColorAttachments=&attachreferences;
subpass.pResolveAttachments=0;
subpass.pDepthStencilAttachment=0;
subpass.preserveAttachmentCount=0;
subpass.pPreserveAttachments=0;
VkRenderPassCreateInfo renderpassinfo;
renderpassinfo.sType=VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderpassinfo.pNext=NULL;
renderpassinfo.flags=0;
renderpassinfo.attachmentCount=1;
renderpassinfo.pAttachments=&colorattachment;
renderpassinfo.subpassCount=1;
renderpassinfo.pSubpasses=&subpass;
renderpassinfo.dependencyCount=0;
renderpassinfo.pDependencies=NULL;
if(vkCreateRenderPass(device->logicaldevice,&renderpassinfo,NULL,&device->renderpass)!=VK_SUCCESS) return 0;
//** Load shaders and handle pipeline creation **//
// Pipeline layout
VkPipelineLayoutCreateInfo createinfo;
createinfo.sType=VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
createinfo.pNext=NULL;
createinfo.flags=0;
createinfo.setLayoutCount=0;
createinfo.pSetLayouts=NULL;
createinfo.pushConstantRangeCount=0;
createinfo.pPushConstantRanges=NULL;
if(vkCreatePipelineLayout(device->logicaldevice,&createinfo,NULL,&device->pipelinelayout)!=VK_SUCCESS) return 0;
// Shader modules.
VkPipelineShaderStageCreateInfo shaderstages[2];
if(loadShader(device,&shaderstages[0].module,VERTEX_SHADER_NAME)==0 && loadShader(device,&shaderstages[1].module,FRAGMENT_SHADER_NAME)==0) return 0;
shaderstages[0].sType=VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderstages[0].pNext=NULL;
shaderstages[0].flags=0;
shaderstages[0].stage=VK_SHADER_STAGE_VERTEX_BIT;
shaderstages[0].pSpecializationInfo=NULL;
shaderstages[0].pName="main";
shaderstages[1].sType=VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderstages[1].pNext=NULL;
shaderstages[1].flags=0;
shaderstages[1].stage=VK_SHADER_STAGE_FRAGMENT_BIT;
shaderstages[1].pSpecializationInfo=NULL;
shaderstages[1].pName="main";
// Descripte the vertex input to pipeline.
VkPipelineVertexInputStateCreateInfo vertexinfo;
vertexinfo.sType=VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexinfo.pNext=NULL;
vertexinfo.flags=0;
vertexinfo.pVertexAttributeDescriptions=NULL;
vertexinfo.vertexAttributeDescriptionCount=0;
vertexinfo.pVertexBindingDescriptions=NULL;
vertexinfo.vertexBindingDescriptionCount=0;
VkPipelineInputAssemblyStateCreateInfo inputassembly;
inputassembly.sType=VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputassembly.pNext=NULL;
inputassembly.flags=0;
inputassembly.topology=VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputassembly.primitiveRestartEnable=VK_FALSE;
// Viewport decide what reqion of framebuffer is used.
VkViewport viewport = {0.0f,0.0f,(float)device->renderwidth,(float)device->renderheight,0.0f,1.0f};
// Scissors decide how much pippeline covers the window (how much info goes to rasterizing).
VkRect2D scissor = {0,0,device->renderwidth,device->renderheight};
VkPipelineViewportStateCreateInfo viewportstate;
viewportstate.sType=VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportstate.pNext=NULL;
viewportstate.flags=0;
viewportstate.pScissors=&scissor;
viewportstate.scissorCount=1;
viewportstate.pViewports=&viewport;
viewportstate.viewportCount=1;
// Rasterization infomration
VkPipelineRasterizationStateCreateInfo rasterization;
rasterization.sType=VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterization.pNext=NULL;
rasterization.flags=0;
rasterization.depthClampEnable=VK_FALSE;
rasterization.rasterizerDiscardEnable=VK_FALSE;
rasterization.polygonMode=VK_POLYGON_MODE_FILL;
rasterization.cullMode=VK_CULL_MODE_BACK_BIT;
rasterization.frontFace=VK_FRONT_FACE_CLOCKWISE;
rasterization.depthBiasEnable=VK_FALSE;
rasterization.depthBiasConstantFactor=0.0f;
rasterization.depthBiasClamp=0.0f;
rasterization.lineWidth=1.0f;
// Multisampling
VkPipelineMultisampleStateCreateInfo multisampling;
multisampling.sType=VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.pNext=NULL;
multisampling.flags=0;
multisampling.rasterizationSamples=VK_SAMPLE_COUNT_1_BIT;
multisampling.sampleShadingEnable=VK_FALSE;
multisampling.minSampleShading=0.0f;
multisampling.pSampleMask=NULL;
multisampling.alphaToCoverageEnable=VK_FALSE;
multisampling.alphaToOneEnable=VK_FALSE;
// Color blending
VkPipelineColorBlendAttachmentState colorblendattachment;
colorblendattachment.colorWriteMask=VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
colorblendattachment.blendEnable=VK_FALSE;
colorblendattachment.srcAlphaBlendFactor=VK_BLEND_FACTOR_ONE;
colorblendattachment.dstAlphaBlendFactor=VK_BLEND_FACTOR_ZERO;
colorblendattachment.colorBlendOp=VK_BLEND_OP_ADD;
colorblendattachment.srcColorBlendFactor=VK_BLEND_FACTOR_ONE;
colorblendattachment.dstColorBlendFactor=VK_BLEND_FACTOR_ZERO;
colorblendattachment.alphaBlendOp=VK_BLEND_OP_ADD;
VkPipelineColorBlendStateCreateInfo colorblend;
colorblend.sType=VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorblend.pNext=NULL;
colorblend.flags=0;
colorblend.logicOpEnable=VK_FALSE;
colorblend.logicOp=VK_LOGIC_OP_COPY;
colorblend.attachmentCount=1;
colorblend.pAttachments=&colorblendattachment;
colorblend.blendConstants[0]=0;
colorblend.blendConstants[1]=0;
colorblend.blendConstants[2]=0;
colorblend.blendConstants[3]=0;
// If tuo want to change viewport, line width, blend constants you have to change it in this data type.
VkPipelineDynamicStateCreateInfo dynamicstateinfo;
dynamicstateinfo.sType=VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dynamicstateinfo.pNext=NULL;
dynamicstateinfo.flags=0;
dynamicstateinfo.dynamicStateCount=0;
dynamicstateinfo.pDynamicStates=NULL;
VkGraphicsPipelineCreateInfo pipelineinfo;
pipelineinfo.sType=VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineinfo.pNext=NULL;
pipelineinfo.flags=0;
pipelineinfo.stageCount=2;
pipelineinfo.pStages=shaderstages;
pipelineinfo.pVertexInputState=&vertexinfo;
pipelineinfo.pInputAssemblyState=&inputassembly;
pipelineinfo.pViewportState=&viewportstate;
pipelineinfo.pRasterizationState=&rasterization;
pipelineinfo.pMultisampleState=&multisampling;
pipelineinfo.pDepthStencilState=NULL;
pipelineinfo.pColorBlendState=&colorblend;
pipelineinfo.pDynamicState=&dynamicstateinfo;
pipelineinfo.layout=device->pipelinelayout;
pipelineinfo.renderPass=device->renderpass;
pipelineinfo.subpass=0;
pipelineinfo.basePipelineHandle=VK_NULL_HANDLE;
pipelineinfo.basePipelineIndex=0;
// ** TODO: SEGMENT FAULT WHILE DEBUG LAYER IS ON! ** //
if((result=vkCreateGraphicsPipelines(device->logicaldevice,VK_NULL_HANDLE,1,&pipelineinfo,NULL,&device->pipeline))!=VK_SUCCESS) return 0;
// Destroy shaders after pipeline creation
vkDestroyShaderModule(device->logicaldevice,shaderstages[0].module,NULL);
vkDestroyShaderModule(device->logicaldevice,shaderstages[1].module,NULL);
}
static int loadShader(VrDevice*device,VkShaderModule*module,char*filename){
//加载着色器
.
.
.
//创建VkShaderModule
VkShaderModuleCreateInfo着色器模块;
shadermodule.sType=VK\u结构\u类型\u着色器\u模块\u创建\u信息;
shadermodule.flags=0;
shadermodule.pNext=NULL;
shadermodule.codeSize=缓冲区大小;
pCode=(const uint32_t*)缓冲区;
VkResult=vkCreateShaderModule(设备->逻辑设备和shadermodule,NULL,模块);
//退出前检查vulkan错误并释放分配的内存
删除[]缓冲区;
if(result==VK_SUCCESS)返回1;
否则返回0;
}
int renderingInit(VrDevice*device,char*appname){
.
.
.
VkApplicationInfo应用程序信息;
appinfo.sType=VK_结构_类型_应用_信息;
appinfo.pNext=NULL;
APPIversion=VK_MAKE_VERSION(1,0,0);
appinfo.pApplicationName=appname;
appinfo.applicationVersion=1;
appinfo.pEngineName=appname;
appinfo.engineVersion=1;
VkInstanceCreateInfo实例信息;
instanceinfo.sType=VK\u结构\u类型\u实例\u创建\u信息;
instanceinfo.pNext=NULL;
instanceinfo.flags=0;
instanceinfo.pApplicationInfo=&appinfo;
instanceinfo.enabledExtensionCount=numextension;
instanceinfo.ppEnabledExtensionNames=扩展名;
instanceinfo.enabledLayerCount=layercount;
instanceinfo.ppEnabledLayerNames=调试层;
if(vkCreateInstance(&instanceinfo,NULL,&device->instance)!=VK_SUCCESS)返回0;
.
.
.
//图形卡相关事宜
uint32_t设备计数=1;
结果=vkEnumeratePhysicalDevices(设备->实例,&devicecount,&device->物理设备);
如果(结果==VK_成功| |结果==VK_不完整){
VGGetPhysicalDeviceProperties(设备->物理设备,&device->physicaldeviceprop);
VGGetPhysicalDeviceMemoryProperties(设备->物理设备,&设备->物理设备管理程序);
VGGetPhysicalDeviceFeatures(设备->物理设备,&设备->物理设备功能);
}
否则返回0
uint32_t队列计数;
VGGetPhysicalDeviceQueueFamilyProperties(设备->物理设备,&queuecount,NULL);
如果(队列计数>0){
VkQueueFamilyProperties*queues=新的VkQueueFamilyProperties[queuecount];
VGGetPhysicalDeviceQueueFamilyProperties(设备->物理设备和队列计数,队列);
uint32_t队列;
对于(队列=0;队列族=队列;
}
否则返回0;
.
.
.
//制作逻辑设备
VkDeviceQueueCreateInfo queueinfo;
queueinfo.sType=VK\u结构\u类型\u设备\u队列\u创建\u信息;
queueinfo.pNext=NULL;
queueinfo.flags=0;
queueinfo.queueCount=1;
queueinfo.queueFamilyIndex=device->queuefamily;
浮动优先级=1.0f;
queueinfo.pQueuePriorities=&priority;
VkDeviceCreateInfo createinfo;
createinfo.sType=VK\u结构\u类型\u设备\u创建\u信息;
createinfo.pNext=NULL;
createinfo.flags=0;
createinfo.pQueueCreateInfos=&queueinfo;
createinfo.queueCreateInfoCount=1;
createinfo.ppEnabledExtensionNames=扩展名;
createinfo.enabledExtensionCount=numextensions;
createinfo.ppEnabledLayerNames=NULL;
createinfo.enabledLayerCount=0;
createinfo.pEnabledFeatures=&device->physicaldevicefeatures;
如果(vkCreateDevice(device->physicaldevice,&createinfo,NULL,&device->logicaldevice)!=VK_SUCCESS)返回0;
VGGetDeviceQueue(设备->逻辑设备,设备->队列系列,0,&设备->队列);
//为Vive创建帧图像
.
.
.
//创建渲染过程
VkAttachmentDescription颜色附件;
colorattachment.format=VK_格式_R8G8B8A8_SRGB;
colorattachment.samples=VK_SAMPLE_COUNT_1_位;
colorattachment.loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
colorattachment.storeOp=VK_ATTACHMENT_STORE_OP_STORE;
colorattachment.stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorattachment.stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorattachment.initialLayout=VK_图像_布局_颜色_附件_最佳;
colorattachment.finalLayout=VK_图像_布局_颜色_附件_优化;
VkatAttachmentReference附件参考;
attachreferences.attachment=0;
attachreferences.layout=VK\u IMAGE\u layout\u COLOR\u ATTACHMENT\u OPTIMAL;
VkSubpassDescription子类;
子类标志=0;
子类pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS;
子类inputAttachmentCount=0;
子类pInputAttachments=NULL;
子类colorAttachmentCount=1;
子类pColorAttachments=&attachreferences;
子类预溶附件=0;
子类pDepthStencilAttachment=0;
子类preserveAttachmentCount=0;
子类pPreserveAttachments=0;
VkRenderPassCreateInfo renderpassinfo;
strenderpassinfo.sType=VK\u结构\u类型\u渲染\u过程\u创建\u信息;
renderpassinfo.pNext=NULL;
renderpassinfo.flags=0;
renderpassinfo.attachmentCount=1;
renderpassinfo.pAttachments=&colorattachment;
renderpassinfo.subpassCount=1;
renderpassinfo.psubPass=&subpass;
renderpassinfo.dependencyCount=0