From 2ce8a377935aac85dd294e74bc060820dea68fcf Mon Sep 17 00:00:00 2001 From: fallenoak Date: Thu, 1 Feb 2024 22:08:57 -0600 Subject: [PATCH] chore(shader): allow multiple precision declarations per shader --- src/lib/map/terrain/shader/fragment.ts | 8 ++++---- src/lib/map/terrain/shader/vertex.ts | 8 ++++---- src/lib/model/shader/fragment.ts | 8 ++++---- src/lib/model/shader/vertex.ts | 8 +++++--- src/lib/shader/util.ts | 4 ++-- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/lib/map/terrain/shader/fragment.ts b/src/lib/map/terrain/shader/fragment.ts index 50984ec..da45875 100644 --- a/src/lib/map/terrain/shader/fragment.ts +++ b/src/lib/map/terrain/shader/fragment.ts @@ -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; diff --git a/src/lib/map/terrain/shader/vertex.ts b/src/lib/map/terrain/shader/vertex.ts index f0430e8..8c897f8 100644 --- a/src/lib/map/terrain/shader/vertex.ts +++ b/src/lib/map/terrain/shader/vertex.ts @@ -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; diff --git a/src/lib/model/shader/fragment.ts b/src/lib/model/shader/fragment.ts index 5c5a16c..74e731b 100644 --- a/src/lib/model/shader/fragment.ts +++ b/src/lib/model/shader/fragment.ts @@ -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 diff --git a/src/lib/model/shader/vertex.ts b/src/lib/model/shader/vertex.ts index 0b9462c..abed88f 100644 --- a/src/lib/model/shader/vertex.ts +++ b/src/lib/model/shader/vertex.ts @@ -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 diff --git a/src/lib/shader/util.ts b/src/lib/shader/util.ts index c15cf97..7fd0246 100644 --- a/src/lib/shader/util.ts +++ b/src/lib/shader/util.ts @@ -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) {