Skip to content

Commit

Permalink
feat(core): Add missing blend factors, restrict types in device-param…
Browse files Browse the repository at this point in the history
…eters (#2040)
  • Loading branch information
donmccurdy authored and felixpalmer committed Mar 18, 2024
1 parent b0e8260 commit 0655669
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
4 changes: 3 additions & 1 deletion modules/constants/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export type {
GLFunctionParameters,
GLParameters,
GLLimits,
GLExtensions
GLExtensions,
GLPolygonMode,
GLProvokingVertex
} from './webgl-types';
4 changes: 4 additions & 0 deletions modules/constants/src/webgl-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
8 changes: 6 additions & 2 deletions modules/core/src/adapter/types/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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';
Expand Down
4 changes: 3 additions & 1 deletion modules/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export type {
ColorParameters,
MultisampleParameters,
RenderPassParameters,
RenderPipelineParameters
RenderPipelineParameters,
PolygonMode,
ProvokingVertex
} from './adapter/types/parameters';

// MEMORY LAYOUT TYPES
Expand Down
65 changes: 37 additions & 28 deletions modules/webgl/src/adapter/converters/device-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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, GLProvokingVertex>(
'provokingVertex',
parameters.provokingVertex,
{
first: GL.FIRST_VERTEX_CONVENTION_WEBGL,
last: GL.LAST_VERTEX_CONVENTION_WEBGL
}
);
ext?.provokingVertexWEBGL(vertex);
}
}
Expand All @@ -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, GLPolygonMode>('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);
Expand Down Expand Up @@ -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<CompareFunction, GLFunction>(parameter, value, {
never: GL.NEVER,
less: GL.LESS,
equal: GL.EQUAL,
Expand All @@ -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<GLFunction, CompareFunction>(parameter, value, {
[GL.NEVER]: 'never',
[GL.LESS]: 'less',
[GL.EQUAL]: 'equal',
Expand All @@ -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<StencilOperation, GLStencilOp>(parameter, value, {
keep: GL.KEEP,
zero: GL.ZERO,
replace: GL.REPLACE,
Expand All @@ -374,17 +378,17 @@ function convertBlendOperationToEquation(
parameter: string,
value: BlendOperation
): GLBlendEquation {
return map(parameter, value, {
return map<BlendOperation, GLBlendEquation>(parameter, value, {
add: GL.FUNC_ADD,
subtract: GL.FUNC_SUBTRACT,
'reverse-subtract': GL.FUNC_REVERSE_SUBTRACT,
min: GL.MIN,
max: GL.MAX
} as Record<BlendOperation, GLBlendEquation>);
});
}

function convertBlendFactorToFunction(parameter: string, value: BlendFactor): GLBlendFunction {
return map(parameter, value, {
return map<BlendFactor, GLBlendFunction>(parameter, value, {
one: GL.ONE,
zero: GL.ZERO,
'src-color': GL.SRC_COLOR,
Expand All @@ -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<BlendFactor, GLBlendFunction>);
'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<string, any>): any {
function map<K extends string | number, V>(parameter: string, value: K, valueMap: Record<K, V>): V {
if (!(value in valueMap)) {
throw new Error(message(parameter, value));
}
Expand Down

0 comments on commit 0655669

Please sign in to comment.