Преглед на файлове

resource: compile with shaderc or texturec by default and fallback to configuration-specific executables

Daniele Bartolini преди 6 години
родител
ревизия
1ce14a6b9e
променени са 5 файла, в които са добавени 73 реда и са изтрити 53 реда
  1. 11 0
      src/resource/compile_options.cpp
  2. 15 0
      src/resource/compile_options.h
  3. 1 11
      src/resource/lua_resource.cpp
  4. 27 25
      src/resource/shader_resource.cpp
  5. 19 17
      src/resource/texture_resource.cpp

+ 11 - 0
src/resource/compile_options.cpp

@@ -178,6 +178,17 @@ void CompileOptions::add_dependency(const char* path)
 	vector::push_back(_dependencies, dep);
 }
 
+const char* CompileOptions::exe_path(const char* const* paths, u32 num)
+{
+	for (u32 ii = 0; ii < num; ++ii)
+	{
+		if (os::access(paths[ii], AccessFlags::EXECUTE) == 0)
+			return paths[ii];
+	}
+
+	return NULL;
+}
+
 int CompileOptions::run_external_compiler(const char* const* argv, StringStream& output)
 {
 	return os::execute_process(argv, output);

+ 15 - 0
src/resource/compile_options.h

@@ -34,6 +34,18 @@
 		, name                                       \
 		)
 
+#if CROWN_PLATFORM_LINUX
+	#define EXE_PREFIX "./"
+	#define EXE_SUFFIX ""
+#elif CROWN_PLATFORM_WINDOWS
+	#define EXE_PREFIX ""
+	#define EXE_SUFFIX ".exe"
+#else
+	#error "Unknown platform"
+#endif // CROWN_PLATFORM_LINUX
+
+#define EXE_PATH(exe) EXE_PREFIX exe EXE_SUFFIX
+
 namespace crown
 {
 struct CompileOptions
@@ -115,6 +127,9 @@ struct CompileOptions
 	///
 	void add_dependency(const char* path);
 
+	/// Returns the first path with executable permissions or NULL if none found.
+	const char* exe_path(const char* const* paths, u32 num);
+
 	///
 	int run_external_compiler(const char* const* argv, StringStream& output);
 };

+ 1 - 11
src/resource/lua_resource.cpp

@@ -12,16 +12,6 @@
 #include "resource/compile_options.h"
 #include "resource/lua_resource.h"
 
-#define LUAJIT_NAME "./luajit"
-
-#if CROWN_PLATFORM_WINDOWS
-	#define EXE ".exe"
-#else
- 	#define EXE ""
-#endif // CROWN_PLATFORM_WINDOWS
-
-#define LUAJIT_EXE LUAJIT_NAME EXE
-
 #if CROWN_DEBUG
 	#define LUAJIT_FLAGS "-bg" // Keep debug info
 #else
@@ -43,7 +33,7 @@ namespace lua_resource_internal
 		StringStream output(ta);
 		const char* argv[] =
 		{
-			LUAJIT_EXE,
+			EXE_PATH("luajit"),
 			LUAJIT_FLAGS,
 			luasrc.c_str(),
 			luabin.c_str(),

+ 27 - 25
src/resource/shader_resource.cpp

@@ -17,24 +17,20 @@
 #include "resource/shader_resource.h"
 #include "world/shader_manager.h"
 
-#if CROWN_DEVELOPMENT
-	#define SHADERC_NAME "shaderc-development"
-#elif CROWN_DEBUG
-	#define SHADERC_NAME "shaderc-debug"
-#else
-	#define SHADERC_NAME "shaderc-release"
-#endif  // CROWN_DEBUG
-
-#if CROWN_PLATFORM_LINUX
-	#define SHADERC_PATH "./" SHADERC_NAME ""
-#elif CROWN_PLATFORM_WINDOWS
-	#define SHADERC_PATH SHADERC_NAME ".exe"
-#else
-	#define SHADERC_PATH ""
-#endif // CROWN_PLATFORM_LINUX
-
 namespace crown
 {
+static const char* shaderc_paths[] =
+{
+	EXE_PATH("shaderc"),
+#if CROWN_DEBUG
+	EXE_PATH("shaderc-debug")
+#elif CROWN_DEVELOPMENT
+	EXE_PATH("shaderc-development")
+#else
+	EXE_PATH("shaderc-release")
+#endif
+};
+
 namespace shader_resource_internal
 {
 	struct DepthFunction
@@ -426,11 +422,11 @@ namespace shader_resource_internal
 		return SamplerWrap::COUNT;
 	}
 
-	static int run_external_compiler(CompileOptions& opts, const char* infile, const char* outfile, const char* varying, const char* type, const char* platform, StringStream& output)
+	static int run_external_compiler(CompileOptions& opts, const char* shaderc, const char* infile, const char* outfile, const char* varying, const char* type, const char* platform, StringStream& output)
 	{
 		const char* argv[] =
 		{
-			SHADERC_PATH,
+			shaderc,
 			"-f",
 			infile,
 			"-o",
@@ -1105,10 +1101,10 @@ namespace shader_resource_internal
 				const RenderState rs_default;
 				const RenderState& rs = hash_map::get(_render_states, render_state, rs_default);
 
-				_opts.write(shader_name._id);                // Shader name
-				_opts.write(rs.encode());                    // Render state
-				compile_sampler_states(bgfx_shader.c_str()); // Sampler states
-				compile(bgfx_shader.c_str(), defines);       // Shader code
+				_opts.write(shader_name._id);                      // Shader name
+				_opts.write(rs.encode());                          // Render state
+				compile_sampler_states(bgfx_shader.c_str());       // Sampler states
+				compile_bgfx_shader(bgfx_shader.c_str(), defines); // Shader code
 			}
 		}
 
@@ -1139,7 +1135,7 @@ namespace shader_resource_internal
 			}
 		}
 
-		void compile(const char* bgfx_shader, const Vector<DynamicString>& defines)
+		void compile_bgfx_shader(const char* bgfx_shader, const Vector<DynamicString>& defines)
 		{
 			TempAllocator512 taa;
 			DynamicString key(taa);
@@ -1181,7 +1177,13 @@ namespace shader_resource_internal
 			TempAllocator4096 ta;
 			StringStream output(ta);
 
-			int ec = run_external_compiler(_opts, _vs_source_path.c_str()
+			const char* shaderc = _opts.exe_path(shaderc_paths, countof(shaderc_paths));
+			DATA_COMPILER_ASSERT(shaderc != NULL
+				, _opts
+				, "shaderc not found"
+				);
+
+			int ec = run_external_compiler(_opts, shaderc, _vs_source_path.c_str()
 				, _vs_compiled_path.c_str()
 				, _varying_path.c_str()
 				, "vertex"
@@ -1199,7 +1201,7 @@ namespace shader_resource_internal
 			}
 
 			array::clear(output);
-			ec = run_external_compiler(_opts, _fs_source_path.c_str()
+			ec = run_external_compiler(_opts, shaderc, _fs_source_path.c_str()
 				, _fs_compiled_path.c_str()
 				, _varying_path.c_str()
 				, "fragment"

+ 19 - 17
src/resource/texture_resource.cpp

@@ -14,24 +14,20 @@
 #include "resource/resource_manager.h"
 #include "resource/texture_resource.h"
 
-#if CROWN_DEVELOPMENT
-	#define TEXTUREC_NAME "texturec-development"
-#elif CROWN_DEBUG
-	#define TEXTUREC_NAME "texturec-debug"
-#else
-	#define TEXTUREC_NAME "texturec-release"
-#endif  // CROWN_DEBUG
-
-#if CROWN_PLATFORM_LINUX
-	#define TEXTUREC_PATH "./" TEXTUREC_NAME ""
-#elif CROWN_PLATFORM_WINDOWS
-	#define TEXTUREC_PATH TEXTUREC_NAME ".exe"
-#else
-	#define TEXTUREC_PATH ""
-#endif // CROWN_PLATFORM_LINUX
-
 namespace crown
 {
+static const char* texturec_paths[] =
+{
+	EXE_PATH("texturec"),
+#if CROWN_DEBUG
+	EXE_PATH("texturec-debug")
+#elif CROWN_DEVELOPMENT
+	EXE_PATH("texturec-development")
+#else
+	EXE_PATH("texturec-release")
+#endif
+};
+
 namespace texture_resource_internal
 {
 	void compile(CompileOptions& opts)
@@ -54,9 +50,15 @@ namespace texture_resource_internal
 		opts.get_absolute_path(name.c_str(), texsrc);
 		opts.get_temporary_path("ktx", texout);
 
+		const char* texturec = opts.exe_path(texturec_paths, countof(texturec_paths));
+		DATA_COMPILER_ASSERT(texturec != NULL
+			, opts
+			, "texturec not found"
+			);
+
 		const char* argv[] =
 		{
-			TEXTUREC_PATH,
+			texturec,
 			"-f",
 			texsrc.c_str(),
 			"-o",