source: trunk/src/gpu/vertexbuffer.h @ 1228

Last change on this file since 1228 was 1228, checked in by sam, 10 years ago

gpu: port the vertex buffer abstraction layer to OpenGL.

File size: 6.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The VertexBuffer and VertexDeclaration classes
13// ----------------------------------------------
14//
15
16#if !defined __LOL_VERTEXBUFFER_H__
17#define __LOL_VERTEXBUFFER_H__
18
19#include <cstring>
20
21namespace lol
22{
23
24class VertexBuffer
25{
26    friend class VertexDeclaration;
27
28public:
29    VertexBuffer(size_t size);
30    ~VertexBuffer();
31
32    void *Lock(size_t offset, size_t size);
33    void Unlock();
34
35private:
36    class VertexBufferData *m_data;
37};
38
39struct VertexUsage
40{
41    enum Value
42    {
43        Position = 0,
44        BlendWeight,
45        BlendIndices,
46        Normal,
47        PSize,
48        TexCoord,
49        Tangent,
50        Binormal,
51        TessFactor,
52        PositionT,
53        Color,
54        Fog,
55        Depth,
56        Sample,
57    }
58    m_value;
59
60    inline VertexUsage(Value v) { m_value = v; }
61    inline operator Value() { return m_value; }
62};
63
64class VertexStreamBase
65{
66    friend class VertexDeclaration;
67
68protected:
69    enum
70    {
71        Typevoid = 0,
72        Typehalf,     Typef16vec2, Typef16vec3, Typef16vec4,
73        Typefloat,    Typevec2,    Typevec3,    Typevec4,
74        Typedouble,   Typef64vec2, Typef64vec3, Typef64vec4,
75        Typeuint8_t,  Typeu8vec2,  Typeu8vec3,  Typeu8vec4,
76        Typeint8_t,   Typei8vec2,  Typei8vec3,  Typei8vec4,
77        Typeuint16_t, Typeu16vec2, Typeu16vec3, Typeu16vec4,
78        Typeint16_t,  Typei16vec2, Typei16vec3, Typei16vec4,
79        Typeuint32_t, Typeuvec2,   Typeuvec3,   Typeuvec4,
80        Typeint32_t,  Typeivec2,   Typeivec3,   Typeivec4,
81    };
82
83#define LOL_TYPE(T) \
84    static uint8_t GetType(T *x) { (void)x; return Type##T; }
85
86    LOL_TYPE(void)
87    LOL_TYPE(half)     LOL_TYPE(f16vec2) LOL_TYPE(f16vec3) LOL_TYPE(f16vec4)
88    LOL_TYPE(float)    LOL_TYPE(vec2)    LOL_TYPE(vec3)    LOL_TYPE(vec4)
89    LOL_TYPE(double)   LOL_TYPE(f64vec2) LOL_TYPE(f64vec3) LOL_TYPE(f64vec4)
90    LOL_TYPE(uint8_t)  LOL_TYPE(u8vec2)  LOL_TYPE(u8vec3)  LOL_TYPE(u8vec4)
91    LOL_TYPE(int8_t)   LOL_TYPE(i8vec2)  LOL_TYPE(i8vec3)  LOL_TYPE(i8vec4)
92    LOL_TYPE(uint16_t) LOL_TYPE(u16vec2) LOL_TYPE(u16vec3) LOL_TYPE(u16vec4)
93    LOL_TYPE(int16_t)  LOL_TYPE(i16vec2) LOL_TYPE(i16vec3) LOL_TYPE(i16vec4)
94    LOL_TYPE(uint32_t) LOL_TYPE(uvec2)   LOL_TYPE(uvec3)   LOL_TYPE(uvec4)
95    LOL_TYPE(int32_t)  LOL_TYPE(ivec2)   LOL_TYPE(ivec3)   LOL_TYPE(ivec4)
96#undef LOL_TYPE
97
98    template<typename T> inline void AddStream(int n, VertexUsage usage)
99    {
100        m_streams[n].stream_type = GetType((T *)NULL);
101        m_streams[n].usage = usage;
102        m_streams[n].size = sizeof(T);
103    }
104
105    VertexStreamBase() {}
106
107private:
108    struct { uint8_t stream_type, usage, size; } m_streams[12 + 1];
109
110    static VertexStreamBase const Empty;
111};
112
113template<>
114inline void VertexStreamBase::AddStream<void>(int n, VertexUsage usage)
115{
116    (void)usage;
117    m_streams[n].size = 0;
118}
119
120template<typename T1 = void,  typename T2 = void,  typename T3 = void,
121         typename T4 = void,  typename T5 = void,  typename T6 = void,
122         typename T7 = void,  typename T8 = void,  typename T9 = void,
123         typename T10 = void, typename T11 = void, typename T12 = void>
124class VertexStream : public VertexStreamBase
125{
126public:
127    inline VertexStream(VertexUsage u1,
128                        VertexUsage u2 = VertexUsage::Position,
129                        VertexUsage u3 = VertexUsage::Position,
130                        VertexUsage u4 = VertexUsage::Position,
131                        VertexUsage u5 = VertexUsage::Position,
132                        VertexUsage u6 = VertexUsage::Position,
133                        VertexUsage u7 = VertexUsage::Position,
134                        VertexUsage u8 = VertexUsage::Position,
135                        VertexUsage u9 = VertexUsage::Position,
136                        VertexUsage u10 = VertexUsage::Position,
137                        VertexUsage u11 = VertexUsage::Position,
138                        VertexUsage u12 = VertexUsage::Position)
139    {
140        AddStream<T1>(0, u1);    AddStream<T2>(1, u2);
141        AddStream<T3>(2, u3);    AddStream<T4>(3, u4);
142        AddStream<T5>(4, u5);    AddStream<T6>(5, u6);
143        AddStream<T7>(6, u7);    AddStream<T8>(7, u8);
144        AddStream<T9>(8, u9);    AddStream<T10>(9, u10);
145        AddStream<T11>(10, u11); AddStream<T12>(11, u12);
146    }
147};
148
149class VertexDeclaration
150{
151public:
152    VertexDeclaration(VertexStreamBase const &s1,
153                      VertexStreamBase const &s2 = VertexStreamBase::Empty,
154                      VertexStreamBase const &s3 = VertexStreamBase::Empty,
155                      VertexStreamBase const &s4 = VertexStreamBase::Empty,
156                      VertexStreamBase const &s5 = VertexStreamBase::Empty,
157                      VertexStreamBase const &s6 = VertexStreamBase::Empty,
158                      VertexStreamBase const &s7 = VertexStreamBase::Empty,
159                      VertexStreamBase const &s8 = VertexStreamBase::Empty,
160                      VertexStreamBase const &s9 = VertexStreamBase::Empty,
161                      VertexStreamBase const &s10 = VertexStreamBase::Empty,
162                      VertexStreamBase const &s11 = VertexStreamBase::Empty,
163                      VertexStreamBase const &s12 = VertexStreamBase::Empty);
164    ~VertexDeclaration();
165
166    void Bind();
167    void Unbind();
168    void SetStream(VertexBuffer *vb, ShaderAttrib attr1,
169                                     ShaderAttrib attr2 = ShaderAttrib(),
170                                     ShaderAttrib attr3 = ShaderAttrib(),
171                                     ShaderAttrib attr4 = ShaderAttrib(),
172                                     ShaderAttrib attr5 = ShaderAttrib(),
173                                     ShaderAttrib attr6 = ShaderAttrib(),
174                                     ShaderAttrib attr7 = ShaderAttrib(),
175                                     ShaderAttrib attr8 = ShaderAttrib(),
176                                     ShaderAttrib attr9 = ShaderAttrib(),
177                                     ShaderAttrib attr10 = ShaderAttrib(),
178                                     ShaderAttrib attr11 = ShaderAttrib(),
179                                     ShaderAttrib attr12 = ShaderAttrib());
180
181private:
182    void Initialize();
183    void AddStream(VertexStreamBase const &);
184
185    struct { uint8_t stream_type, index, usage, size; } m_streams[12 + 1];
186    int m_count;
187
188    void *m_data;
189};
190
191} /* namespace lol */
192
193#endif // __LOL_VERTEXBUFFER_H__
194
Note: See TracBrowser for help on using the repository browser.