]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
Fix several problems reported by valgrind
[gldbg.git] / flavors / gl / source / inspector.cpp
1 #include <stdexcept>
2 #include <cstdio>
3 #include <cstdlib>
4 #include "breakpoint.h"
5 #include "enums.h"
6 #include "functions.h"
7 #include "gldbg.h"
8 #include "inspector.h"
9 #include "strformat.h"
10
11 using namespace std;
12
13 Inspector::Inspector(GlDbg &d):
14         gldbg(d),
15         decoder(gldecoder_new(this, NULL)),
16         query_state(0)
17 {
18         CommandInterpreter &cmd_interp = gldbg.get_command_interpreter();
19
20         cmd_interp.register_command("state", this, &Inspector::cmd_state)
21                 .set_help("Inspects general GL state",
22                         "state vertex\n"
23                         "  Print current vertex attributes\n\n"
24                         "state array\n"
25                         "  Show current vertex arrays\n\n"
26                         "state bind\n"
27                         "  Show current bindings\n");
28
29         cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
30                 .set_help("Inspect texture state",
31                         "texture\n"
32                         "  Lists texture objects\n\n"
33                         "texture ID\n"
34                         "  Print information about a texture object\n");
35
36         cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
37                 .set_help("Inspect buffer object state",
38                         "buffer\n"
39                         "  Lists buffer objects\n\n"
40                         "buffer ID\n"
41                         "  Print information about a buffer object\n");
42
43         cmd_interp.register_command("shader", this, &Inspector::cmd_shader)
44                 .set_help("Inspect shader object state",
45                         "shader\n"
46                         "  List shader objects\n\n"
47                         "shader ID\n"
48                         "  Print information about a shader object\n");
49
50         cmd_interp.register_command("program", this, &Inspector::cmd_program)
51                 .set_help("Inspect program object state",
52                         "program\n"
53                         "  List program objects\n\n"
54                         "program ID\n"
55                         "  Print information about a program object\n");
56
57         decoder->gldBreak = gldBreak;
58 }
59
60 Inspector::~Inspector()
61 {
62         gldecoder_delete(decoder);
63 }
64
65 void Inspector::decode(const char *data, unsigned len)
66 {
67         if(query_state)
68                 gldecoder_decode(decoder, data, len);
69         state.decode(data, len);
70 }
71
72 void Inspector::process_started()
73 {
74         gldbg.set_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
75         query_state = 1;
76 }
77
78 void Inspector::process_stopped(int reason)
79 {
80         if((reason>>8)==3 && query_state==2)
81         {
82                 GlPacket *pkt = packet_begin(FUNC_GLDQUERYLIMITS);
83                 gldbg.send(pkt);
84                 query_state = 0;
85                 gldbg.resume_from_break(this);
86         }
87 }
88
89 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
90 {
91         if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
92                 ++reinterpret_cast<Inspector *>(user_data)->query_state;
93 }
94
95 void Inspector::print_indented(const string &str, unsigned indent)
96 {
97         string spaces(indent, ' ');
98         string::size_type start = 0;
99         while(1)
100         {
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)
106                         break;
107                 start = newline+1;
108         }
109 }
110
111 void Inspector::cmd_state(const string &args)
112 {
113         const GlState &glstate = state;
114         if(args=="vertex")
115         {
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)
121                 {
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);
124                 }
125                 const Vector3 &normal = glstate.get_normal();
126                 printf("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
127         }
128         else if(args=="array")
129         {
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)
139                 {
140                         descr = glstate.get_texture_coord_array(i).describe();
141                         printf("  TexCoord%d: %s\n", i, descr.c_str());
142                 }
143                 count = glstate.get_max_vertex_attribs();
144                 for(unsigned i=0; i<count; ++i)
145                 {
146                         descr = glstate.get_attrib_array(i).describe();
147                         printf("  Attrib%d:%s %s\n", i, (i>=10 ? " " : "  "), descr.c_str());
148                 }
149         }
150         else if(args=="bind")
151         {
152                 printf("Current bindings:\n");
153                 unsigned count = glstate.get_max_texture_units();
154                 for(unsigned i=0; i<count; ++i)
155                 {
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());
166                 }
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)
176                 {
177                         const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
178                         if(binding.buffer)
179                                 printf("      %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
180                 }
181         }
182         else
183                 throw runtime_error("Invalid or missing argument");
184 }
185
186 void Inspector::cmd_texture(const string &args)
187 {
188         if(args.empty())
189         {
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)
193                 {
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());
197                 }
198         }
199         else
200         {
201                 char *end = 0;
202                 unsigned id = strtoul(args.c_str(), &end, 0);
203                 if(end && *end)
204                         throw runtime_error("Invalid texture id");
205
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)
211                 {
212                         static const char *face_labels[6] = { "+X", "-X", "+Y", "-Y", "+Z", "-Z" };
213                         for(unsigned i=0; i<6; ++i)
214                         {
215                                 printf("    Face %s:\n", face_labels[i]);
216                                 for(unsigned j=i; j<tex.images.size(); j+=6)
217                                 {
218                                         string descr = tex.images[j].describe();
219                                         printf("      Level %2d:    %s\n", j/6, descr.c_str());
220                                 }
221                         }
222                 }
223                 else
224                 {
225                         for(unsigned i=0; i<tex.images.size(); ++i)
226                         {
227                                 string descr = tex.images[i].describe();
228                                 printf("    Level %2d:      %s\n", i, descr.c_str());
229                         }
230                 }
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"));
240         }
241 }
242
243 void Inspector::cmd_buffer(const string &args)
244 {
245         if(args.empty())
246         {
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)
250                 {
251                         string descr = i->second.describe();
252                         printf("  %d: %s\n", i->first, descr.c_str());
253                 }
254         }
255         else
256         {
257                 char *end = 0;
258                 unsigned id = strtoul(args.c_str(), &end, 0);
259                 if(end && *end)
260                         throw runtime_error("Invalid buffer id");
261
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)
267                 {
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"));
271
272                         printf("  Data:\n");
273                         unsigned width = 1+buf.content.stride*2;
274                         char fmt[6];
275                         snprintf(fmt, sizeof(fmt), " %%%du", width);
276                         unsigned n_elems = buf.size/buf.content.stride;
277                         string line;
278                         const char *ptr = buf.data;
279                         for(unsigned i=0; i<n_elems; ++i)
280                         {
281                                 if(line.empty())
282                                         line = "   ";
283
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));
290
291                                 if(line.size()+1+width>79)
292                                 {
293                                         printf("%s\n", line.c_str());
294                                         line.clear();
295                                 }
296                         }
297
298                         if(!line.empty())
299                                 printf("%s\n", line.c_str());
300                 }
301                 else if(buf.content.stride)
302                 {
303                         printf("  Stride: %d bytes\n", buf.content.stride);
304
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)
308                         {
309                                 if(i->kind)
310                                         printf("    %2d: %s, %d %s\n", i->offset,
311                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
312                                 else
313                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
314                                                 i->index, i->size, describe_enum(i->type, "DataType"));
315                         }
316
317                         printf("  Data:\n");
318                         string header;
319                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
320                         {
321                                 if(!header.empty())
322                                         header += " | ";
323
324                                 string label;
325                                 if(i->kind==GL_VERTEX_ARRAY)
326                                         label = "Vertex";
327                                 else if(i->kind==GL_NORMAL_ARRAY)
328                                         label = "Normal";
329                                 else if(i->kind==GL_COLOR_ARRAY)
330                                         label = "Color";
331                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
332                                 {
333                                         if(i->size==1)
334                                                 label = "TexC";
335                                         else
336                                                 label = "TexCoord";
337                                 }
338                                 else if(!i->kind)
339                                 {
340                                         if(i->size==1)
341                                                 label = strformat("A %d", i->index);
342                                         else
343                                                 label = strformat("Attrib %d", i->index);
344                                 }
345
346                                 unsigned width = i->size;
347                                 if(i->type==GL_FLOAT)
348                                         width *= 5;
349                                 else if(i->type==GL_UNSIGNED_BYTE)
350                                         width *= 3;
351                                 width += i->size-1;
352
353                                 header.append((width-label.size())/2, ' ');
354                                 header += label;
355                                 header.append((width-label.size()+1)/2, ' ');
356                         }
357                         printf("     %s\n", header.c_str());
358
359                         unsigned n_verts = buf.size/buf.content.stride;
360                         for(unsigned i=0; i<n_verts; ++i)
361                         {
362                                 const char *vertex = buf.data+i*buf.content.stride;
363
364                                 string line;
365                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
366                                 {
367                                         if(!line.empty())
368                                                 line += " |";
369
370                                         const char *base = vertex+j->offset;
371                                         for(unsigned k=0; k<j->size; ++k)
372                                         {
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));
377                                         }
378                                 }
379
380                                 printf("%3d:%s\n", i, line.c_str());
381                         }
382                 }
383         }
384 }
385
386 void Inspector::cmd_shader(const string &args)
387 {
388         if(args.empty())
389         {
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)
393                 {
394                         string descr = i->second.describe();
395                         printf("  %d: %s\n", i->first, descr.c_str());
396                 }
397         }
398         else
399         {
400                 char *end = 0;
401                 unsigned id = strtoul(args.c_str(), &end, 0);
402                 if(end && *end)
403                         throw runtime_error("Invalid shader id");
404
405                 const ShaderState &shader = state.get_shader(id);
406                 printf("Shader %d:\n", shader.id);
407                 printf("  Type: %s\n", describe_enum(shader.type, ""));
408                 unsigned n = 0;
409                 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
410                 {
411                         printf("  Source string %d:\n", n);
412                         print_indented(*i, 4);
413                 }
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");
419                 else
420                 {
421                         printf("  Info log:\n");
422                         print_indented(shader.info_log, 4);
423                 }
424                 if(shader.pending_delete)
425                         printf("  Pending deletion\n");
426         }
427 }
428
429 void Inspector::cmd_program(const std::string &args)
430 {
431         if(args.empty())
432         {
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)
436                 {
437                         string descr = i->second.describe();
438                         printf("  %d: %s\n", i->first, descr.c_str());
439                 }
440         }
441         else
442         {
443                 char *end = 0;
444                 unsigned id = strtoul(args.c_str(), &end, 0);
445                 if(end && *end)
446                         throw runtime_error("Invalid program id");
447
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)
452                 {
453                         string descr = (*i)->describe();
454                         printf("    %d: %s\n", (*i)->id, descr.c_str());
455                 }
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");
461                 else
462                 {
463                         printf("  Info log:\n");
464                         print_indented(program.info_log, 4);
465                 }
466         }
467 }