16void Graphics::resetDeferredFrameGraphs() {
19 if (device.instance) device->waitIdle();
20 for (
auto &
slot : deferredFrameGraphs_)
22 deferredRecordingState_ = {};
23 deferredGraphRecorded_ =
false;
24 deferredGraphRecordedSlot_ = 0;
27vkb::FrameGraph *Graphics::currentDeferredFrameGraph() {
28 const size_t variant = shadowPendingMask != 0 ? 1u : 0
u;
29 return deferredFrameGraphs_[currentFrameSlot() % deferredFrameGraphs_.size()][
variant].get();
32void Graphics::buildDeferredFrameGraphs() {
43 const vk::Format depthFmt = vk::Format::eD32Sfloat;
44 const vk::Format colorFmt = pickGBufferColorFormat(device);
45 const uint32_t mapSize = uint32_t(
46 allocatedShadowMapSize > 0 ? allocatedShadowMapSize : ShadowSchemeSettings::
current().resolvedAtlasMapSize());
48 const uint32_t
w = gbufferWidth > 0 ? uint32_t(gbufferWidth) : 1u;
49 const uint32_t
h = gbufferHeight > 0 ? uint32_t(gbufferHeight) : 1u;
50 const size_t variant = shadowPendingMask != 0 ? 1u : 0
u;
52 for (
size_t i = 0; i < deferredFrameGraphs_.size(); ++i) {
53 auto graph = std::make_unique<vkb::FrameGraph>(&device, 1);
55 vkb::TextureDesc shadowDesc;
56 shadowDesc.format = depthFmt;
57 shadowDesc.extent = vk::Extent3D{mapSize, mapSize, 1};
58 shadowDesc.arrayLayers = shadowLayers;
59 shadowDesc.aspect = vk::ImageAspectFlagBits::eDepth;
60 shadowDesc.usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eDepthStencilAttachment;
63 shadowDesc.afterLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
64 vk::ClearValue shadowClear{};
65 shadowClear.depthStencil = vk::ClearDepthStencilValue{1.0f, 0};
66 const bool haveShadowSlot = i < shadowMaps.size() && shadowMaps[i].image.layerCount() >= shadowLayers;
67 if (haveShadowSlot &&
variant != 0) {
68 const vk::Image shadowImage = shadowMaps[i].image.image();
69 for (uint32_t
c = 0;
c < shadowLayers; ++
c) {
70 auto shadowH =
graph->importTexture(
"shadowCascade" + std::to_string(
c), shadowImage,
71 shadowMaps[i].
image.layerView(
c), shadowDesc);
72 graph->addPass(
"shadow" + std::to_string(
c))
73 .depthAttachment(shadowH, vkb::AttachmentOp::clear(shadowClear))
74 .record([
this,
c](vkb::FrameGraphPassContext &ctx) { recordShadowCascadePass(ctx,
int(
c)); });
78 if (i < gbufferSlots.size() && gbufferWidth > 0 && gbufferHeight > 0) {
79 auto &
slot = gbufferSlots[i];
80 vkb::TextureDesc colorDesc;
81 colorDesc.format = colorFmt;
82 colorDesc.extent = vk::Extent3D{
w,
h, 1};
83 colorDesc.usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eColorAttachment |
84 vk::ImageUsageFlagBits::eTransferSrc;
85 colorDesc.afterLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
86 auto normalH =
graph->importTexture(
"gbNormal",
slot.normal.image(),
slot.normal.imageView(), colorDesc);
88 graph->importTexture(
"gbDepthColor",
slot.depthColor.image(),
slot.depthColor.imageView(), colorDesc);
89 auto albedoH =
graph->importTexture(
"gbAlbedo",
slot.albedo.image(),
slot.albedo.imageView(), colorDesc);
91 graph->importTexture(
"gbPbrParams",
slot.pbrParams.image(),
slot.pbrParams.imageView(), colorDesc);
92 colorDesc.format = vk::Format::eR16G16B16A16Sfloat;
94 graph->importTexture(
"gbEmissive",
slot.emissive.image(),
slot.emissive.imageView(), colorDesc);
96 vkb::TextureDesc depthDesc;
97 depthDesc.format = depthFmt;
98 depthDesc.extent = vk::Extent3D{
w,
h, 1};
99 depthDesc.aspect = vk::ImageAspectFlagBits::eDepth;
100 depthDesc.usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eDepthStencilAttachment;
101 depthDesc.afterLayout = vk::ImageLayout::eShaderReadOnlyOptimal;
102 auto depthH =
graph->importTexture(
"gbHwDepth",
slot.depth.image(),
slot.depth.imageView(), depthDesc);
104 std::array<vk::ClearValue, 6> clears{};
105 clears[0].color = vk::ClearColorValue(std::array<float, 4>{0, 0, 0, 0});
106 clears[1].color = vk::ClearColorValue(std::array<float, 4>{1, 1, 1, 1});
107 clears[2].color = vk::ClearColorValue(std::array<float, 4>{0, 0, 0, 0});
108 clears[3].color = vk::ClearColorValue(std::array<float, 4>{0, 1, 1, 1});
109 clears[4].color = vk::ClearColorValue(std::array<float, 4>{0, 0, 0, 0});
110 clears[5].depthStencil = vk::ClearDepthStencilValue{1.0f, 0};
111 graph->addPass(
"gbuffer")
112 .colorAttachment(normalH, vkb::AttachmentOp::clear(clears[0]))
113 .colorAttachment(depthColorH, vkb::AttachmentOp::clear(clears[1]))
114 .colorAttachment(albedoH, vkb::AttachmentOp::clear(clears[2]))
115 .colorAttachment(pbrParamsH, vkb::AttachmentOp::clear(clears[3]))
116 .colorAttachment(emissiveH, vkb::AttachmentOp::clear(clears[4]))
117 .depthAttachment(depthH, vkb::AttachmentOp::clear(clears[5]))
118 .record([
this](vkb::FrameGraphPassContext& ctx) { recordGBufferPassDraws(ctx); });
125void Graphics::recordShadowCascadePass(vkb::FrameGraphPassContext &ctx,
int cascade) {
131 auto &cb = ctx.commandBuffer();
134 uint32_t
size = uint32_t(allocatedShadowMapSize > 0 ? allocatedShadowMapSize
135 : ShadowSchemeSettings::
current().resolvedAtlasMapSize());
139 recordShadowCascadeDraws(cb, cascade,
size);
142void Graphics::recordShadowCascadeDraws(vk::CommandBuffer cb,
int cascade, uint32_t
size) {
143 setViewportAndScissor(cb,
size,
size);
144 vk::Pipeline boundPipeline{};
145 for (
const auto &
d : shadowCascadeDraws[cascade]) {
146 if (!
d.mesh || !
d.mesh->gpuHandle)
continue;
147 const bool wantAlpha =
d.alphaTest && shadowAlphaPipeline;
148 const bool skinned =
d.skinSet &&
d.mesh->hasGpuSkinning();
149 vk::Pipeline wanted{};
151 wanted = wantAlpha ? (
d.doubleSided ? shadowSkinAlphaPipeline : shadowSkinAlphaSingleSidedPipeline)
152 : (
d.
doubleSided ? shadowSkinPipeline : shadowSkinSingleSidedPipeline);
154 wanted = wantAlpha ? (
d.doubleSided ? shadowAlphaPipeline : shadowAlphaSingleSidedPipeline)
155 : (
d.
doubleSided ? shadowPipeline : shadowSingleSidedPipeline);
157 if (wanted != boundPipeline) {
158 cb.bindPipeline(vk::PipelineBindPoint::eGraphics, wanted);
159 boundPipeline = wanted;
161 auto *gpuMesh =
static_cast<GpuMesh *
>(
d.mesh->gpuHandle);
163 cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, skinPassPipelineLayout, 0, 1, &
d.skinSet, 1,
165 }
else if (wantAlpha) {
166 Texture *alb =
d.albedo ?
d.albedo : whiteTexture;
167 if (alb && alb->gpuHandle && texSetLayout) {
168 auto *gpuTex =
static_cast<GpuTexture *
>(alb->gpuHandle);
169 cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, shadowAlphaPipelineLayout, 0, 1,
170 gpuTex->descriptorSet.ptr(), 0,
nullptr);
175 ShadowAlphaPush
push{
d.mvp,
d.lodFade};
176 cb.pushConstants(shadowAlphaPipelineLayout,
177 vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
180 cb.pushConstants(shadowPipelineLayout, vk::ShaderStageFlagBits::eVertex,
181 0,
sizeof(glm::mat4), &
d.mvp);
184 drawIndexedMesh(cb, *gpuMesh);
188void Graphics::recordGBufferPassDraws(vkb::FrameGraphPassContext &ctx) {
194 auto &cb = ctx.commandBuffer();
195 const vk::Extent2D extent = ctx.extent();
196 const uint32_t
w = extent.width ? extent.width : uint32_t(gbufferWidth);
197 const uint32_t
h = extent.height ? extent.height : uint32_t(gbufferHeight);
198 setViewportAndScissor(cb,
w,
h);
199 vk::Pipeline boundPipeline{};
200 size_t batchIndex = 0;
201 for (
size_t drawIndex = 0; drawIndex < gbufferPassDraws.size();) {
202 const auto&
d = gbufferPassDraws[drawIndex];
203 if (batchIndex < gbufferInstancedBatches.size() && gbufferInstancedBatches[batchIndex].first == drawIndex) {
204 const auto& batch = gbufferInstancedBatches[batchIndex++];
205 auto* gpuMesh =
static_cast<GpuMesh*
>(
d.mesh->gpuHandle);
206 if (gbufferInstancedPipeline != boundPipeline) {
207 cb.bindPipeline(vk::PipelineBindPoint::eGraphics, gbufferInstancedPipeline);
208 boundPipeline = gbufferInstancedPipeline;
210 Texture* alb =
d.albedo ?
d.albedo : whiteTexture;
211 auto* gpuTex =
static_cast<GpuTexture*
>(alb->gpuHandle);
212 cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, gbufferPipelineLayout, 0, 1,
213 gpuTex->descriptorSet.ptr(), 0,
nullptr);
214 cb.bindVertexBuffers(1, 1, &batch.buffer, &batch.offset);
215 drawIndexedMesh(cb, *gpuMesh, batch.count);
216 drawIndex += batch.count;
220 if (!
d.mesh || !
d.mesh->gpuHandle)
continue;
222 cb.bindPipeline(vk::PipelineBindPoint::eGraphics,
d.pbrPipeline);
223 boundPipeline =
d.pbrPipeline;
224 cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
d.pbrLayout, 0, 1, &
d.pbrSet, 0,
nullptr);
225 std::array<vk::Buffer, 11> uvBuffers;
226 uvBuffers.fill(
d.pbrUv);
227 cb.bindVertexBuffers(1, uvBuffers,
d.pbrUvOffsets);
228 drawIndexedMesh(cb, *
static_cast<GpuMesh *
>(
d.mesh->gpuHandle));
231 const bool wantAlpha =
d.alphaTest && gbufferAlphaPipeline;
232 const bool skinned =
d.skinSet &&
d.mesh->hasGpuSkinning();
233 vk::Pipeline wanted = skinned ? (wantAlpha ? gbufferSkinAlphaPipeline : gbufferSkinPipeline)
234 : (wantAlpha ? gbufferAlphaPipeline : gbufferPipeline);
235 if (wanted != boundPipeline) {
236 cb.bindPipeline(vk::PipelineBindPoint::eGraphics, wanted);
237 boundPipeline = wanted;
239 auto *gpuMesh =
static_cast<GpuMesh *
>(
d.mesh->gpuHandle);
240 Texture *alb =
d.albedo ?
d.albedo : whiteTexture;
242 cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, skinPassPipelineLayout, 0, 1, &
d.skinSet, 1,
244 }
else if (alb && alb->gpuHandle && texSetLayout) {
245 auto *gpuTex =
static_cast<GpuTexture *
>(alb->gpuHandle);
246 cb.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, gbufferPipelineLayout, 0, 1,
247 gpuTex->descriptorSet.ptr(), 0,
nullptr);
250 cb.pushConstants(gbufferPipelineLayout,
251 vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment, 0,
252 sizeof(GBufferPush), &
d.push);
253 drawIndexedMesh(cb, *gpuMesh);
257void Graphics::prepareGBufferInstanceBatches() {
259 gbufferInstancedBatches.clear();
260 if (!gbufferPending || !gbufferInstancedPipeline || gbufferSlots.empty())
return;
262 for (
size_t i = 0; i < gbufferPassDraws.size();) {
263 const auto&
first = gbufferPassDraws[i];
270 while (
end < gbufferPassDraws.size()) {
271 const auto&
next = gbufferPassDraws[
end];
277 if (
end - i >= 128) {
278 gbufferInstancedBatches.push_back({i, uint32_t(
end - i), {}, vk::DeviceSize(total *
sizeof(GBufferPush))});
283 if (total == 0)
return;
284 auto&
slot = *currentGBufferSlot();
285 if (
slot.instanceCapacity < total) {
286 const size_t capacity = std::max(total,
slot.instanceCapacity * 2);
287 slot.instanceBuffer =
288 vkb::GenericBuffer(device, vk::BufferUsageFlagBits::eVertexBuffer,
capacity *
sizeof(GBufferPush),
289 vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
292 auto* mapped =
static_cast<GBufferPush*
>(
slot.instanceBuffer.map());
294 for (
auto& batch : gbufferInstancedBatches) {
295 batch.buffer =
slot.instanceBuffer.buffer;
296 batch.offset = vk::DeviceSize(
cursor *
sizeof(GBufferPush));
297 for (uint32_t j = 0; j < batch.count; ++j) mapped[
cursor++] = gbufferPassDraws[batch.first + j].push;
299 slot.instanceBuffer.unmap();
302void Graphics::recordDeferredFrameGraph() {
307 if (!gbufferPending && shadowPendingMask == 0) {
308 dropPendingOffscreenPasses();
311 const size_t slot = currentFrameSlot();
312 if (deferredGraphRecorded_ && deferredGraphRecordedSlot_ ==
slot) {
320 auto *
graph = currentDeferredFrameGraph();
321 if (!
graph && (!shadowMaps.empty() || !gbufferSlots.empty())) {
324 buildDeferredFrameGraphs();
325 graph = currentDeferredFrameGraph();
327 if (!
graph || !gbufferPipeline || !gbufferRenderPass || !shadowPipeline) {
328 dropPendingOffscreenPasses();
335 auto *
targets = currentGBufferSlot();
340 prepareGBufferInstanceBatches();
347 auto *jobs = thread::Thread::create()->getJobSystem();
367 submitGpuTimingPrefix();
372 if (!shadowMaps.empty()) currentShadowMap().image.setCurrentLayout(vk::ImageLayout::eShaderReadOnlyOptimal);
374 for (
auto &
d : shadowCascadeDraws)
d.
clear();
375 gbufferPassDraws.clear();
376 gbufferInstancedBatches.clear();
377 shadowPendingMask = 0;
378 gbufferPending =
false;
379 deferredGraphRecorded_ =
true;
380 deferredGraphRecordedSlot_ =
slot;
std::vector< FieldTarget > targets
std::map< std::string, std::vector< std::string > > graph
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
virtual void reset()
Resets the current color, background color, line style, and so forth.
void push(bool all)
Pushes .
std::unique_ptr< RenderControl > renderControl_
void clear(std::optional< Color > color, std::optional< int > stencil, std::optional< double > depth) override
Clears .
void recordFrameGraphWithJobSystem(vkb::FrameGraph &graph, eve::thread::JobSystem *jobs, FrameGraphRecordingState &state)
Record a vkb::FrameGraph with the engine JobSystem as the parallel recording executor,...
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
static constexpr int kCascades
static constexpr int kTotalLayers
int cascadeMapSizes[ShadowConfig::kCascades]
Per-cascade render viewport edge length (texels); drives shadow pass viewport.