void PrimitiveBuilder::vertex_(const Vector4 &v)
{
const VertexFormat &format = array.get_format();
- for(const unsigned char *a=format.begin(); a!=format.end(); ++a)
+ for(const UInt16 *a=format.begin(); a!=format.end(); ++a)
{
unsigned sem = get_attribute_semantic(*a);
if(sem<attr.size())
#include "vertexarray.h"
#include "vertexarraybuilder.h"
+using namespace std;
+
namespace Msp {
namespace GL {
void VertexArrayBuilder::vertex_(const Vector4 &vtx)
{
- float *ptr = array.append();
+ char *ptr = array.append();
const VertexFormat &format = array.get_format();
- for(const unsigned char *a=format.begin(); a!=format.end(); ++a)
+ for(const UInt16 *a=format.begin(); a!=format.end(); ++a)
{
unsigned sem = get_attribute_semantic(*a);
- unsigned sz = get_attribute_size(*a);
- if(sem>=attr.size())
- ptr += sz;
- else if(*a==COLOR4_UBYTE)
+ DataType type = get_attribute_source_type(*a);
+ unsigned cc = get_attribute_component_count(*a);
+
+ if(sem<attr.size())
{
- union { unsigned char c[4]; float f; } u;
- u.c[0] = static_cast<unsigned char>(attr[sem].x*255);
- u.c[1] = static_cast<unsigned char>(attr[sem].y*255);
- u.c[2] = static_cast<unsigned char>(attr[sem].z*255);
- u.c[3] = static_cast<unsigned char>(attr[sem].w*255);
- *ptr++ = u.f;
+ const Vector4 &value = (sem==0 ? vtx : attr[sem]);
+ if(type==UNSIGNED_BYTE)
+ store_attribute<UInt8>(ptr, value, cc);
+ else if(type==BYTE)
+ store_attribute<Int8>(ptr, value, cc);
+ else if(type==UNSIGNED_SHORT)
+ store_attribute<UInt16>(ptr, value, cc);
+ else if(type==SHORT)
+ store_attribute<Int16>(ptr, value, cc);
+ else if(type==UNSIGNED_INT)
+ store_attribute<UInt32>(ptr, value, cc);
+ else if(type==INT)
+ store_attribute<Int32>(ptr, value, cc);
+ else if(type==FLOAT)
+ store_attribute<float>(ptr, value, cc);
}
+
+ ptr += get_attribute_size(*a);
+ }
+}
+
+template<typename T>
+void VertexArrayBuilder::store_attribute(char *ptr, const Vector4 &value, unsigned count)
+{
+ T *tptr = reinterpret_cast<T *>(ptr);
+ for(unsigned i=0; i<count; ++i)
+ {
+ if(!numeric_limits<T>::is_integer)
+ *tptr++ = value[i];
+ else if(numeric_limits<T>::is_signed)
+ *tptr++ = round(min(max(value[i], -1.0f), 1.0f)*numeric_limits<T>::max());
else
- {
- const Vector4 &v = (sem==0 ? vtx : attr[sem]);
- *ptr++ = v.x;
- if(sz>=2) *ptr++ = v.y;
- if(sz>=3) *ptr++ = v.z;
- if(sz>=4) *ptr++ = v.w;
- }
+ *tptr++ = round(min(max(value[i], 0.0f), 1.0f)*numeric_limits<T>::max());
}
}
private:
virtual void vertex_(const Vector4 &);
+
+ template<typename T>
+ void store_attribute(char *, const Vector4 &, unsigned);
};
} // namespace GL
{ color(Color(r, g, b, a)); }
void color(const Color &c)
- { attrib(get_attribute_semantic(COLOR4_FLOAT), Vector4(c.r, c.g, c.b, c.a)); }
+ { attrib(get_attribute_semantic(COLOR4), Vector4(c.r, c.g, c.b, c.a)); }
void group(int g0)
{ group(g0, 0, 0, 0); }
return vertices.size();
}
-float *Mesh::modify_vertex(unsigned i)
+char *Mesh::modify_vertex(unsigned i)
{
if(vertices.get_format().empty())
throw invalid_operation("Mesh::modify_vertex");
const VertexSetup &get_vertex_setup() const { return vtx_setup; }
const Buffer *get_index_buffer() const { return ibuf; }
unsigned get_n_vertices() const;
- float *modify_vertex(unsigned);
+ char *modify_vertex(unsigned);
void add_batch(const Batch &b);
const std::vector<Batch> &get_batches() const { return batches; }
data.reserve(n*stride);
}
-float *VertexArray::append()
+char *VertexArray::append()
{
if(format.empty())
throw invalid_operation("VertexArray::append");
return &*(data.end()-stride);
}
-float *VertexArray::modify(unsigned i)
+char *VertexArray::modify(unsigned i)
{
if(format.empty())
throw invalid_operation("VertexArray::modify");
unsigned VertexArray::get_data_size() const
{
- return data.size()*sizeof(float);
+ return data.size();
}
private:
VertexFormat format;
- std::vector<float> data;
+ std::vector<char> data;
unsigned stride;
VertexArray(const VertexArray &);
void reserve(unsigned);
/// Append a new vertex at the end of the array and return its location.
- float *append();
+ char *append();
/// Returns the location of a vertex for modification.
- float *modify(unsigned);
+ char *modify(unsigned);
private:
virtual unsigned get_data_size() const;
virtual const void *get_data_pointer() const { return &data[0]; }
public:
unsigned size() const { return data.size()/stride; }
- const std::vector<float> &get_data() const { return data; }
- const float *operator[](unsigned i) const { return &data[0]+i*stride; }
+ const std::vector<char> &get_data() const { return data; }
+ const char *operator[](unsigned i) const { return &data[0]+i*stride; }
};
} // namespace GL
return r;
}
+VertexFormat VertexFormat::operator,(DataType t) const
+{
+ if(!count)
+ throw invalid_operation("VertexFormat::operator,");
+
+ VertexFormat r = *this;
+ UInt16 &a = r.attributes[r.count-1];
+ a = make_typed_attribute(static_cast<VertexAttribute>(a), t);
+
+ return r;
+}
+
VertexFormat VertexFormat::operator,(unsigned i) const
{
if(!count)
throw invalid_operation("VertexFormat::operator,");
VertexFormat r = *this;
- unsigned char &a = r.attributes[r.count-1];
+ UInt16 &a = r.attributes[r.count-1];
a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
return r;
unsigned VertexFormat::stride() const
{
unsigned s = 0;
- for(const unsigned char *i=begin(); i!=end(); ++i)
+ for(const UInt16 *i=begin(); i!=end(); ++i)
s += get_attribute_size(*i);
return s;
}
int VertexFormat::offset(VertexAttribute attr) const
{
unsigned sem = get_attribute_semantic(attr);
- unsigned sz = get_attribute_size(attr);
unsigned offs = 0;
- for(const unsigned char *i=begin(); i!=end(); ++i)
+ for(const UInt16 *i=begin(); i!=end(); ++i)
{
if(get_attribute_semantic(*i)==sem)
{
- if(get_attribute_size(*i)>=sz)
+ if(get_attribute_source_type(*i)==get_attribute_source_type(attr) &&
+ get_attribute_component_count(*i)>=get_attribute_component_count(attr))
return offs;
else
return -1;
}
+VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
+{
+ if(is_matrix(type) || is_vector(type) || is_image(type))
+ throw invalid_argument("make_typed_attribute");
+
+ return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
+}
+
VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
{
unsigned base = attr;
- if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
+ if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
{
if(index>=4)
throw out_of_range("make_indexed_attribute");
}
- else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4)
- base &= 7;
- else if(attr<GENERIC1 || attr>GENERIC4)
+ else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
+ base &= 0x3FF;
+ else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
throw invalid_argument("make_indexed_attribute");
- if(static_cast<int>((base>>3)+index)>=31)
+ if(get_attribute_semantic(base)+index>=63)
throw out_of_range("make_indexed_attribute");
- return static_cast<VertexAttribute>(base+index*8);
+ return static_cast<VertexAttribute>(base+(index<<10));
}
+bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
+{
+ string::const_iterator j = i;
+ for(; (j!=end && *j!='_' && *name); ++name, ++j)
+ if(*j!=*name)
+ return false;
+
+ attr = make_typed_attribute(attr, type);
+ i = j;
+
+ return true;
+}
+
bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
{
string::const_iterator i = str.begin();
return false;
if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
return false;
- VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
+ VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i++-'0'-min_size));
- if(++i!=str.end())
+ while(i!=str.end())
{
if(*i!='_' || ++i==str.end())
return false;
- try
+
+ if(isdigit(*i))
{
- result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
+ unsigned index = 0;
+ for(; (i!=str.end() && isdigit(*i)); ++i)
+ index = index*10+(*i-'0');
+ result = make_indexed_attribute(result, index);
}
- catch(...)
- {
- // The operator>> will throw a more appropriate exception
+ else if(convert_attribute_type(i, str.end(), "UBYTE", result, UNSIGNED_BYTE) ||
+ convert_attribute_type(i, str.end(), "BYTE", result, BYTE) ||
+ convert_attribute_type(i, str.end(), "USHORT", result, UNSIGNED_SHORT) ||
+ convert_attribute_type(i, str.end(), "SHORT", result, SHORT) ||
+ convert_attribute_type(i, str.end(), "UINT", result, UNSIGNED_INT) ||
+ convert_attribute_type(i, str.end(), "INT", result, INT) ||
+ convert_attribute_type(i, str.end(), "FLOAT", result, FLOAT))
+ ;
+ else
return false;
- }
}
attr = result;
void operator>>(const LexicalConverter &conv, VertexAttribute &a)
{
const string &str = conv.get();
- if(str=="NORMAL3")
- a = NORMAL3;
- else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
- a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
- else if(str=="COLOR4_UBYTE")
- a = COLOR4_UBYTE;
- else if(str=="TANGENT3")
- a = TANGENT3;
- else if(str=="BINORMAL3")
+ try
{
- IO::print(IO::cerr, "BINORMAL3 attribute is deprecated\n");
- a = make_indexed_attribute(GENERIC3, 5);
+ if(convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) ||
+ convert_attribute(str, "COLOR", 3, 4, a, COLOR3) ||
+ convert_attribute(str, "NORMAL", 3, 3, a, NORMAL3) ||
+ convert_attribute(str, "TANGENT", 3, 3, a, TANGENT3) ||
+ convert_attribute(str, "GROUP", 1, 4, a, GROUP1) ||
+ convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) ||
+ convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) ||
+ convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
+ return;
}
- else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) &&
- !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) &&
- !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) &&
- !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) &&
- !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
- throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
+ catch(...)
+ { }
+
+ throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
}
} // namespace GL
#ifndef MSP_GL_VERTEXFORMAT_H_
#define MSP_GL_VERTEXFORMAT_H_
+#include <msp/core/inttypes.h>
#include <msp/strings/lexicalcast.h>
+#include "datatype.h"
namespace Msp {
namespace GL {
RAW_ATTRIB is handled in a special way; creating an indexed attribute based on
it uses the index as raw attribute number. Only use it if you know what you
-are doing. */
+are doing.
+
+The values are bitfields laid as follows:
+
+nnnn nn_f gsss _ccc
+ │ │ │ │ └╴Number of components
+ │ │ │ └─────╴Size of one component
+ │ │ └────────╴Signed flag
+ │ └──────────╴Floating-point flag
+ └────────────╴Attribute index (semantic)
+
+This information is presented for internal documentation purposes only; it is
+inadvisable for programs to rely on it.
+*/
enum VertexAttribute
{
- VERTEX2 = 1,
- VERTEX3,
- VERTEX4,
- COLOR4_UBYTE = 8,
- COLOR3_FLOAT = 10,
- COLOR4_FLOAT,
- NORMAL3 = 18,
- TANGENT3 = 26,
- GROUP1 = 32,
- GROUP2,
- GROUP3,
- GROUP4,
- WEIGHT1 = 40,
- WEIGHT2,
- WEIGHT3,
- WEIGHT4,
- TEXCOORD1 = 48,
- TEXCOORD2,
- TEXCOORD3,
- TEXCOORD4,
- GENERIC1 = 80,
- GENERIC2,
- GENERIC3,
- GENERIC4,
- RAW_ATTRIB1 = 248,
- RAW_ATTRIB2,
- RAW_ATTRIB3,
- RAW_ATTRIB4
+ VERTEX2 = 0x01C2,
+ VERTEX3 = 0x01C3,
+ VERTEX4 = 0x01C4,
+ COLOR3 = 0x05C3,
+ COLOR4 = 0x05C4,
+ NORMAL3 = 0x09C3,
+ TANGENT3 = 0x0DC3,
+ GROUP1 = 0x11C1,
+ GROUP2 = 0x11C2,
+ GROUP3 = 0x11C3,
+ GROUP4 = 0x11C4,
+ WEIGHT1 = 0x15C1,
+ WEIGHT2 = 0x15C2,
+ WEIGHT3 = 0x15C3,
+ WEIGHT4 = 0x15C4,
+ TEXCOORD1 = 0x19C1,
+ TEXCOORD2 = 0x19C2,
+ TEXCOORD3 = 0x19C3,
+ TEXCOORD4 = 0x19C4,
+ GENERIC1 = 0x29C1,
+ GENERIC2 = 0x29C2,
+ GENERIC3 = 0x29C3,
+ GENERIC4 = 0x29C4,
+ RAW_ATTRIB1 = 0xFDC1,
+ RAW_ATTRIB2 = 0xFDC2,
+ RAW_ATTRIB3 = 0xFDC3,
+ RAW_ATTRIB4 = 0xFDC4
};
class VertexFormat
private:
enum { MAX_ATTRIBUTES = 15 };
- unsigned char count;
- unsigned char attributes[MAX_ATTRIBUTES];
+ UInt8 count;
+ UInt16 attributes[MAX_ATTRIBUTES];
public:
VertexFormat();
VertexFormat(VertexAttribute);
VertexFormat operator,(VertexAttribute) const;
+ VertexFormat operator,(DataType) const;
VertexFormat operator,(unsigned) const;
bool operator==(const VertexFormat &) const;
bool operator!=(const VertexFormat &other) const { return !(*this==other); }
bool empty() const { return !count; }
- const unsigned char *begin() const { return attributes; }
- const unsigned char *end() const { return attributes+count; }
+ const UInt16 *begin() const { return attributes; }
+ const UInt16 *end() const { return attributes+count; }
unsigned stride() const;
int offset(VertexAttribute) const;
};
inline VertexFormat operator,(VertexAttribute a1, VertexAttribute a2)
{ return (VertexFormat(a1), a2); }
+VertexAttribute make_typed_attribute(VertexAttribute, DataType);
+
+inline VertexAttribute operator,(VertexAttribute a, DataType t)
+{ return make_typed_attribute(a, t); }
+
VertexAttribute make_indexed_attribute(VertexAttribute, unsigned);
inline VertexAttribute operator,(VertexAttribute a, unsigned i)
{ return make_indexed_attribute(a, i); }
-inline unsigned get_attribute_semantic(unsigned char a)
-{ return a>>3; }
+inline unsigned get_attribute_semantic(UInt16 a)
+{ return a>>10; }
+
+inline DataType get_attribute_source_type(UInt16 a)
+{ return static_cast<DataType>((a&0x70)>>4 | (a&0x180)<<1); }
+
+inline unsigned get_attribute_component_count(UInt16 a)
+{ return a&7; }
-inline unsigned get_attribute_size(unsigned char a)
-{ return (a&3)+1; }
+inline unsigned get_attribute_size(UInt16 a)
+{ return get_attribute_component_count(a)*get_type_size(get_attribute_source_type(a)); }
void operator>>(const LexicalConverter &, VertexAttribute &);
unsigned max_attribs = Limits::get_global().max_vertex_attributes;
- for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
+ for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
if(get_attribute_semantic(*a)>=max_attribs)
return false;
glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
const VertexFormat &fmt = array.get_format();
- unsigned stride = fmt.stride()*sizeof(float);
+ unsigned stride = fmt.stride();
if(direct)
{
glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
}
unsigned offset = 0;
- for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
+ for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
{
unsigned sem = get_attribute_semantic(*a);
- unsigned sz = get_attribute_size(*a);
+ GLenum type = get_gl_type(get_attribute_source_type(*a));
+ unsigned cc = get_attribute_component_count(*a);
if(direct)
{
- if(*a==COLOR4_UBYTE)
- glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
- else
- glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
+ glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
glVertexArrayAttribBinding(id, sem, binding);
glEnableVertexArrayAttrib(id, sem);
}
else
{
- if(*a==COLOR4_UBYTE)
- glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
- else
- glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
+ glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
if(ARB_instanced_arrays)
glVertexAttribDivisor(sem, divisor);
glEnableVertexAttribArray(sem);
}
- offset += sz*sizeof(float);
+ offset += get_attribute_size(*a);
}
if(!direct)
glBindVertexArray(id);
glBindBuffer(GL_ARRAY_BUFFER, 0);
- for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
+ for(const UInt16 *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
{
unsigned sem = get_attribute_semantic(*a);
glDisableVertexAttribArray(sem);
glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
}
- for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
+ for(const UInt16 *a=inst_format.begin(); a!=inst_format.end(); ++a)
{
unsigned sem = get_attribute_semantic(*a);
glDisableVertexAttribArray(sem);
const Matrix &m = *instances[index]->get_matrix();
- float *d = instance_data->modify(instances.size()-1);
+ float *d = reinterpret_cast<float *>(instance_data->modify(instances.size()-1)+matrix_offset);
for(unsigned i=0; i<12; ++i)
- d[matrix_offset+i] = m(i/4, i%4);
+ d[i] = m(i/4, i%4);
}
void InstanceArray::render(Renderer &renderer, Tag tag) const
points.reserve(points.size()+n_vertices);
for(unsigned j=0; j<n_vertices; ++j)
{
- const float *v = vertices[j];
- points.push_back(Vector3(v[offset], v[offset+1], (three ? v[offset+2] : 0.0f)));
+ const float *v = reinterpret_cast<const float *>(vertices[j]+offset);
+ points.push_back(Vector3(v[0], v[1], (three ? v[2] : 0.0f)));
}
}
unsigned n_vertices = mesh.get_n_vertices();
for(unsigned i=0; i<n_vertices; ++i)
{
- float *pos = mesh.modify_vertex(i)+pos_offset;
+ float *pos = reinterpret_cast<float *>(mesh.modify_vertex(i)+pos_offset);
pos[0] += horz_adjust;
pos[1] += vert_adjust;
}