From 065566986cb62542a205e0a3de883d6373a64911 Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Fri, 15 Mar 2024 17:49:12 -0400 Subject: [PATCH] feat(core): Add missing blend factors, restrict types in device-parameters (#2040) --- modules/constants/src/index.ts | 4 +- modules/constants/src/webgl-types.ts | 4 ++ modules/core/src/adapter/types/parameters.ts | 8 ++- modules/core/src/index.ts | 4 +- .../adapter/converters/device-parameters.ts | 65 +++++++++++-------- 5 files changed, 53 insertions(+), 32 deletions(-) diff --git a/modules/constants/src/index.ts b/modules/constants/src/index.ts index c3118e0fad..d349638711 100644 --- a/modules/constants/src/index.ts +++ b/modules/constants/src/index.ts @@ -25,5 +25,7 @@ export type { GLFunctionParameters, GLParameters, GLLimits, - GLExtensions + GLExtensions, + GLPolygonMode, + GLProvokingVertex } from './webgl-types'; diff --git a/modules/constants/src/webgl-types.ts b/modules/constants/src/webgl-types.ts index 741628c8f6..626bb8afec 100644 --- a/modules/constants/src/webgl-types.ts +++ b/modules/constants/src/webgl-types.ts @@ -182,6 +182,10 @@ export type GLStencilOp = | GL.DECR_WRAP | GL.INVERT; +export type GLPolygonMode = GL.FILL_WEBGL | GL.LINE_WEBGL; +export type GLCullFaceMode = GL.FRONT | GL.BACK | GL.FRONT_AND_BACK; +export type GLProvokingVertex = GL.FIRST_VERTEX_CONVENTION_WEBGL | GL.LAST_VERTEX_CONVENTION_WEBGL; + /** Parameters for textures and samplers */ export type GLSamplerParameters = { /** Sets the wrap parameter for texture coordinate to either GL_CLAMP_TO_EDGE, GL_MIRRORED_REPEAT, or GL_REPEAT. */ diff --git a/modules/core/src/adapter/types/parameters.ts b/modules/core/src/adapter/types/parameters.ts index de4812545a..c3e902ce7d 100644 --- a/modules/core/src/adapter/types/parameters.ts +++ b/modules/core/src/adapter/types/parameters.ts @@ -31,6 +31,8 @@ export type IndexFormat = 'uint16' | 'uint32'; export type CullMode = 'none' | 'front' | 'back'; export type FrontFace = 'ccw' | 'cw'; +export type PolygonMode = 'fill' | 'line'; +export type ProvokingVertex = 'first' | 'last'; // Rasterization Parameters @@ -134,8 +136,10 @@ export type BlendFactor = | 'dst-alpha' | 'one-minus-dst-alpha' | 'src-alpha-saturated' - | 'blend-color' - | 'one-minus-blend-color'; + | 'constant-color' + | 'one-minus-constant-color' + | 'constant-alpha' + | 'one-minus-constant-alpha'; /** BlendOperation defines the algorithm used to combine source and destination blend factors: */ export type BlendOperation = 'add' | 'subtract' | 'reverse-subtract' | 'min' | 'max'; diff --git a/modules/core/src/index.ts b/modules/core/src/index.ts index 4ea21a1743..309d43c240 100644 --- a/modules/core/src/index.ts +++ b/modules/core/src/index.ts @@ -89,7 +89,9 @@ export type { ColorParameters, MultisampleParameters, RenderPassParameters, - RenderPipelineParameters + RenderPipelineParameters, + PolygonMode, + ProvokingVertex } from './adapter/types/parameters'; // MEMORY LAYOUT TYPES diff --git a/modules/webgl/src/adapter/converters/device-parameters.ts b/modules/webgl/src/adapter/converters/device-parameters.ts index 78abda8e71..b8cbd806f6 100644 --- a/modules/webgl/src/adapter/converters/device-parameters.ts +++ b/modules/webgl/src/adapter/converters/device-parameters.ts @@ -2,18 +2,18 @@ // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors -import { - Device, - Parameters, - CompareFunction, - StencilOperation, - log, - isObjectEmpty, - BlendOperation, - BlendFactor -} from '@luma.gl/core'; +import type {CompareFunction, StencilOperation, BlendOperation, BlendFactor} from '@luma.gl/core'; +import {Device, log, isObjectEmpty, Parameters, PolygonMode, ProvokingVertex} from '@luma.gl/core'; import {GL} from '@luma.gl/constants'; -import type {GLBlendEquation, GLBlendFunction, GLParameters} from '@luma.gl/constants'; +import type { + GLBlendEquation, + GLBlendFunction, + GLFunction, + GLParameters, + GLPolygonMode, + GLProvokingVertex, + GLStencilOp +} from '@luma.gl/constants'; import {pushContextState, popContextState} from '../../context/state-tracker/track-context-state'; import {setGLParameters} from '../../context/parameters/unified-parameter-api'; import {WebGLDevice} from '../webgl-device'; @@ -164,10 +164,14 @@ export function setDeviceParameters(device: Device, parameters: Parameters) { const ext = extensions.WEBGL_provoking_vertex; if (parameters.provokingVertex) { - const vertex = map('provokingVertex', parameters.provokingVertex, { - first: GL.FIRST_VERTEX_CONVENTION_WEBGL, - last: GL.LAST_VERTEX_CONVENTION_WEBGL - }); + const vertex = map( + 'provokingVertex', + parameters.provokingVertex, + { + first: GL.FIRST_VERTEX_CONVENTION_WEBGL, + last: GL.LAST_VERTEX_CONVENTION_WEBGL + } + ); ext?.provokingVertexWEBGL(vertex); } } @@ -177,9 +181,9 @@ export function setDeviceParameters(device: Device, parameters: Parameters) { const ext = extensions.WEBGL_polygon_mode; if (parameters.polygonMode) { - const mode = map('polygonMode', parameters.provokingVertex, { + const mode = map('polygonMode', parameters.polygonMode, { fill: GL.FILL_WEBGL, - lint: GL.LINE_WEBGL + line: GL.LINE_WEBGL }); ext?.polygonModeWEBGL(GL.FRONT, mode); ext?.polygonModeWEBGL(GL.BACK, mode); @@ -331,8 +335,8 @@ export function setDeviceParameters(device: Device, parameters: Parameters) { }); */ -export function convertCompareFunction(parameter: string, value: CompareFunction): GL { - return map(parameter, value, { +export function convertCompareFunction(parameter: string, value: CompareFunction): GLFunction { + return map(parameter, value, { never: GL.NEVER, less: GL.LESS, equal: GL.EQUAL, @@ -344,8 +348,8 @@ export function convertCompareFunction(parameter: string, value: CompareFunction }); } -export function convertToCompareFunction(parameter: string, value: GL): CompareFunction { - return map(parameter, value, { +export function convertToCompareFunction(parameter: string, value: GLFunction): CompareFunction { + return map(parameter, value, { [GL.NEVER]: 'never', [GL.LESS]: 'less', [GL.EQUAL]: 'equal', @@ -358,7 +362,7 @@ export function convertToCompareFunction(parameter: string, value: GL): CompareF } function convertStencilOperation(parameter: string, value: StencilOperation): GL { - return map(parameter, value, { + return map(parameter, value, { keep: GL.KEEP, zero: GL.ZERO, replace: GL.REPLACE, @@ -374,17 +378,17 @@ function convertBlendOperationToEquation( parameter: string, value: BlendOperation ): GLBlendEquation { - return map(parameter, value, { + return map(parameter, value, { add: GL.FUNC_ADD, subtract: GL.FUNC_SUBTRACT, 'reverse-subtract': GL.FUNC_REVERSE_SUBTRACT, min: GL.MIN, max: GL.MAX - } as Record); + }); } function convertBlendFactorToFunction(parameter: string, value: BlendFactor): GLBlendFunction { - return map(parameter, value, { + return map(parameter, value, { one: GL.ONE, zero: GL.ZERO, 'src-color': GL.SRC_COLOR, @@ -394,15 +398,20 @@ function convertBlendFactorToFunction(parameter: string, value: BlendFactor): GL 'src-alpha': GL.SRC_ALPHA, 'one-minus-src-alpha': GL.ONE_MINUS_SRC_ALPHA, 'dst-alpha': GL.DST_ALPHA, - 'one-minus-dst-alpha': GL.ONE_MINUS_DST_ALPHA - } as Record); + 'one-minus-dst-alpha': GL.ONE_MINUS_DST_ALPHA, + 'src-alpha-saturated': GL.SRC_ALPHA_SATURATE, + 'constant-color': GL.CONSTANT_COLOR, + 'one-minus-constant-color': GL.ONE_MINUS_CONSTANT_COLOR, + 'constant-alpha': GL.CONSTANT_ALPHA, + 'one-minus-constant-alpha': GL.ONE_MINUS_CONSTANT_ALPHA + }); } function message(parameter: string, value: any): string { return `Illegal parameter ${value} for ${parameter}`; } -function map(parameter: string, value: any, valueMap: Record): any { +function map(parameter: string, value: K, valueMap: Record): V { if (!(value in valueMap)) { throw new Error(message(parameter, value)); }