<commit_before>﻿/**
 * Copyright (c) 2017 <NAME>
 * Licensed under the MIT License.
 * See LICENSE file for more information.
 */


#include "Renderer.h"

#include "saiga/vulkan/window/Window.h"

namespace Saiga
{
namespace Vulkan
{
VulkanRenderer::VulkanRenderer(VulkanWindow& window, VulkanParameters vulkanParameters)
    : window(window), vulkanParameters(vulkanParameters)
{
    window.setRenderer(this);


    std::vector<const char*> instanceExtensions = window.getRequiredInstanceExtensions();
    instance.create(instanceExtensions, vulkanParameters.enableValidationLayer);


    window.createSurface(instance, &surface);

    base().setPhysicalDevice(instance.pickPhysicalDevice());

    vulkanParameters.physicalDeviceFeatures.fillModeNonSolid = VK_TRUE;
    vulkanParameters.physicalDeviceFeatures.wideLines        = VK_TRUE;
    base().createLogicalDevice(surface, vulkanParameters, true);


    swapChain.connect(instance, base().physicalDevice, base().device);
    swapChain.initSurface(surface);

    state = State::INITIALIZED;
}

VulkanRenderer::~VulkanRenderer()
{
    // Only wait until the queue is done.
    // All vulkan objects are destroyed in their destructor
    waitIdle();
}

void VulkanRenderer::init()
{
    SAIGA_ASSERT(state == State::INITIALIZED);
    //    createSwapChain();
    swapChain.create(&surfaceWidth, &SurfaceHeight, false);

    syncObjects.clear();
    syncObjects.resize(swapChain.imageCount);
    for (auto& sync : syncObjects)
    {
        sync.create(base().device);
    }

    currentBuffer  = 0;
    nextSyncObject = 0;

    if (vulkanParameters.enableImgui)
    {
        imGui.reset();
        imGui = window.createImGui(swapChainSize());
    }

    createBuffers(swapChainSize(), surfaceWidth, SurfaceHeight);

    // Everyting fine.
    // We can start rendering now :).
    state = State::RENDERABLE;
}

void VulkanRenderer::render(Camera*)
{
    if (state == State::RESET)
    {
        state = State::INITIALIZED;
    }


    if (state == State::INITIALIZED)
    {
        init();
    }


    FrameSync& sync = syncObjects[nextSyncObject];
    sync.wait();


    VkResult err = swapChain.acquireNextImage(sync.imageAvailable, &currentBuffer);
    VK_CHECK_RESULT(err);

    if (err == VK_ERROR_OUT_OF_DATE_KHR)
    {
        reset();
        return;
    }


    render(sync, currentBuffer);


    err = swapChain.queuePresent(base().mainQueue, currentBuffer, sync.renderComplete);
    if (err == VK_ERROR_OUT_OF_DATE_KHR)
    {
        reset();
        return;
    }
    nextSyncObject = (nextSyncObject + 1) % syncObjects.size();
}

float VulkanRenderer::getTotalRenderTime()
{
    return window.mainLoop.renderCPUTimer.getTimeMS();
}


void VulkanRenderer::reset()
{
    SAIGA_ASSERT(state == State::RESET || state == State::RENDERABLE);
    waitIdle();
    state = State::RESET;
}
<commit_msg>Fixes error check when resizing the window<commit_after>﻿/**
 * Copyright (c) 2017 <NAME>
 * Licensed under the MIT License.
 * See LICENSE file for more information.
 */


#include "Renderer.h"

#include "saiga/vulkan/window/Window.h"

namespace Saiga
{
namespace Vulkan
{
VulkanRenderer::VulkanRenderer(VulkanWindow& window, VulkanParameters vulkanParameters)
    : window(window), vulkanParameters(vulkanParameters)
{
    window.setRenderer(this);


    std::vector<const char*> instanceExtensions = window.getRequiredInstanceExtensions();
    instance.create(instanceExtensions, vulkanParameters.enableValidationLayer);


    window.createSurface(instance, &surface);

    base().setPhysicalDevice(instance.pickPhysicalDevice());

    vulkanParameters.physicalDeviceFeatures.fillModeNonSolid = VK_TRUE;
    vulkanParameters.physicalDeviceFeatures.wideLines        = VK_TRUE;
    base().createLogicalDevice(surface, vulkanParameters, true);


    swapChain.connect(instance, base().physicalDevice, base().device);
    swapChain.initSurface(surface);

    state = State::INITIALIZED;
}

VulkanRenderer::~VulkanRenderer()
{
    // Only wait until the queue is done.
    // All vulkan objects are destroyed in their destructor
    waitIdle();
}

void VulkanRenderer::init()
{
    SAIGA_ASSERT(state == State::INITIALIZED);
    //    createSwapChain();
    swapChain.create(&surfaceWidth, &SurfaceHeight, false);

    syncObjects.clear();
    syncObjects.resize(swapChain.imageCount);
    for (auto& sync : syncObjects)
    {
        sync.create(base().device);
    }

    currentBuffer  = 0;
    nextSyncObject = 0;

    if (vulkanParameters.enableImgui)
    {
        imGui.reset();
        imGui = window.createImGui(swapChainSize());
    }

    createBuffers(swapChainSize(), surfaceWidth, SurfaceHeight);

    // Everyting fine.
    // We can start rendering now :).
    state = State::RENDERABLE;
}

void VulkanRenderer::render(Camera*)
{
    if (state == State::RESET)
    {
        state = State::INITIALIZED;
    }


    if (state == State::INITIALIZED)
    {
        init();
    }


    FrameSync& sync = syncObjects[nextSyncObject];
    sync.wait();


    VkResult err = swapChain.acquireNextImage(sync.imageAvailable, &currentBuffer);

    if (err == VK_ERROR_OUT_OF_DATE_KHR)
    {
        reset();
        return;
    }
    VK_CHECK_RESULT(err);


    render(sync, currentBuffer);


    err = swapChain.queuePresent(base().mainQueue, currentBuffer, sync.renderComplete);
    if (err == VK_ERROR_OUT_OF_DATE_KHR)
    {
        reset();
        return;
    }
    VK_CHECK_RESULT(err);
    nextSyncObject = (nextSyncObject + 1) % syncObjects.size();
}

float VulkanRenderer::getTotalRenderTime()
{
    return window.mainLoop.renderCPUTimer.getTimeMS();
}


void VulkanRenderer::reset()
{
    SAIGA_ASSERT(state == State::RESET || state == State::RENDERABLE);
    waitIdle();
    state = State::RESET;
}

{'fraction_non_alphanumeric': 0.12181597643389361, 'fraction_numerical': 0.002425922717033443, 'mean_word_length': 3.610223642172524, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '791661', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1749, 'n_words': 325}