From 51bf8067a906db8c6b1fb13245bad9cbe73818b8 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 18 Aug 2012 15:49:40 +0000 Subject: [PATCH] gpu: get rid of the glClearColor, glClearDepth and glClear calls in all projects, we now use Video::SetClearColor, Video::SetClearDepth and Video::Clear instead, so that the Direct3D equivalents can be called. --- src/ticker.cpp | 2 +- src/video.cpp | 65 +++++++++++++++++++++++++++++++++------------ src/video.h | 21 ++++++++++++++- tutorial/08_fbo.cpp | 9 +++---- 4 files changed, 73 insertions(+), 24 deletions(-) diff --git a/src/ticker.cpp b/src/ticker.cpp index 79351c12..85873ecd 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -362,7 +362,7 @@ void Ticker::TickDraw() { case Entity::DRAWGROUP_BEGIN: Scene::GetDefault()->Reset(); - Video::Clear(); + Video::Clear(ClearMask::All); break; case Entity::DRAWGROUP_HUD: Video::SetDepth(false); diff --git a/src/video.cpp b/src/video.cpp index 497e2bf3..e55fdf24 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -51,28 +51,32 @@ class VideoData private: static mat4 proj_matrix; static ivec2 saved_viewport; -#if defined USE_D3D9 +#if defined USE_D3D9 || defined _XBOX +# if defined USE_D3D9 static IDirect3D9 *d3d_ctx; static IDirect3DDevice9 *d3d_dev; - static D3DCOLOR clear_color; -#elif defined _XBOX +# elif defined _XBOX static Direct3D *d3d_ctx; static D3DDevice *d3d_dev; +# endif static D3DCOLOR clear_color; + static float clear_depth; #endif }; mat4 VideoData::proj_matrix; ivec2 VideoData::saved_viewport(0, 0); -#if defined USE_D3D9 +#if defined USE_D3D9 || defined _XBOX +# if defined USE_D3D9 IDirect3D9 *VideoData::d3d_ctx; IDirect3DDevice9 *VideoData::d3d_dev; -D3DCOLOR VideoData::clear_color; -#elif defined _XBOX +# elif defined _XBOX Direct3D *VideoData::d3d_ctx; D3DDevice *VideoData::d3d_dev; +# endif D3DCOLOR VideoData::clear_color; +float VideoData::clear_depth; #endif /* @@ -107,8 +111,6 @@ void Video::Setup(ivec2 size) # endif VideoData::saved_viewport = size; - VideoData::clear_color = D3DCOLOR_XRGB(26, 51, 77); - d3dpp.BackBufferWidth = size.x; d3dpp.BackBufferHeight = size.y; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; @@ -143,13 +145,14 @@ void Video::Setup(ivec2 size) glViewport(0, 0, size.x, size.y); VideoData::saved_viewport = size; - glClearColor(0.1f, 0.2f, 0.3f, 1.0f); - glClearDepth(1.0); - # if defined HAVE_GL_2X && !defined __APPLE__ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); # endif #endif + + /* Initialise reasonable scene default properties */ + SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f)); + SetClearDepth(1.f); } void Video::SetFov(float theta) @@ -218,17 +221,45 @@ void Video::SetClearColor(vec4 color) #endif } -void Video::Clear() +void Video::SetClearDepth(float f) { - ivec2 size = GetSize(); #if defined USE_D3D9 || defined _XBOX - if (FAILED(VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER - | D3DCLEAR_STENCIL, - VideoData::clear_color, 1.0f, 0))) + VideoData::clear_depth = f; +#else + glClearDepth(f); +#endif +} + +void Video::Clear(ClearMask m) +{ +#if defined USE_D3D9 || defined _XBOX + /* Note: D3D9 doesn't appear to support the accumulation buffer. */ + int mask = 0; + if (m & ClearMask::Color) + mask |= D3DCLEAR_TARGET; + if (m & ClearMask::Depth) + mask |= D3DCLEAR_ZBUFFER; + if (m & ClearMask::Stencil) + mask |= D3DCLEAR_STENCIL; + if (FAILED(VideoData::d3d_dev->Clear(0, NULL, mask, + VideoData::clear_color, + VideoData::clear_depth, 0))) Abort(); #else + /* FIXME: is this necessary here? */ + ivec2 size = GetSize(); glViewport(0, 0, size.x, size.y); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + GLbitfield mask = 0; + if (m & ClearMask::Color) + mask |= GL_COLOR_BUFFER_BIT; + if (m & ClearMask::Depth) + mask |= GL_DEPTH_BUFFER_BIT; + if (m & ClearMask::Accum) + mask |= GL_ACCUM_BUFFER_BIT; + if (m & ClearMask::Stencil) + mask |= GL_STENCIL_BUFFER_BIT; + glClear(mask); #endif SetFov(0.0f); diff --git a/src/video.h b/src/video.h index 83f5e2c9..23d8ba2b 100644 --- a/src/video.h +++ b/src/video.h @@ -22,6 +22,24 @@ namespace lol { +struct ClearMask +{ + enum Value + { + Color = 1 << 0, + Depth = 1 << 1, + Accum = 1 << 2, + Stencil = 1 << 3, + + All = 0xffffffff + } + m_value; + + inline ClearMask(Value v) { m_value = v; } + inline ClearMask(uint64_t i) { m_value = (Value)i; } + inline operator Value() { return m_value; } +}; + class Video { public: @@ -30,7 +48,8 @@ public: static void SetFov(float theta); static void SetDepth(bool set); static void SetClearColor(vec4 color); - static void Clear(); + static void SetClearDepth(float f); + static void Clear(ClearMask m); static void Capture(uint32_t *buffer); static ivec2 GetSize(); }; diff --git a/tutorial/08_fbo.cpp b/tutorial/08_fbo.cpp index 60538d4f..755776d7 100644 --- a/tutorial/08_fbo.cpp +++ b/tutorial/08_fbo.cpp @@ -14,7 +14,6 @@ #include "core.h" #include "loldebug.h" -#include "lolgl.h" using namespace std; using namespace lol; @@ -77,9 +76,9 @@ public: m_fbo = new FrameBuffer(Video::GetSize()); m_fbo->Bind(); - glClearColor(0.0, 0.0, 0.0, 1.0f); - glClearDepth(1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f)); + Video::SetClearDepth(1.f); + Video::Clear(ClearMask::Color | ClearMask::Depth); m_fbo->Unbind(); m_ready = true; @@ -89,7 +88,7 @@ public: m_fbo->Bind(); /* FIXME: we should just disable depth test in the shader */ - glClear(GL_DEPTH_BUFFER_BIT); + Video::Clear(ClearMask::Depth); m_shader->Bind(); m_shader->SetUniform(m_uni_flag, 0.f); m_shader->SetUniform(m_uni_point, m_hotspot);