# This file is part of the FidelityFX SDK.
# 
# Copyright (C) 2024 Advanced Micro Devices, Inc.
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files(the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions :
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# Function to compile a set of shaders using the FidelityFX shader compiler driver.
#
# EXECUTABLE			Path to the FidelityFX SC tool executable.
# BASE_ARGS				The base arguments for the compiler.
# API_BASE_ARGS			API (DX12 or Vulkan) specific base args.
# PERMUTATION_ARGS		Permutation set to compile for.
# SHADER_FILES			A list of shader files to compile using the permutation matrix expanded from SC_PERMUTATION_ARGS.
# OUTPUT_PATH			Path to store compiled shader output.
#
# Returns
# A list of header files generated by the FidelityFX Shader Compiler driver.
function(compile_shaders_with_depfile
	EXECUTABLE BASE_ARGS API_BASE_ARGS
	PERMUTATION_ARGS INCLUDES_ARGS
	SHADER_FILES OUTPUT_PATH
	PERMUTATION_OUTPUTS)

	#message(STATUS "OUTPUT_PATH=${OUTPUT_PATH}")
	#message(STATUS "EXE=${EXECUTABLE}")
	#message(STATUS "BASE=${BASE_ARGS}")
	#message(STATUS "API=${API_BASE_ARGS}")
	#message(STATUS "PERMS=${PERMUTATION_ARGS}")
	#message(STATUS "INCLUDES=${INCLUDES_ARGS}")
	#message(STATUS "SHADER_FILES=${SHADER_FILES}")

	# Figure out if this HLSL or GLSL
	list(GET SHADER_FILES 0 FIRST_SHADER_FILE)
	get_filename_component(SHADER_EXT "${FIRST_SHADER_FILE}" LAST_EXT)
	string(TOUPPER "${SHADER_EXT}" SHADER_EXT)
	set(INCLUDE_SPACE )
	set(HLSL_WAVE64_ARGS )
	set(HLSL_WAVE32_ARGS )
	set(HLSL_16BIT_ARGS )
	if (SHADER_EXT STREQUAL ".HLSL")
		set(INCLUDE_SPACE " ")
		set(HLSL_WAVE64_ARGS "-DFFX_PREFER_WAVE64=\"[WaveSize(64)]\"" "-DFFX_HLSL_SM=66" -T cs_6_6)
		set(HLSL_WAVE32_ARGS "-DFFX_HLSL_SM=62" -T cs_6_2)
		set(HLSL_16BIT_ARGS "-enable-16bit-types")
	endif()
	# GLSL requires no spaces: -I<include0> -I<include1> etc.
	# HLSL requires spaces: -I <include0> -I <include1> etc.
	set(COMPILE_INCLUDE_ARGS )
	foreach(INC ${INCLUDES_ARGS})
		list(APPEND COMPILE_INCLUDE_ARGS "-I${INCLUDE_SPACE}${INC}")
	endforeach(INC)

	if (FFX_API_BACKEND STREQUAL GDK_DESKTOP_X64)
		set(FFX_GDK_OPTION -compiler=gdk.desktop.x64)
	elseif (FFX_API_BACKEND STREQUAL GDK_SCARLETT_X64)
		set(FFX_GDK_OPTION -compiler=gdk.scarlett.x64)
	else()
		set(FFX_GDK_OPTION )
	endif()

	foreach(PASS_SHADER ${SHADER_FILES})
		get_filename_component(PASS_SHADER_FILENAME ${PASS_SHADER} NAME_WE)
		get_filename_component(PASS_SHADER_TARGET ${PASS_SHADER} NAME_WLE)

		set(WAVE32_PERMUTATION_HEADER ${OUTPUT_PATH}/${PASS_SHADER_TARGET}_permutations.h)
		set(WAVE64_PERMUTATION_HEADER ${OUTPUT_PATH}/${PASS_SHADER_TARGET}_wave64_permutations.h)
		set(WAVE32_16BIT_PERMUTATION_HEADER ${OUTPUT_PATH}/${PASS_SHADER_TARGET}_16bit_permutations.h)
		set(WAVE64_16BIT_PERMUTATION_HEADER ${OUTPUT_PATH}/${PASS_SHADER_TARGET}_wave64_16bit_permutations.h)

		# combine base and permutation args
		set(SC_ARGS ${BASE_ARGS} ${API_BASE_ARGS} ${PERMUTATION_ARGS})

		# Wave32
		add_custom_command(
			OUTPUT ${WAVE32_PERMUTATION_HEADER}
			COMMAND ${EXECUTABLE} ${FFX_GDK_OPTION} ${SC_ARGS} -name=${PASS_SHADER_FILENAME} -DFFX_HALF=0 ${HLSL_WAVE32_ARGS} ${COMPILE_INCLUDE_ARGS} -output=${OUTPUT_PATH} ${PASS_SHADER}
			WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
			DEPENDS ${PASS_SHADER}
			DEPFILE ${WAVE32_PERMUTATION_HEADER}.d
		)
		list(APPEND PERMUTATION_OUTPUTS ${WAVE32_PERMUTATION_HEADER})

		# Wave64
		add_custom_command(
			OUTPUT ${WAVE64_PERMUTATION_HEADER}
			COMMAND ${EXECUTABLE} ${FFX_GDK_OPTION} ${SC_ARGS} -name=${PASS_SHADER_FILENAME}_wave64 -DFFX_HALF=0 ${HLSL_WAVE64_ARGS} ${COMPILE_INCLUDE_ARGS} -output=${OUTPUT_PATH} ${PASS_SHADER}
			WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
			DEPENDS ${PASS_SHADER}
			DEPFILE ${WAVE64_PERMUTATION_HEADER}.d
		)
		list(APPEND PERMUTATION_OUTPUTS ${WAVE64_PERMUTATION_HEADER})

		# Wave32 16-bit
		add_custom_command(
			OUTPUT ${WAVE32_16BIT_PERMUTATION_HEADER}
			COMMAND ${EXECUTABLE} ${FFX_GDK_OPTION} ${SC_ARGS} -name=${PASS_SHADER_FILENAME}_16bit -DFFX_HALF=1 ${HLSL_16BIT_ARGS} ${HLSL_WAVE32_ARGS} ${COMPILE_INCLUDE_ARGS} -output=${OUTPUT_PATH} ${PASS_SHADER}
			WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
			DEPENDS ${PASS_SHADER}
			DEPFILE ${WAVE32_16BIT_PERMUTATION_HEADER}.d
		)
		list(APPEND PERMUTATION_OUTPUTS ${WAVE32_16BIT_PERMUTATION_HEADER})

		# Wave64 16-bit
		add_custom_command(
			OUTPUT ${WAVE64_16BIT_PERMUTATION_HEADER}
			COMMAND ${EXECUTABLE} ${FFX_GDK_OPTION} ${SC_ARGS} -name=${PASS_SHADER_FILENAME}_wave64_16bit -DFFX_HALF=1 ${HLSL_16BIT_ARGS} ${HLSL_WAVE64_ARGS} ${COMPILE_INCLUDE_ARGS} -output=${OUTPUT_PATH} ${PASS_SHADER}
			WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
			DEPENDS ${PASS_SHADER}
			DEPFILE ${WAVE64_16BIT_PERMUTATION_HEADER}.d
		)
		list(APPEND PERMUTATION_OUTPUTS ${WAVE64_16BIT_PERMUTATION_HEADER})
	endforeach(PASS_SHADER)

	set(${PERMUTATION_OUTPUTS} ${PERMUTATION_OUTPUTS} PARENT_SCOPE)
endfunction()

# macro to add shader output files to a list for dependencies
macro (add_shader_output)
    foreach (_SHADER_SRC ${ARGN})
		#message (STATUS ${_SHADER_SRC})
		list (APPEND FFX_SC_PERMUTATION_OUTPUTS "${_SHADER_SRC}")
    endforeach()

	set (FFX_SC_PERMUTATION_OUTPUTS ${FFX_SC_PERMUTATION_OUTPUTS} PARENT_SCOPE)
endmacro()