254 lines
10 KiB
C++
254 lines
10 KiB
C++
#include<hgl/graph/Shader.h>
|
|
//#include<hgl/graph/UBO.h>
|
|
|
|
namespace hgl
|
|
{
|
|
namespace graph
|
|
{
|
|
/**
|
|
* 取得属性索引地址
|
|
* @param name 属性名称
|
|
* @return 索引地址
|
|
* @return -1 出错
|
|
*/
|
|
int Shader::GetAttribLocation(const char *name)
|
|
{
|
|
if(!name||!(*name))return(-1);
|
|
|
|
int result;
|
|
|
|
if(!attrib_location.Get(name,result))
|
|
{
|
|
result=_GetAttribLocation(name);
|
|
|
|
attrib_location.Add(name,result);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 取得一个变量的地址
|
|
* @param name 变量名称
|
|
* @return 地址
|
|
* @return -1 出错
|
|
*/
|
|
int Shader::GetUniformLocation(const char *name) ///<取得一个变量的地址
|
|
{
|
|
if(!name||!(*name))return(-1);
|
|
|
|
int result;
|
|
|
|
if(!uniform_location.Get(name,result))
|
|
{
|
|
result=_GetUniformLocation(name);
|
|
|
|
uniform_location.Add(name,result);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// /**
|
|
// * 取得一个Shader只读数据区索引
|
|
// * @param name 数据区名称
|
|
// * @return 数据区索引
|
|
// * @return -1 出错
|
|
// */
|
|
// int Shader::GetUniformBlockIndex(const char *name)
|
|
// {
|
|
// if(!name||!(*name))return(-1);
|
|
//
|
|
// int result;
|
|
//
|
|
// if(!uniform_block_index.Get(name,result))
|
|
// {
|
|
// result=_GetUniformBlockIndex(name);
|
|
//
|
|
// uniform_block_index.Add(name,result);
|
|
// }
|
|
//
|
|
// return result;
|
|
// }
|
|
//
|
|
// /**
|
|
// * 取得一个Shader数据存储区索引
|
|
// * @param name 数据存储区名称
|
|
// * @return 数据存储区索引
|
|
// * @return -1 出错
|
|
// */
|
|
// int Shader::GetShaderStorageIndex(const char *name)
|
|
// {
|
|
// if(!name||!(*name))return(-1);
|
|
//
|
|
// int result;
|
|
//
|
|
// if(!ssbo_index.Get(name,result))
|
|
// {
|
|
// result=_GetShaderStorageIndex(name);
|
|
//
|
|
// ssbo_index.Add(name,result);
|
|
// }
|
|
//
|
|
// return result;
|
|
// }
|
|
|
|
#define HGL_GLSL_SetUniform1234(func,type) bool Shader::SetUniform1##func(const char *uniform_name,type v0) \
|
|
{ \
|
|
const int location=GetUniformLocation(uniform_name); \
|
|
\
|
|
if(location==-1)return(false); \
|
|
\
|
|
return SetUniform1##func(location,v0); \
|
|
} \
|
|
\
|
|
bool Shader::SetUniform2##func(const char *uniform_name,type v0,type v1) \
|
|
{ \
|
|
const int location=GetUniformLocation(uniform_name); \
|
|
\
|
|
if(location==-1)return(false); \
|
|
\
|
|
return SetUniform2##func(location,v0,v1); \
|
|
} \
|
|
\
|
|
bool Shader::SetUniform3##func(const char *uniform_name,type v0,type v1,type v2) \
|
|
{ \
|
|
const int location=GetUniformLocation(uniform_name); \
|
|
\
|
|
if(location==-1)return(false); \
|
|
\
|
|
return SetUniform3##func(location,v0,v1,v2); \
|
|
} \
|
|
\
|
|
bool Shader::SetUniform4##func(const char *uniform_name,type v0,type v1,type v2,type v3) \
|
|
{ \
|
|
const int location=GetUniformLocation(uniform_name); \
|
|
\
|
|
if(location==-1)return(false); \
|
|
\
|
|
return SetUniform4##func(location,v0,v1,v2,v3); \
|
|
}
|
|
HGL_GLSL_SetUniform1234(f,float);
|
|
HGL_GLSL_SetUniform1234(i,int);
|
|
HGL_GLSL_SetUniform1234(ui,unsigned int);
|
|
|
|
#undef HGL_GLSL_SetUniform1234
|
|
|
|
#define HGL_GLSL_SetUniformPointer(func,type) bool Shader::SetUniform##func(const char *uniform_name,const type *value) \
|
|
{ \
|
|
const int location=GetUniformLocation(uniform_name); \
|
|
\
|
|
if(location==-1)return(false); \
|
|
\
|
|
return SetUniform##func(location,value); \
|
|
}
|
|
|
|
HGL_GLSL_SetUniformPointer(1fv,float);
|
|
HGL_GLSL_SetUniformPointer(2fv,float);
|
|
HGL_GLSL_SetUniformPointer(3fv,float);
|
|
HGL_GLSL_SetUniformPointer(4fv,float);
|
|
|
|
HGL_GLSL_SetUniformPointer(1iv,int);
|
|
HGL_GLSL_SetUniformPointer(2iv,int);
|
|
HGL_GLSL_SetUniformPointer(3iv,int);
|
|
HGL_GLSL_SetUniformPointer(4iv,int);
|
|
|
|
HGL_GLSL_SetUniformPointer(1uiv,unsigned int);
|
|
HGL_GLSL_SetUniformPointer(2uiv,unsigned int);
|
|
HGL_GLSL_SetUniformPointer(3uiv,unsigned int);
|
|
HGL_GLSL_SetUniformPointer(4uiv,unsigned int);
|
|
|
|
#undef HGL_GLSL_SetUniformPointer
|
|
|
|
#define HGL_GLSL_SetUniformMatrixPointer(func) bool Shader::SetUniformMatrix##func(const char *uniform_name,const float *mat) \
|
|
{ \
|
|
const int location=GetUniformLocation(uniform_name); \
|
|
\
|
|
if(location==-1)return(false); \
|
|
\
|
|
return SetUniformMatrix##func(location,mat); \
|
|
}
|
|
|
|
HGL_GLSL_SetUniformMatrixPointer(2fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(3fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(4fv);
|
|
|
|
HGL_GLSL_SetUniformMatrixPointer(2x3fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(3x2fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(2x4fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(4x2fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(3x4fv);
|
|
HGL_GLSL_SetUniformMatrixPointer(4x3fv);
|
|
|
|
#undef HGL_GLSL_SetUniformMatrixPointer
|
|
|
|
// /**
|
|
// * 取得一个Shader只读数据区的访问对象
|
|
// * @param name 对象名称
|
|
// * @param level 访问级别
|
|
// * @return 对象指针
|
|
// */
|
|
// UBO *Shader::GetUniformBlock(const char *name,uint level)
|
|
// {
|
|
// if(!name||!(*name))
|
|
// {
|
|
// LOG_ERROR(U8_TEXT("UBO name error:"));
|
|
// return(nullptr);
|
|
// }
|
|
//
|
|
// UBO *ubo=uniform_block_object[name];
|
|
//
|
|
// if(ubo)
|
|
// return ubo;
|
|
//
|
|
// const int index=GetUniformBlockIndex(name);
|
|
//
|
|
// if(index<0)
|
|
// {
|
|
// LOG_ERROR(U8_TEXT("UBO name error:")+UTF8String(name));
|
|
// return(nullptr);
|
|
// }
|
|
//
|
|
// ubo=new UBO(name,program,index,level);
|
|
//
|
|
// uniform_block_object.Add(name,ubo);
|
|
//
|
|
// return ubo;
|
|
// }
|
|
//
|
|
// /**
|
|
// * 取得一个Shader数据存储区的访问对象
|
|
// * @param name 对象名称
|
|
// * @param level 访问级别
|
|
// * @return 对象指针
|
|
// */
|
|
// SSBO *Shader::GetShaderStorage(const char *name,uint level)
|
|
// {
|
|
// if(!name||!(*name))
|
|
// {
|
|
// LOG_ERROR(U8_TEXT("SSBO name error:"));
|
|
// return(nullptr);
|
|
// }
|
|
//
|
|
// SSBO *ssbo=ssbo_object[name];
|
|
//
|
|
// if(ssbo)
|
|
// return ssbo;
|
|
//
|
|
// const int index=GetShaderStorageIndex(name);
|
|
//
|
|
// if(index<0)
|
|
// {
|
|
// LOG_ERROR(U8_TEXT("SSBO name error:")+UTF8String(name));
|
|
// return(nullptr);
|
|
// }
|
|
//
|
|
// ssbo=new SSBO(name,program,index,level);
|
|
//
|
|
// ssbo_object.Add(name,ssbo);
|
|
//
|
|
// return ssbo;
|
|
// }
|
|
}//namespace graph
|
|
}//namespace hgl
|