Skip to content

Commit

Permalink
Dynamically build pixel shader code
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed May 10, 2024
1 parent d8eb17b commit b9457b9
Show file tree
Hide file tree
Showing 16 changed files with 557 additions and 274 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7014
#define BUILD_NUMBER 7015
14 changes: 7 additions & 7 deletions Dllmain/Dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void WINAPI DxWrapperSettings(DXWAPPERSETTINGS *DxSettings)

typedef HMODULE(*LoadProc)(const char *ProxyDll, const char *MyDllName);

HMODULE LoadHookedDll(char *dllname, LoadProc Load, DWORD HookSystem32)
HMODULE LoadHookedDll(const char *dllname, LoadProc Load, DWORD HookSystem32)
{
HMODULE dll = Load(nullptr, Config.WrapperName.c_str());
HMODULE currentdll = GetModuleHandle(dllname);
Expand Down Expand Up @@ -291,7 +291,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
else
{
// Load dsound procs
char *dllname = dtypename[dtype.dsound];
const char *dllname = dtypename[dtype.dsound];
HMODULE dll = LoadHookedDll(dllname, Load, Config.DsoundHookSystem32);

// Hook dsound.dll -> DsoundWrapper
Expand Down Expand Up @@ -321,7 +321,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
else
{
// Load dinput procs
char *dllname = dtypename[dtype.dinput];
const char *dllname = dtypename[dtype.dinput];
HMODULE dll = LoadHookedDll(dllname, Load, Config.DinputHookSystem32);

// Hook dinput.dll APIs
Expand Down Expand Up @@ -351,7 +351,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
else
{
// Load dinput8 procs
char *dllname = dtypename[dtype.dinput8];
const char *dllname = dtypename[dtype.dinput8];
HMODULE dll = LoadHookedDll(dllname, Load, Config.Dinput8HookSystem32);

// Hook dinput8.dll APIs
Expand Down Expand Up @@ -394,7 +394,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
using namespace DdrawWrapper;

// Load ddraw procs
char *dllname = dtypename[dtype.ddraw];
const char *dllname = dtypename[dtype.ddraw];
HMODULE dll = LoadHookedDll(dllname, Load, Config.DdrawHookSystem32);

// Hook ddraw.dll APIs
Expand Down Expand Up @@ -466,7 +466,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
if (Config.RealWrapperMode != dtype.d3d8)
{
// Load d3d8 procs
char *dllname = dtypename[dtype.d3d8];
const char *dllname = dtypename[dtype.d3d8];
HMODULE dll = LoadHookedDll(dllname, Load, Config.D3d8HookSystem32);

// Hook d3d8.dll -> D3d8to9
Expand All @@ -490,7 +490,7 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpReserved)
if (Config.RealWrapperMode != dtype.d3d9)
{
// Load d3d9 procs
char *dllname = dtypename[dtype.d3d9];
const char *dllname = dtypename[dtype.d3d9];
HMODULE dll = LoadHookedDll(dllname, Load, Config.D3d9HookSystem32);

// Hook d3d9.dll -> D3d9Wrapper
Expand Down
53 changes: 53 additions & 0 deletions Libraries/d3dx9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#define WIN32_LEAN_AND_MEAN
#include "d3dx9.h"
#include "d3dx9_data.h"
#include <fstream>
#include <iostream>
#include <ostream>
#include "External\MemoryModule\MemoryModule.h"
#include "Utils\Utils.h"
#include "Logging\Logging.h"
Expand All @@ -26,6 +29,8 @@ typedef HRESULT(WINAPI* PFN_D3DXLoadSurfaceFromMemory)(LPDIRECT3DSURFACE9 pDestS
typedef HRESULT(WINAPI* PFN_D3DXLoadSurfaceFromSurface)(LPDIRECT3DSURFACE9 pDestSurface, const PALETTEENTRY* pDestPalette, const RECT* pDestRect, LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* pSrcRect, DWORD Filter, D3DCOLOR ColorKey);
typedef HRESULT(WINAPI* PFN_D3DXSaveSurfaceToFileInMemory)(LPD3DXBUFFER* ppDestBuf, D3DXIMAGE_FILEFORMAT DestFormat, LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* SrcRect);
typedef HRESULT(WINAPI* PFN_D3DXSaveTextureToFileInMemory)(LPD3DXBUFFER* ppDestBuf, D3DXIMAGE_FILEFORMAT DestFormat, LPDIRECT3DBASETEXTURE9 pSrcTexture, const PALETTEENTRY* pSrcPalette);
typedef HRESULT(WINAPI* PFN_D3DXCompileShaderFromFileA)(LPCSTR pSrcFile, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
typedef HRESULT(WINAPI* PFN_D3DXCompileShaderFromFileW)(LPCWSTR pSrcFile, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
typedef HRESULT(WINAPI* PFN_D3DXAssembleShader)(LPCSTR pSrcData, UINT SrcDataLen, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs);
typedef HRESULT(WINAPI* PFN_D3DXDisassembleShader)(const DWORD* pShader, BOOL EnableColorCode, LPCSTR pComments, LPD3DXBUFFER* ppDisassembly);

Expand All @@ -43,6 +48,8 @@ PFN_D3DXLoadSurfaceFromMemory p_D3DXLoadSurfaceFromMemory = nullptr;
PFN_D3DXLoadSurfaceFromSurface p_D3DXLoadSurfaceFromSurface = nullptr;
PFN_D3DXSaveSurfaceToFileInMemory p_D3DXSaveSurfaceToFileInMemory = nullptr;
PFN_D3DXSaveTextureToFileInMemory p_D3DXSaveTextureToFileInMemory = nullptr;
PFN_D3DXCompileShaderFromFileA p_D3DXCompileShaderFromFileA = nullptr;
PFN_D3DXCompileShaderFromFileW p_D3DXCompileShaderFromFileW = nullptr;
PFN_D3DXAssembleShader p_D3DXAssembleShader = nullptr;
PFN_D3DXDisassembleShader p_D3DXDisassembleShader = nullptr;

Expand Down Expand Up @@ -79,6 +86,8 @@ void LoadD3dx9()
p_D3DXLoadSurfaceFromSurface = reinterpret_cast<PFN_D3DXLoadSurfaceFromSurface>(MemoryGetProcAddress(d3dx9Module, "D3DXLoadSurfaceFromSurface"));
p_D3DXSaveSurfaceToFileInMemory = reinterpret_cast<PFN_D3DXSaveSurfaceToFileInMemory>(MemoryGetProcAddress(d3dx9Module, "D3DXSaveSurfaceToFileInMemory"));
p_D3DXSaveTextureToFileInMemory = reinterpret_cast<PFN_D3DXSaveTextureToFileInMemory>(MemoryGetProcAddress(d3dx9Module, "D3DXSaveTextureToFileInMemory"));
p_D3DXCompileShaderFromFileA = reinterpret_cast<PFN_D3DXCompileShaderFromFileA>(MemoryGetProcAddress(d3dx9Module, "D3DXCompileShaderFromFileA"));
p_D3DXCompileShaderFromFileW = reinterpret_cast<PFN_D3DXCompileShaderFromFileW>(MemoryGetProcAddress(d3dx9Module, "D3DXCompileShaderFromFileW"));
p_D3DXAssembleShader = reinterpret_cast<PFN_D3DXAssembleShader>(MemoryGetProcAddress(d3dx9Module, "D3DXAssembleShader"));
p_D3DXDisassembleShader = reinterpret_cast<PFN_D3DXDisassembleShader>(MemoryGetProcAddress(d3dx9Module, "D3DXDisassembleShader"));
p_D3DXFillTexture = reinterpret_cast<PFN_D3DXFillTexture>(MemoryGetProcAddress(d3dx9Module, "D3DXFillTexture"));
Expand Down Expand Up @@ -217,6 +226,50 @@ HRESULT WINAPI D3DXSaveTextureToFileInMemory(LPD3DXBUFFER* ppDestBuf, D3DXIMAGE_
return hr;
}

HRESULT WINAPI D3DXCompileShaderFromFileA(LPCSTR pSrcFile, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable)
{
Logging::LogDebug() << __FUNCTION__;

LoadD3dx9();

if (!p_D3DXCompileShaderFromFileA)
{
LOG_ONCE(__FUNCTION__ << " Error: Could not find ProcAddress!");
return D3DERR_INVALIDCALL;
}

HRESULT hr = p_D3DXCompileShaderFromFileA(pSrcFile, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);

if (FAILED(hr))
{
Logging::Log() << __FUNCTION__ << " Error: Failed to Save texture!";
}

return hr;
}

HRESULT WINAPI D3DXCompileShaderFromFileW(LPCWSTR pSrcFile, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable)
{
Logging::LogDebug() << __FUNCTION__;

LoadD3dx9();

if (!p_D3DXCompileShaderFromFileW)
{
LOG_ONCE(__FUNCTION__ << " Error: Could not find ProcAddress!");
return D3DERR_INVALIDCALL;
}

HRESULT hr = p_D3DXCompileShaderFromFileW(pSrcFile, pDefines, pInclude, pFunctionName, pProfile, Flags, ppShader, ppErrorMsgs, ppConstantTable);

if (FAILED(hr))
{
Logging::Log() << __FUNCTION__ << " Error: Failed to Save texture!";
}

return hr;
}

HRESULT WINAPI D3DXAssembleShader(LPCSTR pSrcData, UINT SrcDataLen, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs)
{
Logging::LogDebug() << __FUNCTION__;
Expand Down
6 changes: 6 additions & 0 deletions Libraries/d3dx9.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ typedef enum D3DXIMAGE_FILEFORMAT {
D3DXIFF_PFM = 8,
D3DXIFF_FORCE_DWORD = 0x7fffffff
} D3DXIMAGE_FILEFORMAT, * LPD3DXIMAGE_FILEFORMAT;

typedef interface ID3DXConstantTable ID3DXConstantTable;
typedef interface ID3DXConstantTable* LPD3DXCONSTANTTABLE;

// Define the D3DXVECTOR4 structure
struct D3DXVECTOR4
{
Expand Down Expand Up @@ -124,6 +128,8 @@ HRESULT WINAPI D3DXLoadSurfaceFromMemory(LPDIRECT3DSURFACE9 pDestSurface, const
HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface, const PALETTEENTRY* pDestPalette, const RECT* pDestRect, LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* pSrcRect, DWORD Filter, D3DCOLOR ColorKey);
HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(LPD3DXBUFFER* ppDestBuf, D3DXIMAGE_FILEFORMAT DestFormat, LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* SrcRect);
HRESULT WINAPI D3DXSaveTextureToFileInMemory(LPD3DXBUFFER* ppDestBuf, D3DXIMAGE_FILEFORMAT DestFormat, LPDIRECT3DBASETEXTURE9 pSrcTexture, const PALETTEENTRY* pSrcPalette);
HRESULT WINAPI D3DXCompileShaderFromFileA(LPCSTR pSrcFile, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
HRESULT WINAPI D3DXCompileShaderFromFileW(LPCWSTR pSrcFile, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE* ppConstantTable);
HRESULT WINAPI D3DXAssembleShader(LPCSTR pSrcData, UINT SrcDataLen, const D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs);
HRESULT WINAPI D3DXDisassembleShader(const DWORD* pShader, BOOL EnableColorCode, LPCSTR pComments, LPD3DXBUFFER* ppDisassembly);

Expand Down
31 changes: 31 additions & 0 deletions MakeShader/Logging/Logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#define LogDebug Log

#define LOG_ONCE(msg) \
{}

#define LOG_LIMIT(num, msg) \
{}

class Logging
{
public:
class Log
{
public:
template <typename T>
Log& operator<<(const T& value)
{
return *this; // Return Log object to support chaining
}
};
static const char* hex(int value)
{
return "";
}
static void LogFormat(const char* fmt, ...)
{}
static void LogFormat(const wchar_t* fmt, ...)
{}
};
130 changes: 130 additions & 0 deletions MakeShader/MakeShader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright (C) 2024 Elisha Riedlinger
*
* This software is provided 'as-is', without any express or implied warranty. In no event will the
* authors be held liable for any damages arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you wrote the
* original software. If you use this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
* being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#define INITGUID
#define WIN32_LEAN_AND_MEAN
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <d3d9.h>
#include "d3dx9.h"
#include "Settings\Settings.h"
#include "Utils\Utils.h"
#include "External\Hooking\Hook.h"

HINSTANCE hModule_dll = nullptr;

CONFIG Config;

bool Wrapper::ValidProcAddress(FARPROC) { return false; }

void* Hook::IATPatch(HMODULE, DWORD, const char*, void*, const char*, void*) { return nullptr; }

bool Hook::UnhookIATPatch(HMODULE, DWORD, const char*, void*, const char*, void*) { return false; }

bool Hook::UnIATPatchAll() { return false; }

// Function to compile HLSL file
bool CompileShader(const wchar_t* hlslFile, LPD3DXBUFFER* compiledShader)
{
LPD3DXBUFFER errorMessages = nullptr;
HRESULT hr = D3DXCompileShaderFromFileW(hlslFile, nullptr, nullptr, "main", "ps_2_0", 0, compiledShader, &errorMessages, nullptr);

if (FAILED(hr))
{
if (errorMessages)
{
std::cerr << "Shader compilation failed: " << static_cast<const char*>(errorMessages->GetBufferPointer()) << std::endl;
errorMessages->Release();
}
return false;
}

return true;
}

// Function to generate header file with shader byte array
bool GenerateHeaderFile(const char* hlslFileName, LPD3DXBUFFER compiledShader, const char* byteArrayName)
{
// Compute output header file path based on HLSL file name
std::string hlslFilePath(hlslFileName);
std::string headerFilePath = hlslFilePath.substr(0, hlslFilePath.find_last_of('.')) + ".h";

std::ofstream headerFile(headerFilePath);
if (!headerFile)
{
std::cerr << "Failed to create header file: " << headerFilePath << std::endl;
return false;
}

headerFile << "// Automatically generated header file\n\n";
headerFile << "#pragma once\n\n";

headerFile << "const unsigned char " << byteArrayName << "[] = {\n";
const unsigned char* bytes = static_cast<const unsigned char*>(compiledShader->GetBufferPointer());
const size_t byteCount = compiledShader->GetBufferSize();

for (size_t i = 0; i < byteCount; ++i)
{
headerFile << "0x" << std::hex << static_cast<int>(bytes[i]);
if (i != byteCount - 1)
headerFile << ", ";
if ((i + 1) % 16 == 0)
headerFile << "\n";
}

headerFile << "\n};\n";

headerFile.close();
return true;
}

// Main function
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cerr << "Usage: " << argv[0] << " <HLSL file path> <Byte array variable name>" << std::endl;
return 1;
}

const char* hlslFileName = argv[1];
const char* byteArrayName = argv[2];

std::wstring wHlslFileName;
wHlslFileName.assign(hlslFileName, hlslFileName + strlen(hlslFileName));

LPD3DXBUFFER compiledShader = nullptr;

if (!CompileShader(wHlslFileName.c_str(), &compiledShader))
{
std::cout << "Failed to compile shader." << std::endl;
return 1;
}

if (!GenerateHeaderFile(hlslFileName, compiledShader, byteArrayName))
{
compiledShader->Release();
std::cout << "Failed to create header." << std::endl;
return 1;
}

compiledShader->Release();

std::cout << "Shader successfully compiled and header file generated." << std::endl;

return 0;
}

0 comments on commit b9457b9

Please sign in to comment.