Permalink
Browse files

Merge branch 'FEAT-4163-Implemented_state_manager_for_caching_DirectX…

…_rendering_state_changes'
  • Loading branch information...
2 parents 2ae9a97 + 540b637 commit 4dcb065b6d7af5cea979681ccd1818f9d841341a @tourettes tourettes committed Mar 24, 2013
@@ -122,6 +122,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
+ <ClCompile Include="source\EffectStateManager.cpp" />
<ClCompile Include="source\fontEngine.cpp">
<AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(DSHOW_BASE); $(WINDOWS_SDK)Include; $(DXSDK_DIR)Include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
@@ -134,6 +135,7 @@
<None Include="source\fontEngine.def" />
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="source\EffectStateManager.h" />
<ClInclude Include="source\TransformMatrix.h" />
<ClInclude Include="source\fontEngine.h" />
<ClInclude Include="source\stdafx.h" />
@@ -0,0 +1,184 @@
+// Copyright (C) 2005-2012 Team MediaPortal
+// http://www.team-mediaportal.com
+//
+// MediaPortal is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// MediaPortal is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
+
+#include "stdafx.h"
+#include "EffectStateManager.h"
+
+CPureDeviceStateManager::CPureDeviceStateManager(LPDIRECT3DDEVICE9 pDevice) :
+ m_pDevice(pDevice),
+ m_lRef(0),
+ m_cacheRenderStates(D3DRS_BLENDOPALPHA),
+ m_vecCacheSamplerStates(CACHED_STAGES, samplerStageCache(D3DSAMP_DMAPOFFSET)),
+ m_vecCacheTextureStates(CACHED_STAGES, textureStateStageCache(D3DTSS_CONSTANT))
+{
+ m_pDevice->AddRef();
+}
+
+CPureDeviceStateManager::~CPureDeviceStateManager()
+{
+ m_pDevice->Release();
+}
+
+HRESULT CPureDeviceStateManager::QueryInterface(REFIID iid, LPVOID* ppv)
+{
+ if (iid == IID_IUnknown || iid == IID_ID3DXEffectStateManager)
+ *ppv = static_cast<ID3DXEffectStateManager*>(this);
+ else
+ {
+ *ppv = NULL;
+ return E_NOINTERFACE;
+ }
+
+ reinterpret_cast<IUnknown*>(this)->AddRef();
+ return S_OK;
+}
+
+ULONG CPureDeviceStateManager::AddRef()
+{
+ return (ULONG)InterlockedIncrement(&m_lRef);
+}
+
+ULONG CPureDeviceStateManager::Release()
+{
+ if (InterlockedDecrement(&m_lRef) == 0L)
+ {
+ delete this;
+ return 0L;
+ }
+
+ return m_lRef;
+}
+
+void CPureDeviceStateManager::DirtyCachedValues()
+{
+ m_cacheRenderStates.dirtyall();
+
+ vector <samplerStageCache>::iterator it_samplerStages;
+ for(it_samplerStages = m_vecCacheSamplerStates.begin(); it_samplerStages != m_vecCacheSamplerStates.end(); it_samplerStages++)
+ {
+ (*it_samplerStages).dirtyall();
+ }
+
+ vector <textureStateStageCache>::iterator it_textureStages;
+ for(it_textureStages = m_vecCacheTextureStates.begin(); it_textureStages != m_vecCacheTextureStates.end(); it_textureStages++)
+ {
+ (*it_textureStages).dirtyall();
+ }
+}
+
+HRESULT CPureDeviceStateManager::SetRenderState(D3DRENDERSTATETYPE d3dRenderState, DWORD dwValue)
+{
+ if (m_cacheRenderStates.set_val(d3dRenderState, dwValue))
+ return m_pDevice->SetRenderState(d3dRenderState, dwValue);
+
+ return S_OK;
+}
+
+HRESULT CPureDeviceStateManager::SetSamplerState(DWORD dwStage, D3DSAMPLERSTATETYPE d3dSamplerState, DWORD dwValue)
+{
+ if (dwStage >= CACHED_STAGES || m_vecCacheSamplerStates[dwStage].set_val(d3dSamplerState, dwValue))
+ return m_pDevice->SetSamplerState(dwStage, d3dSamplerState, dwValue);
+
+ return S_OK;
+}
+
+HRESULT CPureDeviceStateManager::SetTextureStageState(DWORD dwStage, D3DTEXTURESTAGESTATETYPE d3dTextureStageState, DWORD dwValue)
+{
+ if (dwStage >= CACHED_STAGES || m_vecCacheTextureStates[dwStage].set_val(d3dTextureStageState, dwValue))
+ return m_pDevice->SetTextureStageState(dwStage, d3dTextureStageState, dwValue);
+
+ return S_OK;
+}
+
+HRESULT CPureDeviceStateManager::SetTexture(DWORD dwStage, LPDIRECT3DBASETEXTURE9 pTexture)
+{
+ return m_pDevice->SetTexture(dwStage, pTexture);
+}
+
+HRESULT CPureDeviceStateManager::SetVertexShader(LPDIRECT3DVERTEXSHADER9 pShader)
+{
+ return m_pDevice->SetVertexShader(pShader);
+}
+
+HRESULT CPureDeviceStateManager::SetPixelShader(LPDIRECT3DPIXELSHADER9 pShader)
+{
+ return m_pDevice->SetPixelShader(pShader);
+}
+
+HRESULT CPureDeviceStateManager::SetFVF(DWORD dwFVF)
+{
+ return m_pDevice->SetFVF(dwFVF);
+}
+
+HRESULT CPureDeviceStateManager::SetTransform(D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX *pMatrix)
+{
+ return m_pDevice->SetTransform(State, pMatrix);
+}
+
+HRESULT CPureDeviceStateManager::SetMaterial(CONST D3DMATERIAL9 *pMaterial)
+{
+ return m_pDevice->SetMaterial(pMaterial);
+}
+
+HRESULT CPureDeviceStateManager::SetLight(DWORD Index, CONST D3DLIGHT9 *pLight)
+{
+ return m_pDevice->SetLight(Index, pLight);
+}
+
+HRESULT CPureDeviceStateManager::LightEnable(DWORD Index, BOOL Enable)
+{
+ return m_pDevice->LightEnable(Index, Enable);
+}
+
+HRESULT CPureDeviceStateManager::SetNPatchMode(FLOAT NumSegments)
+{
+ return m_pDevice->SetNPatchMode(NumSegments);
+}
+
+HRESULT CPureDeviceStateManager::SetVertexShaderConstantF(UINT RegisterIndex, CONST FLOAT *pConstantData, UINT RegisterCount)
+{
+ return m_pDevice->SetVertexShaderConstantF( RegisterIndex, pConstantData, RegisterCount);
+}
+
+HRESULT CPureDeviceStateManager::SetVertexShaderConstantI(UINT RegisterIndex, CONST INT *pConstantData, UINT RegisterCount)
+{
+ return m_pDevice->SetVertexShaderConstantI( RegisterIndex, pConstantData, RegisterCount);
+}
+
+HRESULT CPureDeviceStateManager::SetVertexShaderConstantB(UINT RegisterIndex, CONST BOOL *pConstantData, UINT RegisterCount)
+{
+ return m_pDevice->SetVertexShaderConstantB(RegisterIndex, pConstantData, RegisterCount);
+}
+
+HRESULT CPureDeviceStateManager::SetPixelShaderConstantF(UINT RegisterIndex, CONST FLOAT *pConstantData, UINT RegisterCount)
+{
+ return m_pDevice->SetPixelShaderConstantF(RegisterIndex, pConstantData, RegisterCount);
+}
+
+HRESULT CPureDeviceStateManager::SetPixelShaderConstantI(UINT RegisterIndex, CONST INT *pConstantData, UINT RegisterCount)
+{
+ return m_pDevice->SetPixelShaderConstantI(RegisterIndex, pConstantData, RegisterCount);
+}
+
+HRESULT CPureDeviceStateManager::SetPixelShaderConstantB(UINT RegisterIndex, CONST BOOL *pConstantData, UINT RegisterCount)
+{
+ return m_pDevice->SetPixelShaderConstantB(RegisterIndex, pConstantData, RegisterCount);
+}
+
+CStateManagerInterface* CStateManagerInterface::Create(LPDIRECT3DDEVICE9 pDevice)
+{
+ return new CPureDeviceStateManager(pDevice);
+}
@@ -0,0 +1,133 @@
+// Copyright (C) 2005-2012 Team MediaPortal
+// http://www.team-mediaportal.com
+//
+// MediaPortal is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// MediaPortal is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
+
+#pragma once
+#ifndef _EFFECTSTATEMANAGER_H_
+#define _EFFECTSTATEMANAGER_H_
+
+#include "stdafx.h"
+
+#pragma warning (push)
+#pragma warning (disable : 4512) // 'class' : assignment operator could not be generated
+#pragma warning (disable : 4702) // unreachable code
+#include <vector>
+#include <utility>
+#pragma warning (pop)
+using std::vector;
+#pragma warning(default: 4995)
+
+#define CACHED_STAGES 4
+
+// template for the cache
+template <typename _Kty, typename _Ty> class multicache
+{
+public:
+
+ multicache(unsigned int scope)
+ {
+ m_vecCache.resize(scope);
+ }
+
+ struct cacheItem
+ {
+ _Ty value;
+ bool set;
+ };
+
+protected:
+ std::vector <cacheItem> m_vecCache;
+
+public:
+ inline void dirtyall()
+ {
+ m_vecCache.clear();
+ }
+
+ inline void dirty(_Kty key)
+ {
+ m_vecCache[key].set = false;
+ }
+
+ inline bool set_val(_Kty key, _Ty value)
+ {
+ if (m_vecCache[key].value == value && m_vecCache[key].set)
+ return false;
+ else
+ {
+ m_vecCache[key].value = value;
+ m_vecCache[key].set = true;
+
+ return true;
+ }
+ }
+};
+
+class CStateManagerInterface : public ID3DXEffectStateManager
+{
+public:
+
+ virtual ~CStateManagerInterface(){};
+ virtual void DirtyCachedValues() = 0;
+
+ static CStateManagerInterface* CStateManagerInterface::Create(LPDIRECT3DDEVICE9 pDevice);
+};
+
+class CPureDeviceStateManager : public CStateManagerInterface
+{
+public:
+
+ CPureDeviceStateManager(LPDIRECT3DDEVICE9 pDevice);
+ virtual ~CPureDeviceStateManager();
+
+ STDMETHOD(QueryInterface)(REFIID iid, LPVOID* ppv);
+ STDMETHOD_(ULONG, AddRef)();
+ STDMETHOD_(ULONG, Release)();
+
+ void DirtyCachedValues();
+
+ STDMETHOD(SetRenderState)(D3DRENDERSTATETYPE d3dRenderState, DWORD dwValue);
+ STDMETHOD(SetSamplerState)(DWORD dwStage, D3DSAMPLERSTATETYPE d3dSamplerState, DWORD dwValue);
+ STDMETHOD(SetTextureStageState)(DWORD dwStage, D3DTEXTURESTAGESTATETYPE d3dTextureStageState, DWORD dwValue);
+ STDMETHOD(SetTexture)(DWORD dwStage, LPDIRECT3DBASETEXTURE9 pTexture);
+ STDMETHOD(SetVertexShader)(LPDIRECT3DVERTEXSHADER9 pShader);
+ STDMETHOD(SetPixelShader)(LPDIRECT3DPIXELSHADER9 pShader);
+ STDMETHOD(SetFVF)(DWORD dwFVF);
+ STDMETHOD(SetTransform)(D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX *pMatrix);
+ STDMETHOD(SetMaterial)(CONST D3DMATERIAL9 *pMaterial);
+ STDMETHOD(SetLight)(DWORD Index, CONST D3DLIGHT9 *pLight);
+ STDMETHOD(LightEnable)(DWORD Index, BOOL Enable);
+ STDMETHOD(SetNPatchMode)(FLOAT NumSegments);
+ STDMETHOD(SetVertexShaderConstantF)(UINT RegisterIndex, CONST FLOAT *pConstantData, UINT RegisterCount);
+ STDMETHOD(SetVertexShaderConstantI)(UINT RegisterIndex, CONST INT *pConstantData, UINT RegisterCount);
+ STDMETHOD(SetVertexShaderConstantB)(UINT RegisterIndex, CONST BOOL *pConstantData, UINT RegisterCount);
+ STDMETHOD(SetPixelShaderConstantF)(UINT RegisterIndex, CONST FLOAT *pConstantData, UINT RegisterCount);
+ STDMETHOD(SetPixelShaderConstantI)(UINT RegisterIndex, CONST INT *pConstantData, UINT RegisterCount);
+ STDMETHOD(SetPixelShaderConstantB)(UINT RegisterIndex, CONST BOOL *pConstantData, UINT RegisterCount);
+
+protected:
+ typedef multicache <DWORD, DWORD> samplerStageCache;
+ typedef multicache <DWORD, DWORD> textureStateStageCache;
+
+protected:
+ LPDIRECT3DDEVICE9 m_pDevice;
+ LONG m_lRef;
+
+ multicache <DWORD, DWORD> m_cacheRenderStates;
+ vector <samplerStageCache> m_vecCacheSamplerStates;
+ vector <textureStateStageCache> m_vecCacheTextureStates;
+};
+
+#endif
Oops, something went wrong.

0 comments on commit 4dcb065

Please sign in to comment.