Skip to content

Commit

Permalink
Implement support shader cache in editors
Browse files Browse the repository at this point in the history
  • Loading branch information
VodkaNET committed Oct 29, 2024
1 parent e76b36e commit dd1cb39
Showing 1 changed file with 52 additions and 7 deletions.
59 changes: 52 additions & 7 deletions src/Editors/xrECore/Editor/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ void CRender::Calculate()
#include "../../../Layers/xrRender/CHudInitializer.cpp"
#include "../../../Layers/xrRender/dxEnvironmentRender.h"
#include "../../../xrEngine/IGame_Level.h"
#include "../../../xrCore/git_version.h"

void CRender::Render()
{
Expand Down Expand Up @@ -855,6 +856,44 @@ HRESULT CRender::shader_compile(

HRESULT _result = E_FAIL;

char extension[3];
strncpy_s(extension, pTarget, 2);

string_path file_name;
{
string_path file;
xr_strcpy(file, "shaders_cache\\");
xr_strcat(file, _VER);
xr_strcat(file, "\\editor\\");
xr_strcat(file, name);
xr_strcat(file, ".");
xr_strcat(file, extension);
xr_strcat(file, "\\");
xr_strcat(file, sh_name);
FS.update_path(file_name, "$app_data_root$", file);
}

u32 const RealCodeCRC = crc32(pSrcData, SrcDataLen);
if (FS.exist(file_name) && ps_r__common_flags.test(RFLAG_USE_CACHE)) {
#ifdef DEBUG
Msg("compilied shader library found %s", file_name);
#endif // DEBUG

IReader* file = FS.r_open(file_name);
if (file->length() > 4)
{
u32 ShaderCRC = file->r_u32();
u32 CodeSRC = file->r_u32();

if (RealCodeCRC == CodeSRC) {
u32 const real_crc = crc32(file->pointer(), file->elapsed());
if (real_crc == ShaderCRC) {
_result = create_shader(pTarget, (DWORD*)file->pointer(), file->elapsed(), file_name, result, false);
}
}
}
file->close();
}

if (FAILED(_result))
{
Expand All @@ -863,21 +902,27 @@ HRESULT CRender::shader_compile(
LPD3DBLOB pErrorBuf = nullptr;

_result = D3DCompile(pSrcData, SrcDataLen,
"",
"",//nullptr, //LPCSTR pFileName, // NVPerfHUD bug workaround.
defines, &Includer, pFunctionName,
pTarget,
Flags, 0,
&pShaderBuf,
&pErrorBuf
);

if (SUCCEEDED(_result))
{

_result = create_shader(pTarget, (DWORD*)pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize(), name, result, false);
if (SUCCEEDED(_result)) {
if (ps_r__common_flags.test(RFLAG_USE_CACHE)) {
IWriter* file = FS.w_open(file_name);
u32 const crc = crc32(pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize());
file->w_u32(crc);
file->w_u32(RealCodeCRC);
file->w(pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize());
FS.w_close(file);
}
_result = create_shader(pTarget, (DWORD*)pShaderBuf->GetBufferPointer(), pShaderBuf->GetBufferSize(), file_name, result, false);
}
else
{
else {
Msg("! %s", file_name);
if (pErrorBuf)
Msg("! error: %s", (LPCSTR)pErrorBuf->GetBufferPointer());
else
Expand Down

0 comments on commit dd1cb39

Please sign in to comment.