Browse Source

added glsl to spir-v shader compilation

Nicolas Cannasse 4 years ago
parent
commit
f5cdabb517
4 changed files with 77 additions and 19 deletions
  1. 1 0
      .gitignore
  2. 6 6
      libs/sdl/sdl.vcxproj
  3. 26 4
      libs/sdl/sdl/Vulkan.hx
  4. 44 9
      libs/sdl/vulkan.c

+ 1 - 0
.gitignore

@@ -53,3 +53,4 @@ node_modules
 /*.tgz
 args.txt
 /other/benchs/hlc
+/include/vulkan

+ 6 - 6
libs/sdl/sdl.vcxproj

@@ -145,7 +145,7 @@
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <OutputFile>../../$(Configuration)/$(TargetName).hdll</OutputFile>
-      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib;shaderc_shared.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@@ -160,7 +160,7 @@
       <SubSystem>Windows</SubSystem>
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
-      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib;shaderc_shared.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@@ -180,7 +180,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
       <OutputFile>../../$(Configuration)/$(TargetName).hdll</OutputFile>
-      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib;shaderc_shared.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseVS2013|Win32'">
@@ -200,7 +200,7 @@
       <GenerateDebugInformation>true</GenerateDebugInformation>
       <ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
       <OutputFile>../../$(Configuration)/$(TargetName).hdll</OutputFile>
-      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib;shaderc_shared.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -218,7 +218,7 @@
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib;shaderc_shared.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='ReleaseVS2013|x64'">
@@ -236,7 +236,7 @@
       <EnableCOMDATFolding>true</EnableCOMDATFolding>
       <OptimizeReferences>true</OptimizeReferences>
       <GenerateDebugInformation>true</GenerateDebugInformation>
-      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib</AdditionalDependencies>
+      <AdditionalDependencies>libhl.lib;winmm.lib;SDL2.lib;opengl32.lib;user32.lib;kernel32.lib;vulkan-1.lib;shaderc_shared.lib</AdditionalDependencies>
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>

+ 26 - 4
libs/sdl/sdl/Vulkan.hx

@@ -1,27 +1,49 @@
 package sdl;
 
+enum abstract ShaderKind(Int) {
+	var Vertex = 0;
+	var Fragment = 1;
+}
+
 @:hlNative("?sdl","vk_")
 class Vulkan {
 	public static function clearColorImage( r : Float, g : Float, b : Float, a : Float ) {
 	}
+
+	public static function compileShader( source : String, mainFunction : String, kind : ShaderKind ) {
+		var outSize = -1;
+		var fileName = "";
+		var bytes = @:privateAccess compile_shader(source.toUtf8(), fileName.toUtf8(), mainFunction.toUtf8(), kind, outSize);
+		if( outSize < 0 ) {
+			var error = @:privateAccess String.fromUTF8(bytes);
+			var lines = source.split("\n");
+			throw error+"\n\nin\n\n"+[for( i => l in lines ) StringTools.rpad((i+1)+":"," ",8)+l].join("\n");
+		}
+		return @:privateAccess new haxe.io.Bytes(bytes, outSize);
+	}
+
+	static function compile_shader( source : hl.Bytes, shaderFile : hl.Bytes, mainFunction : hl.Bytes, kind : ShaderKind, outSize : hl.Ref<Int> ) : hl.Bytes {
+		return null;
+	}
+
 }
 
 
 @:hlNative("?sdl","vk_")
 abstract VKContext(hl.Abstract<"vk_context">) {
-	
+
 	public function initSwapchain( width : Int, height : Int ) : Bool {
 		return false;
 	}
-	
+
 	public function beginFrame() : Bool {
 		return false;
 	}
-		
+
 	public function setCurrent() {
 	}
 
 	public function endFrame() {
 	}
-	
+
 }

+ 44 - 9
libs/sdl/vulkan.c

@@ -4,6 +4,8 @@
 #define VK_USE_PLATFORM_WIN32_KHR
 #include <vulkan/vulkan.h>
 
+#include <shaderc/shaderc.h>
+
 static VkInstance instance = NULL;
 
 VkInstance vk_get_instance() {
@@ -282,14 +284,6 @@ HL_PRIM bool HL_NAME(vk_begin_frame)( VkContext ctx ) {
 	return true;
 }
 
-HL_PRIM void HL_NAME(vk_clear_color_image)( double r, double g, double b, double a ) {
-	VkClearColorValue color = { (float)r, (float)g, (float)b, (float)a };
-	vkCmdClearColorImage(current_buffer,
-		current_context->swapchainImages[current_context->currentImage], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
-		&color, 1, &RANGE_ALL
-	);
-}
-
 HL_PRIM void HL_NAME(vk_end_frame)( VkContext ctx ) {
 	VkFrame *frame = &ctx->frames[ctx->currentFrame];
 
@@ -343,5 +337,46 @@ DEFINE_PRIM(_BOOL, vk_init, _NO_ARG);
 DEFINE_PRIM(_BOOL, vk_init_swapchain, _VCTX _I32 _I32);
 DEFINE_PRIM(_BOOL, vk_begin_frame, _VCTX);
 DEFINE_PRIM(_VOID, vk_set_current, _VCTX);
-DEFINE_PRIM(_VOID, vk_clear_color_image, _F64 _F64 _F64 _F64);
 DEFINE_PRIM(_VOID, vk_end_frame, _VCTX);
+
+// ------ COMMAND BUFFER OPERATIONS -----------------------
+
+HL_PRIM void HL_NAME(vk_clear_color_image)( double r, double g, double b, double a ) {
+	VkClearColorValue color = { (float)r, (float)g, (float)b, (float)a };
+	vkCmdClearColorImage(current_buffer,
+		current_context->swapchainImages[current_context->currentImage], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+		&color, 1, &RANGE_ALL
+	);
+}
+
+DEFINE_PRIM(_VOID, vk_clear_color_image, _F64 _F64 _F64 _F64);
+
+
+// ------ SHADER COMPILATION ------------------------------
+
+HL_PRIM vbyte *HL_NAME(vk_compile_shader)( vbyte *source, vbyte *shaderFile, vbyte *mainFunction, int shaderKind, int *outSize ) {
+	shaderc_compiler_t compiler = shaderc_compiler_initialize();
+	shaderc_compile_options_t opts = shaderc_compile_options_initialize();
+	shaderc_compile_options_set_optimization_level(opts, shaderc_optimization_level_size);
+	shaderc_compilation_result_t result = shaderc_compile_into_spv(compiler, source, strlen(source), shaderKind, shaderFile, mainFunction, opts);
+	shaderc_compiler_release(compiler);
+	shaderc_compile_options_release(opts);
+	
+	if( shaderc_result_get_compilation_status(result) != shaderc_compilation_status_success ) {
+		const char *str = shaderc_result_get_error_message(result);
+		vbyte *error = hl_copy_bytes(str, (int)strlen(str)+1);
+		shaderc_result_release(result);
+		*outSize = -1;
+		return error;
+	}
+
+	int size = (int)shaderc_result_get_length(result);
+	vbyte *data = hl_alloc_bytes(size);
+	memcpy(data, shaderc_result_get_bytes(result), size);
+	shaderc_result_release(result);
+	
+	*outSize = size;
+	return data;
+}
+
+DEFINE_PRIM(_BYTES, vk_compile_shader, _BYTES _BYTES _BYTES _I32 _REF(_I32));