/*
 * Lightweight frame-time governor for ray-marched effects.
 *
 * Iris/Oculus does not allow a shader option to resize the main render target
 * at runtime.  Noxara therefore keeps the image buffers untouched and adapts
 * only sample budgets.  The square-root response is deliberately gentle so a
 * short frame-time spike cannot collapse the visual quality in one step.
 */
float getNoxaraAdaptiveQuality(float currentFrameTime) {
    float quality = 1.0;

    #ifdef DYNAMIC_RESOLUTION
    float targetFrameTime = 1.0 / max(float(TARGET_FPS), 1.0);
    float frameLoad = max(currentFrameTime, 0.001) / targetFrameTime;
    float configuredMinimum = float(MIN_RENDER_SCALE) * 0.01;
    float configuredMaximum = float(MAX_RENDER_SCALE) * 0.01;
    float minimumQuality = min(configuredMinimum, configuredMaximum);
    float maximumQuality = max(configuredMinimum, configuredMaximum);

    quality = inversesqrt(max(frameLoad, 1.0));
    quality = clamp(quality, minimumQuality, maximumQuality);
    #endif

    return quality;
}

int getNoxaraAdaptiveSampleCount(int baseSamples, int minimumSamples, float currentFrameTime) {
    float quality = getNoxaraAdaptiveQuality(currentFrameTime);
    return max(minimumSamples, int(floor(float(baseSamples) * quality + 0.5)));
}
