]> git.tdb.fi Git - gldbg.git/blob - source/bufferstate.cpp
Track vertex array state
[gldbg.git] / source / bufferstate.cpp
1 /* $Id$
2
3 This file is part of gldbg
4 Copyright © 2009-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the GPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include "arraystate.h"
10 #include "bufferstate.h"
11 #include "enums.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 BufferContent::BufferContent():
17         consistent(true),
18         stride(0)
19 { }
20
21 void BufferContent::update(const ArrayState &array)
22 {
23         if(array.stride!=stride)
24                 consistent = false;
25
26         stride = array.stride;
27
28         for(vector<Array>::iterator i=arrays.begin(); i!=arrays.end(); ++i)
29                 if(i->kind==array.kind && i->index==array.index)
30                 {
31                         if(array.size!=i->size || array.type!=i->type || array.pointer!=i->offset)
32                         {
33                                 consistent = false;
34                                 arrays.erase(i);
35                         }
36                         else
37                                 return;
38
39
40                         break;
41                 }
42
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)
46                 {
47                         place = i;
48                         break;
49                 }
50
51         arrays.insert(place, array);
52 }
53
54 string BufferContent::describe() const
55 {
56         if(arrays.empty())
57                 return "(unknown)";
58
59         string result;
60         for(vector<Array>::const_iterator i=arrays.begin(); i!=arrays.end(); ++i)
61         {
62                 char kind = '?';
63                 if(i->kind==GL_VERTEX_ARRAY)
64                         kind = 'V';
65                 else if(i->kind==GL_NORMAL_ARRAY)
66                         kind = 'N';
67                 else if(i->kind==GL_COLOR_ARRAY)
68                         kind = 'C';
69                 else if(i->kind==GL_TEXTURE_COORD_ARRAY && i->index==0)
70                         kind = 'T';
71
72                 char type[3] = { '?', 0, 0 };
73                 if(i->type==GL_FLOAT)
74                         type[0] = 'F';
75                 else if(i->type==GL_DOUBLE)
76                         type[0] = 'D';
77                 else if(i->type==GL_INT || i->type==GL_UNSIGNED_INT)
78                         type[0] = 'I';
79                 else if(i->type==GL_SHORT || i->type==GL_UNSIGNED_SHORT)
80                         type[0] = 'S';
81                 else if(i->type==GL_BYTE || i->type==GL_UNSIGNED_BYTE)
82                         type[0] = 'B';
83
84                 if(i->type==GL_UNSIGNED_INT || i->type==GL_UNSIGNED_SHORT || i->type==GL_UNSIGNED_BYTE)
85                 {
86                         type[1] = type[0];
87                         type[0] = 'U';
88                 }
89
90                 if(!result.empty())
91                         result += '_';
92                 result += format("%c%d%s", kind, i->size, type);
93         }
94
95         return result;
96 }
97
98
99 BufferContent::Array::Array(const ArrayState &a):
100         kind(a.kind),
101         index(a.index),
102         size(a.size),
103         type(a.type),
104         offset(a.pointer)
105 { }
106
107
108 BufferState::BufferState():
109         usage(GL_STATIC_DRAW),
110         size(0),
111         data(0)
112 { }
113
114 void BufferState::set_data(unsigned sz, const void *ptr, GLenum use)
115 {
116         usage = use;
117         size = sz;
118         delete[] data;
119         data = new char[size];
120         if(ptr)
121                 set_sub_data(0, size, ptr);
122         content = BufferContent();
123 }
124
125 void BufferState::set_sub_data(unsigned off, unsigned sz, const void *ptr)
126 {
127         if(data && off+sz<=size)
128         {
129                 const char *cptr = reinterpret_cast<const char *>(ptr);
130                 copy(cptr, cptr+sz, data+off);
131         }
132 }
133
134 string BufferState::describe() const
135 {
136         if(content.stride)
137                 return format("%s, %d vertices (%d bytes), %s",
138                         content.describe(), size/content.stride, size, describe_enum(usage, ""));
139         else
140                 return format("%d bytes, %s", size, describe_enum(usage, ""));
141 }