Optimize the code layout to make it easier to read

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-02-18 15:40:55 +08:00
parent d2568072c7
commit 94e6473ce6

View File

@ -184,57 +184,25 @@ EShLanguage FindLanguage(const VkShaderStageFlagBits shader_type)
}
}
extern "C"
enum class ShaderLanguageType
{
bool InitShaderCompiler()
{
init_default_build_in_resource();
return glslang::InitializeProcess();
}
void CloseShaderCompiler()
{
glslang::FinalizeProcess();
}
bool GetLimit(TBuiltInResource *bir,const int size)
{
if(!bir)return(false);
if(size!=sizeof(TBuiltInResource))return(false);
memcpy(bir,&default_build_in_resource,size);
return(true);
}
bool SetLimit(TBuiltInResource *bir,const int size)
{
if(!bir)return(false);
if(size!=sizeof(TBuiltInResource))return(false);
memcpy(&default_build_in_resource,bir,size);
return(true);
}
enum class ShaderLanguageType
{
GLSL=0,
HLSL,
MAX=0xff
};//enum class ShaderType
};//enum class ShaderType
struct CompileInfo
{
struct CompileInfo
{
ShaderLanguageType shader_type;
const char * entrypoint;
uint32_t includes_count;
const char ** includes;
const char * preamble;
};
};
enum class VertexAttribBaseType
{
enum class VertexAttribBaseType
{
Bool=0,
Int,
UInt,
@ -242,10 +210,10 @@ extern "C"
Double,
MAX=0xff
};//enum class VertexAttribBaseType
};//enum class VertexAttribBaseType
VertexAttribBaseType FromSPIRType(const spirv_cross::SPIRType::BaseType type)
{
VertexAttribBaseType FromSPIRType(const spirv_cross::SPIRType::BaseType type)
{
if(type==spirv_cross::SPIRType::BaseType::Boolean) return VertexAttribBaseType::Bool;
if(type==spirv_cross::SPIRType::BaseType::SByte
||type==spirv_cross::SPIRType::BaseType::Short
@ -260,10 +228,10 @@ extern "C"
if(type==spirv_cross::SPIRType::BaseType::Double) return VertexAttribBaseType::Double;
return VertexAttribBaseType::MAX;
}
}
char *new_strcpy(const char *src)
{
char *new_strcpy(const char *src)
{
size_t len=1+strlen(src);
char *str=new char[len];
@ -271,55 +239,54 @@ extern "C"
memcpy(str,src,len);
return str;
}
}
struct ShaderStage
{
char name[128];
constexpr size_t SHADER_RESOURCE_NAME_MAX_LENGTH=128;
struct ShaderStage
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t location;
uint32_t basetype;
uint32_t vec_size;
};//
};//
struct ShaderStageData
{
struct ShaderStageData
{
uint32_t count;
ShaderStage *items;
};
};
struct ShaderResource
{
char name[128];
union
{
struct
{
struct Descriptor
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t set;
uint8_t binding;
};
};
struct
{
struct PushConstant
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t offset;
uint8_t size;
};
};
struct
{
struct SubpassInput
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t input_attachment_index;
};
};
};
uint8_t binding;
};
struct ShaderResourceData
{
template<typename T>
struct ShaderResourceData
{
uint32_t count;
ShaderResource *items;
};
T *items;
};
struct SPVData
{
struct SPVData
{
bool result;
char *log;
char *debug_log;
@ -328,16 +295,16 @@ extern "C"
uint32_t spv_length;
ShaderStageData input,output;
ShaderResourceData resource[VK_DESCRIPTOR_TYPE_COUNT];
ShaderResourceData push_constant;
ShaderResourceData subpass_input;
ShaderResourceData<Descriptor> resource[VK_DESCRIPTOR_TYPE_COUNT];
ShaderResourceData<PushConstant> push_constant;
ShaderResourceData<SubpassInput> subpass_input;
void Init()
{
memset(this,0,sizeof(SPVData));
}
public:
public:
SPVData(const char *l,const char *dl)
{
@ -385,15 +352,10 @@ extern "C"
delete[] debug_log;
delete[] spv_data;
}
};//struct SPVData
};//struct SPVData
void FreeSPVData(SPVData *spv)
{
delete spv;
}
void OutputShaderStage(ShaderStageData *ssd,ShaderParse *sp,const SPVResVector &stages)
{
void OutputShaderStage(ShaderStageData *ssd,ShaderParse *sp,const SPVResVector &stages)
{
size_t attr_count=stages.size();
ssd->count=(uint32_t)attr_count;
@ -419,17 +381,17 @@ extern "C"
++ss;
}
}
}
void OutputShaderResource(ShaderResourceData *ssd,ShaderParse *sp,const SPVResVector &res)
{
void OutputShaderResource(ShaderResourceData<Descriptor> *ssd,ShaderParse *sp,const SPVResVector &res)
{
size_t count=res.size();
if(count<=0)return;
ssd->count=(uint32_t)count;
ssd->items=new ShaderResource[count];
ShaderResource *sr=ssd->items;
ssd->items=new Descriptor[count];
Descriptor *sr=ssd->items;
for(const spirv_cross::Resource &obj:res)
{
@ -439,17 +401,17 @@ extern "C"
++sr;
}
}
}
void OutputPushConstant(ShaderResourceData* ssd, ShaderParse* sp, const SPVResVector& res)
{
void OutputPushConstant(ShaderResourceData<PushConstant> *ssd, ShaderParse* sp, const SPVResVector& res)
{
size_t count = res.size();
if (count <= 0)return;
ssd->count = (uint32_t)count;
ssd->items = new ShaderResource[count];
ShaderResource* sr = ssd->items;
ssd->items = new PushConstant[count];
PushConstant *sr=ssd->items;
for (const spirv_cross::Resource& obj : res)
{
@ -459,17 +421,17 @@ extern "C"
++sr;
}
}
}
void OutputSubpassInput(ShaderResourceData* ssd, ShaderParse* sp, const SPVResVector& res)
{
void OutputSubpassInput(ShaderResourceData<SubpassInput> *ssd, ShaderParse* sp, const SPVResVector& res)
{
size_t count = res.size();
if (count <= 0)return;
ssd->count = (uint32_t)count;
ssd->items = new ShaderResource[count];
ShaderResource* sr = ssd->items;
ssd->items = new SubpassInput[count];
SubpassInput *sr = ssd->items;
for (const spirv_cross::Resource& obj : res)
{
@ -479,6 +441,43 @@ extern "C"
++sr;
}
}
extern "C"
{
bool InitShaderCompiler()
{
init_default_build_in_resource();
return glslang::InitializeProcess();
}
void CloseShaderCompiler()
{
glslang::FinalizeProcess();
}
bool GetLimit(TBuiltInResource *bir,const int size)
{
if(!bir)return(false);
if(size!=sizeof(TBuiltInResource))return(false);
memcpy(bir,&default_build_in_resource,size);
return(true);
}
bool SetLimit(TBuiltInResource *bir,const int size)
{
if(!bir)return(false);
if(size!=sizeof(TBuiltInResource))return(false);
memcpy(&default_build_in_resource,bir,size);
return(true);
}
void FreeSPVData(SPVData *spv)
{
delete spv;
}
SPVData *Shader2SPV(