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.clear_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
86 gldbg.resume_from_break(this);
90 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
92 if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
93 ++reinterpret_cast<Inspector *>(user_data)->query_state;
96 void Inspector::print_indented(const string &str, unsigned indent)
98 string spaces(indent, ' ');
99 string::size_type start = 0;
102 string::size_type newline = str.find('\n', start);
103 string line = str.substr(start, newline-start);
104 if(newline!=string::npos || !line.empty())
105 printf("%s%s\n", spaces.c_str(), line.c_str());
106 if(newline==string::npos)
112 void Inspector::cmd_state(const string &args)
114 const GlState &glstate = state;
117 printf("Current vertex attributes:\n");
118 const Vector4 &color = glstate.get_color();
119 printf(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
120 unsigned count = glstate.get_max_texture_units();
121 for(unsigned i=0; i<count; ++i)
123 const Vector4 &texcoord = glstate.get_texcoord(i);
124 printf(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
126 const Vector3 &normal = glstate.get_normal();
127 printf(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
129 else if(args=="array")
131 printf("Current vertex arrays:\n");
132 string descr = glstate.get_array(GL_VERTEX_ARRAY).describe();
133 printf(" Vertex: %s\n", descr.c_str());
134 descr = glstate.get_array(GL_NORMAL_ARRAY).describe();
135 printf(" Normal: %s\n", descr.c_str());
136 descr = glstate.get_array(GL_COLOR_ARRAY).describe();
137 printf(" Color: %s\n", descr.c_str());
138 unsigned count = glstate.get_max_texture_units();
139 for(unsigned i=0; i<count; ++i)
141 descr = glstate.get_texture_coord_array(i).describe();
142 printf(" TexCoord%d: %s\n", i, descr.c_str());
144 count = glstate.get_max_vertex_attribs();
145 for(unsigned i=0; i<count; ++i)
147 descr = glstate.get_attrib_array(i).describe();
148 printf(" Attrib%d:%s %s\n", i, (i>=10 ? " " : " "), descr.c_str());
151 else if(args=="bind")
153 printf("Current bindings:\n");
154 unsigned count = glstate.get_max_texture_units();
155 for(unsigned i=0; i<count; ++i)
157 printf(" Texture unit %d:\n", i);
158 const TexUnitState &unit = glstate.get_texture_unit(i);
159 string descr = unit.describe_binding(GL_TEXTURE_1D);
160 printf(" GL_TEXTURE_1D: %s\n", descr.c_str());
161 descr = unit.describe_binding(GL_TEXTURE_2D);
162 printf(" GL_TEXTURE_2D: %s\n", descr.c_str());
163 descr = unit.describe_binding(GL_TEXTURE_3D);
164 printf(" GL_TEXTURE_3D: %s\n", descr.c_str());
165 descr = unit.describe_binding(GL_TEXTURE_CUBE_MAP);
166 printf(" GL_TEXTURE_CUBE_MAP: %s\n", descr.c_str());
168 printf(" Buffers:\n");
169 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
170 printf(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
171 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
172 printf(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
173 buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
174 printf(" GL_UNIFORM_BUFFER: %d\n", (buf ? buf->id : 0));
175 count = glstate.get_max_uniform_buffer_bindings();
176 for(unsigned i=0; i<count; ++i)
178 const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
180 printf(" %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
184 throw runtime_error("Invalid or missing argument");
187 void Inspector::cmd_texture(const string &args)
191 const map<unsigned, TextureState> &textures = state.get_textures();
192 printf("%lu texture objects:\n", textures.size());
193 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
195 const TextureState &tex = i->second;
196 string descr = tex.describe();
197 printf(" %d: %s, %lu images\n", i->first, descr.c_str(), tex.images.size());
203 unsigned id = strtoul(args.c_str(), &end, 0);
205 throw runtime_error("Invalid texture id");
207 const TextureState &tex = state.get_texture(id);
208 printf("Texture object %d\n", id);
209 printf(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
210 printf(" Images:\n");
211 if(tex.target==GL_TEXTURE_CUBE_MAP)
213 static const char *face_labels[6] = { "+X", "-X", "+Y", "-Y", "+Z", "-Z" };
214 for(unsigned i=0; i<6; ++i)
216 printf(" Face %s:\n", face_labels[i]);
217 for(unsigned j=i; j<tex.images.size(); j+=6)
219 string descr = tex.images[j].describe();
220 printf(" Level %2d: %s\n", j/6, descr.c_str());
226 for(unsigned i=0; i<tex.images.size(); ++i)
228 string descr = tex.images[i].describe();
229 printf(" Level %2d: %s\n", i, descr.c_str());
232 printf(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
233 printf(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
234 printf(" Wrap modes:\n");
235 printf(" S: %s\n", describe_enum(tex.wrap_s, "TextureWrapMode"));
236 printf(" T: %s\n", describe_enum(tex.wrap_t, "TextureWrapMode"));
237 printf(" R: %s\n", describe_enum(tex.wrap_r, "TextureWrapMode"));
238 printf(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
239 printf(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
240 printf(" Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
244 void Inspector::cmd_buffer(const string &args)
248 const GlState::BufferMap &buffers = state.get_buffers();
249 printf("%lu buffers:\n", buffers.size());
250 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
252 string descr = i->second.describe();
253 printf(" %d: %s\n", i->first, descr.c_str());
259 unsigned id = strtoul(args.c_str(), &end, 0);
261 throw runtime_error("Invalid buffer id");
263 const BufferState &buf = state.get_buffer(id);
264 printf("Buffer %d:\n", id);
265 printf(" Size: %d bytes\n", buf.size);
266 printf(" Usage: %s\n", describe_enum(buf.usage, ""));
267 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
269 const BufferContent::Array &array = buf.content.arrays.front();
270 printf(" Arrays:\n");
271 printf(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
274 unsigned width = 1+buf.content.stride*2;
276 snprintf(fmt, sizeof(fmt), " %%%du", width);
277 unsigned n_elems = buf.size/buf.content.stride;
279 const char *ptr = buf.data;
280 for(unsigned i=0; i<n_elems; ++i)
285 if(array.type==GL_UNSIGNED_BYTE)
286 line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
287 else if(array.type==GL_UNSIGNED_SHORT)
288 line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
289 else if(array.type==GL_UNSIGNED_INT)
290 line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
292 if(line.size()+1+width>79)
294 printf("%s\n", line.c_str());
300 printf("%s\n", line.c_str());
302 else if(buf.content.stride)
304 printf(" Stride: %d bytes\n", buf.content.stride);
306 printf(" Arrays:\n");
307 const vector<BufferContent::Array> &arrays = buf.content.arrays;
308 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
311 printf(" %2d: %s, %d %s\n", i->offset,
312 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
314 printf(" %2d: Attrib %d, %d %s\n", i->offset,
315 i->index, i->size, describe_enum(i->type, "DataType"));
320 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
326 if(i->kind==GL_VERTEX_ARRAY)
328 else if(i->kind==GL_NORMAL_ARRAY)
330 else if(i->kind==GL_COLOR_ARRAY)
332 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
342 label = strformat("A %d", i->index);
344 label = strformat("Attrib %d", i->index);
347 unsigned width = i->size;
348 if(i->type==GL_FLOAT)
350 else if(i->type==GL_UNSIGNED_BYTE)
354 header.append((width-label.size())/2, ' ');
356 header.append((width-label.size()+1)/2, ' ');
358 printf(" %s\n", header.c_str());
360 unsigned n_verts = buf.size/buf.content.stride;
361 for(unsigned i=0; i<n_verts; ++i)
363 const char *vertex = buf.data+i*buf.content.stride;
366 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
371 const char *base = vertex+j->offset;
372 for(unsigned k=0; k<j->size; ++k)
374 if(j->type==GL_FLOAT)
375 line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
376 else if(j->type==GL_UNSIGNED_BYTE)
377 line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
381 printf("%3d:%s\n", i, line.c_str());
387 void Inspector::cmd_shader(const string &args)
391 const GlState::ShaderMap &shaders = state.get_shaders();
392 printf("%lu shader objects:\n", shaders.size());
393 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
395 string descr = i->second.describe();
396 printf(" %d: %s\n", i->first, descr.c_str());
402 unsigned id = strtoul(args.c_str(), &end, 0);
404 throw runtime_error("Invalid shader id");
406 const ShaderState &shader = state.get_shader(id);
407 printf("Shader %d:\n", shader.id);
408 printf(" Type: %s\n", describe_enum(shader.type, ""));
410 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
412 printf(" Source string %d:\n", n);
413 print_indented(*i, 4);
415 if(shader.source_changed)
416 printf(" Source changed since last compile\n");
417 printf(" Compile status: %d\n", shader.compile_status);
418 if(shader.info_log.empty())
419 printf(" Info log is empty\n");
422 printf(" Info log:\n");
423 print_indented(shader.info_log, 4);
425 if(shader.pending_delete)
426 printf(" Pending deletion\n");
430 void Inspector::cmd_program(const std::string &args)
434 const GlState::ProgramMap &programs = state.get_programs();
435 printf("%lu program objects:\n", programs.size());
436 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
438 string descr = i->second.describe();
439 printf(" %d: %s\n", i->first, descr.c_str());
445 unsigned id = strtoul(args.c_str(), &end, 0);
447 throw runtime_error("Invalid program id");
449 const ProgramState &program = state.get_program(id);
450 printf("Program %d:\n", program.id);
451 printf(" Attached shaders:\n");
452 for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
454 string descr = (*i)->describe();
455 printf(" %d: %s\n", (*i)->id, descr.c_str());
457 if(program.shaders_changed)
458 printf(" Shaders changed since last compile\n");
459 printf(" Link status: %d\n", program.link_status);
460 if(program.info_log.empty())
461 printf(" Info log is empty\n");
464 printf(" Info log:\n");
465 print_indented(program.info_log, 4);