--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include "arraystate.h"
+#include "bufferstate.h"
+
+ArrayState::ArrayState():
+ kind(0),
+ index(0),
+ enabled(false),
+ size(4),
+ type(GL_FLOAT),
+ normalized(false),
+ stride(0),
+ buffer(0),
+ pointer(0)
+{ }
+
+void ArrayState::set(unsigned s, GLenum t, bool n, unsigned r, BufferState *b, long p)
+{
+ size = s;
+ type = t;
+ stride = r;
+ normalized = n;
+ buffer = b;
+ pointer = p;
+
+ if(buffer)
+ buffer->content.update(*this);
+}
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef ARRAYSTATE_H_
+#define ARRAYSTATE_H_
+
+#include "opengl.h"
+
+struct BufferState;
+
+struct ArrayState
+{
+ GLenum kind;
+ unsigned index;
+ bool enabled;
+ unsigned size;
+ GLenum type;
+ bool normalized;
+ unsigned stride;
+ BufferState *buffer;
+ long pointer;
+
+ ArrayState();
+ void set(unsigned, GLenum, bool, unsigned, BufferState *, long);
+};
+
+#endif
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <msp/strings/formatter.h>
+#include "arraystate.h"
+#include "bufferstate.h"
+#include "enums.h"
+
+using namespace std;
+using namespace Msp;
+
+BufferContent::BufferContent():
+ consistent(true),
+ stride(0)
+{ }
+
+void BufferContent::update(const ArrayState &array)
+{
+ if(array.stride!=stride)
+ consistent = false;
+
+ stride = array.stride;
+
+ for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
+ if(i->kind==array.kind && i->index==array.index)
+ {
+ if(array.size!=i->size || array.type!=i->type || array.pointer!=i->offset)
+ {
+ consistent = false;
+ arrays.erase(i);
+ }
+ else
+ return;
+
+
+ break;
+ }
+
+ vector<Array>::iterator place = arrays.end();
+ for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
+ if(i->offset>array.pointer)
+ {
+ place = i;
+ break;
+ }
+
+ arrays.insert(place, array);
+}
+
+string BufferContent::describe() const
+{
+ if(arrays.empty())
+ return "(unknown)";
+
+ string result;
+ for(vector<Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
+ {
+ char kind = '?';
+ if(i->kind==GL_VERTEX_ARRAY)
+ kind = 'V';
+ else if(i->kind==GL_NORMAL_ARRAY)
+ kind = 'N';
+ else if(i->kind==GL_COLOR_ARRAY)
+ kind = 'C';
+ else if(i->kind==GL_TEXTURE_COORD_ARRAY && i->index==0)
+ kind = 'T';
+
+ char type[3] = { '?', 0, 0 };
+ if(i->type==GL_FLOAT)
+ type[0] = 'F';
+ else if(i->type==GL_DOUBLE)
+ type[0] = 'D';
+ else if(i->type==GL_INT || i->type==GL_UNSIGNED_INT)
+ type[0] = 'I';
+ else if(i->type==GL_SHORT || i->type==GL_UNSIGNED_SHORT)
+ type[0] = 'S';
+ else if(i->type==GL_BYTE || i->type==GL_UNSIGNED_BYTE)
+ type[0] = 'B';
+
+ if(i->type==GL_UNSIGNED_INT || i->type==GL_UNSIGNED_SHORT || i->type==GL_UNSIGNED_BYTE)
+ {
+ type[1] = type[0];
+ type[0] = 'U';
+ }
+
+ if(!result.empty())
+ result += '_';
+ result += format("%c%d%s", kind, i->size, type);
+ }
+
+ return result;
+}
+
+
+BufferContent::Array::Array(const ArrayState &a):
+ kind(a.kind),
+ index(a.index),
+ size(a.size),
+ type(a.type),
+ offset(a.pointer)
+{ }
+
+
+BufferState::BufferState():
+ usage(GL_STATIC_DRAW),
+ size(0),
+ data(0)
+{ }
+
+void BufferState::set_data(unsigned sz, const void *ptr, GLenum use)
+{
+ usage = use;
+ size = sz;
+ delete[] data;
+ data = new char[size];
+ if(ptr)
+ set_sub_data(0, size, ptr);
+ content = BufferContent();
+}
+
+void BufferState::set_sub_data(unsigned off, unsigned sz, const void *ptr)
+{
+ if(data && off+sz<=size)
+ {
+ const char *cptr = reinterpret_cast<const char *>(ptr);
+ copy(cptr, cptr+sz, data+off);
+ }
+}
+
+string BufferState::describe() const
+{
+ if(content.stride)
+ return format("%s, %d vertices (%d bytes), %s",
+ content.describe(), size/content.stride, size, describe_enum(usage, ""));
+ else
+ return format("%d bytes, %s", size, describe_enum(usage, ""));
+}
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef BUFFERSTATE_H_
+#define BUFFERSTATE_H_
+
+#include <string>
+#include <vector>
+#include "opengl.h"
+
+struct ArrayState;
+
+struct BufferContent
+{
+ struct Array
+ {
+ GLenum kind;
+ unsigned index;
+ unsigned size;
+ GLenum type;
+ int offset;
+
+ Array(const ArrayState &);
+ };
+
+ bool consistent;
+ unsigned stride;
+ std::vector<Array> arrays;
+
+ BufferContent();
+ void update(const ArrayState &);
+ std::string describe() const;
+};
+
+struct BufferState
+{
+ unsigned id;
+ GLenum target;
+ GLenum usage;
+ unsigned size;
+ char *data;
+ BufferContent content;
+
+ BufferState();
+ void set_data(unsigned, const void *, GLenum);
+ void set_sub_data(unsigned, unsigned, const void *);
+ std::string describe() const;
+};
+
+#endif
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <msp/core/except.h>
+#include "glstate.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace {
+
+template<typename T>
+float normalize(T v);
+
+template<>
+float normalize<GLubyte>(GLubyte v)
+{ return v/255.0f; }
+
+template<>
+float normalize<GLbyte>(GLbyte v)
+{ return (2*v+1)/255.0f; }
+
+template<typename T, void (*func)(void *, float)>
+void wrap_normalized(void *user_data, T v0)
+{ func(user_data, v0); }
+
+template<typename T, void (*func)(void *, float, float)>
+void wrap_normalized(void *user_data, T v0, T v1)
+{ func(user_data, v0, v1); }
+
+template<typename T, void (*func)(void *, float, float, float)>
+void wrap_normalized(void *user_data, T v0, T v1, T v2)
+{ func(user_data, v0, v1, v2); }
+
+template<typename T, void (*func)(void *, float, float, float, float)>
+void wrap_normalized(void *user_data, T v0, T v1, T v2, T v3)
+{ func(user_data, v0, v1, v2, v3); }
+
+template<typename T, void (*func)(void *, T)>
+void wrap_array(void *user_data, const T *v)
+{ func(user_data, v[0]); }
+
+template<typename T, void (*func)(void *, T, T)>
+void wrap_array(void *user_data, const T *v)
+{ func(user_data, v[0], v[1]); }
+
+template<typename T, void (*func)(void *, T, T, T)>
+void wrap_array(void *user_data, const T *v)
+{ func(user_data, v[0], v[1], v[2]); }
+
+template<typename T, void (*func)(void *, T, T, T, T)>
+void wrap_array(void *user_data, const T *v)
+{ func(user_data, v[0], v[1], v[2], v[3]); }
+
+}
+
+
+Vector3::Vector3():
+ x(0), y(0), z(0)
+{ }
+
+
+Vector4::Vector4():
+ x(0), y(0), z(0), w(1)
+{ }
+
+
+GlState::GlState():
+ decoder(gldecoder_new(this, NULL)),
+ active_tex(0),
+ client_active_tex(0),
+ array_buffer(0),
+ element_buffer(0)
+{
+ vertex_array.kind = GL_VERTEX_ARRAY;
+ normal_array.kind = GL_NORMAL_ARRAY;
+ color_array.kind = GL_COLOR_ARRAY;
+ for(unsigned i=0; i<8; ++i)
+ {
+ texcoord_arrays[i].kind = GL_TEXTURE_COORD_ARRAY;
+ texcoord_arrays[i].index = i;
+ }
+
+ decoder->glColor3ub = wrap_normalized<GLubyte, glColor3f>;
+ decoder->glColor3ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor3f> >;
+ decoder->glColor4ub = wrap_normalized<GLubyte, glColor4f>;
+ decoder->glColor4ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor4f> >;
+ decoder->glColor3f = glColor3f;
+ decoder->glColor3fv = wrap_array<float, glColor3f>;
+ decoder->glColor4f = glColor4f;
+ decoder->glColor4fv = wrap_array<float, glColor4f>;
+ decoder->glTexCoord1f = glTexCoord1f;
+ decoder->glTexCoord1fv = wrap_array<float, glTexCoord1f>;
+ decoder->glTexCoord2f = glTexCoord2f;
+ decoder->glTexCoord2fv = wrap_array<float, glTexCoord2f>;
+ decoder->glTexCoord3f = glTexCoord3f;
+ decoder->glTexCoord3fv = wrap_array<float, glTexCoord3f>;
+ decoder->glTexCoord4f = glTexCoord4f;
+ decoder->glTexCoord4fv = wrap_array<float, glTexCoord4f>;
+ decoder->glNormal3f = glNormal3f;
+ decoder->glNormal3fv = wrap_array<float, glNormal3f>;
+
+ decoder->glActiveTexture = glActiveTexture;
+ decoder->glActiveTextureARB = glActiveTexture;
+ decoder->glBindTexture = glBindTexture;
+ decoder->glTexImage2D = glTexImage2D;
+ decoder->glTexParameteri = glTexParameteri;
+ decoder->glTexParameteriv = glTexParameteriv;
+ decoder->glDeleteTextures = glDeleteTextures;
+
+ decoder->glVertexPointer = glVertexPointer;
+ decoder->glNormalPointer = glNormalPointer;
+ decoder->glColorPointer = glColorPointer;
+ decoder->glClientActiveTexture = glClientActiveTexture;
+ decoder->glClientActiveTextureARB = glClientActiveTexture;
+ decoder->glTexCoordPointer = glTexCoordPointer;
+
+ decoder->glBindBuffer = glBindBuffer;
+ decoder->glBindBufferARB = glBindBuffer;
+ decoder->glBufferData = glBufferData;
+ decoder->glBufferDataARB = glBufferData;
+ decoder->glBufferSubData = glBufferSubData;
+ decoder->glBufferSubDataARB = glBufferSubData;
+ decoder->glDeleteBuffers = glDeleteBuffers;
+ decoder->glDeleteBuffersARB = glDeleteBuffers;
+}
+
+GlState::~GlState()
+{
+ gldecoder_delete(decoder);
+}
+
+int GlState::decode(const char *data, unsigned len)
+{
+ return gldecoder_decode(decoder, data, len);
+}
+
+const TextureState &GlState::get_texture(unsigned id) const
+{
+ TextureMap::const_iterator i = textures.find(id);
+ if(i==textures.end())
+ throw KeyError("Unknown texture");
+ return i->second;
+}
+
+const BufferState &GlState::get_buffer(unsigned id) const
+{
+ BufferMap::const_iterator i = buffers.find(id);
+ if(i==buffers.end())
+ throw KeyError("Unknown buffer");
+ return i->second;
+}
+
+const BufferState *GlState::get_current_buffer(GLenum target) const
+{
+ return const_cast<GlState *>(this)->get_current_buffer(target);
+}
+
+bool &GlState::get_boolean_state(GLenum state)
+{
+ if(state==GL_VERTEX_ARRAY)
+ return vertex_array.enabled;
+ else if(state==GL_NORMAL_ARRAY)
+ return normal_array.enabled;
+ else if(state==GL_COLOR_ARRAY)
+ return color_array.enabled;
+ else if(state==GL_TEXTURE_COORD_ARRAY)
+ return texcoord_arrays[client_active_tex].enabled;
+
+ static bool dummy;
+ return dummy;
+}
+
+TextureState *GlState::get_current_texture(GLenum target)
+{
+ return texunits[active_tex].get_current_texture(target);
+}
+
+BufferState *GlState::get_current_buffer(GLenum target)
+{
+ if(target==GL_ARRAY_BUFFER)
+ return array_buffer;
+ else if(target==GL_ELEMENT_ARRAY_BUFFER)
+ return element_buffer;
+ return 0;
+}
+
+const ArrayState &GlState::get_array(GLenum array) const
+{
+ if(array==GL_VERTEX_ARRAY)
+ return vertex_array;
+ else if(array==GL_NORMAL_ARRAY)
+ return normal_array;
+ else if(array==GL_COLOR_ARRAY)
+ return color_array;
+ else if(array==GL_TEXTURE_COORD_ARRAY)
+ return texcoord_arrays[client_active_tex];
+ else
+ throw InvalidParameterValue("Invalid array");
+}
+
+const ArrayState &GlState::get_texture_coord_array(unsigned index) const
+{
+ return texcoord_arrays[index];
+}
+
+const ArrayState &GlState::get_attrib_array(unsigned index) const
+{
+ map<unsigned, ArrayState>::const_iterator i = attrib_arrays.find(index);
+ if(i!=attrib_arrays.end())
+ return i->second;
+
+ // XXX Return a dummy?
+ throw KeyError("Unknown attribute array");
+}
+
+void GlState::set_current_texture(GLenum target, unsigned id)
+{
+ TexUnitState &unit = texunits[active_tex];
+ TextureState *tex = 0;
+ if(id)
+ {
+ tex = &textures[id];
+ if(!tex->id)
+ {
+ tex->id = id;
+ tex->target = target;
+ }
+ else if(tex->target!=target)
+ return;
+ }
+ unit.set_current_texture(target, tex);
+}
+
+void GlState::set_current_buffer(GLenum target, unsigned id)
+{
+ BufferState *buf = 0;
+ if(id)
+ {
+ buf = &buffers[id];
+ if(!buf->id)
+ buf->id = id;
+ }
+
+ if(target==GL_ARRAY_BUFFER)
+ array_buffer = buf;
+ else if(target==GL_ELEMENT_ARRAY_BUFFER)
+ element_buffer = buf;
+}
+
+// Boolean state
+
+void GlState::glEnableClientState(void *user_data, GLenum state)
+{
+ reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = true;
+}
+
+void GlState::glDisableClientState(void *user_data, GLenum state)
+{
+ reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = false;
+}
+
+void GlState::glEnableVertexAttribArray(void *user_data, unsigned index)
+{
+ reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = true;
+}
+
+void GlState::glDisableVertexAttribArray(void *user_data, unsigned index)
+{
+ reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = false;
+}
+
+// Vertex attributes
+
+void GlState::glColor3f(void *user_data, float r, float g, float b)
+{ glColor4f(user_data, r, g, b, 1.0f); }
+
+void GlState::glColor4f(void *user_data, float r, float g, float b, float a)
+{
+ Vector4 &color = reinterpret_cast<GlState *>(user_data)->color;
+ color.r = r;
+ color.g = g;
+ color.b = b;
+ color.a = a;
+}
+
+void GlState::glNormal3f(void *user_data, float x, float y, float z)
+{
+ Vector3 &normal = reinterpret_cast<GlState *>(user_data)->normal;
+ normal.x = x;
+ normal.y = y;
+ normal.z = z;
+}
+
+void GlState::glTexCoord1f(void *user_data, float s)
+{ glTexCoord4f(user_data, s, 0.0f, 0.0f, 1.0f); }
+
+void GlState::glTexCoord2f(void *user_data, float s, float t)
+{ glTexCoord4f(user_data, s, t, 0.0f, 1.0f); }
+
+void GlState::glTexCoord3f(void *user_data, float s, float t, float p)
+{ glTexCoord4f(user_data, s, t, p, 1.0f); }
+
+void GlState::glTexCoord4f(void *user_data, float s, float t, float p, float q)
+{
+ unsigned index = reinterpret_cast<GlState *>(user_data)->active_tex;
+ glMultiTexCoord4f(user_data, index, s, t, p, q);
+}
+
+void GlState::glMultiTexCoord4f(void *user_data, unsigned index, float s, float t, float p, float q)
+{
+ Vector4 &texcoord = reinterpret_cast<GlState *>(user_data)->texcoord[index];
+ texcoord.s = s;
+ texcoord.t = t;
+ texcoord.p = p;
+ texcoord.q = q;
+}
+
+// Textures
+
+void GlState::glActiveTexture(void *user_data, unsigned index)
+{
+ reinterpret_cast<GlState *>(user_data)->active_tex = index-GL_TEXTURE0;
+}
+
+void GlState::glBindTexture(void *user_data, GLenum target, unsigned id)
+{
+ reinterpret_cast<GlState *>(user_data)->set_current_texture(target, id);
+}
+
+void GlState::glTexImage2D(void *user_data, GLenum target, int level, int ifmt, int width, int height, int, GLenum, GLenum, const void *)
+{
+ if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
+ tex->set_image_2d(level, ifmt, width, height);
+}
+
+void GlState::glTexParameteri(void *user_data, GLenum target, GLenum param, int value)
+{ glTexParameteriv(user_data, target, param, &value); }
+
+void GlState::glTexParameteriv(void *user_data, GLenum target, GLenum param, const int *values)
+{
+ if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
+ tex->set_parameter(param, values);
+}
+
+void GlState::glDeleteTextures(void *user_data, int count, const unsigned *ids)
+{
+ for(int i=0; i<count; ++i)
+ reinterpret_cast<GlState *>(user_data)->textures.erase(ids[i]);
+}
+
+// Vertex arrays
+
+void GlState::glVertexPointer(void *user_data, int size, GLenum type, int stride, const void *data)
+{
+ GlState *self = reinterpret_cast<GlState *>(user_data);
+ self->vertex_array.set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
+}
+
+void GlState::glNormalPointer(void *user_data, GLenum type, int stride, const void *data)
+{
+ GlState *self = reinterpret_cast<GlState *>(user_data);
+ self->normal_array.set(3, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
+}
+
+void GlState::glColorPointer(void *user_data, int size, GLenum type, int stride, const void *data)
+{
+ GlState *self = reinterpret_cast<GlState *>(user_data);
+ self->color_array.set(size, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
+}
+
+void GlState::glClientActiveTexture(void *user_data, unsigned index)
+{
+ reinterpret_cast<GlState *>(user_data)->client_active_tex = index-GL_TEXTURE0;
+}
+
+void GlState::glTexCoordPointer(void *user_data, int size, GLenum type, int stride, const void *data)
+{
+ GlState *self = reinterpret_cast<GlState *>(user_data);
+ self->texcoord_arrays[self->client_active_tex].set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
+}
+
+void GlState::glVertexAttribPointer(void *user_data, unsigned index, int size, GLenum type, int norm, int stride, const void *data)
+{
+ GlState *self = reinterpret_cast<GlState *>(user_data);
+ self->attrib_arrays[index].set(size, type, norm, stride, self->array_buffer, reinterpret_cast<long>(data));
+}
+
+// Buffer objects
+
+void GlState::glBindBuffer(void *user_data, GLenum target, unsigned id)
+{ reinterpret_cast<GlState *>(user_data)->set_current_buffer(target, id); }
+
+void GlState::glBufferData(void *user_data, GLenum target, int size, const void *data, GLenum usage)
+{
+ if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
+ buf->set_data(size, data, usage);
+}
+
+void GlState::glBufferSubData(void *user_data, GLenum target, int offset, int size, const void *data)
+{
+ if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
+ buf->set_sub_data(offset, size, data);
+}
+
+void GlState::glDeleteBuffers(void *user_data, int count, const unsigned *ids)
+{
+ for(int i=0; i<count; ++i)
+ reinterpret_cast<GlState *>(user_data)->buffers.erase(ids[i]);
+}
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef GLSTATE_H_
+#define GLSTATE_H_
+
+#include <map>
+#include <vector>
+#include "arraystate.h"
+#include "bufferstate.h"
+#include "gldecoder.h"
+#include "texturestate.h"
+
+struct Vector3
+{
+ float x, y, z;
+
+ Vector3();
+};
+
+struct Vector4
+{
+ union { float x, r, s; };
+ union { float y, g, t; };
+ union { float z, b, p; };
+ union { float w, a, q; };
+
+ Vector4();
+};
+
+/**
+Tracks OpenGL state based on the command stream. This class is essentially a
+(partial) OpenGL implementation in itself.
+
+XXX Should determine the number of texture units the target implementation
+actually supports.
+*/
+class GlState
+{
+public:
+ typedef std::map<unsigned, TextureState> TextureMap;
+ typedef std::map<unsigned, BufferState> BufferMap;
+
+private:
+ GlDecoder *decoder;
+ Vector4 color;
+ Vector4 texcoord[8];
+ Vector3 normal;
+ unsigned active_tex;
+ unsigned client_active_tex;
+ TextureMap textures;
+ TexUnitState texunits[8];
+ BufferMap buffers;
+ BufferState *array_buffer;
+ BufferState *element_buffer;
+ ArrayState vertex_array;
+ ArrayState normal_array;
+ ArrayState color_array;
+ ArrayState texcoord_arrays[8];
+ std::map<unsigned, ArrayState> attrib_arrays;
+
+public:
+ GlState();
+ ~GlState();
+
+ int decode(const char *, unsigned);
+ const Vector4 &get_color() const { return color; }
+ const Vector4 &get_texcoord(unsigned i) const { return texcoord[i]; }
+ const Vector3 &get_normal() const { return normal; }
+ const TextureMap &get_textures() const { return textures; }
+ const TextureState &get_texture(unsigned) const;
+ const TexUnitState &get_texture_unit(unsigned i) const { return texunits[i]; }
+ const BufferMap &get_buffers() const { return buffers; }
+ const BufferState &get_buffer(unsigned) const;
+ const BufferState *get_current_buffer(GLenum) const;
+ const ArrayState &get_array(GLenum) const;
+ const ArrayState &get_texture_coord_array(unsigned) const;
+ const ArrayState &get_attrib_array(unsigned) const;
+private:
+ bool &get_boolean_state(GLenum);
+ TextureState *get_current_texture(GLenum);
+ BufferState *get_current_buffer(GLenum);
+ void set_current_texture(GLenum, unsigned);
+ void set_current_buffer(GLenum, unsigned);
+
+ static void glEnableClientState(void *, GLenum);
+ static void glDisableClientState(void *, GLenum);
+ static void glEnableVertexAttribArray(void *, unsigned);
+ static void glDisableVertexAttribArray(void *, unsigned);
+
+ static void glColor3f(void *, float, float, float);
+ static void glColor4f(void *, float, float, float, float);
+ static void glNormal3f(void *, float, float, float);
+ static void glTexCoord1f(void *, float);
+ static void glTexCoord2f(void *, float, float);
+ static void glTexCoord3f(void *, float, float, float);
+ static void glTexCoord4f(void *, float, float, float, float);
+ static void glMultiTexCoord4f(void *, unsigned, float, float, float, float);
+
+ static void glActiveTexture(void *, unsigned);
+ static void glBindTexture(void *, GLenum, unsigned);
+ static void glTexImage2D(void *, GLenum, int, int, int, int, int, GLenum, GLenum, const void *);
+ static void glTexParameteri(void *, GLenum, GLenum, int);
+ static void glTexParameteriv(void *, GLenum, GLenum, const int *);
+ static void glDeleteTextures(void *, int, const unsigned *);
+
+ static void glVertexPointer(void *, int, GLenum, int, const void *);
+ static void glNormalPointer(void *, GLenum, int, const void *);
+ static void glColorPointer(void *, int, GLenum, int, const void *);
+ static void glClientActiveTexture(void *, unsigned);
+ static void glTexCoordPointer(void *, int, GLenum, int, const void *);
+ static void glVertexAttribPointer(void *, unsigned, int, GLenum, int, int, const void *);
+
+ static void glBindBuffer(void *, GLenum, unsigned);
+ static void glBufferData(void *, GLenum, int, const void *, GLenum);
+ static void glBufferSubData(void *, GLenum, int, int, const void *);
+ static void glDeleteBuffers(void *, int, const unsigned *);
+};
+
+#endif
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <msp/io/print.h>
+#include "enums.h"
+#include "gldbg.h"
+#include "inspector.h"
+
+using namespace std;
+using namespace Msp;
+
+Inspector::Inspector(GlDbg &dbg)
+{
+ CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
+
+ cmd_interp.register_command("state", this, &Inspector::cmd_state)
+ .set_help("Inspects general GL state",
+ "state vertex\n"
+ " Print current vertex attributes\n\n"
+ "state bind\n"
+ " Show current bindings\n");
+
+ cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
+ .set_help("Inspect texture state",
+ "texture\n"
+ " Lists texture objects\n\n"
+ "texture ID\n"
+ " Print information about a texture object\n");
+
+ cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
+ .set_help("Inspect buffer object state",
+ "buffer\n"
+ " Lists buffer objects\n\n"
+ "buffer ID\n"
+ " Print information about a buffer object\n");
+}
+
+void Inspector::decode(const char *data, unsigned len)
+{
+ state.decode(data, len);
+}
+
+void Inspector::cmd_state(const string &args)
+{
+ const GlState &glstate = state;
+ if(args=="vertex")
+ {
+ IO::print("Current vertex attributes:\n");
+ const Vector4 &color = glstate.get_color();
+ IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
+ for(unsigned i=0; i<8; ++i)
+ {
+ const Vector4 &texcoord = glstate.get_texcoord(i);
+ IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
+ }
+ const Vector3 &normal = glstate.get_normal();
+ IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
+ }
+ else if(args=="bind")
+ {
+ IO::print("Current bindings:\n");
+ for(unsigned i=0; i<8; ++i)
+ {
+ IO::print(" Texture unit %d:\n", i);
+ const TexUnitState &unit = glstate.get_texture_unit(i);
+ IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
+ IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
+ }
+ IO::print(" Buffers:\n");
+ const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
+ IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
+ buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
+ IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
+ }
+ else
+ throw InvalidParameterValue("Invalid or missing argument");
+}
+
+void Inspector::cmd_texture(const string &args)
+{
+ if(args.empty())
+ {
+ const map<unsigned, TextureState> &textures = state.get_textures();
+ IO::print("%d texture objects:\n", textures.size());
+ for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
+ {
+ const TextureState &tex = i->second;
+ IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
+ }
+ }
+ else
+ {
+ unsigned id = lexical_cast<unsigned>(args);
+ const TextureState &tex = state.get_texture(id);
+ IO::print("Texture object %d\n", id);
+ IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
+ IO::print(" Images:\n");
+ for(unsigned i=0; i<tex.images.size(); ++i)
+ {
+ const TexImageState &img = tex.images[i];
+ IO::print(" Level %2d: %s\n", i, img.describe());
+ }
+ IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
+ IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
+ IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
+ describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
+ IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
+ IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
+ IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
+ }
+}
+
+void Inspector::cmd_buffer(const string &args)
+{
+ if(args.empty())
+ {
+ const GlState::BufferMap &buffers = state.get_buffers();
+ IO::print("%d buffers:\n", buffers.size());
+ for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
+ IO::print(" %d: %s\n", i->first, i->second.describe());
+ }
+ else
+ {
+ unsigned id = lexical_cast<unsigned>(args);
+ const BufferState &buf = state.get_buffer(id);
+ IO::print("Buffer %d:\n", id);
+ IO::print(" Size: %d bytes\n", buf.size);
+ IO::print(" Usage: %s\n", describe_enum(buf.usage, ""));
+ if(buf.content.stride)
+ {
+ IO::print(" Stride: %d bytes\n", buf.content.stride);
+
+ IO::print(" Arrays:\n");
+ const vector<BufferContent::Array> &arrays = buf.content.arrays;
+ for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
+ {
+ if(i->kind)
+ IO::print(" %2d: %s, %d %s\n", i->offset,
+ describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
+ else
+ IO::print(" %2d: Attrib %d, %d %s\n", i->offset,
+ i->index, i->size, describe_enum(i->type, "DataType"));
+ }
+
+ IO::print(" Data:\n");
+ string header;
+ for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
+ {
+ if(!header.empty())
+ header += " | ";
+
+ string label;
+ if(i->kind==GL_VERTEX_ARRAY)
+ label = "Vertex";
+ else if(i->kind==GL_NORMAL_ARRAY)
+ label = "Normal";
+ else if(i->kind==GL_COLOR_ARRAY)
+ label = "Color";
+ else if(i->kind==GL_TEXTURE_COORD_ARRAY)
+ {
+ if(i->size==1)
+ label = "TexC";
+ else
+ label = "TexCoord";
+ }
+ else if(!i->kind)
+ {
+ if(i->size==1)
+ label = format("A %d", i->index);
+ else
+ label = format("Attrib %d", i->index);
+ }
+
+ unsigned width = i->size;
+ if(i->type==GL_FLOAT)
+ width *= 5;
+ else if(i->type==GL_UNSIGNED_BYTE)
+ width *= 3;
+ width += i->size-1;
+
+ header.append((width-label.size())/2, ' ');
+ header += label;
+ header.append((width-label.size()+1)/2, ' ');
+ }
+ IO::print(" %s\n", header);
+
+ unsigned n_verts = buf.size/buf.content.stride;
+ for(unsigned i=0; i<n_verts; ++i)
+ {
+ const char *vertex = buf.data+i*buf.content.stride;
+
+ string line;
+ for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
+ {
+ if(!line.empty())
+ line += " |";
+
+ const char *base = vertex+j->offset;
+ for(unsigned k=0; k<j->size; ++k)
+ {
+ if(j->type==GL_FLOAT)
+ line += format(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
+ else if(j->type==GL_UNSIGNED_BYTE)
+ line += format(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
+ }
+ }
+
+ IO::print("%3d:%s\n", i, line);
+ }
+ }
+ }
+}
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef INSPECTOR_H_
+#define INSPECTOR_H_
+
+#include "glstate.h"
+#include "tool.h"
+
+class Inspector: public RegisteredTool<Inspector>
+{
+private:
+ GlState state;
+
+public:
+ Inspector(GlDbg &);
+
+ virtual void decode(const char *, unsigned);
+private:
+ void cmd_state(const std::string &);
+ void cmd_texture(const std::string &);
+ void cmd_buffer(const std::string &);
+};
+
+#endif
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <msp/io/print.h>
+#include <msp/time/units.h>
+#include <msp/time/utils.h>
+#include "gldbg.h"
+#include "profiler.h"
+
+using namespace std;
+using namespace Msp;
+
+Profiler::Profiler(GlDbg &dbg):
+ decoder(gldecoder_new(this, 0)),
+ enabled(false)
+{
+ dbg.get_command_interpreter().register_command("profile", this, &Profiler::cmd_profile)
+ .set_help("Profiles GL usage and performance",
+ "profile {on|off}\n"
+ " Enables or disables profiling\n");
+
+ decoder->glDrawArrays = glDrawArrays;
+ decoder->glDrawElements = glDrawElements;
+ decoder->glDrawRangeElements = glDrawRangeElements;
+ decoder->glXSwapBuffers = glXSwapBuffers;
+}
+
+void Profiler::decode(const char *data, unsigned len)
+{
+ if(enabled)
+ gldecoder_decode(decoder, data, len);
+}
+
+void Profiler::cmd_profile(const string &args)
+{
+ if(args.empty() || args=="on")
+ {
+ enabled = true;
+ frames = 0;
+ draw_calls = 0;
+ vertices = 0;
+ triangles = 0;
+ start = Msp::Time::now();
+ }
+ else if(args=="off")
+ enabled = false;
+ else
+ throw InvalidParameterValue("Invalid argument");
+}
+
+void Profiler::glDrawArrays(void *user_data, GLenum mode, int, int count)
+{
+ glDrawElements(user_data, mode, count, 0, 0);
+}
+
+void Profiler::glDrawElements(void *user_data, GLenum mode, int count, GLenum, const void *)
+{
+ Profiler *self = reinterpret_cast<Profiler *>(user_data);
+
+ ++self->draw_calls;
+ self->vertices += count;
+
+ switch(mode)
+ {
+ case GL_TRIANGLES:
+ self->triangles += count/3;
+ break;
+ case GL_TRIANGLE_STRIP:
+ case GL_TRIANGLE_FAN:
+ if(count>2)
+ self->triangles += count-2;
+ break;
+ case GL_QUADS:
+ self->triangles += count/4;
+ break;
+ case GL_QUAD_STRIP:
+ if(count>3)
+ self->triangles += count/2-1;
+ break;
+ }
+}
+
+void Profiler::glDrawRangeElements(void *user_data, GLenum mode, unsigned, unsigned, int count, GLenum type, const void *data)
+{
+ glDrawElements(user_data, mode, count, type, data);
+}
+
+void Profiler::glXSwapBuffers(void *user_data, Display *, GLXDrawable)
+{
+ Profiler *self = reinterpret_cast<Profiler *>(user_data);
+
+ IO::print("%d draw calls, %d vertices, %d triangles\n", self->draw_calls, self->vertices, self->triangles);
+ self->draw_calls = 0;
+ self->vertices = 0;
+ self->triangles = 0;
+
+ ++self->frames;
+ Msp::Time::TimeStamp t = Msp::Time::now();
+ Msp::Time::TimeDelta dt = t-self->start;
+ if(dt>Msp::Time::sec)
+ {
+ Msp::IO::print("%d frames in %s seconds\n", self->frames, dt);
+ self->start = t;
+ self->frames = 0;
+ }
+}
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2010 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef PROFILER_H_
+#define PROFILER_H_
+
+#include <msp/time/timestamp.h>
+#include "gldecoder.h"
+#include "tool.h"
+
+class Profiler: public RegisteredTool<Profiler>
+{
+private:
+ GlDecoder *decoder;
+ bool enabled;
+ Msp::Time::TimeStamp start;
+ unsigned frames;
+ unsigned draw_calls;
+ unsigned vertices;
+ unsigned triangles;
+
+public:
+ Profiler(GlDbg &);
+
+ virtual void decode(const char *, unsigned);
+private:
+ void cmd_profile(const std::string &);
+
+ static void glDrawArrays(void *, GLenum, int, int);
+ static void glDrawElements(void *, GLenum, int, GLenum, const void *);
+ static void glDrawRangeElements(void *, GLenum, unsigned, unsigned, int, GLenum, const void *);
+ static void glXSwapBuffers(void *, Display *, GLXDrawable);
+};
+
+#endif
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#include <msp/strings/formatter.h>
+#include "enums.h"
+#include "texturestate.h"
+
+using namespace std;
+using namespace Msp;
+
+TexImageState::TexImageState():
+ width(0),
+ height(0),
+ depth(0),
+ internal_format(0)
+{ }
+
+void TexImageState::set_2d(GLenum ifmt, unsigned wd, unsigned ht)
+{
+ width = wd;
+ height = ht;
+ depth = 0;
+ internal_format = ifmt;
+}
+
+string TexImageState::describe() const
+{
+ string descr = format("%d", width);
+ if(height)
+ {
+ descr += format("x%d", height);
+ if(depth)
+ descr += format("x%d", height);
+ }
+ descr += format(", %s", describe_enum(internal_format, "PixelFormat"));
+ return descr;
+}
+
+
+TextureState::TextureState():
+ id(0),
+ target(0),
+ min_filter(GL_LINEAR_MIPMAP_LINEAR),
+ mag_filter(GL_LINEAR),
+ wrap_s(GL_REPEAT),
+ wrap_t(GL_REPEAT),
+ wrap_r(GL_REPEAT),
+ compare_mode(GL_NONE),
+ compare_func(GL_LEQUAL),
+ generate_mipmap(false)
+{ }
+
+void TextureState::set_image_2d(unsigned level, GLenum ifmt, unsigned wd, unsigned ht)
+{
+ while(1)
+ {
+ if(images.size()<=level)
+ images.resize(level+1);
+ images[level].set_2d(ifmt, wd, ht);
+
+ if(generate_mipmap)
+ {
+ if(wd==1 && ht==1)
+ break;
+ ++level;
+ if(wd>1)
+ wd /= 2;
+ if(ht>1)
+ ht /= 2;
+ }
+ else
+ break;
+ }
+}
+
+void TextureState::set_parameter(GLenum pname, const int *values)
+{
+ if(pname==GL_TEXTURE_MIN_FILTER)
+ min_filter = values[0];
+ else if(pname==GL_TEXTURE_MAG_FILTER)
+ mag_filter = values[0];
+ else if(pname==GL_TEXTURE_WRAP_S)
+ wrap_s = values[0];
+ else if(pname==GL_TEXTURE_WRAP_T)
+ wrap_t = values[0];
+ else if(pname==GL_TEXTURE_WRAP_R)
+ wrap_r = values[0];
+ else if(pname==GL_TEXTURE_COMPARE_MODE)
+ compare_mode = values[0];
+ else if(pname==GL_TEXTURE_COMPARE_FUNC)
+ compare_func = values[0];
+ else if(pname==GL_GENERATE_MIPMAP)
+ generate_mipmap = values[0];
+}
+
+string TextureState::describe() const
+{
+ string descr = describe_enum(target, "TextureTarget");
+ if(images.empty())
+ descr += ", undefined";
+ else
+ descr += format(", %s", images.front().describe());
+ return descr;
+}
+
+
+TexUnitState::TexUnitState():
+ current_2d(0),
+ current_3d(0)
+{ }
+
+void TexUnitState::set_current_texture(GLenum target, TextureState *tex)
+{
+ if(target==GL_TEXTURE_2D)
+ current_2d = tex;
+ else if(target==GL_TEXTURE_3D)
+ current_3d = tex;
+}
+
+TextureState *TexUnitState::get_current_texture(GLenum target)
+{
+ if(target==GL_TEXTURE_2D)
+ return current_2d;
+ else if(target==GL_TEXTURE_3D)
+ return current_3d;
+ else
+ return 0;
+}
+
+const TextureState *TexUnitState::get_current_texture(GLenum target) const
+{
+ return const_cast<TexUnitState *>(this)->get_current_texture(target);
+}
+
+string TexUnitState::describe_binding(GLenum target) const
+{
+ if(const TextureState *tex = get_current_texture(target))
+ {
+ string descr = format("%d ", tex->id);
+ if(tex->images.empty())
+ descr += "(undefined)";
+ else
+ descr += format("(%s)", tex->images.front().describe());
+ return descr;
+ }
+ else
+ return "None";
+}
--- /dev/null
+/* $Id$
+
+This file is part of gldbg
+Copyright © 2009 Mikko Rasa, Mikkosoft Productions
+Distributed under the GPL
+*/
+
+#ifndef TEXTURESTATE_H_
+#define TEXTURESTATE_H_
+
+#include <string>
+#include <vector>
+#include "opengl.h"
+#include "autoconstptr.h"
+
+struct TexImageState
+{
+ unsigned width;
+ unsigned height;
+ unsigned depth;
+ GLenum internal_format;
+
+ TexImageState();
+ void set_2d(GLenum, unsigned, unsigned);
+ std::string describe() const;
+};
+
+struct TextureState
+{
+ unsigned id;
+ GLenum target;
+ std::vector<TexImageState> images;
+ GLenum min_filter;
+ GLenum mag_filter;
+ GLenum wrap_s;
+ GLenum wrap_t;
+ GLenum wrap_r;
+ GLenum compare_mode;
+ GLenum compare_func;
+ bool generate_mipmap;
+
+ TextureState();
+ void set_image_2d(unsigned, GLenum, unsigned, unsigned);
+ void set_parameter(GLenum, const int *);
+ std::string describe() const;
+};
+
+struct TexUnitState
+{
+ AutoConstPtr<TextureState> current_2d;
+ AutoConstPtr<TextureState> current_3d;
+
+ TexUnitState();
+
+ void set_current_texture(GLenum, TextureState *);
+ TextureState *get_current_texture(GLenum);
+ const TextureState *get_current_texture(GLenum) const;
+ std::string describe_binding(GLenum) const;
+};
+
+#endif
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include "arraystate.h"
-#include "bufferstate.h"
-
-ArrayState::ArrayState():
- kind(0),
- index(0),
- enabled(false),
- size(4),
- type(GL_FLOAT),
- normalized(false),
- stride(0),
- buffer(0),
- pointer(0)
-{ }
-
-void ArrayState::set(unsigned s, GLenum t, bool n, unsigned r, BufferState *b, long p)
-{
- size = s;
- type = t;
- stride = r;
- normalized = n;
- buffer = b;
- pointer = p;
-
- if(buffer)
- buffer->content.update(*this);
-}
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#ifndef ARRAYSTATE_H_
-#define ARRAYSTATE_H_
-
-#include "opengl.h"
-
-struct BufferState;
-
-struct ArrayState
-{
- GLenum kind;
- unsigned index;
- bool enabled;
- unsigned size;
- GLenum type;
- bool normalized;
- unsigned stride;
- BufferState *buffer;
- long pointer;
-
- ArrayState();
- void set(unsigned, GLenum, bool, unsigned, BufferState *, long);
-};
-
-#endif
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/strings/formatter.h>
-#include "arraystate.h"
-#include "bufferstate.h"
-#include "enums.h"
-
-using namespace std;
-using namespace Msp;
-
-BufferContent::BufferContent():
- consistent(true),
- stride(0)
-{ }
-
-void BufferContent::update(const ArrayState &array)
-{
- if(array.stride!=stride)
- consistent = false;
-
- stride = array.stride;
-
- for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
- if(i->kind==array.kind && i->index==array.index)
- {
- if(array.size!=i->size || array.type!=i->type || array.pointer!=i->offset)
- {
- consistent = false;
- arrays.erase(i);
- }
- else
- return;
-
-
- break;
- }
-
- vector<Array>::iterator place = arrays.end();
- for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
- if(i->offset>array.pointer)
- {
- place = i;
- break;
- }
-
- arrays.insert(place, array);
-}
-
-string BufferContent::describe() const
-{
- if(arrays.empty())
- return "(unknown)";
-
- string result;
- for(vector<Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
- {
- char kind = '?';
- if(i->kind==GL_VERTEX_ARRAY)
- kind = 'V';
- else if(i->kind==GL_NORMAL_ARRAY)
- kind = 'N';
- else if(i->kind==GL_COLOR_ARRAY)
- kind = 'C';
- else if(i->kind==GL_TEXTURE_COORD_ARRAY && i->index==0)
- kind = 'T';
-
- char type[3] = { '?', 0, 0 };
- if(i->type==GL_FLOAT)
- type[0] = 'F';
- else if(i->type==GL_DOUBLE)
- type[0] = 'D';
- else if(i->type==GL_INT || i->type==GL_UNSIGNED_INT)
- type[0] = 'I';
- else if(i->type==GL_SHORT || i->type==GL_UNSIGNED_SHORT)
- type[0] = 'S';
- else if(i->type==GL_BYTE || i->type==GL_UNSIGNED_BYTE)
- type[0] = 'B';
-
- if(i->type==GL_UNSIGNED_INT || i->type==GL_UNSIGNED_SHORT || i->type==GL_UNSIGNED_BYTE)
- {
- type[1] = type[0];
- type[0] = 'U';
- }
-
- if(!result.empty())
- result += '_';
- result += format("%c%d%s", kind, i->size, type);
- }
-
- return result;
-}
-
-
-BufferContent::Array::Array(const ArrayState &a):
- kind(a.kind),
- index(a.index),
- size(a.size),
- type(a.type),
- offset(a.pointer)
-{ }
-
-
-BufferState::BufferState():
- usage(GL_STATIC_DRAW),
- size(0),
- data(0)
-{ }
-
-void BufferState::set_data(unsigned sz, const void *ptr, GLenum use)
-{
- usage = use;
- size = sz;
- delete[] data;
- data = new char[size];
- if(ptr)
- set_sub_data(0, size, ptr);
- content = BufferContent();
-}
-
-void BufferState::set_sub_data(unsigned off, unsigned sz, const void *ptr)
-{
- if(data && off+sz<=size)
- {
- const char *cptr = reinterpret_cast<const char *>(ptr);
- copy(cptr, cptr+sz, data+off);
- }
-}
-
-string BufferState::describe() const
-{
- if(content.stride)
- return format("%s, %d vertices (%d bytes), %s",
- content.describe(), size/content.stride, size, describe_enum(usage, ""));
- else
- return format("%d bytes, %s", size, describe_enum(usage, ""));
-}
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#ifndef BUFFERSTATE_H_
-#define BUFFERSTATE_H_
-
-#include <string>
-#include <vector>
-#include "opengl.h"
-
-struct ArrayState;
-
-struct BufferContent
-{
- struct Array
- {
- GLenum kind;
- unsigned index;
- unsigned size;
- GLenum type;
- int offset;
-
- Array(const ArrayState &);
- };
-
- bool consistent;
- unsigned stride;
- std::vector<Array> arrays;
-
- BufferContent();
- void update(const ArrayState &);
- std::string describe() const;
-};
-
-struct BufferState
-{
- unsigned id;
- GLenum target;
- GLenum usage;
- unsigned size;
- char *data;
- BufferContent content;
-
- BufferState();
- void set_data(unsigned, const void *, GLenum);
- void set_sub_data(unsigned, unsigned, const void *);
- std::string describe() const;
-};
-
-#endif
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/core/except.h>
-#include "glstate.h"
-
-using namespace std;
-using namespace Msp;
-
-namespace {
-
-template<typename T>
-float normalize(T v);
-
-template<>
-float normalize<GLubyte>(GLubyte v)
-{ return v/255.0f; }
-
-template<>
-float normalize<GLbyte>(GLbyte v)
-{ return (2*v+1)/255.0f; }
-
-template<typename T, void (*func)(void *, float)>
-void wrap_normalized(void *user_data, T v0)
-{ func(user_data, v0); }
-
-template<typename T, void (*func)(void *, float, float)>
-void wrap_normalized(void *user_data, T v0, T v1)
-{ func(user_data, v0, v1); }
-
-template<typename T, void (*func)(void *, float, float, float)>
-void wrap_normalized(void *user_data, T v0, T v1, T v2)
-{ func(user_data, v0, v1, v2); }
-
-template<typename T, void (*func)(void *, float, float, float, float)>
-void wrap_normalized(void *user_data, T v0, T v1, T v2, T v3)
-{ func(user_data, v0, v1, v2, v3); }
-
-template<typename T, void (*func)(void *, T)>
-void wrap_array(void *user_data, const T *v)
-{ func(user_data, v[0]); }
-
-template<typename T, void (*func)(void *, T, T)>
-void wrap_array(void *user_data, const T *v)
-{ func(user_data, v[0], v[1]); }
-
-template<typename T, void (*func)(void *, T, T, T)>
-void wrap_array(void *user_data, const T *v)
-{ func(user_data, v[0], v[1], v[2]); }
-
-template<typename T, void (*func)(void *, T, T, T, T)>
-void wrap_array(void *user_data, const T *v)
-{ func(user_data, v[0], v[1], v[2], v[3]); }
-
-}
-
-
-Vector3::Vector3():
- x(0), y(0), z(0)
-{ }
-
-
-Vector4::Vector4():
- x(0), y(0), z(0), w(1)
-{ }
-
-
-GlState::GlState():
- decoder(gldecoder_new(this, NULL)),
- active_tex(0),
- client_active_tex(0),
- array_buffer(0),
- element_buffer(0)
-{
- vertex_array.kind = GL_VERTEX_ARRAY;
- normal_array.kind = GL_NORMAL_ARRAY;
- color_array.kind = GL_COLOR_ARRAY;
- for(unsigned i=0; i<8; ++i)
- {
- texcoord_arrays[i].kind = GL_TEXTURE_COORD_ARRAY;
- texcoord_arrays[i].index = i;
- }
-
- decoder->glColor3ub = wrap_normalized<GLubyte, glColor3f>;
- decoder->glColor3ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor3f> >;
- decoder->glColor4ub = wrap_normalized<GLubyte, glColor4f>;
- decoder->glColor4ubv = wrap_array<GLubyte, wrap_normalized<GLubyte, glColor4f> >;
- decoder->glColor3f = glColor3f;
- decoder->glColor3fv = wrap_array<float, glColor3f>;
- decoder->glColor4f = glColor4f;
- decoder->glColor4fv = wrap_array<float, glColor4f>;
- decoder->glTexCoord1f = glTexCoord1f;
- decoder->glTexCoord1fv = wrap_array<float, glTexCoord1f>;
- decoder->glTexCoord2f = glTexCoord2f;
- decoder->glTexCoord2fv = wrap_array<float, glTexCoord2f>;
- decoder->glTexCoord3f = glTexCoord3f;
- decoder->glTexCoord3fv = wrap_array<float, glTexCoord3f>;
- decoder->glTexCoord4f = glTexCoord4f;
- decoder->glTexCoord4fv = wrap_array<float, glTexCoord4f>;
- decoder->glNormal3f = glNormal3f;
- decoder->glNormal3fv = wrap_array<float, glNormal3f>;
-
- decoder->glActiveTexture = glActiveTexture;
- decoder->glActiveTextureARB = glActiveTexture;
- decoder->glBindTexture = glBindTexture;
- decoder->glTexImage2D = glTexImage2D;
- decoder->glTexParameteri = glTexParameteri;
- decoder->glTexParameteriv = glTexParameteriv;
- decoder->glDeleteTextures = glDeleteTextures;
-
- decoder->glVertexPointer = glVertexPointer;
- decoder->glNormalPointer = glNormalPointer;
- decoder->glColorPointer = glColorPointer;
- decoder->glClientActiveTexture = glClientActiveTexture;
- decoder->glClientActiveTextureARB = glClientActiveTexture;
- decoder->glTexCoordPointer = glTexCoordPointer;
-
- decoder->glBindBuffer = glBindBuffer;
- decoder->glBindBufferARB = glBindBuffer;
- decoder->glBufferData = glBufferData;
- decoder->glBufferDataARB = glBufferData;
- decoder->glBufferSubData = glBufferSubData;
- decoder->glBufferSubDataARB = glBufferSubData;
- decoder->glDeleteBuffers = glDeleteBuffers;
- decoder->glDeleteBuffersARB = glDeleteBuffers;
-}
-
-GlState::~GlState()
-{
- gldecoder_delete(decoder);
-}
-
-int GlState::decode(const char *data, unsigned len)
-{
- return gldecoder_decode(decoder, data, len);
-}
-
-const TextureState &GlState::get_texture(unsigned id) const
-{
- TextureMap::const_iterator i = textures.find(id);
- if(i==textures.end())
- throw KeyError("Unknown texture");
- return i->second;
-}
-
-const BufferState &GlState::get_buffer(unsigned id) const
-{
- BufferMap::const_iterator i = buffers.find(id);
- if(i==buffers.end())
- throw KeyError("Unknown buffer");
- return i->second;
-}
-
-const BufferState *GlState::get_current_buffer(GLenum target) const
-{
- return const_cast<GlState *>(this)->get_current_buffer(target);
-}
-
-bool &GlState::get_boolean_state(GLenum state)
-{
- if(state==GL_VERTEX_ARRAY)
- return vertex_array.enabled;
- else if(state==GL_NORMAL_ARRAY)
- return normal_array.enabled;
- else if(state==GL_COLOR_ARRAY)
- return color_array.enabled;
- else if(state==GL_TEXTURE_COORD_ARRAY)
- return texcoord_arrays[client_active_tex].enabled;
-
- static bool dummy;
- return dummy;
-}
-
-TextureState *GlState::get_current_texture(GLenum target)
-{
- return texunits[active_tex].get_current_texture(target);
-}
-
-BufferState *GlState::get_current_buffer(GLenum target)
-{
- if(target==GL_ARRAY_BUFFER)
- return array_buffer;
- else if(target==GL_ELEMENT_ARRAY_BUFFER)
- return element_buffer;
- return 0;
-}
-
-const ArrayState &GlState::get_array(GLenum array) const
-{
- if(array==GL_VERTEX_ARRAY)
- return vertex_array;
- else if(array==GL_NORMAL_ARRAY)
- return normal_array;
- else if(array==GL_COLOR_ARRAY)
- return color_array;
- else if(array==GL_TEXTURE_COORD_ARRAY)
- return texcoord_arrays[client_active_tex];
- else
- throw InvalidParameterValue("Invalid array");
-}
-
-const ArrayState &GlState::get_texture_coord_array(unsigned index) const
-{
- return texcoord_arrays[index];
-}
-
-const ArrayState &GlState::get_attrib_array(unsigned index) const
-{
- map<unsigned, ArrayState>::const_iterator i = attrib_arrays.find(index);
- if(i!=attrib_arrays.end())
- return i->second;
-
- // XXX Return a dummy?
- throw KeyError("Unknown attribute array");
-}
-
-void GlState::set_current_texture(GLenum target, unsigned id)
-{
- TexUnitState &unit = texunits[active_tex];
- TextureState *tex = 0;
- if(id)
- {
- tex = &textures[id];
- if(!tex->id)
- {
- tex->id = id;
- tex->target = target;
- }
- else if(tex->target!=target)
- return;
- }
- unit.set_current_texture(target, tex);
-}
-
-void GlState::set_current_buffer(GLenum target, unsigned id)
-{
- BufferState *buf = 0;
- if(id)
- {
- buf = &buffers[id];
- if(!buf->id)
- buf->id = id;
- }
-
- if(target==GL_ARRAY_BUFFER)
- array_buffer = buf;
- else if(target==GL_ELEMENT_ARRAY_BUFFER)
- element_buffer = buf;
-}
-
-// Boolean state
-
-void GlState::glEnableClientState(void *user_data, GLenum state)
-{
- reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = true;
-}
-
-void GlState::glDisableClientState(void *user_data, GLenum state)
-{
- reinterpret_cast<GlState *>(user_data)->get_boolean_state(state) = false;
-}
-
-void GlState::glEnableVertexAttribArray(void *user_data, unsigned index)
-{
- reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = true;
-}
-
-void GlState::glDisableVertexAttribArray(void *user_data, unsigned index)
-{
- reinterpret_cast<GlState *>(user_data)->attrib_arrays[index].enabled = false;
-}
-
-// Vertex attributes
-
-void GlState::glColor3f(void *user_data, float r, float g, float b)
-{ glColor4f(user_data, r, g, b, 1.0f); }
-
-void GlState::glColor4f(void *user_data, float r, float g, float b, float a)
-{
- Vector4 &color = reinterpret_cast<GlState *>(user_data)->color;
- color.r = r;
- color.g = g;
- color.b = b;
- color.a = a;
-}
-
-void GlState::glNormal3f(void *user_data, float x, float y, float z)
-{
- Vector3 &normal = reinterpret_cast<GlState *>(user_data)->normal;
- normal.x = x;
- normal.y = y;
- normal.z = z;
-}
-
-void GlState::glTexCoord1f(void *user_data, float s)
-{ glTexCoord4f(user_data, s, 0.0f, 0.0f, 1.0f); }
-
-void GlState::glTexCoord2f(void *user_data, float s, float t)
-{ glTexCoord4f(user_data, s, t, 0.0f, 1.0f); }
-
-void GlState::glTexCoord3f(void *user_data, float s, float t, float p)
-{ glTexCoord4f(user_data, s, t, p, 1.0f); }
-
-void GlState::glTexCoord4f(void *user_data, float s, float t, float p, float q)
-{
- unsigned index = reinterpret_cast<GlState *>(user_data)->active_tex;
- glMultiTexCoord4f(user_data, index, s, t, p, q);
-}
-
-void GlState::glMultiTexCoord4f(void *user_data, unsigned index, float s, float t, float p, float q)
-{
- Vector4 &texcoord = reinterpret_cast<GlState *>(user_data)->texcoord[index];
- texcoord.s = s;
- texcoord.t = t;
- texcoord.p = p;
- texcoord.q = q;
-}
-
-// Textures
-
-void GlState::glActiveTexture(void *user_data, unsigned index)
-{
- reinterpret_cast<GlState *>(user_data)->active_tex = index-GL_TEXTURE0;
-}
-
-void GlState::glBindTexture(void *user_data, GLenum target, unsigned id)
-{
- reinterpret_cast<GlState *>(user_data)->set_current_texture(target, id);
-}
-
-void GlState::glTexImage2D(void *user_data, GLenum target, int level, int ifmt, int width, int height, int, GLenum, GLenum, const void *)
-{
- if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
- tex->set_image_2d(level, ifmt, width, height);
-}
-
-void GlState::glTexParameteri(void *user_data, GLenum target, GLenum param, int value)
-{ glTexParameteriv(user_data, target, param, &value); }
-
-void GlState::glTexParameteriv(void *user_data, GLenum target, GLenum param, const int *values)
-{
- if(TextureState *tex = reinterpret_cast<GlState *>(user_data)->get_current_texture(target))
- tex->set_parameter(param, values);
-}
-
-void GlState::glDeleteTextures(void *user_data, int count, const unsigned *ids)
-{
- for(int i=0; i<count; ++i)
- reinterpret_cast<GlState *>(user_data)->textures.erase(ids[i]);
-}
-
-// Vertex arrays
-
-void GlState::glVertexPointer(void *user_data, int size, GLenum type, int stride, const void *data)
-{
- GlState *self = reinterpret_cast<GlState *>(user_data);
- self->vertex_array.set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
-}
-
-void GlState::glNormalPointer(void *user_data, GLenum type, int stride, const void *data)
-{
- GlState *self = reinterpret_cast<GlState *>(user_data);
- self->normal_array.set(3, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
-}
-
-void GlState::glColorPointer(void *user_data, int size, GLenum type, int stride, const void *data)
-{
- GlState *self = reinterpret_cast<GlState *>(user_data);
- self->color_array.set(size, type, true, stride, self->array_buffer, reinterpret_cast<long>(data));
-}
-
-void GlState::glClientActiveTexture(void *user_data, unsigned index)
-{
- reinterpret_cast<GlState *>(user_data)->client_active_tex = index-GL_TEXTURE0;
-}
-
-void GlState::glTexCoordPointer(void *user_data, int size, GLenum type, int stride, const void *data)
-{
- GlState *self = reinterpret_cast<GlState *>(user_data);
- self->texcoord_arrays[self->client_active_tex].set(size, type, false, stride, self->array_buffer, reinterpret_cast<long>(data));
-}
-
-void GlState::glVertexAttribPointer(void *user_data, unsigned index, int size, GLenum type, int norm, int stride, const void *data)
-{
- GlState *self = reinterpret_cast<GlState *>(user_data);
- self->attrib_arrays[index].set(size, type, norm, stride, self->array_buffer, reinterpret_cast<long>(data));
-}
-
-// Buffer objects
-
-void GlState::glBindBuffer(void *user_data, GLenum target, unsigned id)
-{ reinterpret_cast<GlState *>(user_data)->set_current_buffer(target, id); }
-
-void GlState::glBufferData(void *user_data, GLenum target, int size, const void *data, GLenum usage)
-{
- if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
- buf->set_data(size, data, usage);
-}
-
-void GlState::glBufferSubData(void *user_data, GLenum target, int offset, int size, const void *data)
-{
- if(BufferState *buf = reinterpret_cast<GlState *>(user_data)->get_current_buffer(target))
- buf->set_sub_data(offset, size, data);
-}
-
-void GlState::glDeleteBuffers(void *user_data, int count, const unsigned *ids)
-{
- for(int i=0; i<count; ++i)
- reinterpret_cast<GlState *>(user_data)->buffers.erase(ids[i]);
-}
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#ifndef GLSTATE_H_
-#define GLSTATE_H_
-
-#include <map>
-#include <vector>
-#include "arraystate.h"
-#include "bufferstate.h"
-#include "gldecoder.h"
-#include "texturestate.h"
-
-struct Vector3
-{
- float x, y, z;
-
- Vector3();
-};
-
-struct Vector4
-{
- union { float x, r, s; };
- union { float y, g, t; };
- union { float z, b, p; };
- union { float w, a, q; };
-
- Vector4();
-};
-
-/**
-Tracks OpenGL state based on the command stream. This class is essentially a
-(partial) OpenGL implementation in itself.
-
-XXX Should determine the number of texture units the target implementation
-actually supports.
-*/
-class GlState
-{
-public:
- typedef std::map<unsigned, TextureState> TextureMap;
- typedef std::map<unsigned, BufferState> BufferMap;
-
-private:
- GlDecoder *decoder;
- Vector4 color;
- Vector4 texcoord[8];
- Vector3 normal;
- unsigned active_tex;
- unsigned client_active_tex;
- TextureMap textures;
- TexUnitState texunits[8];
- BufferMap buffers;
- BufferState *array_buffer;
- BufferState *element_buffer;
- ArrayState vertex_array;
- ArrayState normal_array;
- ArrayState color_array;
- ArrayState texcoord_arrays[8];
- std::map<unsigned, ArrayState> attrib_arrays;
-
-public:
- GlState();
- ~GlState();
-
- int decode(const char *, unsigned);
- const Vector4 &get_color() const { return color; }
- const Vector4 &get_texcoord(unsigned i) const { return texcoord[i]; }
- const Vector3 &get_normal() const { return normal; }
- const TextureMap &get_textures() const { return textures; }
- const TextureState &get_texture(unsigned) const;
- const TexUnitState &get_texture_unit(unsigned i) const { return texunits[i]; }
- const BufferMap &get_buffers() const { return buffers; }
- const BufferState &get_buffer(unsigned) const;
- const BufferState *get_current_buffer(GLenum) const;
- const ArrayState &get_array(GLenum) const;
- const ArrayState &get_texture_coord_array(unsigned) const;
- const ArrayState &get_attrib_array(unsigned) const;
-private:
- bool &get_boolean_state(GLenum);
- TextureState *get_current_texture(GLenum);
- BufferState *get_current_buffer(GLenum);
- void set_current_texture(GLenum, unsigned);
- void set_current_buffer(GLenum, unsigned);
-
- static void glEnableClientState(void *, GLenum);
- static void glDisableClientState(void *, GLenum);
- static void glEnableVertexAttribArray(void *, unsigned);
- static void glDisableVertexAttribArray(void *, unsigned);
-
- static void glColor3f(void *, float, float, float);
- static void glColor4f(void *, float, float, float, float);
- static void glNormal3f(void *, float, float, float);
- static void glTexCoord1f(void *, float);
- static void glTexCoord2f(void *, float, float);
- static void glTexCoord3f(void *, float, float, float);
- static void glTexCoord4f(void *, float, float, float, float);
- static void glMultiTexCoord4f(void *, unsigned, float, float, float, float);
-
- static void glActiveTexture(void *, unsigned);
- static void glBindTexture(void *, GLenum, unsigned);
- static void glTexImage2D(void *, GLenum, int, int, int, int, int, GLenum, GLenum, const void *);
- static void glTexParameteri(void *, GLenum, GLenum, int);
- static void glTexParameteriv(void *, GLenum, GLenum, const int *);
- static void glDeleteTextures(void *, int, const unsigned *);
-
- static void glVertexPointer(void *, int, GLenum, int, const void *);
- static void glNormalPointer(void *, GLenum, int, const void *);
- static void glColorPointer(void *, int, GLenum, int, const void *);
- static void glClientActiveTexture(void *, unsigned);
- static void glTexCoordPointer(void *, int, GLenum, int, const void *);
- static void glVertexAttribPointer(void *, unsigned, int, GLenum, int, int, const void *);
-
- static void glBindBuffer(void *, GLenum, unsigned);
- static void glBufferData(void *, GLenum, int, const void *, GLenum);
- static void glBufferSubData(void *, GLenum, int, int, const void *);
- static void glDeleteBuffers(void *, int, const unsigned *);
-};
-
-#endif
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/io/print.h>
-#include "enums.h"
-#include "gldbg.h"
-#include "inspector.h"
-
-using namespace std;
-using namespace Msp;
-
-Inspector::Inspector(GlDbg &dbg)
-{
- CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
-
- cmd_interp.register_command("state", this, &Inspector::cmd_state)
- .set_help("Inspects general GL state",
- "state vertex\n"
- " Print current vertex attributes\n\n"
- "state bind\n"
- " Show current bindings\n");
-
- cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
- .set_help("Inspect texture state",
- "texture\n"
- " Lists texture objects\n\n"
- "texture ID\n"
- " Print information about a texture object\n");
-
- cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
- .set_help("Inspect buffer object state",
- "buffer\n"
- " Lists buffer objects\n\n"
- "buffer ID\n"
- " Print information about a buffer object\n");
-}
-
-void Inspector::decode(const char *data, unsigned len)
-{
- state.decode(data, len);
-}
-
-void Inspector::cmd_state(const string &args)
-{
- const GlState &glstate = state;
- if(args=="vertex")
- {
- IO::print("Current vertex attributes:\n");
- const Vector4 &color = glstate.get_color();
- IO::print(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
- for(unsigned i=0; i<8; ++i)
- {
- const Vector4 &texcoord = glstate.get_texcoord(i);
- IO::print(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
- }
- const Vector3 &normal = glstate.get_normal();
- IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
- }
- else if(args=="bind")
- {
- IO::print("Current bindings:\n");
- for(unsigned i=0; i<8; ++i)
- {
- IO::print(" Texture unit %d:\n", i);
- const TexUnitState &unit = glstate.get_texture_unit(i);
- IO::print(" GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
- IO::print(" GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
- }
- IO::print(" Buffers:\n");
- const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
- IO::print(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
- buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
- IO::print(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
- }
- else
- throw InvalidParameterValue("Invalid or missing argument");
-}
-
-void Inspector::cmd_texture(const string &args)
-{
- if(args.empty())
- {
- const map<unsigned, TextureState> &textures = state.get_textures();
- IO::print("%d texture objects:\n", textures.size());
- for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
- {
- const TextureState &tex = i->second;
- IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
- }
- }
- else
- {
- unsigned id = lexical_cast<unsigned>(args);
- const TextureState &tex = state.get_texture(id);
- IO::print("Texture object %d\n", id);
- IO::print(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
- IO::print(" Images:\n");
- for(unsigned i=0; i<tex.images.size(); ++i)
- {
- const TexImageState &img = tex.images[i];
- IO::print(" Level %2d: %s\n", i, img.describe());
- }
- IO::print(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
- IO::print(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
- IO::print(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
- describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
- IO::print(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
- IO::print(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
- IO::print(" Generate mipmap: %s\n", tex.generate_mipmap);
- }
-}
-
-void Inspector::cmd_buffer(const string &args)
-{
- if(args.empty())
- {
- const GlState::BufferMap &buffers = state.get_buffers();
- IO::print("%d buffers:\n", buffers.size());
- for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
- IO::print(" %d: %s\n", i->first, i->second.describe());
- }
- else
- {
- unsigned id = lexical_cast<unsigned>(args);
- const BufferState &buf = state.get_buffer(id);
- IO::print("Buffer %d:\n", id);
- IO::print(" Size: %d bytes\n", buf.size);
- IO::print(" Usage: %s\n", describe_enum(buf.usage, ""));
- if(buf.content.stride)
- {
- IO::print(" Stride: %d bytes\n", buf.content.stride);
-
- IO::print(" Arrays:\n");
- const vector<BufferContent::Array> &arrays = buf.content.arrays;
- for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
- {
- if(i->kind)
- IO::print(" %2d: %s, %d %s\n", i->offset,
- describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
- else
- IO::print(" %2d: Attrib %d, %d %s\n", i->offset,
- i->index, i->size, describe_enum(i->type, "DataType"));
- }
-
- IO::print(" Data:\n");
- string header;
- for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
- {
- if(!header.empty())
- header += " | ";
-
- string label;
- if(i->kind==GL_VERTEX_ARRAY)
- label = "Vertex";
- else if(i->kind==GL_NORMAL_ARRAY)
- label = "Normal";
- else if(i->kind==GL_COLOR_ARRAY)
- label = "Color";
- else if(i->kind==GL_TEXTURE_COORD_ARRAY)
- {
- if(i->size==1)
- label = "TexC";
- else
- label = "TexCoord";
- }
- else if(!i->kind)
- {
- if(i->size==1)
- label = format("A %d", i->index);
- else
- label = format("Attrib %d", i->index);
- }
-
- unsigned width = i->size;
- if(i->type==GL_FLOAT)
- width *= 5;
- else if(i->type==GL_UNSIGNED_BYTE)
- width *= 3;
- width += i->size-1;
-
- header.append((width-label.size())/2, ' ');
- header += label;
- header.append((width-label.size()+1)/2, ' ');
- }
- IO::print(" %s\n", header);
-
- unsigned n_verts = buf.size/buf.content.stride;
- for(unsigned i=0; i<n_verts; ++i)
- {
- const char *vertex = buf.data+i*buf.content.stride;
-
- string line;
- for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
- {
- if(!line.empty())
- line += " |";
-
- const char *base = vertex+j->offset;
- for(unsigned k=0; k<j->size; ++k)
- {
- if(j->type==GL_FLOAT)
- line += format(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
- else if(j->type==GL_UNSIGNED_BYTE)
- line += format(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
- }
- }
-
- IO::print("%3d:%s\n", i, line);
- }
- }
- }
-}
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#ifndef INSPECTOR_H_
-#define INSPECTOR_H_
-
-#include "glstate.h"
-#include "tool.h"
-
-class Inspector: public RegisteredTool<Inspector>
-{
-private:
- GlState state;
-
-public:
- Inspector(GlDbg &);
-
- virtual void decode(const char *, unsigned);
-private:
- void cmd_state(const std::string &);
- void cmd_texture(const std::string &);
- void cmd_buffer(const std::string &);
-};
-
-#endif
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/io/print.h>
-#include <msp/time/units.h>
-#include <msp/time/utils.h>
-#include "gldbg.h"
-#include "profiler.h"
-
-using namespace std;
-using namespace Msp;
-
-Profiler::Profiler(GlDbg &dbg):
- decoder(gldecoder_new(this, 0)),
- enabled(false)
-{
- dbg.get_command_interpreter().register_command("profile", this, &Profiler::cmd_profile)
- .set_help("Profiles GL usage and performance",
- "profile {on|off}\n"
- " Enables or disables profiling\n");
-
- decoder->glDrawArrays = glDrawArrays;
- decoder->glDrawElements = glDrawElements;
- decoder->glDrawRangeElements = glDrawRangeElements;
- decoder->glXSwapBuffers = glXSwapBuffers;
-}
-
-void Profiler::decode(const char *data, unsigned len)
-{
- if(enabled)
- gldecoder_decode(decoder, data, len);
-}
-
-void Profiler::cmd_profile(const string &args)
-{
- if(args.empty() || args=="on")
- {
- enabled = true;
- frames = 0;
- draw_calls = 0;
- vertices = 0;
- triangles = 0;
- start = Msp::Time::now();
- }
- else if(args=="off")
- enabled = false;
- else
- throw InvalidParameterValue("Invalid argument");
-}
-
-void Profiler::glDrawArrays(void *user_data, GLenum mode, int, int count)
-{
- glDrawElements(user_data, mode, count, 0, 0);
-}
-
-void Profiler::glDrawElements(void *user_data, GLenum mode, int count, GLenum, const void *)
-{
- Profiler *self = reinterpret_cast<Profiler *>(user_data);
-
- ++self->draw_calls;
- self->vertices += count;
-
- switch(mode)
- {
- case GL_TRIANGLES:
- self->triangles += count/3;
- break;
- case GL_TRIANGLE_STRIP:
- case GL_TRIANGLE_FAN:
- if(count>2)
- self->triangles += count-2;
- break;
- case GL_QUADS:
- self->triangles += count/4;
- break;
- case GL_QUAD_STRIP:
- if(count>3)
- self->triangles += count/2-1;
- break;
- }
-}
-
-void Profiler::glDrawRangeElements(void *user_data, GLenum mode, unsigned, unsigned, int count, GLenum type, const void *data)
-{
- glDrawElements(user_data, mode, count, type, data);
-}
-
-void Profiler::glXSwapBuffers(void *user_data, Display *, GLXDrawable)
-{
- Profiler *self = reinterpret_cast<Profiler *>(user_data);
-
- IO::print("%d draw calls, %d vertices, %d triangles\n", self->draw_calls, self->vertices, self->triangles);
- self->draw_calls = 0;
- self->vertices = 0;
- self->triangles = 0;
-
- ++self->frames;
- Msp::Time::TimeStamp t = Msp::Time::now();
- Msp::Time::TimeDelta dt = t-self->start;
- if(dt>Msp::Time::sec)
- {
- Msp::IO::print("%d frames in %s seconds\n", self->frames, dt);
- self->start = t;
- self->frames = 0;
- }
-}
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2010 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#ifndef PROFILER_H_
-#define PROFILER_H_
-
-#include <msp/time/timestamp.h>
-#include "gldecoder.h"
-#include "tool.h"
-
-class Profiler: public RegisteredTool<Profiler>
-{
-private:
- GlDecoder *decoder;
- bool enabled;
- Msp::Time::TimeStamp start;
- unsigned frames;
- unsigned draw_calls;
- unsigned vertices;
- unsigned triangles;
-
-public:
- Profiler(GlDbg &);
-
- virtual void decode(const char *, unsigned);
-private:
- void cmd_profile(const std::string &);
-
- static void glDrawArrays(void *, GLenum, int, int);
- static void glDrawElements(void *, GLenum, int, GLenum, const void *);
- static void glDrawRangeElements(void *, GLenum, unsigned, unsigned, int, GLenum, const void *);
- static void glXSwapBuffers(void *, Display *, GLXDrawable);
-};
-
-#endif
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#include <msp/strings/formatter.h>
-#include "enums.h"
-#include "texturestate.h"
-
-using namespace std;
-using namespace Msp;
-
-TexImageState::TexImageState():
- width(0),
- height(0),
- depth(0),
- internal_format(0)
-{ }
-
-void TexImageState::set_2d(GLenum ifmt, unsigned wd, unsigned ht)
-{
- width = wd;
- height = ht;
- depth = 0;
- internal_format = ifmt;
-}
-
-string TexImageState::describe() const
-{
- string descr = format("%d", width);
- if(height)
- {
- descr += format("x%d", height);
- if(depth)
- descr += format("x%d", height);
- }
- descr += format(", %s", describe_enum(internal_format, "PixelFormat"));
- return descr;
-}
-
-
-TextureState::TextureState():
- id(0),
- target(0),
- min_filter(GL_LINEAR_MIPMAP_LINEAR),
- mag_filter(GL_LINEAR),
- wrap_s(GL_REPEAT),
- wrap_t(GL_REPEAT),
- wrap_r(GL_REPEAT),
- compare_mode(GL_NONE),
- compare_func(GL_LEQUAL),
- generate_mipmap(false)
-{ }
-
-void TextureState::set_image_2d(unsigned level, GLenum ifmt, unsigned wd, unsigned ht)
-{
- while(1)
- {
- if(images.size()<=level)
- images.resize(level+1);
- images[level].set_2d(ifmt, wd, ht);
-
- if(generate_mipmap)
- {
- if(wd==1 && ht==1)
- break;
- ++level;
- if(wd>1)
- wd /= 2;
- if(ht>1)
- ht /= 2;
- }
- else
- break;
- }
-}
-
-void TextureState::set_parameter(GLenum pname, const int *values)
-{
- if(pname==GL_TEXTURE_MIN_FILTER)
- min_filter = values[0];
- else if(pname==GL_TEXTURE_MAG_FILTER)
- mag_filter = values[0];
- else if(pname==GL_TEXTURE_WRAP_S)
- wrap_s = values[0];
- else if(pname==GL_TEXTURE_WRAP_T)
- wrap_t = values[0];
- else if(pname==GL_TEXTURE_WRAP_R)
- wrap_r = values[0];
- else if(pname==GL_TEXTURE_COMPARE_MODE)
- compare_mode = values[0];
- else if(pname==GL_TEXTURE_COMPARE_FUNC)
- compare_func = values[0];
- else if(pname==GL_GENERATE_MIPMAP)
- generate_mipmap = values[0];
-}
-
-string TextureState::describe() const
-{
- string descr = describe_enum(target, "TextureTarget");
- if(images.empty())
- descr += ", undefined";
- else
- descr += format(", %s", images.front().describe());
- return descr;
-}
-
-
-TexUnitState::TexUnitState():
- current_2d(0),
- current_3d(0)
-{ }
-
-void TexUnitState::set_current_texture(GLenum target, TextureState *tex)
-{
- if(target==GL_TEXTURE_2D)
- current_2d = tex;
- else if(target==GL_TEXTURE_3D)
- current_3d = tex;
-}
-
-TextureState *TexUnitState::get_current_texture(GLenum target)
-{
- if(target==GL_TEXTURE_2D)
- return current_2d;
- else if(target==GL_TEXTURE_3D)
- return current_3d;
- else
- return 0;
-}
-
-const TextureState *TexUnitState::get_current_texture(GLenum target) const
-{
- return const_cast<TexUnitState *>(this)->get_current_texture(target);
-}
-
-string TexUnitState::describe_binding(GLenum target) const
-{
- if(const TextureState *tex = get_current_texture(target))
- {
- string descr = format("%d ", tex->id);
- if(tex->images.empty())
- descr += "(undefined)";
- else
- descr += format("(%s)", tex->images.front().describe());
- return descr;
- }
- else
- return "None";
-}
+++ /dev/null
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009 Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
-#ifndef TEXTURESTATE_H_
-#define TEXTURESTATE_H_
-
-#include <string>
-#include <vector>
-#include "opengl.h"
-#include "autoconstptr.h"
-
-struct TexImageState
-{
- unsigned width;
- unsigned height;
- unsigned depth;
- GLenum internal_format;
-
- TexImageState();
- void set_2d(GLenum, unsigned, unsigned);
- std::string describe() const;
-};
-
-struct TextureState
-{
- unsigned id;
- GLenum target;
- std::vector<TexImageState> images;
- GLenum min_filter;
- GLenum mag_filter;
- GLenum wrap_s;
- GLenum wrap_t;
- GLenum wrap_r;
- GLenum compare_mode;
- GLenum compare_func;
- bool generate_mipmap;
-
- TextureState();
- void set_image_2d(unsigned, GLenum, unsigned, unsigned);
- void set_parameter(GLenum, const int *);
- std::string describe() const;
-};
-
-struct TexUnitState
-{
- AutoConstPtr<TextureState> current_2d;
- AutoConstPtr<TextureState> current_3d;
-
- TexUnitState();
-
- void set_current_texture(GLenum, TextureState *);
- TextureState *get_current_texture(GLenum);
- const TextureState *get_current_texture(GLenum) const;
- std::string describe_binding(GLenum) const;
-};
-
-#endif