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

Last change on this file since 1251 was 1251, checked in by sam, 9 years ago

gpu: fix a stupid signed/unsigned inconsistency causing vertex declaration
creation failures.

File size: 6.9 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        PointSize,
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
64struct MeshPrimitive
65{
66    enum Value
67    {
68        Triangles,
69    }
70    m_value;
71
72    inline MeshPrimitive(Value v) { m_value = v; }
73    inline operator Value() { return m_value; }
74};
75
76class VertexStreamBase
77{
78    friend class VertexDeclaration;
79
80protected:
81    enum
82    {
83        Typevoid = 0,
84        Typehalf,     Typef16vec2, Typef16vec3, Typef16vec4,
85        Typefloat,    Typevec2,    Typevec3,    Typevec4,
86        Typedouble,   Typef64vec2, Typef64vec3, Typef64vec4,
87        Typeint8_t,   Typei8vec2,  Typei8vec3,  Typei8vec4,
88        Typeuint8_t,  Typeu8vec2,  Typeu8vec3,  Typeu8vec4,
89        Typeint16_t,  Typei16vec2, Typei16vec3, Typei16vec4,
90        Typeuint16_t, Typeu16vec2, Typeu16vec3, Typeu16vec4,
91        Typeint32_t,  Typeivec2,   Typeivec3,   Typeivec4,
92        Typeuint32_t, Typeuvec2,   Typeuvec3,   Typeuvec4,
93    };
94
95#define LOL_TYPE(T) \
96    static uint8_t GetType(T *x) { (void)x; return Type##T; }
97
98    LOL_TYPE(void)
99    LOL_TYPE(half)     LOL_TYPE(f16vec2) LOL_TYPE(f16vec3) LOL_TYPE(f16vec4)
100    LOL_TYPE(float)    LOL_TYPE(vec2)    LOL_TYPE(vec3)    LOL_TYPE(vec4)
101    LOL_TYPE(double)   LOL_TYPE(f64vec2) LOL_TYPE(f64vec3) LOL_TYPE(f64vec4)
102    LOL_TYPE(uint8_t)  LOL_TYPE(u8vec2)  LOL_TYPE(u8vec3)  LOL_TYPE(u8vec4)
103    LOL_TYPE(int8_t)   LOL_TYPE(i8vec2)  LOL_TYPE(i8vec3)  LOL_TYPE(i8vec4)
104    LOL_TYPE(uint16_t) LOL_TYPE(u16vec2) LOL_TYPE(u16vec3) LOL_TYPE(u16vec4)
105    LOL_TYPE(int16_t)  LOL_TYPE(i16vec2) LOL_TYPE(i16vec3) LOL_TYPE(i16vec4)
106    LOL_TYPE(uint32_t) LOL_TYPE(uvec2)   LOL_TYPE(uvec3)   LOL_TYPE(uvec4)
107    LOL_TYPE(int32_t)  LOL_TYPE(ivec2)   LOL_TYPE(ivec3)   LOL_TYPE(ivec4)
108#undef LOL_TYPE
109
110    template<typename T> inline void AddStream(int n, VertexUsage usage)
111    {
112        m_streams[n].stream_type = GetType((T *)NULL);
113        m_streams[n].usage = usage;
114        m_streams[n].size = sizeof(T);
115    }
116
117    VertexStreamBase() {}
118
119private:
120    struct { uint8_t stream_type, usage, size; } m_streams[12 + 1];
121
122    static VertexStreamBase const Empty;
123};
124
125template<>
126inline void VertexStreamBase::AddStream<void>(int n, VertexUsage usage)
127{
128    (void)usage;
129    m_streams[n].size = 0;
130}
131
132template<typename T1 = void,  typename T2 = void,  typename T3 = void,
133         typename T4 = void,  typename T5 = void,  typename T6 = void,
134         typename T7 = void,  typename T8 = void,  typename T9 = void,
135         typename T10 = void, typename T11 = void, typename T12 = void>
136class VertexStream : public VertexStreamBase
137{
138public:
139    inline VertexStream(VertexUsage u1,
140                        VertexUsage u2 = VertexUsage::Position,
141                        VertexUsage u3 = VertexUsage::Position,
142                        VertexUsage u4 = VertexUsage::Position,
143                        VertexUsage u5 = VertexUsage::Position,
144                        VertexUsage u6 = VertexUsage::Position,
145                        VertexUsage u7 = VertexUsage::Position,
146                        VertexUsage u8 = VertexUsage::Position,
147                        VertexUsage u9 = VertexUsage::Position,
148                        VertexUsage u10 = VertexUsage::Position,
149                        VertexUsage u11 = VertexUsage::Position,
150                        VertexUsage u12 = VertexUsage::Position)
151    {
152        AddStream<T1>(0, u1);    AddStream<T2>(1, u2);
153        AddStream<T3>(2, u3);    AddStream<T4>(3, u4);
154        AddStream<T5>(4, u5);    AddStream<T6>(5, u6);
155        AddStream<T7>(6, u7);    AddStream<T8>(7, u8);
156        AddStream<T9>(8, u9);    AddStream<T10>(9, u10);
157        AddStream<T11>(10, u11); AddStream<T12>(11, u12);
158    }
159};
160
161class VertexDeclaration
162{
163public:
164    VertexDeclaration(VertexStreamBase const &s1,
165                      VertexStreamBase const &s2 = VertexStreamBase::Empty,
166                      VertexStreamBase const &s3 = VertexStreamBase::Empty,
167                      VertexStreamBase const &s4 = VertexStreamBase::Empty,
168                      VertexStreamBase const &s5 = VertexStreamBase::Empty,
169                      VertexStreamBase const &s6 = VertexStreamBase::Empty,
170                      VertexStreamBase const &s7 = VertexStreamBase::Empty,
171                      VertexStreamBase const &s8 = VertexStreamBase::Empty,
172                      VertexStreamBase const &s9 = VertexStreamBase::Empty,
173                      VertexStreamBase const &s10 = VertexStreamBase::Empty,
174                      VertexStreamBase const &s11 = VertexStreamBase::Empty,
175                      VertexStreamBase const &s12 = VertexStreamBase::Empty);
176    ~VertexDeclaration();
177
178    void Bind();
179    void DrawElements(MeshPrimitive type, int skip, int count);
180    void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip,
181                             int vcount, int skip, int count);
182    void Unbind();
183    void SetStream(VertexBuffer *vb, ShaderAttrib attr1,
184                                     ShaderAttrib attr2 = ShaderAttrib(),
185                                     ShaderAttrib attr3 = ShaderAttrib(),
186                                     ShaderAttrib attr4 = ShaderAttrib(),
187                                     ShaderAttrib attr5 = ShaderAttrib(),
188                                     ShaderAttrib attr6 = ShaderAttrib(),
189                                     ShaderAttrib attr7 = ShaderAttrib(),
190                                     ShaderAttrib attr8 = ShaderAttrib(),
191                                     ShaderAttrib attr9 = ShaderAttrib(),
192                                     ShaderAttrib attr10 = ShaderAttrib(),
193                                     ShaderAttrib attr11 = ShaderAttrib(),
194                                     ShaderAttrib attr12 = ShaderAttrib());
195
196private:
197    void Initialize();
198    void AddStream(VertexStreamBase const &);
199
200    struct { uint8_t stream_type, index, usage, size; } m_streams[12 + 1];
201    int m_count;
202
203    void *m_data;
204};
205
206} /* namespace lol */
207
208#endif // __LOL_VERTEXBUFFER_H__
209
Note: See TracBrowser for help on using the repository browser.