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

Last change on this file since 1788 was 1788, checked in by sam, 8 years ago

gpu: document and tweak some class declarations.

File size: 7.4 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
39/* A safe enum to indicate how a vertex stream is going to be used. For
40 * now there is only TexCoord and not TexCoord0 TexCoord1 etc. because
41 * we can always reorganise the vertex declaration for the indices to
42 * match. If the need arises these enums will be added. */
43struct VertexUsage
44{
45    enum Value
46    {
47        Position = 0,
48        BlendWeight,
49        BlendIndices,
50        Normal,
51        PointSize,
52        TexCoord,
53        Tangent,
54        Binormal,
55        TessFactor,
56        PositionT,
57        Color,
58        Fog,
59        Depth,
60        Sample,
61    }
62    m_value;
63
64    inline VertexUsage(Value v) : m_value(v) {}
65    inline operator Value() { return m_value; }
66};
67
68/* A safe enum to indicate what kind of primitive to draw. Used in
69 * VertexDeclaration::DrawElements() for instance. */
70struct MeshPrimitive
71{
72    enum Value
73    {
74        Triangles,
75        Points,
76    }
77    m_value;
78
79    inline MeshPrimitive(Value v) : m_value(v) {}
80    inline operator Value() { return m_value; }
81};
82
83class VertexStreamBase
84{
85    friend class VertexDeclaration;
86
87protected:
88    enum
89    {
90        Typevoid = 0,
91        Typehalf,     Typef16vec2, Typef16vec3, Typef16vec4,
92        Typefloat,    Typevec2,    Typevec3,    Typevec4,
93        Typedouble,   Typedvec2,   Typedvec3,   Typedvec4,
94        Typeint8_t,   Typei8vec2,  Typei8vec3,  Typei8vec4,
95        Typeuint8_t,  Typeu8vec2,  Typeu8vec3,  Typeu8vec4,
96        Typeint16_t,  Typei16vec2, Typei16vec3, Typei16vec4,
97        Typeuint16_t, Typeu16vec2, Typeu16vec3, Typeu16vec4,
98        Typeint32_t,  Typeivec2,   Typeivec3,   Typeivec4,
99        Typeuint32_t, Typeuvec2,   Typeuvec3,   Typeuvec4,
100    };
101
102#define LOL_TYPE(T) \
103    static uint8_t GetType(T *x) { (void)x; return Type##T; }
104
105    LOL_TYPE(void)
106    LOL_TYPE(half)     LOL_TYPE(f16vec2) LOL_TYPE(f16vec3) LOL_TYPE(f16vec4)
107    LOL_TYPE(float)    LOL_TYPE(vec2)    LOL_TYPE(vec3)    LOL_TYPE(vec4)
108    LOL_TYPE(double)   LOL_TYPE(dvec2)   LOL_TYPE(dvec3)   LOL_TYPE(dvec4)
109    LOL_TYPE(int8_t)   LOL_TYPE(i8vec2)  LOL_TYPE(i8vec3)  LOL_TYPE(i8vec4)
110    LOL_TYPE(uint8_t)  LOL_TYPE(u8vec2)  LOL_TYPE(u8vec3)  LOL_TYPE(u8vec4)
111    LOL_TYPE(int16_t)  LOL_TYPE(i16vec2) LOL_TYPE(i16vec3) LOL_TYPE(i16vec4)
112    LOL_TYPE(uint16_t) LOL_TYPE(u16vec2) LOL_TYPE(u16vec3) LOL_TYPE(u16vec4)
113    LOL_TYPE(int32_t)  LOL_TYPE(ivec2)   LOL_TYPE(ivec3)   LOL_TYPE(ivec4)
114    LOL_TYPE(uint32_t) LOL_TYPE(uvec2)   LOL_TYPE(uvec3)   LOL_TYPE(uvec4)
115#undef LOL_TYPE
116
117    template<typename T> inline void AddStream(int n, VertexUsage usage)
118    {
119        m_streams[n].stream_type = GetType((T *)NULL);
120        m_streams[n].usage = usage;
121        m_streams[n].size = sizeof(T);
122    }
123
124    VertexStreamBase() {}
125
126private:
127    struct { uint8_t stream_type, usage, size; } m_streams[12 + 1];
128
129    static VertexStreamBase const Empty;
130};
131
132/* Specialise this template for "void" to act as a NOP */
133template<>
134inline void VertexStreamBase::AddStream<void>(int n, VertexUsage usage)
135{
136    (void)usage;
137    m_streams[n].size = 0;
138}
139
140template<typename T1 = void,  typename T2 = void,  typename T3 = void,
141         typename T4 = void,  typename T5 = void,  typename T6 = void,
142         typename T7 = void,  typename T8 = void,  typename T9 = void,
143         typename T10 = void, typename T11 = void, typename T12 = void>
144class VertexStream : public VertexStreamBase
145{
146public:
147    inline VertexStream(VertexUsage u1,
148                        VertexUsage u2 = VertexUsage::Position,
149                        VertexUsage u3 = VertexUsage::Position,
150                        VertexUsage u4 = VertexUsage::Position,
151                        VertexUsage u5 = VertexUsage::Position,
152                        VertexUsage u6 = VertexUsage::Position,
153                        VertexUsage u7 = VertexUsage::Position,
154                        VertexUsage u8 = VertexUsage::Position,
155                        VertexUsage u9 = VertexUsage::Position,
156                        VertexUsage u10 = VertexUsage::Position,
157                        VertexUsage u11 = VertexUsage::Position,
158                        VertexUsage u12 = VertexUsage::Position)
159    {
160        AddStream<T1>(0, u1);    AddStream<T2>(1, u2);
161        AddStream<T3>(2, u3);    AddStream<T4>(3, u4);
162        AddStream<T5>(4, u5);    AddStream<T6>(5, u6);
163        AddStream<T7>(6, u7);    AddStream<T8>(7, u8);
164        AddStream<T9>(8, u9);    AddStream<T10>(9, u10);
165        AddStream<T11>(10, u11); AddStream<T12>(11, u12);
166    }
167};
168
169class VertexDeclaration
170{
171public:
172    VertexDeclaration(VertexStreamBase const &s1,
173                      VertexStreamBase const &s2 = VertexStreamBase::Empty,
174                      VertexStreamBase const &s3 = VertexStreamBase::Empty,
175                      VertexStreamBase const &s4 = VertexStreamBase::Empty,
176                      VertexStreamBase const &s5 = VertexStreamBase::Empty,
177                      VertexStreamBase const &s6 = VertexStreamBase::Empty,
178                      VertexStreamBase const &s7 = VertexStreamBase::Empty,
179                      VertexStreamBase const &s8 = VertexStreamBase::Empty,
180                      VertexStreamBase const &s9 = VertexStreamBase::Empty,
181                      VertexStreamBase const &s10 = VertexStreamBase::Empty,
182                      VertexStreamBase const &s11 = VertexStreamBase::Empty,
183                      VertexStreamBase const &s12 = VertexStreamBase::Empty);
184    ~VertexDeclaration();
185
186    void Bind();
187    void DrawElements(MeshPrimitive type, int skip, int count);
188    void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip,
189                             int vcount, int skip, int count);
190    void Unbind();
191    void SetStream(VertexBuffer *vb, ShaderAttrib attr1,
192                                     ShaderAttrib attr2 = ShaderAttrib(),
193                                     ShaderAttrib attr3 = ShaderAttrib(),
194                                     ShaderAttrib attr4 = ShaderAttrib(),
195                                     ShaderAttrib attr5 = ShaderAttrib(),
196                                     ShaderAttrib attr6 = ShaderAttrib(),
197                                     ShaderAttrib attr7 = ShaderAttrib(),
198                                     ShaderAttrib attr8 = ShaderAttrib(),
199                                     ShaderAttrib attr9 = ShaderAttrib(),
200                                     ShaderAttrib attr10 = ShaderAttrib(),
201                                     ShaderAttrib attr11 = ShaderAttrib(),
202                                     ShaderAttrib attr12 = ShaderAttrib());
203
204private:
205    void Initialize();
206    void AddStream(VertexStreamBase const &);
207
208    struct { uint8_t stream_type, index, usage, size; int reg; } m_streams[12 + 1];
209    int m_count;
210
211    void *m_data;
212};
213
214} /* namespace lol */
215
216#endif // __LOL_VERTEXBUFFER_H__
217
Note: See TracBrowser for help on using the repository browser.