Foundation
Loading...
Searching...
No Matches
MandelbrotCompute.cpp

Mandelbrot set rendering on the compute shader and automatically synchronizes the result back to display.

#include "Examples.hpp"
namespace Examples
{
class MandelbrotComputeDemoApp : public RenderApplication
{
struct PushConstant
{
float time;
float pad;
RHIExtent2D resolution;
};
void OnRendererSetup() override
{
ResourceHandle buffer = createResource(
mRenderer.get(), "Mandelbrot Image",
RHITextureDesc{.usage = RHITextureUsageBits::SampledImage | RHITextureUsageBits::StorageImage,
.extent = mRenderer->GetSwapchainExtent3D(),
.format = RHIResourceFormat::R8G8B8A8Unorm});
ResourceHandle sampler = createSampler(mRenderer.get(), {});
createPass(
mRenderer.get(), "Mandelbrot", RHIDeviceQueueType::Compute,
[=](PassHandle self, Renderer* r)
{
r->BindShader(self, RHIShaderStageBits::Compute, "csMain", "data/shaders/MandelbrotCompute.spv");
r->BindPushConstant(self, RHIShaderStageBits::Compute, 0, sizeof(PushConstant));
r->BindTextureUAV(
self, buffer, "image", RHIPipelineStageBits::ComputeShader,
{.format = RHIResourceFormat::R8G8B8A8Unorm, .range = RHITextureSubresourceRange::Create()});
},
[=, this](PassHandle self, Renderer* r, RHICommandList* cmd)
{
auto image_wh = r->DerefResource(buffer).Get<RHITexture*>()->mDesc.extent;
r->CmdSetPipeline(self, cmd);
r->CmdSetPushConstant(self, cmd, RHIShaderStageBits::Compute, 0,
PushConstant{.time = GetApplicationTime(), .resolution = image_wh});
r->CmdDispatch(self, cmd, image_wh);
});
createPSBackbufferBlitPass(mRenderer.get(), "Backbuffer Blit", sampler, buffer);
}
};
} // namespace Examples
int main(int argc, char** argv)
{
// This will actually be slower when you have async compute enabled
// Overhead from synchronization is more than the gain from parallelism
// (which isn't much for this simple example)
const bool useAsync = CreateMessageBox("Async Compute", "Enable Async Compute?", MessageBoxType::YesNo,
MessageBoxIcon::Question, MessageBoxResult::Yes) == MessageBoxResult::Yes;
app.Initialize<VulkanApplication>(
{.windowTitle = "Mandelbrot Async Compute", .renderer = {.enableAsyncCompute = useAsync}});
app.RunForever();
}
int main(int argc, char **argv)
Definition ImGui.cpp:26
Definition MandelbrotCompute.cpp:11
void OnRendererSetup() override
Set up the renderer by creating passes, resources, and other configurations.
Definition MandelbrotCompute.cpp:18
T GetApplicationTime() const
Returns a high-resolution time in seconds since the application started.
Definition Application.hpp:60
ApplicationInitDesc mDesc
Definition Application.hpp:93
void Initialize(ApplicationInitDesc const &desc={}, Args &&... args)
Initialize the application with the specified RHI backend.
Definition Application.hpp:205
UniquePtr< Renderer > mRenderer
Definition Application.hpp:102
void RunForever()
Start the Render thread and run the application loop indefinitely, until the window is closed or the ...
Definition Application.cpp:127
For a complete list of examples, see the Examples Page.
Definition Examples.hpp:12
MessageBoxResult CreateMessageBox(const char *title, const char *message, MessageBoxType type, MessageBoxIcon icon, MessageBoxResult default_result)
Creates a message box with the specified title and message.
Definition Application.cpp:66
RHIExtent2D resolution
Definition MandelbrotCompute.cpp:16
float pad
Definition MandelbrotCompute.cpp:15
float time
Definition MandelbrotCompute.cpp:14