diff options
Diffstat (limited to 'src/RaytraceDispatcher.ts')
| -rw-r--r-- | src/RaytraceDispatcher.ts | 32 |
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(); } } |