Skip to content

Commit 8cf93b3

Browse files
committed
Updating examples
1 parent bc8be3f commit 8cf93b3

File tree

146 files changed

+20509
-8419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+20509
-8419
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Language: Cpp
22
Standard: Cpp11
33
BasedOnStyle: "Chromium"
4-
ColumnLimit: 128
4+
ColumnLimit: 160
55
IndentWidth: 4
66
UseTab: Never
77

base/CMakeLists.txt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
set(TARGET_NAME base)
22

3-
set(IMGUI_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../external/imgui")
4-
file(GLOB IMGUI_SOURCES
5-
${IMGUI_DIR}/*.c
6-
${IMGUI_DIR}/*.cpp
7-
${IMGUI_DIR}/*.h
8-
${IMGUI_DIR}/*.hpp
9-
)
10-
add_library(imgui STATIC ${IMGUI_SOURCES})
11-
set_target_properties(imgui PROPERTIES FOLDER "externals")
12-
133

14-
set(SHADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../data/shaders")
4+
set(SHADER_DIR "${PROJECT_SOURCE_DIR}/data/shaders")
155
file(GLOB_RECURSE SHADERS
166
${SHADER_DIR}/*.vert
177
${SHADER_DIR}/*.frag
@@ -20,25 +10,30 @@ file(GLOB_RECURSE SHADERS
2010
${SHADER_DIR}/*.tese
2111
${SHADER_DIR}/*.geom
2212
)
13+
GroupSources("data/shaders")
14+
2315

2416
GroupSources("base")
25-
GroupSources("data/shaders")
2617
foreach(SHADER ${SHADERS})
2718
compile_spirv_shader(${SHADER})
2819
list(APPEND COMPILED_SHADERS ${COMPILE_SPIRV_SHADER_RETURN})
2920
source_group("compiled" FILES ${COMPILE_SPIRV_SHADER_RETURN})
3021
endforeach()
3122
add_custom_target(shaders SOURCES ${SHADERS} ${COMPILED_SHADERS})
23+
set_target_properties(shaders PROPERTIES FOLDER "common")
3224

3325
file(GLOB_RECURSE COMMON_SOURCE *.cpp *.h *.hpp)
3426
add_library(${TARGET_NAME} STATIC ${COMMON_SOURCE})
27+
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "common")
28+
add_dependencies(${TARGET_NAME} shaders)
3529
target_link_libraries(${TARGET_NAME} imgui)
3630

3731
target_glfw3()
3832
target_glm()
3933
target_gli()
4034
target_vulkan()
4135
target_assimp()
36+
target_imgui()
4237

4338
if (ANDROID)
4439
add_dependencies(${TARGET_NAME} app-glue)

base/camera.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Camera {
2121
yawPitch.y = std::max(std::min(yawPitch.y, MAX_PITCH), -MAX_PITCH);
2222
}
2323
while (abs(yawPitch.x) > M_PI) {
24-
yawPitch.x += 2.0f * (float)((yawPitch.x > 0) ? -M_PI : M_PI);
24+
yawPitch.x += (float)((yawPitch.x > 0) ? -M_PI : M_PI);
2525
}
2626

2727
if (type == CameraType::firstperson) {

base/common.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,5 @@ extern android_app* global_android_app;
150150
example->run(); \
151151
delete(example); \
152152
ENTRY_POINT_END
153+
154+
#define VULKAN_EXAMPLE_MAIN() RUN_EXAMPLE(VulkanExample)

base/vks/ui.cpp renamed to base/ui.cpp

Lines changed: 39 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
*/
88

99
#include "ui.hpp"
10-
#include "helpers.hpp"
1110

12-
#include "../../external/imgui/imgui.h"
11+
#include "vks/helpers.hpp"
12+
#include "vks/pipelines.hpp"
1313

14-
using namespace vks;
15-
using namespace vks::ui;
14+
#include "../external/imgui/imgui.h"
15+
#include "utils.hpp"
16+
17+
using namespace vkx;
18+
using namespace vkx::ui;
1619

1720
void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {
1821
this->createInfo = createInfo;
1922
#if defined(__ANDROID__)
2023
if (android::screenDensity >= ACONFIGURATION_DENSITY_XXHIGH) {
2124
scale = 3.5f;
22-
}
23-
else if (android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
25+
} else if (android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
2426
scale = 2.5f;
25-
}
26-
else if (android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
27+
} else if (android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
2728
scale = 2.0f;
2829
};
2930
#endif
@@ -47,15 +48,14 @@ void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {
4748
prepareResources();
4849
if (createInfo.renderPass) {
4950
renderPass = createInfo.renderPass;
50-
}
51-
else {
51+
} else {
5252
prepareRenderPass();
5353
}
5454
preparePipeline();
5555
}
5656

5757
/** Free up all Vulkan resources acquired by the UI overlay */
58-
UIOverlay::~UIOverlay() { }
58+
UIOverlay::~UIOverlay() {}
5959

6060
void UIOverlay::destroy() {
6161
if (commandPool) {
@@ -76,8 +76,7 @@ void UIOverlay::destroy() {
7676
}
7777

7878
/** Prepare all vulkan resources required to render the UI overlay */
79-
void UIOverlay::prepareResources()
80-
{
79+
void UIOverlay::prepareResources() {
8180
ImGuiIO& io = ImGui::GetIO();
8281

8382
// Create font texture
@@ -178,76 +177,44 @@ void UIOverlay::prepareResources()
178177

179178
/** Prepare a separate pipeline for the UI overlay rendering decoupled from the main application */
180179
void UIOverlay::preparePipeline() {
181-
182180
// Setup graphics pipeline for UI rendering
183-
vk::PipelineInputAssemblyStateCreateInfo inputAssemblyState{ {}, vk::PrimitiveTopology::eTriangleList };
184-
vk::PipelineRasterizationStateCreateInfo rasterizationState;
185-
rasterizationState.lineWidth = 1.0f;
181+
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder(context.device, pipelineLayout, renderPass);
182+
pipelineBuilder.depthStencilState = { false };
183+
pipelineBuilder.rasterizationState.cullMode = vk::CullModeFlagBits::eNone;
186184

187185
// Enable blending
188-
vk::PipelineColorBlendAttachmentState blendAttachmentState;
189-
blendAttachmentState.blendEnable = VK_TRUE;
190-
blendAttachmentState.colorWriteMask = vks::util::fullColorWriteMask();
191-
blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
192-
blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
193-
blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
194-
blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;
195-
196-
std::vector<vk::PipelineColorBlendAttachmentState> blendStates(createInfo.attachmentCount);
186+
pipelineBuilder.colorBlendState.blendAttachmentStates.resize(createInfo.attachmentCount);
197187
for (uint32_t i = 0; i < createInfo.attachmentCount; i++) {
198-
blendStates[i] = blendAttachmentState;
188+
auto& blendAttachmentState = pipelineBuilder.colorBlendState.blendAttachmentStates[i];
189+
blendAttachmentState.blendEnable = VK_TRUE;
190+
blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
191+
blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
192+
blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
193+
blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;
199194
}
200195

201-
vk::PipelineColorBlendStateCreateInfo colorBlendState;
202-
colorBlendState.attachmentCount = static_cast<uint32_t>(blendStates.size());
203-
colorBlendState.pAttachments = blendStates.data();
204-
205-
vk::PipelineDepthStencilStateCreateInfo depthStencilState;
206-
207-
vk::PipelineViewportStateCreateInfo viewportState{ {}, 1, nullptr, 1, nullptr };
208-
vk::PipelineMultisampleStateCreateInfo multisampleState;
209-
multisampleState.rasterizationSamples = createInfo.rasterizationSamples;
210-
211-
212-
std::vector<vk::DynamicState> dynamicStateEnables = {
213-
vk::DynamicState::eViewport,
214-
vk::DynamicState::eScissor,
215-
};
216-
vk::PipelineDynamicStateCreateInfo dynamicState{ {}, (uint32_t)dynamicStateEnables.size(), dynamicStateEnables.data() };
217-
218-
vk::GraphicsPipelineCreateInfo pipelineCreateInfo;
219-
pipelineCreateInfo.layout = pipelineLayout;
220-
pipelineCreateInfo.renderPass = renderPass;
221-
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
222-
pipelineCreateInfo.pRasterizationState = &rasterizationState;
223-
pipelineCreateInfo.pColorBlendState = &colorBlendState;
224-
pipelineCreateInfo.pMultisampleState = &multisampleState;
225-
pipelineCreateInfo.pViewportState = &viewportState;
226-
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
227-
pipelineCreateInfo.pDynamicState = &dynamicState;
228-
pipelineCreateInfo.stageCount = static_cast<uint32_t>(createInfo.shaders.size());
229-
pipelineCreateInfo.pStages = createInfo.shaders.data();
196+
pipelineBuilder.multisampleState.rasterizationSamples = createInfo.rasterizationSamples;
230197

198+
// Load default shaders if not specified by example
199+
if (!createInfo.shaders.empty()) {
200+
pipelineBuilder.shaderStages = createInfo.shaders;
201+
} else {
202+
pipelineBuilder.loadShader(vkx::getAssetPath() + "shaders/base/uioverlay.vert.spv", vk::ShaderStageFlagBits::eVertex);
203+
pipelineBuilder.loadShader(getAssetPath() + "shaders/base/uioverlay.frag.spv", vk::ShaderStageFlagBits::eFragment);
204+
}
231205

232206
// Vertex bindings an attributes based on ImGui vertex definition
233-
vk::VertexInputBindingDescription vertexInputBindings{ 0, sizeof(ImDrawVert), vk::VertexInputRate::eVertex };
234-
std::vector<vk::VertexInputAttributeDescription> vertexInputAttributes = {
235-
vk::VertexInputAttributeDescription{ 0, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, pos) }, // Location 0: Position
236-
vk::VertexInputAttributeDescription{ 1, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, uv) }, // Location 1: UV
237-
vk::VertexInputAttributeDescription{ 2, 0, vk::Format::eR8G8B8Unorm, offsetof(ImDrawVert, col) }, // Location 0: Color
207+
pipelineBuilder.vertexInputState.bindingDescriptions = { { 0, sizeof(ImDrawVert), vk::VertexInputRate::eVertex } };
208+
pipelineBuilder.vertexInputState.attributeDescriptions = {
209+
{ 0, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, pos) }, // Location 0: Position
210+
{ 1, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, uv) }, // Location 1: UV
211+
{ 2, 0, vk::Format::eR8G8B8Unorm, offsetof(ImDrawVert, col) }, // Location 0: Color
238212
};
239-
vk::PipelineVertexInputStateCreateInfo vertexInputState;
240-
vertexInputState.vertexBindingDescriptionCount = 1;
241-
vertexInputState.pVertexBindingDescriptions = &vertexInputBindings;
242-
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexInputAttributes.size());
243-
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data();
244-
pipelineCreateInfo.pVertexInputState = &vertexInputState;
245-
pipeline = context.device.createGraphicsPipeline(context.pipelineCache, pipelineCreateInfo);
213+
pipeline = pipelineBuilder.create(context.pipelineCache);
246214
}
247215

248216
/** Prepare a separate render pass for rendering the UI as an overlay */
249-
void UIOverlay::prepareRenderPass()
250-
{
217+
void UIOverlay::prepareRenderPass() {
251218
vk::AttachmentDescription attachments[2] = {};
252219

253220
// Color attachment
@@ -265,8 +232,8 @@ void UIOverlay::prepareRenderPass()
265232
attachments[1].stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
266233
attachments[1].finalLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
267234

268-
vk::AttachmentReference colorReference { 0, vk::ImageLayout::eColorAttachmentOptimal };
269-
vk::AttachmentReference depthReference { 1, vk::ImageLayout::eDepthStencilAttachmentOptimal };
235+
vk::AttachmentReference colorReference{ 0, vk::ImageLayout::eColorAttachmentOptimal };
236+
vk::AttachmentReference depthReference{ 1, vk::ImageLayout::eDepthStencilAttachmentOptimal };
270237
vk::SubpassDependency subpassDependencies[2];
271238

272239
// Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commmands executed outside of the actual renderpass)

base/vks/ui.hpp renamed to base/ui.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#pragma once
1010

11-
#include "context.hpp"
11+
#include "vks/context.hpp"
1212

13-
namespace vks { namespace ui {
13+
namespace vkx { namespace ui {
1414

1515
struct UIOverlayCreateInfo {
1616
vk::Queue copyQueue;

base/utils.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "utils.hpp"
2+
3+
#include <mutex>
4+
#include <algorithm>
5+
6+
const std::string& vkx::getAssetPath() {
7+
#if defined(__ANDROID__)
8+
static const std::string NOTHING;
9+
return NOTHING;
10+
#else
11+
static std::string path;
12+
static std::once_flag once;
13+
std::call_once(once, [] {
14+
std::string file(__FILE__);
15+
std::replace(file.begin(), file.end(), '\\', '/');
16+
std::string::size_type lastSlash = file.rfind("/");
17+
file = file.substr(0, lastSlash);
18+
path = file + "/../data/";
19+
});
20+
return path;
21+
#endif
22+
23+
}

base/utils.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace vkx {
6+
const std::string& getAssetPath();
7+
}

0 commit comments

Comments
 (0)