diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/render/direct3d12/SDL_render_d3d12_xbox.cpp')
| -rw-r--r-- | contrib/SDL-3.2.8/src/render/direct3d12/SDL_render_d3d12_xbox.cpp | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/render/direct3d12/SDL_render_d3d12_xbox.cpp b/contrib/SDL-3.2.8/src/render/direct3d12/SDL_render_d3d12_xbox.cpp new file mode 100644 index 0000000..2ea3cf1 --- /dev/null +++ b/contrib/SDL-3.2.8/src/render/direct3d12/SDL_render_d3d12_xbox.cpp | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "../../SDL_internal.h" | ||
| 23 | #if defined(SDL_VIDEO_RENDER_D3D12) && (defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)) | ||
| 24 | #include "SDL_render_d3d12_xbox.h" | ||
| 25 | #include "../../core/windows/SDL_windows.h" | ||
| 26 | #include <XGameRuntime.h> | ||
| 27 | |||
| 28 | #if defined(_MSC_VER) && !defined(__clang__) | ||
| 29 | #define SDL_COMPOSE_ERROR(str) __FUNCTION__ ", " str | ||
| 30 | #else | ||
| 31 | #define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str | ||
| 32 | #endif | ||
| 33 | |||
| 34 | static const GUID SDL_IID_ID3D12Device1 = { 0x77acce80, 0x638e, 0x4e65, { 0x88, 0x95, 0xc1, 0xf2, 0x33, 0x86, 0x86, 0x3e } }; | ||
| 35 | static const GUID SDL_IID_ID3D12Resource = { 0x696442be, 0xa72e, 0x4059, { 0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad } }; | ||
| 36 | static const GUID SDL_IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } }; | ||
| 37 | |||
| 38 | extern "C" | ||
| 39 | HRESULT D3D12_XBOX_CreateDevice(ID3D12Device **device, bool createDebug) | ||
| 40 | { | ||
| 41 | HRESULT result; | ||
| 42 | D3D12XBOX_CREATE_DEVICE_PARAMETERS params; | ||
| 43 | IDXGIDevice1 *dxgiDevice; | ||
| 44 | IDXGIAdapter *dxgiAdapter; | ||
| 45 | IDXGIOutput *dxgiOutput; | ||
| 46 | SDL_zero(params); | ||
| 47 | |||
| 48 | params.Version = D3D12_SDK_VERSION; | ||
| 49 | params.ProcessDebugFlags = createDebug ? D3D12_PROCESS_DEBUG_FLAG_DEBUG_LAYER_ENABLED : D3D12XBOX_PROCESS_DEBUG_FLAG_NONE; | ||
| 50 | params.GraphicsCommandQueueRingSizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES; | ||
| 51 | params.GraphicsScratchMemorySizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES; | ||
| 52 | params.ComputeScratchMemorySizeBytes = D3D12XBOX_DEFAULT_SIZE_BYTES; | ||
| 53 | |||
| 54 | result = D3D12XboxCreateDevice(NULL, ¶ms, SDL_IID_ID3D12Device1, (void **) device); | ||
| 55 | if (FAILED(result)) { | ||
| 56 | WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("[xbox] D3D12XboxCreateDevice"), result); | ||
| 57 | goto done; | ||
| 58 | } | ||
| 59 | |||
| 60 | result = (*device)->QueryInterface(SDL_IID_IDXGIDevice1, (void **) &dxgiDevice); | ||
| 61 | if (FAILED(result)) { | ||
| 62 | WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("[xbox] ID3D12Device to IDXGIDevice1"), result); | ||
| 63 | goto done; | ||
| 64 | } | ||
| 65 | |||
| 66 | result = dxgiDevice->GetAdapter(&dxgiAdapter); | ||
| 67 | if (FAILED(result)) { | ||
| 68 | WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("[xbox] dxgiDevice->GetAdapter"), result); | ||
| 69 | goto done; | ||
| 70 | } | ||
| 71 | |||
| 72 | result = dxgiAdapter->EnumOutputs(0, &dxgiOutput); | ||
| 73 | if (FAILED(result)) { | ||
| 74 | WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("[xbox] dxgiAdapter->EnumOutputs"), result); | ||
| 75 | goto done; | ||
| 76 | } | ||
| 77 | |||
| 78 | // Set frame interval | ||
| 79 | result = (*device)->SetFrameIntervalX(dxgiOutput, D3D12XBOX_FRAME_INTERVAL_60_HZ, 1, D3D12XBOX_FRAME_INTERVAL_FLAG_NONE); | ||
| 80 | if (FAILED(result)) { | ||
| 81 | WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("[xbox] SetFrameIntervalX"), result); | ||
| 82 | goto done; | ||
| 83 | } | ||
| 84 | |||
| 85 | result = (*device)->ScheduleFrameEventX(D3D12XBOX_FRAME_EVENT_ORIGIN, 0, NULL, D3D12XBOX_SCHEDULE_FRAME_EVENT_FLAG_NONE); | ||
| 86 | if (FAILED(result)) { | ||
| 87 | WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("[xbox] ScheduleFrameEventX"), result); | ||
| 88 | goto done; | ||
| 89 | } | ||
| 90 | |||
| 91 | done: | ||
| 92 | return result; | ||
| 93 | } | ||
| 94 | |||
| 95 | extern "C" | ||
| 96 | HRESULT D3D12_XBOX_CreateBackBufferTarget(ID3D12Device1 *device, int width, int height, void **resource) | ||
| 97 | { | ||
| 98 | |||
| 99 | D3D12_HEAP_PROPERTIES heapProps; | ||
| 100 | D3D12_RESOURCE_DESC resourceDesc; | ||
| 101 | |||
| 102 | SDL_zero(heapProps); | ||
| 103 | heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; | ||
| 104 | heapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; | ||
| 105 | heapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; | ||
| 106 | heapProps.CreationNodeMask = 1; | ||
| 107 | heapProps.VisibleNodeMask = 1; | ||
| 108 | |||
| 109 | SDL_zero(resourceDesc); | ||
| 110 | resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 111 | resourceDesc.Alignment = 0; | ||
| 112 | resourceDesc.Width = width; | ||
| 113 | resourceDesc.Height = height; | ||
| 114 | resourceDesc.DepthOrArraySize = 1; | ||
| 115 | resourceDesc.MipLevels = 1; | ||
| 116 | resourceDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; | ||
| 117 | resourceDesc.SampleDesc.Count = 1; | ||
| 118 | resourceDesc.SampleDesc.Quality = 0; | ||
| 119 | resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; | ||
| 120 | resourceDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; | ||
| 121 | |||
| 122 | return device->CreateCommittedResource(&heapProps, | ||
| 123 | D3D12_HEAP_FLAG_ALLOW_DISPLAY, | ||
| 124 | &resourceDesc, | ||
| 125 | D3D12_RESOURCE_STATE_PRESENT, | ||
| 126 | NULL, | ||
| 127 | SDL_IID_ID3D12Resource, | ||
| 128 | resource | ||
| 129 | ); | ||
| 130 | } | ||
| 131 | |||
| 132 | extern "C" | ||
| 133 | HRESULT D3D12_XBOX_StartFrame(ID3D12Device1 *device, UINT64 *outToken) | ||
| 134 | { | ||
| 135 | *outToken = D3D12XBOX_FRAME_PIPELINE_TOKEN_NULL; | ||
| 136 | return device->WaitFrameEventX(D3D12XBOX_FRAME_EVENT_ORIGIN, INFINITE, NULL, D3D12XBOX_WAIT_FRAME_EVENT_FLAG_NONE, outToken); | ||
| 137 | } | ||
| 138 | |||
| 139 | extern "C" | ||
| 140 | HRESULT D3D12_XBOX_PresentFrame(ID3D12CommandQueue *commandQueue, UINT64 token, ID3D12Resource *renderTarget) | ||
| 141 | { | ||
| 142 | D3D12XBOX_PRESENT_PLANE_PARAMETERS planeParameters; | ||
| 143 | SDL_zero(planeParameters); | ||
| 144 | planeParameters.Token = token; | ||
| 145 | planeParameters.ResourceCount = 1; | ||
| 146 | planeParameters.ppResources = &renderTarget; | ||
| 147 | return commandQueue->PresentX(1, &planeParameters, NULL); | ||
| 148 | } | ||
| 149 | |||
| 150 | extern "C" | ||
| 151 | void D3D12_XBOX_GetResolution(Uint32 *width, Uint32 *height) | ||
| 152 | { | ||
| 153 | switch (XSystemGetDeviceType()) { | ||
| 154 | case XSystemDeviceType::XboxScarlettLockhart: | ||
| 155 | *width = 2560; | ||
| 156 | *height = 1440; | ||
| 157 | break; | ||
| 158 | |||
| 159 | case XSystemDeviceType::XboxOneX: | ||
| 160 | case XSystemDeviceType::XboxScarlettAnaconda: | ||
| 161 | case XSystemDeviceType::XboxOneXDevkit: | ||
| 162 | case XSystemDeviceType::XboxScarlettDevkit: | ||
| 163 | *width = 3840; | ||
| 164 | *height = 2160; | ||
| 165 | break; | ||
| 166 | |||
| 167 | default: | ||
| 168 | *width = 1920; | ||
| 169 | *height = 1080; | ||
| 170 | break; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | #endif // SDL_VIDEO_RENDER_D3D12 && (SDL_PLATFORM_XBOXONE || SDL_PLATFORM_XBOXSERIES) | ||
