3 This file is part of gldbg
4 Copyright © 2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
13 #include "inspector.h"
14 #include "strformat.h"
18 Inspector::Inspector(GlDbg &dbg)
20 CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
22 cmd_interp.register_command("state", this, &Inspector::cmd_state)
23 .set_help("Inspects general GL state",
25 " Print current vertex attributes\n\n"
27 " Show current bindings\n");
29 cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
30 .set_help("Inspect texture state",
32 " Lists texture objects\n\n"
34 " Print information about a texture object\n");
36 cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
37 .set_help("Inspect buffer object state",
39 " Lists buffer objects\n\n"
41 " Print information about a buffer object\n");
44 void Inspector::decode(const char *data, unsigned len)
46 state.decode(data, len);
49 void Inspector::cmd_state(const string &args)
51 const GlState &glstate = state;
54 printf("Current vertex attributes:\n");
55 const Vector4 &color = glstate.get_color();
56 printf(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
57 for(unsigned i=0; i<8; ++i)
59 const Vector4 &texcoord = glstate.get_texcoord(i);
60 printf(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
62 const Vector3 &normal = glstate.get_normal();
63 printf(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
67 printf("Current bindings:\n");
68 for(unsigned i=0; i<8; ++i)
70 printf(" Texture unit %d:\n", i);
71 const TexUnitState &unit = glstate.get_texture_unit(i);
72 string descr = unit.describe_binding(GL_TEXTURE_2D);
73 printf(" GL_TEXTURE_2D: %s\n", descr.c_str());
74 descr = unit.describe_binding(GL_TEXTURE_3D);
75 printf(" GL_TEXTURE_3D: %s\n", descr.c_str());
77 printf(" Buffers:\n");
78 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
79 printf(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
80 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
81 printf(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
84 throw runtime_error("Invalid or missing argument");
87 void Inspector::cmd_texture(const string &args)
91 const map<unsigned, TextureState> &textures = state.get_textures();
92 printf("%d texture objects:\n", textures.size());
93 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
95 const TextureState &tex = i->second;
96 string descr = tex.describe();
97 printf(" %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
103 unsigned id = strtoul(args.c_str(), &end, 0);
105 throw runtime_error("Invalid texture id");
107 const TextureState &tex = state.get_texture(id);
108 printf("Texture object %d\n", id);
109 printf(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
110 printf(" Images:\n");
111 for(unsigned i=0; i<tex.images.size(); ++i)
113 string descr = tex.images[i].describe();
114 printf(" Level %2d: %s\n", i, descr.c_str());
116 printf(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
117 printf(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
118 printf(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
119 describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
120 printf(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
121 printf(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
122 printf(" Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
126 void Inspector::cmd_buffer(const string &args)
130 const GlState::BufferMap &buffers = state.get_buffers();
131 printf("%d buffers:\n", buffers.size());
132 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
134 string descr = i->second.describe();
135 printf(" %d: %s\n", i->first, descr.c_str());
141 unsigned id = strtoul(args.c_str(), &end, 0);
143 throw runtime_error("Invalid buffer id");
145 const BufferState &buf = state.get_buffer(id);
146 printf("Buffer %d:\n", id);
147 printf(" Size: %d bytes\n", buf.size);
148 printf(" Usage: %s\n", describe_enum(buf.usage, ""));
149 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
151 const BufferContent::Array &array = buf.content.arrays.front();
152 printf(" Arrays:\n");
153 printf(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
156 unsigned width = 1+buf.content.stride*2;
158 snprintf(fmt, sizeof(fmt), " %%%du", width);
159 unsigned n_elems = buf.size/buf.content.stride;
161 const char *ptr = buf.data;
162 for(unsigned i=0; i<n_elems; ++i)
167 if(array.type==GL_UNSIGNED_BYTE)
168 line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
169 else if(array.type==GL_UNSIGNED_SHORT)
170 line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
171 else if(array.type==GL_UNSIGNED_INT)
172 line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
174 if(line.size()+1+width>79)
176 printf("%s\n", line.c_str());
182 printf("%s\n", line.c_str());
184 else if(buf.content.stride)
186 printf(" Stride: %d bytes\n", buf.content.stride);
188 printf(" Arrays:\n");
189 const vector<BufferContent::Array> &arrays = buf.content.arrays;
190 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
193 printf(" %2d: %s, %d %s\n", i->offset,
194 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
196 printf(" %2d: Attrib %d, %d %s\n", i->offset,
197 i->index, i->size, describe_enum(i->type, "DataType"));
202 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
208 if(i->kind==GL_VERTEX_ARRAY)
210 else if(i->kind==GL_NORMAL_ARRAY)
212 else if(i->kind==GL_COLOR_ARRAY)
214 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
224 label = strformat("A %d", i->index);
226 label = strformat("Attrib %d", i->index);
229 unsigned width = i->size;
230 if(i->type==GL_FLOAT)
232 else if(i->type==GL_UNSIGNED_BYTE)
236 header.append((width-label.size())/2, ' ');
238 header.append((width-label.size()+1)/2, ' ');
240 printf(" %s\n", header.c_str());
242 unsigned n_verts = buf.size/buf.content.stride;
243 for(unsigned i=0; i<n_verts; ++i)
245 const char *vertex = buf.data+i*buf.content.stride;
248 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
253 const char *base = vertex+j->offset;
254 for(unsigned k=0; k<j->size; ++k)
256 if(j->type==GL_FLOAT)
257 line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
258 else if(j->type==GL_UNSIGNED_BYTE)
259 line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
263 printf("%3d:%s\n", i, line.c_str());