Skip to content
Open
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ add_compile_definitions(USE_ETNA VK_GRAPHICS_BASIC_ROOT="${PROJECT_SOURCE_DIR}")

add_subdirectory(external/etna)
#add_subdirectory(external/volk)
#add_subdirectory(src/samples/quad2d)
add_subdirectory(src/samples/quad2d)
add_subdirectory(src/samples/shadowmap)
#add_subdirectory(src/samples/simpleforward)
add_subdirectory(src/samples/simple_compute)
22 changes: 14 additions & 8 deletions src/samples/quad2d/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,25 @@ else()
include_directories(${GLFW_INCLUDE_DIRS})
endif()


set(RENDER_SOURCE
#../../render/scene_mgr.cpp
../../render/scene_mgr.cpp
../../render/render_imgui.cpp
quad2d_render.cpp)
quad2d_render.cpp
render_init.cpp
update.cpp
draw.cpp
present.cpp
)

add_executable(quad_renderer main.cpp ../../utils/glfw_window.cpp ${VK_UTILS_SRC} ${SCENE_LOADER_SRC} ${RENDER_SOURCE} ${IMGUI_SRC})
add_executable(quad2d_renderer main.cpp ../../utils/glfw_window.cpp ${VK_UTILS_SRC} ${SCENE_LOADER_SRC} ${RENDER_SOURCE} ${IMGUI_SRC})

if(CMAKE_SYSTEM_NAME STREQUAL Windows)
set_target_properties(quad_renderer PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
set_target_properties(quad2d_renderer PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

target_link_libraries(quad_renderer PRIVATE project_options
volk glfw3 project_warnings)
target_link_libraries(quad2d_renderer PRIVATE project_options
glfw3 project_warnings etna ${CMAKE_DL_LIBS})
else()
target_link_libraries(quad_renderer PRIVATE project_options
volk glfw project_warnings) #
target_link_libraries(quad2d_renderer PRIVATE project_options
glfw project_warnings etna ${CMAKE_DL_LIBS}) #
endif()
62 changes: 62 additions & 0 deletions src/samples/quad2d/draw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "quad2d_render.h"
#include <etna/Etna.hpp>

#include <imgui/imgui.h>

#include "../../render/render_gui.h"

void SimpleQuad2dRender::DrawFrameSimple()
{
vkWaitForFences(m_context->getDevice(), 1, &m_frameFences[m_presentationResources.currentFrame], VK_TRUE, UINT64_MAX);
vkResetFences(m_context->getDevice(), 1, &m_frameFences[m_presentationResources.currentFrame]);

uint32_t imageIdx;
m_swapchain.AcquireNextImage(m_presentationResources.imageAvailable, &imageIdx);

auto currentCmdBuf = m_cmdBuffersDrawMain[m_presentationResources.currentFrame];

VkSemaphore waitSemaphores[] = {m_presentationResources.imageAvailable};
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};

BuildCommandBufferSimple(currentCmdBuf, m_swapchain.GetAttachment(imageIdx).image, m_swapchain.GetAttachment(imageIdx).view);

std::vector<VkCommandBuffer> submitCmdBufs = { currentCmdBuf };

VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = submitCmdBufs.size();
submitInfo.pCommandBuffers = submitCmdBufs.data();

VkSemaphore signalSemaphores[] = {m_presentationResources.renderingFinished};
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;

VK_CHECK_RESULT(vkQueueSubmit(m_context->getQueue(),
1, &submitInfo, m_frameFences[m_presentationResources.currentFrame]));

VkResult presentRes = m_swapchain.QueuePresent(m_presentationResources.queue, imageIdx,
m_presentationResources.renderingFinished);

if (presentRes == VK_ERROR_OUT_OF_DATE_KHR || presentRes == VK_SUBOPTIMAL_KHR)
{
RecreateSwapChain();
}
else if (presentRes != VK_SUCCESS)
{
RUN_TIME_ERROR("Failed to present swapchain image");
}

m_presentationResources.currentFrame = (m_presentationResources.currentFrame + 1) % m_framesInFlight;

vkQueueWaitIdle(m_presentationResources.queue);

etna::submit();
}

void SimpleQuad2dRender::DrawFrame(float a_time, DrawMode a_mode)
{
DrawFrameSimple();
}
22 changes: 12 additions & 10 deletions src/samples/quad2d/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "quad2d_render.h"
#include "utils/glfw_window.h"
#include <etna/Etna.hpp>

void initVulkanGLFW(std::shared_ptr<IRender> &app, GLFWwindow* window, int deviceID, bool initGUI)
void initVulkanGLFW(std::shared_ptr<IRender> &app, GLFWwindow* window)
{
uint32_t glfwExtensionCount = 0;
const char** glfwExtensions;
Expand All @@ -12,25 +13,22 @@ void initVulkanGLFW(std::shared_ptr<IRender> &app, GLFWwindow* window, int devic
std::cout << "WARNING. Can't connect Vulkan to GLFW window (glfwGetRequiredInstanceExtensions returns NULL)" << std::endl;
}

app->InitVulkan(glfwExtensions, glfwExtensionCount, deviceID);
app->InitVulkan(glfwExtensions, glfwExtensionCount, /* useless param */ 0);

if(glfwExtensions != nullptr)
{
VkSurfaceKHR surface;
VK_CHECK_RESULT(glfwCreateWindowSurface(app->GetVkInstance(), window, nullptr, &surface));
setupImGuiContext(window);
app->InitPresentation(surface, initGUI);
app->InitPresentation(surface, false);
}
}

int main()
{
constexpr int WIDTH = 1024;
constexpr int HEIGHT = 1024;
constexpr int VULKAN_DEVICE_ID = 0;
constexpr bool showGUI = true;

std::shared_ptr<IRender> app = std::make_unique<Quad2D_Render>(WIDTH, HEIGHT);
std::shared_ptr<IRender> app = std::make_unique<SimpleQuad2dRender>(WIDTH, HEIGHT);
if(app == nullptr)
{
std::cout << "Can't create render of specified type" << std::endl;
Expand All @@ -39,11 +37,15 @@ int main()

auto* window = initWindow(WIDTH, HEIGHT);

initVulkanGLFW(app, window, VULKAN_DEVICE_ID, showGUI);
initVulkanGLFW(app, window);

app->LoadScene("../resources/scenes/043_cornell_normals/statex_00001.xml", false);
app->LoadScene(VK_GRAPHICS_BASIC_ROOT "", false);

mainLoop(app, window, showGUI);
mainLoop(app, window, true);

app = {};
if (etna::is_initilized())
etna::shutdown();

return 0;
}
65 changes: 65 additions & 0 deletions src/samples/quad2d/present.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "quad2d_render.h"

void SimpleQuad2dRender::InitPresentStuff()
{
VkSemaphoreCreateInfo semaphoreInfo = {};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VK_CHECK_RESULT(vkCreateSemaphore(m_context->getDevice(), &semaphoreInfo, nullptr, &m_presentationResources.imageAvailable));
VK_CHECK_RESULT(vkCreateSemaphore(m_context->getDevice(), &semaphoreInfo, nullptr, &m_presentationResources.renderingFinished));

// TODO: Move to customizable initialization
m_commandPool = vk_utils::createCommandPool(m_context->getDevice(), m_context->getQueueFamilyIdx(), VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);

m_cmdBuffersDrawMain.reserve(m_framesInFlight);
m_cmdBuffersDrawMain = vk_utils::createCommandBuffers(m_context->getDevice(), m_commandPool, m_framesInFlight);

m_frameFences.resize(m_framesInFlight);
VkFenceCreateInfo fenceInfo = {};
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
for (size_t i = 0; i < m_framesInFlight; i++)
{
VK_CHECK_RESULT(vkCreateFence(m_context->getDevice(), &fenceInfo, nullptr, &m_frameFences[i]));
}
}

void SimpleQuad2dRender::ResetPresentStuff()
{
if (!m_cmdBuffersDrawMain.empty())
{
vkFreeCommandBuffers(m_context->getDevice(), m_commandPool, static_cast<uint32_t>(m_cmdBuffersDrawMain.size()),
m_cmdBuffersDrawMain.data());
m_cmdBuffersDrawMain.clear();
}

for (size_t i = 0; i < m_frameFences.size(); i++)
{
vkDestroyFence(m_context->getDevice(), m_frameFences[i], nullptr);
}

if (m_presentationResources.imageAvailable != VK_NULL_HANDLE)
{
vkDestroySemaphore(m_context->getDevice(), m_presentationResources.imageAvailable, nullptr);
}
if (m_presentationResources.renderingFinished != VK_NULL_HANDLE)
{
vkDestroySemaphore(m_context->getDevice(), m_presentationResources.renderingFinished, nullptr);
}

if (m_commandPool != VK_NULL_HANDLE)
{
vkDestroyCommandPool(m_context->getDevice(), m_commandPool, nullptr);
}
}

void SimpleQuad2dRender::InitPresentation(VkSurfaceKHR &a_surface, bool)
{
m_surface = a_surface;

m_presentationResources.queue = m_swapchain.CreateSwapChain(
m_context->getPhysicalDevice(), m_context->getDevice(), m_surface,
m_width, m_height, m_framesInFlight, m_vsync);
m_presentationResources.currentFrame = 0;

InitPresentStuff();
}
Loading