改用新的方式编写BufferObject,同时支持DSA,DSA_EXT,Bind三种模式
This commit is contained in:
parent
6e4e99f56b
commit
ad03777a7a
@ -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
|
||||||
|
@ -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);
|
if(!data||size<=0)return(false);
|
||||||
|
|
||||||
user_pattern=up;
|
user_pattern=up;
|
||||||
glBindBuffer(buffer_type,buffer_index);
|
gl::BufferData(buffer_type,buffer_index,data,size,user_pattern);
|
||||||
glBufferData(buffer_type,size,data,user_pattern);
|
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Change(const void *data,GLsizeiptr start,GLsizeiptr size) override
|
bool BufferObject::Submit(const BufferData *buf_data,GLenum user_pattern)
|
||||||
{
|
{
|
||||||
if(!data||start<0||size<=0)return(false);
|
if(!buf_data)return(false);
|
||||||
|
buffer_data=buf_data;
|
||||||
|
|
||||||
glBindBuffer(buffer_type,buffer_index);
|
const void * data=buf_data->GetData();
|
||||||
glBufferSubData(buffer_type,start,size,data);
|
const GLsizeiptr size=buf_data->GetTotalBytes();
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
};//class BufferObjectBind:public BufferObject
|
|
||||||
|
|
||||||
class BufferObjectDSA:public BufferObject
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
using BufferObject::BufferObject;
|
|
||||||
~BufferObjectDSA()=default;
|
|
||||||
|
|
||||||
bool Submit(const void *data,GLsizeiptr size,GLenum up) override
|
|
||||||
{
|
|
||||||
if(!data||size<=0)return(false);
|
if(!data||size<=0)return(false);
|
||||||
|
|
||||||
user_pattern=up;
|
return Submit(data,size,user_pattern);
|
||||||
buffer_bytes=size;
|
|
||||||
glNamedBufferData(buffer_index,size,data,user_pattern);
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Change(const void *data,GLsizeiptr start,GLsizeiptr size) override
|
bool BufferObject::Change(void *data,GLsizeiptr start,GLsizeiptr size)
|
||||||
{
|
{
|
||||||
if(!data||start<0||size<=0)return(false);
|
if(!data||start<0||size<=0)return(false);
|
||||||
|
|
||||||
glNamedBufferSubData(buffer_index,start,size,data);
|
gl::BufferSubData(buffer_type,buffer_index,data,start,size);
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
};//class BufferObjectDSA:public BufferObject
|
|
||||||
|
|
||||||
BufferObject *CreateBuffer(GLenum type,GLenum user_pattern,BufferData *buf)
|
/**
|
||||||
|
* 创建一个缓冲区对象
|
||||||
|
* @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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user