Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ document names.

| variable | default | what it changes |
|---|---|---|
| `LEONIDA_DEVICE_TYPE` | `7` | The `XSystemDeviceType` that `XSystemGetDeviceType` returns: `0` Unknown, `1` Pc, `2` XboxOne, `3` XboxOneS, `4` XboxOneX, `5` XboxOneXDevkit, `6` XboxScarlettLockhart (Series S), `7` XboxScarlettAnaconda (Series X), `8` XboxScarlettDevkit. A number outside 0..8 is ignored; text that is not a number counts as `0`. `6` also makes `XSystemGetAnalyticsInfo` report the form `Xbox Series S` instead of `Xbox Series X`. |
| `LEONIDA_DEVICE_TYPE` | `6` under 10 GiB of VRAM, else `7` | The `XSystemDeviceType` that `XSystemGetDeviceType` returns. Unset, it is Series S (`6`) on a GPU with less than Series X's 10 GiB of GPU-optimal memory and Series X (`7`) otherwise or when the VRAM can't be read (`runtime/src/device_profile.h`; the GPU's DXGI `DedicatedVideoMemory`, with `LEONIDA_VK_DEVICE` picking among several), and the runtime logs which and why. Values: `0` Unknown, `1` Pc, `2` XboxOne, `3` XboxOneS, `4` XboxOneX, `5` XboxOneXDevkit, `6` XboxScarlettLockhart (Series S), `7` XboxScarlettAnaconda (Series X), `8` XboxScarlettDevkit. A number outside 0..8 is ignored; text that is not a number counts as `0`. `6` also makes `XSystemGetAnalyticsInfo` report the form `Xbox Series S` instead of `Xbox Series X`. |
| `LEONIDA_ANALYTICS_FORM` | unset | The `form` string of `XSystemGetAnalyticsInfo`, in place of the one the device type gives (up to 63 characters). |
| `LEONIDA_PACKAGE_ID` | `LeonidaPackageIdentifier00000000` | The identifier `XPackageGetCurrentProcessPackageIdentifier` returns (up to 32 characters). It is also the one identifier `XPackageMount` refuses with `E_ACCESSDENIED`, as the game's own package. |
| `LEONIDA_CONTENT_PACKAGES` | unset | The content packages `XPackageMount` can mount: `id1=path1;id2=path2` (up to 1,023 characters). The mount path of a listed identifier is its path, as written; any other identifier is `ERROR_NOT_FOUND`, and a path longer than 259 characters is `ERROR_INSUFFICIENT_BUFFER`. |
Expand Down
53 changes: 50 additions & 3 deletions runtime/shims/xgameruntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// "xgameruntime.dll" is the name the PC GDK uses. If the executable's import table names another module, register
// the same functions under that name too; nothing else changes.
#include <windows.h>
#include <dxgi.h>

#include <algorithm>
#include <atomic>
Expand All @@ -33,6 +34,8 @@

#include "file_system.h"
#include "shims.h"
#include "../src/device_profile.h"
#include "../src/vulkan_device_filter.h"

namespace rt::shims {
bool xgameruntime_user_query(const GUID& class_id, REFIID interface_id, void** out, HRESULT* hr);
Expand Down Expand Up @@ -546,16 +549,60 @@ enum class XSystemDeviceType : uint32_t {
XboxScarlettLockhart = 6, XboxScarlettAnaconda = 7, XboxScarlettDevkit = 8,
};

// XSystemDeviceType XSystemGetDeviceType(). The package is the Xbox Series build, so report Series X (Anaconda).
// LEONIDA_DEVICE_TYPE=<0..8> overrides it, for trying other code paths on day one.
// The dedicated video memory of the GPU the runtime draws with, in bytes; 0 when it can't be read. Read once, through
// DXGI, which needs no Vulkan headers here and can answer before the title creates its device: DedicatedVideoMemory
// is the GPU's largest DEVICE_LOCAL heap, the value the layer's own IDXGIAdapter (dxgi_com.cpp) reports. The GPU is
// the runtime's: LEONIDA_VK_DEVICE filters by name, software and Microsoft adapters are left out (vulkan_device_rank
// puts them last), and the one with the most dedicated memory wins, the discrete GPU on a machine with an iGPU too.
std::uint64_t runtime_gpu_vram() {
static const std::uint64_t bytes = [] {
using CreateFactory = HRESULT(WINAPI*)(REFIID, void**);
const HMODULE dxgi = LoadLibraryW(L"dxgi.dll");
const auto create = dxgi ? reinterpret_cast<CreateFactory>(
reinterpret_cast<void*>(GetProcAddress(dxgi, "CreateDXGIFactory1")))
: nullptr;
IDXGIFactory1* factory = nullptr;
if (!create || FAILED(create(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory))))
return std::uint64_t(0);
std::uint64_t best = 0;
IDXGIAdapter1* adapter = nullptr;
for (UINT i = 0; factory->EnumAdapters1(i, &adapter) == S_OK; ++i) {
DXGI_ADAPTER_DESC1 desc{};
if (SUCCEEDED(adapter->GetDesc1(&desc)) && !(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) &&
desc.VendorId != leonida::microsoft_vulkan_vendor_id) {
char name[128 * 4] = {};
WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, name, int(sizeof(name)), nullptr, nullptr);
if (leonida::vulkan_device_wanted(name))
best = std::max<std::uint64_t>(best, desc.DedicatedVideoMemory);
}
adapter->Release();
}
factory->Release();
return best;
}();
return bytes;
}

// XSystemDeviceType XSystemGetDeviceType(). The package is the Xbox Series build, one executable for both consoles:
// Series S (Lockhart) on a GPU with less than Series X's 10 GB of GPU-optimal memory, Series X (Anaconda) otherwise
// or when the VRAM can't be read (device_profile.h). LEONIDA_DEVICE_TYPE=<0..8> overrides it.
uint32_t __stdcall XSystemGetDeviceType() {
char s[16];
const DWORD n = GetEnvironmentVariableA("LEONIDA_DEVICE_TYPE", s, sizeof(s));
if (n > 0 && n < sizeof(s)) {
int v = std::atoi(s);
if (v >= 0 && v <= 8) return static_cast<uint32_t>(v);
}
return static_cast<uint32_t>(XSystemDeviceType::XboxScarlettAnaconda);
const std::uint64_t vram = runtime_gpu_vram();
const std::uint32_t type = leonida::default_device_type(vram);
static std::atomic<bool> logged{false};
if (!logged.exchange(true, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] xgameruntime: reporting Xbox Series %s: the GPU has %llu MiB of VRAM%s "
"(LEONIDA_DEVICE_TYPE=6 or 7 picks Series S or X)\n",
type == leonida::device_type_lockhart ? "S" : "X",
static_cast<unsigned long long>(vram >> 20),
!vram ? " (unknown)" : type == leonida::device_type_lockhart ? ", less than 10 GiB" : "");
return type;
}

// HRESULT XSystemGetConsoleId(size_t consoleIdSize, char* consoleId, size_t* consoleIdUsed).
Expand Down
28 changes: 28 additions & 0 deletions runtime/src/device_profile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

// Which console the runtime says it is, when LEONIDA_DEVICE_TYPE doesn't name one (shims/xgameruntime.cpp).
//
// The package is the Xbox Series build, one executable for both consoles, so the title reads the console type at run
// time to pick its profile. Series X (Anaconda) has 10 GB of GPU-optimal memory; Series S (Lockhart) is the profile
// a GPU with less VRAM can hold, the 8 GB cards among them (RX 5700 XT, RTX 3060 Ti, RTX 3070). With the GPU's VRAM
// unknown (0), the answer stays Series X, as before.
//
// No Windows, Vulkan or third-party headers.

#include <cstdint>

namespace leonida {

// XSystemDeviceType values (the order of the public documentation).
inline constexpr std::uint32_t device_type_lockhart = 6; // Xbox Series S
inline constexpr std::uint32_t device_type_anaconda = 7; // Xbox Series X

// Series X's GPU-optimal memory: a GPU with less VRAM gets the Series S profile.
inline constexpr std::uint64_t series_x_gpu_memory = std::uint64_t(10) << 30;

// The device type for a GPU with `vram` bytes of dedicated video memory (0: unknown).
inline std::uint32_t default_device_type(std::uint64_t vram) {
return vram && vram < series_x_gpu_memory ? device_type_lockhart : device_type_anaconda;
}

} // namespace leonida
4 changes: 3 additions & 1 deletion runtime/test/fixtures/tiny_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ int main(int argc, char** argv) {
check(sum == 500500, "a second thread ran and joined");

check(XGameRuntimeInitialize() == S_OK, "XGameRuntimeInitialize is answered by the gta6rt shim (S_OK)");
check(XSystemGetDeviceType() == 7, "XSystemGetDeviceType is answered by the gta6rt shim (Series X)");
// Series S (6) or X (7), by the GPU's VRAM; the stub DLL a host would load answers 0.
const uint32_t device_type = XSystemGetDeviceType();
check(device_type == 6 || device_type == 7, "XSystemGetDeviceType is answered by the gta6rt shim (Series S or X)");
XGameRuntimeUninitialize();

if (has_arg(argc, argv, "tls")) {
Expand Down
27 changes: 25 additions & 2 deletions runtime/test/xgameruntime_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include "file_system.h"
#include "shims.h"
#include "../src/device_profile.h"

#include <tuple>

namespace {

Expand Down Expand Up @@ -730,7 +733,18 @@ int main() {
check(init() == S_OK, "XGameRuntimeInitialize returns S_OK");
check(init() == S_OK, "a second XGameRuntimeInitialize also returns S_OK (reference counted)");

check(get<DeviceTypeFn>("XSystemGetDeviceType")() == 7, "XSystemGetDeviceType reports XboxScarlettAnaconda (7)");
// The console type: Series S under 10 GiB of VRAM, Series X from it or when it's unknown (device_profile.h).
check(leonida::default_device_type(std::uint64_t(8) << 30) == 6 &&
leonida::default_device_type(std::uint64_t(4) << 30) == 6,
"a GPU with 4 or 8 GiB of VRAM gets XboxScarlettLockhart (6)");
check(leonida::default_device_type(std::uint64_t(10) << 30) == 7 &&
leonida::default_device_type(std::uint64_t(16) << 30) == 7,
"a GPU with 10 or 16 GiB of VRAM gets XboxScarlettAnaconda (7)");
check(leonida::default_device_type(0) == 7, "unknown VRAM keeps XboxScarlettAnaconda (7)");
SetEnvironmentVariableA("LEONIDA_DEVICE_TYPE", nullptr);
const uint32_t device_type = get<DeviceTypeFn>("XSystemGetDeviceType")();
std::printf("XSystemGetDeviceType on this machine's GPU: %u\n", device_type);
check(device_type == 6 || device_type == 7, "XSystemGetDeviceType reports Series S (6) or Series X (7) by default");

auto console_id = get<ConsoleIdFn>("XSystemGetConsoleId");
char id[64] = {};
Expand All @@ -749,7 +763,16 @@ int main() {
info.hostingOsVersion.build == 22000 && info.hostingOsVersion.revision == 0,
"XSystemGetAnalyticsInfo returns hostingOsVersion 10.0.22000.0");
check(std::strcmp(info.family, "Xbox") == 0, "XSystemGetAnalyticsInfo returns family 'Xbox'");
check(std::strcmp(info.form, "Xbox Series X") == 0, "XSystemGetAnalyticsInfo returns form 'Xbox Series X'");
check(std::strcmp(info.form, device_type == 6 ? "Xbox Series S" : "Xbox Series X") == 0,
"XSystemGetAnalyticsInfo's form follows the default device type");
// LEONIDA_DEVICE_TYPE still decides, whatever the GPU, and the form follows it.
for (const auto& [value, want, form] :
{std::tuple{"7", 7u, "Xbox Series X"}, std::tuple{"6", 6u, "Xbox Series S"}}) {
SetEnvironmentVariableA("LEONIDA_DEVICE_TYPE", value);
check(get<DeviceTypeFn>("XSystemGetDeviceType")() == want && std::strcmp(analytics_info().form, form) == 0,
"LEONIDA_DEVICE_TYPE picks the device type and the analytics form");
}
SetEnvironmentVariableA("LEONIDA_DEVICE_TYPE", nullptr);

auto package_id = get<PackageIdentifierFn>("XPackageGetCurrentProcessPackageIdentifier");
check(package_id(0, nullptr) == E_INVALIDARG, "XPackageGetCurrentProcessPackageIdentifier rejects null buffer and size 0");
Expand Down
Loading