##============================================================================
##  Copyright (c) Kitware, Inc.
##  All rights reserved.
##  See LICENSE.txt for details.
##  This software is distributed WITHOUT ANY WARRANTY; without even
##  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
##  PURPOSE.  See the above copyright notice for more information.
##
##  Copyright 2016 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
##  Copyright 2016 UT-Battelle, LLC.
##  Copyright 2016 Los Alamos National Security.
##
##  Under the terms of Contract DE-NA0003525 with NTESS,
##  the U.S. Government retains certain rights in this software.
##
##  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
##  Laboratory (LANL), the U.S. Government retains certain rights in
##  this software.
##============================================================================

set(headers
  Actor.h
  AxisAnnotation.h
  AxisAnnotation2D.h
  AxisAnnotation3D.h
  BitmapFont.h
  BitmapFontFactory.h
  BoundingBoxAnnotation.h
  Camera.h
  Canvas.h
  CanvasRayTracer.h
  Color.h
  ColorBarAnnotation.h
  ColorLegendAnnotation.h
  ColorTable.h
  ConnectivityProxy.h
  DecodePNG.h
  LineRenderer.h
  MatrixHelpers.h
  Scene.h
  Mapper.h
  MapperRayTracer.h
  MapperVolume.h
  MapperConnectivity.h
  MapperWireframer.h
  TextAnnotation.h
  TextAnnotationBillboard.h
  TextAnnotationScreen.h
  TextRenderer.h
  Texture2D.h
  Triangulator.h
  View.h
  View1D.h
  View2D.h
  View3D.h
  WorldAnnotator.h
  )

set(sources
  Actor.cxx
  AxisAnnotation.cxx
  AxisAnnotation2D.cxx
  AxisAnnotation3D.cxx
  BitmapFont.cxx
  BitmapFontFactory.cxx
  BoundingBoxAnnotation.cxx
  Camera.cxx
  Canvas.cxx
  CanvasRayTracer.cxx
  Color.cxx
  ColorBarAnnotation.cxx
  ColorLegendAnnotation.cxx
  ColorTable.cxx
  ConnectivityProxy.cxx
  DecodePNG.cxx
  LineRenderer.cxx
  Mapper.cxx
  MapperRayTracer.cxx
  MapperVolume.cxx
  MapperConnectivity.cxx
  MapperWireframer.cxx
  Scene.cxx
  TextAnnotation.cxx
  TextAnnotationBillboard.cxx
  TextAnnotationScreen.cxx
  TextRenderer.cxx
  View.cxx
  View1D.cxx
  View2D.cxx
  View3D.cxx
  WorldAnnotator.cxx

  internal/RunTriangulator.cxx
  raytracing/BoundingVolumeHierarchy.cxx
  raytracing/Camera.cxx
  raytracing/ChannelBuffer.cxx
  raytracing/Logger.cxx
  raytracing/RayTracer.cxx
  raytracing/RayOperations.cxx
  raytracing/VolumeRendererStructured.cxx
  )

set(opengl_headers
  CanvasGL.h
  MapperGL.h
  TextureGL.h
  WorldAnnotatorGL.h
  )

set(opengl_sources
  CanvasGL.cxx
  MapperGL.cxx
  TextureGL.cxx
  WorldAnnotatorGL.cxx
  )

set(egl_headers
  CanvasEGL.h
)

set(egl_sources
  CanvasEGL.cxx
  )

set(osmesa_headers
  CanvasOSMesa.h
  )

set(osmesa_sources
  CanvasOSMesa.cxx
  )

# This list of sources has code that uses devices and so might need to be
# compiled with a device-specific compiler (like CUDA).
set(device_sources
  Mapper.cxx
  MapperWireframer.cxx
  Canvas.cxx
  CanvasRayTracer.cxx
  ConnectivityProxy.cxx
  TextRenderer.cxx
  raytracing/BoundingVolumeHierarchy.cxx
  raytracing/Camera.cxx
  raytracing/ChannelBuffer.cxx
  raytracing/VolumeRendererStructured.cxx
  raytracing/RayOperations.cxx
  raytracing/RayTracer.cxx
  )
# include some vtkm-specific cmake code.
include(VTKmMacros)

#-----------------------------------------------------------------------------
# The configuration for OpenGL
#-----------------------------------------------------------------------------
if(VTKm_ENABLE_OSMESA)
  find_package(MESA ${VTKm_FIND_PACKAGE_QUIETLY})
  vtkm_create_interface(OSMesa
          DEPENDENT_VARIABLES OSMESA_FOUND
          ADD_INCLUDES ${OSMESA_INCLUDE_DIR}
          ADD_LIBRARIES ${OSMESA_LIBRARY}
          )
endif()

# Now do the real OpenGL stuff.
if(NOT VTKm_OSMesa_FOUND)
  find_package(OpenGL ${VTKm_FIND_PACKAGE_QUIETLY})

  set(vtkm_opengl_dependent_vars VTKm_Base_FOUND OPENGL_FOUND)
  set(vtkm_opengl_includes ${OPENGL_INCLUDE_DIR})
  set(vtkm_opengl_libraries ${OPENGL_LIBRARIES})
  set(vtkm_opengl_components)
else()
  # OSMesa comes with its own implementation of OpenGL. So if OSMesa has been
  # found, then simply report that OpenGL has been found and use the includes
  # and libraries already added for OSMesa.
  set(vtkm_opengl_dependent_vars)
  set(vtkm_opengl_includes)
  set(vtkm_opengl_libraries)
  set(vtkm_opengl_components OSMesa)
endif()

# Many OpenGL classes in VTK-m require GLEW (too many to try to separate them
# out and still get something worth using). So require that too.
find_package(GLEW ${VTKm_FIND_PACKAGE_QUIETLY})

list(APPEND vtkm_opengl_dependent_vars GLEW_FOUND)
if(GLEW_FOUND)
  list(APPEND vtkm_opengl_includes ${GLEW_INCLUDE_DIRS})
  list(APPEND vtkm_opengl_libraries ${GLEW_LIBRARIES})
endif()
#on unix/linux Glew uses pthreads, so we need to find that, and link to it
#explicitly or else in release mode we get sigsegv on launch
if(UNIX)
  find_package(Threads ${VTKm_FIND_PACKAGE_QUIETLY})
  list(APPEND vtkm_interop_dependent_vars CMAKE_USE_PTHREADS_INIT)
  list(APPEND vtkm_opengl_libraries ${CMAKE_THREAD_LIBS_INIT})
endif()

vtkm_create_interface(OpenGL
        DEPENDENT_VARIABLES ${vtkm_opengl_dependent_vars}
        ADD_INCLUDES ${vtkm_opengl_includes}
        ADD_LIBRARIES ${vtkm_opengl_libraries}
        ADD_COMPONENTS ${vtkm_opengl_components}
        )

find_package(EGL ${VTKm_FIND_PACKAGE_QUIETLY})

vtkm_create_interface(EGL
        IS_OPTIONAL
        DEPENDENT_VARIABLES VTKm_OpenGL_FOUND EGL_FOUND
        ADD_INCLUDES ${EGL_INCLUDE_DIRS}
        ADD_LIBRARIES ${EGL_LIBRARIES}
        )
#-----------------------------------------------------------------------------
# The configuration for rendering
#-----------------------------------------------------------------------------

if(VTKm_OpenGL_FOUND)
  list(APPEND headers ${opengl_headers})
  list(APPEND sources ${opengl_sources})

  if(VTKm_OSMesa_FOUND)
    list(APPEND headers ${osmesa_headers})
    list(APPEND sources ${osmesa_sources})
  endif()

#  vtkm_configure_component_EGL()
#  if(VTKm_EGL_FOUND)
#    list(APPEND headers ${egl_headers})
#    list(APPEND sources ${egl_sources})
#  endif()
endif()

if(VTKm_ENABLE_OSMESA AND NOT VTKm_OSMesa_FOUND)
  message(SEND_ERROR "OSMesa was requested by not properly configured.
Either make sure the OSMesa headers and library are properly found or set
VTKm_ENABLE_OSMESA to OFF.")
endif()

vtkm_declare_headers(${headers})

vtkm_library(
  SOURCES ${sources}
  WRAP_FOR_CUDA ${device_sources}
  )

# EGL Libs are added here to ensure propper linking when statically compiling.
# This is safe to do even when not using EGL as the values will then be empty.
target_link_libraries(vtkm_rendering
                      PUBLIC vtkm_cont
                      PRIVATE ${VTKm_OPENGL_LIBRARIES}
                              ${VTKm_BACKEND_LIBRARIES}
                              ${EGL_LIBRARIES}
                      )

# EGL Dirs are added here to ensure propper linking when statically compiling.
# This is safe to do even when not using EGL as the values will then be empty.
target_include_directories(vtkm_rendering
                          PRIVATE ${VTKm_OPENGL_INCLUDE_DIRS}
                                  ${VTKm_BACKEND_INCLUDE_DIRS}
                                  ${EGL_INCLUDE_DIRS}
                          )

if(UNIX AND NOT APPLE)
  target_link_libraries(vtkm_rendering PRIVATE rt )
endif()


vtkm_create_interface(Rendering
        DEPENDENT_VARIABLES VTKm_ENABLE_RENDERING VTKm_Base_FOUND
        ADD_LIBRARIES vtkm_rendering
        )

add_subdirectory(internal)
add_subdirectory(raytracing)

#-----------------------------------------------------------------------------
add_subdirectory(testing)
