Skip to content

Commit 6cf3a4f

Browse files
committed
refactor module
1 parent 1359bb1 commit 6cf3a4f

30 files changed

+5298
-188
lines changed
16.3 KB
Binary file not shown.

docs/md/04/00_cxxmodule.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,8 @@ Intel(R) UHD Graphics ...
179179

180180
最终代码:[下载](../../codes/04/00_cxxmodule/vk_module_demo.zip)
181181

182-
## **相关库的模块化**
183-
184-
1. GLFW : **[这里](https://github.com/Mysvac/glfw_cpp_module)**
185-
2. GLM : TODO
186-
3. stb_image : TODO
187-
4. tinyobjloader : TODO
188-
189182
## **项目模块化示例**
190183

191-
**此小节待重构!!**
192-
193184
请下载下面的示例代码,此代码将“移动摄像机”的代码进行了模块化拆分,使得结构更加清晰:
194185

195186
**[点击下载](../../codes/04/00_cxxmodule/module_code.zip)**
@@ -200,6 +191,10 @@ Intel(R) UHD Graphics ...
200191

201192
这有助于你了解 Vulkan 各组件的依赖关系,因为它们现在严格分离在多个模块文件中,而不像之前一样挤在一起。
202193

194+
## **其他库的模块化**
195+
196+
参考上一部分 **[项目模块化示例代码](../../codes/04/00_cxxmodule/module_code.zip)** ,在 `src/third` 中为你提供了其他四个库的模块接口文件。
197+
203198
---
204199

205200
第一部分:

shaders/CMakeLists.txt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ find_package(Vulkan REQUIRED)
55
set(SHADER_DIR ${CMAKE_CURRENT_SOURCE_DIR})
66
set(STAGE_VERT "-fshader-stage=vert")
77
set(STAGE_FRAG "-fshader-stage=frag")
8-
set(STAGE_COMP "-fshader-stage=comp")
98
set(GRAPHICS_VERT_SHADER ${SHADER_DIR}/graphics.vert.glsl)
109
set(GRAPHICS_FRAG_SHADER ${SHADER_DIR}/graphics.frag.glsl)
11-
set(COMPUTE_COMP_SHADER ${SHADER_DIR}/compute.comp.glsl)
1210
set(GRAPHICS_SPIRV_VERT ${SHADER_DIR}/graphics.vert.spv)
1311
set(GRAPHICS_SPIRV_FRAG ${SHADER_DIR}/graphics.frag.spv)
14-
set(COMPUTE_SPIRV_COMP ${SHADER_DIR}/compute.comp.spv)
1512

1613
add_custom_command(
1714
OUTPUT ${GRAPHICS_SPIRV_VERT}
@@ -27,13 +24,7 @@ add_custom_command(
2724
DEPENDS ${GRAPHICS_FRAG_SHADER}
2825
)
2926

30-
add_custom_command(
31-
OUTPUT ${COMPUTE_SPIRV_COMP}
32-
COMMAND ${Vulkan_GLSLC_EXECUTABLE} ${STAGE_COMP} ${COMPUTE_COMP_SHADER} -o ${COMPUTE_SPIRV_COMP}
33-
COMMENT "Compiling compute.comp.glsl to compute.comp.spv"
34-
DEPENDS ${COMPUTE_COMP_SHADER}
35-
)
3627

3728
add_custom_target(CompileShaders ALL
38-
DEPENDS ${GRAPHICS_SPIRV_VERT} ${GRAPHICS_SPIRV_FRAG} ${COMPUTE_SPIRV_COMP}
29+
DEPENDS ${GRAPHICS_SPIRV_VERT} ${GRAPHICS_SPIRV_FRAG}
3930
)

shaders/graphics.frag.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#version 450
22

3-
layout(location = 0) in vec3 fragColor;
3+
layout(set = 1, binding = 0) uniform sampler2D texSampler;
4+
5+
layout(location = 0) in vec2 fragTexCoord;
46

57
layout(location = 0) out vec4 outColor;
68

79
void main() {
8-
9-
vec2 coord = gl_PointCoord - vec2(0.5);
10-
outColor = vec4(fragColor, 0.5 - length(coord));
10+
outColor = texture(texSampler, fragTexCoord);
1111
}

shaders/graphics.vert.glsl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
#version 450
22

3-
layout(location = 0) in vec2 inPosition;
4-
layout(location = 1) in vec4 inColor;
3+
layout(binding = 0) uniform UniformBufferObject {
4+
mat4 model;
5+
mat4 view;
6+
mat4 proj;
7+
} ubo;
58

6-
layout(location = 0) out vec3 fragColor;
9+
layout(location = 0) in vec3 inPosition;
10+
layout(location = 1) in vec2 inTexCoord;
11+
12+
layout(location = 0) out vec2 fragTexCoord;
713

814
void main() {
9-
gl_PointSize = 14.0;
10-
gl_Position = vec4(inPosition.xy, 1.0, 1.0);
11-
fragColor = inColor.rgb;
15+
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
16+
fragTexCoord = inTexCoord;
1217
}

shaders/second.frag

Lines changed: 0 additions & 38 deletions
This file was deleted.

shaders/second.vert

Lines changed: 0 additions & 16 deletions
This file was deleted.

shaders/shader.frag

Lines changed: 0 additions & 29 deletions
This file was deleted.

shaders/shader.vert

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main.cpp

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,15 @@
11
import std;
2-
import glfw;
32
import vulkan_hpp;
3+
import App;
44

55
int main() {
6-
const vk::raii::Context context{};
7-
8-
// Initialize GLFW and create a window
9-
glfw::init();
10-
glfw::window_hint(glfw::CLIENT_API, glfw::NO_API);
11-
glfw::window_hint(glfw::RESIZABLE, glfw::FALSE);
12-
glfw::Window* window = glfw::create_window(800, 600, "Vulkan", nullptr, nullptr);
13-
14-
// get the required Vulkan extensions for GLFW
15-
unsigned int count = 0;
16-
const char ** extensions = glfw::get_required_instance_extensions(&count);
17-
std::vector<const char*> extension_list(extensions, extensions + count);
18-
19-
// create a Vulkan instance
20-
constexpr vk::ApplicationInfo appInfo{
21-
"Vulkan GLFW Example", 1,
22-
"No Engine",1 ,
23-
vk::makeApiVersion(1,4,0,0)
24-
};
25-
vk::InstanceCreateInfo create_info{{}, &appInfo, };
26-
create_info.setPEnabledExtensionNames(extension_list);
27-
28-
const vk::raii::Instance instance{context, create_info};
29-
30-
// create a Vulkan surface from the GLFW window
31-
glfw::VkSurfaceKHR c_surface{};
32-
if (static_cast<vk::Result>(glfw::create_window_surface(*instance, window, nullptr, &c_surface)) != vk::Result::eSuccess) {
33-
std::println(std::cerr << "Failed to create window surface!");
34-
return -1;
35-
}
36-
vk::raii::SurfaceKHR surface{ instance, c_surface };
37-
std::println("Vulkan window surface created successfully!");
38-
39-
// wait for the window to close
40-
while (!glfw::window_should_close(window)) {
41-
glfw::poll_events();
6+
try {
7+
vht::App app{};
8+
app.run();
9+
} catch (const vk::SystemError& e) {
10+
std::cerr << e.code().message() << std::endl;
11+
std::cerr << e.what() << std::endl;
12+
} catch (const std::exception& e) {
13+
std::cerr << e.what() << std::endl;
4214
}
43-
44-
// clean up
45-
glfw::destroy_window(window);
46-
glfw::terminate();
4715
}

0 commit comments

Comments
 (0)