From 76324b7394d25ba654e938eb33ee985532df7cc6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 19 Dec 2010 08:55:09 +0000 Subject: [PATCH] Decode and display element buffer contents --- flavors/gl/source/bufferstate.cpp | 41 +++++++++++++++++++++++++++++-- flavors/gl/source/bufferstate.h | 2 ++ flavors/gl/source/glstate.cpp | 16 ++++++++++++ flavors/gl/source/glstate.h | 3 +++ flavors/gl/source/inspector.cpp | 36 ++++++++++++++++++++++++++- 5 files changed, 95 insertions(+), 3 deletions(-) diff --git a/flavors/gl/source/bufferstate.cpp b/flavors/gl/source/bufferstate.cpp index 356e82b..c8b9f13 100644 --- a/flavors/gl/source/bufferstate.cpp +++ b/flavors/gl/source/bufferstate.cpp @@ -6,6 +6,7 @@ Distributed under the GPL */ #include +#include "arraysize.h" #include "arraystate.h" #include "bufferstate.h" #include "enums.h" @@ -51,6 +52,27 @@ void BufferContent::update(const ArrayState &array) arrays.insert(place, array); } +void BufferContent::update_elements(GLenum type) +{ + if(arrays.empty()) + { + Array array; + array.kind = GL_ELEMENT_ARRAY_BUFFER; + array.type = type; + arrays.push_back(array); + stride = typesize(type); + } + else if(arrays.size()>1 || arrays.front().kind!=GL_ELEMENT_ARRAY_BUFFER) + consistent = false; + else + { + if(arrays.front().type!=type) + consistent = false; + arrays.front().type = type; + stride = typesize(type); + } +} + string BufferContent::describe() const { if(arrays.empty()) @@ -68,6 +90,8 @@ string BufferContent::describe() const kind = 'C'; else if(i->kind==GL_TEXTURE_COORD_ARRAY && i->index==0) kind = 'T'; + else if(i->kind==GL_ELEMENT_ARRAY_BUFFER) + kind = 'E'; char type[3] = { '?', 0, 0 }; if(i->type==GL_FLOAT) @@ -96,6 +120,14 @@ string BufferContent::describe() const } +BufferContent::Array::Array(): + kind(GL_NONE), + index(0), + size(1), + type(GL_NONE), + offset(0) +{ } + BufferContent::Array::Array(const ArrayState &a): kind(a.kind), index(a.index), @@ -106,6 +138,8 @@ BufferContent::Array::Array(const ArrayState &a): BufferState::BufferState(): + id(0), + target(0), usage(GL_STATIC_DRAW), size(0), data(0) @@ -134,8 +168,11 @@ void BufferState::set_sub_data(unsigned off, unsigned sz, const void *ptr) string BufferState::describe() const { if(content.stride) - return format("%s, %d vertices (%d bytes), %s", - content.describe(), size/content.stride, size, describe_enum(usage, "")); + { + const char *what = (content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER ? "indices" : "vertices"); + return format("%s, %d %s (%d bytes), %s", + content.describe(), size/content.stride, what, size, describe_enum(usage, "")); + } else return format("%d bytes, %s", size, describe_enum(usage, "")); } diff --git a/flavors/gl/source/bufferstate.h b/flavors/gl/source/bufferstate.h index c3f504b..5557a56 100644 --- a/flavors/gl/source/bufferstate.h +++ b/flavors/gl/source/bufferstate.h @@ -24,6 +24,7 @@ struct BufferContent GLenum type; int offset; + Array(); Array(const ArrayState &); }; @@ -33,6 +34,7 @@ struct BufferContent BufferContent(); void update(const ArrayState &); + void update_elements(GLenum); std::string describe() const; }; diff --git a/flavors/gl/source/glstate.cpp b/flavors/gl/source/glstate.cpp index 1459324..e33e490 100644 --- a/flavors/gl/source/glstate.cpp +++ b/flavors/gl/source/glstate.cpp @@ -127,6 +127,10 @@ GlState::GlState(): decoder->glBufferSubDataARB = glBufferSubData; decoder->glDeleteBuffers = glDeleteBuffers; decoder->glDeleteBuffersARB = glDeleteBuffers; + + decoder->glDrawElements = glDrawElements; + decoder->glDrawRangeElements = glDrawRangeElements; + decoder->glDrawRangeElementsEXT = glDrawRangeElements; } GlState::~GlState() @@ -412,3 +416,15 @@ void GlState::glDeleteBuffers(void *user_data, int count, const unsigned *ids) for(int i=0; i(user_data)->buffers.erase(ids[i]); } + +void GlState::glDrawElements(void *user_data, GLenum, int, GLenum type, const void *) +{ + if(BufferState *buf = reinterpret_cast(user_data)->element_buffer) + buf->content.update_elements(type); +} + +void GlState::glDrawRangeElements(void *user_data, GLenum, unsigned, unsigned, int, GLenum type, const void *) +{ + if(BufferState *buf = reinterpret_cast(user_data)->element_buffer) + buf->content.update_elements(type); +} diff --git a/flavors/gl/source/glstate.h b/flavors/gl/source/glstate.h index 86cc538..43e8f39 100644 --- a/flavors/gl/source/glstate.h +++ b/flavors/gl/source/glstate.h @@ -119,6 +119,9 @@ private: static void glBufferData(void *, GLenum, int, const void *, GLenum); static void glBufferSubData(void *, GLenum, int, int, const void *); static void glDeleteBuffers(void *, int, const unsigned *); + + static void glDrawElements(void *, GLenum, int, GLenum, const void *); + static void glDrawRangeElements(void *, GLenum, unsigned, unsigned, int, GLenum, const void *); }; #endif diff --git a/flavors/gl/source/inspector.cpp b/flavors/gl/source/inspector.cpp index 912214d..87adf17 100644 --- a/flavors/gl/source/inspector.cpp +++ b/flavors/gl/source/inspector.cpp @@ -130,7 +130,41 @@ void Inspector::cmd_buffer(const string &args) IO::print("Buffer %d:\n", id); IO::print(" Size: %d bytes\n", buf.size); IO::print(" Usage: %s\n", describe_enum(buf.usage, "")); - if(buf.content.stride) + if(buf.content.arrays.size()==1 && buf.content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER) + { + const BufferContent::Array &array = buf.content.arrays.front(); + IO::print(" Arrays:\n"); + IO::print(" 0: Element indices, 1 %s\n", describe_enum(array.type, "DataType")); + + IO::print(" Data:\n"); + unsigned width = 1+buf.content.stride*2; + string fmt = format(" %%%du", width); + unsigned n_elems = buf.size/buf.content.stride; + string line; + const char *ptr = buf.data; + for(unsigned i=0; i(ptr)+i)); + else if(array.type==GL_UNSIGNED_SHORT) + line += format(fmt, *(reinterpret_cast(ptr)+i)); + else if(array.type==GL_UNSIGNED_INT) + line += format(fmt, *(reinterpret_cast(ptr)+i)); + + if(line.size()+1+width>79) + { + IO::print("%s\n", line); + line.clear(); + } + } + + if(!line.empty()) + IO::print("%s\n", line); + } + else if(buf.content.stride) { IO::print(" Stride: %d bytes\n", buf.content.stride); -- 2.43.0