Skip to content

Commit

Permalink
Custom edit
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshaySG14 authored and jay2142 committed Sep 25, 2024
1 parent cde22b1 commit 11a1a9d
Show file tree
Hide file tree
Showing 28 changed files with 395 additions and 102 deletions.
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@
"@pixi/colord": "^2.9.6",
"@types/css-font-loading-module": "^0.0.12",
"@types/earcut": "^2.1.4",
"@types/trusted-types": "^2.0.7",
"@webgpu/types": "^0.1.40",
"@xmldom/xmldom": "^0.8.10",
"earcut": "^2.2.4",
"eventemitter3": "^5.0.1",
"ismobilejs": "^1.1.1",
"parse-svg-path": "^0.1.2"
"parse-svg-path": "^0.1.2",
"trusted-types": "^2.0.0"
},
"devDependencies": {
"@pixi/eslint-config": "^5.1.0",
Expand Down
6 changes: 3 additions & 3 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import jscc from 'rollup-plugin-jscc';
import sourcemaps from 'rollup-plugin-sourcemaps';
import { string } from 'rollup-plugin-string';
import { fileURLToPath } from 'url';
import webworker from '@pixi/webworker-plugins/rollup-plugin';
import repo from './package.json' assert { type: 'json' };
import webworker from './webworker-plugin/rollup-plugin/index.js';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
Expand Down Expand Up @@ -169,7 +169,7 @@ async function main()
banner: nsBanner,
footer,
file,
format: 'iife',
format: 'umd',
freeze: false,
sourcemap: true,
},
Expand All @@ -193,7 +193,7 @@ async function main()
banner: nsBanner,
footer,
file: prodName(file),
format: 'iife',
format: 'umd',
freeze: false,
sourcemap: true,
},
Expand Down
66 changes: 18 additions & 48 deletions src/assets/loader/workers/WorkerManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import CheckImageBitmapWorker from 'worker:./checkImageBitmap.worker.ts';
import LoadImageBitmapWorker from 'worker:./loadImageBitmap.worker.ts';

let UUID = 0;
Expand All @@ -11,8 +10,7 @@ type LoadImageBitmapResult = {
id: string,
};

class WorkerManagerClass
{
class WorkerManagerClass {
public worker: Worker;
private _resolveHash: {
[key: string]: {
Expand All @@ -31,61 +29,41 @@ class WorkerManagerClass
private _createdWorkers = 0;
private _isImageBitmapSupported?: Promise<boolean>;

constructor()
{
constructor() {
this._workerPool = [];
this._queue = [];

this._resolveHash = {};
}

public isImageBitmapSupported(): Promise<boolean>
{
if (this._isImageBitmapSupported !== undefined) return this._isImageBitmapSupported;

this._isImageBitmapSupported = new Promise((resolve) =>
{
const { worker } = new CheckImageBitmapWorker();

worker.addEventListener('message', (event: MessageEvent<boolean>) =>
{
worker.terminate();
CheckImageBitmapWorker.revokeObjectURL();
resolve(event.data);
});
});
public isImageBitmapSupported(): Promise<boolean> {
this._isImageBitmapSupported = new Promise((resolve) => resolve(true));

return this._isImageBitmapSupported;
}

public loadImageBitmap(src: string): Promise<ImageBitmap>
{
public loadImageBitmap(src: string): Promise<ImageBitmap> {
return this._run('loadImageBitmap', [src]) as Promise<ImageBitmap>;
}

private async _initWorkers()
{
private async _initWorkers() {
if (this._initialized) return;

this._initialized = true;
}

private _getWorker(): Worker
{
if (MAX_WORKERS === undefined)
{
private _getWorker(): Worker {
if (MAX_WORKERS === undefined) {
MAX_WORKERS = navigator.hardwareConcurrency || 4;
}
let worker = this._workerPool.pop();

if (!worker && this._createdWorkers < MAX_WORKERS)
{
if (!worker && this._createdWorkers < MAX_WORKERS) {
// only create as many as MAX_WORKERS allows..
this._createdWorkers++;
worker = new LoadImageBitmapWorker().worker;

worker.addEventListener('message', (event: MessageEvent) =>
{
worker.addEventListener('message', (event: MessageEvent) => {
this._complete(event.data);

this._returnWorker(event.target as Worker);
Expand All @@ -96,32 +74,26 @@ class WorkerManagerClass
return worker;
}

private _returnWorker(worker: Worker)
{
private _returnWorker(worker: Worker) {
this._workerPool.push(worker);
}

private _complete(data: LoadImageBitmapResult): void
{
if (data.error !== undefined)
{
private _complete(data: LoadImageBitmapResult): void {
if (data.error !== undefined) {
this._resolveHash[data.uuid].reject(data.error);
}
else
{
else {
this._resolveHash[data.uuid].resolve(data.data);
}

this._resolveHash[data.uuid] = null;
}

private async _run(id: string, args: any[]): Promise<any>
{
private async _run(id: string, args: any[]): Promise<any> {
await this._initWorkers();
// push into the queue...

const promise = new Promise((resolve, reject) =>
{
const promise = new Promise((resolve, reject) => {
this._queue.push({ id, arguments: args, resolve, reject });
});

Expand All @@ -130,16 +102,14 @@ class WorkerManagerClass
return promise;
}

private _next(): void
{
private _next(): void {
// nothing to do
if (!this._queue.length) return;

const worker = this._getWorker();

// no workers available...
if (!worker)
{
if (!worker) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/assets/loader/workers/loadImageBitmap.worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
interface MessageData
{
interface MessageData {
data: any[];
uuid: number;
id: string;
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/renderers/gl/GlUboSystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExtensionType } from '../../../extensions/Extensions';
import { generateUboSyncPolyfillSTD40 } from '../../../unsafe-eval/ubo/generateUboSyncPolyfill';
import { UboSystem } from '../shared/shader/UboSystem';
import { createUboElementsSTD40 } from './shader/utils/createUboElementsSTD40';
import { createUboSyncFunctionSTD40 } from './shader/utils/createUboSyncSTD40';

/**
* System plugin to the renderer to manage uniform buffers. But with an WGSL adaptor.
Expand All @@ -19,7 +19,7 @@ export class GlUboSystem extends UboSystem
{
super({
createUboElements: createUboElementsSTD40,
generateUboSync: createUboSyncFunctionSTD40,
generateUboSync: generateUboSyncPolyfillSTD40,
});
}
}
8 changes: 4 additions & 4 deletions src/rendering/renderers/gl/shader/GlShaderSystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ExtensionType } from '../../../../extensions/Extensions';
import { generateShaderSyncCode } from './GenerateShaderSyncCode';
import { generateShaderSyncPolyfill } from '../../../../unsafe-eval/shader/generateShaderSyncPolyfill';
import { generateProgram } from './program/generateProgram';

import type { BufferResource } from '../../shared/buffer/BufferResource';
Expand Down Expand Up @@ -89,7 +89,7 @@ export class GlShaderSystem

if (!syncFunction)
{
syncFunction = this._shaderSyncFunctions[shader.glProgram._key] = this._generateShaderSync(shader, this);
syncFunction = this._shaderSyncFunctions[shader.glProgram._key] = this._generateShaderSync();
}

syncFunction(this._renderer, shader, defaultSyncData);
Expand Down Expand Up @@ -214,8 +214,8 @@ export class GlShaderSystem
* @returns - the generated sync function
* @ignore
*/
public _generateShaderSync(shader: Shader, shaderSystem: GlShaderSystem): ShaderSyncFunction
public _generateShaderSync(): ShaderSyncFunction
{
return generateShaderSyncCode(shader, shaderSystem);
return generateShaderSyncPolyfill();
}
}
57 changes: 55 additions & 2 deletions src/rendering/renderers/gl/shader/GlUniformGroupSystem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { ExtensionType } from '../../../../extensions/Extensions';
import { generateUniformsSync } from './utils/generateUniformsSync';
// eslint-disable-next-line max-len
import { uniformArrayParserFunctions, uniformParserFunctions, uniformSingleParserFunctions } from '../../../../unsafe-eval/uniforms/uniformSyncFunctions';
import { uniformParsers } from '../../shared/shader/utils/uniformParsers';

import type { UniformUploadFunction } from '../../../../unsafe-eval/uniforms/uniformSyncFunctions';
import type { UniformsSyncCallback } from '../../shared/shader/types';
import type { UniformGroup } from '../../shared/shader/UniformGroup';
import type { System } from '../../shared/system/System';
Expand Down Expand Up @@ -99,7 +102,57 @@ export class GlUniformGroupSystem implements System

private _generateUniformsSync(group: UniformGroup, uniformData: Record<string, GlUniformData>): UniformsSyncCallback
{
return generateUniformsSync(group, uniformData);
// loop through all the uniforms..
const functionMap: Record<string, UniformUploadFunction> = {};

for (const i in group.uniformStructures)
{
if (!uniformData[i]) continue;

const uniform = group.uniformStructures[i];

let parsed = false;

for (let j = 0; j < uniformParsers.length; j++)
{
const parser = uniformParsers[j];

if (uniform.type === parser.type && parser.test(uniform))
{
functionMap[i] = uniformParserFunctions[j];

parsed = true;

break;
}
}

// if not parsed...

if (!parsed)
{
const templateType = uniform.size === 1 ? uniformSingleParserFunctions : uniformArrayParserFunctions;

functionMap[i] = templateType[uniform.type];
}
}

return (
ud: Record<string, any>,
uv: Record<string, any>,
renderer: WebGLRenderer) =>
{
const gl = renderer.gl;

for (const i in functionMap)
{
const v = uv[i];
const cu = ud[i];
const cv = ud[i].value;

functionMap[i](i, cu, cv, v, ud, uv, gl);
}
};
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/rendering/renderers/gpu/GpuUboSystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExtensionType } from '../../../extensions/Extensions';
import { generateUboSyncPolyfillWGSL } from '../../../unsafe-eval/ubo/generateUboSyncPolyfill';
import { UboSystem } from '../shared/shader/UboSystem';
import { createUboElementsWGSL } from './shader/utils/createUboElementsWGSL';
import { createUboSyncFunctionWGSL } from './shader/utils/createUboSyncFunctionWGSL';

/**
* System plugin to the renderer to manage uniform buffers. With a WGSL twist!
Expand All @@ -19,7 +19,7 @@ export class GpuUboSystem extends UboSystem
{
super({
createUboElements: createUboElementsWGSL,
generateUboSync: createUboSyncFunctionWGSL,
generateUboSync: generateUboSyncPolyfillWGSL,
});
}
}
Loading

0 comments on commit 11a1a9d

Please sign in to comment.