From ceb95b7617845e7d563d2e4bcf7fafabadbd10e4 Mon Sep 17 00:00:00 2001 From: tga Date: Fri, 13 Oct 2023 18:59:25 +0800 Subject: [PATCH] fix shader error report --- src/gfx.ts | 6 ++---- src/kaboom.ts | 12 +++++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/gfx.ts b/src/gfx.ts index bf35570dd..2f612d7bb 100644 --- a/src/gfx.ts +++ b/src/gfx.ts @@ -236,11 +236,9 @@ export class Shader { if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { const vertError = gl.getShaderInfoLog(vertShader) + if (vertError) throw new Error("VERTEX SHADER " + vertError) const fragError = gl.getShaderInfoLog(fragShader) - let msg = "" - if (vertError) msg += vertError - if (fragError) msg += fragError - throw new Error(msg) + if (fragError) throw new Error("FRAGMENT SHADER " + fragError) } gl.deleteShader(vertShader) diff --git a/src/kaboom.ts b/src/kaboom.ts index ad19d9cf4..eab6ef464 100644 --- a/src/kaboom.ts +++ b/src/kaboom.ts @@ -1369,7 +1369,17 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { ): Shader { const vcode = VERT_TEMPLATE.replace("{{user}}", vertSrc ?? DEF_VERT) const fcode = FRAG_TEMPLATE.replace("{{user}}", fragSrc ?? DEF_FRAG) - return new Shader(ggl, vcode, fcode, VERTEX_FORMAT.map((vert) => vert.name)) + try { + return new Shader(ggl, vcode, fcode, VERTEX_FORMAT.map((vert) => vert.name)) + } catch (e) { + const lineOffset = 14 + const fmtRegex = /(?^\w+) SHADER ERROR: 0:(?\d+): (?.+)/ + const match = (e as Error).message.match(fmtRegex) + const line = Number(match.groups.line) - lineOffset + const msg = match.groups.msg.trim() + const ty = match.groups.type.toLowerCase() + throw new Error(`${ty} shader line ${line}: ${msg}`) + } } function makeFont(