Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gameknife committed Jun 12, 2024
2 parents 9af7d17 + 03c6f8d commit fc02ae9
Show file tree
Hide file tree
Showing 18 changed files with 422 additions and 143 deletions.
21 changes: 20 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@
"unordered_set": "cpp",
"valarray": "cpp",
"variant": "cpp",
"algorithm": "cpp"
"algorithm": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"compare": "cpp",
"concepts": "cpp",
"expected": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"format": "cpp",
"ranges": "cpp",
"stop_token": "cpp",
"cfenv": "cpp"
},
}
5 changes: 3 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
android:theme="@style/Application.Fullscreen">

<activity android:name=".MainActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize|keyboardHidden"
android:exported="true"

tools:ignore="DiscouragedApi">
<!-- Tell GameActivity the name of our .so file.
This will be optional after the release 1.1.0 -->
Expand Down
6 changes: 5 additions & 1 deletion src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ Assets::UniformBufferObject NextRendererApplication<Renderer>::GetUniformBufferO
extent.width / static_cast<float>(extent.height), 0.1f, 10000.0f);
ubo.Projection[1][1] *= -1;
#if ANDROID
ubo.Projection = glm::perspective(glm::radians(userSettings_.FieldOfView),
extent.height / static_cast<float>(extent.width), 0.1f, 10000.0f);
ubo.Projection[1][1] *= -1;
ubo.Projection = pre_rotate_mat * ubo.Projection;
#endif
// Inverting Y for Vulkan, https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/
Expand Down Expand Up @@ -233,6 +236,7 @@ void NextRendererApplication<Renderer>::Render(VkCommandBuffer commandBuffer, co
Statistics stats = {};
stats.FramebufferSize = Renderer::Window().FramebufferSize();
stats.FrameRate = frameRate;
stats.FrameTime = static_cast<float>(timeDelta * 1000);

stats.CamPosX = modelViewController_.Position()[0];
stats.CamPosY = modelViewController_.Position()[1];
Expand All @@ -254,7 +258,7 @@ void NextRendererApplication<Renderer>::Render(VkCommandBuffer commandBuffer, co
stats.TotalSamples = totalNumberOfSamples_;
}

userInterface_->Render(commandBuffer, Renderer::SwapChainFrameBuffer(imageIndex), stats);
userInterface_->Render(commandBuffer, Renderer::SwapChainFrameBuffer(imageIndex), stats, Renderer::GpuTimer());
}

template <typename Renderer>
Expand Down
1 change: 1 addition & 0 deletions src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Options::Options(const int argc, const char* argv[])
("help", "Display help message.")
("benchmark", bool_switch(&Benchmark)->default_value(false), "Run the application in benchmark mode.")
("savefile", bool_switch(&SaveFile)->default_value(false), "Save screenshot every benchmark finish.")
("renderdoc", bool_switch(&RenderDoc)->default_value(false), "Attach renderdoc if avaliable.")
;

desc.add(benchmark);
Expand Down
1 change: 1 addition & 0 deletions src/Options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Options final
// Application options.
bool Benchmark{};
bool SaveFile{};
bool RenderDoc{};

// Benchmark options.
bool BenchmarkNextScenes{};
Expand Down
24 changes: 18 additions & 6 deletions src/UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <array>

#include "Utilities/FileHelper.hpp"
#include "Vulkan/VulkanBaseRenderer.hpp"

namespace
{
Expand Down Expand Up @@ -94,7 +95,7 @@ UserInterface::UserInterface(

// Window scaling and style.
#if ANDROID
const auto scaleFactor = 4.0;
const auto scaleFactor = 1.5;
#else
const auto scaleFactor = 1.0;//window.ContentScale();
#endif
Expand Down Expand Up @@ -130,7 +131,7 @@ UserInterface::~UserInterface()
ImGui::DestroyContext();
}

void UserInterface::Render(VkCommandBuffer commandBuffer, const Vulkan::FrameBuffer& frameBuffer, const Statistics& statistics)
void UserInterface::Render(VkCommandBuffer commandBuffer, const Vulkan::FrameBuffer& frameBuffer, const Statistics& statistics, Vulkan::VulkanGpuTimer* gpuTimer)
{
#if !ANDROID
ImGui_ImplGlfw_NewFrame();
Expand All @@ -143,7 +144,7 @@ void UserInterface::Render(VkCommandBuffer commandBuffer, const Vulkan::FrameBuf
#if !ANDROID
DrawSettings();
#endif
DrawOverlay(statistics);
DrawOverlay(statistics, gpuTimer);
//ImGui::ShowStyleEditor();
ImGui::Render();

Expand Down Expand Up @@ -258,7 +259,7 @@ void UserInterface::DrawSettings()
ImGui::End();
}

void UserInterface::DrawOverlay(const Statistics& statistics)
void UserInterface::DrawOverlay(const Statistics& statistics, Vulkan::VulkanGpuTimer* gpuTimer)
{
if (!Settings().ShowOverlay)
{
Expand All @@ -267,8 +268,13 @@ void UserInterface::DrawOverlay(const Statistics& statistics)

const auto& io = ImGui::GetIO();
const float distance = 10.0f;
#if ANDROID
const ImVec2 pos = ImVec2(distance, distance);
const ImVec2 posPivot = ImVec2(0.0f, 0.0f);
#else
const ImVec2 pos = ImVec2(io.DisplaySize.x - distance, distance);
const ImVec2 posPivot = ImVec2(1.0f, 0.0f);
#endif
ImGui::SetNextWindowPos(pos, ImGuiCond_Always, posPivot);
ImGui::SetNextWindowBgAlpha(0.3f); // Transparent background

Expand All @@ -285,11 +291,17 @@ void UserInterface::DrawOverlay(const Statistics& statistics)
ImGui::Text("Statistics (%dx%d):", statistics.FramebufferSize.width, statistics.FramebufferSize.height);
ImGui::Separator();
ImGui::Text("Frame rate: %.0f fps", statistics.FrameRate);
ImGui::Text("Primary ray rate: %.2f Gr/s", statistics.RayRate);
ImGui::Text("Accumulated samples: %u", statistics.TotalSamples);
ImGui::Text("Campos: %.2f %.2f %.2f", statistics.CamPosX, statistics.CamPosY, statistics.CamPosZ);
ImGui::Text("Tris: %d", statistics.TriCount);
ImGui::Text("Instance: %d", statistics.InstanceCount);

ImGui::Text("frametime: %.2fms", statistics.FrameTime);
// auto fetch timer & display
auto times = gpuTimer->FetchAllTimes();
for(auto& time : times)
{
ImGui::Text("%s: %.2fms", std::get<0>(time).c_str(), std::get<1>(time));
}
}
ImGui::End();
}
6 changes: 4 additions & 2 deletions src/UserInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Vulkan
class FrameBuffer;
class RenderPass;
class SwapChain;
class VulkanGpuTimer;
}

struct UserSettings;
Expand All @@ -18,6 +19,7 @@ struct Statistics final
{
VkExtent2D FramebufferSize;
float FrameRate;
float FrameTime;
float RayRate;
uint32_t TotalSamples;
float CamPosX;
Expand All @@ -42,7 +44,7 @@ class UserInterface final
UserSettings& userSettings);
~UserInterface();

void Render(VkCommandBuffer commandBuffer, const Vulkan::FrameBuffer& frameBuffer, const Statistics& statistics);
void Render(VkCommandBuffer commandBuffer, const Vulkan::FrameBuffer& frameBuffer, const Statistics& statistics, Vulkan::VulkanGpuTimer* gpuTimer);

bool WantsToCaptureKeyboard() const;
bool WantsToCaptureMouse() const;
Expand All @@ -52,7 +54,7 @@ class UserInterface final
private:

void DrawSettings();
void DrawOverlay(const Statistics& statistics);
void DrawOverlay(const Statistics& statistics, Vulkan::VulkanGpuTimer* gpuTimer);

std::unique_ptr<Vulkan::DescriptorPool> descriptorPool_;
std::unique_ptr<Vulkan::RenderPass> renderPass_;
Expand Down
2 changes: 2 additions & 0 deletions src/Vulkan/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Device::Device(
vkGetDeviceQueue(device_, computeFamilyIndex_, 0, &computeQueue_);
vkGetDeviceQueue(device_, presentFamilyIndex_, 0, &presentQueue_);
//vkGetDeviceQueue(device_, transferFamilyIndex_, 0, &transferQueue_);

vkGetPhysicalDeviceProperties(PhysicalDevice(), &deviceProp_);
}

Device::~Device()
Expand Down
4 changes: 4 additions & 0 deletions src/Vulkan/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace Vulkan
VkQueue PresentQueue() const { return presentQueue_; }
//VkQueue TransferQueue() const { return transferQueue_; }

VkPhysicalDeviceProperties DeviceProperties() const { return deviceProp_; }

void WaitIdle() const;

private:
Expand All @@ -60,6 +62,8 @@ namespace Vulkan
VkQueue computeQueue_{};
VkQueue presentQueue_{};
//VkQueue transferQueue_{};

VkPhysicalDeviceProperties deviceProp_;
};

}
95 changes: 52 additions & 43 deletions src/Vulkan/LegacyDeferred/LegacyDeferredRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,54 +150,63 @@ void LegacyDeferredRenderer::Render(VkCommandBuffer commandBuffer, uint32_t imag
renderPassInfo.renderArea.extent = SwapChain().Extent();
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();

// make it to generate gbuffer
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);

{
const auto& scene = GetScene();

VkDescriptorSet descriptorSets[] = { gbufferPipeline_->DescriptorSet(imageIndex) };
VkBuffer vertexBuffers[] = { scene.VertexBuffer().Handle() };
const VkBuffer indexBuffer = scene.IndexBuffer().Handle();
VkDeviceSize offsets[] = { 0 };

vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, gbufferPipeline_->Handle());
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, gbufferPipeline_->PipelineLayout().Handle(), 0, 1, descriptorSets, 0, nullptr);
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT32);

// indirect draw
vkCmdDrawIndexedIndirect(commandBuffer, scene.IndirectDrawBuffer().Handle(), 0, scene.GetIndirectDrawBatchCount(), sizeof(VkDrawIndexedIndirectCommand));
}
vkCmdEndRenderPass(commandBuffer);

ImageMemoryBarrier::Insert(commandBuffer, outputImage_->Handle(), subresourceRange,
0, VK_ACCESS_SHADER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
SCOPED_GPU_TIMER("drawpass");
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
{
const auto& scene = GetScene();

VkDescriptorSet descriptorSets[] = { gbufferPipeline_->DescriptorSet(imageIndex) };
VkBuffer vertexBuffers[] = { scene.VertexBuffer().Handle() };
const VkBuffer indexBuffer = scene.IndexBuffer().Handle();
VkDeviceSize offsets[] = { 0 };

vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, gbufferPipeline_->Handle());
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, gbufferPipeline_->PipelineLayout().Handle(), 0, 1, descriptorSets, 0, nullptr);
vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets);
vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT32);

// indirect draw
vkCmdDrawIndexedIndirect(commandBuffer, scene.IndirectDrawBuffer().Handle(), 0, scene.GetIndirectDrawBatchCount(), sizeof(VkDrawIndexedIndirectCommand));
}
vkCmdEndRenderPass(commandBuffer);

ImageMemoryBarrier::Insert(commandBuffer, outputImage_->Handle(), subresourceRange,
0, VK_ACCESS_SHADER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_GENERAL);
ImageMemoryBarrier::Insert(commandBuffer, gbuffer0BufferImage_->Handle(), subresourceRange,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_GENERAL);
ImageMemoryBarrier::Insert(commandBuffer, gbuffer1BufferImage_->Handle(), subresourceRange,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_GENERAL);
ImageMemoryBarrier::Insert(commandBuffer, gbuffer0BufferImage_->Handle(), subresourceRange,
ImageMemoryBarrier::Insert(commandBuffer, gbuffer2BufferImage_->Handle(), subresourceRange,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_GENERAL);
ImageMemoryBarrier::Insert(commandBuffer, gbuffer1BufferImage_->Handle(), subresourceRange,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_GENERAL);
ImageMemoryBarrier::Insert(commandBuffer, gbuffer2BufferImage_->Handle(), subresourceRange,
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_GENERAL);
// cs shading pass
VkDescriptorSet denoiserDescriptorSets[] = {deferredShadingPipeline_->DescriptorSet(imageIndex)};
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, deferredShadingPipeline_->Handle());
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE,
deferredShadingPipeline_->PipelineLayout().Handle(), 0, 1, denoiserDescriptorSets, 0, nullptr);
vkCmdDispatch(commandBuffer, SwapChain().Extent().width / 8 / ( CheckerboxRendering() ? 2 : 1 ), SwapChain().Extent().height / 4, 1);
}

// copy to swap-buffer
ImageMemoryBarrier::Insert(commandBuffer, outputImage_->Handle(), subresourceRange,
VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_IMAGE_LAYOUT_GENERAL,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);

ImageMemoryBarrier::Insert(commandBuffer, SwapChain().Images()[imageIndex], subresourceRange, 0,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);

{
SCOPED_GPU_TIMER("shadingpass");

// cs shading pass
VkDescriptorSet denoiserDescriptorSets[] = {deferredShadingPipeline_->DescriptorSet(imageIndex)};
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, deferredShadingPipeline_->Handle());
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE,
deferredShadingPipeline_->PipelineLayout().Handle(), 0, 1, denoiserDescriptorSets, 0, nullptr);
vkCmdDispatch(commandBuffer, SwapChain().Extent().width / 8 / ( CheckerboxRendering() ? 2 : 1 ), SwapChain().Extent().height / 4, 1);

// copy to swap-buffer
ImageMemoryBarrier::Insert(commandBuffer, outputImage_->Handle(), subresourceRange,
VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT, VK_IMAGE_LAYOUT_GENERAL,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);

ImageMemoryBarrier::Insert(commandBuffer, SwapChain().Images()[imageIndex], subresourceRange, 0,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
}


// Copy output image into swap-chain image.
VkImageCopy copyRegion;
Expand Down
Loading

0 comments on commit fc02ae9

Please sign in to comment.