Skip to content

Commit

Permalink
xrRender_R4: added support for non-unique named shader resources. (Cl…
Browse files Browse the repository at this point in the history
…oses #281)
  • Loading branch information
Xottab-DUTY committed Oct 9, 2019
1 parent 7bcf3b9 commit d38f18d
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 57 deletions.
13 changes: 11 additions & 2 deletions src/Layers/xrRender/Blender_Recorder_R2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ void CBlender_Compile::r_Pass(std::pair<cpcstr, cpcstr> _vs, LPCSTR _ps, bool bF

// Create shaders
SPS* ps = RImplementation.Resources->_CreatePS(_ps);
SVS* vs = RImplementation.Resources->_CreateVS(_vs.first, _vs.second);
u32 flags = 0;
#if defined(USEDX10) || defined(USE_DX11)
if (ps->constants.dx9compatibility)
flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
#endif
SVS* vs = RImplementation.Resources->_CreateVS(_vs.first, _vs.second, flags);
dest.ps = ps;
dest.vs = vs;
#ifndef USE_DX9
Expand Down Expand Up @@ -76,7 +81,7 @@ u32 CBlender_Compile::i_Sampler(LPCSTR _name)
fix_texture_name(name);

// Find index
ref_constant C = ctable.get(name);
ref_constant C = ctable.get(name, ctable.dx9compatibility ? RC_sampler : u16(-1));
if (!C)
return u32(-1);

Expand Down Expand Up @@ -125,7 +130,11 @@ u32 CBlender_Compile::r_Sampler(
dwStage = i_Sampler(_name);
if (u32(-1) != dwStage)
{
#if defined(USE_DX10) || defined(USE_DX11)
r_dx10Texture(_name, texture, true);
#else
i_Texture(dwStage, texture);
#endif

// force ANISO-TF for "s_base"
if ((0 == xr_strcmp(_name, "s_base")) && (fmin == D3DTEXF_LINEAR))
Expand Down
5 changes: 3 additions & 2 deletions src/Layers/xrRender/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class ECORE_API CResourceManager
SPS* _CreatePS(LPCSTR Name);
void _DeletePS(const SPS* PS);

SVS* _CreateVS(cpcstr shader, cpcstr fallbackShader = nullptr);
SVS* _CreateVS(cpcstr shader, cpcstr fallbackShader = nullptr, u32 flags = 0);
void _DeleteVS(const SVS* VS);

SPass* _CreatePass(const SPass& proto);
Expand Down Expand Up @@ -262,7 +262,8 @@ class ECORE_API CResourceManager
T& GetShaderMap();

template <typename T>
T* CreateShader(cpcstr name, pcstr filename = nullptr, pcstr fallbackShader = nullptr);
T* CreateShader(cpcstr name, pcstr filename = nullptr,
pcstr fallbackShader = nullptr, u32 flags = 0);

template <typename T>
bool DestroyShader(const T* sh);
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRender/ResourceManager_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void CResourceManager::_DeleteDecl(const SDeclaration* dcl)
}

//--------------------------------------------------------------------------------------------------------------
SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullptr*/)
SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullptr*/, u32 flags /*= 0*/)
{
string_path name;
xr_strcpy(name, shader);
Expand All @@ -173,7 +173,7 @@ SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullpt
if (4 == GEnv.Render->m_skinning)
xr_strcat(name, "_4");

return CreateShader<SVS>(name, shader, fallbackShader);
return CreateShader<SVS>(name, shader, fallbackShader, flags);
}

void CResourceManager::_DeleteVS(const SVS* vs) { DestroyShader(vs); }
Expand Down
12 changes: 5 additions & 7 deletions src/Layers/xrRender/ShaderResourceTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ inline CResourceManager::map_CS& CResourceManager::GetShaderMap()

template <typename T>
T* CResourceManager::CreateShader(cpcstr name, pcstr filename /*= nullptr*/,
pcstr fallbackShader /*= nullptr*/)
pcstr fallbackShader /*= nullptr*/, u32 flags /*= 0*/)
{
typename ShaderTypeTraits<T>::MapType& sh_map = GetShaderMap<typename ShaderTypeTraits<T>::MapType>();
LPSTR N = LPSTR(name);
Expand Down Expand Up @@ -656,12 +656,10 @@ T* CResourceManager::CreateShader(cpcstr name, pcstr filename /*= nullptr*/,
pcstr c_target, c_entry;
ShaderTypeTraits<T>::GetCompilationTarget(c_target, c_entry, data);

#ifdef USE_OGL
DWORD flags = 0;
#elif defined(USE_DX10) || defined(USE_DX11)
DWORD flags = D3D10_SHADER_PACK_MATRIX_ROW_MAJOR;
#else
DWORD flags = D3DXSHADER_DEBUG | D3DXSHADER_PACKMATRIX_ROWMAJOR;
#if defined(USE_DX10) || defined(USE_DX11)
flags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
#elif defined(USE_DX9)
flags |= D3DXSHADER_DEBUG | D3DXSHADER_PACKMATRIX_ROWMAJOR;
#endif

// Compile
Expand Down
6 changes: 3 additions & 3 deletions src/Layers/xrRender/blenders/Blender_Recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ class CBlender_Compile
#endif

#if defined(USE_DX10) || defined(USE_DX11)
void r_dx10Texture(LPCSTR ResourceName, LPCSTR texture);
void r_dx10Texture(LPCSTR ResourceName, shared_str texture)
void r_dx10Texture(LPCSTR ResourceName, LPCSTR texture, bool recursive = false);
void r_dx10Texture(LPCSTR ResourceName, shared_str texture, bool recursive = false)
{
return r_dx10Texture(ResourceName, texture.c_str());
return r_dx10Texture(ResourceName, texture.c_str(), recursive);
};
u32 r_dx10Sampler(LPCSTR ResourceName);
#endif // USE_DX10
Expand Down
57 changes: 43 additions & 14 deletions src/Layers/xrRender/r_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,57 @@
// R_constant_table::~R_constant_table () { dxRenderDeviceRender::Instance().Resources->_DeleteConstantTable(this);
// }

struct search_entry
{
cpcstr name;
const u16 type;
};

R_constant_table::~R_constant_table() { RImplementation.Resources->_DeleteConstantTable(this); }
void R_constant_table::fatal(LPCSTR S) { FATAL(S); }
// predicates
IC bool p_search(const ref_constant& C, cpcstr S) { return xr_strcmp(*C->name, S) < 0; }
IC bool p_sort(const ref_constant& C1, const ref_constant C2) { return xr_strcmp(C1->name, C2->name) < 0; }
ref_constant R_constant_table::get(LPCSTR S)
ref_constant R_constant_table::get(pcstr S, u16 type /*= u16(-1)*/)
{
// assumption - sorted by name
c_table::iterator I = std::lower_bound(table.begin(), table.end(), S, p_search);
if (I == table.end() || (0 != xr_strcmp(*(*I)->name, S)))
return nullptr;
c_table::iterator it;
if (type == u16(-1))
{
it = std::lower_bound(table.begin(), table.end(), S, p_search);
}
else
return *I;
{
it = std::find_if(table.begin(), table.end(), [&](const ref_constant& constant)
{
return 0 == xr_strcmp(constant->name.c_str(), S) && constant->type == type;
});
}

if (it == table.end() || (0 != xr_strcmp((*it)->name.c_str(), S)))
return nullptr;
return *it;
}
ref_constant R_constant_table::get(const shared_str& S)
ref_constant R_constant_table::get(const shared_str& S, u16 type /*= u16(-1)*/)
{
// linear search, but only ptr-compare
c_table::iterator I = table.begin();
c_table::iterator E = table.end();
for (; I != E; ++I)
if (type == u16(-1))
{
ref_constant C = *I;
if (C->name.equal(S))
return C;
for (const ref_constant& C : table)
{
if (C->name.equal(S))
return C;
}
}
else
{
for (const ref_constant& C : table)
{
if (C->name.equal(S) && C->type == type)
return C;
}
}

return nullptr;
}

Expand Down Expand Up @@ -174,12 +200,15 @@ void R_constant_table::merge(R_constant_table* T)
if (nullptr == T)
return;

if (T->dx9compatibility)
dx9compatibility = true;

// Real merge
for (u32 it = 0; it < T->table.size(); it++)
{
ref_constant src = T->table[it];
ref_constant C = get(*src->name);
if (!C)
ref_constant C = get(*src->name, dx9compatibility ? src->type : u16(-1));
if (!C || (dx9compatibility && C->type != src->type))
{
C = new R_constant(); //.g_constant_allocator.create();
C->name = src->name;
Expand Down
6 changes: 4 additions & 2 deletions src/Layers/xrRender/r_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class ECORE_API R_constant_setup
class ECORE_API R_constant_table : public xr_resource_flagged
{
public:
bool dx9compatibility{};

typedef xr_vector<ref_constant> c_table;
c_table table;

Expand All @@ -177,8 +179,8 @@ class ECORE_API R_constant_table : public xr_resource_flagged
void clear();
BOOL parse(void* desc, u32 destination);
void merge(R_constant_table* C);
ref_constant get(LPCSTR name); // slow search
ref_constant get(const shared_str& name); // fast search
ref_constant get(pcstr name, u16 type = u16(-1)); // slow search
ref_constant get(const shared_str& name, u16 type = u16(-1)); // fast search

BOOL equal(R_constant_table& C);
BOOL equal(R_constant_table* C) { return equal(*C); }
Expand Down
16 changes: 13 additions & 3 deletions src/Layers/xrRenderDX10/Blender_Recorder_R3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ void CBlender_Compile::r_Stencil(BOOL Enable, u32 Func, u32 Mask, u32 WriteMask,

void CBlender_Compile::r_StencilRef(u32 Ref) { RS.SetRS(D3DRS_STENCILREF, Ref); }
void CBlender_Compile::r_CullMode(D3DCULL Mode) { RS.SetRS(D3DRS_CULLMODE, (u32)Mode); }
void CBlender_Compile::r_dx10Texture(LPCSTR ResourceName, LPCSTR texture)
void CBlender_Compile::r_dx10Texture(LPCSTR ResourceName, LPCSTR texture, bool recursive /*= false*/)
{
if (ctable.dx9compatibility && !recursive)
{
const u32 stage = r_Sampler(ResourceName, texture);
if (stage != u16(-1))
return;
}

VERIFY(ResourceName);
if (!texture)
return;
Expand All @@ -41,7 +48,7 @@ void CBlender_Compile::r_dx10Texture(LPCSTR ResourceName, LPCSTR texture)
fix_texture_name(TexName);

// Find index
ref_constant C = ctable.get(ResourceName);
ref_constant C = ctable.get(ResourceName, ctable.dx9compatibility ? RC_dx10texture : u16(-1));
// VERIFY(C);
if (!C)
return;
Expand Down Expand Up @@ -139,7 +146,10 @@ void CBlender_Compile::r_Pass(LPCSTR _vs, LPCSTR _gs, LPCSTR _ps, bool bFog, BOO

// Create shaders
SPS* ps = RImplementation.Resources->_CreatePS(_ps);
SVS* vs = RImplementation.Resources->_CreateVS(_vs);
u32 flags = 0;
if (ps->constants.dx9compatibility)
flags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
SVS* vs = RImplementation.Resources->_CreateVS(_vs, nullptr, flags);
SGS* gs = RImplementation.Resources->_CreateGS(_gs);
dest.ps = ps;
dest.vs = vs;
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void CResourceManager::_DeletePass(const SPass* P)
}

//--------------------------------------------------------------------------------------------------------------
SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullptr*/)
SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullptr*/, u32 flags /*= 0*/)
{
string_path name;
xr_strcpy(name, shader);
Expand All @@ -130,7 +130,7 @@ SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullpt
if (4 == GEnv.Render->m_skinning)
xr_strcat(name, "_4");

return CreateShader<SVS>(name, shader, fallbackShader);
return CreateShader<SVS>(name, shader, fallbackShader, flags);
}

void CResourceManager::_DeleteVS(const SVS* vs)
Expand Down
2 changes: 1 addition & 1 deletion src/Layers/xrRenderDX10/dx10r_constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ BOOL R_constant_table::parseResources(ID3DShaderReflection* pReflection, int Res
VERIFY(0);
}

ref_constant C = get(ResDesc.Name);
ref_constant C = get(ResDesc.Name, dx9compatibility ? type : u16(-1));
if (!C)
{
C = table.emplace_back(new R_constant()); //.g_constant_allocator.create();
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRenderPC_GL/glResourceManager_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void CResourceManager::_DeleteDecl(const SDeclaration* dcl)
}

//--------------------------------------------------------------------------------------------------------------
SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullptr*/)
SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullptr*/, u32 flags /*= 0*/)
{
string_path name;
xr_strcpy(name, shader);
Expand All @@ -176,7 +176,7 @@ SVS* CResourceManager::_CreateVS(cpcstr shader, cpcstr fallbackShader /*= nullpt
if (4 == GEnv.Render->m_skinning)
xr_strcat(name, "_4");

return CreateShader<SVS>(name, shader, fallbackShader);
return CreateShader<SVS>(name, shader, fallbackShader, flags);
}

void CResourceManager::_DeleteVS(const SVS* vs) { DestroyShader(vs); }
Expand Down
Loading

0 comments on commit d38f18d

Please sign in to comment.