to advance ShaderResource/VKBuffer

This commit is contained in:
hyzboy 2020-07-09 20:37:34 +08:00
parent f76235ef5c
commit 967c66569b
10 changed files with 88 additions and 61 deletions

@ -1 +1 @@
Subproject commit fccbd425e23f0b82f2953313fd8bacf7a01c5379 Subproject commit c4f5e3d339dac74cfb48f7cf17ea18e51990c709

View File

@ -58,7 +58,7 @@ private:
{ {
ro_sphere=CreateRenderableSphere(db,material,128); ro_sphere=CreateRenderableSphere(db,material,128);
} }
bool InitAtomsphereUBO(vulkan::MaterialInstance *mi,const AnsiString &sun_node_name) bool InitAtomsphereUBO(vulkan::MaterialInstance *mi,const AnsiString &sun_node_name)
{ {
atomsphere_data.position.Set(0,0.1f,-1.0f); atomsphere_data.position.Set(0,0.1f,-1.0f);

View File

@ -1,6 +1,3 @@
// 2.RectanglePrimivate
// 该示例是texture_rect的进化演示使用GeometryShader画矩形
#include"VulkanAppFramework.h" #include"VulkanAppFramework.h"
#include<hgl/graph/vulkan/VKTexture.h> #include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/graph/vulkan/VKSampler.h> #include<hgl/graph/vulkan/VKSampler.h>

View File

@ -36,8 +36,8 @@ namespace hgl
} }
virtual VkFormat GetDataType ()const=0; ///<取得数据类型 virtual VkFormat GetDataType ()const=0; ///<取得数据类型
// const uint32_t GetDataBytes ()const{return comp_stride;} ///<取得每数据字节数 const uint32_t GetDataBytes ()const{return comp_stride;} ///<取得每数据字节数
// const uint32_t GetComponent ()const{return dc_num; } ///<取数缓冲区元数据数量 const uint32_t GetComponent ()const{return dc_num; } ///<取数缓冲区元数据数量
const uint32_t GetCount ()const{return count; } ///<取得数据数量 const uint32_t GetCount ()const{return count; } ///<取得数据数量
const uint32_t GetStride ()const{return stride;} ///<取得每一组数据字节数 const uint32_t GetStride ()const{return stride;} ///<取得每一组数据字节数
void * GetData ()const{return mem_data;} ///<取得数据指针 void * GetData ()const{return mem_data;} ///<取得数据指针

View File

@ -9,6 +9,7 @@ VK_NAMESPACE_BEGIN
{ {
AnsiString name; AnsiString name;
uint location; uint location;
uint component;
VkFormat format; VkFormat format;
};//struct ShaderStage };//struct ShaderStage
@ -60,7 +61,8 @@ VK_NAMESPACE_BEGIN
ShaderStageList &GetStageInputs(){return stage_inputs;} ShaderStageList &GetStageInputs(){return stage_inputs;}
ShaderStageList &GetStageOutputs(){return stage_outputs;} ShaderStageList &GetStageOutputs(){return stage_outputs;}
const int GetStageInputBinding(const AnsiString &); const ShaderStage *GetStage(const AnsiString &)const;
const int GetStageInputBinding(const AnsiString &)const;
const ShaderDescriptorList *GetDescriptorList()const{return descriptor_list;} const ShaderDescriptorList *GetDescriptorList()const{return descriptor_list;}
ShaderDescriptorList *GetDescriptorList(VkDescriptorType desc_type) ShaderDescriptorList *GetDescriptorList(VkDescriptorType desc_type)

View File

@ -72,6 +72,7 @@ public:
* binding并非是shader中的binding/locationvkCmdBindVertexBuffer的缓冲区序列号 * binding并非是shader中的binding/locationvkCmdBindVertexBuffer的缓冲区序列号
*/ */
const int GetStageInputBinding(const AnsiString &name)const{return shader_resource->GetStageInputBinding(name);} const int GetStageInputBinding(const AnsiString &name)const{return shader_resource->GetStageInputBinding(name);}
const ShaderStage * GetStage(const AnsiString &name)const{return shader_resource->GetStage(name);}
const uint32_t GetAttrCount()const{return attr_count;} const uint32_t GetAttrCount()const{return attr_count;}

View File

@ -1,4 +1,6 @@
#include<hgl/graph/vulkan/VKBuffer.h> #include<hgl/graph/vulkan/VKBuffer.h>
#include<hgl/graph/VertexBuffer.h>
#include<spirv_cross/spirv_common.hpp>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Buffer::~Buffer() Buffer::~Buffer()
@ -6,4 +8,14 @@ Buffer::~Buffer()
if(buf.memory)delete buf.memory; if(buf.memory)delete buf.memory;
vkDestroyBuffer(device,buf.buffer,nullptr); vkDestroyBuffer(device,buf.buffer,nullptr);
} }
VertexBufferCreater *CreateVBO(const uint32_t base_type,const uint32_t vecsize,const uint32_t vertex_count)
{
if(vecsize==1)return(new VB1f(vertex_count));else
if(vecsize==2)return(new VB2f(vertex_count));else
if(vecsize==3)return(new VB3f(vertex_count));else
if(vecsize==4)return(new VB4f(vertex_count));else
}
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -21,13 +21,13 @@ namespace hgl
const vulkan::VertexShaderModule *vsm=nullptr; const vulkan::VertexShaderModule *vsm=nullptr;
vulkan::Renderable *render_obj=nullptr; vulkan::Renderable *render_obj=nullptr;
int vertex_binding =-1; int vertex_binding =-1;
int color_binding =-1; int color_binding =-1;
int normal_binding =-1; int normal_binding =-1;
int tangent_binding =-1; int tangent_binding =-1;
int texcoord_binding=-1; int texcoord_binding=-1;
VERTEX_VB_FORMAT *vertex =nullptr; VERTEX_VB_FORMAT *vertex =nullptr;
VB4f *color =nullptr; VB4f *color =nullptr;
VB3f *normal =nullptr; VB3f *normal =nullptr;
@ -41,7 +41,7 @@ namespace hgl
GeometryCreater(SceneDB *sdb,vulkan::Material *m):db(sdb),mtl(m) GeometryCreater(SceneDB *sdb,vulkan::Material *m):db(sdb),mtl(m)
{ {
vsm=mtl->GetVertexShaderModule(); vsm=mtl->GetVertexShaderModule();
vertex_binding =vsm->GetStageInputBinding("Vertex"); vertex_binding =vsm->GetStageInputBinding("Vertex");
color_binding =vsm->GetStageInputBinding("Color"); color_binding =vsm->GetStageInputBinding("Color");
normal_binding =vsm->GetStageInputBinding("Normal"); normal_binding =vsm->GetStageInputBinding("Normal");
@ -102,7 +102,7 @@ namespace hgl
} }
private: private:
void Finish(int binding,VertexBufferCreater *vb) void Finish(int binding,VertexBufferCreater *vb)
{ {
render_obj->Set(binding,db->CreateVBO(vb)); render_obj->Set(binding,db->CreateVBO(vb));
@ -125,11 +125,11 @@ namespace hgl
if(tex_coord)Finish(texcoord_binding, tex_coord); if(tex_coord)Finish(texcoord_binding, tex_coord);
if(ibo) if(ibo)
{ {
ibo->Unmap(); ibo->Unmap();
render_obj->Set(ibo); render_obj->Set(ibo);
} }
vulkan::Renderable *result=render_obj; vulkan::Renderable *result=render_obj;
db->Add(render_obj); db->Add(render_obj);
@ -148,7 +148,7 @@ namespace hgl
if(!gc.Init(4)) if(!gc.Init(4))
return(nullptr); return(nullptr);
VB2f *vertex=gc.GetVertex(); VB2f *vertex=gc.GetVertex();
vertex->WriteRectFan(rci->scope); vertex->WriteRectFan(rci->scope);
@ -173,7 +173,7 @@ namespace hgl
{ {
if(!gc.Init(4)) if(!gc.Init(4))
return(nullptr); return(nullptr);
VB2f *vertex=gc.GetVertex(); VB2f *vertex=gc.GetVertex();
vertex->WriteRectFan(rci->scope); vertex->WriteRectFan(rci->scope);
@ -187,7 +187,7 @@ namespace hgl
if(!gc.Init(rci->round_per*4)) if(!gc.Init(rci->round_per*4))
return(nullptr); return(nullptr);
VB2f *vertex=gc.GetVertex(); VB2f *vertex=gc.GetVertex();
vec2<float> *coord=new vec2<float>[rci->round_per]; vec2<float> *coord=new vec2<float>[rci->round_per];
@ -235,7 +235,7 @@ namespace hgl
delete[] coord; delete[] coord;
} }
return gc.Finish(); return gc.Finish();
} }
@ -254,7 +254,7 @@ namespace hgl
float x=cci->center.x+sin(hgl_ang2rad(ang))*cci->radius.x; float x=cci->center.x+sin(hgl_ang2rad(ang))*cci->radius.x;
float y=cci->center.y+cos(hgl_ang2rad(ang))*cci->radius.y; float y=cci->center.y+cos(hgl_ang2rad(ang))*cci->radius.y;
vertex->Write(x,y); vertex->Write(x,y);
} }
@ -341,7 +341,7 @@ namespace hgl
{ {
xy_tex_coord[2]=xy_tex_coord[4]=pci->tile.x; xy_tex_coord[2]=xy_tex_coord[4]=pci->tile.x;
xy_tex_coord[5]=xy_tex_coord[7]=pci->tile.y; xy_tex_coord[5]=xy_tex_coord[7]=pci->tile.y;
tex_coord->BufferData(xy_tex_coord); tex_coord->BufferData(xy_tex_coord);
} }
} }
@ -418,10 +418,10 @@ namespace hgl
} }
} }
gc.CreateIBO16(6*2*3,indices); gc.CreateIBO16(6*2*3,indices);
return gc.Finish(); return gc.Finish();
} }
template<typename T> void CreateSphereIndices(T *tp,uint numberParallels,const uint numberSlices) template<typename T> void CreateSphereIndices(T *tp,uint numberParallels,const uint numberSlices)
{ {
for (uint i = 0; i < numberParallels; i++) for (uint i = 0; i < numberParallels; i++)
@ -440,7 +440,7 @@ namespace hgl
} }
namespace namespace
{ {
constexpr uint GLUS_VERTICES_FACTOR =4; constexpr uint GLUS_VERTICES_FACTOR =4;
constexpr uint GLUS_VERTICES_DIVISOR=4; constexpr uint GLUS_VERTICES_DIVISOR=4;
constexpr uint GLUS_MAX_VERTICES =1048576; constexpr uint GLUS_MAX_VERTICES =1048576;
@ -465,7 +465,7 @@ namespace hgl
quaternion[2] = sinf(halfAngleRadian); quaternion[2] = sinf(halfAngleRadian);
quaternion[3] = cosf(halfAngleRadian); quaternion[3] = cosf(halfAngleRadian);
} }
void glusQuaternionGetMatrix4x4f(float matrix[16], const float quaternion[4]) void glusQuaternionGetMatrix4x4f(float matrix[16], const float quaternion[4])
{ {
float x = quaternion[0]; float x = quaternion[0];
@ -493,7 +493,7 @@ namespace hgl
matrix[14] = 0.0f; matrix[14] = 0.0f;
matrix[15] = 1.0f; matrix[15] = 1.0f;
} }
void glusMatrix4x4MultiplyVector3f(float result[3], const float matrix[16], const float vector[3]) void glusMatrix4x4MultiplyVector3f(float result[3], const float matrix[16], const float vector[3])
{ {
int i; int i;
@ -535,7 +535,7 @@ namespace hgl
if(!gc.Init(numberVertices)) if(!gc.Init(numberVertices))
return(nullptr); return(nullptr);
float *vp=gc.GetVertexPointer(); float *vp=gc.GetVertexPointer();
float *np=gc.GetNormalPointer(); float *np=gc.GetNormalPointer();
float *tp=gc.GetTangentPointer(); float *tp=gc.GetTangentPointer();
@ -548,7 +548,7 @@ namespace hgl
float x = sin(angleStep * (double) i) * sin(angleStep * (double) j); float x = sin(angleStep * (double) i) * sin(angleStep * (double) j);
float y = sin(angleStep * (double) i) * cos(angleStep * (double) j); float y = sin(angleStep * (double) i) * cos(angleStep * (double) j);
float z = cos(angleStep * (double) i); float z = cos(angleStep * (double) i);
*vp=x;++vp; *vp=x;++vp;
*vp=y;++vp; *vp=y;++vp;
*vp=z;++vp; *vp=z;++vp;
@ -566,7 +566,7 @@ namespace hgl
*tc=tex_x;++tc; *tc=tex_x;++tc;
*tc=1.0f - (float) i / (float) numberParallels;++tc; *tc=1.0f - (float) i / (float) numberParallels;++tc;
if(tp) if(tp)
{ {
// use quaternion to get the tangent vector // use quaternion to get the tangent vector
@ -579,7 +579,7 @@ namespace hgl
} }
} }
} }
if(numberVertices<=0xffff) if(numberVertices<=0xffff)
CreateSphereIndices<uint16>(gc.CreateIBO16(numberIndices),numberParallels,numberSlices); CreateSphereIndices<uint16>(gc.CreateIBO16(numberIndices),numberParallels,numberSlices);
else else
@ -587,7 +587,7 @@ namespace hgl
return gc.Finish(); return gc.Finish();
} }
vulkan::Renderable *CreateRenderableDome(SceneDB *db,vulkan::Material *mtl,const DomeCreateInfo *dci) vulkan::Renderable *CreateRenderableDome(SceneDB *db,vulkan::Material *mtl,const DomeCreateInfo *dci)
{ {
GeometryCreater3D gc(db,mtl); GeometryCreater3D gc(db,mtl);
@ -608,10 +608,10 @@ namespace hgl
if (dci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) if (dci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES)
return nullptr; return nullptr;
if(!gc.Init(numberVertices)) if(!gc.Init(numberVertices))
return(nullptr); return(nullptr);
float *vp=gc.GetVertexPointer(); float *vp=gc.GetVertexPointer();
float *np=gc.GetNormalPointer(); float *np=gc.GetNormalPointer();
float *tp=gc.GetTangentPointer(); float *tp=gc.GetTangentPointer();
@ -658,7 +658,7 @@ namespace hgl
} }
} }
} }
if(numberVertices<=0xffff) if(numberVertices<=0xffff)
CreateSphereIndices<uint16>(gc.CreateIBO16(numberIndices),numberParallels,dci->numberSlices); CreateSphereIndices<uint16>(gc.CreateIBO16(numberIndices),numberParallels,dci->numberSlices);
else else
@ -677,7 +677,7 @@ namespace hgl
// used to generate the indices // used to generate the indices
uint v0, v1, v2, v3; uint v0, v1, v2, v3;
for (sideCount = 0; sideCount < numberSlices; ++sideCount) for (sideCount = 0; sideCount < numberSlices; ++sideCount)
{ {
for (faceCount = 0; faceCount < numberStacks; ++faceCount) for (faceCount = 0; faceCount < numberStacks; ++faceCount)
@ -741,7 +741,7 @@ namespace hgl
if(!gc.Init(numberVertices)) if(!gc.Init(numberVertices))
return(nullptr); return(nullptr);
float *vp=gc.GetVertexPointer(); float *vp=gc.GetVertexPointer();
float *np=gc.GetNormalPointer(); float *np=gc.GetNormalPointer();
float *tp=gc.GetTangentPointer(); float *tp=gc.GetTangentPointer();
@ -794,7 +794,7 @@ namespace hgl
} }
} }
} }
if(numberVertices<=0xffff) if(numberVertices<=0xffff)
CreateTorusIndices<uint16>(gc.CreateIBO16(numberIndices),tci->numberSlices,tci->numberStacks); CreateTorusIndices<uint16>(gc.CreateIBO16(numberIndices),tci->numberSlices,tci->numberStacks);
else else
@ -871,7 +871,7 @@ namespace hgl
if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES) if (cci->numberSlices < 3 || numberVertices > GLUS_MAX_VERTICES || numberIndices > GLUS_MAX_INDICES)
return nullptr; return nullptr;
float *vp=gc.GetVertexPointer(); float *vp=gc.GetVertexPointer();
float *np=gc.GetNormalPointer(); float *np=gc.GetNormalPointer();
float *tp=gc.GetTangentPointer(); float *tp=gc.GetTangentPointer();
@ -900,7 +900,7 @@ namespace hgl
*tc = 0.0f; ++tc; *tc = 0.0f; ++tc;
*tc = 0.0f; ++tc; *tc = 0.0f; ++tc;
} }
for(uint i = 0; i < cci->numberSlices + 1; i++) for(uint i = 0; i < cci->numberSlices + 1; i++)
{ {
float currentAngle = angleStep * (float)i; float currentAngle = angleStep * (float)i;
@ -929,7 +929,7 @@ namespace hgl
*tc = 0.0f; ++tc; *tc = 0.0f; ++tc;
} }
} }
*vp = 0.0f; ++vp; *vp = 0.0f; ++vp;
*vp = 0.0f; ++vp; *vp = 0.0f; ++vp;
*vp = cci->halfExtend; ++vp; *vp = cci->halfExtend; ++vp;
@ -982,7 +982,7 @@ namespace hgl
*tc = 1.0f; ++tc; *tc = 1.0f; ++tc;
} }
} }
for(uint i = 0; i < cci->numberSlices + 1; i++) for(uint i = 0; i < cci->numberSlices + 1; i++)
{ {
float currentAngle = angleStep * (float)i; float currentAngle = angleStep * (float)i;
@ -1018,7 +1018,7 @@ namespace hgl
sign = 1.0f; sign = 1.0f;
} }
} }
if(numberVertices<=0xffff) if(numberVertices<=0xffff)
CreateCylinderIndices<uint16>(gc.CreateIBO16(numberIndices),cci->numberSlices); CreateCylinderIndices<uint16>(gc.CreateIBO16(numberIndices),cci->numberSlices);
else else
@ -1055,7 +1055,7 @@ namespace hgl
*tp = indexCounter; ++tp; *tp = indexCounter; ++tp;
*tp = indexCounter + numberSlices + 1; ++tp; *tp = indexCounter + numberSlices + 1; ++tp;
*tp = indexCounter + 1; ++tp; *tp = indexCounter + 1; ++tp;
*tp = indexCounter + 1; ++tp; *tp = indexCounter + 1; ++tp;
*tp = indexCounter + numberSlices + 1; ++tp; *tp = indexCounter + numberSlices + 1; ++tp;
*tp = indexCounter + numberSlices + 2; ++tp; *tp = indexCounter + numberSlices + 2; ++tp;
@ -1066,7 +1066,7 @@ namespace hgl
} }
} }
}//namespace }//namespace
vulkan::Renderable *CreateRenderableCone(SceneDB *db,vulkan::Material *mtl,const ConeCreateInfo *cci) vulkan::Renderable *CreateRenderableCone(SceneDB *db,vulkan::Material *mtl,const ConeCreateInfo *cci)
{ {
GeometryCreater3D gc(db,mtl); GeometryCreater3D gc(db,mtl);
@ -1117,7 +1117,7 @@ namespace hgl
*tc = 0.0f; ++tc; *tc = 0.0f; ++tc;
*tc = 0.0f; ++tc; *tc = 0.0f; ++tc;
} }
for (i = 0; i < cci->numberSlices + 1; i++) for (i = 0; i < cci->numberSlices + 1; i++)
{ {
float currentAngle = angleStep * (float)i; float currentAngle = angleStep * (float)i;
@ -1180,7 +1180,7 @@ namespace hgl
} }
} }
} }
if(numberVertices<=0xffff) if(numberVertices<=0xffff)
CreateConeIndices<uint16>(gc.CreateIBO16(numberIndices),cci->numberSlices,cci->numberStacks); CreateConeIndices<uint16>(gc.CreateIBO16(numberIndices),cci->numberSlices,cci->numberStacks);
else else
@ -1219,16 +1219,16 @@ namespace hgl
// Points of a cube. // Points of a cube.
/* 4 5 */ const float points[]={ -0.5,-0.5, 0.5, 0.5,-0.5,0.5, 0.5,-0.5,-0.5, -0.5,-0.5,-0.5, /* 4 5 */ const float points[]={ -0.5,-0.5, 0.5, 0.5,-0.5,0.5, 0.5,-0.5,-0.5, -0.5,-0.5,-0.5,
/* *------------* */ -0.5, 0.5, 0.5, 0.5, 0.5,0.5, 0.5, 0.5,-0.5, -0.5, 0.5,-0.5}; /* *------------* */ -0.5, 0.5, 0.5, 0.5, 0.5,0.5, 0.5, 0.5,-0.5, -0.5, 0.5,-0.5};
/* /| /| */ /* /| /| */
/* 0/ | 1/ | */ /* 0/ | 1/ | */
/* *--+---------* | */ /* *--+---------* | */
/* | | | | */ /* | | | | */
/* | 7| | 6| */ /* | 7| | 6| */
/* | *---------+--* */ /* | *---------+--* */
/* | / | / */ /* | / | / */
/* |/ 2|/ */ /* |/ 2|/ */
/* 3*------------* */ /* 3*------------* */
const uint16 indices[]= const uint16 indices[]=
{ {
0,1, 1,2, 2,3, 3,0, 0,1, 1,2, 2,3, 3,0,
@ -1244,7 +1244,7 @@ namespace hgl
VB3f *vertex=gc.GetVertex(); VB3f *vertex=gc.GetVertex();
if(!vertex)return(nullptr); if(!vertex)return(nullptr);
if(cci->center ==Vector3f(0,0,0) if(cci->center ==Vector3f(0,0,0)
&&cci->size ==Vector3f(1,1,1)) &&cci->size ==Vector3f(1,1,1))
{ {

View File

@ -33,7 +33,7 @@ namespace hgl
bool MakeCharBitmap(FontBitmap *,u32char) override; ///<产生字体数据 bool MakeCharBitmap(FontBitmap *,u32char) override; ///<产生字体数据
int GetLineHeight()const override{return LineHeight;} ///<取得行高 int GetLineHeight()const override{return LineHeight;} ///<取得行高
int GetCharWidth(const u32char &) override; int GetCharAdvWidth(const u32char &) override;
};//class WinBitmapFont };//class WinBitmapFont
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

@ -28,7 +28,6 @@ VK_NAMESPACE_BEGIN
const uint32 total_bytes=AccessByPointer(data,uint32); const uint32 total_bytes=AccessByPointer(data,uint32);
int basetype; int basetype;
int vec_size;
int str_len; int str_len;
ShaderStage *ss; ShaderStage *ss;
@ -39,9 +38,9 @@ VK_NAMESPACE_BEGIN
ss->location=*data++; ss->location=*data++;
basetype=*data++; basetype=*data++;
vec_size=*data++; ss->component=*data++;
ss->format=VK_NAMESPACE::GetVulkanFormat(basetype,vec_size); ss->format=VK_NAMESPACE::GetVulkanFormat(basetype,ss->component);
str_len=*data++; str_len=*data++;
ss->name.SetString((char *)data,str_len); ss->name.SetString((char *)data,str_len);
@ -86,7 +85,23 @@ VK_NAMESPACE_BEGIN
delete[] data; delete[] data;
} }
const int ShaderResource::GetStageInputBinding(const AnsiString &name) const ShaderStage *ShaderResource::GetStage(const AnsiString &name) const
{
const int count=stage_inputs.GetCount();
ShaderStage **ss=stage_inputs.GetData();
for(int i=0;i<count;i++)
{
if(name==(*ss)->name)
return *ss;
++ss;
}
return nullptr;
}
const int ShaderResource::GetStageInputBinding(const AnsiString &name) const
{ {
const int count=stage_inputs.GetCount(); const int count=stage_inputs.GetCount();
ShaderStage **ss=stage_inputs.GetData(); ShaderStage **ss=stage_inputs.GetData();