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