Added a hint to create the D3D device in thread-safe mode: SDL_HINT_RENDER_DIRECT3D_THREADSAFE
authorSam Lantinga <slouken@libsdl.org>
Sat, 28 Sep 2013 14:07:08 -0700
changeset 7764 0f48b5f28668
parent 7763 fc802d707136
child 7765 09937804892a
Added a hint to create the D3D device in thread-safe mode: SDL_HINT_RENDER_DIRECT3D_THREADSAFE
include/SDL_hints.h
src/render/direct3d/SDL_render_d3d.c
--- a/include/SDL_hints.h	Sat Sep 28 14:07:05 2013 -0700
+++ b/include/SDL_hints.h	Sat Sep 28 14:07:08 2013 -0700
@@ -95,6 +95,17 @@
 #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:
--- a/src/render/direct3d/SDL_render_d3d.c	Sat Sep 28 14:07:05 2013 -0700
+++ b/src/render/direct3d/SDL_render_d3d.c	Sat Sep 28 14:07:08 2013 -0700
@@ -544,9 +544,11 @@
     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 @@
         }
     }
 
-
-
     if (!data->d3d || !data->matrixStack) {
         SDL_free(renderer);
         SDL_free(data);
@@ -667,14 +667,22 @@
 
     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);