Shader Replacement
By implementing OnCreateShader(), you can intercept and replace a shader at the point it's loaded.
An example of this process could go like this:
- Open Monster Hunter World in RenderDoc.
- Make a capture and find the shader you want to edit.
- Click View to inspect its bytecode and get its hash, click Save to write it to a file.
- Disassemble the shader with
cmd_Decompiler.exefrom 3Dmigoto.
.\cmd_Decompiler.exe -d target_shader.dxbc
- Edit the DXBC bytecode in
target_shader.asm. - Reassemble the shader.
.\cmd_Decompiler.exe -a target_shader.asm
- Use
target_shader.shdrlike the example below.
using SharpPluginLoader.Core;
using SharpPluginLoader.Core.Rendering;
public class Plugin : IPlugin
{
public unsafe void OnCreateShader(ShaderInfo* info)
{
string hash = new string(info->DxbcHash);
if (hash == "ca33bab6-2119df84-41786a69-404471cd")
{
if (info->Replacement.Source != null)
{
Log.Warn("Color grading shader already has a replacement.");
return;
}
string path = "nativePC/plugins/CSharp/Shaders/color_grading.shdr";
if (!File.Exists(path))
{
Log.Warn($"File not found: {path}.");
return;
}
byte[] bytes;
try
{
bytes = File.ReadAllBytes(path);
}
catch (Exception e)
{
Log.Error($"Failed to read file: {path} ({e.Message}).");
return;
}
// Can also use ShaderSourceType.HLSL, supply text and it will be compiled by D3DCompile().
info->Replacement.Set(bytes, ShaderSourceType.Binary);
Log.Info("Color grading shader replaced.");
}
}
}
You can also use 3Dmigoto to decompile a shader to HLSL and try to get that running in RenderDoc to quickly view your changes.
.\cmd_Decompiler.exe -D target_shader.dxbc
You'll have to do manual edits to fix the generated HLSL and likely run into bugs with RenderDoc and 3Dmigoto. It's still worth a try though, as it can be very useful.