MRT成功,但normal计算不对,待修正

This commit is contained in:
hyzboy 2019-07-10 18:04:50 +08:00
parent 7f3a193676
commit 5b4dad6941
8 changed files with 102 additions and 32 deletions

View File

@ -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})

View File

@ -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<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,sp->material,gbuffer.renderpass,device->GetExtent());
AutoDelete<vulkan::PipelineCreater> 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;
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,11 +385,13 @@ 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);
}

View File

@ -16,7 +16,7 @@ class DescriptorSets
VkPipelineLayout pipeline_layout;
List<VkDescriptorImageInfo> desc_image_info;
ObjectList<VkDescriptorImageInfo> desc_image_info;
List<VkWriteDescriptorSet> write_desc_sets;
private:

View File

@ -164,7 +164,7 @@ public: //material相关
public: //Command Buffer 相关
CommandBuffer * CreateCommandBuffer();
CommandBuffer * CreateCommandBuffer(const VkExtent2D *extent=nullptr);
bool CreateAttachment( List<VkAttachmentReference> &ref_list,
List<VkAttachmentDescription> &desc_list,

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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));
}
/**