Skip to content

Commit

Permalink
Merge pull request #348 from KomputeProject/simplify_examples_shader_…
Browse files Browse the repository at this point in the history
…compilation

Simplify interface for shader compilation
  • Loading branch information
axsaucedo authored Jan 21, 2024
2 parents 717e8ad + d83f5ee commit 6dd9e6d
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 132 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cpp_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
configure-options: -DKOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER=ON -DKOMPUTE_OPT_FROM_SOURCE=ON
build-options: --parallel # Given we don't build too many resources we can leverage parallel
- name: Run tests
run: ./examples/array_multiplication/build/src/kompute_array_mult
run: ./examples/array_multiplication/build/kompute_array_mult

logistc-regression-example:
runs-on: ubuntu-latest
Expand All @@ -55,4 +55,4 @@ jobs:
configure-options: -DKOMPUTE_OPT_USE_BUILT_IN_VULKAN_HEADER=ON -DKOMPUTE_OPT_FROM_SOURCE=ON
build-options: --parallel # Given we don't build too many resources we can leverage parallel
- name: Run tests
run: ./examples/logistic_regression/build/src/kompute_logistic_regression
run: ./examples/logistic_regression/build/kompute_logistic_regression
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ vk_swiftshader_icd.json
tmp_kp_shader.comp.spv
tmp_kp_shader.comp

# Shaders
test/shaders/glsl/*.hpp

# Docs
_build/

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ mk_cmake:
-DKOMPUTE_OPT_INSTALL=ON \
-DKOMPUTE_OPT_BUILD_TESTS=ON \
-DKOMPUTE_OPT_BUILD_DOCS=ON \
-DKOMPUTE_OPT_BUILD_SHADERS=ON \
-DKOMPUTE_OPT_BUILT_IN_VULKAN_HEADER_TAG="v1.3.275" \
-DKOMPUTE_OPT_CODE_COVERAGE=ON \
-DKOMPUTE_OPT_CODE_COVERAGE=ON \
-DKOMPUTE_OPT_USE_SPDLOG=1 \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
Expand Down
26 changes: 18 additions & 8 deletions cmake/vulkan_shader_compiler.cmake
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Setting CWD as later on it's not possible if called from outside
set_property(GLOBAL PROPERTY
VULKAN_COMPILE_SHADER_CWD_PROPERTY "${CMAKE_CURRENT_LIST_DIR}")

function(vulkan_compile_shader)
find_program(GLS_LANG_VALIDATOR_PATH NAMES glslangValidator)
if(GLS_LANG_VALIDATOR_PATH STREQUAL "GLS_LANG_VALIDATOR_PATH-NOTFOUND")
message(FATAL_ERROR "glslangValidator not found.")
return()
endif()

cmake_parse_arguments(SHADER_COMPILE "" "INFILE;OUTFILE;NAMESPACE;RELATIVE_PATH" "" ${ARGN})
cmake_parse_arguments(SHADER_COMPILE "" "INFILE;OUTFILE;NAMESPACE" "" ${ARGN})
set(SHADER_COMPILE_INFILE_FULL "${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_COMPILE_INFILE}")
set(SHADER_COMPILE_SPV_FILE_FULL "${CMAKE_CURRENT_BINARY_DIR}/${SHADER_COMPILE_INFILE}.spv")
set(SHADER_COMPILE_HEADER_FILE_FULL "${CMAKE_CURRENT_BINARY_DIR}/${SHADER_COMPILE_OUTFILE}")
set(SHADER_COMPILE_HEADER_FILE_FULL "${CMAKE_CURRENT_SOURCE_DIR}/${SHADER_COMPILE_OUTFILE}")

if(NOT SHADER_COMPILE_RELATIVE_PATH)
set(SHADER_COMPILE_RELATIVE_PATH "${PROJECT_SOURCE_DIR}/cmake")
endif()

# .comp -> .spv
add_custom_command(OUTPUT "${SHADER_COMPILE_SPV_FILE_FULL}"
COMMAND "${GLS_LANG_VALIDATOR_PATH}"
Expand All @@ -29,15 +29,25 @@ function(vulkan_compile_shader)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)

# .spv -> .hpp
## Property needs to be retrieved explicitly from globals
get_property(VULKAN_COMPILE_SHADER_CWD GLOBAL PROPERTY VULKAN_COMPILE_SHADER_CWD_PROPERTY)
## The directory may not be created so we need to ensure its present
get_filename_component(SHADER_COMPILE_SPV_PATH ${SHADER_COMPILE_SPV_FILE_FULL} DIRECTORY)
if(NOT EXISTS ${SHADER_COMPILE_SPV_PATH})
add_custom_target(build-time-make-directory ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${SHADER_COMPILE_SPV_PATH})
endif()
## Requires custom command function as this is the only way to call
## a function during compile time from cmake (ie through cmake script)
add_custom_command(OUTPUT "${SHADER_COMPILE_HEADER_FILE_FULL}"
COMMAND ${CMAKE_COMMAND}
ARGS "-DINPUT_SHADER_FILE=${SHADER_COMPILE_SPV_FILE_FULL}"
"-DOUTPUT_HEADER_FILE=${SHADER_COMPILE_HEADER_FILE_FULL}"
"-DHEADER_NAMESPACE=${SHADER_COMPILE_NAMESPACE}"
"-DIS_BIG_ENDIAN=${IS_BIG_ENDIAN}"
"-P"
"${SHADER_COMPILE_RELATIVE_PATH}/bin_file_to_header.cmake"
WORKING_DIRECTORY "${SHADER_COMPILE_RELATIVE_PATH}"
"${VULKAN_COMPILE_SHADER_CWD}/bin_file_to_header.cmake"
WORKING_DIRECTORY "${VULKAN_COMPILE_SHADER_CWD}"
COMMENT "Converting compiled shader '${SHADER_COMPILE_SPV_FILE_FULL}' to header file '${SHADER_COMPILE_HEADER_FILE_FULL}'."
MAIN_DEPENDENCY "${SHADER_COMPILE_SPV_FILE_FULL}")
endfunction()
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignoring all shaders
*/shader/*
16 changes: 11 additions & 5 deletions examples/android/android-simple/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 17)

# Options
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" 1344ece4ac278f9b3be3b4555ffaace7a032b91f)
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" 0)
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" v0.9.0)
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" ON)

set(KOMPUTE_OPT_ANDROID_BUILD ON)
set(KOMPUTE_OPT_DISABLE_VK_DEBUG_LAYERS ON)
Expand All @@ -19,9 +19,15 @@ else()
include_directories(${kompute_SOURCE_DIR}/src/include)
endif()

# Add to the list, so CMake can later find the code to compile shaders to header files
list(APPEND CMAKE_PREFIX_PATH "${kompute_SOURCE_DIR}/cmake")
add_subdirectory(shader)
# Compiling shader
# To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader
vulkan_compile_shader(
INFILE shader/my_shader.comp
OUTFILE shader/my_shader.hpp
NAMESPACE "shader")

# Then add it to the library, so you can access it later in your code
target_include_directories(shader INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

add_library(kompute-jni SHARED KomputeJniNative.cpp
KomputeModelML.cpp
Expand Down

This file was deleted.

43 changes: 16 additions & 27 deletions examples/array_multiplication/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,8 @@ project(kompute_array_mult)
set(CMAKE_CXX_STANDARD 14)

# Options
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" 1344ece4ac278f9b3be3b4555ffaace7a032b91f)
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" 0)

# Set a default build type if none was specified
# Based on: https://github.com/openchemistry/tomviz/blob/master/cmake/BuildType.cmake
set(DEFAULT_BUILD_TYPE "Release")

if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
set(DEFAULT_BUILD_TYPE "Debug")
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE STRING "Choose the type of build." FORCE)

# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

if(WIN32) # Install dlls in the same directory as the executable on Windows
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" v0.9.0)
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" ON)

if(KOMPUTE_OPT_FROM_SOURCE)
add_subdirectory(../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build)
Expand All @@ -38,8 +17,18 @@ else()
include_directories(${kompute_SOURCE_DIR}/src/include)
endif()

# Add to the list, so CMake can later find the code to compile shaders to header files
list(APPEND CMAKE_PREFIX_PATH "${kompute_SOURCE_DIR}/cmake")
# Compiling shader
# To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader
vulkan_compile_shader(
INFILE shader/my_shader.comp
OUTFILE shader/my_shader.hpp
NAMESPACE "shader")

# Then add it to the library, so you can access it later in your code
add_library(shader INTERFACE "shader/my_shader.hpp")
target_include_directories(shader INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

# Setting up main example code
add_executable(kompute_array_mult src/main.cpp)
target_link_libraries(kompute_array_mult PRIVATE shader kompute::kompute)

add_subdirectory(shader)
add_subdirectory(src)
15 changes: 0 additions & 15 deletions examples/array_multiplication/shader/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions examples/array_multiplication/src/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion examples/array_multiplication/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <memory>
#include <vector>

#include "my_shader.hpp"
#include <shader/my_shader.hpp>
#include <kompute/Kompute.hpp>

int
Expand Down
42 changes: 15 additions & 27 deletions examples/logistic_regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,8 @@ project(kompute_logistic_regression)
set(CMAKE_CXX_STANDARD 14)

# Options
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" 1344ece4ac278f9b3be3b4555ffaace7a032b91f)
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" 0)

# Set a default build type if none was specified
# Based on: https://github.com/openchemistry/tomviz/blob/master/cmake/BuildType.cmake
set(DEFAULT_BUILD_TYPE "Release")

if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
set(DEFAULT_BUILD_TYPE "Debug")
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE STRING "Choose the type of build." FORCE)

# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

if(WIN32) # Install dlls in the same directory as the executable on Windows
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()
option(KOMPUTE_OPT_GIT_TAG "The tag of the repo to use for the example" v0.9.0)
option(KOMPUTE_OPT_FROM_SOURCE "Whether to build example from source or from git fetch repo" ON)

if(KOMPUTE_OPT_FROM_SOURCE)
add_subdirectory(../../ ${CMAKE_CURRENT_BINARY_DIR}/kompute_build)
Expand All @@ -38,8 +17,17 @@ else()
include_directories(${kompute_SOURCE_DIR}/src/include)
endif()

# Add to the list, so CMake can later find the code to compile shaders to header files
list(APPEND CMAKE_PREFIX_PATH "${kompute_SOURCE_DIR}/cmake")
# Compiling shader
# To add more shaders simply copy the vulkan_compile_shader command and replace it with your new shader
vulkan_compile_shader(
INFILE shader/my_shader.comp
OUTFILE shader/my_shader.hpp
NAMESPACE "shader")

# Then add it to the library, so you can access it later in your code
add_library(shader INTERFACE "shader/my_shader.hpp")
target_include_directories(shader INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

add_subdirectory(shader)
add_subdirectory(src)
# Setting up main example code
add_executable(kompute_logistic_regression src/main.cpp)
target_link_libraries(kompute_logistic_regression PRIVATE shader kompute::kompute)
15 changes: 0 additions & 15 deletions examples/logistic_regression/shader/CMakeLists.txt

This file was deleted.

4 changes: 0 additions & 4 deletions examples/logistic_regression/src/CMakeLists.txt

This file was deleted.

5 changes: 2 additions & 3 deletions examples/logistic_regression/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
#include <memory>
#include <vector>

#include "kompute/Tensor.hpp"
#include "my_shader.hpp"
#include <kompute/Kompute.hpp>
#include "kompute/Kompute.hpp"
#include "shader/my_shader.hpp"

int
main()
Expand Down
10 changes: 5 additions & 5 deletions test/shaders/glsl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ vulkan_compile_shader(INFILE test_shader.comp
OUTFILE test_shader.hpp
NAMESPACE "kp")

add_library(test_shaders_glsl INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/test_logistic_regression_shader.hpp"
"${CMAKE_CURRENT_BINARY_DIR}/test_op_custom_shader.hpp"
"${CMAKE_CURRENT_BINARY_DIR}/test_workgroup_shader.hpp"
"${CMAKE_CURRENT_BINARY_DIR}/test_shader.hpp")
add_library(test_shaders_glsl INTERFACE test_logistic_regression_shader.hpp
test_op_custom_shader.hpp
test_workgroup_shader.hpp
test_shader.hpp)

target_include_directories(test_shaders_glsl INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
target_include_directories(test_shaders_glsl INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

0 comments on commit 6dd9e6d

Please sign in to comment.