Browse Source

Added support for xbo (#121)

Pascal Peridont 7 years ago
parent
commit
ddb620104a
11 changed files with 108 additions and 39 deletions
  1. 31 12
      libs/directx/directx.cpp
  2. 16 0
      libs/directx/directx.h
  3. 2 0
      libs/ui/ui_stub.c
  4. 19 4
      src/hl.h
  5. 10 9
      src/hlc.h
  6. 9 5
      src/hlc_main.c
  7. 5 1
      src/std/error.c
  8. 4 0
      src/std/file.c
  9. 5 1
      src/std/math.c
  10. 1 1
      src/std/random.c
  11. 6 6
      src/std/sys.c

+ 31 - 12
libs/directx/directx.cpp

@@ -1,22 +1,19 @@
 #define HL_NAME(n) directx_##n
 #include <hl.h>
 
+#ifdef HL_WIN_DESKTOP
 #include <dxgi.h>
 #include <d3dcommon.h>
 #include <d3d11.h>
 #include <D3Dcompiler.h>
+#else
+#include <xbo_directx.h>
+#endif
+#include <assert.h>
+#include "directx.h"
 
 #define DXERR(cmd)	{ HRESULT __ret = cmd; if( __ret == E_OUTOFMEMORY ) return NULL; if( __ret != S_OK ) ReportDxError(__ret,__LINE__); }
 
-typedef struct {
-	ID3D11Device *device;
-	ID3D11DeviceContext *context;
-	IDXGISwapChain *swapchain;
-	ID3D11RenderTargetView *renderTarget;
-	D3D_FEATURE_LEVEL feature;
-	int init_flags;
-} dx_driver;
-
 template <typename T> class dx_struct {
 	hl_type *t;
 public:
@@ -67,18 +64,30 @@ HL_PRIM dx_driver *HL_NAME(create)( HWND window, int format, int flags, int rest
 	desc.BufferDesc.Format = (DXGI_FORMAT)format;
 	desc.SampleDesc.Count = 1; // NO AA for now
 	desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+#ifdef HL_WIN_DESKTOP
 	desc.BufferCount = 1;
 	desc.Windowed = true;
+#else
+	desc.BufferCount = 2;
+	desc.Windowed = false;
+#endif
 	desc.OutputWindow = window;
 	if( restrictLevel >= maxLevels ) restrictLevel = maxLevels - 1;
+#if _DEBUG
+	flags |= D3D11_CREATE_DEVICE_INSTRUMENTED;
+#endif
 	d->init_flags = flags;
 	result = D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,flags,levels + restrictLevel,maxLevels - restrictLevel,D3D11_SDK_VERSION,&desc,&d->swapchain,&d->device,&d->feature,&d->context);
+
+#ifdef HL_WIN_DESKTOP
 	if( result == DXGI_ERROR_SDK_COMPONENT_MISSING && (flags & D3D11_CREATE_DEVICE_DEBUG) != 0 ) {
 		// no debug driver available, retry
 		flags &= ~D3D11_CREATE_DEVICE_DEBUG;
 		d->init_flags = flags;
 		result = E_INVALIDARG;
 	}
+#endif
+
 	if( result == E_INVALIDARG ) // most likely no DX11.1 support, try again
 		result = D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,flags,NULL,0,D3D11_SDK_VERSION, &desc, &d->swapchain, &d->device, &d->feature, &d->context);
 
@@ -88,14 +97,23 @@ HL_PRIM dx_driver *HL_NAME(create)( HWND window, int format, int flags, int rest
 	return d;
 }
 
+HL_PRIM dx_driver *HL_NAME(get_driver)(){
+	return driver;
+}
+
 HL_PRIM dx_resource *HL_NAME(get_back_buffer)() {
 	ID3D11Texture2D *backBuffer;
 	DXERR( driver->swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer) );
 	return backBuffer;
 }
 
-HL_PRIM bool HL_NAME(resize)( int width, int height, int format ) {
-	return driver->swapchain->ResizeBuffers(1,width,height,(DXGI_FORMAT)format,0) == S_OK;
+HL_PRIM bool HL_NAME(resize)(int width, int height, int format) {
+#ifdef HL_WIN_DESKTOP
+	HRESULT res = driver->swapchain->ResizeBuffers(1, width, height, (DXGI_FORMAT)format, 0); assert(res == S_OK);
+	return res == S_OK;
+#else
+	return TRUE; //Should not be called if the window is not resized (in the case here it will never happen)
+#endif
 }
 
 HL_PRIM dx_pointer *HL_NAME(create_render_target_view)( dx_resource *r, dx_struct<D3D11_RENDER_TARGET_VIEW_DESC> *desc ) {
@@ -136,7 +154,8 @@ HL_PRIM void HL_NAME(clear_color)( dx_pointer *rt, double r, double g, double b,
 }
 
 HL_PRIM void HL_NAME(present)( int interval, int flags ) {
-	driver->swapchain->Present(interval,flags);
+	HRESULT ret = driver->swapchain->Present(interval, flags);
+	if (ret != S_OK) ReportDxError(ret, __LINE__);
 }
 
 HL_PRIM const uchar *HL_NAME(get_device_name)() {

+ 16 - 0
libs/directx/directx.h

@@ -0,0 +1,16 @@
+#pragma once
+#ifndef DIRECTX_H
+#define DIRECTX_H
+
+typedef struct dx_driver_ {
+    ID3D11Device *device;
+    ID3D11DeviceContext *context;
+    IDXGISwapChain *swapchain;
+    ID3D11RenderTargetView *renderTarget;
+    D3D_FEATURE_LEVEL feature;
+    int init_flags;
+} dx_driver;
+
+HL_PRIM dx_driver *directx_get_driver();
+
+#endif

+ 2 - 0
libs/ui/ui_stub.c

@@ -1,6 +1,8 @@
 #define HL_NAME(n) ui_##n
 #include <hl.h>
+#ifndef HL_WIN
 #include <unistd.h>
+#endif
 
 #define wref void
 #define vsentinel void

+ 19 - 4
src/hl.h

@@ -29,8 +29,11 @@
 
 #define HL_VERSION	0x150
 
-#ifdef _WIN32
+#if defined(_WIN32)
 #	define HL_WIN
+#	ifndef _DURANGO
+#		define HL_WIN_DESKTOP
+#	endif
 #endif
 
 #if defined(__APPLE__) || defined(__MACH__) || defined(macintosh)
@@ -65,7 +68,11 @@
 #	define HL_NX
 #endif
 
-#if defined(HL_PS) || defined(HL_NX)
+#ifdef _DURANGO
+#	define HL_XBO
+#endif
+
+#if defined(HL_PS) || defined(HL_NX) || defined(HL_XBO)
 #	define HL_CONSOLE
 #endif
 
@@ -183,7 +190,11 @@ typedef unsigned long long uint64;
 // -------------- UNICODE -----------------------------------
 
 #if defined(HL_WIN) && !defined(HL_LLVM)
-#	include <windows.h>
+#ifdef HL_WIN_DESKTOP
+#	include <Windows.h>
+#else
+#	include <xdk.h>
+#endif
 #	include <wchar.h>
 typedef wchar_t	uchar;
 #	define USTR(str)	L##str
@@ -709,7 +720,11 @@ typedef struct {
 #		define DEFINE_PRIM_WITH_NAME(t,name,args,realName)
 #	endif
 #elif defined(LIBHL_STATIC)
-#define	HL_PRIM
+#	ifdef __cplusplus
+#		define	HL_PRIM				extern "C" 
+#	else
+#		define	HL_PRIM				
+#	endif
 #define DEFINE_PRIM_WITH_NAME(t,name,args,realName)
 #else
 #	ifdef __cplusplus

+ 10 - 9
src/hlc.h

@@ -44,17 +44,18 @@
 #undef STRICT
 
 // disable some warnings triggered by HLC code generator
-
+
 #ifdef HL_VCC
-#	pragma warning(disable:4702) // unreachable code
-#	pragma warning(disable:4100) // unreferenced param
-#	pragma warning(disable:4101) // unreferenced local var
-#	pragma warning(disable:4102) // unreferenced label
-#	pragma warning(disable:4700) // uninitialized local variable used
+#	pragma warning(disable:4702) // unreachable code
+#	pragma warning(disable:4100) // unreferenced param
+#	pragma warning(disable:4101) // unreferenced local var
+#	pragma warning(disable:4102) // unreferenced label
+#	pragma warning(disable:4700) // uninitialized local variable used
 #	pragma warning(disable:4703) // potentially uninitialized local
-#	pragma warning(disable:4723) // potential divide by 0
-#	pragma warning(disable:4715) // control paths must return a value
-#	pragma warning(disable:4716) // must return a value (ends with throw)
+#	pragma warning(disable:4723) // potential divide by 0
+#	pragma warning(disable:4715) // control paths must return a value
+#	pragma warning(disable:4716) // must return a value (ends with throw)
+#	pragma warning(disable:4701) // potentially uninitialized local variable
 #else
 #	pragma GCC diagnostic ignored "-Wunused-variable"
 #	pragma GCC diagnostic ignored "-Wunused-function"

+ 9 - 5
src/hlc_main.c

@@ -25,7 +25,7 @@
 #   include <SDL_main.h>
 #endif
 
-#ifdef _WIN32
+#ifdef HL_WIN_DESKTOP
 #	pragma warning(disable:4091)
 #	include <DbgHelp.h>
 #	pragma comment(lib, "Dbghelp.lib")
@@ -45,7 +45,7 @@ extern void sys_global_exit();
 #endif
 
 static uchar *hlc_resolve_symbol( void *addr, uchar *out, int *outSize ) {
-#ifdef _WIN32
+#ifdef HL_WIN_DESKTOP
 	static HANDLE stack_process_handle = NULL;
 	DWORD64 index;
 	IMAGEHLP_LINEW64 line;
@@ -75,25 +75,29 @@ static uchar *hlc_resolve_symbol( void *addr, uchar *out, int *outSize ) {
 
 static int hlc_capture_stack( void **stack, int size ) {
 	int count = 0;
-#	ifdef _WIN32
+#	ifdef HL_WIN_DESKTOP
 	count = CaptureStackBackTrace(2, size, stack, NULL) - 8; // 8 startup
 	if( count < 0 ) count = 0;
 #	endif
 	return count;
 }
 
-#ifdef HL_VCC
+#if defined( HL_VCC )
 static int throw_handler( int code ) {
+	#if !defined(HL_XBO)
 	switch( code ) {
 	case EXCEPTION_ACCESS_VIOLATION: hl_error("Access violation");
 	case EXCEPTION_STACK_OVERFLOW: hl_error("Stack overflow");
 	default: hl_error("Unknown runtime error");
 	}
 	return EXCEPTION_CONTINUE_SEARCH;
+	#else
+	return 0;
+	#endif
 }
 #endif
 
-#ifdef HL_WIN
+#ifdef HL_WIN_DESKTOP
 int wmain(int argc, uchar *argv[]) {
 #else
 int main(int argc, char *argv[]) {

+ 5 - 1
src/std/error.c

@@ -23,6 +23,10 @@
 #include <stdarg.h>
 #include <string.h>
 
+#ifdef HL_CONSOLE
+#include <posix/posix.h>
+#endif
+
 HL_PRIM hl_trap_ctx *hl_current_trap = NULL;
 HL_PRIM vdynamic *hl_current_exc = NULL;
 HL_PRIM vdynamic **hl_debug_exc = NULL;
@@ -33,7 +37,7 @@ static bool exc_rethrow = false;
 
 HL_PRIM void *hl_fatal_error( const char *msg, const char *file, int line ) {
 	hl_blocking(true);
-#	ifdef _WIN32
+#	ifdef HL_WIN_DESKTOP
     HWND consoleWnd = GetConsoleWindow();
     DWORD pid;
     GetWindowThreadProcessId(consoleWnd, &pid);

+ 4 - 0
src/std/file.c

@@ -25,7 +25,11 @@
 #	include <posix/posix.h>
 #endif
 #ifdef HL_WIN
+#ifdef HL_WIN_DESKTOP
 #	include <windows.h>
+#else
+#	include<xdk.h>
+#endif
 #	define fopen(name,mode) _wfopen(name,mode)
 #	define HL_UFOPEN
 #endif

+ 5 - 1
src/std/math.c

@@ -36,7 +36,11 @@ HL_PRIM double hl_math_abs( double a ) {
 }
 
 HL_PRIM bool hl_math_isnan( double a ) {
-	return a != a;
+#ifdef HL_WIN
+	return isnan(a);
+#else
+	return a != a; //does not work on some platforms
+#endif
 }
 
 typedef union {

+ 1 - 1
src/std/random.c

@@ -22,7 +22,7 @@
 #include <hl.h>
 #include <time.h>
 #include <string.h>
-#if defined(HL_WIN)
+#if defined(HL_WIN_DESKTOP)
 #	include <windows.h>
 #	include <process.h>
 #elif defined(HL_CONSOLE)

+ 6 - 6
src/std/sys.c

@@ -23,8 +23,8 @@
 
 #ifdef HL_CONSOLE
 #	include <posix/posix.h>
-#else
-
+#endif
+#if !defined(HL_CONSOLE) || defined(HL_WIN)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -125,7 +125,7 @@ HL_PRIM vbyte *hl_sys_string() {
 }
 
 HL_PRIM vbyte *hl_sys_locale() {
-#ifdef HL_WIN
+#ifdef HL_WIN_DESKTOP
 	wchar_t loc[LOCALE_NAME_MAX_LENGTH];
 	int len = GetSystemDefaultLocaleName(loc,LOCALE_NAME_MAX_LENGTH);
 	return len == 0 ? NULL : hl_copy_bytes((vbyte*)loc,(len+1)*2);
@@ -183,7 +183,7 @@ HL_PRIM bool hl_sys_put_env( vbyte *e, vbyte *v ) {
 #	define environ (*_NSGetEnviron())
 #endif
 
-#ifdef HL_WIN
+#ifdef HL_WIN_DESKTOP
 #	undef environ
 #	define environ _wenviron
 #else
@@ -529,7 +529,7 @@ HL_PRIM vbyte *hl_sys_exe_path() {
 		return NULL;
 	return (vbyte*)pstrdup(path,-1);
 #elif defined(HL_CONSOLE)
-	return (vbyte*)sys_exe_path();
+	return sys_exe_path();
 #else
 	const pchar *p = getenv("_");
 	if( p != NULL )
@@ -546,7 +546,7 @@ HL_PRIM vbyte *hl_sys_exe_path() {
 }
 
 HL_PRIM int hl_sys_get_char( bool b ) {
-#	if defined(HL_WIN)
+#	if defined(HL_WIN_DESKTOP)
 	return b?getche():getch();
 #	elif defined(HL_CONSOLE)
 	return -1;