Skip to content

Commit 79820de

Browse files
authored
Wrap GPU.js kernel functions in templates (#299)
* Wrap kernel functions in templates * Add TODO
1 parent 6ce0614 commit 79820de

File tree

5 files changed

+13
-10
lines changed

5 files changed

+13
-10
lines changed

visualizer/src/Canvas/Labeled/LabeledCanvas.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const LabeledCanvas = ({ setCanvases }) => {
3939
useEffect(() => {
4040
const gpu = new GPU({ canvas: kernelCanvasRef.current });
4141
const kernel = gpu.createKernel(
42-
function (labelArray, colormap, foreground, highlight, highlightColor, opacity) {
42+
`function (labelArray, colormap, foreground, highlight, highlightColor, opacity) {
4343
const label = labelArray[this.constants.h - 1 - this.thread.y][this.thread.x];
4444
if (highlight && label === foreground && foreground !== 0) {
4545
const [r, g, b] = highlightColor;
@@ -48,7 +48,7 @@ export const LabeledCanvas = ({ setCanvases }) => {
4848
const [r, g, b] = colormap[label];
4949
this.color(r / 255, g / 255, b / 255, opacity);
5050
}
51-
},
51+
}`,
5252
{
5353
constants: { w: width, h: height },
5454
output: [width, height],

visualizer/src/Canvas/Labeled/OutlineCanvas.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ const OutlineCanvas = ({ setCanvases }) => {
3535
useEffect(() => {
3636
const gpu = new GPU({ canvas: kernelCanvasRef.current });
3737
const kernel = gpu.createKernel(
38-
function (data, outlineAll, foreground, background) {
38+
// template string needed to avoid minification breaking function
39+
// by changing if (x) { y } to x && y
40+
// TODO: research how to work around minification changes
41+
`function (data, outlineAll, foreground, background) {
3942
const x = this.thread.x;
4043
const y = this.constants.h - 1 - this.thread.y;
4144
const label = data[y][x];
@@ -56,7 +59,7 @@ const OutlineCanvas = ({ setCanvases }) => {
5659
} else if (outlineAll && onOutline) {
5760
this.color(1, 1, 1, 1);
5861
}
59-
},
62+
}`,
6063
{
6164
constants: { w: width, h: height },
6265
output: [width, height],

visualizer/src/Canvas/Raw/ChannelCanvas.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const ChannelCanvas = ({ layer, setCanvases }) => {
3232
useEffect(() => {
3333
const gpu = new GPU();
3434
const kernel = gpu.createKernel(
35-
function (data, on, color, min, max) {
35+
`function (data, on, color, min, max) {
3636
if (on) {
3737
const x = this.thread.x;
3838
const y = this.constants.h - 1 - this.thread.y;
@@ -44,7 +44,7 @@ export const ChannelCanvas = ({ layer, setCanvases }) => {
4444
} else {
4545
this.color(0, 0, 0, 0);
4646
}
47-
},
47+
}`,
4848
{
4949
constants: { w: width, h: height },
5050
output: [width, height],

visualizer/src/Canvas/Raw/GrayscaleCanvas.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const GrayscaleCanvas = ({ setCanvases }) => {
2626
useEffect(() => {
2727
const gpu = new GPU();
2828
const kernel = gpu.createKernel(
29-
function (data, min, max, brightness, contrast, invert) {
29+
`function (data, min, max, brightness, contrast, invert) {
3030
const x = this.thread.x;
3131
const y = this.constants.h - 1 - this.thread.y;
3232
// Rescale value from min - max to 0 - 1
@@ -44,7 +44,7 @@ export const GrayscaleCanvas = ({ setCanvases }) => {
4444
v = 1 - v;
4545
}
4646
this.color(v, v, v, 1);
47-
},
47+
}`,
4848
{
4949
constants: { w: width, h: height },
5050
output: [width, height],

visualizer/src/Canvas/Tool/ThresholdCanvas.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const ThresholdCanvas = ({ setCanvases }) => {
2121
useEffect(() => {
2222
const gpu = new GPU({ canvas: kernelCanvasRef.current });
2323
const kernel = gpu.createKernel(
24-
function (x1, y1, x2, y2) {
24+
`function (x1, y1, x2, y2) {
2525
const x = this.thread.x;
2626
const y = this.constants.h - this.thread.y;
2727
const minX = Math.min(x1, x2);
@@ -37,7 +37,7 @@ const ThresholdCanvas = ({ setCanvases }) => {
3737
} else {
3838
this.color(0, 0, 0, 0);
3939
}
40-
},
40+
}`,
4141
{
4242
constants: { w: width, h: height },
4343
output: [width, height],

0 commit comments

Comments
 (0)