PT / GI Packs

pt_surface.hlsl - applying a pack in path tracing and offline GI. PackEvaluate, PtPackIn / PtPackOut, PT_PACK_HAS_LIGHTING lighting separation, extended parameters, textures and limitations.

What a PT / GI pack is

A surface shader pack (surface.hlsl) shades raster and ray-traced camera views with PackShade. The path tracer (--render pt, the PT video renderer) and the offline GI renderer (GI stills and video) trace all rays inside one compute shader, so PackShade cannot run there. A pack takes part through a second file, pt_surface.hlsl, which describes the material at hit points. Light transport such as sun shadows, indirect light, the irradiance cache and the denoiser stays with the renderer.

Packs without that file use the default shading in PT and GI. Real-time raster and RT use only surface.hlsl.

Files

my_pack/
  pack.json        manifest
  surface.hlsl     raster / real-time RT       (PackShade)
  pt_surface.hlsl  path tracing / offline GI    (PackEvaluate)
  shared.hlsli     optional: maths shared by both files

Both files can #include "shared.hlsli" from the pack folder, so the look is written only once. There is no need to raise apiVersion in pack.json; new features are declared with requires.

PackEvaluate

PtPackOut PackEvaluate(PtPackIn i);

PtPackIn

FieldMeaning
pos, normal, V, uvWorld position, shading normal, camera direction, texture coordinates
uv1, uv2 (float4)PMX extra UV1 / UV2 (0 for models without them, detected with PT_PACK_API_ADDUV)
L, sunVisSun direction, sun visibility 0..1 from the traced shadow ray (0 for diffuse bounces)
baseColorLinear texture colour × material colour, unlit
materialClassPACK_BODY / SKIN / FACE / EYE / HAIR / WEAPON (disabled materials never reach the pack)
params[16]Slider values in pack.json order
headPos, headScale, headRight, headUp, headForward, headValidThe same head-bone frame as PackSurface

PtPackOut

FieldMeaning
albedoLinear surface colour. What diffuse bounces and reflections see
shadowTintLinear multiplier applied on the shadow side
shadowBiasShifts the terminator in N.L units (smoothed face normal: dot(Nsmooth, L) - dot(normal, L))
specularAdded linear radiance (rim, gloss, matcap). Multiplied by sunVis
terminatorThe N.L endpoints (lo, hi) of the boundary smoothstep. hi <= lo (e.g. (0, 0)) uses the engine default (-0.12, 0.22)
flatFaceUse the engine’s flat face treatment (no indirect light gradient on this surface)

Declaring surface.output adds:

FieldMeaning
normalCustom shading normal used for later ray bounces and reflections
emissionLinear emission added to ray throughput
tag0..15, written to bits 0-3 of the ID image for primary ray hits

Assign every field. Unassigned fields are undefined, and pack_check warns about them.

PtPackOut PackEvaluate(PtPackIn i) {
    PtPackOut o;
    o.albedo = i.baseColor;
    o.shadowTint = float3(1, 1, 1);
    o.shadowBias = 0.0;
    o.terminator = float2(-0.12, 0.22);
    o.specular = 0;
    o.flatFace = false;
    return o;
}

Separating lighting: PT_PACK_HAS_LIGHTING

flatFace ties two things together: flattening indirect light and using the material’s own boundary. It also offers no way to soften the traced sun shadow. API 1.9.1 and later can separate these with PackEvaluateLighting.

#ifdef PT_PACK_API_LIGHTING
#define PT_PACK_HAS_LIGHTING 1
#endif
...
#ifdef PT_PACK_HAS_LIGHTING
PtPackLighting PackEvaluateLighting(PtPackIn i, PtPackOut o) {
    PtPackLighting l;
    l.flatIndirect = 1.0;   // flatten indirect light, the pack owns the boundary
    l.sunShadow = 0.0;      // this surface ignores traced shadow
    l.lit = -1.0;           // use terminator / shadowBias
    return l;
}
#endif

PtPackLighting fields:

  • flatIndirect (0..1): above 0.5, indirect light is gathered flat relative to the view direction (surrounding geometry ignored). The pack keeps its own terminator / shadowBias. Without this field, flatFace = true does the same job.
  • sunShadow (0..1): how much traced sun visibility (sunVis) affects the direct-light boundary and specular. 0 ignores this surface’s shadow, 1 is the previous behaviour.
  • lit (0..1, -1 = unused): the direct-light brightness (SDF / ramp result). At 0 or above it replaces the terminator / shadowBias combination. With sunShadow = 1 it matches the old term = smoothstep(edge, N.L + shadowBias) * sunVis, and the pack can avoid the bias round trip.

Packs that do not define PT_PACK_HAS_LIGHTING keep the previous behaviour (flatIndirect follows flatFace, sunShadow = 1, no lit). A file that defines the macro must assign every field.

How the renderer uses this

  • Camera / specular chain: lerp(albedo * shadowTint, albedo, term * sunVis) * sunIntensity + specular * sunVis, where term is the smoothstep of terminator shifted by shadowBias. With PT_PACK_HAS_LIGHTING, lit replaces terminator / shadowBias, sunShadow scales sunVis, and flatIndirect flattens only indirect light while keeping the pack’s boundary.
  • Diffuse bounces (light gathered by other surfaces): uses only albedo. Toon terms do not run for gather rays.
  • Do not add an ambient term. The path tracer traces real indirect light.
  • A half-Lambert ramp threshold ± softness maps to terminator = (2 * (threshold - softness) - 1, 2 * (threshold + softness) - 1).
  • Hits with Ray-MMD PBR records still let an active pack (materialClass != PACK_CLASS_OFF) win.
  • The irradiance cache warm-up pass keeps the default albedo.

Textures and slot helpers

Textures declared in pack.json are available (explicit LOD only, no implicit derivatives):

float4 PtPackSampleTex(uint i, float2 uv);                 // level 0
float4 PtPackSampleTexLevel(uint i, float2 uv, float lod);
uint   PtPackTexCount();
float2 PtPackTexSize(uint i);

Out-of-range indices or missing files are (1, 1, 1, 1) / size (1, 1). sRGB textures sample as linear values.

API v6 Sekai slots (mesh UVs, LOD 0, wrap, morphs applied):

bool   PtPackHasSphereTex();                 // a real sphere slot texture exists
float4 PtPackSampleSphereUv(float2 uv);      // _H parameter mask
bool   PtPackHasToonMap();                   // the toon slot is a UV-mapped _S
float4 PtPackSampleToonUv(float2 uv);        // _S shadow colour

Detect uv1 / uv2 and the helpers above with PT_PACK_API_ADDUV.

Extended parameters (params.ext)

Declaring "requires": ["params.ext"] makes the 128 extended parameters available in PT / GI too. Shaders receive them via a per-geometry offset into gPackExtSlots (t3, space1), and PACK_CAP_PARAMS_EXT is defined.

float PtPackParam(uint i);          // slot i.x
int   PtPackParamInt(uint i);
bool  PtPackParamBool(uint i);
float3 PtPackParamColor(uint i);    // linear
float4 PtPackParamColor4(uint i);
float3 PtPackParamVec3(uint i);
uint  PtPackParamCount();

The aliases PackParam / PackParamInt / PackParamBool / PackParamColor / PackParamColor4 / PackParamVec3 / PackParamCount return the same values. Without params.ext, use i.params[16].

#ifdef PACK_CAP_PARAMS_EXT
float rim = PtPackParam(0);
#else
float rim = i.params[0];
#endif

Limitations

  • Characters only. Stages and props do not receive packs.
  • If several different PT packs appear in one scene, the first is used (a warning is logged).
  • Offline GI outlines: packs using PACK_HAS_EDGE + PackEdge draw their outlines in GI too. Packs without them use the app’s default outlines.
  • The denoiser treats the pack’s direct light like the default toon lighting. In still tests hard step() highlight boundaries remained crisp, but fast motion was not checked.
  • Avoid loops with an unbounded end in PackEvaluate. If the GPU hangs, the app terminates.
  • Check with pack_check <folder> --compile. It compiles the raster, offline GI and path tracer variants. The template is shaders/pack_template/pt_surface.hlsl.