]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
Add a command to show current vertex array state
[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_2D);
154                         printf("    GL_TEXTURE_2D: %s\n", descr.c_str());
155                         descr = unit.describe_binding(GL_TEXTURE_3D);
156                         printf("    GL_TEXTURE_3D: %s\n", descr.c_str());
157                 }
158                 printf("  Buffers:\n");
159                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
160                 printf("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
161                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
162                 printf("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
163                 buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
164                 printf("    GL_UNIFORM_BUFFER:       %d\n", (buf ? buf->id : 0));
165                 count = glstate.get_max_uniform_buffer_bindings();
166                 for(unsigned i=0; i<count; ++i)
167                 {
168                         const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
169                         if(binding.buffer)
170                                 printf("      %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
171                 }
172         }
173         else
174                 throw runtime_error("Invalid or missing argument");
175 }
176
177 void Inspector::cmd_texture(const string &args)
178 {
179         if(args.empty())
180         {
181                 const map<unsigned, TextureState> &textures = state.get_textures();
182                 printf("%d texture objects:\n", textures.size());
183                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
184                 {
185                         const TextureState &tex = i->second;
186                         string descr = tex.describe();
187                         printf("  %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
188                 }
189         }
190         else
191         {
192                 char *end = 0;
193                 unsigned id = strtoul(args.c_str(), &end, 0);
194                 if(end && *end)
195                         throw runtime_error("Invalid texture id");
196
197                 const TextureState &tex = state.get_texture(id);
198                 printf("Texture object %d\n", id);
199                 printf("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
200                 printf("  Images:\n");
201                 for(unsigned i=0; i<tex.images.size(); ++i)
202                 {
203                         string descr = tex.images[i].describe();
204                         printf("    Level %2d:      %s\n", i, descr.c_str());
205                 }
206                 printf("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
207                 printf("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
208                 printf("  Wrap modes:      S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
209                         describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
210                 printf("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
211                 printf("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
212                 printf("  Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
213         }
214 }
215
216 void Inspector::cmd_buffer(const string &args)
217 {
218         if(args.empty())
219         {
220                 const GlState::BufferMap &buffers = state.get_buffers();
221                 printf("%d buffers:\n", buffers.size());
222                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
223                 {
224                         string descr = i->second.describe();
225                         printf("  %d: %s\n", i->first, descr.c_str());
226                 }
227         }
228         else
229         {
230                 char *end = 0;
231                 unsigned id = strtoul(args.c_str(), &end, 0);
232                 if(end && *end)
233                         throw runtime_error("Invalid buffer id");
234
235                 const BufferState &buf = state.get_buffer(id);
236                 printf("Buffer %d:\n", id);
237                 printf("  Size:  %d bytes\n", buf.size);
238                 printf("  Usage: %s\n", describe_enum(buf.usage, ""));
239                 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
240                 {
241                         const BufferContent::Array &array = buf.content.arrays.front();
242                         printf("  Arrays:\n");
243                         printf("    0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
244
245                         printf("  Data:\n");
246                         unsigned width = 1+buf.content.stride*2;
247                         char fmt[6];
248                         snprintf(fmt, sizeof(fmt), " %%%du", width);
249                         unsigned n_elems = buf.size/buf.content.stride;
250                         string line;
251                         const char *ptr = buf.data;
252                         for(unsigned i=0; i<n_elems; ++i)
253                         {
254                                 if(line.empty())
255                                         line = "   ";
256
257                                 if(array.type==GL_UNSIGNED_BYTE)
258                                         line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
259                                 else if(array.type==GL_UNSIGNED_SHORT)
260                                         line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
261                                 else if(array.type==GL_UNSIGNED_INT)
262                                         line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
263
264                                 if(line.size()+1+width>79)
265                                 {
266                                         printf("%s\n", line.c_str());
267                                         line.clear();
268                                 }
269                         }
270
271                         if(!line.empty())
272                                 printf("%s\n", line.c_str());
273                 }
274                 else if(buf.content.stride)
275                 {
276                         printf("  Stride: %d bytes\n", buf.content.stride);
277
278                         printf("  Arrays:\n");
279                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
280                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
281                         {
282                                 if(i->kind)
283                                         printf("    %2d: %s, %d %s\n", i->offset,
284                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
285                                 else
286                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
287                                                 i->index, i->size, describe_enum(i->type, "DataType"));
288                         }
289
290                         printf("  Data:\n");
291                         string header;
292                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
293                         {
294                                 if(!header.empty())
295                                         header += " | ";
296
297                                 string label;
298                                 if(i->kind==GL_VERTEX_ARRAY)
299                                         label = "Vertex";
300                                 else if(i->kind==GL_NORMAL_ARRAY)
301                                         label = "Normal";
302                                 else if(i->kind==GL_COLOR_ARRAY)
303                                         label = "Color";
304                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
305                                 {
306                                         if(i->size==1)
307                                                 label = "TexC";
308                                         else
309                                                 label = "TexCoord";
310                                 }
311                                 else if(!i->kind)
312                                 {
313                                         if(i->size==1)
314                                                 label = strformat("A %d", i->index);
315                                         else
316                                                 label = strformat("Attrib %d", i->index);
317                                 }
318
319                                 unsigned width = i->size;
320                                 if(i->type==GL_FLOAT)
321                                         width *= 5;
322                                 else if(i->type==GL_UNSIGNED_BYTE)
323                                         width *= 3;
324                                 width += i->size-1;
325
326                                 header.append((width-label.size())/2, ' ');
327                                 header += label;
328                                 header.append((width-label.size()+1)/2, ' ');
329                         }
330                         printf("     %s\n", header.c_str());
331
332                         unsigned n_verts = buf.size/buf.content.stride;
333                         for(unsigned i=0; i<n_verts; ++i)
334                         {
335                                 const char *vertex = buf.data+i*buf.content.stride;
336
337                                 string line;
338                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
339                                 {
340                                         if(!line.empty())
341                                                 line += " |";
342
343                                         const char *base = vertex+j->offset;
344                                         for(unsigned k=0; k<j->size; ++k)
345                                         {
346                                                 if(j->type==GL_FLOAT)
347                                                         line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
348                                                 else if(j->type==GL_UNSIGNED_BYTE)
349                                                         line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
350                                         }
351                                 }
352
353                                 printf("%3d:%s\n", i, line.c_str());
354                         }
355                 }
356         }
357 }
358
359 void Inspector::cmd_shader(const string &args)
360 {
361         if(args.empty())
362         {
363                 const GlState::ShaderMap &shaders = state.get_shaders();
364                 printf("%d shader objects:\n", shaders.size());
365                 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
366                 {
367                         string descr = i->second.describe();
368                         printf("  %d: %s\n", i->first, descr.c_str());
369                 }
370         }
371         else
372         {
373                 char *end = 0;
374                 unsigned id = strtoul(args.c_str(), &end, 0);
375                 if(end && *end)
376                         throw runtime_error("Invalid shader id");
377
378                 const ShaderState &shader = state.get_shader(id);
379                 printf("Shader %d:\n", shader.id);
380                 printf("  Type: %s\n", describe_enum(shader.type, ""));
381                 unsigned n = 0;
382                 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
383                 {
384                         printf("  Source string %d:\n", n);
385                         print_indented(*i, 4);
386                 }
387                 if(shader.source_changed)
388                         printf("  Source changed since last compile\n");
389                 printf("  Compile status: %d\n", shader.compile_status);
390                 if(shader.info_log.empty())
391                         printf("  Info log is empty\n");
392                 else
393                 {
394                         printf("  Info log:\n");
395                         print_indented(shader.info_log, 4);
396                 }
397                 if(shader.pending_delete)
398                         printf("  Pending deletion\n");
399         }
400 }
401
402 void Inspector::cmd_program(const std::string &args)
403 {
404         if(args.empty())
405         {
406                 const GlState::ProgramMap &programs = state.get_programs();
407                 printf("%d program objects:\n", programs.size());
408                 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
409                 {
410                         string descr = i->second.describe();
411                         printf("  %d: %s\n", i->first, descr.c_str());
412                 }
413         }
414         else
415         {
416                 char *end = 0;
417                 unsigned id = strtoul(args.c_str(), &end, 0);
418                 if(end && *end)
419                         throw runtime_error("Invalid program id");
420
421                 const ProgramState &program = state.get_program(id);
422                 printf("Program %d:\n", program.id);
423                 printf("  Attached shaders:\n");
424                 for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
425                 {
426                         string descr = (*i)->describe();
427                         printf("    %d: %s\n", (*i)->id, descr.c_str());
428                 }
429                 if(program.shaders_changed)
430                         printf("  Shaders changed since last compile\n");
431                 printf("  Link status: %d\n", program.link_status);
432                 if(program.info_log.empty())
433                         printf("  Info log is empty\n");
434                 else
435                 {
436                         printf("  Info log:\n");
437                         print_indented(program.info_log, 4);
438                 }
439         }
440 }