source: trunk/src/lol/gpu/vertexbuffer.h @ 2426

Last change on this file since 2426 was 2426, checked in by touky, 7 years ago

easymesh : vertices datas are now stored in a very user-friendly struct. Heavier on datas, though.
vertexbuffer : added TexCoordExt, which is the same as TexCoord but means "use vec4 for TexCoord"

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