removed SPVParseData

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-03-21 18:04:26 +08:00
parent 0dae30fa06
commit fb4c8ed8c2
2 changed files with 147 additions and 268 deletions

View File

@ -3,196 +3,151 @@
#include<hgl/type/StringList.h>
#include<hgl/filesystem/FileSystem.h>
using namespace hgl;
using namespace hgl::io;
namespace glsl_compiler
namespace hgl
{
enum class ShaderLanguageType
namespace graph
{
GLSL=0,
HLSL,
MAX=0xff
};//enum class ShaderType
struct CompileInfo
{
ShaderLanguageType shader_type = ShaderLanguageType::GLSL;
const char * entrypoint = nullptr; //it only used in HLSL
uint32_t includes_count = 0;
const char ** includes = nullptr;
const char * preamble = nullptr;
};
UTF8StringList include_list;
CompileInfo compile_info;
struct GLSLCompilerInterface
{
bool (*Init)();
void (*Close)();
bool (*GetLimit)(void *,const int);
bool (*SetLimit)(void *,const int);
uint32_t (*GetType)(const char *ext_name);
SPVData * (*Compile)(const uint32_t stage,const char *shader_source, const CompileInfo *compile_info);
SPVData * (*CompileFromPath)(const uint32_t stage,const char *shader_filename, const CompileInfo *compile_info);
void (*Free)(SPVData *);
SPVParseData *(*ParseSPV)(SPVData *spv_data);
void (*FreeParseSPVData)(SPVParseData *);
};
ExternalModule *gsi_module=nullptr;
static GLSLCompilerInterface *gsi=nullptr;
typedef GLSLCompilerInterface *(*GetInterfaceFUNC)();
bool Init()
{
compile_info.includes=nullptr;
compile_info.includes_count=0;
if(gsi)return(true);
OSString cur_path;
OSString glsl_compiler_fullname;
if(!filesystem::GetCurrentPath(cur_path))
return(false);
glsl_compiler_fullname=filesystem::MergeFilename(cur_path,OS_TEXT("GLSLCompiler") HGL_PLUGIN_EXTNAME);
gsi_module=LoadExternalModule(glsl_compiler_fullname);
if(!gsi_module)return(false);
GetInterfaceFUNC get_func;
get_func=GetInterfaceFUNC(gsi_module->GetFunc("GetInterface"));
if(get_func)
enum class ShaderLanguageType
{
gsi=get_func();
GLSL=0,
HLSL,
MAX=0xff
};//enum class ShaderType
struct CompileInfo
{
ShaderLanguageType shader_type = ShaderLanguageType::GLSL;
const char * entrypoint = nullptr; //it only used in HLSL
uint32_t includes_count = 0;
const char ** includes = nullptr;
const char * preamble = nullptr;
};
CompileInfo compile_info;
struct SPVParseData;
struct GLSLCompilerInterface
{
bool (*Init)();
void (*Close)();
bool (*GetLimit)(void *,const int);
bool (*SetLimit)(void *,const int);
uint32_t (*GetType)(const char *ext_name);
SPVData * (*Compile)(const uint32_t stage,const char *shader_source, const CompileInfo *compile_info);
SPVData * (*CompileFromPath)(const uint32_t stage,const char *shader_filename, const CompileInfo *compile_info);
void (*Free)(SPVData *);
SPVParseData *(*ParseSPV)(SPVData *spv_data);
void (*FreeParseSPVData)(SPVParseData *);
};
ExternalModule *gsi_module=nullptr;
static GLSLCompilerInterface *gsi=nullptr;
typedef GLSLCompilerInterface *(*GetInterfaceFUNC)();
bool InitShaderCompiler()
{
compile_info.includes=nullptr;
compile_info.includes_count=0;
if(gsi)return(true);
OSString cur_path;
OSString glsl_compiler_fullname;
if(!filesystem::GetCurrentPath(cur_path))
return(false);
glsl_compiler_fullname=filesystem::MergeFilename(cur_path,OS_TEXT("GLSLCompiler") HGL_PLUGIN_EXTNAME);
gsi_module=LoadExternalModule(glsl_compiler_fullname);
if(!gsi_module)return(false);
GetInterfaceFUNC get_func;
get_func=GetInterfaceFUNC(gsi_module->GetFunc("GetInterface"));
if(get_func)
{
gsi=get_func();
if(gsi)
{
if(gsi->Init())
return(true);
}
}
delete gsi_module;
gsi_module=nullptr;
return(false);
}
void CloseShaderCompiler()
{
delete[] compile_info.includes;
compile_info.includes=nullptr;
if(gsi)
{
if(gsi->Init())
return(true);
gsi->Close();
gsi=nullptr;
}
if(gsi_module)
{
delete gsi_module;
gsi_module=nullptr;
}
}
delete gsi_module;
gsi_module=nullptr;
return(false);
}
const char PreambleString[]="";//#extension GL_GOOGLE_include_directive : require\n";
void Close()
{
delete[] compile_info.includes;
compile_info.includes=nullptr;
if(gsi)
void RebuildGLSLIncludePath()
{
gsi->Close();
gsi=nullptr;
}
if(gsi_module)
{
delete gsi_module;
gsi_module=nullptr;
}
}
void AddGLSLIncludePath(const char *path)
{
if(include_list.Find(path)==-1)
include_list.Add(path);
}
const char PreambleString[]="#extension GL_GOOGLE_include_directive : require\n";
void RebuildGLSLIncludePath()
{
delete[] compile_info.includes;
compile_info.includes_count=include_list.GetCount();
compile_info.includes=new const char *[compile_info.includes_count];
for(uint32_t i=0;i<compile_info.includes_count;i++)
compile_info.includes[i]=include_list[i].c_str();
if(compile_info.includes_count>0)
compile_info.preamble=PreambleString;
}
uint32_t GetType (const char *ext_name)
{
if(gsi)
return gsi->GetType(ext_name);
return 0;
}
void Free (SPVData *spv_data)
{
if(gsi)
gsi->Free(spv_data);
}
SPVParseData *ParseSPV(SPVData *spv_data)
{
return gsi?gsi->ParseSPV(spv_data):nullptr;
}
void FreeSPVParse(SPVParseData *spv)
{
if(gsi&&spv)
gsi->FreeParseSPVData(spv);
}
SPVData *Compile(const uint32_t type,const char *source)
{
if(!gsi)
return(nullptr);
ByteOrderMask bom=CheckBOM(source);
if(bom==ByteOrderMask::UTF8)
source+=3;
else
if(bom!=ByteOrderMask::NONE)
{
std::cerr<<"GLSLCompiler does not support BOMHeader outside of UTF8"<<std::endl;
return(nullptr);
}
glsl_compiler::SPVData *spv=gsi->Compile(type,source,&compile_info);
if(!spv)return(nullptr);
const bool result=spv->result;
if(!result)
void FreeSPVData(SPVData *spv_data)
{
std::cerr<<"glsl compile failed."<<std::endl;
if(spv->log)
std::cerr<<"info: "<<spv->log<<std::endl;
if(spv->debug_log)
std::cerr<<"debug info: "<<spv->debug_log<<std::endl;
glsl_compiler::Free(spv);
return(nullptr);
if(gsi)
gsi->Free(spv_data);
}
std::cout<<"Compile successed! spv length "<<spv->spv_length<<" bytes."<<std::endl;
SPVData *CompileShader(const uint32_t type,const char *source)
{
if(!gsi)
return(nullptr);
return spv;
}
}//namespace glsl_compiler
ByteOrderMask bom=CheckBOM(source);
if(bom==ByteOrderMask::UTF8)
source+=3;
else
if(bom!=ByteOrderMask::NONE)
return(nullptr);
SPVData *spv=gsi->Compile(type,source,&compile_info);
if(!spv)return(nullptr);
const bool result=spv->result;
if(!result)
{
FreeSPVData(spv);
return(nullptr);
}
return spv;
}
}//namespace graph
}//namespace hgl

View File

@ -1,102 +1,26 @@
#ifndef HGL_GLSL_COMPILER_INCLUDE
#define HGL_GLSL_COMPILER_INCLUDE
#include<hgl/type/String.h>
#include<hgl/type/Map.h>
#include<stdint.h>
#include<hgl/graph/VKShaderStage.h>
#include<hgl/type/DataType.h>
namespace hgl
{
namespace io
namespace graph
{
class MemoryOutputStream;
class DataOutputStream;
}
}
struct SPVData
{
bool result;
char *log;
char *debug_log;
namespace glsl_compiler
{
using namespace hgl;
using namespace hgl::graph;
uint32 *spv_data;
uint32 spv_length;
};
enum class DescriptorType //等同VkDescriptorType
{
SAMPLER = 0,
COMBINED_IMAGE_SAMPLER,
SAMPLED_IMAGE,
STORAGE_IMAGE,
UNIFORM_TEXEL_BUFFER,
STORAGE_TEXEL_BUFFER,
UNIFORM_BUFFER,
STORAGE_BUFFER,
UNIFORM_BUFFER_DYNAMIC,
STORAGE_BUFFER_DYNAMIC,
INPUT_ATTACHMENT,
ENUM_CLASS_RANGE(SAMPLER,INPUT_ATTACHMENT)
};
bool InitShaderCompiler();
void CloseShaderCompiler();
struct Descriptor
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t set;
uint8_t binding;
};
struct PushConstant
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t offset;
uint8_t size;
};
struct SubpassInput
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
uint8_t input_attachment_index;
uint8_t binding;
};
template<typename T>
struct ShaderResourceData
{
uint32_t count;
T *items;
};
using ShaderDescriptorResource=ShaderResourceData<Descriptor>[size_t(DescriptorType::RANGE_SIZE)];
struct SPVData
{
bool result;
char *log;
char *debug_log;
uint32_t *spv_data;
uint32_t spv_length;
};
struct SPVParseData
{
ShaderStageIO stage_io;
ShaderDescriptorResource resource;
ShaderResourceData<PushConstant> push_constant;
ShaderResourceData<SubpassInput> subpass_input;
};
bool Init();
void Close();
void AddGLSLIncludePath(const char *);
void RebuildGLSLIncludePath();
uint32_t GetType (const char *ext_name);
SPVData * Compile (const uint32_t type,const char *source);
void Free (SPVData *spv_data);
SPVParseData * ParseSPV(SPVData *spv_data);
void FreeSPVParse(SPVParseData *spv);
}//namespace glsl_compiler
SPVData * CompileShader (const uint32 type,const char *source);
void FreeSPVData (SPVData *spv_data);
}//namespace graph
}//namespace hgl
#endif//HGL_GLSL_COMPILER_INCLUDE