Skip to content

Commit

Permalink
[功能增强与构建系统优化]: 引入 Vulkan 支持并对 CMake 配置进行细化
Browse files Browse the repository at this point in the history
- 细化了 GitHub Actions 工作流,区分了 macOS 和 Ubuntu 的构建条件。
- 在 `CMakeLists.txt` 中新增 `BUILD_VULKAN` 选项,允许用户选择是否编译 Vulkan 相关代码。
- 对示例项目和 GPU 图形模块的 CMake 配置进行了调整,根据 Vulkan 构建选项包含或排除源文件。
- 更新了 Qt 的 `.pro` 文件,为 Windows 平台添加了条件编译定义和源文件。
- 根据 Vulkan 支持更新了主窗口和其他相关类的实现。
- 新增了 `gpudata.hpp` 和 `gpudata.cc` 文件,为 GPU 渲染提供顶点和索引数据。
- 调整了 OpenGL 和 Vulkan 着色器代码,以适应新的数据结构和命名规范。
- 对 Vulkan 渲染器的代码进行了扩展,包括索引缓冲区的创建和管理。
- 更新了着色器文件,以确保它们与 Vulkan 渲染器兼容。
  • Loading branch information
RealChuan committed Aug 16, 2024
1 parent 9ac4565 commit 92fd676
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 126 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,19 @@ jobs:
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} `
-G "${{ matrix.generators }}"
cmake --build ./build --config ${{ matrix.build_type }}
- name: Configure and build on ubuntu or macos
if: startsWith(matrix.os, 'ubuntu') || startsWith(matrix.os, 'macos')
- name: Configure and build on macos
if: startsWith(matrix.os, 'macos')
shell: bash
run: |
cmake \
-S . \
-B ./build \
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
-DBUILD_VULKAN=OFF \
-G "${{ matrix.generators }}"
cmake --build ./build --config ${{ matrix.build_type }}
- name: Configure and build on ubuntu
if: startsWith(matrix.os, 'ubuntu')
shell: bash
run: |
cmake \
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ project(
HOMEPAGE_URL "https://github.com/RealChuan/Qt-Graphics"
LANGUAGES CXX)

option(BUILD_VULKAN "build vulkan" ON)

include(cmake/common.cmake)

find_package(Qt6 REQUIRED COMPONENTS Gui Widgets Network Core5Compat Concurrent
Expand Down
10 changes: 7 additions & 3 deletions examples/graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ set(PROJECT_SOURCES
subtitlsplicingwidget.cc
subtitlsplicingwidget.hpp
viewer.cc
viewer.hpp
vulkanviewer.cc
vulkanviewer.hpp)
viewer.hpp)

if(BUILD_VULKAN)
list(APPEND PROJECT_SOURCES vulkanviewer.cc vulkanviewer.hpp)
endif()
qt_add_executable(Qt-Graphics MANUAL_FINALIZATION ${PROJECT_SOURCES})
if(BUILD_VULKAN)
target_compile_definitions(Qt-Graphics PRIVATE "BUILD_VULKAN")
endif()

target_link_libraries(
Qt-Graphics
Expand Down
14 changes: 10 additions & 4 deletions examples/graphics/graphics.pro
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ SOURCES += \
selectionwidget.cc \
stretchparamssettingdailog.cc \
subtitlsplicingwidget.cc \
viewer.cc \
vulkanviewer.cc
viewer.cc

HEADERS += \
capturewidget.hpp \
Expand All @@ -59,5 +58,12 @@ HEADERS += \
selectionwidget.hpp \
stretchparamssettingdailog.hpp \
subtitlsplicingwidget.hpp \
viewer.hpp \
vulkanviewer.hpp
viewer.hpp

win32{
SOURCES += vulkanviewer.cc

HEADERS += vulkanviewer.hpp

DEFINES += BUILD_VULKAN
}
10 changes: 9 additions & 1 deletion examples/graphics/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "openglviewer.hpp"
#include "recordwidget.hpp"
#include "subtitlsplicingwidget.hpp"
#ifdef BUILD_VULKAN
#include "vulkanviewer.hpp"
#endif

#include <utils/utils.h>

Expand All @@ -20,22 +22,26 @@ class MainWindow::MainWindowPrivate
drawWidget = new DrawWidget(q_ptr);
imageViewer = new ImageViewer(q_ptr);
openglViewer = new OpenglViewer(q_ptr);
vulkanViewer = new VulkanViewer(q_ptr);
subtitlSplicingWidget = new SubtitlSplicingWidget(q_ptr);
stackedWidget = new QStackedWidget(q_ptr);
stackedWidget->addWidget(imageViewer);
stackedWidget->addWidget(drawWidget);
stackedWidget->addWidget(subtitlSplicingWidget);
stackedWidget->addWidget(openglViewer);
#ifdef BUILD_VULKAN
vulkanViewer = new VulkanViewer(q_ptr);
stackedWidget->addWidget(vulkanViewer);
#endif
}
~MainWindowPrivate() {}

QWidget *q_ptr;
DrawWidget *drawWidget;
ImageViewer *imageViewer;
OpenglViewer *openglViewer;
#ifdef BUILD_VULKAN
VulkanViewer *vulkanViewer;
#endif
SubtitlSplicingWidget *subtitlSplicingWidget;
QStackedWidget *stackedWidget;
};
Expand Down Expand Up @@ -80,9 +86,11 @@ void MainWindow::initMenuBar()
menu->addAction(tr("Opengl Viewer"), this, [this] {
d_ptr->stackedWidget->setCurrentWidget(d_ptr->openglViewer);
});
#ifdef BUILD_VULKAN
menu->addAction(tr("Vulakn Viewer"), this, [this] {
d_ptr->stackedWidget->setCurrentWidget(d_ptr->vulkanViewer);
});
#endif
menu->addAction(tr("Draw"), this, [this] {
d_ptr->stackedWidget->setCurrentWidget(d_ptr->drawWidget);
});
Expand Down
65 changes: 32 additions & 33 deletions src/gpugraphics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
set(PROJECT_SOURCES
gpugraphics_global.hpp
openglshaderprogram.cc
openglshaderprogram.hpp
openglview.cc
openglview.hpp
vulkanrenderer.cc
vulkanrenderer.hpp
vulkanview.cc
vulkanview.hpp)
set(PROJECT_SOURCES gpugraphics_global.hpp openglshaderprogram.cc
openglshaderprogram.hpp openglview.cc openglview.hpp)

if(BUILD_VULKAN)
list(APPEND PROJECT_SOURCES vulkanrenderer.cc vulkanrenderer.hpp
vulkanview.cc vulkanview.hpp)
endif()

qt_add_resources(SOURCES shader.qrc)

Expand All @@ -19,29 +16,31 @@ if(CMAKE_HOST_WIN32)
target_compile_definitions(gpugraphics PRIVATE "GPUGRAPHICS_LIBRARY")
endif()

file(GLOB_RECURSE GLSL_SOURCE_FILES "shader/vulkan*")
if(BUILD_VULKAN)
file(GLOB_RECURSE GLSL_SOURCE_FILES "shader/vulkan*")

foreach(GLSL ${GLSL_SOURCE_FILES})
get_filename_component(FILE_NAME ${GLSL} NAME)
set(SPIRV "${PROJECT_BINARY_DIR}/shaders/vulkan/${FILE_NAME}.spv")
add_custom_command(
OUTPUT ${SPIRV}
COMMAND ${CMAKE_COMMAND} -E make_directory
"${PROJECT_BINARY_DIR}/shaders/vulkan/"
COMMAND glslc ${GLSL} -o ${SPIRV}
DEPENDS ${GLSL})
list(APPEND SPIRV_BINARY_FILES ${SPIRV})
endforeach(GLSL)

add_custom_target(TXShaders DEPENDS ${SPIRV_BINARY_FILES})

add_dependencies(gpugraphics TXShaders)

foreach(GLSL ${GLSL_SOURCE_FILES})
get_filename_component(FILE_NAME ${GLSL} NAME)
set(SPIRV "${PROJECT_BINARY_DIR}/shaders/vulkan/${FILE_NAME}.spv")
add_custom_command(
OUTPUT ${SPIRV}
TARGET gpugraphics
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"${PROJECT_BINARY_DIR}/shaders/vulkan/"
COMMAND glslc ${GLSL} -o ${SPIRV}
DEPENDS ${GLSL})
list(APPEND SPIRV_BINARY_FILES ${SPIRV})
endforeach(GLSL)

add_custom_target(TXShaders DEPENDS ${SPIRV_BINARY_FILES})

add_dependencies(gpugraphics TXShaders)

add_custom_command(
TARGET gpugraphics
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"${EXECUTABLE_OUTPUT_PATH}/vulkan_shader/"
COMMAND
${CMAKE_COMMAND} -E copy_directory "${PROJECT_BINARY_DIR}/shaders/vulkan"
"${EXECUTABLE_OUTPUT_PATH}/vulkan_shader")
"${EXECUTABLE_OUTPUT_PATH}/vulkan_shader/"
COMMAND
${CMAKE_COMMAND} -E copy_directory "${PROJECT_BINARY_DIR}/shaders/vulkan"
"${EXECUTABLE_OUTPUT_PATH}/vulkan_shader")
endif()
2 changes: 2 additions & 0 deletions src/gpugraphics/gpudata.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "gpudata.hpp"

14 changes: 14 additions & 0 deletions src/gpugraphics/gpudata.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace GpuGraphics {

static float vertices[] = {
// positions // texture coords
1.0F, 1.0F, 0.0F, 1.0F, 1.0F, // top right
1.0F, -1.0F, 0.0F, 1.0F, 0.0F, // bottom right
-1.0F, -1.0F, 0.0F, 0.0F, 0.0F, // bottom left
-1.0F, 1.0F, 0.0F, 0.0F, 1.0F // top left
};
static unsigned int indices[] = {0, 1, 3, 1, 2, 3};

} // namespace GpuGraphics
19 changes: 13 additions & 6 deletions src/gpugraphics/gpugraphics.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ DEFINES += GPUGRAPHICS_LIBRARY
TARGET = $$replaceLibName(gpugraphics)

HEADERS += \
gpudata.hpp \
gpugraphics_global.hpp \
openglshaderprogram.hpp \
openglview.hpp \
vulkanrenderer.hpp \
vulkanview.hpp
openglview.hpp

SOURCES += \
gpudata.cc \
openglshaderprogram.cc \
openglview.cc \
vulkanrenderer.cc \
vulkanview.cc
openglview.cc

RESOURCES += \
shader.qrc

win32{
HEADERS += \
vulkanrenderer.hpp \
vulkanview.hpp

SOURCES += \
vulkanrenderer.cc \
vulkanview.cc
}
18 changes: 5 additions & 13 deletions src/gpugraphics/openglshaderprogram.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "openglshaderprogram.hpp"
#include "gpudata.hpp"

#include <QOpenGLBuffer>

Expand Down Expand Up @@ -27,19 +28,10 @@ OpenGLShaderProgram::~OpenGLShaderProgram()
clear();
}

void OpenGLShaderProgram::initVertex(const QString &pos, const QString &texCord)
void OpenGLShaderProgram::initVertex(const QString &pos, const QString &texCoord)
{
auto posAttr = attributeLocation(pos);
auto texCordAttr = attributeLocation(texCord);

float vertices[] = {
// positions // texture coords
1.0F, 1.0F, 0.0F, 1.0F, 1.0F, // top right
1.0F, -1.0F, 0.0F, 1.0F, 0.0F, // bottom right
-1.0F, -1.0F, 0.0F, 0.0F, 0.0F, // bottom left
-1.0F, 1.0F, 0.0F, 0.0F, 1.0F // top left
};
unsigned int indices[] = {0, 1, 3, 1, 2, 3};
auto texCoordAttr = attributeLocation(texCoord);

d_ptr->vbo.destroy();
d_ptr->vbo.create();
Expand All @@ -53,8 +45,8 @@ void OpenGLShaderProgram::initVertex(const QString &pos, const QString &texCord)

setAttributeBuffer(posAttr, GL_FLOAT, 0, 3, sizeof(float) * 5);
enableAttributeArray(posAttr);
setAttributeBuffer(texCordAttr, GL_FLOAT, 3 * sizeof(float), 2, sizeof(float) * 5);
enableAttributeArray(texCordAttr);
setAttributeBuffer(texCoordAttr, GL_FLOAT, 3 * sizeof(float), 2, sizeof(float) * 5);
enableAttributeArray(texCoordAttr);
}

void OpenGLShaderProgram::clear()
Expand Down
2 changes: 1 addition & 1 deletion src/gpugraphics/openglshaderprogram.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class OpenGLShaderProgram : public QOpenGLShaderProgram, public QOpenGLFunctions
explicit OpenGLShaderProgram(QObject *parent = nullptr);
~OpenGLShaderProgram() override;

void initVertex(const QString &pos, const QString &texCord);
void initVertex(const QString &pos, const QString &texCoord);

void clear();

Expand Down
2 changes: 1 addition & 1 deletion src/gpugraphics/openglview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void OpenglView::initializeGL()
d_ptr->programPtr->link();
d_ptr->programPtr->bind();

d_ptr->programPtr->initVertex("aPos", "aTexCord");
d_ptr->programPtr->initVertex("inPosition", "inTexCoord");
initTexture();

d_ptr->programPtr->release();
Expand Down
6 changes: 3 additions & 3 deletions src/gpugraphics/shader/texture.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

uniform sampler2D tex;

layout(location = 0) out vec4 FragColor;
in vec2 TexCord;
layout(location = 0) out vec4 fragOutColor;
in vec2 fragTexCoord;

void main()
{
FragColor = texture(tex, TexCord);
fragOutColor = texture(tex, fragTexCoord);
}
10 changes: 5 additions & 5 deletions src/gpugraphics/shader/texture.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

uniform mat4 transform;

out vec2 TexCord;
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTexCord;
out vec2 fragTexCoord;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inTexCoord;

void main()
{
TexCord = vec2(aTexCord.x, 1.0 - aTexCord.y);
gl_Position = transform * vec4(aPos, 1.0);
fragTexCoord = vec2(inTexCoord.x, 1.0 - inTexCoord.y);
gl_Position = transform * vec4(inPosition, 1.0);
}
8 changes: 4 additions & 4 deletions src/gpugraphics/shader/vulkan.frag
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#version 450
#version 440

layout(binding = 1) uniform sampler2D texSampler;
layout(binding = 1) uniform sampler2D tex;

layout(location = 0) out vec4 outColor;
layout(location = 0) out vec4 fragOutColor;
layout(location = 0) in vec2 fragTexCoord;

void main()
{
outColor = texture(texSampler, fragTexCoord);
fragOutColor = texture(tex, fragTexCoord);
}

10 changes: 5 additions & 5 deletions src/gpugraphics/shader/vulkan.vert
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#version 450
#version 440

layout(binding = 0, std140) uniform UniformBufferObject
{
mat4 transform;
} ubo;
}ubo;

layout(location = 0) out vec2 fragTexCoord;
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inTexCoord;
layout(location = 0) in vec4 inPosition;

void main()
{
fragTexCoord = inTexCoord;
gl_Position = ubo.transform * inPosition;
fragTexCoord = vec2(inTexCoord.x, 1.0 - inTexCoord.y);
gl_Position = ubo.transform * vec4(inPosition, 1.0);
}
Loading

0 comments on commit 92fd676

Please sign in to comment.