mirror of
https://github.com/scp-fs2open/fs2open.github.com.git
synced 2026-11-12 19:06:09 -10:00
* chore: vendor ImPlot library
Adds ImPlot as a build dependency, to be used for the upcoming ImGui
frame profiler overlay's frametime graph and pie chart.
* feat: add ImGui frame profiler overlay
Replaces the old text-dump frame profile display (-profile_frame_time)
with a runtime-toggleable ImGui/ImPlot overlay showing average/median
frametime, a scrolling frametime graph, and a pie chart of the top
contributors to frame time. Toggled via a new "Frame Profiler Overlay"
option, which also seeds from -profile_frame_time for backward
compatibility and now drives frame profiling collection at runtime
instead of only at startup.
* Add graphics and Vulkan debug stats to ImGui profiler overlay
- Integrated per-backend diagnostic counters into the ImGui frame profiler overlay, triggered by `-gr_debug`.
- Consolidated and cleaned up Vulkan rendering state and descriptor management for compatibility with ImGui's Vulkan pipeline.
- Enabled uniform buffer and draw statistics reporting for enhanced performance insights during debugging.
* Update ImPlot to v1.1 WIP and improve internal consistency
- Adjust copyright notices for extended license coverage (2025-2026).
- Modernize constants with `constexpr` replacements, improving type safety and clarity.
- Enforce `nullptr` usage over `NULL` throughout, aligning with modern C++ practices.
- Add various enhancements to ImPlot legends, markers, and axes behavior.
- Introduce additional helper methods for time handling and timestamp calculations.
- Address UI and typo improvements in comments and documentation, refining accuracy.
* Refactor frame profiler to use per-category self-time accumulation
- Introduced `accumulate_self_times` for single-pass exclusive time computation, improving performance and accuracy.
- Updated `FrameProfiler` and overlay snapshot logic to leverage `Category`'s stable `getId` and `getCount`.
- Replaced pie chart with a stacked bar for frame budget visualization, optimizing rendering cost.
- Cached `get_tid` and `get_pid` with thread-local storage to minimize syscall overhead.
- Added support for unit testing frame profiling functionality and included new test cases.
- Improved code comments and clarified documentation.
* Use dynamic uniform buffers to stop per-draw descriptor set churn in Vulkan
The Material and PerDraw descriptor sets were reallocated and fully rewritten on
essentially every draw call. model_draw_list::render_buffer rebinds ModelData with
a fresh uniform buffer offset for each queued draw, and that offset was part of the
Material set's memoization key, so the cache in VulkanDrawManager::applyMaterial
missed every time - taking a frame-pool allocation, a full template write, the
16-entry material texture resolution and a vkUpdateDescriptorSets with it.
GenericData/Matrices did the same to the PerDraw set. renderShadowDraw was worse
still: all three sets allocated and rewritten per shadow draw, with no memoization
at all.
A large asteroid field (Solaris m21) measured 2566 descriptor sets and 19107
descriptor writes per frame at 1606 model draws, and grew an extra descriptor pool
chunk every frame.
Declare the bindings whose offset moves per draw - MaterialBinding::ModelData,
MaterialBinding::ShadowMapData, PerDrawBinding::GenericData and
PerDrawBinding::Matrices - as eUniformBufferDynamic. DescriptorWriter::setBuffer
now splits a dynamic binding's offset out of the descriptor, writing
{buffer, 0, range} and stashing the offset for vkCmdBindDescriptorSets'
pDynamicOffsets, so an offset-only change leaves the set's contents identical and
the memoization caches start hitting. Shader sources are unchanged.
VulkanStateTracker::bindDescriptorSet now compares the dynamic offsets as well as
the set handle. This is required, not an optimization: the common case is now the
same set rebound with a new offset, and the old handle-only redundancy check would
have skipped that bind and fed every draw the previous draw's uniforms.
renderShadowDraw memoizes its three sets the way applyMaterial does. It does not
invalidate applyMaterial's caches - those record what a set contains, the shadow
pass allocates its own sets rather than overwriting theirs, and it binds through
the state tracker, so the next material cache hit still rebinds correctly.
Also switch allocateFrameSet to the non-allocating allocateDescriptorSets overload;
the vector-returning one heap-allocated once per set, thousands of times a frame.
MAX_SETS_PER_POOL stays at 1024 - the per-frame pool growth it used to log was a
symptom of the churn, and one chunk now covers the scene outright.
Measured on that mission in a Debug build, A/B on the same tree with and without
this change:
descriptor sets/frame @ ~1370 draws 2566 -> 250-345
descriptor writes/frame 19107 -> ~1700-2300
median frametime 92.9/83.6 ms -> 72.0/66.5 ms
Render Buffer (profiler overlay) 24.68 ms (30.8%) -> 12.73 ms (18.4%)
Build Shadow Map 5.44 ms -> off the top-5 entirely
Release builds already inlined most of this away and are unchanged, as expected.
Verified with the validation layers (sync validation enabled): no new VUIDs versus
the same build without the change.
Co-Authored-By: Claude Opus 5 <[email protected]>
* Refactor Vulkan descriptor set binding to reduce dynamic offset churn
- Consolidated dynamic descriptor offset logic into `DescriptorWriter::bindSets`, replacing repetitive `vkCmdBindDescriptorSets` patterns across Vulkan modules.
- Eliminated redundant descriptor binding operations for Material and PerDraw sets by centralizing logic in `bindSets`.
- Simplified descriptors by directly splitting offset changes from descriptor contents, avoiding unnecessary updates.
- Updated comments and documentation to align with the new approach while maintaining reference consistency.
- Applied optimizations across Vulkan modules (`VulkanPostProcessing`, `VulkanDescriptorManager`, etc.) to reduce per-draw overhead and improve readability.
* Enable primitive restart for supported topologies in Vulkan pipeline
- Added `topologySupportsPrimitiveRestart` to determine restart eligibility for each topology.
- Updated `primitiveRestartEnable` to be conditionally set based on topology, addressing compatibility issues with Metal/MoltenVK.
- Prevented crash scenarios on macOS by ensuring pipelines are valid for strip/fan topologies requiring primitive restart.
* Enable VK_KHR_portability_subset for compatibility
* Replace exception with sentinel for non-dynamic Vulkan descriptor bindings
- Introduced `DYNAMIC_SLOT_INVALID` sentinel to replace the throw in `dynamic_offset_slot` for MSVC compatibility.
- Added `static_assert` checks to validate binding constants, ensuring compile-time validation for dynamic offsets.
- Updated comments to clarify design rationale and avoid potential misuse.
* Add memory statistics and tracking to profiler overlay
* fix ci issues
* Add OpenGL memory statistics to the profiler overlay
The memory panel showed no texture bytes and no geometry bytes under
OpenGL. Only the depth renderbuffers went into the render target group.
All three groups now give a value.
Textures: GL_texture_bytes_used holds the bytes of every live texture
cache slot. opengl_texture_release_bytes() removes the bytes of a slot at
each site that drops its texture data. The bitmap render targets record
the bytes of their color texture.
Geometry: the unused GL_vertex_data_in global becomes
GL_geometry_bytes_used, which counts only vertex and index buffers. This
matches the geometry group of the Vulkan backend.
gr_opengl_delete_buffer() sets the size of the buffer to 0, because
opengl_destroy_all_buffers() calls it a second time for a buffer that the
game already deleted.
Render targets: three new functions add the scene textures, the
post-processing textures, and the shadow map to the group. They sum the
bytes at the moment of the call from the textures that exist, thus they
need no bookkeeping at the create and delete sites.
Co-Authored-By: Claude Opus 5 <[email protected]>
* Fix profiler overlay crash on a category that is not a static
The category registry kept a Category pointer for each id. The comment said that all categories
are global statics, but that is not correct. The Lua API tracing_category constructs a Category
and copies it into a script-owned object. The registry then kept a pointer to the dead temporary.
build_overlay_snapshot called getName() on that pointer, made an SCP_string from the garbage
char*, and the process stopped with SIGABRT.
The registry now keeps the name by value and never keeps a Category pointer. getById() becomes
getNameById(), which is all that build_overlay_snapshot needs. A copy of a Category keeps the id
of the original, which stays correct because the id only selects a name.
Add two tests: one shows that the name stays available after the Category object is dead, and one
drives the full path that failed, from processEvent to the overlay snapshot.
Co-Authored-By: Claude Opus 5 <[email protected]>
* Replace category pointers with IDs to prevent use-after-free
- Updated `trace_event` to store `category_id` instead of a pointer to prevent dangling references to category objects that may already be destroyed.
- Modified all references to category names to use `Category::getNameById()` instead of dereferencing pointers.
- Expanded memory statistics in the profiler overlay with new sound, Lua, and OS-level memory usage tracking.
- Added new tests to verify proper use of category IDs in frame profiling and event tracing.
- Improved memory safety and stability across tracing, profiling, and overlay systems.
* Refactor profiler overlay to use snapshot-based frame timing
* Address review feedback on the profiler overlay
Skip complete::end for a scope that did not start. This fixes a crash
when Lua turns the overlay on while a scope is open. Track the GPU begin
event so that turning the overlay off mid-scope still closes it.
Use XSTR 1936 and 1937 for the overlay option. Master now uses 1932
and 1933.
Remove the default value of dynamicOffsets in bindDescriptorSet. Use the
PerDraw binding constants in the PerDraw key.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
* Use wall-clock frame time and read the history without a copy
The Avg and FPS line used traced self-time, which reads too high.
Measure the time between overlay frames instead. Draw the graph with
PlotLineG so the history deque needs no per-frame copy.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
* Address second round of review feedback on the profiler overlay
- Make the category registry thread-safe and give categories with the
same name one id, so the registry does not grow without limit.
- Add tests that toggle the profiler inside an open trace scope.
- Correct the next free XSTR id note to 1938.
- Use the existing Windows include block for psapi.h in osapi.cpp.
- Use list_range in ship_get_num_ships() and shorten some comments.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Return size_t from Category::getCount() and tidy the registry code
- Use structured bindings and std::scoped_lock in the category registry.
- Return size_t from getCount() and update its callers and tests.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Address third round of review feedback on the profiler overlay
- Move the overlay option strings to XSTR 1940 and 1941. The touch
overlay on master uses 1936 to 1939.
- Give FrameProfiler the main thread ID when it is made. Before, it
used the thread of the first event, which was a race.
- Let mouse motion go to the game while the pointer is over the
overlay, so that mouse flight does not stop there.
- Find the dynamic bindings of the PerDraw key with
dynamic_offset_slot(), as the other call sites do.
- Make disable_inside_open_scope count the GPU timestamp queries, so
that it fails if end() does not close the GPU event.
- Make the copy test copy the category, not bind a reference.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* apply review suggestions
---------
Co-authored-by: Claude Opus 5 <[email protected]>
871 lines
31 KiB
C++
871 lines
31 KiB
C++
#include "VulkanPostProcessing.h"
|
|
|
|
#include <array>
|
|
|
|
#include "gr_vulkan.h"
|
|
#include "VulkanRenderer.h"
|
|
#include "VulkanBuffer.h"
|
|
#include "VulkanTexture.h"
|
|
#include "VulkanPipeline.h"
|
|
#include "VulkanDescriptorManager.h"
|
|
#include "graphics/util/uniform_structs.h"
|
|
#include "graphics/util/primitives.h"
|
|
#include "graphics/grinternal.h"
|
|
#include "graphics/light.h"
|
|
#include "graphics/matrix.h"
|
|
#include "graphics/shadows.h"
|
|
#include "graphics/2d.h"
|
|
#include "bmpman/bmpman.h"
|
|
#include "lighting/lighting_profiles.h"
|
|
#include "lighting/lighting.h"
|
|
#include "math/vecmat.h"
|
|
#include "mod_table/mod_table.h"
|
|
#include "render/3d.h"
|
|
#include "tracing/tracing.h"
|
|
#include "nebula/neb.h"
|
|
#include "mission/missionparse.h"
|
|
|
|
extern float Sun_spot;
|
|
extern int Game_subspace_effect;
|
|
extern SCP_vector<light> Lights;
|
|
extern int Num_lights;
|
|
|
|
|
|
namespace graphics::vulkan {
|
|
|
|
|
|
// ===== Light Accumulation (Deferred Lighting) =====
|
|
|
|
void VulkanDeferredLighting::init(PostProcessContext& ctx, const RenderTarget& sceneColor,
|
|
const VulkanDeferredGBuffer& gbuffer, const VulkanShadowMap& shadow)
|
|
{
|
|
m_ctx = &ctx;
|
|
m_sceneColor = &sceneColor;
|
|
m_gbuffer = &gbuffer;
|
|
m_shadow = &shadow;
|
|
}
|
|
|
|
bool VulkanDeferredLighting::initLightVolumes()
|
|
{
|
|
if (m_lightVolumesInitialized) {
|
|
return true;
|
|
}
|
|
|
|
// Generate sphere mesh (16 rings x 16 segments)
|
|
{
|
|
auto mesh = graphics::util::generate_sphere_mesh(16, 16);
|
|
m_sphereMesh.vertexCount = mesh.vertex_count;
|
|
m_sphereMesh.indexCount = mesh.index_count;
|
|
|
|
// Create VBO
|
|
vk::BufferCreateInfo vboInfo;
|
|
vboInfo.size = mesh.vertices.size() * sizeof(float);
|
|
vboInfo.usage = vk::BufferUsageFlagBits::eVertexBuffer;
|
|
vboInfo.sharingMode = vk::SharingMode::eExclusive;
|
|
|
|
try {
|
|
m_sphereMesh.vbo = m_ctx->device.createBuffer(vboInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create sphere VBO: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
|
|
if (!m_ctx->memoryManager->allocateBufferMemory(
|
|
m_sphereMesh.vbo, MemoryUsage::CpuToGpu, m_sphereMesh.vboAlloc, MemoryPurpose::Geometry)) {
|
|
m_ctx->device.destroyBuffer(m_sphereMesh.vbo);
|
|
m_sphereMesh.vbo = nullptr;
|
|
return false;
|
|
}
|
|
|
|
auto* mapped = m_ctx->memoryManager->mapMemory(m_sphereMesh.vboAlloc);
|
|
if (mapped) {
|
|
memcpy(mapped, mesh.vertices.data(), mesh.vertices.size() * sizeof(float));
|
|
m_ctx->memoryManager->unmapMemory(m_sphereMesh.vboAlloc);
|
|
}
|
|
|
|
// Create IBO
|
|
vk::BufferCreateInfo iboInfo;
|
|
iboInfo.size = mesh.indices.size() * sizeof(ushort);
|
|
iboInfo.usage = vk::BufferUsageFlagBits::eIndexBuffer;
|
|
iboInfo.sharingMode = vk::SharingMode::eExclusive;
|
|
|
|
try {
|
|
m_sphereMesh.ibo = m_ctx->device.createBuffer(iboInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create sphere IBO: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
|
|
if (!m_ctx->memoryManager->allocateBufferMemory(
|
|
m_sphereMesh.ibo, MemoryUsage::CpuToGpu, m_sphereMesh.iboAlloc, MemoryPurpose::Geometry)) {
|
|
m_ctx->device.destroyBuffer(m_sphereMesh.ibo);
|
|
m_sphereMesh.ibo = nullptr;
|
|
return false;
|
|
}
|
|
|
|
mapped = m_ctx->memoryManager->mapMemory(m_sphereMesh.iboAlloc);
|
|
if (mapped) {
|
|
memcpy(mapped, mesh.indices.data(), mesh.indices.size() * sizeof(ushort));
|
|
m_ctx->memoryManager->unmapMemory(m_sphereMesh.iboAlloc);
|
|
}
|
|
}
|
|
|
|
// Generate cylinder mesh (16 segments)
|
|
{
|
|
auto mesh = graphics::util::generate_cylinder_mesh(16);
|
|
m_cylinderMesh.vertexCount = mesh.vertex_count;
|
|
m_cylinderMesh.indexCount = mesh.index_count;
|
|
|
|
vk::BufferCreateInfo vboInfo;
|
|
vboInfo.size = mesh.vertices.size() * sizeof(float);
|
|
vboInfo.usage = vk::BufferUsageFlagBits::eVertexBuffer;
|
|
vboInfo.sharingMode = vk::SharingMode::eExclusive;
|
|
|
|
try {
|
|
m_cylinderMesh.vbo = m_ctx->device.createBuffer(vboInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create cylinder VBO: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
|
|
if (!m_ctx->memoryManager->allocateBufferMemory(
|
|
m_cylinderMesh.vbo, MemoryUsage::CpuToGpu, m_cylinderMesh.vboAlloc, MemoryPurpose::Geometry)) {
|
|
m_ctx->device.destroyBuffer(m_cylinderMesh.vbo);
|
|
m_cylinderMesh.vbo = nullptr;
|
|
return false;
|
|
}
|
|
|
|
auto* mapped = m_ctx->memoryManager->mapMemory(m_cylinderMesh.vboAlloc);
|
|
if (mapped) {
|
|
memcpy(mapped, mesh.vertices.data(), mesh.vertices.size() * sizeof(float));
|
|
m_ctx->memoryManager->unmapMemory(m_cylinderMesh.vboAlloc);
|
|
}
|
|
|
|
vk::BufferCreateInfo iboInfo;
|
|
iboInfo.size = mesh.indices.size() * sizeof(ushort);
|
|
iboInfo.usage = vk::BufferUsageFlagBits::eIndexBuffer;
|
|
iboInfo.sharingMode = vk::SharingMode::eExclusive;
|
|
|
|
try {
|
|
m_cylinderMesh.ibo = m_ctx->device.createBuffer(iboInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create cylinder IBO: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
|
|
if (!m_ctx->memoryManager->allocateBufferMemory(
|
|
m_cylinderMesh.ibo, MemoryUsage::CpuToGpu, m_cylinderMesh.iboAlloc, MemoryPurpose::Geometry)) {
|
|
m_ctx->device.destroyBuffer(m_cylinderMesh.ibo);
|
|
m_cylinderMesh.ibo = nullptr;
|
|
return false;
|
|
}
|
|
|
|
mapped = m_ctx->memoryManager->mapMemory(m_cylinderMesh.iboAlloc);
|
|
if (mapped) {
|
|
memcpy(mapped, mesh.indices.data(), mesh.indices.size() * sizeof(ushort));
|
|
m_ctx->memoryManager->unmapMemory(m_cylinderMesh.iboAlloc);
|
|
}
|
|
}
|
|
|
|
// Create deferred UBO for light data (host-visible). One full-size region per
|
|
// (frame-in-flight, lighting mode); see DEFERRED_UBO_REGIONS.
|
|
{
|
|
vk::BufferCreateInfo bufInfo;
|
|
bufInfo.size = static_cast<vk::DeviceSize>(DEFERRED_UBO_REGIONS) * DEFERRED_UBO_SIZE;
|
|
bufInfo.usage = vk::BufferUsageFlagBits::eUniformBuffer;
|
|
bufInfo.sharingMode = vk::SharingMode::eExclusive;
|
|
|
|
try {
|
|
m_deferredUBO = m_ctx->device.createBuffer(bufInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create deferred UBO: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
|
|
if (!m_ctx->memoryManager->allocateBufferMemory(m_deferredUBO, MemoryUsage::CpuToGpu, m_deferredUBOAlloc)) {
|
|
m_ctx->device.destroyBuffer(m_deferredUBO);
|
|
m_deferredUBO = nullptr;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
m_lightVolumesInitialized = true;
|
|
nprintf(("vulkan", "VulkanPostProcessor: Light volumes initialized (sphere: %u verts/%u idx, cylinder: %u verts/%u idx)\n",
|
|
m_sphereMesh.vertexCount, m_sphereMesh.indexCount,
|
|
m_cylinderMesh.vertexCount, m_cylinderMesh.indexCount));
|
|
return true;
|
|
}
|
|
|
|
void VulkanDeferredLighting::shutdown()
|
|
{
|
|
if (!m_ctx || !m_ctx->device) {
|
|
return;
|
|
}
|
|
|
|
auto destroyMesh = [&](LightVolumeMesh& mesh) {
|
|
if (mesh.vbo) { m_ctx->device.destroyBuffer(mesh.vbo); mesh.vbo = nullptr; }
|
|
if (mesh.vboAlloc.isValid()) { m_ctx->memoryManager->freeAllocation(mesh.vboAlloc); }
|
|
if (mesh.ibo) { m_ctx->device.destroyBuffer(mesh.ibo); mesh.ibo = nullptr; }
|
|
if (mesh.iboAlloc.isValid()) { m_ctx->memoryManager->freeAllocation(mesh.iboAlloc); }
|
|
mesh.vertexCount = 0;
|
|
mesh.indexCount = 0;
|
|
};
|
|
|
|
destroyMesh(m_sphereMesh);
|
|
destroyMesh(m_cylinderMesh);
|
|
|
|
if (m_deferredUBO) {
|
|
m_ctx->device.destroyBuffer(m_deferredUBO);
|
|
m_deferredUBO = nullptr;
|
|
}
|
|
if (m_deferredUBOAlloc.isValid()) {
|
|
m_ctx->memoryManager->freeAllocation(m_deferredUBOAlloc);
|
|
}
|
|
|
|
if (m_lightAccumFramebuffer) {
|
|
m_ctx->device.destroyFramebuffer(m_lightAccumFramebuffer);
|
|
m_lightAccumFramebuffer = nullptr;
|
|
}
|
|
if (m_lightAccumRenderPass) {
|
|
m_ctx->device.destroyRenderPass(m_lightAccumRenderPass);
|
|
m_lightAccumRenderPass = nullptr;
|
|
}
|
|
|
|
m_lightVolumesInitialized = false;
|
|
}
|
|
|
|
bool VulkanDeferredLighting::initLightAccumPass()
|
|
{
|
|
// Light accumulation render pass: single RGBA16F color attachment
|
|
// loadOp=eLoad (preserves emissive copy), storeOp=eStore
|
|
// initialLayout=eColorAttachmentOptimal, finalLayout=eShaderReadOnlyOptimal
|
|
{
|
|
vk::AttachmentDescription att;
|
|
att.format = HDR_COLOR_FORMAT;
|
|
att.samples = vk::SampleCountFlagBits::e1;
|
|
att.loadOp = vk::AttachmentLoadOp::eLoad;
|
|
att.storeOp = vk::AttachmentStoreOp::eStore;
|
|
att.stencilLoadOp = vk::AttachmentLoadOp::eDontCare;
|
|
att.stencilStoreOp = vk::AttachmentStoreOp::eDontCare;
|
|
att.initialLayout = vk::ImageLayout::eColorAttachmentOptimal;
|
|
att.finalLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
|
|
|
|
vk::AttachmentReference colorRef;
|
|
colorRef.attachment = 0;
|
|
colorRef.layout = vk::ImageLayout::eColorAttachmentOptimal;
|
|
|
|
vk::SubpassDescription subpass;
|
|
subpass.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
|
|
subpass.colorAttachmentCount = 1;
|
|
subpass.pColorAttachments = &colorRef;
|
|
|
|
vk::SubpassDependency dep;
|
|
dep.srcSubpass = VK_SUBPASS_EXTERNAL;
|
|
dep.dstSubpass = 0;
|
|
dep.srcStageMask = vk::PipelineStageFlagBits::eTransfer
|
|
| vk::PipelineStageFlagBits::eColorAttachmentOutput;
|
|
dep.dstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput
|
|
| vk::PipelineStageFlagBits::eFragmentShader;
|
|
dep.srcAccessMask = vk::AccessFlagBits::eTransferWrite
|
|
| vk::AccessFlagBits::eColorAttachmentWrite;
|
|
dep.dstAccessMask = vk::AccessFlagBits::eColorAttachmentRead
|
|
| vk::AccessFlagBits::eColorAttachmentWrite
|
|
| vk::AccessFlagBits::eShaderRead;
|
|
|
|
vk::RenderPassCreateInfo rpInfo;
|
|
rpInfo.attachmentCount = 1;
|
|
rpInfo.pAttachments = &att;
|
|
rpInfo.subpassCount = 1;
|
|
rpInfo.pSubpasses = &subpass;
|
|
rpInfo.dependencyCount = 1;
|
|
rpInfo.pDependencies = &dep;
|
|
|
|
try {
|
|
m_lightAccumRenderPass = m_ctx->device.createRenderPass(rpInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create light accum render pass: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Framebuffer using composite image as sole color attachment
|
|
return createLightAccumFramebuffer();
|
|
}
|
|
|
|
bool VulkanDeferredLighting::createLightAccumFramebuffer()
|
|
{
|
|
std::array<vk::ImageView, 1> attachments = { m_gbuffer->compositeView() };
|
|
|
|
vk::FramebufferCreateInfo fbInfo;
|
|
fbInfo.renderPass = m_lightAccumRenderPass;
|
|
fbInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
|
|
fbInfo.pAttachments = attachments.data();
|
|
fbInfo.width = m_ctx->sceneExtent.width;
|
|
fbInfo.height = m_ctx->sceneExtent.height;
|
|
fbInfo.layers = 1;
|
|
|
|
try {
|
|
m_lightAccumFramebuffer = m_ctx->device.createFramebuffer(fbInfo);
|
|
} catch (const vk::SystemError& e) {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Failed to create light accum framebuffer: %s\n", e.what()));
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool VulkanDeferredLighting::onResize()
|
|
{
|
|
if (!m_lightVolumesInitialized) {
|
|
// Lazy resources don't exist yet; first render picks up the new views.
|
|
return true;
|
|
}
|
|
|
|
// Only the framebuffer is extent/view-dependent: it attaches the G-buffer
|
|
// composite view, which the G-buffer resize just recreated. Meshes, the
|
|
// deferred UBO, and the render pass are unaffected.
|
|
if (m_lightAccumFramebuffer) {
|
|
m_ctx->device.destroyFramebuffer(m_lightAccumFramebuffer);
|
|
m_lightAccumFramebuffer = nullptr;
|
|
}
|
|
|
|
// If the G-buffer got disabled by a failed resize there is no composite
|
|
// view to attach anymore; disable deferred lighting too.
|
|
if (!m_gbuffer->isInitialized()) {
|
|
return false;
|
|
}
|
|
|
|
return createLightAccumFramebuffer();
|
|
}
|
|
|
|
namespace ltp = lighting_profiles;
|
|
|
|
static graphics::deferred_light_data* prepare_light_uniforms(light& l, uint8_t* dest, const ltp::profile* lp)
|
|
{
|
|
auto* light_data = reinterpret_cast<graphics::deferred_light_data*>(dest);
|
|
memset(light_data, 0, sizeof(graphics::deferred_light_data));
|
|
|
|
light_data->lightType = static_cast<int>(l.type);
|
|
|
|
float intensity =
|
|
(Lighting_mode == lighting_mode::COCKPIT) ? lp->cockpit_light_intensity_modifier.handle(l.intensity) : l.intensity;
|
|
|
|
vec3d diffuse;
|
|
diffuse.xyz.x = l.r * intensity;
|
|
diffuse.xyz.y = l.g * intensity;
|
|
diffuse.xyz.z = l.b * intensity;
|
|
|
|
light_data->diffuseLightColor = diffuse;
|
|
light_data->enable_shadows = 0;
|
|
light_data->sourceRadius = l.source_radius;
|
|
return light_data;
|
|
}
|
|
|
|
void VulkanDeferredLighting::render(vk::CommandBuffer cmd)
|
|
{
|
|
TRACE_SCOPE(tracing::ApplyLights);
|
|
|
|
if (!m_gbuffer->isInitialized()) {
|
|
return;
|
|
}
|
|
|
|
// Lazy-init light volumes and accumulation pass on first use
|
|
if (!m_lightVolumesInitialized) {
|
|
if (!initLightVolumes() || !initLightAccumPass()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
auto* pipelineMgr = getPipelineManager();
|
|
auto* descriptorMgr = getDescriptorManager();
|
|
auto* bufferMgr = getBufferManager();
|
|
auto* texMgr = getTextureManager();
|
|
|
|
if (!pipelineMgr || !descriptorMgr || !bufferMgr || !texMgr) {
|
|
return;
|
|
}
|
|
|
|
GR_DEBUG_SCOPE("Build buffer data");
|
|
|
|
// Sort lights by type (same stable sort as OpenGL)
|
|
std::stable_sort(Lights.begin(), Lights.end(), light_compare_by_type);
|
|
|
|
// Categorize lights
|
|
SCP_vector<light> full_frame_lights;
|
|
SCP_vector<light> sphere_lights;
|
|
SCP_vector<light> cylinder_lights;
|
|
for (auto& l : Lights) {
|
|
switch (l.type) {
|
|
case Light_Type::Directional:
|
|
full_frame_lights.push_back(l);
|
|
break;
|
|
case Light_Type::Cone:
|
|
case Light_Type::Point:
|
|
sphere_lights.push_back(l);
|
|
break;
|
|
case Light_Type::Tube:
|
|
cylinder_lights.push_back(l);
|
|
break;
|
|
case Light_Type::Ambient:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Add ambient light
|
|
{
|
|
light& l = full_frame_lights.emplace_back();
|
|
memset(&l, 0, sizeof(light));
|
|
vec3d ambient;
|
|
gr_get_ambient_light(&ambient);
|
|
l.r = ambient.xyz.x;
|
|
l.g = ambient.xyz.y;
|
|
l.b = ambient.xyz.z;
|
|
l.type = Light_Type::Ambient;
|
|
l.intensity = 1.f;
|
|
l.source_radius = 0.f;
|
|
}
|
|
|
|
size_t total_lights = full_frame_lights.size() + sphere_lights.size() + cylinder_lights.size();
|
|
if (total_lights == 0) {
|
|
return;
|
|
}
|
|
|
|
// Map UBO and pack data
|
|
auto* uboMapped = static_cast<uint8_t*>(m_ctx->memoryManager->mapMemory(m_deferredUBOAlloc));
|
|
if (!uboMapped) {
|
|
return;
|
|
}
|
|
|
|
// Determine alignment requirement
|
|
uint32_t uboAlign = getRendererInstance()->getMinUniformBufferOffsetAlignment();
|
|
auto alignUp = [uboAlign](uint32_t v) -> uint32_t {
|
|
return (v + uboAlign - 1) & ~(uboAlign - 1);
|
|
};
|
|
|
|
// Layout within this pass's region:
|
|
// [0]: deferred_global_data (header)
|
|
// [aligned offset 1..N]: deferred_light_data per light
|
|
// [aligned offset N+1..2N]: matrix_uniforms per light
|
|
uint32_t globalDataSize = alignUp(static_cast<uint32_t>(sizeof(graphics::deferred_global_data)));
|
|
uint32_t lightDataSize = alignUp(static_cast<uint32_t>(sizeof(graphics::deferred_light_data)));
|
|
uint32_t matrixDataSize = alignUp(static_cast<uint32_t>(sizeof(graphics::matrix_uniforms)));
|
|
|
|
uint32_t lightDataOffset = globalDataSize;
|
|
uint32_t matrixDataOffset = lightDataOffset + (static_cast<uint32_t>(total_lights) * lightDataSize);
|
|
uint32_t totalUBOSize = matrixDataOffset + (static_cast<uint32_t>(total_lights) * matrixDataSize);
|
|
|
|
if (totalUBOSize > DEFERRED_UBO_SIZE) {
|
|
// Fire a release-visible warning once so a user who hits this knows to
|
|
// report it and we know to bump DEFERRED_MAX_LIGHTS / DEFERRED_UBO_SIZE.
|
|
// Subsequent overflows this session only log quietly to avoid spam.
|
|
static bool warned_deferred_ubo_overflow = false;
|
|
if (!warned_deferred_ubo_overflow) {
|
|
warned_deferred_ubo_overflow = true;
|
|
ReleaseWarning(LOCATION,
|
|
"Deferred lighting UBO overflow: needed %u bytes but the buffer is only %u "
|
|
"(%d lights this frame). Some lights will not be rendered. Please report this so "
|
|
"the deferred light budget (DEFERRED_MAX_LIGHTS) can be increased.",
|
|
totalUBOSize, DEFERRED_UBO_SIZE, static_cast<int>(total_lights));
|
|
} else {
|
|
nprintf(("vulkan", "VulkanPostProcessor: Deferred UBO overflow (%u > %u), skipping lights\n", totalUBOSize, DEFERRED_UBO_SIZE));
|
|
}
|
|
m_ctx->memoryManager->unmapMemory(m_deferredUBOAlloc);
|
|
return;
|
|
}
|
|
|
|
// Nothing may pack over bytes another pass could still be reading. The scene and
|
|
// cockpit passes share one command buffer, so the second would otherwise clobber
|
|
// the first before either ran; and flip() waits only on the fence of the slot it
|
|
// reuses, so frame N+1 packs while frame N's light draws may still be executing.
|
|
const uint32_t regionIndex = (descriptorMgr->getCurrentFrame() * static_cast<uint32_t>(lighting_mode::MAX_LIGHTING_MODES))
|
|
+ static_cast<uint32_t>(Lighting_mode);
|
|
const uint32_t regionBase = regionIndex * DEFERRED_UBO_SIZE;
|
|
|
|
lightDataOffset += regionBase;
|
|
matrixDataOffset += regionBase;
|
|
|
|
// Pack global header
|
|
auto lp = ltp::current();
|
|
// Determine if environment maps are available
|
|
bool envMapAvailable = (ENVMAP > 0);
|
|
tcache_slot_vulkan* envMapSlot = nullptr;
|
|
tcache_slot_vulkan* irrMapSlot = nullptr;
|
|
if (envMapAvailable) {
|
|
texMgr->ensureCubemapUploaded(ENVMAP);
|
|
envMapSlot = texMgr->getTextureSlot(ENVMAP);
|
|
if (!envMapSlot || !envMapSlot->imageView || !envMapSlot->isCubemap) {
|
|
envMapAvailable = false;
|
|
}
|
|
}
|
|
if (envMapAvailable && IRRMAP > 0) {
|
|
irrMapSlot = texMgr->getTextureSlot(IRRMAP);
|
|
if (!irrMapSlot || !irrMapSlot->imageView || !irrMapSlot->isCubemap) {
|
|
irrMapSlot = nullptr; // Fall back to fallback cube for irrmap
|
|
}
|
|
}
|
|
|
|
{
|
|
auto* header = reinterpret_cast<graphics::deferred_global_data*>(uboMapped + regionBase);
|
|
memset(header, 0, sizeof(graphics::deferred_global_data));
|
|
header->invScreenWidth = 1.0f / gr_screen.max_w;
|
|
header->invScreenHeight = 1.0f / gr_screen.max_h;
|
|
header->nearPlane = gr_near_plane;
|
|
|
|
if (m_shadow->isInitialized() && Shadow_quality != ShadowQuality::Disabled) {
|
|
vm_inverse_matrix4(&header->inv_view_matrix, &Shadow_view_matrix_render);
|
|
shadow_cascade_params_bind_deferred();
|
|
}
|
|
}
|
|
|
|
// Pack per-light data
|
|
size_t lightIdx = 0;
|
|
int shadowedDirectionalCount = 0;
|
|
// Cascade shadow maps are only ever built for a single light (Static_light.front()),
|
|
// so only raytraced shadows can shadow more than one directional light here -- a second
|
|
// light reading the first light's cascade data would produce incorrect shadows.
|
|
int maxShadowedDirectionalLights = shadows_use_raytracing() ? Max_rt_shadow_lights : 1;
|
|
|
|
for (auto& l : full_frame_lights) {
|
|
auto* ld = prepare_light_uniforms(l, uboMapped + lightDataOffset + (lightIdx * lightDataSize), lp);
|
|
|
|
if (l.type == Light_Type::Directional) {
|
|
if (m_shadow->isInitialized() && Shadow_quality != ShadowQuality::Disabled
|
|
&& shadowedDirectionalCount < maxShadowedDirectionalLights) {
|
|
ld->enable_shadows = 1;
|
|
++shadowedDirectionalCount;
|
|
} else {
|
|
ld->enable_shadows = 0;
|
|
}
|
|
|
|
vec4 light_dir;
|
|
light_dir.xyzw.x = -l.vec.xyz.x;
|
|
light_dir.xyzw.y = -l.vec.xyz.y;
|
|
light_dir.xyzw.z = -l.vec.xyz.z;
|
|
light_dir.xyzw.w = 0.0f;
|
|
vec4 view_dir;
|
|
vm_vec_transform(&view_dir, &light_dir, &gr_view_matrix);
|
|
ld->lightDir.xyz.x = view_dir.xyzw.x;
|
|
ld->lightDir.xyz.y = view_dir.xyzw.y;
|
|
ld->lightDir.xyz.z = view_dir.xyzw.z;
|
|
}
|
|
|
|
// Matrix: env texture matrix for full-frame lights
|
|
auto* md = reinterpret_cast<graphics::matrix_uniforms*>(uboMapped + matrixDataOffset + (lightIdx * matrixDataSize));
|
|
memset(md, 0, sizeof(graphics::matrix_uniforms));
|
|
md->modelViewMatrix = gr_env_texture_matrix;
|
|
++lightIdx;
|
|
}
|
|
|
|
// Decide which local (point/tube/cone) lights get raytraced shadows this frame. This
|
|
// cap is independent of maxShadowedDirectionalLights above -- directional and local
|
|
// lights have very different per-mission counts, so one slider can't sensibly tune
|
|
// both. Priority is nearest-to-camera first, ranked across both light-volume groups
|
|
// together (sphere_lights covers point/cone, cylinder_lights covers tube) so the cap
|
|
// isn't biased toward whichever group happens to be packed first.
|
|
SCP_vector<bool> sphereLightShadowed(sphere_lights.size(), false);
|
|
SCP_vector<bool> cylinderLightShadowed(cylinder_lights.size(), false);
|
|
if (shadows_use_raytraced_local_lights() && m_shadow->isInitialized() && Shadow_quality != ShadowQuality::Disabled) {
|
|
struct LocalLightCandidate {
|
|
float dist_sq;
|
|
bool is_cylinder;
|
|
size_t index;
|
|
};
|
|
SCP_vector<LocalLightCandidate> candidates;
|
|
candidates.reserve(sphere_lights.size() + cylinder_lights.size());
|
|
|
|
auto distSqToEye = [](const vec3d& pos) {
|
|
vec3d diff;
|
|
vm_vec_sub(&diff, &pos, &Eye_position);
|
|
return vm_vec_mag_squared(&diff);
|
|
};
|
|
|
|
for (size_t i = 0; i < sphere_lights.size(); ++i) {
|
|
// e.g. WRT_LASER weapon bolt lights -- these fire far too often to be worth
|
|
// raytracing a shadow for, and excluding them here is a large, cheap cut to
|
|
// the number of lights this selection has to consider every frame.
|
|
if (sphere_lights[i].flags & LF_NO_RT_SHADOW) {
|
|
continue;
|
|
}
|
|
candidates.push_back({distSqToEye(sphere_lights[i].vec), false, i});
|
|
}
|
|
for (size_t i = 0; i < cylinder_lights.size(); ++i) {
|
|
if (cylinder_lights[i].flags & LF_NO_RT_SHADOW) {
|
|
continue;
|
|
}
|
|
candidates.push_back({distSqToEye(cylinder_lights[i].vec), true, i});
|
|
}
|
|
|
|
std::sort(candidates.begin(), candidates.end(), [](const LocalLightCandidate& a, const LocalLightCandidate& b) {
|
|
return a.dist_sq < b.dist_sq;
|
|
});
|
|
|
|
size_t shadowCount = std::min(candidates.size(), static_cast<size_t>(std::max(Max_rt_shadow_local_lights, 0)));
|
|
for (size_t i = 0; i < shadowCount; ++i) {
|
|
if (candidates[i].is_cylinder) {
|
|
cylinderLightShadowed[candidates[i].index] = true;
|
|
} else {
|
|
sphereLightShadowed[candidates[i].index] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (size_t sphereIdx = 0; sphereIdx < sphere_lights.size(); ++sphereIdx) {
|
|
light& l = sphere_lights[sphereIdx];
|
|
auto* ld = prepare_light_uniforms(l, uboMapped + lightDataOffset + (lightIdx * lightDataSize), lp);
|
|
|
|
if (l.type == Light_Type::Cone) {
|
|
ld->dualCone = (l.flags & LF_DUAL_CONE) ? 1 : 0;
|
|
ld->coneAngle = l.cone_angle;
|
|
ld->coneInnerAngle = l.cone_inner_angle;
|
|
ld->coneDir = l.vec2;
|
|
}
|
|
ld->enable_shadows = sphereLightShadowed[sphereIdx] ? 1 : 0;
|
|
float rad = (Lighting_mode == lighting_mode::COCKPIT)
|
|
? lp->cockpit_light_radius_modifier.handle(MAX(l.rada, l.radb))
|
|
: MAX(l.rada, l.radb);
|
|
ld->lightRadius = rad;
|
|
ld->scale.xyz.x = rad * 1.05f;
|
|
ld->scale.xyz.y = rad * 1.05f;
|
|
ld->scale.xyz.z = rad * 1.05f;
|
|
|
|
// Matrix: model-view + projection for light volume
|
|
auto* md = reinterpret_cast<graphics::matrix_uniforms*>(uboMapped + matrixDataOffset + (lightIdx * matrixDataSize));
|
|
g3_start_instance_matrix(&l.vec, &vmd_identity_matrix, true);
|
|
md->modelViewMatrix = gr_model_view_matrix;
|
|
md->projMatrix = gr_projection_matrix;
|
|
g3_done_instance(true);
|
|
++lightIdx;
|
|
}
|
|
|
|
for (size_t cylinderIdx = 0; cylinderIdx < cylinder_lights.size(); ++cylinderIdx) {
|
|
light& l = cylinder_lights[cylinderIdx];
|
|
auto* ld = prepare_light_uniforms(l, uboMapped + lightDataOffset + (lightIdx * lightDataSize), lp);
|
|
float rad =
|
|
(Lighting_mode == lighting_mode::COCKPIT) ? lp->cockpit_light_radius_modifier.handle(l.radb) : l.radb;
|
|
ld->lightRadius = rad;
|
|
ld->lightType = LT_TUBE;
|
|
ld->enable_shadows = cylinderLightShadowed[cylinderIdx] ? 1 : 0;
|
|
|
|
vec3d a;
|
|
vm_vec_sub(&a, &l.vec, &l.vec2);
|
|
auto length = vm_vec_mag(&a);
|
|
length += ld->lightRadius * 2.0f;
|
|
|
|
ld->scale.xyz.x = rad * 1.05f;
|
|
ld->scale.xyz.y = rad * 1.05f;
|
|
ld->scale.xyz.z = length;
|
|
|
|
// Matrix: oriented instance matrix for cylinder
|
|
auto* md = reinterpret_cast<graphics::matrix_uniforms*>(uboMapped + matrixDataOffset + (lightIdx * matrixDataSize));
|
|
vec3d dir, newPos;
|
|
matrix orient;
|
|
vm_vec_normalized_dir(&dir, &l.vec, &l.vec2);
|
|
vm_vector_2_matrix_norm(&orient, &dir, nullptr, nullptr);
|
|
vm_vec_scale_sub(&newPos, &l.vec2, &dir, l.radb);
|
|
|
|
g3_start_instance_matrix(&newPos, &orient, true);
|
|
md->modelViewMatrix = gr_model_view_matrix;
|
|
md->projMatrix = gr_projection_matrix;
|
|
g3_done_instance(true);
|
|
++lightIdx;
|
|
}
|
|
|
|
m_ctx->memoryManager->flushMemory(m_deferredUBOAlloc, regionBase, totalUBOSize);
|
|
m_ctx->memoryManager->unmapMemory(m_deferredUBOAlloc);
|
|
|
|
GR_DEBUG_SCOPE("Render light geometry");
|
|
|
|
// Both fullscreen and volume lights use the same vertex layout (POSITION3).
|
|
// For fullscreen lights the shader ignores vertex data and generates positions
|
|
// from gl_VertexIndex, but Vulkan requires all declared vertex inputs to have
|
|
// matching pipeline attributes and bound buffers.
|
|
vertex_layout volLayout;
|
|
volLayout.add_vertex_component(vertex_format_data::POSITION3, sizeof(float) * 3, 0);
|
|
|
|
bool rtShadowsActive = shadows_use_raytracing() && m_shadow->isInitialized() && Shadow_quality != ShadowQuality::Disabled;
|
|
|
|
unsigned int lightShaderFlags = 0;
|
|
if (rtShadowsActive) {
|
|
lightShaderFlags |= SDR_FLAG_DEFERRED_RT_SHADOWS;
|
|
}
|
|
if (envMapAvailable) {
|
|
lightShaderFlags |= SDR_FLAG_ENV_MAP;
|
|
}
|
|
|
|
PipelineConfig lightConfig;
|
|
lightConfig.shaderType = SDR_TYPE_DEFERRED_LIGHTING;
|
|
lightConfig.shaderFlags = lightShaderFlags;
|
|
lightConfig.vertexLayoutHash = volLayout.hash();
|
|
lightConfig.primitiveType = PRIM_TYPE_TRIS;
|
|
lightConfig.depthMode = ZBUFFER_TYPE_NONE;
|
|
lightConfig.blendMode = ALPHA_BLEND_ADDITIVE;
|
|
lightConfig.cullEnabled = false;
|
|
lightConfig.depthWriteEnabled = false;
|
|
lightConfig.renderPass = m_lightAccumRenderPass;
|
|
|
|
vk::Pipeline lightPipeline = pipelineMgr->getPipeline(lightConfig, volLayout);
|
|
if (!lightPipeline) {
|
|
return;
|
|
}
|
|
|
|
vk::PipelineLayout pipelineLayout = pipelineMgr->getPipelineLayout();
|
|
|
|
// Begin light accumulation render pass
|
|
{
|
|
vk::RenderPassBeginInfo rpBegin;
|
|
rpBegin.renderPass = m_lightAccumRenderPass;
|
|
rpBegin.framebuffer = m_lightAccumFramebuffer;
|
|
rpBegin.renderArea.offset = vk::Offset2D(0, 0);
|
|
rpBegin.renderArea.extent = m_ctx->sceneExtent;
|
|
|
|
cmd.beginRenderPass(rpBegin, vk::SubpassContents::eInline);
|
|
}
|
|
|
|
// Set viewport and scissor.
|
|
// Negative-height viewport (Y-flip) to match the G-buffer render pass: the sphere/cylinder
|
|
// light volumes are transformed with the same projMatrix/modelViewMatrix convention used
|
|
// for regular scene geometry, which assumes this flip. Without it, point/tube light volumes
|
|
// rasterize mirrored vertically relative to the G-buffer they're sampling, misaligning them
|
|
// with the actual light position (full-frame directional/ambient lights are unaffected since
|
|
// their fullscreen triangle covers every pixel regardless of orientation).
|
|
vk::Viewport viewport;
|
|
viewport.x = 0.0f;
|
|
viewport.y = static_cast<float>(m_ctx->sceneExtent.height);
|
|
viewport.width = static_cast<float>(m_ctx->sceneExtent.width);
|
|
viewport.height = -static_cast<float>(m_ctx->sceneExtent.height);
|
|
viewport.minDepth = 0.0f;
|
|
viewport.maxDepth = 1.0f;
|
|
cmd.setViewport(0, viewport);
|
|
|
|
vk::Rect2D scissor;
|
|
scissor.offset = vk::Offset2D(0, 0);
|
|
scissor.extent = m_ctx->sceneExtent;
|
|
cmd.setScissor(0, scissor);
|
|
|
|
// Pre-build G-buffer texture array (shared across all light draws)
|
|
const auto& fallbacks = descriptorMgr->getFallbacks();
|
|
std::array<vk::DescriptorImageInfo, VulkanDescriptorManager::MAX_TEXTURE_BINDINGS> gbufTexArray;
|
|
gbufTexArray.fill(fallbacks.texture2D);
|
|
gbufTexArray[0] = {m_ctx->linearSampler, m_sceneColor->view, vk::ImageLayout::eShaderReadOnlyOptimal};
|
|
gbufTexArray[1] = {m_ctx->linearSampler, m_gbuffer->normalView(), vk::ImageLayout::eShaderReadOnlyOptimal};
|
|
gbufTexArray[2] = {m_ctx->linearSampler, m_gbuffer->positionView(), vk::ImageLayout::eShaderReadOnlyOptimal};
|
|
gbufTexArray[3] = {m_ctx->linearSampler, m_gbuffer->specularView(), vk::ImageLayout::eShaderReadOnlyOptimal};
|
|
|
|
// Pre-build shadow/env/irr image infos (shared across all light draws).
|
|
// Shadow map is depth-only, sampled with a depth-compare sampler
|
|
// (sampler2DArrayShadow) for hardware PCF.
|
|
vk::DescriptorImageInfo shadowTexInfo;
|
|
if (m_shadow->isInitialized() && m_shadow->depthView()) {
|
|
shadowTexInfo = {m_shadow->compareSampler(), m_shadow->depthView(), vk::ImageLayout::eShaderReadOnlyOptimal};
|
|
}
|
|
|
|
// Shadow cascade params (projection matrices/distances) are bound per-frame via
|
|
// shadow_cascade_params_bind() -> gr_bind_uniform_buffer(), landing in the draw
|
|
// manager's pending uniform binding table; pick it up the same way applyMaterial()
|
|
// does for other pending UBOs.
|
|
vk::DescriptorBufferInfo shadowCascadeParamsInfo;
|
|
{
|
|
auto* drawManager = getDrawManager();
|
|
const auto& pending = drawManager->getPendingUniformBinding(static_cast<size_t>(uniform_block_type::ShadowCascadeParams));
|
|
if (pending.valid) {
|
|
vk::Buffer buf = bufferMgr->getVkBuffer(pending.bufferHandle);
|
|
if (buf) {
|
|
shadowCascadeParamsInfo = vk::DescriptorBufferInfo(buf, pending.offset, pending.size);
|
|
}
|
|
}
|
|
}
|
|
vk::DescriptorImageInfo envTexInfo;
|
|
if (envMapAvailable && envMapSlot) {
|
|
envTexInfo = fallbacks.textureCube;
|
|
envTexInfo.imageView = envMapSlot->imageView;
|
|
}
|
|
vk::DescriptorImageInfo irrTexInfo;
|
|
if (envMapAvailable && irrMapSlot) {
|
|
irrTexInfo = fallbacks.textureCube;
|
|
irrTexInfo.imageView = irrMapSlot->imageView;
|
|
}
|
|
|
|
// Helper lambda to allocate + write descriptor sets for a single light draw
|
|
auto bindLightDescriptors = [&](size_t li) {
|
|
DescriptorWriter writer;
|
|
writer.reset(m_ctx->device, fallbacks);
|
|
|
|
// Set 0: Global
|
|
vk::DescriptorSet globalSet = descriptorMgr->allocateFrameSet(DescriptorSetIndex::Global);
|
|
if (!globalSet) return false;
|
|
writer.writeSet(DescriptorSetIndex::Global, globalSet);
|
|
writer.setBuffer(GlobalBinding::Lights, {m_deferredUBO,
|
|
lightDataOffset + (li * lightDataSize), sizeof(graphics::deferred_light_data)});
|
|
writer.setBuffer(GlobalBinding::DeferredData, {m_deferredUBO,
|
|
regionBase, sizeof(graphics::deferred_global_data)});
|
|
writer.setImage(GlobalBinding::ShadowMap, shadowTexInfo);
|
|
writer.setImage(GlobalBinding::EnvMap, envTexInfo);
|
|
writer.setImage(GlobalBinding::IrradianceMap, irrTexInfo);
|
|
writer.setBuffer(GlobalBinding::ShadowCascadeParams, shadowCascadeParamsInfo);
|
|
|
|
// Set 1: Material
|
|
vk::DescriptorSet materialSet = descriptorMgr->allocateFrameSet(DescriptorSetIndex::Material);
|
|
if (!materialSet) return false;
|
|
writer.writeSet(DescriptorSetIndex::Material, materialSet);
|
|
writer.setImageArray(MaterialBinding::TextureArray, gbufTexArray);
|
|
|
|
// Set 2: PerDraw
|
|
vk::DescriptorSet perDrawSet = descriptorMgr->allocateFrameSet(DescriptorSetIndex::PerDraw);
|
|
if (!perDrawSet) return false;
|
|
writer.writeSet(DescriptorSetIndex::PerDraw, perDrawSet);
|
|
writer.setBuffer(PerDrawBinding::Matrices, {m_deferredUBO,
|
|
matrixDataOffset + (li * matrixDataSize), sizeof(graphics::matrix_uniforms)});
|
|
writer.flush();
|
|
|
|
const vk::DescriptorSet sets[] = {globalSet, materialSet, perDrawSet};
|
|
writer.bindSets(cmd, pipelineLayout, DescriptorSetIndex::Global, sets);
|
|
|
|
return true;
|
|
};
|
|
|
|
cmd.bindPipeline(vk::PipelineBindPoint::eGraphics, lightPipeline);
|
|
|
|
// Draw full-frame lights (directional + ambient)
|
|
// Bind sphere VBO as dummy — shader ignores vertex data for these light types.
|
|
lightIdx = 0;
|
|
if (!full_frame_lights.empty()) {
|
|
cmd.bindVertexBuffers(0, m_sphereMesh.vbo, vk::DeviceSize(0));
|
|
for (size_t i = 0; i < full_frame_lights.size(); ++i) {
|
|
GR_DEBUG_SCOPE("Deferred apply single dir light");
|
|
|
|
if (bindLightDescriptors(lightIdx)) {
|
|
cmd.draw(3, 1, 0, 0);
|
|
}
|
|
++lightIdx;
|
|
}
|
|
}
|
|
|
|
// Draw sphere lights (point + cone)
|
|
if (!sphere_lights.empty()) {
|
|
cmd.bindVertexBuffers(0, m_sphereMesh.vbo, vk::DeviceSize(0));
|
|
cmd.bindIndexBuffer(m_sphereMesh.ibo, 0, vk::IndexType::eUint16);
|
|
for (size_t i = 0; i < sphere_lights.size(); ++i) {
|
|
if (bindLightDescriptors(lightIdx)) {
|
|
cmd.drawIndexed(m_sphereMesh.indexCount, 1, 0, 0, 0);
|
|
}
|
|
++lightIdx;
|
|
}
|
|
}
|
|
|
|
// Draw cylinder lights (tube)
|
|
if (!cylinder_lights.empty()) {
|
|
cmd.bindVertexBuffers(0, m_cylinderMesh.vbo, vk::DeviceSize(0));
|
|
cmd.bindIndexBuffer(m_cylinderMesh.ibo, 0, vk::IndexType::eUint16);
|
|
for (size_t i = 0; i < cylinder_lights.size(); ++i) {
|
|
if (bindLightDescriptors(lightIdx)) {
|
|
cmd.drawIndexed(m_cylinderMesh.indexCount, 1, 0, 0, 0);
|
|
}
|
|
++lightIdx;
|
|
}
|
|
}
|
|
|
|
// End render pass (composite → eShaderReadOnlyOptimal)
|
|
cmd.endRenderPass();
|
|
}
|
|
|
|
} // namespace graphics::vulkan
|