aboutsummaryrefslogtreecommitdiff
path: root/src/RaytraceDispatcher.ts
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2022-01-03 17:02:22 +0000
committerJames Barnett <noreply@jamesbarnett.xyz>2022-01-03 17:02:22 +0000
commit026a006b410a0132c2cb573edff4352b4333b857 (patch)
tree2a641595671a44a2e66619dd6f98dad118d30f0f /src/RaytraceDispatcher.ts
parentdde729e31a3aa9b5dadd0c0f07b3cb1f497067bb (diff)
downloadjs-raytracer-026a006b410a0132c2cb573edff4352b4333b857.tar.xz
js-raytracer-026a006b410a0132c2cb573edff4352b4333b857.zip
Performance optimisations
Diffstat (limited to 'src/RaytraceDispatcher.ts')
-rw-r--r--src/RaytraceDispatcher.ts32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/RaytraceDispatcher.ts b/src/RaytraceDispatcher.ts
index 1ce412f..be36bb1 100644
--- a/src/RaytraceDispatcher.ts
+++ b/src/RaytraceDispatcher.ts
@@ -1,4 +1,3 @@
-import {Colour} from './Colour';
import {Framebuffer} from './Framebuffer';
import {RaytraceContext} from './RaytraceContext';
import {instanceToPlain} from 'class-transformer';
@@ -7,7 +6,8 @@ import {Logger} from './Logger';
export class RaytraceDispatcher {
private renderStartMs: number;
- private responsesReceived = 0;
+ private completedThreads = 0;
+ private processedResponses = 0;
constructor(
readonly framebuffer: Framebuffer,
readonly context: RaytraceContext,
@@ -18,7 +18,7 @@ export class RaytraceDispatcher {
}
requestRender() {
- // Assumes height and threads are always even
+ // Assumes height%threads = 0
const rowBatchSize = this.context.height / this.context.options.numThreads;
for (let y = 0; y < this.context.height; y += rowBatchSize) {
@@ -55,7 +55,8 @@ export class RaytraceDispatcher {
}
private handleWorkerComplete() {
- if (++this.responsesReceived === this.context.options.numThreads) {
+ if (++this.completedThreads === this.context.options.numThreads) {
+ this.framebuffer.flush();
this.logger.log(
`Raytrace completed in ${new Date().getTime() - this.renderStartMs}ms`
);
@@ -63,10 +64,25 @@ export class RaytraceDispatcher {
}
}
- private processResponse(rowIndex: number, rowData: Colour[]) {
- for (let x = 0; x < this.framebuffer.width; x++) {
- this.framebuffer.writePixelAt(x, rowIndex, rowData[x]);
+ private processResponse(rowIndex: number, rowData: ArrayBuffer) {
+ const clampedRowData = new Uint8ClampedArray(rowData);
+
+ for (let x = 0; x < this.context.width; x++) {
+ const idx = x * 3;
+ const r = clampedRowData[idx];
+ const g = clampedRowData[idx + 1];
+ const b = clampedRowData[idx + 2];
+ this.framebuffer.writePixelAt(x, rowIndex, r, g, b);
+ }
+
+ if (
+ this.context.options.bufferDrawCalls &&
+ ++this.processedResponses >= this.context.height * 0.05
+ ) {
+ this.framebuffer.flush();
+ this.processedResponses = 0;
+ } else if (!this.context.options.bufferDrawCalls) {
+ this.framebuffer.flush();
}
- this.framebuffer.flush();
}
}