JA
games/rob-revisited / src / Revisited / Render / Quality.php · commit f9a1307
<?phpnamespace App\Revisited\Render;/** * The graphics quality levels of "Experience - Rob revisited" (docs/reborn.md "Quality"), from the cheapest: * *   ultra   "Très basse": the 3D at the game's view resolution (320 x 240, big pixels, ~20x fewer pixels than a *           window), the cheapest shaders, a short view distance in fog, no grass, few props; *   low     "Basse": window resolution, simple shaders, no grass; *   medium  "Moyenne": window resolution, detail maps, grass; *   high    "Haute": window resolution x 2 supersampling (capped at MAX_PIXELS), everything. * * And the renderer's name (the software ones: Chrome's SwiftShader, Mesa's llvmpipe, Windows' Basic Render). */final class Quality{    /** from the cheapest */    public const LEVELS = ['ultra', 'low', 'medium', 'high'];    public const LABELS = ['auto' => 'Auto', 'ultra' => 'Très basse', 'low' => 'Basse', 'medium' => 'Moyenne', 'high' => 'Haute'];    /** the option's choices, in the order the menu cycles through them */    public const CHOICES = ['auto', 'ultra', 'low', 'medium', 'high'];    /** most 3D pixels drawn per frame with supersampling (high): a 4K window is not drawn at 4 x its size */    public const MAX_PIXELS = 3_000_000;    public static function valid(string $level): bool    {        return in_array($level, self::LEVELS, true);    }    /** The next level down (null: none), up (null: none). */    public static function lower(string $level): ?string    {        $i = array_search($level, self::LEVELS, true);        return is_int($i) && $i > 0 ? self::LEVELS[$i - 1] : null;    }    public static function higher(string $level): ?string    {        $i = array_search($level, self::LEVELS, true);        return is_int($i) && $i < count(self::LEVELS) - 1 ? self::LEVELS[$i + 1] : null;    }    /** Supersampling for a layer of $w x $h window pixels at this level: 2 in high while it stays under MAX_PIXELS. */    public static function supersample(string $level, int $w, int $h): int    {        return $level === 'high' && $w * $h * 4 <= self::MAX_PIXELS ? 2 : 1;    }    /** A renderer that draws on the CPU (no graphics card used). */    public static function isSoftware(string $renderer): bool    {        return $renderer !== '' && preg_match('/swiftshader|llvmpipe|softpipe|software|basic render|gdi generic|swrast/i', $renderer) === 1;    }    /**     * The GL renderer's name, as much as the driver tells: the unmasked renderer (WEBGL_debug_renderer_info,     * 0x9246) when the browser gives it, GL_RENDERER, GL_VENDOR. In a browser the unmasked one needs the extension     * enabled, which the engine does not do: Chrome then only says "WebKit WebGL" (Firefox gives a sanitized real     * name). Call it with a GL context only (WebGl::isAvailable()).     */    public static function renderer(): string    {        if (!function_exists('glGetString')) {            return '';        }        $parts = [];        foreach ([0x9246, 0x9245, 7937, 7936] as $enum) {     // unmasked renderer / vendor, GL_RENDERER, GL_VENDOR            $s = @glGetString($enum);            if (is_string($s) && $s !== '' && !in_array($s, $parts, true)) {                $parts[] = $s;            }        }        if (function_exists('glGetError')) {            for ($i = 0; $i < 8 && glGetError() !== 0; $i++) {                // forget the INVALID_ENUM of an unknown name            }        }        return implode(' | ', $parts);    }}