Skip to content

Commit

Permalink
Merge d5a21a5 into 5186867
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Mar 15, 2024
2 parents 5186867 + d5a21a5 commit 390b8eb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 29 deletions.
4 changes: 3 additions & 1 deletion modules/constants/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ export type {
GLFunctionParameters,
GLParameters,
GLLimits,
GLExtensions
GLExtensions,
GLPolygonMode,
GLProvokingVertex
} from './webgl-types';
12 changes: 7 additions & 5 deletions modules/constants/src/webgl-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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 Expand Up @@ -364,7 +368,7 @@ export type GLFunctionParameters = {
colorMask?: [boolean, boolean, boolean, boolean] | boolean[];

cull?: boolean;
cullFace?: GL.FRONT | GL.BACK | GL.FRONT_AND_BACK;
cullFace?: GLCullFaceMode;

depthTest?: boolean;
depthFunc?: GLFunction;
Expand Down Expand Up @@ -606,15 +610,13 @@ type EXT_depth_clamp = {
type WEBGL_provoking_vertex = {
// Constants in GL enum
/** Set the provoking vertex */
provokingVertexWEBGL(
provokeMode: GL.FIRST_VERTEX_CONVENTION_WEBGL | GL.LAST_VERTEX_CONVENTION_WEBGL
): void;
provokingVertexWEBGL(provokeMode: GLProvokingVertex): void;
};

/** WEBGL_polygon_mode https://registry.khronos.org/webgl/extensions/WEBGL_polygon_mode/ */
type WEBGL_polygon_mode = {
/** Set polygon mode of face to fill or line */
polygonModeWEBGL(face: GL.FRONT | GL.BACK, mode: GL.LINE_WEBGL | GL.FILL_WEBGL): void;
polygonModeWEBGL(face: GL.FRONT | GL.BACK, mode: GLPolygonMode): void;
};

/** WEBGL_clip_cull_distance https://registry.khronos.org/webgl/extensions/WEBGL_clip_cull_distance/ */
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 @@ -27,6 +27,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 @@ -130,8 +132,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 @@ -103,7 +103,9 @@ export type {
ColorParameters,
MultisampleParameters,
RenderPassParameters,
RenderPipelineParameters
RenderPipelineParameters,
PolygonMode,
ProvokingVertex
} from './adapter/types/parameters';

export type {ColorAttachment, DepthStencilAttachment} from './adapter/types/attachments';
Expand Down
49 changes: 29 additions & 20 deletions modules/webgl/src/adapter/converters/device-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// Copyright (c) vis.gl contributors

import type {CompareFunction, StencilOperation, BlendOperation, BlendFactor} from '@luma.gl/core';
import {Device, log, Parameters} from '@luma.gl/core';
import {GL} from '@luma.gl/constants';
import type {GLBlendEquation, GLBlendFunction, GLParameters} from '@luma.gl/constants';
import {Device, log, Parameters, PolygonMode, ProvokingVertex} from '@luma.gl/core';
import {GL, GLFunction, GLPolygonMode, GLProvokingVertex} from '@luma.gl/constants';
import type {GLBlendEquation, GLBlendFunction, GLParameters, 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 @@ -156,10 +156,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 @@ -169,9 +173,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 @@ -323,8 +327,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 @@ -336,8 +340,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 @@ -350,7 +354,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 @@ -366,17 +370,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 @@ -386,15 +390,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 390b8eb

Please sign in to comment.