Skip to content

Commit

Permalink
Recompile compiled shaders if sources are updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
qweasdd136963 committed Sep 29, 2018
1 parent 1864b71 commit b148872
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 109 deletions.
8 changes: 4 additions & 4 deletions src/Layers/xrRender/ShaderResourceTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,13 @@ inline T* CResourceManager::CreateShader(const char* name, const char* filename
FS.update_path(cname, "$game_shaders$", cname);
file = FS.r_open(cname);
}
R_ASSERT2(file, cname);
R_ASSERT3(file, "Shader file doesnt exist:", cname);

// Duplicate and zero-terminate
const auto 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 = ShaderTypeTraits<T>::GetCompilationTarget();
Expand All @@ -417,8 +416,9 @@ inline T* CResourceManager::CreateShader(const char* name, const char* filename
#endif

// Compile
HRESULT const _hr = GEnv.Render->shader_compile(name, (DWORD const*)data, size,
c_entry, c_target, flags, (void*&)sh);
HRESULT const _hr = GEnv.Render->shader_compile(name, file, c_entry, c_target, flags, (void*&)sh);

FS.r_close(file);

VERIFY(SUCCEEDED(_hr));

Expand Down
20 changes: 7 additions & 13 deletions src/Layers/xrRenderPC_GL/glResourceManager_Resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,13 @@ SVS* CResourceManager::_CreateVS(LPCSTR _name)
FS.update_path(cname, "$game_shaders$", cname);
file = FS.r_open(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
_vs->sh = glCreateShader(GL_VERTEX_SHADER);
void* _result = &_vs->sh;
HRESULT const _hr = GEnv.Render->shader_compile(name, (DWORD const*)data, size, nullptr, nullptr, NULL, _result);
HRESULT const _hr = GEnv.Render->shader_compile(name, file, nullptr, nullptr, NULL, _result);

FS.r_close(file);

VERIFY(SUCCEEDED(_hr));

Expand Down Expand Up @@ -291,16 +288,13 @@ SPS* CResourceManager::_CreatePS(LPCSTR _name)
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
_ps->sh = glCreateShader(GL_FRAGMENT_SHADER);
void* _result = &_ps->sh;
HRESULT const _hr = GEnv.Render->shader_compile(name, (DWORD const*)data, size, nullptr, nullptr, NULL, _result);
HRESULT const _hr = GEnv.Render->shader_compile(name, file, nullptr, nullptr, NULL, _result);

FS.r_close(file);

VERIFY(SUCCEEDED(_hr));

Expand Down Expand Up @@ -367,7 +361,7 @@ SGS* CResourceManager::_CreateGS(LPCSTR name)
// Select target
_gs->sh = glCreateShader(GL_GEOMETRY_SHADER);
void* _result = &_gs->sh;
HRESULT const _hr = GEnv.Render->shader_compile(name, (DWORD const*)file->pointer(), file->length(), nullptr,
HRESULT const _hr = GEnv.Render->shader_compile(name, file, nullptr,
nullptr, NULL, _result);

VERIFY(SUCCEEDED(_hr));
Expand Down
3 changes: 1 addition & 2 deletions src/Layers/xrRenderPC_GL/rgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ class CRender : public D3DXRenderBase
GLuint texture_load(LPCSTR fname, u32& msize, GLenum& ret_desc);
HRESULT shader_compile(
LPCSTR name,
DWORD const* pSrcData,
UINT SrcDataLen,
IReader* fs,
LPCSTR pFunctionName,
LPCSTR pTarget,
DWORD Flags,
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRenderPC_GL/rgl_shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct SHADER_MACRO
char *Define = "#define ", *Name = "\n", *Sep = "\t", *Definition = "\n", *EOL = "\n";
};

HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
HRESULT CRender::shader_compile(LPCSTR name, IReader* fs, LPCSTR pFunctionName,
LPCSTR pTarget, DWORD Flags, void*& result)
{
xr_vector<char*> source, includes;
Expand All @@ -77,7 +77,7 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
VERIFY(!Flags);

// open included files
load_includes((LPCSTR)pSrcData, SrcDataLen, source, includes);
load_includes((LPCSTR)fs->pointer(), fs->length(), source, includes);

char sh_name[MAX_PATH] = "";
u32 len = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRenderPC_R1/FStaticRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class CRender : public D3DXRenderBase
virtual void level_Unload() override;

virtual IDirect3DBaseTexture9* texture_load(LPCSTR fname, u32& msize);
virtual HRESULT shader_compile(LPCSTR name, const DWORD* pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
LPCSTR pTarget, DWORD Flags, void*& result) override;
virtual HRESULT shader_compile(LPCSTR name, IReader* fs, LPCSTR pFunctionName, LPCSTR pTarget, DWORD Flags,
void*& result) override;

// Information
virtual void DumpStatistics(class IGameFont& font, class IPerformanceAlert* alert) override;
Expand Down
45 changes: 27 additions & 18 deletions src/Layers/xrRenderPC_R1/FStaticRender_Shaders.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "FStaticRender.h"
#include "Layers/xrRender/ShaderResourceTraits.h"
#include "xrCore/FileCRC32.h"

template <typename T>
static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 const buffer_size, LPCSTR const file_name,
Expand Down Expand Up @@ -89,8 +90,7 @@ static inline bool match_shader_id(
LPCSTR const debug_shader_id, LPCSTR const full_shader_id, FS_FileSet const& file_set, string_path& result);


HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
LPCSTR pTarget, DWORD Flags, void*& result)
HRESULT CRender::shader_compile(LPCSTR name, IReader* fs, LPCSTR pFunctionName, LPCSTR pTarget, DWORD Flags, void*& result)
{
D3DXMACRO defines[128];
int def_it = 0;
Expand Down Expand Up @@ -177,14 +177,11 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData

HRESULT _result = E_FAIL;

char extension[3];
string_path folder_name, folder;
xr_strcpy(folder, "r1\\objects\\r1\\");
xr_strcat(folder, name);
xr_strcat(folder, ".");

char extension[3];
strncpy_s(extension, pTarget, 2);
xr_strcat(folder, extension);
strconcat(sizeof(folder), folder, "r1\\objects\\r1\\", name, ".", extension);

FS.update_path(folder_name, "$game_shaders$", folder);
xr_strcat(folder_name, "\\");
Expand All @@ -196,12 +193,7 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
if (!match_shader_id(name, sh_name, m_file_set, temp_file_name))
{
string_path file;
xr_strcpy(file, "shaders_cache\\r1\\");
xr_strcat(file, name);
xr_strcat(file, ".");
xr_strcat(file, extension);
xr_strcat(file, "\\");
xr_strcat(file, sh_name);
strconcat(sizeof(file), file, "shaders_cache\\r1\\", name, ".", extension, "\\", sh_name);
FS.update_path(file_name, "$app_data_root$", file);
}
else
Expand All @@ -210,15 +202,27 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
xr_strcat(file_name, temp_file_name);
}

string_path shadersFolder;
FS.update_path(shadersFolder, "$game_shaders$", GEnv.Render->getShaderPath());

u32 fileCrc = 0;
getFileCrc32(fs, shadersFolder, fileCrc);
fs->seek(0);

if (FS.exist(file_name))
{
IReader* file = FS.r_open(file_name);
if (file->length() > 4)
{
u32 crc = file->r_u32();
u32 crcComp = crc32(file->pointer(), file->elapsed());
if (crcComp == crc)
_result = create_shader(pTarget, (DWORD*)file->pointer(), file->elapsed(), file_name, result, o.disasm);
u32 savedFileCrc = file->r_u32();
if (savedFileCrc == fileCrc)
{
u32 savedBytecodeCrc = file->r_u32();
u32 bytecodeCrc = crc32(file->pointer(), file->elapsed());
if (bytecodeCrc == savedBytecodeCrc)
_result =
create_shader(pTarget, (DWORD*)file->pointer(), file->elapsed(), file_name, result, o.disasm);
}
}
file->close();
}
Expand All @@ -231,14 +235,19 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
LPD3DXCONSTANTTABLE pConstants = nullptr;
LPD3DXINCLUDE pInclude = (LPD3DXINCLUDE)&Includer;

_result = D3DXCompileShader((LPCSTR)pSrcData, SrcDataLen, defines, pInclude, pFunctionName, pTarget,
_result = D3DXCompileShader((LPCSTR)fs->pointer(), fs->length(), defines, pInclude, pFunctionName, pTarget,
Flags | D3DXSHADER_USE_LEGACY_D3DX9_31_DLL, &pShaderBuf, &pErrorBuf, &pConstants);
if (SUCCEEDED(_result))
{
IWriter* file = FS.w_open(file_name);

file->w_u32(fileCrc);

u32 crc = crc32(pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize());
file->w_u32(crc);

file->w(pShaderBuf->GetBufferPointer(), (u32)pShaderBuf->GetBufferSize());

FS.w_close(file);

_result = create_shader(pTarget, (DWORD*)pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize(),
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRenderPC_R2/r2.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ class CRender : public D3DXRenderBase
virtual void level_Unload();

virtual IDirect3DBaseTexture9* texture_load(LPCSTR fname, u32& msize);
virtual HRESULT shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
LPCSTR pTarget, DWORD Flags, void*& result);
virtual HRESULT shader_compile(
LPCSTR name, IReader* fs, LPCSTR pFunctionName, LPCSTR pTarget, DWORD Flags, void*& result);

// Information
virtual void DumpStatistics(class IGameFont& font, class IPerformanceAlert* alert) override;
Expand Down
46 changes: 28 additions & 18 deletions src/Layers/xrRenderPC_R2/r2_shaders.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "r2.h"
#include "Layers/xrRender/ShaderResourceTraits.h"
#include "xrCore/FileCRC32.h"

template <typename T>
static HRESULT create_shader(LPCSTR const pTarget, DWORD const* buffer, u32 const buffer_size, LPCSTR const file_name,
Expand Down Expand Up @@ -87,8 +88,8 @@ class includer : public ID3DXInclude
static inline bool match_shader_id(
LPCSTR const debug_shader_id, LPCSTR const full_shader_id, FS_FileSet const& file_set, string_path& result);

HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
LPCSTR pTarget, DWORD Flags, void*& result)
HRESULT CRender::shader_compile(
LPCSTR name, IReader* fs, LPCSTR pFunctionName, LPCSTR pTarget, DWORD Flags, void*& result)
{
D3DXMACRO defines[128];
int def_it = 0;
Expand Down Expand Up @@ -439,14 +440,11 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData

HRESULT _result = E_FAIL;

char extension[3];
string_path folder_name, folder;
xr_strcpy(folder, "r2\\objects\\r2\\");
xr_strcat(folder, name);
xr_strcat(folder, ".");

char extension[3];
strncpy_s(extension, pTarget, 2);
xr_strcat(folder, extension);
strconcat(sizeof(folder), folder, "r2\\objects\\r2\\", name, ".", extension);

FS.update_path(folder_name, "$game_shaders$", folder);
xr_strcat(folder_name, "\\");
Expand All @@ -458,12 +456,7 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
if (!match_shader_id(name, sh_name, m_file_set, temp_file_name))
{
string_path file;
xr_strcpy(file, "shaders_cache\\r2\\");
xr_strcat(file, name);
xr_strcat(file, ".");
xr_strcat(file, extension);
xr_strcat(file, "\\");
xr_strcat(file, sh_name);
strconcat(sizeof(file), file, "shaders_cache\\r2\\", name, ".", extension, "\\", sh_name);
FS.update_path(file_name, "$app_data_root$", file);
}
else
Expand All @@ -472,15 +465,27 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
xr_strcat(file_name, temp_file_name);
}

string_path shadersFolder;
FS.update_path(shadersFolder, "$game_shaders$", GEnv.Render->getShaderPath());

u32 fileCrc = 0;
getFileCrc32(fs, shadersFolder, fileCrc);
fs->seek(0);

if (FS.exist(file_name))
{
IReader* file = FS.r_open(file_name);
if (file->length() > 4)
{
u32 crc = file->r_u32();
u32 crcComp = crc32(file->pointer(), file->elapsed());
if (crcComp == crc)
_result = create_shader(pTarget, (DWORD*)file->pointer(), file->elapsed(), file_name, result, o.disasm);
u32 savedFileCrc = file->r_u32();
if (savedFileCrc == fileCrc)
{
u32 savedBytecodeCrc = file->r_u32();
u32 bytecodeCrc = crc32(file->pointer(), file->elapsed());
if (bytecodeCrc == savedBytecodeCrc)
_result =
create_shader(pTarget, (DWORD*)file->pointer(), file->elapsed(), file_name, result, o.disasm);
}
}
file->close();
}
Expand All @@ -501,14 +506,19 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
LPD3DXCONSTANTTABLE pConstants = nullptr;
LPD3DXINCLUDE pInclude = (LPD3DXINCLUDE)&Includer;

_result = D3DXCompileShader((LPCSTR)pSrcData, SrcDataLen, defines, pInclude, pFunctionName, pTarget,
_result = D3DXCompileShader((LPCSTR)fs->pointer(), fs->length(), defines, pInclude, pFunctionName, pTarget,
Flags | D3DXSHADER_USE_LEGACY_D3DX9_31_DLL, &pShaderBuf, &pErrorBuf, &pConstants);
if (SUCCEEDED(_result))
{
IWriter* file = FS.w_open(file_name);

file->w_u32(fileCrc);

u32 crc = crc32(pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize());
file->w_u32(crc);

file->w(pShaderBuf->GetBufferPointer(), (u32)pShaderBuf->GetBufferSize());

FS.w_close(file);

_result = create_shader(pTarget, (DWORD*)pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize(),
Expand Down
4 changes: 2 additions & 2 deletions src/Layers/xrRenderPC_R3/r3.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ class CRender : public D3DXRenderBase
virtual void level_Unload();

ID3DBaseTexture* texture_load(LPCSTR fname, u32& msize, bool bStaging = false);
virtual HRESULT shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
LPCSTR pTarget, DWORD Flags, void*& result);
virtual HRESULT shader_compile(
LPCSTR name, IReader* fs, LPCSTR pFunctionName, LPCSTR pTarget, DWORD Flags, void*& result);

// Information
virtual void DumpStatistics(class IGameFont& font, class IPerformanceAlert* alert) override;
Expand Down
Loading

0 comments on commit b148872

Please sign in to comment.