-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.py
More file actions
134 lines (119 loc) · 5.79 KB
/
Copy pathpatch.py
File metadata and controls
134 lines (119 loc) · 5.79 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import sys
import re
with open('src/interpolator.cpp', 'r') as f:
content = f.read()
# Insert ofstream definition at the top
if '#include <fstream>' not in content:
content = '#include <fstream>\n' + content
# Patch createTex function
new_createTex = '''auto createTex = [&](int w, int h, DXGI_FORMAT fmt,
Microsoft::WRL::ComPtr<ID3D11Texture2D>& tex,
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>& srv,
Microsoft::WRL::ComPtr<ID3D11UnorderedAccessView>& uav) {
if (!m_device) {
std::ofstream ofs("trace_interpolator.txt", std::ios::app);
ofs << "[createTex] m_device is null! w=" << w << " h=" << h << std::endl;
return;
}
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = w;
desc.Height = h;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = fmt;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
std::ofstream ofs("trace_interpolator.txt", std::ios::app);
ofs << "[createTex] Creating texture w=" << w << " h=" << h << " fmt=" << fmt << std::endl;
if (FAILED(m_device->CreateTexture2D(&desc, nullptr, &tex))) {
ofs << "[createTex] CreateTexture2D failed" << std::endl;
return;
}
if (FAILED(m_device->CreateShaderResourceView(tex.Get(), nullptr, &srv))) {
ofs << "[createTex] CreateSRV failed" << std::endl;
tex.Reset();
return;
}
if (FAILED(m_device->CreateUnorderedAccessView(tex.Get(), nullptr, &uav))) {
ofs << "[createTex] CreateUAV failed" << std::endl;
tex.Reset(); srv.Reset();
return;
}
ofs << "[createTex] Success" << std::endl;
};'''
old_createTex = '''auto createTex = [&](int w, int h, DXGI_FORMAT fmt,
Microsoft::WRL::ComPtr<ID3D11Texture2D>& tex,
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>& srv,
Microsoft::WRL::ComPtr<ID3D11UnorderedAccessView>& uav) {
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = w;
desc.Height = h;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = fmt;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
if (FAILED(m_device->CreateTexture2D(&desc, nullptr, &tex))) return;
if (FAILED(m_device->CreateShaderResourceView(tex.Get(), nullptr, &srv))) { tex.Reset(); return; }
if (FAILED(m_device->CreateUnorderedAccessView(tex.Get(), nullptr, &uav))) { tex.Reset(); srv.Reset(); return; }
};'''
if old_createTex in content:
content = content.replace(old_createTex, new_createTex)
else:
print('Could not find old_createTex')
# Patch createUavTex
new_createUavTex = '''auto createUavTex = [&](int w, int h, DXGI_FORMAT fmt,
Microsoft::WRL::ComPtr<ID3D11Texture2D>& tex,
Microsoft::WRL::ComPtr<ID3D11UnorderedAccessView>& uav) {
std::ofstream ofs("trace_interpolator.txt", std::ios::app);
ofs << "[createUavTex] Creating w=" << w << " h=" << h << std::endl;
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = w;
desc.Height = h;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = fmt;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
if (FAILED(m_device->CreateTexture2D(&desc, nullptr, &tex))) {
ofs << "[createUavTex] CreateTexture2D failed" << std::endl;
return;
}
if (FAILED(m_device->CreateUnorderedAccessView(tex.Get(), nullptr, &uav))) {
ofs << "[createUavTex] CreateUAV failed" << std::endl;
tex.Reset(); return;
}
ofs << "[createUavTex] Success" << std::endl;
};'''
old_createUavTex = '''auto createUavTex = [&](int w, int h, DXGI_FORMAT fmt,
Microsoft::WRL::ComPtr<ID3D11Texture2D>& tex,
Microsoft::WRL::ComPtr<ID3D11UnorderedAccessView>& uav) {
D3D11_TEXTURE2D_DESC desc = {};
desc.Width = w;
desc.Height = h;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = fmt;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
if (FAILED(m_device->CreateTexture2D(&desc, nullptr, &tex))) return;
if (FAILED(m_device->CreateUnorderedAccessView(tex.Get(), nullptr, &uav))) { tex.Reset(); return; }
};'''
if old_createUavTex in content:
content = content.replace(old_createUavTex, new_createUavTex)
else:
print('Could not find old_createUavTex')
# Add trace at end of CreateResources:
if 'if (!m_outputTexture || !m_outputSrv || !m_outputUav ||' in content:
content = content.replace('if (!m_outputTexture || !m_outputSrv || !m_outputUav ||', 'std::ofstream ofs("trace_interpolator.txt", std::ios::app); ofs << "[CreateResources] Checking valid resources" << std::endl; if (!m_outputTexture || !m_outputSrv || !m_outputUav ||')
if 'return true;\n}' in content:
content = content.replace('return true;\n}', 'std::ofstream ofs_end("trace_interpolator.txt", std::ios::app); ofs_end << "[CreateResources] Returning true" << std::endl;\nreturn true;\n}')
if 'bool Interpolator::Execute(' in content:
content = content.replace('bool Interpolator::Execute(', 'bool Interpolator::Execute(\n std::ofstream ofs_exec("trace_interpolator.txt", std::ios::app);\n ofs_exec << "[Execute] Started" << std::endl;\n')
with open('src/interpolator.cpp', 'w') as f:
f.write(content)
print('Patched successfully')