From 7af7b2bc6d2e6bc90730525dbb2822deb1b29cff Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Tue, 3 Nov 2015 22:34:30 -0800 Subject: [PATCH] Add methods to "force" glstate to certain values. This way we don't have to use totally different syntax when doing: glstate.scissorTest.force(false); // Do stuff that needs scissorTest temporarily off. glstate.scissorTest.restore(); There's cleaner options maybe but this is a simple first step. --- GPU/Directx9/helper/dx_state.h | 113 +++++++++++++++++++++++++++------ GPU/GLES/GLStateCache.h | 84 ++++++++++++++++++------ 2 files changed, 158 insertions(+), 39 deletions(-) diff --git a/GPU/Directx9/helper/dx_state.h b/GPU/Directx9/helper/dx_state.h index e1f6845e18..156abf0abf 100644 --- a/GPU/Directx9/helper/dx_state.h +++ b/GPU/Directx9/helper/dx_state.h @@ -19,9 +19,14 @@ private: inline void set(bool value) { if (_value != value) { _value = value; - pD3Ddevice->SetRenderState(cap, value); + restore(); } } + void force(bool value) { + bool old = _value; + set(value); + _value = old; + } inline void enable() { set(true); } @@ -51,9 +56,14 @@ private: inline void set(DWORD newp1) { if (p1 != newp1) { p1 = newp1; - pD3Ddevice->SetRenderState(_state1, p1); + restore(); } } + void force(DWORD newp1) { + DWORD old = p1; + set(newp1); + p1 = old; + } void restore() { pD3Ddevice->SetRenderState(_state1, p1); } @@ -71,9 +81,14 @@ private: inline void set(DWORD newp1) { if (p1 != newp1) { p1 = newp1; - pD3Ddevice->SetSamplerState(0, _state1, p1); + restore(); } } + void force(DWORD newp1) { + DWORD old = p1; + set(newp1); + p1 = old; + } void restore() { pD3Ddevice->SetSamplerState(0, _state1, p1); } @@ -95,9 +110,14 @@ private: inline void set(FLOAT newp1) { if (p1 != newp1) { p1 = newp1; - pD3Ddevice->SetSamplerState(0, _state1, p1d); + restore(); } } + void force(FLOAT newp1) { + FLOAT old = p1; + set(newp1); + p1 = old; + } void restore() { pD3Ddevice->SetSamplerState(0, _state1, p1d); } @@ -124,6 +144,13 @@ private: pD3Ddevice->SetRenderState(_state2, p2); } } + void force(DWORD newp1, DWORD newp2) { + DWORD old1 = p1; + DWORD old2 = p2; + set(newp1, newp2); + p1 = old1; + p2 = old2; + } void restore() { pD3Ddevice->SetRenderState(_state1, p1); pD3Ddevice->SetRenderState(_state2, p2); @@ -158,10 +185,19 @@ private: pD3Ddevice->SetRenderState(_state3, p3); } } + void force(DWORD newp1, DWORD newp2, DWORD newp3) { + DWORD old1 = p1; + DWORD old2 = p2; + DWORD old3 = p3; + set(newp1, newp2, newp3); + p1 = old1; + p2 = old2; + p3 = old3; + } void restore() { - pD3Ddevice->SetRenderState(_state1, p1); - pD3Ddevice->SetRenderState(_state2, p2); - pD3Ddevice->SetRenderState(_state3, p3); + pD3Ddevice->SetRenderState(_state1, p1); + pD3Ddevice->SetRenderState(_state2, p2); + pD3Ddevice->SetRenderState(_state3, p3); } }; @@ -199,6 +235,17 @@ private: pD3Ddevice->SetRenderState(_state4, p4); } } + void force(DWORD newp1, DWORD newp2, DWORD newp3, DWORD newp4) { + DWORD old1 = p1; + DWORD old2 = p2; + DWORD old3 = p3; + DWORD old4 = p4; + set(newp1, newp2, newp3, newp4); + p1 = old1; + p2 = old2; + p3 = old3; + p4 = old4; + } void restore() { pD3Ddevice->SetRenderState(_state1, p1); pD3Ddevice->SetRenderState(_state2, p2); @@ -219,9 +266,14 @@ private: DWORD newc = D3DCOLOR_COLORVALUE(v[0], v[1], v[2], v[3]); if (c != newc) { c = newc; - pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c); + restore(); } } + void force(const float v[4]) { + DWORD old = c; + set(v); + c = old; + } inline void restore() { pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c); } @@ -251,9 +303,13 @@ private: } if (mask != newmask) { mask = newmask; - pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask); + restore(); } - + } + void force(bool r, bool g, bool b, bool a) { + DWORD old = mask; + set(r, g, b, a); + mask = old; } inline void restore() { pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask); @@ -267,12 +323,14 @@ private: DirectXState::state_count++; } inline void set(bool) { - + // Nothing. + } + void force(bool) { + // Nothing. } inline void restore() { - + // Nothing. } - inline void enable() { set(true); } @@ -282,9 +340,9 @@ private: }; class StateVp { - D3DVIEWPORT9 viewport; + D3DVIEWPORT9 viewport; public: - inline void set(int x, int y, int w, int h, float n = 0.f, float f = 1.f) { + inline void set(int x, int y, int w, int h, float n = 0.f, float f = 1.f) { D3DVIEWPORT9 newviewport; newviewport.X = x; newviewport.Y = y; @@ -295,10 +353,16 @@ private: if (memcmp(&viewport, &newviewport, sizeof(viewport))) { viewport = newviewport; - pD3Ddevice->SetViewport(&viewport); + restore(); } } + void force(int x, int y, int w, int h, float n = 0.f, float f = 1.f) { + D3DVIEWPORT9 old = viewport; + set(x, y, w, h, n, f); + viewport = old; + } + inline void restore() { pD3Ddevice->SetViewport(&viewport); } @@ -307,14 +371,20 @@ private: class StateScissor { RECT rect; public: - inline void set(int x1, int y1, int x2, int y2) { + inline void set(int x1, int y1, int x2, int y2) { RECT newrect = {x1, y1, x2, y2}; if (memcmp(&rect, &newrect, sizeof(rect))) { rect = newrect; - pD3Ddevice->SetScissorRect(&rect); + restore(); } } + void force(int x1, int y1, int x2, int y2) { + RECT old = rect; + set(x1, y1, x2, y2); + rect = old; + } + inline void restore() { pD3Ddevice->SetScissorRect(&rect); } @@ -337,9 +407,14 @@ private: } if (cull != newcull) { cull = newcull; - pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull); + restore(); } } + void force(int wantcull, int cullmode) { + DWORD old = cull; + set(wantcull, cullmode); + cull = old; + } inline void restore() { pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull); } diff --git a/GPU/GLES/GLStateCache.h b/GPU/GLES/GLStateCache.h index c1d6f2669d..f4a853ffb1 100644 --- a/GPU/GLES/GLStateCache.h +++ b/GPU/GLES/GLStateCache.h @@ -14,22 +14,23 @@ private: template class BoolState { private: - bool _value; + bool v; public: - BoolState() : _value(init) { + BoolState() : v(init) { OpenGLState::state_count++; } inline void set(bool value) { - if (value && value != _value) { - _value = value; - glEnable(cap); - } - if (!value && value != _value) { - _value = value; - glDisable(cap); + if (value != v) { + v = value; + restore(); } } + inline void force(bool value) { + bool old = v; + set(v); + v = old; + } inline void enable() { set(true); } @@ -40,10 +41,10 @@ private: return isset(); } inline bool isset() { - return _value; + return v; } void restore() { - if (_value) + if (v) glEnable(cap); else glDisable(cap); @@ -60,10 +61,15 @@ private: void set(p1type newp1) { \ if (newp1 != p1) { \ p1 = newp1; \ - func(p1); \ + restore(); \ } \ } \ - void restore() { \ + void force(p1type newp1) { \ + p1type old = p1; \ + set(newp1); \ + p1 = old; \ + } \ + inline void restore() { \ func(p1); \ } \ } @@ -80,9 +86,16 @@ private: if (newp1 != p1 || newp2 != p2) { \ p1 = newp1; \ p2 = newp2; \ - func(p1, p2); \ + restore(); \ } \ } \ + void force(p1type newp1, p2type newp2) { \ + p1type old1 = p1; \ + p2type old2 = p2; \ + set(newp1, newp2); \ + p1 = old1; \ + p2 = old2; \ + } \ inline void restore() { \ func(p1, p2); \ } \ @@ -102,9 +115,18 @@ private: p1 = newp1; \ p2 = newp2; \ p3 = newp3; \ - func(p1, p2, p3); \ + restore(); \ } \ } \ + void force(p1type newp1, p2type newp2, p3type newp3) { \ + p1type old1 = p1; \ + p2type old2 = p2; \ + p3type old3 = p3; \ + set(newp1, newp2, newp3); \ + p1 = old1; \ + p2 = old2; \ + p3 = old3; \ + } \ inline void restore() { \ func(p1, p2, p3); \ } \ @@ -126,9 +148,20 @@ private: p2 = newp2; \ p3 = newp3; \ p4 = newp4; \ - func(p1, p2, p3, p4); \ + restore(); \ } \ } \ + void force(p1type newp1, p2type newp2, p3type newp3, p4type newp4) { \ + p1type old1 = p1; \ + p2type old2 = p2; \ + p3type old3 = p3; \ + p4type old4 = p4; \ + set(newp1, newp2, newp3, newp4); \ + p1 = old1; \ + p2 = old2; \ + p3 = old3; \ + p4 = old4; \ + } \ inline void restore() { \ func(p1, p2, p3, p4); \ } \ @@ -143,11 +176,17 @@ private: OpenGLState::state_count++; \ } \ inline void set(const float v[4]) { \ - if (memcmp(p,v,sizeof(float)*4)) { \ - memcpy(p,v,sizeof(float)*4); \ - func(p[0], p[1], p[2], p[3]); \ + if (memcmp(p, v, sizeof(float) * 4)) { \ + memcpy(p, v, sizeof(float) * 4); \ + restore(); \ } \ } \ + void force(const float v[4]) { \ + float old[4]; \ + memcpy(old, p, sizeof(float) * 4); \ + set(v); \ + memcpy(p, old, sizeof(float) * 4); \ + } \ inline void restore() { \ func(p[0], p[1], p[2], p[3]); \ } \ @@ -163,10 +202,15 @@ private: } \ inline void bind(GLuint val) { \ if (val_ != val) { \ - func(target, val); \ val_ = val; \ + restore(); \ } \ } \ + inline void force(GLuint val) { \ + GLuint old = val_; \ + bind(val); \ + val_ = val; \ + } \ inline void unbind() { \ bind(0); \ } \