Skip to content

Commit c7b5504

Browse files
author
Carmelo Fernandez Aguera
committedJan 10, 2021
Integrated shaders into the build system
1 parent f16e284 commit c7b5504

File tree

10 files changed

+151
-14
lines changed

10 files changed

+151
-14
lines changed
 

‎CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ option(REV_BUILD_TEST "Build engine unit tests" ON)
1212
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
1313
set(CMAKE_CXX_STANDARD 20)
1414

15+
# Find GLSL compiler
16+
find_package(Vulkan REQUIRED)
17+
set(VULKAN_BIN_DIR ${Vulkan_INCLUDE_DIR}/../bin/)
18+
set(GLSLANGVALIDATOR ${VULKAN_BIN_DIR}glslc.exe)
19+
1520
add_subdirectory(engine)
1621

1722
if(REV_BUILD_TOOLS)
1823
add_subdirectory(tools)
1924
endif()
2025

26+
# Compile sample projects
2127
add_subdirectory(samples/ShaderToy)
2228
add_subdirectory(samples/GltfViewer)
2329

‎cmake/common.cmake

+54
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,58 @@ macro(GroupSources curdir dirLabel)
2121
${PROJECT_SOURCE_DIR}/${curdir}/${child})
2222
endif()
2323
endforeach()
24+
endmacro()
25+
26+
#####################################################################################
27+
# Macro to add custom build for SPIR-V, with additional arbitrary glslangvalidator
28+
# flags (run glslangvalidator --help for a list of possible flags).
29+
# Inputs:
30+
# _SOURCE can be more than one file (.vert + .frag)
31+
# _OUTPUT is the .spv file, resulting from the linkage
32+
# _FLAGS are the flags to add to the command line
33+
# Outputs:
34+
# SOURCE_LIST has _SOURCE appended to it
35+
# OUTPUT_LIST has _OUTPUT appended to it
36+
#
37+
macro(_compile_GLSL_flags _SOURCE _OUTPUT _FLAGS SOURCE_LIST OUTPUT_LIST)
38+
LIST(APPEND ${SOURCE_LIST} ${_SOURCE})
39+
LIST(APPEND ${OUTPUT_LIST} ${_OUTPUT})
40+
if(GLSLANGVALIDATOR)
41+
set(_COMMAND ${GLSLANGVALIDATOR} ${_SOURCE} -o ${_OUTPUT} ${_FLAGS})
42+
add_custom_command(
43+
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${_OUTPUT}
44+
COMMAND echo ${_COMMAND}
45+
COMMAND ${_COMMAND}
46+
MAIN_DEPENDENCY ${_SOURCE}
47+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
48+
)
49+
else(GLSLANGVALIDATOR)
50+
MESSAGE(WARNING "could not find GLSLANGVALIDATOR to compile shaders")
51+
endif(GLSLANGVALIDATOR)
52+
endmacro()
53+
54+
#####################################################################################
55+
# Macro to add custom build for SPIR-V, with debug information (glslangvalidator's -g flag)
56+
# Inputs:
57+
# _SOURCE can be more than one file (.vert + .frag)
58+
# _OUTPUT is the .spv file, resulting from the linkage
59+
# Outputs:
60+
# SOURCE_LIST has _SOURCE appended to it
61+
# OUTPUT_LIST has _OUTPUT appended to it
62+
#
63+
macro(_compile_GLSL _SOURCE _OUTPUT SOURCE_LIST OUTPUT_LIST)
64+
_compile_GLSL_flags(${_SOURCE} ${_OUTPUT} "-g" ${SOURCE_LIST} ${OUTPUT_LIST})
65+
endmacro()
66+
67+
#####################################################################################
68+
# Macro to add custom build for SPIR-V, without debug information
69+
# Inputs:
70+
# _SOURCE can be more than one file (.vert + .frag)
71+
# _OUTPUT is the .spv file, resulting from the linkage
72+
# Outputs:
73+
# SOURCE_LIST has _SOURCE appended to it
74+
# OUTPUT_LIST has _OUTPUT appended to it
75+
#
76+
macro(_compile_GLSL_no_debug_info _SOURCE _OUTPUT SOURCE_LIST OUTPUT_LIST)
77+
_compile_GLSL_flags(${_SOURCE} ${_OUTPUT} "" ${SOURCE_LIST} ${OUTPUT_LIST})
2478
endmacro()

‎engine/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ project(engine)
66

77
include(../cmake/common.cmake)
88

9-
find_package(Vulkan REQUIRED)
10-
119
#######################################################################
1210
####### Create separate libraries for each module of the engine #######
1311
# Core

‎samples/gltfViewer/CMakeLists.txt

+26-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,31 @@ project(GltfViewer)
77

88
include(../../cmake/common.cmake)
99

10+
UNSET(GLSL_SOURCES)
11+
UNSET(SPV_OUTPUT)
12+
file(GLOB_RECURSE GLSL_HEADER_FILES "shaders/*.h" "shaders/*.glsl")
13+
file(GLOB_RECURSE GLSL_SOURCE_FILES
14+
"shaders/*.comp"
15+
"shaders/*.frag"
16+
"shaders/*.vert"
17+
"shaders/*.rchit"
18+
"shaders/*.rahit"
19+
"shaders/*.rmiss"
20+
"shaders/*.rgen"
21+
)
22+
foreach(GLSL ${GLSL_SOURCE_FILES})
23+
get_filename_component(FILE_NAME ${GLSL} NAME)
24+
_compile_GLSL(${GLSL} "./bin/${FILE_NAME}.spv" GLSL_SOURCES SPV_OUTPUT)
25+
endforeach(GLSL)
26+
27+
list(APPEND GLSL_SOURCES ${GLSL_HEADER_FILES})
28+
source_group(shaders FILES ${GLSL_SOURCES})
29+
1030
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp" "src/*.h" "src/*.inl")
11-
GroupSources(src GltfViewer)
12-
add_executable(GltfViewer ${SOURCE_FILES})
13-
# Visual studio specifics
31+
GroupSources(src src)
32+
add_executable(GltfViewer ${SOURCE_FILES} ${GLSL_SOURCES})
33+
set_target_properties( GltfViewer PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
34+
target_link_libraries (GltfViewer LINK_PUBLIC revCore revMath revGfx revGame revInput)
1435

15-
set_target_properties(GltfViewer PROPERTIES FOLDER "samples")
16-
target_link_libraries (GltfViewer LINK_PUBLIC revCore revMath revGfx revGame revInput)
36+
# Visual studio specifics
37+
set_target_properties(GltfViewer PROPERTIES FOLDER "samples")

‎samples/gltfViewer/bin/shaders/ui.frag

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
#version 450
2121

22+
layout(location = 0) in vec3 vVtxColor;
2223
layout(location = 0) out vec4 outColor;
2324

2425
void main() {
25-
outColor = vec4(1.0, 0.0, 0.0, 1.0);
26+
outColor = vec4(vVtxColor, 1.0);
2627
}

‎samples/gltfViewer/bin/shaders/ui.vert

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
#version 450
2121

22-
vec2 positions[3] = vec2[](
23-
vec2(0.0, -0.5),
24-
vec2(-0.5, 0.5),
25-
vec2(0.5, 0.5)
26-
);
22+
layout(location = 0) in vec2 position;
23+
layout(location = 1) in vec3 color;
24+
25+
layout(location = 0) out vec3 vVtxColor;
2726

2827
void main() {
29-
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
28+
gl_Position = vec4(position, 0.0, 1.0);
29+
vVtxColor = color;
3030
}

‎samples/gltfViewer/bin/ui.frag.spv

2.27 KB
Binary file not shown.

‎samples/gltfViewer/bin/ui.vert.spv

2.84 KB
Binary file not shown.

‎samples/gltfViewer/shaders/ui.frag

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//--------------------------------------------------------------------------------------------------
2+
// Revolution Engine
3+
//--------------------------------------------------------------------------------------------------
4+
// Copyright 2021 Carmelo J Fdez-Aguera
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
7+
// and associated documentation files (the "Software"), to deal in the Software without restriction,
8+
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
9+
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all copies or
13+
// substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16+
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
#version 450
21+
22+
layout(location = 0) in vec3 vVtxColor;
23+
layout(location = 0) out vec4 outColor;
24+
25+
void main() {
26+
outColor = vec4(vVtxColor, 1.0);
27+
}

‎samples/gltfViewer/shaders/ui.vert

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//--------------------------------------------------------------------------------------------------
2+
// Revolution Engine
3+
//--------------------------------------------------------------------------------------------------
4+
// Copyright 2021 Carmelo J Fdez-Aguera
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
7+
// and associated documentation files (the "Software"), to deal in the Software without restriction,
8+
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
9+
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all copies or
13+
// substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16+
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
#version 450
21+
22+
layout(location = 0) in vec2 position;
23+
layout(location = 1) in vec3 color;
24+
25+
layout(location = 0) out vec3 vVtxColor;
26+
27+
void main() {
28+
gl_Position = vec4(position, 0.0, 1.0);
29+
vVtxColor = color;
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.