]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
Query implementation limits on process startup
[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 bind\n"
25                         "  Show current bindings\n");
26
27         cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
28                 .set_help("Inspect texture state",
29                         "texture\n"
30                         "  Lists texture objects\n\n"
31                         "texture ID\n"
32                         "  Print information about a texture object\n");
33
34         cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
35                 .set_help("Inspect buffer object state",
36                         "buffer\n"
37                         "  Lists buffer objects\n\n"
38                         "buffer ID\n"
39                         "  Print information about a buffer object\n");
40
41         cmd_interp.register_command("shader", this, &Inspector::cmd_shader)
42                 .set_help("Inspect shader object state",
43                         "shader\n"
44                         "  List shader objects\n\n"
45                         "shader ID\n"
46                         "  Print information about a shader object\n");
47
48         cmd_interp.register_command("program", this, &Inspector::cmd_program)
49                 .set_help("Inspect program object state",
50                         "program\n"
51                         "  List program objects\n\n"
52                         "program ID\n"
53                         "  Print information about a program object\n");
54
55         decoder->gldBreak = gldBreak;
56 }
57
58 void Inspector::decode(const char *data, unsigned len)
59 {
60         if(query_state)
61                 gldecoder_decode(decoder, data, len);
62         state.decode(data, len);
63 }
64
65 void Inspector::process_started()
66 {
67         gldbg.set_breakpoint(FUNC_GLXMAKECURRENT, BREAK_RETURN, this);
68         query_state = 1;
69 }
70
71 void Inspector::process_stopped(int reason)
72 {
73         if((reason>>8)==3 && query_state==2)
74         {
75                 GlPacket *pkt = packet_begin(FUNC_GLDQUERYLIMITS);
76                 gldbg.send(pkt);
77                 query_state = 0;
78                 gldbg.resume_from_break(this);
79         }
80 }
81
82 void Inspector::gldBreak(void *user_data, unsigned short func, unsigned char flag)
83 {
84         if(func==FUNC_GLXMAKECURRENT && flag==BREAK_RETURN)
85                 ++reinterpret_cast<Inspector *>(user_data)->query_state;
86 }
87
88 void Inspector::print_indented(const string &str, unsigned indent)
89 {
90         string spaces(indent, ' ');
91         string::size_type start = 0;
92         while(1)
93         {
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)
99                         break;
100                 start = newline+1;
101         }
102 }
103
104 void Inspector::cmd_state(const string &args)
105 {
106         const GlState &glstate = state;
107         if(args=="vertex")
108         {
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)
114                 {
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);
117                 }
118                 const Vector3 &normal = glstate.get_normal();
119                 printf("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
120         }
121         else if(args=="bind")
122         {
123                 printf("Current bindings:\n");
124                 unsigned count = glstate.get_max_texture_units();
125                 for(unsigned i=0; i<count; ++i)
126                 {
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());
133                 }
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)
143                 {
144                         const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
145                         if(binding.buffer)
146                                 printf("      %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
147                 }
148         }
149         else
150                 throw runtime_error("Invalid or missing argument");
151 }
152
153 void Inspector::cmd_texture(const string &args)
154 {
155         if(args.empty())
156         {
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)
160                 {
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());
164                 }
165         }
166         else
167         {
168                 char *end = 0;
169                 unsigned id = strtoul(args.c_str(), &end, 0);
170                 if(end && *end)
171                         throw runtime_error("Invalid texture id");
172
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)
178                 {
179                         string descr = tex.images[i].describe();
180                         printf("    Level %2d:      %s\n", i, descr.c_str());
181                 }
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"));
189         }
190 }
191
192 void Inspector::cmd_buffer(const string &args)
193 {
194         if(args.empty())
195         {
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)
199                 {
200                         string descr = i->second.describe();
201                         printf("  %d: %s\n", i->first, descr.c_str());
202                 }
203         }
204         else
205         {
206                 char *end = 0;
207                 unsigned id = strtoul(args.c_str(), &end, 0);
208                 if(end && *end)
209                         throw runtime_error("Invalid buffer id");
210
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)
216                 {
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"));
220
221                         printf("  Data:\n");
222                         unsigned width = 1+buf.content.stride*2;
223                         char fmt[6];
224                         snprintf(fmt, sizeof(fmt), " %%%du", width);
225                         unsigned n_elems = buf.size/buf.content.stride;
226                         string line;
227                         const char *ptr = buf.data;
228                         for(unsigned i=0; i<n_elems; ++i)
229                         {
230                                 if(line.empty())
231                                         line = "   ";
232
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));
239
240                                 if(line.size()+1+width>79)
241                                 {
242                                         printf("%s\n", line.c_str());
243                                         line.clear();
244                                 }
245                         }
246
247                         if(!line.empty())
248                                 printf("%s\n", line.c_str());
249                 }
250                 else if(buf.content.stride)
251                 {
252                         printf("  Stride: %d bytes\n", buf.content.stride);
253
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)
257                         {
258                                 if(i->kind)
259                                         printf("    %2d: %s, %d %s\n", i->offset,
260                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
261                                 else
262                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
263                                                 i->index, i->size, describe_enum(i->type, "DataType"));
264                         }
265
266                         printf("  Data:\n");
267                         string header;
268                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
269                         {
270                                 if(!header.empty())
271                                         header += " | ";
272
273                                 string label;
274                                 if(i->kind==GL_VERTEX_ARRAY)
275                                         label = "Vertex";
276                                 else if(i->kind==GL_NORMAL_ARRAY)
277                                         label = "Normal";
278                                 else if(i->kind==GL_COLOR_ARRAY)
279                                         label = "Color";
280                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
281                                 {
282                                         if(i->size==1)
283                                                 label = "TexC";
284                                         else
285                                                 label = "TexCoord";
286                                 }
287                                 else if(!i->kind)
288                                 {
289                                         if(i->size==1)
290                                                 label = strformat("A %d", i->index);
291                                         else
292                                                 label = strformat("Attrib %d", i->index);
293                                 }
294
295                                 unsigned width = i->size;
296                                 if(i->type==GL_FLOAT)
297                                         width *= 5;
298                                 else if(i->type==GL_UNSIGNED_BYTE)
299                                         width *= 3;
300                                 width += i->size-1;
301
302                                 header.append((width-label.size())/2, ' ');
303                                 header += label;
304                                 header.append((width-label.size()+1)/2, ' ');
305                         }
306                         printf("     %s\n", header.c_str());
307
308                         unsigned n_verts = buf.size/buf.content.stride;
309                         for(unsigned i=0; i<n_verts; ++i)
310                         {
311                                 const char *vertex = buf.data+i*buf.content.stride;
312
313                                 string line;
314                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
315                                 {
316                                         if(!line.empty())
317                                                 line += " |";
318
319                                         const char *base = vertex+j->offset;
320                                         for(unsigned k=0; k<j->size; ++k)
321                                         {
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));
326                                         }
327                                 }
328
329                                 printf("%3d:%s\n", i, line.c_str());
330                         }
331                 }
332         }
333 }
334
335 void Inspector::cmd_shader(const string &args)
336 {
337         if(args.empty())
338         {
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)
342                 {
343                         string descr = i->second.describe();
344                         printf("  %d: %s\n", i->first, descr.c_str());
345                 }
346         }
347         else
348         {
349                 char *end = 0;
350                 unsigned id = strtoul(args.c_str(), &end, 0);
351                 if(end && *end)
352                         throw runtime_error("Invalid shader id");
353
354                 const ShaderState &shader = state.get_shader(id);
355                 printf("Shader %d:\n", shader.id);
356                 printf("  Type: %s\n", describe_enum(shader.type, ""));
357                 unsigned n = 0;
358                 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
359                 {
360                         printf("  Source string %d:\n", n);
361                         print_indented(*i, 4);
362                 }
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");
368                 else
369                 {
370                         printf("  Info log:\n");
371                         print_indented(shader.info_log, 4);
372                 }
373                 if(shader.pending_delete)
374                         printf("  Pending deletion\n");
375         }
376 }
377
378 void Inspector::cmd_program(const std::string &args)
379 {
380         if(args.empty())
381         {
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)
385                 {
386                         string descr = i->second.describe();
387                         printf("  %d: %s\n", i->first, descr.c_str());
388                 }
389         }
390         else
391         {
392                 char *end = 0;
393                 unsigned id = strtoul(args.c_str(), &end, 0);
394                 if(end && *end)
395                         throw runtime_error("Invalid program id");
396
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)
401                 {
402                         string descr = (*i)->describe();
403                         printf("    %d: %s\n", (*i)->id, descr.c_str());
404                 }
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");
410                 else
411                 {
412                         printf("  Info log:\n");
413                         print_indented(program.info_log, 4);
414                 }
415         }
416 }