1# Copyright (c) Meta Platforms, Inc. and affiliates. 2# All rights reserved. 3# 4# This source code is licensed under the BSD-style license found in the 5# LICENSE file in the root directory of this source tree. 6 7# ### Editing this file ### 8# 9# This file should be formatted with 10# ~~~ 11# cmake-format -i ATenVulkan.cmake 12# ~~~ 13# It should also be cmake-lint clean. 14# 15# The targets in this file will be built if EXECUTORCH_BUILD_VULKAN is ON 16 17if(NOT PYTHON_EXECUTABLE) 18 message( 19 "WARNING: PYTHON_EXECUTABLE is not set! A failure is likely imminent." 20 ) 21endif() 22 23if(NOT EXECUTORCH_ROOT) 24 message("WARNING: EXECUTORCH_ROOT is not set! A failure is likely imminent.") 25endif() 26 27if(ANDROID) 28 if(NOT ANDROID_NDK) 29 message(FATAL_ERROR "ANDROID_NDK not set") 30 endif() 31 32 set(GLSLC_PATH 33 "${ANDROID_NDK}/shader-tools/${ANDROID_NDK_HOST_SYSTEM_NAME}/glslc" 34 ) 35else() 36 find_program(GLSLC_PATH glslc PATHS $ENV{PATH}) 37 38 if(NOT GLSLC_PATH) 39 message(FATAL_ERROR "USE_VULKAN glslc not found") 40 endif() 41endif() 42 43# Required to enable linking with --whole-archive 44include(${EXECUTORCH_ROOT}/build/Utils.cmake) 45 46function(gen_vulkan_shader_lib_cpp shaders_path) 47 set(VULKAN_SHADERGEN_ENV "") 48 set(VULKAN_SHADERGEN_OUT_PATH ${CMAKE_BINARY_DIR}/${ARGV1}) 49 50 execute_process( 51 COMMAND 52 "${PYTHON_EXECUTABLE}" 53 ${EXECUTORCH_ROOT}/backends/vulkan/runtime/gen_vulkan_spv.py --glsl-path 54 ${shaders_path} --output-path ${VULKAN_SHADERGEN_OUT_PATH} 55 --glslc-path=${GLSLC_PATH} --tmp-dir-path=${VULKAN_SHADERGEN_OUT_PATH} 56 --env ${VULKAN_GEN_ARG_ENV} 57 RESULT_VARIABLE error_code 58 ) 59 60 set(generated_spv_cpp 61 ${VULKAN_SHADERGEN_OUT_PATH}/spv.cpp 62 PARENT_SCOPE 63 ) 64endfunction() 65 66function(vulkan_shader_lib library_name generated_spv_cpp) 67 add_library(${library_name} STATIC ${generated_spv_cpp}) 68 target_include_directories( 69 ${library_name} 70 PRIVATE 71 ${EXECUTORCH_ROOT}/.. 72 ${EXECUTORCH_ROOT}/backends/vulkan/third-party/Vulkan-Headers/include 73 ${EXECUTORCH_ROOT}/backends/vulkan/third-party/volk 74 ) 75 target_link_libraries(${library_name} vulkan_backend) 76 target_compile_options(${library_name} PRIVATE ${VULKAN_CXX_FLAGS}) 77 # Link this library with --whole-archive due to dynamic shader registrations 78 target_link_options_shared_lib(${library_name}) 79endfunction() 80 81# Convenience macro to generate a SPIR-V shader library target. Given the path 82# to the shaders to compile and the name of the library, it will create a static 83# library containing the generated SPIR-V shaders. The generated_spv_cpp 84# variable can be used to reference the generated CPP file outside the macro. 85macro(vulkan_shader_library shaders_path library_name) 86 set(VULKAN_SHADERGEN_ENV "") 87 set(VULKAN_SHADERGEN_OUT_PATH ${CMAKE_BINARY_DIR}/${library_name}) 88 89 # execute_process( COMMAND "${PYTHON_EXECUTABLE}" 90 # ${EXECUTORCH_ROOT}/backends/vulkan/runtime/gen_vulkan_spv.py --glsl-path 91 # ${shaders_path} --output-path ${VULKAN_SHADERGEN_OUT_PATH} 92 # --glslc-path=${GLSLC_PATH} --tmp-dir-path=${VULKAN_SHADERGEN_OUT_PATH} --env 93 # ${VULKAN_GEN_ARG_ENV} RESULT_VARIABLE error_code ) set(ENV{PYTHONPATH} 94 # ${PYTHONPATH}) 95 96 set(generated_spv_cpp ${VULKAN_SHADERGEN_OUT_PATH}/spv.cpp) 97 98 add_library(${library_name} STATIC ${generated_spv_cpp}) 99 target_include_directories( 100 ${library_name} 101 PRIVATE 102 ${EXECUTORCH_ROOT}/.. 103 ${EXECUTORCH_ROOT}/backends/vulkan/third-party/Vulkan-Headers/include 104 ${EXECUTORCH_ROOT}/backends/vulkan/third-party/volk 105 ) 106 target_link_libraries(${library_name} vulkan_backend) 107 target_compile_options(${library_name} PRIVATE ${VULKAN_CXX_FLAGS}) 108 # Link this library with --whole-archive due to dynamic shader registrations 109 target_link_options_shared_lib(${library_name}) 110 111 unset(VULKAN_SHADERGEN_ENV) 112 unset(VULKAN_SHADERGEN_OUT_PATH) 113endmacro() 114