Skip to content

Commit

Permalink
R3/R4: create pixel shader using ShaderTypeTraits
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed May 28, 2018
1 parent 995fe8f commit 5708cd2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 168 deletions.
63 changes: 63 additions & 0 deletions src/Layers/xrRender/ShaderResourceTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,63 @@ template <typename T>
struct ShaderTypeTraits;

#if defined(USE_DX10) || defined(USE_DX11)
template <>
struct ShaderTypeTraits<SPS>
{
typedef CResourceManager::map_PS MapType;
typedef ID3DPixelShader DXIface;

static inline const char* GetShaderExt() { return ".ps"; }
static inline const char* GetCompilationTarget()
{
return "ps_2_0";
}

static void GetCompilationTarget(const char*& target, const char*& entry, const char* data)
{
if (strstr(data, "main_ps_1_1"))
{
target = "ps_1_1";
entry = "main_ps_1_1";
}
if (strstr(data, "main_ps_1_2"))
{
target = "ps_1_2";
entry = "main_ps_1_2";
}
if (strstr(data, "main_ps_1_3"))
{
target = "ps_1_3";
entry = "main_ps_1_3";
}
if (strstr(data, "main_ps_1_4"))
{
target = "ps_1_4";
entry = "main_ps_1_4";
}
if (strstr(data, "main_ps_2_0"))
{
target = "ps_2_0";
entry = "main_ps_2_0";
}
}

static inline DXIface* CreateHWShader(DWORD const* buffer, size_t size)
{
DXIface* ps = 0;
#ifdef USE_DX11
R_CHK(HW.pDevice->CreatePixelShader(buffer, size, 0, &ps));
#elif defined(USE_DX10)
R_CHK(HW.pDevice->CreatePixelShader(buffer, size, &ps));
#else
R_CHK(HW.pDevice->CreatePixelShader(buffer, &ps);
#endif
return ps;
}

static inline u32 GetShaderDest() { return RC_dest_pixel; }
};

template <>
struct ShaderTypeTraits<SGS>
{
Expand Down Expand Up @@ -133,6 +190,12 @@ struct ShaderTypeTraits<SCS>
};
#endif

template <>
inline CResourceManager::map_PS& CResourceManager::GetShaderMap()
{
return m_ps;
}

#if defined(USE_DX10) || defined(USE_DX11)
template <>
inline CResourceManager::map_GS& CResourceManager::GetShaderMap()
Expand Down
100 changes: 2 additions & 98 deletions src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,107 +258,11 @@ SPS* CResourceManager::_CreatePS(LPCSTR _name)
xr_strcat(name, "_6");
if (7 == GEnv.Render->m_MSAASample)
xr_strcat(name, "_7");
LPSTR N = LPSTR(name);
map_PS::iterator I = m_ps.find(N);
if (I != m_ps.end())
return I->second;
else
{
SPS* _ps = new SPS();
_ps->dwFlags |= xr_resource_flagged::RF_REGISTERED;
m_ps.insert(std::make_pair(_ps->set_name(name), _ps));
if (0 == xr_stricmp(_name, "null"))
{
_ps->sh = NULL;
return _ps;
}

string_path shName;
const char* pchr = strchr(_name, '(');
ptrdiff_t strSize = pchr ? pchr - _name : xr_strlen(_name);
strncpy(shName, _name, strSize);
shName[strSize] = 0;

// Open file
string_path cname;
strconcat(sizeof(cname), cname, GEnv.Render->getShaderPath(), /*_name*/ shName, ".ps");
FS.update_path(cname, "$game_shaders$", cname);

// duplicate and zero-terminate
IReader* file = FS.r_open(cname);
// TODO: DX10: HACK: Implement all shaders. Remove this for PS
if (!file)
{
string1024 tmp;
// TODO: HACK: Test failure
// Memory.mem_compact();
xr_sprintf(tmp, "DX10: %s is missing. Replace with stub_default.ps", cname);
Msg(tmp);
strconcat(sizeof(cname), cname, GEnv.Render->getShaderPath(), "stub_default", ".ps");
FS.update_path(cname, "$game_shaders$", cname);
file = FS.r_open(cname);
}
R_ASSERT2(file, cname);
u32 const size = file->length();
char* const data = (LPSTR)_alloca(size + 1);
CopyMemory(data, file->pointer(), size);
data[size] = 0;
FS.r_close(file);

// Select target
LPCSTR c_target = "ps_2_0";
LPCSTR c_entry = "main";
if (strstr(data, "main_ps_1_1"))
{
c_target = "ps_1_1";
c_entry = "main_ps_1_1";
}
if (strstr(data, "main_ps_1_2"))
{
c_target = "ps_1_2";
c_entry = "main_ps_1_2";
}
if (strstr(data, "main_ps_1_3"))
{
c_target = "ps_1_3";
c_entry = "main_ps_1_3";
}
if (strstr(data, "main_ps_1_4"))
{
c_target = "ps_1_4";
c_entry = "main_ps_1_4";
}
if (strstr(data, "main_ps_2_0"))
{
c_target = "ps_2_0";
c_entry = "main_ps_2_0";
}

HRESULT const _hr = GEnv.Render->shader_compile(
name, (DWORD const*)data, size, c_entry, c_target, D3D10_SHADER_PACK_MATRIX_ROW_MAJOR, (void*&)_ps);

VERIFY(SUCCEEDED(_hr));

CHECK_OR_EXIT(!FAILED(_hr), "Your video card doesn't meet game requirements.\n\nTry to lower game settings.");

return _ps;
}
}

void CResourceManager::_DeletePS(const SPS* ps)
{
if (0 == (ps->dwFlags & xr_resource_flagged::RF_REGISTERED))
return;
LPSTR N = LPSTR(*ps->cName);
map_PS::iterator I = m_ps.find(N);
if (I != m_ps.end())
{
m_ps.erase(I);
return;
}
Msg("! ERROR: Failed to find compiled pixel-shader '%s'", *ps->cName);
return CreateShader<SPS>(name, true);
}

void CResourceManager::_DeletePS(const SPS* ps) { DestroyShader(ps); }
//--------------------------------------------------------------------------------------------------------------
static BOOL dcl_equal(D3DVERTEXELEMENT9* a, D3DVERTEXELEMENT9* b)
{
Expand Down
36 changes: 1 addition & 35 deletions src/Layers/xrRenderPC_R3/r3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,41 +796,7 @@ static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 cons
HRESULT _result = E_FAIL;
if (pTarget[0] == 'p')
{
SPS* sps_result = (SPS*)result;
#ifdef USE_DX11
_result = HW.pDevice->CreatePixelShader(buffer, buffer_size, 0, &sps_result->sh);
#else // #ifdef USE_DX11
_result = HW.pDevice->CreatePixelShader(buffer, buffer_size, &sps_result->sh);
#endif // #ifdef USE_DX11
if (!SUCCEEDED(_result))
{
Log("! PS: ", file_name);
Msg("! CreatePixelShader hr == 0x%08x", _result);
return E_FAIL;
}

ID3DShaderReflection* pReflection = 0;

#ifdef USE_DX11
_result = D3DReflect(buffer, buffer_size, IID_ID3DShaderReflection, (void**)&pReflection);
#else
_result = D3D10ReflectShader(buffer, buffer_size, &pReflection);
#endif

// Parse constant, texture, sampler binding
// Store input signature blob
if (SUCCEEDED(_result) && pReflection)
{
// Let constant table parse it's data
sps_result->constants.parse(pReflection, RC_dest_pixel);

_RELEASE(pReflection);
}
else
{
Log("! PS: ", file_name);
Msg("! D3DReflectShader hr == 0x%08x", _result);
}
_result = create_shader(pTarget, buffer, buffer_size, file_name, (SPS*&)result, disasm);
}
else if (pTarget[0] == 'v')
{
Expand Down
36 changes: 1 addition & 35 deletions src/Layers/xrRenderPC_R4/r4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,41 +813,7 @@ static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 cons
HRESULT _result = E_FAIL;
if (pTarget[0] == 'p')
{
SPS* sps_result = (SPS*)result;
#ifdef USE_DX11
_result = HW.pDevice->CreatePixelShader(buffer, buffer_size, 0, &sps_result->sh);
#else // #ifdef USE_DX11
_result = HW.pDevice->CreatePixelShader(buffer, buffer_size, &sps_result->sh);
#endif // #ifdef USE_DX11
if (!SUCCEEDED(_result))
{
Log("! PS: ", file_name);
Msg("! CreatePixelShader hr == 0x%08x", _result);
return E_FAIL;
}

ID3DShaderReflection* pReflection = 0;

#ifdef USE_DX11
_result = D3DReflect(buffer, buffer_size, IID_ID3DShaderReflection, (void**)&pReflection);
#else
_result = D3D10ReflectShader(buffer, buffer_size, &pReflection);
#endif

// Parse constant, texture, sampler binding
// Store input signature blob
if (SUCCEEDED(_result) && pReflection)
{
// Let constant table parse it's data
sps_result->constants.parse(pReflection, RC_dest_pixel);

_RELEASE(pReflection);
}
else
{
Log("! PS: ", file_name);
Msg("! D3DReflectShader hr == 0x%08x", _result);
}
_result = create_shader(pTarget, buffer, buffer_size, file_name, (SPS*&)result, disasm);
}
else if (pTarget[0] == 'v')
{
Expand Down

0 comments on commit 5708cd2

Please sign in to comment.