Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(shadertools): Fully eliminate implicit typings #1619

Merged
merged 3 commits into from Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions modules/shadertools/src/lib/filters/prop-types.ts
@@ -1,22 +1,22 @@
export type PropDef = {
export type PropType = {
type?: string;
max?: number;
min?: number;
value?: any;
} | number;

export type PropType = {
export type PropDef = {
type: string;
value: any;
max?: number;
min?: number;
private?: boolean;
validate?(value: any, propDef: PropType): boolean;
validate?(value: any, propDef: PropDef): boolean;
};

const TYPE_DEFINITIONS = {
const TYPE_DEFINITIONS: Record<string, {validate: (value: unknown, propType: PropType) => boolean}> = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better called VALIDATOR or similar

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good idea, addressed in #1679

number: {
validate(value: unknown, propType: PropDef) {
validate(value: unknown, propType: PropType) {
return (
Number.isFinite(value) &&
typeof propType === 'object' &&
Expand All @@ -26,14 +26,16 @@ const TYPE_DEFINITIONS = {
}
},
array: {
validate(value: unknown, propType: PropDef) {
validate(value: unknown, propType: PropType) {
return Array.isArray(value) || ArrayBuffer.isView(value);
}
}
};

export function parsePropTypes(propDefs: Record<string, PropDef>): Record<string, PropType> {
const propTypes: Record<string, PropType> = {};


export function parsePropTypes(propDefs: Record<string, PropType>): Record<string, PropDef> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the types and parameter naming confusing. It's not clear why you chose to swap the names of the types but kept the parameter names.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Overhauled in same PR #1679

const propTypes: Record<string, PropDef> = {};
for (const [name, propDef] of Object.entries(propDefs)) {
propTypes[name] = parsePropType(propDef);
}
Expand All @@ -45,7 +47,7 @@ export function parsePropTypes(propDefs: Record<string, PropDef>): Record<string
* - a valid prop type object ({type, ...})
* - or just a default value, in which case type and name inference is used
*/
function parsePropType(propDef: PropDef): PropType {
function parsePropType(propDef: PropType): PropDef {
let type = getTypeOf(propDef);
if (type !== 'object') {
return {type, value: propDef, ...TYPE_DEFINITIONS[type]};
Expand All @@ -55,13 +57,15 @@ function parsePropType(propDef: PropDef): PropType {
return {type: 'object', value: null};
}
if ('type' in propDef) {
return {...propDef, ...TYPE_DEFINITIONS[propDef.type]};
// @ts-expect-error
return {...propDef, ...TYPE_DEFINITIONS[propDef.type]};
}
if (!('value' in propDef)) {
// If no type and value this object is likely the value
return {type: 'object', value: propDef};
}
type = getTypeOf(propDef.value);
// @ts-expect-error
return {type, ...propDef, ...TYPE_DEFINITIONS[type]};
}
}
Expand Down
46 changes: 27 additions & 19 deletions modules/shadertools/src/lib/shader-assembler/assemble-shaders.ts
Expand Up @@ -3,6 +3,8 @@ import {getPlatformShaderDefines, getVersionDefines, PlatformInfo} from './platf
import injectShader, {DECLARATION_INJECT_MARKER} from './inject-shader';
import transpileShader from '../transpiler/transpile-shader';
import {assert} from '../utils/assert';
import {ShaderModuleInstance} from '../shader-module/shader-module-instance';
import type { Injection } from '../shader-module/shader-module-instance';


const INJECT_SHADER_DECLARATIONS = `\n\n${DECLARATION_INJECT_MARKER}\n\n`;
Expand All @@ -24,10 +26,7 @@ precision highp float;
/** Define map */
type Defines = Record<string, string | number | boolean>;

export type HookFunction = string | { hook: string; header: string; footer: string; } | {
vs: string;
fs: string;
};
export type HookFunction = { hook: string; header: string; footer: string; signature?: string};

export type AssembleShaderOptions = {
id?: string;
Expand All @@ -36,7 +35,7 @@ export type AssembleShaderOptions = {
type?: any;
modules?: any[];
defines?: Defines;
hookFunctions?: HookFunction[] | [string, string];
hookFunctions?: (HookFunction | string | { hook: string; header: string; footer: string; })[];
inject?: object;
transpileToGLSL100?: boolean;
prologue?: boolean;
Expand Down Expand Up @@ -130,7 +129,7 @@ function assembleShader(
? `\
${versionLine}
${getShaderName({id, source, type})}
${getShaderType({type})}
${getShaderType(type)}
${getPlatformShaderDefines(platformInfo)}
${getVersionDefines(platformInfo)}
${getApplicationDefines(allDefines)}
Expand All @@ -142,9 +141,9 @@ ${isVertex ? '' : FRAGMENT_SHADER_PROLOGUE}
const hookFunctionMap = normalizeHookFunctions(hookFunctions);

// Add source of dependent modules in resolved order
const hookInjections: Record<string, string[]> = {};
const declInjections: Record<string, string[]> = {};
const mainInjections: Record<string, string[]> = {};
const hookInjections: Record<string, Injection[]> = {};
const declInjections: Record<string, Injection[]> = {};
const mainInjections: Record<string, Injection[]> = {};

for (const key in inject) {
const injection =
Expand Down Expand Up @@ -221,8 +220,8 @@ ${isVertex ? '' : FRAGMENT_SHADER_PROLOGUE}
* @param modules
* @returns
*/
function assembleGetUniforms(modules) {
return function getUniforms(opts) {
function assembleGetUniforms(modules: ShaderModuleInstance[]) {
return function getUniforms(opts: Record<string, any>): Record<string, any> {
const uniforms = {};
for (const module of modules) {
// `modules` is already sorted by dependency level. This guarantees that
Expand All @@ -234,7 +233,7 @@ function assembleGetUniforms(modules) {
};
}

function getShaderType({type}) {
function getShaderType(type: 'fs' | 'vs') {
return `
#define SHADER_TYPE_${SHADER_TYPE[type].toUpperCase()}
`;
Expand Down Expand Up @@ -277,7 +276,8 @@ function getApplicationDefines(defines: Defines = {}): string {
return sourceText;
}

function getHookFunctions(hookFunctions, hookInjections): string {
function getHookFunctions(
hookFunctions: Record<string, HookFunction>, hookInjections: Record<string, Injection[]>): string {
let result = '';
for (const hookName in hookFunctions) {
const hookFunction = hookFunctions[hookName];
Expand All @@ -301,23 +301,31 @@ function getHookFunctions(hookFunctions, hookInjections): string {
return result;
}

function normalizeHookFunctions(hookFunctions): {vs: Record<string, any>, fs: Record<string, any>} {
function normalizeHookFunctions(hookFunctions: (string | HookFunction)[]): {
vs: Record<string, HookFunction>,
fs: Record<string, HookFunction>
} {
const result: {vs: Record<string, any>, fs: Record<string, any>} = {
vs: {},
fs: {}
};

hookFunctions.forEach((hook) => {
let opts;
if (typeof hook !== 'string') {
opts = hook;
hookFunctions.forEach((hookFunction: string | HookFunction) => {
let opts: HookFunction;
let hook: string;
if (typeof hookFunction !== 'string') {
opts = hookFunction;
hook = opts.hook;
} else {
opts = {};
opts = {} as HookFunction;
hook = hookFunction;
}
hook = hook.trim();
const [stage, signature] = hook.split(':');
const name = hook.replace(/\(.+/, '');
if (stage !== 'vs' && stage !== 'fs') {
throw new Error(stage);
}
result[stage][name] = Object.assign(opts, {signature});
});

Expand Down
7 changes: 4 additions & 3 deletions modules/shadertools/src/lib/shader-assembler/inject-shader.ts
@@ -1,4 +1,5 @@
import {MODULE_INJECTORS_VS, MODULE_INJECTORS_FS} from '../../modules/module-injectors';
import type { Injection } from '../shader-module/shader-module-instance';
import {assert} from '../utils/assert';

// TODO - experimental
Expand Down Expand Up @@ -26,14 +27,14 @@ export const DECLARATION_INJECT_MARKER = '__LUMA_INJECT_DECLARATIONS__';
export default function injectShader(
source: string,
type: 'vs' | 'fs',
inject: Record<string, any>,
inject: Record<string, Injection[]>,
injectStandardStubs = false
): string {
const isVertex = type === 'vs';

for (const key in inject) {
const fragmentData = inject[key];
fragmentData.sort((a, b) => a.order - b.order);
fragmentData.sort((a: Injection, b: Injection): number => a.order - b.order);
fragments.length = fragmentData.length;
for (let i = 0, len = fragmentData.length; i < len; ++i) {
fragments[i] = fragmentData[i].injection;
Expand Down Expand Up @@ -97,7 +98,7 @@ export default function injectShader(
}

// Takes an array of inject objects and combines them into one
export function combineInjects(injects: {}[]): Record<string, string> {
export function combineInjects(injects: any[]): Record<string, string> {
const result: Record<string, string> = {};
assert(Array.isArray(injects) && injects.length > 1);
injects.forEach((inject) => {
Expand Down
Expand Up @@ -2,16 +2,24 @@ import {assert} from '../utils/assert';
import {parsePropTypes} from '../filters/prop-types';
import {ShaderModule, ShaderModuleDeprecation} from '../../types';

export type Injection = {
injection: string;
order: number;
}

export class ShaderModuleInstance {
name: string;
vs: string;
fs: string;
getModuleUniforms;
dependencies: ShaderModule[];
deprecations: ShaderModuleDeprecation[];
defines;
injections;
uniforms;
defines: Record<string, string | number>;
injections: {
vs: Record<string, Injection>;
fs: Record<string, Injection>;
};
uniforms: Record<string, any>;

constructor(props: ShaderModule) {
const {
Expand Down Expand Up @@ -70,7 +78,7 @@ ${moduleSource}\
`;
}

getUniforms(opts, uniforms) {
getUniforms(opts: Record<string, any>, uniforms: Record<string, any>): Record<string, any> {
if (this.getModuleUniforms) {
return this.getModuleUniforms(opts, uniforms);
}
Expand All @@ -81,7 +89,7 @@ ${moduleSource}\
return {};
}

getDefines(): Record<string, number> {
getDefines(): Record<string, string | number> {
return this.defines;
}

Expand Down Expand Up @@ -112,7 +120,7 @@ ${moduleSource}\
return deprecations;
}

_defaultGetUniforms(opts = {}): Record<string, any> {
_defaultGetUniforms(opts: Record<string, any> = {}): Record<string, any> {
const uniforms: Record<string, any> = {};
const propTypes = this.uniforms;

Expand All @@ -133,15 +141,24 @@ ${moduleSource}\
}


function normalizeInjections(injections) {
const result = {
function normalizeInjections(injections: Record<string, string | Injection>): {
vs: Record<string, Injection>,
fs: Record<string, Injection>
} {
const result: {
vs: Record<string, Injection>,
fs: Record<string, Injection>
} = {
vs: {},
fs: {}
};

for (const hook in injections) {
let injection = injections[hook];
const stage = hook.slice(0, 2);
if (stage !== 'vs' && stage !== 'fs') {
throw new Error(stage);
}

if (typeof injection === 'string') {
injection = {
Expand Down
2 changes: 1 addition & 1 deletion modules/shadertools/src/lib/transpiler/transpile-shader.ts
Expand Up @@ -6,7 +6,7 @@
* @note We always run transpiler even if same version e.g. 3.00 => 3.00
* RFC: https://github.com/visgl/luma.gl/blob/7.0-release/dev-docs/RFCs/v6.0/portable-glsl-300-rfc.md
*/
export default function transpileShader(source: string, targetGLSLVersion: number, isVertex): string {
export default function transpileShader(source: string, targetGLSLVersion: number, isVertex: boolean): string {
switch (targetGLSLVersion) {
case 300:
return isVertex
Expand Down
2 changes: 1 addition & 1 deletion modules/shadertools/tsconfig.json
Expand Up @@ -3,7 +3,7 @@
"include": ["src/**/*"],
"exclude": ["node_modules", "test"],
"compilerOptions": {
"noImplicitAny": false,
"noImplicitAny": true,
"composite": true,
"rootDir": "src",
"outDir": "dist"
Expand Down
4 changes: 2 additions & 2 deletions modules/webgl/src/adapter/converters/device-parameters.ts
Expand Up @@ -198,7 +198,7 @@ function convertStencilOperation(parameter: string, value: StencilOperation): GL
});
}

function convertBlendOperationToEquation(parameter, value): number {
function convertBlendOperationToEquation(parameter: string, value: string): number {
return map(parameter, value, {
'add': GL.FUNC_ADD,
'sub': GL.FUNC_SUBTRACT,
Expand All @@ -220,6 +220,6 @@ function map(parameter: string, value: any, valueMap: Record<string, any>): any
return valueMap[value];
}

function mapBoolean(parameter, value) {
function mapBoolean(parameter: string, value: boolean): boolean {
return value;
}
2 changes: 1 addition & 1 deletion modules/webgl/src/adapter/converters/sampler-parameters.ts
Expand Up @@ -40,7 +40,7 @@ export function convertSamplerParametersToWebGL(props: SamplerParameters): WebGL
}
// Note depends on WebGL extension
if (props.maxAnisotropy) {
props[GL.TEXTURE_MAX_ANISOTROPY_EXT] = props.maxAnisotropy;
params[GL.TEXTURE_MAX_ANISOTROPY_EXT] = props.maxAnisotropy;
}
return params;
}
Expand Down
3 changes: 2 additions & 1 deletion modules/webgl/src/adapter/resources/webgl-texture.ts
Expand Up @@ -357,7 +357,8 @@ export default class WEBGLTexture extends Texture {
* If size has changed, reinitializes with current format
* @note note clears image and mipmaps
*/
resize({height, width, mipmaps = false}): this {
resize(options: {height: number, width: number, mipmaps?: boolean}): this {
const {height, width, mipmaps = false} = options;
if (width !== this.width || height !== this.height) {
return this.initialize({
width,
Expand Down