-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderInjectionSystem.cpp
82 lines (70 loc) · 2 KB
/
ShaderInjectionSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "ShaderInjectionSystem.h"
void SetupShaderInjection(ID3D11Device* device) {
ShaderInjectionSystem injector(device);
// Register custom injector
injector.RegisterInjector("LIGHTING", [](const std::string& code) {
return "#define ENABLE_PBR 1\n"
"#define MAX_LIGHTS 8\n" +
code;
});
// Add injection points
injector.AddInjectionPoint("PixelShader.hlsl", {
"// @INJECTION_POINT: LIGHTING",
R"(
float3 CalculatePBR(float3 albedo, float metallic, float roughness) {
// PBR calculation code
return albedo;
}
)",
true
});
// Create shader variant
ShaderVariant highQualityVariant;
highQualityVariant.name = "HighQuality";
highQualityVariant.defines = {
"MAX_LIGHTS=16",
"ENABLE_SHADOWS=1",
"ENABLE_AO=1"
};
highQualityVariant.injectionPoints = {
{
"// @INJECTION_POINT: SHADOW",
R"(
float CalculateShadow(float4 position) {
// High quality shadow calculation
return 1.0f;
}
)",
true
}
};
injector.CreateVariant("PixelShader.hlsl", highQualityVariant);
// Compile shader with variant
auto shader = injector.CompileVertexShader(
"VertexShader.hlsl",
"HighQuality"
);
// Runtime patching
injector.PatchShader(
"PixelShader.hlsl",
"// @INJECTION_POINT: LIGHTING",
R"(
float3 CalculateLighting(float3 normal, float3 position) {
// New lighting calculation
return float3(1, 1, 1);
}
)"
);
}
// Example shader code (PixelShader.hlsl):
/*
cbuffer Constants : register(b0) {
float4x4 WorldViewProj;
}
// @INJECTION_POINT: LIGHTING
float4 main(VSOutput input) : SV_Target {
float4 color = input.Color;
// @INJECTION_POINT: SHADOW
return color;
}
*/