diff --git a/inc/hgl/graph/BufferObject.h b/inc/hgl/graph/BufferObject.h index 563859dc..5200f14e 100644 --- a/inc/hgl/graph/BufferObject.h +++ b/inc/hgl/graph/BufferObject.h @@ -25,15 +25,7 @@ namespace hgl friend BufferObject *CreateBuffer(GLenum type,GLenum user_pattern,BufferData *buf); - BufferObject(GLuint index,GLenum type) - { - buffer_index=index; - buffer_type =type; - - user_pattern=0; - buffer_bytes=0; - buffer_data=nullptr; - } + BufferObject(GLuint index,GLenum type); public: @@ -49,79 +41,15 @@ namespace hgl public: - virtual bool Submit (const void *,GLsizeiptr,GLenum user_pattern)=0; ///<提交数据 - bool Submit (const BufferData *buf_data,GLenum user_pattern) ///<提交数据 - { - if(!buf_data)return(false); - 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; ///<修改数据 + bool Create (GLsizeiptr,GLenum user_pattern); ///<创建数据区 + bool Submit (void *,GLsizeiptr,GLenum user_pattern); ///<提交数据 + bool Submit (const BufferData *buf_data,GLenum user_pattern); ///<提交数据 + bool Change (void *,GLsizeiptr,GLsizeiptr); ///<修改数据 };//class BufferObject - /** - * 创建一个缓冲区 - * @param type 缓冲区类型(GL_ARRAY_BUFFER,GL_ELEMENT_ARRAY_BUFFER等) - * @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); - } + 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); ///<创建一个缓冲区对象 + BufferObject *CreateBufferObject(const GLenum &buf_type,const GLenum &user_pattern,const GLsizeiptr &total_bytes,void *data); ///<创建一个缓冲区对象 }//namespace graph }//namespace hgl #endif//HGL_GRAPH_BUFFER_OBJECT_INCLUDE diff --git a/src/RenderDriver/BufferObject.cpp b/src/RenderDriver/BufferObject.cpp index e9288761..214952e5 100644 --- a/src/RenderDriver/BufferObject.cpp +++ b/src/RenderDriver/BufferObject.cpp @@ -3,6 +3,100 @@ namespace hgl { 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() { SAFE_CLEAR(buffer_data); @@ -10,83 +104,119 @@ namespace hgl glDeleteBuffers(1,&buffer_index); } - class BufferObjectBind:public BufferObject + bool BufferObject::Create(GLsizeiptr size,GLenum up) { - public: + return true; + } - using BufferObject::BufferObject; - ~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 + bool BufferObject::Submit(void *data,GLsizeiptr size,GLenum up) { - public: + if(!data||size<=0)return(false); - using BufferObject::BufferObject; - ~BufferObjectDSA()=default; + user_pattern=up; + gl::BufferData(buffer_type,buffer_index,data,size,user_pattern); - bool Submit(const void *data,GLsizeiptr size,GLenum up) override - { - if(!data||size<=0)return(false); + return(true); + } - user_pattern=up; - buffer_bytes=size; - glNamedBufferData(buffer_index,size,data,user_pattern); + bool BufferObject::Submit(const BufferData *buf_data,GLenum 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||start<0||size<=0)return(false); + if(!data||size<=0)return(false); - glNamedBufferSubData(buffer_index,start,size,data); + return Submit(data,size,user_pattern); + } - return(true); - } - };//class BufferObjectDSA:public BufferObject + bool BufferObject::Change(void *data,GLsizeiptr start,GLsizeiptr size) + { + 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; BufferObject *obj; - if(GLEW_VERSION_4_5||GLEW_ARB_direct_state_access) - { - glCreateBuffers(1,&index); - obj=new BufferObjectDSA(index,type); - } - else - { - glGenBuffers(1,&index); - obj=new BufferObjectBind(index,type); - } + gl::CreateBuffer(1,&index); + + obj=new BufferObject(index,type); if(buf) obj->Submit(buf->GetData(),buf->GetTotalBytes(),user_pattern); 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 hgl