-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdh_main.py
357 lines (262 loc) · 13.4 KB
/
rdh_main.py
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import renderdoc as rd
import qrenderdoc as qrd
from . import rdh_export
import PySide2
from PySide2 import QtCore, QtWidgets, QtGui
export_obj_path: str = ""
class MainData():
def __init__(self) -> None:
self.meshes_data: list = []
self.textures_data: list = []
def CLearData(self):
self.meshes_data.clear()
self.textures_data.clear()
class RDHCaptureViewer(qrd.CaptureViewer):
def __init__(self, ctx: qrd.CaptureContext, version: str):
super().__init__()
self.ctx: qrd.CaptureContext = ctx
self.version: str = version
self.main_widget: MainWidget = MainWidget()
self.main_data = MainData()
# TEST TEST TEST TEST
self.testPrint = QtWidgets.QMessageBox()
########## FINAL THINGS
ctx.AddDockWindow(self.main_widget, qrd.DockReference.MainToolArea, None)
ctx.AddCaptureViewer(self)
### qrd.CaptureViewer Functions
def OnCaptureLoaded(self):
self.main_data.CLearData()
self.main_widget.ClearUI()
def OnCaptureClosed(self):
self.main_data.CLearData()
self.main_widget.ClearUI()
def OnSelectedEventChanged(self, event):
pass
def OnEventChanged(self, event):
pass
### RDHCaptureViewer Functions
def UpdateData(self):
self.main_data.CLearData()
self.ctx.Replay().BlockInvoke(self.UpdateDataWithReplay)
self.main_widget.UpdateUI()
def UpdateDataWithReplay(self, r_ctrl: rd.ReplayController):
startEID: int = self.main_widget.GetStartEID()
endEID: int = self.main_widget.GetEndEID()
# Check
if (startEID < 0 or endEID < 1) or endEID <= startEID:
return
for i, res in enumerate(self.ctx.GetResources()):
res_id = res.resourceId
usages = r_ctrl.GetUsage(res_id)
if res.type == rd.ResourceType.Texture:
tex = self.ctx.GetTexture(res_id)
for use in usages:
event_id: int = use.eventId
# Check
if event_id < startEID or event_id > endEID:
continue
if usages and tex.type == rd.TextureType.Texture2D:
tex_width: int = tex.width
tex_height: int = tex.height
tex_size = max(tex_width, tex_height)
if tex_size >= 128:
self.main_data.textures_data.append((event_id, tex_width,
tex_height, tex_size, i))
if self.main_widget.uniques_textures_checkbox.isChecked():
break
elif res.type == rd.ResourceType.Buffer:
buf = the_window.ctx.GetBuffer(res_id)
if 'Index' in str(buf.creationFlags):
for use in usages:
event_id: int = use.eventId
# Check
if event_id < startEID or event_id > endEID:
continue
if use.usage == rd.ResourceUsage.IndexBuffer:
action: rd.ActionDescription = self.ctx.GetAction(event_id)
if action.numIndices >= 3:
triangles_num = int(action.numIndices / 3)
self.main_data.meshes_data.append((event_id, triangles_num, i))
if self.main_widget.uniques_meshes_checkbox.isChecked():
break
if self.main_data.textures_data:
self.main_data.textures_data.sort(key=lambda value: value[3], reverse=True)
if self.main_data.meshes_data:
self.main_data.meshes_data.sort(key=lambda value: value[1], reverse=True)
def ExportToObjWithReplay(self, r_ctrl: rd.ReplayController):
global export_obj_path
vertex_index = 0
with open(export_obj_path, 'w') as obj_file:
obj_file.write("# OBJ file \n")
for sel_index in self.main_widget.meshes_list.selectedIndexes():
mesh_data = self.main_data.meshes_data[sel_index.row()]
event_id = mesh_data[0]
triangles_num = mesh_data[1]
# Check
if triangles_num < 1:
continue
# Move to that draw
r_ctrl.SetFrameEvent(event_id, True)
action: rd.ActionDescription = self.ctx.GetAction(event_id)
instances = action.numInstances
if action.numIndices > 0:
indices = None
obj_name = "o Tris" + str(int(triangles_num)) + "_EID" + str(event_id) + '\n'
# Pase Instances reversed so that indices calculated faster
for i in range(instances):
# Fetch the postvs data
postvs = r_ctrl.GetPostVSData(i, 0, rd.MeshDataStage.VSOut)
obj_file.write(obj_name)
# Calcualte the mesh configuration
mesh_outputs = rdh_export.GetMeshOutputs(r_ctrl, postvs)
try:
if not indices:
# postvs2 = ctrl.GetPostVSData(0, 0, rd.MeshDataStage.VSOut)
indices = rdh_export.GetIndices(r_ctrl, mesh_outputs[0])
# if not data:
# data = ctrl.GetBufferData(mesh_outputs[0].vertexResourceId, 0, 0)
buffer_data = r_ctrl.GetBufferData(mesh_outputs[0].vertexResourceId,
mesh_outputs[0].vertexByteOffset, 0)
if indices:
vertex_index_2 = rdh_export.ExportToOBJ(r_ctrl, mesh_outputs, obj_file,
vertex_index, indices, buffer_data)
vertex_index += vertex_index_2
except Exception as e:
print(e)
if buffer_data:
del buffer_data
the_window: RDHCaptureViewer = None
class MainWidget(QtWidgets.QWidget):
def __init__(self) -> None:
super().__init__()
self.setWindowTitle('RDHelper')
self.main_container: QtWidgets.QHBoxLayout = QtWidgets.QHBoxLayout(self)
self.left_main_layout: QtWidgets.QVBoxLayout = QtWidgets.QVBoxLayout()
self.left_main_layout.setAlignment(QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop)
self.right_main_layout: QtWidgets.QHBoxLayout = QtWidgets.QHBoxLayout()
self.main_container.addLayout(self.left_main_layout, stretch=1)
self.main_container.addLayout(self.right_main_layout, stretch=10)
########## LEFT SIDE
# Update Button
update_btn: QtWidgets.QPushButton = QtWidgets.QPushButton("Update")
update_btn.clicked.connect(lambda: self.UpdateData_Btn())
self.left_main_layout.addWidget(update_btn)
# Set Start EID Button
self.set_startEID_btn: QtWidgets.QPushButton = QtWidgets.QPushButton("Set Start EID")
self.set_startEID_btn.clicked.connect(lambda: self.SetStartEID_Btn())
self.left_main_layout.addWidget(self.set_startEID_btn)
# Set End EID Button
self.set_endEID_btn: QtWidgets.QPushButton = QtWidgets.QPushButton("Set End EID")
self.set_endEID_btn.clicked.connect(lambda: self.SetEndEID_Btn())
self.left_main_layout.addWidget(self.set_endEID_btn)
label_tmp = QtWidgets.QLabel('Start EID:')
self.left_main_layout.addWidget(label_tmp)
self.startEID: QtWidgets.QLineEdit = QtWidgets.QLineEdit()
self.startEID.setValidator(QtGui.QIntValidator())
self.startEID.setObjectName("Start EID")
self.startEID.setText("0")
self.left_main_layout.addWidget(self.startEID)
label_tmp = QtWidgets.QLabel('End EID:')
self.left_main_layout.addWidget(label_tmp)
self.endEID: QtWidgets.QLineEdit = QtWidgets.QLineEdit()
self.endEID.setValidator(QtGui.QIntValidator())
self.endEID.setObjectName("End EID")
self.endEID.setText("1000000")
self.left_main_layout.addWidget(self.endEID)
self.uniques_meshes_checkbox = QtWidgets.QCheckBox("Unique Meshes")
self.uniques_meshes_checkbox.setChecked(True)
self.left_main_layout.addWidget(self.uniques_meshes_checkbox)
self.uniques_textures_checkbox = QtWidgets.QCheckBox("Unique Textures")
self.uniques_textures_checkbox.setChecked(True)
self.left_main_layout.addWidget(self.uniques_textures_checkbox)
########## RIGHT SIDE
### Meshes
self.meshes_layout: QtWidgets.QVBoxLayout = QtWidgets.QVBoxLayout()
self.right_main_layout.addLayout(self.meshes_layout)
# Export Meshes to OBJ
export_meshes_btn: QtWidgets.QPushButton = QtWidgets.QPushButton("Export to OBJ")
export_meshes_btn.clicked.connect(lambda: self.ExportMeshes_Btn())
self.meshes_layout.addWidget(export_meshes_btn)
# Select Mesh
select_mesh_eid_btn: QtWidgets.QPushButton = QtWidgets.QPushButton("Select Mesh")
select_mesh_eid_btn.clicked.connect(lambda: self.SelectMeshEID_Btn())
self.meshes_layout.addWidget(select_mesh_eid_btn)
self.meshes_list: QtWidgets.QListWidget = QtWidgets.QListWidget()
self.meshes_list.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.meshes_list.setMinimumHeight(50)
self.meshes_list.setMaximumWidth(300)
self.meshes_layout.addWidget(self.meshes_list)
### Textures
self.texures_layout: QtWidgets.QVBoxLayout = QtWidgets.QVBoxLayout()
self.right_main_layout.addLayout(self.texures_layout)
select_texture_eid_btn: QtWidgets.QPushButton = QtWidgets.QPushButton("Select Texture")
select_texture_eid_btn.clicked.connect(lambda: self.SelectTextureEID_Btn())
self.texures_layout.addWidget(select_texture_eid_btn)
self.textures_list: QtWidgets.QListWidget = QtWidgets.QListWidget()
self.textures_list.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.textures_list.setMinimumHeight(50)
self.textures_list.setMaximumWidth(300)
self.texures_layout.addWidget(self.textures_list)
def GetStartEID(self):
return int(self.startEID.text())
def GetEndEID(self):
return int(self.endEID.text())
def ExportMeshes_Btn(self):
global the_window
if len(self.meshes_list.selectedIndexes()) > 0:
file_path = QtWidgets.QFileDialog.getSaveFileName(None, "Save To Obj", '', "Files (*.obj)")
if file_path[0]:
global export_obj_path
export_obj_path = file_path[0]
the_window.ctx.Replay().BlockInvoke(the_window.ExportToObjWithReplay)
def SelectMeshEID_Btn(self):
global the_window
if len(self.meshes_list.selectedIndexes()) > 0:
cur_index = self.meshes_list.currentIndex().row()
cur_data = the_window.main_data.meshes_data[cur_index]
the_window.ctx.SetEventID([], the_window.ctx.CurEvent(), cur_data[0], False)
the_window.ctx.ShowMeshPreview()
def SelectTextureEID_Btn(self):
global the_window
if len(self.textures_list.selectedIndexes()) > 0:
cur_index = self.textures_list.currentIndex().row()
cur_data = the_window.main_data.textures_data[cur_index]
event_id = cur_data[0]
res: rd.ResourceDescription = the_window.ctx.GetResources()[cur_data[4]]
the_window.ctx.SetEventID([], the_window.ctx.CurEvent(), event_id, False)
the_window.ctx.GetTextureViewer().ViewTexture(res.resourceId, rd.CompType.UNormSRGB, True)
def UpdateData_Btn(self):
global the_window
the_window.UpdateData()
def SetStartEID_Btn(self):
global the_window
self.startEID.setText(str(the_window.ctx.CurEvent()))
def SetEndEID_Btn(self):
global the_window
self.endEID.setText(str(the_window.ctx.CurEvent()))
def ClearUI(self):
self.meshes_list.clear()
self.textures_list.clear()
def UpdateUI(self):
global the_window
self.ClearUI()
unique_res_ids: set = set()
# Set Meshes
for mesh_data in the_window.main_data.meshes_data:
mesh_item: QtWidgets.QListWidgetItem = QtWidgets.QListWidgetItem()
mesh_item.setText(str(mesh_data[1]))
res_id = mesh_data[2]
if res_id not in unique_res_ids:
mesh_item.setTextColor(QtGui.QColor(150, 50, 0))
unique_res_ids.add(res_id)
self.meshes_list.addItem(mesh_item)
unique_res_ids.clear()
# Set Textures
for tex_data in the_window.main_data.textures_data:
tex_item: QtWidgets.QListWidgetItem = QtWidgets.QListWidgetItem()
tex_item.setText(str(tex_data[1]) + " x " + str(tex_data[2]))
res_id = tex_data[4]
if res_id not in unique_res_ids:
tex_item.setTextColor(QtGui.QColor(160, 80, 0))
unique_res_ids.add(res_id)
self.textures_list.addItem(tex_item)