]> git.tdb.fi Git - gldbg.git/blob - flavors/gl/source/inspector.cpp
Decode and display element buffer contents
[gldbg.git] / flavors / gl / 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.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER)
134                 {
135                         const BufferContent::Array &array = buf.content.arrays.front();
136                         IO::print("  Arrays:\n");
137                         IO::print("    0: Element indices, 1 %s\n", describe_enum(array.type, "DataType"));
138
139                         IO::print("  Data:\n");
140                         unsigned width = 1+buf.content.stride*2;
141                         string fmt = format(" %%%du", width);
142                         unsigned n_elems = buf.size/buf.content.stride;
143                         string line;
144                         const char *ptr = buf.data;
145                         for(unsigned i=0; i<n_elems; ++i)
146                         {
147                                 if(line.empty())
148                                         line = "   ";
149
150                                 if(array.type==GL_UNSIGNED_BYTE)
151                                         line += format(fmt, *(reinterpret_cast<unsigned char *>(ptr)+i));
152                                 else if(array.type==GL_UNSIGNED_SHORT)
153                                         line += format(fmt, *(reinterpret_cast<unsigned short *>(ptr)+i));
154                                 else if(array.type==GL_UNSIGNED_INT)
155                                         line += format(fmt, *(reinterpret_cast<unsigned *>(ptr)+i));
156
157                                 if(line.size()+1+width>79)
158                                 {
159                                         IO::print("%s\n", line);
160                                         line.clear();
161                                 }
162                         }
163
164                         if(!line.empty())
165                                 IO::print("%s\n", line);
166                 }
167                 else if(buf.content.stride)
168                 {
169                         IO::print("  Stride: %d bytes\n", buf.content.stride);
170
171                         IO::print("  Arrays:\n");
172                         const vector<BufferContent::Array> &arrays = buf.content.arrays;
173                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
174                         {
175                                 if(i->kind)
176                                         IO::print("    %2d: %s, %d %s\n", i->offset,
177                                                 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
178                                 else
179                                         IO::print("    %2d: Attrib %d, %d %s\n", i->offset,
180                                                 i->index, i->size, describe_enum(i->type, "DataType"));
181                         }
182
183                         IO::print("  Data:\n");
184                         string header;
185                         for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
186                         {
187                                 if(!header.empty())
188                                         header += " | ";
189
190                                 string label;
191                                 if(i->kind==GL_VERTEX_ARRAY)
192                                         label = "Vertex";
193                                 else if(i->kind==GL_NORMAL_ARRAY)
194                                         label = "Normal";
195                                 else if(i->kind==GL_COLOR_ARRAY)
196                                         label = "Color";
197                                 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
198                                 {
199                                         if(i->size==1)
200                                                 label = "TexC";
201                                         else
202                                                 label = "TexCoord";
203                                 }
204                                 else if(!i->kind)
205                                 {
206                                         if(i->size==1)
207                                                 label = format("A %d", i->index);
208                                         else
209                                                 label = format("Attrib %d", i->index);
210                                 }
211
212                                 unsigned width = i->size;
213                                 if(i->type==GL_FLOAT)
214                                         width *= 5;
215                                 else if(i->type==GL_UNSIGNED_BYTE)
216                                         width *= 3;
217                                 width += i->size-1;
218
219                                 header.append((width-label.size())/2, ' ');
220                                 header += label;
221                                 header.append((width-label.size()+1)/2, ' ');
222                         }
223                         IO::print("     %s\n", header);
224
225                         unsigned n_verts = buf.size/buf.content.stride;
226                         for(unsigned i=0; i<n_verts; ++i)
227                         {
228                                 const char *vertex = buf.data+i*buf.content.stride;
229
230                                 string line;
231                                 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
232                                 {
233                                         if(!line.empty())
234                                                 line += " |";
235
236                                         const char *base = vertex+j->offset;
237                                         for(unsigned k=0; k<j->size; ++k)
238                                         {
239                                                 if(j->type==GL_FLOAT)
240                                                         line += format(" %5.2f", *(reinterpret_cast<const float *>(base)+k));
241                                                 else if(j->type==GL_UNSIGNED_BYTE)
242                                                         line += format(" %3u", *(reinterpret_cast<const unsigned char *>(base)+k));
243                                         }
244                                 }
245
246                                 IO::print("%3d:%s\n", i, line);
247                         }
248                 }
249         }
250 }