summaryrefslogtreecommitdiff
path: root/src/shared/glemu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/glemu.cpp')
-rw-r--r--src/shared/glemu.cpp173
1 files changed, 48 insertions, 125 deletions
diff --git a/src/shared/glemu.cpp b/src/shared/glemu.cpp
index 58e071d..c728b9f 100644
--- a/src/shared/glemu.cpp
+++ b/src/shared/glemu.cpp
@@ -3,25 +3,18 @@
extern int glversion;
extern int intel_mapbufferrange_bug;
-namespace gle
-{
- struct attribinfo
- {
+namespace gle {
+ struct attribinfo {
int type, size, formatsize, offset;
GLenum format;
-
attribinfo() : type(0), size(0), formatsize(0), offset(0), format(GL_FALSE) {}
-
- bool operator==(const attribinfo &a) const
- {
+ bool operator==(const attribinfo &a) const {
return type == a.type && size == a.size && format == a.format && offset == a.offset;
}
- bool operator!=(const attribinfo &a) const
- {
+ bool operator!=(const attribinfo &a) const {
return type != a.type || size != a.size || format != a.format || offset != a.offset;
}
};
-
extern const char * const attribnames[MAXATTRIBS] = { "vvertex", "vcolor", "vtexcoord0", "vtexcoord1", "vnormal", "vtangent", "vboneweight", "vboneindex" };
ucharbuf attribbuf;
static uchar *attribdata;
@@ -33,33 +26,23 @@ namespace gle
static bool changedattribs = false;
static vector<GLint> multidrawstart;
static vector<GLsizei> multidrawcount;
-
#define MAXQUADS (0x10000/4)
static GLuint quadindexes = 0;
static bool quadsenabled = false;
-
#define MAXVBOSIZE (4*1024*1024)
static GLuint vbo = 0;
static int vbooffset = MAXVBOSIZE;
-
static GLuint defaultvao = 0;
-
- void enablequads()
- {
+ void enablequads() {
quadsenabled = true;
-
if(glversion < 300) return;
-
- if(quadindexes)
- {
+ if(quadindexes) {
glBindBuffer_(GL_ELEMENT_ARRAY_BUFFER, quadindexes);
return;
}
-
glGenBuffers_(1, &quadindexes);
ushort *data = new ushort[MAXQUADS*6], *dst = data;
- for(int idx = 0; idx < MAXQUADS*4; idx += 4, dst += 6)
- {
+ for(int idx = 0; idx < MAXQUADS*4; idx += 4, dst += 6) {
dst[0] = idx;
dst[1] = idx + 1;
dst[2] = idx + 2;
@@ -71,36 +54,25 @@ namespace gle
glBufferData_(GL_ELEMENT_ARRAY_BUFFER, MAXQUADS*6*sizeof(ushort), data, GL_STATIC_DRAW);
delete[] data;
}
-
- void disablequads()
- {
+ void disablequads() {
quadsenabled = false;
-
if(glversion < 300) return;
-
glBindBuffer_(GL_ELEMENT_ARRAY_BUFFER, 0);
}
-
- void drawquads(int offset, int count)
- {
+ void drawquads(int offset, int count) {
if(count <= 0) return;
- if(glversion < 300)
- {
+ if(glversion < 300) {
glDrawArrays(GL_QUADS, offset*4, count*4);
return;
}
- if(offset + count > MAXQUADS)
- {
+ if(offset + count > MAXQUADS) {
if(offset >= MAXQUADS) return;
count = MAXQUADS - offset;
}
glDrawRangeElements_(GL_TRIANGLES, offset*4, (offset + count)*4-1, count*6, GL_UNSIGNED_SHORT, (ushort *)0 + offset*6);
}
-
- void defattrib(int type, int size, int format)
- {
- if(type == ATTRIB_VERTEX)
- {
+ void defattrib(int type, int size, int format) {
+ if(type == ATTRIB_VERTEX) {
numattribs = attribmask = 0;
vertexsize = 0;
}
@@ -110,8 +82,7 @@ namespace gle
a.type = type;
a.size = size;
a.format = format;
- switch(format)
- {
+ switch(format) {
case 'B': case GL_UNSIGNED_BYTE: a.formatsize = 1; a.format = GL_UNSIGNED_BYTE; break;
case 'b': case GL_BYTE: a.formatsize = 1; a.format = GL_BYTE; break;
case 'S': case GL_UNSIGNED_SHORT: a.formatsize = 2; a.format = GL_UNSIGNED_SHORT; break;
@@ -126,14 +97,10 @@ namespace gle
a.offset = vertexsize;
vertexsize += a.formatsize;
}
-
- void defattribs(const char *fmt)
- {
- for(;; fmt += 3)
- {
+ void defattribs(const char *fmt) {
+ for(;; fmt += 3) {
GLenum format;
- switch(fmt[0])
- {
+ switch(fmt[0]) {
case 'v': format = ATTRIB_VERTEX; break;
case 'c': format = ATTRIB_COLOR; break;
case 't': format = ATTRIB_TEXCOORD0; break;
@@ -147,11 +114,8 @@ namespace gle
defattrib(format, fmt[1]-'0', fmt[2]);
}
}
-
- static inline void setattrib(const attribinfo &a, uchar *buf)
- {
- switch(a.type)
- {
+ static inline void setattrib(const attribinfo &a, uchar *buf) {
+ switch(a.type) {
case ATTRIB_VERTEX:
case ATTRIB_TEXCOORD0:
case ATTRIB_TEXCOORD1:
@@ -165,36 +129,27 @@ namespace gle
glVertexAttribPointer_(a.type, a.size, a.format, GL_TRUE, vertexsize, buf);
break;
}
- if(!(enabled&(1<<a.type)))
- {
+ if(!(enabled&(1<<a.type))) {
glEnableVertexAttribArray_(a.type);
enabled |= 1<<a.type;
}
}
-
- static inline void unsetattrib(const attribinfo &a)
- {
+ static inline void unsetattrib(const attribinfo &a) {
glDisableVertexAttribArray_(a.type);
enabled &= ~(1<<a.type);
}
-
- static inline void setattribs(uchar *buf)
- {
+ static inline void setattribs(uchar *buf) {
bool forceattribs = numattribs != numlastattribs || vertexsize != lastvertexsize || buf != lastbuf;
- if(forceattribs || changedattribs)
- {
+ if(forceattribs || changedattribs) {
int diffmask = enabled & lastattribmask & ~attribmask;
- if(diffmask) loopi(numlastattribs)
- {
+ if(diffmask) loopi(numlastattribs) {
const attribinfo &a = lastattribs[i];
if(diffmask & (1<<a.type)) unsetattrib(a);
}
uchar *src = buf;
- loopi(numattribs)
- {
+ loopi(numattribs) {
const attribinfo &a = attribdefs[i];
- if(forceattribs || a != lastattribs[i])
- {
+ if(forceattribs || a != lastattribs[i]) {
setattrib(a, src);
lastattribs[i] = a;
}
@@ -207,20 +162,14 @@ namespace gle
changedattribs = false;
}
}
-
- void begin(GLenum mode)
- {
+ void begin(GLenum mode) {
primtype = mode;
}
-
- void begin(GLenum mode, int numverts)
- {
+ void begin(GLenum mode, int numverts) {
primtype = mode;
- if(glversion >= 300 && !intel_mapbufferrange_bug)
- {
+ if(glversion >= 300 && !intel_mapbufferrange_bug) {
int len = numverts * vertexsize;
- if(vbooffset + len >= MAXVBOSIZE)
- {
+ if(vbooffset + len >= MAXVBOSIZE) {
len = min(len, MAXVBOSIZE);
if(!vbo) glGenBuffers_(1, &vbo);
glBindBuffer_(GL_ARRAY_BUFFER, vbo);
@@ -232,37 +181,27 @@ namespace gle
if(buf) attribbuf.reset((uchar *)buf, len);
}
}
-
- void multidraw()
- {
+ void multidraw() {
int start = multidrawstart.length() ? multidrawstart.last() + multidrawcount.last() : 0,
count = attribbuf.length()/vertexsize - start;
- if(count > 0)
- {
+ if(count > 0) {
multidrawstart.add(start);
multidrawcount.add(count);
}
}
-
- int end()
- {
+ int end() {
uchar *buf = attribbuf.getbuf();
- if(attribbuf.empty())
- {
- if(buf != attribdata)
- {
+ if(attribbuf.empty()) {
+ if(buf != attribdata) {
glUnmapBuffer_(GL_ARRAY_BUFFER);
attribbuf.reset(attribdata, MAXVBOSIZE);
}
return 0;
}
int start = 0;
- if(glversion >= 300)
- {
- if(buf == attribdata)
- {
- if(vbooffset + attribbuf.length() >= MAXVBOSIZE)
- {
+ if(glversion >= 300) {
+ if(buf == attribdata) {
+ if(vbooffset + attribbuf.length() >= MAXVBOSIZE) {
if(!vbo) glGenBuffers_(1, &vbo);
glBindBuffer_(GL_ARRAY_BUFFER, vbo);
glBufferData_(GL_ARRAY_BUFFER, MAXVBOSIZE, NULL, GL_STREAM_DRAW);
@@ -271,8 +210,7 @@ namespace gle
else if(!lastvertexsize) glBindBuffer_(GL_ARRAY_BUFFER, vbo);
void *dst = intel_mapbufferrange_bug ? NULL :
glMapBufferRange_(GL_ARRAY_BUFFER, vbooffset, attribbuf.length(), GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_RANGE_BIT|GL_MAP_UNSYNCHRONIZED_BIT);
- if(dst)
- {
+ if(dst) {
memcpy(dst, attribbuf.getbuf(), attribbuf.length());
glUnmapBuffer_(GL_ARRAY_BUFFER);
}
@@ -280,8 +218,7 @@ namespace gle
}
else glUnmapBuffer_(GL_ARRAY_BUFFER);
buf = (uchar *)0 + vbooffset;
- if(vertexsize == lastvertexsize && buf >= lastbuf)
- {
+ if(vertexsize == lastvertexsize && buf >= lastbuf) {
start = int(buf - lastbuf)/vertexsize;
if(primtype == GL_QUADS && (start%4 || start + attribbuf.length()/vertexsize >= 4*MAXQUADS))
start = 0;
@@ -291,11 +228,9 @@ namespace gle
}
setattribs(buf);
int numvertexes = attribbuf.length()/vertexsize;
- if(primtype == GL_QUADS)
- {
+ if(primtype == GL_QUADS) {
if(!quadsenabled) enablequads();
- for(int quads = numvertexes/4;;)
- {
+ for(int quads = numvertexes/4;;) {
int count = min(quads, MAXQUADS);
drawquads(start/4, count);
quads -= count;
@@ -304,10 +239,8 @@ namespace gle
start = 0;
}
}
- else
- {
- if(multidrawstart.length())
- {
+ else {
+ if(multidrawstart.length()) {
multidraw();
if(start) loopv(multidrawstart) multidrawstart[i] += start;
glMultiDrawArrays_(primtype, multidrawstart.getbuf(), multidrawcount.getbuf(), multidrawstart.length());
@@ -319,36 +252,26 @@ namespace gle
attribbuf.reset(attribdata, MAXVBOSIZE);
return numvertexes;
}
-
- void forcedisable()
- {
+ void forcedisable() {
for(int i = 0; enabled; i++) if(enabled&(1<<i)) { glDisableVertexAttribArray_(i); enabled &= ~(1<<i); }
numlastattribs = lastattribmask = lastvertexsize = 0;
lastbuf = NULL;
if(quadsenabled) disablequads();
if(glversion >= 300) glBindBuffer_(GL_ARRAY_BUFFER, 0);
}
-
- void setup()
- {
- if(glversion >= 300)
- {
+ void setup() {
+ if(glversion >= 300) {
if(!defaultvao) glGenVertexArrays_(1, &defaultvao);
glBindVertexArray_(defaultvao);
}
attribdata = new uchar[MAXVBOSIZE];
attribbuf.reset(attribdata, MAXVBOSIZE);
}
-
- void cleanup()
- {
+ void cleanup() {
disable();
-
if(quadindexes) { glDeleteBuffers_(1, &quadindexes); quadindexes = 0; }
-
if(vbo) { glDeleteBuffers_(1, &vbo); vbo = 0; }
vbooffset = MAXVBOSIZE;
-
if(defaultvao) { glDeleteVertexArrays_(1, &defaultvao); defaultvao = 0; }
}
}