source: trunk/src/gpu/vertexbuffer.cpp @ 1266

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

gpu: normalize 8-bit integer data in 0..1 by default.

File size: 14.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://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lolgl.h"
17
18#if defined _WIN32 && defined USE_D3D9
19#   define FAR
20#   define NEAR
21#   include <d3d9.h>
22#endif
23
24using namespace std;
25
26#if defined USE_D3D9
27extern IDirect3DDevice9 *g_d3ddevice;
28#elif defined _XBOX
29extern D3DDevice *g_d3ddevice;
30#endif
31
32namespace lol
33{
34
35//
36// The VertexBufferData class
37// --------------------------
38//
39
40class VertexBufferData
41{
42    friend class VertexBuffer;
43    friend class VertexDeclaration;
44
45#if defined USE_D3D9
46    IDirect3DVertexBuffer9 *m_vbo;
47#elif defined _XBOX
48    D3DVertexBuffer *m_vbo;
49#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
50    GLuint m_vbo;
51    uint8_t *m_memory;
52    size_t m_size;
53#endif
54};
55
56//
57// The VertexDeclaration class
58// ---------------------------
59//
60
61VertexStreamBase const VertexStreamBase::Empty;
62
63VertexDeclaration::VertexDeclaration(VertexStreamBase const &s1,
64                                     VertexStreamBase const &s2,
65                                     VertexStreamBase const &s3,
66                                     VertexStreamBase const &s4,
67                                     VertexStreamBase const &s5,
68                                     VertexStreamBase const &s6,
69                                     VertexStreamBase const &s7,
70                                     VertexStreamBase const &s8,
71                                     VertexStreamBase const &s9,
72                                     VertexStreamBase const &s10,
73                                     VertexStreamBase const &s11,
74                                     VertexStreamBase const &s12) : m_count(0)
75{
76    if (&s1 != &VertexStreamBase::Empty) AddStream(s1);
77    if (&s2 != &VertexStreamBase::Empty) AddStream(s2);
78    if (&s3 != &VertexStreamBase::Empty) AddStream(s3);
79    if (&s4 != &VertexStreamBase::Empty) AddStream(s4);
80    if (&s5 != &VertexStreamBase::Empty) AddStream(s5);
81    if (&s6 != &VertexStreamBase::Empty) AddStream(s6);
82    if (&s7 != &VertexStreamBase::Empty) AddStream(s7);
83    if (&s8 != &VertexStreamBase::Empty) AddStream(s8);
84    if (&s9 != &VertexStreamBase::Empty) AddStream(s9);
85    if (&s10 != &VertexStreamBase::Empty) AddStream(s10);
86    if (&s11 != &VertexStreamBase::Empty) AddStream(s11);
87    if (&s12 != &VertexStreamBase::Empty) AddStream(s12);
88    Initialize();
89}
90
91VertexDeclaration::~VertexDeclaration()
92{
93#if defined _XBOX || defined USE_D3D9
94#   if defined USE_D3D9
95    IDirect3DVertexDeclaration9 *vdecl = (IDirect3DVertexDeclaration9 *)m_data;
96#   elif defined _XBOX
97    D3DVertexDeclaration *vdecl = (D3DVertexDeclaration *)m_data;
98#   endif
99
100    if (FAILED(vdecl->Release()))
101        Abort();
102#else
103
104#endif
105}
106
107void VertexDeclaration::Bind()
108{
109#if defined _XBOX || defined USE_D3D9
110#   if defined USE_D3D9
111    IDirect3DVertexDeclaration9 *vdecl = (IDirect3DVertexDeclaration9 *)m_data;
112#   elif defined _XBOX
113    D3DVertexDeclaration *vdecl = (D3DVertexDeclaration *)m_data;
114#   endif
115
116    if (FAILED(g_d3ddevice->SetVertexDeclaration(vdecl)))
117        Abort();
118#else
119    /* FIXME: Nothing to do? */
120#endif
121}
122
123void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count)
124{
125#if defined _XBOX || defined USE_D3D9
126    if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
127        Abort();
128    switch (type)
129    {
130    case MeshPrimitive::Triangles:
131        if (FAILED(g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, skip, count)))
132            Abort();
133        break;
134    }
135#else
136    switch (type)
137    {
138    case MeshPrimitive::Triangles:
139        glDrawArrays(GL_TRIANGLES, skip * 3, count * 3);
140        break;
141    }
142#endif
143}
144
145void VertexDeclaration::DrawIndexedElements(MeshPrimitive type, int vbase,
146                                            int vskip, int vcount,
147                                            int skip, int count)
148{
149#if defined _XBOX || defined USE_D3D9
150    if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
151        Abort();
152    switch (type)
153    {
154    case MeshPrimitive::Triangles:
155        if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vbase, vskip, vcount, skip, count)))
156            Abort();
157        break;
158    }
159#else
160    switch (type)
161    {
162    case MeshPrimitive::Triangles:
163        /* FIXME: ignores most of the arguments! */
164        glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_SHORT, 0);
165        break;
166    }
167#endif
168}
169
170void VertexDeclaration::Unbind()
171{
172#if defined _XBOX || defined USE_D3D9
173    int stream = -1;
174    for (int i = 0; i < m_count; i++)
175        if (m_streams[i].index != stream)
176        {
177            stream = m_streams[i].index;
178            if (FAILED(g_d3ddevice->SetStreamSource(stream, 0, 0, 0)))
179                Abort();
180        }
181    if (FAILED(g_d3ddevice->SetVertexDeclaration(NULL)))
182        Abort();
183#else
184    /* FIXME: we need to unbind what we bound */
185    //glDisableVertexAttribArray(m_attrib);
186    /* FIXME: only useful for VAOs */
187    //glBindBuffer(GL_ARRAY_BUFFER, 0);
188    /* Or: */
189    //glDisableVertexAttribArray(m_attrib);
190    /* Or even: */
191    //glDisableClientState(GL_VERTEX_ARRAY);
192#endif
193}
194
195void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1,
196                                                    ShaderAttrib attr2,
197                                                    ShaderAttrib attr3,
198                                                    ShaderAttrib attr4,
199                                                    ShaderAttrib attr5,
200                                                    ShaderAttrib attr6,
201                                                    ShaderAttrib attr7,
202                                                    ShaderAttrib attr8,
203                                                    ShaderAttrib attr9,
204                                                    ShaderAttrib attr10,
205                                                    ShaderAttrib attr11,
206                                                    ShaderAttrib attr12)
207{
208#if defined _XBOX || defined USE_D3D9
209    /* Only the first item is required to know which stream this
210     * is about; the rest of the information is stored in the
211     * vertex declaration already. */
212    uint32_t usage = (attr1.m_flags >> 16) & 0xffff;
213    uint32_t index = attr1.m_flags & 0xffff;
214
215    /* Find the stream number */
216    int usage_index = 0, stream = -1;
217    for (int i = 0; i < m_count; i++)
218        if (m_streams[i].usage == usage)
219            if (usage_index++ == index)
220            {
221                stream = m_streams[i].index;
222                break;
223            }
224
225    /* Compute this stream's stride */
226    int stride = 0;
227    for (int i = 0; i < m_count; i++)
228        if (stream == m_streams[i].index)
229            stride += m_streams[i].size;
230
231    /* Now we know the stream index and the element stride */
232    /* FIXME: precompute most of the crap above! */
233    if (stream >= 0)
234    {
235        if (FAILED(g_d3ddevice->SetStreamSource(stream, vb->m_data->m_vbo, 0, stride)))
236            Abort();
237    }
238#else
239    glBindBuffer(GL_ARRAY_BUFFER, vb->m_data->m_vbo);
240    ShaderAttrib l[12] = { attr1, attr2, attr3, attr4, attr5, attr6,
241                           attr7, attr8, attr9, attr10, attr11, attr12 };
242    for (int n = 0; n < 12 && l[n].m_flags != (uint64_t)0 - 1; n++)
243    {
244        uint32_t reg = l[n].m_flags >> 32;
245        uint32_t usage = (l[n].m_flags >> 16) & 0xffff;
246        uint32_t index = l[n].m_flags & 0xffff;
247
248        glEnableVertexAttribArray((GLint)reg);
249
250        /* We need to parse the whole vertex declaration to retrieve
251         * the information. It sucks. */
252
253        int attr_index = 0, usage_index = 0;
254        /* First, find the stream index */
255        for (; attr_index < m_count; attr_index++)
256            if (m_streams[attr_index].usage == usage)
257                if (usage_index++ == index)
258                    break;
259
260        /* Now compute the stride and offset up to this stream index */
261        int stride = 0, offset = 0;
262        for (int i = 0; i < m_count; i++)
263            if (m_streams[i].index == m_streams[attr_index].index)
264            {
265                stride += m_streams[i].size;
266                if (i < attr_index)
267                    offset += m_streams[i].size;
268            }
269
270        /* Finally, we need to retrieve the type of the data */
271        static struct { GLint size; GLenum type; } const tlut[] =
272        {
273            { 0, 0 },
274            { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, /* half */
275            { 1, GL_FLOAT }, { 2, GL_FLOAT }, { 3, GL_FLOAT },
276                { 4, GL_FLOAT }, /* float */
277            { 1, GL_DOUBLE }, { 2, GL_DOUBLE }, { 3, GL_DOUBLE },
278                { 4, GL_DOUBLE }, /* double */
279            { 1, GL_BYTE }, { 2, GL_BYTE }, { 3, GL_BYTE },
280                { 4, GL_BYTE }, /* int8_t */
281            { 1, GL_UNSIGNED_BYTE }, { 2, GL_UNSIGNED_BYTE },
282                { 3, GL_UNSIGNED_BYTE }, { 4, GL_UNSIGNED_BYTE }, /* uint8_t */
283            { 1, GL_SHORT }, { 2, GL_SHORT }, { 3, GL_SHORT },
284                { 4, GL_SHORT }, /* int16_t */
285            { 1, GL_UNSIGNED_SHORT }, { 2, GL_UNSIGNED_SHORT }, { 3,
286                GL_UNSIGNED_SHORT }, { 4, GL_UNSIGNED_SHORT }, /* uint16_t */
287            { 1, GL_INT }, { 2, GL_INT }, { 3, GL_INT },
288                { 4, GL_INT }, /* int32_t */
289            { 1, GL_UNSIGNED_INT }, { 2, GL_UNSIGNED_INT },
290                { 3, GL_UNSIGNED_INT }, { 4, GL_UNSIGNED_INT }, /* uint32_t */
291        };
292
293        int type_index = m_streams[attr_index].stream_type;
294        if (type_index < 0 || type_index >= sizeof(tlut) / sizeof(*tlut))
295            type_index = 0;
296
297        /* Normalize unsigned bytes by default, because it's usually
298         * some color information. */
299        GLboolean normalize = (tlut[type_index].type == GL_UNSIGNED_BYTE)
300                           || (tlut[type_index].type == GL_BYTE);
301
302        glVertexAttribPointer((GLint)reg, tlut[type_index].size,
303                              tlut[type_index].type, normalize,
304                              stride, (GLvoid const *)(uintptr_t)offset);
305    }
306#endif
307}
308
309void VertexDeclaration::Initialize()
310{
311#if defined _XBOX || defined USE_D3D9
312    static D3DVERTEXELEMENT9 const end_element[] = { D3DDECL_END() };
313    static D3DDECLTYPE const X = D3DDECLTYPE_UNUSED;
314    static D3DDECLTYPE const tlut[] =
315    {
316        D3DDECLTYPE_UNUSED,
317        X, D3DDECLTYPE_FLOAT16_2, X, D3DDECLTYPE_FLOAT16_4, /* half */
318        D3DDECLTYPE_FLOAT1, D3DDECLTYPE_FLOAT2, D3DDECLTYPE_FLOAT3,
319            D3DDECLTYPE_FLOAT4, /* float */
320        X, X, X, X, /* double */
321        X, X, X, X, /* int8_t */
322        X, X, X, D3DDECLTYPE_UBYTE4N, /* uint8_t */
323        X, D3DDECLTYPE_SHORT2N, X, D3DDECLTYPE_SHORT4N, /* int16_t */
324        X, D3DDECLTYPE_USHORT2N, X, D3DDECLTYPE_USHORT4N, /* uint16_t */
325        X, X, X, X, /* int32_t */
326        X, X, X, X, /* uint32_t */
327    };
328    static D3DDECLUSAGE const ulut[] =
329    {
330        D3DDECLUSAGE_POSITION,
331        D3DDECLUSAGE_BLENDWEIGHT,
332        D3DDECLUSAGE_BLENDINDICES,
333        D3DDECLUSAGE_NORMAL,
334        D3DDECLUSAGE_PSIZE,
335        D3DDECLUSAGE_TEXCOORD,
336        D3DDECLUSAGE_TANGENT,
337        D3DDECLUSAGE_BINORMAL,
338        D3DDECLUSAGE_TESSFACTOR,
339#if defined _XBOX
340        D3DDECLUSAGE_TEXCOORD, /* FIXME: nonexistent */
341#else
342        D3DDECLUSAGE_POSITIONT,
343#endif
344        D3DDECLUSAGE_COLOR,
345        D3DDECLUSAGE_FOG,
346        D3DDECLUSAGE_DEPTH,
347        D3DDECLUSAGE_SAMPLE,
348    };
349
350    D3DVERTEXELEMENT9 elements[12 + 1];
351    for (int n = 0; n < m_count; n++)
352    {
353        elements[n].Stream = m_streams[n].index;
354        elements[n].Offset = 0;
355        for (int i = 0; i < n; i++)
356            if (m_streams[i].index == m_streams[n].index)
357                elements[n].Offset += m_streams[i].size;
358
359        if (m_streams[n].stream_type >= 0
360             && m_streams[n].stream_type < sizeof(tlut) / sizeof(*tlut))
361            elements[n].Type = tlut[m_streams[n].stream_type];
362        else
363            elements[n].Type = D3DDECLTYPE_UNUSED;
364
365        elements[n].Method = D3DDECLMETHOD_DEFAULT;
366
367        if (m_streams[n].usage >= 0
368             && m_streams[n].usage < sizeof(ulut) / sizeof(*ulut))
369            elements[n].Usage = ulut[m_streams[n].usage];
370        else
371            elements[n].Usage = D3DDECLUSAGE_POSITION;
372
373        elements[n].UsageIndex = 0;
374        for (int i = 0; i < n; i++)
375            if (elements[i].Stream == elements[n].Stream
376                 && elements[i].Usage == elements[n].Usage)
377                elements[n].UsageIndex++;
378    }
379    elements[m_count] = end_element[0];
380
381#   if defined USE_D3D9
382    IDirect3DVertexDeclaration9 *vdecl;
383#   elif defined _XBOX
384    D3DVertexDeclaration *vdecl;
385#   endif
386
387    if (FAILED(g_d3ddevice->CreateVertexDeclaration(elements, &vdecl)))
388        Abort();
389
390    m_data = vdecl;
391#else
392
393#endif
394}
395
396void VertexDeclaration::AddStream(VertexStreamBase const &s)
397{
398    int index = m_count ? m_streams[m_count - 1].index + 1 : 0;
399
400    for (int i = 0; s.m_streams[i].size; i++)
401    {
402        m_streams[m_count].stream_type = s.m_streams[i].stream_type;
403        m_streams[m_count].usage = s.m_streams[i].usage;
404        m_streams[m_count].size = s.m_streams[i].size;
405        m_streams[m_count].index = index;
406        m_count++;
407    }
408}
409
410//
411// The VertexBuffer class
412// ----------------------
413//
414
415VertexBuffer::VertexBuffer(size_t size)
416  : m_data(new VertexBufferData)
417{
418#if defined USE_D3D9 || defined _XBOX
419    if (FAILED(g_d3ddevice->CreateVertexBuffer(size, D3DUSAGE_WRITEONLY, NULL,
420                                               D3DPOOL_MANAGED, &m_data->m_vbo, NULL)))
421        Abort();
422#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
423    glGenBuffers(1, &m_data->m_vbo);
424    m_data->m_memory = new uint8_t[size];
425    m_data->m_size = size;
426#endif
427}
428
429VertexBuffer::~VertexBuffer()
430{
431#if defined USE_D3D9 || defined _XBOX
432    if (FAILED(m_data->m_vbo->Release()))
433        Abort();
434#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
435    glDeleteBuffers(1, &m_data->m_vbo);
436    delete[] m_data->m_memory;
437#endif
438}
439
440void *VertexBuffer::Lock(size_t offset, size_t size)
441{
442#if defined USE_D3D9 || defined _XBOX
443    void *ret;
444    if (FAILED(m_data->m_vbo->Lock(offset, size, (void **)&ret, 0)))
445        Abort();
446    return ret;
447#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
448    return m_data->m_memory + offset;
449#endif
450}
451
452void VertexBuffer::Unlock()
453{
454#if defined USE_D3D9 || defined _XBOX
455    if (FAILED(m_data->m_vbo->Unlock()))
456        Abort();
457#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
458    glBindBuffer(GL_ARRAY_BUFFER, m_data->m_vbo);
459    glBufferData(GL_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
460                 GL_STATIC_DRAW);
461#endif
462}
463
464} /* namespace lol */
465
Note: See TracBrowser for help on using the repository browser.