3 This file is part of gldbg
4 Copyright © 2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
8 #include <msp/io/print.h>
11 #include "inspector.h"
16 Inspector::Inspector(GlDbg &dbg)
18 CommandInterpreter &cmd_interp = dbg.get_command_interpreter();
20 cmd_interp.register_command("state", this, &Inspector::cmd_state)
21 .set_help("Inspects general GL state",
23 " Print current vertex attributes\n\n"
25 " Show current bindings\n");
27 cmd_interp.register_command("texture", this, &Inspector::cmd_texture)
28 .set_help("Inspect texture state",
30 " Lists texture objects\n\n"
32 " Print information about a texture object\n");
34 cmd_interp.register_command("buffer", this, &Inspector::cmd_buffer)
35 .set_help("Inspect buffer object state",
37 " Lists buffer objects\n\n"
39 " Print information about a buffer object\n");
42 void Inspector::decode(const char *data, unsigned len)
44 state.decode(data, len);
47 void Inspector::cmd_state(const string &args)
49 const GlState &glstate = state;
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)
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);
60 const Vector3 &normal = glstate.get_normal();
61 IO::print(" Normal: [%05.3f, %05.3f, %05.3f]\n", normal.x, normal.y, normal.z);
65 IO::print("Current bindings:\n");
66 for(unsigned i=0; i<8; ++i)
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));
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));
80 throw InvalidParameterValue("Invalid or missing argument");
83 void Inspector::cmd_texture(const string &args)
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)
91 const TextureState &tex = i->second;
92 IO::print(" %d: %s, %d images\n", i->first, tex.describe(), tex.images.size());
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)
104 const TexImageState &img = tex.images[i];
105 IO::print(" Level %2d: %s\n", i, img.describe());
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);
117 void Inspector::cmd_buffer(const string &args)
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());
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)
135 IO::print(" Stride: %d bytes\n", buf.content.stride);
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)
142 IO::print(" %2d: %s, %d %s\n", i->offset,
143 describe_enum(i->kind, ""), i->size, describe_enum(i->type, "DataType"));
145 IO::print(" %2d: Attrib %d, %d %s\n", i->offset,
146 i->index, i->size, describe_enum(i->type, "DataType"));
149 IO::print(" Data:\n");
151 for(vector<BufferContent::Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
157 if(i->kind==GL_VERTEX_ARRAY)
159 else if(i->kind==GL_NORMAL_ARRAY)
161 else if(i->kind==GL_COLOR_ARRAY)
163 else if(i->kind==GL_TEXTURE_COORD_ARRAY)
173 label = format("A %d", i->index);
175 label = format("Attrib %d", i->index);
178 unsigned width = i->size;
179 if(i->type==GL_FLOAT)
181 else if(i->type==GL_UNSIGNED_BYTE)
185 header.append((width-label.size())/2, ' ');
187 header.append((width-label.size()+1)/2, ' ');
189 IO::print(" %s\n", header);
191 unsigned n_verts = buf.size/buf.content.stride;
192 for(unsigned i=0; i<n_verts; ++i)
194 const char *vertex = buf.data+i*buf.content.stride;
197 for(vector<BufferContent::Array>::const_iterator j=arrays.begin(); j!=arrays.end(); ++j)
202 const char *base = vertex+j->offset;
203 for(unsigned k=0; k<j->size; ++k)
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));
212 IO::print("%3d:%s\n", i, line);