Bläddra i källkod

render: simplify Renderer code

The scene now owns its renderer, so there is no need to keep a list
of existing renderers and to call Renderer::Get().
legacy
Sam Hocevar 5 år sedan
förälder
incheckning
beeded3d85
21 ändrade filer med 212 tillägg och 237 borttagningar
  1. +2
    -2
      doc/samples/btphystest.cpp
  2. +3
    -3
      doc/samples/meshviewer/meshviewer.cpp
  3. +2
    -2
      doc/tutorial/02_cube.cpp
  4. +2
    -2
      doc/tutorial/05_easymesh.cpp
  5. +1
    -1
      doc/tutorial/06_sprite.cpp
  6. +2
    -2
      doc/tutorial/07_input.cpp
  7. +4
    -4
      doc/tutorial/08_fbo.cpp
  8. +15
    -14
      doc/tutorial/12_voronoi.cpp
  9. +3
    -3
      src/camera.cpp
  10. +11
    -9
      src/debug/lines.cpp
  11. +2
    -2
      src/engine/ticker.cpp
  12. +6
    -4
      src/gpu/framebuffer.cpp
  13. +112
    -109
      src/gpu/rendercontext.cpp
  14. +1
    -34
      src/gpu/renderer.cpp
  15. +12
    -7
      src/lol/gpu/rendercontext.h
  16. +10
    -16
      src/lol/gpu/renderer.h
  17. +2
    -2
      src/lolimgui.cpp
  18. +2
    -2
      src/messageservice.cpp
  19. +13
    -10
      src/scene.cpp
  20. +5
    -1
      src/scene.h
  21. +2
    -8
      src/video.cpp

+ 2
- 2
doc/samples/btphystest.cpp Visa fil

@@ -2,7 +2,7 @@
// Lol Engine — BtPhys tutorial
//
// Copyright © 2009—2015 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
@@ -404,7 +404,7 @@ void BtPhysTest::tick_game(float seconds)

if (!m_init_status)
{
if (Renderer::GetCount())
if (Scene::GetCount())
InitApp();
return;
}


+ 3
- 3
doc/samples/meshviewer/meshviewer.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine — EasyMesh tutorial
//
// Copyright © 2011—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
// © 2012—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -946,7 +946,7 @@ void MeshViewer::Draw(float seconds, Scene &scene)
m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
#endif //!HAS_WEB && WITH_TEXTURE

Renderer::Get()->SetClearColor(m_ssetup->m_clear_color);
scene.get_renderer()->SetClearColor(m_ssetup->m_clear_color);

for (int i = 0; i < m_gizmos.count(); ++i)
{
@@ -1057,7 +1057,7 @@ void MeshViewer::Draw(float seconds, Scene &scene)
mat4 new_proj = mat_obj_offset * mat_count_offset * mat_align * mat_count_scale * save_proj;
m_camera->SetProjection(new_proj);
m_meshes[i].m1->Render(scene, m_mat);
Renderer::Get()->Clear(ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Depth);
}
m_camera->SetProjection(save_proj);
#else


+ 2
- 2
doc/tutorial/02_cube.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine — Cube tutorial
//
// Copyright © 2011—2015 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
@@ -130,7 +130,7 @@ public:
m_ready = true;
}

Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));

m_shader->Bind();
m_vdecl->SetStream(m_vbo, m_coord, m_color);


+ 2
- 2
doc/tutorial/05_easymesh.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine — EasyMesh tutorial
//
// Copyright © 2011—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2011—2019 Sam Hocevar <sam@hocevar.net>
// © 2012—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -129,7 +129,7 @@ public:

if (!m_ready)
{
Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));

/* Upload vertex data to GPU */
for (int i = 0; i < m_gears.count(); i++)


+ 1
- 1
doc/tutorial/06_sprite.cpp Visa fil

@@ -72,7 +72,7 @@ public:

if (!m_ready)
{
Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
m_ready = true;
}



+ 2
- 2
doc/tutorial/07_input.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine — Input tutorial
//
// Copyright © 2011—2015 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
@@ -191,7 +191,7 @@ public:
m_ready = true;
}

Renderer::Get()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
scene.get_renderer()->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));

m_shader->Bind();
m_vdecl->SetStream(m_vbo, m_coord, m_color);


+ 4
- 4
doc/tutorial/08_fbo.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine — Framebuffer Object 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
@@ -76,10 +76,10 @@ public:
m_fbo->Bind();

{
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}

m_fbo->Unbind();
@@ -90,7 +90,7 @@ public:
}

/* FIXME: we should just disable depth test in the shader */
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetDepthFunc(DepthFunc::Disabled);

/* FIXME: this no longer works because we don’t restore the


+ 15
- 14
doc/tutorial/12_voronoi.cpp Visa fil

@@ -1,7 +1,8 @@
//
// Lol Engine — Voronoi diagram tutorial
//
// Copyright © 2011—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
// Copyright © 2015—2019 Sam Hocevar <sam@hocevar.net>
// © 2011—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
@@ -143,10 +144,10 @@ public:

m_fbos.last().m1->Bind();
{
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
m_fbos.last().m1->Unbind();
}
@@ -154,10 +155,10 @@ public:
temp_buffer = new Framebuffer(Video::GetSize());
temp_buffer->Bind();
{
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
temp_buffer->Unbind();

@@ -218,10 +219,10 @@ public:

temp_buffer->Bind();
{
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
temp_buffer->Unbind();

@@ -248,10 +249,10 @@ public:

m_fbos[f].m1->Bind();
{
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
m_fbos[f].m1->Unbind();

@@ -274,7 +275,7 @@ public:

dst_buf->Bind();
/* FIXME: we should just disable depth test in the shader */
Renderer::Get()->Clear(ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Depth);
m_fbos[f].m2->Bind();

int i = 0;
@@ -293,7 +294,7 @@ public:
}
}

Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);

//FRAME BUFFER DRAW
m_timer -= seconds;
@@ -302,10 +303,10 @@ public:
//m_timer = 1.0f;
m_fbos[m_cur_fbo].m1->Bind();
{
RenderContext rc;
RenderContext rc(scene.get_renderer());
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
}
m_fbos[m_cur_fbo].m1->Unbind();

@@ -336,7 +337,7 @@ public:

dst_buf->Bind();
/* FIXME: we should just disable depth test in the shader */
Renderer::Get()->Clear(ClearMask::Depth);
scene.get_renderer()->Clear(ClearMask::Depth);
shader->Bind();

int i = 0;


+ 3
- 3
src/camera.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
// © 2009—2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -30,8 +30,8 @@ Camera::Camera()
m_gamegroup = GAMEGROUP_CAMERA;
m_drawgroup = DRAWGROUP_CAMERA;

//Arbitrary values when g_renderer is not ready.
ivec2 screen_size = (Renderer::GetCount()) ? (Video::GetSize()) : (ivec2(800, 600));
//Arbitrary values when scene renderer is not ready.
ivec2 screen_size = (Scene::GetCount()) ? (Video::GetSize()) : (ivec2(800, 600));
m_fov = radians(45.f);
m_near = -1000.f;
m_far = 1000.f;


+ 11
- 9
src/debug/lines.cpp Visa fil

@@ -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.
//

#include <lol/engine-internal.h>
@@ -227,14 +229,14 @@ void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, DCD data)
}
void Debug::DrawBox(vec2 a, float s, mat2 transform, DCD data)
{
vec2 b = s * vec2(1.f, Renderer::Get()->GetXYRatio());
vec2 b = s * vec2(1.f, Scene::GetScene(0).get_renderer()->GetXYRatio());
Debug::DrawBox(a - b, a + b, transform, data);
}

//-- CIRCLE -------------------------------------------------------------------
void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, Renderer::Get()->GetXYRatio()), DC::GetGlobalData()); }
void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, Scene::GetScene(0).get_renderer()->GetXYRatio()), DC::GetGlobalData()); }
void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, DC::GetGlobalData()); }
void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, Renderer::Get()->GetXYRatio()), DC::GetGlobalData()); }
void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, Scene::GetScene(0).get_renderer()->GetXYRatio()), DC::GetGlobalData()); }
void Debug::DrawCircle(vec3 a, vec3 x, vec3 y) { Debug::DrawCircle(a, x, y, DC::GetGlobalData()); }
void Debug::DrawCircle(vec2 a, vec2 x, vec2 y) { Debug::DrawCircle(a, x, y, DC::GetGlobalData()); }
void Debug::DrawCircle(vec3 a, vec3 n, DCD data)


+ 2
- 2
src/engine/ticker.cpp Visa fil

@@ -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
@@ -430,7 +430,7 @@ void TickerData::DrawThreadTick()

/* Enable display */
scene.EnableDisplay();
Renderer::Get(idx)->Clear(ClearMask::All);
scene.get_renderer()->Clear(ClearMask::All);

scene.pre_render(data->deltatime);



+ 6
- 4
src/gpu/framebuffer.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2017 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
@@ -373,8 +373,9 @@ 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. */
m_data->m_saved_viewport = Renderer::Get()->GetViewport();
Renderer::Get()->SetViewport(ibox2(ivec2::zero, m_data->m_size));
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;
}

@@ -388,7 +389,8 @@ void Framebuffer::Unbind()
glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
#endif

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



+ 112
- 109
src/gpu/rendercontext.cpp Visa fil

@@ -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.
//

#include <lol/engine-internal.h>
@@ -15,32 +17,32 @@
namespace lol
{

template<typename T> class TrackedState
template<typename T> class tracked_var
{
public:
inline TrackedState()
: m_changed(false)
inline tracked_var()
: m_dirty(false)
{}

inline void TrackValue(T const &value)
inline void set(T const &value)
{
m_value = value;
m_changed = true;
m_dirty = true;
}

inline bool HasChanged()
inline bool is_dirty()
{
return m_changed;
return m_dirty;
}

inline T GetValue()
inline T get()
{
return m_value;
}

private:
T m_value;
bool m_changed;
bool m_dirty;
};

class RenderContextData
@@ -48,242 +50,243 @@ class RenderContextData
friend class RenderContext;

private:
TrackedState<ibox2> m_viewport;
TrackedState<vec4> m_clear_color;
TrackedState<float> m_clear_depth;
TrackedState<AlphaFunc> m_alpha_func;
TrackedState<float> m_alpha_value;
TrackedState<BlendEquation> m_blend_rgb, m_blend_alpha;
TrackedState<BlendFunc> m_blend_src, m_blend_dst;
TrackedState<DepthFunc> m_depth_func;
TrackedState<DepthMask> m_depth_mask;
TrackedState<CullMode> m_cull_mode;
TrackedState<PolygonMode> m_polygon_mode;
TrackedState<ScissorMode> m_scissor_mode;
TrackedState<vec4> m_scissor_rect;
tracked_var<ibox2> m_viewport;
tracked_var<vec4> m_clear_color;
tracked_var<float> m_clear_depth;
tracked_var<AlphaFunc> m_alpha_func;
tracked_var<float> m_alpha_value;
tracked_var<BlendEquation> m_blend_rgb, m_blend_alpha;
tracked_var<BlendFunc> m_blend_src, m_blend_dst;
tracked_var<DepthFunc> m_depth_func;
tracked_var<DepthMask> m_depth_mask;
tracked_var<CullMode> m_cull_mode;
tracked_var<PolygonMode> m_polygon_mode;
tracked_var<ScissorMode> m_scissor_mode;
tracked_var<vec4> m_scissor_rect;
};

/*
* Public RenderContext class
*/

RenderContext::RenderContext()
: m_data(new RenderContextData())
RenderContext::RenderContext(Renderer *renderer)
: m_renderer(renderer),
m_data(new RenderContextData())
{
}

RenderContext::~RenderContext()
{
if (m_data->m_viewport.HasChanged())
Renderer::Get()->SetViewport(m_data->m_viewport.GetValue());
if (m_data->m_viewport.is_dirty())
m_renderer->SetViewport(m_data->m_viewport.get());

if (m_data->m_clear_color.HasChanged())
Renderer::Get()->SetClearColor(m_data->m_clear_color.GetValue());
if (m_data->m_clear_color.is_dirty())
m_renderer->SetClearColor(m_data->m_clear_color.get());

if (m_data->m_clear_depth.HasChanged())
Renderer::Get()->SetClearDepth(m_data->m_clear_depth.GetValue());
if (m_data->m_clear_depth.is_dirty())
m_renderer->SetClearDepth(m_data->m_clear_depth.get());

if (m_data->m_alpha_func.HasChanged())
Renderer::Get()->SetAlphaFunc(m_data->m_alpha_func.GetValue(),
m_data->m_alpha_value.GetValue());
if (m_data->m_alpha_func.is_dirty())
m_renderer->SetAlphaFunc(m_data->m_alpha_func.get(),
m_data->m_alpha_value.get());

if (m_data->m_blend_src.HasChanged())
Renderer::Get()->SetBlendFunc(m_data->m_blend_src.GetValue(),
m_data->m_blend_dst.GetValue());
if (m_data->m_blend_src.is_dirty())
m_renderer->SetBlendFunc(m_data->m_blend_src.get(),
m_data->m_blend_dst.get());

if (m_data->m_depth_func.HasChanged())
Renderer::Get()->SetDepthFunc(m_data->m_depth_func.GetValue());
if (m_data->m_depth_func.is_dirty())
m_renderer->SetDepthFunc(m_data->m_depth_func.get());

if (m_data->m_depth_mask.HasChanged())
Renderer::Get()->SetDepthMask(m_data->m_depth_mask.GetValue());
if (m_data->m_depth_mask.is_dirty())
m_renderer->SetDepthMask(m_data->m_depth_mask.get());

if (m_data->m_cull_mode.HasChanged())
Renderer::Get()->SetCullMode(m_data->m_cull_mode.GetValue());
if (m_data->m_cull_mode.is_dirty())
m_renderer->SetCullMode(m_data->m_cull_mode.get());

if (m_data->m_polygon_mode.HasChanged())
Renderer::Get()->SetPolygonMode(m_data->m_polygon_mode.GetValue());
if (m_data->m_polygon_mode.is_dirty())
m_renderer->SetPolygonMode(m_data->m_polygon_mode.get());

if (m_data->m_scissor_mode.HasChanged())
Renderer::Get()->SetScissorMode(m_data->m_scissor_mode.GetValue());
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)
{
if (!m_data->m_viewport.HasChanged())
m_data->m_viewport.TrackValue(Renderer::Get()->GetViewport());
if (!m_data->m_viewport.is_dirty())
m_data->m_viewport.set(m_renderer->GetViewport());

Renderer::Get()->SetViewport(viewport);
m_renderer->SetViewport(viewport);
}

ibox2 RenderContext::GetViewport()
{
return Renderer::Get()->GetViewport();
return m_renderer->GetViewport();
}

void RenderContext::SetClearColor(vec4 color)
{
if (!m_data->m_clear_color.HasChanged())
m_data->m_clear_color.TrackValue(Renderer::Get()->GetClearColor());
if (!m_data->m_clear_color.is_dirty())
m_data->m_clear_color.set(m_renderer->GetClearColor());

Renderer::Get()->SetClearColor(color);
m_renderer->SetClearColor(color);
}

vec4 RenderContext::GetClearColor()
{
return Renderer::Get()->GetClearColor();
return m_renderer->GetClearColor();
}

void RenderContext::SetClearDepth(float depth)
{
if (!m_data->m_clear_depth.HasChanged())
m_data->m_clear_depth.TrackValue(Renderer::Get()->GetClearDepth());
if (!m_data->m_clear_depth.is_dirty())
m_data->m_clear_depth.set(m_renderer->GetClearDepth());

Renderer::Get()->SetClearDepth(depth);
m_renderer->SetClearDepth(depth);
}

float RenderContext::GetClearDepth()
{
return Renderer::Get()->GetClearDepth();
return m_renderer->GetClearDepth();
}

void RenderContext::SetAlphaFunc(AlphaFunc func, float alpha)
{
if (!m_data->m_alpha_func.HasChanged())
m_data->m_alpha_func.TrackValue(Renderer::Get()->GetAlphaFunc());
if (!m_data->m_alpha_value.HasChanged())
m_data->m_alpha_value.TrackValue(Renderer::Get()->GetAlphaValue());
if (!m_data->m_alpha_func.is_dirty())
m_data->m_alpha_func.set(m_renderer->GetAlphaFunc());
if (!m_data->m_alpha_value.is_dirty())
m_data->m_alpha_value.set(m_renderer->GetAlphaValue());

Renderer::Get()->SetAlphaFunc(func, alpha);
m_renderer->SetAlphaFunc(func, alpha);
}

AlphaFunc RenderContext::GetAlphaFunc()
{
return Renderer::Get()->GetAlphaFunc();
return m_renderer->GetAlphaFunc();
}

float RenderContext::GetAlphaValue()
{
return Renderer::Get()->GetAlphaValue();
return m_renderer->GetAlphaValue();
}

void RenderContext::SetBlendEquation(BlendEquation rgb, BlendEquation alpha)
{
if (!m_data->m_blend_rgb.HasChanged())
m_data->m_blend_rgb.TrackValue(Renderer::Get()->GetBlendEquationRgb());
if (!m_data->m_blend_alpha.HasChanged())
m_data->m_blend_alpha.TrackValue(Renderer::Get()->GetBlendEquationAlpha());
if (!m_data->m_blend_rgb.is_dirty())
m_data->m_blend_rgb.set(m_renderer->GetBlendEquationRgb());
if (!m_data->m_blend_alpha.is_dirty())
m_data->m_blend_alpha.set(m_renderer->GetBlendEquationAlpha());

Renderer::Get()->SetBlendEquation(rgb, alpha);
m_renderer->SetBlendEquation(rgb, alpha);
}

BlendEquation RenderContext::GetBlendEquationRgb()
{
return Renderer::Get()->GetBlendEquationRgb();
return m_renderer->GetBlendEquationRgb();
}

BlendEquation RenderContext::GetBlendEquationAlpha()
{
return Renderer::Get()->GetBlendEquationAlpha();
return m_renderer->GetBlendEquationAlpha();
}

void RenderContext::SetBlendFunc(BlendFunc src, BlendFunc dst)
{
if (!m_data->m_blend_src.HasChanged())
m_data->m_blend_src.TrackValue(Renderer::Get()->GetBlendFuncSrc());
if (!m_data->m_blend_dst.HasChanged())
m_data->m_blend_dst.TrackValue(Renderer::Get()->GetBlendFuncDst());
if (!m_data->m_blend_src.is_dirty())
m_data->m_blend_src.set(m_renderer->GetBlendFuncSrc());
if (!m_data->m_blend_dst.is_dirty())
m_data->m_blend_dst.set(m_renderer->GetBlendFuncDst());

Renderer::Get()->SetBlendFunc(src, dst);
m_renderer->SetBlendFunc(src, dst);
}

BlendFunc RenderContext::GetBlendFuncSrc()
{
return Renderer::Get()->GetBlendFuncSrc();
return m_renderer->GetBlendFuncSrc();
}

BlendFunc RenderContext::GetBlendFuncDst()
{
return Renderer::Get()->GetBlendFuncDst();
return m_renderer->GetBlendFuncDst();
}

void RenderContext::SetDepthFunc(DepthFunc func)
{
if (!m_data->m_depth_func.HasChanged())
m_data->m_depth_func.TrackValue(Renderer::Get()->GetDepthFunc());
if (!m_data->m_depth_func.is_dirty())
m_data->m_depth_func.set(m_renderer->GetDepthFunc());

Renderer::Get()->SetDepthFunc(func);
m_renderer->SetDepthFunc(func);
}

DepthFunc RenderContext::GetDepthFunc()
{
return Renderer::Get()->GetDepthFunc();
return m_renderer->GetDepthFunc();
}

void RenderContext::SetDepthMask(DepthMask mask)
{
if (!m_data->m_depth_mask.HasChanged())
m_data->m_depth_mask.TrackValue(Renderer::Get()->GetDepthMask());
if (!m_data->m_depth_mask.is_dirty())
m_data->m_depth_mask.set(m_renderer->GetDepthMask());

Renderer::Get()->SetDepthMask(mask);
m_renderer->SetDepthMask(mask);
}

DepthMask RenderContext::GetDepthMask()
{
return Renderer::Get()->GetDepthMask();
return m_renderer->GetDepthMask();
}

void RenderContext::SetCullMode(CullMode mode)
{
if (!m_data->m_cull_mode.HasChanged())
m_data->m_cull_mode.TrackValue(Renderer::Get()->GetCullMode());
if (!m_data->m_cull_mode.is_dirty())
m_data->m_cull_mode.set(m_renderer->GetCullMode());

Renderer::Get()->SetCullMode(mode);
m_renderer->SetCullMode(mode);
}

CullMode RenderContext::GetCullMode()
{
return Renderer::Get()->GetCullMode();
return m_renderer->GetCullMode();
}

void RenderContext::SetPolygonMode(PolygonMode mode)
{
if (!m_data->m_polygon_mode.HasChanged())
m_data->m_polygon_mode.TrackValue(Renderer::Get()->GetPolygonMode());
if (!m_data->m_polygon_mode.is_dirty())
m_data->m_polygon_mode.set(m_renderer->GetPolygonMode());

Renderer::Get()->SetPolygonMode(mode);
m_renderer->SetPolygonMode(mode);
}

PolygonMode RenderContext::GetPolygonMode()
{
return Renderer::Get()->GetPolygonMode();
return m_renderer->GetPolygonMode();
}

void RenderContext::SetScissorMode(ScissorMode mode)
{
if (!m_data->m_scissor_mode.HasChanged())
m_data->m_scissor_mode.TrackValue(Renderer::Get()->GetScissorMode());
if (!m_data->m_scissor_mode.is_dirty())
m_data->m_scissor_mode.set(m_renderer->GetScissorMode());

Renderer::Get()->SetScissorMode(mode);
m_renderer->SetScissorMode(mode);
}

void RenderContext::SetScissorRect(vec4 rect)
{
if (!m_data->m_scissor_rect.HasChanged())
m_data->m_scissor_rect.TrackValue(Renderer::Get()->GetScissorRect());
if (!m_data->m_scissor_rect.is_dirty())
m_data->m_scissor_rect.set(m_renderer->GetScissorRect());

Renderer::Get()->SetScissorRect(rect);
m_renderer->SetScissorRect(rect);
}

ScissorMode RenderContext::GetScissorMode()
{
return Renderer::Get()->GetScissorMode();
return m_renderer->GetScissorMode();
}

vec4 RenderContext::GetScissorRect()
{
return Renderer::Get()->GetScissorRect();
return m_renderer->GetScissorRect();
}

} /* namespace lol */


+ 1
- 34
src/gpu/renderer.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2017 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
@@ -30,13 +30,6 @@
namespace lol
{

/*
* The global g_renderers object, initialised by Video::Setup
*/

//Renderer *g_renderer = nullptr;
array<Renderer*> g_renderers;

/*
* Private RendererData class
*/
@@ -142,32 +135,6 @@ void Renderer::Clear(ClearMask mask)
glClear(m);
}

/*
* Renderer static
*/

void Renderer::AddNew(ivec2 size)
{
g_renderers << new Renderer(size);
}

int Renderer::GetCount()
{
return g_renderers.count();
}

Renderer* Renderer::Get(int index)
{
return g_renderers[index];
}

void Renderer::DestroyAll()
{
for (Renderer* renderer : g_renderers)
delete renderer;
g_renderers.clear();
}

/*
* Viewport dimensions
*/


+ 12
- 7
src/lol/gpu/rendercontext.h Visa fil

@@ -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.
//

#pragma once

#include <lol/gpu/renderer.h>

//
// The RenderContext class
// -----------------------
@@ -23,7 +27,7 @@ class RenderContextData;
class RenderContext
{
public:
RenderContext();
RenderContext(Renderer *renderer);
~RenderContext();

void SetViewport(ibox2 viewport);
@@ -56,6 +60,7 @@ public:
vec4 GetScissorRect();

private:
Renderer *m_renderer;
RenderContextData *m_data;
};



+ 10
- 16
src/lol/gpu/renderer.h Visa fil

@@ -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
@@ -133,8 +135,8 @@ enum class ScissorMode : uint8_t
class Renderer
{
private:
/* Only the Video class can create a renderer for now. */
friend class Video;
/* Only the Scene class can create a renderer for now. */
friend class Scene;

Renderer(ivec2 size);
~Renderer();
@@ -142,12 +144,6 @@ private:
public:
void Clear(ClearMask mask);

static void AddNew(ivec2 size);
static int GetCount();
static Renderer* Get(int index = 0);
static void DestroyAll();

public:
void SetViewport(ibox2 viewport);
ibox2 GetViewport() const;
float GetXYRatio() const;
@@ -192,7 +188,5 @@ private:
RendererData *m_data;
};

//extern Renderer *g_renderer;

} /* namespace lol */


+ 2
- 2
src/lolimgui.cpp Visa fil

@@ -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
@@ -357,7 +357,7 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data)
if (!m_shader)
return;

RenderContext rc;
RenderContext rc(Scene::GetScene(0).get_renderer());
rc.SetCullMode(CullMode::Disabled);
rc.SetDepthFunc(DepthFunc::Disabled);
rc.SetScissorMode(ScissorMode::Enabled);


+ 2
- 2
src/messageservice.cpp Visa fil

@@ -2,7 +2,7 @@
// Lol Engine
//
// Copyright © 2013—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
@@ -32,7 +32,7 @@ extern "C"
#endif //EMSCRIPTEN

/*
* The global g_renderer object, initialised by Video::Init
* The global g_messageservice object, initialised by MessageService::Setup()
*/
MessageService *g_messageservice = nullptr;



+ 13
- 10
src/scene.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
// © 2014—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -206,6 +206,8 @@ mutex SceneData::m_prim_mutex;
Scene::Scene(ivec2 size)
: data(new SceneData())
{
m_renderer = new Renderer(size);

data->m_renderbuffer[0] = new Framebuffer(size);
data->m_renderbuffer[1] = new Framebuffer(size);
data->m_renderbuffer[2] = new Framebuffer(size);
@@ -260,6 +262,7 @@ Scene::~Scene()
delete data->m_line_api.m_vdecl;
delete data->m_tile_api.m_vdecl;
delete data;
delete m_renderer;
}

//-----------------------------------------------------------------------------
@@ -595,14 +598,14 @@ void Scene::pre_render(float)
}

{
RenderContext rc;
RenderContext rc(m_renderer);
if (do_pp)
{
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
}

Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
m_renderer->Clear(ClearMask::Color | ClearMask::Depth);
}
}

@@ -629,10 +632,10 @@ void Scene::post_render(float)

data->m_renderbuffer[3]->Bind();

RenderContext rc;
RenderContext rc(m_renderer);
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
m_renderer->Clear(ClearMask::Color | ClearMask::Depth);

/* Execute post process */
data->m_pp.m_shader[1]->Bind();
@@ -653,10 +656,10 @@ void Scene::post_render(float)

data->m_pp.m_shader[0]->Bind();

RenderContext rc;
RenderContext rc(m_renderer);
rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
rc.SetClearDepth(1.f);
Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
m_renderer->Clear(ClearMask::Color | ClearMask::Depth);

/* Blit final image to screen */
data->m_pp.m_shader[0]->SetUniform(data->m_pp.m_buffer_uni[0][0], data->m_renderbuffer[3]->GetTextureUniform(), 3);
@@ -683,7 +686,7 @@ void Scene::render_primitives()
ASSERT(!!data, "Trying to access a non-ready scene");

/* FIXME: Temp fix for mesh having no render context*/
RenderContext rc;
RenderContext rc(m_renderer);
rc.SetCullMode(CullMode::Clockwise);
rc.SetDepthFunc(DepthFunc::LessOrEqual);

@@ -703,7 +706,7 @@ void Scene::render_tiles() // XXX: rename to Blit()
{
ASSERT(!!data, "Trying to access a non-ready scene");

RenderContext rc;
RenderContext rc(m_renderer);

/* Early test if nothing needs to be rendered */
if (!data->m_tile_api.m_tiles.count() && !data->m_tile_api.m_palettes.count())
@@ -827,7 +830,7 @@ void Scene::render_lines(float seconds)
{
ASSERT(!!data, "Trying to access a non-ready scene");

RenderContext rc;
RenderContext rc(m_renderer);

if (!data->m_line_api.m_lines.count())
return;


+ 5
- 1
src/scene.h Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
// Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
// © 2014—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
//
// Lol Engine is free software. It comes without any warranty, to
@@ -24,6 +24,7 @@
#include "light.h"
#include "camera.h"
#include "mesh/mesh.h"
#include <lol/gpu/renderer.h>

#define LOL_MAX_LIGHT_COUNT 8

@@ -128,6 +129,8 @@ public:

void Reset();

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 */
/* ============================== */
@@ -257,6 +260,7 @@ private:
void render_lines(float seconds);

SceneData *data;
Renderer *m_renderer;
};

} /* namespace lol */


+ 2
- 8
src/video.cpp Visa fil

@@ -1,7 +1,7 @@
//
// Lol Engine
//
// Copyright © 2010—2016 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
@@ -41,8 +41,6 @@ DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;

void Video::Setup(ivec2 size)
{
//g_renderer = new Renderer(size);
Renderer::AddNew(size);
Scene::AddNew(size);

/* Initialise reasonable scene default properties */
@@ -90,10 +88,6 @@ DebugRenderMode Video::GetDebugRenderMode()
void Video::Destroy()
{
Scene::DestroyAll();
Renderer::DestroyAll();

//delete g_renderer;
//g_renderer = nullptr;
}

void Video::Capture(uint32_t *buffer)
@@ -128,7 +122,7 @@ void Video::Capture(uint32_t *buffer)

ivec2 Video::GetSize()
{
return Renderer::Get()->GetViewport().extent();
return Scene::GetScene(0).get_renderer()->GetViewport().extent();
}

} /* namespace lol */


Laddar…
Avbryt
Spara