|
| 1 | +# FrameGraph |
| 2 | + |
| 3 | +[](https://www.codefactor.io/repository/github/skaarj1989/framegraph) |
| 4 | + |
| 5 | + |
| 6 | +This is a renderer agnostic implementation of **FrameGraph**, inspired by the **GDC** presentation: |
| 7 | +[_FrameGraph: Extensible Rendering Architecture in Frostbite_](https://www.gdcvault.com/play/1024045/FrameGraph-Extensible-Rendering-Architecture-in) by Yuriy O'Donnell |
| 8 | + |
| 9 | +## How to use? |
| 10 | + |
| 11 | +### Basic |
| 12 | + |
| 13 | +```cpp |
| 14 | +#include "fg/FrameGraph.hpp" |
| 15 | + |
| 16 | +void renderFrame() { |
| 17 | + FrameGraph fg; |
| 18 | + |
| 19 | + struct PassData { |
| 20 | + FrameGraphResource target; |
| 21 | + }; |
| 22 | + fg.addCallbackPass<PassData>("SimplePass", |
| 23 | + [&](FrameGraph::Builder &builder, PassData &data) { |
| 24 | + data.target = builder.create<FrameGraphTexture>("Foo", { 1280, 720 }); |
| 25 | + data.target = builder.write(data.target); |
| 26 | + }, |
| 27 | + [=](const PassData &data, FrameGraphPassResources &resources, void *) { |
| 28 | + auto &texture = resources.get<FrameGraphTexture>(data.target); |
| 29 | + // ... |
| 30 | + } |
| 31 | + ); |
| 32 | + |
| 33 | + fg.compile(); |
| 34 | + fg.execute(&renderContext); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Blackboard |
| 39 | + |
| 40 | +Communication between modules |
| 41 | + |
| 42 | +```cpp |
| 43 | +#include "fg/FrameGraph.hpp" |
| 44 | +#include "fg/Blackboard.hpp" |
| 45 | + |
| 46 | +struct GBufferData { |
| 47 | + FrameGraphResource depth; |
| 48 | + FrameGraphResource normal; |
| 49 | + FrameGraphResource albedo; |
| 50 | +}; |
| 51 | +GBufferPass::GBufferPass(FrameGraph &fg, FrameGraphBlackboard &blackboard, |
| 52 | + std::span<const Renderable> renderables) { |
| 53 | + blackboard.add<GBufferData>() = fg.addCallbackPass<GBufferData>( |
| 54 | + "GBuffer Pass", |
| 55 | + [&](FrameGraph::Builder &builder, GBufferData &data) { |
| 56 | + data.depth = builder.create<FrameGraphTexture>( |
| 57 | + "SceneDepth", {/* extent, pixelFormat ... */}); |
| 58 | + data.depth = builder.write(data.depth); |
| 59 | + |
| 60 | + data.normal = builder.create<FrameGraphTexture>("Normal", {}); |
| 61 | + data.normal = builder.write(data.normal); |
| 62 | + |
| 63 | + data.albedo = builder.create<FrameGraphTexture>("Albedo", {}); |
| 64 | + data.albedo = builder.write(data.albedo); |
| 65 | + }, |
| 66 | + [=](const GBufferData &data, FrameGraphPassResources &resources, |
| 67 | + void *ctx) { |
| 68 | + auto &rc = *static_cast<RenderContext *>(ctx); |
| 69 | + rc.beginRenderPass({ |
| 70 | + resources.get<FrameGraphTexture>(data.depth), |
| 71 | + resources.get<FrameGraphTexture>(data.normal), |
| 72 | + resources.get<FrameGraphTexture>(data.albedo), |
| 73 | + }); |
| 74 | + for (const auto &renderable : renderables) |
| 75 | + drawMesh(rc, renderable.mesh, renderable.material); |
| 76 | + rc.endRenderPass(); |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +struct SceneColorData { |
| 81 | + FrameGraphResource hdr; |
| 82 | +}; |
| 83 | +DeferredLightingPass::DeferredLightingPass(FrameGraph &fg, |
| 84 | + FrameGraphBlackboard &blackboard) { |
| 85 | + const auto &gBuffer = blackboard.get<GBufferData>(); |
| 86 | + |
| 87 | + blackboard.add<SceneColorData>() = fg.addCallbackPass<SceneColorData>( |
| 88 | + "GBuffer Pass", |
| 89 | + [&](FrameGraph::Builder &builder, SceneColorData &data) { |
| 90 | + builder.read(gBuffer.depth); |
| 91 | + builder.read(gBuffer.normal); |
| 92 | + builder.read(gBuffer.albedo); |
| 93 | + |
| 94 | + data.hdr = builder.create<FrameGraphTexture>("SceneColor", {}); |
| 95 | + data.hdr = builder.write(data.hdr); |
| 96 | + }, |
| 97 | + [=](const SceneColorData &data, FrameGraphPassResources &resources, |
| 98 | + void *ctx) { |
| 99 | + auto &rc = *static_cast<RenderContext *>(ctx); |
| 100 | + rc.beginRenderPass({resources.get<FrameGraphTexture>(data.hdr)}) |
| 101 | + .bindTextures({ |
| 102 | + resources.get<FrameGraphTexture>(gBuffer.depth), |
| 103 | + resources.get<FrameGraphTexture>(gBuffer.normal), |
| 104 | + resources.get<FrameGraphTexture>(gBuffer.albedo), |
| 105 | + }) |
| 106 | + .drawFullScreenQuad() |
| 107 | + .endRenderPass(); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +void renderFrame(std::span<const Renderable> renderables) { |
| 112 | + FrameGraph fg; |
| 113 | + FrameGraphBlackboard blackboard; |
| 114 | + |
| 115 | + GBufferPass{fg, blackboard, renderables}; |
| 116 | + DeferredLightingPass{fg, blackboard}; |
| 117 | + |
| 118 | + fg.compile(); |
| 119 | + fg.execute(&renderContext); |
| 120 | +} |
| 121 | +``` |
| 122 | +
|
| 123 | +### Visualization |
| 124 | +
|
| 125 | +```cpp |
| 126 | +std::ofstream{"fg.dot"} << fg; |
| 127 | +``` |
| 128 | + |
| 129 | + |
| 130 | +_(Graph created by one of tests)_ |
| 131 | + |
| 132 | +### Resources |
| 133 | + |
| 134 | +To integrate a resource with **FrameGraph**, the following requirements should be met. |
| 135 | +**T** is a type meeting the requirements of **FrameGraph** resource. |
| 136 | + |
| 137 | +| Expression | Type | Description | |
| 138 | +| --------------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------- | |
| 139 | +| <pre lang="cpp">T{}</pre> | <pre lang="cpp">T</pre> | Is default/move constructible. | |
| 140 | +| <pre lang="cpp">T::Desc</pre> | <pre lang="cpp">struct</pre> | Resource descriptor. | |
| 141 | +| <pre lang="cpp">T::create</pre> | <pre lang="cpp">void(const T::Desc &, void \*)</pre> | A function used by implementation to create transient resource. | |
| 142 | +| <pre lang="cpp">T::destroy</pre> | <pre lang="cpp">void(const T::Dest &, void \*)</pre> | A function used by implementation to destroy transient resource. | |
| 143 | +| <pre lang="cpp">T::toString</pre> | <pre lang="cpp">std::string(const T::Desc &)<pre> | _(optional)_<br/>Static function used to embed resource descriptor inside graph node. | |
| 144 | + |
| 145 | +## Installation |
| 146 | + |
| 147 | +```bash |
| 148 | +git submodule init |
| 149 | +git submodule add https://github.com/skaarj1989/FrameGraph.git extern/FrameGraph |
| 150 | +``` |
| 151 | + |
| 152 | +```cmake |
| 153 | +add_subdirectory(extern/FrameGraph) |
| 154 | +target_link_libraries(YourProject PRIVATE fg::FrameGraph) |
| 155 | +``` |
| 156 | + |
| 157 | +Another possibility is to use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html): |
| 158 | + |
| 159 | +```cmake |
| 160 | +include(FetchContent) |
| 161 | +
|
| 162 | +FetchContent_Declare( |
| 163 | + FrameGraph |
| 164 | + GIT_REPOSITORY https://github.com/skaarj1989/FrameGraph.git |
| 165 | + GIT_TAG master) |
| 166 | +FetchContent_MakeAvailable(FrameGraph) |
| 167 | +
|
| 168 | +target_link_libraries(YourProject PRIVATE fg::FrameGraph) |
| 169 | +``` |
| 170 | + |
| 171 | +## Example |
| 172 | + |
| 173 | +Coming soon ... |
| 174 | + |
| 175 | +## License |
| 176 | + |
| 177 | +[MIT](LICENSE) |
0 commit comments