Browse Source

Extended memory loading support

Brian Fiete 6 months ago
parent
commit
29c0f82bba

+ 9 - 7
BeefySysLib/BeefySysLib.vcxproj

@@ -177,6 +177,7 @@
     <LibraryPath>third_party\AK\lib\release;$(VCInstallDir)lib;$(VCInstallDir)atlmfc\lib;$(WindowsSdkDir)lib;C:\Program Files (x86)\Microsoft DirectX SDK (June 2010)\Lib\x86;$(FrameworkSDKDir)\lib</LibraryPath>
     <IncludePath>$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath);$(WindowsSdkDir)include;$(FrameworkSDKDir)\include;C:\Program Files (x86)\Microsoft DirectX SDK (June 2007)\Include;C:\temp\wx\wxMSW-2.8.12\include;C:\temp\wx\wxMSW-2.8.12\include\msvc</IncludePath>
     <OutDir>$(SolutionDir)\IDE\dist\</OutDir>
+    <TargetName>$(ProjectName)64_Static</TargetName>
   </PropertyGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
     <ClCompile>
@@ -414,11 +415,12 @@ copy /y "$(OutDir)$(TargetName).lib" "$(SolutionDir)\BeefLibs\Beefy2D\dist\"</Co
       <PrecompiledHeader>
       </PrecompiledHeader>
       <Optimization>MaxSpeed</Optimization>
-      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <FunctionLevelLinking>false</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;BFSYSLIB_DYNAMIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;BFSYSLIB_DYNAMIC;BF_NO_FBX;FT2_BUILD_LIBRARY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
-      <AdditionalIncludeDirectories>./; ./platform/win/; third_party/agg-2.4/include; third_party/agg-2.4/include/platform/win32; third_party/; third_party/libffi/i686-pc-cygwin; third_party/libffi/i686-pc-cygwin/include; third_party/libffi/include</AdditionalIncludeDirectories>
+      <AdditionalIncludeDirectories>./;./platform/win/;./platform/sdl/;third_party/agg-2.4/include;third_party/agg-2.4/include/platform/win32;third_party/;third_party/libffi/i686-pc-cygwin;third_party/libffi/i686-pc-cygwin/include;third_party/libffi/include;third_party/SDL2-2.0.1/include;../extern/fbxsdk/include;third_party/freetype/include;../BeefLibs/SDL2/include</AdditionalIncludeDirectories>
+      <WholeProgramOptimization>false</WholeProgramOptimization>
     </ClCompile>
     <Link>
       <SubSystem>Windows</SubSystem>
@@ -482,7 +484,7 @@ copy /y "$(OutDir)$(TargetName).lib" "$(SolutionDir)\BeefLibs\Beefy2D\dist\"</Co
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|x64'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
     </ClCompile>
     <ClCompile Include="platform\sdl\SdlBFApp.cpp">
@@ -492,7 +494,7 @@ copy /y "$(OutDir)$(TargetName).lib" "$(SolutionDir)\BeefLibs\Beefy2D\dist\"</Co
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|x64'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
     </ClCompile>
     <ClCompile Include="platform\win\CrashCatcher.cpp" />
@@ -2001,7 +2003,7 @@ copy /y "$(OutDir)$(TargetName).lib" "$(SolutionDir)\BeefLibs\Beefy2D\dist\"</Co
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|x64'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
     </ClInclude>
     <ClInclude Include="platform\sdl\SdlBFApp.h">
@@ -2010,7 +2012,7 @@ copy /y "$(OutDir)$(TargetName).lib" "$(SolutionDir)\BeefLibs\Beefy2D\dist\"</Co
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|Win32'">true</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug Static|x64'">true</ExcludedFromBuild>
-      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release Static|x64'">false</ExcludedFromBuild>
       <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
     </ClInclude>
     <ClInclude Include="platform\win\CrashCatcher.h" />

+ 11 - 1
BeefySysLib/Common.cpp

@@ -1380,6 +1380,16 @@ void Beefy::BFFatalError(const char* message, const char* file, int line)
 	BFFatalError(String(message), String(file), line);
 }
 
-void MakeUpper(const StringImpl& theString)
+bool Beefy::ParseMemorySpan(const StringImpl& str, void*& outPtr, int& outSize)
 {
+	if (str.StartsWith("@"))
+	{
+		int colon = (int)str.IndexOf(':');
+		String addrStr = str.Substring(1, colon - 1);
+		String lenStr = str.Substring(colon + 1);
+		outPtr = (void*)(intptr)strtoll(addrStr.c_str(), NULL, 16);
+		outSize = (int)strtol(lenStr.c_str(), NULL, 10);
+		return true;
+	}
+	return false;
 }

+ 1 - 0
BeefySysLib/Common.h

@@ -242,6 +242,7 @@ bool FileExists(const StringImpl& path, String* outActualName = NULL);
 bool DirectoryExists(const StringImpl& path, String* outActualName = NULL);
 bool RecursiveCreateDirectory(const StringImpl& dirName);
 bool RecursiveDeleteDirectory(const StringImpl& dirName);
+bool ParseMemorySpan(const StringImpl& str, void*& outPtr, int& outSize);
 
 #define CHARTAG(val) FromBIGEndian(val)
 

+ 13 - 2
BeefySysLib/gfx/FTFont.cpp

@@ -147,13 +147,24 @@ bool FTFont::Load(const StringImpl& fileName, float pointSize)
 
 		String useFileName = fileName;
 		int faceIdx = 0;
-		int atPos = (int)useFileName.IndexOf('@');
+		int atPos = (int)useFileName.IndexOf('@', 1);
 		if (atPos != -1)
 		{
 			faceIdx = atoi(useFileName.c_str() + atPos + 1);
 			useFileName.RemoveToEnd(atPos);
 		}
-		auto error = FT_New_Face(gFTLibrary, useFileName.c_str(), faceIdx, &ftFace);
+
+		void* memPtr = NULL;
+		int memLen = 0;
+		if (ParseMemorySpan(fileName, memPtr, memLen))		
+		{			
+			FT_New_Memory_Face(gFTLibrary, (FT_Byte*)memPtr, memLen, faceIdx, &ftFace);
+		}
+		else
+		{ 
+			FT_New_Face(gFTLibrary, useFileName.c_str(), faceIdx, &ftFace);
+		}
+		
 		face->mFTFace = ftFace;
 	}
 	else

+ 6 - 8
BeefySysLib/gfx/RenderDevice.cpp

@@ -150,14 +150,12 @@ Texture* RenderDevice::LoadTexture(const StringImpl& fileName, int flags)
 	if (!handled)
 	{
 		imageData->mWantsAlphaPremultiplied = (flags & TextureFlag_NoPremult) == 0;
-		if (fileName.StartsWith("@"))
-		{
-			int colon = (int)fileName.IndexOf(':');
-			String addrStr = fileName.Substring(1, colon - 1);
-			String lenStr = fileName.Substring(colon + 1);
-			void* addr = (void*)(intptr)strtoll(addrStr.c_str(), NULL, 16);
-			int len = (int)strtol(lenStr.c_str(), NULL, 10);
-			if (!imageData->LoadFromMemory(addr, len))
+
+		void* memPtr = NULL;
+		int memLen = 0;
+		if (ParseMemorySpan(fileName, memPtr, memLen))
+		{			
+			if (!imageData->LoadFromMemory(memPtr, memLen))
 			{
 				failed = true;
 				delete imageData;

+ 95 - 14
BeefySysLib/platform/win/DXRenderDevice.cpp

@@ -5,6 +5,7 @@
 #include "img/ImageData.h"
 #include "util/PerfTimer.h"
 #include "util/BeefPerf.h"
+#include "Span.h"
 #include "FileStream.h"
 #include "DDS.h"
 
@@ -225,12 +226,14 @@ void DXShader::ReleaseNative()
 	mConstBuffer = NULL;
 }
 
-extern "C"
-typedef HRESULT(WINAPI* Func_D3DX10CompileFromFileW)(LPCWSTR pSrcFile, CONST D3D10_SHADER_MACRO* pDefines, LPD3D10INCLUDE pInclude,
+extern "C" typedef HRESULT(WINAPI* Func_D3DX10CompileFromFileW)(LPCWSTR pSrcFile, CONST D3D10_SHADER_MACRO* pDefines, LPD3D10INCLUDE pInclude,
 	LPCSTR pFunctionName, LPCSTR pProfile, UINT Flags1, UINT Flags2, ID3D10Blob** ppShader, ID3D10Blob** ppErrorMsgs);
-
 static Func_D3DX10CompileFromFileW gFunc_D3DX10CompileFromFileW;
 
+extern "C" typedef HRESULT(WINAPI* Func_D3DX10Compile)(void* srcData, size_t srcSize, char* sourceName, CONST D3D10_SHADER_MACRO* pDefines, LPD3D10INCLUDE pInclude,
+	LPCSTR pFunctionName, LPCSTR pProfile, UINT Flags1, UINT Flags2, ID3D10Blob** ppShader, ID3D10Blob** ppErrorMsgs);
+static Func_D3DX10Compile gFunc_D3DX10Compile;
+
 static bool LoadDXShader(const StringImpl& filePath, const StringImpl& entry, const StringImpl& profile, ID3D10Blob** outBuffer)
 {
 	HRESULT hr;
@@ -239,7 +242,7 @@ static bool LoadDXShader(const StringImpl& filePath, const StringImpl& entry, co
 	bool useCache = false;
 	auto srcDate = ::BfpFile_GetTime_LastWrite(filePath.c_str());
 	auto cacheDate = ::BfpFile_GetTime_LastWrite(outObj.c_str());
-	if (cacheDate >= srcDate)
+	if ((cacheDate != 0) && (cacheDate >= srcDate))
 		useCache = true;
 
 	if (!useCache)
@@ -257,16 +260,37 @@ static bool LoadDXShader(const StringImpl& filePath, const StringImpl& entry, co
 
 	if (!useCache)
 	{
-		if (gFunc_D3DX10CompileFromFileW == NULL)
-		{
-			auto lib = LoadLibraryA("D3DCompiler_47.dll");
-			if (lib != NULL)
-				gFunc_D3DX10CompileFromFileW = (Func_D3DX10CompileFromFileW)::GetProcAddress(lib, "D3DCompileFromFile");
-		}
+		bool useCompile = true;
 
+		HRESULT dxResult;
 		ID3D10Blob* errorMessage = NULL;
-		auto dxResult = gFunc_D3DX10CompileFromFileW(UTF8Decode(filePath).c_str(), NULL, NULL, entry.c_str(), profile.c_str(),
-			D3D10_SHADER_DEBUG | D3D10_SHADER_ENABLE_STRICTNESS, 0, outBuffer, &errorMessage);
+		if (useCompile)
+		{
+			if (gFunc_D3DX10Compile == NULL)
+			{
+				auto lib = LoadLibraryA("D3DCompiler_47.dll");
+				if (lib != NULL)
+					gFunc_D3DX10Compile = (Func_D3DX10Compile)::GetProcAddress(lib, "D3DCompile");
+			}
+
+			int memSize = 0;
+			uint8* memPtr = LoadBinaryData(filePath, &memSize);
+
+			dxResult = gFunc_D3DX10Compile(memPtr, memSize, "Shader", NULL, NULL, entry.c_str(), profile.c_str(),
+				D3D10_SHADER_DEBUG | D3D10_SHADER_ENABLE_STRICTNESS, 0, outBuffer, &errorMessage);
+		}
+		else
+		{
+			if (gFunc_D3DX10CompileFromFileW == NULL)
+			{
+				auto lib = LoadLibraryA("D3DCompiler_47.dll");
+				if (lib != NULL)
+					gFunc_D3DX10CompileFromFileW = (Func_D3DX10CompileFromFileW)::GetProcAddress(lib, "D3DCompileFromFile");
+			}
+			
+			dxResult = gFunc_D3DX10CompileFromFileW(UTF8Decode(filePath).c_str(), NULL, NULL, entry.c_str(), profile.c_str(),
+				D3D10_SHADER_DEBUG | D3D10_SHADER_ENABLE_STRICTNESS, 0, outBuffer, &errorMessage);
+		}				
 
 		if (DXFAILED(dxResult))
 		{
@@ -310,6 +334,36 @@ static bool LoadDXShader(const StringImpl& filePath, const StringImpl& entry, co
 	return true;
 }
 
+static bool LoadDXShader(Span<uint8> fileData, const StringImpl& entry, const StringImpl& profile, ID3D10Blob** outBuffer)
+{
+	HRESULT hr;
+	
+	if (gFunc_D3DX10Compile == NULL)
+	{
+		auto lib = LoadLibraryA("D3DCompiler_47.dll");
+		if (lib != NULL)
+			gFunc_D3DX10Compile = (Func_D3DX10Compile)::GetProcAddress(lib, "D3DCompile");
+	}
+
+	ID3D10Blob* errorMessage = NULL;
+	auto dxResult = gFunc_D3DX10Compile(fileData.mVals, fileData.mSize, "ShaderSource", NULL, NULL, entry.c_str(), profile.c_str(),
+		D3D10_SHADER_DEBUG | D3D10_SHADER_ENABLE_STRICTNESS, 0, outBuffer, &errorMessage);
+
+	if (DXFAILED(dxResult))
+	{
+		if (errorMessage != NULL)
+		{
+			BF_FATAL(StrFormat("Vertex shader load failed: %s", (char*)errorMessage->GetBufferPointer()).c_str());
+			errorMessage->Release();
+		}
+		else
+			BF_FATAL("Shader load failed");
+		return false;
+	}
+
+	return true;
+}
+
 bool DXShader::Load()
 {
 	//HRESULT hr;
@@ -318,8 +372,35 @@ bool DXShader::Load()
 	ID3D10Blob* vertexShaderBuffer = NULL;
 	ID3D10Blob* pixelShaderBuffer = NULL;
 
-	LoadDXShader(mSrcPath + ".fx", "VS", "vs_4_0", &vertexShaderBuffer);
-	LoadDXShader(mSrcPath + ".fx", "PS", "ps_4_0", &pixelShaderBuffer);
+	void* memPtr = NULL;
+	int memSize = 0;
+	if (ParseMemorySpan(mSrcPath, memPtr, memSize))
+	{		
+		int crPos = (int)mSrcPath.IndexOf('\n');
+		if (crPos != -1)
+		{
+			void* memPtr2 = NULL;
+			int memSize2 = 0;
+			if (ParseMemorySpan(mSrcPath.Substring(crPos + 1), memPtr2, memSize2))
+			{
+				D3D10CreateBlob(memSize, &vertexShaderBuffer);
+				memcpy(vertexShaderBuffer->GetBufferPointer(), memPtr, memSize);
+				D3D10CreateBlob(memSize2, &pixelShaderBuffer);
+				memcpy(pixelShaderBuffer->GetBufferPointer(), memPtr2, memSize2);	
+			}
+		}
+		else
+		{
+			Span<uint8> span((uint8*)memPtr, memSize);
+			LoadDXShader(span, "VS", "vs_4_0", &vertexShaderBuffer);
+			LoadDXShader(span, "PS", "ps_4_0", &pixelShaderBuffer);
+		}
+	}
+	else
+	{
+		LoadDXShader(mSrcPath + ".fx", "VS", "vs_4_0", &vertexShaderBuffer);
+		LoadDXShader(mSrcPath + ".fx", "PS", "ps_4_0", &pixelShaderBuffer);
+	}
 
 	defer(
 		{

+ 1 - 1
BeefySysLib/util/BeefPerf.h

@@ -320,7 +320,7 @@ public:
 #define BP_EXPORT BF_EXPORT
 #define BP_CALLTYPE BF_CALLTYPE
 #else
-#define BP_EXPORT
+#define BP_EXPORT extern "C"
 #define BP_CALLTYPE
 #endif