]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
bbdcfedbf1f6054b778bcaf6c37bb79efa54510e
[gldbg.git] / flavors / gl / source / inspector.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <stdexcept>
9 #include <cstdio>
10 #include <cstdlib>
11 #include "enums.h"
12 #include "gldbg.h"
13 #include "inspector.h"
14 #include "strformat.h"
15
16 using namespace std;
17
18 Inspector::Inspector(GlDbg &dbg)
19 {
20         CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
21
22         cmd_interp.register_command("state", this, &Inspector::cmd_state)
23                 .set_help("Inspects general GL state",
24                         "state vertex\n"
25                         "  Print current vertex attributes\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
44 void Inspector::decode(const char *data, unsigned len)
45 {
46         state.decode(data, len);
47 }
48
49 void Inspector::cmd_state(const string &args)
50 {
51         const GlState &glstate = state;
52         if(args=="vertex")
53         {
54                 printf("Current vertex attributes:\n");
55                 const Vector4 &color = glstate.get_color();
56                 printf("  Color:     [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
57                 for(unsigned i=0; i<8; ++i)
58                 {
59                         const Vector4 &texcoord = glstate.get_texcoord(i);
60                         printf("  TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
61                 }
62                 const Vector3 &normal = glstate.get_normal();
63                 printf("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
64         }
65         else if(args=="bind")
66         {
67                 printf("Current bindings:\n");
68                 for(unsigned i=0; i<8; ++i)
69                 {
70                         printf("  Texture unit %d:\n", i);
71                         const TexUnitState &unit = glstate.get_texture_unit(i);
72                         string descr = unit.describe_binding(GL_TEXTURE_2D);
73                         printf("    GL_TEXTURE_2D: %s\n", descr.c_str());
74                         descr = unit.describe_binding(GL_TEXTURE_3D);
75                         printf("    GL_TEXTURE_3D: %s\n", descr.c_str());
76                 }
77                 printf("  Buffers:\n");
78                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
79                 printf("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
80                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
81                 printf("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
82         }
83         else
84                 throw runtime_error("Invalid or missing argument");
85 }
86
87 void Inspector::cmd_texture(const string &args)
88 {
89         if(args.empty())
90         {
91                 const map<unsigned, TextureState> &textures = state.get_textures();
92                 printf("%d texture objects:\n", textures.size());
93                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
94                 {
95                         const TextureState &tex = i->second;
96                         string descr = tex.describe();
97                         printf("  %d: %s, %d images\n", i->first, descr.c_str(), tex.images.size());
98                 }
99         }
100         else
101         {
102                 char *end = 0;
103                 unsigned id = strtoul(args.c_str(), &end, 0);
104                 if(end && *end)
105                         throw runtime_error("Invalid texture id");
106
107                 const TextureState &tex = state.get_texture(id);
108                 printf("Texture object %d\n", id);
109                 printf("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
110                 printf("  Images:\n");
111                 for(unsigned i=0; i<tex.images.size(); ++i)
112                 {
113                         string descr = tex.images[i].describe();
114                         printf("    Level %2d:      %s\n", i, descr.c_str());
115                 }
116                 printf("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
117                 printf("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
118                 printf("  Wrap modes:      S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
119                         describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
120                 printf("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
121                 printf("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
122                 printf("  Generate mipmap: %s\n", (tex.generate_mipmap ? "true" : "false"));
123         }
124 }
125
126 void Inspector::cmd_buffer(const string &args)
127 {
128         if(args.empty())
129         {
130                 const GlState::BufferMap &buffers = state.get_buffers();
131                 printf("%d buffers:\n", buffers.size());
132                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
133                 {
134                         string descr = i->second.describe();
135                         printf("  %d: %s\n", i->first, descr.c_str());
136                 }
137         }
138         else
139         {
140                 char *end = 0;
141                 unsigned id = strtoul(args.c_str(), &end, 0);
142                 if(end && *end)
143                         throw runtime_error("Invalid buffer id");
144
145                 const BufferState &buf = state.get_buffer(id);
146                 printf("Buffer %d:\n", id);
147                 printf("  Size:  %d bytes\n", buf.size);
148                 printf("  Usage: %s\n", describe_enum(buf.usage, ""));
149                 if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
150                 {
151                         const BufferContent::Array &array = buf.content.arrays.front();
152                         printf("  Arrays:\n");
153                         printf("    0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
154
155                         printf("  Data:\n");
156                         unsigned width = 1+buf.content.stride*2;
157                         char fmt[6];
158                         snprintf(fmt, sizeof(fmt), " %%%du", width);
159                         unsigned n_elems = buf.size/buf.content.stride;
160                         string line;
161                         const char *ptr = buf.data;
162                         for(unsigned i=0; i<n_elems; ++i)
163                         {
164                                 if(line.empty())
165                                         line = "   ";
166
167                                 if(array.type==GL_UNSIGNED_BYTE)
168                                         line += strformat(fmt, *(reinterpret_cast<const unsigned char *>(ptr)+i));
169                                 else if(array.type==GL_UNSIGNED_SHORT)
170                                         line += strformat(fmt, *(reinterpret_cast<const unsigned short *>(ptr)+i));
171                                 else if(array.type==GL_UNSIGNED_INT)
172                                         line += strformat(fmt, *(reinterpret_cast<const unsigned *>(ptr)+i));
173
174                                 if(line.size()+1+width>79)
175                                 {
176                                         printf("%s\n", line.c_str());
177                                         line.clear();
178                                 }
179                         }
180
181                         if(!line.empty())
182                                 printf("%s\n", line.c_str());
183                 }
184                 else if(buf.content.stride)
185                 {
186                         printf("  Stride: %d bytes\n", buf.content.stride);
187
188                         printf("  Arrays:\n");
189                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
190                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
191                         {
192                                 if(i->kind)
193                                         printf("    %2d: %s, %d %s\n", i->offset,
194                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
195                                 else
196                                         printf("    %2d: Attrib %d, %d %s\n", i->offset,
197                                                 i->index, i->size, describe_enum(i->type, "DataType"));
198                         }
199
200                         printf("  Data:\n");
201                         string header;
202                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
203                         {
204                                 if(!header.empty())
205                                         header += " | ";
206
207                                 string label;
208                                 if(i->kind==GL_VERTEX_ARRAY)
209                                         label = "Vertex";
210                                 else if(i->kind==GL_NORMAL_ARRAY)
211                                         label = "Normal";
212                                 else if(i->kind==GL_COLOR_ARRAY)
213                                         label = "Color";
214                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
215                                 {
216                                         if(i->size==1)
217                                                 label = "TexC";
218                                         else
219                                                 label = "TexCoord";
220                                 }
221                                 else if(!i->kind)
222                                 {
223                                         if(i->size==1)
224                                                 label = strformat("A %d", i->index);
225                                         else
226                                                 label = strformat("Attrib %d", i->index);
227                                 }
228
229                                 unsigned width = i->size;
230                                 if(i->type==GL_FLOAT)
231                                         width *= 5;
232                                 else if(i->type==GL_UNSIGNED_BYTE)
233                                         width *= 3;
234                                 width += i->size-1;
235
236                                 header.append((width-label.size())/2, ' ');
237                                 header += label;
238                                 header.append((width-label.size()+1)/2, ' ');
239                         }
240                         printf("     %s\n", header.c_str());
241
242                         unsigned n_verts = buf.size/buf.content.stride;
243                         for(unsigned i=0; i<n_verts; ++i)
244                         {
245                                 const char *vertex = buf.data+i*buf.content.stride;
246
247                                 string line;
248                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
249                                 {
250                                         if(!line.empty())
251                                                 line += " |";
252
253                                         const char *base = vertex+j->offset;
254                                         for(unsigned k=0; k<j->size; ++k)
255                                         {
256                                                 if(j->type==GL_FLOAT)
257                                                         line += strformat(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
258                                                 else if(j->type==GL_UNSIGNED_BYTE)
259                                                         line += strformat(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
260                                         }
261                                 }
262
263                                 printf("%3d:%s\n", i, line.c_str());
264                         }
265                 }
266         }
267 }