Screen Effects (effect.hlsl)

Screen effects applied to the whole screen (API 3-6) - PackEffect, state, passes, history / mips, focus, the ID image, extended parameters, the effect stack and Studio keys.

What screen effects are

A screen effect ("type": "effect") applies to the entire finished screen. Examples include chromatic aberration, film grain, CRT scanlines, glitch, vignette, lens flare and bokeh depth of field. Effects belong to the screen, not to a model, and run as one entry in the effect stack.

Effect packs are not bundled with the app. Install them from the Online list in the Effects tab or import a zip. The gallery includes examples such as chromatic_aberration, film_grain, crt_scanlines, lens_dirt, lens_reflection, lens_flare, bokeh_dof and screen_ao.

Using effects

  1. Install an effect pack from the Online list in the Effects tab.
  2. In the lobby’s Effects tab, expand the Screen effect row, or press the screen effects button on the playback bar.
  3. Enable the effects you want, reorder them with the arrows and adjust the sliders.

The stack runs top to bottom on the final image. It applies to the playback screen, the lobby preview and real-time video / still renders (raster, real-time RT, real-time PT). It also applies to the offline GI renderer (pre-bloom effects to the lit image, post effects to the result). It does not apply to unlit or wireframe views or the Studio quad view. With no effects enabled there are no extra targets or passes, so the frame is exactly the same as in a build without the feature.

On the command line, --effect <id>[,<id>...] changes the stack for a single run, and --effect none empties it.

Folder layout

my_effect/
  pack.json             manifest (same fields as surface packs + type / stage)
  effect.hlsl           PackEffect implementation
  preview.png           optional, 16:9 recommended
  preview_before.png    optional, the same frame with the effect off (16:9)
  textures/             optional, pack.json "textures" (same rules as surface packs)

If you provide preview.png and preview_before.png together, the app’s effect screen and the gallery let you drag to compare before and after.

Manifest

{
  "format": 1,
  "id": "film_grain",
  "version": "1.0.0",
  "apiVersion": 3,
  "minAppVersion": "1.4.0",
  "type": "effect",
  "stage": "post",
  "name": { "ko": "필름 그레인", "en": "Film grain" },
  "params": [
    { "key": "strength", "label": { "ko": "세기", "en": "Strength" }, "default": 0.12, "min": 0.0, "max": 0.6 }
  ]
}
  • "type": "effect" makes it a screen effect (the default is "surface").
  • "stage" is "post" (default) or "pre-bloom".
  • classes is not used. params (16 by default) and textures (up to 16) behave exactly as in surface packs. If you need more types / groups, use params.ext. See Manifest.
stageInput / outputUse
postDisplay sRGB after tone mapping and the colour LUT, 8-bitGrain, scanlines, lens smearing, vignette
pre-bloomLinear HDR RGBA16F before bloomGlow-friendly or exposure-aware effects

You can mix effects from both stages in one stack. Each stage runs only its own effects, in stack order.

effect.hlsl

A pack implements one function.

float3 PackEffect(PackEffectInput i) { return i.color.rgb; }

PackEffectInput fields:

FieldMeaning
uvTexel centre, 0..1, top left = (0, 0)
pixelSV_Position.xy (pixels)
outputSizeOutput resolution (pixels)
timeSeconds since the scene started (seconds)
frameIndexFrame counter (wraps at 64)
colorThe screen so far at this pixel. pre-bloom is linear HDR; post is display sRGB with alpha
depthRaw device depth, 1 = background (LinearZ() for view z)
normalView-space normal (octahedral encoded, OctDecode). Meaningless direction on the background
motionMotion vector, uv(current) - uv(previous). (0, 0) when there is no motion
dtSeconds since the previous rendered frame

Helpers:

  • gEffectSource.SampleLevel(gLinear, uv, 0) reads any pixel of the screen so far (bilinear, clamp).
  • PackParam(i) is the value of the i-th parameter in pack.json order (or its default).
  • PackFxSampleTex(i, uv), PackFxSampleTexLevel, PackFxTexSize and PackFxTexCount sample pack textures. sRGB textures return linear values.
  • Everything in common.hlsli: Luminance, LinearZ, Ign, SrgbToLinear, gTime, gNearZ / gFarZ and more.

line is an HLSL reserved word.

Example: chromatic aberration

float3 PackEffect(PackEffectInput i) {
    const float2 dir = i.uv - 0.5;
    const float r = length(dir) * 1.41421356;   // 0 at the centre, 1 at the corners
    const float2 offset = dir * (PackParam(0) * 0.03 * pow(r, PackParam(1)));
    float3 c;
    c.r = gEffectSource.SampleLevel(gLinear, i.uv + offset, 0).r;
    c.g = i.color.g;
    c.b = gEffectSource.SampleLevel(gLinear, i.uv - offset, 0).b;
    return c;
}

State (API 4)

Effects can carry up to 16 floats from frame to frame (eye adaptation, smoothly following focus, counters). Declare "state": { "floats": N } together with "apiVersion": 4 and implement PackEffectState next to PackEffect. Read the updated values with PackState(i).

{ "apiVersion": 4, "type": "effect", "state": { "floats": 2 } }
void PackEffectState(PackStateInput i, out float s[16]) {
    for (int k = 0; k < 16; ++k) s[k] = 0;
    float luma = Luminance(gEffectSource.SampleLevel(gLinear, float2(0.5, 0.5), 0).rgb);
    s[0] = i.reset ? luma : lerp(i.prevState[0], luma, 1.0 - exp(-2.0 * i.dt));
}
float3 PackEffect(PackEffectInput i) { return i.color.rgb / max(PackState(0) * 4.0, 0.25); }

PackStateInput contains prevState[16], reset, dt, time, frameIndex and outputSize.

When reset is true, prevState is all zero (a newly added or reordered entry, a pack reload, an entry absent from the previous rendered frame, a camera cut, a size change, the first frame of a render). dt is the frame time during interactive playback (clamped to 1/240 .. 0.1 s), 1/60 for deterministic runs, and fixed to 1/fps for offline video. GI stills run the state 64 times before drawing.

Passes and camera focus (API 5)

A single full-screen function makes wide blurs or finding light sources efficiently hard. With "apiVersion": 5 you can list up to 8 passes that run before PackEffect. Each pass is a float4 name(PackPassInput i) function and draws to an RGBA16F target of output size × scale (1/64 .. 1). Later passes and PackEffect read them with PackPassSample(n, uv), PackPassLoad(n, pixel) and PackPassSize(n).

{
  "apiVersion": 5,
  "type": "effect",
  "stage": "pre-bloom",
  "passes": [
    { "entry": "Bright", "scale": 0.25 },
    { "entry": "Blur",   "scale": 0.0625 }
  ],
  "replaces": ["dof"]
}
float4 Bright(PackPassInput i) { return float4(max(gEffectSource.SampleLevel(gLinear, i.uv, 0).rgb - 1.0, 0.0), 1); }
float4 Blur(PackPassInput i)   { return PackPassSample(0, i.uv); }
float3 PackEffect(PackEffectInput i) { return i.color.rgb + PackPassSample(1, i.uv).rgb; }
  • PackPassInput contains the pass target’s uv, pixel and size, plus outputSize, time, frameIndex, dt and pass. The screen, depth, normal, motion, textures, parameters and state are available in every pass. A pass can only read earlier passes (its own target and later passes are undefined).
  • Every effect can follow the app’s focus: PackFocusZ() (the Studio focus track, the character’s face on the playback screen, otherwise automatic centre focus), PackFocusAperture() (the aperture of the focus key, 0 = sharp), PackDofAperture() / PackDofMaxRadius() (the user’s depth of field settings) and PackCocPx(viewZ, focusZ, aperture, maxRadius) (the app’s lens model, in pixels, negative = in front of focus).
  • "replaces": ["dof"] (pre-bloom packs only) turns off the built-in depth of field and the GI lens while the pack is on, so a bokeh pack can take their place.
  • Packs without passes cost nothing extra.

Pass history and mips (capabilities)

These are declared under requires, independently of the API version.

passes.history

{
  "apiVersion": 5,
  "type": "effect",
  "requires": ["passes.history"],
  "passes": [
    { "entry": "Accumulate", "scale": 1.0, "history": true }
  ]
}
  • A pass with "history": true keeps the previous frame’s result in space9.
  • It is cleared to zero on camera cuts, scene load / reset and resolution changes.
  • Read with float4 PackPassSampleHistory(uint n, float2 uv) and float4 PackPassLoadHistory(uint n, int2 px).
  • Offline stills run the history forward 64 times first, like v4 state, to converge.

passes.mips

{
  "apiVersion": 5,
  "type": "effect",
  "requires": ["passes.mips"],
  "passes": [
    { "entry": "DownsampleBase", "scale": 0.5, "mips": true }
  ]
}
  • Builds a mip chain down to 1×1 after the pass runs.
  • Read with float4 PackPassSampleLevel(uint n, float2 uv, float lod).

The ID image (inputs.id)

Declaring "requires": ["inputs.id"] provides a non-MSAA R16_UINT ID buffer at render resolution as gEffectId (t4, space6). Pixels with alpha < 0.98 and the background are 0.

BitsFieldMeaning
0-3tagThe 4-bit tag written by surface packs via PackResult.tag / PtPackOut.tag (0..15)
4-6classMaterial class (0..5, 7 = none / pack off)
7isCharacter1 for a character, 0 for stage / other geometry
8-15modelModel index + 1 (0 = background / transparent)

Helpers:

  • uint PackSampleId(float2 uv): reads the 16-bit ID at the UV.
  • uint PackIdModel(uint id): 1-based model index (id >> 8) & 0xFF.
  • uint PackIdClass(uint id): material class (id >> 4) & 0x7.
  • uint PackIdTag(uint id): tag id & 0xF.
  • bool PackIdIsCharacter(uint id): bit 7.
float4 Ghost(PackPassInput i) {
    uint id = PackSampleId(i.uv);
    float mask = PackIdIsCharacter(id) && PackIdTag(id) == 3 ? 1.0 : 0.0;
    return float4(gEffectSource.SampleLevel(gLinear, i.uv, 0).rgb * mask, 1);
}

Extended parameters (params.ext)

Declaring "requires": ["params.ext"] lets you organise up to 128 typed parameters into groups. Effects receive them as gPackParamSlots[128] (b4), and the accessors match surface packs:

  • PackParam / PackParamInt / PackParamBool / PackParamColor / PackParamColor4 / PackParamVec3 / PackParamCount.
{
  "requires": ["params.ext"],
  "groups": [ { "id": "look", "label": { "ko": "룩", "en": "Look" } } ],
  "params": [
    { "key": "tint", "type": "color", "group": "look", "alpha": true, "default": "#ffd0a0cc" },
    { "key": "passes", "type": "int", "group": "look", "default": 4, "min": 1, "max": 16, "step": 1 }
  ]
}

See Manifest for all fields and default value formats.

Compiling and debugging

Shaders are compiled at runtime with DXC (ps_6_0). If compilation fails, that effect is skipped, an [E] line is written to mmdx12.log, and a toast plus the Effects tab state tell you. The app does not crash. Saving effect.hlsl while the app is running reloads it.

On the command line, check with pack_check <folder|zip> --compile. See Debugging. If you need a starting point, use shaders/pack_template_effect from the distribution.

Stack and Studio keys

  • The effect stack is saved in the app settings (ini), and in Studio projects it is saved with .mmdxproj.
  • In Studio you can add per-parameter timeline keys on each effect entry. Keys undo, and evaluate to the same values in the viewport, video rendering and GI rendering. Play mode and look presets use static values.
  • color / vec3 parameters are keyed per component, and int / bool evaluate as steps.

Distribution

Screen effects follow the same zip layout, versioning and review rules as surface shaders. See Publishing. Set "minAppVersion": "1.4.0" for "apiVersion": 3 packs, and 1.7.0 or later for passes (v5). For packs using the newest features it is safer to require the corresponding release or later.