Skip to content

Latest commit

 

History

History
181 lines (103 loc) · 16.9 KB

HDR_GAPS.md

File metadata and controls

181 lines (103 loc) · 16.9 KB

HDR Pipeline Gaps and Risks

This document tracks known gaps, risks, and mitigations in the Vulkan HDR rendering pipeline.

6.1 Lightmap Color Space

Issue: BSP lightmaps are 8-bit; q3map2 may output gamma-encoded or linear values.

Current: Used as linear; r_hdr_lightmap_scale only scales intensity.

Mitigation: Added r_lightmap_srgb_decode (0=linear assumed, 1=sRGB→linear). When r_hdr 1/2 and lightmaps are gamma-encoded (e.g. q3map2 -gamma), set to 1 to avoid darkening in bright areas.

How to tell if lightmaps are gamma-encoded: Maps compiled with q3map2 -gamma (or -gamma 2.2 etc.) produce gamma-encoded lightmaps. Many mod workflows use -gamma for a brighter look; if bright areas appear too dark or washed out with HDR on, try r_lightmap_srgb_decode 1. Linear lightmaps (no -gamma) are typical for vanilla Q3A and some modern maps.

6.1b Double-Precision (64-bit) Render Targets

Option: Historical r_hdr 3 requested RGBA64F.

Current: r_hdr 3 aliases to 32-bit HDR (r_hdr 2) with a startup warning. True RGBA64F color output / dvec4 fragment paths are not implemented and are not advertised. Prefer r_hdr 2 explicitly.

6.2 No True 32-bit HDR Lightmaps

Current: 8-bit lightmaps + r_hdr_lightmap_scale. No real HDR range; scale can cause banding.

Future: Support q3map2 HDR lightmaps (3×16-bit or float) if available. Requires BSP format extension and loader changes.

6.3 Pre-exposure Scale

Current: r_pre_exposure_scale cvar (default 1.0) wired to gamma pass. Use for bloom/tonemap pipeline tweaks or future HDR pipeline adjustments.

Typical use: Scale bloom and tonemap input uniformly (e.g. 0.5 to dim highlights, 1.5 to brighten). Useful when tuning AgX/Filmic tonemap or when debug views (r_post_debug) show clipped highlights. Leave at 1.0 for normal use.

6.4 Bloom Knee

Current: applyBloomKnee in gamma pass applies a soft shoulder (smoothstep) to the main scene before tonemap. Bloom extraction uses r_bloom_threshold and r_bloomKnee; knee controls highlight rolloff. Both are applied; verified consistent with extraction intent.

6.5 Luminance Pass Timing

Current: Luminance from frame N used for frame N+1 (one-frame delay). Expected.

Mitigation: Camera cut detection-when view origin jumps >128 units, eye adaptation uses 4× faster blend (0.5) to snap within ~2 frames. Prevents slow adaptation after teleports or level loads.

Death blowout: On camera cut (e.g. death camera jump), target exposure is capped by r_exposure_auto_cap_on_cut (default 0.75) to avoid blowing out when the view suddenly faces a bright sky. Set to 0 to disable the cap.

6.5b Wobble / Underwater-like Distortion

Post-processing effects that can produce a "wobble" or underwater feel (especially during death or cutscenes):

Cvar Default Effect
r_chromaticAberration 0.22 RGB separation on edges; can look like lens distortion. Set to 0 to disable.
r_filmGrain 0.75 Adds noise; with r_filmLook can create soft-light grain. Set to 0 to disable.
r_paniniBarrelDistortion 0.0 Barrel/pincushion warp. Non-zero adds visible distortion.
cg_underwaterFovWarp 0 (OSP) FOV warping when in water. If a mod enables it incorrectly, can cause wobble when not underwater.

If the camera feels like it's underwater when it isn't, try r_chromaticAberration 0 and cg_underwaterFovWarp 0 (if using OSP).

6.5c Death-State and Console Artifacts

Single-pixel artifacts when console is open: Film grain (r_filmGrain, default 0.75) adds per-pixel noise across the screen. Set r_filmGrain 0 to eliminate. Chromatic aberration can also cause fine color fringing.

Streaky/smudged visuals when dead: Volumetric fog and TAA stationary feedback accumulate when the death camera is nearly static (HUD/crosshair stay sharp). r_volumetricFogSkipStatic (default 1) skips volumetrics and disables TAA for the same ~0.5 s near-static window so history cannot smear sky/fog into streaks. Set to 0 to always keep fog/TAA history. If artifacts persist, try r_volumetricFog 0 / r_taa 0 to confirm, or r_volumetricFogForceCameraCut 1 to force an immediate reset (debug).

Quick isolation: r_filmGrain 0 r_volumetricFog 0 r_oit 0 r_ssao 0 r_ssr 0 then vid_restart to narrow down the source.

6.6 HDR Skybox vs Procedural Sky

  • HDR skybox: EXR or Radiance .hdr with r_skyboxHDR_exposure, r_skyboxHDR_intensity
  • Procedural: r_atmosphere, r_atmosphere_scale

Both feed into linear HDR; no conflict.

6.7 Dynamic Light Intensity

Current: All GPU light consumers (projector, Forward+, deferred, volumetric) share R_DynamicLightColor() in tr_light.c.

Cvar Default Role
r_dynamicLightScale 1 Global multiplier (0.25–4); use with HDR + r_exposure instead of r_gamma.
r_lightGammaLink 1 When 0, drop pow(r_intensity,r_gamma); use 2*r_intensity (+ scale) only.

Legacy path (no hardware gamma, no FBO): 2 * pow(r_intensity, r_gamma) * r_dynamicLightScale.

HDR FBO path: Raw dl->color unless r_dynamicLightScale ≠ 1 or r_lightGammaLink 0 (then 2*r_intensity applies).

Forward+ fix (2026): Tile SSBO packing now uses the same scale as projector dlights (was raw dl->color on legacy paths).

Tuning: Prefer r_exposure / r_exposure_auto for scene brightness; r_dynamicLightScale for muzzle flashes and explosions; r_lightGammaLink 0 when r_gamma should affect display only.

6.9 Color pipeline tiers (gamma pass)

World resolve (menus/UI skip via paniniPad1): exposure (r_exposure / auto), pre-exposure (r_pre_exposure_scale), bloom knee, tonemap (r_tonemap), then display encode (r_gamma via apply_srgb_gamma when the swapchain is UNORM).

Tonemap + grade: Filmic (r_tonemap 3) and AgX (r_tonemap 4) both read toneMapParams0/1 (toe / shoulder / whitePoint / highlightDesat from r_grade_*). AgX is no longer a fixed curve.

Grading stack (r_post 1): local exposure, white balance, LUT, contrast/saturation, motion blur, DOF, chromatic aberration, vignette, film grain, outline — all gated separately from resolve so r_post 0 still tonemaps HDR instead of hard-clipping to 1.0.

Swapchain: apply_srgb_gamma is 0 for *_SRGB present formats (hardware encodes); linear UNORM gets shader gamma from r_gamma. Hardware gamma tables are bypassed when vk.fboActive (s_gammatable_linear).

Lightmaps: r_lightmap_srgb_decode 1 decodes gamma-encoded BSP lightmaps in gen_frag when r_hdr is on. Albedo PBR paths use VK_FORMAT_R8G8B8A8_SRGB via vk_create_pbr_albedo_srgb.

Known legacy coupling: VK_SetLightParams only applies r_intensity/r_gamma scaling when hardware gamma is off and FBO is off; HDR FBO uses raw dl->color.

6.8 Presentation AA / Temporal Reconstruction (r_aaMode, r_taa)

Shipping default: SMAA 1x via r_aaMode 2 (r_ext_smaa 1, r_taa 0) in engine defaults and modern_vulkan.cfg. Temporal Reconstruction is opt-in (r_aaMode 4/5 or r_taa 1).

Policy: vk_aa_policy.c maps r_aaMode 0–6 onto SMAA / FXAA / Present-Time Adaptive Reconstruction / Temporal Reconstruction / supersample. Mode 3 is current-frame-first adaptive recon (migrated from SMAA T2x). Mode 5 defers SMAA until after temporal resolve (cleanup). Mode 6 is spatial supersample reference. Certified zero-history remains mode 2.

Temporal Reconstruction (r_taa): confidence-guided resolve after the post-fog source, before luminance and gamma. Uses vk_temporal reset policy (resize, map load, camera cut, missing prev matrices). Skips portals / near-static streak guard. Partial unreliable motion softens history weight instead of killing the whole pass.

Deferred × TAA anti-ghost (mode 1/3 with r_deferredLighting 1): deferred GGX specular soft-caps relative to diffuse before writing lit HDR; host TAA history weight is reduced when deferred lighting (and further when r_deferredSpecular 1) is active; TAA reactive prefers current on bright highlight trails and tightens YCoCg clamp on high-luma pixels. Stable mode 2 (r_deferredLighting 0, r_taa 0) is unaffected. Deferred overlay keeps TAA off by default and pins r_temporalWeaponAfterTaa / variance-clip / disocclusion for opt-in stacks.

Echoing glow / silhouette trails: Heuristic reactive (near-weapon depth, luma flash, history-bleed when former bright pixels overpaint dark geo) always runs whenever TAA is on — it is no longer gated behind r_temporalReactiveMask (that cvar only enables the stamped OIT/transparent mask). Temporal recon overlay uses r_aaMode 4 (no post-TAA SMAA cleanup) with a lower history weight to avoid white skyline / HOST-banner ghosts.

Weapon ownership (r_temporalWeaponAfterTaa 1, default): RDF_NOWORLDMODEL weapon/view-model draws are deferred until after world Temporal Reconstruction (RB_TryDeferWeaponDrawSurfs / RB_FlushDeferredWeaponAfterTaa). Weapon pixels never enter taa_history, which was the primary cause of dark offset silhouette trails when looking around with TAA on. With the cvar at 0, weapons fall back into the world HDR buffer before TAA (legacy / debug).

History: Per-pixel confidence (not one fixed global weight) from depth disocclusion (r_temporalDisocclusion), velocity/MV validity, luminance delta, and reactive path: heuristics (near depth, motion, luma flash, highlight ghost, history-bleed) always run with TAA and are max’d with an optional stamped full-res R8 mask when r_temporalReactiveMask 1 (vk.reactive_mask_*, TAA set 5). High reactive hard-rejects history (reactive > 0.82). Cap via r_temporalHistoryWeight (default 0.72; temporal overlay 0.68; further lowered when deferred lighting/specular is active).

OIT vs TAA order: OIT accumulates and resolves into current HDR scene color before Temporal Reconstruction; raw OIT buffers are never temporally blended. Resolve uses texelFetch + NEAREST and an explicit COLOR→SHADER_READ full-framebuffer barrier after accum (avoids BY_REGION / same-layout races that appear as horizontal scanline tears). Reactive reveal stamping runs after resolve. Stochastic alpha mode 2 falls back to mode 1 when TAA is off.

Prev matrices: Shared vk_prev_* view/projection are committed only in vk_temporal_commit_frame_state at frame end, from world matrices snapshotted by vk_temporal_capture_world_viewparms when doneWorldScene is set (weapon/HUD RDF_NOWORLDMODEL must not replace the commit source). Volumetric fog must not overwrite them mid-post (that collapsed TAA matrix reprojection to identity for pixels without motion vectors).

OIT accum stability: WBOIT clamps alpha to [0,0.999] (same as MBOIT) and soft-caps Forward+ lit luminance before weighting; depth weight is the McGuire/Bavoil curve adapted for reversed-Z and clamped to [1e-2, 3e3] (prevents fp16 underflow stipple). Resolve soft-caps c_avg and skips composite when coverage < 1e-4.

Debug: r_debugMotionVectors (1); r_debugHistoryRejection 0–8 (MV / reject reasons / reactive / confidence / disocclusion / history UV / near-weapon / world-vs-reactive). SMAA-only (r_aaMode 2, r_taa 0) never allocates the reactive mask.

Motion: r_taaMotionVectors 1 (default) samples the main-pass motion attachment. 0 uses depth + prevViewProj reprojection only.

Tuning: r_taa_feedbackStationary / r_taa_feedbackMotion / r_taa_sharpen. Use r_temporalDebug 1 to log reset reasons.

Hybrid1 / path-trace: Hybrid1 keeps separate SVGF channel histories and must not bind world taa_history as an RT denoiser. r_hybrid1_taa is a legacy hint (default 0); use r_aaMode for presentation AA. Path-trace reference: r_aaMode 0 + progressive accumulate (no world temporal over converged PT).


7. Render Order

  1. Main pass → scene to vk.color_image (RGBA16F or RGBA32F when r_hdr 1 or 2)
  2. OIT (if r_oit): opaque copy to fog_scene, OIT accum for transparents, resolve to color_image. Runs during draw; output stays in HDR.
  3. Copy scene → vk.fog_scene_image (for volumetric composite)
  4. Atmosphere pass → additive sky where depth == far (only when tr.world and not RDF_NOWORLDMODEL; skipped for menus, videos)
  5. Directional Ambient Visibility (deferred opaque path, r_ambientVisibilityMode 2–5): GTAO/RTAO/Hybrid/Reference production and temporal/filter passes run after G-buffer capture and before deferred dynamic lights. It attenuates the existing indirect/static-lit color and is sampled by supported GI composites; it does not darken later direct lights, emissive/transmission classes, Hybrid1 traced channels, or full path tracing. Mode 1 retains legacy post SSAO below.
  6. SSR (if r_ssr): screen-space reflections; reads color + depth, writes back to color_image. Before bloom. The Vulkan SSR subpass is created only when SSR is enabled (vk_update_post_process_pipelines); toggling r_ssr (or bloom/SSAO/SMAA/OIT, HDR color format, or certain bloom cvars) schedules a post-pipeline rebuild at the next frame start (PostFX_PostPipelinesNeedUpdate in vk_postfx.c, refresh in vk_post_process_refresh.c). Quality tuning cvars do not require that rebuild.
  7. Bloom extraction → from color_image to bloom chain (threshold + knee). Bloom blend → additive blend of blurred bloom back to color_image. Both before SSAO and luminance.
  8. Legacy SSAO (mode 1, if r_ssao): copy color_image to fog_scene, samples depth, blur, combine with scene into fog_scene. When SSAO is on, fog_scene becomes the post-fog source.
  9. Volumetric compute + composite → fog over scene (when enabled). Reads and writes fog_scene.
  10. SMAA / FXAA (if enabled and not deferred cleanup): edge detect / resolve. Runs after volumetrics (or after 2D overlays if volumetrics skipped). Output is the post-fog source.
  11. Temporal / Adaptive Reconstruction (if r_taa 1 / r_aaMode 3–5): confidence resolve on post-fog source; history ping-pong in taa_history images. Mode 3 (Present-Time Adaptive) uses strict history caps + current-frame spatial fallback on reject — no full-frame SMAA over the resolve. Mode 5 may run light SMAA cleanup. When r_temporalWeaponAfterTaa 1, deferred weapon/view-model draws flush after this step into color_image (separate temporal ownership).
  12. Luminance pass (if r_exposure_auto): compute pass on post-fog source (after temporal when enabled); result used next frame for eye adaptation.
  13. Gamma pass → tonemap, exposure, gamma → swapchain.

OIT / SSAO / SSR and HDR: All operate in HDR space. OIT resolve outputs to color_image; SSAO combine writes to fog_scene; SSR modifies color_image. Luminance and gamma consume the final post-fog source (SMAA output or fog_scene/color_image when SMAA off).


8. HDR UI

Current: When RDF_NOWORLDMODEL or no world (menus, player config, videos), the gamma pass sets paniniPad1 = 1.0, which disables the HDR film pipeline (noWorldLdr). UI is composited after tonemap — in-game HUD/menu StretchPics use the UI overlay target and are alpha-blended onto the swapchain after gamma when uiOverlayContentValid is set (survives FinishBloom / bloom leaving the overlay recording pass). Main-menu / no-world UI draws into color_image via the post_bloom fallback and is tonemapped with forced brightness/exposure 1.0. Brightness and exposure are forced to 1.0 for these frames to avoid darkening or blowing out UI. No HDR-specific scaling or correction for UI; it is drawn in the same LDR output space as the gamma pass.


9. Screen-Space Effects: SSAO, SSR, Volumetric Fog

9.1 SSAO Edge Halos

Issue: Normals are derived from depth gradients (dFdx/dFdy). At depth discontinuities (object silhouettes, horizon), gradients are unreliable and produce dark/light halos.

Mitigation: r_ssaoMaxDepthGradient (default 0.08). Pixels where neighbor depth differs more than this threshold output full AO (1.0) instead of computing from bad normals. Set to 0 to disable. Lower = stricter edge rejection.

9.2 SSR Depth Discontinuities

Issue: Same depth-derived normal problem. Thin black/white lines can appear at object edges.

Mitigation: r_ssr_maxDepthGradient (default 0.08). SSR skips pixels at depth edges. Lower = stricter; may reduce reflection coverage near silhouettes.

9.3 Volumetric Fog Artifacts

Sources:

  • Transparent/alpha-tested: Fog compositing assumes opaque depth; foliage, fences, grates can show incorrect fog density at alpha edges.
  • Temporal reprojection: When view is nearly static (e.g. death cam), accumulated history can drift. Periodic camera cut (vk_near_static_view_frames ~90) resets history.
  • Motion vectors: Animated entities without previous-frame skinning output zero velocity; entities with customShader (vertex deformation) also use zero motion to avoid ghosting.