Skip to content

Commit f2e096c

Browse files
author
HarshKhandeparkar
committed
feat: getData and loadData
1 parent ad5c886 commit f2e096c

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

dist/gpujs-real-renderer-browser.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@
103103
exports.getCloneTextureKernel = getCloneTextureKernel;
104104
});
105105

106+
var loadData = createCommonjsModule(function (module, exports) {
107+
Object.defineProperty(exports, "__esModule", { value: true });
108+
exports.getLoadDataKernel = void 0;
109+
/**
110+
* @param gpu GPU.js Instance
111+
* @param dimensions Dimensions of the Output Graph
112+
*/
113+
function getLoadDataKernel(gpu, dimensions) {
114+
return gpu.createKernel(function (graphPixels) {
115+
return [
116+
graphPixels[this.thread.y * this.output.x * 3 + this.thread.x * 3 + 0],
117+
graphPixels[this.thread.y * this.output.x * 3 + this.thread.x * 3 + 1],
118+
graphPixels[this.thread.y * this.output.x * 3 + this.thread.x * 3 + 2]
119+
];
120+
}, {
121+
argumentTypes: {
122+
graphPixels: 'Array'
123+
},
124+
output: dimensions,
125+
pipeline: true
126+
});
127+
}
128+
exports.getLoadDataKernel = getLoadDataKernel;
129+
});
130+
106131
var RealRendererTypes = createCommonjsModule(function (module, exports) {
107132
Object.defineProperty(exports, "__esModule", { value: true });
108133
});
@@ -153,6 +178,7 @@
153178

154179

155180

181+
156182
exports.RealRendererTypes = RealRendererTypes;
157183

158184
__exportStar(RealRendererDefaults, exports);
@@ -186,6 +212,7 @@
186212
this._blankGraph = blankGraph.getBlankGraphKernel(this.gpu, this.dimensions, this.xOffset, this.yOffset, this.bgColor, this.axesColor, this.drawAxes);
187213
this._cloneTexture = cloneTexture.getCloneTextureKernel(this.gpu, this.dimensions);
188214
this.graphPixels = this._blankGraph();
215+
this._loadData = loadData.getLoadDataKernel(this.gpu, this.dimensions);
189216
this._display = display.getDisplayKernel(this.gpu, this.dimensions);
190217
this._doRender = false;
191218
}
@@ -235,6 +262,22 @@
235262
this.time = 0;
236263
return this;
237264
};
265+
RealRenderer.prototype.getData = function () {
266+
var returnedArray = this.graphPixels.toArray();
267+
var outArr = [];
268+
for (var i = 0; i < returnedArray.length; i++) {
269+
for (var j = 0; j < returnedArray[0].length; j++) {
270+
for (var k = 0; k < returnedArray[0][0].length; k++) {
271+
outArr[i * returnedArray[0].length * returnedArray[0][0].length + j * returnedArray[0][0].length + k] = returnedArray[i][j][k];
272+
}
273+
}
274+
}
275+
return outArr;
276+
};
277+
RealRenderer.prototype.loadData = function (pixels) {
278+
this.graphPixels = this._loadData(pixels);
279+
this._display(this.graphPixels);
280+
};
238281
RealRenderer.prototype.reset = function () {
239282
this.graphPixels = this._blankGraph();
240283
this.resetTime();

dist/gpujs-real-renderer-browser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/kernels/cloneTexture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ export function getCloneTextureKernel(
1616
output: dimensions,
1717
pipeline: true
1818
})
19-
}
19+
}

src/kernels/loadData.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { GPU } from 'gpu.js';
2+
import { GraphDimensions } from '../types/RealRendererTypes';
3+
4+
/**
5+
* @param gpu GPU.js Instance
6+
* @param dimensions Dimensions of the Output Graph
7+
*/
8+
export function getLoadDataKernel(
9+
gpu: GPU,
10+
dimensions: GraphDimensions
11+
) {
12+
return gpu.createKernel(
13+
function(graphPixels) {
14+
return [
15+
graphPixels[this.thread.y * this.output.x * 3 + this.thread.x * 3 + 0],
16+
graphPixels[this.thread.y * this.output.x * 3 + this.thread.x * 3 + 1],
17+
graphPixels[this.thread.y * this.output.x * 3 + this.thread.x * 3 + 2]
18+
]
19+
},
20+
{
21+
argumentTypes: {
22+
graphPixels: 'Array'
23+
},
24+
output: dimensions,
25+
pipeline: true
26+
}
27+
)
28+
}

src/renderers/RealRenderer.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getDisplayKernel } from '../kernels/display';
22
import { getBlankGraphKernel } from '../kernels/blankGraph';
33
import { getCloneTextureKernel } from '../kernels/cloneTexture';
4+
import { getLoadDataKernel } from '../kernels/loadData';
45

56
import { GraphDimensions, Color, RealRendererOptions } from '../types/RealRendererTypes';
67
export * as RealRendererTypes from '../types/RealRendererTypes';
@@ -28,6 +29,7 @@ export class RealRenderer {
2829
graphPixels: Texture;
2930
_blankGraph: IKernelRunShortcut;
3031
_cloneTexture: IKernelRunShortcut;
32+
_loadData: IKernelRunShortcut;
3133
_display: IKernelRunShortcut;
3234
_doRender: boolean;
3335

@@ -81,6 +83,8 @@ export class RealRenderer {
8183

8284
this.graphPixels = this._blankGraph() as Texture;
8385

86+
this._loadData = getLoadDataKernel(this.gpu, this.dimensions);
87+
8488
this._display = getDisplayKernel(this.gpu, this.dimensions);
8589

8690
this._doRender = false;
@@ -141,6 +145,26 @@ export class RealRenderer {
141145
return this;
142146
}
143147

148+
getData() {
149+
let returnedArray: number[][][] = <number[][][]>this.graphPixels.toArray();
150+
let outArr: number[] = [];
151+
152+
for (let i = 0; i < returnedArray.length; i++) {
153+
for (let j = 0; j < returnedArray[0].length; j++) {
154+
for (let k = 0; k < returnedArray[0][0].length; k++) {
155+
outArr[i * returnedArray[0].length * returnedArray[0][0].length + j * returnedArray[0][0].length + k] = returnedArray[i][j][k];
156+
}
157+
}
158+
}
159+
160+
return outArr;
161+
}
162+
163+
loadData(pixels: number[]) {
164+
this.graphPixels = <Texture>this._loadData(pixels);
165+
this._display(this.graphPixels);
166+
}
167+
144168
reset() {
145169
this.graphPixels = this._blankGraph() as Texture;
146170
this.resetTime();

0 commit comments

Comments
 (0)