712 lines
27 KiB
C++
712 lines
27 KiB
C++
// =============================================
|
|
// Aster: model_render.cpp
|
|
// Copyright (c) 2020-2025 Anish Bhobe
|
|
// =============================================
|
|
|
|
#include "aster/aster.h"
|
|
|
|
#include "aster/core/buffer.h"
|
|
#include "aster/core/constants.h"
|
|
#include "aster/core/instance.h"
|
|
#include "aster/core/device.h"
|
|
#include "aster/core/image.h"
|
|
#include "aster/core/physical_device.h"
|
|
#include "aster/core/pipeline.h"
|
|
#include "aster/core/swapchain.h"
|
|
#include "aster/core/window.h"
|
|
|
|
#include "asset_loader.h"
|
|
#include "frame.h"
|
|
#include "helpers.h"
|
|
#include "light_manager.h"
|
|
|
|
#include "aster/systems/buffer_manager.h"
|
|
#include "gui.h"
|
|
#include "ibl_helpers.h"
|
|
#include "pipeline_utils.h"
|
|
|
|
#include <EASTL/array.h>
|
|
#include <aster/systems/commit_manager.h>
|
|
#include <aster/systems/resource_manager.h>
|
|
#include <tiny_gltf.h>
|
|
#include <filesystem>
|
|
|
|
constexpr u32 MAX_FRAMES_IN_FLIGHT = 3;
|
|
constexpr auto PIPELINE_CACHE_FILE = "PipelineCacheData.bin";
|
|
constexpr auto MODEL_FILE = "model/Sponza/Sponza.gltf";
|
|
constexpr auto BACKDROP_FILE = "image/photo_studio_loft_hall_4k.hdr";
|
|
constexpr u32 INIT_WIDTH = 1280;
|
|
constexpr u32 INIT_HEIGHT = 720;
|
|
|
|
struct Camera
|
|
{
|
|
mat4 m_View;
|
|
mat4 m_Perspective;
|
|
mat4 m_InverseView;
|
|
mat4 m_InversePerspective;
|
|
vec3 m_Position;
|
|
f32 m_PositionHomogenousPad_ = 1.0f;
|
|
|
|
void
|
|
CalculateInverses()
|
|
{
|
|
m_InverseView = inverse(m_View);
|
|
m_InversePerspective = inverse(m_Perspective);
|
|
}
|
|
};
|
|
|
|
struct CameraController
|
|
{
|
|
constexpr static auto UP = vec3(0.0f, 1.0f, 0.0f);
|
|
|
|
f32 m_Fov;
|
|
f32 m_Pitch;
|
|
f32 m_Yaw;
|
|
f32 m_AspectRatio;
|
|
|
|
Camera m_Camera;
|
|
|
|
CameraController(const vec3 &position, const vec3 &target, const f32 vFov, const f32 aspectRatio)
|
|
: m_Fov(vFov)
|
|
, m_Pitch{0.0f}
|
|
, m_Yaw{0.0f}
|
|
, m_AspectRatio{aspectRatio}
|
|
, m_Camera{
|
|
.m_View = lookAt(position, target, UP),
|
|
.m_Perspective = glm::perspective(vFov, aspectRatio, 0.1f, 100.0f),
|
|
.m_Position = position,
|
|
}
|
|
{
|
|
const vec3 dir = normalize(target - vec3(position));
|
|
m_Pitch = asin(dir.y);
|
|
m_Yaw = acos(-dir.z / sqrt(1.0f - dir.y * dir.y));
|
|
m_Camera.CalculateInverses();
|
|
}
|
|
|
|
void
|
|
SetAspectRatio(const f32 aspectRatio)
|
|
{
|
|
m_AspectRatio = aspectRatio;
|
|
m_Camera.m_Perspective = glm::perspective(m_Fov, aspectRatio, 0.1f, 100.0f);
|
|
|
|
m_Camera.CalculateInverses();
|
|
}
|
|
|
|
void
|
|
SetPosition(const vec3 &position)
|
|
{
|
|
m_Camera.m_Position = vec4(position, 1.0f);
|
|
|
|
f32 cosPitch = cos(m_Pitch);
|
|
const auto target = vec3(sin(m_Yaw) * cosPitch, sin(m_Pitch), -cos(m_Yaw) * cosPitch);
|
|
m_Camera.m_View = lookAt(position, position + target, UP);
|
|
|
|
m_Camera.CalculateInverses();
|
|
}
|
|
|
|
void
|
|
SetPitchYaw(f32 pitch, f32 yaw)
|
|
{
|
|
m_Pitch = pitch;
|
|
m_Yaw = yaw;
|
|
|
|
f32 cosPitch = cos(m_Pitch);
|
|
const auto target = vec3(sin(m_Yaw) * cosPitch, sin(m_Pitch), -cos(m_Yaw) * cosPitch);
|
|
const vec3 position = m_Camera.m_Position;
|
|
m_Camera.m_View = lookAt(position, position + target, UP);
|
|
|
|
m_Camera.CalculateInverses();
|
|
}
|
|
|
|
void
|
|
SetLookAt(const vec3 &target)
|
|
{
|
|
const vec3 dir = normalize(target - m_Camera.m_Position);
|
|
m_Pitch = acos(dir.y);
|
|
m_Yaw = acos(dir.z / sqrt(1.0f - dir.y * dir.y));
|
|
m_Camera.m_View = lookAt(m_Camera.m_Position, m_Camera.m_Position + target, UP);
|
|
|
|
m_Camera.CalculateInverses();
|
|
}
|
|
};
|
|
|
|
int
|
|
main(int, char **)
|
|
{
|
|
MIN_LOG_LEVEL(Logger::LogType::eInfo);
|
|
|
|
Window window = {"ModelRender (Aster)", {INIT_WIDTH, INIT_HEIGHT}};
|
|
Instance context = {"ModelRender", VERSION};
|
|
Surface surface = {&context, &window, "Primary Surface"};
|
|
|
|
PhysicalDevices physicalDevices = {&surface, &context};
|
|
PhysicalDevice deviceToUse = FindSuitableDevice(physicalDevices);
|
|
|
|
usize physicalDeviceOffsetAlignment = deviceToUse.m_DeviceProperties.limits.minUniformBufferOffsetAlignment;
|
|
|
|
INFO("Using {} as the primary device.", deviceToUse.m_DeviceProperties.deviceName.data());
|
|
|
|
Features enabledDeviceFeatures = {
|
|
.m_Vulkan10Features = {.samplerAnisotropy = true},
|
|
.m_Vulkan12Features =
|
|
{
|
|
.descriptorIndexing = true,
|
|
.shaderSampledImageArrayNonUniformIndexing = true,
|
|
.shaderStorageBufferArrayNonUniformIndexing = true,
|
|
.shaderStorageImageArrayNonUniformIndexing = true,
|
|
.descriptorBindingUniformBufferUpdateAfterBind = true, // Not related to Bindless
|
|
.descriptorBindingSampledImageUpdateAfterBind = true,
|
|
.descriptorBindingStorageImageUpdateAfterBind = true,
|
|
.descriptorBindingStorageBufferUpdateAfterBind = true,
|
|
.descriptorBindingPartiallyBound = true,
|
|
.runtimeDescriptorArray = true,
|
|
.bufferDeviceAddress = true,
|
|
.bufferDeviceAddressCaptureReplay = true,
|
|
},
|
|
.m_Vulkan13Features =
|
|
{
|
|
.synchronization2 = true,
|
|
.dynamicRendering = true,
|
|
},
|
|
};
|
|
|
|
auto pipelineCacheData = ReadFileBytes(PIPELINE_CACHE_FILE, false);
|
|
|
|
QueueAllocation queueAllocation = FindAppropriateQueueAllocation(&deviceToUse);
|
|
Device device = {&context, &deviceToUse, &enabledDeviceFeatures,
|
|
{queueAllocation}, pipelineCacheData, "Primary Device"};
|
|
vk::Queue graphicsQueue = device.GetQueue(queueAllocation.m_Family, 0);
|
|
Swapchain swapchain = {&surface, &device, window.GetSize(), "Primary Chain"};
|
|
|
|
auto resourceManager = systems::ResourceManager{&device};
|
|
systems::CommitManager commitManager = {&device, 1000, 1000, 1000,
|
|
resourceManager.Samplers().CreateSampler({.m_Name = "Default Sampler"})};
|
|
|
|
AssetLoader assetLoader = {&resourceManager, &commitManager, graphicsQueue, queueAllocation.m_Family,
|
|
queueAllocation.m_Family};
|
|
auto lightManager = LightManager{&resourceManager, &commitManager};
|
|
|
|
Model model = assetLoader.LoadModelToGpu(MODEL_FILE);
|
|
auto environmentHdri = assetLoader.LoadHdrImage(BACKDROP_FILE);
|
|
auto envHdriHandle = commitManager.CommitTexture(environmentHdri);
|
|
|
|
auto environment = CreateCubeFromHdrEnv(&assetLoader, graphicsQueue, 512, envHdriHandle, "Cube Env");
|
|
|
|
auto attachmentFormat = vk::Format::eR8G8B8A8Srgb;
|
|
|
|
Pipeline pipeline = CreatePipeline(&device, attachmentFormat, &commitManager);
|
|
Pipeline backGroundPipeline = CreateBackgroundPipeline(&device, attachmentFormat, &commitManager);
|
|
|
|
lightManager.AddPoint(vec3{-5.0f, -5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
|
|
lightManager.AddPoint(vec3{5.0f, -5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
|
|
lightManager.AddPoint(vec3{-5.0f, 5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
|
|
lightManager.AddPoint(vec3{5.0f, 5.0f, 5.0f}, vec3{1.0f}, 30.0f, 16.0f);
|
|
|
|
lightManager.Update();
|
|
|
|
vk::DescriptorPool descriptorPool;
|
|
vk::DescriptorSet perFrameDescriptor;
|
|
|
|
{
|
|
vk::DescriptorSetLayout descriptorSetLayout = pipeline.m_SetLayouts[1];
|
|
eastl::array poolSizes = {
|
|
vk::DescriptorPoolSize{
|
|
.type = vk::DescriptorType::eUniformBuffer,
|
|
.descriptorCount = 3,
|
|
},
|
|
};
|
|
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo = {
|
|
.maxSets = 1, .poolSizeCount = static_cast<u32>(poolSizes.size()), .pPoolSizes = poolSizes.data()};
|
|
AbortIfFailed(device.m_Device.createDescriptorPool(&descriptorPoolCreateInfo, nullptr, &descriptorPool));
|
|
|
|
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo = {
|
|
.descriptorPool = descriptorPool,
|
|
.descriptorSetCount = 1,
|
|
.pSetLayouts = &descriptorSetLayout,
|
|
};
|
|
AbortIfFailed(device.m_Device.allocateDescriptorSets(&descriptorSetAllocateInfo, &perFrameDescriptor));
|
|
}
|
|
|
|
vk::Extent2D internalResolution = {1920, 1080};
|
|
|
|
CameraController cameraController = {vec3{0.0f, 0.0f, 2.0f}, vec3{0.0f}, 70_deg,
|
|
static_cast<f32>(swapchain.m_Extent.width) / static_cast<f32>(swapchain.m_Extent.height)};
|
|
|
|
usize uboSize = 0;
|
|
usize cameraSize = sizeof cameraController.m_Camera;
|
|
uboSize += ClosestMultiple(cameraSize, physicalDeviceOffsetAlignment);
|
|
usize lightOffset = uboSize;
|
|
usize lightingSize = sizeof environment + sizeof lightManager.m_MetaInfo;
|
|
uboSize += ClosestMultiple(lightingSize, physicalDeviceOffsetAlignment);
|
|
|
|
auto data = new u8[uboSize];
|
|
memcpy(data, &cameraController.m_Camera, cameraSize);
|
|
memcpy(data + lightOffset, &environment, sizeof environment);
|
|
memcpy(data + lightOffset + sizeof environment, &lightManager.m_MetaInfo, sizeof lightManager.m_MetaInfo);
|
|
|
|
auto ubo = resourceManager.Buffers().CreateUniformBuffer(uboSize, "Desc1 UBO");
|
|
ubo->Write(0, ubo->m_Size, data);
|
|
|
|
delete[] data;
|
|
|
|
vk::DescriptorBufferInfo cameraBufferInfo = {
|
|
.buffer = ubo->m_Buffer,
|
|
.offset = 0,
|
|
.range = cameraSize,
|
|
};
|
|
vk::DescriptorBufferInfo lightingBufferInfo = {
|
|
.buffer = ubo->m_Buffer,
|
|
.offset = lightOffset,
|
|
.range = lightingSize,
|
|
};
|
|
eastl::array writeDescriptors = {
|
|
vk::WriteDescriptorSet{
|
|
.dstSet = perFrameDescriptor,
|
|
.dstBinding = 0,
|
|
.dstArrayElement = 0,
|
|
.descriptorCount = 1,
|
|
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
|
.pBufferInfo = &cameraBufferInfo,
|
|
},
|
|
vk::WriteDescriptorSet{
|
|
.dstSet = perFrameDescriptor,
|
|
.dstBinding = 1,
|
|
.dstArrayElement = 0,
|
|
.descriptorCount = 1,
|
|
.descriptorType = vk::DescriptorType::eUniformBuffer,
|
|
.pBufferInfo = &lightingBufferInfo,
|
|
},
|
|
};
|
|
device.m_Device.updateDescriptorSets(static_cast<u32>(writeDescriptors.size()), writeDescriptors.data(), 0, nullptr);
|
|
|
|
commitManager.Update();
|
|
|
|
// Persistent variables
|
|
vk::Viewport viewport = {
|
|
.x = 0,
|
|
.y = static_cast<f32>(internalResolution.height),
|
|
.width = static_cast<f32>(internalResolution.width),
|
|
.height = -static_cast<f32>(internalResolution.height),
|
|
.minDepth = 0.0,
|
|
.maxDepth = 1.0,
|
|
};
|
|
|
|
vk::Rect2D scissor = {
|
|
.offset = {0, 0},
|
|
.extent = internalResolution,
|
|
};
|
|
|
|
vk::ImageSubresourceRange subresourceRange = {
|
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
|
.baseMipLevel = 0,
|
|
.levelCount = 1,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = 1,
|
|
};
|
|
|
|
vk::ImageMemoryBarrier2 preRenderBarrier = {
|
|
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
|
|
.srcAccessMask = vk::AccessFlagBits2::eTransferRead,
|
|
.dstStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
|
|
.dstAccessMask = vk::AccessFlagBits2::eColorAttachmentWrite,
|
|
.oldLayout = vk::ImageLayout::eUndefined,
|
|
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
|
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.subresourceRange = subresourceRange,
|
|
};
|
|
vk::DependencyInfo preRenderDependencies = {
|
|
.imageMemoryBarrierCount = 1,
|
|
.pImageMemoryBarriers = &preRenderBarrier,
|
|
};
|
|
|
|
vk::ImageMemoryBarrier2 renderToBlitBarrier = {
|
|
.srcStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
|
|
.srcAccessMask = vk::AccessFlagBits2::eColorAttachmentWrite,
|
|
.dstStageMask = vk::PipelineStageFlagBits2::eBlit,
|
|
.dstAccessMask = vk::AccessFlagBits2::eTransferRead,
|
|
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
|
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
|
|
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.subresourceRange = subresourceRange,
|
|
};
|
|
vk::ImageMemoryBarrier2 acquireToTransferDstBarrier = {
|
|
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
|
|
.srcAccessMask = vk::AccessFlagBits2::eNone,
|
|
.dstStageMask = vk::PipelineStageFlagBits2::eBlit,
|
|
.dstAccessMask = vk::AccessFlagBits2::eTransferWrite,
|
|
.oldLayout = vk::ImageLayout::eUndefined,
|
|
.newLayout = vk::ImageLayout::eTransferDstOptimal,
|
|
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.subresourceRange = subresourceRange,
|
|
};
|
|
eastl::array postRenderBarriers = {
|
|
renderToBlitBarrier,
|
|
acquireToTransferDstBarrier,
|
|
};
|
|
vk::DependencyInfo postRenderDependencies = {
|
|
.imageMemoryBarrierCount = static_cast<u32>(postRenderBarriers.size()),
|
|
.pImageMemoryBarriers = postRenderBarriers.data(),
|
|
};
|
|
|
|
vk::ImageMemoryBarrier2 transferDstToGuiRenderBarrier = {
|
|
.srcStageMask = vk::PipelineStageFlagBits2::eTransfer,
|
|
.srcAccessMask = vk::AccessFlagBits2::eTransferWrite | vk::AccessFlagBits2::eTransferRead,
|
|
.dstStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
|
|
.dstAccessMask = vk::AccessFlagBits2::eColorAttachmentRead,
|
|
.oldLayout = vk::ImageLayout::eTransferDstOptimal,
|
|
.newLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
|
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.subresourceRange = subresourceRange,
|
|
};
|
|
vk::DependencyInfo preGuiDependencies = {
|
|
.imageMemoryBarrierCount = 1,
|
|
.pImageMemoryBarriers = &transferDstToGuiRenderBarrier,
|
|
};
|
|
|
|
vk::ImageMemoryBarrier2 prePresentBarrier = {
|
|
.srcStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput,
|
|
.srcAccessMask = vk::AccessFlagBits2::eColorAttachmentWrite,
|
|
.dstStageMask = vk::PipelineStageFlagBits2::eBottomOfPipe,
|
|
.dstAccessMask = vk::AccessFlagBits2::eNone,
|
|
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
|
.newLayout = vk::ImageLayout::ePresentSrcKHR,
|
|
.srcQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.dstQueueFamilyIndex = vk::QueueFamilyIgnored,
|
|
.subresourceRange = subresourceRange,
|
|
};
|
|
vk::DependencyInfo prePresentDependencies = {
|
|
.imageMemoryBarrierCount = 1,
|
|
.pImageMemoryBarriers = &prePresentBarrier,
|
|
};
|
|
|
|
FrameManager frameManager = {&device, queueAllocation.m_Family, MAX_FRAMES_IN_FLIGHT};
|
|
eastl::fixed_vector<Ref<ImageView>, MAX_FRAMES_IN_FLIGHT> depthImages;
|
|
eastl::fixed_vector<Ref<ImageView>, MAX_FRAMES_IN_FLIGHT> attachmentImages;
|
|
|
|
for (u32 index = 0; index < frameManager.m_FramesInFlight; ++index)
|
|
{
|
|
auto name = fmt::format("Depth Frame{}", index);
|
|
depthImages.emplace_back(resourceManager.CombinedImageViews().CreateDepthStencilImage({
|
|
.m_Extent = internalResolution,
|
|
.m_Name = name.c_str(),
|
|
}));
|
|
|
|
name = fmt::format("Attachment0 Frame{}", index);
|
|
attachmentImages.emplace_back(resourceManager.CombinedImageViews().CreateAttachment({
|
|
.m_Format = attachmentFormat,
|
|
.m_Extent = internalResolution,
|
|
.m_Name = name.c_str(),
|
|
}));
|
|
}
|
|
|
|
gui::Init(&context, &device, &window, swapchain.m_Format, static_cast<u32>(swapchain.m_ImageViews.size()),
|
|
queueAllocation.m_Family, graphicsQueue);
|
|
bool rotating = false;
|
|
bool lockToScreen = true;
|
|
bool showDiffuse = false;
|
|
bool useDiffuse = true;
|
|
bool showPrefilter = false;
|
|
bool useSpecular = true;
|
|
|
|
constexpr static u32 USE_DIFFUSE_BIT = 1;
|
|
constexpr static u32 USE_SPECULAR_BIT = 1 << 1;
|
|
constexpr static u32 SHOW_DIFFUSE_BIT = 1 << 2;
|
|
constexpr static u32 SHOW_PREFILTER_BIT = 1 << 3;
|
|
|
|
i32 height = static_cast<i32>(internalResolution.height);
|
|
f32 camPitch = glm::degrees(cameraController.m_Pitch);
|
|
f32 camYaw = glm::degrees(cameraController.m_Yaw);
|
|
vec3 camPosition = cameraController.m_Camera.m_Position;
|
|
vk::Extent2D inputResolution = internalResolution;
|
|
|
|
swapchain.RegisterResizeCallback([&cameraController](vk::Extent2D extent) {
|
|
cameraController.SetAspectRatio(static_cast<f32>(extent.width) / static_cast<f32>(extent.height));
|
|
});
|
|
|
|
Time::Init();
|
|
|
|
INFO("Starting loop");
|
|
while (window.Poll())
|
|
{
|
|
Time::Update();
|
|
commitManager.Update();
|
|
|
|
gui::StartBuild();
|
|
|
|
gui::Begin("Settings");
|
|
gui::Text("Window Resolution: %ux%u", swapchain.m_Extent.width, swapchain.m_Extent.height);
|
|
gui::Text("Render Resolution: %ux%u", internalResolution.width, internalResolution.height);
|
|
gui::Checkbox("Lock Resolution to Window", &lockToScreen);
|
|
if (!lockToScreen)
|
|
{
|
|
if (gui::InputInt("FrameBuffer Height", &height, 1, 10))
|
|
{
|
|
height = eastl::clamp(height, 64, 4320);
|
|
}
|
|
|
|
inputResolution.height = height;
|
|
inputResolution.width = static_cast<i32>(cameraController.m_AspectRatio * static_cast<f32>(inputResolution.height));
|
|
|
|
if (gui::Button("Change Resolution"))
|
|
{
|
|
if (inputResolution.width != internalResolution.width ||
|
|
inputResolution.height != internalResolution.height)
|
|
{
|
|
internalResolution = inputResolution;
|
|
viewport.width = static_cast<f32>(internalResolution.width);
|
|
viewport.height = -static_cast<f32>(internalResolution.height);
|
|
viewport.y = static_cast<f32>(internalResolution.height);
|
|
scissor.extent = internalResolution;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (swapchain.m_Extent.width != internalResolution.width ||
|
|
swapchain.m_Extent.height != internalResolution.height)
|
|
{
|
|
internalResolution = swapchain.m_Extent;
|
|
viewport.width = static_cast<f32>(internalResolution.width);
|
|
viewport.height = -static_cast<f32>(internalResolution.height);
|
|
viewport.y = static_cast<f32>(internalResolution.height);
|
|
scissor.extent = internalResolution;
|
|
}
|
|
}
|
|
gui::Separator();
|
|
gui::Text("Delta: %0.6f ms", 1000.0f * Time::m_Delta);
|
|
gui::Text("FPS: %0.6f", 1.0f / Time::m_Delta);
|
|
gui::Separator();
|
|
gui::PushItemWidth(100);
|
|
bool yawChange = gui::DragFloat("Camera Yaw", &camYaw);
|
|
bool pitchChange = gui::DragFloat("Camera Pitch", &camPitch, 1, -89.0f, 89.0f);
|
|
if (yawChange || pitchChange)
|
|
{
|
|
camYaw = camYaw - floor((camYaw + 180.0f) / 360.0f) * 360.0f;
|
|
cameraController.SetPitchYaw(glm::radians(camPitch), glm::radians(camYaw));
|
|
}
|
|
if (gui::InputFloat3("Camera Position", reinterpret_cast<f32 *>(&camPosition)))
|
|
{
|
|
cameraController.SetPosition(camPosition);
|
|
}
|
|
gui::Separator();
|
|
gui::Text("IBL");
|
|
gui::Checkbox("Show DiffIrr", &showDiffuse);
|
|
gui::Checkbox("Use DiffIrr", &useDiffuse);
|
|
gui::Checkbox("Show Prefilter", &showPrefilter);
|
|
gui::Checkbox("Use Specular", &useSpecular);
|
|
gui::Separator();
|
|
gui::Checkbox("Rotate", &rotating);
|
|
gui::PopItemWidth();
|
|
if (gui::Button("Exit"))
|
|
{
|
|
window.RequestExit();
|
|
}
|
|
gui::End();
|
|
|
|
gui::EndBuild();
|
|
|
|
if (rotating)
|
|
{
|
|
model.SetModelTransform(
|
|
rotate(model.GetModelTransform(), static_cast<f32>(45.0_deg * Time::m_Delta), vec3(0.0f, 1.0f, 0.0f)));
|
|
}
|
|
model.Update();
|
|
cameraController.m_Camera.CalculateInverses();
|
|
ubo->Write(0, sizeof cameraController.m_Camera, &cameraController.m_Camera);
|
|
|
|
Frame *currentFrame = frameManager.GetNextFrame(&swapchain, &surface, window.GetSize());
|
|
|
|
u32 imageIndex = currentFrame->m_ImageIdx;
|
|
vk::Image currentSwapchainImage = swapchain.m_Images[imageIndex];
|
|
vk::ImageView currentSwapchainImageView = swapchain.m_ImageViews[imageIndex];
|
|
vk::CommandBuffer cmd = currentFrame->m_CommandBuffer;
|
|
|
|
auto ¤tDepthImage = depthImages[currentFrame->m_FrameIdx];
|
|
auto ¤tAttachment = attachmentImages[currentFrame->m_FrameIdx];
|
|
|
|
if (currentAttachment->m_Extent.width != internalResolution.width ||
|
|
currentAttachment->m_Extent.height != internalResolution.height)
|
|
{
|
|
auto name = fmt::format("Depth Frame{}", currentFrame->m_FrameIdx);
|
|
currentDepthImage = resourceManager.CombinedImageViews().CreateDepthStencilImage({
|
|
.m_Extent = internalResolution,
|
|
.m_Name = name.c_str(),
|
|
});
|
|
|
|
name = fmt::format("Attachment0 Frame{}", currentFrame->m_FrameIdx);
|
|
currentAttachment = resourceManager.CombinedImageViews().CreateAttachment({
|
|
.m_Format = attachmentFormat,
|
|
.m_Extent = internalResolution,
|
|
.m_Name = name.c_str(),
|
|
});
|
|
}
|
|
|
|
vk::ImageView currentDepthImageView = currentDepthImage->m_View;
|
|
vk::Image currentImage = currentAttachment->m_Image->m_Image;
|
|
vk::ImageView currentImageView = currentAttachment->m_View;
|
|
|
|
preRenderBarrier.image = currentImage;
|
|
postRenderBarriers[0].image = currentImage;
|
|
postRenderBarriers[1].image = currentSwapchainImage;
|
|
transferDstToGuiRenderBarrier.image = currentSwapchainImage;
|
|
prePresentBarrier.image = currentSwapchainImage;
|
|
|
|
vk::CommandBufferBeginInfo beginInfo = {.flags = vk::CommandBufferUsageFlagBits::eOneTimeSubmit};
|
|
AbortIfFailed(cmd.begin(&beginInfo));
|
|
|
|
cmd.pipelineBarrier2(&preRenderDependencies);
|
|
|
|
// Render
|
|
eastl::array attachmentInfos = {
|
|
vk::RenderingAttachmentInfo{
|
|
.imageView = currentImageView,
|
|
.imageLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
|
.resolveMode = vk::ResolveModeFlagBits::eNone,
|
|
.loadOp = vk::AttachmentLoadOp::eClear,
|
|
.storeOp = vk::AttachmentStoreOp::eStore,
|
|
.clearValue = vk::ClearColorValue{0.0f, 0.0f, 0.0f, 1.0f},
|
|
},
|
|
};
|
|
|
|
vk::RenderingAttachmentInfo depthAttachment = {
|
|
.imageView = currentDepthImageView,
|
|
.imageLayout = vk::ImageLayout::eDepthAttachmentOptimal,
|
|
.resolveMode = vk::ResolveModeFlagBits::eNone,
|
|
.loadOp = vk::AttachmentLoadOp::eClear,
|
|
.storeOp = vk::AttachmentStoreOp::eDontCare,
|
|
.clearValue = vk::ClearDepthStencilValue{.depth = 1.0f, .stencil = 0},
|
|
};
|
|
|
|
vk::RenderingInfo renderingInfo = {
|
|
.renderArea = {.extent = ToExtent2D(currentAttachment->m_Extent)},
|
|
.layerCount = 1,
|
|
.colorAttachmentCount = static_cast<u32>(attachmentInfos.size()),
|
|
.pColorAttachments = attachmentInfos.data(),
|
|
.pDepthAttachment = &depthAttachment,
|
|
};
|
|
|
|
cmd.beginRendering(&renderingInfo);
|
|
|
|
cmd.setViewport(0, 1, &viewport);
|
|
cmd.setScissor(0, 1, &scissor);
|
|
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 0, 1,
|
|
&commitManager.GetDescriptorSet(), 0, nullptr);
|
|
|
|
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline.m_Layout, 1, 1, &perFrameDescriptor, 0,
|
|
nullptr);
|
|
|
|
cmd.bindIndexBuffer(model.m_IndexBuffer->m_Buffer, 0, vk::IndexType::eUint32);
|
|
|
|
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline.m_Pipeline);
|
|
|
|
u32 flags = 0;
|
|
if (useSpecular)
|
|
{
|
|
flags |= USE_SPECULAR_BIT;
|
|
}
|
|
if (useDiffuse)
|
|
{
|
|
flags |= USE_DIFFUSE_BIT;
|
|
}
|
|
if (showPrefilter)
|
|
{
|
|
flags |= SHOW_PREFILTER_BIT;
|
|
}
|
|
if (showDiffuse)
|
|
{
|
|
flags |= SHOW_DIFFUSE_BIT;
|
|
}
|
|
|
|
u32 pcbOffset = 0;
|
|
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof model.m_Handles,
|
|
&model.m_Handles);
|
|
pcbOffset += sizeof model.m_Handles;
|
|
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, pcbOffset, sizeof flags, &flags);
|
|
pcbOffset += sizeof flags;
|
|
|
|
for (auto &prim : model.m_MeshPrimitives)
|
|
{
|
|
u32 innerPcbOffset = pcbOffset;
|
|
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, innerPcbOffset,
|
|
sizeof prim.m_MaterialIdx, &prim.m_MaterialIdx);
|
|
innerPcbOffset += sizeof prim.m_MaterialIdx;
|
|
cmd.pushConstants(pipeline.m_Layout, vk::ShaderStageFlagBits::eAll, innerPcbOffset,
|
|
sizeof prim.m_TransformIdx, &prim.m_TransformIdx);
|
|
innerPcbOffset += sizeof prim.m_TransformIdx;
|
|
cmd.drawIndexed(prim.m_IndexCount, 1, prim.m_FirstIndex, static_cast<i32>(prim.m_VertexOffset), 0);
|
|
}
|
|
|
|
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, backGroundPipeline.m_Pipeline);
|
|
cmd.draw(3, 1, 0, 0);
|
|
|
|
cmd.endRendering();
|
|
|
|
cmd.pipelineBarrier2(&postRenderDependencies);
|
|
|
|
vk::ImageBlit blitRegion = {
|
|
.srcSubresource =
|
|
{
|
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
|
.mipLevel = 0,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = 1,
|
|
},
|
|
.srcOffsets =
|
|
std::array{
|
|
vk::Offset3D{0, 0, 0},
|
|
ToOffset3D(currentAttachment->m_Extent),
|
|
},
|
|
.dstSubresource =
|
|
{
|
|
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
|
.mipLevel = 0,
|
|
.baseArrayLayer = 0,
|
|
.layerCount = 1,
|
|
},
|
|
.dstOffsets =
|
|
std::array{
|
|
vk::Offset3D{0, 0, 0},
|
|
vk::Offset3D{static_cast<i32>(swapchain.m_Extent.width), static_cast<i32>(swapchain.m_Extent.height), 1},
|
|
},
|
|
};
|
|
cmd.blitImage(currentImage, postRenderBarriers[0].newLayout, currentSwapchainImage,
|
|
postRenderBarriers[1].newLayout, 1, &blitRegion, vk::Filter::eLinear);
|
|
|
|
cmd.pipelineBarrier2(&preGuiDependencies);
|
|
|
|
gui::Draw(cmd, swapchain.m_Extent, currentSwapchainImageView);
|
|
|
|
cmd.pipelineBarrier2(&prePresentDependencies);
|
|
|
|
AbortIfFailed(cmd.end());
|
|
|
|
vk::PipelineStageFlags waitDstStage = vk::PipelineStageFlagBits::eTransfer;
|
|
vk::SubmitInfo submitInfo = {
|
|
.waitSemaphoreCount = 1,
|
|
.pWaitSemaphores = ¤tFrame->m_ImageAcquireSem,
|
|
.pWaitDstStageMask = &waitDstStage,
|
|
.commandBufferCount = 1,
|
|
.pCommandBuffers = &cmd,
|
|
.signalSemaphoreCount = 1,
|
|
.pSignalSemaphores = ¤tFrame->m_RenderFinishSem,
|
|
};
|
|
AbortIfFailed(graphicsQueue.submit(1, &submitInfo, currentFrame->m_FrameAvailableFence));
|
|
|
|
currentFrame->Present(graphicsQueue, &swapchain, &surface, window.GetSize());
|
|
}
|
|
|
|
device.WaitIdle();
|
|
|
|
pipelineCacheData = device.DumpPipelineCache();
|
|
ERROR_IF(!WriteFileBytes(PIPELINE_CACHE_FILE, pipelineCacheData), "Pipeline Cache incorrectly written");
|
|
|
|
gui::Destroy(&device);
|
|
|
|
device.m_Device.destroy(descriptorPool, nullptr);
|
|
|
|
return 0;
|
|
} |