11#include " stdafx.h"
22#include " r2.h"
33#include " Layers/xrRender/ShaderResourceTraits.h"
4+ #include " xrCore/FileCRC32.h"
45
56template <typename T>
67static HRESULT create_shader (LPCSTR const pTarget, DWORD const * buffer, u32 const buffer_size, LPCSTR const file_name,
@@ -87,8 +88,8 @@ class includer : public ID3DXInclude
8788static inline bool match_shader_id (
8889 LPCSTR const debug_shader_id, LPCSTR const full_shader_id, FS_FileSet const & file_set, string_path& result);
8990
90- HRESULT CRender::shader_compile (LPCSTR name, DWORD const * pSrcData, UINT SrcDataLen, LPCSTR pFunctionName,
91- LPCSTR pTarget, DWORD Flags, void *& result)
91+ HRESULT CRender::shader_compile (
92+ LPCSTR name, IReader* fs, LPCSTR pFunctionName, LPCSTR pTarget, DWORD Flags, void *& result)
9293{
9394 D3DXMACRO defines[128 ];
9495 int def_it = 0 ;
@@ -439,14 +440,11 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
439440
440441 HRESULT _result = E_FAIL;
441442
443+ char extension[3 ];
442444 string_path folder_name, folder;
443- xr_strcpy (folder, " r2\\ objects\\ r2\\ " );
444- xr_strcat (folder, name);
445- xr_strcat (folder, " ." );
446445
447- char extension[3 ];
448446 strncpy_s (extension, pTarget, 2 );
449- xr_strcat ( folder, extension);
447+ strconcat ( sizeof ( folder), folder, " r2 \\ objects \\ r2 \\ " , name, " . " , extension);
450448
451449 FS.update_path (folder_name, " $game_shaders$" , folder);
452450 xr_strcat (folder_name, " \\ " );
@@ -458,12 +456,7 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
458456 if (!match_shader_id (name, sh_name, m_file_set, temp_file_name))
459457 {
460458 string_path file;
461- xr_strcpy (file, " shaders_cache\\ r2\\ " );
462- xr_strcat (file, name);
463- xr_strcat (file, " ." );
464- xr_strcat (file, extension);
465- xr_strcat (file, " \\ " );
466- xr_strcat (file, sh_name);
459+ strconcat (sizeof (file), file, " shaders_cache\\ r2\\ " , name, " ." , extension, " \\ " , sh_name);
467460 FS.update_path (file_name, " $app_data_root$" , file);
468461 }
469462 else
@@ -472,15 +465,27 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
472465 xr_strcat (file_name, temp_file_name);
473466 }
474467
468+ string_path shadersFolder;
469+ FS.update_path (shadersFolder, " $game_shaders$" , GEnv.Render ->getShaderPath ());
470+
471+ u32 fileCrc = 0 ;
472+ getFileCrc32 (fs, shadersFolder, fileCrc);
473+ fs->seek (0 );
474+
475475 if (FS.exist (file_name))
476476 {
477477 IReader* file = FS.r_open (file_name);
478478 if (file->length () > 4 )
479479 {
480- u32 crc = file->r_u32 ();
481- u32 crcComp = crc32 (file->pointer (), file->elapsed ());
482- if (crcComp == crc)
483- _result = create_shader (pTarget, (DWORD*)file->pointer (), file->elapsed (), file_name, result, o.disasm );
480+ u32 savedFileCrc = file->r_u32 ();
481+ if (savedFileCrc == fileCrc)
482+ {
483+ u32 savedBytecodeCrc = file->r_u32 ();
484+ u32 bytecodeCrc = crc32 (file->pointer (), file->elapsed ());
485+ if (bytecodeCrc == savedBytecodeCrc)
486+ _result =
487+ create_shader (pTarget, (DWORD*)file->pointer (), file->elapsed (), file_name, result, o.disasm );
488+ }
484489 }
485490 file->close ();
486491 }
@@ -501,14 +506,19 @@ HRESULT CRender::shader_compile(LPCSTR name, DWORD const* pSrcData, UINT SrcData
501506 LPD3DXCONSTANTTABLE pConstants = nullptr ;
502507 LPD3DXINCLUDE pInclude = (LPD3DXINCLUDE)&Includer;
503508
504- _result = D3DXCompileShader ((LPCSTR)pSrcData, SrcDataLen , defines, pInclude, pFunctionName, pTarget,
509+ _result = D3DXCompileShader ((LPCSTR)fs-> pointer (), fs-> length () , defines, pInclude, pFunctionName, pTarget,
505510 Flags | D3DXSHADER_USE_LEGACY_D3DX9_31_DLL, &pShaderBuf, &pErrorBuf, &pConstants);
506511 if (SUCCEEDED (_result))
507512 {
508513 IWriter* file = FS.w_open (file_name);
514+
515+ file->w_u32 (fileCrc);
516+
509517 u32 crc = crc32 (pShaderBuf->GetBufferPointer (), pShaderBuf->GetBufferSize ());
510518 file->w_u32 (crc);
519+
511520 file->w (pShaderBuf->GetBufferPointer (), (u32 )pShaderBuf->GetBufferSize ());
521+
512522 FS.w_close (file);
513523
514524 _result = create_shader (pTarget, (DWORD*)pShaderBuf->GetBufferPointer (), pShaderBuf->GetBufferSize (),
0 commit comments