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 bindings\n");
27 cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
28 .set_help("Inspect texture state",
30 " Lists texture objects\n\n"
32 " Print information about a texture object\n");
34 cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
35 .set_help("Inspect buffer object state",
37 " Lists buffer objects\n\n"
39 " Print information about a buffer object\n");
41 cmd_interp.register_command("shader", this, &Inspector::cmd_shader)
42 .set_help("Inspect shader object state",
44 " List shader objects\n\n"
46 " Print information about a shader object\n");
48 cmd_interp.register_command("program", this, &Inspector::cmd_program)
49 .set_help("Inspect program object state",
51 " List program objects\n\n"
53 " Print information about a program object\n");
55 decoder->gldBreak = gldBreak;
58 void Inspector::decode(const char *data, unsigned len)
61 gldecoder_decode(decoder, data, len);
62 state.decode(data, len);
65 void Inspector::process_started()
67 gldbg.set_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
71 void Inspector::process_stopped(int reason)
73 if((reason>>8)==3 && query_state==2)
75 GlPacket *pkt = packet_begin(FUNC_GLDQUERYLIMITS);
78 gldbg.resume_from_break(this);
82 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
84 if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
85 ++reinterpret_cast<Inspector *>(user_data)->query_state;
88 void Inspector::print_indented(const string &str, unsigned indent)
90 string spaces(indent, ' ');
91 string::size_type start = 0;
94 string::size_type newline = str.find('\n', start);
95 string line = str.substr(start, newline-start);
96 if(newline!=string::npos || !line.empty())
97 printf("%s%s\n", spaces.c_str(), line.c_str());
98 if(newline==string::npos)
104 void Inspector::cmd_state(const string &args)
106 const GlState &glstate = state;
109 printf("Current vertex attributes:\n");
110 const Vector4 &color = glstate.get_color();
111 printf(" Color: [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
112 unsigned count = glstate.get_max_texture_units();
113 for(unsigned i=0; i<count; ++i)
115 const Vector4 &texcoord = glstate.get_texcoord(i);
116 printf(" TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
118 const Vector3 &normal = glstate.get_normal();
119 printf(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
121 else if(args=="bind")
123 printf("Current bindings:\n");
124 unsigned count = glstate.get_max_texture_units();
125 for(unsigned i=0; i<count; ++i)
127 printf(" Texture unit %d:\n", i);
128 const TexUnitState &unit = glstate.get_texture_unit(i);
129 string descr = unit.describe_binding(GL_TEXTURE_2D);
130 printf(" GL_TEXTURE_2D: %s\n", descr.c_str());
131 descr = unit.describe_binding(GL_TEXTURE_3D);
132 printf(" GL_TEXTURE_3D: %s\n", descr.c_str());
134 printf(" Buffers:\n");
135 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
136 printf(" GL_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
137 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
138 printf(" GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
139 buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
140 printf(" GL_UNIFORM_BUFFER: %d\n", (buf ? buf->id : 0));
141 count = glstate.get_max_uniform_buffer_bindings();
142 for(unsigned i=0; i<count; ++i)
144 const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
146 printf(" %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
150 throw runtime_error("Invalid or missing argument");
153 void Inspector::cmd_texture(const string &args)
157 const map<unsigned, TextureState> &textures = state.get_textures();
158 printf("%d texture objects:\n", textures.size());
159 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
161 const TextureState &tex = i->second;
162 string descr = tex.describe();
163 printf(" %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
169 unsigned id = strtoul(args.c_str(), &end, 0);
171 throw runtime_error("Invalid texture id");
173 const TextureState &tex = state.get_texture(id);
174 printf("Texture object %d\n", id);
175 printf(" Target: %s\n", describe_enum(tex.target, "TextureTarget"));
176 printf(" Images:\n");
177 for(unsigned i=0; i<tex.images.size(); ++i)
179 string descr = tex.images[i].describe();
180 printf(" Level %2d: %s\n", i, descr.c_str());
182 printf(" Min. filter: %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
183 printf(" Mag. filter: %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
184 printf(" Wrap modes: S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
185 describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
186 printf(" Compare mode: %s\n", describe_enum(tex.compare_mode, ""));
187 printf(" Compare func: %s\n", describe_enum(tex.compare_func, "DepthFunction"));
188 printf(" Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
192 void Inspector::cmd_buffer(const string &args)
196 const GlState::BufferMap &buffers = state.get_buffers();
197 printf("%d buffers:\n", buffers.size());
198 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
200 string descr = i->second.describe();
201 printf(" %d: %s\n", i->first, descr.c_str());
207 unsigned id = strtoul(args.c_str(), &end, 0);
209 throw runtime_error("Invalid buffer id");
211 const BufferState &buf = state.get_buffer(id);
212 printf("Buffer %d:\n", id);
213 printf(" Size: %d bytes\n", buf.size);
214 printf(" Usage: %s\n", describe_enum(buf.usage, ""));
215 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
217 const BufferContent::Array &array = buf.content.arrays.front();
218 printf(" Arrays:\n");
219 printf(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
222 unsigned width = 1+buf.content.stride*2;
224 snprintf(fmt, sizeof(fmt), " %%%du", width);
225 unsigned n_elems = buf.size/buf.content.stride;
227 const char *ptr = buf.data;
228 for(unsigned i=0; i<n_elems; ++i)
233 if(array.type==GL_UNSIGNED_BYTE)
234 line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
235 else if(array.type==GL_UNSIGNED_SHORT)
236 line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
237 else if(array.type==GL_UNSIGNED_INT)
238 line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
240 if(line.size()+1+width>79)
242 printf("%s\n", line.c_str());
248 printf("%s\n", line.c_str());
250 else if(buf.content.stride)
252 printf(" Stride: %d bytes\n", buf.content.stride);
254 printf(" Arrays:\n");
255 const vector<BufferContent::Array> &arrays = buf.content.arrays;
256 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
259 printf(" %2d: %s, %d %s\n", i->offset,
260 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
262 printf(" %2d: Attrib %d, %d %s\n", i->offset,
263 i->index, i->size, describe_enum(i->type, "DataType"));
268 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
274 if(i->kind==GL_VERTEX_ARRAY)
276 else if(i->kind==GL_NORMAL_ARRAY)
278 else if(i->kind==GL_COLOR_ARRAY)
280 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
290 label = strformat("A %d", i->index);
292 label = strformat("Attrib %d", i->index);
295 unsigned width = i->size;
296 if(i->type==GL_FLOAT)
298 else if(i->type==GL_UNSIGNED_BYTE)
302 header.append((width-label.size())/2, ' ');
304 header.append((width-label.size()+1)/2, ' ');
306 printf(" %s\n", header.c_str());
308 unsigned n_verts = buf.size/buf.content.stride;
309 for(unsigned i=0; i<n_verts; ++i)
311 const char *vertex = buf.data+i*buf.content.stride;
314 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
319 const char *base = vertex+j->offset;
320 for(unsigned k=0; k<j->size; ++k)
322 if(j->type==GL_FLOAT)
323 line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
324 else if(j->type==GL_UNSIGNED_BYTE)
325 line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
329 printf("%3d:%s\n", i, line.c_str());
335 void Inspector::cmd_shader(const string &args)
339 const GlState::ShaderMap &shaders = state.get_shaders();
340 printf("%d shader objects:\n", shaders.size());
341 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
343 string descr = i->second.describe();
344 printf(" %d: %s\n", i->first, descr.c_str());
350 unsigned id = strtoul(args.c_str(), &end, 0);
352 throw runtime_error("Invalid shader id");
354 const ShaderState &shader = state.get_shader(id);
355 printf("Shader %d:\n", shader.id);
356 printf(" Type: %s\n", describe_enum(shader.type, ""));
358 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
360 printf(" Source string %d:\n", n);
361 print_indented(*i, 4);
363 if(shader.source_changed)
364 printf(" Source changed since last compile\n");
365 printf(" Compile status: %d\n", shader.compile_status);
366 if(shader.info_log.empty())
367 printf(" Info log is empty\n");
370 printf(" Info log:\n");
371 print_indented(shader.info_log, 4);
373 if(shader.pending_delete)
374 printf(" Pending deletion\n");
378 void Inspector::cmd_program(const std::string &args)
382 const GlState::ProgramMap &programs = state.get_programs();
383 printf("%d program objects:\n", programs.size());
384 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
386 string descr = i->second.describe();
387 printf(" %d: %s\n", i->first, descr.c_str());
393 unsigned id = strtoul(args.c_str(), &end, 0);
395 throw runtime_error("Invalid program id");
397 const ProgramState &program = state.get_program(id);
398 printf("Program %d:\n", program.id);
399 printf(" Attached shaders:\n");
400 for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
402 string descr = (*i)->describe();
403 printf(" %d: %s\n", (*i)->id, descr.c_str());
405 if(program.shaders_changed)
406 printf(" Shaders changed since last compile\n");
407 printf(" Link status: %d\n", program.link_status);
408 if(program.info_log.empty())
409 printf(" Info log is empty\n");
412 printf(" Info log:\n");
413 print_indented(program.info_log, 4);