Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
yknishidate committed Jun 1, 2024
1 parent 06eaf72 commit 9754c0e
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vcpkg"]
path = vcpkg
url = https://github.com/microsoft/vcpkg
2 changes: 1 addition & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"CMAKE_TOOLCHAIN_FILE": "vcpkg/scripts/buildsystems/vcpkg.cmake",
"CMAKE_CXX_STANDARD": "20",
"CMAKE_CXX_FLAGS": "/W4 /WX /wd4100 /permissive- /analyze /EHsc /Zc:preprocessor /Zc:inline /Zc:strictStrings /Zc:rvalueCast /MP /external:anglebrackets /analyze:external-",
"CMAKE_EXE_LINKER_FLAGS": "/ignore:4099",
Expand Down
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,12 @@ Personal Vulkan wrapper using

- Vulkan SDK
- CMake
- vcpkg

## Run cmake

```sh
# Make sure VCPKG_ROOT is set

# Windows
cmake . -B build -D CMAKE_TOOLCHAIN_FILE=%VCPKG_ROOT%\scripts\buildsystems\vcpkg.cmake
# or
setup_build.bat

# Linux
cmake . -B build -D CMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT\scripts\buildsystems\vcpkg.cmake
# For Windows
cmake . --preset vs
```

## Usage (in your project)
Expand Down Expand Up @@ -117,5 +109,5 @@ target_include_directories(${PROJECT_NAME} PUBLIC

```sh
# change to your vcpkg path
cmake . -B build -D CMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake
cmake . -B build -D CMAKE_TOOLCHAIN_FILE=.\reactive\vcpkg\scripts\buildsystems\vcpkg.cmake
```
4 changes: 4 additions & 0 deletions include/reactive/Scene/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ struct SphereMeshCreateInfo {
int numSlices = 32;
int numStacks = 32;
float radius = 1.0;
bool useForAccelStruct = false;
std::string name = "Sphere";
};

struct CubeMeshCreateInfo {
bool useForAccelStruct = false;
std::string name = "Cube";
};

Expand All @@ -48,6 +50,7 @@ struct PlaneMeshCreateInfo {
float height = 1;
uint32_t widthSegments = 1;
uint32_t heightSegments = 1;
bool useForAccelStruct = false;
std::string name = "Plane";
};

Expand All @@ -66,6 +69,7 @@ class Mesh {
vk::MemoryPropertyFlags memoryProps,
std::vector<Vertex> _vertices,
std::vector<uint32_t> _indices,
bool useForAccelStruct,
std::string _name);
static auto createSphereMesh(const Context& context, SphereMeshCreateInfo createInfo) -> Mesh;
static auto createPlaneMesh(const Context& context, PlaneMeshCreateInfo createInfo) -> Mesh;
Expand Down
1 change: 1 addition & 0 deletions include/reactive/reactive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
#include "Scene/Camera.hpp"
#include "Timer/CPUTimer.hpp"
#include "Timer/GPUTimer.hpp"
#include "Window.hpp"
13 changes: 7 additions & 6 deletions sample/hello_compute/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HelloApp : public App {
void onStart() override {
image = context.createImage({
.usage = ImageUsage::Storage,
.extent = {width, height, 1},
.extent = {Window::getWidth(), Window::getHeight(), 1},
.format = vk::Format::eB8G8R8A8Unorm,
});
image->createImageView();
Expand Down Expand Up @@ -62,22 +62,23 @@ class HelloApp : public App {
}

void onCursorPos(float xpos, float ypos) override {
if (isMouseButtonDown(GLFW_MOUSE_BUTTON_1)) {
spdlog::info("cursor: {}", glm::to_string(getMouseDragLeft()));
translate += -getMouseDragLeft() / static_cast<float>(height) / scale;
if (Window::isMouseButtonDown(GLFW_MOUSE_BUTTON_1)) {
spdlog::info("cursor: {}", glm::to_string(Window::getMouseDragLeft()));
translate +=
-Window::getMouseDragLeft() / static_cast<float>(Window::getHeight()) / scale;
}
}

void onRender(const CommandBufferHandle& commandBuffer) override {
float aspect = width / static_cast<float>(height);
float aspect = Window::getWidth() / static_cast<float>(Window::getHeight());
params.lowerLeft = glm::vec2(-1 * aspect, -1) / scale + translate;
params.upperRight = glm::vec2(1 * aspect, 1) / scale + translate;
params.maxIterations = static_cast<int>(scale * 10);

commandBuffer->copyBuffer(buffer, &params);
commandBuffer->bindDescriptorSet(descSet, pipeline);
commandBuffer->bindPipeline(pipeline);
commandBuffer->dispatch(width, height, 1);
commandBuffer->dispatch(Window::getWidth(), Window::getHeight(), 1);
commandBuffer->copyImage(image, getCurrentColorImage(), vk::ImageLayout::eGeneral,
vk::ImageLayout::ePresentSrcKHR);
}
Expand Down
7 changes: 4 additions & 3 deletions sample/hello_graphics/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ class HelloApp : public App {
}

commandBuffer->clearColorImage(getCurrentColorImage(), {0.0f, 0.0f, 0.5f, 1.0f});
commandBuffer->setViewport(width, height);
commandBuffer->setScissor(width, height);
commandBuffer->setViewport(Window::getWidth(), Window::getHeight());
commandBuffer->setScissor(Window::getWidth(), Window::getHeight());
commandBuffer->bindDescriptorSet(descSet, pipeline);
commandBuffer->bindPipeline(pipeline);
commandBuffer->beginTimestamp(gpuTimer);
commandBuffer->beginRendering(getCurrentColorImage(), nullptr, {0, 0}, {width, height});
commandBuffer->beginRendering(getCurrentColorImage(), nullptr, {0, 0},
{Window::getWidth(), Window::getHeight()});
commandBuffer->draw(3, 1, 0, 0);
commandBuffer->endRendering();
commandBuffer->endTimestamp(gpuTimer);
Expand Down
7 changes: 4 additions & 3 deletions sample/hello_mesh_shader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ class HelloApp : public App {
void onRender(const CommandBufferHandle& commandBuffer) override {
ImGui::SliderInt("Test slider", &testInt, 0, 100);
commandBuffer->clearColorImage(getCurrentColorImage(), {0.0f, 0.0f, 0.5f, 1.0f});
commandBuffer->setViewport(width, height);
commandBuffer->setScissor(width, height);
commandBuffer->setViewport(Window::getWidth(), Window::getHeight());
commandBuffer->setScissor(Window::getWidth(), Window::getHeight());
commandBuffer->bindDescriptorSet(descSet, pipeline);
commandBuffer->bindPipeline(pipeline);
commandBuffer->beginRendering(getCurrentColorImage(), nullptr, {0, 0}, {width, height});
commandBuffer->beginRendering(getCurrentColorImage(), nullptr, {0, 0},
{Window::getWidth(), Window::getHeight()});
commandBuffer->drawMeshTasks(1, 1, 1);
commandBuffer->endRendering();
}
Expand Down
15 changes: 8 additions & 7 deletions sample/hello_raytracing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class HelloApp : public App {
}) {}

void onStart() override {
camera = Camera{Camera::Type::Orbital, static_cast<float>(width) / height};
camera = Camera{Camera::Type::Orbital,
static_cast<float>(Window::getWidth()) / Window::getHeight()};

mesh = Mesh{context, MemoryUsage::Device, vertices, indices, "Triangle"};
mesh = Mesh{context, MemoryUsage::Device, vertices, indices, true, "Triangle"};

bottomAccel = context.createBottomAccel({
.vertexBuffer = mesh.vertexBuffer,
Expand All @@ -41,7 +42,7 @@ class HelloApp : public App {

image = context.createImage({
.usage = ImageUsage::Storage,
.extent = {width, height, 1},
.extent = {Window::getWidth(), Window::getHeight(), 1},
.format = vk::Format::eB8G8R8A8Unorm,
});
image->createImageView();
Expand Down Expand Up @@ -85,9 +86,9 @@ class HelloApp : public App {
});
}

void onUpdate() override {
camera.processMouseDragLeft(getMouseDragLeft());
camera.processMouseScroll(getMouseScroll());
void onUpdate(float dt) override {
camera.processMouseDragLeft(Window::getMouseDragLeft());
camera.processMouseScroll(Window::getMouseScroll());

pushConstants.invProj = camera.getInvProj();
pushConstants.invView = camera.getInvView();
Expand All @@ -99,7 +100,7 @@ class HelloApp : public App {
commandBuffer->bindDescriptorSet(descSet, pipeline);
commandBuffer->bindPipeline(pipeline);
commandBuffer->pushConstants(pipeline, &pushConstants);
commandBuffer->traceRays(pipeline, width, height, 1);
commandBuffer->traceRays(pipeline, Window::getWidth(), Window::getHeight(), 1);
commandBuffer->copyImage(image, getCurrentColorImage(), vk::ImageLayout::eGeneral,
vk::ImageLayout::ePresentSrcKHR);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Graphics/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ void Context::initInstance(bool enableValidation,
}

void Context::initPhysicalDevice(vk::SurfaceKHR surface) {
spdlog::info("Context::initPhysicalDevice");

// Select discrete gpu
for (auto gpu : instance->enumeratePhysicalDevices()) {
if (gpu.getProperties().deviceType == vk::PhysicalDeviceType::eDiscreteGpu) {
Expand Down
24 changes: 17 additions & 7 deletions src/Scene/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ Mesh::Mesh(const Context& _context,
vk::MemoryPropertyFlags memoryProps,
std::vector<Vertex> _vertices,
std::vector<uint32_t> _indices,
bool useForAccelStruct,
std::string _name)
: context{&_context},
name{std::move(_name)},
vertices{std::move(_vertices)},
indices{std::move(_indices)} {
vertexBuffer = context->createBuffer({
.usage = BufferUsage::Vertex,
.usage = useForAccelStruct
? BufferUsage::Vertex |
vk::BufferUsageFlagBits::eAccelerationStructureBuildInputReadOnlyKHR
: BufferUsage::Vertex,
.memory = memoryProps,
.size = sizeof(Vertex) * vertices.size(),
.debugName = name + "::vertexBuffer",
});

indexBuffer = context->createBuffer({
.usage = BufferUsage::Index,
.usage = useForAccelStruct
? BufferUsage::Index |
vk::BufferUsageFlagBits::eAccelerationStructureBuildInputReadOnlyKHR
: BufferUsage::Index,
.memory = memoryProps,
.size = sizeof(uint32_t) * indices.size(),
.debugName = name + "::indexBuffer",
Expand Down Expand Up @@ -106,7 +113,8 @@ auto Mesh::createSphereMesh(const Context& context, SphereMeshCreateInfo createI
}
}

return {context, MemoryUsage::Device, vertices, indices, createInfo.name};
return {context, MemoryUsage::Device, vertices,
indices, createInfo.useForAccelStruct, createInfo.name};
}

auto Mesh::createPlaneMesh(const Context& context, PlaneMeshCreateInfo createInfo) -> Mesh {
Expand Down Expand Up @@ -147,7 +155,8 @@ auto Mesh::createPlaneMesh(const Context& context, PlaneMeshCreateInfo createInf
}
}

return {context, MemoryUsage::Device, vertices, indices, createInfo.name};
return {context, MemoryUsage::Device, vertices,
indices, createInfo.useForAccelStruct, createInfo.name};
}

auto Mesh::createPlaneLineMesh(const Context& context, PlaneLineMeshCreateInfo createInfo) -> Mesh {
Expand Down Expand Up @@ -193,7 +202,7 @@ auto Mesh::createPlaneLineMesh(const Context& context, PlaneLineMeshCreateInfo c
for (uint32_t i = 0; i < indicesCount; i++) {
indices.push_back(i);
}
return {context, MemoryUsage::Device, vertices, indices, createInfo.name};
return {context, MemoryUsage::Device, vertices, indices, false, createInfo.name};
}

auto Mesh::createCubeMesh(const Context& context, CubeMeshCreateInfo createInfo) -> Mesh {
Expand Down Expand Up @@ -242,7 +251,8 @@ auto Mesh::createCubeMesh(const Context& context, CubeMeshCreateInfo createInfo)
for (int i = 0; i < vertices.size(); i++) {
indices.push_back(i);
}
return {context, MemoryUsage::Device, vertices, indices, createInfo.name};
return {context, MemoryUsage::Device, vertices,
indices, createInfo.useForAccelStruct, createInfo.name};
}

auto Mesh::createCubeLineMesh(const Context& context, CubeLineMeshCreateInfo createInfo) -> Mesh {
Expand All @@ -253,6 +263,6 @@ auto Mesh::createCubeLineMesh(const Context& context, CubeLineMeshCreateInfo cre
};
std::vector<uint32_t> indices = {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6,
6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7};
return {context, MemoryUsage::Device, vertices, indices, createInfo.name};
return {context, MemoryUsage::Device, vertices, indices, false, createInfo.name};
}
} // namespace rv
1 change: 1 addition & 0 deletions vcpkg
Submodule vcpkg added at 47364f

0 comments on commit 9754c0e

Please sign in to comment.