Skip to content

Commit

Permalink
chore(shader): allow multiple precision declarations per shader
Browse files Browse the repository at this point in the history
fallenoak committed Feb 2, 2024
1 parent 1382b7c commit 2ce8a37
Showing 5 changed files with 19 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/lib/map/terrain/shader/fragment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FUNCTION_APPLY_FOG, UNIFORM_FOG_COLOR, VARIABLE_FOG_FACTOR } from '../../../shader/fog.js';
import { composeShader } from '../../../shader/util.js';

const FRAGMENT_SHADER_PRECISION = 'highp float';
const FRAGMENT_SHADER_PRECISIONS = ['highp float'];

const FRAGMENT_SHADER_UNIFORMS = [
{ name: 'layerCount', type: 'int' },
@@ -66,9 +66,9 @@ applyFog(color, ${UNIFORM_FOG_COLOR.name}, ${VARIABLE_FOG_FACTOR.name});
`;

const fragmentShader = (() => {
// Precision
// Precisions

const precision = FRAGMENT_SHADER_PRECISION;
const precisions = FRAGMENT_SHADER_PRECISIONS;

// Uniforms

@@ -100,7 +100,7 @@ const fragmentShader = (() => {
main.push(FRAGMENT_SHADER_MAIN_LIGHTING);
main.push(FRAGMENT_SHADER_MAIN_FOG);

return composeShader(precision, uniforms, inputs, outputs, functions, main);
return composeShader(precisions, uniforms, inputs, outputs, functions, main);
})();

export default fragmentShader;
8 changes: 4 additions & 4 deletions src/lib/map/terrain/shader/vertex.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import {
} from '../../../shader/fog.js';
import { composeShader } from '../../../shader/util.js';

const VERTEX_SHADER_PRECISION = 'highp float';
const VERTEX_SHADER_PRECISIONS = ['highp float'];

const VERTEX_SHADER_UNIFORMS = [
{ name: 'modelMatrix', type: 'mat4' },
@@ -58,9 +58,9 @@ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
`;

const vertexShader = (() => {
// Precision
// Precisions

const precision = VERTEX_SHADER_PRECISION;
const precisions = VERTEX_SHADER_PRECISIONS;

// Uniforms

@@ -94,7 +94,7 @@ const vertexShader = (() => {
main.push(VERTEX_SHADER_MAIN_FOG);
main.push(VERTEX_SHADER_MAIN_POSITION);

return composeShader(precision, uniforms, inputs, outputs, functions, main);
return composeShader(precisions, uniforms, inputs, outputs, functions, main);
})();

export default vertexShader;
8 changes: 4 additions & 4 deletions src/lib/model/shader/fragment.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { M2_FRAGMENT_SHADER } from '@wowserhq/format';
import { VARIABLE_FOG_FACTOR, FUNCTION_APPLY_FOG, UNIFORM_FOG_COLOR } from '../../shader/fog.js';
import { composeShader } from '../../shader/util.js';

const FRAGMENT_SHADER_PRECISION = 'highp float';
const FRAGMENT_SHADER_PRECISIONS = ['highp float'];

const FRAGMENT_SHADER_UNIFORMS = [
{ name: 'textures[2]', type: 'sampler2D' },
@@ -140,9 +140,9 @@ applyFog(color, ${UNIFORM_FOG_COLOR.name}, ${VARIABLE_FOG_FACTOR.name} * materia
`;

const createFragmentShader = (textureCount: number, combineFunction: string) => {
// Precision
// Precisions

const precision = FRAGMENT_SHADER_PRECISION;
const precisions = FRAGMENT_SHADER_PRECISIONS;

// Uniforms

@@ -197,7 +197,7 @@ const createFragmentShader = (textureCount: number, combineFunction: string) =>

main.push(FRAGMENT_SHADER_MAIN_FOG);

return composeShader(precision, uniforms, inputs, outputs, functions, main);
return composeShader(precisions, uniforms, inputs, outputs, functions, main);
};

// prettier-ignore
8 changes: 5 additions & 3 deletions src/lib/model/shader/vertex.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import {
} from '../../shader/fog.js';
import { composeShader } from '../../shader/util.js';

const VERTEX_SHADER_PRECISION = 'highp float';
const VERTEX_SHADER_PRECISIONS = ['highp float'];

const VERTEX_SHADER_UNIFORMS = [
{ name: 'bindMatrix', type: 'mat4', if: 'USE_SKINNING' },
@@ -113,7 +113,9 @@ const VERTEX_SHADER_MAIN_POSITION = `
`;

const createVertexShader = (texCoord1?: M2_TEXTURE_COORD, texCoord2?: M2_TEXTURE_COORD) => {
const precision = VERTEX_SHADER_PRECISION;
// Precisions

const precisions = VERTEX_SHADER_PRECISIONS;

// Uniforms

@@ -181,7 +183,7 @@ const createVertexShader = (texCoord1?: M2_TEXTURE_COORD, texCoord2?: M2_TEXTURE

main.push(VERTEX_SHADER_MAIN_POSITION);

return composeShader(precision, uniforms, inputs, outputs, functions, main);
return composeShader(precisions, uniforms, inputs, outputs, functions, main);
};

// prettier-ignore
4 changes: 2 additions & 2 deletions src/lib/shader/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const composeShader = (
precision: string,
precisions: string[],
uniforms: { name: string; type: string; if?: string }[],
inputs: { name: string; type: string; if?: string }[],
outputs: { name: string; type: string }[],
@@ -8,7 +8,7 @@ const composeShader = (
) => {
const lines = [];

lines.push(`precision ${precision};`);
lines.push(...precisions.map((precision) => `precision ${precision};`));

lines.push('');
for (const uniform of uniforms) {

0 comments on commit 2ce8a37

Please sign in to comment.