]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
b6456ce7ec1afcfabf52e8b995bb2840cde1e38d
[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 void Inspector::decode(const char *data, unsigned len)
61 {
62         if(query_state)
63                 gldecoder_decode(decoder, data, len);
64         state.decode(data, len);
65 }
66
67 void Inspector::process_started()
68 {
69         gldbg.set_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
70         query_state = 1;
71 }
72
73 void Inspector::process_stopped(int reason)
74 {
75         if((reason>>8)==3 && query_state==2)
76         {
77                 GlPacket *pkt = packet_begin(FUNC_GLDQUERYLIMITS);
78                 gldbg.send(pkt);
79                 query_state = 0;
80                 gldbg.resume_from_break(this);
81         }
82 }
83
84 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
85 {
86         if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
87                 ++reinterpret_cast<Inspector *>(user_data)->query_state;
88 }
89
90 void Inspector::print_indented(const string &str, unsigned indent)
91 {
92         string spaces(indent, ' ');
93         string::size_type start = 0;
94         while(1)
95         {
96                 string::size_type newline = str.find('\n', start);
97                 string line = str.substr(start, newline-start);
98                 if(newline!=string::npos || !line.empty())
99                         printf("%s%s\n", spaces.c_str(), line.c_str());
100                 if(newline==string::npos)
101                         break;
102                 start = newline+1;
103         }
104 }
105
106 void Inspector::cmd_state(const string &args)
107 {
108         const GlState &glstate = state;
109         if(args=="vertex")
110         {
111                 printf("Current vertex attributes:\n");
112                 const Vector4 &color = glstate.get_color();
113                 printf("  Color:     [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
114                 unsigned count = glstate.get_max_texture_units();
115                 for(unsigned i=0; i<count; ++i)
116                 {
117                         const Vector4 &texcoord = glstate.get_texcoord(i);
118                         printf("  TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
119                 }
120                 const Vector3 &normal = glstate.get_normal();
121                 printf("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
122         }
123         else if(args=="array")
124         {
125                 printf("Current vertex arrays:\n");
126                 string descr = glstate.get_array(GL_VERTEX_ARRAY).describe();
127                 printf("  Vertex:    %s\n", descr.c_str());
128                 descr = glstate.get_array(GL_NORMAL_ARRAY).describe();
129                 printf("  Normal:    %s\n", descr.c_str());
130                 descr = glstate.get_array(GL_COLOR_ARRAY).describe();
131                 printf("  Color:     %s\n", descr.c_str());
132                 unsigned count = glstate.get_max_texture_units();
133                 for(unsigned i=0; i<count; ++i)
134                 {
135                         descr = glstate.get_texture_coord_array(i).describe();
136                         printf("  TexCoord%d: %s\n", i, descr.c_str());
137                 }
138                 count = glstate.get_max_vertex_attribs();
139                 for(unsigned i=0; i<count; ++i)
140                 {
141                         descr = glstate.get_attrib_array(i).describe();
142                         printf("  Attrib%d:%s %s\n", i, (i>=10 ? " " : "  "), descr.c_str());
143                 }
144         }
145         else if(args=="bind")
146         {
147                 printf("Current bindings:\n");
148                 unsigned count = glstate.get_max_texture_units();
149                 for(unsigned i=0; i<count; ++i)
150                 {
151                         printf("  Texture unit %d:\n", i);
152                         const TexUnitState &unit = glstate.get_texture_unit(i);
153                         string descr = unit.describe_binding(GL_TEXTURE_1D);
154                         printf("    GL_TEXTURE_1D:       %s\n", descr.c_str());
155                         descr = unit.describe_binding(GL_TEXTURE_2D);
156                         printf("    GL_TEXTURE_2D:       %s\n", descr.c_str());
157                         descr = unit.describe_binding(GL_TEXTURE_3D);
158                         printf("    GL_TEXTURE_3D:       %s\n", descr.c_str());
159                         descr = unit.describe_binding(GL_TEXTURE_CUBE_MAP);
160                         printf("    GL_TEXTURE_CUBE_MAP: %s\n", descr.c_str());
161                 }
162                 printf("  Buffers:\n");
163                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
164                 printf("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
165                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
166                 printf("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
167                 buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
168                 printf("    GL_UNIFORM_BUFFER:       %d\n", (buf ? buf->id : 0));
169                 count = glstate.get_max_uniform_buffer_bindings();
170                 for(unsigned i=0; i<count; ++i)
171                 {
172                         const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
173                         if(binding.buffer)
174                                 printf("      %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
175                 }
176         }
177         else
178                 throw runtime_error("Invalid or missing argument");
179 }
180
181 void Inspector::cmd_texture(const string &args)
182 {
183         if(args.empty())
184         {
185                 const map<unsigned, TextureState> &textures = state.get_textures();
186                 printf("%d texture objects:\n", textures.size());
187                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
188                 {
189                         const TextureState &tex = i->second;
190                         string descr = tex.describe();
191                         printf("  %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
192                 }
193         }
194         else
195         {
196                 char *end = 0;
197                 unsigned id = strtoul(args.c_str(), &end, 0);
198                 if(end && *end)
199                         throw runtime_error("Invalid texture id");
200
201                 const TextureState &tex = state.get_texture(id);
202                 printf("Texture object %d\n", id);
203                 printf("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
204                 printf("  Images:\n");
205                 if(tex.target==GL_TEXTURE_CUBE_MAP)
206                 {
207                         static const char *face_labels[6] = { "+X", "-X", "+Y", "-Y", "+Z", "-Z" };
208                         for(unsigned i=0; i<6; ++i)
209                         {
210                                 printf("    Face %s:\n", face_labels[i]);
211                                 for(unsigned j=i; j<tex.images.size(); j+=6)
212                                 {
213                                         string descr = tex.images[j].describe();
214                                         printf("      Level %2d:    %s\n", j/6, descr.c_str());
215                                 }
216                         }
217                 }
218                 else
219                 {
220                         for(unsigned i=0; i<tex.images.size(); ++i)
221                         {
222                                 string descr = tex.images[i].describe();
223                                 printf("    Level %2d:      %s\n", i, descr.c_str());
224                         }
225                 }
226                 printf("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
227                 printf("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
228                 printf("  Wrap modes:\n");
229                 printf("    S:             %s\n", describe_enum(tex.wrap_s, "TextureWrapMode"));
230                 printf("    T:             %s\n", describe_enum(tex.wrap_t, "TextureWrapMode"));
231                 printf("    R:             %s\n", describe_enum(tex.wrap_r, "TextureWrapMode"));
232                 printf("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
233                 printf("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
234                 printf("  Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
235         }
236 }
237
238 void Inspector::cmd_buffer(const string &args)
239 {
240         if(args.empty())
241         {
242                 const GlState::BufferMap &buffers = state.get_buffers();
243                 printf("%d buffers:\n", buffers.size());
244                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
245                 {
246                         string descr = i->second.describe();
247                         printf("  %d: %s\n", i->first, descr.c_str());
248                 }
249         }
250         else
251         {
252                 char *end = 0;
253                 unsigned id = strtoul(args.c_str(), &end, 0);
254                 if(end && *end)
255                         throw runtime_error("Invalid buffer id");
256
257                 const BufferState &buf = state.get_buffer(id);
258                 printf("Buffer %d:\n", id);
259                 printf("  Size:  %d bytes\n", buf.size);
260                 printf("  Usage: %s\n", describe_enum(buf.usage, ""));
261                 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
262                 {
263                         const BufferContent::Array &array = buf.content.arrays.front();
264                         printf("  Arrays:\n");
265                         printf("    0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
266
267                         printf("  Data:\n");
268                         unsigned width = 1+buf.content.stride*2;
269                         char fmt[6];
270                         snprintf(fmt, sizeof(fmt), " %%%du", width);
271                         unsigned n_elems = buf.size/buf.content.stride;
272                         string line;
273                         const char *ptr = buf.data;
274                         for(unsigned i=0; i<n_elems; ++i)
275                         {
276                                 if(line.empty())
277                                         line = "   ";
278
279                                 if(array.type==GL_UNSIGNED_BYTE)
280                                         line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
281                                 else if(array.type==GL_UNSIGNED_SHORT)
282                                         line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
283                                 else if(array.type==GL_UNSIGNED_INT)
284                                         line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
285
286                                 if(line.size()+1+width>79)
287                                 {
288                                         printf("%s\n", line.c_str());
289                                         line.clear();
290                                 }
291                         }
292
293                         if(!line.empty())
294                                 printf("%s\n", line.c_str());
295                 }
296                 else if(buf.content.stride)
297                 {
298                         printf("  Stride: %d bytes\n", buf.content.stride);
299
300                         printf("  Arrays:\n");
301                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
302                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
303                         {
304                                 if(i->kind)
305                                         printf("    %2d: %s, %d %s\n", i->offset,
306                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
307                                 else
308                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
309                                                 i->index, i->size, describe_enum(i->type, "DataType"));
310                         }
311
312                         printf("  Data:\n");
313                         string header;
314                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
315                         {
316                                 if(!header.empty())
317                                         header += " | ";
318
319                                 string label;
320                                 if(i->kind==GL_VERTEX_ARRAY)
321                                         label = "Vertex";
322                                 else if(i->kind==GL_NORMAL_ARRAY)
323                                         label = "Normal";
324                                 else if(i->kind==GL_COLOR_ARRAY)
325                                         label = "Color";
326                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
327                                 {
328                                         if(i->size==1)
329                                                 label = "TexC";
330                                         else
331                                                 label = "TexCoord";
332                                 }
333                                 else if(!i->kind)
334                                 {
335                                         if(i->size==1)
336                                                 label = strformat("A %d", i->index);
337                                         else
338                                                 label = strformat("Attrib %d", i->index);
339                                 }
340
341                                 unsigned width = i->size;
342                                 if(i->type==GL_FLOAT)
343                                         width *= 5;
344                                 else if(i->type==GL_UNSIGNED_BYTE)
345                                         width *= 3;
346                                 width += i->size-1;
347
348                                 header.append((width-label.size())/2, ' ');
349                                 header += label;
350                                 header.append((width-label.size()+1)/2, ' ');
351                         }
352                         printf("     %s\n", header.c_str());
353
354                         unsigned n_verts = buf.size/buf.content.stride;
355                         for(unsigned i=0; i<n_verts; ++i)
356                         {
357                                 const char *vertex = buf.data+i*buf.content.stride;
358
359                                 string line;
360                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
361                                 {
362                                         if(!line.empty())
363                                                 line += " |";
364
365                                         const char *base = vertex+j->offset;
366                                         for(unsigned k=0; k<j->size; ++k)
367                                         {
368                                                 if(j->type==GL_FLOAT)
369                                                         line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
370                                                 else if(j->type==GL_UNSIGNED_BYTE)
371                                                         line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
372                                         }
373                                 }
374
375                                 printf("%3d:%s\n", i, line.c_str());
376                         }
377                 }
378         }
379 }
380
381 void Inspector::cmd_shader(const string &args)
382 {
383         if(args.empty())
384         {
385                 const GlState::ShaderMap &shaders = state.get_shaders();
386                 printf("%d shader objects:\n", shaders.size());
387                 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
388                 {
389                         string descr = i->second.describe();
390                         printf("  %d: %s\n", i->first, descr.c_str());
391                 }
392         }
393         else
394         {
395                 char *end = 0;
396                 unsigned id = strtoul(args.c_str(), &end, 0);
397                 if(end && *end)
398                         throw runtime_error("Invalid shader id");
399
400                 const ShaderState &shader = state.get_shader(id);
401                 printf("Shader %d:\n", shader.id);
402                 printf("  Type: %s\n", describe_enum(shader.type, ""));
403                 unsigned n = 0;
404                 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
405                 {
406                         printf("  Source string %d:\n", n);
407                         print_indented(*i, 4);
408                 }
409                 if(shader.source_changed)
410                         printf("  Source changed since last compile\n");
411                 printf("  Compile status: %d\n", shader.compile_status);
412                 if(shader.info_log.empty())
413                         printf("  Info log is empty\n");
414                 else
415                 {
416                         printf("  Info log:\n");
417                         print_indented(shader.info_log, 4);
418                 }
419                 if(shader.pending_delete)
420                         printf("  Pending deletion\n");
421         }
422 }
423
424 void Inspector::cmd_program(const std::string &args)
425 {
426         if(args.empty())
427         {
428                 const GlState::ProgramMap &programs = state.get_programs();
429                 printf("%d program objects:\n", programs.size());
430                 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
431                 {
432                         string descr = i->second.describe();
433                         printf("  %d: %s\n", i->first, descr.c_str());
434                 }
435         }
436         else
437         {
438                 char *end = 0;
439                 unsigned id = strtoul(args.c_str(), &end, 0);
440                 if(end && *end)
441                         throw runtime_error("Invalid program id");
442
443                 const ProgramState &program = state.get_program(id);
444                 printf("Program %d:\n", program.id);
445                 printf("  Attached shaders:\n");
446                 for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
447                 {
448                         string descr = (*i)->describe();
449                         printf("    %d: %s\n", (*i)->id, descr.c_str());
450                 }
451                 if(program.shaders_changed)
452                         printf("  Shaders changed since last compile\n");
453                 printf("  Link status: %d\n", program.link_status);
454                 if(program.info_log.empty())
455                         printf("  Info log is empty\n");
456                 else
457                 {
458                         printf("  Info log:\n");
459                         print_indented(program.info_log, 4);
460                 }
461         }
462 }