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

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

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