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
feat(render): post-process effects can be compute shaders, and Sharpen is one
yhhd8267637d60b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 75b852de3..3b2737e42 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,7 @@ published separately; it ships inside the editor.### Added- **Contact shadows and screen-space bounce light.** Contact Shadows and Screen Space GI effects add small sharp shadows at contacts and one bounce of colour between surfaces, and the weather softens the sun's shadows. `contactShadows` marches a short ray toward the scene's sun in the depth buffer; `ssgi` gathers eight rays a pixel over the G-buffer's normals at half size into a float target and averages the neighbours on the same surface before laying the light, times the receiver's colour, over the picture. The Weather component raises the sun's `sourceAngle` and lowers its `shadowStrength` as the sky clouds over.+- **Compute effects.** A post-process effect can be a WGSL compute shader over the picture, run on WebGPU and left out on WebGL2, and Sharpen is the first. An effect registers with `compute` instead of `factory`; it is handed the picture so far, a storage texture to write, its parameters, the size and the scene's depth. Sharpen pushes each pixel away from its neighbours and holds it inside their range, so an edge steepens without a halo. A compute effect refines a picture that is already whole, so a device with no compute stage simply goes without it.- **Sky.** The Sky Atmosphere effect scatters the sun's light through an atmosphere and draws volumetric clouds that the sun lights and the wind moves. The dome is single scattering through a planet's atmosphere (Rayleigh and Mie), so a sunset's reds and the afterglow come from the same calculation as noon's blue; the clouds are a slab marched against a noise density, lit through what lies between them and the sun, and a Weather's overcast raises their coverage.- **Time of day.** A TimeOfDay component moves the sun and the moon across the sky, and a Sky Atmosphere effect draws the day, the dusk and the night. The sun is placed from the hour, the latitude and the season; the scene's directional light is aimed at it by day and at the moon by night, scaled and tinted by a day's curve, and the ambient light, baked reflections, fog and exposure follow. Sky Atmosphere draws the dome, the sun, a dusk glow, the moon and stars where there is no surface, and Weather greys it as it clouds over. A directional light that follows a panorama's sun can now be aimed by its own Transform (`aimFollowedSun`), which is what lets a panorama photographed with the sun in one place be lit by a sun that moves.- **Weather.** A Weather component puts a scene under clear, mist, overcast, rain or storm, turns between them and cycles by itself. A preset is a set of multipliers of what the scene already has — the sun at a fraction of its strength, the fog thicker, the picture greyer — plus absolute levels for rain and wetness, so one preset fits any project. The component drives the directional and ambient lights (in play mode only), the effects the scene's volumes resolve (Height Fog, Rain, Screen Space Reflections, Colour Grade, Auto Exposure), and takes `registerWeatherPreset` for weathers of a project's own. Volumes whose effects differ only in numbers now write the numbers into the passes they have instead of rebuilding them. Reflection probes follow the weather: the ambient light scales what baked probes show and how much colour they keep. The Ambient `Light` gains `reflectionScale` and `reflectionSaturation`, which the weather drives. Rain stays off what stands under a roof, and the Weather component turns a looping rain track up and down. The frame draws the scene once from straight above, depth only, around the camera whenever a `rain` effect is in the chain, and the effect keeps wetness, puddles, ripples and streaks off ground that something stands over; `soundSource` names the `AudioSource` the weather drives.diff --git a/desktop b/desktopindex 7a4114229..b0b3e718c 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit 7a4114229c64e56045d2bd31e1e5b4b83d6a0903+Subproject commit b0b3e718c4b901401334eb303b4675613726be49diff --git a/docs/astro/src/content/docs/graphics/post-processing.mdx b/docs/astro/src/content/docs/graphics/post-processing.mdxindex d23a6b707..130d5253d 100644--- a/docs/astro/src/content/docs/graphics/post-processing.mdx+++ b/docs/astro/src/content/docs/graphics/post-processing.mdx@@ -25,6 +25,7 @@ several can stack in one frame at no extra target cost beyond the passes.| `distanceFog` | Linear fog by distance from the camera | `near`, `far`, `intensity`, colour || `heightFog` | Exponential height fog with the sun's glow and a hazy horizon | `density`, `heightFalloff`, `baseHeight`, `sunAzimuth`, `sunElevation`, `skyFog`, colours || `skyAtmosphere` | An atmosphere that scatters the sun's light, with sun, moon, stars and volumetric clouds, drawn where there is no surface | `intensity`, `haze`, `altitude`, `clouds`, `cloudBase`, `cloudThickness`, `windSpeed`, `overcast`, `sunAzimuth`, `sunElevation` |+| `sharpen` | Sharpening that cannot make a halo (a compute effect: WebGPU only) | `intensity`, `limit` || `lightShafts` | Sun rays through whatever stands in front of it | `intensity`, `sunAzimuth`, `sunElevation`, `decay`, `reach` || `contactShadows` | The small sharp shadows at a contact a shadow map is too coarse to hold | `intensity`, `length`, `thickness` || `ssgi` | One bounce of diffuse light between the surfaces the camera sees | `intensity`, `radius`, `thickness`, `maxBounce` |@@ -147,6 +148,43 @@ a second output (`layout(location = 1) out vec4`, or a struct return in WGSL) anOn WebGL2 and WebGPU alike the attachment is multisampled with the scene and resolved with it. A blended draw never writesit — a normal averaged with the one behind it is neither's — and the scene's own background clears it to 0.+### Compute effects (WebGPU)++An effect can be a **compute shader** instead of a draw: WGSL run over the whole picture, which is how an effect that reads+many neighbours or keeps a result of its own is written without a render pass per step. `sharpen` is one: each pixel is+pushed away from the mean of its neighbours and then held inside the range they span, so an edge gets steeper without a halo.++A compute effect needs a device with a compute stage, which is **WebGPU**. On WebGL2 (and in Play, which runs on WebGL2) the effect+is left out of the chain and the picture goes through as it was — a compute effect is for refining a picture that is already whole,+so write one that way. Ask `postprocess_supportsCompute` (or check the backend) if your game needs to know.++Register your own like any other effect, with `compute` in place of `factory`:++```ts+import { registerEffect } from 'esengine';++registerEffect({+ type: 'myInvert',+ label: 'Invert',+ compute: { source: INVERT_WGSL, workgroupSize: [8, 8] },+ // The params block: these, as f32s, in this order.+ uniforms: [{ name: 'u_amount', label: 'Amount', min: 0, max: 1, step: 0.01, defaultValue: 1 }],+});+```++The shader gets these at group 0, and declares the ones it uses, with an entry point named `main`:++| Binding | What | Declared as |+|---|---|---|+| 0 | the image so far | `texture_2d<f32>` |+| 1 | the image it writes, the same size | `texture_storage_2d<rgba16float, write>` |+| 2 | the effect's `uniforms`, as f32s in listed order | `var<uniform>` of a struct that starts with them |+| 3 | the size, `vec4f(w, h, 1/w, 1/h)` | `var<uniform>` |+| 4 | the scene's depth | `texture_depth_2d` |+| 5 | a linear clamp sampler | `sampler` |++It is dispatched over the whole picture in groups of `workgroupSize`, so it checks that its pixel is inside the image.+### Fog and atmosphere`heightFog` is fog that thins with height: a valley fills, a hilltop clears. Its density fallsdiff --git a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdxindex 5a9911eaf..6a148c65a 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/post-processing.mdx@@ -24,6 +24,7 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess| `distanceFog` | 按到相机的距离做线性雾 | `near`、`far`、`intensity`、颜色 || `heightFog` | 指数高度雾,带太阳辉光和朦胧的地平线 | `density`、`heightFalloff`、`baseHeight`、`sunAzimuth`、`sunElevation`、`skyFog`、颜色 || `skyAtmosphere` | 散射太阳光的大气,带太阳、月亮、星星和体积云,画在没有表面的地方 | `intensity`、`haze`、`altitude`、`clouds`、`cloudBase`、`cloudThickness`、`windSpeed`、`overcast`、`sunAzimuth`、`sunElevation` |+| `sharpen` | 不会出光晕的锐化(计算特效:仅 WebGPU) | `intensity`、`limit` || `lightShafts` | 太阳光穿过前方遮挡物形成的光束 | `intensity`、`sunAzimuth`、`sunElevation`、`decay`、`reach` || `contactShadows` | 阴影贴图太粗放不下的接触处的小而锐利的阴影 | `intensity`、`length`、`thickness` || `ssgi` | 相机看得见的表面之间的一次漫反射光弹射 | `intensity`、`radius`、`thickness`、`maxBounce` |@@ -114,6 +115,41 @@ Estella 通过 ping-pong 帧缓冲运行全屏后处理。加一个 `PostProcess在 WebGL2 和 WebGPU 上,附件都与场景一起多重采样、一起解析。混合绘制从不写它——与背后那个平均过的法线谁也不是——场景自己的背景把它清成 0。+### 计算特效(WebGPU)++一个特效可以是**计算着色器**而不是绘制:在整张画面上运行的 WGSL,适合需要读取很多相邻像素、或要保留自己结果的特效,不必每一步都开一个渲染 pass。+`sharpen` 就是一个:每个像素被推离邻居的均值,再被限制在邻居所张成的范围内,所以边缘变陡却不会出光晕。++计算特效需要有计算阶段的设备,也就是 **WebGPU**。在 WebGL2(以及运行在 WebGL2 上的 Play)里,这个特效会被从链路中拿掉,画面原样通过——+计算特效是用来精修一张已经完整的画面的,所以请按这个思路写。游戏需要知道时,可以问 `postprocess_supportsCompute`(或检查后端)。++像注册其他特效一样注册你自己的,用 `compute` 代替 `factory`:++```ts+import { registerEffect } from 'esengine';++registerEffect({+ type: 'myInvert',+ label: '反相',+ compute: { source: INVERT_WGSL, workgroupSize: [8, 8] },+ // 参数块:这些,按这个顺序,作为 f32。+ uniforms: [{ name: 'u_amount', label: '强度', min: 0, max: 1, step: 0.01, defaultValue: 1 }],+});+```++着色器在 group 0 里拿到下面这些,声明它用到的即可,入口函数叫 `main`:++| 绑定 | 内容 | 声明为 |+|---|---|---|+| 0 | 到目前为止的画面 | `texture_2d<f32>` |+| 1 | 它要写入的画面,同样大小 | `texture_storage_2d<rgba16float, write>` |+| 2 | 特效的 `uniforms`,按列出的顺序作为 f32 | 以它们开头的结构体的 `var<uniform>` |+| 3 | 尺寸,`vec4f(w, h, 1/w, 1/h)` | `var<uniform>` |+| 4 | 场景深度 | `texture_depth_2d` |+| 5 | 线性夹取采样器 | `sampler` |++它按 `workgroupSize` 为一组铺满整张画面,所以要自己检查像素是否在图像内。+### 雾与大气`heightFog` 是随高度变薄的雾:山谷被填满,山顶是清朗的。它的密度按指数下降,所以射线穿过多少雾有diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.jsonindex c1da6762e..c0fa93f4a 100644--- a/docs/astro/src/data/apiStability.generated.json+++ b/docs/astro/src/data/apiStability.generated.json@@ -3,10 +3,10 @@"counts": {"public": 122,"beta": 164,- "experimental": 2297,+ "experimental": 2298,"deprecated": 3},- "total": 2583,+ "total": 2584,"subsystems": [{"id": "mobile-controls",diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex f454824b5..797cbb9ef 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -1,7 +1,7 @@# API surface — esengine/index<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 122 public · 152 beta · 1669 experimental · 3 deprecated+Symbols: 122 public · 152 beta · 1670 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -2605,6 +2605,12 @@ thresholdDb: number | undefinedtype: "compressor"```+## ComputeEffectSource — interface @experimental+```+source: string+workgroupSize: [number, number] | undefined+```+## ConsoleLevel — type @experimental```'info' | 'warn' | 'error'@@ -3571,7 +3577,8 @@ v: WorldAxis## EffectDef — interface @experimental```colors: EffectColorDef[] | undefined-factory: () => ShaderHandle+compute: ComputeEffectSource | undefined+factory: (() => ShaderHandle) | undefinedlabel: stringmultiPass: EffectSubPass[] | undefinedtextures: EffectTextureDef[] | undefined@@ -6649,6 +6656,7 @@ static prototype: PostProcessPlugin## PostProcessStack — class @experimental```+addComputePass: (name: string, compute: ComputePassSource) => PostProcessStackaddPass: (name: string, shader: ShaderHandle, scale?: number, continuesEffect?: boolean) => PostProcessStackclearDirty: () => voidclearPasses: () => PostProcessStackdiff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 339d456c1..7912e7e38 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -1,7 +1,7 @@# API surface — esengine/index.native<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 122 public · 157 beta · 1688 experimental · 3 deprecated+Symbols: 122 public · 157 beta · 1689 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -2613,6 +2613,12 @@ thresholdDb: number | undefinedtype: "compressor"```+## ComputeEffectSource — interface @experimental+```+source: string+workgroupSize: [number, number] | undefined+```+## ConsoleLevel — type @experimental```'info' | 'warn' | 'error'@@ -3579,7 +3585,8 @@ v: WorldAxis## EffectDef — interface @experimental```colors: EffectColorDef[] | undefined-factory: () => ShaderHandle+compute: ComputeEffectSource | undefined+factory: (() => ShaderHandle) | undefinedlabel: stringmultiPass: EffectSubPass[] | undefinedtextures: EffectTextureDef[] | undefined@@ -6852,6 +6859,7 @@ static prototype: PostProcessPlugin## PostProcessStack — class @experimental```+addComputePass: (name: string, compute: ComputePassSource) => PostProcessStackaddPass: (name: string, shader: ShaderHandle, scale?: number, continuesEffect?: boolean) => PostProcessStackclearDirty: () => voidclearPasses: () => PostProcessStackdiff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 8ce8ead9f..9681a7345 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -1,7 +1,7 @@# API surface — esengine/index.node<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 122 public · 154 beta · 1668 experimental · 3 deprecated+Symbols: 122 public · 154 beta · 1669 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -2600,6 +2600,12 @@ thresholdDb: number | undefinedtype: "compressor"```+## ComputeEffectSource — interface @experimental+```+source: string+workgroupSize: [number, number] | undefined+```+## ConsoleLevel — type @experimental```'info' | 'warn' | 'error'@@ -3566,7 +3572,8 @@ v: WorldAxis## EffectDef — interface @experimental```colors: EffectColorDef[] | undefined-factory: () => ShaderHandle+compute: ComputeEffectSource | undefined+factory: (() => ShaderHandle) | undefinedlabel: stringmultiPass: EffectSubPass[] | undefinedtextures: EffectTextureDef[] | undefined@@ -6644,6 +6651,7 @@ static prototype: PostProcessPlugin## PostProcessStack — class @experimental```+addComputePass: (name: string, compute: ComputePassSource) => PostProcessStackaddPass: (name: string, shader: ShaderHandle, scale?: number, continuesEffect?: boolean) => PostProcessStackclearDirty: () => voidclearPasses: () => PostProcessStackdiff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 4042bc453..079a4fe30 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -1,7 +1,7 @@# API surface — esengine/minigame<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 122 public · 152 beta · 1690 experimental · 3 deprecated+Symbols: 122 public · 152 beta · 1691 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -2600,6 +2600,12 @@ thresholdDb: number | undefinedtype: "compressor"```+## ComputeEffectSource — interface @experimental+```+source: string+workgroupSize: [number, number] | undefined+```+## ConsoleLevel — type @experimental```'info' | 'warn' | 'error'@@ -3566,7 +3572,8 @@ v: WorldAxis## EffectDef — interface @experimental```colors: EffectColorDef[] | undefined-factory: () => ShaderHandle+compute: ComputeEffectSource | undefined+factory: (() => ShaderHandle) | undefinedlabel: stringmultiPass: EffectSubPass[] | undefinedtextures: EffectTextureDef[] | undefined@@ -6937,6 +6944,7 @@ static prototype: PostProcessPlugin## PostProcessStack — class @experimental```+addComputePass: (name: string, compute: ComputePassSource) => PostProcessStackaddPass: (name: string, shader: ShaderHandle, scale?: number, continuesEffect?: boolean) => PostProcessStackclearDirty: () => voidclearPasses: () => PostProcessStackdiff --git a/sdk/etc/wasm.api.md b/sdk/etc/wasm.api.mdindex d3b8b7071..e742f7621 100644--- a/sdk/etc/wasm.api.md+++ b/sdk/etc/wasm.api.md@@ -382,6 +382,7 @@ particle_set_size_lut: ((entity: number, ptr: number, count: number) => void) |particle_set_speed_lut: ((entity: number, ptr: number, count: number) => void) | undefinedparticle_stop: ((registry: CppRegistry, entity: number) => void) | undefinedparticle_update: ((registry: CppRegistry, dt: number) => void) | undefined+postprocess_addComputePass: ((name: string, wgsl: string, params: string, workgroupX: number, workgroupY: number) => number) | undefinedpostprocess_addPass: (name: string, shaderHandle: number) => numberpostprocess_addScreenPass: (name: string, shaderHandle: number) => numberpostprocess_begin: () => void@@ -412,6 +413,7 @@ postprocess_setUniformFloat: (passName: string, uniform: string, value: number)postprocess_setUniformVec4: (passName: string, uniform: string, x: number, y: number, z: number, w: number) => voidpostprocess_setViewExposure: ((ev: number) => void) | undefinedpostprocess_shutdown: () => void+postprocess_supportsCompute: (() => boolean) | undefinedprobe_volume_create: ((resX: number, resY: number, resZ: number, shPtr: number) => number) | undefinedprobe_volume_release: ((volumeHandle: number) => void) | undefinedprovideReplacementDevice: (() => boolean) | undefineddiff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex 8017f78bc..894753c0e 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -1,7 +1,7 @@# API surface — esengine/wechat<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 122 public · 152 beta · 1681 experimental · 3 deprecated+Symbols: 122 public · 152 beta · 1682 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -2600,6 +2600,12 @@ thresholdDb: number | undefinedtype: "compressor"```+## ComputeEffectSource — interface @experimental+```+source: string+workgroupSize: [number, number] | undefined+```+## ConsoleLevel — type @experimental```'info' | 'warn' | 'error'@@ -3566,7 +3572,8 @@ v: WorldAxis## EffectDef — interface @experimental```colors: EffectColorDef[] | undefined-factory: () => ShaderHandle+compute: ComputeEffectSource | undefined+factory: (() => ShaderHandle) | undefinedlabel: stringmultiPass: EffectSubPass[] | undefinedtextures: EffectTextureDef[] | undefined@@ -6651,6 +6658,7 @@ static prototype: PostProcessPlugin## PostProcessStack — class @experimental```+addComputePass: (name: string, compute: ComputePassSource) => PostProcessStackaddPass: (name: string, shader: ShaderHandle, scale?: number, continuesEffect?: boolean) => PostProcessStackclearDirty: () => voidclearPasses: () => PostProcessStackdiff --git a/sdk/src/core-render.ts b/sdk/src/core-render.tsindex 1e1e225cb..54105419e 100644--- a/sdk/src/core-render.ts+++ b/sdk/src/core-render.ts@@ -170,6 +170,7 @@ export {type OutputTransform,type HdrFormat,type EffectDef,+ type ComputeEffectSource,type EffectUniformDef,getEffectDef,getEffectTypes,diff --git a/sdk/src/ecs/bridge/nativeEngineApi.generated.ts b/sdk/src/ecs/bridge/nativeEngineApi.generated.tsindex ea183698c..ec2d406ea 100644--- a/sdk/src/ecs/bridge/nativeEngineApi.generated.ts+++ b/sdk/src/ecs/bridge/nativeEngineApi.generated.ts@@ -99,6 +99,7 @@ export interface NativeEngineApi {particle_set_speed_lut?(entity: number, ptr: number, count: number): void;particle_stop?(registry: unknown, entity: number): void;particle_update?(registry: unknown, dt: number): void;+ postprocess_addComputePass?(name: string, wgsl: string, params: string, workgroupX: number, workgroupY: number): number;postprocess_addPass?(name: string, shaderHandle: number): number;postprocess_addScreenPass?(name: string, shaderHandle: number): number;postprocess_begin?(): void;@@ -129,6 +130,7 @@ export interface NativeEngineApi {postprocess_setUniformVec4?(passName: string, uniform: string, x: number, y: number, z: number, w: number): void;postprocess_setViewExposure?(ev: number): void;postprocess_shutdown?(): void;+ postprocess_supportsCompute?(): boolean;probe_volume_create?(resX: number, resY: number, resZ: number, shPtr: number): number;probe_volume_release?(volumeHandle: number): void;registry_batchSyncPhysicsTransforms?(registry: unknown, bufferPtr: number, count: number, ppu: number): void;@@ -412,6 +414,7 @@ export function createNativeEngineApi(bind('particle_set_speed_lut', 'es_particle_set_speed_lut', false);bind('particle_stop', 'es_particle_stop', true);bind('particle_update', 'es_particle_update', true);+ bind('postprocess_addComputePass', 'es_postprocess_addComputePass', false);bind('postprocess_addPass', 'es_postprocess_addPass', false);bind('postprocess_addScreenPass', 'es_postprocess_addScreenPass', false);bind('postprocess_begin', 'es_postprocess_begin', false);@@ -442,6 +445,7 @@ export function createNativeEngineApi(bind('postprocess_setUniformVec4', 'es_postprocess_setUniformVec4', false);bind('postprocess_setViewExposure', 'es_postprocess_setViewExposure', false);bind('postprocess_shutdown', 'es_postprocess_shutdown', false);+ bind('postprocess_supportsCompute', 'es_postprocess_supportsCompute', false);bind('probe_volume_create', 'es_probe_volume_create', false);bind('probe_volume_release', 'es_probe_volume_release', false);bind('registry_batchSyncPhysicsTransforms', 'es_registry_batchSyncPhysicsTransforms', true);diff --git a/sdk/src/postprocess.ts b/sdk/src/postprocess.tsindex 8d259fe82..b5a9fd79c 100644--- a/sdk/src/postprocess.ts+++ b/sdk/src/postprocess.ts@@ -14,6 +14,7 @@ export {POSTPROCESS_VERTEX,type PassConfig,type EffectDef,+ type ComputeEffectSource,type EffectUniformDef,registerEffect,getEffectDef,diff --git a/sdk/src/postprocess/PostProcessAPI.ts b/sdk/src/postprocess/PostProcessAPI.tsindex 354d1a948..a85af9876 100644--- a/sdk/src/postprocess/PostProcessAPI.ts+++ b/sdk/src/postprocess/PostProcessAPI.ts@@ -28,7 +28,9 @@ type PostProcessCore = Required<Pick<NonNullable<EngineApi>,| 'postprocess_beginScreenCapture' | 'postprocess_endScreenCapture'| 'postprocess_executeScreenPasses' | 'postprocess_addScreenPass'| 'postprocess_clearScreenPasses' | 'postprocess_setScreenUniformFloat'- | 'postprocess_setScreenUniformVec4'>>;+ | 'postprocess_setScreenUniformVec4'>>+ // A core without a compute stage (or built before there was one) does not answer this one.+ & Pick<NonNullable<EngineApi>, 'postprocess_addComputePass' | 'postprocess_supportsCompute'>;/** Guarded view of the core: after a wasm abort a call throws instead of reaching* a dead module; a native host's bindings never abort. */@@ -118,7 +120,14 @@ export function syncStackToWasm(stack: PostProcessStack, force = false,for (const pass of stack.passes) {if (!pass.enabled || !allow(pass.name)) continue;try {- m.postprocess_addPass(pass.name, pass.shader);+ if (pass.compute) {+ // No compute on this core (WebGL2): the pass is left out and the picture goes through as it was.+ if (!m.postprocess_addComputePass) continue;+ m.postprocess_addComputePass(pass.name, pass.compute.source, pass.compute.params.join(','),+ pass.compute.workgroupSize[0], pass.compute.workgroupSize[1]);+ } else {+ m.postprocess_addPass(pass.name, pass.shader);+ }// Only when it is not 1: every pass that predates fractional sizing// means full size, and saying so is the same answer.if (pass.scale !== 1) m.postprocess_setPassScale(pass.name, pass.scale);diff --git a/sdk/src/postprocess/PostProcessStack.ts b/sdk/src/postprocess/PostProcessStack.tsindex 108bcfe50..201112978 100644--- a/sdk/src/postprocess/PostProcessStack.ts+++ b/sdk/src/postprocess/PostProcessStack.ts@@ -20,6 +20,15 @@ export interface PassConfig {/** A later pass of a multi-pass effect: its composite reads the input the* effect started from rather than the pass before it. */continuesEffect: boolean;+ /** Set for a compute pass: it has no `shader`, and the engine dispatches this WGSL over the picture instead. */+ compute?: ComputePassSource;+}++/** What a compute pass runs: the WGSL, the names of its f32 params in the order its params block lists them, and its workgroup size. */+export interface ComputePassSource {+ source: string;+ params: string[];+ workgroupSize: [number, number];}/**@@ -77,6 +86,23 @@ export class PostProcessStack {return this;}+ /** Adds a compute pass (see {@link ComputePassSource}). A device with no compute leaves it out of the chain. */+ addComputePass(name: string, compute: ComputePassSource): this {+ this.passes_.push({+ name,+ shader: 0 as ShaderHandle,+ enabled: true,+ floatUniforms: new Map(),+ vec4Uniforms: new Map(),+ textureUniforms: new Map(),+ scale: 1,+ continuesEffect: false,+ compute,+ });+ this.dirty_ = true;+ return this;+ }+removePass(name: string): this {const idx = this.passes_.findIndex(p => p.name === name);if (idx !== -1) {diff --git a/sdk/src/postprocess/computeEffects.ts b/sdk/src/postprocess/computeEffects.tsnew file mode 100644index 000000000..ecb1d0dd1--- /dev/null+++ b/sdk/src/postprocess/computeEffects.ts@@ -0,0 +1,43 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file computeEffects.ts+ * @brief The effects that run as compute shaders — WGSL over the picture, for a device with a compute stage.+ *+ * @details See {@link ComputeEffectSource} for what a compute effect is handed. These leave the chain untouched on a device+ * with none (WebGL2), which is the right answer for an effect that only refines a picture that is already whole.+ */++/**+ * Sharpening that cannot make a halo: each pixel is pushed away from the mean of its four neighbours, then held inside the range+ * those neighbours (and the pixel) span, widened by `limit`. A flat area has nothing to push against and an edge cannot overshoot+ * past what is on either side of it, which is what a plain unsharp mask gets wrong. Runs in the chain's own (HDR) values.+ */+export const SHARPEN_WGSL = /* wgsl */ `+struct Params { intensity : f32, limit : f32, pad0 : f32, pad1 : f32 };+@group(0) @binding(0) var src : texture_2d<f32>;+@group(0) @binding(1) var dst : texture_storage_2d<rgba16float, write>;+@group(0) @binding(2) var<uniform> params : Params;++fn load(p : vec2i, size : vec2i) -> vec4f {+ return textureLoad(src, clamp(p, vec2i(0), size - vec2i(1)), 0);+}++@compute @workgroup_size(8, 8)+fn main(@builtin(global_invocation_id) id : vec3u) {+ let size = vec2i(textureDimensions(src));+ let p = vec2i(id.xy);+ if (p.x >= size.x || p.y >= size.y) { return; }+ let c = load(p, size);+ let n = load(p + vec2i(0, -1), size).rgb;+ let s = load(p + vec2i(0, 1), size).rgb;+ let w = load(p + vec2i(-1, 0), size).rgb;+ let e = load(p + vec2i(1, 0), size).rgb;+ let lo = min(min(min(n, s), min(w, e)), c.rgb);+ let hi = max(max(max(n, s), max(w, e)), c.rgb);+ var sharp = c.rgb + params.intensity * (4.0 * c.rgb - n - s - w - e);+ let room = (hi - lo) * params.limit;+ sharp = clamp(sharp, lo - room, hi + room);+ textureStore(dst, p, vec4f(max(sharp, vec3f(0.0)), c.a));+}+`;diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.tsindex 7cce90016..7e43a9c21 100644--- a/sdk/src/postprocess/effects.ts+++ b/sdk/src/postprocess/effects.ts@@ -2,6 +2,7 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport type { ShaderHandle } from '../render/material';import { postProcessEffects } from './postProcessEffects';+import { SHARPEN_WGSL } from './computeEffects';export interface EffectUniformDef {name: string;@@ -43,10 +44,26 @@ export interface EffectSubPass {scale?: number;}+/**+ * An effect that is a compute shader over the picture rather than a draw. The WGSL is handed, at group 0: `@binding(0)` the image+ * so far (`texture_2d<f32>`), `@binding(1)` the image it writes (`texture_storage_2d<rgba16float, write>`), `@binding(2)` a uniform+ * block of the effect's `uniforms` as f32s in the order they are listed, `@binding(3)` the size (`vec4f(w, h, 1/w, 1/h)`),+ * `@binding(4)` the scene's depth (`texture_depth_2d`) and `@binding(5)` a linear clamp sampler; it declares the ones it uses and+ * an entry point named `main`. Only a device with a compute stage (WebGPU) runs it: on WebGL2 the effect is left out.+ */+export interface ComputeEffectSource {+ source: string;+ /** The workgroup size the shader declares; it is dispatched over the whole picture in groups of this. Default 8 x 8. */+ workgroupSize?: [number, number];+}+export interface EffectDef {type: string;label: string;- factory: () => ShaderHandle;+ /** What compiles the effect's draw shader. Absent for a compute effect (see {@link compute}). */+ factory?: () => ShaderHandle;+ /** Set instead of `factory` for an effect that runs as a compute shader. */+ compute?: ComputeEffectSource;uniforms: EffectUniformDef[];/** Texture params, carried on the volume as serialized refs (`textures`). */textures?: EffectTextureDef[];@@ -270,6 +287,19 @@ registerEffect({],});+// A compute effect: only a device with a compute stage (WebGPU) runs it; on WebGL2 the picture goes through as it was.+registerEffect({+ type: 'sharpen',+ label: 'Sharpen',+ compute: { source: SHARPEN_WGSL },+ uniforms: [+ // How hard a pixel is pushed away from the mean of its neighbours. 0 leaves the picture alone.+ { name: 'u_intensity', label: 'Intensity', min: 0, max: 2, step: 0.01, defaultValue: 0.5, neutralValue: 0 },+ // How far past the range of its neighbours a pixel may be pushed, as a share of that range: 0 cannot overshoot at all.+ { name: 'u_limit', label: 'Overshoot Limit', min: 0, max: 1, step: 0.01, defaultValue: 0.25 },+ ],+});+registerEffect({type: 'heightFog',label: 'Height Fog',diff --git a/sdk/src/postprocess/index.ts b/sdk/src/postprocess/index.tsindex 38d99cdd8..f9fa7c869 100644--- a/sdk/src/postprocess/index.ts+++ b/sdk/src/postprocess/index.ts@@ -9,6 +9,7 @@ export { postProcessEffects } from './postProcessEffects';export { POSTPROCESS_VERTEX } from './shaders';export {type EffectDef,+ type ComputeEffectSource,type EffectUniformDef,registerEffect,getEffectDef,diff --git a/sdk/src/postprocess/volumeSystem.ts b/sdk/src/postprocess/volumeSystem.tsindex 3897870e8..cb7da4ffb 100644--- a/sdk/src/postprocess/volumeSystem.ts+++ b/sdk/src/postprocess/volumeSystem.ts@@ -175,7 +175,15 @@ function applyBlendedEffects(applyTextures(stack, subPass.name, effectData.textures);applyColors(stack, subPass.name, effectData.colors);}- } else {+ } else if (def.compute) {+ // The params block is the effect's uniforms in their listed order, each at the volume's value or the effect's own default.+ stack.addComputePass(effectType, {+ source: def.compute.source,+ params: def.uniforms.map((u) => u.name),+ workgroupSize: def.compute.workgroupSize ?? [8, 8],+ });+ for (const u of def.uniforms) stack.setUniform(effectType, u.name, effectData.uniforms.get(u.name) ?? u.defaultValue);+ } else if (def.factory) {const shader = getOrCreateShader(api, effectType, def.factory);stack.addPass(effectType, shader);for (const [uniformName, uniformValue] of effectData.uniforms) {diff --git a/sdk/src/wasm.ts b/sdk/src/wasm.tsindex 7d5ed48dc..c54b6c923 100644--- a/sdk/src/wasm.ts+++ b/sdk/src/wasm.ts@@ -522,6 +522,8 @@ export interface ESEngineModule {postprocess_shutdown(): void;postprocess_resize(width: number, height: number): void;postprocess_addPass(name: string, shaderHandle: number): number;+ postprocess_addComputePass?(name: string, wgsl: string, params: string, workgroupX: number, workgroupY: number): number;+ postprocess_supportsCompute?(): boolean;postprocess_setPassScale(name: string, scale: number): void;postprocess_setPassContinuesEffect(name: string): void;postprocess_setMsaaSamples(samples: number): void;diff --git a/sdk/tests/compute-effect.test.ts b/sdk/tests/compute-effect.test.tsnew file mode 100644index 000000000..7a52fc2de--- /dev/null+++ b/sdk/tests/compute-effect.test.ts@@ -0,0 +1,98 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+vi.mock('../src/render/material', () => ({+ Material: {+ createShader: vi.fn().mockReturnValue(42),+ compileShader: vi.fn().mockReturnValue(42),+ releaseShader: vi.fn(),+ },+}));++import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';+import {+ PostProcessAPI, PostProcessStack, initPostProcessAPI, shutdownPostProcessAPI, getEffectDef, registerEffect, getAllEffectDefs,+} from '../src/postprocess';+import { syncStackToWasm } from '../src/postprocess/PostProcessAPI';+import type { ESEngineModule } from '../src/wasm';++function mockCore(withCompute: boolean) {+ const mock: Record<string, unknown> = {+ postprocess_init: vi.fn().mockReturnValue(true),+ postprocess_shutdown: vi.fn(),+ postprocess_resize: vi.fn(),+ postprocess_addPass: vi.fn().mockReturnValue(0),+ postprocess_clearPasses: vi.fn(),+ postprocess_setPassScale: vi.fn(),+ postprocess_setPassContinuesEffect: vi.fn(),+ postprocess_setPassTexture: vi.fn(),+ postprocess_setUniformFloat: vi.fn(),+ postprocess_setUniformVec4: vi.fn(),+ postprocess_isInitialized: vi.fn().mockReturnValue(true),+ };+ if (withCompute) mock.postprocess_addComputePass = vi.fn().mockReturnValue(0);+ return mock;+}++describe('compute effects', () => {+ let pp: PostProcessAPI;++ beforeEach(() => {+ pp = new PostProcessAPI();+ });++ afterEach(() => {+ shutdownPostProcessAPI();+ });++ it('registers Sharpen as a compute effect with no draw shader', () => {+ const def = getEffectDef('sharpen');+ expect(def).toBeDefined();+ expect(def!.factory).toBeUndefined();+ expect(def!.compute?.source).toContain('@compute');+ expect(def!.compute!.source).toContain('textureStore');+ // The params block is the uniforms in their listed order, so the shader's struct has to start with them.+ expect(def!.uniforms.map((u) => u.name)).toEqual(['u_intensity', 'u_limit']);+ expect(def!.compute!.source).toMatch(/struct Params \{ intensity : f32, limit : f32/);+ });++ it('keeps every effect either a draw or a compute shader, never neither', () => {+ for (const def of getAllEffectDefs()) {+ const kinds = (def.factory ? 1 : 0) + (def.compute ? 1 : 0) + (def.multiPass ? 1 : 0);+ expect(kinds, `${def.type} must say how it runs`).toBeGreaterThanOrEqual(1);+ }+ });++ it('pushes a compute pass with its source, its param names in order and its workgroup', () => {+ const mock = mockCore(true);+ initPostProcessAPI(mock as unknown as ESEngineModule);+ const stack = new PostProcessStack(pp.state);+ stack.addComputePass('sharpen', { source: 'WGSL', params: ['a', 'b'], workgroupSize: [16, 4] });+ stack.setUniform('sharpen', 'a', 0.5);+ syncStackToWasm(stack, true);+ expect(mock.postprocess_addComputePass).toHaveBeenCalledWith('sharpen', 'WGSL', 'a,b', 16, 4);+ expect(mock.postprocess_addPass).not.toHaveBeenCalled();+ expect(mock.postprocess_setUniformFloat).toHaveBeenCalledWith('sharpen', 'a', 0.5);+ });++ it('leaves a compute pass out on a core that has no compute stage', () => {+ const mock = mockCore(false);+ initPostProcessAPI(mock as unknown as ESEngineModule);+ const stack = new PostProcessStack(pp.state);+ stack.addComputePass('sharpen', { source: 'WGSL', params: [], workgroupSize: [8, 8] });+ stack.addPass('blur', 7);+ expect(() => syncStackToWasm(stack, true)).not.toThrow();+ // The draw pass beside it is untouched, and nothing was asked to compute.+ expect(mock.postprocess_addPass).toHaveBeenCalledWith('blur', 7);+ expect((mock.postprocess_setUniformFloat as ReturnType<typeof vi.fn>)).not.toHaveBeenCalled();+ });++ it('lets a project register its own compute effect', () => {+ registerEffect({+ type: 'testCompute',+ label: 'Test',+ compute: { source: '@compute @workgroup_size(8, 8) fn main() {}', workgroupSize: [8, 8] },+ uniforms: [{ name: 'u_amount', label: 'Amount', min: 0, max: 1, step: 0.1, defaultValue: 0.5 }],+ });+ expect(getEffectDef('testCompute')!.compute!.workgroupSize).toEqual([8, 8]);+ });+});diff --git a/src/esengine/bindings/PostProcessBindings.cpp b/src/esengine/bindings/PostProcessBindings.cppindex d98f4a7d0..51b09df60 100644--- a/src/esengine/bindings/PostProcessBindings.cpp+++ b/src/esengine/bindings/PostProcessBindings.cpp@@ -66,6 +66,25 @@ u32 postprocess_addPass(const std::string& name, u32 shaderHandle) {return g_postProcessPipeline->addPass(name, resource::ShaderHandle(shaderHandle));}+u32 postprocess_addComputePass(const std::string& name, const std::string& wgsl, const std::string& params,+ u32 workgroupX, u32 workgroupY) {+ if (!g_postProcessPipeline) return 0;+ std::vector<std::string> names;+ usize start = 0;+ while (start <= params.size() && !params.empty()) {+ const usize comma = params.find(',', start);+ const usize end = comma == std::string::npos ? params.size() : comma;+ if (end > start) names.push_back(params.substr(start, end - start));+ if (comma == std::string::npos) break;+ start = comma + 1;+ }+ return g_postProcessPipeline->addComputePass(name, wgsl, names, workgroupX, workgroupY);+}++bool postprocess_supportsCompute() {+ return g_postProcessPipeline ? g_postProcessPipeline->supportsCompute() : (g_device && g_device->supportsCompute());+}+void postprocess_setMsaaSamples(u32 samples) {if (g_postProcessPipeline) g_postProcessPipeline->setRequestedSamples(samples);}diff --git a/src/esengine/bindings/PostProcessBindings.hpp b/src/esengine/bindings/PostProcessBindings.hppindex 658e24528..3c9dd65f7 100644--- a/src/esengine/bindings/PostProcessBindings.hpp+++ b/src/esengine/bindings/PostProcessBindings.hpp@@ -12,6 +12,15 @@ bool postprocess_init(u32 width, u32 height);void postprocess_shutdown();void postprocess_resize(u32 width, u32 height);u32 postprocess_addPass(const std::string& name, u32 shaderHandle);+/**+ * @brief Adds a pass that is a WGSL compute shader over the picture (see PostProcessPipeline::addComputePass).+ * @param params The names of the f32 params the shader's block holds, in order, comma separated.+ * @details On a device with no compute stage the pass is left out of the chain and the picture goes through as it was.+ */+u32 postprocess_addComputePass(const std::string& name, const std::string& wgsl, const std::string& params,+ u32 workgroupX, u32 workgroupY);+/** @brief Whether compute passes run on this device: WebGPU yes, WebGL2 no. */+bool postprocess_supportsCompute();/** The project's multisampling request for the scene target; 1 = off. Clamped* by what the device supports — the device answers capability, not policy. */void postprocess_setMsaaSamples(u32 samples);diff --git a/src/esengine/bindings/WebSDKEntry.cpp b/src/esengine/bindings/WebSDKEntry.cppindex 35172e548..11e71d273 100644--- a/src/esengine/bindings/WebSDKEntry.cpp+++ b/src/esengine/bindings/WebSDKEntry.cpp@@ -596,6 +596,8 @@ EMSCRIPTEN_BINDINGS(esengine_renderer) {emscripten::function("postprocess_shutdown", &esengine::postprocess_shutdown);emscripten::function("postprocess_resize", &esengine::postprocess_resize);emscripten::function("postprocess_addPass", &esengine::postprocess_addPass);+ emscripten::function("postprocess_addComputePass", &esengine::postprocess_addComputePass);+ emscripten::function("postprocess_supportsCompute", &esengine::postprocess_supportsCompute);emscripten::function("postprocess_setPassScale", &esengine::postprocess_setPassScale);emscripten::function("postprocess_setPassContinuesEffect", &esengine::postprocess_setPassContinuesEffect);emscripten::function("postprocess_setMsaaSamples", &esengine::postprocess_setMsaaSamples);diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cppindex 0b2b854ae..26797b353 100644--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp@@ -178,6 +178,12 @@ void PostProcessPipeline::shutdown() {for (auto& pass : passes_) releasePassResources(pass);for (auto& pass : screenPasses_) releasePassResources(pass);+ for (auto& [source, program] : compute_cache_) device_.deleteCompute(program);+ compute_cache_.clear();+ if (compute_view_ubo_ != BufferHandle::Invalid) {+ device_.deleteBuffer(compute_view_ubo_);+ compute_view_ubo_ = BufferHandle::Invalid;+ }passes_.clear();screenPasses_.clear();temporal_.clear();@@ -251,6 +257,38 @@ u32 PostProcessPipeline::addPass(const std::string& name, resource::ShaderHandlereturn static_cast<u32>(passes_.size() - 1);}+bool PostProcessPipeline::supportsCompute() const { return device_.supportsCompute(); }++bool PostProcessPipeline::runnable(const PostProcessPass& pass) const {+ return pass.enabled && (!pass.isCompute || (device_.supportsCompute() && pass.compute != ComputeHandle::Invalid));+}++u32 PostProcessPipeline::addComputePass(const std::string& name, const std::string& wgsl,+ const std::vector<std::string>& params, u32 workgroupX, u32 workgroupY) {+ PostProcessPass pass;+ pass.name = name;+ pass.enabled = true;+ pass.isCompute = true;+ pass.computeParams = params;+ pass.workgroupX = std::max(workgroupX, 1u);+ pass.workgroupY = std::max(workgroupY, 1u);+ if (device_.supportsCompute()) {+ auto cached = compute_cache_.find(wgsl);+ if (cached == compute_cache_.end()) {+ std::string log;+ const ComputeHandle program = device_.createCompute(GfxComputeSource{GfxShaderLanguage::WGSL, wgsl.c_str(), "main"}, &log);+ if (program == ComputeHandle::Invalid) {+ ES_LOG_ERROR("PostProcessPipeline: compute pass '{}' did not compile: {}", name, log);+ }+ // A program that failed is remembered as failed, or every camera of every frame would try it again.+ cached = compute_cache_.emplace(wgsl, program).first;+ }+ pass.compute = cached->second;+ }+ passes_.push_back(pass);+ return static_cast<u32>(passes_.size() - 1);+}+/*** The device answers what it CAN do; the project says what it SHOULD do. The* effective count is the second clamped by the first — a backend must never@@ -831,6 +869,52 @@ void PostProcessPipeline::renderPass(PostProcessPass& pass, const rg::PassContexdrawScreenQuad();}+void PostProcessPipeline::computePass(PostProcessPass& pass, const rg::PassContext& ctx) {+ if (pass.compute == ComputeHandle::Invalid || ctx.output == TextureHandle::Invalid) return;+ const TextureHandle input = ctx.input(0);+ if (input == TextureHandle::Invalid) return;++ // The params block: the pass's floats in the order the effect declared them, at least one vec4 (a uniform+ // binding is never smaller than that).+ const usize floats = std::max<usize>(4, (pass.computeParams.size() + 3) / 4 * 4);+ std::vector<f32> values(floats, 0.0f);+ for (usize i = 0; i < pass.computeParams.size(); ++i) {+ if (auto it = pass.floatUniforms.find(pass.computeParams[i]); it != pass.floatUniforms.end()) values[i] = it->second;+ }+ const u32 paramBytes = static_cast<u32>(values.size() * sizeof(f32));+ if (pass.paramUbo == BufferHandle::Invalid) {+ pass.paramUbo = device_.createBuffer({GfxBufferUsage::Uniform, paramBytes, /*dynamic=*/true},+ GfxContent::retained(), values.data());+ } else {+ device_.updateBuffer(pass.paramUbo, 0, values.data(), paramBytes);+ }++ const f32 w = static_cast<f32>(std::max(ctx.width, 1u));+ const f32 h = static_cast<f32>(std::max(ctx.height, 1u));+ const f32 view[4] = {w, h, 1.0f / w, 1.0f / h};+ if (compute_view_ubo_ == BufferHandle::Invalid) {+ compute_view_ubo_ = device_.createBuffer({GfxBufferUsage::Uniform, sizeof(view), /*dynamic=*/true},+ GfxContent::retained(), view);+ } else {+ device_.updateBuffer(compute_view_ubo_, 0, view, sizeof(view));+ }++ // The scene's depth where the frame wrote one; the device stands a blank in where it did not.+ const TextureHandle depth = (graph_ && sceneResource_ != rg::kNoResource) ? graph_->depthTextureOf(sceneResource_)+ : TextureHandle::Invalid;+ const GfxComputeBinding bindings[6] = {+ GfxComputeBinding::sampled(0, input),+ GfxComputeBinding::storageTexture(1, ctx.output),+ GfxComputeBinding::uniform(2, pass.paramUbo),+ GfxComputeBinding::uniform(3, compute_view_ubo_),+ GfxComputeBinding::depth(4, depth),+ GfxComputeBinding::sampler(5, TextureFilter::Linear),+ };+ const u32 groupsX = (ctx.width + pass.workgroupX - 1) / pass.workgroupX;+ const u32 groupsY = (ctx.height + pass.workgroupY - 1) / pass.workgroupY;+ device_.dispatchCompute(pass.compute, bindings, 6, groupsX, groupsY, 1);+}+void PostProcessPipeline::clearPasses() {for (auto& pass : passes_) releasePassResources(pass);passes_.clear();@@ -880,14 +964,14 @@ void PostProcessPipeline::runChain(std::vector<PostProcessPass>& passes, rg::Res// Which enabled pass is last decides which target the blit will sample.usize lastEnabled = passes.size();for (usize i = passes.size(); i-- > 0;) {- if (passes[i].enabled) { lastEnabled = i; break; }+ if (runnable(passes[i])) { lastEnabled = i; break; }}rg::ResourceId input = scene;rg::ResourceId effectInput = scene;for (usize i = 0; i < passes.size(); ++i) {auto& pass = passes[i];- if (!pass.enabled) continue;+ if (!runnable(pass)) continue;if (!pass.continuesEffect) effectInput = input;rg::PassDesc node;node.name = pass.name;@@ -915,6 +999,23 @@ void PostProcessPipeline::runChain(std::vector<PostProcessPass>& passes, rg::Resrg::TargetDesc writeDesc = (i == lastEnabled) ? blitDesc : desc;if (i != lastEnabled) writeDesc.scale = pass.scale;if (passIsHighPrecision(pass)) writeDesc.format = GfxPixelFormat::RGBA16F;+ if (pass.isCompute) {+ // A compute pass writes a storage texture, which is a half float whatever the chain keeps between passes (the+ // shader says its format), and it reads the scene's depth, so the scene has to outlive it.+ writeDesc.format = GfxPixelFormat::RGBA16F;+ writeDesc.storage = true;+ writeDesc.linearFilter = true;+ pass.temporal = false;+ pass.feedback = false;+ node.compute = true;+ node.reads = {input};+ node.dependencies = {scene};+ node.write = graph_->createTarget(writeDesc);+ node.execute = [this, &pass](const rg::PassContext& ctx) { computePass(pass, ctx); };+ input = node.write;+ graph_->addPass(std::move(node));+ continue;+ }// A temporal pass writes the history target itself, so that what it wrote is// still there next frame, and the passes after it read it from there.TemporalState* temporal = nullptr;diff --git a/src/esengine/renderer/frame/PostProcessPipeline.hpp b/src/esengine/renderer/frame/PostProcessPipeline.hppindex 735a40be8..007d721d0 100644--- a/src/esengine/renderer/frame/PostProcessPipeline.hpp+++ b/src/esengine/renderer/frame/PostProcessPipeline.hpp@@ -83,6 +83,14 @@ struct PostProcessPass {/// starts an effect, so a single-pass effect sees the chain as it reached it.bool continuesEffect = false;+ /// A compute pass (see PostProcessPipeline::addComputePass): the program, which floats its params block holds, in+ /// order, and the workgroup the shader declares. Invalid for every pass that draws.+ ComputeHandle compute = ComputeHandle::Invalid;+ bool isCompute = false;+ std::vector<std::string> computeParams;+ u32 workgroupX = 8;+ u32 workgroupY = 8;+/// #pragma-param shaders: the pass's packed MaterialConstants payload +/// its UBO (binding 1) — the same reflected block a material would use./// Rebuilt from float/vec4Uniforms over the layout defaults when dirty.@@ -146,6 +154,21 @@ public:* @return Index of the added pass*/u32 addPass(const std::string& name, resource::ShaderHandle shader);+ /**+ * @brief Adds a pass that is a compute shader over the picture instead of a draw.+ * @details The WGSL is handed these, at group 0: `@binding(0)` the image so far (`texture_2d<f32>`), `@binding(1)`+ * the image it writes (`texture_storage_2d<rgba16float, write>`, the same size), `@binding(2)` a uniform+ * block of @p params as f32s in that order, `@binding(3)` the size as `vec4f(w, h, 1/w, 1/h)`, `@binding(4)`+ * the scene's depth (`texture_depth_2d`), `@binding(5)` a linear clamp sampler. A shader declares the ones it+ * uses. It is dispatched over the whole image in groups of @p workgroupX x @p workgroupY, which it declares.+ * Compute needs a device that supportsCompute(): on one that does not the pass is left out of the chain, and+ * the picture goes through as it was.+ * @return Index of the added pass.+ */+ u32 addComputePass(const std::string& name, const std::string& wgsl, const std::vector<std::string>& params,+ u32 workgroupX, u32 workgroupY);+ /// Whether passes added with addComputePass run on this device.+ bool supportsCompute() const;/// The project's multisampling request; 1 = off. Clamped by the device.void setRequestedSamples(u32 samples);/// What the scene target is actually created with: the request after the@@ -496,6 +519,15 @@ private:void drawScreenQuad();void applyPassPipeline(const Shader& shader);void renderPass(PostProcessPass& pass, const rg::PassContext& ctx);+ /// A compute pass: its params and the size into a block each, the picture, the depth and the output bound, one dispatch over it.+ void computePass(PostProcessPass& pass, const rg::PassContext& ctx);+ /// Whether a pass has anything to run on this device: a drawn pass always, a compute one only where there is compute.+ bool runnable(const PostProcessPass& pass) const;+ /// Compute programs by source, kept while the pipeline lives: the passes are pushed again every camera, and a+ /// program is a compiled pipeline, not something to build per frame.+ std::unordered_map<std::string, ComputeHandle> compute_cache_;+ /// The size block a compute pass reads, one for all of them (each dispatch writes it just before it runs).+ BufferHandle compute_view_ubo_ = BufferHandle::Invalid;/// Declares one chain onto an already-begun graph and runs it, ending in the/// output target. @p scene is the resource the first effect reads.void runChain(std::vector<PostProcessPass>& passes, rg::ResourceId scene);diff --git a/src/esengine/renderer/graph/RenderGraph.cpp b/src/esengine/renderer/graph/RenderGraph.cppindex db3e46f17..14261e3e9 100644--- a/src/esengine/renderer/graph/RenderGraph.cpp+++ b/src/esengine/renderer/graph/RenderGraph.cpp@@ -198,6 +198,8 @@ void RenderGraph::execute() {if (pass.compute) {// A dispatch is not a draw: it runs between render passes, with nothing bound for it by the graph.+ // The pass before it is still open on a device that leaves one open until the next begins; a dispatch needs it closed.+ device_.endRenderPass();inputScratch_.clear();for (const ResourceId id : pass.reads) inputScratch_.push_back(textureOf(id));if (pass.execute) {diff --git a/src/esengine/renderer/rhi/GfxEnums.hpp b/src/esengine/renderer/rhi/GfxEnums.hppindex b75194e5e..432a385c3 100644--- a/src/esengine/renderer/rhi/GfxEnums.hpp+++ b/src/esengine/renderer/rhi/GfxEnums.hpp@@ -648,7 +648,7 @@ struct GfxComputeSource {* format is the texture's, and `Sampler` is a `sampler`.*/struct GfxComputeBinding {- enum class Kind : u8 { Uniform, StorageRead, Storage, Texture, StorageTexture, Sampler };+ enum class Kind : u8 { Uniform, StorageRead, Storage, Texture, StorageTexture, Sampler, DepthTexture };Kind kind = Kind::Uniform;u32 binding = 0;BufferHandle buffer = BufferHandle::Invalid;@@ -674,6 +674,11 @@ struct GfxComputeBinding {static GfxComputeBinding storageTexture(u32 binding, TextureHandle texture) {GfxComputeBinding b; b.kind = Kind::StorageTexture; b.binding = binding; b.texture = texture; return b;}+ /// A depth texture, read with textureLoad as `texture_depth_2d`. An Invalid handle binds a stand-in, so a shader+ /// that declares the binding runs whether or not the frame wrote depth.+ static GfxComputeBinding depth(u32 binding, TextureHandle texture) {+ GfxComputeBinding b; b.kind = Kind::DepthTexture; b.binding = binding; b.texture = texture; return b;+ }static GfxComputeBinding sampler(u32 binding, TextureFilter filter) {GfxComputeBinding b; b.kind = Kind::Sampler; b.binding = binding; b.filter = filter; return b;}diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex bb7ed9eb2..e64880da9 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -2818,6 +2818,18 @@ bool WebGPUDevice::backendDispatchCompute(u32 id, const GfxComputeBinding* bindikey += "." + std::to_string(static_cast<u32>(le.texture.sampleType));break;}+ case GfxComputeBinding::Kind::DepthTexture: {+ auto it = textures_.find(static_cast<u32>(b.texture));+ const TextureRec* rec = (it != textures_.end() && it->second.texture && isDepthFormat(it->second.format)) ? &it->second : nullptr;+ if (!rec) {+ ensureDummies();+ rec = &dummy_depth_texture_;+ }+ le.texture.sampleType = WGPUTextureSampleType_Depth;+ le.texture.viewDimension = WGPUTextureViewDimension_2D;+ ge.textureView = rec->sampleView;+ break;+ }case GfxComputeBinding::Kind::StorageTexture: {auto it = textures_.find(static_cast<u32>(b.texture));if (it == textures_.end() || !it->second.texture) {diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 15e613ecb..af12d5df7 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -2493,6 +2493,8 @@ export const NOTED = {{"note":"Skinned meshes are followed by their bones, from the palette they were last drawn with."},"feat(render): fog follows the scene's sun and can walk the air against its shadow map":{"note":"Height Fog and Light Shafts follow the scene's own sun, and Height Fog can carry the sun's shadows as volumetric light."},+ "feat(render): post-process effects can be compute shaders, and Sharpen is one":+ {"note":"A post-process effect can be a WGSL compute shader over the picture, run on WebGPU and left out on WebGL2, and Sharpen is the first."},"feat(render): the WebGPU device runs compute shaders, and the render graph has compute passes":{"internal":"Renderer plumbing with no creator-facing surface yet: no effect or component asks for compute until the first consumer lands."},"feat(render): the sky scatters light like an atmosphere and carries volumetric clouds the sun lights":