Просмотр исходного кода

gpu: abstraction class for index buffers; the cube tutorial no longer

needs to know about Direct3D headers etc.
legacy
Sam Hocevar sam 12 лет назад
Родитель
Сommit
578368f452
10 измененных файлов: 188 добавлений и 184 удалений
  1. +1
    -1
      src/Makefile.am
  2. +1
    -1
      src/core.h
  3. +134
    -0
      src/gpu/indexbuffer.cpp
  4. +14
    -16
      src/gpu/indexbuffer.h
  5. +0
    -100
      src/gpu/vbo.cpp
  6. +25
    -0
      src/gpu/vertexbuffer.cpp
  7. +2
    -0
      src/gpu/vertexbuffer.h
  8. +7
    -60
      test/tutorial/02_cube.cpp
  9. +0
    -2
      win32/lolcore.vcxproj
  10. +4
    -4
      win32/lolcore.vcxproj.filters

+ 1
- 1
src/Makefile.am Просмотреть файл

@@ -29,7 +29,7 @@ liblol_a_SOURCES = \
math/vector.cpp math/real.cpp math/half.cpp math/trig.cpp math/trig.h \
\
gpu/shader.cpp gpu/shader.h \
gpu/vbo.cpp gpu/vbo.h \
gpu/indexbuffer.cpp gpu/indexbuffer.h \
gpu/vertexbuffer.cpp gpu/vertexbuffer.h \
\
image/image.cpp image/image.h image/image-private.h \


+ 1
- 1
src/core.h Просмотреть файл

@@ -100,8 +100,8 @@ static inline int isnan(float f)
#include "map.h"
#include "layer.h"
#include "gpu/shader.h"
#include "gpu/indexbuffer.h"
#include "gpu/vertexbuffer.h"
#include "gpu/vbo.h"
#include "image/image.h"
#include "application/application.h"



+ 134
- 0
src/gpu/indexbuffer.cpp Просмотреть файл

@@ -0,0 +1,134 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"
#include "lolgl.h"

#if defined _WIN32 && defined USE_D3D9
# define FAR
# define NEAR
# include <d3d9.h>
#endif

using namespace std;

#if defined USE_D3D9
extern IDirect3DDevice9 *g_d3ddevice;
#elif defined _XBOX
extern D3DDevice *g_d3ddevice;
#endif

namespace lol
{

//
// The IndexBufferData class
// --------------------------
//

class IndexBufferData
{
friend class IndexBuffer;

#if defined USE_D3D9
IDirect3DIndexBuffer9 *m_ibo;
#elif defined _XBOX
D3DIndexBuffer *m_ibo;
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
GLuint m_ibo;
uint8_t *m_memory;
size_t m_size;
#endif
};

//
// The IndexBuffer class
// ----------------------
//

IndexBuffer::IndexBuffer(size_t size)
: m_data(new IndexBufferData)
{
#if defined USE_D3D9 || defined _XBOX
if (FAILED(g_d3ddevice->CreateIndexBuffer(size, D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_MANAGED,
&m_data->m_ibo, NULL)))
Abort();
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glGenBuffers(1, &m_data->m_ibo);
m_data->m_memory = new uint8_t[size];
m_data->m_size = size;
#endif
}

IndexBuffer::~IndexBuffer()
{
#if defined USE_D3D9 || defined _XBOX
if (FAILED(m_data->m_ibo->Release()))
Abort();
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glDeleteBuffers(1, &m_data->m_ibo);
delete[] m_data->m_memory;
#endif
}

void *IndexBuffer::Lock(size_t offset, size_t size)
{
#if defined USE_D3D9 || defined _XBOX
void *ret;
if (FAILED(m_data->m_ibo->Lock(offset, size, (void **)&ret, 0)))
Abort();
return ret;
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
return m_data->m_memory + offset;
#endif
}

void IndexBuffer::Unlock()
{
#if defined USE_D3D9 || defined _XBOX
if (FAILED(m_data->m_ibo->Unlock()))
Abort();
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_data->m_size, m_data->m_memory,
GL_STATIC_DRAW);
#endif
}

void IndexBuffer::Bind()
{
#if defined USE_D3D9 || defined _XBOX
if (FAILED(g_d3ddevice->SetIndices(m_data->m_ibo)))
Abort();
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_data->m_ibo);
/* XXX: not necessary because we kept track of the size */
//int size;
//glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
#endif
}

void IndexBuffer::Unbind()
{
#if defined USE_D3D9 || defined _XBOX
if (FAILED(g_d3ddevice->SetIndices(NULL)))
Abort();
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif
}

} /* namespace lol */


src/gpu/vbo.h → src/gpu/indexbuffer.h Просмотреть файл

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
@@ -9,37 +9,35 @@
//

//
// The GpuVbo class
// ----------------
// The IndexBuffer class
// ---------------------
//

#if !defined __LOL_VBO_H__
#define __LOL_VBO_H__
#if !defined __LOL_INDEXBUFFER_H__
#define __LOL_INDEXBUFFER_H__

#include <cstring>

namespace lol
{

class GpuVboData;

class GpuVbo
class IndexBuffer
{
public:
GpuVbo();
~GpuVbo();
IndexBuffer(size_t size);
~IndexBuffer();

void SetSize(size_t elemsize, size_t elemcount);
size_t GetSize();
uint8_t *GetData();
uint8_t const *GetData() const;
void *Lock(size_t offset, size_t size);
void Unlock();

void Bind();
void Unbind();

private:
GpuVboData *data;
class IndexBufferData *m_data;
};

} /* namespace lol */

#endif // __LOL_VBO_H__
#endif // __LOL_INDEXBUFFER_H__


+ 0
- 100
src/gpu/vbo.cpp Просмотреть файл

@@ -1,100 +0,0 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"
#include "lolgl.h"
#include "gpu/vbo.h"

using namespace std;

namespace lol
{

/*
* GpuVbo implementation class
*/

class GpuVboData
{
friend class GpuVbo;

size_t elemsize, elemcount;
uint8_t *alloc_buffer;

static size_t const GPU_ALIGN = 128;
};

/*
* Public GpuVbo class
*/

GpuVbo::GpuVbo()
: data(new GpuVboData())
{
data->elemsize = 0;
data->elemcount = 0;
data->alloc_buffer = 0;
}

void GpuVbo::SetSize(size_t elemsize, size_t elemcount)
{
size_t oldsize = data->elemsize * data->elemcount;
size_t newsize = elemsize * elemcount;

if (newsize == oldsize)
return;

if (oldsize)
delete[] data->alloc_buffer;
data->alloc_buffer = NULL;
if (newsize)
data->alloc_buffer = new uint8_t[newsize + GpuVboData::GPU_ALIGN - 1];

data->elemsize = elemsize;
data->elemcount = elemcount;
}

size_t GpuVbo::GetSize()
{
return data->elemsize * data->elemcount;
}

uint8_t *GpuVbo::GetData()
{
return (uint8_t *)(((uintptr_t)data->alloc_buffer)
& ~((uintptr_t)GpuVboData::GPU_ALIGN - 1));
}

uint8_t const *GpuVbo::GetData() const
{
return (uint8_t const *)(((uintptr_t)data->alloc_buffer)
& ~((uintptr_t)GpuVboData::GPU_ALIGN - 1));
}

void GpuVbo::Bind()
{
}

void GpuVbo::Unbind()
{
}

GpuVbo::~GpuVbo()
{
delete[] data->alloc_buffer;
delete data;
}

} /* namespace lol */


+ 25
- 0
src/gpu/vertexbuffer.cpp Просмотреть файл

@@ -142,6 +142,31 @@ void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count)
#endif
}

void VertexDeclaration::DrawIndexedElements(MeshPrimitive type, int vbase,
int vskip, int vcount,
int skip, int count)
{
#if defined _XBOX || defined USE_D3D9
if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
Abort();
switch (type)
{
case MeshPrimitive::Triangles:
if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, vbase, vskip, vcount, skip, count)))
Abort();
break;
}
#else
switch (type)
{
case MeshPrimitive::Triangles:
/* FIXME: ignores most of the arguments! */
glDrawElements(GL_TRIANGLES, count * 3, GL_UNSIGNED_SHORT, 0);
break;
}
#endif
}

void VertexDeclaration::Unbind()
{
#if defined _XBOX || defined USE_D3D9


+ 2
- 0
src/gpu/vertexbuffer.h Просмотреть файл

@@ -177,6 +177,8 @@ public:

void Bind();
void DrawElements(MeshPrimitive type, int skip, int count);
void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip,
int vcount, int skip, int count);
void Unbind();
void SetStream(VertexBuffer *vb, ShaderAttrib attr1,
ShaderAttrib attr2 = ShaderAttrib(),


+ 7
- 60
test/tutorial/02_cube.cpp Просмотреть файл

@@ -13,18 +13,11 @@
#endif

#include "core.h"
#include "lolgl.h"
#include "loldebug.h"

using namespace std;
using namespace lol;

#if defined _WIN32 && defined USE_D3D9
# define FAR
# define NEAR
# include <d3d9.h>
#endif

#if USE_SDL && defined __APPLE__
# include <SDL_main.h>
#endif
@@ -34,12 +27,6 @@ using namespace lol;
# include <direct.h>
#endif

#if defined USE_D3D9
extern IDirect3DDevice9 *g_d3ddevice;
#elif defined _XBOX
extern D3DDevice *g_d3ddevice;
#endif

class Cube : public WorldEntity
{
public:
@@ -144,23 +131,10 @@ public:
memcpy(mesh, &m_mesh[0], m_mesh.Bytes());
m_vbo->Unlock();

#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__ && !defined _XBOX && !defined USE_D3D9
/* Method 1: store vertex buffer on the GPU memory */
glGenBuffers(1, &m_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.Bytes(),
&m_indices[0], GL_STATIC_DRAW);
#elif defined _XBOX || defined USE_D3D9
int16_t *indices;
if (FAILED(g_d3ddevice->CreateIndexBuffer(m_indices.Bytes(), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ibo, NULL)))
Abort();
if (FAILED(m_ibo->Lock(0, 0, (void **)&indices, 0)))
Abort();
m_ibo = new IndexBuffer(m_indices.Bytes());
void *indices = m_ibo->Lock(0, 0);
memcpy(indices, &m_indices[0], m_indices.Bytes());
m_ibo->Unlock();
#else
/* TODO */
#endif

/* FIXME: this object never cleans up */
m_ready = true;
@@ -172,29 +146,10 @@ public:
m_shader->SetUniform(m_mvp, m_matrix);
m_vdecl->SetStream(m_vbo, m_coord, m_color);
m_vdecl->Bind();

#if defined _XBOX || defined USE_D3D9
if (FAILED(g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)))
Abort();
if (FAILED(g_d3ddevice->SetIndices(m_ibo)))
Abort();
if (FAILED(g_d3ddevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, m_indices.Count())))
Abort();
#elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
int size;
glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);


glDrawElements(GL_TRIANGLES, size / sizeof(uint16_t), GL_UNSIGNED_SHORT, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
#else
/* FIXME */
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, m_vertices);
glDisableClientState(GL_VERTEX_ARRAY);
#endif
m_ibo->Bind();
m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, 8, 0,
m_indices.Count());
m_ibo->Unbind();
m_vdecl->Unbind();
}

@@ -208,15 +163,7 @@ private:
ShaderUniform m_mvp;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
#if defined USE_D3D9
IDirect3DIndexBuffer9 *m_ibo;
#elif defined _XBOX
D3DIndexBuffer *m_ibo;
#else
#if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
GLuint m_ibo;
#endif
#endif
IndexBuffer *m_ibo;
bool m_ready;
};



+ 0
- 2
win32/lolcore.vcxproj Просмотреть файл

@@ -86,7 +86,6 @@
<ClCompile Include="..\src\font.cpp" />
<ClCompile Include="..\src\forge.cpp" />
<ClCompile Include="..\src\gpu\shader.cpp" />
<ClCompile Include="..\src\gpu\vbo.cpp" />
<ClCompile Include="..\src\gpu\vertexbuffer.cpp" />
<ClCompile Include="..\src\gradient.cpp" />
<ClCompile Include="..\src\hash.cpp" />
@@ -140,7 +139,6 @@
<ClInclude Include="..\src\font.h" />
<ClInclude Include="..\src\forge.h" />
<ClInclude Include="..\src\gpu\shader.h" />
<ClInclude Include="..\src\gpu\vbo.h" />
<ClInclude Include="..\src\gpu\vertexbuffer.h" />
<ClInclude Include="..\src\gradient.h" />
<ClInclude Include="..\src\hash.h" />


+ 4
- 4
win32/lolcore.vcxproj.filters Просмотреть файл

@@ -160,10 +160,10 @@
<ClCompile Include="..\src\worldentity.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\gpu\shader.cpp">
<ClCompile Include="..\src\gpu\indexbuffer.cpp">
<Filter>src\gpu</Filter>
</ClCompile>
<ClCompile Include="..\src\gpu\vbo.cpp">
<ClCompile Include="..\src\gpu\shader.cpp">
<Filter>src\gpu</Filter>
</ClCompile>
<ClCompile Include="..\src\gpu\vertexbuffer.cpp">
@@ -336,10 +336,10 @@
<ClInclude Include="..\src\lol\unit.h">
<Filter>src\lol</Filter>
</ClInclude>
<ClInclude Include="..\src\gpu\shader.h">
<ClInclude Include="..\src\gpu\indexbuffer.h">
<Filter>src\gpu</Filter>
</ClInclude>
<ClInclude Include="..\src\gpu\vbo.h">
<ClInclude Include="..\src\gpu\shader.h">
<Filter>src\gpu</Filter>
</ClInclude>
<ClInclude Include="..\src\gpu\vertexbuffer.h">


Загрузка…
Отмена
Сохранить