53 Commits

Author SHA1 Message Date
0f38f94139 split to two class they are RenderL2WBuffer and RenderMIBuffer from RenderAssignBuffer, two sample program RUN OK! 2024-03-26 01:51:59 +08:00
0639440a13 fixed sample program. 2024-03-26 01:33:17 +08:00
8492b886c9 Layout codes 2024-03-26 01:22:58 +08:00
a60ffbb7da first step OK that it's to resumed to use L2W in VertexInputStream. 2024-03-26 01:20:20 +08:00
b48ef1ac30 use HGL_MATH_LIB instead of HGL_GLM_LIB 2024-03-25 22:55:04 +08:00
c363a6fa51 upgraded to 3.5 from 3.0 that it's cmake version minimum required. 2024-03-19 22:00:26 +08:00
c873271f6e little codes 2024-03-18 13:30:06 +08:00
68ff8a472c few of code, but can RUN. 2024-03-16 14:08:55 +08:00
60ba3e4df2 improved safe. 2024-03-16 00:41:20 +08:00
35b7c9651f used 3 colors in BlinnPhongDirectionLight.cpp 2024-03-16 00:20:27 +08:00
89b9735f99 improved sample. 2024-03-15 01:38:47 +08:00
7f744d1207 updated test codes. 2024-03-13 00:13:58 +08:00
b9f5c69f85 optimized to get normal in AutoShader 2024-03-12 23:29:38 +08:00
c469fde84a adjusted order of codes in Std2DMaterialLoader.cpp and Std3DMaterialLoader.cpp 2024-03-12 23:29:06 +08:00
dd6b2f4fb3 add "hasInput(const char *name)" in ShaderDescriptorInfo and ShaderCreateInfoVertex classes. 2024-03-12 23:28:23 +08:00
ba8b44659b resume GetNormalMatrix/GetNormal in auto shader. 2024-03-12 23:04:12 +08:00
13b2ca0575 fixed Viewport UBO bug. 2024-03-12 22:54:33 +08:00
9d704eb33d updated comments. 2024-03-12 22:31:58 +08:00
86c3b65a42 fixed -normal bug in InlineGeometry.cpp 2024-03-12 21:53:41 +08:00
f6e61e70ce 更改了specular算法,但是有点问题。。。而且高光区有点象是VS算的一样,怀疑是NORMAL在VS算的原因。但fs没有GetLocalToWorld函数,需要尝试修复。 2024-03-12 00:42:32 +08:00
5347ac7091 specular OK in BlinnPhongDirectionLight sample. but direction error. 2024-03-10 22:45:38 +08:00
f56928d734 Fixed bug about SunLightPureColor.mtl 2024-03-10 01:00:42 +08:00
f3a59c1bb3 test sunlight UBO ok! 2024-03-10 00:35:35 +08:00
5188d4149c added Unknow and Static in DescriptorSetType 2024-03-09 23:35:56 +08:00
d4096735f5 adjusted order of codes in MaterialCreateInfo.h 2024-03-09 23:23:34 +08:00
4fafd938b1 new function, MaterialFileData load UBO OK! to make shader OK. 2024-03-09 22:26:45 +08:00
f0848541c2 added a newly AddUBO() in MaterialCreateInfo 2024-03-09 22:25:03 +08:00
cc9e756ece fixed Load of ubo codes. 2024-03-09 21:23:02 +08:00
cdedc81f9f save codes of UBO in AccumMemoryManager 2024-03-09 20:48:30 +08:00
c229259623 added comments 2024-03-09 19:49:44 +08:00
2c2f11f47e added little safe check of filename in MaterialFileLoader.cpp 2024-03-07 23:04:54 +08:00
c8b3ed0286 preparing blinnphong light sample. added codes of to load ubo. 2024-03-07 13:59:28 +08:00
2ce36ec26c fixed a bug in ShaderCreateInfoVertex.cpp, put "C-style string" in function that it's a bug. 2024-03-07 00:07:49 +08:00
5ba3c1790d removed standalone ".glsl" files ,merge to c++ codes. 2024-03-06 23:24:57 +08:00
3874d771d3 aligned codes, append CreateIBO8 functions in VKRenderResource.h 2024-03-06 22:51:07 +08:00
49eb8f42f0 updated DescriptorBinding::BindUBO, to support dynamic param. 2024-03-06 22:40:51 +08:00
c355f96f9b little codes 2024-03-06 13:54:05 +08:00
dc3d7b7792 removed BindingMapping 2024-03-06 00:29:17 +08:00
5f1c60a24a Improved VKDescriptorBindingManage 2024-03-06 00:26:23 +08:00
ff6a644e8f added BlinnPhong.h 2024-03-04 13:13:33 +08:00
b9e70426a1 removed sun.ubo, next step is UBO in C++ 2024-03-04 13:12:29 +08:00
a7af276a44 updated SunLightPureColor.mtl 2024-03-04 12:58:22 +08:00
8b82c6c25c support the new other codes. 2024-03-02 10:35:27 +08:00
a8d68c295d renamed many values. 2024-02-13 22:27:12 +08:00
f666d5b248 renamed values of BlinnPhongDirectionLight.cpp 2024-02-12 08:23:31 +08:00
55c27a883b init empty BlinnPhongDirectionLight example 2024-02-12 08:20:24 +08:00
6a7f8e7754 fixed access method 2024-02-03 19:53:19 +08:00
1dbf8ae5c7 renamed values. 2023-12-24 01:35:30 +08:00
8dbc1d899c added to Check ubolist in MaterialFileLoader.cpp 2023-12-05 02:24:09 +08:00
d010ee4278 I updated two sub-modules, Platform and Util. 2023-12-05 02:13:21 +08:00
4aeb28c571 added a parse in MaterialFileLoader.cpp that it's UBOParse 2023-12-05 02:08:18 +08:00
775e031422 added VK_LAYER_AMD_switchable_graphics in VKInstance.h/VKProperties.cpp 2023-12-05 02:07:25 +08:00
deefdf1416 used uint instead of int in Size of viewport 2023-11-10 04:24:10 +08:00
73 changed files with 1294 additions and 579 deletions

2
CMCore

Submodule CMCore updated: 1141f37cc4...cf13064cea

2
CMUtil

Submodule CMUtil updated: c0990c52eb...39faf8b475

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
PROJECT(ULRE)
@@ -26,7 +26,7 @@ SET(ULRE CMCore
ULRE.Util
ULRE.ShaderGen
ULRE.SceneGraph
${HGL_GLM_LIB}
${HGL_MATH_LIB}
${RENDER_LIBRARY}
${Vulkan_LIBRARIES})

View File

@@ -1,4 +0,0 @@
mat4 GetLocalToWorld()
{
return l2w.mats[Assign.x];
}

View File

@@ -1,8 +0,0 @@
MaterialInstance GetMI()
{
#if ShaderStage == VertexShader
return mtl.mi[Assign.y];
#else
return mtl.mi[Input.MaterialInstanceID];
#endif
}

View File

@@ -1,10 +0,0 @@
void HandoverMI()
{
#if ShaderStage == VertexShader
Output.MaterialInstanceID=Assign.y;
#elif ShaderStage == GeometryShader
Output.MaterialInstanceID=Input[0].MaterialInstanceID;
#else
Output.MaterialInstanceID=Input.MaterialInstanceID;
#endif
}

View File

@@ -1,10 +0,0 @@
#version 460 core
#define VertexShader 0x01
#define TessControlShader 0x02
#define TeseEvalShader 0x04
#define GeometryShader 0x08
#define FragmentShader 0x10
#define ComputeShader 0x20
#define TaskShader 0x40
#define MeshShader 0x80

View File

@@ -0,0 +1,2 @@
vec4 direction;
vec4 color;

View File

@@ -0,0 +1,92 @@
#Material
Name BlinnPhong+HalfLambert shading model only color
Reference https://zhuanlan.zhihu.com/p/442023993
Base Std3D/BlinnPhong
//某些Require并不真的存在.ubo文件写成一行一个是为了方便未来改成带路径的
Require LocalToWorld
Require Camera
define HAVE_SPECULAR off //默认不定义HAVE_SPECULAR
UBO
{
File BlinnPhongSun.ubo //文件名,如果/开头表示从ShaderLibrary根目录开始没有则表示同一目录
Struct BlinnPhongSun //结构名称
Name sun //在代码中的变量名
Stage Fragment //会引用的shader
Set Global //Descriptor Set
}
#MaterialInstance
Length 16
Stage Fragment
Code
{
vec3 Color;
float Gloss;
}
#VertexInput
vec3 Normal
#Vertex
Output
{
vec4 Position;
vec3 Normal;
}
Code
{
void main()
{
Output.Normal =GetNormal();
Output.Position =GetPosition3D();
HandoverMI();
gl_Position =Output.Position;
}
}
#Fragment
Output
{
vec4 FragColor;
}
Code
{
#define HAVE_SPECULAR
void main()
{
MaterialInstance mi=GetMI();
//点乘法线和光照
float intensity=0.5*max(dot(Input.Normal,sun.direction.xyz),0.0)+0.5;
//直接光颜色
vec3 direct_color =intensity*sun.color.rgb*mi.Color.rgb;
#ifndef HAVE_SPECULAR
FragColor=vec4(direct_color,1.0);
#else
vec3 spec_color=vec3(0.0);
if(intensity>0.0)
{
vec3 half_vector=normalize(sun.direction.xyz+normalize(Input.Position.xyz+camera.pos));
float specular=max(dot(half_vector,Input.Normal),0.0);
spec_color=specular*pow(specular,mi.Gloss)*sun.color.rgb;
}
FragColor=vec4(direct_color+spec_color,1.0);
#endif//HAVE_SPECULAR
}
}

View File

@@ -1,64 +0,0 @@
#Material
Name BlinnPhong+HalfLambert shading model only color
Reference https://zhuanlan.zhihu.com/p/442023993
Base Std3D
Require LocalToWorld,Camera,Sun
#MaterialInstance
#VertexInput
vec3 Normal
#Vertex
Output
{
vec3 Normal;
}
Code
{
void main()
{
gl_Position=GetPosition3D();
}
}
#Fragment
Output
{
vec4 FragColor;
}
Code
{
void main()
{
//将法线归一化
vec3 world_normal =normalize(Input.Normal);
//对世界坐标下的灯光方法归一化
vec3 world_light_direction =normalize(sun.direction);
//点乘法线和光照
vec3 diffuse =0.5*dot(world_light_direction,world_normal)+0.5;
//直接光颜色
vec3 direct_color =sun.diffuse*diffuse*sun.color;
#ifndef HAVE_SPECULAR
FragColor=vec4(direct_color,1.0);
#else
//归一代视角方向
vec3 view_direction =normalize(camera.pos-world_position);
//世界坐标下的反射光方向
vec3 reflect_direction =normalize(reflect(-world_light_direction,world_normal));
//高光
vec3 specular =sun.specular*pow(saturate(dot(reflect_direction,view_direction)),gloss);
FragColor=vec4(direct_color+specular,1.0);
#endif//HAVE_SPECULAR
}
}

Binary file not shown.

View File

@@ -143,7 +143,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
cam.width=w;
cam.height=h;

View File

@@ -165,7 +165,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
cam.width=w;
cam.height=h;

View File

@@ -116,7 +116,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -95,7 +95,7 @@ private:
if(!render_obj[i].r)
return(false);
render_root.CreateSubNode(rotate(deg2rad(360/DRAW_OBJECT_COUNT*i),Vector3f(0,0,1)),render_obj[i].r);
render_root.CreateSubNode(rotate(deg2rad(double(360/DRAW_OBJECT_COUNT*i)),Vector3f(0,0,1)),render_obj[i].r);
}
render_root.RefreshMatrix();
@@ -130,7 +130,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -145,7 +145,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -120,7 +120,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -26,7 +26,7 @@ private:
Material * mtl_vtx_lum =nullptr;
MaterialInstance * mi_plane_grid =nullptr;
Pipeline * pipeline_vtx_lum =nullptr;
Primitive * ro_plane_grid =nullptr;
Primitive * prim_plane_grid =nullptr;
Material * mtl_vtx_color =nullptr;
MaterialInstance * mi_line =nullptr;
@@ -151,16 +151,14 @@ private:
{
struct PlaneGridCreateInfo pgci;
pgci.grid_size.width =32;
pgci.grid_size.height=32;
pgci.grid_size.Set(32,32);
pgci.sub_count.width =8;
pgci.sub_count.height=8;
pgci.sub_count.Set(8,8);
pgci.lum=0.5;
pgci.sub_lum=0.75;
ro_plane_grid=CreatePlaneGrid(db,mtl_vtx_lum->GetDefaultVIL(),&pgci);
prim_plane_grid=CreatePlaneGrid(db,mtl_vtx_lum->GetDefaultVIL(),&pgci);
}
{
@@ -191,7 +189,7 @@ private:
bool InitScene()
{
Add(ro_plane_grid,mi_plane_grid,pipeline_vtx_lum);
Add(prim_plane_grid,mi_plane_grid,pipeline_vtx_lum);
Add(ro_line,mi_line,pipeline_vtx_color);
camera->pos=Vector3f(32,32,32);
@@ -206,7 +204,7 @@ private:
public:
bool Init(int w,int h) override
bool Init(uint w,uint h) override
{
if(!SceneAppFramework::Init(w,h))
return(false);

View File

@@ -104,7 +104,7 @@ private:
{
Add(material_instance,scale(PLANE_SIZE,PLANE_SIZE,1));
camera->pos=Vector3f(PLANE_SIZE/2,PLANE_SIZE/2,PLANE_SIZE/2);
camera->pos=Vector3f(PLANE_SIZE/4,PLANE_SIZE/2,PLANE_SIZE/4);
camera_control->SetTarget(Vector3f(0,0,0));
camera_control->Refresh();

View File

@@ -19,7 +19,7 @@ private:
Material * material =nullptr;
Pipeline * pipeline =nullptr;
Primitive * ro_plane_grid =nullptr;
Primitive * prim_plane_grid =nullptr;
MaterialInstance * material_instance[3]{};
private:
@@ -56,23 +56,20 @@ private:
struct PlaneGridCreateInfo pgci;
pgci.grid_size.width =32;
pgci.grid_size.height=32;
pgci.grid_size.Set(32,32);
pgci.sub_count.Set(8,8);
pgci.sub_count.width =8;
pgci.sub_count.height=8;
pgci.lum=0.5;
pgci.lum=0.75;
pgci.sub_lum=1.0;
ro_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci);
prim_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci);
return ro_plane_grid;
return prim_plane_grid;
}
Renderable *Add(MaterialInstance *mi,const Matrix4f &mat)
{
Renderable *ri=db->CreateRenderable(ro_plane_grid,mi,pipeline);
Renderable *ri=db->CreateRenderable(prim_plane_grid,mi,pipeline);
if(!ri)
return(nullptr);

View File

@@ -38,7 +38,7 @@ private:
Pipeline * pipeline =nullptr;
Primitive * ro_plane_grid =nullptr;
Primitive * prim_plane_grid =nullptr;
Primitive * ro_line =nullptr;
@@ -93,16 +93,13 @@ private:
{
struct PlaneGridCreateInfo pgci;
pgci.grid_size.width =32;
pgci.grid_size.height=32;
pgci.sub_count.width =8;
pgci.sub_count.height=8;
pgci.grid_size.Set(32,32);
pgci.sub_count.Set(8,8);
pgci.lum=0.5;
pgci.sub_lum=0.75;
ro_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci);
prim_plane_grid=CreatePlaneGrid(db,material->GetDefaultVIL(),&pgci);
}
{
@@ -118,7 +115,7 @@ private:
bool InitScene()
{
Add(ro_plane_grid,mi_plane_grid);
Add(prim_plane_grid,mi_plane_grid);
Add(ro_line,mi_line);
camera->pos=Vector3f(32,32,32);

View File

@@ -0,0 +1,271 @@
// BlinnPhong direction light
#include"VulkanAppFramework.h"
#include<hgl/filesystem/FileSystem.h>
#include<hgl/graph/InlineGeometry.h>
#include<hgl/graph/VKRenderResource.h>
#include<hgl/graph/RenderList.h>
#include<hgl/graph/Camera.h>
#include<hgl/graph/Ray.h>
#include<hgl/graph/VKVertexAttribBuffer.h>
#include<hgl/graph/mtl/Material3DCreateConfig.h>
#include<hgl/graph/mtl/BlinnPhong.h>
#include<hgl/color/Color.h>
using namespace hgl;
using namespace hgl::graph;
static float lumiance_data[2]={1,1};
static Color4f white_color(1,1,1,1);
static mtl::blinnphong::SunLight sun_light=
{
Vector4f(1,1,1,0), //direction
Vector4f(1,0.95,0.9,1) //color
};
constexpr const COLOR AxisColor[4]=
{
COLOR::Red, //X轴颜色
COLOR::Green, //Y轴颜色
COLOR::Blue, //Z轴颜色
COLOR::White //全局颜色
};
class VertexDataManager
{
uint vi_count; ///<顶点输入流数量
const VertexInputFormat * vif_list; ///<顶点输入格式列表
VkDeviceSize vbo_max_size; ///<顶点缓冲区分配空间大小
VkDeviceSize vbo_cur_size; ///<顶点缓冲区当前使用大小
VBO ** vbo; ///<顶点缓冲区列表
VkDeviceSize ibo_cur_size; ///<索引缓冲区当前使用大小
IndexBuffer * ibo; ///<索引缓冲区
struct DataOffset
{
VkDeviceSize vbo_start;
VkDeviceSize vbo_size;
VkDeviceSize ibo_start;
VkDeviceSize ibo_size;
};
public:
VertexDataManager(const VIL &_vil)
{
vi_count=_vil.GetCount();
vif_list=_vil.GetVIFList();
vbo_max_size=0;
vbo_cur_size=0;
vbo=hgl_zero_new<VBO *>(vi_count);
ibo_cur_size=0;
ibo=nullptr;
}
const VkDeviceSize GetVBOMaxCount()const{return vbo_max_size;} ///<取得顶点缓冲区分配的空间最大数量
const VkDeviceSize GetVBOCurCount()const{return vbo_cur_size;} ///<取得顶点缓冲区当前数量
const IndexType GetIBOType ()const{return ibo?ibo->GetType():IndexType::ERR;} ///<取得索引缓冲区类型
const VkDeviceSize GetIBOMaxCount ()const{return ibo?ibo->GetCount():-1;} ///<取得索引缓冲区分配的空间最大数量
const VkDeviceSize GetIBOCurCount ()const{return ibo?ibo_cur_size:-1;} ///<取得索引缓冲区当前数量
};//class VertexDataManager
class TestApp:public SceneAppFramework
{
private: //plane grid
Material * mtl_vertex_lum =nullptr;
MaterialInstance * mi_plane_grid =nullptr;
Pipeline * p_line =nullptr;
Primitive * prim_plane_grid =nullptr;
private:
DeviceBuffer * ubo_sun =nullptr;
private: //sphere
Material * mtl_blinnphong =nullptr;
MaterialInstance * mi_blinnphong[4]{};
Pipeline * p_blinnphong =nullptr;
Primitive * prim_sphere =nullptr;
Primitive * prim_cone =nullptr;
Primitive * prim_cylinder =nullptr;
private:
bool InitVertexLumMP()
{
mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"VertexLuminance3D",Prim::Lines);
cfg.local_to_world=true;
mtl_vertex_lum=db->LoadMaterial("Std3D/VertexLum3D",&cfg);
if(!mtl_vertex_lum)return(false);
mi_plane_grid=db->CreateMaterialInstance(mtl_vertex_lum,nullptr,&white_color);
if(!mi_plane_grid)return(false);
p_line=CreatePipeline(mtl_vertex_lum,InlinePipeline::Solid3D,Prim::Lines);
if(!p_line)
return(false);
return(true);
}
bool CreateBlinnPhongUBO()
{
ubo_sun=db->CreateUBO("sun",sizeof(sun_light),&sun_light);
if(!ubo_sun)return(false);
return(true);
}
bool InitBlinnPhongSunLightMP()
{
mtl::Material3DCreateConfig cfg(device->GetDeviceAttribute(),"BlinnPhong3D",Prim::Triangles);
cfg.local_to_world=true;
mtl_blinnphong=db->LoadMaterial("Std3D/BlinnPhong/SunLightPureColor",&cfg);
if(!mtl_blinnphong)return(false);
mtl_blinnphong->BindUBO(DescriptorSetType::Global,"sun",ubo_sun);
mtl_blinnphong->Update();
Color4f mi_data;
for(uint i=0;i<4;i++)
{
mi_data=GetColor4f(AxisColor[i],4);
mi_blinnphong[i]=db->CreateMaterialInstance(mtl_blinnphong,nullptr,&mi_data);
if(!mi_blinnphong[i])return(false);
}
p_blinnphong=CreatePipeline(mtl_blinnphong,InlinePipeline::Solid3D,Prim::Triangles);
if(!p_blinnphong)
return(false);
return(true);
}
bool CreateRenderObject()
{
using namespace inline_geometry;
//Plane Grid
{
struct PlaneGridCreateInfo pgci;
pgci.grid_size.Set(32,32);
pgci.sub_count.Set(8,8);
pgci.lum=0.5;
pgci.sub_lum=0.75;
prim_plane_grid=CreatePlaneGrid(db,mtl_vertex_lum->GetDefaultVIL(),&pgci);
}
//Sphere
prim_sphere=CreateSphere(db,mi_blinnphong[0]->GetVIL(),16);
//Cone
{
struct ConeCreateInfo cci;
cci.radius =1; //圆锥半径
cci.halfExtend =1; //圆锤一半高度
cci.numberSlices=16; //圆锥底部分割数
cci.numberStacks=8; //圆锥高度分割数
prim_cone=CreateCone(db,mi_blinnphong[1]->GetVIL(),&cci);
}
//Cyliner
{
struct CylinderCreateInfo cci;
cci.halfExtend =4; //圆柱一半高度
cci.numberSlices=16; //圆柱底部分割数
cci.radius =0.25f; //圆柱半径
prim_cylinder=CreateCylinder(db,mi_blinnphong[2]->GetVIL(),&cci);
}
return(true);
}
Renderable *Add(Primitive *r,MaterialInstance *mi,Pipeline *p,const Matrix4f &mat=Identity4f)
{
Renderable *ri=db->CreateRenderable(r,mi,p);
if(!ri)
{
LOG_ERROR(OS_TEXT("Create Renderable failed."));
return(nullptr);
}
render_root.CreateSubNode(mat,ri);
return ri;
}
bool InitScene()
{
Add(prim_plane_grid,mi_plane_grid,p_line,Identity4f);
Add(prim_sphere, mi_blinnphong[0],p_blinnphong,translate(Vector3f(0,0,2)));
Add(prim_cone, mi_blinnphong[1],p_blinnphong);
Add(prim_cylinder, mi_blinnphong[2],p_blinnphong,translate(Vector3f(0,0,-5)));
camera->pos=Vector3f(32,32,32);
camera_control->SetTarget(Vector3f(0,0,0));
camera_control->Refresh();
render_root.RefreshMatrix();
render_list->Expend(&render_root);
return(true);
}
public:
bool Init(uint w,uint h)
{
if(!SceneAppFramework::Init(w,h))
return(false);
if(!InitVertexLumMP())
return(false);
if(!CreateBlinnPhongUBO())
return(false);
if(!InitBlinnPhongSunLightMP())
return(false);
if(!CreateRenderObject())
return(false);
if(!InitScene())
return(false);
return(true);
}
};//class TestApp:public CameraAppFramework
int main(int,char **)
{
return RunApp<TestApp>(1280,720);
}

View File

@@ -112,7 +112,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -108,7 +108,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -184,7 +184,7 @@ public:
return(true);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
VulkanApplicationFramework::Resize(w,h);

View File

@@ -90,7 +90,7 @@ public:
SAFE_CLEAR(inst);
}
virtual bool Init(int w,int h)
virtual bool Init(uint w,uint h)
{
logger::InitLogger(OS_TEXT("VulkanTest"));
@@ -153,13 +153,13 @@ public:
ubo_vp_info=db->CreateUBO("Viewport",sizeof(ViewportInfo),&vp_info);
db->global_descriptor.AddUBO(mtl::SBS_ViewportInfo.name,ubo_vp_info);
db->static_descriptor.AddUBO(mtl::SBS_ViewportInfo.name,ubo_vp_info);
}
return(true);
}
virtual void Resize(int w,int h)
virtual void Resize(uint w,uint h)
{
vp_info.Set(w,h);
ubo_vp_info->Write(&vp_info);
@@ -460,6 +460,7 @@ public:
{
camera=wc;
cur_time=0;
last_time=0;
}
const Vector2f &GetMouseCoord()const{return mouse_pos;}
@@ -494,7 +495,7 @@ public:
SAFE_CLEAR(camera);
}
virtual bool Init(int w,int h) override
virtual bool Init(uint w,uint h) override
{
if(!VulkanApplicationFramework::Init(w,h))
return(false);
@@ -526,7 +527,7 @@ public:
db->global_descriptor.AddUBO(mtl::SBS_CameraInfo.name,ubo_camera_info);
}
void Resize(int w,int h)override
void Resize(uint w,uint h)override
{
vp_info.Set(w,h);
@@ -580,7 +581,7 @@ public:
SAFE_CLEAR(render_list);
}
virtual bool Init(int width,int height) override
virtual bool Init(uint width,uint height) override
{
if(!CameraAppFramework::Init(width,height))
return(false);
@@ -595,7 +596,7 @@ public:
VulkanApplicationFramework::BuildCommandBuffer(index,render_list);
}
virtual void Resize(int w,int h) override
virtual void Resize(uint w,uint h) override
{
CameraAppFramework::Resize(w,h);

View File

@@ -2,7 +2,7 @@
#define HGL_GRAPH_LIGHT_INCLUDE
#include<hgl/math/Math.h>
#include<hgl/type/Color4f.h>
#include<hgl/color/Color4f.h>
namespace hgl
{
namespace graph

View File

@@ -3,7 +3,8 @@
#include<hgl/graph/VKVBOList.h>
VK_NAMESPACE_BEGIN
class RenderAssignBuffer;
class RenderL2WBuffer;
class RenderMIBuffer;
/**
* 同一材质的对象渲染列表
@@ -19,7 +20,8 @@ class MaterialRenderList
private:
RenderAssignBuffer *assign_buffer;
RenderL2WBuffer *l2w_buffer;
RenderMIBuffer *mi_buffer;
struct RenderItem
{
@@ -39,6 +41,8 @@ private:
DataArray<RenderItem> ri_array;
uint ri_count;
VkDeviceSize l2w_buffer_size[4];
void StatMI();
void Stat();
@@ -56,7 +60,7 @@ protected:
public:
MaterialRenderList(GPUDevice *d,Material *m);
MaterialRenderList(GPUDevice *d,bool l2w,Material *m);
~MaterialRenderList();
void Add(Renderable *ri,const Matrix4f &mat);

View File

@@ -26,7 +26,9 @@ constexpr size_t VK_DESCRIPTOR_TYPE_RANGE_SIZE=VK_DESCRIPTOR_TYPE_END_RANGE-VK_D
#endif//VK_DESCRIPTOR_TYPE_RANGE_SIZE
using CharPointerList=hgl::List<const char *>;
using BindingMapping=Map<uint32_t,int>;
using BindingMap =Map<AnsiString,int>;
using BindingMapArray =BindingMap[VK_DESCRIPTOR_TYPE_RANGE_SIZE];
class VulkanInstance;
class GPUPhysicalDevice;
@@ -142,21 +144,14 @@ enum IndexType:uint
U16=0,
U32,
U8=VK_INDEX_TYPE_UINT8_EXT,
ERR=VK_INDEX_TYPE_MAX_ENUM
};
/**
* max-lengths:
*
256 bytes: nvidia,arm
128 bytes: amd,intel,powervr,adreno
*/
struct PushConstant
{
Matrix4f local_to_world;
Matrix4f normal;
};
constexpr uint32_t MAX_PUSH_CONSTANT_BYTES=sizeof(PushConstant);
//Push Constant max-lengths:
//
// 256 bytes: nvidia,arm
// 128 bytes: amd,intel,powervr,adreno
inline void copy(VkExtent3D &e3d,const VkExtent2D &e2d,const uint32 depth=1)
{

View File

@@ -1,8 +1,9 @@
#ifndef HGL_GRAPH_DESCRIPTOR_BINDING_MANAGE_INCLUDE
#ifndef HGL_GRAPH_DESCRIPTOR_BINDING_MANAGE_INCLUDE
#define HGL_GRAPH_DESCRIPTOR_BINDING_MANAGE_INCLUDE
#include<hgl/type/Map.h>
#include<hgl/type/String.h>
#include<hgl/graph/VK.h>
#include<hgl/graph/VKDescriptorSetType.h>
namespace hgl
{
@@ -11,10 +12,15 @@ namespace hgl
class DeviceBuffer;
class Texture;
class Material;
class MaterialParameters;
/**
* 描述符绑定器<Br>
* 一般用于注册通用数据,为材质进行自动绑定。
*/
class DescriptorBinding
{
DescriptorSetType set_type;
DescriptorSetType set_type; ///<描述符合集类型
Map<AnsiString,DeviceBuffer *> ubo_map;
Map<AnsiString,DeviceBuffer *> ssbo_map;
@@ -93,6 +99,12 @@ namespace hgl
texture_map.DeleteByValue(tex);
}
private:
void BindUBO(MaterialParameters *,const BindingMap &,bool dynamic);
public:
bool Bind(Material *);
};//class DescriptorBinding
}//namespace graph

View File

@@ -28,6 +28,11 @@ VK_NAMESPACE_BEGIN
VK_BOOL1BIT(profiles)
}khronos;
struct
{
VK_BOOL1BIT(switchable_graphics)
}amd;
struct
{
VK_BOOL1BIT(optimus)

View File

@@ -49,15 +49,15 @@ public:
virtual ~Material();
const UTF8String & GetName ()const{return name;}
const AnsiString & GetName ()const{return name;}
const VertexInput * GetVertexInput ()const{return vertex_input;}
const ShaderStageCreateInfoList & GetStageList ()const{return shader_stage_list;}
const MaterialDescriptorManager * GetDescriptorSets ()const{return desc_manager;}
// const MaterialDescriptorManager * GetDescriptorManager ()const{return desc_manager;}
const VkPipelineLayout GetPipelineLayout ()const;
const PipelineLayoutData * GetPipelineLayoutData ()const{return pipeline_layout_data;}
// const PipelineLayoutData * GetPipelineLayoutData ()const{return pipeline_layout_data;}
public:
@@ -69,7 +69,6 @@ public:
}
const bool hasSet (const DescriptorSetType &type)const;
const bool hasAssign ()const;
const VIL * GetDefaultVIL()const;
VIL * CreateVIL(const VILConfig *format_map=nullptr);

View File

@@ -5,9 +5,6 @@
#include<hgl/graph/VKShaderDescriptorSet.h>
VK_NAMESPACE_BEGIN
using BindingMap=Map<AnsiString,int>;
using BindingMapArray=BindingMap[VK_DESCRIPTOR_TYPE_RANGE_SIZE];
class MaterialDescriptorManager
{
UTF8String mtl_name;

View File

@@ -35,6 +35,8 @@ public:
void Free() override {/* DON'T RUN ANY OPERATION.*/}
void Flush(const VkDeviceSize);
bool Write(const void *source,const uint64 offset,const uint64 size) override;
};//class VKMemoryAllocator:public AbstractMemoryAllocator
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_MEMORY_ALLOCATOR_INCLUDE

View File

@@ -80,11 +80,15 @@ private:
public:
//注:并非一定要走这里,这里只是提供一个注册和自动绑定的机制
DescriptorBinding global_descriptor; ///<全局属性描述符绑定管理
DescriptorBinding static_descriptor; ///<静态属性描述符绑定管理
DescriptorBinding global_descriptor; ///<全局属性描述符绑定管理
public:
RenderResource(GPUDevice *dev):device(dev),global_descriptor(DescriptorSetType::Global){}
RenderResource(GPUDevice *dev):device(dev),
static_descriptor(DescriptorSetType::Static),
global_descriptor(DescriptorSetType::Global)
{}
virtual ~RenderResource()=default;
public: //Add
@@ -100,9 +104,9 @@ public: //Add
public: // VBO/VAO
VBO *CreateVBO(VkFormat format,uint32_t count,const void *data,SharingMode sm=SharingMode::Exclusive);
VBO *CreateVBO(VkFormat format,uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateVBO(format,count,nullptr,sm);}
VBO *CreateVBO(const VAD *vad,SharingMode sm=SharingMode::Exclusive){return CreateVBO(vad->GetFormat(),vad->GetCount(),vad->GetData(),sm);}
VBO *CreateVBO(VkFormat format,uint32_t count,const void *data, SharingMode sm=SharingMode::Exclusive);
VBO *CreateVBO(VkFormat format,uint32_t count, SharingMode sm=SharingMode::Exclusive){return CreateVBO(format, count, nullptr, sm);}
VBO *CreateVBO(const VAD *vad, SharingMode sm=SharingMode::Exclusive){return CreateVBO(vad->GetFormat(), vad->GetCount(),vad->GetData(), sm);}
#define SCENE_DB_CREATE_FUNC(name) DeviceBuffer *Create##name(const AnsiString &buf_name,VkDeviceSize size,void *data,SharingMode sm=SharingMode::Exclusive); \
DeviceBuffer *Create##name(const AnsiString &buf_name,VkDeviceSize size,SharingMode sm=SharingMode::Exclusive){return Create##name(buf_name,size,nullptr,sm);}
@@ -114,12 +118,14 @@ public: // VBO/VAO
#undef SCENE_DB_CREATE_FUNC
IndexBuffer *CreateIBO(IndexType index_type,uint32_t count,const void * data, SharingMode sm=SharingMode::Exclusive);
IndexBuffer *CreateIBO8 ( uint32_t count,const uint8 *data, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U8 ,count,(void *)data,sm);}
IndexBuffer *CreateIBO16( uint32_t count,const uint16 *data, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U16,count,(void *)data,sm);}
IndexBuffer *CreateIBO32( uint32_t count,const uint32 *data, SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U32,count,(void *)data,sm);}
IndexBuffer *CreateIBO(IndexType index_type,uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(index_type,count,nullptr,sm);}
IndexBuffer *CreateIBO16( uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U16,count,nullptr,sm);}
IndexBuffer *CreateIBO32( uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U32,count,nullptr,sm);}
IndexBuffer *CreateIBO(IndexType index_type,uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(index_type, count,nullptr,sm);}
IndexBuffer *CreateIBO8 ( uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U8, count,nullptr,sm);}
IndexBuffer *CreateIBO16( uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U16, count,nullptr,sm);}
IndexBuffer *CreateIBO32( uint32_t count,SharingMode sm=SharingMode::Exclusive){return CreateIBO(IndexType::U32, count,nullptr,sm);}
public: //Material

View File

@@ -0,0 +1,26 @@
#pragma once
#include<hgl/graph/mtl/StdMaterial.h>
#include<hgl/graph/mtl/ShaderBuffer.h>
#include<hgl/math/Vector.h>
STD_MTL_NAMESPACE_BEGIN
namespace blinnphong
{
struct SunLight
{
Vector4f direction;
Vector4f color;
};//struct SunLight
constexpr const ShaderBufferSource SBS_SunLight=
{
"SunLight",
"sun",
R"(
vec4 direction;
vec4 color;
)"
};
}//namespace blinnphong
STD_MTL_NAMESPACE_END

View File

@@ -45,15 +45,6 @@ constexpr const ShaderBufferSource SBS_CameraInfo=
// UBO必须严格指定数组的大小
// SSBO则不需要使用[]方式指定为动态大小数组
constexpr const ShaderBufferSource SBS_LocalToWorld=
{
"LocalToWorldData",
"l2w",
R"(
mat4 mats[L2W_MAX_COUNT];)"
};
constexpr const char MaterialInstanceStruct[]="MaterialInstance";
constexpr const ShaderBufferSource SBS_MaterialInstance=

View File

@@ -0,0 +1,24 @@
#pragma once
#include<hgl/graph/mtl/StdMaterial.h>
#include<hgl/graph/mtl/ShaderBuffer.h>
STD_MTL_NAMESPACE_BEGIN
/**
* 所有UBO的基类它即向生成器提供代码也可以为渲染器提供数据
*/
class UniformBuffer
{
private:
ShaderBufferSource *sbs;
public:
virtual const AnsiString &GetStructName ()const{return sbs->struct_name;} ///<取得结构名称
virtual const AnsiString &GetDefaultValueName ()const{return sbs->name;} ///<取得默认变量名称
virtual const AnsiString &GetShaderCodes ()const{return sbs->codes;} ///<取得Shader代码
};//class UniformBuffer
STD_MTL_NAMESPACE_END

View File

@@ -34,8 +34,6 @@ namespace hgl{namespace graph
UBODescriptor *mi_ubo;
uint32_t l2w_shader_stage;
uint32_t l2w_max_count;
UBODescriptor *l2w_ubo;
ShaderCreateInfoMap shader_map; ///<着色器列表
@@ -83,10 +81,11 @@ namespace hgl{namespace graph
bool AddStruct(const AnsiString &ubo_typename,const AnsiString &codes);
bool AddStruct(const ShaderBufferSource &ss){return AddStruct(ss.struct_name,ss.codes);}
bool AddUBO(const VkShaderStageFlagBits flag_bits,const DescriptorSetType set_type,const AnsiString &type_name,const AnsiString &name);
bool AddSampler(const VkShaderStageFlagBits flag_bits,const DescriptorSetType set_type,const SamplerType &st,const AnsiString &name);
bool AddUBO(const VkShaderStageFlagBits flag_bits,const DescriptorSetType set_type,const AnsiString &struct_name,const AnsiString &name);
bool AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const AnsiString &struct_name,const AnsiString &name);
bool AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const ShaderBufferSource &ss){return AddUBO(flag_bits,set_type,ss.struct_name,ss.name);}
bool AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const ShaderBufferSource &ss);
bool AddSampler(const VkShaderStageFlagBits flag_bits,const DescriptorSetType set_type,const SamplerType &st,const AnsiString &name);
bool CreateShader();
};//class MaterialCreateInfo

View File

@@ -80,13 +80,10 @@ public:
int AddOutput(const AnsiString &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth);
void AddFunction(const char *str){function_list.Add(str);}
void AddFunction(const AnsiString *str){function_list.Add(str->c_str());}
void SetMaterialInstance(UBODescriptor *,const AnsiString &);
void AddMaterialInstanceOutput();
void SetLocalToWorld(UBODescriptor *);
void SetMain(const AnsiString &str){main_function=str;}
void SetMain(const char *str,const int len)
{

View File

@@ -19,8 +19,11 @@ namespace hgl
int AddInput(const graph::VAT &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic);
int AddInput(const AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic);
int hasInput(const char *);
void AddLocalToWorld();
void AddMaterialInstanceID();
void AddJoint();
void AddAssign();
};//class ShaderCreateInfoVertex:public ShaderCreateInfo
}//namespace graph
}//namespace hgl::graph

View File

@@ -59,6 +59,8 @@ public:
bool AddInput(ShaderAttribute *);
bool AddOutput(ShaderAttribute *);
bool hasInput(const char *)const; ///<是否有指定输入
void AddStruct(const AnsiString &);
bool AddUBO(DescriptorSetType type,const UBODescriptor *sd);
bool AddSampler(DescriptorSetType type,const SamplerDescriptor *sd);

View File

@@ -166,24 +166,24 @@ namespace hgl
{
PrimitiveCreater rc(db,vil);
if(!rc.Init(((pgci->grid_size.width+1)+(pgci->grid_size.height+1))*2))
if(!rc.Init(((pgci->grid_size.Width()+1)+(pgci->grid_size.Height()+1))*2))
return(nullptr);
AutoDelete<VB3f> vertex=rc.AccessVAD<VB3f>(VAN::Position);
const float right=float(pgci->grid_size.width)/2.0f;
const float right=float(pgci->grid_size.Width())/2.0f;
const float left =-right;
const float bottom=float(pgci->grid_size.height)/2.0f;
const float bottom=float(pgci->grid_size.Height())/2.0f;
const float top =-bottom;
for(int row=0;row<=pgci->grid_size.height;row++)
for(int row=0;row<=pgci->grid_size.Height();row++)
{
vertex->WriteLine( Vector3f(left ,top+row,0),
Vector3f(right,top+row,0));
}
for(int col=0;col<=pgci->grid_size.width;col++)
for(int col=0;col<=pgci->grid_size.Width();col++)
{
vertex->WriteLine(Vector3f(left+col,top, 0),
Vector3f(left+col,bottom,0));
@@ -192,17 +192,17 @@ namespace hgl
AutoDelete<VB1f> lum=rc.AccessVAD<VB1f>(VAN::Luminance);
if(lum)
{
for(int row=0;row<=pgci->grid_size.height;row++)
for(int row=0;row<=pgci->grid_size.Height();row++)
{
if((row%pgci->sub_count.height)==0)
if((row%pgci->sub_count.Height())==0)
lum->RepeatWrite(pgci->sub_lum,2);
else
lum->RepeatWrite(pgci->lum,2);
}
for(int col=0;col<=pgci->grid_size.width;col++)
for(int col=0;col<=pgci->grid_size.Width();col++)
{
if((col%pgci->sub_count.width)==0)
if((col%pgci->sub_count.Width())==0)
lum->RepeatWrite(pgci->sub_lum,2);
else
lum->RepeatWrite(pgci->lum,2);
@@ -483,9 +483,9 @@ namespace hgl
if(np)
{
*np=+x;++np;
*np=-y;++np;
*np=+z;++np;
*np=x;++np;
*np=y;++np;
*np=z;++np;
}
if(tcp)
@@ -712,7 +712,7 @@ namespace hgl
// NOTE: cos (2PIx) = cos (x) and sin (2PIx) = sin (x) so, we can use this formula
// normal = {cos(2PIs)cos(2PIt) , sin(2PIs)cos(2PIt) ,sin(2PIt)}
*np = +cos2PIs * cos2PIt; ++np;
*np = -sin2PIt; ++np;
*np = +sin2PIt; ++np;
*np = +sin2PIs * cos2PIt; ++np;
}
@@ -943,7 +943,7 @@ namespace hgl
if(np)
{
*np = cos(currentAngle); ++np;
*np = sin(currentAngle); ++np;
*np = -sin(currentAngle); ++np;
*np = 0.0f; ++np;
}
@@ -1112,7 +1112,7 @@ namespace hgl
if(np)
{
*np = h / l * cos(currentAngle); ++np;
*np = h / l * sin(currentAngle); ++np;
*np =-h / l * sin(currentAngle); ++np;
*np = r / l; ++np;
}

View File

@@ -47,16 +47,21 @@ int Comparator<hgl::graph::RenderNode>::compare(const hgl::graph::RenderNode &ob
}
VK_NAMESPACE_BEGIN
MaterialRenderList::MaterialRenderList(GPUDevice *d,Material *m)
MaterialRenderList::MaterialRenderList(GPUDevice *d,bool l2w,Material *m)
{
device=d;
cmd_buf=nullptr;
material=m;
if(material->hasAssign())
assign_buffer=new RenderAssignBuffer(d,material->GetMIDataBytes());
if(l2w)
l2w_buffer=new RenderL2WBuffer(device);
else
assign_buffer=nullptr;
l2w_buffer=nullptr;
if(material->HasMI())
mi_buffer=new RenderMIBuffer(device,material->GetMIDataBytes());
else
mi_buffer=nullptr;
vbo_list=new VBOList(material->GetVertexInput()->GetCount());
}
@@ -64,7 +69,8 @@ MaterialRenderList::MaterialRenderList(GPUDevice *d,Material *m)
MaterialRenderList::~MaterialRenderList()
{
SAFE_CLEAR(vbo_list);
SAFE_CLEAR(assign_buffer)
SAFE_CLEAR(mi_buffer);
SAFE_CLEAR(l2w_buffer);
}
void MaterialRenderList::Add(Renderable *ri,const Matrix4f &mat)
@@ -88,13 +94,15 @@ void MaterialRenderList::End()
Stat();
if(assign_buffer)
if(l2w_buffer)
{
if(material->HasMI())
StatMI();
l2w_buffer->WriteNode(rn_list.GetData(),node_count);
}
//写入LocalToWorld数据
assign_buffer->WriteNode(rn_list.GetData(),node_count,mi_set);
if(mi_buffer)
{
StatMI();
mi_buffer->WriteNode(rn_list.GetData(),node_count,mi_set);
}
}
@@ -179,6 +187,17 @@ bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index)
vbo_list->Add(vid->buffer_list,vid->buffer_offset,vid->binding_count);
}
if(l2w_buffer)//LocalToWorld组由RenderList合成
{
for(uint i=0;i<4;i++)
l2w_buffer_size[i]=ri_index*16; //mat4每列都是rgba32f自然是16字节
vbo_list->Add(l2w_buffer->GetVBO(),l2w_buffer_size,4);
}
if(mi_buffer) //材质实例组
vbo_list->Add(mi_buffer->GetVBO(),MI_VBO_STRIDE_BYTES*ri_index);
//if(!vbo_list.IsFull()) //Joint组暂未支持
//{
// const uint joint_id_binding_count=vil->GetCount(VertexInputGroup::JointID);
@@ -207,12 +226,6 @@ bool MaterialRenderList::Bind(const VertexInputData *vid,const uint ri_index)
// }
//}
//if(!vbo_list->IsFull()) //Assign组
{
if(assign_buffer)
vbo_list->Add(assign_buffer->GetAssignVBO(),ASSIGN_VBO_STRIDE_BYTES*ri_index);
}
//if(count!=binding_count)
//{
// //还有没支持的绑定组????
@@ -273,8 +286,8 @@ void MaterialRenderList::Render(RenderCmdBuffer *rcb)
last_pipeline =nullptr;
last_vid =nullptr;
if(assign_buffer)
assign_buffer->Bind(material);
if(mi_buffer)
mi_buffer->Bind(material);
cmd_buf->BindDescriptorSets(material);

View File

@@ -8,54 +8,130 @@
#include<hgl/graph/mtl/UBOCommon.h>
VK_NAMESPACE_BEGIN
RenderAssignBuffer::RenderAssignBuffer(GPUDevice *dev,const uint mi_bytes)
RenderL2WBuffer::RenderL2WBuffer(GPUDevice *dev)
{
hgl_zero(*this);
device=dev;
mi_data_bytes=mi_bytes;
ubo_mi=nullptr;
mi_count=0;
}
VkBuffer RenderAssignBuffer::GetAssignVBO()const
void RenderL2WBuffer::Clear()
{
return vbo_assigns->GetBuffer();
}
void RenderAssignBuffer::Bind(Material *mtl)const
{
if(!mtl)return;
if(!mtl->hasAssign())
return;
mtl->BindUBO(DescriptorSetType::PerFrame,mtl::SBS_LocalToWorld.name,ubo_l2w);
mtl->BindUBO(DescriptorSetType::PerMaterial,mtl::SBS_MaterialInstance.name,ubo_mi);
}
void RenderAssignBuffer::Clear()
{
SAFE_CLEAR(ubo_l2w);
SAFE_CLEAR(ubo_mi);
SAFE_CLEAR(vbo_assigns);
SAFE_CLEAR(l2w_vbo[0])
SAFE_CLEAR(l2w_vbo[1])
SAFE_CLEAR(l2w_vbo[2])
SAFE_CLEAR(l2w_vbo[3])
node_count=0;
mi_count=0;
}
void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
#ifdef _DEBUG
namespace
{
constexpr const char *l2w_buffer_name[]=
{
"VBO:Buffer:LocalToWorld:0",
"VBO:Buffer:LocalToWorld:1",
"VBO:Buffer:LocalToWorld:2",
"VBO:Buffer:LocalToWorld:3"
};
constexpr const char *l2w_memory_name[]=
{
"VBO:Memory:LocalToWorld:0",
"VBO:Memory:LocalToWorld:1",
"VBO:Memory:LocalToWorld:2",
"VBO:Memory:LocalToWorld:3"
};
}
#endif//_DEBUG
void RenderL2WBuffer::Alloc(const uint nc)
{
Clear();
{
node_count=nc;
ubo_l2w=device->CreateUBO(node_count*sizeof(Matrix4f));
for(uint i=0;i<4;i++)
{
l2w_vbo[i]=device->CreateVBO(VF_V4F,node_count);
l2w_buffer[i]=l2w_vbo[i]->GetBuffer();
}
}
#ifdef _DEBUG
DebugUtils *du=device->GetDebugUtils();
if(du)
{
for(int i=0;i<4;i++)
{
du->SetBuffer(l2w_buffer[i],l2w_buffer_name[i]);
du->SetDeviceMemory(l2w_vbo[i]->GetVkMemory(),l2w_memory_name[i]);
}
}
#endif//_DEBUG
}
void RenderL2WBuffer::WriteNode(RenderNode *render_node,const uint count)
{
RenderNode *rn;
Alloc(count);
glm::vec4 *tp;
for(uint col=0;col<4;col++)
{
tp=(glm::vec4 *)(l2w_vbo[col]->Map());
rn=render_node;
for(uint i=0;i<count;i++)
{
*tp=rn->local_to_world[col];
++tp;
++rn;
}
l2w_vbo[col]->Unmap();
}
}
VK_NAMESPACE_END
VK_NAMESPACE_BEGIN
RenderMIBuffer::RenderMIBuffer(GPUDevice *dev,const uint mi_bytes)
{
hgl_zero(*this);
device=dev;
mi_data_bytes=mi_bytes;
}
void RenderMIBuffer::Bind(Material *mtl)const
{
if(!mtl)return;
mtl->BindUBO(DescriptorSetType::PerMaterial,mtl::SBS_MaterialInstance.name,ubo_mi);
}
void RenderMIBuffer::Clear()
{
SAFE_CLEAR(ubo_mi);
SAFE_CLEAR(vbo_mi);
mi_count=0;
node_count=0;
}
void RenderMIBuffer::Alloc(const uint nc,const uint mc)
{
Clear();
node_count=nc;
if(mi_data_bytes>0&&mc>0)
{
mi_count=mc;
@@ -63,72 +139,53 @@ void RenderAssignBuffer::Alloc(const uint nc,const uint mc)
ubo_mi=device->CreateUBO(mi_data_bytes*mi_count);
}
vbo_assigns=device->CreateVBO(ASSIGN_VBO_FMT,node_count);
vbo_mi=device->CreateVBO(MI_VBO_FMT,node_count);
mi_buffer=vbo_mi->GetBuffer();
#ifdef _DEBUG
DebugUtils *du=device->GetDebugUtils();
if(du)
{
du->SetBuffer(ubo_l2w->GetBuffer(),"UBO:Buffer:LocalToWorld");
du->SetDeviceMemory(ubo_l2w->GetVkMemory(),"UBO:Memory:LocalToWorld");
du->SetBuffer(ubo_mi->GetBuffer(),"UBO:Buffer:MaterialInstance");
du->SetDeviceMemory(ubo_mi->GetVkMemory(),"UBO:Memory:MaterialInstance");
if(ubo_mi)
{
du->SetBuffer(ubo_mi->GetBuffer(),"UBO:Buffer:MaterialInstance");
du->SetDeviceMemory(ubo_mi->GetVkMemory(),"UBO:Memory:MaterialInstance");
}
du->SetBuffer(vbo_assigns->GetBuffer(),"VBO:Buffer:Assign");
du->SetDeviceMemory(vbo_assigns->GetVkMemory(),"VBO:Memory:Assign");
du->SetBuffer(vbo_mi->GetBuffer(),"VBO:Buffer:MaterialInstanceID");
du->SetDeviceMemory(vbo_mi->GetVkMemory(),"VBO:Memory:MaterialInstanceID");
}
#endif//_DEBUG
}
void RenderAssignBuffer::WriteNode(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set)
void RenderMIBuffer::WriteNode(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set)
{
RenderNode *rn;
Alloc(count,mi_set.GetCount());
if(ubo_mi)
uint8 *mip=(uint8 *)(ubo_mi->Map());
for(MaterialInstance *mi:mi_set)
{
uint8 *mip=(uint8 *)(ubo_mi->Map());
for(MaterialInstance *mi:mi_set)
{
memcpy(mip,mi->GetMIData(),mi_data_bytes);
mip+=mi_data_bytes;
}
ubo_mi->Unmap();
memcpy(mip,mi->GetMIData(),mi_data_bytes);
mip+=mi_data_bytes;
}
uint16 *idp=(uint16 *)(vbo_assigns->Map());
ubo_mi->Unmap();
uint16 *idp=(uint16 *)(vbo_mi->Map());
{
Matrix4f *tp=(hgl::Matrix4f *)(ubo_l2w->Map());
Matrix4f *l2w=tp;
rn=render_node;
for(uint i=0;i<count;i++)
{
*l2w=rn->local_to_world;
++l2w;
*idp=i;
++idp;
*idp=mi_set.Find(rn->ri->GetMaterialInstance());
++idp;
++rn;
}
ubo_l2w->Unmap();
}
vbo_assigns->Unmap();
vbo_mi->Unmap();
}
VK_NAMESPACE_END

View File

@@ -25,24 +25,50 @@ VK_NAMESPACE_BEGIN
struct RenderNode;
class MaterialInstance;
class RenderL2WBuffer
{
GPUDevice *device;
uint node_count; ///<渲染节点数量
VBO *l2w_vbo[4];
VkBuffer l2w_buffer[4];
private:
void Alloc(const uint nc);
void Clear();
public:
const VkBuffer *GetVBO()const{return l2w_buffer;}
public:
RenderL2WBuffer(GPUDevice *dev);
~RenderL2WBuffer(){Clear();}
void WriteNode(RenderNode *render_node,const uint count);
};//class RenderL2WBuffer
/*
* 渲染节点额外提供的数据
*/
class RenderAssignBuffer
class RenderMIBuffer
{
private:
GPUDevice *device;
uint node_count; ///<渲染节点数量
DeviceBuffer *ubo_l2w; ///<Local2World数据
uint32_t mi_data_bytes; ///<材质实例数据字节数
uint32_t mi_count; ///<材质实例数量
DeviceBuffer *ubo_mi; ///<材质实例数据
//Assign VBO
VBO *vbo_assigns; ///<RG16UI格式的VertexInputStream,,,,X:L2W ID,Y:MI ID
VBO *vbo_mi; ///<材质实例ID(R16UI格式)
VkBuffer mi_buffer;
private:
@@ -52,16 +78,20 @@ private:
public:
VkBuffer GetAssignVBO()const;
const VkBuffer GetVBO()const{return mi_buffer;}
void Bind(Material *)const;
public:
RenderAssignBuffer(GPUDevice *dev,const uint32_t mi_bytes);
~RenderAssignBuffer(){Clear();}
RenderMIBuffer(GPUDevice *dev,const uint32_t mi_bytes);
~RenderMIBuffer(){Clear();}
void WriteNode(RenderNode *render_node,const uint count,const MaterialInstanceSets &mi_set);
//下一代将MaterialInstanceSets使用提前化这样不用每一次绘制都重新写入MI DATA可以提升效率。
//虽然这样就不自动化了,但我们要的就是不自动化。
//必须在外部全部准备好MaterialInstanceSets然后一次性写入。
//渲染时找不到就直接用0号材质实例
};//struct RenderAssignBuffer
void WriteNode(RenderNode *render_node,const uint nc,const MaterialInstanceSets &mi_set);
};//struct RenderMIBuffer
VK_NAMESPACE_END

View File

@@ -35,7 +35,7 @@ namespace hgl
if(!mrl_map.Get(mtl,mrl))
{
mrl=new MaterialRenderList(device,mtl);
mrl=new MaterialRenderList(device,true,mtl);
mrl_map.Add(mtl,mrl);
}

View File

@@ -62,6 +62,7 @@ namespace
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance,const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,const VkAllocationCallbacks *pAllocator,VkDebugUtilsMessengerEXT *pDebugMessenger)
{
auto func=(PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance,"vkCreateDebugUtilsMessengerEXT");
if(func)
{
return func(instance,pCreateInfo,pAllocator,pDebugMessenger);
@@ -75,6 +76,7 @@ namespace
void DestroyDebugUtilsMessengerEXT(VkInstance instance,VkDebugUtilsMessengerEXT debugMessenger,const VkAllocationCallbacks *pAllocator)
{
auto func=(PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance,"vkDestroyDebugUtilsMessengerEXT");
if(func)
{
func(instance,debugMessenger,pAllocator);
@@ -85,10 +87,8 @@ namespace
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData)
{
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
std::cerr<<"[ERROR] "; else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
std::cerr<<"[WARNING] "; else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) std::cerr<<"[ERROR] "; else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) std::cerr<<"[WARNING] "; else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) std::cerr<<"[INFO] "; else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) std::cerr<<"[VERBOSE] "; else
std::cerr<<"[Validation layer] ";
@@ -147,10 +147,8 @@ namespace
{
const char *obj_type_name=GetVkDebugReportObjectTypename(objType);
if(msgFlags&VK_DEBUG_REPORT_ERROR_BIT_EXT)
std::cerr<<"[ERROR:"; else
if(msgFlags&VK_DEBUG_REPORT_WARNING_BIT_EXT)
std::cerr<<"[WARNING:"; else
if(msgFlags&VK_DEBUG_REPORT_ERROR_BIT_EXT) std::cerr<<"[ERROR:"; else
if(msgFlags&VK_DEBUG_REPORT_WARNING_BIT_EXT) std::cerr<<"[WARNING:"; else
if(msgFlags&VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)std::cerr<<"[PERFORMANCE WARNING:"; else
if(msgFlags&VK_DEBUG_REPORT_INFORMATION_BIT_EXT) std::cerr<<"[INFO:"; else
if(msgFlags&VK_DEBUG_REPORT_DEBUG_BIT_EXT) std::cerr<<"[DEBUG:";

View File

@@ -38,13 +38,10 @@ void RenderCmdBuffer::SetFBO(Framebuffer *fb)
clear_values[cv_count-1].depthStencil.depth = 1.0f;
clear_values[cv_count-1].depthStencil.stencil = 0;
}
else
else if(clear_values)
{
if(clear_values)
{
hgl_free(clear_values);
clear_values=nullptr;
}
hgl_free(clear_values);
clear_values=nullptr;
}
render_area.offset.x=0;

View File

@@ -3,6 +3,26 @@
#include<hgl/graph/VKMaterialInstance.h>
VK_NAMESPACE_BEGIN
void DescriptorBinding::BindUBO(MaterialParameters *mp,const BindingMap &binding_map,bool dynamic)
{
if (binding_map.GetCount() <= 0)return;
DeviceBuffer* buf = nullptr;
const auto *dp =binding_map.GetDataList();
const uint count =binding_map.GetCount();
for(uint i=0;i<count;i++)
{
buf=GetUBO((*dp)->key);
if(buf)
mp->BindUBO((*dp)->value,buf,dynamic);
++dp;
}
}
bool DescriptorBinding::Bind(Material *mtl)
{
if(!mtl)
@@ -15,47 +35,13 @@ bool DescriptorBinding::Bind(Material *mtl)
const BindingMapArray &bma=mp->GetBindingMap();
const BindingMap &ubo_bm=bma[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER];
const BindingMap &ubo_dynamic_bm=bma[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC];
// const BindingMap &ssbo_bm=bma[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER];
// const BindingMap &ssbo_dynamic_bm=bma[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC];
// const BindingMap &texture_bm=bma[VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER];
DeviceBuffer *buf=nullptr;
if(ubo_bm.GetCount()>0)
{
const auto *dp=ubo_bm.GetDataList();
const uint count=ubo_bm.GetCount();
for(uint i=0;i<count;i++)
{
buf=GetUBO((*dp)->key);
if(buf)
mp->BindUBO((*dp)->value,buf,false);
++dp;
}
}
if(ubo_dynamic_bm.GetCount()>0)
{
const auto *dp=ubo_dynamic_bm.GetDataList();
const uint count=ubo_dynamic_bm.GetCount();
for(uint i=0;i<count;i++)
{
buf=GetUBO((*dp)->key);
if(buf)
mp->BindUBO((*dp)->value,buf,true);
++dp;
}
}
BindUBO(mp,bma[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER],false);
BindUBO(mp,bma[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC],true);
mp->Update();
return(true);

View File

@@ -47,11 +47,6 @@ const bool Material::hasSet(const DescriptorSetType &dst)const
return desc_manager->hasSet(dst);
}
const bool Material::hasAssign()const
{
return vertex_input->hasAssign();
}
const VIL *Material::GetDefaultVIL()const
{
return vertex_input->GetDefaultVIL();

View File

@@ -45,4 +45,12 @@ void VKMemoryAllocator::Flush(const VkDeviceSize size)
{
gpu_buffer->Flush(size);
}
bool VKMemoryAllocator::Write(const void *source,const uint64 offset,const uint64 size)
{
if(!source||size==0)
return(false);
return gpu_buffer->Write(source,offset,size);
}
VK_NAMESPACE_END

View File

@@ -90,6 +90,10 @@ void CheckInstanceLayer(CharPointerList &layer_list,CreateInstanceLayerInfo *lay
VK_LAYER_KHRONOS_ADD(validation)
VK_LAYER_KHRONOS_ADD(profiles)
#define VK_LAYER_AMD_ADD(name) VK_LAYER_CHECK(amd,"AMD",name)
VK_LAYER_AMD_ADD(switchable_graphics)
#define VK_LAYER_NV_ADD(name) VK_LAYER_CHECK(nv,"NV",name)
VK_LAYER_NV_ADD(optimus)

View File

@@ -161,6 +161,8 @@ Texture2DArray *RenderResource::CreateTexture2DArray(const AnsiString &name,cons
if(ta)
Add(ta);
else
return nullptr;
#ifdef _DEBUG
DebugUtils *du=device->GetDebugUtils();

View File

@@ -175,6 +175,7 @@ Material *RenderResource::CreateMaterial(const mtl::MaterialCreateInfo *mci)
Add(mtl);
static_descriptor.Bind(mtl);
global_descriptor.Bind(mtl);
material_by_name.Add(mtl_name,mtl);

View File

@@ -9,9 +9,13 @@ namespace hgl
default_source=fs;
if(fs)
{
fs->RefAcquire(this);
max_char_height=fs->GetCharHeight();
max_char_height=fs->GetCharHeight();
}
else
max_char_height=0;
}
FontSourceMulti::~FontSourceMulti()

View File

@@ -39,6 +39,7 @@ void main()
if(!Std2DMaterial::CustomVertexShader(vsc))
return(false);
vsc->AddMaterialInstanceID();
vsc->SetMain(vs_main);
return(true);
}

View File

@@ -16,10 +16,9 @@ bool Std2DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc)
if(cfg->local_to_world)
{
vsc->AddLocalToWorld();
mci->SetLocalToWorld(VK_SHADER_STAGE_ALL_GRAPHICS);
vsc->AddAssign();
if(is_rect)
vsc->AddFunction(func::GetPosition2DRectL2W[size_t(cfg->coordinate_system)]);
else
@@ -35,8 +34,10 @@ bool Std2DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc)
if(cfg->coordinate_system==CoordinateSystem2D::Ortho)
{
mci->AddStruct(SBS_ViewportInfo);
mci->AddUBO(VK_SHADER_STAGE_ALL_GRAPHICS,
DescriptorSetType::Global,
DescriptorSetType::Static,
SBS_ViewportInfo);
}

View File

@@ -4,6 +4,8 @@
STD_MTL_NAMESPACE_BEGIN
const char *GetUBOCodes(const AccumMemoryManager::Block *block);
namespace
{
class Std2DMaterialLoader:public Std2DMaterial
@@ -36,6 +38,18 @@ namespace
mfd->mi.shader_stage_flag_bits);
}
for(const auto ubo:mfd->ubo_list)
{
const char *ubo_codes=GetUBOCodes(ubo.block);
mci->AddStruct(ubo.struct_name,ubo_codes);
mci->AddUBO(ubo.shader_stage_flag_bits,
ubo.set,
ubo.struct_name,
ubo.name);
}
return true;
}
@@ -59,12 +73,15 @@ namespace
bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override
{
if(!Std2DMaterial::CustomVertexShader(vsc))
return(false);
for(auto &ua:mfd->vi)
vsc->AddInput(ua.vat,ua.name);
if(mfd->mi.mi_bytes>0)
vsc->AddMaterialInstanceID();
if(!Std2DMaterial::CustomVertexShader(vsc))
return(false);
return CommonProc(VK_SHADER_STAGE_VERTEX_BIT,vsc);
}

View File

@@ -24,21 +24,26 @@ bool Std3DMaterial::CustomVertexShader(ShaderCreateInfoVertex *vsc)
{
mci->SetLocalToWorld(VK_SHADER_STAGE_ALL_GRAPHICS);
vsc->AddAssign();
vsc->AddLocalToWorld();
vsc->AddFunction(cfg->camera?func::GetPosition3DL2WCamera:func::GetPosition3DL2W);
}
else
vsc->AddFunction(cfg->camera?func::GetPosition3DCamera:func::GetPosition3D);
//if(cfg->camera
// &&cfg->local_to_world)
//{
// vsc->AddFunction(func::GetNormalMatrix);
// vsc->AddFunction(func::GetNormal);
//}
if(cfg->camera
&&cfg->local_to_world)
{
vsc->AddFunction(func::GetNormalMatrix);
vsc->AddFunction(func::GetNormal);
if(vsc->hasInput(VAN::Normal))
vsc->AddFunction(func::GetNormalVS);
}
mci->AddStruct(SBS_ViewportInfo);
mci->AddUBO(VK_SHADER_STAGE_ALL_GRAPHICS,
DescriptorSetType::Global,
DescriptorSetType::Static,
SBS_ViewportInfo);
return(true);

View File

@@ -1,103 +1,125 @@
#include "MaterialFileData.h"
#include "Std3DMaterial.h"
#include <hgl/shadergen/MaterialCreateInfo.h>
#include"MaterialFileData.h"
#include"Std3DMaterial.h"
#include<hgl/shadergen/MaterialCreateInfo.h>
STD_MTL_NAMESPACE_BEGIN
namespace {
class Std3DMaterialLoader : public Std3DMaterial {
protected:
material_file::MaterialFileData* mfd;
const char *GetUBOCodes(const AccumMemoryManager::Block *block);
public:
Std3DMaterialLoader(material_file::MaterialFileData* data, const Material3DCreateConfig* cfg)
: Std3DMaterial(cfg)
namespace
{
class Std3DMaterialLoader: public Std3DMaterial
{
mfd = data;
}
protected:
~Std3DMaterialLoader()
{
delete mfd;
}
material_file::MaterialFileData *mfd;
bool BeginCustomShader() override
{
if (!Std3DMaterial::BeginCustomShader())
return (false);
public:
if (mfd->mi.mi_bytes > 0) {
mci->SetMaterialInstance(mfd->mi.code,
mfd->mi.mi_bytes,
mfd->mi.shader_stage_flag_bits);
Std3DMaterialLoader(material_file::MaterialFileData *data,const Material3DCreateConfig *cfg)
: Std3DMaterial(cfg)
{
mfd=data;
}
return true;
}
~Std3DMaterialLoader()
{
delete mfd;
}
material_file::ShaderData* CommonProc(VkShaderStageFlagBits ss, ShaderCreateInfo* sc)
{
material_file::ShaderData* sd = mfd->shader[ss];
bool BeginCustomShader() override
{
if(!Std3DMaterial::BeginCustomShader())
return (false);
if (!sd)
return (nullptr);
for(const auto ubo:mfd->ubo_list)
{
const char *ubo_codes=GetUBOCodes(ubo.block);
for (auto& ua : sd->output)
sc->AddOutput(ua.vat, ua.name);
mci->AddStruct(ubo.struct_name,ubo_codes);
for (auto& s : sd->sampler)
mci->AddSampler(ss, DescriptorSetType::PerMaterial, s.type, s.name);
mci->AddUBO(ubo.shader_stage_flag_bits,
ubo.set,
ubo.struct_name,
ubo.name);
}
sc->SetMain(sd->code, sd->code_length); // 这里会产生复制这个string但我不希望产生这个。未来想办法改进
if(mfd->mi.mi_bytes>0)
{
mci->SetMaterialInstance(mfd->mi.code,
mfd->mi.mi_bytes,
mfd->mi.shader_stage_flag_bits);
}
return sd;
}
return true;
}
bool CustomVertexShader(ShaderCreateInfoVertex* vsc) override
{
if (!Std3DMaterial::CustomVertexShader(vsc))
return (false);
material_file::ShaderData *CommonProc(VkShaderStageFlagBits ss,ShaderCreateInfo *sc)
{
material_file::ShaderData *sd=mfd->shader[ss];
for (auto& ua : mfd->vi)
vsc->AddInput(ua.vat, ua.name);
if(!sd)
return (nullptr);
return CommonProc(VK_SHADER_STAGE_VERTEX_BIT, vsc);
}
for(auto &ua:sd->output)
sc->AddOutput(ua.vat,ua.name);
bool CustomGeometryShader(ShaderCreateInfoGeometry* gsc) override
{
material_file::GeometryShaderData* sd = (material_file::GeometryShaderData*)CommonProc(VK_SHADER_STAGE_GEOMETRY_BIT, gsc);
for(auto &s:sd->sampler)
mci->AddSampler(ss,DescriptorSetType::PerMaterial,s.type,s.name);
if (!sd)
return (false);
sc->SetMain(sd->code,sd->code_length); // 这里会产生复制这个string但我不希望产生这个。未来想办法改进
gsc->SetGeom(sd->input_prim, sd->output_prim, sd->max_vertices);
return sd;
}
return true;
}
bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override
{
for(auto &ua:mfd->vi)
vsc->AddInput(ua.vat,ua.name);
bool CustomFragmentShader(ShaderCreateInfoFragment* fsc) override
{
return CommonProc(VK_SHADER_STAGE_FRAGMENT_BIT, fsc);
}
}; // class Std3DMaterialLoader:public Std3DMaterial
if(mfd->mi.mi_bytes>0)
vsc->AddMaterialInstanceID();
if(!Std3DMaterial::CustomVertexShader(vsc))
return (false);
return CommonProc(VK_SHADER_STAGE_VERTEX_BIT,vsc);
}
bool CustomGeometryShader(ShaderCreateInfoGeometry *gsc) override
{
material_file::GeometryShaderData *sd=(material_file::GeometryShaderData *)CommonProc(VK_SHADER_STAGE_GEOMETRY_BIT,gsc);
if(!sd)
return (false);
gsc->SetGeom(sd->input_prim,sd->output_prim,sd->max_vertices);
return true;
}
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
{
return CommonProc(VK_SHADER_STAGE_FRAGMENT_BIT,fsc);
}
}; // class Std3DMaterialLoader:public Std3DMaterial
} // namespace
material_file::MaterialFileData* LoadMaterialDataFromFile(const AnsiString& mtl_filename);
material_file::MaterialFileData *LoadMaterialDataFromFile(const AnsiString &mtl_filename);
MaterialCreateInfo* LoadMaterialFromFile(const AnsiString& name, Material3DCreateConfig* cfg)
MaterialCreateInfo *LoadMaterialFromFile(const AnsiString &name,Material3DCreateConfig *cfg)
{
if (name.IsEmpty() || !cfg)
if(name.IsEmpty()||!cfg)
return (nullptr);
material_file::MaterialFileData* mfd = LoadMaterialDataFromFile(name);
material_file::MaterialFileData *mfd=LoadMaterialDataFromFile(name);
if (!mfd)
if(!mfd)
return nullptr;
cfg->shader_stage_flag_bit = mfd->shader_stage_flag_bit;
cfg->shader_stage_flag_bit=mfd->shader_stage_flag_bit;
Std3DMaterialLoader mtl(mfd, cfg);
Std3DMaterialLoader mtl(mfd,cfg);
return mtl.Create();
}

View File

@@ -36,7 +36,8 @@ SOURCE_GROUP("GLSL Compiler" FILES ${GLSL_COMPILER_SOURCE})
set(STD_MTL_HEADER_PATH ${ROOT_INCLUDE_PATH}/hgl/graph/mtl)
SET(SHADERGEN_COMMON_FILES ${STD_MTL_HEADER_PATH}/UBOCommon.h
SET(SHADERGEN_COMMON_FILES ${STD_MTL_HEADER_PATH}/UniformBuffer.h
${STD_MTL_HEADER_PATH}/UBOCommon.h
${STD_MTL_HEADER_PATH}/SamplerName.h
common/MFCommon.h
common/MFGetPosition.h
@@ -49,6 +50,8 @@ SOURCE_GROUP("Common" FILES ${SHADERGEN_COMMON_FILES})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
SET(BLINN_PHONG_HEADER_FILES ${STD_MTL_HEADER_PATH}/BlinnPhong.h)
SET(STD_MTL_2D_SOURCE_FILES ${STD_MTL_HEADER_PATH}/Material2DCreateConfig.h
2d/Std2DMaterial.h
2d/Std2DMaterial.cpp
@@ -76,6 +79,9 @@ SET(STD_MTL_SOURCE ${STD_MTL_HEADER_PATH}/MaterialConfig.h
MaterialFileLoader.cpp)
SOURCE_GROUP("Standard Material" FILES ${STD_MTL_SOURCE})
SOURCE_GROUP("Standard Material\\Light Model\\Blinn Phong" FILES ${BLINN_PHONG_HEADER_FILES})
SOURCE_GROUP("Standard Material\\2D" FILES ${STD_MTL_2D_SOURCE_FILES})
SOURCE_GROUP("Standard Material\\3D" FILES ${STD_MTL_3D_SOURCE_FILES})
@@ -93,6 +99,7 @@ add_cm_library(ULRE.ShaderGen "ULRE" ${DESC_INFO_HEADER_FILES}
${SHADERGEN_COMMON_FILES}
${SHADER_LIBRARY_FILES}
${STD_MTL_SOURCE}
${BLINN_PHONG_HEADER_FILES}
${STD_MTL_2D_SOURCE_FILES}
${STD_MTL_3D_SOURCE_FILES}
)

View File

@@ -9,12 +9,6 @@ using namespace hgl;
using namespace hgl::graph;
STD_MTL_NAMESPACE_BEGIN
namespace
{
const AnsiString *MF_HandoverMI=nullptr;
}//namespace
MaterialCreateInfo::MaterialCreateInfo(const MaterialCreateConfig *mc)
{
config=mc;
@@ -32,16 +26,6 @@ MaterialCreateInfo::MaterialCreateInfo(const MaterialCreateConfig *mc)
mi_max_count=0;
mi_ubo=nullptr;
}
{
l2w_max_count=hgl_min<uint32_t>(ubo_range/sizeof(Matrix4f),HGL_U16_MAX);
l2w_shader_stage=0;
l2w_ubo=nullptr;
}
if(!MF_HandoverMI)
MF_HandoverMI=LoadShader("HandoverMI");
}
bool MaterialCreateInfo::AddStruct(const AnsiString &struct_name,const AnsiString &codes)
@@ -52,12 +36,12 @@ bool MaterialCreateInfo::AddStruct(const AnsiString &struct_name,const AnsiStrin
return mdi.AddStruct(struct_name,codes);
}
bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const AnsiString &type_name,const AnsiString &name)
bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const AnsiString &struct_name,const AnsiString &name)
{
if(!shader_map.KeyExist(flag_bit))
return(false);
if(!mdi.hasStruct(type_name))
if(!mdi.hasStruct(struct_name))
return(false);
ShaderCreateInfo *sc=shader_map[flag_bit];
@@ -69,7 +53,7 @@ bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const Descr
if(ubo)
{
if(ubo->type!=type_name)
if(ubo->type!=struct_name)
return(false);
ubo->stage_flag|=flag_bit;
@@ -80,13 +64,35 @@ bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const Descr
{
ubo=new UBODescriptor();
ubo->type=type_name;
ubo->type=struct_name;
hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,name);
return sc->sdm->AddUBO(set_type,mdi.AddUBO(flag_bit,set_type,ubo));
}
}
bool MaterialCreateInfo::AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const AnsiString &struct_name,const AnsiString &name)
{
if(flag_bits==0)return(false); //没有任何SHADER用?
if(!mdi.hasStruct(struct_name))
return(false);
uint result=0;
VkShaderStageFlagBits bit;
for(int i=0;i<shader_map.GetCount();i++)
{
shader_map.GetKey(i,bit);
if(flag_bits&bit)
if(AddUBO(bit,set_type,struct_name,name))
++result;
}
return(result==shader_map.GetCount());
}
bool MaterialCreateInfo::AddSampler(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const SamplerType &st,const AnsiString &name)
{
if(!shader_map.KeyExist(flag_bit))
@@ -123,28 +129,6 @@ bool MaterialCreateInfo::AddSampler(const VkShaderStageFlagBits flag_bit,const D
}
}
bool MaterialCreateInfo::AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const ShaderBufferSource &ss)
{
if(flag_bits==0)return(false); //没有任何SHADER用
if(!mdi.hasStruct(ss.struct_name))
mdi.AddStruct(ss.struct_name,ss.codes);
uint result=0;
VkShaderStageFlagBits bit;
for(int i=0;i<shader_map.GetCount();i++)
{
shader_map.GetKey(i,bit);
if(flag_bits&bit)
if(AddUBO(bit,set_type,ss.struct_name,ss.name))
++result;
}
return(result==shader_map.GetCount());
}
namespace
{
UBODescriptor *CreateUBODescriptor(const ShaderBufferSource &sbs,const uint32_t flag_bits)
@@ -214,27 +198,6 @@ bool MaterialCreateInfo::SetLocalToWorld(const uint32_t shader_stage_flag_bits)
{
if(shader_stage_flag_bits==0)return(false);
mdi.AddStruct(SBS_LocalToWorld);
l2w_ubo=CreateUBODescriptor(SBS_LocalToWorld,shader_stage_flag_bits);
mdi.AddUBO(shader_stage_flag_bits,DescriptorSetType::PerFrame,l2w_ubo);
const AnsiString L2W_MAX_COUNT=AnsiString::numberOf(l2w_max_count);
auto *it=shader_map.GetDataList();
for(int i=0;i<shader_map.GetCount();i++)
{
if((*it)->key&shader_stage_flag_bits)
{
(*it)->value->AddDefine("L2W_MAX_COUNT",L2W_MAX_COUNT);
(*it)->value->SetLocalToWorld(l2w_ubo);
}
++it;
}
l2w_shader_stage=shader_stage_flag_bits;
return(true);
@@ -255,12 +218,8 @@ bool MaterialCreateInfo::CreateShader()
return(false);
if(sc->GetShaderStage()<mi_shader_stage)
{
sc->AddMaterialInstanceOutput();
sc->AddFunction(MF_HandoverMI);
}
sc->CreateShader(last);
last=sc;

View File

@@ -1,16 +1,18 @@
#pragma once
#include<hgl/type/Map.h>
#include<hgl/type/StringList.h>
#include<hgl/type/AccumMemoryManager.h>
#include<hgl/graph/VKShaderStage.h>
#include<hgl/graph/VKSamplerType.h>
#include<hgl/graph/VKPrimitiveType.h>
#include<hgl/graph/VKDescriptorSetType.h>
namespace material_file
{
using namespace hgl;
using namespace hgl::graph;
struct UniformAttrib
struct ShaderIOAttrib
{
VAT vat;
@@ -34,16 +36,32 @@ namespace material_file
SamplerType type;
};
struct UBOData
{
char struct_name[SHADER_RESOURCE_NAME_MAX_LENGTH];
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
char filename[HGL_MAX_PATH];
uint32_t shader_stage_flag_bits;
hgl::graph::DescriptorSetType set;
AccumMemoryManager::Block *block;
};
using UBODataList=List<UBOData>;
struct ShaderData
{
VkShaderStageFlagBits shader_stage;
VkShaderStageFlagBits shader_stage;
const char *code;
uint code_length;
const char * code;
uint code_length;
List<UniformAttrib> output;
List<ShaderIOAttrib> output;
List<SamplerData> sampler;
List<SamplerData> sampler;
public:
@@ -89,7 +107,9 @@ namespace material_file
MaterialInstanceData mi{};
List<UniformAttrib> vi; ///<Vertex Input
List<ShaderIOAttrib> vi; ///<Vertex Input
UBODataList ubo_list;
ShaderDataMap shader;

View File

@@ -7,6 +7,9 @@
#include<hgl/io/TextInputStream.h>
#include<hgl/filesystem/FileSystem.h>
#include<hgl/type/AccumMemoryManager.h>
#include<hgl/io/FileAccess.h>
#include"MaterialFileData.h"
STD_MTL_NAMESPACE_BEGIN
@@ -63,19 +66,142 @@ namespace
return MaterialFileBlock::None;
}
const uint32_t ShaderStageParse(char *text,const char *ep)
{
uint32_t shader_stage_flag_bits=0;
char *sp;
while(*text==' '||*text=='\t')++text;
while(text<ep)
{
sp=text;
while(hgl::isalpha(*text))
++text;
shader_stage_flag_bits|=GetShaderStageFlagBits(sp,text-sp);
++text;
}
return shader_stage_flag_bits;
}
int ClipCodeString(char *str,const int max_len,const char *text)
{
while(*text==' '||*text=='\t')++text;
const char *sp=text;
while(hgl::iscodechar(*text))++text;
hgl::strcpy(str,max_len,sp,text-sp);
return text-sp;
}
int ClipFilename(char *str,const int max_len,const char *text)
{
while(*text==' '||*text=='\t')++text;
const char *sp=text;
while(hgl::isfilenamechar(*text))++text;
hgl::strcpy(str,max_len,sp,text-sp);
return text-sp;
}
struct UBOParse:public TextParse
{
UBOData ubo_data;
public:
UBOParse()=default;
void Clear()
{
hgl_zero(ubo_data);
}
bool OnLine(char *text,const int len) override
{
if(!text||!*text||len<=0)
return(false);
if(*text=='{')
{
++text;
while(*text=='\r'||*text=='\n')++text;
return(false);
}
if(*text=='}')
{
*text=0;
return(true);
}
while(*text==' '||*text=='\t')++text;
if(text[0]=='/'&&text[1]=='/')
return(false);
if(hgl::stricmp(text,"File ",5)==0)
{
ClipFilename(ubo_data.filename,sizeof(ubo_data.filename),text+5);
}
else
if(hgl::stricmp(text,"Struct ",7)==0)
{
ClipCodeString(ubo_data.struct_name,sizeof(ubo_data.struct_name),text+7);
}
else
if(hgl::stricmp(text,"Name ",5)==0)
{
ClipCodeString(ubo_data.name,sizeof(ubo_data.name),text+5);
}
else
if(hgl::stricmp(text,"Stage ",6)==0)
{
ubo_data.shader_stage_flag_bits=ShaderStageParse(text+6,text+len);
}
else
if(hgl::stricmp(text,"Set ",4)==0)
{
char set_name[32];
ClipCodeString(set_name,sizeof(set_name),text+4);
ubo_data.set=GetDescriptorSetType(set_name);
}
return(false);
}
};//struct UBOParse
struct MaterialBlockParse:public TextParse
{
MaterialFileBlock state;
AnsiStringList *require_list=nullptr;
UBODataList *ubo_list=nullptr;
bool ubo=false;
UBOParse ubo_parse;
public:
MaterialBlockParse(AnsiStringList *asl)
MaterialBlockParse(AnsiStringList *asl,UBODataList *udl)
{
state=MaterialFileBlock::None;
require_list=asl;
ubo_list=udl;
}
bool OnLine(char *text,const int len) override
@@ -85,6 +211,19 @@ namespace
char *ep=text+len;
if(ubo)
{
if(ubo_parse.OnLine(text,len))
{
ubo_list->Add(ubo_parse.ubo_data);
ubo_parse.Clear();
ubo=false;
}
return(true);
}
if(hgl::stricmp(text,"Require ",8)==0)
{
text+=8;
@@ -102,6 +241,11 @@ namespace
sp=text;
}
}
else
if(hgl::stricmp(text,"UBO",3)==0)
{
ubo=true;
}
return(true);
}
@@ -195,31 +339,14 @@ namespace
else
if(hgl::stricmp(text,"Stage ",6)==0)
{
const char *ep=text+len;
const char *sp;
text+=6;
while(*text==' '||*text=='\t')++text;
while(text<ep)
{
sp=text;
while(hgl::isalpha(*text))
++text;
shader_stage_flag_bits|=GetShaderStageFlagBits(sp,text-sp);
++text;
}
shader_stage_flag_bits=ShaderStageParse(text+6,text+len);
}
return(true);
}
};//struct MaterialInstanceBlockParse
bool ParseUniformAttrib(UniformAttrib *ua,const char *str)
bool ParseUniformAttrib(ShaderIOAttrib *ua,const char *str)
{
const char *sp;
@@ -242,11 +369,11 @@ namespace
struct VertexInputBlockParse:public TextParse
{
List<UniformAttrib> *vi_list=nullptr;
List<ShaderIOAttrib> *vi_list=nullptr;
public:
VertexInputBlockParse(List<UniformAttrib> *ual)
VertexInputBlockParse(List<ShaderIOAttrib> *ual)
{
vi_list=ual;
}
@@ -256,7 +383,7 @@ namespace
if(!text||!*text||len<=0)
return(false);
UniformAttrib ua;
ShaderIOAttrib ua;
if(ParseUniformAttrib(&ua,text))
vi_list->Add(ua);
@@ -307,7 +434,7 @@ namespace
return(true);
}
UniformAttrib ua;
ShaderIOAttrib ua;
if(ParseUniformAttrib(&ua,text))
shader_data->output.Add(ua);
@@ -461,7 +588,7 @@ namespace
state=GetMaterialFileState(text+1,len-1);
if(state==MaterialFileBlock::Material)
parse=new MaterialBlockParse(&(mfd->require_list));
parse=new MaterialBlockParse(&(mfd->require_list),&(mfd->ubo_list));
else
if(state==MaterialFileBlock::MaterialInstance)
parse=new MaterialInstanceBlockParse(&(mfd->mi));
@@ -519,11 +646,63 @@ namespace
};
}//namespace MaterialFile
namespace
{
constexpr const os_char HGL_SHADER_LIBRARY_FOLDER[]=OS_TEXT("ShaderLibrary");
AccumMemoryManager ubo_memory;
hgl::Map<OSString,AccumMemoryManager::Block *> ubo_codes_map;
AccumMemoryManager::Block *LoadUBO2Block(const OSString &filename)
{
AccumMemoryManager::Block *block;
if(ubo_codes_map.Get(filename,block))
return block;
hgl::io::FileAccess fa;
if(!fa.OpenRead(filename))
{
ubo_codes_map.Add(filename,nullptr);
return(nullptr);
}
const int64 size=fa.GetSize();
block=ubo_memory.Acquire(size+1);
char *ptr=(char *)ubo_memory.Access(block);
fa.Read(ptr,size);
ptr[size]=0;
ptr+=size-1;
while(*ptr=='\r'||*ptr=='\n')
{
*ptr=0;
--ptr;
}
ubo_codes_map.Add(filename,block);
return block;
}
}//namespace
const char *GetUBOCodes(const AccumMemoryManager::Block *block)
{
if(!block)return nullptr;
return (const char *)ubo_memory.Access(block);
}
MaterialFileData *LoadMaterialDataFromFile(const AnsiString &mtl_filename)
{
const OSString mtl_osfn=ToOSString(mtl_filename+".mtl");
const OSString mtl_osfn=filesystem::FixFilename(ToOSString(mtl_filename+".mtl"));
const OSString mtl_os_filename=filesystem::MergeFilename(OS_TEXT("ShaderLibrary"),mtl_osfn);
const OSString mtl_os_filename=filesystem::MergeFilename(HGL_SHADER_LIBRARY_FOLDER,mtl_osfn);
if(!filesystem::FileExist(mtl_os_filename))
return nullptr;
@@ -543,6 +722,32 @@ MaterialFileData *LoadMaterialDataFromFile(const AnsiString &mtl_filename)
if(!tis.Run())
return nullptr;
if(mfd->ubo_list.GetCount()<=0)
return mfd;
const OSString mtl_path=filesystem::ClipPathname(mtl_os_filename,false);
OSString ubo_os_fn;
OSString ubo_os_full_filename;
for(UBOData &ud:mfd->ubo_list)
{
ubo_os_fn=filesystem::FixFilename(ToOSString(ud.filename));
if(ubo_os_fn.GetFirstChar()==HGL_DIRECTORY_SEPARATOR)
ubo_os_full_filename=filesystem::MergeFilename(HGL_SHADER_LIBRARY_FOLDER,ubo_os_fn);
else
ubo_os_full_filename=filesystem::MergeFilename(mtl_path,ubo_os_fn);
ud.block=LoadUBO2Block(ubo_os_full_filename);
if(!ud.block)
{
delete mfd;
return nullptr;
}
}
return mfd;
}
STD_MTL_NAMESPACE_END

View File

@@ -1,4 +1,4 @@
#include<hgl/shadergen/ShaderCreateInfo.h>
#include<hgl/shadergen/ShaderCreateInfo.h>
#include<hgl/shadergen/ShaderDescriptorInfo.h>
#include<hgl/graph/mtl/UBOCommon.h>
#include<hgl/log/LogInfo.h>
@@ -9,12 +9,6 @@
namespace hgl{namespace graph{
namespace
{
const AnsiString *ShaderFileHeader=nullptr;
const AnsiString *MF_GetMI=nullptr;
}//namespace
ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorInfo *m)
{
shader_stage=ss;
@@ -25,9 +19,6 @@ ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorIn
define_macro_max_length=0;
define_value_max_length=0;
if(!ShaderFileHeader)ShaderFileHeader =mtl::LoadShader("ShaderHeader");
if(!MF_GetMI )MF_GetMI =mtl::LoadShader("GetMI");
}
ShaderCreateInfo::~ShaderCreateInfo()
@@ -163,12 +154,22 @@ bool ShaderCreateInfo::ProcSubpassInput()
return(true);
}
namespace
{
constexpr const char MF_GetMI_VS []="\nMaterialInstance GetMI(){return mtl.mi[MaterialInstanceID];}\n";
constexpr const char MF_GetMI_Other []="\nMaterialInstance GetMI(){return mtl.mi[Input.MaterialInstanceID];}\n";
constexpr const char MF_HandoverMI_VS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=MaterialInstanceID;}\n";
constexpr const char MF_HandoverMI_GS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input[0].MaterialInstanceID;}\n";
constexpr const char MF_HandoverMI_OTHER[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input.MaterialInstanceID;}\n";
}//namespace
void ShaderCreateInfo::SetMaterialInstance(UBODescriptor *ubo,const AnsiString &mi)
{
sdm->AddUBO(DescriptorSetType::PerMaterial,ubo);
sdm->AddStruct(mtl::MaterialInstanceStruct);
AddFunction(MF_GetMI);
AddFunction(shader_stage==VK_SHADER_STAGE_VERTEX_BIT?MF_GetMI_VS:MF_GetMI_Other);
mi_codes=mi;
}
@@ -176,11 +177,10 @@ void ShaderCreateInfo::SetMaterialInstance(UBODescriptor *ubo,const AnsiString &
void ShaderCreateInfo::AddMaterialInstanceOutput()
{
AddOutput(VAT_UINT,mtl::func::MaterialInstanceID,Interpolation::Flat);
}
void ShaderCreateInfo::SetLocalToWorld(UBODescriptor *ubo)
{
sdm->AddUBO(DescriptorSetType::PerFrame,ubo);
if(shader_stage==VK_SHADER_STAGE_VERTEX_BIT) AddFunction(MF_HandoverMI_VS);else
if(shader_stage==VK_SHADER_STAGE_GEOMETRY_BIT) AddFunction(MF_HandoverMI_GS);else
AddFunction(MF_HandoverMI_OTHER);
}
bool ShaderCreateInfo::ProcInput(ShaderCreateInfo *last_sc)
@@ -386,13 +386,21 @@ bool ShaderCreateInfo::ProcSampler()
bool ShaderCreateInfo::CreateShader(ShaderCreateInfo *last_sc)
{
if(!ShaderFileHeader)
return(false);
if(main_function.IsEmpty())
return(false);
final_shader=ShaderFileHeader->c_str();
final_shader=R"(
#version 460 core
#define VertexShader 0x01
#define TessControlShader 0x02
#define TeseEvalShader 0x04
#define GeometryShader 0x08
#define FragmentShader 0x10
#define ComputeShader 0x20
#define TaskShader 0x40
#define MeshShader 0x80
)";
{
char ss_hex_str[9];
@@ -430,10 +438,12 @@ bool ShaderCreateInfo::CreateShader(ShaderCreateInfo *last_sc)
for(const char *str:function_list)
final_shader+=str;
final_shader+="\n";
final_shader+=main_function;
#ifdef _DEBUG
//想办法存成文件或是输出行号,以方便出错了调试
LOG_INFO(AnsiString(GetShaderStageName(shader_stage))+" shader: \n"+final_shader);
#endif//_DEBUG

View File

@@ -8,14 +8,8 @@
#include"ShaderLibrary.h"
VK_NAMESPACE_BEGIN
namespace
{
const AnsiString *MF_GetLocalToWorld=nullptr;
}//namespace
ShaderCreateInfoVertex::ShaderCreateInfoVertex(MaterialDescriptorInfo *m):ShaderCreateInfo(VK_SHADER_STAGE_VERTEX_BIT,m)
{
if(!MF_GetLocalToWorld)MF_GetLocalToWorld=mtl::LoadShader("GetLocalToWorld");
}
int ShaderCreateInfoVertex::AddInput(const VAT &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group)
@@ -43,22 +37,52 @@ int ShaderCreateInfoVertex::AddInput(const AnsiString &type,const AnsiString &na
return AddInput(vat,name,input_rate,group);
}
int ShaderCreateInfoVertex::hasInput(const char *name)
{
return sdm->hasInput(name);
}
void ShaderCreateInfoVertex::AddJoint()
{
AddInput(VAT_UVEC4, VAN::JointID, VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::JointID);
AddInput(VAT_VEC4, VAN::JointWeight,VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::JointWeight);
}
void ShaderCreateInfoVertex::AddAssign()
namespace
{
AddInput( ASSIGN_VAT_FMT,
ASSIGN_VIS_NAME,
VK_VERTEX_INPUT_RATE_INSTANCE,
VertexInputGroup::Assign);
constexpr const char MF_GetLocalToWorld[]=R"(
mat4 GetLocalToWorld()
{
return mat4(LocalToWorld_0,
LocalToWorld_1,
LocalToWorld_2,
LocalToWorld_3);
}
)";
}
void ShaderCreateInfoVertex::AddLocalToWorld()
{
char name[]= "LocalToWorld_?";
for(uint i=0;i<4;i++)
{
name[sizeof(name)-2]='0'+i;
AddInput(VAT_VEC4,name,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::LocalToWorld);
}
AddFunction(MF_GetLocalToWorld);
}
void ShaderCreateInfoVertex::AddMaterialInstanceID()
{
AddInput( MI_VAT_FMT,
MI_VIS_NAME,
VK_VERTEX_INPUT_RATE_INSTANCE,
VertexInputGroup::MaterialInstanceID);
}
bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *)
{
const auto &input=sdm->GetShaderStageIO().input;

View File

@@ -12,13 +12,13 @@ ShaderDescriptorInfo::ShaderDescriptorInfo(VkShaderStageFlagBits flag_bit)
namespace
{
bool Find(ShaderAttributeArray &sad,const ShaderAttribute *ss)
bool Find(const ShaderAttributeArray &sad,const char *name)
{
if(sad.count<=0)
return(false);
for(uint i=0;i<sad.count;i++)
if(hgl::strcmp(sad.items[i].name,ss->name)==0)
if(hgl::strcmp(sad.items[i].name,name)==0)
return(true);
return(false);
@@ -29,7 +29,7 @@ bool ShaderDescriptorInfo::AddInput(ShaderAttribute *ss)
{
if(!ss)return(false);
if(Find(stage_io.input,ss))return(false);
if(Find(stage_io.input,ss->name))return(false);
ss->location=stage_io.input.count;
@@ -37,11 +37,20 @@ bool ShaderDescriptorInfo::AddInput(ShaderAttribute *ss)
return(true);
}
bool ShaderDescriptorInfo::hasInput(const char *name)const
{
if(!name||!*name)return(false);
return Find(stage_io.input,name);
}
bool ShaderDescriptorInfo::AddOutput(ShaderAttribute *ss)
{
if(!ss)return(false);
if(Find(stage_io.output,ss))return(false);
if(Find(stage_io.output,ss->name))return(false);
ss->location=stage_io.output.count;

View File

@@ -9,5 +9,14 @@ namespace func
constexpr const char MaterialInstanceID[]="MaterialInstanceID";
constexpr const char GetLocalToWorld[]=R"(
mat4 GetLocalToWorld()
{
return mat4(LocalToWorld_0,
LocalToWorld_1,
LocalToWorld_2,
LocalToWorld_3);
}
)";
}//namespace func
STD_MTL_NAMESPACE_END

View File

@@ -17,6 +17,13 @@ vec3 GetNormal(mat3 normal_matrix,vec3 normal)
{
return normalize(normal_matrix*normal);
}
)";
constexpr const char *GetNormalVS=R"(
vec3 GetNormal()
{
return normalize(mat3(camera.view*GetLocalToWorld())*Normal);
}
)";
}//namespace func
STD_MTL_NAMESPACE_END