diff --git a/CMakeLists.txt b/CMakeLists.txt
index 18cf7302b..f5e0e344e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1596,6 +1596,7 @@ elseif(WINDOWS)
check_include_file(d3d9.h HAVE_D3D_H)
check_include_file(d3d11_1.h HAVE_D3D11_H)
+ check_include_file(d3d12.h HAVE_D3D12_H)
check_include_file(ddraw.h HAVE_DDRAW_H)
check_include_file(dsound.h HAVE_DSOUND_H)
check_include_file(dinput.h HAVE_DINPUT_H)
@@ -1603,7 +1604,7 @@ elseif(WINDOWS)
set(HAVE_DINPUT_H 0)
endif()
check_include_file(dxgi.h HAVE_DXGI_H)
- if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H)
+ if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_D3D12_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H)
set(HAVE_DIRECTX TRUE)
if(NOT MINGW AND NOT USE_WINSDK_DIRECTX)
# TODO: change $ENV{DXSDL_DIR} to get the path from the include checks
@@ -1697,6 +1698,10 @@ elseif(WINDOWS)
set(SDL_VIDEO_RENDER_D3D11 1)
set(HAVE_RENDER_D3D TRUE)
endif()
+ if(SDL_RENDER_D3D AND HAVE_D3D12_H AND NOT WINDOWS_STORE)
+ set(SDL_VIDEO_RENDER_D3D12 1)
+ set(HAVE_RENDER_D3D TRUE)
+ endif()
set(HAVE_SDL_VIDEO TRUE)
endif()
diff --git a/Makefile.w32 b/Makefile.w32
index 60775f13f..ee5c4b10f 100644
--- a/Makefile.w32
+++ b/Makefile.w32
@@ -80,6 +80,7 @@ SRCS+= SDL_syspower.c
SRCS+= SDL_d3dmath.c
SRCS+= SDL_render_d3d.c SDL_shaders_d3d.c
SRCS+= SDL_render_d3d11.c SDL_shaders_d3d11.c
+SRCS+= SDL_render_d3d12.c SDL_shaders_d3d12.c
SRCS+= SDL_render_gl.c SDL_shaders_gl.c
SRCS+= SDL_render_gles2.c SDL_shaders_gles2.c
SRCS+= SDL_windowssensor.c
@@ -99,7 +100,7 @@ RCOBJS= $(RCSRCS:.rc=.res)
.c: ./src;./src/dynapi;./src/audio;./src/cpuinfo;./src/events;./src/file;./src/haptic;./src/joystick;./src/power;./src/render;./src/render/software;./src/sensor;./src/stdlib;./src/thread;./src/timer;./src/video;./src/video/yuv2rgb;./src/atomic;./src/audio/disk;
.c: ./src/haptic/dummy;./src/joystick/dummy;./src/joystick/virtual;./src/audio/dummy;./src/video/dummy;./src/sensor/dummy;
.c: ./src/core/windows;./src/audio/winmm;./src/audio/directsound;./src/audio/wasapi;./src/loadso/windows;./src/filesystem/windows;./src/haptic/windows;./src/joystick/windows;./src/sensor/windows;./src/thread/windows;./src/timer/windows;./src/video/windows;
-.c: ./src/locale/;./src/locale/windows;./src/misc;./src/misc/windows;./src/power/windows;./src/joystick/hidapi;./src/hidapi;./src/render/direct3d;./src/render/direct3d11;./src/render/opengl;./src/render/opengles2
+.c: ./src/locale/;./src/locale/windows;./src/misc;./src/misc/windows;./src/power/windows;./src/joystick/hidapi;./src/hidapi;./src/render/direct3d;./src/render/direct3d11;./src/render/direct3d12;./src/render/opengl;./src/render/opengles2
.rc: ./src/main/windows
all: $(DLLFILE) $(LIBFILE) $(TLIB) .symbolic
diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj
index aaeda8717..04ed6b46f 100644
--- a/VisualC/SDL/SDL.vcxproj
+++ b/VisualC/SDL/SDL.vcxproj
@@ -348,6 +348,7 @@
+
@@ -527,6 +528,8 @@
+
+
@@ -617,4 +620,4 @@
-
+
\ No newline at end of file
diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters
index a3d9d5c80..a093f4dd6 100644
--- a/VisualC/SDL/SDL.vcxproj.filters
+++ b/VisualC/SDL/SDL.vcxproj.filters
@@ -169,6 +169,9 @@
{4755f3a6-49ac-46d6-86be-21f5c21f2197}
+
+ {f48c2b17-1bee-4fec-a7c8-24cf619abe08}
+
@@ -829,6 +832,9 @@
+
+ render\direct3d12
+
@@ -1326,8 +1332,14 @@
power\windows
+
+ render\direct3d12
+
+
+ render\direct3d12
+
-
+
\ No newline at end of file
diff --git a/configure.ac b/configure.ac
index 421ea98ce..dc5c4c718 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3264,6 +3264,7 @@ CheckDIRECTX()
if test x$enable_directx = xyes; then
AC_CHECK_HEADER(d3d9.h, have_d3d=yes)
AC_CHECK_HEADER(d3d11_1.h, have_d3d11=yes)
+ AC_CHECK_HEADER(d3d12.h, have_d3d12=yes)
AC_CHECK_HEADER(ddraw.h, have_ddraw=yes)
AC_CHECK_HEADER(dsound.h, have_dsound=yes)
AC_CHECK_HEADER(dinput.h, have_dinput=yes)
@@ -3991,6 +3992,10 @@ case "$host" in
AC_DEFINE(SDL_VIDEO_RENDER_D3D11, 1, [ ])
SUMMARY_video="${SUMMARY_video} d3d11"
fi
+ if test x$enable_render_d3d = xyes -a x$have_d3d12 = xyes; then
+ AC_DEFINE(SDL_VIDEO_RENDER_D3D12, 1, [ ])
+ SUMMARY_video="${SUMMARY_video} d3d12"
+ fi
fi
# Set up files for the audio library
if test x$enable_audio = xyes; then
diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake
index 9e058218d..b6b583992 100644
--- a/include/SDL_config.h.cmake
+++ b/include/SDL_config.h.cmake
@@ -243,6 +243,7 @@
#cmakedefine HAVE_D3D_H @HAVE_D3D_H@
#cmakedefine HAVE_D3D11_H @HAVE_D3D11_H@
+#cmakedefine HAVE_D3D12_H @HAVE_D3D12_H@
#cmakedefine HAVE_DDRAW_H @HAVE_DDRAW_H@
#cmakedefine HAVE_DSOUND_H @HAVE_DSOUND_H@
#cmakedefine HAVE_DINPUT_H @HAVE_DINPUT_H@
diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in
index 9a80d45b7..c18077fea 100644
--- a/include/SDL_config.h.in
+++ b/include/SDL_config.h.in
@@ -413,6 +413,7 @@
#undef SDL_VIDEO_RENDER_D3D
#undef SDL_VIDEO_RENDER_D3D11
+#undef SDL_VIDEO_RENDER_D3D12
#undef SDL_VIDEO_RENDER_OGL
#undef SDL_VIDEO_RENDER_OGL_ES
#undef SDL_VIDEO_RENDER_OGL_ES2
diff --git a/include/SDL_config_windows.h b/include/SDL_config_windows.h
index 81fde40c8..076bea410 100644
--- a/include/SDL_config_windows.h
+++ b/include/SDL_config_windows.h
@@ -38,6 +38,18 @@
#include
#endif
+/* sdkddkver.h defines more specific SDK version numbers. This is needed because older versions of the
+ * Windows 10 SDK have broken declarations for the C API for DirectX 12. */
+#if !defined(HAVE_SDKDDKVER_H) && defined(__has_include)
+#if __has_include()
+#define HAVE_SDKDDKVER_H 1
+#endif
+#endif
+
+#ifdef HAVE_SDKDDKVER_H
+#include
+#endif
+
/* This is a set of defines to configure the SDL features */
#if !defined(_STDINT_H_) && (!defined(HAVE_STDINT_H) || !_HAVE_STDINT_H)
@@ -107,6 +119,9 @@ typedef unsigned int uintptr_t;
#define HAVE_D3D11_H 1
#define HAVE_ROAPI_H 1
#endif
+#if defined(WDK_NTDDI_VERSION) && WDK_NTDDI_VERSION > 0x0A000008 /* 10.0.19041.0 */
+#define HAVE_D3D12_H 1
+#endif
#define HAVE_MMDEVICEAPI_H 1
#define HAVE_AUDIOCLIENT_H 1
#define HAVE_TPCSHRD_H 1
@@ -295,6 +310,9 @@ typedef unsigned int uintptr_t;
#if !defined(SDL_VIDEO_RENDER_D3D11) && defined(HAVE_D3D11_H)
#define SDL_VIDEO_RENDER_D3D11 1
#endif
+#if !defined(SDL_VIDEO_RENDER_D3D12) && defined(HAVE_D3D12_H)
+#define SDL_VIDEO_RENDER_D3D12 1
+#endif
/* Enable OpenGL support */
#ifndef SDL_VIDEO_OPENGL
diff --git a/include/SDL_config_winrt.h b/include/SDL_config_winrt.h
index 75aef4ebb..556940369 100644
--- a/include/SDL_config_winrt.h
+++ b/include/SDL_config_winrt.h
@@ -250,6 +250,9 @@ typedef unsigned int uintptr_t;
/* Enable appropriate renderer(s) */
#define SDL_VIDEO_RENDER_D3D11 1
+/* Disable D3D12 as it's not implemented for WinRT */
+#define SDL_VIDEO_RENDER_D3D12 0
+
#if SDL_VIDEO_OPENGL_ES2
#define SDL_VIDEO_RENDER_OGL_ES2 1
#endif
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 83cfe89a6..2699b6ae9 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1200,6 +1200,8 @@ extern "C" {
*
* This variable is case insensitive and can be set to the following values:
* "direct3d"
+ * "direct3d11"
+ * "direct3d12"
* "opengl"
* "opengles2"
* "opengles"
diff --git a/include/SDL_system.h b/include/SDL_system.h
index bbe8d4558..60871ff90 100644
--- a/include/SDL_system.h
+++ b/include/SDL_system.h
@@ -102,6 +102,22 @@ typedef struct ID3D11Device ID3D11Device;
*/
extern DECLSPEC ID3D11Device* SDLCALL SDL_RenderGetD3D11Device(SDL_Renderer * renderer);
+typedef struct ID3D12Device ID3D12Device;
+
+/**
+ * Get the D3D12 device associated with a renderer.
+ *
+ * Once you are done using the device, you should release it to avoid a
+ * resource leak.
+ *
+ * \param renderer the renderer from which to get the associated D3D12 device
+ * \returns the D3D12 device associated with given renderer or NULL if it is
+ * not a D3D12 renderer; call SDL_GetError() for more information.
+ *
+ * \since This function is available since SDL 2.0.23.
+ */
+extern DECLSPEC ID3D12Device* SDLCALL SDL_RenderGetD3D12Device(SDL_Renderer* renderer);
+
/**
* Get the DXGI Adapter and Output indices for the specified display index.
*
diff --git a/src/core/windows/SDL_windows.h b/src/core/windows/SDL_windows.h
index 681f01b87..9b0149253 100644
--- a/src/core/windows/SDL_windows.h
+++ b/src/core/windows/SDL_windows.h
@@ -33,7 +33,11 @@
#undef WINVER
#define WINVER 0x0501
#undef _WIN32_WINNT
+#if !defined(SDL_VIDEO_RENDER_D3D12)
#define _WIN32_WINNT 0x501 /* Need 0x410 for AlphaBlend() and 0x500 for EnumDisplayDevices(), 0x501 for raw input */
+#else
+#define _WIN32_WINNT 0xA00 /* For D3D12, 0xA00 is required */
+#endif
#endif
#include
diff --git a/src/dynapi/SDL2.exports b/src/dynapi/SDL2.exports
index 481e1db0b..5bc965dfd 100644
--- a/src/dynapi/SDL2.exports
+++ b/src/dynapi/SDL2.exports
@@ -847,3 +847,4 @@
++'_SDL_GUIDFromString'.'SDL2.dll'.'SDL_GUIDFromString'
++'_SDL_HasLSX'.'SDL2.dll'.'SDL_HasLSX'
++'_SDL_HasLASX'.'SDL2.dll'.'SDL_HasLASX'
+++'_SDL_RenderGetD3D12Device'.'SDL2.dll'.'SDL_RenderGetD3D12Device'
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 97f3b8007..76c9e9199 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -873,3 +873,4 @@
#define SDL_GUIDFromString SDL_GUIDFromString_REAL
#define SDL_HasLSX SDL_HasLSX_REAL
#define SDL_HasLASX SDL_HasLASX_REAL
+#define SDL_RenderGetD3D12Device SDL_RenderGetD3D12Device_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index a61333a39..ddc655760 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -948,3 +948,6 @@ SDL_DYNAPI_PROC(void,SDL_GUIDToString,(SDL_GUID a, char *b, int c),(a,b,c),)
SDL_DYNAPI_PROC(SDL_GUID,SDL_GUIDFromString,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasLSX,(void),(),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_HasLASX,(void),(),return)
+#ifdef __WIN32__
+SDL_DYNAPI_PROC(ID3D12Device*,SDL_RenderGetD3D12Device,(SDL_Renderer *a),(a),return)
+#endif
diff --git a/src/render/SDL_d3dmath.c b/src/render/SDL_d3dmath.c
index 63048bb88..fb7de371f 100644
--- a/src/render/SDL_d3dmath.c
+++ b/src/render/SDL_d3dmath.c
@@ -20,7 +20,7 @@
*/
#include "../SDL_internal.h"
-#if (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11) && !SDL_RENDER_DISABLED
+#if (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11 || SDL_VIDEO_RENDER_D3D12) && !SDL_RENDER_DISABLED
#include "SDL_stdinc.h"
#include "SDL_d3dmath.h"
@@ -131,6 +131,6 @@ Float4X4 MatrixRotationZ(float r)
}
-#endif /* (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11) && !SDL_RENDER_DISABLED */
+#endif /* (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11 || SDL_VIDEO_RENDER_D3D12) && !SDL_RENDER_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/render/SDL_d3dmath.h b/src/render/SDL_d3dmath.h
index 7b74cfe75..211bfdd5a 100644
--- a/src/render/SDL_d3dmath.h
+++ b/src/render/SDL_d3dmath.h
@@ -20,7 +20,7 @@
*/
#include "../SDL_internal.h"
-#if (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11) && !SDL_RENDER_DISABLED
+#if (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11 || SDL_VIDEO_RENDER_D3D12) && !SDL_RENDER_DISABLED
/* Direct3D matrix math functions */
@@ -67,6 +67,6 @@ Float4X4 MatrixRotationX(float r);
Float4X4 MatrixRotationY(float r);
Float4X4 MatrixRotationZ(float r);
-#endif /* (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11) && !SDL_RENDER_DISABLED */
+#endif /* (SDL_VIDEO_RENDER_D3D || SDL_VIDEO_RENDER_D3D11 || SDL_VIDEO_RENDER_D3D12) && !SDL_RENDER_DISABLED */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index d94b0a988..4224a70f6 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -96,6 +96,9 @@ static const SDL_RenderDriver *render_drivers[] = {
#if SDL_VIDEO_RENDER_D3D11
&D3D11_RenderDriver,
#endif
+#if SDL_VIDEO_RENDER_D3D12
+ &D3D12_RenderDriver,
+#endif
#if SDL_VIDEO_RENDER_METAL
&METAL_RenderDriver,
#endif
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 7aeb46710..481ae91bf 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -283,6 +283,7 @@ struct SDL_RenderDriver
/* Not all of these are available in a given build. Use #ifdefs, etc. */
extern SDL_RenderDriver D3D_RenderDriver;
extern SDL_RenderDriver D3D11_RenderDriver;
+extern SDL_RenderDriver D3D12_RenderDriver;
extern SDL_RenderDriver GL_RenderDriver;
extern SDL_RenderDriver GLES2_RenderDriver;
extern SDL_RenderDriver GLES_RenderDriver;
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
new file mode 100644
index 000000000..447e6a07b
--- /dev/null
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -0,0 +1,3042 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#include "SDL_render.h"
+#include "SDL_system.h"
+
+#if SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED
+
+#define SDL_D3D12_NUM_BUFFERS 2
+#define SDL_D3D12_NUM_VERTEX_BUFFERS 256
+#define SDL_D3D12_VERTEX_BUFFER_MAX_TRIS 2048
+#define SDL_D3D12_MAX_NUM_TEXTURES 16384
+#define SDL_D3D12_NUM_UPLOAD_BUFFERS 32
+
+#define COBJMACROS
+#include "../../core/windows/SDL_windows.h"
+#include "SDL_hints.h"
+#include "SDL_loadso.h"
+#include "SDL_syswm.h"
+#include "../SDL_sysrender.h"
+#include "../SDL_d3dmath.h"
+
+#include
+#include
+#include
+#include
+
+#include "SDL_shaders_d3d12.h"
+
+#if defined(_MSC_VER) && !defined(__clang__)
+#define SDL_COMPOSE_ERROR(str) __FUNCTION__ ", " str
+#else
+#define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str
+#endif
+
+#define SAFE_RELEASE(X) if ((X)) { IUnknown_Release(SDL_static_cast(IUnknown*, X)); X = NULL; }
+
+
+/* !!! FIXME: vertex buffer bandwidth could be lower; only use UV coords when
+ !!! FIXME: textures are needed. */
+
+/* Vertex shader, common values */
+typedef struct
+{
+ Float4X4 model;
+ Float4X4 projectionAndView;
+} VertexShaderConstants;
+
+/* Per-vertex data */
+typedef struct
+{
+ Float2 pos;
+ Float2 tex;
+ SDL_Color color;
+} VertexPositionColor;
+
+/* Per-texture data */
+typedef struct
+{
+ ID3D12Resource *mainTexture;
+ D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceView;
+ D3D12_RESOURCE_STATES mainResourceState;
+ SIZE_T mainSRVIndex;
+ D3D12_CPU_DESCRIPTOR_HANDLE mainTextureRenderTargetView;
+ DXGI_FORMAT mainTextureFormat;
+ ID3D12Resource *stagingBuffer;
+ D3D12_RESOURCE_STATES stagingResourceState;
+ D3D12_FILTER scaleMode;
+#if SDL_HAVE_YUV
+ /* YV12 texture support */
+ SDL_bool yuv;
+ ID3D12Resource *mainTextureU;
+ D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceViewU;
+ D3D12_RESOURCE_STATES mainResourceStateU;
+ SIZE_T mainSRVIndexU;
+ ID3D12Resource *mainTextureV;
+ D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceViewV;
+ D3D12_RESOURCE_STATES mainResourceStateV;
+ SIZE_T mainSRVIndexV;
+
+ /* NV12 texture support */
+ SDL_bool nv12;
+ ID3D12Resource *mainTextureNV;
+ D3D12_CPU_DESCRIPTOR_HANDLE mainTextureResourceViewNV;
+ D3D12_RESOURCE_STATES mainResourceStateNV;
+ SIZE_T mainSRVIndexNV;
+
+ Uint8 *pixels;
+ int pitch;
+#endif
+ SDL_Rect lockedRect;
+} D3D12_TextureData;
+
+/* Pipeline State Object data */
+typedef struct
+{
+ D3D12_Shader shader;
+ SDL_BlendMode blendMode;
+ D3D12_PRIMITIVE_TOPOLOGY_TYPE topology;
+ DXGI_FORMAT rtvFormat;
+ ID3D12PipelineState *pipelineState;
+} D3D12_PipelineState;
+
+/* Vertex Buffer */
+typedef struct
+{
+ ID3D12Resource *resource;
+ D3D12_VERTEX_BUFFER_VIEW view;
+} D3D12_VertexBuffer;
+
+/* For SRV pool allocator */
+typedef struct
+{
+ SIZE_T index;
+ void *next;
+} D3D12_SRVPoolNode;
+
+/* Private renderer data */
+typedef struct
+{
+ void *hDXGIMod;
+ void *hD3D12Mod;
+ IDXGIFactory6 *dxgiFactory;
+ IDXGIAdapter4 *dxgiAdapter;
+ ID3D12Device5 *d3dDevice;
+ ID3D12Debug *debugInterface;
+ IDXGIDebug1 *dxgiDebug;
+ ID3D12CommandQueue *commandQueue;
+ ID3D12GraphicsCommandList2 *commandList;
+ IDXGISwapChain4 *swapChain;
+ DXGI_SWAP_EFFECT swapEffect;
+
+ /* Descriptor heaps */
+ ID3D12DescriptorHeap* rtvDescriptorHeap;
+ UINT rtvDescriptorSize;
+ ID3D12DescriptorHeap* textureRTVDescriptorHeap;
+ ID3D12DescriptorHeap* srvDescriptorHeap;
+ UINT srvDescriptorSize;
+ ID3D12DescriptorHeap* samplerDescriptorHeap;
+ UINT samplerDescriptorSize;
+
+ /* Data needed per backbuffer */
+ ID3D12CommandAllocator *commandAllocators[SDL_D3D12_NUM_BUFFERS];
+ ID3D12Resource *renderTargets[SDL_D3D12_NUM_BUFFERS];
+ UINT64 fenceValue;
+ int currentBackBufferIndex;
+
+ /* Fences */
+ ID3D12Fence *fence;
+ HANDLE fenceEvent;
+
+ /* Root signature and pipeline state data */
+ ID3D12RootSignature *rootSignatures[NUM_ROOTSIGS];
+ int pipelineStateCount;
+ D3D12_PipelineState *pipelineStates;
+ D3D12_PipelineState *currentPipelineState;
+
+ D3D12_VertexBuffer vertexBuffers[SDL_D3D12_NUM_VERTEX_BUFFERS];
+ ID3D12Heap *vertexBufferHeap;
+ D3D12_CPU_DESCRIPTOR_HANDLE nearestPixelSampler;
+ D3D12_CPU_DESCRIPTOR_HANDLE linearSampler;
+
+ /* Data for staging/allocating textures */
+ ID3D12Resource *uploadBuffers[SDL_D3D12_NUM_UPLOAD_BUFFERS];
+ int currentUploadBuffer;
+
+ /* Pool allocator to handle reusing SRV heap indices */
+ D3D12_SRVPoolNode *srvPoolHead;
+ D3D12_SRVPoolNode srvPoolNodes[SDL_D3D12_MAX_NUM_TEXTURES];
+
+ /* Vertex buffer constants */
+ VertexShaderConstants vertexShaderConstantsData;
+
+ /* Cached renderer properties */
+ DXGI_MODE_ROTATION rotation;
+ D3D12_TextureData* textureRenderTarget;
+ D3D12_CPU_DESCRIPTOR_HANDLE currentRenderTargetView;
+ D3D12_CPU_DESCRIPTOR_HANDLE currentShaderResource;
+ D3D12_CPU_DESCRIPTOR_HANDLE currentSampler;
+ SDL_bool cliprectDirty;
+ SDL_bool currentCliprectEnabled;
+ SDL_Rect currentCliprect;
+ SDL_Rect currentViewport;
+ int currentViewportRotation;
+ SDL_bool viewportDirty;
+ Float4X4 identity;
+ int currentVertexBuffer;
+ SDL_bool issueBatch;
+} D3D12_RenderData;
+
+
+/* Define D3D GUIDs here so we don't have to include uuid.lib. */
+
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-const-variable"
+#endif
+
+static const GUID SDL_IID_IDXGIFactory6 = { 0xc1b6694f, 0xff09, 0x44a9, { 0xb0, 0x3c, 0x77, 0x90, 0x0a, 0x0a, 0x1d, 0x17 } };
+static const GUID SDL_IID_IDXGIAdapter4 = { 0x3c8d99d1, 0x4fbf, 0x4181, { 0xa8, 0x2c, 0xaf, 0x66, 0xbf, 0x7b, 0xd2, 0x4e } };
+static const GUID SDL_IID_IDXGIDevice1 = { 0x77db970f, 0x6276, 0x48ba, { 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39, 0x2c } };
+static const GUID SDL_IID_ID3D12Device5 = { 0x8b4f173b, 0x2fea, 0x4b80, { 0x8f, 0x58, 0x43, 0x07, 0x19, 0x1a, 0xb9, 0x5d } };
+static const GUID SDL_IID_IDXGISwapChain4 = { 0x3D585D5A, 0xBD4A, 0x489E, { 0xB1, 0xF4, 0x3D, 0xBC, 0xB6, 0x45, 0x2F, 0xFB } };
+static const GUID SDL_IID_IDXGIDebug1 = { 0xc5a05f0c, 0x16f2, 0x4adf, { 0x9f, 0x4d, 0xa8, 0xc4, 0xd5, 0x8a, 0xc5, 0x50 } };
+static const GUID SDL_IID_IDXGIInfoQueue = { 0xD67441C7,0x672A,0x476f, { 0x9E,0x82,0xCD,0x55,0xB4,0x49,0x49,0xCE } };
+static const GUID SDL_IID_ID3D12Debug = { 0x344488b7, 0x6846, 0x474b, { 0xb9, 0x89, 0xf0, 0x27, 0x44, 0x82, 0x45, 0xe0 } };
+static const GUID SDL_DXGI_DEBUG_ALL = { 0xe48ae283, 0xda80, 0x490b, { 0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x8 } };
+static const GUID SDL_IID_ID3D12CommandQueue = { 0x0ec870a6, 0x5d7e, 0x4c22, { 0x8c, 0xfc, 0x5b, 0xaa, 0xe0, 0x76, 0x16, 0xed } };
+static const GUID SDL_IID_ID3D12DescriptorHeap = { 0x8efb471d, 0x616c, 0x4f49, { 0x90, 0xf7, 0x12, 0x7b, 0xb7, 0x63, 0xfa, 0x51 } };
+static const GUID SDL_IID_ID3D12CommandAllocator = { 0x6102dee4, 0xaf59, 0x4b09, { 0xb9, 0x99, 0xb4, 0x4d, 0x73, 0xf0, 0x9b, 0x24 } };
+static const GUID SDL_IID_ID3D12GraphicsCommandList2 = { 0x38C3E585, 0xFF17, 0x412C, { 0x91, 0x50, 0x4F, 0xC6, 0xF9, 0xD7, 0x2A, 0x28 } };
+static const GUID SDL_IID_ID3D12Fence = { 0x0a753dcf, 0xc4d8, 0x4b91, { 0xad, 0xf6, 0xbe, 0x5a, 0x60, 0xd9, 0x5a, 0x76 } };
+static const GUID SDL_IID_ID3D12Resource = { 0x696442be, 0xa72e, 0x4059, { 0xbc, 0x79, 0x5b, 0x5c, 0x98, 0x04, 0x0f, 0xad } };
+static const GUID SDL_IID_ID3D12RootSignature = { 0xc54a6b66, 0x72df, 0x4ee8, { 0x8b, 0xe5, 0xa9, 0x46, 0xa1, 0x42, 0x92, 0x14 } };
+static const GUID SDL_IID_ID3D12PipelineState = { 0x765a30f3, 0xf624, 0x4c6f, { 0xa8, 0x28, 0xac, 0xe9, 0x48, 0x62, 0x24, 0x45 } };
+static const GUID SDL_IID_ID3D12Heap = { 0x6b3b2502, 0x6e51, 0x45b3, { 0x90, 0xee, 0x98, 0x84, 0x26, 0x5e, 0x8d, 0xf3 } };
+static const GUID SDL_IID_ID3D12InfoQueue = { 0x0742a90b, 0xc387, 0x483f, { 0xb9, 0x46, 0x30, 0xa7, 0xe4, 0xe6, 0x14, 0x58 } };
+
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
+
+
+
+UINT
+D3D12_Align(UINT location, UINT alignment)
+{
+ return ((location + (alignment - 1)) & ~(alignment - 1));
+}
+
+Uint32
+D3D12_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat)
+{
+ switch (dxgiFormat) {
+ case DXGI_FORMAT_B8G8R8A8_UNORM:
+ return SDL_PIXELFORMAT_ARGB8888;
+ case DXGI_FORMAT_B8G8R8X8_UNORM:
+ return SDL_PIXELFORMAT_RGB888;
+ default:
+ return SDL_PIXELFORMAT_UNKNOWN;
+ }
+}
+
+static DXGI_FORMAT
+SDLPixelFormatToDXGIFormat(Uint32 sdlFormat)
+{
+ switch (sdlFormat) {
+ case SDL_PIXELFORMAT_ARGB8888:
+ return DXGI_FORMAT_B8G8R8A8_UNORM;
+ case SDL_PIXELFORMAT_RGB888:
+ return DXGI_FORMAT_B8G8R8X8_UNORM;
+ case SDL_PIXELFORMAT_YV12:
+ case SDL_PIXELFORMAT_IYUV:
+ case SDL_PIXELFORMAT_NV12: /* For the Y texture */
+ case SDL_PIXELFORMAT_NV21: /* For the Y texture */
+ return DXGI_FORMAT_R8_UNORM;
+ default:
+ return DXGI_FORMAT_UNKNOWN;
+ }
+}
+
+static void D3D12_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
+
+static void
+D3D12_ReleaseAll(SDL_Renderer * renderer)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+ SDL_Texture *texture = NULL;
+
+ /* Release all textures */
+ for (texture = renderer->textures; texture; texture = texture->next) {
+ D3D12_DestroyTexture(renderer, texture);
+ }
+
+ /* Release/reset everything else */
+ if (data) {
+ int i;
+
+ SAFE_RELEASE(data->dxgiFactory);
+ SAFE_RELEASE(data->dxgiAdapter);
+ SAFE_RELEASE(data->d3dDevice);
+ SAFE_RELEASE(data->debugInterface);
+ SAFE_RELEASE(data->commandQueue);
+ SAFE_RELEASE(data->commandList);
+ SAFE_RELEASE(data->rtvDescriptorHeap);
+ SAFE_RELEASE(data->textureRTVDescriptorHeap);
+ SAFE_RELEASE(data->srvDescriptorHeap);
+ SAFE_RELEASE(data->samplerDescriptorHeap);
+ SAFE_RELEASE(data->swapChain);
+ SAFE_RELEASE(data->fence);
+ SAFE_RELEASE(data->vertexBufferHeap);
+
+ for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
+ SAFE_RELEASE(data->commandAllocators[i]);
+ SAFE_RELEASE(data->renderTargets[i]);
+ }
+
+ if (data->pipelineStateCount > 0) {
+ for (i = 0; i < data->pipelineStateCount; ++i) {
+ SAFE_RELEASE(data->pipelineStates[i].pipelineState);
+ }
+ SDL_free(data->pipelineStates);
+ data->pipelineStateCount = 0;
+ }
+
+ for (i = 0; i < NUM_ROOTSIGS; ++i) {
+ SAFE_RELEASE(data->rootSignatures[i]);
+ }
+
+ for (i = 0; i < SDL_D3D12_NUM_VERTEX_BUFFERS; ++i) {
+ SAFE_RELEASE(data->vertexBuffers[i].resource);
+ }
+
+ data->swapEffect = (DXGI_SWAP_EFFECT) 0;
+ data->currentRenderTargetView.ptr = 0;
+ data->currentSampler.ptr = 0;
+
+ /* Check for any leaks if in debug mode */
+ if (data->dxgiDebug) {
+ IDXGIDebug1_ReportLiveObjects(data->dxgiDebug, SDL_DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_SUMMARY | DXGI_DEBUG_RLO_IGNORE_INTERNAL);
+ SAFE_RELEASE(data->dxgiDebug);
+ }
+
+ /* Unload the D3D libraries. This should be done last, in order
+ * to prevent IUnknown::Release() calls from crashing.
+ */
+ if (data->hD3D12Mod) {
+ SDL_UnloadObject(data->hD3D12Mod);
+ data->hD3D12Mod = NULL;
+ }
+ if (data->hDXGIMod) {
+ SDL_UnloadObject(data->hDXGIMod);
+ data->hDXGIMod = NULL;
+ }
+ }
+}
+
+static D3D12_GPU_DESCRIPTOR_HANDLE
+D3D12_CPUtoGPUHandle(ID3D12DescriptorHeap * heap, D3D12_CPU_DESCRIPTOR_HANDLE CPUHandle)
+{
+ D3D12_CPU_DESCRIPTOR_HANDLE CPUHeapStart;
+ D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle;
+ SIZE_T offset;
+
+ /* Calculate the correct offset into the heap */
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(heap, &CPUHeapStart);
+ offset = CPUHandle.ptr - CPUHeapStart.ptr;
+
+ ID3D12DescriptorHeap_GetGPUDescriptorHandleForHeapStart(heap, &GPUHandle);
+ GPUHandle.ptr += offset;
+
+ return GPUHandle;
+}
+
+static void
+D3D12_WaitForGPU(D3D12_RenderData * data)
+{
+ HRESULT result;
+
+ if (data->commandQueue && data->fence && data->fenceEvent)
+ {
+ result = ID3D12CommandQueue_Signal(data->commandQueue, data->fence, data->fenceValue);
+ if (ID3D12Fence_GetCompletedValue(data->fence) < data->fenceValue) {
+ result = ID3D12Fence_SetEventOnCompletion(
+ data->fence,
+ data->fenceValue,
+ data->fenceEvent
+ );
+ WaitForSingleObjectEx(data->fenceEvent, INFINITE, FALSE);
+ }
+
+ data->fenceValue++;
+ }
+}
+
+static D3D12_CPU_DESCRIPTOR_HANDLE
+D3D12_GetCurrentRenderTargetView(SDL_Renderer * renderer)
+{
+ D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
+ D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor;
+
+ if (data->textureRenderTarget) {
+ return data->textureRenderTarget->mainTextureRenderTargetView;
+ }
+
+ SDL_zero(rtvDescriptor);
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(data->rtvDescriptorHeap, &rtvDescriptor);
+ rtvDescriptor.ptr += data->currentBackBufferIndex * data->rtvDescriptorSize;
+ return rtvDescriptor;
+}
+
+static void
+D3D12_TransitionResource(D3D12_RenderData * data,
+ ID3D12Resource * resource,
+ D3D12_RESOURCE_STATES beforeState,
+ D3D12_RESOURCE_STATES afterState
+)
+{
+ D3D12_RESOURCE_BARRIER barrier;
+
+ if (beforeState != afterState) {
+ SDL_zero(barrier);
+ barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
+ barrier.Transition.pResource = resource;
+ barrier.Transition.StateBefore = beforeState;
+ barrier.Transition.StateAfter = afterState;
+ barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
+
+ ID3D12GraphicsCommandList2_ResourceBarrier(data->commandList, 1, &barrier);
+ }
+}
+
+static void
+D3D12_ResetCommandList(D3D12_RenderData * data)
+{
+ int i;
+ ID3D12DescriptorHeap *rootDescriptorHeaps[] = { data->srvDescriptorHeap, data->samplerDescriptorHeap };
+
+ ID3D12CommandAllocator_Reset(data->commandAllocators[data->currentBackBufferIndex]);
+ ID3D12GraphicsCommandList2_Reset(data->commandList, data->commandAllocators[data->currentBackBufferIndex], NULL);
+ data->currentPipelineState = NULL;
+ data->currentVertexBuffer = 0;
+ data->issueBatch = SDL_FALSE;
+ data->cliprectDirty = SDL_TRUE;
+ data->viewportDirty = SDL_TRUE;
+ data->currentRenderTargetView.ptr = 0;
+
+ /* Release any upload buffers that were inflight */
+ for (i = 0; i < data->currentUploadBuffer; ++i) {
+ SAFE_RELEASE(data->uploadBuffers[i]);
+ }
+ data->currentUploadBuffer = 0;
+
+ ID3D12GraphicsCommandList2_SetDescriptorHeaps(data->commandList, 2, rootDescriptorHeaps);
+}
+
+static int
+D3D12_IssueBatch(D3D12_RenderData * data)
+{
+ HRESULT result = S_OK;
+
+ /* Issue the command list */
+ result = ID3D12GraphicsCommandList2_Close(data->commandList);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12_IssueBatch"), result);
+ return result;
+ }
+ ID3D12CommandQueue_ExecuteCommandLists(data->commandQueue, 1, (ID3D12CommandList* const*)&data->commandList);
+
+ D3D12_WaitForGPU(data);
+
+ D3D12_ResetCommandList(data);
+
+ return result;
+}
+
+static void
+D3D12_DestroyRenderer(SDL_Renderer * renderer)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+ D3D12_WaitForGPU(data);
+ D3D12_ReleaseAll(renderer);
+ if (data) {
+ SDL_free(data);
+ }
+ SDL_free(renderer);
+}
+
+static D3D12_BLEND
+GetBlendFunc(SDL_BlendFactor factor)
+{
+ switch (factor) {
+ case SDL_BLENDFACTOR_ZERO:
+ return D3D12_BLEND_ZERO;
+ case SDL_BLENDFACTOR_ONE:
+ return D3D12_BLEND_ONE;
+ case SDL_BLENDFACTOR_SRC_COLOR:
+ return D3D12_BLEND_SRC_COLOR;
+ case SDL_BLENDFACTOR_ONE_MINUS_SRC_COLOR:
+ return D3D12_BLEND_INV_SRC_COLOR;
+ case SDL_BLENDFACTOR_SRC_ALPHA:
+ return D3D12_BLEND_SRC_ALPHA;
+ case SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA:
+ return D3D12_BLEND_INV_SRC_ALPHA;
+ case SDL_BLENDFACTOR_DST_COLOR:
+ return D3D12_BLEND_DEST_COLOR;
+ case SDL_BLENDFACTOR_ONE_MINUS_DST_COLOR:
+ return D3D12_BLEND_INV_DEST_COLOR;
+ case SDL_BLENDFACTOR_DST_ALPHA:
+ return D3D12_BLEND_DEST_ALPHA;
+ case SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA:
+ return D3D12_BLEND_INV_DEST_ALPHA;
+ default:
+ return (D3D12_BLEND)0;
+ }
+}
+
+static D3D12_BLEND_OP
+GetBlendEquation(SDL_BlendOperation operation)
+{
+ switch (operation) {
+ case SDL_BLENDOPERATION_ADD:
+ return D3D12_BLEND_OP_ADD;
+ case SDL_BLENDOPERATION_SUBTRACT:
+ return D3D12_BLEND_OP_SUBTRACT;
+ case SDL_BLENDOPERATION_REV_SUBTRACT:
+ return D3D12_BLEND_OP_REV_SUBTRACT;
+ case SDL_BLENDOPERATION_MINIMUM:
+ return D3D12_BLEND_OP_MIN;
+ case SDL_BLENDOPERATION_MAXIMUM:
+ return D3D12_BLEND_OP_MAX;
+ default:
+ return (D3D12_BLEND_OP)0;
+ }
+}
+
+static void
+D3D12_CreateBlendState(SDL_Renderer * renderer, SDL_BlendMode blendMode, D3D12_BLEND_DESC * outBlendDesc)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+ SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
+ SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
+ SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
+ SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
+ SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
+ SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
+
+ SDL_zero(*outBlendDesc);
+ outBlendDesc->AlphaToCoverageEnable = FALSE;
+ outBlendDesc->IndependentBlendEnable = FALSE;
+ outBlendDesc->RenderTarget[0].BlendEnable = TRUE;
+ outBlendDesc->RenderTarget[0].SrcBlend = GetBlendFunc(srcColorFactor);
+ outBlendDesc->RenderTarget[0].DestBlend = GetBlendFunc(dstColorFactor);
+ outBlendDesc->RenderTarget[0].BlendOp = GetBlendEquation(colorOperation);
+ outBlendDesc->RenderTarget[0].SrcBlendAlpha = GetBlendFunc(srcAlphaFactor);
+ outBlendDesc->RenderTarget[0].DestBlendAlpha = GetBlendFunc(dstAlphaFactor);
+ outBlendDesc->RenderTarget[0].BlendOpAlpha = GetBlendEquation(alphaOperation);
+ outBlendDesc->RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
+}
+
+static D3D12_PipelineState *
+D3D12_CreatePipelineState(SDL_Renderer * renderer,
+ D3D12_Shader shader,
+ SDL_BlendMode blendMode,
+ D3D12_PRIMITIVE_TOPOLOGY_TYPE topology,
+ DXGI_FORMAT rtvFormat
+ )
+{
+ const D3D12_INPUT_ELEMENT_DESC vertexDesc[] = {
+ { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
+ { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
+ { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 16, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
+ };
+ D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
+ D3D12_GRAPHICS_PIPELINE_STATE_DESC pipelineDesc;
+ ID3D12PipelineState* pipelineState = NULL;
+ D3D12_PipelineState* pipelineStates;
+ HRESULT result = S_OK;
+
+ SDL_zero(pipelineDesc);
+ pipelineDesc.pRootSignature = data->rootSignatures[D3D12_GetRootSignatureType(shader)];
+ D3D12_GetVertexShader(shader, &pipelineDesc.VS);
+ D3D12_GetPixelShader(shader, &pipelineDesc.PS);
+ D3D12_CreateBlendState(renderer, blendMode, &pipelineDesc.BlendState);
+ pipelineDesc.SampleMask = 0xffffffff;
+
+ pipelineDesc.RasterizerState.AntialiasedLineEnable = FALSE;
+ pipelineDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
+ pipelineDesc.RasterizerState.DepthBias = 0;
+ pipelineDesc.RasterizerState.DepthBiasClamp = 0.0f;
+ pipelineDesc.RasterizerState.DepthClipEnable = TRUE;
+ pipelineDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
+ pipelineDesc.RasterizerState.FrontCounterClockwise = FALSE;
+ pipelineDesc.RasterizerState.MultisampleEnable = FALSE;
+ pipelineDesc.RasterizerState.SlopeScaledDepthBias = 0.0f;
+
+ pipelineDesc.InputLayout.pInputElementDescs = vertexDesc;
+ pipelineDesc.InputLayout.NumElements = 3;
+
+ pipelineDesc.PrimitiveTopologyType = topology;
+
+ pipelineDesc.NumRenderTargets = 1;
+ pipelineDesc.RTVFormats[0] = rtvFormat;
+ pipelineDesc.SampleDesc.Count = 1;
+ pipelineDesc.SampleDesc.Quality = 0;
+
+ result = ID3D12Device_CreateGraphicsPipelineState(
+ data->d3dDevice,
+ &pipelineDesc,
+ &SDL_IID_ID3D12PipelineState,
+ &pipelineState
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateGraphicsPipelineState"), result);
+ return NULL;
+ }
+
+ pipelineStates = (D3D12_PipelineState*)SDL_realloc(data->pipelineStates, (data->pipelineStateCount + 1) * sizeof(*pipelineStates));
+ if (!pipelineStates) {
+ SAFE_RELEASE(pipelineState);
+ SDL_OutOfMemory();
+ return NULL;
+ }
+
+ pipelineStates[data->pipelineStateCount].shader = shader;
+ pipelineStates[data->pipelineStateCount].blendMode = blendMode;
+ pipelineStates[data->pipelineStateCount].topology = topology;
+ pipelineStates[data->pipelineStateCount].rtvFormat = rtvFormat;
+ pipelineStates[data->pipelineStateCount].pipelineState = pipelineState;
+ data->pipelineStates = pipelineStates;
+ ++data->pipelineStateCount;
+
+ return &pipelineStates[data->pipelineStateCount - 1];
+}
+
+/* Create resources that depend on the device. */
+static HRESULT
+D3D12_CreateDeviceResources(SDL_Renderer* renderer)
+{
+ typedef HRESULT(WINAPI* PFN_CREATE_DXGI_FACTORY)(UINT flags, REFIID riid, void** ppFactory);
+ PFN_CREATE_DXGI_FACTORY CreateDXGIFactoryFunc;
+ D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
+ PFN_D3D12_CREATE_DEVICE D3D12CreateDeviceFunc;
+ ID3D12Device* d3dDevice = NULL;
+ HRESULT result = S_OK;
+ UINT creationFlags = 0;
+ int i, j, k, l;
+ SDL_bool createDebug = SDL_FALSE;
+
+ D3D12_COMMAND_QUEUE_DESC queueDesc;
+ D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc;
+ D3D12_HEAP_DESC vbufferHeapDesc;
+ D3D12_RESOURCE_DESC vbufferDesc;
+ D3D12_SAMPLER_DESC samplerDesc;
+ ID3D12DescriptorHeap *rootDescriptorHeaps[2];
+
+ const SDL_BlendMode defaultBlendModes[] = {
+ SDL_BLENDMODE_NONE,
+ SDL_BLENDMODE_BLEND,
+ SDL_BLENDMODE_ADD,
+ SDL_BLENDMODE_MOD,
+ SDL_BLENDMODE_MUL
+ };
+ const DXGI_FORMAT defaultRTVFormats[] = {
+ DXGI_FORMAT_B8G8R8A8_UNORM,
+ DXGI_FORMAT_B8G8R8X8_UNORM,
+ DXGI_FORMAT_R8_UNORM
+ };
+
+ data->hDXGIMod = SDL_LoadObject("dxgi.dll");
+ if (!data->hDXGIMod) {
+ result = E_FAIL;
+ goto done;
+ }
+
+ CreateDXGIFactoryFunc = (PFN_CREATE_DXGI_FACTORY)SDL_LoadFunction(data->hDXGIMod, "CreateDXGIFactory2");
+ if (!CreateDXGIFactoryFunc) {
+ result = E_FAIL;
+ goto done;
+ }
+
+ data->hD3D12Mod = SDL_LoadObject("D3D12.dll");
+ if (!data->hD3D12Mod) {
+ result = E_FAIL;
+ goto done;
+ }
+
+ D3D12CreateDeviceFunc = (PFN_D3D12_CREATE_DEVICE)SDL_LoadFunction(data->hD3D12Mod, "D3D12CreateDevice");
+ if (!D3D12CreateDeviceFunc) {
+ result = E_FAIL;
+ goto done;
+ }
+
+ /* See if we need debug interfaces */
+ createDebug = SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D11_DEBUG, SDL_FALSE);
+
+ if (createDebug) {
+ IDXGIInfoQueue* dxgiInfoQueue = NULL;
+ PFN_D3D12_GET_DEBUG_INTERFACE D3D12GetDebugInterfaceFunc;
+ PFN_CREATE_DXGI_FACTORY DXGIGetDebugInterfaceFunc;
+
+ D3D12GetDebugInterfaceFunc = (PFN_D3D12_GET_DEBUG_INTERFACE)SDL_LoadFunction(data->hD3D12Mod, "D3D12GetDebugInterface");
+ if (!D3D12GetDebugInterfaceFunc) {
+ result = E_FAIL;
+ goto done;
+ }
+ D3D12GetDebugInterfaceFunc(&SDL_IID_ID3D12Debug, (void**)&data->debugInterface);
+ ID3D12Debug_EnableDebugLayer(data->debugInterface);
+
+ /* If the debug hint is set, also create the DXGI factory in debug mode */
+ DXGIGetDebugInterfaceFunc = (PFN_CREATE_DXGI_FACTORY)SDL_LoadFunction(data->hDXGIMod, "DXGIGetDebugInterface1");
+ if (!DXGIGetDebugInterfaceFunc) {
+ result = E_FAIL;
+ goto done;
+ }
+
+ result = DXGIGetDebugInterfaceFunc(0, &SDL_IID_IDXGIDebug1, &data->dxgiDebug);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("DXGIGetDebugInterface1"), result);
+ goto done;
+ }
+
+ result = DXGIGetDebugInterfaceFunc(0, &SDL_IID_IDXGIInfoQueue, &dxgiInfoQueue);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("DXGIGetDebugInterface1"), result);
+ goto done;
+ }
+
+ IDXGIInfoQueue_SetBreakOnSeverity(dxgiInfoQueue, SDL_DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, TRUE);
+ IDXGIInfoQueue_SetBreakOnSeverity(dxgiInfoQueue, SDL_DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, TRUE);
+ creationFlags = DXGI_CREATE_FACTORY_DEBUG;
+ SAFE_RELEASE(dxgiInfoQueue);
+ }
+
+ result = CreateDXGIFactoryFunc(creationFlags, &SDL_IID_IDXGIFactory6, (void**)&data->dxgiFactory);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("CreateDXGIFactory"), result);
+ goto done;
+ }
+
+ /* Prefer a high performance adapter if there are multiple choices */
+ result = IDXGIFactory6_EnumAdapterByGpuPreference(data->dxgiFactory,
+ 0,
+ DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
+ &SDL_IID_IDXGIAdapter4,
+ &data->dxgiAdapter
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12CreateDevice"), result);
+ goto done;
+ }
+
+ result = D3D12CreateDeviceFunc((IUnknown *)data->dxgiAdapter,
+ D3D_FEATURE_LEVEL_11_0, /* Request minimum feature level 11.0 for maximum compatibility */
+ &SDL_IID_ID3D12Device5,
+ &d3dDevice
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("D3D12CreateDevice"), result);
+ goto done;
+ }
+
+ /* Setup the info queue if in debug mode */
+ if (createDebug) {
+ ID3D12InfoQueue *infoQueue = NULL;
+ D3D12_MESSAGE_SEVERITY severities[] = { D3D12_MESSAGE_SEVERITY_INFO };
+ D3D12_INFO_QUEUE_FILTER filter;
+
+ result = ID3D12Device_QueryInterface(d3dDevice, &SDL_IID_ID3D12InfoQueue, (void **)&infoQueue);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device to ID3D12InfoQueue"), result);
+ goto done;
+ }
+
+ SDL_zero(filter);
+ filter.DenyList.NumSeverities = 1;
+ filter.DenyList.pSeverityList = severities;
+ ID3D12InfoQueue_PushStorageFilter(infoQueue, &filter);
+
+ ID3D12InfoQueue_SetBreakOnSeverity(infoQueue, D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
+ ID3D12InfoQueue_SetBreakOnSeverity(infoQueue, D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
+
+ SAFE_RELEASE(infoQueue);
+ }
+
+ result = ID3D12Device_QueryInterface(d3dDevice, &SDL_IID_ID3D12Device5, (void **)&data->d3dDevice);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device to ID3D12Device5"), result);
+ goto done;
+ }
+
+ /* Create a command queue */
+ SDL_zero(queueDesc);
+ queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
+ queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
+
+ result = ID3D12Device_CreateCommandQueue(data->d3dDevice,
+ &queueDesc,
+ &SDL_IID_ID3D12CommandQueue,
+ &data->commandQueue
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommandQueue"), result);
+ goto done;
+ }
+
+ /* Create the descriptor heaps for the render target view, texture SRVs, and samplers */
+ SDL_zero(descriptorHeapDesc);
+ descriptorHeapDesc.NumDescriptors = SDL_D3D12_NUM_BUFFERS;
+ descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
+ result = ID3D12Device_CreateDescriptorHeap(data->d3dDevice,
+ &descriptorHeapDesc,
+ &SDL_IID_ID3D12DescriptorHeap,
+ &data->rtvDescriptorHeap
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [rtv]"), result);
+ goto done;
+ }
+ data->rtvDescriptorSize = ID3D12Device_GetDescriptorHandleIncrementSize(d3dDevice, D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
+
+ descriptorHeapDesc.NumDescriptors = SDL_D3D12_MAX_NUM_TEXTURES;
+ result = ID3D12Device_CreateDescriptorHeap(data->d3dDevice,
+ &descriptorHeapDesc,
+ &SDL_IID_ID3D12DescriptorHeap,
+ &data->textureRTVDescriptorHeap
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [texture rtv]"), result);
+ goto done;
+ }
+
+ SDL_zero(descriptorHeapDesc);
+ descriptorHeapDesc.NumDescriptors = SDL_D3D12_MAX_NUM_TEXTURES;
+ descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
+ descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
+ result = ID3D12Device_CreateDescriptorHeap(data->d3dDevice,
+ &descriptorHeapDesc,
+ &SDL_IID_ID3D12DescriptorHeap,
+ &data->srvDescriptorHeap
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [srv]"), result);
+ goto done;
+ }
+ rootDescriptorHeaps[0] = data->srvDescriptorHeap;
+ data->srvDescriptorSize = ID3D12Device_GetDescriptorHandleIncrementSize(d3dDevice, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
+
+ SDL_zero(descriptorHeapDesc);
+ descriptorHeapDesc.NumDescriptors = 2;
+ descriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
+ descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
+ result = ID3D12Device_CreateDescriptorHeap(data->d3dDevice,
+ &descriptorHeapDesc,
+ &SDL_IID_ID3D12DescriptorHeap,
+ &data->samplerDescriptorHeap
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateDescriptorHeap [sampler]"), result);
+ goto done;
+ }
+ rootDescriptorHeaps[1] = data->samplerDescriptorHeap;
+ data->samplerDescriptorSize = ID3D12Device_GetDescriptorHandleIncrementSize(d3dDevice, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
+
+ /* Create a command allocator for each back buffer */
+ for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
+ result = ID3D12Device_CreateCommandAllocator(data->d3dDevice,
+ D3D12_COMMAND_LIST_TYPE_DIRECT,
+ &SDL_IID_ID3D12CommandAllocator,
+ &data->commandAllocators[i]
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommandAllocator"), result);
+ goto done;
+ }
+ }
+
+ /* Create the command list */
+ result = ID3D12Device_CreateCommandList(data->d3dDevice,
+ 0,
+ D3D12_COMMAND_LIST_TYPE_DIRECT,
+ data->commandAllocators[0],
+ NULL,
+ &SDL_IID_ID3D12GraphicsCommandList2,
+ &data->commandList
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommandList"), result);
+ goto done;
+ }
+
+ /* Set the descriptor heaps to the correct initial value */
+ ID3D12GraphicsCommandList2_SetDescriptorHeaps(data->commandList, 2, rootDescriptorHeaps);
+
+ /* Create the fence and fence event */
+ result = ID3D12Device_CreateFence(data->d3dDevice,
+ data->fenceValue,
+ D3D12_FENCE_FLAG_NONE,
+ &SDL_IID_ID3D12Fence,
+ &data->fence
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateFence"), result);
+ goto done;
+ }
+
+ data->fenceValue++;
+
+ data->fenceEvent = CreateEventEx(NULL, NULL, 0, EVENT_MODIFY_STATE | SYNCHRONIZE);
+ if (!data->fenceEvent) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("CreateEventEx"), result);
+ goto done;
+ }
+
+ /* Create all the root signatures */
+ for (i = 0; i < NUM_ROOTSIGS; ++i) {
+ D3D12_SHADER_BYTECODE rootSigData;
+ D3D12_GetRootSignatureData(i, &rootSigData);
+ result = ID3D12Device_CreateRootSignature(data->d3dDevice,
+ 0,
+ rootSigData.pShaderBytecode,
+ rootSigData.BytecodeLength,
+ &SDL_IID_ID3D12RootSignature,
+ &data->rootSignatures[i]
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateRootSignature"), result);
+ goto done;
+ }
+ }
+
+ /* Create all the default pipeline state objects
+ (will add everything except custom blend states) */
+ for (i = 0; i < NUM_SHADERS; ++i) {
+ for (j = 0; j < SDL_arraysize(defaultBlendModes); ++j) {
+ for (k = D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT; k < D3D12_PRIMITIVE_TOPOLOGY_TYPE_PATCH; ++k) {
+ for (l = 0; l < SDL_arraysize(defaultRTVFormats); ++l) {
+ if (!D3D12_CreatePipelineState(renderer, i, defaultBlendModes[j], k, defaultRTVFormats[l])) {
+ /* D3D12_CreatePipelineState will set the SDL error, if it fails */
+ goto done;
+ }
+ }
+ }
+ }
+ }
+
+ /* Create a vertex buffer heap and populate it with resources */
+ SDL_zero(vbufferHeapDesc);
+ vbufferHeapDesc.SizeInBytes = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT * SDL_D3D12_NUM_VERTEX_BUFFERS;
+ ID3D12Device_GetCustomHeapProperties(data->d3dDevice, &vbufferHeapDesc.Properties, 0, D3D12_HEAP_TYPE_UPLOAD);
+ vbufferHeapDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ vbufferHeapDesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
+ result = ID3D12Device_CreateHeap(data->d3dDevice, &vbufferHeapDesc, &SDL_IID_ID3D12Heap, &data->vertexBufferHeap);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateHeap"), result);
+ goto done;
+ }
+
+ SDL_zero(vbufferDesc);
+ vbufferDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ vbufferDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ vbufferDesc.Width = sizeof(VertexPositionColor) * SDL_D3D12_VERTEX_BUFFER_MAX_TRIS;
+ vbufferDesc.Height = 1;
+ vbufferDesc.DepthOrArraySize = 1;
+ vbufferDesc.MipLevels = 1;
+ vbufferDesc.Format = DXGI_FORMAT_UNKNOWN;
+ vbufferDesc.SampleDesc.Count = 1;
+ vbufferDesc.SampleDesc.Quality = 0;
+ vbufferDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+ vbufferDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
+
+ for (i = 0; i < SDL_D3D12_NUM_VERTEX_BUFFERS; ++i) {
+ result = ID3D12Device_CreatePlacedResource(data->d3dDevice,
+ data->vertexBufferHeap,
+ i * D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT,
+ &vbufferDesc,
+ D3D12_RESOURCE_STATE_GENERIC_READ,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &data->vertexBuffers[i].resource
+ );
+
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreatePlacedResource [vertex buffer]"), result);
+ goto done;
+ }
+
+ data->vertexBuffers[i].view.BufferLocation = ID3D12Resource_GetGPUVirtualAddress(data->vertexBuffers[i].resource);
+ data->vertexBuffers[i].view.StrideInBytes = sizeof(VertexPositionColor);
+ }
+
+ /* Create samplers to use when drawing textures: */
+ SDL_zero(samplerDesc);
+ samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
+ samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
+ samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
+ samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
+ samplerDesc.MipLODBias = 0.0f;
+ samplerDesc.MaxAnisotropy = 1;
+ samplerDesc.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+ samplerDesc.MinLOD = 0.0f;
+ samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(data->samplerDescriptorHeap, &data->nearestPixelSampler);
+ ID3D12Device_CreateSampler(data->d3dDevice, &samplerDesc, data->nearestPixelSampler);
+
+ samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR;
+ data->linearSampler.ptr = data->nearestPixelSampler.ptr + data->samplerDescriptorSize;
+ ID3D12Device_CreateSampler(data->d3dDevice, &samplerDesc, data->linearSampler);
+
+ /* Initialize the pool allocator for SRVs */
+ for (i = 0; i < SDL_D3D12_MAX_NUM_TEXTURES; ++i) {
+ data->srvPoolNodes[i].index = (SIZE_T)i;
+ if (i != SDL_D3D12_MAX_NUM_TEXTURES - 1) {
+ data->srvPoolNodes[i].next = &data->srvPoolNodes[i + 1];
+ }
+ }
+ data->srvPoolHead = &data->srvPoolNodes[0];
+done:
+ SAFE_RELEASE(d3dDevice);
+ return result;
+}
+
+#ifdef __WIN32__
+
+static DXGI_MODE_ROTATION
+D3D12_GetCurrentRotation()
+{
+ /* FIXME */
+ return DXGI_MODE_ROTATION_IDENTITY;
+}
+
+#endif /* __WIN32__ */
+
+static BOOL
+D3D12_IsDisplayRotated90Degrees(DXGI_MODE_ROTATION rotation)
+{
+ switch (rotation) {
+ case DXGI_MODE_ROTATION_ROTATE90:
+ case DXGI_MODE_ROTATION_ROTATE270:
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+static int
+D3D12_GetRotationForCurrentRenderTarget(SDL_Renderer * renderer)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *)renderer->driverdata;
+ if (data->textureRenderTarget) {
+ return DXGI_MODE_ROTATION_IDENTITY;
+ } else {
+ return data->rotation;
+ }
+}
+
+static int
+D3D12_GetViewportAlignedD3DRect(SDL_Renderer * renderer, const SDL_Rect * sdlRect, D3D12_RECT * outRect, BOOL includeViewportOffset)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+ const int rotation = D3D12_GetRotationForCurrentRenderTarget(renderer);
+ const SDL_Rect *viewport = &data->currentViewport;
+
+ switch (rotation) {
+ case DXGI_MODE_ROTATION_IDENTITY:
+ outRect->left = sdlRect->x;
+ outRect->right = sdlRect->x + sdlRect->w;
+ outRect->top = sdlRect->y;
+ outRect->bottom = sdlRect->y + sdlRect->h;
+ if (includeViewportOffset) {
+ outRect->left += viewport->x;
+ outRect->right += viewport->x;
+ outRect->top += viewport->y;
+ outRect->bottom += viewport->y;
+ }
+ break;
+ case DXGI_MODE_ROTATION_ROTATE270:
+ outRect->left = sdlRect->y;
+ outRect->right = sdlRect->y + sdlRect->h;
+ outRect->top = viewport->w - sdlRect->x - sdlRect->w;
+ outRect->bottom = viewport->w - sdlRect->x;
+ break;
+ case DXGI_MODE_ROTATION_ROTATE180:
+ outRect->left = viewport->w - sdlRect->x - sdlRect->w;
+ outRect->right = viewport->w - sdlRect->x;
+ outRect->top = viewport->h - sdlRect->y - sdlRect->h;
+ outRect->bottom = viewport->h - sdlRect->y;
+ break;
+ case DXGI_MODE_ROTATION_ROTATE90:
+ outRect->left = viewport->h - sdlRect->y - sdlRect->h;
+ outRect->right = viewport->h - sdlRect->y;
+ outRect->top = sdlRect->x;
+ outRect->bottom = sdlRect->x + sdlRect->h;
+ break;
+ default:
+ return SDL_SetError("The physical display is in an unknown or unsupported rotation");
+ }
+ return 0;
+}
+
+static HRESULT
+D3D12_CreateSwapChain(SDL_Renderer * renderer, int w, int h)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *)renderer->driverdata;
+ IDXGISwapChain1* swapChain;
+ HRESULT result = S_OK;
+
+ /* Create a swap chain using the same adapter as the existing Direct3D device. */
+ DXGI_SWAP_CHAIN_DESC1 swapChainDesc;
+ SDL_zero(swapChainDesc);
+ swapChainDesc.Width = w;
+ swapChainDesc.Height = h;
+ swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; /* This is the most common swap chain format. */
+ swapChainDesc.Stereo = FALSE;
+ swapChainDesc.SampleDesc.Count = 1; /* Don't use multi-sampling. */
+ swapChainDesc.SampleDesc.Quality = 0;
+ swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+ swapChainDesc.BufferCount = 2; /* Use double-buffering to minimize latency. */
+ if (WIN_IsWindows8OrGreater()) {
+ swapChainDesc.Scaling = DXGI_SCALING_NONE;
+ } else {
+ swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
+ }
+ swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; /* All Windows Store apps must use this SwapEffect. */
+ swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT | /* To support SetMaximumFrameLatency */
+ DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING; /* To support presenting with allow tearing on */
+
+ SDL_SysWMinfo windowinfo;
+ SDL_VERSION(&windowinfo.version);
+ SDL_GetWindowWMInfo(renderer->window, &windowinfo);
+
+ result = IDXGIFactory6_CreateSwapChainForHwnd(data->dxgiFactory,
+ (IUnknown *)data->commandQueue,
+ windowinfo.info.win.window,
+ &swapChainDesc,
+ NULL,
+ NULL, /* Allow on all displays. */
+ &swapChain
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGIFactory2::CreateSwapChainForHwnd"), result);
+ goto done;
+ }
+
+ IDXGIFactory_MakeWindowAssociation(data->dxgiFactory, windowinfo.info.win.window, DXGI_MWA_NO_WINDOW_CHANGES);
+
+ result = IDXGISwapChain1_QueryInterface(swapChain, &SDL_IID_IDXGISwapChain4, &data->swapChain);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain1::QueryInterface"), result);
+ goto done;
+ }
+
+ /* Ensure that the swapchain does not queue more than one frame at a time. This both reduces latency
+ * and ensures that the application will only render after each VSync, minimizing power consumption.
+ */
+ result = IDXGISwapChain4_SetMaximumFrameLatency(data->swapChain, 1);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain4::SetMaximumFrameLatency"), result);
+ goto done;
+ }
+
+ data->swapEffect = swapChainDesc.SwapEffect;
+
+done:
+ SAFE_RELEASE(swapChain);
+ return result;
+}
+
+static HRESULT D3D12_UpdateForWindowSizeChange(SDL_Renderer * renderer);
+
+
+HRESULT
+D3D12_HandleDeviceLost(SDL_Renderer * renderer)
+{
+ HRESULT result = S_OK;
+
+ D3D12_ReleaseAll(renderer);
+
+ result = D3D12_CreateDeviceResources(renderer);
+ if (FAILED(result)) {
+ /* D3D12_CreateDeviceResources will set the SDL error */
+ return result;
+ }
+
+ result = D3D12_UpdateForWindowSizeChange(renderer);
+ if (FAILED(result)) {
+ /* D3D12_UpdateForWindowSizeChange will set the SDL error */
+ return result;
+ }
+
+ /* Let the application know that the device has been reset */
+ {
+ SDL_Event event;
+ event.type = SDL_RENDER_DEVICE_RESET;
+ SDL_PushEvent(&event);
+ }
+
+ return S_OK;
+}
+
+/* Initialize all resources that change when the window's size changes. */
+static HRESULT
+D3D12_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *)renderer->driverdata;
+ HRESULT result = S_OK;
+ int i, w, h;
+
+ D3D12_RENDER_TARGET_VIEW_DESC rtvDesc;
+ D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor;
+
+ /* Release render targets */
+ for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
+ SAFE_RELEASE(data->renderTargets[i]);
+ }
+ /* The width and height of the swap chain must be based on the display's
+ * non-rotated size.
+ */
+ SDL_GetWindowSize(renderer->window, &w, &h);
+ data->rotation = D3D12_GetCurrentRotation();
+ if (D3D12_IsDisplayRotated90Degrees(data->rotation)) {
+ int tmp = w;
+ w = h;
+ h = tmp;
+ }
+
+ if (data->swapChain) {
+ /* If the swap chain already exists, resize it. */
+ result = IDXGISwapChain_ResizeBuffers(data->swapChain,
+ 0,
+ w, h,
+ DXGI_FORMAT_UNKNOWN,
+ 0
+ );
+ if (result == DXGI_ERROR_DEVICE_REMOVED) {
+ /* If the device was removed for any reason, a new device and swap chain will need to be created. */
+ D3D12_HandleDeviceLost(renderer);
+
+ /* Everything is set up now. Do not continue execution of this method. HandleDeviceLost will reenter this method
+ * and correctly set up the new device.
+ */
+ goto done;
+ } else if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::ResizeBuffers"), result);
+ goto done;
+ }
+ } else {
+ result = D3D12_CreateSwapChain(renderer, w, h);
+ if (FAILED(result)) {
+ goto done;
+ }
+ }
+
+ /* Set the proper rotation for the swap chain. */
+ if (WIN_IsWindows8OrGreater()) {
+ if (data->swapEffect == DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL) {
+ result = IDXGISwapChain4_SetRotation(data->swapChain, data->rotation);
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain4::SetRotation"), result);
+ goto done;
+ }
+ }
+ }
+
+ /* Get each back buffer render target and create render target views */
+ for (i = 0; i < SDL_D3D12_NUM_BUFFERS; ++i) {
+ result = IDXGISwapChain4_GetBuffer(
+ data->swapChain,
+ i,
+ &SDL_IID_ID3D12Resource,
+ &data->renderTargets[i]
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain4::GetBuffer"), result);
+ goto done;
+ }
+
+ SDL_zero(rtvDesc);
+ rtvDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
+ rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
+
+ SDL_zero(rtvDescriptor);
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(data->rtvDescriptorHeap, &rtvDescriptor);
+ rtvDescriptor.ptr += i * data->rtvDescriptorSize;
+ ID3D12Device5_CreateRenderTargetView(data->d3dDevice, data->renderTargets[i], &rtvDesc, rtvDescriptor);
+ }
+
+ /* Set back buffer index to current buffer */
+ data->currentBackBufferIndex = IDXGISwapChain4_GetCurrentBackBufferIndex(data->swapChain);
+
+ /* Set the swap chain target immediately, so that a target is always set
+ * even before we get to SetDrawState. Without this it's possible to hit
+ * null references in places like ReadPixels!
+ */
+ data->currentRenderTargetView = D3D12_GetCurrentRenderTargetView(renderer);
+ ID3D12GraphicsCommandList2_OMSetRenderTargets(data->commandList, 1, &data->currentRenderTargetView, FALSE, NULL);
+ D3D12_TransitionResource(data,
+ data->renderTargets[data->currentBackBufferIndex],
+ D3D12_RESOURCE_STATE_PRESENT,
+ D3D12_RESOURCE_STATE_RENDER_TARGET
+ );
+
+ data->viewportDirty = SDL_TRUE;
+
+done:
+ return result;
+}
+
+/* This method is called when the window's size changes. */
+static HRESULT
+D3D12_UpdateForWindowSizeChange(SDL_Renderer * renderer)
+{
+ D3D12_RenderData* data = (D3D12_RenderData*)renderer->driverdata;
+ /* If the GPU has previous work, wait for it to be done first */
+ D3D12_WaitForGPU(data);
+ return D3D12_CreateWindowSizeDependentResources(renderer);
+}
+
+static void
+D3D12_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
+{
+ if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+ D3D12_UpdateForWindowSizeChange(renderer);
+ }
+}
+
+static SDL_bool
+D3D12_SupportsBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
+{
+ SDL_BlendFactor srcColorFactor = SDL_GetBlendModeSrcColorFactor(blendMode);
+ SDL_BlendFactor srcAlphaFactor = SDL_GetBlendModeSrcAlphaFactor(blendMode);
+ SDL_BlendOperation colorOperation = SDL_GetBlendModeColorOperation(blendMode);
+ SDL_BlendFactor dstColorFactor = SDL_GetBlendModeDstColorFactor(blendMode);
+ SDL_BlendFactor dstAlphaFactor = SDL_GetBlendModeDstAlphaFactor(blendMode);
+ SDL_BlendOperation alphaOperation = SDL_GetBlendModeAlphaOperation(blendMode);
+
+ if (!GetBlendFunc(srcColorFactor) || !GetBlendFunc(srcAlphaFactor) ||
+ !GetBlendEquation(colorOperation) ||
+ !GetBlendFunc(dstColorFactor) || !GetBlendFunc(dstAlphaFactor) ||
+ !GetBlendEquation(alphaOperation)) {
+ return SDL_FALSE;
+ }
+ return SDL_TRUE;
+}
+
+static SIZE_T
+D3D12_GetAvailableSRVIndex(SDL_Renderer * renderer)
+{
+ D3D12_RenderData* rendererData = (D3D12_RenderData*)renderer->driverdata;
+ if (rendererData->srvPoolHead) {
+ SIZE_T index = rendererData->srvPoolHead->index;
+ rendererData->srvPoolHead = rendererData->srvPoolHead->next;
+ return index;
+ } else {
+ SDL_SetError("[d3d12] Cannot allocate more than %d textures!", SDL_D3D12_MAX_NUM_TEXTURES);
+ return SDL_D3D12_MAX_NUM_TEXTURES + 1;
+ }
+}
+
+static void
+D3D12_FreeSRVIndex(SDL_Renderer * renderer, SIZE_T index)
+{
+ D3D12_RenderData* rendererData = (D3D12_RenderData*)renderer->driverdata;
+ rendererData->srvPoolNodes[index].next = rendererData->srvPoolHead;
+ rendererData->srvPoolHead = &rendererData->srvPoolNodes[index];
+}
+
+static int
+D3D12_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ D3D12_TextureData *textureData;
+ HRESULT result;
+ DXGI_FORMAT textureFormat = SDLPixelFormatToDXGIFormat(texture->format);
+ D3D12_RESOURCE_DESC textureDesc;
+ D3D12_HEAP_PROPERTIES heapProps;
+ D3D12_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
+
+ if (textureFormat == DXGI_FORMAT_UNKNOWN) {
+ return SDL_SetError("%s, An unsupported SDL pixel format (0x%x) was specified",
+ __FUNCTION__, texture->format);
+ }
+
+ textureData = (D3D12_TextureData*) SDL_calloc(1, sizeof(*textureData));
+ if (!textureData) {
+ SDL_OutOfMemory();
+ return -1;
+ }
+ textureData->scaleMode = (texture->scaleMode == SDL_ScaleModeNearest) ? D3D12_FILTER_MIN_MAG_MIP_POINT : D3D12_FILTER_MIN_MAG_MIP_LINEAR;
+
+ texture->driverdata = textureData;
+ textureData->mainTextureFormat = textureFormat;
+
+ SDL_zero(textureDesc);
+ textureDesc.Width = texture->w;
+ textureDesc.Height = texture->h;
+ textureDesc.MipLevels = 1;
+ textureDesc.DepthOrArraySize = 1;
+ textureDesc.Format = textureFormat;
+ textureDesc.SampleDesc.Count = 1;
+ textureDesc.SampleDesc.Quality = 0;
+ textureDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
+ textureDesc.Flags = 0;
+
+ if (texture->access == SDL_TEXTUREACCESS_TARGET) {
+ textureDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
+ }
+
+ SDL_zero(heapProps);
+ heapProps.Type = D3D12_HEAP_TYPE_DEFAULT;
+ heapProps.CreationNodeMask = 1;
+ heapProps.VisibleNodeMask = 1;
+
+ result = ID3D12Device_CreateCommittedResource(rendererData->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &textureDesc,
+ D3D12_RESOURCE_STATE_COPY_DEST,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &textureData->mainTexture
+ );
+ textureData->mainResourceState = D3D12_RESOURCE_STATE_COPY_DEST;
+ if (FAILED(result)) {
+ D3D12_DestroyTexture(renderer, texture);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [texture]"), result);
+ }
+#if SDL_HAVE_YUV
+ if (texture->format == SDL_PIXELFORMAT_YV12 ||
+ texture->format == SDL_PIXELFORMAT_IYUV) {
+ textureData->yuv = SDL_TRUE;
+
+ textureDesc.Width = (textureDesc.Width + 1) / 2;
+ textureDesc.Height = (textureDesc.Height + 1) / 2;
+
+ result = ID3D12Device_CreateCommittedResource(rendererData->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &textureDesc,
+ D3D12_RESOURCE_STATE_COPY_DEST,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &textureData->mainTextureU
+ );
+ textureData->mainResourceStateU = D3D12_RESOURCE_STATE_COPY_DEST;
+ if (FAILED(result)) {
+ D3D12_DestroyTexture(renderer, texture);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [texture]"), result);
+ }
+
+ result = ID3D12Device_CreateCommittedResource(rendererData->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &textureDesc,
+ D3D12_RESOURCE_STATE_COPY_DEST,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &textureData->mainTextureV
+ );
+ textureData->mainResourceStateV = D3D12_RESOURCE_STATE_COPY_DEST;
+ if (FAILED(result)) {
+ D3D12_DestroyTexture(renderer, texture);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [texture]"), result);
+ }
+ }
+
+ if (texture->format == SDL_PIXELFORMAT_NV12 ||
+ texture->format == SDL_PIXELFORMAT_NV21) {
+ D3D12_RESOURCE_DESC nvTextureDesc = textureDesc;
+
+ textureData->nv12 = SDL_TRUE;
+
+ nvTextureDesc.Format = DXGI_FORMAT_R8G8_UNORM;
+ nvTextureDesc.Width = (textureDesc.Width + 1) / 2;
+ nvTextureDesc.Height = (textureDesc.Height + 1) / 2;
+
+ result = ID3D12Device_CreateCommittedResource(rendererData->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &nvTextureDesc,
+ D3D12_RESOURCE_STATE_COPY_DEST,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &textureData->mainTextureNV
+ );
+ textureData->mainResourceStateNV = D3D12_RESOURCE_STATE_COPY_DEST;
+ if (FAILED(result)) {
+ D3D12_DestroyTexture(renderer, texture);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device1::CreateTexture2D"), result);
+ }
+ }
+#endif /* SDL_HAVE_YUV */
+ SDL_zero(resourceViewDesc);
+ resourceViewDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
+ resourceViewDesc.Format = textureDesc.Format;
+ resourceViewDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
+ resourceViewDesc.Texture2D.MostDetailedMip = 0;
+ resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
+
+ textureData->mainSRVIndex = D3D12_GetAvailableSRVIndex(renderer);
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(rendererData->srvDescriptorHeap, &textureData->mainTextureResourceView);
+ textureData->mainTextureResourceView.ptr += textureData->mainSRVIndex * rendererData->srvDescriptorSize;
+
+ ID3D12Device_CreateShaderResourceView(rendererData->d3dDevice,
+ textureData->mainTexture,
+ &resourceViewDesc,
+ textureData->mainTextureResourceView
+ );
+#if SDL_HAVE_YUV
+ if (textureData->yuv) {
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(rendererData->srvDescriptorHeap, &textureData->mainTextureResourceViewU);
+ textureData->mainSRVIndexU = D3D12_GetAvailableSRVIndex(renderer);
+ textureData->mainTextureResourceViewU.ptr += textureData->mainSRVIndexU * rendererData->srvDescriptorSize;
+ ID3D12Device_CreateShaderResourceView(rendererData->d3dDevice,
+ textureData->mainTextureU,
+ &resourceViewDesc,
+ textureData->mainTextureResourceViewU
+ );
+
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(rendererData->srvDescriptorHeap, &textureData->mainTextureResourceViewV);
+ textureData->mainSRVIndexV = D3D12_GetAvailableSRVIndex(renderer);
+ textureData->mainTextureResourceViewV.ptr += textureData->mainSRVIndexV * rendererData->srvDescriptorSize;
+ ID3D12Device_CreateShaderResourceView(rendererData->d3dDevice,
+ textureData->mainTextureV,
+ &resourceViewDesc,
+ textureData->mainTextureResourceViewV
+ );
+ }
+
+ if (textureData->nv12) {
+ D3D12_SHADER_RESOURCE_VIEW_DESC nvResourceViewDesc = resourceViewDesc;
+
+ nvResourceViewDesc.Format = DXGI_FORMAT_R8G8_UNORM;
+
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(rendererData->srvDescriptorHeap, &textureData->mainTextureResourceViewNV);
+ textureData->mainSRVIndexNV = D3D12_GetAvailableSRVIndex(renderer);
+ textureData->mainTextureResourceViewNV.ptr += textureData->mainSRVIndexNV * rendererData->srvDescriptorSize;
+ ID3D12Device_CreateShaderResourceView(rendererData->d3dDevice,
+ textureData->mainTextureNV,
+ &nvResourceViewDesc,
+ textureData->mainTextureResourceViewNV
+ );
+ }
+#endif /* SDL_HAVE_YUV */
+
+ if (texture->access & SDL_TEXTUREACCESS_TARGET) {
+ D3D12_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
+ renderTargetViewDesc.Format = textureDesc.Format;
+ renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
+ renderTargetViewDesc.Texture2D.MipSlice = 0;
+
+ ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(rendererData->textureRTVDescriptorHeap, &textureData->mainTextureRenderTargetView);
+ textureData->mainTextureRenderTargetView.ptr += textureData->mainSRVIndex * rendererData->rtvDescriptorSize;
+
+ ID3D12Device_CreateRenderTargetView(rendererData->d3dDevice,
+ (ID3D12Resource*)textureData->mainTexture,
+ &renderTargetViewDesc,
+ textureData->mainTextureRenderTargetView);
+ }
+
+ return 0;
+}
+
+static void
+D3D12_DestroyTexture(SDL_Renderer * renderer,
+ SDL_Texture * texture)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData*)renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
+
+ if (!textureData) {
+ return;
+ }
+
+ /* Because SDL_DestroyTexture might be called while the data is in-flight, we need to issue the batch first
+ Unfortunately, this means that deleting a lot of textures mid-frame will have poor performance. */
+ D3D12_IssueBatch(rendererData);
+
+ SAFE_RELEASE(textureData->mainTexture);
+ SAFE_RELEASE(textureData->stagingBuffer);
+ D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndex);
+#if SDL_HAVE_YUV
+ SAFE_RELEASE(textureData->mainTextureU);
+ SAFE_RELEASE(textureData->mainTextureV);
+ if (textureData->yuv) {
+ D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexU);
+ D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexV);
+ }
+ SAFE_RELEASE(textureData->mainTextureNV);
+ if (textureData->yuv) {
+ D3D12_FreeSRVIndex(renderer, textureData->mainSRVIndexNV);
+ }
+ SDL_free(textureData->pixels);
+#endif
+ SDL_free(textureData);
+ texture->driverdata = NULL;
+}
+
+static int
+D3D12_UpdateTextureInternal(D3D12_RenderData * rendererData, ID3D12Resource * texture, int bpp, int x, int y, int w, int h, const void *pixels, int pitch, D3D12_RESOURCE_STATES *resourceState)
+{
+ const Uint8 *src;
+ Uint8 *dst;
+ int row;
+ UINT length;
+ HRESULT result;
+ D3D12_RESOURCE_DESC textureDesc;
+ D3D12_RESOURCE_DESC uploadDesc;
+ D3D12_HEAP_PROPERTIES heapProps;
+ D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
+ D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTextureDesc;
+ D3D12_TEXTURE_COPY_LOCATION srcLocation;
+ D3D12_TEXTURE_COPY_LOCATION dstLocation;
+ BYTE *textureMemory;
+
+ /* Create an upload buffer, which will be used to write to the main texture. */
+ ID3D12Resource_GetDesc(texture, &textureDesc);
+ textureDesc.Width = w;
+ textureDesc.Height = h;
+
+ SDL_zero(uploadDesc);
+ uploadDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ uploadDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ uploadDesc.Height = 1;
+ uploadDesc.DepthOrArraySize = 1;
+ uploadDesc.MipLevels = 1;
+ uploadDesc.Format = DXGI_FORMAT_UNKNOWN;
+ uploadDesc.SampleDesc.Count = 1;
+ uploadDesc.SampleDesc.Quality = 0;
+ uploadDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+ uploadDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
+
+ /* Figure out how much we need to allocate for the upload buffer */
+ ID3D12Device_GetCopyableFootprints(rendererData->d3dDevice,
+ &textureDesc,
+ 0,
+ 1,
+ 0,
+ NULL,
+ NULL,
+ NULL,
+ &uploadDesc.Width
+ );
+
+ SDL_zero(heapProps);
+ heapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
+ heapProps.CreationNodeMask = 1;
+ heapProps.VisibleNodeMask = 1;
+
+ /* Create the upload buffer */
+ result = ID3D12Device_CreateCommittedResource(rendererData->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &uploadDesc,
+ D3D12_RESOURCE_STATE_GENERIC_READ,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &rendererData->uploadBuffers[rendererData->currentUploadBuffer]
+ );
+ if (FAILED(result)) {
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [create upload buffer]"), result);
+ }
+
+ /* Get a write-only pointer to data in the upload buffer: */
+ result = ID3D12Resource_Map(rendererData->uploadBuffers[rendererData->currentUploadBuffer],
+ 0,
+ NULL,
+ &textureMemory
+ );
+ if (FAILED(result)) {
+ SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
+ }
+
+ SDL_zero(pitchedDesc);
+ pitchedDesc.Format = textureDesc.Format;
+ pitchedDesc.Width = w;
+ pitchedDesc.Height = h;
+ pitchedDesc.Depth = 1;
+ pitchedDesc.RowPitch = D3D12_Align(w * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
+
+ SDL_zero(placedTextureDesc);
+ placedTextureDesc.Offset = 0;
+ placedTextureDesc.Footprint = pitchedDesc;
+
+ src = (const Uint8 *)pixels;
+ dst = textureMemory;
+ length = w * bpp;
+ if (length == pitch && length == pitchedDesc.RowPitch) {
+ SDL_memcpy(dst, src, length*h);
+ } else {
+ if (length > (UINT)pitch) {
+ length = pitch;
+ }
+ if (length > pitchedDesc.RowPitch) {
+ length = pitchedDesc.RowPitch;
+ }
+ for (row = 0; row < h; ++row) {
+ SDL_memcpy(dst, src, length);
+ src += pitch;
+ dst += pitchedDesc.RowPitch;
+ }
+ }
+
+ /* Commit the changes back to the upload buffer: */
+ ID3D12Resource_Unmap(rendererData->uploadBuffers[rendererData->currentUploadBuffer], 0, NULL);
+
+ /* Make sure the destination is in the correct resource state */
+ D3D12_TransitionResource(rendererData, texture, *resourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ *resourceState = D3D12_RESOURCE_STATE_COPY_DEST;
+
+ SDL_zero(dstLocation);
+ dstLocation.pResource = texture;
+ dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
+ dstLocation.SubresourceIndex = 0;
+
+ SDL_zero(srcLocation);
+ srcLocation.pResource = rendererData->uploadBuffers[rendererData->currentUploadBuffer];
+ srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
+ srcLocation.PlacedFootprint = placedTextureDesc;
+
+ ID3D12GraphicsCommandList_CopyTextureRegion(rendererData->commandList,
+ &dstLocation,
+ x,
+ y,
+ 0,
+ &srcLocation,
+ NULL
+ );
+
+ /* Transition the texture to be shader accessible */
+ D3D12_TransitionResource(rendererData, texture, *resourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ *resourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+
+ rendererData->currentUploadBuffer++;
+ /* If we've used up all the upload buffers, we need to issue the batch */
+ if (rendererData->currentUploadBuffer == SDL_D3D12_NUM_UPLOAD_BUFFERS) {
+ D3D12_IssueBatch(rendererData);
+ }
+
+ return 0;
+}
+
+static int
+D3D12_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect, const void * srcPixels,
+ int srcPitch)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
+
+ if (!textureData) {
+ return SDL_SetError("Texture is not currently available");
+ }
+
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, srcPixels, srcPitch, &textureData->mainResourceState) < 0) {
+ return -1;
+ }
+#if SDL_HAVE_YUV
+ if (textureData->yuv) {
+ /* Skip to the correct offset into the next texture */
+ srcPixels = (const void*)((const Uint8*)srcPixels + rect->h * srcPitch);
+
+ if (D3D12_UpdateTextureInternal(rendererData, texture->format == SDL_PIXELFORMAT_YV12 ? textureData->mainTextureV : textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2, texture->format == SDL_PIXELFORMAT_YV12 ? &textureData->mainResourceStateV : &textureData->mainResourceStateU) < 0) {
+ return -1;
+ }
+
+ /* Skip to the correct offset into the next texture */
+ srcPixels = (const void*)((const Uint8*)srcPixels + ((rect->h + 1) / 2) * ((srcPitch + 1) / 2));
+ if (D3D12_UpdateTextureInternal(rendererData, texture->format == SDL_PIXELFORMAT_YV12 ? textureData->mainTextureU : textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, (rect->w + 1) / 2, (rect->h + 1) / 2, srcPixels, (srcPitch + 1) / 2, texture->format == SDL_PIXELFORMAT_YV12 ? &textureData->mainResourceStateU : &textureData->mainResourceStateV) < 0) {
+ return -1;
+ }
+ }
+
+ if (textureData->nv12) {
+ /* Skip to the correct offset into the next texture */
+ srcPixels = (const void*)((const Uint8*)srcPixels + rect->h * srcPitch);
+
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureNV, 2, rect->x / 2, rect->y / 2, ((rect->w + 1) / 2), (rect->h + 1) / 2, srcPixels, 2*((srcPitch + 1) / 2), &textureData->mainResourceStateNV) < 0) {
+ return -1;
+ }
+ }
+#endif /* SDL_HAVE_YUV */
+ return 0;
+}
+
+#if SDL_HAVE_YUV
+static int
+D3D12_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *Uplane, int Upitch,
+ const Uint8 *Vplane, int Vpitch)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
+
+ if (!textureData) {
+ return SDL_SetError("Texture is not currently available");
+ }
+
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState) < 0) {
+ return -1;
+ }
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureU, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Uplane, Upitch, &textureData->mainResourceStateU) < 0) {
+ return -1;
+ }
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureV, SDL_BYTESPERPIXEL(texture->format), rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, Vplane, Vpitch, &textureData->mainResourceStateV) < 0) {
+ return -1;
+ }
+ return 0;
+}
+
+static int
+D3D12_UpdateTextureNV(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect,
+ const Uint8 *Yplane, int Ypitch,
+ const Uint8 *UVplane, int UVpitch)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *)texture->driverdata;
+
+ if (!textureData) {
+ return SDL_SetError("Texture is not currently available");
+ }
+
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTexture, SDL_BYTESPERPIXEL(texture->format), rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch, &textureData->mainResourceState) < 0) {
+ return -1;
+ }
+
+ if (D3D12_UpdateTextureInternal(rendererData, textureData->mainTextureNV, 2, rect->x / 2, rect->y / 2, ((rect->w + 1) / 2), (rect->h + 1) / 2, UVplane, UVpitch, &textureData->mainResourceStateNV) < 0) {
+ return -1;
+ }
+ return 0;
+}
+#endif
+
+static int
+D3D12_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
+ const SDL_Rect * rect, void **pixels, int *pitch)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
+ HRESULT result = S_OK;
+
+ D3D12_RESOURCE_DESC textureDesc;
+ D3D12_RESOURCE_DESC uploadDesc;
+ D3D12_HEAP_PROPERTIES heapProps;
+ D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
+ BYTE *textureMemory;
+ int bpp;
+
+ if (!textureData) {
+ return SDL_SetError("Texture is not currently available");
+ }
+#if SDL_HAVE_YUV
+ if (textureData->yuv || textureData->nv12) {
+ /* It's more efficient to upload directly... */
+ if (!textureData->pixels) {
+ textureData->pitch = texture->w;
+ textureData->pixels = (Uint8 *)SDL_malloc((texture->h * textureData->pitch * 3) / 2);
+ if (!textureData->pixels) {
+ return SDL_OutOfMemory();
+ }
+ }
+ textureData->lockedRect = *rect;
+ *pixels =
+ (void *)((Uint8 *)textureData->pixels + rect->y * textureData->pitch +
+ rect->x * SDL_BYTESPERPIXEL(texture->format));
+ *pitch = textureData->pitch;
+ return 0;
+ }
+#endif
+ if (textureData->stagingBuffer) {
+ return SDL_SetError("texture is already locked");
+ }
+
+ /* Create an upload buffer, which will be used to write to the main texture. */
+ ID3D12Resource_GetDesc(textureData->mainTexture, &textureDesc);
+ textureDesc.Width = rect->w;
+ textureDesc.Height = rect->h;
+
+ SDL_zero(uploadDesc);
+ uploadDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ uploadDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ uploadDesc.Height = 1;
+ uploadDesc.DepthOrArraySize = 1;
+ uploadDesc.MipLevels = 1;
+ uploadDesc.Format = DXGI_FORMAT_UNKNOWN;
+ uploadDesc.SampleDesc.Count = 1;
+ uploadDesc.SampleDesc.Quality = 0;
+ uploadDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+ uploadDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
+
+ /* Figure out how much we need to allocate for the upload buffer */
+ ID3D12Device_GetCopyableFootprints(rendererData->d3dDevice,
+ &textureDesc,
+ 0,
+ 1,
+ 0,
+ NULL,
+ NULL,
+ NULL,
+ &uploadDesc.Width
+ );
+
+ SDL_zero(heapProps);
+ heapProps.Type = D3D12_HEAP_TYPE_UPLOAD;
+ heapProps.CreationNodeMask = 1;
+ heapProps.VisibleNodeMask = 1;
+
+ /* Create the upload buffer */
+ result = ID3D12Device_CreateCommittedResource(rendererData->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &uploadDesc,
+ D3D12_RESOURCE_STATE_GENERIC_READ,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &textureData->stagingBuffer
+ );
+ if (FAILED(result)) {
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device::CreateCommittedResource [create upload buffer]"), result);
+ }
+
+ /* Get a write-only pointer to data in the upload buffer: */
+ result = ID3D12Resource_Map(textureData->stagingBuffer,
+ 0,
+ NULL,
+ &textureMemory
+ );
+ if (FAILED(result)) {
+ SAFE_RELEASE(rendererData->uploadBuffers[rendererData->currentUploadBuffer]);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
+ }
+
+ SDL_zero(pitchedDesc);
+ pitchedDesc.Format = textureDesc.Format;
+ pitchedDesc.Width = rect->w;
+ pitchedDesc.Height = rect->h;
+ pitchedDesc.Depth = 1;
+ if (pitchedDesc.Format == DXGI_FORMAT_R8_UNORM) {
+ bpp = 1;
+ }
+ else {
+ bpp = 4;
+ }
+ pitchedDesc.RowPitch = D3D12_Align(rect->w * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
+
+ /* Make note of where the staging texture will be written to
+ * (on a call to SDL_UnlockTexture):
+ */
+ textureData->lockedRect = *rect;
+
+ /* Make sure the caller has information on the texture's pixel buffer,
+ * then return:
+ */
+ *pixels = textureMemory;
+ *pitch = pitchedDesc.RowPitch;
+ return 0;
+}
+
+static void
+D3D12_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
+
+ D3D12_RESOURCE_DESC textureDesc;
+ D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
+ D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTextureDesc;
+ D3D12_TEXTURE_COPY_LOCATION srcLocation;
+ D3D12_TEXTURE_COPY_LOCATION dstLocation;
+ int bpp;
+
+ if (!textureData) {
+ return;
+ }
+#if SDL_HAVE_YUV
+ if (textureData->yuv || textureData->nv12) {
+ const SDL_Rect *rect = &textureData->lockedRect;
+ void *pixels =
+ (void *) ((Uint8 *) textureData->pixels + rect->y * textureData->pitch +
+ rect->x * SDL_BYTESPERPIXEL(texture->format));
+ D3D12_UpdateTexture(renderer, texture, rect, pixels, textureData->pitch);
+ return;
+ }
+#endif
+ /* Commit the pixel buffer's changes back to the staging texture: */
+ ID3D12Resource_Unmap(textureData->stagingBuffer, 0, NULL);
+
+ ID3D12Resource_GetDesc(textureData->mainTexture, &textureDesc);
+ textureDesc.Width = textureData->lockedRect.w;
+ textureDesc.Height = textureData->lockedRect.h;
+
+ SDL_zero(pitchedDesc);
+ pitchedDesc.Format = textureDesc.Format;
+ pitchedDesc.Width = (UINT)textureDesc.Width;
+ pitchedDesc.Height = textureDesc.Height;
+ pitchedDesc.Depth = 1;
+ if (pitchedDesc.Format == DXGI_FORMAT_R8_UNORM) {
+ bpp = 1;
+ }
+ else {
+ bpp = 4;
+ }
+ pitchedDesc.RowPitch = D3D12_Align(textureData->lockedRect.w * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
+
+ SDL_zero(placedTextureDesc);
+ placedTextureDesc.Offset = 0;
+ placedTextureDesc.Footprint = pitchedDesc;
+
+ D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_COPY_DEST);
+ textureData->mainResourceState = D3D12_RESOURCE_STATE_COPY_DEST;
+
+ SDL_zero(dstLocation);
+ dstLocation.pResource = textureData->mainTexture;
+ dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
+ dstLocation.SubresourceIndex = 0;
+
+ SDL_zero(srcLocation);
+ srcLocation.pResource = textureData->stagingBuffer;
+ srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
+ srcLocation.PlacedFootprint = placedTextureDesc;
+
+ ID3D12GraphicsCommandList_CopyTextureRegion(rendererData->commandList,
+ &dstLocation,
+ textureData->lockedRect.x,
+ textureData->lockedRect.y,
+ 0,
+ &srcLocation,
+ NULL
+ );
+
+ /* Transition the texture to be shader accessible */
+ D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+
+ /* Execute the command list before releasing the staging buffer */
+ D3D12_IssueBatch(rendererData);
+ SAFE_RELEASE(textureData->stagingBuffer);
+}
+
+static void
+D3D12_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture, SDL_ScaleMode scaleMode)
+{
+ D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
+
+ if (!textureData) {
+ return;
+ }
+
+ textureData->scaleMode = (scaleMode == SDL_ScaleModeNearest) ? D3D12_FILTER_MIN_MAG_MIP_POINT : D3D12_FILTER_MIN_MAG_MIP_LINEAR;
+}
+
+static int
+D3D12_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ D3D12_TextureData *textureData = NULL;
+
+ if (texture == NULL) {
+ if (rendererData->textureRenderTarget) {
+ D3D12_TransitionResource(rendererData,
+ rendererData->textureRenderTarget->mainTexture,
+ rendererData->textureRenderTarget->mainResourceState,
+ D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE
+ );
+ rendererData->textureRenderTarget->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ }
+ rendererData->textureRenderTarget = NULL;
+ return 0;
+ }
+
+ textureData = (D3D12_TextureData *) texture->driverdata;
+
+ if (!textureData->mainTextureRenderTargetView.ptr) {
+ return SDL_SetError("specified texture is not a render target");
+ }
+
+ rendererData->textureRenderTarget = textureData;
+ D3D12_TransitionResource(rendererData,
+ rendererData->textureRenderTarget->mainTexture,
+ rendererData->textureRenderTarget->mainResourceState,
+ D3D12_RESOURCE_STATE_RENDER_TARGET
+ );
+ rendererData->textureRenderTarget->mainResourceState = D3D12_RESOURCE_STATE_RENDER_TARGET;
+
+
+ return 0;
+}
+
+static int
+D3D12_QueueSetViewport(SDL_Renderer * renderer, SDL_RenderCommand *cmd)
+{
+ return 0; /* nothing to do in this backend. */
+}
+
+static int
+D3D12_QueueDrawPoints(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
+{
+ VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
+ int i;
+ SDL_Color color;
+ color.r = cmd->data.draw.r;
+ color.g = cmd->data.draw.g;
+ color.b = cmd->data.draw.b;
+ color.a = cmd->data.draw.a;
+
+ if (!verts) {
+ return -1;
+ }
+
+ cmd->data.draw.count = count;
+
+ for (i = 0; i < count; i++) {
+ verts->pos.x = points[i].x + 0.5f;
+ verts->pos.y = points[i].y + 0.5f;
+ verts->tex.x = 0.0f;
+ verts->tex.y = 0.0f;
+ verts->color = color;
+ verts++;
+ }
+
+ return 0;
+}
+
+static int
+D3D12_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture,
+ const float *xy, int xy_stride, const SDL_Color *color, int color_stride, const float *uv, int uv_stride,
+ int num_vertices, const void *indices, int num_indices, int size_indices,
+ float scale_x, float scale_y)
+{
+ int i;
+ int count = indices ? num_indices : num_vertices;
+ VertexPositionColor *verts = (VertexPositionColor *) SDL_AllocateRenderVertices(renderer, count * sizeof (VertexPositionColor), 0, &cmd->data.draw.first);
+
+ if (!verts) {
+ return -1;
+ }
+
+ cmd->data.draw.count = count;
+ size_indices = indices ? size_indices : 0;
+
+ for (i = 0; i < count; i++) {
+ int j;
+ float *xy_;
+ if (size_indices == 4) {
+ j = ((const Uint32 *)indices)[i];
+ } else if (size_indices == 2) {
+ j = ((const Uint16 *)indices)[i];
+ } else if (size_indices == 1) {
+ j = ((const Uint8 *)indices)[i];
+ } else {
+ j = i;
+ }
+
+ xy_ = (float *)((char*)xy + j * xy_stride);
+
+ verts->pos.x = xy_[0] * scale_x;
+ verts->pos.y = xy_[1] * scale_y;
+ verts->color = *(SDL_Color*)((char*)color + j * color_stride);
+
+ if (texture) {
+ float *uv_ = (float *)((char*)uv + j * uv_stride);
+ verts->tex.x = uv_[0];
+ verts->tex.y = uv_[1];
+ } else {
+ verts->tex.x = 0.0f;
+ verts->tex.y = 0.0f;
+ }
+
+ verts += 1;
+ }
+ return 0;
+}
+
+static int
+D3D12_UpdateVertexBuffer(SDL_Renderer *renderer,
+ const void * vertexData, size_t dataSizeInBytes)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ HRESULT result = S_OK;
+ const int vbidx = rendererData->currentVertexBuffer;
+ const UINT stride = sizeof(VertexPositionColor);
+ const UINT offset = 0;
+ UINT8* vertexBufferData = NULL;
+ D3D12_RANGE range;
+
+ range.Begin = 0;
+ range.End = 0;
+
+ if (dataSizeInBytes == 0) {
+ return 0; /* nothing to do. */
+ }
+
+ if (rendererData->issueBatch) {
+ if (FAILED(D3D12_IssueBatch(rendererData))) {
+ SDL_SetError("Failed to issue intermediate batch");
+ return E_FAIL;
+ }
+ }
+
+ if (dataSizeInBytes > SDL_D3D12_VERTEX_BUFFER_MAX_TRIS * stride) {
+ SDL_SetError("d3d12 renderer does not support more than %d verts in a vertex buffer", SDL_D3D12_VERTEX_BUFFER_MAX_TRIS);
+ return E_FAIL;
+ }
+
+ result = ID3D12Resource_Map(rendererData->vertexBuffers[vbidx].resource, 0, &range, &vertexBufferData);
+ if (FAILED(result)) {
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [vertex buffer]"), result);
+ }
+ SDL_memcpy(vertexBufferData, vertexData, dataSizeInBytes);
+ ID3D12Resource_Unmap(rendererData->vertexBuffers[vbidx].resource, 0, NULL);
+
+ rendererData->vertexBuffers[vbidx].view.SizeInBytes = (UINT)dataSizeInBytes;
+
+ ID3D12GraphicsCommandList_IASetVertexBuffers(rendererData->commandList, 0, 1, &rendererData->vertexBuffers[vbidx].view);
+
+ rendererData->currentVertexBuffer++;
+ if (rendererData->currentVertexBuffer >= SDL_D3D12_NUM_VERTEX_BUFFERS) {
+ rendererData->currentVertexBuffer = 0;
+ rendererData->issueBatch = SDL_TRUE;
+ }
+
+ return S_OK;
+}
+
+static int
+D3D12_UpdateViewport(SDL_Renderer * renderer)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+ const SDL_Rect *viewport = &data->currentViewport;
+ Float4X4 projection;
+ Float4X4 view;
+ SDL_FRect orientationAlignedViewport;
+ BOOL swapDimensions;
+ D3D12_VIEWPORT d3dviewport;
+ const int rotation = D3D12_GetRotationForCurrentRenderTarget(renderer);
+
+ if (viewport->w == 0 || viewport->h == 0) {
+ /* If the viewport is empty, assume that it is because
+ * SDL_CreateRenderer is calling it, and will call it again later
+ * with a non-empty viewport.
+ */
+ /* SDL_Log("%s, no viewport was set!\n", __FUNCTION__); */
+ return -1;
+ }
+
+ /* Make sure the SDL viewport gets rotated to that of the physical display's rotation.
+ * Keep in mind here that the Y-axis will be been inverted (from Direct3D's
+ * default coordinate system) so rotations will be done in the opposite
+ * direction of the DXGI_MODE_ROTATION enumeration.
+ */
+ switch (rotation) {
+ case DXGI_MODE_ROTATION_IDENTITY:
+ projection = MatrixIdentity();
+ break;
+ case DXGI_MODE_ROTATION_ROTATE270:
+ projection = MatrixRotationZ(SDL_static_cast(float, M_PI * 0.5f));
+ break;
+ case DXGI_MODE_ROTATION_ROTATE180:
+ projection = MatrixRotationZ(SDL_static_cast(float, M_PI));
+ break;
+ case DXGI_MODE_ROTATION_ROTATE90:
+ projection = MatrixRotationZ(SDL_static_cast(float, -M_PI * 0.5f));
+ break;
+ default:
+ return SDL_SetError("An unknown DisplayOrientation is being used");
+ }
+
+ /* Update the view matrix */
+ SDL_zero(view);
+ view.m[0][0] = 2.0f / viewport->w;
+ view.m[1][1] = -2.0f / viewport->h;
+ view.m[2][2] = 1.0f;
+ view.m[3][0] = -1.0f;
+ view.m[3][1] = 1.0f;
+ view.m[3][3] = 1.0f;
+
+ /* Combine the projection + view matrix together now, as both only get
+ * set here (as of this writing, on Dec 26, 2013). When done, store it
+ * for eventual transfer to the GPU.
+ */
+ data->vertexShaderConstantsData.projectionAndView = MatrixMultiply(
+ view,
+ projection);
+
+ /* Update the Direct3D viewport, which seems to be aligned to the
+ * swap buffer's coordinate space, which is always in either
+ * a landscape mode, for all Windows 8/RT devices, or a portrait mode,
+ * for Windows Phone devices.
+ */
+ swapDimensions = D3D12_IsDisplayRotated90Degrees(rotation);
+ if (swapDimensions) {
+ orientationAlignedViewport.x = (float) viewport->y;
+ orientationAlignedViewport.y = (float) viewport->x;
+ orientationAlignedViewport.w = (float) viewport->h;
+ orientationAlignedViewport.h = (float) viewport->w;
+ } else {
+ orientationAlignedViewport.x = (float) viewport->x;
+ orientationAlignedViewport.y = (float) viewport->y;
+ orientationAlignedViewport.w = (float) viewport->w;
+ orientationAlignedViewport.h = (float) viewport->h;
+ }
+
+ d3dviewport.TopLeftX = orientationAlignedViewport.x;
+ d3dviewport.TopLeftY = orientationAlignedViewport.y;
+ d3dviewport.Width = orientationAlignedViewport.w;
+ d3dviewport.Height = orientationAlignedViewport.h;
+ d3dviewport.MinDepth = 0.0f;
+ d3dviewport.MaxDepth = 1.0f;
+ /* SDL_Log("%s: D3D viewport = {%f,%f,%f,%f}\n", __FUNCTION__, d3dviewport.TopLeftX, d3dviewport.TopLeftY, d3dviewport.Width, d3dviewport.Height); */
+ ID3D12GraphicsCommandList_RSSetViewports(data->commandList, 1, &d3dviewport);
+
+ data->viewportDirty = SDL_FALSE;
+
+ return 0;
+}
+
+static int
+D3D12_SetDrawState(SDL_Renderer * renderer, const SDL_RenderCommand *cmd, D3D12_Shader shader,
+ D3D12_PRIMITIVE_TOPOLOGY_TYPE topology,
+ const int numShaderResources, D3D12_CPU_DESCRIPTOR_HANDLE * shaderResources,
+ D3D12_CPU_DESCRIPTOR_HANDLE * sampler, const Float4X4 *matrix)
+
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
+ const Float4X4 *newmatrix = matrix ? matrix : &rendererData->identity;
+ D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = D3D12_GetCurrentRenderTargetView(renderer);
+ const SDL_BlendMode blendMode = cmd->data.draw.blend;
+ SDL_bool updateSubresource = SDL_FALSE;
+ int i;
+ D3D12_CPU_DESCRIPTOR_HANDLE firstShaderResource;
+ DXGI_FORMAT rtvFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
+
+ if (rendererData->textureRenderTarget) {
+ rtvFormat = rendererData->textureRenderTarget->mainTextureFormat;
+ }
+
+ /* See if we need to change the pipeline state */
+ if (!rendererData->currentPipelineState ||
+ rendererData->currentPipelineState->shader != shader ||
+ rendererData->currentPipelineState->blendMode != blendMode ||
+ rendererData->currentPipelineState->topology != topology ||
+ rendererData->currentPipelineState->rtvFormat != rtvFormat) {
+
+ /* Find the matching pipeline.
+ NOTE: Although it may seem inefficient to linearly search through ~450 pipelines
+ to find the correct one, in profiling this doesn't come up at all.
+ It's unlikely that using a hash table would affect performance a measurable amount unless
+ it's a degenerate case that's chaning the pipline state dozens of times per frame.
+ */
+ rendererData->currentPipelineState = NULL;
+ for (i = 0; i < rendererData->pipelineStateCount; ++i) {
+ D3D12_PipelineState* candidatePiplineState = &rendererData->pipelineStates[i];
+ if (candidatePiplineState->shader == shader &&
+ candidatePiplineState->blendMode == blendMode &&
+ candidatePiplineState->topology == topology &&
+ candidatePiplineState->rtvFormat == rtvFormat) {
+ rendererData->currentPipelineState = candidatePiplineState;
+ break;
+ }
+ }
+
+ /* If we didn't find a match, create a new one -- it must mean the blend mode is non-standard */
+ if (!rendererData->currentPipelineState) {
+ rendererData->currentPipelineState = D3D12_CreatePipelineState(renderer, shader, blendMode, topology, rtvFormat);
+ }
+
+ if (!rendererData->currentPipelineState) {
+ return SDL_SetError("[direct3d12] Unable to create required pipeline state");
+ }
+
+ ID3D12GraphicsCommandList2_SetPipelineState(rendererData->commandList, rendererData->currentPipelineState->pipelineState);
+ ID3D12GraphicsCommandList2_SetGraphicsRootSignature(rendererData->commandList,
+ rendererData->rootSignatures[D3D12_GetRootSignatureType(rendererData->currentPipelineState->shader)]);
+ /* When we change these we will need to re-upload the constant buffer and reset any descriptors */
+ updateSubresource = SDL_TRUE;
+ rendererData->currentSampler.ptr = 0;
+ rendererData->currentShaderResource.ptr = 0;
+ }
+
+ if (renderTargetView.ptr != rendererData->currentRenderTargetView.ptr) {
+ ID3D12GraphicsCommandList2_OMSetRenderTargets(rendererData->commandList, 1, &renderTargetView, FALSE, NULL);
+ rendererData->currentRenderTargetView = renderTargetView;
+ }
+
+ if (rendererData->viewportDirty) {
+ if (D3D12_UpdateViewport(renderer) == 0) {
+ /* vertexShaderConstantsData.projectionAndView has changed */
+ updateSubresource = SDL_TRUE;
+ }
+ }
+
+ if (rendererData->cliprectDirty) {
+ D3D12_RECT scissorRect;
+ if (D3D12_GetViewportAlignedD3DRect(renderer, &rendererData->currentCliprect, &scissorRect, TRUE) != 0) {
+ /* D3D12_GetViewportAlignedD3DRect will have set the SDL error */
+ return -1;
+ }
+ ID3D12GraphicsCommandList2_RSSetScissorRects(rendererData->commandList, 1, &scissorRect);
+ rendererData->cliprectDirty = SDL_FALSE;
+ }
+
+ if (numShaderResources > 0) {
+ firstShaderResource = shaderResources[0];
+ } else {
+ firstShaderResource.ptr = 0;
+ }
+ if (firstShaderResource.ptr != rendererData->currentShaderResource.ptr) {
+ for (i = 0; i < numShaderResources; ++i) {
+ D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle = D3D12_CPUtoGPUHandle(rendererData->srvDescriptorHeap, shaderResources[i]);
+ ID3D12GraphicsCommandList2_SetGraphicsRootDescriptorTable(rendererData->commandList, i + 1, GPUHandle);
+ }
+ rendererData->currentShaderResource.ptr = firstShaderResource.ptr;
+ }
+
+ if (sampler && sampler->ptr != rendererData->currentSampler.ptr) {
+ D3D12_GPU_DESCRIPTOR_HANDLE GPUHandle = D3D12_CPUtoGPUHandle(rendererData->samplerDescriptorHeap, *sampler);
+ UINT tableIndex = 0;
+
+ /* Figure out the correct sampler descriptor table index based on the type of shader */
+ switch (shader) {
+ case SHADER_RGB:
+ tableIndex = 2;
+ break;
+#if SDL_HAVE_YUV
+ case SHADER_YUV_JPEG:
+ case SHADER_YUV_BT601:
+ case SHADER_YUV_BT709:
+ tableIndex = 4;
+ break;
+ case SHADER_NV12_JPEG:
+ case SHADER_NV12_BT601:
+ case SHADER_NV12_BT709:
+ case SHADER_NV21_JPEG:
+ case SHADER_NV21_BT601:
+ case SHADER_NV21_BT709:
+ tableIndex = 3;
+ break;
+#endif
+ default:
+ return SDL_SetError("[direct3d12] Trying to set a sampler for a shader which doesn't have one");
+ break;
+ }
+
+ ID3D12GraphicsCommandList2_SetGraphicsRootDescriptorTable(rendererData->commandList, tableIndex, GPUHandle);
+ rendererData->currentSampler = *sampler;
+ }
+
+ if (updateSubresource == SDL_TRUE || SDL_memcmp(&rendererData->vertexShaderConstantsData.model, newmatrix, sizeof (*newmatrix)) != 0) {
+ SDL_memcpy(&rendererData->vertexShaderConstantsData.model, newmatrix, sizeof (*newmatrix));
+ ID3D12GraphicsCommandList2_SetGraphicsRoot32BitConstants(rendererData->commandList,
+ 0,
+ 32,
+ &rendererData->vertexShaderConstantsData,
+ 0
+ );
+ }
+
+ return 0;
+}
+
+static int
+D3D12_SetCopyState(SDL_Renderer * renderer, const SDL_RenderCommand *cmd, const Float4X4 *matrix)
+{
+ SDL_Texture *texture = cmd->data.draw.texture;
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ D3D12_TextureData *textureData = (D3D12_TextureData *) texture->driverdata;
+ D3D12_CPU_DESCRIPTOR_HANDLE *textureSampler;
+
+ switch (textureData->scaleMode) {
+ case D3D12_FILTER_MIN_MAG_MIP_POINT:
+ textureSampler = &rendererData->nearestPixelSampler;
+ break;
+ case D3D12_FILTER_MIN_MAG_MIP_LINEAR:
+ textureSampler = &rendererData->linearSampler;
+ break;
+ default:
+ return SDL_SetError("Unknown scale mode: %d\n", textureData->scaleMode);
+ }
+#if SDL_HAVE_YUV
+ if (textureData->yuv) {
+ D3D12_CPU_DESCRIPTOR_HANDLE shaderResources[] = {
+ textureData->mainTextureResourceView,
+ textureData->mainTextureResourceViewU,
+ textureData->mainTextureResourceViewV
+ };
+ D3D12_Shader shader;
+
+ switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
+ case SDL_YUV_CONVERSION_JPEG:
+ shader = SHADER_YUV_JPEG;
+ break;
+ case SDL_YUV_CONVERSION_BT601:
+ shader = SHADER_YUV_BT601;
+ break;
+ case SDL_YUV_CONVERSION_BT709:
+ shader = SHADER_YUV_BT709;
+ break;
+ default:
+ return SDL_SetError("Unsupported YUV conversion mode");
+ }
+
+ /* Make sure each texture is in the correct state to be accessed by the pixel shader. */
+ D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ D3D12_TransitionResource(rendererData, textureData->mainTextureU, textureData->mainResourceStateU, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceStateU = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ D3D12_TransitionResource(rendererData, textureData->mainTextureV, textureData->mainResourceStateV, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceStateV = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+
+ return D3D12_SetDrawState(renderer, cmd, shader, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
+ SDL_arraysize(shaderResources), shaderResources, textureSampler, matrix);
+ } else if (textureData->nv12) {
+ D3D12_CPU_DESCRIPTOR_HANDLE shaderResources[] = {
+ textureData->mainTextureResourceView,
+ textureData->mainTextureResourceViewNV,
+ };
+ D3D12_Shader shader;
+
+ switch (SDL_GetYUVConversionModeForResolution(texture->w, texture->h)) {
+ case SDL_YUV_CONVERSION_JPEG:
+ shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_JPEG : SHADER_NV21_JPEG;
+ break;
+ case SDL_YUV_CONVERSION_BT601:
+ shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_BT601 : SHADER_NV21_BT601;
+ break;
+ case SDL_YUV_CONVERSION_BT709:
+ shader = texture->format == SDL_PIXELFORMAT_NV12 ? SHADER_NV12_BT709 : SHADER_NV21_BT709;
+ break;
+ default:
+ return SDL_SetError("Unsupported YUV conversion mode");
+ }
+
+ /* Make sure each texture is in the correct state to be accessed by the pixel shader. */
+ D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ D3D12_TransitionResource(rendererData, textureData->mainTextureNV, textureData->mainResourceStateNV, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceStateNV = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+
+ return D3D12_SetDrawState(renderer, cmd, shader, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
+ SDL_arraysize(shaderResources), shaderResources, textureSampler, matrix);
+ }
+#endif /* SDL_HAVE_YUV */
+ D3D12_TransitionResource(rendererData, textureData->mainTexture, textureData->mainResourceState, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
+ textureData->mainResourceState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
+ return D3D12_SetDrawState(renderer, cmd, SHADER_RGB, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
+ 1, &textureData->mainTextureResourceView, textureSampler, matrix);
+}
+
+static void
+D3D12_DrawPrimitives(SDL_Renderer * renderer, D3D12_PRIMITIVE_TOPOLOGY primitiveTopology, const size_t vertexStart, const size_t vertexCount)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ ID3D12GraphicsCommandList_IASetPrimitiveTopology(rendererData->commandList, primitiveTopology);
+ ID3D12GraphicsCommandList_DrawInstanced(rendererData->commandList, (UINT)vertexCount, 1, 0, 0);
+}
+
+static int
+D3D12_RunCommandQueue(SDL_Renderer * renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
+{
+ D3D12_RenderData *rendererData = (D3D12_RenderData *) renderer->driverdata;
+ const int viewportRotation = D3D12_GetRotationForCurrentRenderTarget(renderer);
+
+ if (rendererData->currentViewportRotation != viewportRotation) {
+ rendererData->currentViewportRotation = viewportRotation;
+ rendererData->viewportDirty = SDL_TRUE;
+ }
+
+ if (D3D12_UpdateVertexBuffer(renderer, vertices, vertsize) < 0) {
+ return -1;
+ }
+
+ while (cmd) {
+ switch (cmd->command) {
+ case SDL_RENDERCMD_SETDRAWCOLOR: {
+ break; /* this isn't currently used in this render backend. */
+ }
+
+ case SDL_RENDERCMD_SETVIEWPORT: {
+ SDL_Rect *viewport = &rendererData->currentViewport;
+ if (SDL_memcmp(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect)) != 0) {
+ SDL_memcpy(viewport, &cmd->data.viewport.rect, sizeof (SDL_Rect));
+ rendererData->viewportDirty = SDL_TRUE;
+ }
+ break;
+ }
+
+ case SDL_RENDERCMD_SETCLIPRECT: {
+ const SDL_Rect *rect = &cmd->data.cliprect.rect;
+ if (rendererData->currentCliprectEnabled != cmd->data.cliprect.enabled) {
+ rendererData->currentCliprectEnabled = cmd->data.cliprect.enabled;
+ rendererData->cliprectDirty = SDL_TRUE;
+ }
+ if (!rendererData->currentCliprectEnabled) {
+ /* If the clip rect is disabled, then the scissor rect should be the whole viewport,
+ since direct3d12 doesn't allow disabling the scissor rectangle */
+ rect = &rendererData->currentViewport;
+ }
+ if (SDL_memcmp(&rendererData->currentCliprect, rect, sizeof (SDL_Rect)) != 0) {
+ SDL_memcpy(&rendererData->currentCliprect, rect, sizeof (SDL_Rect));
+ rendererData->cliprectDirty = SDL_TRUE;
+ }
+ break;
+ }
+
+ case SDL_RENDERCMD_CLEAR: {
+ const float colorRGBA[] = {
+ (cmd->data.color.r / 255.0f),
+ (cmd->data.color.g / 255.0f),
+ (cmd->data.color.b / 255.0f),
+ (cmd->data.color.a / 255.0f)
+ };
+
+ D3D12_CPU_DESCRIPTOR_HANDLE rtvDescriptor = D3D12_GetCurrentRenderTargetView(renderer);
+ ID3D12GraphicsCommandList2_ClearRenderTargetView(rendererData->commandList, rtvDescriptor, colorRGBA, 0, NULL);
+ break;
+ }
+
+ case SDL_RENDERCMD_DRAW_POINTS: {
+ const size_t count = cmd->data.draw.count;
+ const size_t first = cmd->data.draw.first;
+ const size_t start = first / sizeof (VertexPositionColor);
+ D3D12_SetDrawState(renderer, cmd, SHADER_SOLID, D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT, 0, NULL, NULL, NULL);
+ D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_POINTLIST, start, count);
+ break;
+ }
+
+ case SDL_RENDERCMD_DRAW_LINES: {
+ const size_t count = cmd->data.draw.count;
+ const size_t first = cmd->data.draw.first;
+ const size_t start = first / sizeof (VertexPositionColor);
+ const VertexPositionColor *verts = (VertexPositionColor *) (((Uint8 *) vertices) + first);
+ D3D12_SetDrawState(renderer, cmd, SHADER_SOLID, D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE, 0, NULL, NULL, NULL);
+ D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_LINESTRIP, start, count);
+ if (verts[0].pos.x != verts[count - 1].pos.x || verts[0].pos.y != verts[count - 1].pos.y) {
+ D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_POINTLIST, start + (count-1), 1);
+ }
+ break;
+ }
+
+ case SDL_RENDERCMD_FILL_RECTS: /* unused */
+ break;
+
+ case SDL_RENDERCMD_COPY: /* unused */
+ break;
+
+ case SDL_RENDERCMD_COPY_EX: /* unused */
+ break;
+
+ case SDL_RENDERCMD_GEOMETRY: {
+ SDL_Texture *texture = cmd->data.draw.texture;
+ const size_t count = cmd->data.draw.count;
+ const size_t first = cmd->data.draw.first;
+ const size_t start = first / sizeof (VertexPositionColor);
+
+ if (texture) {
+ D3D12_SetCopyState(renderer, cmd, NULL);
+ } else {
+ D3D12_SetDrawState(renderer, cmd, SHADER_SOLID, D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE, 0, NULL, NULL, NULL);
+ }
+
+ D3D12_DrawPrimitives(renderer, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, start, count);
+ break;
+ }
+
+ case SDL_RENDERCMD_NO_OP:
+ break;
+ }
+
+ cmd = cmd->next;
+ }
+
+ return 0;
+}
+
+static int
+D3D12_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
+ Uint32 format, void * pixels, int pitch)
+{
+ D3D12_RenderData * data = (D3D12_RenderData *) renderer->driverdata;
+ ID3D12Resource *backBuffer = NULL;
+ ID3D12Resource *readbackBuffer = NULL;
+ HRESULT result;
+ int status = -1;
+ D3D12_RESOURCE_DESC textureDesc;
+ D3D12_RESOURCE_DESC readbackDesc;
+ D3D12_HEAP_PROPERTIES heapProps;
+ D3D12_RECT srcRect = {0, 0, 0, 0};
+ D3D12_BOX srcBox;
+ D3D12_TEXTURE_COPY_LOCATION dstLocation;
+ D3D12_TEXTURE_COPY_LOCATION srcLocation;
+ D3D12_PLACED_SUBRESOURCE_FOOTPRINT placedTextureDesc;
+ D3D12_SUBRESOURCE_FOOTPRINT pitchedDesc;
+ BYTE *textureMemory;
+ int bpp;
+
+ if (data->textureRenderTarget) {
+ backBuffer = data->textureRenderTarget->mainTexture;
+ } else {
+ backBuffer = data->renderTargets[data->currentBackBufferIndex];
+ }
+
+ /* Create a staging texture to copy the screen's data to: */
+ ID3D12Resource_GetDesc(backBuffer, &textureDesc);
+ textureDesc.Width = rect->w;
+ textureDesc.Height = rect->h;
+
+ SDL_zero(readbackDesc);
+ readbackDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ readbackDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ readbackDesc.Height = 1;
+ readbackDesc.DepthOrArraySize = 1;
+ readbackDesc.MipLevels = 1;
+ readbackDesc.Format = DXGI_FORMAT_UNKNOWN;
+ readbackDesc.SampleDesc.Count = 1;
+ readbackDesc.SampleDesc.Quality = 0;
+ readbackDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+ readbackDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
+
+ /* Figure out how much we need to allocate for the upload buffer */
+ ID3D12Device_GetCopyableFootprints(data->d3dDevice,
+ &textureDesc,
+ 0,
+ 1,
+ 0,
+ NULL,
+ NULL,
+ NULL,
+ &readbackDesc.Width
+ );
+
+ SDL_zero(heapProps);
+ heapProps.Type = D3D12_HEAP_TYPE_READBACK;
+ heapProps.CreationNodeMask = 1;
+ heapProps.VisibleNodeMask = 1;
+
+ result = ID3D12Device_CreateCommittedResource(data->d3dDevice,
+ &heapProps,
+ D3D12_HEAP_FLAG_NONE,
+ &readbackDesc,
+ D3D12_RESOURCE_STATE_COPY_DEST,
+ NULL,
+ &SDL_IID_ID3D12Resource,
+ &readbackBuffer
+ );
+ if (FAILED(result)) {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Device1::CreateTexture2D [create staging texture]"), result);
+ goto done;
+ }
+
+ /* Transition the render target to be copyable from */
+ D3D12_TransitionResource(data, backBuffer, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
+
+ /* Copy the desired portion of the back buffer to the staging texture: */
+ if (D3D12_GetViewportAlignedD3DRect(renderer, rect, &srcRect, FALSE) != 0) {
+ /* D3D12_GetViewportAlignedD3DRect will have set the SDL error */
+ goto done;
+ }
+ srcBox.left = srcRect.left;
+ srcBox.right = srcRect.right;
+ srcBox.top = srcRect.top;
+ srcBox.bottom = srcRect.bottom;
+ srcBox.front = 0;
+ srcBox.back = 1;
+
+ /* Issue the copy texture region */
+ SDL_zero(pitchedDesc);
+ pitchedDesc.Format = textureDesc.Format;
+ pitchedDesc.Width = (UINT)textureDesc.Width;
+ pitchedDesc.Height = textureDesc.Height;
+ pitchedDesc.Depth = 1;
+ if (pitchedDesc.Format == DXGI_FORMAT_R8_UNORM) {
+ bpp = 1;
+ }
+ else {
+ bpp = 4;
+ }
+ pitchedDesc.RowPitch = D3D12_Align(pitchedDesc.Width * bpp, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT);
+
+ SDL_zero(placedTextureDesc);
+ placedTextureDesc.Offset = 0;
+ placedTextureDesc.Footprint = pitchedDesc;
+
+ SDL_zero(dstLocation);
+ dstLocation.pResource = readbackBuffer;
+ dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
+ dstLocation.PlacedFootprint = placedTextureDesc;
+
+ SDL_zero(srcLocation);
+ srcLocation.pResource = backBuffer;
+ srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
+ srcLocation.SubresourceIndex = 0;
+
+ ID3D12GraphicsCommandList_CopyTextureRegion(data->commandList,
+ &dstLocation,
+ 0, 0, 0,
+ &srcLocation,
+ &srcBox
+ );
+
+ /* We need to issue the command list for the copy to finish */
+ D3D12_IssueBatch(data);
+
+ /* Transition the render target back to a render target */
+ D3D12_TransitionResource(data, backBuffer, D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET );
+
+ /* Map the staging texture's data to CPU-accessible memory: */
+ result = ID3D12Resource_Map(readbackBuffer,
+ 0,
+ NULL,
+ &textureMemory
+ );
+ if (FAILED(result)) {
+ SAFE_RELEASE(readbackBuffer);
+ return WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D12Resource::Map [map staging texture]"), result);
+ }
+
+ /* Copy the data into the desired buffer, converting pixels to the
+ * desired format at the same time:
+ */
+ status = SDL_ConvertPixels(
+ rect->w, rect->h,
+ D3D12_DXGIFormatToSDLPixelFormat(textureDesc.Format),
+ textureMemory,
+ pitchedDesc.RowPitch,
+ format,
+ pixels,
+ pitch);
+
+ /* Unmap the texture: */
+ ID3D12Resource_Unmap(readbackBuffer, 0, NULL);
+
+done:
+ SAFE_RELEASE(readbackBuffer);
+ return status;
+}
+
+static void
+D3D12_RenderPresent(SDL_Renderer * renderer)
+{
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+ UINT syncInterval;
+ UINT presentFlags;
+ HRESULT result;
+
+ /* Transition the render target to present state */
+ D3D12_TransitionResource(data,
+ data->renderTargets[data->currentBackBufferIndex],
+ D3D12_RESOURCE_STATE_RENDER_TARGET,
+ D3D12_RESOURCE_STATE_PRESENT
+ );
+
+ /* Issue the command list */
+ result = ID3D12GraphicsCommandList2_Close(data->commandList);
+ ID3D12CommandQueue_ExecuteCommandLists(data->commandQueue, 1, (ID3D12CommandList * const *)&data->commandList);
+
+ if (renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) {
+ syncInterval = 1;
+ presentFlags = 0;
+ } else {
+ syncInterval = 0;
+ presentFlags = DXGI_PRESENT_ALLOW_TEARING;
+ }
+
+ /* The application may optionally specify "dirty" or "scroll"
+ * rects to improve efficiency in certain scenarios.
+ */
+ result = IDXGISwapChain4_Present(data->swapChain, syncInterval, presentFlags);
+
+ if (FAILED(result) && result != DXGI_ERROR_WAS_STILL_DRAWING) {
+ /* If the device was removed either by a disconnect or a driver upgrade, we
+ * must recreate all device resources.
+ */
+ if ( result == DXGI_ERROR_DEVICE_REMOVED ) {
+ D3D12_HandleDeviceLost(renderer);
+ } else if (result == DXGI_ERROR_INVALID_CALL) {
+ /* We probably went through a fullscreen <-> windowed transition */
+ D3D12_CreateWindowSizeDependentResources(renderer);
+ } else {
+ WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain::Present"), result);
+ }
+ } else {
+ /* Wait for the GPU and move to the next frame */
+ result = ID3D12CommandQueue_Signal(data->commandQueue, data->fence, data->fenceValue);
+
+ if (ID3D12Fence_GetCompletedValue(data->fence) < data->fenceValue) {
+ result = ID3D12Fence_SetEventOnCompletion(
+ data->fence,
+ data->fenceValue,
+ data->fenceEvent
+ );
+ WaitForSingleObjectEx(data->fenceEvent, INFINITE, FALSE);
+ }
+
+ data->fenceValue++;
+ data->currentBackBufferIndex = IDXGISwapChain4_GetCurrentBackBufferIndex(data->swapChain);
+
+ /* Reset the command allocator and command list, and transition back to render target */
+ D3D12_ResetCommandList(data);
+ D3D12_TransitionResource(data,
+ data->renderTargets[data->currentBackBufferIndex],
+ D3D12_RESOURCE_STATE_PRESENT,
+ D3D12_RESOURCE_STATE_RENDER_TARGET
+ );
+ }
+}
+
+static int
+D3D12_SetVSync(SDL_Renderer * renderer, const int vsync)
+{
+ if (vsync) {
+ renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
+ } else {
+ renderer->info.flags &= ~SDL_RENDERER_PRESENTVSYNC;
+ }
+ return 0;
+}
+
+SDL_Renderer *
+D3D12_CreateRenderer(SDL_Window * window, Uint32 flags)
+{
+ SDL_Renderer *renderer;
+ D3D12_RenderData *data;
+
+ renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
+ if (!renderer) {
+ SDL_OutOfMemory();
+ return NULL;
+ }
+
+ data = (D3D12_RenderData *) SDL_calloc(1, sizeof(*data));
+ if (!data) {
+ SDL_free(renderer);
+ SDL_OutOfMemory();
+ return NULL;
+ }
+
+ data->identity = MatrixIdentity();
+
+ renderer->WindowEvent = D3D12_WindowEvent;
+ renderer->SupportsBlendMode = D3D12_SupportsBlendMode;
+ renderer->CreateTexture = D3D12_CreateTexture;
+ renderer->UpdateTexture = D3D12_UpdateTexture;
+#if SDL_HAVE_YUV
+ renderer->UpdateTextureYUV = D3D12_UpdateTextureYUV;
+ renderer->UpdateTextureNV = D3D12_UpdateTextureNV;
+#endif
+ renderer->LockTexture = D3D12_LockTexture;
+ renderer->UnlockTexture = D3D12_UnlockTexture;
+ renderer->SetTextureScaleMode = D3D12_SetTextureScaleMode;
+ renderer->SetRenderTarget = D3D12_SetRenderTarget;
+ renderer->QueueSetViewport = D3D12_QueueSetViewport;
+ renderer->QueueSetDrawColor = D3D12_QueueSetViewport; /* SetViewport and SetDrawColor are (currently) no-ops. */
+ renderer->QueueDrawPoints = D3D12_QueueDrawPoints;
+ renderer->QueueDrawLines = D3D12_QueueDrawPoints; /* lines and points queue vertices the same way. */
+ renderer->QueueGeometry = D3D12_QueueGeometry;
+ renderer->RunCommandQueue = D3D12_RunCommandQueue;
+ renderer->RenderReadPixels = D3D12_RenderReadPixels;
+ renderer->RenderPresent = D3D12_RenderPresent;
+ renderer->DestroyTexture = D3D12_DestroyTexture;
+ renderer->DestroyRenderer = D3D12_DestroyRenderer;
+ renderer->info = D3D12_RenderDriver.info;
+ renderer->info.flags = (SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
+ renderer->driverdata = data;
+
+ if ((flags & SDL_RENDERER_PRESENTVSYNC)) {
+ renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
+ }
+ renderer->SetVSync = D3D12_SetVSync;
+
+ /* HACK: make sure the SDL_Renderer references the SDL_Window data now, in
+ * order to give init functions access to the underlying window handle:
+ */
+ renderer->window = window;
+
+ /* Initialize Direct3D resources */
+ if (FAILED(D3D12_CreateDeviceResources(renderer))) {
+ D3D12_DestroyRenderer(renderer);
+ return NULL;
+ }
+ if (FAILED(D3D12_CreateWindowSizeDependentResources(renderer))) {
+ D3D12_DestroyRenderer(renderer);
+ return NULL;
+ }
+
+ return renderer;
+}
+
+SDL_RenderDriver D3D12_RenderDriver = {
+ D3D12_CreateRenderer,
+ {
+ "direct3d12",
+ (
+ SDL_RENDERER_ACCELERATED |
+ SDL_RENDERER_PRESENTVSYNC |
+ SDL_RENDERER_TARGETTEXTURE
+ ), /* flags. see SDL_RendererFlags */
+ 6, /* num_texture_formats */
+ { /* texture_formats */
+ SDL_PIXELFORMAT_ARGB8888,
+ SDL_PIXELFORMAT_RGB888,
+ SDL_PIXELFORMAT_YV12,
+ SDL_PIXELFORMAT_IYUV,
+ SDL_PIXELFORMAT_NV12,
+ SDL_PIXELFORMAT_NV21
+ },
+ 16384, /* max_texture_width */
+ 16384 /* max_texture_height */
+ }
+};
+
+#endif /* SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED */
+
+#ifdef __WIN32__
+/* This function needs to always exist on Windows, for the Dynamic API. */
+ID3D12Device *
+SDL_RenderGetD3D12Device(SDL_Renderer * renderer)
+{
+ ID3D12Device *device = NULL;
+
+#if SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED
+ D3D12_RenderData *data = (D3D12_RenderData *) renderer->driverdata;
+
+ /* Make sure that this is a D3D renderer */
+ if (renderer->DestroyRenderer != D3D12_DestroyRenderer) {
+ SDL_SetError("Renderer is not a D3D12 renderer");
+ return NULL;
+ }
+
+ device = (ID3D12Device *)data->d3dDevice;
+ if (device) {
+ ID3D12Device_AddRef(device);
+ }
+#endif /* SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED */
+
+ return device;
+}
+#endif /* __WIN32__ */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/render/direct3d12/SDL_shaders_d3d12.c b/src/render/direct3d12/SDL_shaders_d3d12.c
new file mode 100644
index 000000000..85e4c55d3
--- /dev/null
+++ b/src/render/direct3d12/SDL_shaders_d3d12.c
@@ -0,0 +1,6967 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2022 Sam Lantinga
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#if SDL_VIDEO_RENDER_D3D12 && !SDL_RENDER_DISABLED
+
+#include "SDL_stdinc.h"
+
+#define COBJMACROS
+#include "../../core/windows/SDL_windows.h"
+#include
+
+#include "SDL_shaders_d3d12.h"
+
+#define SDL_COMPOSE_ERROR(str) SDL_STRINGIFY_ARG(__FUNCTION__) ", " str
+
+
+/* Direct3D 12 shaders
+
+ SDL's shaders are compiled into SDL itself, to simplify distribution.
+
+ All Direct3D 12 shaders were compiled with the following:
+
+ dxc -E -T -Fo