Przeglądaj źródła

Add shaders and cmake support

Kajetan Johannes Hammerle 2 miesięcy temu
rodzic
commit
eab0b48411
4 zmienionych plików z 53 dodań i 0 usunięć
  1. 7 0
      CMakeLists.txt
  2. 18 0
      cmake/add_shader.cmake
  3. 8 0
      shaders/fragment.frag.glsl
  4. 20 0
      shaders/vertex.vert.glsl

+ 7 - 0
CMakeLists.txt

@@ -77,5 +77,12 @@ target_sources(gaming_core PUBLIC
 )
 install(TARGETS gaming_core FILE_SET HEADERS)
 
+include("cmake/add_shader.cmake")
+
+add_shader(NAME shader SOURCES
+    shaders/vertex.vert.glsl
+    shaders/fragment.frag.glsl
+)
+
 add_executable(test ${SRC_TESTS})
 target_link_libraries(test PRIVATE gaming_core)

+ 18 - 0
cmake/add_shader.cmake

@@ -0,0 +1,18 @@
+function(add_shader)
+    cmake_parse_arguments(args
+        "" "NAME" "SOURCES" ${ARGN}
+    )
+    if("${args_NAME}" STREQUAL "")
+        message( FATAL_ERROR "add_shader misses NAME" )
+    endif()
+
+    set(output ${CMAKE_BINARY_DIR}/shader/${args_NAME}.spv)
+
+    add_custom_command(
+        OUTPUT ${output}
+        COMMAND glslangValidator -V -o ${output} ${args_SOURCES}
+        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+        DEPENDS ${args_SOURCES}
+    )
+    add_custom_target(${args_NAME} ALL DEPENDS ${output})
+endfunction()

+ 8 - 0
shaders/fragment.frag.glsl

@@ -0,0 +1,8 @@
+#version 450
+
+layout(location = 0) out vec4 outColor;
+layout(location = 0) in vec3 fragColor;
+
+void main() {
+    outColor = vec4(fragColor, 1.0);
+}

+ 20 - 0
shaders/vertex.vert.glsl

@@ -0,0 +1,20 @@
+#version 450
+
+layout(location = 0) out vec3 fragColor;
+
+vec2 positions[3] = vec2[](
+    vec2(0.0, -0.5),
+    vec2(0.5, 0.5),
+    vec2(-0.5, 0.5)
+);
+
+vec3 colors[3] = vec3[](
+    vec3(1.0, 0.0, 0.0),
+    vec3(0.0, 1.0, 0.0),
+    vec3(0.0, 0.0, 1.0)
+);
+
+void main() {
+    gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
+    fragColor = colors[gl_VertexIndex];
+}