Skip to content

Commit abbb4ce

Browse files
Option: Scale external textures
Add an option to downscale external textures during capture to save space.
1 parent f08a46f commit abbb4ce

7 files changed

Lines changed: 124 additions & 14 deletions

File tree

capture/manifest.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,32 @@
1212
"permissions": [
1313
"activeTab",
1414
"scripting",
15-
"storage",
16-
"unlimitedStorage"
15+
"storage"
1716
],
1817
"action": {
1918
"default_title": "Save WebGPU capture",
2019
"default_icon": "images/icon-16.png"
2120
},
2221
"content_scripts": [
2322
{
24-
"js": ["scripts/content.js"],
23+
"js": ["scripts/mainContent.js"],
2524
"matches": [
2625
"http://*/*",
2726
"https://*/*"
2827
],
2928
"run_at": "document_start",
3029
"world": "MAIN"
30+
},
31+
{
32+
"js": ["scripts/isolatedContent.js"],
33+
"matches": [
34+
"http://*/*",
35+
"https://*/*"
36+
],
37+
"run_at": "document_end",
38+
"world": "ISOLATED"
3139
}
3240
],
41+
"options_page": "options.html",
3342
$BROWSER_SPECIFIC
3443
}

capture/options.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>WebGPUReconstruct Options</title>
5+
</head>
6+
<body>
7+
<h1>WebGPUReconstruct Options</h1>
8+
9+
<div>
10+
<h2>External textures</h2>
11+
12+
<span>Scale (%)</span>
13+
<input type="number" id="externalTextureScale" min="1" max="100" />
14+
</div>
15+
16+
<div>
17+
<button id="save">Save</button>
18+
<div id="status"></div>
19+
</div>
20+
21+
<script src="scripts/options.js"></script>
22+
</body>
23+
</html>

capture/scripts/isolatedContent.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
chrome.storage.local.get(
2+
{
3+
externalTextureScale: 100
4+
},
5+
(items) => {
6+
window.postMessage({ type: "WebGPUReconstruct Options", message: items }, "*");
7+
}
8+
);
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,17 @@ $CAPTURE_COMMANDS
445445
function __WebGPUReconstruct_GPU_requestAdapter(originalMethod, options) {
446446
__WebGPUReconstruct_DebugOutput("requestAdapter");
447447

448-
return originalMethod.call(this, options).then((adapter) => {
449-
let features = new Set();
450-
for (const value of adapter.features) {
451-
if (__WebGPUReconstruct_supportedFeatures.has(value)) {
452-
features.add(value);
448+
return __webGPUReconstruct.optionsPromise.then(() => {
449+
return originalMethod.call(this, options).then((adapter) => {
450+
let features = new Set();
451+
for (const value of adapter.features) {
452+
if (__WebGPUReconstruct_supportedFeatures.has(value)) {
453+
features.add(value);
454+
}
453455
}
454-
}
455-
adapter.__defineGetter__("features", function() { return features;});
456-
return adapter;
456+
adapter.__defineGetter__("features", function() { return features;});
457+
return adapter;
458+
});
457459
});
458460
}
459461

@@ -640,6 +642,19 @@ $WRAP_COMMANDS
640642
}
641643
}
642644

645+
configure(configuration) {
646+
this.configuration = configuration;
647+
if (this.configuration == undefined) {
648+
this.configuration = {};
649+
}
650+
651+
if (this.configuration.externalTextureScale === undefined) {
652+
this.configuration.externalTextureScale = 100;
653+
} else {
654+
this.configuration.externalTextureScale = Number(this.configuration.externalTextureScale);
655+
}
656+
}
657+
643658
finishCapture() {
644659
if (!this.firstCapture) {
645660
console.error("You need to reload the page between captures.");

capture/scripts/options.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const saveOptions = () => {
2+
const externalTextureScale = document.getElementById('externalTextureScale').value;
3+
4+
chrome.storage.local.set(
5+
{
6+
externalTextureScale: externalTextureScale
7+
},
8+
() => {
9+
// Update status to let user know options were saved.
10+
const status = document.getElementById('status');
11+
status.textContent = 'Options saved.';
12+
setTimeout(() => {
13+
status.textContent = '';
14+
}, 750);
15+
}
16+
);
17+
};
18+
19+
const restoreOptions = () => {
20+
chrome.storage.local.get(
21+
{
22+
externalTextureScale: 100
23+
},
24+
(items) => {
25+
document.getElementById('externalTextureScale').value = items.externalTextureScale;
26+
}
27+
);
28+
};
29+
30+
document.addEventListener('DOMContentLoaded', restoreOptions);
31+
document.getElementById('save').addEventListener('click', saveOptions);

code_generation/code_generation.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def write_browser_extension(configuration, browser):
7070
}
7171
""")
7272

73-
contentPath = "build/capture/" + browser + "/scripts/content.js"
73+
contentPath = "build/capture/" + browser + "/scripts/mainContent.js"
7474
write_content_script(contentPath, configuration)
7575
add_suffix_to_file(contentPath, """
7676
let __webGPUReconstruct = new __WebGPUReconstruct();
@@ -80,20 +80,32 @@ def write_browser_extension(configuration, browser):
8080
document.addEventListener('__WebGPUReconstruct_saveCapture', function() {
8181
__webGPUReconstruct.finishCapture();
8282
});
83+
84+
__webGPUReconstruct.optionsPromise = new Promise((resolve) => {
85+
window.addEventListener("message", function __WebGPUReconstruct_MessageListener(event) {
86+
if (event.source === window && event?.data?.type === "WebGPUReconstruct Options") {
87+
__webGPUReconstruct.configure(event.data.message);
88+
window.removeEventListener("message", __WebGPUReconstruct_MessageListener);
89+
resolve();
90+
}
91+
});
92+
});
8393
""")
8494

8595
shutil.make_archive("build/capture/" + browser, 'zip', "build/capture/" + browser)
8696

8797
def write_module(configuration):
8898
Path("build/capture/module").mkdir(parents=True, exist_ok=True)
8999
contentPath = "build/capture/module/WebGPUReconstruct.js"
90-
shutil.copyfile("capture/scripts/content.js", contentPath)
100+
shutil.copyfile("capture/scripts/mainContent.js", contentPath)
91101
write_content_script(contentPath, configuration)
92102
add_suffix_to_file(contentPath, """
93103
let __webGPUReconstruct;
94104
95-
function start() {
105+
function start(configuration) {
96106
__webGPUReconstruct = new __WebGPUReconstruct();
107+
__webGPUReconstruct.configure(configuration);
108+
__webGPUReconstruct.optionsPromise = new Promise((resolve) => { resolve() });
97109
}
98110
99111
function finish() {

code_generation/commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,18 @@ def add_override_command(classType, methodName, argumentCount, overrides):
683683
} else {
684684
console.error("Unknown source in importExternalTexture");
685685
}
686+
687+
// Scale external textures to save memory / disk space.
688+
const externalTextureScale = __webGPUReconstruct.configuration.externalTextureScale / 100;
689+
width = Math.floor(width * externalTextureScale);
690+
height = Math.floor(height * externalTextureScale);
691+
if (width <= 0) {
692+
width = 1;
693+
}
694+
if (height <= 0) {
695+
height = 1;
696+
}
697+
686698
__WebGPUReconstruct_file.writeUint32(width);
687699
__WebGPUReconstruct_file.writeUint32(height);
688700

0 commit comments

Comments
 (0)