From 5b4dad69415089c30c47a182428c4af706c2a732 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 10 Jul 2019 18:04:50 +0800 Subject: [PATCH] =?UTF-8?q?MRT=E6=88=90=E5=8A=9F=EF=BC=8C=E4=BD=86normal?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E4=B8=8D=E5=AF=B9=EF=BC=8C=E5=BE=85=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 6 +- example/Vulkan/Deferred.cpp | 108 +++++++++++++++---- inc/hgl/graph/vulkan/VKDescriptorSets.h | 2 +- inc/hgl/graph/vulkan/VKDevice.h | 2 +- res/shader/gbuffer_opaque.frag | 2 +- res/shader/gbuffer_opaque.vert | 6 +- src/RenderDevice/Vulkan/VKDescriptorSets.cpp | 4 +- src/RenderDevice/Vulkan/VKDevice.cpp | 4 +- 8 files changed, 102 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 085c9e47..dc91538e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,16 +46,14 @@ SET(ULRE ULRE.Base ${RENDER_LIBRARY} ${Vulkan_LIBRARIES}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/assimp/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/MathGeoLib/src) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/NvTriStrip) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/assimp/include) -include_directories(${CMAKE_CURRENT_BINARY_DIR}/3rdpty/assimp/include) - SET(ROOT_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/inc) +SET(SPIRV_CROSS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/3rdpty/SPIRV-Cross) -SET(SPIRV_CROSS_PATH 3rdpty/SPIRV-Cross) include_directories(${SPIRV_CROSS_PATH}) add_subdirectory(${SPIRV_CROSS_PATH}) diff --git a/example/Vulkan/Deferred.cpp b/example/Vulkan/Deferred.cpp index bfea2c94..1829553c 100644 --- a/example/Vulkan/Deferred.cpp +++ b/example/Vulkan/Deferred.cpp @@ -41,7 +41,7 @@ private: struct DeferredGBuffer { - uint32_t width,height; + VkExtent2D extent; vulkan::Framebuffer *framebuffer; vulkan::RenderPass *renderpass; @@ -81,15 +81,20 @@ private: SubpassParam sp_gbuffer; SubpassParam sp_composition; - vulkan::Renderable * ro_cube; + vulkan::Renderable *ro_plane_grid, + *ro_cube, + *ro_sphere, + *ro_torus, + *ro_cylinder, + *ro_cone; vulkan::Sampler * sampler=nullptr; struct { - vulkan::Texture2D *color=nullptr, - *normal=nullptr, - *specular=nullptr; + Texture2DPointer color=nullptr; + Texture2DPointer normal=nullptr; +// Texture2DPointer specular=nullptr; }texture; vulkan::CommandBuffer *gbuffer_cmd=nullptr; @@ -99,7 +104,7 @@ public: ~TestApp() { SAFE_CLEAR(gbuffer_cmd); - SAFE_CLEAR(texture.specular); + //SAFE_CLEAR(texture.specular); SAFE_CLEAR(texture.normal); SAFE_CLEAR(texture.color); SAFE_CLEAR(sampler); @@ -138,8 +143,8 @@ private: bool InitGBuffer() { - gbuffer.width =GBUFFER_WIDTH; - gbuffer.height =GBUFFER_HEIGHT; + gbuffer.extent.width =GBUFFER_WIDTH; + gbuffer.extent.height =GBUFFER_HEIGHT; //根据候选格式表选择格式 const VkFormat position_format =GetCandidateFormat(position_candidate_format, sizeof(position_candidate_format)); @@ -153,10 +158,10 @@ private: ||depth_format ==FMT_UNDEFINED) return(false); - gbuffer.position=device->CreateAttachmentTextureColor(position_format, gbuffer.width,gbuffer.height); - gbuffer.color =device->CreateAttachmentTextureColor(color_format, gbuffer.width,gbuffer.height); - gbuffer.normal =device->CreateAttachmentTextureColor(normal_format, gbuffer.width,gbuffer.height); - gbuffer.depth =device->CreateAttachmentTextureDepth(depth_format, gbuffer.width,gbuffer.height); + gbuffer.position=device->CreateAttachmentTextureColor(position_format, gbuffer.extent.width,gbuffer.extent.height); + gbuffer.color =device->CreateAttachmentTextureColor(color_format, gbuffer.extent.width,gbuffer.extent.height); + gbuffer.normal =device->CreateAttachmentTextureColor(normal_format, gbuffer.extent.width,gbuffer.extent.height); + gbuffer.depth =device->CreateAttachmentTextureDepth(depth_format, gbuffer.extent.width,gbuffer.extent.height); for(uint i=0;i<3;i++) { @@ -211,7 +216,7 @@ private: bool InitGBufferPipeline(SubpassParam *sp) { - AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,sp->material,gbuffer.renderpass,device->GetExtent()); + AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,sp->material,gbuffer.renderpass,gbuffer.extent); pipeline_creater->SetDepthTest(true); pipeline_creater->SetDepthWrite(true); pipeline_creater->SetCullMode(VK_CULL_MODE_BACK_BIT); @@ -253,7 +258,7 @@ private: texture.color =vulkan::LoadTGATexture(OS_TEXT("cardboardPlainStain.tga"),device); texture.normal =vulkan::LoadTGATexture(OS_TEXT("APOCWALL029_NRM.tga"),device); - texture.specular=vulkan::LoadTGATexture(OS_TEXT("APOCWALL029_SPEC.tga"),device); + //texture.specular=vulkan::LoadTGATexture(OS_TEXT("APOCWALL029_SPEC.tga"),device); VkSamplerCreateInfo sampler_create_info; @@ -289,16 +294,79 @@ private: void CreateRenderObject(vulkan::Material *mtl) { - struct CubeCreateInfo cci; + { + struct PlaneGridCreateInfo pgci; - ro_cube=CreateRenderableCube(db,mtl,&cci); + pgci.coord[0].Set(-100,-100,0); + pgci.coord[1].Set( 100,-100,0); + pgci.coord[2].Set( 100, 100,0); + pgci.coord[3].Set(-100, 100,0); + + pgci.step.u=20; + pgci.step.v=20; + + pgci.side_step.u=10; + pgci.side_step.v=10; + + pgci.color.Set(0.75,0,0,1); + pgci.side_color.Set(1,0,0,1); + + ro_plane_grid=CreateRenderablePlaneGrid(db,mtl,&pgci); + } + + { + struct CubeCreateInfo cci; + ro_cube=CreateRenderableCube(db,mtl,&cci); + } + + { + ro_sphere=CreateRenderableSphere(db,mtl,16); + } + + { + TorusCreateInfo tci; + + tci.innerRadius=50; + tci.outerRadius=70; + + tci.numberSlices=32; + tci.numberStacks=16; + + ro_torus=CreateRenderableTorus(db,mtl,&tci); + } + + { + CylinderCreateInfo cci; + + cci.halfExtend=10; + cci.radius=10; + cci.numberSlices=16; + + ro_cylinder=CreateRenderableCylinder(db,mtl,&cci); + } + + { + ConeCreateInfo cci; + + cci.halfExtend=10; + cci.radius=10; + cci.numberSlices=16; + cci.numberStacks=1; + + ro_cone=CreateRenderableCone(db,mtl,&cci); + } } bool InitScene(SubpassParam *sp) { CreateRenderObject(sp->material); - render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cube),scale(1000)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_plane_grid)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_torus)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cube ),translate(-10, 0, 5)*scale(10,10,10)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_sphere ),translate( 10, 0, 5)*scale(10,10,10)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cylinder ),translate( 0, 16, 0)); + render_root.Add(db->CreateRenderableInstance(sp->pipeline,sp->desc_sets,ro_cone ),translate( 0,-16, 0)); render_root.RefreshMatrix(); render_root.ExpendToList(&render_list); @@ -308,7 +376,7 @@ private: bool InitGBufferCommandBuffer() { - gbuffer_cmd=device->CreateCommandBuffer(); + gbuffer_cmd=device->CreateCommandBuffer(&gbuffer.extent); if(!gbuffer_cmd) return(false); @@ -317,10 +385,12 @@ private: if(!gbuffer_cmd->BeginRenderPass(gbuffer.renderpass,gbuffer.framebuffer)) return(false); - + render_list.Render(gbuffer_cmd); gbuffer_cmd->EndRenderPass(); gbuffer_cmd->End(); + + device->SubmitDraw(*gbuffer_cmd); return(true); } diff --git a/inc/hgl/graph/vulkan/VKDescriptorSets.h b/inc/hgl/graph/vulkan/VKDescriptorSets.h index 9661f713..0f9ceb9e 100644 --- a/inc/hgl/graph/vulkan/VKDescriptorSets.h +++ b/inc/hgl/graph/vulkan/VKDescriptorSets.h @@ -16,7 +16,7 @@ class DescriptorSets VkPipelineLayout pipeline_layout; - List desc_image_info; + ObjectList desc_image_info; List write_desc_sets; private: diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 8847414b..58f361c3 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -164,7 +164,7 @@ public: //material相关 public: //Command Buffer 相关 - CommandBuffer * CreateCommandBuffer(); + CommandBuffer * CreateCommandBuffer(const VkExtent2D *extent=nullptr); bool CreateAttachment( List &ref_list, List &desc_list, diff --git a/res/shader/gbuffer_opaque.frag b/res/shader/gbuffer_opaque.frag index df04bd27..534ac869 100644 --- a/res/shader/gbuffer_opaque.frag +++ b/res/shader/gbuffer_opaque.frag @@ -20,7 +20,7 @@ void main() vec3 T = normalize(FragmentTangent); vec3 B = cross(N,T); mat3 TBN = mat3(T,B,N); - vec3 tnorm = TBN * normalize(texture(TextureNormal,FragmentTexCoord).xyz*2.0-vec3(1.0)); + vec3 tnorm = normalize(texture(TextureNormal,FragmentTexCoord).xyz*2.0-vec3(1.0))*TBN; outNormal=vec4(tnorm,1.0); outColor=texture(TextureColor,FragmentTexCoord); diff --git a/res/shader/gbuffer_opaque.vert b/res/shader/gbuffer_opaque.vert index 9f171ed5..b3bc4e5c 100644 --- a/res/shader/gbuffer_opaque.vert +++ b/res/shader/gbuffer_opaque.vert @@ -32,8 +32,8 @@ void main() FragmentPosition=pos.xyz; FragmentTexCoord=TexCoord; - mat3 n=transpose(inverse(mat3(pc.local_to_world))); + mat3 n=inverse(mat3(pc.local_to_world)); - FragmentNormal=n*normalize(Normal); - FragmentTangent=n*normalize(Tangent); + FragmentNormal=normalize(Normal)*n; + FragmentTangent=normalize(Tangent)*n; } diff --git a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp index 3273b15c..0abf9992 100644 --- a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp +++ b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp @@ -54,11 +54,13 @@ bool DescriptorSets::BindSampler(const uint32_t binding,Texture *tex,Sampler *sa if(!tex||!sampler) return(false); - VkDescriptorImageInfo *image_info=desc_image_info.Add(); + VkDescriptorImageInfo *image_info=new VkDescriptorImageInfo; image_info->imageView =tex->GetVulkanImageView(); image_info->imageLayout =tex->GetImageLayout(); image_info->sampler =*sampler; + desc_image_info.Add(image_info); + VkWriteDescriptorSet writeDescriptorSet; writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index f15605b4..b2e1fb80 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -100,7 +100,7 @@ bool Device::Resize(uint width,uint height) return(true); } -CommandBuffer *Device::CreateCommandBuffer() +CommandBuffer *Device::CreateCommandBuffer(const VkExtent2D *extent) { if(!attr->cmd_pool) return(nullptr); @@ -119,7 +119,7 @@ CommandBuffer *Device::CreateCommandBuffer() if(res!=VK_SUCCESS) return(nullptr); - return(new CommandBuffer(attr->device,attr->swapchain_extent,attr->cmd_pool,cmd_buf)); + return(new CommandBuffer(attr->device,extent?*extent:attr->swapchain_extent,attr->cmd_pool,cmd_buf)); } /**