改进配合测试

This commit is contained in:
HuYingzhuo 2019-04-20 16:12:22 +08:00
parent ffb14f48d4
commit 52d153677d
7 changed files with 162 additions and 102 deletions

View File

@ -60,8 +60,11 @@ namespace
Device::Device(DeviceAttribute *da)
{
attr=da;
current_framebuffer=0;
current_frame=0;
image_acquired_semaphore=this->CreateSem();
draw_fence=this->CreateFence();
present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
present.pNext = nullptr;
@ -73,7 +76,11 @@ Device::Device(DeviceAttribute *da)
}
Device::~Device()
{
const uint32_t sc_count=attr->sc_image_views.GetCount();
delete image_acquired_semaphore;
delete draw_fence;
delete attr;
}
@ -142,15 +149,15 @@ RenderPass *Device::CreateRenderPass()
attachments[0].finalLayout=VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
attachments[0].flags=0;
attachments[1].format=attr->depth.format;
attachments[1].samples=VK_SAMPLE_COUNT_1_BIT;
attachments[1].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[1].storeOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[1].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
attachments[1].finalLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachments[1].flags=0;
//attachments[1].format=attr->depth.format;
//attachments[1].samples=VK_SAMPLE_COUNT_1_BIT;
//attachments[1].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
//attachments[1].storeOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
//attachments[1].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
//attachments[1].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
//attachments[1].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
//attachments[1].finalLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
//attachments[1].flags=0;
VkAttachmentReference color_reference={0,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkAttachmentReference depth_reference={1,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
@ -163,19 +170,27 @@ RenderPass *Device::CreateRenderPass()
subpass.colorAttachmentCount=1;
subpass.pColorAttachments=&color_reference;
subpass.pResolveAttachments=nullptr;
subpass.pDepthStencilAttachment=&depth_reference;
subpass.pDepthStencilAttachment=nullptr;//&depth_reference;
subpass.preserveAttachmentCount=0;
subpass.pPreserveAttachments=nullptr;
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo rp_info={};
rp_info.sType=VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
rp_info.pNext=nullptr;
rp_info.attachmentCount=2;
rp_info.attachmentCount=1;
rp_info.pAttachments=attachments;
rp_info.subpassCount=1;
rp_info.pSubpasses=&subpass;
rp_info.dependencyCount=0;
rp_info.pDependencies=nullptr;
rp_info.dependencyCount=1;
rp_info.pDependencies=&dependency;
VkRenderPass render_pass;
@ -192,12 +207,12 @@ Fence *Device::CreateFence()
fenceInfo.pNext = nullptr;
fenceInfo.flags = 0;
VkFence drawFence;
VkFence fence;
if(vkCreateFence(attr->device, &fenceInfo, nullptr, &drawFence)!=VK_SUCCESS)
if(vkCreateFence(attr->device, &fenceInfo, nullptr, &fence)!=VK_SUCCESS)
return(nullptr);
return(new Fence(attr->device,drawFence));
return(new Fence(attr->device,fence));
}
Semaphore *Device::CreateSem()
@ -216,48 +231,48 @@ Semaphore *Device::CreateSem()
bool Device::AcquireNextImage()
{
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*image_acquired_semaphore,VK_NULL_HANDLE,&current_framebuffer)==VK_SUCCESS);
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*image_acquired_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS);
}
bool Device::QueueSubmit(CommandBuffer *buf,Fence *fence)
bool Device::QueueSubmit(CommandBuffer *buf)
{
if(!buf||!fence)
if(!buf)
return(false);
VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkSubmitInfo submit_info[1] = {};
VkSubmitInfo submit_info = {};
VkSemaphore sem=*image_acquired_semaphore;
VkSemaphore wait_sem=*image_acquired_semaphore;
VkCommandBuffer cmd_bufs=*buf;
submit_info[0].pNext = nullptr;
submit_info[0].sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info[0].waitSemaphoreCount = 1;
submit_info[0].pWaitSemaphores = &sem;
submit_info[0].pWaitDstStageMask = &pipe_stage_flags;
submit_info[0].commandBufferCount = 1;
submit_info[0].pCommandBuffers = &cmd_bufs;
submit_info[0].signalSemaphoreCount = 0;
submit_info[0].pSignalSemaphores = nullptr;
submit_info.pNext = nullptr;
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info.waitSemaphoreCount = 1;
submit_info.pWaitSemaphores = &wait_sem;
submit_info.pWaitDstStageMask = &pipe_stage_flags;
submit_info.commandBufferCount = 1;
submit_info.pCommandBuffers = &cmd_bufs;
submit_info.signalSemaphoreCount = 0;
submit_info.pSignalSemaphores = nullptr;
return(vkQueueSubmit(attr->graphics_queue, 1, submit_info, *fence)==VK_SUCCESS);
return(vkQueueSubmit(attr->graphics_queue, 1, &submit_info, *draw_fence)==VK_SUCCESS);
}
bool Device::Wait(Fence *f,bool wait_all,uint64_t time_out)
bool Device::Wait(bool wait_all,uint64_t time_out)
{
VkResult res;
VkFence fence=*f;
VkFence fence=*draw_fence;
do {
res = vkWaitForFences(attr->device, 1, &fence, wait_all, time_out);
} while (res == VK_TIMEOUT);
vkWaitForFences(attr->device, 1, &fence, wait_all, time_out);
vkResetFences(attr->device,1,&fence);
return(true);
}
bool Device::QueuePresent()
{
present.pImageIndices = &current_framebuffer;
present.pImageIndices = &current_frame;
present.waitSemaphoreCount = 0;
present.pWaitSemaphores = nullptr;
return(vkQueuePresentKHR(attr->present_queue, &present)==VK_SUCCESS);
}

View File

@ -15,12 +15,16 @@ class RenderPass;
class Fence;
class Semaphore;
#define MAX_FRAMES_IN_FLIGHT 2
class Device
{
DeviceAttribute *attr;
Semaphore *image_acquired_semaphore;
uint32_t current_framebuffer;
Fence *draw_fence;
uint32_t current_frame;
VkPresentInfoKHR present;
@ -44,9 +48,12 @@ public:
public:
VkImageView GetColorImageView (int index=-1) {return GetObject(attr->sc_image_views,index==-1?current_framebuffer:index);}
const uint32_t GetSwapChainImageCount ()const {return attr->sc_image_views.GetCount();}
VkImageView GetColorImageView (int index) {return GetObject(attr->sc_image_views,index);}
VkImageView GetDepthImageView () {return attr->depth.view;}
const uint32_t GetCurrentFrameIndices () {return current_frame;}
public:
Buffer * CreateBuffer(VkBufferUsageFlags buf_usage,VkDeviceSize size,VkSharingMode sharing_mode=VK_SHARING_MODE_EXCLUSIVE);
@ -71,8 +78,8 @@ public:
Semaphore * CreateSem();
bool AcquireNextImage ();
bool QueueSubmit (CommandBuffer *,Fence *);
bool Wait (Fence *,bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1);
bool QueueSubmit (CommandBuffer *);
bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1);
bool QueuePresent ();
};//class Device
VK_NAMESPACE_END

View File

@ -1,6 +1,7 @@
#include"VKDevice.h"
#include"VKInstance.h"
#include"VKPhysicalDevice.h"
#include"VKFramebuffer.h"
VK_NAMESPACE_BEGIN
VkSurfaceKHR CreateRenderDevice(VkInstance,Window *);
@ -109,11 +110,8 @@ namespace
swapchain_ci.clipped=true;
swapchain_ci.imageColorSpace=VK_COLORSPACE_SRGB_NONLINEAR_KHR;
swapchain_ci.imageUsage=VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE;
swapchain_ci.queueFamilyIndexCount=0;
swapchain_ci.pQueueFamilyIndices=nullptr;
uint32_t queueFamilyIndices[2]={(uint32_t)rsa->graphics_family, (uint32_t)rsa->present_family};
uint32_t queueFamilyIndices[2]={rsa->graphics_family, rsa->present_family};
if(rsa->graphics_family!=rsa->present_family)
{
// If the graphics and present queues are from different queue families,
@ -124,6 +122,10 @@ namespace
swapchain_ci.queueFamilyIndexCount=2;
swapchain_ci.pQueueFamilyIndices=queueFamilyIndices;
}
else
{
swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE;
}
VkSwapchainKHR swap_chain;

View File

@ -125,16 +125,19 @@ Instance *CreateInstance(const UTF8String &app_name)
const char *validation_layers[]=
{
// "VK_LAYER_LUNARG_api_dump",
// "VK_LAYER_LUNARG_assistant_layer",
"VK_LAYER_LUNARG_assistant_layer",
"VK_LAYER_LUNARG_core_validation",
// "VK_LAYER_LUNARG_device_simulation",
// "VK_LAYER_LUNARG_monitor",
"VK_LAYER_LUNARG_monitor",
"VK_LAYER_LUNARG_object_tracker",
"VK_LAYER_LUNARG_standard_validation",
"VK_LAYER_LUNARG_parameter_validation",
// "VK_LAYER_LUNARG_vktrace",
// "VK_LAYER_RENDERDOC_Capture",
// "VK_LAYER_KHRONOS_validation",
"VK_LAYER_KHRONOS_validation",
// "VK_LAYER_NV_nsight-sys",
"VK_LAYER_GOOGLE_unique_objects",
"VK_LAYER_GOOGLE_threading"

View File

@ -19,6 +19,20 @@ PipelineCreater::PipelineCreater(Device *dev)
hgl_zero(pipelineInfo);
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
{
vis_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vis_create_info.pNext = nullptr;
vis_create_info.flags = 0;
vis_create_info.vertexBindingDescriptionCount = 0;
vis_create_info.pVertexBindingDescriptions = nullptr;
vis_create_info.vertexAttributeDescriptionCount = 0;
vis_create_info.pVertexAttributeDescriptions = nullptr;
pipelineInfo.pVertexInputState=&vis_create_info;
}
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = extent.width;
@ -42,8 +56,8 @@ PipelineCreater::PipelineCreater(Device *dev)
depthStencilState.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthStencilState.pNext = nullptr;
depthStencilState.flags = 0;
depthStencilState.depthTestEnable = VK_TRUE;
depthStencilState.depthWriteEnable = VK_TRUE;
depthStencilState.depthTestEnable = VK_FALSE;
depthStencilState.depthWriteEnable = VK_FALSE;
depthStencilState.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
depthStencilState.depthBoundsTestEnable = VK_FALSE;
depthStencilState.minDepthBounds = 0;
@ -66,8 +80,8 @@ PipelineCreater::PipelineCreater(Device *dev)
rasterizer.depthClampEnable = VK_FALSE;
rasterizer.rasterizerDiscardEnable = VK_FALSE;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
rasterizer.cullMode = VK_CULL_MODE_FRONT_AND_BACK;
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
rasterizer.depthBiasEnable = VK_FALSE;
rasterizer.depthBiasConstantFactor = 0;
rasterizer.depthBiasClamp = 0;
@ -117,6 +131,8 @@ PipelineCreater::PipelineCreater(Device *dev)
dynamicState.flags = 0;
dynamicState.pDynamicStates = dynamicStateEnables;
dynamicState.dynamicStateCount = 0;
dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_VIEWPORT;
dynamicStateEnables[dynamicState.dynamicStateCount++] = VK_DYNAMIC_STATE_SCISSOR;
pipelineInfo.pDynamicState=&dynamicState;
@ -147,7 +163,6 @@ bool PipelineCreater::Set(const VertexInput *vi)
vis_create_info=vertex_input->GetPipelineVertexInputStateCreateInfo();
pipelineInfo.pVertexInputState=&vis_create_info;
return(true);
}
@ -157,6 +172,8 @@ bool PipelineCreater::Set(const VkPrimitiveTopology topology,bool restart)
if(topology!=PRIM_RECTANGLE)return(false);
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.pNext = nullptr;
inputAssembly.flags = 0;
inputAssembly.topology = (topology==PRIM_RECTANGLE?VK_PRIMITIVE_TOPOLOGY_POINT_LIST:topology);
inputAssembly.primitiveRestartEnable = restart;

View File

@ -20,6 +20,8 @@ namespace hgl
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_PAINT:
return 0;
default:
break;
}

View File

@ -70,40 +70,43 @@ vulkan::Shader *LoadShader(VkDevice device)
return(nullptr);
}
constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f };
constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 };
vulkan::VertexInput *CreateVertexBuffer(vulkan::Device *dev)
{
vulkan::VertexBuffer *vb=dev->CreateVBO(FMT_RG32F,6*sizeof(float));
vulkan::VertexBuffer *cb=dev->CreateVBO(FMT_RGB32F,9*sizeof(float));
{
float *p=(float *)vb->Map();
memcpy(p,vertex_data,6*sizeof(float));
vb->Unmap();
}
{
float *p=(float *)cb->Map();
memcpy(p,color_data,9*sizeof(float));
cb->Unmap();
}
vulkan::VertexInput *vi=new vulkan::VertexInput();
constexpr uint32_t position_shader_location=0; //对应shader中的layout(locaiton=0暂时这样写
constexpr uint32_t color_shader_location=1;
vi->Add(position_shader_location, vb);
vi->Add(color_shader_location, cb);
return vi;
}
//constexpr float vertex_data[]={0.0f,0.5f, -0.5f,-0.5f, 0.5f,-0.5f };
//constexpr float color_data[]={1,0,0, 0,1,0, 0,0,1 };
//
//vulkan::VertexBuffer *vertex_buffer=nullptr;
//vulkan::VertexBuffer *color_buffer=nullptr;
//
//vulkan::VertexInput *CreateVertexBuffer(vulkan::Device *dev)
//{
// vertex_buffer=dev->CreateVBO(FMT_RG32F,6*sizeof(float));
// color_buffer=dev->CreateVBO(FMT_RGB32F,9*sizeof(float));
//
// {
// float *p=(float *)vertex_buffer->Map();
//
// memcpy(p,vertex_data,6*sizeof(float));
//
// vertex_buffer->Unmap();
// }
//
// {
// float *p=(float *)color_buffer->Map();
//
// memcpy(p,color_data,9*sizeof(float));
//
// color_buffer->Unmap();
// }
//
// vulkan::VertexInput *vi=new vulkan::VertexInput();
//
// constexpr uint32_t position_shader_location=0; //对应shader中的layout(locaiton=0暂时这样写
// constexpr uint32_t color_shader_location=1;
//
// vi->Add(position_shader_location, vertex_buffer);
// vi->Add(color_shader_location, color_buffer);
//
// return vi;
//}
void wait_seconds(int seconds) {
#ifdef WIN32
@ -166,10 +169,9 @@ int main(int,char **)
if(!shader)
return -3;
vulkan::Fence *fence=device->CreateFence();
vulkan::Semaphore *sem=device->CreateSem();
vulkan::VertexInput *vi=CreateVertexBuffer(device);
// vulkan::VertexInput *vi=CreateVertexBuffer(device);
vulkan::PipelineCreater pc(device);
vulkan::RenderPass *rp=device->CreateRenderPass();
@ -179,7 +181,7 @@ int main(int,char **)
vulkan::PipelineLayout *pl=CreatePipelineLayout(device->GetDevice(),dsl);
pc.Set(shader);
pc.Set(vi);
//pc.Set(vi);
pc.Set(PRIM_TRIANGLES);
pc.Set(*pl);
pc.Set(*rp);
@ -189,32 +191,44 @@ int main(int,char **)
if(!pipeline)
return(-4);
device->AcquireNextImage(); //这样才会得到current_framebuffer的值下面的device->GetColorImageView()才会正确
device->AcquireNextImage();
vulkan::Framebuffer *fb=vulkan::CreateFramebuffer(device,rp,device->GetColorImageView(),device->GetDepthImageView());
const int image_count=device->GetSwapChainImageCount();
vulkan::Framebuffer **fb=new vulkan::Framebuffer *[image_count];
cmd_buf->Begin(rp,fb);
for(int i=0;i<image_count;i++)
fb[i]=vulkan::CreateFramebuffer(device,rp,device->GetColorImageView(i));
cmd_buf->Begin(rp,fb[0]);
cmd_buf->Bind(pipeline);
cmd_buf->Bind(pl);
cmd_buf->Bind(vi);
// cmd_buf->Bind(vi);
cmd_buf->Draw(3);
cmd_buf->End();
device->QueueSubmit(cmd_buf,fence);
device->Wait(fence);
device->QueueSubmit(cmd_buf);
device->Wait();
device->QueuePresent();
wait_seconds(3);
wait_seconds(1);
for(int i=0;i<image_count;i++)
delete fb[i];
delete[] fb;
delete pipeline;
delete pl;
delete dsl;
delete sem;
delete fence;
delete rp;
delete vi;
// delete vi;
// delete ubo;
delete shader;
delete cmd_buf;
delete device;
delete inst;