Ductane v1.6.0
Contents

Docs / Rendering

Graphics settings

GraphicsSettings holds the rows every 3D game's graphics menu has, and the world renderer applies every one of them to what it draws. A game with its own renderer applies them itself. The defaults are the High preset, the game as made with 4× anti-aliasing, so nothing looks worse unless a player asks. SceneScaling, under the render scale, draws the 3D scene smaller than the window and scales it up.

The settings

SettingValues (default first)What it changes
shadows.high, .low, .offHigh: every sun cascade at 2048 texels a side. Low: 1024, and nothing is drawn into the farthest cascade. Off: no sun shadows
antiAliasing4, or 1, 2samples per pixel in the scene pass (FrameRenderer.sampleCount); 1 is none
renderScale.automatic, .full, .threeQuarters, .halfthe scene's size against the window; the interface is always drawn sharp
frameRate60, or 120, 30what the display link is asked for
textureQuality.full, .half, .quartereach texture side halved reduction times as it is loaded
clouds.high, .medium, .flatvolumetric at half the screen's width and height, at a quarter of its pixels, or a flat layer
fog.high, .medium, .lowvolumetric in cells of 16 pixels and 64 slices, 24 and 48, or distance fog alone
snowDepthtruewhether lying snow raises the ground (Weather.snowDepth) or is only a white layer
reflectionstruewhether water and the like reflect the world, read from the solid scene
drawDistance1, range 0.5...1how far scattered things are drawn, as a share of each kind's own distance
plantDensity1, range 0.25...1the share of thin ground cover drawn: grass, flowers, crops

Low shadows save where the time goes: in a dense wood, the smaller map alone saved one millisecond and leaving the far cascade's casters out saved seven. Texture quality is mostly memory: a 4096² texture held uncompressed with its mips is 85 MB, and 21 MB at half. Images of 128 pixels or less (TextureStore.smallestReduced) always load whole, since a halved icon is a blur. Reflections cost about 2 ms at 1960×1490 with a pond filling the view.

Plant density thins only ground cover. Thinning trees and bushes would change the world, not its cost. GraphicsSettings.keeps(_:atDensity:) says whether a copy is drawn. It hashes the index rather than drawing at random, so the same copies are kept every frame and every run, and lowering the density only removes copies. A game with no plants leaves that row and draw distance out of its menu.

Presets

GraphicsSettings.Preset has .low, .medium and .high; settings gives each one's values. High is the defaults. Medium has low shadows, 2× anti-aliasing, 80% draw distance, 75% ground cover, and medium clouds and fog. Low turns shadows, anti-aliasing, reflections and snow depth off, with half the ground cover, 60% draw distance, the scene at 75%, half-size textures, flat clouds and distance fog.

preset reads settings back as the preset they match, or nil once a player has changed anything: a menu's "Custom". summary is the settings in one line (High, 60 fps, or every value), for a report that has to say what it measured, such as a flythrough.

Applying them with WorldRenderer

The world renderer applies most of them. Start it with the saved settings, so textures load at the right size from the first prop, and set graphicsSettings when a player changes something:

let world = try WorldRenderer(engine: engine, graphics: saved)
world.onFrameRateChange = { displayLink.preferredFrameRateRange = $0.frameRateRange }   // your game owns the link

world.graphicsSettings = GraphicsSettings.Preset.medium.settings
world.graphicsSettings.shadows = .high                  // now a player's own: preset is nil

A change is applied at the start of the next frame, and what it replaces is kept until no frame in flight can be using it.

A game with its own renderer applies the three itself: GraphicsSettings.keeps(_:atDensity:) picks the same copies of a kind at a density, and WorldScene.isGroundCover(_:size:cutOut:) tells ground cover.

Applying them with your own renderer

FrameRenderer.apply(_:automaticScaling:) sets sampleCount, as far as the device supports it, and sceneScaling, with automaticScaling standing for .automatic. Call it in encodePrePasses or before a frame; the targets follow in that frame. Every scene-pass pipeline is built with a rasterSampleCount, so rebuild yours when effectiveSampleCount changes:

let before = engine.effectiveSampleCount
engine.apply(settings, automaticScaling: .capped(width: 1920, beyond: 2600))
if engine.effectiveSampleCount != before { rebuildScenePipelines() }   // your game

A FrameRenderer draws one sample per pixel until you apply settings that ask for four. That is on purpose: every scene pipeline is built for the renderer's count, so it starts where they do, and the settings' 4 is where a graphics menu starts. Shadows are yours: settings.shadows.cascades(splits:lightDepthRange:) makes the ShadowCascades for your splits at the setting's resolution, nil when off, and castingCascades(of:) goes to ShadowMap.encode(into:frameSlot:batches:castingCascades:), which clears the cascades past it so they read as lit. Pass frameRateRange to makeDisplayLink(for:frameRateRange:), whose own default asks for up to 120: that is a game which has said nothing, where the settings start at 60 so a world dressed for 60 is not pushed to 120 unasked. See shadows.

Saving them

GraphicsSettings is Codable, so a SaveStore keeps it as it is:

let graphicsStore = SaveStore<GraphicsSettings>(container: class="p">"MyGame", name: class="p">"graphics")   // your game
let saved = (try? graphicsStore.load()) ?? GraphicsSettings()
try? graphicsStore.save(world.graphicsSettings)

A set saved by an older build still loads: a setting that is missing or unreadable takes its default, and the rest keep what they said. clamped brings stale values back: the two fractions into range, a frame rate not in frameRates to 60, an anti-aliasing count not in antiAliasingChoices to 1. apply and frameRateRange use it, so a hand-edited file can't ask for what the screen never offers.

Scene scaling

A base M2 filling a 4K display draws 8.3 million pixels a frame, and alpha-tested foliage makes each one expensive. FrameRenderer.sceneScaling draws the scene smaller, and the tone-map pass scales it up with linear filtering. The interface is drawn after, at the drawable's size, so it stays sharp.

CaseThe scene is drawn
.nativeat the drawable's size (the default)
.capped(width:beyond:)width pixels across, keeping the drawable's shape, once the drawable is wider than beyond
.fraction(_:)at this fraction of the drawable each way, at any size; 1 or more is full size, and never under one pixel
engine.sceneScaling = .capped(width: 1920, beyond: 2600)

capped takes two numbers on purpose. At 4K with a cap of 1920 the scale is exactly 2×, which looks the same. A window 1,960 wide drawn at 1,920 and stretched is softer than either for nothing, so only a drawable well past the cap is scaled. .fraction is a player's trade instead; the .threeQuarters and .half render scales become it.

Change it whenever; the targets follow on the next frame, and sceneSize says what the scene is drawn at. viewportSizeChanged(width:height:) still gets the drawable's size, not the scene's, so aspect and layout don't move. Size anything that matches the scene pixel for pixel to sceneSize.

What it does not do

There is no auto-detected preset and no settings screen. The scale doesn't follow frame time by itself, and the upscale is linear filtering, not a temporal upscaler.