Skip to content

Commit 05dd174

Browse files
fix: partial (maybe whole) for #408 Break up texture constructors
So we can use `texture.toArray()`, this uses much less resources
1 parent 4e1bf1f commit 05dd174

39 files changed

+5151
-2937
lines changed

bin/gpu-browser-core.js

Lines changed: 1032 additions & 598 deletions
Large diffs are not rendered by default.

bin/gpu-browser-core.min.js

Lines changed: 1033 additions & 599 deletions
Large diffs are not rendered by default.

bin/gpu-browser.js

Lines changed: 1032 additions & 598 deletions
Large diffs are not rendered by default.

bin/gpu-browser.min.js

Lines changed: 1033 additions & 599 deletions
Large diffs are not rendered by default.

src/backend/gl/kernel-string.js

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { Texture } = require('../../texture');
44

55
function toStringWithoutUtils(fn) {
66
return fn.toString()
7+
.replace('=>', '')
78
.replace(/^function /, '')
89
.replace(/utils[.]/g, '/*utils.*/');
910
}
@@ -12,7 +13,7 @@ function toStringWithoutUtils(fn) {
1213
*
1314
* @param {Kernel} Kernel
1415
* @param {KernelVariable[]} args
15-
* @param {IKernel} originKernel
16+
* @param {Kernel} originKernel
1617
* @param {string} [setupContextString]
1718
* @param {string} [destroyContextString]
1819
* @returns {string}
@@ -68,26 +69,12 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
6869
result.push(`function ${toStringWithoutUtils(utils.isArray)}`);
6970
if (kernel.renderOutput === kernel.renderTexture) {
7071
result.push(Texture.toString());
71-
result.push(
72-
` const renderOutput = function ${
73-
toStringWithoutUtils(kernel.renderOutput.toString())
74-
.replace(`this.outputTexture`, 'null')
75-
.replace('this.texSize', `new Int32Array(${JSON.stringify(Array.from(kernel.texSize))})`)
76-
.replace('this.threadDim', `new Int32Array(${JSON.stringify(Array.from(kernel.threadDim))})`)
77-
.replace('this.output', `new Int32Array(${JSON.stringify(this.output)})`)
78-
.replace('this.context', 'gl')
79-
.replace('this.gpu', 'null')
80-
.replace('this.getReturnTextureType()', `'${kernel.getReturnTextureType()}'`)
81-
};`
82-
);
72+
if (kernel.TextureConstructor !== Texture) {
73+
result.push(kernel.TextureConstructor.toString());
74+
}
8375
} else {
8476
result.push(
85-
` const renderOutput = function ${toStringWithoutUtils(kernel.renderOutput.toString())
86-
.replace('() {', '(pixels) {')
87-
.replace(' const pixels = this.readFloatPixelsToFloat32Array();\n', '')
88-
.replace('this.readPackedPixelsToFloat32Array()', 'new Float32Array(pixels.buffer)')
89-
.replace('this.output;', JSON.stringify(kernel.output) + ';')
90-
};`
77+
` const renderOutput = function ${toStringWithoutUtils(kernel.formatValues)};`
9178
);
9279
}
9380
kernel.kernelArguments.forEach(kernelArgument => {
@@ -108,7 +95,12 @@ function glKernelString(Kernel, args, originKernel, setupContextString, destroyC
10895
});
10996
result.push('/** end setup uploads for kernel values **/');
11097
result.push(context.toString());
111-
result.push(` ${destroyContextString ? '\n' + destroyContextString + ' ': ''}return renderOutput(${context.getReadPixelsVariableName});`);
98+
result.push(` ${destroyContextString ? '\n' + destroyContextString + ' ': ''}`);
99+
if (context.getReadPixelsVariableName) {
100+
result.push(` return renderOutput(${kernel.precision === 'single' ? context.getReadPixelsVariableName : `new Float32Array(${context.getReadPixelsVariableName}.buffer)`}, ${kernel.output[0]}, ${kernel.output[1]}, ${kernel.output[2]});`);
101+
} else {
102+
result.push(` return null;`);
103+
}
112104
result.push(' };');
113105
return `function kernel(context = null) {
114106
${setupContextString ? setupContextString : ''}${result.join('\n')} }`;

0 commit comments

Comments
 (0)