]> git.tdb.fi Git - gldbg.git/blob - source/inspector.cpp
Add some virtual destructors to shut up older gcc versions
[gldbg.git] / source / inspector.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <msp/io/print.h>
9 #include "enums.h"
10 #include "gldbg.h"
11 #include "inspector.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 Inspector::Inspector(GlDbg &dbg)
17 {
18         CommandInterpreter &cmd_interp = dbg.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
42 void Inspector::decode(const char *data, unsigned len)
43 {
44         state.decode(data, len);
45 }
46
47 void Inspector::cmd_state(const string &args)
48 {
49         const GlState &glstate = state;
50         if(args=="vertex")
51         {
52                 IO::print("Current vertex attributes:\n");
53                 const Vector4 &color = glstate.get_color();
54                 IO::print("  Color:     [%05.3f, %05.3f, %05.3f, %05.3f]\n", color.r, color.g, color.b, color.a);
55                 for(unsigned i=0; i<8; ++i)
56                 {
57                         const Vector4 &texcoord = glstate.get_texcoord(i);
58                         IO::print("  TexCoord%d: [%05.3f, %05.3f, %05.3f, %05.3f]\n", i, texcoord.s, texcoord.t, texcoord.p, texcoord.q);
59                 }
60                 const Vector3 &normal = glstate.get_normal();
61                 IO::print("  Normal:    [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
62         }
63         else if(args=="bind")
64         {
65                 IO::print("Current bindings:\n");
66                 for(unsigned i=0; i<8; ++i)
67                 {
68                         IO::print("  Texture unit %d:\n", i);
69                         const TexUnitState &unit = glstate.get_texture_unit(i);
70                         IO::print("    GL_TEXTURE_2D: %s\n", unit.describe_binding(GL_TEXTURE_2D));
71                         IO::print("    GL_TEXTURE_3D: %s\n", unit.describe_binding(GL_TEXTURE_3D));
72                 }
73                 IO::print("  Buffers:\n");
74                 const BufferState *buf = glstate.get_current_buffer(GL_ARRAY_BUFFER);
75                 IO::print("    GL_ARRAY_BUFFER:         %d\n", (buf ? buf->id : 0));
76                 buf = glstate.get_current_buffer(GL_ELEMENT_ARRAY_BUFFER);
77                 IO::print("    GL_ELEMENT_ARRAY_BUFFER: %d\n", (buf ? buf->id : 0));
78         }
79         else
80                 throw InvalidParameterValue("Invalid or missing argument");
81 }
82
83 void Inspector::cmd_texture(const string &args)
84 {
85         if(args.empty())
86         {
87                 const map<unsigned, TextureState> &textures = state.get_textures();
88                 IO::print("%d texture objects:\n", textures.size());
89                 for(map<unsigned, TextureState>::const_iterator i = textures.begin(); i!=textures.end(); ++i)
90                 {
91                         const TextureState &tex = i->second;
92                         IO::print("  %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
93                 }
94         }
95         else
96         {
97                 unsigned id = lexical_cast<unsigned>(args);
98                 const TextureState &tex = state.get_texture(id);
99                 IO::print("Texture object %d\n", id);
100                 IO::print("  Target:          %s\n", describe_enum(tex.target, "TextureTarget"));
101                 IO::print("  Images:\n");
102                 for(unsigned i=0; i<tex.images.size(); ++i)
103                 {
104                         const TexImageState &img = tex.images[i];
105                         IO::print("    Level %2d:      %s\n", i, img.describe());
106                 }
107                 IO::print("  Min. filter:     %s\n", describe_enum(tex.min_filter, "TextureMinFilter"));
108                 IO::print("  Mag. filter:     %s\n", describe_enum(tex.mag_filter, "TextureMagFilter"));
109                 IO::print("  Wrap modes:      S=%s / T=%s / R=%s\n", describe_enum(tex.wrap_s, "TextureWrapMode"),
110                         describe_enum(tex.wrap_t, "TextureWrapMode"), describe_enum(tex.wrap_r, "TextureWrapMode"));
111                 IO::print("  Compare mode:    %s\n", describe_enum(tex.compare_mode, ""));
112                 IO::print("  Compare func:    %s\n", describe_enum(tex.compare_func, "DepthFunction"));
113                 IO::print("  Generate mipmap: %s\n", tex.generate_mipmap);
114         }
115 }
116
117 void Inspector::cmd_buffer(const string &args)
118 {
119         if(args.empty())
120         {
121                 const GlState::BufferMap &buffers = state.get_buffers();
122                 IO::print("%d buffers:\n", buffers.size());
123                 for(GlState::BufferMap::const_iterator i=buffers.begin(); i!=buffers.end(); ++i)
124                         IO::print("  %d: %s\n", i->first, i->second.describe());
125         }
126         else
127         {
128                 unsigned id = lexical_cast<unsigned>(args);
129                 const BufferState &buf = state.get_buffer(id);
130                 IO::print("Buffer %d:\n", id);
131                 IO::print("  Size:  %d bytes\n", buf.size);
132                 IO::print("  Usage: %s\n", describe_enum(buf.usage, ""));
133                 if(buf.content.stride)
134                 {
135                         IO::print("  Stride: %d bytes\n", buf.content.stride);
136
137                         IO::print("  Arrays:\n");
138                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
139                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
140                         {
141                                 if(i->kind)
142                                         IO::print("    %2d: %s, %d %s\n", i->offset,
143                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
144                                 else
145                                         IO::print("    %2d: Attrib %d, %d %s\n", i->offset,
146                                                 i->index, i->size, describe_enum(i->type, "DataType"));
147                         }
148
149                         IO::print("  Data:\n");
150                         string header;
151                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
152                         {
153                                 if(!header.empty())
154                                         header += " | ";
155
156                                 string label;
157                                 if(i->kind==GL_VERTEX_ARRAY)
158                                         label = "Vertex";
159                                 else if(i->kind==GL_NORMAL_ARRAY)
160                                         label = "Normal";
161                                 else if(i->kind==GL_COLOR_ARRAY)
162                                         label = "Color";
163                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
164                                 {
165                                         if(i->size==1)
166                                                 label = "TexC";
167                                         else
168                                                 label = "TexCoord";
169                                 }
170                                 else if(!i->kind)
171                                 {
172                                         if(i->size==1)
173                                                 label = format("A %d", i->index);
174                                         else
175                                                 label = format("Attrib %d", i->index);
176                                 }
177
178                                 unsigned width = i->size;
179                                 if(i->type==GL_FLOAT)
180                                         width *= 5;
181                                 else if(i->type==GL_UNSIGNED_BYTE)
182                                         width *= 3;
183                                 width += i->size-1;
184
185                                 header.append((width-label.size())/2, ' ');
186                                 header += label;
187                                 header.append((width-label.size()+1)/2, ' ');
188                         }
189                         IO::print("     %s\n", header);
190
191                         unsigned n_verts = buf.size/buf.content.stride;
192                         for(unsigned i=0; i<n_verts; ++i)
193                         {
194                                 const char *vertex = buf.data+i*buf.content.stride;
195
196                                 string line;
197                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
198                                 {
199                                         if(!line.empty())
200                                                 line += " |";
201
202                                         const char *base = vertex+j->offset;
203                                         for(unsigned k=0; k<j->size; ++k)
204                                         {
205                                                 if(j->type==GL_FLOAT)
206                                                         line += format(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
207                                                 else if(j->type==GL_UNSIGNED_BYTE)
208                                                         line += format(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
209                                         }
210                                 }
211
212                                 IO::print("%3d:%s\n", i, line);
213                         }
214                 }
215         }
216 }