Skip to content

Commit d757f8a

Browse files
committed
R1/R2/R3/R4: create vertex shader using ShaderResourceTraits
(R3/R4: only partial code replacement)
1 parent 2c2a2a1 commit d757f8a

File tree

7 files changed

+72
-201
lines changed

7 files changed

+72
-201
lines changed

src/Layers/xrRender/ResourceManager_Resources.cpp

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ void CResourceManager::_DeleteDecl(const SDeclaration* dcl)
157157
}
158158

159159
//--------------------------------------------------------------------------------------------------------------
160-
#ifndef _EDITOR
161160
SVS* CResourceManager::_CreateVS(LPCSTR _name)
162161
{
163162
string_path name;
@@ -172,86 +171,11 @@ SVS* CResourceManager::_CreateVS(LPCSTR _name)
172171
xr_strcat(name, "_3");
173172
if (4 == GEnv.Render->m_skinning)
174173
xr_strcat(name, "_4");
175-
LPSTR N = LPSTR(name);
176-
map_VS::iterator I = m_vs.find(N);
177-
if (I != m_vs.end())
178-
return I->second;
179-
else
180-
{
181-
SVS* _vs = new SVS();
182-
_vs->dwFlags |= xr_resource_flagged::RF_REGISTERED;
183-
m_vs.insert(std::make_pair(_vs->set_name(name), _vs));
184-
if (0 == xr_stricmp(_name, "null"))
185-
{
186-
_vs->sh = nullptr;
187-
return _vs;
188-
}
189-
190-
string_path cname;
191-
strconcat(sizeof(cname), cname, GEnv.Render->getShaderPath(), _name, ".vs");
192-
FS.update_path(cname, "$game_shaders$", cname);
193-
// LPCSTR target = NULL;
194-
195-
IReader* fs = FS.r_open(cname);
196-
R_ASSERT3(fs, "shader file doesnt exist", cname);
197-
198-
// Select target
199-
LPCSTR c_target = "vs_2_0";
200-
LPCSTR c_entry = "main";
201-
if (HW.Caps.geometry_major >= 2)
202-
c_target = "vs_2_0";
203-
else
204-
c_target = "vs_1_1";
205-
206-
// duplicate and zero-terminate
207-
IReader* file = FS.r_open(cname);
208-
R_ASSERT2(file, cname);
209-
u32 const size = file->length();
210-
char* const data = (LPSTR)_alloca(size + 1);
211-
CopyMemory(data, file->pointer(), size);
212-
data[size] = 0;
213-
FS.r_close(file);
214-
215-
if (strstr(data, "main_vs_1_1"))
216-
{
217-
c_target = "vs_1_1";
218-
c_entry = "main_vs_1_1";
219-
}
220-
if (strstr(data, "main_vs_2_0"))
221-
{
222-
c_target = "vs_2_0";
223-
c_entry = "main_vs_2_0";
224-
}
225-
226-
Msg("compiling shader %s", name);
227-
HRESULT const _hr = GEnv.Render->shader_compile(name, (DWORD const*)data, size, c_entry, c_target,
228-
D3DXSHADER_DEBUG | D3DXSHADER_PACKMATRIX_ROWMAJOR, (void*&)_vs);
229-
230-
if (FAILED(_hr))
231-
{
232-
FlushLog();
233-
}
234-
235-
CHECK_OR_EXIT(!FAILED(_hr), "Your video card doesn't meet game requirements.\n\nTry to lower game settings.");
236-
237-
return _vs;
238-
}
174+
175+
return CreateShader<SVS>(name, true);
239176
}
240-
#endif
241177

242-
void CResourceManager::_DeleteVS(const SVS* vs)
243-
{
244-
if (0 == (vs->dwFlags & xr_resource_flagged::RF_REGISTERED))
245-
return;
246-
LPSTR N = LPSTR(*vs->cName);
247-
map_VS::iterator I = m_vs.find(N);
248-
if (I != m_vs.end())
249-
{
250-
m_vs.erase(I);
251-
return;
252-
}
253-
Msg("! ERROR: Failed to find compiled vertex-shader '%s'", *vs->cName);
254-
}
178+
void CResourceManager::_DeleteVS(const SVS* vs) { DestroyShader(vs); }
255179

256180
SPS* CResourceManager::_CreatePS(LPCSTR name) { return CreateShader<SPS>(name, true); }
257181
void CResourceManager::_DeletePS(const SPS* ps) { DestroyShader(ps); }

src/Layers/xrRender/ShaderResourceTraits.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@
55
template <typename T>
66
struct ShaderTypeTraits;
77

8+
template <>
9+
struct ShaderTypeTraits<SVS>
10+
{
11+
typedef CResourceManager::map_VS MapType;
12+
typedef ID3DVertexShader DXIface;
13+
14+
static inline const char* GetShaderExt() { return ".vs"; }
15+
static inline const char* GetCompilationTarget()
16+
{
17+
return "vs_2_0";
18+
}
19+
20+
static void GetCompilationTarget(const char*& target, const char*& entry, const char* data)
21+
{
22+
if (HW.Caps.geometry_major >= 2)
23+
target = "vs_2_0";
24+
else
25+
target = "vs_1_1";
26+
27+
if (strstr(data, "main_vs_1_1"))
28+
{
29+
target = "vs_1_1";
30+
entry = "main_vs_1_1";
31+
}
32+
33+
if (strstr(data, "main_vs_2_0"))
34+
{
35+
target = "vs_2_0";
36+
entry = "main_vs_2_0";
37+
}
38+
}
39+
40+
static inline DXIface* CreateHWShader(DWORD const* buffer, size_t size)
41+
{
42+
DXIface* vs = 0;
43+
#ifdef USE_DX11
44+
R_CHK(HW.pDevice->CreateVertexShader(buffer, size, 0, &vs));
45+
#elif defined(USE_DX10)
46+
R_CHK(HW.pDevice->CreateVertexShader(buffer, size, &vs));
47+
#else
48+
R_CHK(HW.pDevice->CreateVertexShader(buffer, &vs));
49+
#endif
50+
return vs;
51+
}
52+
53+
static inline u32 GetShaderDest() { return RC_dest_vertex; }
54+
};
55+
856
template <>
957
struct ShaderTypeTraits<SPS>
1058
{
@@ -194,6 +242,12 @@ inline CResourceManager::map_PS& CResourceManager::GetShaderMap()
194242
return m_ps;
195243
}
196244

245+
template <>
246+
inline CResourceManager::map_VS& CResourceManager::GetShaderMap()
247+
{
248+
return m_vs;
249+
}
250+
197251
#if defined(USE_DX10) || defined(USE_DX11)
198252
template <>
199253
inline CResourceManager::map_GS& CResourceManager::GetShaderMap()
@@ -256,7 +310,7 @@ inline T* CResourceManager::CreateShader(const char* name, const bool searchForE
256310
ShaderTypeTraits<T>::GetShaderExt());
257311
FS.update_path(cname, "$game_shaders$", cname);
258312

259-
// duplicate and zero-terminate
313+
// Try to open
260314
IReader* file = FS.r_open(cname);
261315
if (!file && strstr(Core.Params, "-lack_of_shaders"))
262316
{
@@ -269,6 +323,7 @@ inline T* CResourceManager::CreateShader(const char* name, const bool searchForE
269323
}
270324
R_ASSERT2(file, cname);
271325

326+
// Duplicate and zero-terminate
272327
const auto size = file->length();
273328
char* const data = (LPSTR)_alloca(size + 1);
274329
CopyMemory(data, file->pointer(), size);

src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -134,85 +134,15 @@ SVS* CResourceManager::_CreateVS(LPCSTR _name)
134134
xr_strcat(name, "_3");
135135
if (4 == GEnv.Render->m_skinning)
136136
xr_strcat(name, "_4");
137-
LPSTR N = LPSTR(name);
138-
map_VS::iterator I = m_vs.find(N);
139-
if (I != m_vs.end())
140-
return I->second;
141-
else
142-
{
143-
SVS* _vs = new SVS();
144-
_vs->dwFlags |= xr_resource_flagged::RF_REGISTERED;
145-
m_vs.insert(std::make_pair(_vs->set_name(name), _vs));
146-
//_vs->sh = NULL;
147-
//_vs->signature = NULL;
148-
if (0 == xr_stricmp(_name, "null"))
149-
{
150-
return _vs;
151-
}
152-
153-
string_path shName;
154-
{
155-
const char* pchr = strchr(_name, '(');
156-
ptrdiff_t size = pchr ? pchr - _name : xr_strlen(_name);
157-
strncpy(shName, _name, size);
158-
shName[size] = 0;
159-
}
160-
161-
string_path cname;
162-
strconcat(sizeof(cname), cname, GEnv.Render->getShaderPath(), /*_name*/ shName, ".vs");
163-
FS.update_path(cname, "$game_shaders$", cname);
164-
// LPCSTR target = NULL;
165-
166-
// duplicate and zero-terminate
167-
IReader* file = FS.r_open(cname);
168-
// TODO: DX10: HACK: Implement all shaders. Remove this for PS
169-
if (!file)
170-
{
171-
string1024 tmp;
172-
xr_sprintf(tmp, "DX10: %s is missing. Replace with stub_default.vs", cname);
173-
Msg(tmp);
174-
strconcat(sizeof(cname), cname, GEnv.Render->getShaderPath(), "stub_default", ".vs");
175-
FS.update_path(cname, "$game_shaders$", cname);
176-
file = FS.r_open(cname);
177-
}
178-
u32 const size = file->length();
179-
char* const data = (LPSTR)_alloca(size + 1);
180-
CopyMemory(data, file->pointer(), size);
181-
data[size] = 0;
182-
FS.r_close(file);
183-
184-
// Select target
185-
LPCSTR c_target = "vs_2_0";
186-
LPCSTR c_entry = "main";
187-
if (HW.Caps.geometry_major >= 2)
188-
c_target = "vs_2_0";
189-
else
190-
c_target = "vs_1_1";
191-
192-
if (strstr(data, "main_vs_1_1"))
193-
{
194-
c_target = "vs_1_1";
195-
c_entry = "main_vs_1_1";
196-
}
197-
if (strstr(data, "main_vs_2_0"))
198-
{
199-
c_target = "vs_2_0";
200-
c_entry = "main_vs_2_0";
201-
}
202-
203-
HRESULT const _hr = GEnv.Render->shader_compile(
204-
name, (DWORD const*)data, size, c_entry, c_target, D3D10_SHADER_PACK_MATRIX_ROW_MAJOR, (void*&)_vs);
205-
206-
VERIFY(SUCCEEDED(_hr));
207-
208-
CHECK_OR_EXIT(!FAILED(_hr), "Your video card doesn't meet game requirements.\n\nTry to lower game settings.");
209-
210-
return _vs;
211-
}
137+
138+
return CreateShader<SVS>(name, true);
212139
}
213140

214141
void CResourceManager::_DeleteVS(const SVS* vs)
215142
{
143+
// XXX: try to use code below
144+
// DestroyShader(vs);
145+
216146
if (0 == (vs->dwFlags & xr_resource_flagged::RF_REGISTERED))
217147
return;
218148
LPSTR N = LPSTR(*vs->cName);

src/Layers/xrRenderPC_R1/FStaticRender.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,6 @@ template <typename T>
758758
static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 const buffer_size, LPCSTR const file_name,
759759
T*& result, bool const disasm)
760760
{
761-
// XXX: disasm it
762-
763761
result->sh = ShaderTypeTraits<T>::CreateHWShader(buffer, buffer_size);
764762

765763
LPCVOID data = nullptr;
@@ -789,27 +787,7 @@ static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 cons
789787
}
790788
else if (pTarget[0] == 'v')
791789
{
792-
SVS* svs_result = (SVS*)result;
793-
_result = HW.pDevice->CreateVertexShader(buffer, &svs_result->sh);
794-
if (!SUCCEEDED(_result))
795-
{
796-
Log("! VS: ", file_name);
797-
Msg("! CreatePixelShader hr == 0x%08x", _result);
798-
return E_FAIL;
799-
}
800-
801-
LPCVOID data = nullptr;
802-
_result = D3DXFindShaderComment(buffer, MAKEFOURCC('C', 'T', 'A', 'B'), &data, nullptr);
803-
if (SUCCEEDED(_result) && data)
804-
{
805-
LPD3DXSHADER_CONSTANTTABLE pConstants = LPD3DXSHADER_CONSTANTTABLE(data);
806-
svs_result->constants.parse(pConstants, 0x2);
807-
}
808-
else
809-
{
810-
Log("! VS: ", file_name);
811-
Msg("! D3DXFindShaderComment hr == 0x%08x", _result);
812-
}
790+
_result = create_shader(pTarget, buffer, buffer_size, file_name, (SVS*&)result, disasm);
813791
}
814792
else
815793
{

src/Layers/xrRenderPC_R2/r2.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,6 @@ template <typename T>
633633
static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 const buffer_size, LPCSTR const file_name,
634634
T*& result, bool const disasm)
635635
{
636-
// XXX: disasm it
637-
638636
result->sh = ShaderTypeTraits<T>::CreateHWShader(buffer, buffer_size);
639637

640638
LPCVOID data = nullptr;
@@ -664,27 +662,7 @@ static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 cons
664662
}
665663
else if (pTarget[0] == 'v')
666664
{
667-
SVS* svs_result = (SVS*)result;
668-
_result = HW.pDevice->CreateVertexShader(buffer, &svs_result->sh);
669-
if (!SUCCEEDED(_result))
670-
{
671-
Log("! VS: ", file_name);
672-
Msg("! CreatePixelShader hr == 0x%08x", _result);
673-
return E_FAIL;
674-
}
675-
676-
LPCVOID data = nullptr;
677-
_result = D3DXFindShaderComment(buffer, MAKEFOURCC('C', 'T', 'A', 'B'), &data, nullptr);
678-
if (SUCCEEDED(_result) && data)
679-
{
680-
LPD3DXSHADER_CONSTANTTABLE pConstants = LPD3DXSHADER_CONSTANTTABLE(data);
681-
svs_result->constants.parse(pConstants, 0x2);
682-
}
683-
else
684-
{
685-
Log("! VS: ", file_name);
686-
Msg("! D3DXFindShaderComment hr == 0x%08x", _result);
687-
}
665+
_result = create_shader(pTarget, buffer, buffer_size, file_name, (SVS*&)result, disasm);
688666
}
689667
else
690668
{

src/Layers/xrRenderPC_R3/r3.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 cons
800800
}
801801
else if (pTarget[0] == 'v')
802802
{
803+
// XXX: try to use code below
804+
// _result = create_shader(pTarget, buffer, buffer_size, file_name, (SVS*&)result, disasm);
805+
803806
SVS* svs_result = (SVS*)result;
804807
#ifdef USE_DX11
805808
_result = HW.pDevice->CreateVertexShader(buffer, buffer_size, 0, &svs_result->sh);

src/Layers/xrRenderPC_R4/r4.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,9 @@ static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 cons
817817
}
818818
else if (pTarget[0] == 'v')
819819
{
820+
// XXX: try to use code below
821+
// _result = create_shader(pTarget, buffer, buffer_size, file_name, (SVS*&)result, disasm);
822+
820823
SVS* svs_result = (SVS*)result;
821824
#ifdef USE_DX11
822825
_result = HW.pDevice->CreateVertexShader(buffer, buffer_size, 0, &svs_result->sh);

0 commit comments

Comments
 (0)