Skip to content

Commit

Permalink
Updating examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jherico committed Jan 2, 2018
1 parent bc8be3f commit 8cf93b3
Show file tree
Hide file tree
Showing 146 changed files with 20,509 additions and 8,419 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Language: Cpp
Standard: Cpp11
BasedOnStyle: "Chromium"
ColumnLimit: 128
ColumnLimit: 160
IndentWidth: 4
UseTab: Never

Expand Down
19 changes: 7 additions & 12 deletions base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
set(TARGET_NAME base)

set(IMGUI_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../external/imgui")
file(GLOB IMGUI_SOURCES
${IMGUI_DIR}/*.c
${IMGUI_DIR}/*.cpp
${IMGUI_DIR}/*.h
${IMGUI_DIR}/*.hpp
)
add_library(imgui STATIC ${IMGUI_SOURCES})
set_target_properties(imgui PROPERTIES FOLDER "externals")


set(SHADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../data/shaders")
set(SHADER_DIR "${PROJECT_SOURCE_DIR}/data/shaders")
file(GLOB_RECURSE SHADERS
${SHADER_DIR}/*.vert
${SHADER_DIR}/*.frag
Expand All @@ -20,25 +10,30 @@ file(GLOB_RECURSE SHADERS
${SHADER_DIR}/*.tese
${SHADER_DIR}/*.geom
)
GroupSources("data/shaders")


GroupSources("base")
GroupSources("data/shaders")
foreach(SHADER ${SHADERS})
compile_spirv_shader(${SHADER})
list(APPEND COMPILED_SHADERS ${COMPILE_SPIRV_SHADER_RETURN})
source_group("compiled" FILES ${COMPILE_SPIRV_SHADER_RETURN})
endforeach()
add_custom_target(shaders SOURCES ${SHADERS} ${COMPILED_SHADERS})
set_target_properties(shaders PROPERTIES FOLDER "common")

file(GLOB_RECURSE COMMON_SOURCE *.cpp *.h *.hpp)
add_library(${TARGET_NAME} STATIC ${COMMON_SOURCE})
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "common")
add_dependencies(${TARGET_NAME} shaders)
target_link_libraries(${TARGET_NAME} imgui)

target_glfw3()
target_glm()
target_gli()
target_vulkan()
target_assimp()
target_imgui()

if (ANDROID)
add_dependencies(${TARGET_NAME} app-glue)
Expand Down
2 changes: 1 addition & 1 deletion base/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Camera {
yawPitch.y = std::max(std::min(yawPitch.y, MAX_PITCH), -MAX_PITCH);
}
while (abs(yawPitch.x) > M_PI) {
yawPitch.x += 2.0f * (float)((yawPitch.x > 0) ? -M_PI : M_PI);
yawPitch.x += (float)((yawPitch.x > 0) ? -M_PI : M_PI);
}

if (type == CameraType::firstperson) {
Expand Down
2 changes: 2 additions & 0 deletions base/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,5 @@ extern android_app* global_android_app;
example->run(); \
delete(example); \
ENTRY_POINT_END

#define VULKAN_EXAMPLE_MAIN() RUN_EXAMPLE(VulkanExample)
111 changes: 39 additions & 72 deletions base/vks/ui.cpp → base/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@
*/

#include "ui.hpp"
#include "helpers.hpp"

#include "../../external/imgui/imgui.h"
#include "vks/helpers.hpp"
#include "vks/pipelines.hpp"

using namespace vks;
using namespace vks::ui;
#include "../external/imgui/imgui.h"
#include "utils.hpp"

using namespace vkx;
using namespace vkx::ui;

void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {
this->createInfo = createInfo;
#if defined(__ANDROID__)
if (android::screenDensity >= ACONFIGURATION_DENSITY_XXHIGH) {
scale = 3.5f;
}
else if (android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
} else if (android::screenDensity >= ACONFIGURATION_DENSITY_XHIGH) {
scale = 2.5f;
}
else if (android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
} else if (android::screenDensity >= ACONFIGURATION_DENSITY_HIGH) {
scale = 2.0f;
};
#endif
Expand All @@ -47,15 +48,14 @@ void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {
prepareResources();
if (createInfo.renderPass) {
renderPass = createInfo.renderPass;
}
else {
} else {
prepareRenderPass();
}
preparePipeline();
}

/** Free up all Vulkan resources acquired by the UI overlay */
UIOverlay::~UIOverlay() { }
UIOverlay::~UIOverlay() {}

void UIOverlay::destroy() {
if (commandPool) {
Expand All @@ -76,8 +76,7 @@ void UIOverlay::destroy() {
}

/** Prepare all vulkan resources required to render the UI overlay */
void UIOverlay::prepareResources()
{
void UIOverlay::prepareResources() {
ImGuiIO& io = ImGui::GetIO();

// Create font texture
Expand Down Expand Up @@ -178,76 +177,44 @@ void UIOverlay::prepareResources()

/** Prepare a separate pipeline for the UI overlay rendering decoupled from the main application */
void UIOverlay::preparePipeline() {

// Setup graphics pipeline for UI rendering
vk::PipelineInputAssemblyStateCreateInfo inputAssemblyState{ {}, vk::PrimitiveTopology::eTriangleList };
vk::PipelineRasterizationStateCreateInfo rasterizationState;
rasterizationState.lineWidth = 1.0f;
vks::pipelines::GraphicsPipelineBuilder pipelineBuilder(context.device, pipelineLayout, renderPass);
pipelineBuilder.depthStencilState = { false };
pipelineBuilder.rasterizationState.cullMode = vk::CullModeFlagBits::eNone;

// Enable blending
vk::PipelineColorBlendAttachmentState blendAttachmentState;
blendAttachmentState.blendEnable = VK_TRUE;
blendAttachmentState.colorWriteMask = vks::util::fullColorWriteMask();
blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;

std::vector<vk::PipelineColorBlendAttachmentState> blendStates(createInfo.attachmentCount);
pipelineBuilder.colorBlendState.blendAttachmentStates.resize(createInfo.attachmentCount);
for (uint32_t i = 0; i < createInfo.attachmentCount; i++) {
blendStates[i] = blendAttachmentState;
auto& blendAttachmentState = pipelineBuilder.colorBlendState.blendAttachmentStates[i];
blendAttachmentState.blendEnable = VK_TRUE;
blendAttachmentState.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
blendAttachmentState.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
blendAttachmentState.srcAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
blendAttachmentState.dstAlphaBlendFactor = vk::BlendFactor::eZero;
}

vk::PipelineColorBlendStateCreateInfo colorBlendState;
colorBlendState.attachmentCount = static_cast<uint32_t>(blendStates.size());
colorBlendState.pAttachments = blendStates.data();

vk::PipelineDepthStencilStateCreateInfo depthStencilState;

vk::PipelineViewportStateCreateInfo viewportState{ {}, 1, nullptr, 1, nullptr };
vk::PipelineMultisampleStateCreateInfo multisampleState;
multisampleState.rasterizationSamples = createInfo.rasterizationSamples;


std::vector<vk::DynamicState> dynamicStateEnables = {
vk::DynamicState::eViewport,
vk::DynamicState::eScissor,
};
vk::PipelineDynamicStateCreateInfo dynamicState{ {}, (uint32_t)dynamicStateEnables.size(), dynamicStateEnables.data() };

vk::GraphicsPipelineCreateInfo pipelineCreateInfo;
pipelineCreateInfo.layout = pipelineLayout;
pipelineCreateInfo.renderPass = renderPass;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pColorBlendState = &colorBlendState;
pipelineCreateInfo.pMultisampleState = &multisampleState;
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.stageCount = static_cast<uint32_t>(createInfo.shaders.size());
pipelineCreateInfo.pStages = createInfo.shaders.data();
pipelineBuilder.multisampleState.rasterizationSamples = createInfo.rasterizationSamples;

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

// Vertex bindings an attributes based on ImGui vertex definition
vk::VertexInputBindingDescription vertexInputBindings{ 0, sizeof(ImDrawVert), vk::VertexInputRate::eVertex };
std::vector<vk::VertexInputAttributeDescription> vertexInputAttributes = {
vk::VertexInputAttributeDescription{ 0, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, pos) }, // Location 0: Position
vk::VertexInputAttributeDescription{ 1, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, uv) }, // Location 1: UV
vk::VertexInputAttributeDescription{ 2, 0, vk::Format::eR8G8B8Unorm, offsetof(ImDrawVert, col) }, // Location 0: Color
pipelineBuilder.vertexInputState.bindingDescriptions = { { 0, sizeof(ImDrawVert), vk::VertexInputRate::eVertex } };
pipelineBuilder.vertexInputState.attributeDescriptions = {
{ 0, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, pos) }, // Location 0: Position
{ 1, 0, vk::Format::eR32G32Sfloat, offsetof(ImDrawVert, uv) }, // Location 1: UV
{ 2, 0, vk::Format::eR8G8B8Unorm, offsetof(ImDrawVert, col) }, // Location 0: Color
};
vk::PipelineVertexInputStateCreateInfo vertexInputState;
vertexInputState.vertexBindingDescriptionCount = 1;
vertexInputState.pVertexBindingDescriptions = &vertexInputBindings;
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexInputAttributes.size());
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data();
pipelineCreateInfo.pVertexInputState = &vertexInputState;
pipeline = context.device.createGraphicsPipeline(context.pipelineCache, pipelineCreateInfo);
pipeline = pipelineBuilder.create(context.pipelineCache);
}

/** Prepare a separate render pass for rendering the UI as an overlay */
void UIOverlay::prepareRenderPass()
{
void UIOverlay::prepareRenderPass() {
vk::AttachmentDescription attachments[2] = {};

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

vk::AttachmentReference colorReference { 0, vk::ImageLayout::eColorAttachmentOptimal };
vk::AttachmentReference depthReference { 1, vk::ImageLayout::eDepthStencilAttachmentOptimal };
vk::AttachmentReference colorReference{ 0, vk::ImageLayout::eColorAttachmentOptimal };
vk::AttachmentReference depthReference{ 1, vk::ImageLayout::eDepthStencilAttachmentOptimal };
vk::SubpassDependency subpassDependencies[2];

// Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commmands executed outside of the actual renderpass)
Expand Down
4 changes: 2 additions & 2 deletions base/vks/ui.hpp → base/ui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#pragma once

#include "context.hpp"
#include "vks/context.hpp"

namespace vks { namespace ui {
namespace vkx { namespace ui {

struct UIOverlayCreateInfo {
vk::Queue copyQueue;
Expand Down
23 changes: 23 additions & 0 deletions base/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "utils.hpp"

#include <mutex>
#include <algorithm>

const std::string& vkx::getAssetPath() {
#if defined(__ANDROID__)
static const std::string NOTHING;
return NOTHING;
#else
static std::string path;
static std::once_flag once;
std::call_once(once, [] {
std::string file(__FILE__);
std::replace(file.begin(), file.end(), '\\', '/');
std::string::size_type lastSlash = file.rfind("/");
file = file.substr(0, lastSlash);
path = file + "/../data/";
});
return path;
#endif

}
7 changes: 7 additions & 0 deletions base/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <string>

namespace vkx {
const std::string& getAssetPath();
}
Loading

0 comments on commit 8cf93b3

Please sign in to comment.