]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
f7d90f846802c72f97e63b1b6198d53127b70fd9
[gldbg.git] / flavors / gl / source / inspector.cpp
1 #include <stdexcept>
2 #include <cstdio>
3 #include <cstdlib>
4 #include "enums.h"
5 #include "gldbg.h"
6 #include "inspector.h"
7 #include "strformat.h"
8
9 using namespace std;
10
11 Inspector::Inspector(GlDbg &dbg)
12 {
13         CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
14
15         cmd_interp.register_command("state", this, &Inspector::cmd_state)
16                 .set_help("Inspects general GL state",
17                         "state vertex\n"
18                         "  Print current vertex attributes\n\n"
19                         "state bind\n"
20                         "  Show current bindings\n");
21
22         cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
23                 .set_help("Inspect texture state",
24                         "texture\n"
25                         "  Lists texture objects\n\n"
26                         "texture ID\n"
27                         "  Print information about a texture object\n");
28
29         cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
30                 .set_help("Inspect buffer object state",
31                         "buffer\n"
32                         "  Lists buffer objects\n\n"
33                         "buffer ID\n"
34                         "  Print information about a buffer object\n");
35
36         cmd_interp.register_command("shader", this, &Inspector::cmd_shader)
37                 .set_help("Inspect shader object state",
38                         "shader\n"
39                         "  List shader objects\n\n"
40                         "shader ID\n"
41                         "  Print information about a shader object\n");
42
43         cmd_interp.register_command("program", this, &Inspector::cmd_program)
44                 .set_help("Inspect program object state",
45                         "program\n"
46                         "  List program objects\n\n"
47                         "program ID\n"
48                         "  Print information about a program object\n");
49 }
50
51 void Inspector::decode(const char *data, unsigned len)
52 {
53         state.decode(data, len);
54 }
55
56 void Inspector::print_indented(const string &str, unsigned indent)
57 {
58         string spaces(indent, ' ');
59         string::size_type start = 0;
60         while(1)
61         {
62                 string::size_type newline = str.find('\n', start);
63                 string line = str.substr(start, newline-start);
64                 if(newline!=string::npos || !line.empty())
65                         printf("%s%s\n", spaces.c_str(), line.c_str());
66                 if(newline==string::npos)
67                         break;
68                 start = newline+1;
69         }
70 }
71
72 void Inspector::cmd_state(const string &args)
73 {
74         const GlState &glstate = state;
75         if(args=="vertex")
76         {
77                 printf("Current vertex attributes:\n");
78                 const Vector4 &color = glstate.get_color();
79                 printf("  Color:     [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
80                 for(unsigned i=0; i<8; ++i)
81                 {
82                         const Vector4 &texcoord = glstate.get_texcoord(i);
83                         printf("  TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
84                 }
85                 const Vector3 &normal = glstate.get_normal();
86                 printf("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
87         }
88         else if(args=="bind")
89         {
90                 printf("Current bindings:\n");
91                 for(unsigned i=0; i<8; ++i)
92                 {
93                         printf("  Texture unit %d:\n", i);
94                         const TexUnitState &unit = glstate.get_texture_unit(i);
95                         string descr = unit.describe_binding(GL_TEXTURE_2D);
96                         printf("    GL_TEXTURE_2D: %s\n", descr.c_str());
97                         descr = unit.describe_binding(GL_TEXTURE_3D);
98                         printf("    GL_TEXTURE_3D: %s\n", descr.c_str());
99                 }
100                 printf("  Buffers:\n");
101                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
102                 printf("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
103                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
104                 printf("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
105                 buf = glstate.get_current_buffer(GL_UNIFORM_BUFFER);
106                 printf("    GL_UNIFORM_BUFFER:       %d\n", (buf ? buf->id : 0));
107                 for(unsigned i=0; i<64; ++i)
108                 {
109                         const BufferBindingState &binding = glstate.get_buffer_binding(GL_UNIFORM_BUFFER, i);
110                         if(binding.buffer)
111                                 printf("      %d: %d (%d bytes at %d)\n", i, binding.buffer->id, binding.size, binding.offset);
112                 }
113         }
114         else
115                 throw runtime_error("Invalid or missing argument");
116 }
117
118 void Inspector::cmd_texture(const string &args)
119 {
120         if(args.empty())
121         {
122                 const map<unsigned, TextureState> &textures = state.get_textures();
123                 printf("%d texture objects:\n", textures.size());
124                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
125                 {
126                         const TextureState &tex = i->second;
127                         string descr = tex.describe();
128                         printf("  %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
129                 }
130         }
131         else
132         {
133                 char *end = 0;
134                 unsigned id = strtoul(args.c_str(), &end, 0);
135                 if(end && *end)
136                         throw runtime_error("Invalid texture id");
137
138                 const TextureState &tex = state.get_texture(id);
139                 printf("Texture object %d\n", id);
140                 printf("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
141                 printf("  Images:\n");
142                 for(unsigned i=0; i<tex.images.size(); ++i)
143                 {
144                         string descr = tex.images[i].describe();
145                         printf("    Level %2d:      %s\n", i, descr.c_str());
146                 }
147                 printf("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
148                 printf("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
149                 printf("  Wrap modes:      S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
150                         describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
151                 printf("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
152                 printf("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
153                 printf("  Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
154         }
155 }
156
157 void Inspector::cmd_buffer(const string &args)
158 {
159         if(args.empty())
160         {
161                 const GlState::BufferMap &buffers = state.get_buffers();
162                 printf("%d buffers:\n", buffers.size());
163                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
164                 {
165                         string descr = i->second.describe();
166                         printf("  %d: %s\n", i->first, descr.c_str());
167                 }
168         }
169         else
170         {
171                 char *end = 0;
172                 unsigned id = strtoul(args.c_str(), &end, 0);
173                 if(end && *end)
174                         throw runtime_error("Invalid buffer id");
175
176                 const BufferState &buf = state.get_buffer(id);
177                 printf("Buffer %d:\n", id);
178                 printf("  Size:  %d bytes\n", buf.size);
179                 printf("  Usage: %s\n", describe_enum(buf.usage, ""));
180                 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
181                 {
182                         const BufferContent::Array &array = buf.content.arrays.front();
183                         printf("  Arrays:\n");
184                         printf("    0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
185
186                         printf("  Data:\n");
187                         unsigned width = 1+buf.content.stride*2;
188                         char fmt[6];
189                         snprintf(fmt, sizeof(fmt), " %%%du", width);
190                         unsigned n_elems = buf.size/buf.content.stride;
191                         string line;
192                         const char *ptr = buf.data;
193                         for(unsigned i=0; i<n_elems; ++i)
194                         {
195                                 if(line.empty())
196                                         line = "   ";
197
198                                 if(array.type==GL_UNSIGNED_BYTE)
199                                         line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
200                                 else if(array.type==GL_UNSIGNED_SHORT)
201                                         line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
202                                 else if(array.type==GL_UNSIGNED_INT)
203                                         line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
204
205                                 if(line.size()+1+width>79)
206                                 {
207                                         printf("%s\n", line.c_str());
208                                         line.clear();
209                                 }
210                         }
211
212                         if(!line.empty())
213                                 printf("%s\n", line.c_str());
214                 }
215                 else if(buf.content.stride)
216                 {
217                         printf("  Stride: %d bytes\n", buf.content.stride);
218
219                         printf("  Arrays:\n");
220                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
221                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
222                         {
223                                 if(i->kind)
224                                         printf("    %2d: %s, %d %s\n", i->offset,
225                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
226                                 else
227                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
228                                                 i->index, i->size, describe_enum(i->type, "DataType"));
229                         }
230
231                         printf("  Data:\n");
232                         string header;
233                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
234                         {
235                                 if(!header.empty())
236                                         header += " | ";
237
238                                 string label;
239                                 if(i->kind==GL_VERTEX_ARRAY)
240                                         label = "Vertex";
241                                 else if(i->kind==GL_NORMAL_ARRAY)
242                                         label = "Normal";
243                                 else if(i->kind==GL_COLOR_ARRAY)
244                                         label = "Color";
245                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
246                                 {
247                                         if(i->size==1)
248                                                 label = "TexC";
249                                         else
250                                                 label = "TexCoord";
251                                 }
252                                 else if(!i->kind)
253                                 {
254                                         if(i->size==1)
255                                                 label = strformat("A %d", i->index);
256                                         else
257                                                 label = strformat("Attrib %d", i->index);
258                                 }
259
260                                 unsigned width = i->size;
261                                 if(i->type==GL_FLOAT)
262                                         width *= 5;
263                                 else if(i->type==GL_UNSIGNED_BYTE)
264                                         width *= 3;
265                                 width += i->size-1;
266
267                                 header.append((width-label.size())/2, ' ');
268                                 header += label;
269                                 header.append((width-label.size()+1)/2, ' ');
270                         }
271                         printf("     %s\n", header.c_str());
272
273                         unsigned n_verts = buf.size/buf.content.stride;
274                         for(unsigned i=0; i<n_verts; ++i)
275                         {
276                                 const char *vertex = buf.data+i*buf.content.stride;
277
278                                 string line;
279                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
280                                 {
281                                         if(!line.empty())
282                                                 line += " |";
283
284                                         const char *base = vertex+j->offset;
285                                         for(unsigned k=0; k<j->size; ++k)
286                                         {
287                                                 if(j->type==GL_FLOAT)
288                                                         line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
289                                                 else if(j->type==GL_UNSIGNED_BYTE)
290                                                         line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
291                                         }
292                                 }
293
294                                 printf("%3d:%s\n", i, line.c_str());
295                         }
296                 }
297         }
298 }
299
300 void Inspector::cmd_shader(const string &args)
301 {
302         if(args.empty())
303         {
304                 const GlState::ShaderMap &shaders = state.get_shaders();
305                 printf("%d shader objects:\n", shaders.size());
306                 for(GlState::ShaderMap::const_iterator i=shaders.begin(); i!=shaders.end(); ++i)
307                 {
308                         string descr = i->second.describe();
309                         printf("  %d: %s\n", i->first, descr.c_str());
310                 }
311         }
312         else
313         {
314                 char *end = 0;
315                 unsigned id = strtoul(args.c_str(), &end, 0);
316                 if(end && *end)
317                         throw runtime_error("Invalid shader id");
318
319                 const ShaderState &shader = state.get_shader(id);
320                 printf("Shader %d:\n", shader.id);
321                 printf("  Type: %s\n", describe_enum(shader.type, ""));
322                 unsigned n = 0;
323                 for(vector<string>::const_iterator i=shader.source.begin(); i!=shader.source.end(); ++i, ++n)
324                 {
325                         printf("  Source string %d:\n", n);
326                         print_indented(*i, 4);
327                 }
328                 if(shader.source_changed)
329                         printf("  Source changed since last compile\n");
330                 printf("  Compile status: %d\n", shader.compile_status);
331                 if(shader.info_log.empty())
332                         printf("  Info log is empty\n");
333                 else
334                 {
335                         printf("  Info log:\n");
336                         print_indented(shader.info_log, 4);
337                 }
338                 if(shader.pending_delete)
339                         printf("  Pending deletion\n");
340         }
341 }
342
343 void Inspector::cmd_program(const std::string &args)
344 {
345         if(args.empty())
346         {
347                 const GlState::ProgramMap &programs = state.get_programs();
348                 printf("%d program objects:\n", programs.size());
349                 for(GlState::ProgramMap::const_iterator i=programs.begin(); i!=programs.end(); ++i)
350                 {
351                         string descr = i->second.describe();
352                         printf("  %d: %s\n", i->first, descr.c_str());
353                 }
354         }
355         else
356         {
357                 char *end = 0;
358                 unsigned id = strtoul(args.c_str(), &end, 0);
359                 if(end && *end)
360                         throw runtime_error("Invalid program id");
361
362                 const ProgramState &program = state.get_program(id);
363                 printf("Program %d:\n", program.id);
364                 printf("  Attached shaders:\n");
365                 for(vector<ShaderState *>::const_iterator i=program.shaders.begin(); i!=program.shaders.end(); ++i)
366                 {
367                         string descr = (*i)->describe();
368                         printf("    %d: %s\n", (*i)->id, descr.c_str());
369                 }
370                 if(program.shaders_changed)
371                         printf("  Shaders changed since last compile\n");
372                 printf("  Link status: %d\n", program.link_status);
373                 if(program.info_log.empty())
374                         printf("  Info log is empty\n");
375                 else
376                 {
377                         printf("  Info log:\n");
378                         print_indented(program.info_log, 4);
379                 }
380         }
381 }