cmake_minimum_required(VERSION 3.24)

project(video_shader LANGUAGES C CXX RC)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/apps/LoopThroughWithOpenGLCompositing")
set(SLANG_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/3rdParty/slang-2026.8-windows-x86_64" CACHE PATH "Path to a Slang binary release containing bin/slangc.exe")

if(NOT EXISTS "${APP_DIR}/LoopThroughWithOpenGLCompositing.cpp")
	message(FATAL_ERROR "Imported app sources were not found under ${APP_DIR}")
endif()

set(SLANG_RUNTIME_FILES
	"${SLANG_ROOT}/bin/slangc.exe"
	"${SLANG_ROOT}/bin/slang-compiler.dll"
	"${SLANG_ROOT}/bin/slang-glslang.dll"
)

foreach(SLANG_RUNTIME_FILE IN LISTS SLANG_RUNTIME_FILES)
	if(NOT EXISTS "${SLANG_RUNTIME_FILE}")
		message(FATAL_ERROR "Required Slang runtime file not found: ${SLANG_RUNTIME_FILE}")
	endif()
endforeach()

set(SLANG_LICENSE_FILE "${SLANG_ROOT}/LICENSE")
if(NOT EXISTS "${SLANG_LICENSE_FILE}")
	message(FATAL_ERROR "Slang license file not found: ${SLANG_LICENSE_FILE}")
endif()

set(APP_SOURCES
	"${APP_DIR}/videoio/decklink/DeckLinkAPI_i.c"
	"${APP_DIR}/control/ControlServer.cpp"
	"${APP_DIR}/control/ControlServer.h"
	"${APP_DIR}/control/OscServer.cpp"
	"${APP_DIR}/control/OscServer.h"
	"${APP_DIR}/control/RuntimeControlBridge.cpp"
	"${APP_DIR}/control/RuntimeControlBridge.h"
	"${APP_DIR}/control/RuntimeServices.cpp"
	"${APP_DIR}/control/RuntimeServices.h"
	"${APP_DIR}/videoio/decklink/DeckLinkAPI_h.h"
	"${APP_DIR}/videoio/decklink/DeckLinkDisplayMode.cpp"
	"${APP_DIR}/videoio/decklink/DeckLinkDisplayMode.h"
	"${APP_DIR}/videoio/decklink/DeckLinkFrameTransfer.cpp"
	"${APP_DIR}/videoio/decklink/DeckLinkFrameTransfer.h"
	"${APP_DIR}/videoio/decklink/DeckLinkSession.cpp"
	"${APP_DIR}/videoio/decklink/DeckLinkSession.h"
	"${APP_DIR}/videoio/decklink/DeckLinkVideoIOFormat.cpp"
	"${APP_DIR}/videoio/decklink/DeckLinkVideoIOFormat.h"
	"${APP_DIR}/gl/renderer/GLExtensions.cpp"
	"${APP_DIR}/gl/renderer/GLExtensions.h"
	"${APP_DIR}/gl/shader/GlobalParamsBuffer.cpp"
	"${APP_DIR}/gl/shader/GlobalParamsBuffer.h"
	"${APP_DIR}/gl/renderer/GlRenderConstants.h"
	"${APP_DIR}/gl/renderer/GlScopedObjects.h"
	"${APP_DIR}/gl/shader/GlShaderSources.cpp"
	"${APP_DIR}/gl/shader/GlShaderSources.h"
	"${APP_DIR}/gl/OpenGLComposite.cpp"
	"${APP_DIR}/gl/OpenGLComposite.h"
	"${APP_DIR}/gl/OpenGLCompositeRuntimeControls.cpp"
	"${APP_DIR}/gl/pipeline/OpenGLRenderPass.cpp"
	"${APP_DIR}/gl/pipeline/OpenGLRenderPass.h"
	"${APP_DIR}/gl/pipeline/OpenGLRenderPipeline.cpp"
	"${APP_DIR}/gl/pipeline/OpenGLRenderPipeline.h"
	"${APP_DIR}/gl/renderer/OpenGLRenderer.cpp"
	"${APP_DIR}/gl/renderer/OpenGLRenderer.h"
	"${APP_DIR}/gl/pipeline/OpenGLVideoIOBridge.cpp"
	"${APP_DIR}/gl/pipeline/OpenGLVideoIOBridge.h"
	"${APP_DIR}/gl/shader/OpenGLShaderPrograms.cpp"
	"${APP_DIR}/gl/shader/OpenGLShaderPrograms.h"
	"${APP_DIR}/gl/pipeline/PngScreenshotWriter.cpp"
	"${APP_DIR}/gl/pipeline/PngScreenshotWriter.h"
	"${APP_DIR}/gl/shader/ShaderProgramCompiler.cpp"
	"${APP_DIR}/gl/shader/ShaderProgramCompiler.h"
	"${APP_DIR}/gl/shader/ShaderBuildQueue.cpp"
	"${APP_DIR}/gl/shader/ShaderBuildQueue.h"
	"${APP_DIR}/gl/shader/ShaderTextureBindings.cpp"
	"${APP_DIR}/gl/shader/ShaderTextureBindings.h"
	"${APP_DIR}/gl/shader/Std140Buffer.h"
	"${APP_DIR}/gl/shader/TextRasterizer.cpp"
	"${APP_DIR}/gl/shader/TextRasterizer.h"
	"${APP_DIR}/gl/shader/TextureAssetLoader.cpp"
	"${APP_DIR}/gl/shader/TextureAssetLoader.h"
	"${APP_DIR}/gl/pipeline/TemporalHistoryBuffers.cpp"
	"${APP_DIR}/gl/pipeline/TemporalHistoryBuffers.h"
	"${APP_DIR}/LoopThroughWithOpenGLCompositing.cpp"
	"${APP_DIR}/LoopThroughWithOpenGLCompositing.h"
	"${APP_DIR}/LoopThroughWithOpenGLCompositing.rc"
	"${APP_DIR}/platform/NativeHandles.h"
	"${APP_DIR}/platform/NativeSockets.h"
	"${APP_DIR}/resource.h"
	"${APP_DIR}/runtime/RuntimeHost.cpp"
	"${APP_DIR}/runtime/RuntimeHost.h"
	"${APP_DIR}/runtime/RuntimeClock.cpp"
	"${APP_DIR}/runtime/RuntimeClock.h"
	"${APP_DIR}/runtime/RuntimeJson.cpp"
	"${APP_DIR}/runtime/RuntimeJson.h"
	"${APP_DIR}/runtime/RuntimeParameterUtils.cpp"
	"${APP_DIR}/runtime/RuntimeParameterUtils.h"
	"${APP_DIR}/shader/ShaderCompiler.cpp"
	"${APP_DIR}/shader/ShaderCompiler.h"
	"${APP_DIR}/shader/ShaderPackageRegistry.cpp"
	"${APP_DIR}/shader/ShaderPackageRegistry.h"
	"${APP_DIR}/shader/ShaderTypes.h"
	"${APP_DIR}/stdafx.cpp"
	"${APP_DIR}/stdafx.h"
	"${APP_DIR}/targetver.h"
	"${APP_DIR}/videoio/VideoIOFormat.cpp"
	"${APP_DIR}/videoio/VideoIOFormat.h"
	"${APP_DIR}/videoio/VideoIOTypes.h"
	"${APP_DIR}/videoio/VideoPlayoutScheduler.cpp"
	"${APP_DIR}/videoio/VideoPlayoutScheduler.h"
)

add_executable(LoopThroughWithOpenGLCompositing WIN32 ${APP_SOURCES})

target_include_directories(LoopThroughWithOpenGLCompositing PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/control"
	"${APP_DIR}/gl"
	"${APP_DIR}/gl/pipeline"
	"${APP_DIR}/gl/renderer"
	"${APP_DIR}/gl/shader"
	"${APP_DIR}/platform"
	"${APP_DIR}/runtime"
	"${APP_DIR}/shader"
	"${APP_DIR}/videoio"
	"${APP_DIR}/videoio/decklink"
)

target_link_libraries(LoopThroughWithOpenGLCompositing PRIVATE
	opengl32
	glu32
	Ws2_32
	Crypt32
	Advapi32
	Gdiplus
	Ole32
	Windowscodecs
)

target_compile_definitions(LoopThroughWithOpenGLCompositing PRIVATE
	_UNICODE
	UNICODE
)

if(MSVC)
	target_compile_options(LoopThroughWithOpenGLCompositing PRIVATE /W3)
endif()

add_executable(RuntimeJsonTests
	"${APP_DIR}/runtime/RuntimeJson.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/RuntimeJsonTests.cpp"
)

target_include_directories(RuntimeJsonTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/runtime"
)

if(MSVC)
	target_compile_options(RuntimeJsonTests PRIVATE /W3)
endif()

enable_testing()
add_test(NAME RuntimeJsonTests COMMAND RuntimeJsonTests)

add_executable(RuntimeClockTests
	"${APP_DIR}/runtime/RuntimeClock.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/RuntimeClockTests.cpp"
)

target_include_directories(RuntimeClockTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/runtime"
)

if(MSVC)
	target_compile_options(RuntimeClockTests PRIVATE /W3)
endif()

add_test(NAME RuntimeClockTests COMMAND RuntimeClockTests)

add_executable(RuntimeParameterUtilsTests
	"${APP_DIR}/runtime/RuntimeJson.cpp"
	"${APP_DIR}/runtime/RuntimeParameterUtils.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/RuntimeParameterUtilsTests.cpp"
)

target_include_directories(RuntimeParameterUtilsTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/runtime"
	"${APP_DIR}/shader"
)

if(MSVC)
	target_compile_options(RuntimeParameterUtilsTests PRIVATE /W3)
endif()

add_test(NAME RuntimeParameterUtilsTests COMMAND RuntimeParameterUtilsTests)

add_executable(Std140BufferTests
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/Std140BufferTests.cpp"
)

target_include_directories(Std140BufferTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/gl"
	"${APP_DIR}/gl/shader"
)

if(MSVC)
	target_compile_options(Std140BufferTests PRIVATE /W3)
endif()

add_test(NAME Std140BufferTests COMMAND Std140BufferTests)

add_executable(ShaderPackageRegistryTests
	"${APP_DIR}/runtime/RuntimeJson.cpp"
	"${APP_DIR}/shader/ShaderPackageRegistry.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/ShaderPackageRegistryTests.cpp"
)

target_include_directories(ShaderPackageRegistryTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/runtime"
	"${APP_DIR}/shader"
)

if(MSVC)
	target_compile_options(ShaderPackageRegistryTests PRIVATE /W3)
endif()

add_test(NAME ShaderPackageRegistryTests COMMAND ShaderPackageRegistryTests)

add_executable(ShaderSlangValidationTests
	"${APP_DIR}/runtime/RuntimeJson.cpp"
	"${APP_DIR}/shader/ShaderCompiler.cpp"
	"${APP_DIR}/shader/ShaderPackageRegistry.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/ShaderSlangValidationTests.cpp"
)

target_include_directories(ShaderSlangValidationTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/platform"
	"${APP_DIR}/runtime"
	"${APP_DIR}/shader"
)

if(MSVC)
	target_compile_options(ShaderSlangValidationTests PRIVATE /W3)
endif()

add_test(NAME ShaderSlangValidationTests COMMAND ShaderSlangValidationTests)
set_tests_properties(ShaderSlangValidationTests PROPERTIES
	ENVIRONMENT "SLANG_ROOT=${SLANG_ROOT}"
)

add_executable(OscServerTests
	"${APP_DIR}/control/OscServer.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/OscServerTests.cpp"
)

target_include_directories(OscServerTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/control"
	"${APP_DIR}/platform"
)

target_link_libraries(OscServerTests PRIVATE
	Ws2_32
)

if(MSVC)
	target_compile_options(OscServerTests PRIVATE /W3)
endif()

add_test(NAME OscServerTests COMMAND OscServerTests)

add_executable(VideoIOFormatTests
	"${APP_DIR}/videoio/decklink/DeckLinkVideoIOFormat.cpp"
	"${APP_DIR}/videoio/VideoIOFormat.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/VideoIOFormatTests.cpp"
)

target_include_directories(VideoIOFormatTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/videoio"
	"${APP_DIR}/videoio/decklink"
)

if(MSVC)
	target_compile_options(VideoIOFormatTests PRIVATE /W3)
endif()

add_test(NAME VideoIOFormatTests COMMAND VideoIOFormatTests)

add_executable(VideoPlayoutSchedulerTests
	"${APP_DIR}/videoio/VideoPlayoutScheduler.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/VideoPlayoutSchedulerTests.cpp"
)

target_include_directories(VideoPlayoutSchedulerTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/videoio"
	"${APP_DIR}/videoio/decklink"
)

if(MSVC)
	target_compile_options(VideoPlayoutSchedulerTests PRIVATE /W3)
endif()

add_test(NAME VideoPlayoutSchedulerTests COMMAND VideoPlayoutSchedulerTests)

add_executable(VideoIODeviceFakeTests
	"${APP_DIR}/videoio/VideoIOFormat.cpp"
	"${CMAKE_CURRENT_SOURCE_DIR}/tests/VideoIODeviceFakeTests.cpp"
)

target_include_directories(VideoIODeviceFakeTests PRIVATE
	"${APP_DIR}"
	"${APP_DIR}/videoio"
	"${APP_DIR}/videoio/decklink"
)

if(MSVC)
	target_compile_options(VideoIODeviceFakeTests PRIVATE /W3)
endif()

add_test(NAME VideoIODeviceFakeTests COMMAND VideoIODeviceFakeTests)

install(TARGETS LoopThroughWithOpenGLCompositing
	RUNTIME DESTINATION "."
)

install(FILES ${SLANG_RUNTIME_FILES}
	DESTINATION "3rdParty/slang/bin"
)

install(FILES "${SLANG_LICENSE_FILE}"
	DESTINATION "third_party_notices"
	RENAME "SLANG_LICENSE.txt"
)

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/SHADER_CONTRACT.md"
	DESTINATION "."
)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/config/"
	DESTINATION "config"
)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/shaders/"
	DESTINATION "shaders"
)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/runtime/templates/"
	DESTINATION "runtime/templates"
)

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/runtime/README.md"
	DESTINATION "runtime"
)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ui/dist/"
	DESTINATION "ui/dist"
	OPTIONAL
)

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/docs/"
	DESTINATION "docs"
	OPTIONAL
)

source_group(TREE "${APP_DIR}" FILES ${APP_SOURCES})
