改用新的方式编写BufferObject,同时支持DSA,DSA_EXT,Bind三种模式

This commit is contained in:
hyzboy 2019-03-23 10:09:34 +08:00
parent 6e4e99f56b
commit ad03777a7a
2 changed files with 195 additions and 137 deletions

View File

@ -25,15 +25,7 @@ namespace hgl
friend BufferObject *CreateBuffer(GLenum type,GLenum user_pattern,BufferData *buf); friend BufferObject *CreateBuffer(GLenum type,GLenum user_pattern,BufferData *buf);
BufferObject(GLuint index,GLenum type) BufferObject(GLuint index,GLenum type);
{
buffer_index=index;
buffer_type =type;
user_pattern=0;
buffer_bytes=0;
buffer_data=nullptr;
}
public: public:
@ -49,79 +41,15 @@ namespace hgl
public: public:
virtual bool Submit (const void *,GLsizeiptr,GLenum user_pattern)=0; ///<提交数据 bool Create (GLsizeiptr,GLenum user_pattern); ///<创建数据区
bool Submit (const BufferData *buf_data,GLenum user_pattern) ///<提交数据 bool Submit (void *,GLsizeiptr,GLenum user_pattern); ///<提交数据
{ bool Submit (const BufferData *buf_data,GLenum user_pattern); ///<提交数据
if(!buf_data)return(false); bool Change (void *,GLsizeiptr,GLsizeiptr); ///<修改数据
buffer_data=buf_data;
const void * data=buf_data->GetData();
const GLsizeiptr size=buf_data->GetTotalBytes();
if(!data||size<=0)return(false);
return Submit(data,size,user_pattern);
}
virtual bool Change (const void *,GLsizeiptr,GLsizeiptr)=0; ///<修改数据
};//class BufferObject };//class BufferObject
/** BufferObject *CreateBufferObject(GLenum type,GLenum user_pattern=0,BufferData *buf=nullptr); ///<创建一个缓冲区对象
* BufferObject *CreateBufferObject(const GLenum &buf_type,const GLenum &user_pattern,const GLsizeiptr &total_bytes); ///<创建一个缓冲区对象
* @param type (GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等) BufferObject *CreateBufferObject(const GLenum &buf_type,const GLenum &user_pattern,const GLsizeiptr &total_bytes,void *data); ///<创建一个缓冲区对象
* @param user_pattern (GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
* @param buf
*/
BufferObject *CreateBuffer(GLenum type,GLenum user_pattern=0,BufferData *buf=nullptr);
/**
*
* @param buf_type (GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
* @param user_pattern 使(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
* @param total_bytes
*/
inline BufferObject *CreateBuffer( const GLenum &buf_type,
const GLenum &user_pattern,
const GLsizeiptr &total_bytes)
{
if(total_bytes<=0)return(nullptr);
BufferObject *buf=CreateBuffer(buf_type);
if(!buf)
return(nullptr);
if(buf->Create(data,total_bytes))
return buf;
delete buf;
return(nullptr);
}
/**
*
* @param buf_type (GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
* @param user_pattern 使(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
* @param total_bytes
* @param data
*/
inline BufferObject *CreateBuffer( const GLenum &buf_type,
const GLenum &user_pattern,
const GLsizeiptr &total_bytes,void *data)
{
if(total_bytes<=0)return(nullptr);
if(!data)return(nullptr);
BufferObject *buf=CreateBuffer(buf_type);
if(!buf)
return(nullptr);
if(buf->Submit(data,total_bytes,user_pattern))
return buf;
delete buf;
return(nullptr);
}
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl
#endif//HGL_GRAPH_BUFFER_OBJECT_INCLUDE #endif//HGL_GRAPH_BUFFER_OBJECT_INCLUDE

View File

@ -3,6 +3,100 @@ namespace hgl
{ {
namespace graph namespace graph
{ {
namespace gl
{
namespace dsa
{
void CreateBuffer(GLenum type,GLuint *index)
{
glCreateBuffers(1,index);
}
void BufferData(GLenum type,GLuint index,void *data,GLsizeiptr size,GLenum user_pattern)
{
glNamedBufferData(index,size,data,user_pattern);
}
void BufferSubData(GLenum type,GLuint index,void *data,GLsizeiptr start,GLsizeiptr size)
{
glNamedBufferSubData(index,start,size,data);
}
}//namespace dsa
namespace dsa_ext
{
void BufferData(GLenum type,GLuint index,void *data,GLsizeiptr size,GLenum user_pattern)
{
glNamedBufferDataEXT(index,size,data,user_pattern);
}
void BufferSubData(GLenum type,GLuint index,void *data,GLsizeiptr start,GLsizeiptr size)
{
glNamedBufferSubDataEXT(index,start,size,data);
}
}//namespace dsa
namespace bind
{
void CreateBuffer(GLenum type,GLuint *index)
{
glGenBuffers(1,index);
}
void BufferData(GLenum type,GLuint index,void *data,GLsizeiptr size,GLenum user_pattern)
{
glBindBuffer(type,index);
glBufferData(type,size,data,user_pattern);
}
void BufferSubData(GLenum type,GLuint index,void *data,GLsizeiptr start,GLsizeiptr size)
{
glBindBuffer(type,index);
glBufferSubData(type,start,size,data);
}
}
static void (*CreateBuffer)(GLenum type,GLuint *);
static void (*BufferData)(GLenum type,GLuint index,void *data,GLsizeiptr size,GLenum user_pattern);
static void (*BufferSubData)(GLenum type,GLuint index,void *data,GLsizeiptr start,GLsizeiptr size);
void InitBufferObjectAPI()
{
if(glCreateBuffers
||glNamedBufferData
||glNamedBufferSubData) //dsa
{
hgl::graph::gl::CreateBuffer =dsa::CreateBuffer;
hgl::graph::gl::BufferData =dsa::BufferData;
hgl::graph::gl::BufferSubData =dsa::BufferSubData;
}
else
if(glNamedBufferDataEXT
||glNamedBufferSubDataEXT)
{
hgl::graph::gl::CreateBuffer =bind::CreateBuffer;
hgl::graph::gl::BufferData =dsa_ext::BufferData;
hgl::graph::gl::BufferSubData =dsa_ext::BufferSubData;
}
else
{
hgl::graph::gl::CreateBuffer =bind::CreateBuffer;
hgl::graph::gl::BufferData =bind::BufferData;
hgl::graph::gl::BufferSubData =bind::BufferSubData;
}
}
}//namespace gl
BufferObject::BufferObject(GLuint index,GLenum type)
{
buffer_index=index;
buffer_type =type;
user_pattern=0;
buffer_bytes=0;
buffer_data=nullptr;
}
BufferObject::~BufferObject() BufferObject::~BufferObject()
{ {
SAFE_CLEAR(buffer_data); SAFE_CLEAR(buffer_data);
@ -10,83 +104,119 @@ namespace hgl
glDeleteBuffers(1,&buffer_index); glDeleteBuffers(1,&buffer_index);
} }
class BufferObjectBind:public BufferObject bool BufferObject::Create(GLsizeiptr size,GLenum up)
{ {
public: return true;
}
using BufferObject::BufferObject; bool BufferObject::Submit(void *data,GLsizeiptr size,GLenum up)
~BufferObjectBind()=default;
bool Submit(const void *data,GLsizeiptr size,GLenum up) override
{
if(!data||size<=0)return(false);
user_pattern=up;
glBindBuffer(buffer_type,buffer_index);
glBufferData(buffer_type,size,data,user_pattern);
return(true);
}
bool Change(const void *data,GLsizeiptr start,GLsizeiptr size) override
{
if(!data||start<0||size<=0)return(false);
glBindBuffer(buffer_type,buffer_index);
glBufferSubData(buffer_type,start,size,data);
return(true);
}
};//class BufferObjectBind:public BufferObject
class BufferObjectDSA:public BufferObject
{ {
public: if(!data||size<=0)return(false);
using BufferObject::BufferObject; user_pattern=up;
~BufferObjectDSA()=default; gl::BufferData(buffer_type,buffer_index,data,size,user_pattern);
bool Submit(const void *data,GLsizeiptr size,GLenum up) override return(true);
{ }
if(!data||size<=0)return(false);
user_pattern=up; bool BufferObject::Submit(const BufferData *buf_data,GLenum user_pattern)
buffer_bytes=size; {
glNamedBufferData(buffer_index,size,data,user_pattern); if(!buf_data)return(false);
buffer_data=buf_data;
return(true); const void * data=buf_data->GetData();
} const GLsizeiptr size=buf_data->GetTotalBytes();
bool Change(const void *data,GLsizeiptr start,GLsizeiptr size) override if(!data||size<=0)return(false);
{
if(!data||start<0||size<=0)return(false);
glNamedBufferSubData(buffer_index,start,size,data); return Submit(data,size,user_pattern);
}
return(true); bool BufferObject::Change(void *data,GLsizeiptr start,GLsizeiptr size)
} {
};//class BufferObjectDSA:public BufferObject if(!data||start<0||size<=0)return(false);
BufferObject *CreateBuffer(GLenum type,GLenum user_pattern,BufferData *buf) gl::BufferSubData(buffer_type,buffer_index,data,start,size);
return(true);
}
/**
*
* @param type (GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
* @param user_pattern (GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
* @param buf
*/
BufferObject *CreateBufferObject(GLenum type,GLenum user_pattern,BufferData *buf)
{ {
GLuint index; GLuint index;
BufferObject *obj; BufferObject *obj;
if(GLEW_VERSION_4_5||GLEW_ARB_direct_state_access) gl::CreateBuffer(1,&index);
{
glCreateBuffers(1,&index); obj=new BufferObject(index,type);
obj=new BufferObjectDSA(index,type);
}
else
{
glGenBuffers(1,&index);
obj=new BufferObjectBind(index,type);
}
if(buf) if(buf)
obj->Submit(buf->GetData(),buf->GetTotalBytes(),user_pattern); obj->Submit(buf->GetData(),buf->GetTotalBytes(),user_pattern);
return(obj); return(obj);
} }
/**
*
* @param buf_type (GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
* @param user_pattern 使(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
* @param total_bytes
*/
BufferObject *CreateBufferObject( const GLenum &buf_type,
const GLenum &user_pattern,
const GLsizeiptr &total_bytes)
{
if(total_bytes<=0)return(nullptr);
GLuint index;
BufferObject *obj;
gl::CreateBuffer(1,&index);
BufferObject *buf=new BufferObject(index,buf_type);
if(buf->Create(total_bytes,user_pattern))
return buf;
delete buf;
return(nullptr);
}
/**
*
* @param buf_type (GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等)
* @param user_pattern 使(GL_STATIC_DRAW,GL_DYNAMIC_DRAW等)
* @param total_bytes
* @param data
*/
inline BufferObject *CreateBufferObject( const GLenum &buf_type,
const GLenum &user_pattern,
const GLsizeiptr &total_bytes,void *data)
{
if(total_bytes<=0)return(nullptr);
if(!data)return(nullptr);
GLuint index;
BufferObject *obj;
gl::CreateBuffer(1,&index);
BufferObject *buf=new BufferObject(index,buf_type);
if(buf->Create(total_bytes,user_pattern))
return buf;
if(buf->Submit(data,total_bytes,user_pattern))
return buf;
delete buf;
return(nullptr);
}
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl