3 This file is part of gldbg
4 Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
9 #include "arraystate.h"
10 #include "bufferstate.h"
12 #include "strformat.h"
16 BufferContent::BufferContent():
21 void BufferContent::update(const ArrayState &array)
23 if(array.stride!=stride)
26 stride = array.stride;
28 for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
29 if(i->kind==array.kind && i->index==array.index)
31 if(array.size!=i->size || array.type!=i->type || array.pointer!=i->offset)
43 vector<Array>::iterator place = arrays.end();
44 for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
45 if(i->offset>array.pointer)
51 arrays.insert(place, array);
54 void BufferContent::update_elements(GLenum type)
59 array.kind = GL_ELEMENT_ARRAY_BUFFER;
61 arrays.push_back(array);
62 stride = typesize(type);
64 else if(arrays.size()>1 || arrays.front().kind!=GL_ELEMENT_ARRAY_BUFFER)
68 if(arrays.front().type!=type)
70 arrays.front().type = type;
71 stride = typesize(type);
75 string BufferContent::describe() const
81 for(vector<Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
84 if(i->kind==GL_VERTEX_ARRAY)
86 else if(i->kind==GL_NORMAL_ARRAY)
88 else if(i->kind==GL_COLOR_ARRAY)
90 else if(i->kind==GL_TEXTURE_COORD_ARRAY && i->index==0)
92 else if(i->kind==GL_ELEMENT_ARRAY_BUFFER)
95 char type[3] = { '?', 0, 0 };
98 else if(i->type==GL_DOUBLE)
100 else if(i->type==GL_INT || i->type==GL_UNSIGNED_INT)
102 else if(i->type==GL_SHORT || i->type==GL_UNSIGNED_SHORT)
104 else if(i->type==GL_BYTE || i->type==GL_UNSIGNED_BYTE)
107 if(i->type==GL_UNSIGNED_INT || i->type==GL_UNSIGNED_SHORT || i->type==GL_UNSIGNED_BYTE)
115 result += strformat("%c%d%s", kind, i->size, type);
122 BufferContent::Array::Array():
130 BufferContent::Array::Array(const ArrayState &a):
139 BufferState::BufferState():
142 usage(GL_STATIC_DRAW),
147 void BufferState::set_data(unsigned sz, const void *ptr, GLenum use)
152 data = new char[size];
154 set_sub_data(0, size, ptr);
155 content = BufferContent();
158 void BufferState::set_sub_data(unsigned off, unsigned sz, const void *ptr)
160 if(data && off+sz<=size)
162 const char *cptr = reinterpret_cast<const char *>(ptr);
163 copy(cptr, cptr+sz, data+off);
167 string BufferState::describe() const
171 const char *what = (content.arrays.front().kind==GL_ELEMENT_ARRAY_BUFFER ? "indices" : "vertices");
172 return content.describe()+strformat(", %d %s (%d bytes), %s",
173 size/content.stride, what, size, describe_enum(usage, ""));
176 return strformat("%d bytes, %s", size, describe_enum(usage, ""));