

find_program(SIMUL_FX_EXECUTABLE fx)


function ( add_sfx_shader_project targetName configJsonFile )
	if(SIMUL_BUILD_SHADERS)
	#message(${configJsonFile})
		cmake_parse_arguments(sfx "" "INTERMEDIATE;OUTPUT;FOLDER" "INCLUDES;SOURCES;OPTIONS;DEFINES" ${ARGN} )
		if (NOT TARGET ${targetName})
			if("${sfx_FOLDER}" STREQUAL "")
				set(sfx_FOLDER Shaders)
			endif()
		endif()
		set(SET_DEFINES -E\"PLATFORM=${SIMUL_PLATFORM_DIR}\" )
		foreach(opt_d ${sfx_DEFINES})
			set(SET_DEFINES "${SET_DEFINES} -E\"${opt_d}\"" )
		endforeach() 
		set(EXTRA_OPTS)
		foreach(opt_in ${sfx_OPTIONS})
			set(EXTRA_OPTS "${EXTRA_OPTS} ${opt_in}" )
		endforeach()
		set(INCLUDE_OPTS)
		foreach(incl_path ${sfx_INCLUDES})
			set(INCLUDE_OPTS ${INCLUDE_OPTS} -I"${incl_path}" )
		endforeach()
		if(NOT "${EXTRA_OPTS}" STREQUAL "")
			string(REPLACE "\"" "" EXTRA_OPTS ${EXTRA_OPTS})
		endif()
		set(EXTRA_OPTS "${EXTRA_OPTS} ${SET_DEFINES}")
		separate_arguments(EXTRA_OPTS_S WINDOWS_COMMAND "${EXTRA_OPTS}")
		if(SIMUL_DEBUG_SHADERS)
			set(EXTRA_OPTS_S ${EXTRA_OPTS_S} -v -d)
		endif()
		set(srcs_includes)
		set(srcs_shaders)
		set(srcs)
		set(outputs${targetName})
		set(out_folder ${sfx_OUTPUT})
		if("${sfx_INTERMEDIATE}" STREQUAL "")
			set(intermediate_folder "${CMAKE_CURRENT_BINARY_DIR}/sfx_intermediate")
		else()
			set(intermediate_folder ${sfx_INTERMEDIATE})
		endif()
		foreach(in_f ${sfx_SOURCES})
			list(APPEND srcs ${in_f})
			string(FIND ${in_f} ".sl" slpos REVERSE)
			if(NOT slpos EQUAL -1)
				list(APPEND srcs_includes ${in_f}) 
			endif()
			string(FIND ${in_f} ".sfx" pos REVERSE)
			if(NOT ${pos} EQUAL -1)
				list(APPEND srcs_shaders ${in_f})
				get_filename_component(name ${in_f} NAME )
				string(REPLACE ".sfx" ".sfxo" out_f ${name})
				set(out_f "${out_folder}/${out_f}")
				add_custom_command(OUTPUT ${out_f}
					COMMAND "${SIMUL_SFX_EXECUTABLE}" ${in_f} ${INCLUDE_OPTS} -O"${out_folder}" -P"${configJsonFile}" -M"${intermediate_folder}" ${EXTRA_OPTS_S}
					MAIN_DEPENDENCY ${in_f}
					WORKING_DIRECTORY ${out_folder}
					DEPENDS ${SIMUL_SFX_EXECUTABLE}
					)
				list(APPEND outputs${targetName} ${out_f})
			else()
				if(MSVC)
					set_source_files_properties( ${in_f} PROPERTIES HEADER_FILE_ONLY ON )
				endif()
			endif()
		endforeach()
		source_group("Shaders" FILES  ${srcs_shaders} )
		source_group("Shader Includes" FILES ${srcs_includes} )
		if (NOT TARGET ${targetName})
			add_custom_target(${targetName} DEPENDS ${outputs${targetName}} SOURCES ${srcs} ${configJsonFile} )
			set_target_properties( ${targetName} PROPERTIES FOLDER ${sfx_FOLDER})
		endif()
		set_target_properties( ${targetName} PROPERTIES EXCLUDE_FROM_ALL FALSE )
		set_target_properties( ${targetName} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_DEBUG FALSE )
		set_target_properties( ${targetName} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_RELEASE FALSE )
		if((PLATFORM_WINDOWS OR PLATFORM_LINUX) AND SIMUL_SOURCE_BUILD)
			add_dependencies( ${targetName} Sfx )
		endif()
		set(${out_var} "${outputs${targetName}}" PARENT_SCOPE)
	endif()
endfunction()


find_program(SIMUL_SFX_EXECUTABLE sfx)
