Commit cf5e5a83600899f1902122d5873f11d0ff5cd4ce

Sam Lantinga 2013-09-28T14:07:08

Added a hint to create the D3D device in thread-safe mode: SDL_HINT_RENDER_DIRECT3D_THREADSAFE

diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 9dc9cfe..dac928e 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -95,6 +95,17 @@ extern "C" {
 #define SDL_HINT_RENDER_OPENGL_SHADERS      "SDL_RENDER_OPENGL_SHADERS"
 
 /**
+ *  \brief  A variable controlling whether the Direct3D device is initialized for thread-safe operations.
+ *
+ *  This variable can be set to the following values:
+ *    "0"       - Thread-safety is not enabled (faster)
+ *    "1"       - Thread-safety is enabled
+ *
+ *  By default the Direct3D device is created with thread-safety disabled.
+ */
+#define SDL_HINT_RENDER_DIRECT3D_THREADSAFE "SDL_RENDER_DIRECT3D_THREADSAFE"
+
+/**
  *  \brief  A variable controlling the scaling quality
  *
  *  This variable can be set to the following values:
diff --git a/src/render/direct3d/SDL_render_d3d.c b/src/render/direct3d/SDL_render_d3d.c
index 2ac5974..fdaf796 100644
--- a/src/render/direct3d/SDL_render_d3d.c
+++ b/src/render/direct3d/SDL_render_d3d.c
@@ -544,9 +544,11 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
     D3D_RenderData *data;
     SDL_SysWMinfo windowinfo;
     HRESULT result;
+	const char *hint;
     D3DPRESENT_PARAMETERS pparams;
     IDirect3DSwapChain9 *chain;
     D3DCAPS9 caps;
+	DWORD device_flags;
     Uint32 window_flags;
     int w, h;
     SDL_DisplayMode fullscreen_mode;
@@ -589,8 +591,6 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
         }
     }
 
-
-
     if (!data->d3d || !data->matrixStack) {
         SDL_free(renderer);
         SDL_free(data);
@@ -667,14 +667,22 @@ D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
 
     IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
 
+	device_flags = D3DCREATE_FPU_PRESERVE;
+	if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) {
+		device_flags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
+	} else {
+		device_flags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
+	}
+
+	hint = SDL_GetHint(SDL_HINT_RENDER_DIRECT3D_THREADSAFE);
+	if (hint && SDL_atoi(hint)) {
+		device_flags |= D3DCREATE_MULTITHREADED;
+	}
+
     result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
                                      D3DDEVTYPE_HAL,
                                      pparams.hDeviceWindow,
-                                     D3DCREATE_FPU_PRESERVE | ((caps.
-                                      DevCaps &
-                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
-                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
-                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING),
+                                     device_flags,
                                      &pparams, &data->device);
     if (FAILED(result)) {
         D3D_DestroyRenderer(renderer);