🧊

搭建开发环境

目标:建立基于CMake的工程,安装Vulkan SDK并下载需要的依赖

Hello World

首先创建一个基于CMake的Hello World项目,目录结构如下:
📦Ash ┣ 📜CMakeLists.txt ┗ 📜main.cpp
CMakeLists.txt:
cmake_minimum_required(VERSION 3.27) project(Ash) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED True) add_executable(Ash main.cpp)
这里我们使用了C++20标准
main.cpp:
#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
使用CLion打开Ash文件夹,或者用CMake生成VS/Xcode的项目文件,即可正常调试/运行

Vulkan SDK

官网下载最新的Vulkan SDK进行安装,安装时不要勾选任何第三方库,防止和后续手动下载的库产生冲突
安装好后在CMakeLists.txt添加以下内容来查找SDK:
# Locate Vulkan SDK message(STATUS "Attempting auto locate Vulkan using CMake......") cmake_path(SET VULKAN_PATH $ENV{VULKAN_SDK}) if (${VULKAN_PATH} STREQUAL "" OR NOT EXISTS "${VULKAN_PATH}") message(FATAL_ERROR "Error: Unable to locate Vulkan SDK, please check if the Vulkan SDK is installed correctly.") endif() message(STATUS "Found Vulkan: ${VULKAN_PATH}") target_include_directories(Ash PUBLIC ${VULKAN_PATH}/Include) target_link_directories(Ash PUBLIC ${VULKAN_PATH}/Bin PUBLIC ${VULKAN_PATH}/Lib;) # Find glslangValidator find_program(GLSL_VALIDATOR glslangValidator HINTS /usr/bin /usr/local/bin $ENV{VULKAN_SDK}/Bin/ $ENV{VULKAN_SDK}/Bin32/) if(NOT EXISTS "${GLSL_VALIDATOR}") message(FATAL_ERROR "Error: Unable to find glslangValidator, please check if the Vulkan SDK is installed correctly.") endif() message(STATUS "Found glslangValidator: ${GLSL_VALIDATOR}")

第三方库

其他的第三方库我们使用CMake的FetchContent功能自动进行下载
需要下载以下依赖:
  • glm - 数学库
  • SDL3 - 跨平台窗口和输入
  • stb - 图片加载
 
在CMakeLists.txt添加以下内容来进行下载:
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) include(FetchContent) set(GLM_ENABLE_CXX_20 ON) FetchContent_Declare( glm GIT_REPOSITORY https://github.com/icaven/glm.git GIT_TAG dca38025fba63bb9284023e6de55f756b9e37cec GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(glm) target_link_libraries(Ash PUBLIC glm::glm-header-only) set(SDL_SHARED OFF) set(SDL_STATIC ON) FetchContent_Declare( SDL GIT_REPOSITORY https://github.com/libsdl-org/SDL.git GIT_TAG prerelease-3.1.1 GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(SDL) target_link_libraries(Ash PUBLIC SDL3::SDL3) FetchContent_Declare( stb GIT_REPOSITORY https://github.com/nothings/stb.git GIT_TAG ae721c50eaf761660b4f90cc590453cdb0c2acd0 GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(stb) target_include_directories(Ash PUBLIC ${stb_SOURCE_DIR}) set(LVK_DEPLOY_DEPS OFF) set(LVK_WITH_GLFW OFF) set(LVK_WITH_SAMPLES OFF) FetchContent_Declare( lvk GIT_REPOSITORY https://github.com/litianqi/lightweightvk.git GIT_TAG 1c803afb0dc75bff7803a90374bc1951a4dd93a7 GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(lvk) target_link_libraries(Ash PUBLIC LVKLibrary) FetchContent_Declare( simdjson GIT_REPOSITORY https://github.com/simdjson/simdjson.git GIT_TAG v3.8.0 GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(simdjson) target_link_libraries(Ash PUBLIC simdjson::simdjson) set(FASTGLTF_COMPILE_AS_CPP20 ON) FetchContent_Declare( fastgltf GIT_REPOSITORY https://github.com/spnda/fastgltf.git GIT_TAG v0.7.1 GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(fastgltf) target_link_libraries(Ash PUBLIC fastgltf::fastgltf) set(SPDLOG_USE_STD_FORMAT ON) FetchContent_Declare( spdlog GIT_REPOSITORY https://github.com/gabime/spdlog.git GIT_TAG v1.13.0 GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(spdlog) target_link_libraries(Ash PUBLIC spdlog::spdlog_header_only) FetchContent_Declare( slot_map GIT_REPOSITORY https://github.com/SergeyMakeev/slot_map.git GIT_TAG b8ac8ebd89aa1cd18f20ce6e4ad1cac716f1933f GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(slot_map) target_link_libraries(Ash PUBLIC slot_map)
完成后执行CMake的Configure
第一次会失败,这是因为LightweightVK的依赖还没有下载,进入LightweightVK所在文件夹,执行python deploy_deps.py,再次Configure即可