From 716c2b5592245096e035abddecb45ac5c193e463 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 18 Jun 2013 22:58:46 +0000 Subject: [PATCH] gpu: create a Renderer and a RenderContext class to handle render states such as alpha blending, depth test, etc. --- src/Makefile.am | 4 +- src/gpu/rendercontext.cpp | 91 +++++++++++++++++++++++++++++++++++++ src/gpu/renderer.cpp | 75 ++++++++++++++++++++++++++++++ src/lol/gpu/all.h | 2 + src/lol/gpu/rendercontext.h | 39 ++++++++++++++++ src/lol/gpu/renderer.h | 42 +++++++++++++++++ src/lolcore.vcxproj | 4 ++ src/lolcore.vcxproj.filters | 12 +++++ src/video.cpp | 5 +- src/video.h | 3 +- 10 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 src/gpu/rendercontext.cpp create mode 100644 src/gpu/renderer.cpp create mode 100644 src/lol/gpu/rendercontext.h create mode 100644 src/lol/gpu/renderer.h diff --git a/src/Makefile.am b/src/Makefile.am index 38eba13b..ff756141 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -50,6 +50,7 @@ liblolcore_headers = \ lol/gpu/all.h \ lol/gpu/shader.h lol/gpu/indexbuffer.h lol/gpu/vertexbuffer.h \ lol/gpu/framebuffer.h lol/gpu/texture.h lol/gpu/lolfx.h \ + lol/gpu/renderer.h lol/gpu/rendercontext.h \ \ lol/debug/all.h \ lol/debug/lines.h \ @@ -77,7 +78,8 @@ liblolcore_sources = \ math/geometry.cpp \ \ gpu/shader.cpp gpu/indexbuffer.cpp gpu/vertexbuffer.cpp \ - gpu/framebuffer.cpp gpu/texture.cpp \ + gpu/framebuffer.cpp gpu/texture.cpp gpu/renderer.cpp \ + gpu/rendercontext.cpp \ \ input/input.cpp input/input.h \ input/keyboard.cpp input/keyboard.h \ diff --git a/src/gpu/rendercontext.cpp b/src/gpu/rendercontext.cpp new file mode 100644 index 00000000..2e3bf93e --- /dev/null +++ b/src/gpu/rendercontext.cpp @@ -0,0 +1,91 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "core.h" + +namespace lol +{ + +class TrackedState +{ +public: + inline TrackedState() + : m_state(Unchanged) + {} + + inline void TrackValue(bool set) + { + m_state = set ? MustSet : MustUnset; + } + + inline bool HasChanged() + { + return m_state != Unchanged; + } + + inline bool GetValue() + { + return m_state == MustSet; + } + +private: + enum + { + Unchanged, + MustSet, + MustUnset, + } + m_state; +}; + +class RenderContextData +{ + friend class RenderContext; + +private: + Scene *m_scene; + + TrackedState m_blend; +}; + +/* + * Public RenderContext class + */ + +RenderContext::RenderContext() + : m_data(new RenderContextData()) +{ + m_data->m_scene = Scene::GetDefault(); +} + +RenderContext::~RenderContext() +{ + if (m_data->m_blend.HasChanged()) + g_renderer->SetBlendState(m_data->m_blend.GetValue()); + + delete m_data; +} + +void RenderContext::SetBlendState(bool set) +{ + if (!m_data->m_blend.HasChanged()) + m_data->m_blend.TrackValue(g_renderer->GetBlendState()); + + g_renderer->SetBlendState(set); +} + +} /* namespace lol */ + diff --git a/src/gpu/renderer.cpp b/src/gpu/renderer.cpp new file mode 100644 index 00000000..3364aee5 --- /dev/null +++ b/src/gpu/renderer.cpp @@ -0,0 +1,75 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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. +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "core.h" + +namespace lol +{ + +/* + * The global g_renderer object, initialised by Video::Init + */ +Renderer *g_renderer; + +/* + * Private RendererData class + */ +class RendererData +{ + friend class Renderer; + +private: + bool m_blend; +}; + +/* + * Public Renderer class + */ + +Renderer::Renderer() + : m_data(new RendererData()) +{ + m_data->m_blend = false; +} + +Renderer::~Renderer() +{ + delete m_data; +} + +void Renderer::SetBlendState(bool set) +{ + if (m_data->m_blend == set) + return; + + m_data->m_blend = set; + if (set) + { + + } + else + { + + } +} + +bool Renderer::GetBlendState() const +{ + return m_data->m_blend; +} + +} /* namespace lol */ + diff --git a/src/lol/gpu/all.h b/src/lol/gpu/all.h index d134f539..dbff0d97 100644 --- a/src/lol/gpu/all.h +++ b/src/lol/gpu/all.h @@ -17,6 +17,8 @@ #include #include #include +#include +#include #endif // __LOL_GPU_ALL_H__ diff --git a/src/lol/gpu/rendercontext.h b/src/lol/gpu/rendercontext.h new file mode 100644 index 00000000..e60a9159 --- /dev/null +++ b/src/lol/gpu/rendercontext.h @@ -0,0 +1,39 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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. +// + +// +// The RenderContext class +// ----------------------- +// + +#if !defined __LOL_RENDERCONTEXT_H__ +#define __LOL_RENDERCONTEXT_H__ + +namespace lol +{ + +class RenderContextData; + +class RenderContext +{ +public: + RenderContext(); + ~RenderContext(); + + void SetBlendState(bool set); + +private: + RenderContextData *m_data; +}; + +} /* namespace lol */ + +#endif // __LOL_RENDERCONTEXT_H__ + diff --git a/src/lol/gpu/renderer.h b/src/lol/gpu/renderer.h new file mode 100644 index 00000000..31956399 --- /dev/null +++ b/src/lol/gpu/renderer.h @@ -0,0 +1,42 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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. +// + +// +// The Renderer class +// ------------------ +// + +#if !defined __LOL_RENDERER_H__ +#define __LOL_RENDERER_H__ + +namespace lol +{ + +class RendererData; + +class Renderer +{ +public: + Renderer(); + ~Renderer(); + + void SetBlendState(bool set); + bool GetBlendState() const; + +private: + RendererData *m_data; +}; + +extern Renderer *g_renderer; + +} /* namespace lol */ + +#endif // __LOL_RENDERER_H__ + diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index ba6c2cac..3fbf14cb 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -115,6 +115,8 @@ + + @@ -209,6 +211,8 @@ + + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 59a26f57..6bee23b6 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -111,6 +111,12 @@ mesh + + gpu + + + gpu + gpu @@ -560,6 +566,12 @@ lol\base + + lol\gpu + + + lol\gpu + lol\gpu diff --git a/src/video.cpp b/src/video.cpp index 742b3263..e1cf17b7 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -91,6 +91,8 @@ float VideoData::clear_depth; void Video::Setup(ivec2 size) { + g_renderer = new Renderer(); + #if defined USE_D3D9 || defined _XBOX VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION); if (!VideoData::d3d_ctx) @@ -402,7 +404,8 @@ void Video::Clear(ClearMask m) void Video::Destroy() { - ; + delete g_renderer; + g_renderer = nullptr; } void Video::Capture(uint32_t *buffer) diff --git a/src/video.h b/src/video.h index 99e0e9e9..7e28020c 100644 --- a/src/video.h +++ b/src/video.h @@ -66,9 +66,10 @@ class Video { public: static void Setup(ivec2 size); + static void Destroy(); + static void SetCustomSize(ivec2 size); static void RestoreSize(); - static void Destroy(); static void SetFov(float theta); static void SetDepth(bool set); static void SetFaceCulling(bool set);