Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(NAME VulkanCppExamples)
include(${CMAKE_SOURCE_DIR}/cmake/ezvcpkg/ezvcpkg.cmake)

ezvcpkg_fetch(
COMMIT af2287382b1991dbdcb7e5112d236f3323b9dd7a
PACKAGES assimp basisu imgui glad glfw3 gli glm vulkan
UPDATE_TOOLCHAIN
)
Expand Down
18 changes: 9 additions & 9 deletions base/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
// Vulkan!
#include <vulkan/vulkan.hpp>

#include "keycodes.hpp"
#if defined(__ANDROID__)
#include "android.hpp"
#else
#include "gl.hpp"
// Cross platform window management (except android)
#include "glfw/glfw.hpp"
#endif

using glm::ivec2;
using glm::mat3;
using glm::mat4;
Expand Down Expand Up @@ -96,15 +105,6 @@ class Vectors {
static const vec3 ZERO4;
};

#include "keycodes.hpp"
#if defined(__ANDROID__)
#include "android.hpp"

#else
#include "gl.hpp"
// Cross platform window management (except android)
#include "glfw/glfw.hpp"
#endif

// Boilerplate for running an example
#if defined(__ANDROID__)
Expand Down
26 changes: 14 additions & 12 deletions base/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
using namespace vkx;
using namespace vkx::ui;

void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {
this->createInfo = createInfo;
void UIOverlay::create(const UIOverlayCreateInfo& createInfo_) {
createInfo = createInfo_;
#if defined(__ANDROID__)
// Screen density
if (vkx::android::screenDensity >= ACONFIGURATION_DENSITY_XXXHIGH) {
Expand Down Expand Up @@ -62,6 +62,7 @@ void UIOverlay::create(const UIOverlayCreateInfo& createInfo) {

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

void UIOverlay::destroy() {
Expand All @@ -79,6 +80,7 @@ void UIOverlay::destroy() {
context.device.freeCommandBuffers(commandPool, cmdBuffers);
context.device.destroyCommandPool(commandPool);
context.device.destroyFence(fence);
commandPool = nullptr;
}
}

Expand Down Expand Up @@ -217,7 +219,7 @@ void UIOverlay::preparePipeline() {

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

// Color attachment
attachments[0].format = createInfo.colorformat;
Expand All @@ -236,7 +238,7 @@ void UIOverlay::prepareRenderPass() {

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

// Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commmands executed outside of the actual renderpass)
subpassDependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
Expand All @@ -263,11 +265,11 @@ void UIOverlay::prepareRenderPass() {

vk::RenderPassCreateInfo renderPassInfo;
renderPassInfo.attachmentCount = 2;
renderPassInfo.pAttachments = attachments;
renderPassInfo.pAttachments = attachments.data();
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpassDescription;
renderPassInfo.dependencyCount = 2;
renderPassInfo.pDependencies = subpassDependencies;
renderPassInfo.pDependencies = subpassDependencies.data();

renderPass = context.device.createRenderPass(renderPassInfo);
}
Expand All @@ -292,7 +294,7 @@ void UIOverlay::updateCommandBuffers() {

if (cmdBuffers.size()) {
context.trashAll<vk::CommandBuffer>(cmdBuffers,
[&](const std::vector<vk::CommandBuffer>& buffers) { context.device.freeCommandBuffers(commandPool, buffers); });
[this](const std::vector<vk::CommandBuffer>& buffers) { context.device.freeCommandBuffers(commandPool, buffers); });
cmdBuffers.clear();
}

Expand Down Expand Up @@ -404,8 +406,8 @@ void UIOverlay::update() {
}

// Upload data
ImDrawVert* vtxDst = (ImDrawVert*)vertexBuffer.mapped;
ImDrawIdx* idxDst = (ImDrawIdx*)indexBuffer.mapped;
auto vtxDst = (ImDrawVert*)vertexBuffer.mapped;
auto idxDst = (ImDrawIdx*)indexBuffer.mapped;

for (int n = 0; n < imDrawData->CmdListsCount; n++) {
const ImDrawList* cmd_list = imDrawData->CmdLists[n];
Expand Down Expand Up @@ -465,8 +467,8 @@ bool UIOverlay::checkBox(const char* caption, int32_t* value) const {
return res;
}

bool UIOverlay::inputFloat(const char* caption, float* value, float step, uint32_t precision) const {
return ImGui::InputFloat(caption, value, step, step * 10.0f, precision);
bool UIOverlay::inputFloat(const char* caption, float* value, float step, const char* format) const {
return ImGui::InputFloat(caption, value, step, step * 10.0f, format);
}

bool UIOverlay::sliderFloat(const char* caption, float* value, float min, float max) const {
Expand All @@ -486,7 +488,7 @@ bool UIOverlay::comboBox(const char* caption, int32_t* itemindex, const std::vec
for (size_t i = 0; i < items.size(); i++) {
charitems.push_back(items[i].c_str());
}
uint32_t itemCount = static_cast<uint32_t>(charitems.size());
auto itemCount = static_cast<uint32_t>(charitems.size());
return ImGui::Combo(caption, itemindex, &charitems[0], itemCount, itemCount);
}

Expand Down
7 changes: 4 additions & 3 deletions base/ui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class UIOverlay {
struct PushConstBlock {
glm::vec2 scale;
glm::vec2 translate;
} pushConstBlock;
};
PushConstBlock pushConstBlock;

void prepareResources();
void preparePipeline();
Expand All @@ -66,7 +67,7 @@ class UIOverlay {

std::vector<vk::CommandBuffer> cmdBuffers;

UIOverlay(const vks::Context& context)
explicit UIOverlay(const vks::Context& context)
: context(context) {}
~UIOverlay();

Expand All @@ -81,7 +82,7 @@ class UIOverlay {
bool header(const char* caption) const;
bool checkBox(const char* caption, bool* value) const;
bool checkBox(const char* caption, int32_t* value) const;
bool inputFloat(const char* caption, float* value, float step, uint32_t precision) const;
bool inputFloat(const char* caption, float* value, float step, const char* precision = "%.3f") const;
bool sliderFloat(const char* caption, float* value, float min, float max) const;
bool sliderInt(const char* caption, int32_t* value, int32_t min, int32_t max) const;
bool comboBox(const char* caption, int32_t* itemindex, const std::vector<std::string>& items) const;
Expand Down
10 changes: 5 additions & 5 deletions base/vks/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

namespace vks { namespace file {

void withBinaryFileContents(const std::string& filename, std::function<void(size_t size, const void* data)> handler) {
withBinaryFileContents(filename, [&](const char* filename, size_t size, const void* data) { handler(size, data); });
void withBinaryFileContents(const std::string& filename, const SimpleHandler& handler) {
withBinaryFileContents(filename, [&handler](const char*, size_t size, const void* data_) { handler(size, data_); });
}

void withBinaryFileContents(const std::string& filename, std::function<void(const char* filename, size_t size, const void* data)> handler) {
void withBinaryFileContents(const std::string& filename, const NamedHandler& handler) {
auto storage = storage::Storage::readFile(filename);
handler(filename.c_str(), storage->size(), storage->data());
}

std::vector<uint8_t> readBinaryFile(const std::string& filename) {
std::vector<uint8_t> result;
withBinaryFileContents(filename, [&](size_t size, const void* data) {
withBinaryFileContents(filename, [&result](size_t size, const void* data) {
result.resize(size);
memcpy(result.data(), data, size);
});
Expand All @@ -33,7 +33,7 @@ std::string readTextFile(const std::string& fileName) {
std::ifstream fileStream(fileName, std::ios::in);

if (!fileStream.is_open()) {
throw std::runtime_error("File " + fileName + " not found");
throw std::invalid_argument("File " + fileName + " not found");
}
std::string line = "";
while (!fileStream.eof()) {
Expand Down
7 changes: 5 additions & 2 deletions base/vks/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

namespace vks { namespace file {

void withBinaryFileContents(const std::string& filename, std::function<void(const char* filename, size_t size, const void* data)> handler);
using SimpleHandler = std::function<void(size_t, const void*)>;
using NamedHandler = std::function<void(const char*, size_t, const void*)>;

void withBinaryFileContents(const std::string& filename, std::function<void(size_t size, const void* data)> handler);
void withBinaryFileContents(const std::string& filename, const SimpleHandler& handler);

void withBinaryFileContents(const std::string& filename, const NamedHandler& handler);

std::string readTextFile(const std::string& fileName);

Expand Down
14 changes: 8 additions & 6 deletions base/vks/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ using namespace vks::model;
const int Model::defaultFlags =
aiProcess_FlipWindingOrder | aiProcess_Triangulate | aiProcess_PreTransformVertices | aiProcess_CalcTangentSpace | aiProcess_GenSmoothNormals;

void Model::loadFromFile(const Context& context, const std::string& filename, const VertexLayout& layout, const ModelCreateInfo& createInfo, const int flags) {
this->layout = layout;
void Model::loadFromFile(const Context& context, const std::string& filename, const VertexLayout& layout_, const ModelCreateInfo& createInfo, const int flags) {
layout = layout_;
scale = createInfo.scale;
uvscale = createInfo.uvscale;
center = createInfo.center;
Expand All @@ -32,15 +32,14 @@ void Model::loadFromFile(const Context& context, const std::string& filename, co
Assimp::Importer importer;
const aiScene* pScene;


// Load file
vks::file::withBinaryFileContents(filename, [&](const char* filename, size_t size, const void* data) {
pScene = importer.ReadFileFromMemory(data, size, flags, filename);
vks::file::withBinaryFileContents(filename, [flags, &pScene, &importer](const char* filename_, size_t size, const void* data) {
pScene = importer.ReadFileFromMemory(data, size, flags, filename_);
});

if (!pScene) {
std::string error = importer.GetErrorString();
throw std::runtime_error(
throw std::invalid_argument(
error +
"\n\nThe file may be part of the additional asset pack.\n\nRun \"download_assets.py\" in the repository root to download the latest version.");
}
Expand Down Expand Up @@ -158,6 +157,9 @@ void Model::appendVertex(std::vector<uint8_t>& outputBuffer, const aiScene* pSce
vertexBuffer.push_back(0.0f);
vertexBuffer.push_back(0.0f);
break;
default:
throw new std::invalid_argument("Bad case");

};
}
appendOutput(outputBuffer, vertexBuffer);
Expand Down
5 changes: 3 additions & 2 deletions base/vks/pipelines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "context.hpp"
#include "model.hpp"
#include "shaders.hpp"
#include <exception>

namespace vks { namespace pipelines {
struct PipelineRasterizationStateCreateInfo : public vk::PipelineRasterizationStateCreateInfo {
Expand Down Expand Up @@ -72,7 +73,7 @@ struct PipelineVertexInputStateCreateInfo : public vk::PipelineVertexInputStateC
auto attributeIndexOffset = (uint32_t)attributeDescriptions.size();
for (uint32_t i = 0; i < componentsSize; ++i) {
const auto& component = vertexLayout.components[i];
const auto format = vertexLayout.componentFormat(component);
const auto format = vks::model::VertexLayout::componentFormat(component);
const auto offset = vertexLayout.offset(i);
attributeDescriptions.emplace_back(attributeIndexOffset + i, binding, format, offset);
}
Expand Down Expand Up @@ -188,7 +189,7 @@ struct GraphicsPipelineBuilder {

vk::Pipeline create(const vk::PipelineCache& cache) {
update();
return device.createGraphicsPipeline(cache, pipelineCreateInfo);
return device.createGraphicsPipeline(cache, pipelineCreateInfo).value;
}

vk::Pipeline create() { return create(pipelineCache); }
Expand Down
6 changes: 3 additions & 3 deletions base/vulkanExampleBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ void ExampleBase::renderLoop() {
}

std::string ExampleBase::getWindowTitle() {
std::string device(context.deviceProperties.deviceName);
std::string deviceName = context.deviceProperties.deviceName;
std::string windowTitle;
windowTitle = title + " - " + device + " - " + std::to_string(frameCounter) + " fps";
windowTitle = title + " - " + deviceName + " - " + std::to_string(frameCounter) + " fps";
return windowTitle;
}

Expand Down Expand Up @@ -665,7 +665,7 @@ void ExampleBase::updateOverlay() {

ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0);
ImGui::SetNextWindowPos(ImVec2(10, 10));
ImGui::SetNextWindowSize(ImVec2(0, 0), ImGuiSetCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(0, 0), ImGuiCond_FirstUseEver);
ImGui::Begin("Vulkan Example", nullptr, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove);
ImGui::TextUnformatted(title.c_str());
ImGui::TextUnformatted(context.deviceProperties.deviceName);
Expand Down
2 changes: 1 addition & 1 deletion cmake/ezvcpkg/ezvcpkg.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ macro(EZVCPKG_BOOTSTRAP)
# present and the user will have the default checkout, rather than their requested commit
message(STATUS "EZVCPKG Checking out commit ${EZVCPKG_COMMIT}")
execute_process(
COMMAND ${GIT_EXECUTABLE} "checkout" ${EZVCPKG_COMMIT}
COMMAND ${GIT_EXECUTABLE} "-c" "advice.detachedHead=false" "checkout" ${EZVCPKG_COMMIT}
WORKING_DIRECTORY ${EZVCPKG_DIR}
OUTPUT_QUIET)
endif()
Expand Down
7 changes: 4 additions & 3 deletions cmake/macros/TargetAssimp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
# Distributed under the Apache License, Version 2.0.
# See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
#

macro(TARGET_ASSIMP)
find_package(ASSIMP CONFIG REQUIRED)
target_include_directories(${TARGET_NAME} PUBLIC ${ASSIMP_INCLUDE_DIRS})
target_link_libraries(${TARGET_NAME} PRIVATE ${ASSIMP_LIBRARIES})
find_package(assimp REQUIRED)
target_include_directories(${TARGET_NAME} PUBLIC assimp::assimp)
target_link_libraries(${TARGET_NAME} PRIVATE assimp::assimp)
endmacro()
2 changes: 1 addition & 1 deletion cmake/macros/TargetGlm.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
macro(TARGET_GLM)
find_package(glm CONFIG REQUIRED)
target_link_libraries(${TARGET_NAME} PUBLIC glm)
target_link_libraries(${TARGET_NAME} PUBLIC glm::glm)
target_compile_definitions(${TARGET_NAME} PUBLIC GLM_FORCE_RADIANS)
target_compile_definitions(${TARGET_NAME} PUBLIC GLM_FORCE_CTOR_INIT)
endmacro()
Expand Down
2 changes: 1 addition & 1 deletion examples/bloom/bloom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class VulkanExample : public vkx::OffscreenExampleBase {
if (ui.checkBox("Bloom", &bloom)) {
buildCommandBuffers();
}
if (ui.inputFloat("Scale", &ubos.blurParams.blurScale, 0.1f, 2)) {
if (ui.inputFloat("Scale", &ubos.blurParams.blurScale, 0.1f, "%.2f")) {
updateUniformBuffersBlur();
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/computecloth/computecloth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct Compute : public vkx::Compute {
computePipelineCreateInfo.layout = pipelineLayout;
computePipelineCreateInfo.stage =
vks::shaders::loadShader(context.device, vkx::getAssetPath() + "shaders/computecloth/cloth.comp.spv", vk::ShaderStageFlagBits::eCompute);
pipeline = device.createComputePipeline(context.pipelineCache, computePipelineCreateInfo);
pipeline = device.createComputePipeline(context.pipelineCache, computePipelineCreateInfo).value;
device.destroyShaderModule(computePipelineCreateInfo.stage.module);
}

Expand Down
6 changes: 3 additions & 3 deletions examples/computecullandlod/computecullandlod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ struct Compute : public vkx::Compute {
specializationInfo.pData = &specializationData;

computePipelineCreateInfo.stage.pSpecializationInfo = &specializationInfo;
pipeline = device.createComputePipeline(context.pipelineCache, computePipelineCreateInfo);
pipeline = device.createComputePipeline(context.pipelineCache, computePipelineCreateInfo).value;
device.destroyShaderModule(computePipelineCreateInfo.stage.module);
}

Expand Down Expand Up @@ -272,7 +272,7 @@ class VulkanExample : public vkx::ExampleBase {
}
}

void updateDrawCommandBuffer(const vk::CommandBuffer& drawCommandBuffer) {
void updateDrawCommandBuffer(const vk::CommandBuffer& drawCommandBuffer) override {
drawCommandBuffer.setViewport(0, viewport());
drawCommandBuffer.setScissor(0, scissor());
drawCommandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr);
Expand Down Expand Up @@ -451,7 +451,7 @@ class VulkanExample : public vkx::ExampleBase {
compute.uniformData.scene.copy(uboScene);
}

void draw() {
void draw() override {
ExampleBase::prepareFrame();

// Submit compute shader for frustum culling
Expand Down
4 changes: 2 additions & 2 deletions examples/computeheadless/computeheadless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class VulkanExample {
#endif
context.createInstance();
context.createDevice();
LOG("GPU: %s\n", context.deviceProperties.deviceName);
LOG("GPU: %s\n", context.deviceProperties.deviceName.data());

// Get a compute queue
queue = context.device.getQueue(context.queueIndices.compute, 0);
Expand Down Expand Up @@ -117,7 +117,7 @@ class VulkanExample {
vk::SpecializationMapEntry specializationMapEntry{ 0, 0, sizeof(uint32_t) };
vk::SpecializationInfo specializationInfo{ 1, &specializationMapEntry, sizeof(SpecializationData), &specializationData };
computePipelineCreateInfo.stage.pSpecializationInfo = &specializationInfo;
pipeline = device.createComputePipeline(context.pipelineCache, computePipelineCreateInfo);
pipeline = device.createComputePipeline(context.pipelineCache, computePipelineCreateInfo).value;
device.destroyShaderModule(computePipelineCreateInfo.stage.module);
}

Expand Down
Loading