]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
b641b05d0381a19a10afe4c4b853cab86a5a39a0
[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.clear_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
86                 gldbg.resume_from_break(this);
87         }
88 }
89
90 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
91 {
92         if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
93                 ++reinterpret_cast<Inspector *>(user_data)->query_state;
94 }
95
96 void Inspector::print_indented(const string &str, unsigned indent)
97 {
98         string spaces(indent, ' ');
99         string::size_type start = 0;
100         while(1)
101         {
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)
107                         break;
108                 start = newline+1;
109         }
110 }
111
112 void Inspector::cmd_state(const string &args)
113 {
114         const GlState &glstate = state;
115         if(args=="vertex")
116         {
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)
122                 {
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);
125                 }
126                 const Vector3 &normal = glstate.get_normal();
127                 printf("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
128         }
129         else if(args=="array")
130         {
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)
140                 {
141                         descr = glstate.get_texture_coord_array(i).describe();
142                         printf("  TexCoord%d: %s\n", i, descr.c_str());
143                 }
144                 count = glstate.get_max_vertex_attribs();
145                 for(unsigned i=0; i<count; ++i)
146                 {
147                         descr = glstate.get_attrib_array(i).describe();
148                         printf("  Attrib%d:%s %s\n", i, (i>=10 ? " " : "  "), descr.c_str());
149                 }
150         }
151         else if(args=="bind")
152         {
153                 printf("Current bindings:\n");
154                 unsigned count = glstate.get_max_texture_units();
155                 for(unsigned i=0; i<count; ++i)
156                 {
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());
167                 }
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)
177                 {
178                         const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
179                         if(binding.buffer)
180                                 printf("      %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
181                 }
182         }
183         else
184                 throw runtime_error("Invalid or missing argument");
185 }
186
187 void Inspector::cmd_texture(const string &args)
188 {
189         if(args.empty())
190         {
191                 const map<unsigned, TextureState> &textures = state.get_textures();
192                 printf("%d texture objects:\n", textures.size());
193                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
194                 {
195                         const TextureState &tex = i->second;
196                         string descr = tex.describe();
197                         printf("  %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
198                 }
199         }
200         else
201         {
202                 char *end = 0;
203                 unsigned id = strtoul(args.c_str(), &end, 0);
204                 if(end && *end)
205                         throw runtime_error("Invalid texture id");
206
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)
212                 {
213                         static const char *face_labels[6] = { "+X", "-X", "+Y", "-Y", "+Z", "-Z" };
214                         for(unsigned i=0; i<6; ++i)
215                         {
216                                 printf("    Face %s:\n", face_labels[i]);
217                                 for(unsigned j=i; j<tex.images.size(); j+=6)
218                                 {
219                                         string descr = tex.images[j].describe();
220                                         printf("      Level %2d:    %s\n", j/6, descr.c_str());
221                                 }
222                         }
223                 }
224                 else
225                 {
226                         for(unsigned i=0; i<tex.images.size(); ++i)
227                         {
228                                 string descr = tex.images[i].describe();
229                                 printf("    Level %2d:      %s\n", i, descr.c_str());
230                         }
231                 }
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"));
241         }
242 }
243
244 void Inspector::cmd_buffer(const string &args)
245 {
246         if(args.empty())
247         {
248                 const GlState::BufferMap &buffers = state.get_buffers();
249                 printf("%d buffers:\n", buffers.size());
250                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
251                 {
252                         string descr = i->second.describe();
253                         printf("  %d: %s\n", i->first, descr.c_str());
254                 }
255         }
256         else
257         {
258                 char *end = 0;
259                 unsigned id = strtoul(args.c_str(), &end, 0);
260                 if(end && *end)
261                         throw runtime_error("Invalid buffer id");
262
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)
268                 {
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"));
272
273                         printf("  Data:\n");
274                         unsigned width = 1+buf.content.stride*2;
275                         char fmt[6];
276                         snprintf(fmt, sizeof(fmt), " %%%du", width);
277                         unsigned n_elems = buf.size/buf.content.stride;
278                         string line;
279                         const char *ptr = buf.data;
280                         for(unsigned i=0; i<n_elems; ++i)
281                         {
282                                 if(line.empty())
283                                         line = "   ";
284
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));
291
292                                 if(line.size()+1+width>79)
293                                 {
294                                         printf("%s\n", line.c_str());
295                                         line.clear();
296                                 }
297                         }
298
299                         if(!line.empty())
300                                 printf("%s\n", line.c_str());
301                 }
302                 else if(buf.content.stride)
303                 {
304                         printf("  Stride: %d bytes\n", buf.content.stride);
305
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)
309                         {
310                                 if(i->kind)
311                                         printf("    %2d: %s, %d %s\n", i->offset,
312                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
313                                 else
314                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
315                                                 i->index, i->size, describe_enum(i->type, "DataType"));
316                         }
317
318                         printf("  Data:\n");
319                         string header;
320                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
321                         {
322                                 if(!header.empty())
323                                         header += " | ";
324
325                                 string label;
326                                 if(i->kind==GL_VERTEX_ARRAY)
327                                         label = "Vertex";
328                                 else if(i->kind==GL_NORMAL_ARRAY)
329                                         label = "Normal";
330                                 else if(i->kind==GL_COLOR_ARRAY)
331                                         label = "Color";
332                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
333                                 {
334                                         if(i->size==1)
335                                                 label = "TexC";
336                                         else
337                                                 label = "TexCoord";
338                                 }
339                                 else if(!i->kind)
340                                 {
341                                         if(i->size==1)
342                                                 label = strformat("A %d", i->index);
343                                         else
344                                                 label = strformat("Attrib %d", i->index);
345                                 }
346
347                                 unsigned width = i->size;
348                                 if(i->type==GL_FLOAT)
349                                         width *= 5;
350                                 else if(i->type==GL_UNSIGNED_BYTE)
351                                         width *= 3;
352                                 width += i->size-1;
353
354                                 header.append((width-label.size())/2, ' ');
355                                 header += label;
356                                 header.append((width-label.size()+1)/2, ' ');
357                         }
358                         printf("     %s\n", header.c_str());
359
360                         unsigned n_verts = buf.size/buf.content.stride;
361                         for(unsigned i=0; i<n_verts; ++i)
362                         {
363                                 const char *vertex = buf.data+i*buf.content.stride;
364
365                                 string line;
366                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
367                                 {
368                                         if(!line.empty())
369                                                 line += " |";
370
371                                         const char *base = vertex+j->offset;
372                                         for(unsigned k=0; k<j->size; ++k)
373                                         {
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));
378                                         }
379                                 }
380
381                                 printf("%3d:%s\n", i, line.c_str());
382                         }
383                 }
384         }
385 }
386
387 void Inspector::cmd_shader(const string &args)
388 {
389         if(args.empty())
390         {
391                 const GlState::ShaderMap &shaders = state.get_shaders();
392                 printf("%d shader objects:\n", shaders.size());
393                 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
394                 {
395                         string descr = i->second.describe();
396                         printf("  %d: %s\n", i->first, descr.c_str());
397                 }
398         }
399         else
400         {
401                 char *end = 0;
402                 unsigned id = strtoul(args.c_str(), &end, 0);
403                 if(end && *end)
404                         throw runtime_error("Invalid shader id");
405
406                 const ShaderState &shader = state.get_shader(id);
407                 printf("Shader %d:\n", shader.id);
408                 printf("  Type: %s\n", describe_enum(shader.type, ""));
409                 unsigned n = 0;
410                 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
411                 {
412                         printf("  Source string %d:\n", n);
413                         print_indented(*i, 4);
414                 }
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");
420                 else
421                 {
422                         printf("  Info log:\n");
423                         print_indented(shader.info_log, 4);
424                 }
425                 if(shader.pending_delete)
426                         printf("  Pending deletion\n");
427         }
428 }
429
430 void Inspector::cmd_program(const std::string &args)
431 {
432         if(args.empty())
433         {
434                 const GlState::ProgramMap &programs = state.get_programs();
435                 printf("%d program objects:\n", programs.size());
436                 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
437                 {
438                         string descr = i->second.describe();
439                         printf("  %d: %s\n", i->first, descr.c_str());
440                 }
441         }
442         else
443         {
444                 char *end = 0;
445                 unsigned id = strtoul(args.c_str(), &end, 0);
446                 if(end && *end)
447                         throw runtime_error("Invalid program id");
448
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)
453                 {
454                         string descr = (*i)->describe();
455                         printf("    %d: %s\n", (*i)->id, descr.c_str());
456                 }
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");
462                 else
463                 {
464                         printf("  Info log:\n");
465                         print_indented(program.info_log, 4);
466                 }
467         }
468 }