A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(webgpu): meshes, sprites and particles sample through their mip chain, and a compressed texture keeps every level
yhh6c96ef935067
diff --git a/sdk/src/asset/loaders/TextureLoader.ts b/sdk/src/asset/loaders/TextureLoader.tsindex a101b6f91..2d3dc77c5 100644--- a/sdk/src/asset/loaders/TextureLoader.ts+++ b/sdk/src/asset/loaders/TextureLoader.ts@@ -318,12 +318,10 @@ export class TextureLoader implements AssetLoader<TextureResult> {this.lastDecision_ = compressedUploadDecision(target, t !== null);if (target && t) {const code = engineFormatCode(target, srgb);- // Level 0 first, packed, as the device unpacks them; a WebGPU copy- // refuses a level that is not whole blocks, so the chain stops there.- const all = t.levels ?? [t];- let count = 1;- while (count < all.length && all[count].width % 4 === 0 && all[count].height % 4 === 0) count++;- const levels = all.slice(0, count);+ // Level 0 first, packed, as the device unpacks them, every level of the chain: the device copies each as whole blocks+ // (its physical size), so a level whose side is not a multiple of four is uploaded too. Stopping at the first such level+ // left a 1240x1268 hair atlas with level 0 alone, sampled unfiltered at any distance.+ const levels = t.levels ?? [t];const size = levels.reduce((n, l) => n + l.data.length, 0);const handle = withMalloc(module, size, (ptr) => {let at = ptr;diff --git a/src/esengine/data/shaders/batch.esshader b/src/esengine/data/shaders/batch.esshaderindex d5ca363f5..39a575a89 100644--- a/src/esengine/data/shaders/batch.esshader+++ b/src/esengine/data/shaders/batch.esshader@@ -192,19 +192,22 @@ struct VSOut {#endif};-// textureSampleLevel(…, 0) keeps sampling uniform-flow-legal inside the-// constant branch chain; 2D sprites sample mip 0.+// The gradients are taken before the branch chain, where every fragment of the quad runs, and each branch samples through the mip+// chain with them, as the GLSL twin's texture() does: a sprite drawn smaller than its texture reads the level that fits instead of+// shimmering from level 0, and one drawn at its size reads level 0 as before.@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {let idx = i32(v.v_texIndex + 0.5);+ let duvx = dpdx(v.v_texCoord);+ let duvy = dpdy(v.v_texCoord);var texColor : vec4f;- if (idx == 0) { texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0); }- else if (idx == 1) { texColor = textureSampleLevel(t1, s1, v.v_texCoord, 0.0); }- else if (idx == 2) { texColor = textureSampleLevel(t2, s2, v.v_texCoord, 0.0); }- else if (idx == 3) { texColor = textureSampleLevel(t3, s3, v.v_texCoord, 0.0); }- else if (idx == 4) { texColor = textureSampleLevel(t4, s4, v.v_texCoord, 0.0); }- else if (idx == 5) { texColor = textureSampleLevel(t5, s5, v.v_texCoord, 0.0); }- else if (idx == 6) { texColor = textureSampleLevel(t6, s6, v.v_texCoord, 0.0); }- else { texColor = textureSampleLevel(t7, s7, v.v_texCoord, 0.0); }+ if (idx == 0) { texColor = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy); }+ else if (idx == 1) { texColor = textureSampleGrad(t1, s1, v.v_texCoord, duvx, duvy); }+ else if (idx == 2) { texColor = textureSampleGrad(t2, s2, v.v_texCoord, duvx, duvy); }+ else if (idx == 3) { texColor = textureSampleGrad(t3, s3, v.v_texCoord, duvx, duvy); }+ else if (idx == 4) { texColor = textureSampleGrad(t4, s4, v.v_texCoord, duvx, duvy); }+ else if (idx == 5) { texColor = textureSampleGrad(t5, s5, v.v_texCoord, duvx, duvy); }+ else if (idx == 6) { texColor = textureSampleGrad(t6, s6, v.v_texCoord, duvx, duvy); }+ else { texColor = textureSampleGrad(t7, s7, v.v_texCoord, duvx, duvy); }#ifdef ES_LINEARvar tint = vec4f(srgbToLinear(v.v_color.rgb), v.v_color.a);#elsediff --git a/src/esengine/data/shaders/mesh.esshader b/src/esengine/data/shaders/mesh.esshaderindex f1a65a556..e49059b3c 100644--- a/src/esengine/data/shaders/mesh.esshader+++ b/src/esengine/data/shaders/mesh.esshader@@ -652,11 +652,15 @@ fn packNormal(n : vec3f) -> vec2f {return vec4f(packDepth(clamp(v.v_shadowClip.z / v.v_shadowClip.w * 0.5 + 0.5,0.0, 1.0)), 1.0);#else- let base = textureSampleLevel(t0, s0, v.v_texCoord, 0.0) * v.v_color;+ // Through the mip chain, as the GLSL twin's texture() is: level 0 alone shimmers on anything seen smaller than its texture, and+ // the gradients are taken here, before any branch, where every fragment of the quad still runs.+ let duvx = dpdx(v.v_texCoord);+ let duvy = dpdy(v.v_texCoord);+ let base = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy) * v.v_color;#ifdef LITvar N = normalize(v.v_worldNormal);#ifdef NORMAL_MAP- N = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormal(t1, s1, v.v_texCoord));+ N = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormalGrad(t1, s1, v.v_texCoord, duvx, duvy));#endifif (!front) { N = -N; }// A rough dielectric, and the position it really has: a shadow is cast on adiff --git a/src/esengine/data/shaders/particle.esshader b/src/esengine/data/shaders/particle.esshaderindex a2b90e662..147285e11 100644--- a/src/esengine/data/shaders/particle.esshader+++ b/src/esengine/data/shaders/particle.esshader@@ -25,7 +25,8 @@ void main() {// vertex stage — declaring them here would declare them twice.#pragma fragment wgsl@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {- let texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);+ // Through the mip chain, as the GLSL twin's texture() is.+ let texColor = textureSampleGrad(t0, s0, v.v_texCoord, dpdx(v.v_texCoord), dpdy(v.v_texCoord));return texColor * v.v_color;}#pragma enddiff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hppindex 1b206e47c..3d4890f48 100644--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp@@ -198,17 +198,20 @@ struct VSOut {+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {let idx = i32(v.v_texIndex + 0.5);+ let duvx = dpdx(v.v_texCoord);+ let duvy = dpdy(v.v_texCoord);var texColor : vec4f;- if (idx == 0) { texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0); }- else if (idx == 1) { texColor = textureSampleLevel(t1, s1, v.v_texCoord, 0.0); }- else if (idx == 2) { texColor = textureSampleLevel(t2, s2, v.v_texCoord, 0.0); }- else if (idx == 3) { texColor = textureSampleLevel(t3, s3, v.v_texCoord, 0.0); }- else if (idx == 4) { texColor = textureSampleLevel(t4, s4, v.v_texCoord, 0.0); }- else if (idx == 5) { texColor = textureSampleLevel(t5, s5, v.v_texCoord, 0.0); }- else if (idx == 6) { texColor = textureSampleLevel(t6, s6, v.v_texCoord, 0.0); }- else { texColor = textureSampleLevel(t7, s7, v.v_texCoord, 0.0); }+ if (idx == 0) { texColor = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy); }+ else if (idx == 1) { texColor = textureSampleGrad(t1, s1, v.v_texCoord, duvx, duvy); }+ else if (idx == 2) { texColor = textureSampleGrad(t2, s2, v.v_texCoord, duvx, duvy); }+ else if (idx == 3) { texColor = textureSampleGrad(t3, s3, v.v_texCoord, duvx, duvy); }+ else if (idx == 4) { texColor = textureSampleGrad(t4, s4, v.v_texCoord, duvx, duvy); }+ else if (idx == 5) { texColor = textureSampleGrad(t5, s5, v.v_texCoord, duvx, duvy); }+ else if (idx == 6) { texColor = textureSampleGrad(t6, s6, v.v_texCoord, duvx, duvy); }+ else { texColor = textureSampleGrad(t7, s7, v.v_texCoord, duvx, duvy); }#ifdef ES_LINEARvar tint = vec4f(srgbToLinear(v.v_color.rgb), v.v_color.a);#else@@ -1127,11 +1130,15 @@ fn packNormal(n : vec3f) -> vec2f {return vec4f(packDepth(clamp(v.v_shadowClip.z / v.v_shadowClip.w * 0.5 + 0.5,0.0, 1.0)), 1.0);#else- let base = textureSampleLevel(t0, s0, v.v_texCoord, 0.0) * v.v_color;+++ let duvx = dpdx(v.v_texCoord);+ let duvy = dpdy(v.v_texCoord);+ let base = textureSampleGrad(t0, s0, v.v_texCoord, duvx, duvy) * v.v_color;#ifdef LITvar N = normalize(v.v_worldNormal);#ifdef NORMAL_MAP- N = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormal(t1, s1, v.v_texCoord));+ N = perturbNormal(N, v.v_worldPos, v.v_texCoord, sampleNormalGrad(t1, s1, v.v_texCoord, duvx, duvy));#endifif (!front) { N = -N; }@@ -1188,7 +1195,8 @@ void main() {#pragma fragment wgsl@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {- let texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);++ let texColor = textureSampleGrad(t0, s0, v.v_texCoord, dpdx(v.v_texCoord), dpdy(v.v_texCoord));return texColor * v.v_color;}#pragma enddiff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex 8e945f4a7..5431bfae3 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -1025,7 +1025,9 @@ bool WebGPUDevice::backendCreateCompressedTexture(u32 id, const TextureDesc& desWGPUTexelCopyBufferLayout layout{};layout.bytesPerRow = blocksX * bi.bytesPerBlock;layout.rowsPerImage = blocksY;- WGPUExtent3D extent{lw, lh, 1};+ // The level's physical size, whole blocks: a level 634 texels tall is stored as 636, and a copy must name whole blocks. Its last+ // block row holds texels past the edge, which the sampler never reads.+ WGPUExtent3D extent{blocksX * bi.blockWidth, blocksY * bi.blockHeight, 1};wgpuQueueWriteTexture(queue_, &dst, ptr, levelBytes, &layout, &extent);ptr += levelBytes;}