SDL_render_d3d12_xbox.cpp 6.2 KB

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