# ITK
FIND_PACKAGE(ITK REQUIRED)
IF(ITK_FOUND)
	INCLUDE_DIRECTORIES(${ITK_INCLUDE_DIRS})
	INCLUDE(${ITK_USE_FILE})
ELSE()
	MESSAGE(FATAL_ERROR "ITK not found, but needed.")
ENDIF(ITK_FOUND)

# Qt4
FIND_PACKAGE(Qt4 REQUIRED)
IF(QT4_FOUND)
	INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR})
	INCLUDE(${QT_USE_FILE})	
ELSE()
	MESSAGE(FATAL_ERROR "QT4 not found, but needed.")
ENDIF(QT4_FOUND)

# CUDA
FIND_PACKAGE(CUDA)
IF(CUDA_FOUND)
	INCLUDE_DIRECTORIES(${CUDA_INCLUDE_DIRS})
	SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} "--ptxas-options=-v")
ELSE()
	MESSAGE(FATAL_ERROR "CUDA not found, but needed.")
ENDIF(CUDA_FOUND)


# The headers and sources in the ritkCUDA module
#===============================================
SET(HDRS
	ritkCuda.h
	ritkCudaMacros.h
	ritkCudaContext.h
	ritkCudaJob.h
	ritkCudaImportImageContainer.h
	ritkCudaRegularMemoryImportImageContainer.h
	ritkCudaPitchedMemoryImportImageContainer.h
	ritkCudaArrayImportImageContainer.h
	ritkCuda3DArrayImportImageContainer.h
	ritkCuda3DPitchedMemoryImportImageContainer.h
	ritkCudaMemoryPool.h
	ritkCudaRayCasterInterfaces.h
	ritkCudaRayCaster.h
	ritkCudaRayCasterCommon.h
	ritkOpenGLVolumeRayCastEntityShaders.h
	ritkOpenGLFloatVolumeCudaRayCastEntity.h
	ritkOpenGLRGBAVolumeCudaRayCastEntity.h
	ritkTruncatedSignedDistanceFunctionInterfaces.h
	ritkTruncatedSignedDistanceFunction.h
	ritkTruncatedSignedDistanceFunctionRayCaster.h
	ritkTruncatedSignedDistanceFunctionRayCasterInterfaces.h
	ritkTruncatedSignedDistanceFunctionRayCasterEntity.h
	ritkTruncatedSignedDistanceFunctionRayCasterEntityControlUnit.h
)


SET(TMPS
	ritkCudaImportImageContainer.txx
	ritkCudaRegularMemoryImportImageContainer.txx
	ritkCudaPitchedMemoryImportImageContainer.txx
	ritkCudaArrayImportImageContainer.txx
	ritkCuda3DArrayImportImageContainer.txx
	ritkCuda3DPitchedMemoryImportImageContainer.txx
	ritkCudaMemoryPool.txx
	ritkCudaRayCaster.txx
	ritkTruncatedSignedDistanceFunctionRayCaster.txx
)

SET(SRCS
	${TMPS}
	ritkCudaContext.cxx
	ritkCudaJob.cxx
	ritkCudaInstantiations.cxx
	ritkOpenGLFloatVolumeCudaRayCastEntity.cxx
	ritkOpenGLRGBAVolumeCudaRayCastEntity.cxx
	ritkOpenGLVolumeCudaRayCastInstantiations.cxx
	ritkTruncatedSignedDistanceFunction.cxx
	ritkTruncatedSignedDistanceFunctionRayCasterInstantiations.cxx
	ritkTruncatedSignedDistanceFunctionRayCasterEntity.cxx
	ritkTruncatedSignedDistanceFunctionRayCasterEntityControlUnit.cxx
	ThirdParty/GLee.c
)


SET(CUDA_MOC_HDRS 	
	ritkOpenGLFloatVolumeCudaRayCastEntity.h
	ritkOpenGLRGBAVolumeCudaRayCastEntity.h
	ritkTruncatedSignedDistanceFunctionRayCasterEntity.h
	ritkTruncatedSignedDistanceFunctionRayCasterEntityControlUnit.h
)

SET(CUDA_UIS
	ritkTruncatedSignedDistanceFunctionRayCasterEntityControlUnit.ui
)

SET(CUDA_SRCS 
	ritkFloatVolumeCudaRayCaster.cu
	ritkRGBAVolumeCudaRayCaster.cu
	ritkTruncatedSignedDistanceFunction.cu
	ritkTruncatedSignedDistanceFunctionRayCaster.cu
)

SET(CUDA_HDRS 
	ritkCudaRayCasterCommon.h
	ritkFloatVolumeCudaRayCasterKernel.h
	ritkRGBAVolumeCudaRayCasterKernel.h
	ritkTruncatedSignedDistanceFunctionKernels.h
	ritkTruncatedSignedDistanceFunctionRayCasterKernels.h
)


QT4_WRAP_UI(UI_HDRS ${CUDA_UIS})	
QT4_WRAP_CPP(MOC_SRCS ${CUDA_MOC_HDRS})


# Set the library name for the ritkCUDA module
# ============================================
SET(RITK_CUDA_LIBRARY ritkCuda CACHE INTERNAL "")
SET(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)


# Project settings
# ================
SET(RITK_CUDA_INCLUDE_DIRS
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_BINARY_DIR}	
	${CUDA_INCLUDE_DIRS} 
	${CUDA_CUT_INCLUDE_DIR}
	${CORE_INCLUDE_DIRS}
	${COMMON_INCLUDE_DIRS}
	${VISUALIZATION_INCLUDE_DIRS}
	CACHE INTERNAL ""
)
INCLUDE_DIRECTORIES(${RITK_CUDA_INCLUDE_DIRS})	
ADD_DEFINITIONS("/DRITKCUDA_SHAREDLIB_EXPORT")
CUDA_ADD_LIBRARY(${RITK_CUDA_LIBRARY} SHARED ${SRCS} ${HDRS} ${ITK_SRCS} ${ITK_HDRS} ${MOC_SRCS} ${UI_HDRS} ${CUDA_HDRS} ${CUDA_SRCS})

IF(UNIX)
	TARGET_LINK_LIBRARIES(${RITK_CUDA_LIBRARY}
		${ITK_LIBRARIES}
		${COMMON_LIBRARY} 
		${CORE_LIBRARY}
		${VISUALIZATION_LIBRARY}
		${CUDA_CUDA_LIBRARY}
		${QT_QTOPENGL_LIBRARY}
		${CUDA_CUDART_LIBRARY}
		${OPENGL_gl_LIBRARY} 
		${OPENGL_glu_LIBRARY}
		GLU
		glut
	)
ENDIF(UNIX)

IF(WIN32)
	TARGET_LINK_LIBRARIES(${RITK_CUDA_LIBRARY}
		${ITK_LIBRARIES}
		${COMMON_LIBRARY} 
		${CORE_LIBRARY}
		${VISUALIZATION_LIBRARY}
		${CUDA_CUDA_LIBRARY}
		${QT_QTOPENGL_LIBRARY}
		${CUDA_CUDART_LIBRARY}
		${OPENGL_gl_LIBRARY} 
		${OPENGL_glu_LIBRARY}
		GLU32
	)
ENDIF(WIN32)

SET_TARGET_PROPERTIES(${RITK_CUDA_LIBRARY} PROPERTIES DEBUG_POSTFIX "d")
	
	
# Module installation
# ====================
INSTALL(FILES ${HDRS} DESTINATION dev/include/Cuda)
INSTALL(FILES ${TMPS} DESTINATION dev/include/Cuda)
INSTALL(FILES ${UI_HDRS} DESTINATION dev/include/Cuda/ui)
INSTALL(TARGETS ${RITK_CUDA_LIBRARY} CONFIGURATIONS Debug
	RUNTIME DESTINATION bin/debug
	LIBRARY DESTINATION dev/lib/debug
	ARCHIVE DESTINATION dev/lib/debug
)
INSTALL(TARGETS ${RITK_CUDA_LIBRARY} CONFIGURATIONS Release
	RUNTIME DESTINATION bin/release
	LIBRARY DESTINATION dev/lib/release
	ARCHIVE DESTINATION dev/lib/release
)
