Skip to content

Commit

Permalink
Resolve NumberType | NumberArrayType ambiguity with a new SizeType
Browse files Browse the repository at this point in the history
  • Loading branch information
ahocevar committed May 10, 2024
1 parent d9cce8c commit 993af2e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/ol/expr/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export const NumberType = 1 << numTypes++;
export const StringType = 1 << numTypes++;
export const ColorType = 1 << numTypes++;
export const NumberArrayType = 1 << numTypes++;
export const SizeType = NumberType | NumberArrayType;
export const AnyType = Math.pow(2, numTypes) - 1;

const typeNames = {
Expand Down
18 changes: 18 additions & 0 deletions src/ol/expr/gpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
NumberArrayType,
NumberType,
Ops,
SizeType,
StringType,
computeGeometryType,
isType,
Expand All @@ -19,6 +20,7 @@ import {
} from './expression.js';
import {Uniforms} from '../renderer/webgl/TileLayer.js';
import {asArray} from '../color.js';
import {toSize} from '../size.js';

/**
* @param {string} operator Operator
Expand Down Expand Up @@ -72,6 +74,16 @@ export function colorToGlsl(color) {
]);
}

/**
* Normalizes and converts a number or array toa `vec2` array compatible with GLSL.
* @param {number|import('../size.js').Size} size Size.
* @return {string} The color expressed in the `vec4(1.0, 1.0, 1.0, 1.0)` form.
*/
export function sizeToGlsl(size) {
const array = toSize(size);
return arrayToGlsl(array);
}

/** @type {Object<string, number>} */
const stringToFloatMap = {};
let stringToFloatCounter = 0;
Expand Down Expand Up @@ -485,6 +497,12 @@ function compile(expression, returnType, context) {
return arrayToGlsl(/** @type {Array<number>} */ (expression.value));
}

if ((expression.type & SizeType) > 0) {
return sizeToGlsl(
/** @type {number|import('../size.js').Size} */ (expression.value),
);
}

throw new Error(
`Unexpected expression ${expression.value} (expected type ${typeName(
returnType,
Expand Down
19 changes: 6 additions & 13 deletions src/ol/webgl/styleparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ColorType,
NumberArrayType,
NumberType,
SizeType,
StringType,
newParsingContext,
} from '../expr/expression.js';
Expand Down Expand Up @@ -67,7 +68,7 @@ const UNPACK_COLOR_FN = `vec4 unpackColor(vec2 packedColor) {
* @return {1|2|3|4} The amount of components for this value
*/
function getGlslSizeFromType(type) {
if (type === ColorType) {
if (type === ColorType || type === SizeType) {
return 2;
}
if (type === NumberArrayType) {
Expand Down Expand Up @@ -134,7 +135,7 @@ function parseCommonSymbolProperties(style, builder, vertContext, prefix) {
const scale = expressionToGlsl(
vertContext,
style[`${prefix}scale`],
NumberType | NumberArrayType,
SizeType,
);
builder.setSymbolSizeExpression(
`${builder.getSymbolSizeExpression()} * ${scale}`,
Expand Down Expand Up @@ -301,7 +302,7 @@ function parseCircleProperties(
const scale = expressionToGlsl(
fragContext,
style['circle-scale'],
NumberType | NumberArrayType,
SizeType,
);
currentPoint = `coordsPx / ${scale}`;
}
Expand Down Expand Up @@ -420,11 +421,7 @@ function parseShapeProperties(
// SCALE
let currentPoint = 'coordsPx';
if ('shape-scale' in style) {
const scale = expressionToGlsl(
fragContext,
style['shape-scale'],
NumberType | NumberArrayType,
);
const scale = expressionToGlsl(fragContext, style['shape-scale'], SizeType);
currentPoint = `coordsPx / ${scale}`;
}

Expand Down Expand Up @@ -584,11 +581,7 @@ function parseIconProperties(
);
let scale = `1.0`;
if (`icon-scale` in style) {
scale = expressionToGlsl(
vertContext,
style[`icon-scale`],
NumberType | NumberArrayType,
);
scale = expressionToGlsl(vertContext, style[`icon-scale`], SizeType);
}
let shiftPx;
if (
Expand Down
8 changes: 4 additions & 4 deletions test/browser/spec/ol/webgl/styleparser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,12 +898,12 @@ describe('ol.webgl.styleparser', () => {
],
'circle-radius': 8,
'circle-fill-color': ['get', 'color'],
'circle-scale': ['get', 'iconSize', 'number[]'],
'circle-scale': ['get', 'iconSize'],
});
});
it('adds attributes to the shader builder', () => {
expect(parseResult.builder.attributes_).to.eql([
'vec4 a_prop_iconSize',
'vec2 a_prop_iconSize',
'float a_prop_lineType',
'float a_prop_lineWidth',
'vec2 a_prop_color',
Expand All @@ -915,7 +915,7 @@ describe('ol.webgl.styleparser', () => {
expect(parseResult.builder.varyings_).to.eql([
{
name: 'v_prop_iconSize',
type: 'vec4',
type: 'vec2',
expression: 'a_prop_iconSize',
},
{
Expand Down Expand Up @@ -943,7 +943,7 @@ describe('ol.webgl.styleparser', () => {
});
it('returns attributes with their callbacks in the result', () => {
expect(parseResult.attributes).to.eql({
iconSize: {size: 4, callback: {}},
iconSize: {size: 2, callback: {}},
color: {size: 2, callback: {}},
lineType: {size: 1, callback: {}},
lineWidth: {size: 1, callback: {}},
Expand Down

0 comments on commit 993af2e

Please sign in to comment.