4 #include "breakpoint.h"
13 Inspector::Inspector(GlDbg &d):
15 decoder(gldecoder_new(this, NULL)),
18 CommandInterpreter &cmd_interp = gldbg.get_command_interpreter();
20 cmd_interp.register_command("state", this, &Inspector::cmd_state)
21 .set_help("Inspects general GL state",
23 " Print current vertex attributes\n\n"
25 " Show current vertex arrays\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");
43 cmd_interp.register_command("shader", this, &Inspector::cmd_shader)
44 .set_help("Inspect shader object state",
46 " List shader objects\n\n"
48 " Print information about a shader object\n");
50 cmd_interp.register_command("program", this, &Inspector::cmd_program)
51 .set_help("Inspect program object state",
53 " List program objects\n\n"
55 " Print information about a program object\n");
57 decoder->gldBreak = gldBreak;
60 Inspector::~Inspector()
62 gldecoder_delete(decoder);
65 void Inspector::decode(const char *data, unsigned len)
68 gldecoder_decode(decoder, data, len);
69 state.decode(data, len);
72 void Inspector::process_started()
74 gldbg.set_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
78 void Inspector::process_stopped(int reason)
80 if((reason>>8)==3 && query_state==2)
82 GlPacket *pkt = packet_begin(FUNC_GLDQUERYLIMITS);
85 gldbg.resume_from_break(this);
89 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
91 if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
92 ++reinterpret_cast<Inspector *>(user_data)->query_state;
95 void Inspector::print_indented(const string &str, unsigned indent)
97 string spaces(indent, ' ');
98 string::size_type start = 0;
101 string::size_type newline = str.find('\n', start);
102 string line = str.substr(start, newline-start);
103 if(newline!=string::npos || !line.empty())
104 printf("%s%s\n", spaces.c_str(), line.c_str());
105 if(newline==string::npos)
111 void Inspector::cmd_state(const string &args)
113 const GlState &glstate = state;
116 printf("Current vertex attributes:\n");
117 const Vector4 &color = glstate.get_color();
118 printf(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
119 unsigned count = glstate.get_max_texture_units();
120 for(unsigned i=0; i<count; ++i)
122 const Vector4 &texcoord = glstate.get_texcoord(i);
123 printf(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
125 const Vector3 &normal = glstate.get_normal();
126 printf(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
128 else if(args=="array")
130 printf("Current vertex arrays:\n");
131 string descr = glstate.get_array(GL_VERTEX_ARRAY).describe();
132 printf(" Vertex: %s\n", descr.c_str());
133 descr = glstate.get_array(GL_NORMAL_ARRAY).describe();
134 printf(" Normal: %s\n", descr.c_str());
135 descr = glstate.get_array(GL_COLOR_ARRAY).describe();
136 printf(" Color: %s\n", descr.c_str());
137 unsigned count = glstate.get_max_texture_units();
138 for(unsigned i=0; i<count; ++i)
140 descr = glstate.get_texture_coord_array(i).describe();
141 printf(" TexCoord%d: %s\n", i, descr.c_str());
143 count = glstate.get_max_vertex_attribs();
144 for(unsigned i=0; i<count; ++i)
146 descr = glstate.get_attrib_array(i).describe();
147 printf(" Attrib%d:%s %s\n", i, (i>=10 ? " " : " "), descr.c_str());
150 else if(args=="bind")
152 printf("Current bindings:\n");
153 unsigned count = glstate.get_max_texture_units();
154 for(unsigned i=0; i<count; ++i)
156 printf(" Texture unit %d:\n", i);
157 const TexUnitState &unit = glstate.get_texture_unit(i);
158 string descr = unit.describe_binding(GL_TEXTURE_1D);
159 printf(" GL_TEXTURE_1D: %s\n", descr.c_str());
160 descr = unit.describe_binding(GL_TEXTURE_2D);
161 printf(" GL_TEXTURE_2D: %s\n", descr.c_str());
162 descr = unit.describe_binding(GL_TEXTURE_3D);
163 printf(" GL_TEXTURE_3D: %s\n", descr.c_str());
164 descr = unit.describe_binding(GL_TEXTURE_CUBE_MAP);
165 printf(" GL_TEXTURE_CUBE_MAP: %s\n", descr.c_str());
167 printf(" Buffers:\n");
168 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
169 printf(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
170 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
171 printf(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
172 buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
173 printf(" GL_UNIFORM_BUFFER: %d\n", (buf ? buf->id : 0));
174 count = glstate.get_max_uniform_buffer_bindings();
175 for(unsigned i=0; i<count; ++i)
177 const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
179 printf(" %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
183 throw runtime_error("Invalid or missing argument");
186 void Inspector::cmd_texture(const string &args)
190 const map<unsigned, TextureState> &textures = state.get_textures();
191 printf("%d texture objects:\n", textures.size());
192 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
194 const TextureState &tex = i->second;
195 string descr = tex.describe();
196 printf(" %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
202 unsigned id = strtoul(args.c_str(), &end, 0);
204 throw runtime_error("Invalid texture id");
206 const TextureState &tex = state.get_texture(id);
207 printf("Texture object %d\n", id);
208 printf(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
209 printf(" Images:\n");
210 if(tex.target==GL_TEXTURE_CUBE_MAP)
212 static const char *face_labels[6] = { "+X", "-X", "+Y", "-Y", "+Z", "-Z" };
213 for(unsigned i=0; i<6; ++i)
215 printf(" Face %s:\n", face_labels[i]);
216 for(unsigned j=i; j<tex.images.size(); j+=6)
218 string descr = tex.images[j].describe();
219 printf(" Level %2d: %s\n", j/6, descr.c_str());
225 for(unsigned i=0; i<tex.images.size(); ++i)
227 string descr = tex.images[i].describe();
228 printf(" Level %2d: %s\n", i, descr.c_str());
231 printf(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
232 printf(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
233 printf(" Wrap modes:\n");
234 printf(" S: %s\n", describe_enum(tex.wrap_s, "TextureWrapMode"));
235 printf(" T: %s\n", describe_enum(tex.wrap_t, "TextureWrapMode"));
236 printf(" R: %s\n", describe_enum(tex.wrap_r, "TextureWrapMode"));
237 printf(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
238 printf(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
239 printf(" Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
243 void Inspector::cmd_buffer(const string &args)
247 const GlState::BufferMap &buffers = state.get_buffers();
248 printf("%d buffers:\n", buffers.size());
249 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
251 string descr = i->second.describe();
252 printf(" %d: %s\n", i->first, descr.c_str());
258 unsigned id = strtoul(args.c_str(), &end, 0);
260 throw runtime_error("Invalid buffer id");
262 const BufferState &buf = state.get_buffer(id);
263 printf("Buffer %d:\n", id);
264 printf(" Size: %d bytes\n", buf.size);
265 printf(" Usage: %s\n", describe_enum(buf.usage, ""));
266 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
268 const BufferContent::Array &array = buf.content.arrays.front();
269 printf(" Arrays:\n");
270 printf(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
273 unsigned width = 1+buf.content.stride*2;
275 snprintf(fmt, sizeof(fmt), " %%%du", width);
276 unsigned n_elems = buf.size/buf.content.stride;
278 const char *ptr = buf.data;
279 for(unsigned i=0; i<n_elems; ++i)
284 if(array.type==GL_UNSIGNED_BYTE)
285 line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
286 else if(array.type==GL_UNSIGNED_SHORT)
287 line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
288 else if(array.type==GL_UNSIGNED_INT)
289 line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
291 if(line.size()+1+width>79)
293 printf("%s\n", line.c_str());
299 printf("%s\n", line.c_str());
301 else if(buf.content.stride)
303 printf(" Stride: %d bytes\n", buf.content.stride);
305 printf(" Arrays:\n");
306 const vector<BufferContent::Array> &arrays = buf.content.arrays;
307 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
310 printf(" %2d: %s, %d %s\n", i->offset,
311 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
313 printf(" %2d: Attrib %d, %d %s\n", i->offset,
314 i->index, i->size, describe_enum(i->type, "DataType"));
319 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
325 if(i->kind==GL_VERTEX_ARRAY)
327 else if(i->kind==GL_NORMAL_ARRAY)
329 else if(i->kind==GL_COLOR_ARRAY)
331 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
341 label = strformat("A %d", i->index);
343 label = strformat("Attrib %d", i->index);
346 unsigned width = i->size;
347 if(i->type==GL_FLOAT)
349 else if(i->type==GL_UNSIGNED_BYTE)
353 header.append((width-label.size())/2, ' ');
355 header.append((width-label.size()+1)/2, ' ');
357 printf(" %s\n", header.c_str());
359 unsigned n_verts = buf.size/buf.content.stride;
360 for(unsigned i=0; i<n_verts; ++i)
362 const char *vertex = buf.data+i*buf.content.stride;
365 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
370 const char *base = vertex+j->offset;
371 for(unsigned k=0; k<j->size; ++k)
373 if(j->type==GL_FLOAT)
374 line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
375 else if(j->type==GL_UNSIGNED_BYTE)
376 line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
380 printf("%3d:%s\n", i, line.c_str());
386 void Inspector::cmd_shader(const string &args)
390 const GlState::ShaderMap &shaders = state.get_shaders();
391 printf("%d shader objects:\n", shaders.size());
392 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
394 string descr = i->second.describe();
395 printf(" %d: %s\n", i->first, descr.c_str());
401 unsigned id = strtoul(args.c_str(), &end, 0);
403 throw runtime_error("Invalid shader id");
405 const ShaderState &shader = state.get_shader(id);
406 printf("Shader %d:\n", shader.id);
407 printf(" Type: %s\n", describe_enum(shader.type, ""));
409 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
411 printf(" Source string %d:\n", n);
412 print_indented(*i, 4);
414 if(shader.source_changed)
415 printf(" Source changed since last compile\n");
416 printf(" Compile status: %d\n", shader.compile_status);
417 if(shader.info_log.empty())
418 printf(" Info log is empty\n");
421 printf(" Info log:\n");
422 print_indented(shader.info_log, 4);
424 if(shader.pending_delete)
425 printf(" Pending deletion\n");
429 void Inspector::cmd_program(const std::string &args)
433 const GlState::ProgramMap &programs = state.get_programs();
434 printf("%d program objects:\n", programs.size());
435 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
437 string descr = i->second.describe();
438 printf(" %d: %s\n", i->first, descr.c_str());
444 unsigned id = strtoul(args.c_str(), &end, 0);
446 throw runtime_error("Invalid program id");
448 const ProgramState &program = state.get_program(id);
449 printf("Program %d:\n", program.id);
450 printf(" Attached shaders:\n");
451 for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
453 string descr = (*i)->describe();
454 printf(" %d: %s\n", (*i)->id, descr.c_str());
456 if(program.shaders_changed)
457 printf(" Shaders changed since last compile\n");
458 printf(" Link status: %d\n", program.link_status);
459 if(program.info_log.empty())
460 printf(" Info log is empty\n");
463 printf(" Info log:\n");
464 print_indented(program.info_log, 4);