Browse Source

added set_error_handler (required for future context lost support)

ncannasse 7 years ago
parent
commit
da4defd10e
3 changed files with 33 additions and 1 deletions
  1. 22 0
      libs/directx/directx.cpp
  2. 10 0
      libs/directx/dx/Driver.hx
  3. 1 1
      libs/directx/haxelib.json

+ 22 - 0
libs/directx/directx.cpp

@@ -25,6 +25,7 @@ typedef ID3D11DeviceChild dx_pointer;
 
 static dx_driver *driver = NULL;
 static IDXGIFactory *factory = NULL;
+static vclosure *on_dx_error = NULL;
 
 static IDXGIFactory *GetDXGI() {
 	if( factory == NULL && CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory) != S_OK )
@@ -33,6 +34,18 @@ static IDXGIFactory *GetDXGI() {
 }
 
 static void ReportDxError( HRESULT err, int line ) {
+	if( on_dx_error ) {
+		vdynamic args[3];
+		vdynamic *vargs[3] = { &args[0], &args[1], &args[2] };
+		args[0].t = &hlt_i32;
+		args[1].t = &hlt_i32;
+		args[2].t = &hlt_i32;
+		args[0].v.i = (int)err;
+		args[1].v.i = (err == DXGI_ERROR_DEVICE_REMOVED ? (int)driver->device->GetDeviceRemovedReason() : 0);
+		args[2].v.i = line;
+		hl_dyn_call(on_dx_error,vargs,3);
+		return;
+	}
 	if( err == DXGI_ERROR_DEVICE_REMOVED && driver ){
 		err = driver->device->GetDeviceRemovedReason();
 		hl_error_msg(USTR("DXGI_ERROR_DEVICE_REMOVED reason 0x%X line %d"),(DWORD)err,line);
@@ -41,6 +54,14 @@ static void ReportDxError( HRESULT err, int line ) {
 	}
 }
 
+HL_PRIM void HL_NAME(set_error_handler)( vclosure *c ) {
+	if( !on_dx_error ) {
+		if( !c ) return;
+		hl_add_root(&on_dx_error);
+	}
+	on_dx_error = c;
+}
+
 HL_PRIM dx_driver *HL_NAME(create)( HWND window, int format, int flags, int restrictLevel ) {
 	static D3D_FEATURE_LEVEL levels[] = {
 		D3D_FEATURE_LEVEL_11_1,
@@ -406,6 +427,7 @@ HL_PRIM void HL_NAME(debug_print)( vbyte *b ) {
 #define _POINTER _ABSTRACT(dx_pointer)
 #define _RESOURCE _ABSTRACT(dx_resource)
 
+DEFINE_PRIM(_VOID, set_error_handler, _FUN(_VOID, _I32 _I32 _I32));
 DEFINE_PRIM(_DRIVER, create, _ABSTRACT(dx_window) _I32 _I32 _I32);
 DEFINE_PRIM(_BOOL, resize, _I32 _I32 _I32);
 DEFINE_PRIM(_RESOURCE, get_back_buffer, _NO_ARG);

+ 10 - 0
libs/directx/dx/Driver.hx

@@ -464,6 +464,16 @@ class ShaderResourceViewDesc {
 class Driver {
 
 	public static var fullScreen(get, set) : Bool;
+	
+	/**
+		Setup an error handler instead of getting String exceptions:
+		The first parameter is the DirectX error code
+		The second parameter is the removed reason code if the first is DXGI_ERROR_DEVICE_REMOVED
+		The third parameter is the line in directx.cpp sources where was triggered the error.
+		Allocation methods will return null if an error handler is setup and does not raise exception.
+	**/
+	public static function setErrorHandler( f : Int -> Int -> Int -> Void ) {
+	}
 
 	public static function create( win : Window, format : Format, flags : DriverInitFlags = None, restrictLevel = 0 ) {
 		return dxCreate(@:privateAccess win.win, format, flags, restrictLevel);

+ 1 - 1
libs/directx/haxelib.json

@@ -4,7 +4,7 @@
 	"license" : "BSD",
 	"contributors" : ["ncannasse"],
 	"description" : "DirectX support for Haxe/HL.",
-	"version" : "1.4.0",
+	"version" : "1.6.0",
 	"releasenote" : "",
 	"dependencies": { "hlopenal" : "" }
 }