Przeglądaj źródła

Use smart pointers in a lot of the rendering code.

legacy
Sam Hocevar 5 lat temu
rodzic
commit
1f9be92a66
33 zmienionych plików z 348 dodań i 373 usunięć
  1. +1
    -1
      doc/samples/btphystest.cpp
  2. +4
    -3
      doc/samples/btphystest.h
  3. +8
    -6
      doc/tutorial/01_triangle.cpp
  4. +9
    -9
      doc/tutorial/02_cube.cpp
  5. +8
    -6
      doc/tutorial/03_noise.cpp
  6. +8
    -9
      doc/tutorial/04_texture.cpp
  7. +1
    -2
      doc/tutorial/05_easymesh.cpp
  8. +9
    -9
      doc/tutorial/07_input.cpp
  9. +9
    -7
      doc/tutorial/08_fbo.cpp
  10. +12
    -11
      doc/tutorial/11_fractal.cpp
  11. +14
    -14
      doc/tutorial/12_voronoi.cpp
  12. +2
    -2
      doc/tutorial/13_shader_builder.cpp
  13. +45
    -48
      src/easymesh/easymeshrender.cpp
  14. +12
    -13
      src/easymesh/easymeshrender.h
  15. +2
    -2
      src/gpu/framebuffer.cpp
  16. +2
    -4
      src/gpu/rendercontext.cpp
  17. +0
    -1
      src/gpu/renderer.cpp
  18. +13
    -26
      src/gpu/shader.cpp
  19. +14
    -13
      src/gpu/vertexbuffer.cpp
  20. +19
    -18
      src/gradient.cpp
  21. +4
    -2
      src/gradient.h
  22. +5
    -3
      src/lol/gpu/rendercontext.h
  23. +3
    -6
      src/lol/gpu/renderer.h
  24. +3
    -4
      src/lol/gpu/shader.h
  25. +16
    -14
      src/lol/gpu/vertexbuffer.h
  26. +4
    -8
      src/lolimgui.cpp
  27. +4
    -3
      src/lolimgui.h
  28. +19
    -18
      src/mesh/mesh.cpp
  29. +18
    -15
      src/mesh/mesh.h
  30. +12
    -9
      src/mesh/primitivemesh.cpp
  31. +11
    -9
      src/mesh/primitivemesh.h
  32. +43
    -65
      src/scene.cpp
  33. +14
    -13
      src/scene.h

+ 1
- 1
doc/samples/btphystest.cpp Wyświetl plik

@@ -721,7 +721,7 @@ void BtPhysTest::tick_draw(float seconds, Scene &scene)
//-----------------------------------------------------------------------------
// CShaderData
//-----------------------------------------------------------------------------
CatShaderData::CatShaderData(uint32_t vert_decl_flags, Shader* shader)
CatShaderData::CatShaderData(uint32_t vert_decl_flags, std::shared_ptr<Shader> shader)
: GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
{
m_sprite_orientation = .0f;


+ 4
- 3
doc/samples/btphystest.h Wyświetl plik

@@ -2,7 +2,7 @@
// Lol Engine — Bullet physics test
//
// Copyright © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
// © 2012—2018 Sam Hocevar <sam@hocevar.net>
// © 2012—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -13,6 +13,7 @@

#pragma once

#include <memory>
#include <string>
#include <map>

@@ -22,7 +23,7 @@ class CatShaderData : public GpuShaderData

public:
//---
CatShaderData(uint32_t vert_decl_flags, Shader* shader);
CatShaderData(uint32_t vert_decl_flags, std::shared_ptr<Shader> shader);
//---
void SetupDefaultData();
virtual void SetupShaderDatas(mat4 const &model);
@@ -89,7 +90,7 @@ private:


TileSet* m_cat_texture;
Shader* m_cat_shader;
std::shared_ptr<Shader> m_cat_shader;
CatShaderData* m_cat_sdata;
Camera* m_camera;
Controller* m_controller;


+ 8
- 6
doc/tutorial/01_triangle.cpp Wyświetl plik

@@ -1,7 +1,7 @@
//
// Lol Engine — Triangle tutorial
//
// Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2012—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -14,6 +14,8 @@
# include "config.h"
#endif

#include <memory>

#include <lol/engine.h>
#include "loldebug.h"

@@ -41,9 +43,9 @@ public:
m_shader = Shader::Create(LOLFX_RESOURCE_NAME(01_triangle));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);

m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = new VertexBuffer(m_vertices.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
@@ -62,10 +64,10 @@ public:

private:
array<vec2> m_vertices;
Shader *m_shader;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
bool m_ready;
};



+ 9
- 9
doc/tutorial/02_cube.cpp Wyświetl plik

@@ -107,21 +107,21 @@ public:
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);

m_vdecl =
new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
m_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3>(VertexUsage::Position,
VertexUsage::Color));

m_vbo = new VertexBuffer(m_mesh.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_mesh.bytes());
void *mesh = m_vbo->Lock(0, 0);
memcpy(mesh, &m_mesh[0], m_mesh.bytes());
m_vbo->Unlock();

m_lines_ibo = new IndexBuffer(m_lines_indices.bytes());
m_lines_ibo = std::make_shared<IndexBuffer>(m_lines_indices.bytes());
void *indices = m_lines_ibo->Lock(0, 0);
memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
m_lines_ibo->Unlock();

m_faces_ibo = new IndexBuffer(m_faces_indices.bytes());
m_faces_ibo = std::make_shared<IndexBuffer>(m_faces_indices.bytes());
indices = m_faces_ibo->Lock(0, 0);
memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
m_faces_ibo->Unlock();
@@ -156,12 +156,12 @@ private:
array<vec3,vec3> m_mesh;
array<uint16_t> m_lines_indices, m_faces_indices;

Shader *m_shader;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord, m_color;
ShaderUniform m_mvp;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
IndexBuffer *m_lines_ibo, *m_faces_ibo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
std::shared_ptr<IndexBuffer> m_lines_ibo, m_faces_ibo;

bool m_ready;
};


+ 8
- 6
doc/tutorial/03_noise.cpp Wyświetl plik

@@ -1,7 +1,7 @@
//
// Lol Engine — Noise tutorial
//
// Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2012—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -14,6 +14,8 @@
# include "config.h"
#endif

#include <memory>

#include <lol/engine.h>
#include "loldebug.h"

@@ -48,9 +50,9 @@ public:
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_time_uni = m_shader->GetUniformLocation("u_time");

m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = new VertexBuffer(m_vertices.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
@@ -70,11 +72,11 @@ public:

private:
array<vec2> m_vertices;
Shader *m_shader;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
ShaderUniform m_time_uni;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
float m_time;
bool m_ready;
};


+ 8
- 9
doc/tutorial/04_texture.cpp Wyświetl plik

@@ -1,7 +1,7 @@
//
// Lol Engine — Graphing tutorial
//
// Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2012—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -67,15 +67,15 @@ public:
/* Initialise GPU data */
if (!m_ready)
{
m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
m_texture = std::make_shared<Texture>(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);

m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_texture_uni = m_shader->GetUniformLocation("u_texture");

m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = new VertexBuffer(m_vertices.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
@@ -99,12 +99,12 @@ public:

private:
array<vec2> m_vertices;
Texture *m_texture;
Shader *m_shader;
std::shared_ptr<Texture> m_texture;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
ShaderUniform m_texture_uni;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
array<uint8_t> m_heightmap;
int m_frames;
bool m_ready;
@@ -116,7 +116,6 @@ int main(int argc, char **argv)

Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);

//new DebugFps(50, 50);
new TextureDemo();

app.Run();


+ 1
- 2
doc/tutorial/05_easymesh.cpp Wyświetl plik

@@ -137,7 +137,7 @@ public:

#if USE_CUSTOM_SHADER
/* Custom Shader: Init the shader */
Shader *custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny));
auto custom_shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny));
// any other shader stuf here (Get uniform, mostly, and set texture)

for (int i = 0; i < m_gears.count(); i++)
@@ -154,7 +154,6 @@ public:
}

private:
Shader* m_custom_shader;
array<EasyMesh, mat4, float> m_gears;
float m_angle;
mat4 m_mat;


+ 9
- 9
doc/tutorial/07_input.cpp Wyświetl plik

@@ -168,21 +168,21 @@ public:
m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
m_color = m_shader->GetAttribLocation(VertexUsage::Color, 0);

m_vdecl =
new VertexDeclaration(VertexStream<vec3,vec3>(VertexUsage::Position,
m_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3>(VertexUsage::Position,
VertexUsage::Color));

m_vbo = new VertexBuffer(m_mesh.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_mesh.bytes());
void *mesh = m_vbo->Lock(0, 0);
memcpy(mesh, &m_mesh[0], m_mesh.bytes());
m_vbo->Unlock();

m_lines_ibo = new IndexBuffer(m_lines_indices.bytes());
m_lines_ibo = std::make_shared<IndexBuffer>(m_lines_indices.bytes());
void *indices = m_lines_ibo->Lock(0, 0);
memcpy(indices, &m_lines_indices[0], m_lines_indices.bytes());
m_lines_ibo->Unlock();

m_faces_ibo = new IndexBuffer(m_faces_indices.bytes());
m_faces_ibo = std::make_shared<IndexBuffer>(m_faces_indices.bytes());
indices = m_faces_ibo->Lock(0, 0);
memcpy(indices, &m_faces_indices[0], m_faces_indices.bytes());
m_faces_ibo->Unlock();
@@ -238,12 +238,12 @@ private:
array<vec3,vec3> m_mesh;
array<uint16_t> m_lines_indices, m_faces_indices;

Shader *m_shader;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord, m_color;
ShaderUniform m_mvp;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
IndexBuffer *m_lines_ibo, *m_faces_ibo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
std::shared_ptr<IndexBuffer> m_lines_ibo, m_faces_ibo;

Text *m_text;
bool m_ready;


+ 9
- 7
doc/tutorial/08_fbo.cpp Wyświetl plik

@@ -16,6 +16,8 @@

#include <lol/engine.h>

#include <memory>

using namespace lol;

LOLFX_RESOURCE_DECLARE(08_fbo);
@@ -65,14 +67,14 @@ public:
m_uni_color = m_shader->GetUniformLocation("u_color");
m_uni_texture = m_shader->GetUniformLocation("u_texture");

m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = new VertexBuffer(m_vertices.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();

m_fbo = new Framebuffer(Video::GetSize());
m_fbo = std::make_shared<Framebuffer>(Video::GetSize());
m_fbo->Bind();

{
@@ -120,12 +122,12 @@ public:

private:
array<vec2> m_vertices;
Shader *m_shader;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_coord;
ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
Framebuffer *m_fbo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;
std::shared_ptr<Framebuffer> m_fbo;
double m_time;
vec3 m_hotspot, m_color;
bool m_ready;


+ 12
- 11
doc/tutorial/11_fractal.cpp Wyświetl plik

@@ -1,7 +1,7 @@
//
// Lol Engine — Fractal tutorial
//
// Copyright © 2011—2016 Sam Hocevar <sam@hocevar.net>
// Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -14,6 +14,7 @@
# include "config.h"
#endif

#include <memory>
#include <cstring>
#include <cstdio>

@@ -460,8 +461,8 @@ public:
{
/* Create a texture of half the width and twice the height
* so that we can upload four different subimages each frame. */
m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2),
PixelFormat::RGBA_8);
m_texture = std::make_shared<Texture>(ivec2(m_size.x / 2, m_size.y * 2),
PixelFormat::RGBA_8);

/* Ensure the texture data is complete at least once, otherwise
* uploading subimages will not work. */
@@ -476,11 +477,11 @@ public:
m_screenuni = m_shader->GetUniformLocation("u_screen_size");
m_zoomuni = m_shader->GetUniformLocation("u_zoom_settings");

m_vdecl =
new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
m_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec2>(VertexUsage::Position),
VertexStream<vec2>(VertexUsage::TexCoord));
m_vbo = new VertexBuffer(sizeof(vertices));
m_tbo = new VertexBuffer(sizeof(texcoords));
m_vbo = std::make_shared<VertexBuffer>(sizeof(vertices));
m_tbo = std::make_shared<VertexBuffer>(sizeof(texcoords));

void *tmp = m_vbo->Lock(0, 0);
memcpy(tmp, vertices, sizeof(vertices));
@@ -537,13 +538,13 @@ private:
dvec2 m_texel2world;
array<u8vec4> m_pixels, m_palette;

Shader *m_shader;
std::shared_ptr<Shader> m_shader;
ShaderAttrib m_vertexattrib, m_texattrib;
ShaderUniform m_texuni, m_texeluni, m_screenuni, m_zoomuni;

VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo, *m_tbo;
Texture *m_texture;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo, m_tbo;
std::shared_ptr<Texture> m_texture;

int m_frame, m_slices, m_dirty[4];
bool m_ready, m_drag;


+ 14
- 14
doc/tutorial/12_voronoi.cpp Wyświetl plik

@@ -15,6 +15,8 @@
# include "config.h"
#endif

#include <memory>

#include <lol/engine.h>
#include "loldebug.h"

@@ -102,9 +104,9 @@ public:

if (!m_ready)
{
m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));

m_vbo = new VertexBuffer(m_vertices.bytes());
m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
void *vertices = m_vbo->Lock(0, 0);
memcpy(vertices, &m_vertices[0], m_vertices.bytes());
m_vbo->Unlock();
@@ -115,7 +117,7 @@ public:

for (int i = 0; i < MaxFboType; ++i)
{
m_fbos.push(new Framebuffer(Video::GetSize()), 0, array<ShaderUniform>(), array<ShaderAttrib>() );
m_fbos.push(std::make_shared<Framebuffer>(Video::GetSize()), 0, array<ShaderUniform>(), array<ShaderAttrib>() );

if (i == SrcVoronoiFbo)
{
@@ -152,7 +154,7 @@ public:
m_fbos.last().m1->Unbind();
}

temp_buffer = new Framebuffer(Video::GetSize());
temp_buffer = std::make_shared<Framebuffer>(Video::GetSize());
temp_buffer->Bind();
{
RenderContext rc(scene.get_renderer());
@@ -259,8 +261,7 @@ public:
int buf = voronoi_points.count() % 2;
for (int j = 0; j < voronoi_points.count(); ++j)
{
Framebuffer *dst_buf;
Framebuffer *src_buf;
std::shared_ptr<Framebuffer> dst_buf, src_buf;

if (buf)
{
@@ -314,9 +315,8 @@ public:
int buf = 0;
while (1)
{
Framebuffer *dst_buf;
Framebuffer *src_buf;
Shader *shader;
std::shared_ptr<Framebuffer> dst_buf, src_buf;
std::shared_ptr<Shader> shader;

if (curres == ivec2::zero)
shader = m_screen_shader;
@@ -385,15 +385,15 @@ private:
Controller* m_controller;
array<vec3, vec2> voronoi_points;
array<vec2> m_vertices;
Shader *m_screen_shader;
std::shared_ptr<Shader> m_screen_shader;
ShaderAttrib m_screen_coord;
ShaderUniform m_screen_texture;

VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo;

array<Framebuffer *, Shader *, array<ShaderUniform>, array<ShaderAttrib> > m_fbos;
Framebuffer *temp_buffer;
array<std::shared_ptr<Framebuffer>, std::shared_ptr<Shader>, array<ShaderUniform>, array<ShaderAttrib> > m_fbos;
std::shared_ptr<Framebuffer> temp_buffer;

int mode;
int m_cur_fbo;


+ 2
- 2
doc/tutorial/13_shader_builder.cpp Wyświetl plik

@@ -114,8 +114,8 @@ public:
//code = file.ReadString();
file.Close();

Shader* shader = Shader::Create(builder.GetName(), code);
Shader::Destroy(shader);
std::shared_ptr<Shader> shader = Shader::Create(builder.GetName(), code);
shader = nullptr;

Ticker::Shutdown();
}


+ 45
- 48
src/easymesh/easymeshrender.cpp Wyświetl plik

@@ -2,7 +2,7 @@
// Lol Engine
//
// Copyright © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
// © 2010—2018 Sam Hocevar <sam@hocevar.net>
// © 2010—2019 Sam Hocevar <sam@hocevar.net>
// © 2009—2013 Cédric Lecacheur <jordx@free.fr>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -29,10 +29,10 @@ LOLFX_RESOURCE_DECLARE(easymesh_shiny_SK);
void EasyMesh::MeshConvert()
{
/* Default material */
Shader *shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny));
auto shader = Shader::Create(LOLFX_RESOURCE_NAME(easymesh_shiny));

/* Push index buffer to GPU */
IndexBuffer *ibo = new IndexBuffer(m_indices.count() * sizeof(uint16_t));
auto ibo = std::make_shared<IndexBuffer>(m_indices.count() * sizeof(uint16_t));
uint16_t *indices = (uint16_t *)ibo->Lock(0, 0);
for (int i = 0; i < m_indices.count(); ++i)
indices[i] = m_indices[i];
@@ -46,13 +46,13 @@ void EasyMesh::MeshConvert()
vec4 texcoord;
};

VertexDeclaration *vdecl = new VertexDeclaration(
auto vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3, vec3, u8vec4, vec4>(VertexUsage::Position,
VertexUsage::Normal,
VertexUsage::Color,
VertexUsage::TexCoord));

VertexBuffer *vbo = new VertexBuffer(m_vert.count() * sizeof(Vertex));
auto vbo = std::make_shared<VertexBuffer>(m_vert.count() * sizeof(Vertex));
Vertex *vert = (Vertex *)vbo->Lock(0, 0);
for (int i = 0; i < m_vert.count(); ++i)
{
@@ -64,9 +64,9 @@ void EasyMesh::MeshConvert()
vbo->Unlock();

/* Reference our new data in our submesh */
m_submeshes.push(new SubMesh(shader, vdecl));
m_submeshes.last()->SetIndexBuffer(ibo);
m_submeshes.last()->SetVertexBuffer(0, vbo);
m_submeshes.push_back(std::make_shared<SubMesh>(shader, vdecl));
m_submeshes.back()->SetIndexBuffer(ibo);
m_submeshes.back()->SetVertexBuffer(0, vbo);

m_state = MeshRender::CanRender;
}
@@ -78,11 +78,11 @@ GpuShaderData::GpuShaderData()
}

//-----------------------------------------------------------------------------
GpuShaderData::GpuShaderData(uint16_t vert_decl_flags, Shader* shader, DebugRenderMode render_mode)
GpuShaderData::GpuShaderData(uint16_t vert_decl_flags, std::shared_ptr<Shader> shader, DebugRenderMode render_mode)
: m_render_mode(render_mode),
m_shader(shader),
m_vert_decl_flags(vert_decl_flags)
{
m_render_mode = render_mode;
m_shader = shader;
m_vert_decl_flags = vert_decl_flags;
}

//-----------------------------------------------------------------------------
@@ -152,7 +152,7 @@ DefaultShaderData::DefaultShaderData(DebugRenderMode render_mode)
}

//-----------------------------------------------------------------------------
DefaultShaderData::DefaultShaderData(uint16_t vert_decl_flags, Shader* shader, bool with_UV)
DefaultShaderData::DefaultShaderData(uint16_t vert_decl_flags, std::shared_ptr<Shader> shader, bool with_UV)
: GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
{
StoreUniformNames();
@@ -222,16 +222,13 @@ GpuEasyMeshData::GpuEasyMeshData()
{
m_vertexcount = 0;
m_indexcount = 0;
m_ibo = nullptr;
}

//-----------------------------------------------------------------------------
GpuEasyMeshData::~GpuEasyMeshData()
{
m_gpudatas.clear();
m_vdatas.clear();
if (m_ibo)
delete m_ibo;
m_gpudata.clear();
m_vdata.clear();
}

#define BUILD_VFLAG(bool_value, flag_value, check_flag) \
@@ -245,7 +242,7 @@ GpuEasyMeshData::~GpuEasyMeshData()
count_value += (int)bool_value;

//-----------------------------------------------------------------------------
void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh)
void GpuEasyMeshData::AddGpuData(std::shared_ptr<GpuShaderData> gpudata, std::shared_ptr<EasyMesh> src_mesh)
{
uint16_t vflags = gpudata->m_vert_decl_flags;

@@ -273,7 +270,7 @@ void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh)
indexlist << src_mesh->m_indices[i + 2];
}

m_ibo = new IndexBuffer(indexlist.bytes());
m_ibo = std::make_shared<IndexBuffer>(indexlist.bytes());
void *indices = m_ibo->Lock(0, 0);
memcpy(indices, &indexlist[0], indexlist.bytes());
m_ibo->Unlock();
@@ -282,26 +279,26 @@ void GpuEasyMeshData::AddGpuData(GpuShaderData* gpudata, EasyMesh* src_mesh)
}

//init to a minimum of gpudata->m_render_mode size
if (m_gpudatas.count() <= gpudata->m_render_mode)
if (m_gpudata.count() <= gpudata->m_render_mode)
{
int i = m_gpudatas.count();
int i = m_gpudata.count();
int max = gpudata->m_render_mode + 1;
m_gpudatas.reserve(max);
m_gpudata.reserve(max);
for (; i < max; i++)
m_gpudatas << nullptr;
m_gpudata.push(nullptr);
}
m_gpudatas[gpudata->m_render_mode] = gpudata;
m_gpudata[gpudata->m_render_mode] = gpudata;
}

//-----------------------------------------------------------------------------
void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
void GpuEasyMeshData::SetupVertexData(uint16_t vflags, std::shared_ptr<EasyMesh> src_mesh)
{
for (int i = 0; i < m_vdatas.count(); ++i)
if (m_vdatas[i].m1 == vflags)
for (int i = 0; i < m_vdata.count(); ++i)
if (m_vdata[i].m1 == vflags)
return;

VertexDeclaration* new_vdecl = nullptr;
VertexBuffer* new_vbo = nullptr;
std::shared_ptr<VertexDeclaration> new_vdecl;
std::shared_ptr<VertexBuffer> new_vbo;
void *vbo_data = nullptr;
int vbo_bytes = 0;

@@ -309,7 +306,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
vbo_data = &vertexlist[0]; \
vbo_bytes = vertexlist.bytes(); \
m_vertexcount = vertexlist.count(); \
new_vbo = new VertexBuffer(vbo_bytes); \
new_vbo = std::make_shared<VertexBuffer>(vbo_bytes); \
void *mesh = new_vbo->Lock(0, 0); \
memcpy(mesh, vbo_data, vbo_bytes); \
new_vbo->Unlock();
@@ -326,7 +323,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)

if (flagnb == 5 && has_position && has_normal && has_color && has_texcoord && has_texcoordExt)
{
new_vdecl = new VertexDeclaration(
new_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3,u8vec4,vec4>(
VertexUsage::Position,
VertexUsage::Normal,
@@ -343,7 +340,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
}
else if (flagnb == 4 && has_position && has_normal && has_color && has_texcoord)
{
new_vdecl = new VertexDeclaration(
new_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3,u8vec4,vec2>(
VertexUsage::Position,
VertexUsage::Normal,
@@ -360,7 +357,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
}
else if (flagnb == 4 && has_position && has_color && has_texcoord && has_texcoordExt)
{
new_vdecl = new VertexDeclaration(VertexStream<vec3,vec4,vec4>(VertexUsage::Position, VertexUsage::Color, VertexUsage::TexCoord));
new_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec3,vec4,vec4>(VertexUsage::Position, VertexUsage::Color, VertexUsage::TexCoord));

array<vec3, vec4, vec4> vertexlist;
for (int i = 0; i < src_mesh->m_vert.count(); i++)
@@ -369,7 +366,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
}
else if (flagnb == 3 && has_position && has_normal && has_color)
{
new_vdecl = new VertexDeclaration(
new_vdecl = std::make_shared<VertexDeclaration>(
VertexStream<vec3,vec3,u8vec4>(
VertexUsage::Position,
VertexUsage::Normal,
@@ -384,7 +381,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
}
else if (flagnb == 3 && has_position && has_texcoord && has_texcoordExt)
{
new_vdecl = new VertexDeclaration(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::TexCoord));
new_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec3,vec4>(VertexUsage::Position, VertexUsage::TexCoord));

array<vec3, vec4> vertexlist;
for (int i = 0; i < src_mesh->m_vert.count(); i++)
@@ -393,7 +390,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
}
else if (flagnb == 2 && has_position && has_texcoord)
{
new_vdecl = new VertexDeclaration(VertexStream<vec3,vec2>(VertexUsage::Position, VertexUsage::TexCoord));
new_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec3,vec2>(VertexUsage::Position, VertexUsage::TexCoord));

array<vec3, vec2> vertexlist;
for (int i = 0; i < src_mesh->m_vert.count(); i++)
@@ -402,7 +399,7 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
}
else if (flagnb == 2 && has_position && has_color)
{
new_vdecl = new VertexDeclaration(VertexStream<vec3,u8vec4>(VertexUsage::Position, VertexUsage::Color));
new_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec3,u8vec4>(VertexUsage::Position, VertexUsage::Color));

array<vec3, u8vec4> vertexlist;
for (int i = 0; i < src_mesh->m_vert.count(); i++)
@@ -412,27 +409,27 @@ void GpuEasyMeshData::SetupVertexData(uint16_t vflags, EasyMesh* src_mesh)
else
ASSERT(0, "no Vertex Declaration combination for 0x%04x", vflags);

m_vdatas.push(vflags, new_vdecl, new_vbo);
m_vdata.push(vflags, new_vdecl, new_vbo);
}

//-----------------------------------------------------------------------------
void GpuEasyMeshData::RenderMeshData(mat4 const &model, int render_mode)
{
ASSERT(0 <= render_mode && render_mode < m_gpudatas.count(), "render mode is not in the defined range");
ASSERT(m_gpudatas[render_mode], "gpu datas for this render mode don't exist");
GpuShaderData& gpu_sd = *(m_gpudatas[render_mode]);
ASSERT(0 <= render_mode && render_mode < m_gpudata.count(), "render mode is not in the defined range");
ASSERT(m_gpudata[render_mode], "gpu datas for this render mode don't exist");
GpuShaderData& gpu_sd = *(m_gpudata[render_mode]);

int vdecl_idx = 0;
for (; vdecl_idx < m_vdatas.count(); ++vdecl_idx)
if (m_vdatas[vdecl_idx].m1 == gpu_sd.m_vert_decl_flags)
for (; vdecl_idx < m_vdata.count(); ++vdecl_idx)
if (m_vdata[vdecl_idx].m1 == gpu_sd.m_vert_decl_flags)
break;

if (vdecl_idx >= m_vdatas.count())
if (vdecl_idx >= m_vdata.count())
return;

uint16_t vflags = m_vdatas[vdecl_idx].m1;
VertexDeclaration* vdecl = m_vdatas[vdecl_idx].m2;
VertexBuffer* vbo = m_vdatas[vdecl_idx].m3;
uint16_t vflags = m_vdata[vdecl_idx].m1;
auto vdecl = m_vdata[vdecl_idx].m2;
auto vbo = m_vdata[vdecl_idx].m3;

gpu_sd.m_shader->Bind();
gpu_sd.SetupShaderDatas(model);


+ 12
- 13
src/easymesh/easymeshrender.h Wyświetl plik

@@ -2,7 +2,7 @@
// Lol Engine
//
// Copyright © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
// © 2010—2018 Sam Hocevar <sam@hocevar.net>
// © 2010—2019 Sam Hocevar <sam@hocevar.net>
// © 2009—2013 Cédric Lecacheur <jordx@free.fr>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -79,7 +79,7 @@ protected:
GpuShaderData();
public:
//--
GpuShaderData(uint16_t vert_decl_flags, Shader* shader, DebugRenderMode render_mode);
GpuShaderData(uint16_t vert_decl_flags, std::shared_ptr<Shader> shader, DebugRenderMode render_mode);
virtual ~GpuShaderData();
//--
void AddUniform(std::string const &new_uniform);
@@ -92,7 +92,7 @@ public:

protected:
uint16_t m_vert_decl_flags;
Shader *m_shader;
std::shared_ptr<Shader> m_shader;
int m_render_mode;
array<std::string, ShaderUniform> m_shader_uniform;
array<ShaderAttrib> m_shader_attrib;
@@ -103,7 +103,7 @@ class DefaultShaderData : public GpuShaderData
public:
//---
DefaultShaderData(DebugRenderMode render_mode);
DefaultShaderData(uint16_t vert_decl_flags, Shader* shader, bool with_UV);
DefaultShaderData(uint16_t vert_decl_flags, std::shared_ptr<Shader> shader, bool with_UV);
virtual ~DefaultShaderData() {}
void StoreUniformNames();
//---
@@ -122,21 +122,20 @@ public:
GpuEasyMeshData();
~GpuEasyMeshData();
//---
void AddGpuData(GpuShaderData* gpudata, class EasyMesh* src_mesh);
void AddGpuData(std::shared_ptr<GpuShaderData> gpudata, std::shared_ptr<class EasyMesh> src_mesh);
void RenderMeshData(mat4 const &model, int render_mode=Video::GetDebugRenderMode());
bool HasData(int render_mode) { return (0 <= render_mode && render_mode < m_gpudatas.count() && !!m_gpudatas[render_mode]); }
bool HasData(int render_mode) { return (0 <= render_mode && render_mode < m_gpudata.count() && !!m_gpudata[render_mode]); }

private:
void SetupVertexData(uint16_t vdecl_flags, EasyMesh* src_mesh);
void SetupVertexData(uint16_t vdecl_flags, std::shared_ptr<EasyMesh> src_mesh);

array<GpuShaderData*> m_gpudatas;
array<std::shared_ptr<GpuShaderData>> m_gpudata;
//uint16_t are the vdecl/vbo flags to avoid copy same vdecl several times.
array<uint16_t, VertexDeclaration*,
VertexBuffer*> m_vdatas;
int m_vertexcount;
array<uint16_t, std::shared_ptr<VertexDeclaration>, std::shared_ptr<VertexBuffer>> m_vdata;
int m_vertexcount;
//We only need only one ibo for the whole mesh
IndexBuffer * m_ibo;
int m_indexcount;
std::shared_ptr<IndexBuffer> m_ibo;
int m_indexcount;
};

} /* namespace lol */


+ 2
- 2
src/gpu/framebuffer.cpp Wyświetl plik

@@ -373,7 +373,7 @@ void Framebuffer::Bind()
/* FIXME: this should be done in the RenderContext object
* instead, maybe by getting rid of Framebuffer::Bind() and
* creating RenderContext::SetFramebuffer() instead. */
auto *renderer = Scene::GetScene(0).get_renderer();
auto renderer = Scene::GetScene(0).get_renderer();
m_data->m_saved_viewport = renderer->GetViewport();
renderer->SetViewport(ibox2(ivec2::zero, m_data->m_size));
m_data->m_bound = true;
@@ -389,7 +389,7 @@ void Framebuffer::Unbind()
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
#endif

auto *renderer = Scene::GetScene(0).get_renderer();
auto renderer = Scene::GetScene(0).get_renderer();
renderer->SetViewport(m_data->m_saved_viewport);
m_data->m_bound = false;
}


+ 2
- 4
src/gpu/rendercontext.cpp Wyświetl plik

@@ -69,9 +69,9 @@ private:
* Public RenderContext class
*/

RenderContext::RenderContext(Renderer *renderer)
RenderContext::RenderContext(std::shared_ptr<Renderer> renderer)
: m_renderer(renderer),
m_data(new RenderContextData())
m_data(std::make_unique<RenderContextData>())
{
}

@@ -108,8 +108,6 @@ RenderContext::~RenderContext()

if (m_data->m_scissor_mode.is_dirty())
m_renderer->SetScissorMode(m_data->m_scissor_mode.get());

delete m_data;
}

void RenderContext::SetViewport(ibox2 viewport)


+ 0
- 1
src/gpu/renderer.cpp Wyświetl plik

@@ -116,7 +116,6 @@ Renderer::Renderer(ivec2 size)

Renderer::~Renderer()
{
delete m_data;
}

/*


+ 13
- 26
src/gpu/shader.cpp Wyświetl plik

@@ -13,7 +13,9 @@
#include <lol/engine-internal.h>

#include <string>
#include <memory>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>

@@ -89,14 +91,10 @@ private:
/* Shader patcher */
static int GetVersion();
static std::string Patch(std::string const &code, ShaderType type);

/* Global shader cache */
static Shader *shaders[];
static int nshaders;
};

Shader *ShaderData::shaders[256];
int ShaderData::nshaders = 0;
/* Global shader cache */
static std::set<std::shared_ptr<Shader>> g_shaders;

/*
* LolFx parser
@@ -187,7 +185,7 @@ struct lolfx_parser::action<lolfx_parser::code_section>
* Public Shader class
*/

Shader *Shader::Create(std::string const &name, std::string const &code)
std::shared_ptr<Shader> Shader::Create(std::string const &name, std::string const &code)
{
lolfx_parser p(code);

@@ -203,31 +201,22 @@ Shader *Shader::Create(std::string const &name, std::string const &code)
size_t new_vert_crc = std::hash<std::string>{}(vert);
size_t new_frag_crc = std::hash<std::string>{}(frag);

for (int n = 0; n < ShaderData::nshaders; n++)
for (auto shader : g_shaders)
{
if (ShaderData::shaders[n]->data->vert_crc == new_vert_crc
&& ShaderData::shaders[n]->data->frag_crc == new_frag_crc)
{
return ShaderData::shaders[n];
}
if (shader->data->vert_crc == new_vert_crc
&& shader->data->frag_crc == new_frag_crc)
return shader;
}

Shader *ret = new Shader(name, vert, frag);
ShaderData::shaders[ShaderData::nshaders] = ret;
ShaderData::nshaders++;

// FIXME: the cache never expires!
auto ret = std::make_shared<Shader>(name, vert, frag);
g_shaders.insert(ret);
return ret;
}

void Shader::Destroy(Shader *shader)
{
/* XXX: do nothing! the shader should remain in cache */
UNUSED(shader);
}

Shader::Shader(std::string const &name,
std::string const &vert, std::string const &frag)
: data(new ShaderData())
: data(std::make_unique<ShaderData>())
{
data->m_name = name;

@@ -515,8 +504,6 @@ Shader::~Shader()
glDeleteShader(data->vert_id);
glDeleteShader(data->frag_id);
glDeleteProgram(data->prog_id);

delete data;
}

/* Try to detect shader compiler features */


+ 14
- 13
src/gpu/vertexbuffer.cpp Wyświetl plik

@@ -150,18 +150,19 @@ void VertexDeclaration::Unbind()
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1,
ShaderAttrib attr2,
ShaderAttrib attr3,
ShaderAttrib attr4,
ShaderAttrib attr5,
ShaderAttrib attr6,
ShaderAttrib attr7,
ShaderAttrib attr8,
ShaderAttrib attr9,
ShaderAttrib attr10,
ShaderAttrib attr11,
ShaderAttrib attr12)
void VertexDeclaration::SetStream(std::shared_ptr<VertexBuffer> vb,
ShaderAttrib attr1,
ShaderAttrib attr2,
ShaderAttrib attr3,
ShaderAttrib attr4,
ShaderAttrib attr5,
ShaderAttrib attr6,
ShaderAttrib attr7,
ShaderAttrib attr8,
ShaderAttrib attr9,
ShaderAttrib attr10,
ShaderAttrib attr11,
ShaderAttrib attr12)
{
ShaderAttrib attribs[12] = { attr1, attr2, attr3, attr4, attr5, attr6,
attr7, attr8, attr9, attr10, attr11, attr12 };
@@ -169,7 +170,7 @@ void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attr1,
SetStream(vb, attribs);
}

void VertexDeclaration::SetStream(VertexBuffer *vb, ShaderAttrib attribs[])
void VertexDeclaration::SetStream(std::shared_ptr<VertexBuffer> vb, ShaderAttrib attribs[])
{
if (!vb->m_data->m_size)
return;


+ 19
- 18
src/gradient.cpp Wyświetl plik

@@ -1,15 +1,19 @@
//
// Lol Engine
// Lol Engine
//
// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. 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 the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#include <lol/engine-internal.h>

#include <memory>

LOLFX_RESOURCE_DECLARE(gradient);

namespace lol
@@ -24,9 +28,9 @@ class GradientData
friend class Gradient;

private:
Shader *shader;
VertexDeclaration *m_vdecl;
VertexBuffer *m_vbo, *m_cbo;
std::shared_ptr<Shader> shader;
std::shared_ptr<VertexDeclaration> m_vdecl;
std::shared_ptr<VertexBuffer> m_vbo, m_cbo;
};

/*
@@ -34,14 +38,12 @@ private:
*/

Gradient::Gradient(vec3 aa, vec3 bb)
: data(new GradientData())
: data(std::make_unique<GradientData>())
{
/* FIXME: this should not be hardcoded */
m_position = aa;
m_aabb.aa = aa;
m_aabb.bb = bb;

data->shader = nullptr;
}

void Gradient::tick_game(float seconds)
@@ -71,11 +73,12 @@ void Gradient::tick_draw(float seconds, Scene &scene)
{
data->shader = Shader::Create(LOLFX_RESOURCE_NAME(gradient));

data->m_vbo = new VertexBuffer(sizeof(vertex));
data->m_cbo = new VertexBuffer(sizeof(color));
data->m_vbo = std::make_shared<VertexBuffer>(sizeof(vertex));
data->m_cbo = std::make_shared<VertexBuffer>(sizeof(color));

data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
VertexStream<vec4>(VertexUsage::Color));
data->m_vdecl = std::make_shared<VertexDeclaration>
(VertexStream<vec3>(VertexUsage::Position),
VertexStream<vec4>(VertexUsage::Color));
}

mat4 model_matrix = mat4(1.0f);
@@ -115,8 +118,6 @@ void Gradient::tick_draw(float seconds, Scene &scene)

Gradient::~Gradient()
{
/* FIXME: destroy shader */
delete data;
}

} /* namespace lol */

+ 4
- 2
src/gradient.h Wyświetl plik

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -19,6 +19,8 @@

#include "engine/worldentity.h"

#include <memory>

namespace lol
{

@@ -37,7 +39,7 @@ protected:
virtual void tick_draw(float seconds, Scene &scene);

private:
GradientData *data;
std::unique_ptr<GradientData> data;
};

} /* namespace lol */


+ 5
- 3
src/lol/gpu/rendercontext.h Wyświetl plik

@@ -14,6 +14,8 @@

#include <lol/gpu/renderer.h>

#include <memory>

//
// The RenderContext class
// -----------------------
@@ -27,7 +29,7 @@ class RenderContextData;
class RenderContext
{
public:
RenderContext(Renderer *renderer);
RenderContext(std::shared_ptr<Renderer> renderer);
~RenderContext();

void SetViewport(ibox2 viewport);
@@ -60,8 +62,8 @@ public:
vec4 GetScissorRect();

private:
Renderer *m_renderer;
RenderContextData *m_data;
std::shared_ptr<Renderer> m_renderer;
std::unique_ptr<RenderContextData> m_data;
};

} /* namespace lol */


+ 3
- 6
src/lol/gpu/renderer.h Wyświetl plik

@@ -134,14 +134,11 @@ enum class ScissorMode : uint8_t

class Renderer
{
private:
/* Only the Scene class can create a renderer for now. */
friend class Scene;

public:
// FIXME: only the Scene class should be allowed to create a renderer
Renderer(ivec2 size);
~Renderer();

public:
void Clear(ClearMask mask);

void SetViewport(ibox2 viewport);
@@ -185,7 +182,7 @@ public:
vec4 GetScissorRect() const;

private:
RendererData *m_data;
std::unique_ptr<RendererData> m_data;
};

} /* namespace lol */


+ 3
- 4
src/lol/gpu/shader.h Wyświetl plik

@@ -19,6 +19,7 @@

#include <string>
#include <map>
#include <memory>
#include <cstdint>

#include "engine/entity.h"
@@ -316,8 +317,7 @@ class ShaderData;
class Shader : public Entity
{
public:
static Shader *Create(std::string const &name, std::string const &code);
static void Destroy(Shader *shader);
static std::shared_ptr<Shader> Create(std::string const &name, std::string const &code);

int GetAttribCount() const;
ShaderAttrib GetAttribLocation(VertexUsage usage, int index) const;
@@ -345,12 +345,11 @@ public:
void Bind() const;
void Unbind() const;

protected:
Shader(std::string const &name, std::string const &vert, std::string const &frag);
~Shader();

private:
ShaderData *data;
std::unique_ptr<ShaderData> data;

public:
static std::string GetVariablePrefix(const ShaderVariable variable);


+ 16
- 14
src/lol/gpu/vertexbuffer.h Wyświetl plik

@@ -214,20 +214,22 @@ public:
void DrawIndexedElements(MeshPrimitive type, int count, const short* skip = nullptr, short typeSize = 2);

void Unbind();
void SetStream(VertexBuffer *vb, ShaderAttrib attr1,
ShaderAttrib attr2 = ShaderAttrib(),
ShaderAttrib attr3 = ShaderAttrib(),
ShaderAttrib attr4 = ShaderAttrib(),
ShaderAttrib attr5 = ShaderAttrib(),
ShaderAttrib attr6 = ShaderAttrib(),
ShaderAttrib attr7 = ShaderAttrib(),
ShaderAttrib attr8 = ShaderAttrib(),
ShaderAttrib attr9 = ShaderAttrib(),
ShaderAttrib attr10 = ShaderAttrib(),
ShaderAttrib attr11 = ShaderAttrib(),
ShaderAttrib attr12 = ShaderAttrib());

void SetStream(VertexBuffer *vb, ShaderAttrib attribs[]);
void SetStream(std::shared_ptr<VertexBuffer> vb,
ShaderAttrib attr1,
ShaderAttrib attr2 = ShaderAttrib(),
ShaderAttrib attr3 = ShaderAttrib(),
ShaderAttrib attr4 = ShaderAttrib(),
ShaderAttrib attr5 = ShaderAttrib(),
ShaderAttrib attr6 = ShaderAttrib(),
ShaderAttrib attr7 = ShaderAttrib(),
ShaderAttrib attr8 = ShaderAttrib(),
ShaderAttrib attr9 = ShaderAttrib(),
ShaderAttrib attr10 = ShaderAttrib(),
ShaderAttrib attr11 = ShaderAttrib(),
ShaderAttrib attr12 = ShaderAttrib());

void SetStream(std::shared_ptr<VertexBuffer> vb,
ShaderAttrib attribs[]);

int GetStreamCount() const;



+ 4
- 8
src/lolimgui.cpp Wyświetl plik

@@ -103,7 +103,6 @@ LolImGui::~LolImGui()
Ticker::Unref(m_font);
m_font = nullptr;

Shader::Destroy(m_shader);
delete m_vdecl;

ImGui::DestroyContext();
@@ -295,10 +294,10 @@ void LolImGui::tick_draw(float seconds, Scene &scene)
{
super::tick_draw(seconds, scene);

scene.AddPrimitiveRenderer(this, new PrimitiveLolImGui());
scene.AddPrimitiveRenderer(this, std::make_shared<PrimitiveLolImGui>());
}

void PrimitiveLolImGui::Render(Scene& scene, PrimitiveSource* primitive)
void PrimitiveLolImGui::Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive)
{
UNUSED(scene, primitive);

@@ -383,12 +382,12 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data)
u8vec4 color;
};

VertexBuffer* vbo = new VertexBuffer(command_list.VtxBuffer.Size * sizeof(ImDrawVert));
auto vbo = std::make_shared<VertexBuffer>(command_list.VtxBuffer.Size * sizeof(ImDrawVert));
ImDrawVert *vert = (ImDrawVert *)vbo->Lock(0, 0);
memcpy(vert, command_list.VtxBuffer.Data, command_list.VtxBuffer.Size * sizeof(ImDrawVert));
vbo->Unlock();

IndexBuffer *ibo = new IndexBuffer(command_list.IdxBuffer.Size * sizeof(ImDrawIdx));
auto ibo = std::make_shared<IndexBuffer>(command_list.IdxBuffer.Size * sizeof(ImDrawIdx));
ImDrawIdx *indices = (ImDrawIdx *)ibo->Lock(0, 0);
memcpy(indices, command_list.IdxBuffer.Data, command_list.IdxBuffer.Size * sizeof(ImDrawIdx));
ibo->Unlock();
@@ -460,9 +459,6 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data)

m_vdecl->Unbind();
ibo->Unbind();

delete vbo;
delete ibo;
}

m_shader->Unbind();


+ 4
- 3
src/lolimgui.h Wyświetl plik

@@ -2,7 +2,7 @@
// Lol Engine
//
// Copyright © 2009—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
// © 2017—2018 Sam Hocevar <sam@hocevar.net>
// © 2017—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -15,6 +15,7 @@

#include <string>
#include <map>
#include <memory>

//
// The Imgui integration
@@ -186,7 +187,7 @@ protected:
//-------------------------------------------------------------------------
TextureImage* m_font = nullptr;
ShaderBuilder m_builder = ShaderBuilder("imgui_shader", "120");
Shader* m_shader = nullptr;
std::shared_ptr<Shader> m_shader = nullptr;
Uniform m_ortho;
Uniform m_texture;
array<ShaderAttrib> m_attribs;
@@ -205,7 +206,7 @@ class PrimitiveLolImGui : public PrimitiveRenderer
{
public:
PrimitiveLolImGui() { }
virtual void Render(Scene& scene, PrimitiveSource* primitive);
virtual void Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive);
};

//bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks);


+ 19
- 18
src/mesh/mesh.cpp Wyświetl plik

@@ -12,6 +12,7 @@

#include <lol/engine-internal.h>

#include <memory>
#include <cstring>
#include <cstdlib>

@@ -34,38 +35,38 @@ void Mesh::Render(Scene& scene, mat4 const &matrix)
{
//if (scene.HasPrimitiveRenderer(this) < m_submeshes.count())
{
for (int i = 0; i < m_submeshes.count(); ++i)
scene.AddPrimitiveRenderer(this, new PrimitiveMesh(m_submeshes[i], matrix));
for (auto submesh : m_submeshes)
scene.AddPrimitiveRenderer(this, std::make_shared<PrimitiveMesh>(submesh, matrix));
}
}

void Mesh::Render()
{
for (int i = 0; i < m_submeshes.count(); ++i)
m_submeshes[i]->Render();
for (auto submesh : m_submeshes)
submesh->Render();
}

void Mesh::SetMaterial(Shader *shader)
void Mesh::SetMaterial(std::shared_ptr<Shader> shader)
{
for (int i = 0; i < m_submeshes.count(); ++i)
m_submeshes[i]->SetShader(shader);
for (auto submesh : m_submeshes)
submesh->SetShader(shader);
}

/*
* SubMesh class
*/

SubMesh::SubMesh(Shader *shader, VertexDeclaration *vdecl)
SubMesh::SubMesh(std::shared_ptr<Shader> shader, std::shared_ptr<VertexDeclaration> vdecl)
: m_mesh_prim(MeshPrimitive::Triangles),
m_shader(shader),
m_vdecl(vdecl)
{
Ticker::Ref(m_shader);
Ticker::Ref(m_shader.get());
}

SubMesh::~SubMesh()
{
Ticker::Unref(m_shader);
Ticker::Unref(m_shader.get());
// TODO: cleanup
}

@@ -74,24 +75,24 @@ void SubMesh::SetMeshPrimitive(MeshPrimitive mesh_primitive)
m_mesh_prim = mesh_primitive;
}

void SubMesh::SetShader(Shader *shader)
void SubMesh::SetShader(std::shared_ptr<Shader> shader)
{
Ticker::Unref(m_shader);
Ticker::Unref(m_shader.get());
m_shader = shader;
Ticker::Ref(m_shader);
Ticker::Ref(m_shader.get());
}

Shader *SubMesh::GetShader()
std::shared_ptr<Shader> SubMesh::GetShader()
{
return m_shader;
}

void SubMesh::SetVertexDeclaration(VertexDeclaration *vdecl)
void SubMesh::SetVertexDeclaration(std::shared_ptr<VertexDeclaration> vdecl)
{
m_vdecl = vdecl;
}

void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo)
void SubMesh::SetVertexBuffer(int index, std::shared_ptr<VertexBuffer> vbo)
{
while (index >= m_vbos.count())
m_vbos.push(nullptr);
@@ -99,12 +100,12 @@ void SubMesh::SetVertexBuffer(int index, VertexBuffer* vbo)
m_vbos[index] = vbo;
}

void SubMesh::SetIndexBuffer(IndexBuffer* ibo)
void SubMesh::SetIndexBuffer(std::shared_ptr<IndexBuffer> ibo)
{
m_ibo = ibo;
}

void SubMesh::AddTexture(std::string const &name, Texture* texture)
void SubMesh::AddTexture(std::string const &name, std::shared_ptr<Texture> texture)
{
m_textures.push(name, texture);
}


+ 18
- 15
src/mesh/mesh.h Wyświetl plik

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -17,6 +17,9 @@
// --------------
//

#include <vector>
#include <memory>

#include <lol/gpu/vertexbuffer.h>
#include <lol/gpu/indexbuffer.h>

@@ -59,7 +62,7 @@ public:

/* FIXME: this should eventually take a “material” as argument, which
* may behave differently between submeshes. */
void SetMaterial(Shader *shader);
void SetMaterial(std::shared_ptr<Shader> shader);

//TODO: Not sure about the name
void Render(Scene& scene, mat4 const &matrix);
@@ -68,7 +71,7 @@ protected:
void Render();

public:
array<class SubMesh *> m_submeshes;
std::vector<std::shared_ptr<class SubMesh>> m_submeshes;
};

/*
@@ -85,27 +88,27 @@ class SubMesh
friend class Mesh;

public:
SubMesh(Shader *shader, VertexDeclaration* vdecl);
SubMesh(std::shared_ptr<Shader> shader, std::shared_ptr<VertexDeclaration> vdecl);
~SubMesh();

void SetMeshPrimitive(MeshPrimitive mesh_primitive);
void SetShader(Shader *shader);
Shader *GetShader();
void SetVertexDeclaration(VertexDeclaration *vdecl);
void SetVertexBuffer(int index, VertexBuffer* vbo);
void SetIndexBuffer(IndexBuffer* ibo);
void AddTexture(std::string const &name, Texture* texture);
void SetShader(std::shared_ptr<Shader> shader);
std::shared_ptr<Shader> GetShader();
void SetVertexDeclaration(std::shared_ptr<VertexDeclaration> vdecl);
void SetVertexBuffer(int index, std::shared_ptr<VertexBuffer> vbo);
void SetIndexBuffer(std::shared_ptr<IndexBuffer> ibo);
void AddTexture(std::string const &name, std::shared_ptr<Texture> texture);

protected:
void Render();

MeshPrimitive m_mesh_prim;
Shader *m_shader;
VertexDeclaration* m_vdecl;
array<VertexBuffer *> m_vbos;
IndexBuffer *m_ibo;
std::shared_ptr<Shader> m_shader;
std::shared_ptr<VertexDeclaration> m_vdecl;
array<std::shared_ptr<VertexBuffer>> m_vbos;
std::shared_ptr<IndexBuffer> m_ibo;

array<std::string, Texture*> m_textures;
array<std::string, std::shared_ptr<Texture>> m_textures;
};

} /* namespace lol */


+ 12
- 9
src/mesh/primitivemesh.cpp Wyświetl plik

@@ -1,15 +1,18 @@
//
// Lol Engine
// Lol Engine
//
// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. 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 the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#include <lol/engine-internal.h>

#include <memory>
#include <cstring>
#include <cstdlib>

@@ -20,7 +23,7 @@ namespace lol
* Primitive class
*/

PrimitiveMesh::PrimitiveMesh(SubMesh *submesh, mat4 const &matrix)
PrimitiveMesh::PrimitiveMesh(std::shared_ptr<SubMesh> submesh, mat4 const &matrix)
: m_submesh(submesh),
m_matrix(matrix)
{
@@ -30,12 +33,12 @@ PrimitiveMesh::~PrimitiveMesh()
{
}

void PrimitiveMesh::Render(Scene& scene, PrimitiveSource* primitive)
void PrimitiveMesh::Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive)
{
/* TODO: this should be the main entry for rendering of all
* primitives found in the scene graph. When we have one. */

Shader *shader = nullptr;
std::shared_ptr<Shader> shader;
ShaderUniform u_model, u_modelview, u_normalmat, uni_tex, uni_texsize;
ShaderAttrib a_pos, a_tex;



+ 11
- 9
src/mesh/primitivemesh.h Wyświetl plik

@@ -1,11 +1,13 @@
//
// Lol Engine
// Lol Engine
//
// Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. 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 the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#pragma once
@@ -23,12 +25,12 @@ class PrimitiveMesh : public PrimitiveRenderer
friend class Scene;

public:
PrimitiveMesh(SubMesh *submesh, mat4 const &matrix);
PrimitiveMesh(std::shared_ptr<SubMesh> submesh, mat4 const &matrix);
virtual ~PrimitiveMesh();
virtual void Render(Scene& scene, PrimitiveSource* primitive);
virtual void Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive);

private:
SubMesh *m_submesh;
std::shared_ptr<SubMesh> m_submesh;
mat4 m_matrix;
};



+ 43
- 65
src/scene.cpp Wyświetl plik

@@ -109,7 +109,7 @@ void SceneDisplay::Disable()

void PrimitiveSource::Render(Scene& scene) { UNUSED(scene); }

void PrimitiveRenderer::Render(Scene& scene, PrimitiveSource* primitive)
void PrimitiveRenderer::Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive)
{
UNUSED(scene);
UNUSED(primitive);
@@ -123,7 +123,7 @@ class SceneData
{
friend class Scene;

private:
public:
SceneData()
{
/* TODO: FIX THAT */
@@ -132,6 +132,7 @@ private:
m_used_id = m_used_id << 1;
}

private:
/* Mask ID */
/* TODO: Do a mask class that handles more than 64 slots */
static uint64_t m_used_id;
@@ -146,9 +147,9 @@ private:

struct postprocess
{
Shader *m_shader[2];
VertexBuffer *m_vbo = nullptr;
VertexDeclaration *m_vdecl = nullptr;
std::shared_ptr<Shader> m_shader[2];
std::shared_ptr<VertexBuffer> m_vbo;
std::shared_ptr<VertexDeclaration> m_vdecl;
ShaderUniform m_buffer_uni[2][3];
ShaderAttrib m_coord[2];
}
@@ -161,8 +162,8 @@ private:
* - Updated by entity
* - Marked Fire&Forget
* - Scene is destroyed */
std::map<uintptr_t, array<PrimitiveRenderer*>> m_prim_renderers;
static std::map<uintptr_t, array<PrimitiveSource*>> m_prim_sources;
std::map<uintptr_t, array<std::shared_ptr<PrimitiveRenderer>>> m_prim_renderers;
static std::map<uintptr_t, array<std::shared_ptr<PrimitiveSource>>> m_prim_sources;
static mutex m_prim_mutex;

Camera *m_default_cam;
@@ -175,8 +176,8 @@ private:
//vec4 m_color;
array<vec3, vec3, vec4, float, int, bool, bool> m_lines;
int /*m_mask,*/ m_debug_mask;
Shader *m_shader;
VertexDeclaration *m_vdecl;
std::shared_ptr<Shader> m_shader;
std::shared_ptr<VertexDeclaration> m_vdecl;
}
m_line_api;

@@ -188,16 +189,16 @@ private:
array<Tile> m_palettes;
array<Light *> m_lights;

Shader *m_shader;
Shader *m_palette_shader;
std::shared_ptr<Shader> m_shader;
std::shared_ptr<Shader> m_palette_shader;

VertexDeclaration *m_vdecl;
array<VertexBuffer *> m_bufs;
std::shared_ptr<VertexDeclaration> m_vdecl;
array<std::shared_ptr<VertexBuffer>> m_bufs;
}
m_tile_api;
};
uint64_t SceneData::m_used_id = 1;
std::map<uintptr_t, array<PrimitiveSource*>> SceneData::m_prim_sources;
std::map<uintptr_t, array<std::shared_ptr<PrimitiveSource>>> SceneData::m_prim_sources;
mutex SceneData::m_prim_mutex;

/*
@@ -206,8 +207,8 @@ mutex SceneData::m_prim_mutex;
Scene::Scene(ivec2 size)
: m_size(size),
m_wanted_size(size),
data(new SceneData()),
m_renderer(new Renderer(size))
data(std::make_unique<SceneData>()),
m_renderer(std::make_shared<Renderer>(size))
{
data->m_renderbuffer[0] = new Framebuffer(m_size);
data->m_renderbuffer[1] = new Framebuffer(m_size);
@@ -217,7 +218,7 @@ Scene::Scene(ivec2 size)
data->m_pp.m_shader[1] = Shader::Create(LOLFX_RESOURCE_NAME(gpu_postprocess));
data->m_pp.m_coord[0] = data->m_pp.m_shader[0]->GetAttribLocation(VertexUsage::Position, 0);
data->m_pp.m_coord[1] = data->m_pp.m_shader[1]->GetAttribLocation(VertexUsage::Position, 0);
data->m_pp.m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
data->m_pp.m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
data->m_pp.m_buffer_uni[0][0] = data->m_pp.m_shader[0]->GetUniformLocation("u_buffer");
data->m_pp.m_buffer_uni[1][0] = data->m_pp.m_shader[1]->GetUniformLocation("u_buffer");
data->m_pp.m_buffer_uni[1][1] = data->m_pp.m_shader[1]->GetUniformLocation("u_prev_buffer");
@@ -226,7 +227,7 @@ Scene::Scene(ivec2 size)
array<vec2> quad { vec2( 1.0, 1.0), vec2(-1.0, -1.0), vec2( 1.0, -1.0),
vec2(-1.0, -1.0), vec2( 1.0, 1.0), vec2(-1.0, 1.0), };

data->m_pp.m_vbo = new VertexBuffer(quad.bytes());
data->m_pp.m_vbo = std::make_shared<VertexBuffer>(quad.bytes());
void *vertices = data->m_pp.m_vbo->Lock(0, 0);
memcpy(vertices, quad.data(), quad.bytes());
data->m_pp.m_vbo->Unlock();
@@ -240,11 +241,11 @@ Scene::Scene(ivec2 size)
data->m_tile_api.m_cam = -1;
data->m_tile_api.m_shader = 0;
data->m_tile_api.m_palette_shader = 0;
data->m_tile_api.m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
VertexStream<vec2>(VertexUsage::TexCoord));
data->m_tile_api.m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec3>(VertexUsage::Position),
VertexStream<vec2>(VertexUsage::TexCoord));

data->m_line_api.m_shader = 0;
data->m_line_api.m_vdecl = new VertexDeclaration(VertexStream<vec4,vec4>(VertexUsage::Position, VertexUsage::Color));
data->m_line_api.m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec4,vec4>(VertexUsage::Position, VertexUsage::Color));

data->m_line_api.m_debug_mask = 1;
}
@@ -264,11 +265,6 @@ Scene::~Scene()
/* FIXME: also, make sure we do not add code to Reset() that will
* reallocate stuff */
Reset();

delete data->m_line_api.m_vdecl;
delete data->m_tile_api.m_vdecl;
delete data;
delete m_renderer;
}

//-----------------------------------------------------------------------------
@@ -381,10 +377,7 @@ void Scene::Reset()
ReleasePrimitiveRenderer(idx--, key);
}

for (int i = 0; i < data->m_tile_api.m_bufs.count(); i++)
delete data->m_tile_api.m_bufs[i];
data->m_tile_api.m_bufs.clear();

data->m_tile_api.m_lights.clear();
}

@@ -400,7 +393,7 @@ int Scene::HasPrimitiveSource(uintptr_t key)
return count;
}

int Scene::AddPrimitiveSource(uintptr_t key, PrimitiveSource* source)
int Scene::AddPrimitiveSource(uintptr_t key, std::shared_ptr<PrimitiveSource> source)
{
int count;
SceneData::m_prim_mutex.lock();
@@ -412,12 +405,14 @@ int Scene::AddPrimitiveSource(uintptr_t key, PrimitiveSource* source)
return count;
}

void Scene::SetPrimitiveSource(int index, uintptr_t key, PrimitiveSource* source)
void Scene::SetPrimitiveSource(int index, uintptr_t key, std::shared_ptr<PrimitiveSource> source)
{
ASSERT(source);
ASSERT(index >= 0);

PrimitiveSource* old = nullptr;
// Keep reference to old source until AFTER we release the lock
std::shared_ptr<PrimitiveSource> old;

SceneData::m_prim_mutex.lock();
{
if (index < SceneData::m_prim_sources[key].count())
@@ -427,14 +422,11 @@ void Scene::SetPrimitiveSource(int index, uintptr_t key, PrimitiveSource* source
SceneData::m_prim_sources[key][index] = source;
}
SceneData::m_prim_mutex.unlock();

// Delete old AFTER having released the lock
delete old;
}

void Scene::ReleasePrimitiveSource(int index, uintptr_t key)
{
PrimitiveSource* old = nullptr;
std::shared_ptr<PrimitiveSource> old;
SceneData::m_prim_mutex.lock();
{
ASSERT(0 <= index && index < SceneData::m_prim_sources[key].count());
@@ -442,26 +434,21 @@ void Scene::ReleasePrimitiveSource(int index, uintptr_t key)
SceneData::m_prim_sources[key].remove(index);
}
SceneData::m_prim_mutex.unlock();

// Delete old AFTER having released the lock
delete old;
}

void Scene::ReleaseAllPrimitiveSources(uintptr_t key)
{
array<PrimitiveSource*> oldies;
// Delete oldies AFTER having released the lock
array<std::shared_ptr<PrimitiveSource>> oldies;

SceneData::m_prim_mutex.lock();
{
oldies.reserve(SceneData::m_prim_sources[key].count());
for (PrimitiveSource* source : SceneData::m_prim_sources[key])
for (auto source : SceneData::m_prim_sources[key])
oldies << source;
SceneData::m_prim_sources[key].clear();
}
SceneData::m_prim_mutex.unlock();

// Delete oldies AFTER having released the lock
for (PrimitiveSource* old : oldies)
delete old;
}

//---- Primitive renderer stuff -----------------------------------------------
@@ -470,23 +457,18 @@ int Scene::HasPrimitiveRenderer(uintptr_t key)
return data->m_prim_renderers[key].count();
}

void Scene::AddPrimitiveRenderer(uintptr_t key, PrimitiveRenderer* renderer)
void Scene::AddPrimitiveRenderer(uintptr_t key, std::shared_ptr<PrimitiveRenderer> renderer)
{
renderer->m_fire_and_forget = true;
data->m_prim_renderers[key].push(renderer);
}

void Scene::SetPrimitiveRenderer(int index, uintptr_t key, PrimitiveRenderer* renderer)
void Scene::SetPrimitiveRenderer(int index, uintptr_t key, std::shared_ptr<PrimitiveRenderer> renderer)
{
ASSERT(renderer);
ASSERT(index >= 0);

if (index < data->m_prim_renderers[key].count())
{
ASSERT(data->m_prim_renderers[key][index]);
delete data->m_prim_renderers[key][index];
}
else
if (index >= data->m_prim_renderers[key].count())
data->m_prim_renderers[key].resize(index + 1);
data->m_prim_renderers[key][index] = renderer;
}
@@ -495,17 +477,13 @@ void Scene::ReleasePrimitiveRenderer(int index, uintptr_t key)
{
ASSERT(0 <= index && index < data->m_prim_renderers[key].count());

delete data->m_prim_renderers[key][index];
data->m_prim_renderers[key].remove(index);
}

void Scene::ReleaseAllPrimitiveRenderers(uintptr_t key)
{
for (PrimitiveRenderer*& renderer : data->m_prim_renderers[key])
{
delete renderer;
for (auto& renderer : data->m_prim_renderers[key])
renderer = nullptr;
}
}

//-----------------------------------------------------------------------------
@@ -719,7 +697,8 @@ void Scene::render_primitives()
for (int idx = 0; idx < data->m_prim_renderers[key].count(); ++idx)
{
/* TODO: Not sure if thread compliant */
data->m_prim_renderers[key][idx]->Render(*this, idx < SceneData::m_prim_sources[key].count() ? SceneData::m_prim_sources[key][idx] : nullptr);
if (idx < SceneData::m_prim_sources[key].count())
data->m_prim_renderers[key][idx]->Render(*this, SceneData::m_prim_sources[key][idx]);
}
}
}
@@ -755,8 +734,8 @@ void Scene::render_tiles() // XXX: rename to Blit()

for (int p = 0; p < 2; p++)
{
Shader *shader = (p == 0) ? data->m_tile_api.m_shader : data->m_tile_api.m_palette_shader;
array<Tile>& tiles = (p == 0) ? data->m_tile_api.m_tiles : data->m_tile_api.m_palettes;
auto shader = (p == 0) ? data->m_tile_api.m_shader : data->m_tile_api.m_palette_shader;
auto &tiles = (p == 0) ? data->m_tile_api.m_tiles : data->m_tile_api.m_palettes;

if (tiles.count() == 0)
continue;
@@ -787,9 +766,9 @@ void Scene::render_tiles() // XXX: rename to Blit()
break;

/* Create a vertex array object */
VertexBuffer *vb1 = new VertexBuffer(6 * (n - i) * sizeof(vec3));
auto vb1 = std::make_shared<VertexBuffer>(6 * (n - i) * sizeof(vec3));
vec3 *vertex = (vec3 *)vb1->Lock(0, 0);
VertexBuffer *vb2 = new VertexBuffer(6 * (n - i) * sizeof(vec2));
auto vb2 = std::make_shared<VertexBuffer>(6 * (n - i) * sizeof(vec2));
vec2 *texture = (vec2 *)vb2->Lock(0, 0);

data->m_tile_api.m_bufs.push(vb1);
@@ -889,7 +868,7 @@ void Scene::render_lines(float seconds)
linecount--;
}
}
VertexBuffer *vb = new VertexBuffer(buff.bytes());
auto vb = std::make_shared<VertexBuffer>(buff.bytes());
float *vertex = (float *)vb->Lock(0, 0);
memcpy(vertex, buff.data(), buff.bytes());
vb->Unlock();
@@ -915,7 +894,6 @@ void Scene::render_lines(float seconds)
data->m_line_api.m_shader->Unbind();

//data->m_line_api.m_lines.clear();
delete vb;
}

} /* namespace lol */


+ 14
- 13
src/scene.h Wyświetl plik

@@ -18,7 +18,8 @@
// ---------------
//

#include <stdint.h>
#include <memory>
#include <cstdint>

#include "tileset.h"
#include "light.h"
@@ -51,7 +52,7 @@ class PrimitiveRenderer
public:
PrimitiveRenderer() { }
virtual ~PrimitiveRenderer() { }
virtual void Render(Scene& scene, PrimitiveSource* primitive);
virtual void Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive);

private:
bool m_fire_and_forget = false;
@@ -129,15 +130,15 @@ public:

void Reset();

Renderer *get_renderer() { return m_renderer; }
std::shared_ptr<Renderer> get_renderer() { return m_renderer; }

/* ============================== */
# define _KEY_IDX (uintptr_t)key /* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */
/* ============================== */
private:
int HasPrimitiveSource(uintptr_t key);
int AddPrimitiveSource(uintptr_t key, class PrimitiveSource* source);
void SetPrimitiveSource(int index, uintptr_t key, class PrimitiveSource* source);
int AddPrimitiveSource(uintptr_t key, std::shared_ptr<class PrimitiveSource> source);
void SetPrimitiveSource(int index, uintptr_t key, std::shared_ptr<class PrimitiveSource> source);
void ReleasePrimitiveSource(int index, uintptr_t key);
void ReleaseAllPrimitiveSources(uintptr_t key);
public:
@@ -152,7 +153,7 @@ public:
/* Add a primitive sources linked to the given entity
* Returns the slot number */
template <typename T>
int AddPrimitiveSource(T* key, class PrimitiveSource* source)
int AddPrimitiveSource(T* key, std::shared_ptr<class PrimitiveSource> source)
{
ASSERT(key);
return AddPrimitiveSource(_KEY_IDX, source);
@@ -161,7 +162,7 @@ public:
* Deletes the old one
* The slot is kept even if source == nullptr */
template <typename T>
void SetPrimitiveSource(int index, T* key, class PrimitiveSource* source)
void SetPrimitiveSource(int index, T* key, std::shared_ptr<class PrimitiveSource> source)
{
ASSERT(key);
SetPrimitiveSource(index, _KEY_IDX, source);
@@ -184,8 +185,8 @@ public:
private:
int HasPrimitiveRenderer(uintptr_t key);

void AddPrimitiveRenderer(uintptr_t key, class PrimitiveRenderer* renderer);
void SetPrimitiveRenderer(int index, uintptr_t key, class PrimitiveRenderer* renderer);
void AddPrimitiveRenderer(uintptr_t key, std::shared_ptr<class PrimitiveRenderer> renderer);
void SetPrimitiveRenderer(int index, uintptr_t key, std::shared_ptr<class PrimitiveRenderer> renderer);
void ReleasePrimitiveRenderer(int index, uintptr_t key);
void ReleaseAllPrimitiveRenderers(uintptr_t key);
public:
@@ -201,7 +202,7 @@ public:
* The primitive is considered as Fire&Forget and
* will be destroyed at the end of the frame */
template <typename T>
void AddPrimitiveRenderer(T* key, class PrimitiveRenderer* renderer)
void AddPrimitiveRenderer(T* key, std::shared_ptr<class PrimitiveRenderer> renderer)
{
ASSERT(key);
AddPrimitiveRenderer(_KEY_IDX, renderer);
@@ -210,7 +211,7 @@ public:
* Deletes the old one
* Will assert if renderer == nullptr */
template <typename T>
void SetPrimitiveRenderer(int index, T* key, class PrimitiveRenderer* renderer)
void SetPrimitiveRenderer(int index, T* key, std::shared_ptr<class PrimitiveRenderer> renderer)
{
ASSERT(key && renderer);
SetPrimitiveRenderer(index, _KEY_IDX, renderer);
@@ -263,8 +264,8 @@ private:

ivec2 m_size, m_wanted_size;

SceneData *data;
Renderer *m_renderer;
std::unique_ptr<SceneData> data;
std::shared_ptr<Renderer> m_renderer;
};

} /* namespace lol */


Ładowanie…
Anuluj
Zapisz