-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblur-ping-pong.cpp
146 lines (130 loc) · 4.96 KB
/
blur-ping-pong.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "graphicsApp.h"
class PingPongBlur : public GraphicsApp
{
static constexpr uint32_t numPasses = 32;
struct Constants
{
VkBool32 ping;
};
struct Pass
{
std::shared_ptr<magma::aux::ColorFramebuffer> framebuffer;
std::shared_ptr<magma::DescriptorSet> set;
std::shared_ptr<magma::GraphicsPipeline> pipeline;
} ping, pong;
std::shared_ptr<magma::GraphicsPipeline> checkerboardPipeline;
bool blurImage = true;
public:
explicit PingPongBlur(const AppEntry& entry):
GraphicsApp(entry, TEXT("Ping-pong blur"), 1280, 32 * 22, false)
{
createPingPongPasses();
setupGraphicsPipelines();
renderScene(FrontBuffer);
renderScene(BackBuffer);
}
virtual void render(uint32_t bufferIndex) override
{
queue->submit(commandBuffers[bufferIndex],
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
presentFinished, // Wait for swapchain
renderFinished,
waitFences[bufferIndex]);
}
virtual void onKeyDown(char key, int repeat, uint32_t flags) override
{
switch (key)
{
case AppKey::Space:
blurImage = !blurImage;
renderScene(FrontBuffer);
renderScene(BackBuffer);
break;
}
VulkanApp::onKeyDown(key, repeat, flags);
}
Pass createPass(std::shared_ptr<magma::DescriptorSetLayout> layout, bool ping)
{
Pass pass;
pass.framebuffer = std::make_shared<magma::aux::ColorFramebuffer>(device,
VK_FORMAT_R8G8B8A8_UNORM, msaaFramebuffer->getExtent(), false);
pass.set = descriptorPool->allocateDescriptorSet(layout);
const Constants constant = {MAGMA_BOOLEAN(ping)};
const magma::SpecializationEntry entry(0, &Constants::ping);
auto specialization = std::make_shared<magma::Specialization>(constant, entry);
pass.pipeline = createFullscreenPipeline("quadoff.o", "bilerp.o", std::move(specialization), std::move(layout), pass.framebuffer);
return pass;
}
void createPingPongPasses()
{
auto layout = std::make_shared<magma::DescriptorSetLayout>(device,
magma::bindings::VertexFragmentStageBinding(0, magma::descriptors::CombinedImageSampler(1)));
ping = createPass(layout, true);
pong = createPass(layout, false);
ping.set->writeDescriptor(0, pong.framebuffer->getColorView(), bilinearRepeat); // Use hardware bilinear filtering
pong.set->writeDescriptor(0, ping.framebuffer->getColorView(), bilinearRepeat); // Use hardware bilinear filtering
}
void setupGraphicsPipelines()
{
checkerboardPipeline = createFullscreenPipeline("quad.o", "checkerboard.o", nullptr, nullptr, pong.framebuffer);
bltRect = std::make_unique<magma::aux::BlitRectangle>(renderPass);
}
void renderScene(uint32_t bufferIndex)
{
std::shared_ptr<magma::CommandBuffer> cmdBuffer = commandBuffers[bufferIndex];
cmdBuffer->begin();
{
if (!blurImage)
checkerboardPass(cmdBuffer, bufferIndex);
else
{
if (FrontBuffer == bufferIndex)
{ // Draw once
checkerboardPass(cmdBuffer, bufferIndex);
blurPass(cmdBuffer);
}
blitPass(cmdBuffer, bufferIndex);
}
}
cmdBuffer->end();
}
void checkerboardPass(std::shared_ptr<magma::CommandBuffer> cmdBuffer, uint32_t bufferIndex)
{
if (blurImage)
cmdBuffer->beginRenderPass(pong.framebuffer->getRenderPass(), pong.framebuffer->getFramebuffer());
else // Draw to swapchain
cmdBuffer->beginRenderPass(renderPass, framebuffers[bufferIndex]);
{
cmdBuffer->bindPipeline(checkerboardPipeline);
cmdBuffer->draw(4, 0);
}
cmdBuffer->endRenderPass();
}
void blurPass(std::shared_ptr<magma::CommandBuffer> cmdBuffer)
{
for (uint32_t i = 0; i < numPasses; ++i)
{
const Pass& pass = i % 2 ? pong : ping;
cmdBuffer->beginRenderPass(pass.framebuffer->getRenderPass(), pass.framebuffer->getFramebuffer());
{
cmdBuffer->bindPipeline(pass.pipeline);
cmdBuffer->bindDescriptorSet(pass.pipeline, pass.set);
cmdBuffer->draw(4, 0);
}
cmdBuffer->endRenderPass();
}
}
void blitPass(std::shared_ptr<magma::CommandBuffer> cmdBuffer, uint32_t bufferIndex)
{
cmdBuffer->beginRenderPass(renderPass, framebuffers[bufferIndex]);
{
const VkRect2D rc{0, 0, width, height};
bltRect->blit(cmdBuffer, pong.framebuffer->getColorView(), VK_FILTER_NEAREST, rc);
}
cmdBuffer->endRenderPass();
}
};
std::unique_ptr<IApplication> appFactory(const AppEntry& entry)
{
return std::unique_ptr<PingPongBlur>(new PingPongBlur(entry));
}