Sfoglia il codice sorgente

added shader compilation

Nicolas Cannasse 3 anni fa
parent
commit
31e4dc512b
3 ha cambiato i file con 73 aggiunte e 4 eliminazioni
  1. 20 0
      libs/directx/dx/Dx12.hx
  2. 49 0
      libs/directx/dx12.cpp
  3. 4 4
      libs/directx/dx12.vcxproj

+ 20 - 0
libs/directx/dx/Dx12.hx

@@ -106,6 +106,26 @@ abstract Address(Int64) from Int64 {
 	}
 }
 
+@:hlNative("dx12","compiler_")
+abstract ShaderCompiler(hl.Abstract<"dx_compiler">) {
+	public function new() {
+		this = cast create();
+	}
+	public function compile( source : String, profile : String, args : Array<String> ) : haxe.io.Bytes {
+		var outLen = 0;
+		var nargs = new hl.NativeArray(args.length);
+		for( i in 0...args.length )
+			nargs[i] = @:privateAccess args[i].bytes;
+		var bytes = do_compile(cast this, @:privateAccess source.bytes, @:privateAccess profile.bytes, nargs, outLen);
+		return @:privateAccess new haxe.io.Bytes(bytes, outLen);
+	}
+	@:hlNative("dx12","compiler_compile")
+	static function do_compile( comp : ShaderCompiler, source : hl.Bytes, profile : hl.Bytes, args : hl.NativeArray<hl.Bytes>, out : hl.Ref<Int> ) : hl.Bytes {
+		return null;
+	}
+	static function create() : ShaderCompiler { return null; }
+}
+
 @:hlNative("dx12","descriptor_heap_")
 abstract DescriptorHeap(Resource) {
 	public function new(desc) {

+ 49 - 0
libs/directx/dx12.cpp

@@ -5,6 +5,7 @@
 #include <dxgi.h>
 #include <dxgi1_5.h>
 #include <d3d12.h>
+#include <dxcapi.h>
 #endif
 
 #define DXERR(cmd)	{ HRESULT __ret = cmd; if( __ret == E_OUTOFMEMORY ) return NULL; if( __ret != S_OK ) ReportDxError(__ret,__LINE__); }
@@ -155,6 +156,7 @@ void HL_NAME(flush_messages)() {
 		SIZE_T len = 0;
 		drv->infoQueue->GetMessage(i, NULL, &len);
 		D3D12_MESSAGE *msg = (D3D12_MESSAGE*)malloc(len);
+		if( msg == NULL ) break;
 		drv->infoQueue->GetMessage(i, msg, &len);
 		printf("%s\n",msg->pDescription);
 		free(msg);
@@ -192,6 +194,53 @@ DEFINE_PRIM(_VOID, signal, _RES _I64);
 DEFINE_PRIM(_VOID, flush_messages, _NO_ARG);
 DEFINE_PRIM(_BYTES, get_device_name, _NO_ARG);
 
+// ---- SHADERS
+
+typedef struct {
+	IDxcLibrary *library;
+	IDxcCompiler *compiler;
+} dx_compiler;
+
+dx_compiler *HL_NAME(compiler_create)() {
+	dx_compiler *comp = (dx_compiler*)hl_gc_alloc_raw(sizeof(dx_compiler));
+	memset(comp,0,sizeof(dx_compiler));
+	CHKERR(DxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&comp->library)));
+	CHKERR(DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&comp->compiler)));
+	return comp;
+}
+
+vbyte *HL_NAME(compiler_compile)( dx_compiler *comp, uchar *source, uchar *profile, varray *args, int *dataLen ) {
+	IDxcBlobEncoding *blob = NULL;
+	IDxcOperationResult *result = NULL;
+	comp->library->CreateBlobWithEncodingFromPinned(source,(int)ustrlen(source)*2,DXC_CP_UTF16,&blob);
+	if( blob == NULL )
+		hl_error("Could not create blob");
+	comp->compiler->Compile(blob,L"",L"main",profile,hl_aptr(args,LPCWSTR),args->size,NULL,0,NULL,&result);
+	HRESULT hr;
+	result->GetStatus(&hr);
+	if( !SUCCEEDED(hr) ) {
+		IDxcBlobEncoding *error = NULL;
+		result->GetErrorBuffer(&error);
+		uchar *c = hl_to_utf16((char*)error->GetBufferPointer());
+		blob->Release();
+		result->Release();
+		error->Release();
+		hl_error("%s",c);
+	}
+	IDxcBlob *out = NULL;
+	result->GetResult(&out);
+	*dataLen = (int)out->GetBufferSize();
+	vbyte *bytes = hl_copy_bytes((vbyte*)out->GetBufferPointer(), *dataLen);
+	out->Release();
+	blob->Release();
+	result->Release();
+	return bytes;
+}
+
+#define _COMPILER _ABSTRACT(dx_compiler)
+DEFINE_PRIM(_COMPILER, compiler_create, _NO_ARG);
+DEFINE_PRIM(_BYTES, compiler_compile, _COMPILER _BYTES _BYTES _ARR _REF(_I32));
+
 // ---- HEAPS
 
 ID3D12DescriptorHeap *HL_NAME(descriptor_heap_create)( D3D12_DESCRIPTOR_HEAP_DESC *desc ) {

+ 4 - 4
libs/directx/dx12.vcxproj

@@ -110,7 +110,7 @@
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <OutputFile>../../$(Configuration)/$(TargetName).hdll</OutputFile>
-      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib;dxcompiler.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -124,7 +124,7 @@
     <Link>
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib;dxcompiler.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseVS2017|Win32'">
@@ -143,7 +143,7 @@
       <OptimizeReferences>true</OptimizeReferences>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <OutputFile>../../$(Configuration)/$(TargetName).hdll</OutputFile>
-      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib;dxcompiler.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseVS2017|x64'">
@@ -161,7 +161,7 @@
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;D3D12.lib;dxgi.lib;dxcompiler.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />