From 8e6f672505fe5d52ca317127c6fc676a2428195e Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 13 Jan 2020 20:13:48 +0800 Subject: [PATCH] add NormalMatrix in PushConstants --- CMAssetsManage | 2 +- CMCore | 2 +- CMakeLists.txt | 2 ++ inc/hgl/graph/WorldMatrix.h | 18 ++++++++--------- inc/hgl/graph/vulkan/VK.h | 3 ++- res/shader/UBO_WorldMatrix.glsl | 2 +- res/shader/push_constant_3d.glsl | 3 ++- src/RenderDevice/Shader/DefaultShader.cpp | 24 +++++++++++++++++++++++ src/SceneGraph/SceneOrient.cpp | 15 ++++++++------ 9 files changed, 51 insertions(+), 20 deletions(-) diff --git a/CMAssetsManage b/CMAssetsManage index a03628ac..74e1d564 160000 --- a/CMAssetsManage +++ b/CMAssetsManage @@ -1 +1 @@ -Subproject commit a03628acf307e07d383d8ab41c2a9ca73a052fb3 +Subproject commit 74e1d564be1a9c2af21b3fc2e4ca319e23b6616a diff --git a/CMCore b/CMCore index cdc28054..f895b3c3 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit cdc2805477fc91ccebb87d56e5e9b8d7bcb492e6 +Subproject commit f895b3c3b08a4cb7abd3b2b8ca49faa9c8512f5a diff --git a/CMakeLists.txt b/CMakeLists.txt index d601de5a..300020b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ use_mgl(${ULRE_3RDPTY_ROOT_PATH}/MathGeoLib) include(use_cm_module) use_cm_module(Core) use_cm_module(Platform) +use_cm_module(AssetsManage) if(CMAKE_SYSTEM_NAME STREQUAL "Windows") include_directories(${ULRE_3RDPTY_ROOT_PATH}/jsoncpp/include) @@ -24,6 +25,7 @@ ENDIF() SET(ULRE CMCore CMPlatform + CMAssetsManage ULRE.Util ULRE.Shader ULRE.RenderDevice.Vulkan diff --git a/inc/hgl/graph/WorldMatrix.h b/inc/hgl/graph/WorldMatrix.h index 9177a4c4..33e2a19d 100644 --- a/inc/hgl/graph/WorldMatrix.h +++ b/inc/hgl/graph/WorldMatrix.h @@ -11,20 +11,20 @@ namespace hgl * 世界矩阵数据 * @see res/shader/UBO_WorldMatrix.glsl */ - struct WorldMatrix + struct alignas(4) WorldMatrix { - alignas(16) Matrix4f ortho; //2D正角视图矩阵 + Matrix4f ortho; //2D正角视图矩阵 - alignas(16) Matrix4f projection; - alignas(16) Matrix4f inverse_projection; + Matrix4f projection; + Matrix4f inverse_projection; - alignas(16) Matrix4f modelview; - alignas(16) Matrix4f inverse_modelview; + Matrix4f modelview; + Matrix4f inverse_modelview; - alignas(16) Matrix4f mvp; - alignas(16) Matrix4f inverse_map; + Matrix4f mvp; + Matrix4f inverse_map; - alignas(16) Vector4f view_pos; ///<眼睛坐标 + Vector4f view_pos; ///<眼睛坐标 };//struct WorldMatrix }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/vulkan/VK.h b/inc/hgl/graph/vulkan/VK.h index 342ffa16..5fe5b36a 100644 --- a/inc/hgl/graph/vulkan/VK.h +++ b/inc/hgl/graph/vulkan/VK.h @@ -65,9 +65,10 @@ enum class ShaderStage Compute =VK_SHADER_STAGE_COMPUTE_BIT };//enum class ShaderStage -struct PushConstant +struct alignas(4) PushConstant { Matrix4f local_to_world; + Matrix3f normal; }; inline void copy(VkExtent3D &e3d,const VkExtent2D &e2d) diff --git a/res/shader/UBO_WorldMatrix.glsl b/res/shader/UBO_WorldMatrix.glsl index 6336904a..8a079472 100644 --- a/res/shader/UBO_WorldMatrix.glsl +++ b/res/shader/UBO_WorldMatrix.glsl @@ -1,4 +1,4 @@ -layout(binding = 0) uniform WorldMatrix // hgl/math/Math.h +layout(std430,binding = 0,row_major) uniform WorldMatrix // hgl/math/Math.h { mat4 ortho; diff --git a/res/shader/push_constant_3d.glsl b/res/shader/push_constant_3d.glsl index 84b562dc..933ce0c0 100644 --- a/res/shader/push_constant_3d.glsl +++ b/res/shader/push_constant_3d.glsl @@ -1,3 +1,4 @@ -layout(push_constant) uniform Consts { +layout(std430,push_constant,row_major) uniform Consts { mat4 local_to_world; + mat3 normal; } pc; diff --git a/src/RenderDevice/Shader/DefaultShader.cpp b/src/RenderDevice/Shader/DefaultShader.cpp index 3f9521e4..d694fd57 100644 --- a/src/RenderDevice/Shader/DefaultShader.cpp +++ b/src/RenderDevice/Shader/DefaultShader.cpp @@ -5,6 +5,7 @@ #include #include #include +#include BEGIN_SHADER_NAMESPACE namespace @@ -16,6 +17,26 @@ namespace UTF8String PushConstant; UTF8String VSOutputLayout; + + bool Load() + { + assets::AssetsSource *as=assets::GetSource("shader"); + + if(!as) + return(false); + + #ifndef USE_MOBILE_SHADER + Header =UTF8String(as->Open("header_desktop.glsl")); + #else + Header =UTF8String(as->Open("header_mobile.glsl")); + #endif// + + UBOWorldMatrix =UTF8String(as->Open("UBO_WorldMatrix.glsl")); + PushConstant =UTF8String(as->Open("push_constant_3d.glsl")); + VSOutputLayout =UTF8String(as->Open("vertex_shader_output_layout.glsl")); + + return(true); + } }//namespace InlineShader constexpr enum class API DEFAULT_RENDER_API =API::Vulkan; @@ -77,6 +98,9 @@ namespace bool CreateDefaultMaterial() { + if(!InlineShader::Load()) + return(false); + if(!CreateDefaultVertexShader())return(false); if(!CreateDefaultFragmentVertex())return(false); diff --git a/src/SceneGraph/SceneOrient.cpp b/src/SceneGraph/SceneOrient.cpp index ea33da18..508bea03 100644 --- a/src/SceneGraph/SceneOrient.cpp +++ b/src/SceneGraph/SceneOrient.cpp @@ -7,11 +7,13 @@ namespace hgl SceneOrient::SceneOrient() { - pc.local_to_world= - LocalMatrix= - LocalToWorldMatrix= - InverseLocalMatrix= - InverseLocalToWorldMatrix=identity(); + pc.local_to_world =Matrix4f::identity; + LocalMatrix =Matrix4f::identity; + LocalToWorldMatrix =Matrix4f::identity; + InverseLocalMatrix =Matrix4f::identity; + InverseLocalToWorldMatrix =Matrix4f::identity; + + pc.normal =Matrix3f::identity; } Matrix4f &SceneOrient::SetLocalMatrix(const Matrix4f &m) @@ -29,7 +31,8 @@ namespace hgl InverseLocalToWorldMatrix=inverse(LocalToWorldMatrix); - pc.local_to_world=LocalToWorldMatrix; + pc.local_to_world =LocalToWorldMatrix; + pc.normal =pc.local_to_world.Float3x3Part(); return LocalToWorldMatrix; }