| 1 |
/* |
| 2 |
* Copyright (C) 2019 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <gtest/gtest.h> |
| 18 |
|
| 19 |
#include "ResourceAllocator.h" |
| 20 |
|
| 21 |
#include <backend/Platform.h> |
| 22 |
|
| 23 |
#include "private/backend/CommandStream.h" |
| 24 |
|
| 25 |
#include "fg2/FrameGraph.h" |
| 26 |
#include "fg2/FrameGraphResources.h" |
| 27 |
#include "fg2/details/DependencyGraph.h" |
| 28 |
|
| 29 |
#include "details/Texture.h" |
| 30 |
|
| 31 |
using namespace filament; |
| 32 |
using namespace backend; |
| 33 |
|
| 34 |
class MockResourceAllocator : public ResourceAllocatorInterface { |
| 35 |
uint32_t handle = 0; |
| 36 |
public: |
| 37 |
backend::RenderTargetHandle createRenderTarget(const char* name, |
| 38 |
backend::TargetBufferFlags targetBufferFlags, |
| 39 |
uint32_t width, |
| 40 |
uint32_t height, |
| 41 |
uint8_t samples, |
| 42 |
backend::MRT color, |
| 43 |
backend::TargetBufferInfo depth, |
| 44 |
backend::TargetBufferInfo stencil) noexcept override { |
| 45 |
return backend::RenderTargetHandle(++handle); |
| 46 |
} |
| 47 |
|
| 48 |
void destroyRenderTarget(backend::RenderTargetHandle h) noexcept override { |
| 49 |
} |
| 50 |
|
| 51 |
backend::TextureHandle createTexture(const char* name, backend::SamplerType target, |
| 52 |
uint8_t levels, |
| 53 |
backend::TextureFormat format, uint8_t samples, uint32_t width, uint32_t height, |
| 54 |
uint32_t depth, std::array<backend::TextureSwizzle, 4>, |
| 55 |
backend::TextureUsage usage) noexcept override { |
| 56 |
return backend::TextureHandle(++handle); |
| 57 |
} |
| 58 |
|
| 59 |
void destroyTexture(backend::TextureHandle h) noexcept override { |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
class FrameGraphTest : public testing::Test { |
| 64 |
protected: |
| 65 |
void SetUp() override { |
| 66 |
} |
| 67 |
|
| 68 |
void TearDown() override { |
| 69 |
//fg.export_graphviz(utils::slog.d); |
| 70 |
} |
| 71 |
|
| 72 |
Backend backend = Backend::NOOP; |
| 73 |
CircularBuffer buffer = CircularBuffer{ 8192 }; |
| 74 |
DefaultPlatform* platform = DefaultPlatform::create(&backend); |
| 75 |
CommandStream driverApi = CommandStream{ *platform->createDriver(nullptr), buffer }; |
| 76 |
MockResourceAllocator resourceAllocator; |
| 77 |
FrameGraph fg{resourceAllocator}; |
| 78 |
}; |
| 79 |
|
| 80 |
class Node : public DependencyGraph::Node { |
| 81 |
const char *mName; |
| 82 |
char const* getName() const noexcept override { return mName; } |
| 83 |
public: |
| 84 |
Node(DependencyGraph& graph, const char* name) noexcept : DependencyGraph::Node(graph), mName(name) { } |
| 85 |
bool isCulledCalled() const noexcept { return this->isCulled(); } |
| 86 |
}; |
| 87 |
|
| 88 |
TEST(DependencyGraphTest, Simple) { |
| 89 |
DependencyGraph graph; |
| 90 |
Node* n0 = new Node(graph, "node 0"); |
| 91 |
Node* n1 = new Node(graph, "node 1"); |
| 92 |
Node* n2 = new Node(graph, "node 2"); |
| 93 |
|
| 94 |
new DependencyGraph::Edge(graph, n0, n1); |
| 95 |
new DependencyGraph::Edge(graph, n1, n2); |
| 96 |
n2->makeTarget(); |
| 97 |
|
| 98 |
graph.cull(); |
| 99 |
|
| 100 |
//graph.export_graphviz(utils::slog.d); |
| 101 |
|
| 102 |
EXPECT_FALSE(n2->isCulled()); |
| 103 |
EXPECT_FALSE(n1->isCulled()); |
| 104 |
EXPECT_FALSE(n0->isCulled()); |
| 105 |
EXPECT_FALSE(n2->isCulledCalled()); |
| 106 |
EXPECT_FALSE(n1->isCulledCalled()); |
| 107 |
EXPECT_FALSE(n0->isCulledCalled()); |
| 108 |
|
| 109 |
EXPECT_EQ(n0->getRefCount(), 1); |
| 110 |
EXPECT_EQ(n1->getRefCount(), 1); |
| 111 |
EXPECT_EQ(n2->getRefCount(), 1); |
| 112 |
|
| 113 |
auto edges = graph.getEdges(); |
| 114 |
auto nodes = graph.getNodes(); |
| 115 |
graph.clear(); |
| 116 |
for (auto e : edges) { delete e; } |
| 117 |
for (auto n : nodes) { delete n; } |
| 118 |
} |
| 119 |
|
| 120 |
TEST(DependencyGraphTest, Culling1) { |
| 121 |
DependencyGraph graph; |
| 122 |
Node* n0 = new Node(graph, "node 0"); |
| 123 |
Node* n1 = new Node(graph, "node 1"); |
| 124 |
Node* n2 = new Node(graph, "node 2"); |
| 125 |
Node* n1_0 = new Node(graph, "node 1.0"); |
| 126 |
|
| 127 |
new DependencyGraph::Edge(graph, n0, n1); |
| 128 |
new DependencyGraph::Edge(graph, n1, n2); |
| 129 |
new DependencyGraph::Edge(graph, n1, n1_0); |
| 130 |
n2->makeTarget(); |
| 131 |
|
| 132 |
graph.cull(); |
| 133 |
|
| 134 |
//graph.export_graphviz(utils::slog.d); |
| 135 |
|
| 136 |
EXPECT_TRUE(n1_0->isCulled()); |
| 137 |
EXPECT_TRUE(n1_0->isCulledCalled()); |
| 138 |
|
| 139 |
EXPECT_FALSE(n2->isCulled()); |
| 140 |
EXPECT_FALSE(n1->isCulled()); |
| 141 |
EXPECT_FALSE(n0->isCulled()); |
| 142 |
EXPECT_FALSE(n2->isCulledCalled()); |
| 143 |
EXPECT_FALSE(n1->isCulledCalled()); |
| 144 |
EXPECT_FALSE(n0->isCulledCalled()); |
| 145 |
|
| 146 |
EXPECT_EQ(n0->getRefCount(), 1); |
| 147 |
EXPECT_EQ(n1->getRefCount(), 1); |
| 148 |
EXPECT_EQ(n2->getRefCount(), 1); |
| 149 |
|
| 150 |
auto edges = graph.getEdges(); |
| 151 |
auto nodes = graph.getNodes(); |
| 152 |
graph.clear(); |
| 153 |
for (auto e : edges) { delete e; } |
| 154 |
for (auto n : nodes) { delete n; } |
| 155 |
} |
| 156 |
|
| 157 |
TEST(DependencyGraphTest, Culling2) { |
| 158 |
DependencyGraph graph; |
| 159 |
Node* n0 = new Node(graph, "node 0"); |
| 160 |
Node* n1 = new Node(graph, "node 1"); |
| 161 |
Node* n2 = new Node(graph, "node 2"); |
| 162 |
Node* n1_0 = new Node(graph, "node 1.0"); |
| 163 |
Node* n1_0_0 = new Node(graph, "node 1.0.0"); |
| 164 |
Node* n1_0_1 = new Node(graph, "node 1.0.0"); |
| 165 |
|
| 166 |
new DependencyGraph::Edge(graph, n0, n1); |
| 167 |
new DependencyGraph::Edge(graph, n1, n2); |
| 168 |
new DependencyGraph::Edge(graph, n1, n1_0); |
| 169 |
new DependencyGraph::Edge(graph, n1_0, n1_0_0); |
| 170 |
new DependencyGraph::Edge(graph, n1_0, n1_0_1); |
| 171 |
n2->makeTarget(); |
| 172 |
|
| 173 |
graph.cull(); |
| 174 |
|
| 175 |
//graph.export_graphviz(utils::slog.d); |
| 176 |
|
| 177 |
EXPECT_TRUE(n1_0->isCulled()); |
| 178 |
EXPECT_TRUE(n1_0_0->isCulled()); |
| 179 |
EXPECT_TRUE(n1_0_1->isCulled()); |
| 180 |
EXPECT_TRUE(n1_0->isCulledCalled()); |
| 181 |
EXPECT_TRUE(n1_0_0->isCulledCalled()); |
| 182 |
EXPECT_TRUE(n1_0_1->isCulledCalled()); |
| 183 |
|
| 184 |
EXPECT_FALSE(n2->isCulled()); |
| 185 |
EXPECT_FALSE(n1->isCulled()); |
| 186 |
EXPECT_FALSE(n0->isCulled()); |
| 187 |
EXPECT_FALSE(n2->isCulledCalled()); |
| 188 |
EXPECT_FALSE(n1->isCulledCalled()); |
| 189 |
EXPECT_FALSE(n0->isCulledCalled()); |
| 190 |
|
| 191 |
EXPECT_EQ(n0->getRefCount(), 1); |
| 192 |
EXPECT_EQ(n1->getRefCount(), 1); |
| 193 |
EXPECT_EQ(n2->getRefCount(), 1); |
| 194 |
|
| 195 |
auto edges = graph.getEdges(); |
| 196 |
auto nodes = graph.getNodes(); |
| 197 |
graph.clear(); |
| 198 |
for (auto e : edges) { delete e; } |
| 199 |
for (auto n : nodes) { delete n; } |
| 200 |
} |
| 201 |
|
| 202 |
TEST_F(FrameGraphTest, ReadRead) { |
| 203 |
struct PassData { |
| 204 |
FrameGraphId<FrameGraphTexture> input; |
| 205 |
}; |
| 206 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 207 |
data.input = builder.create<FrameGraphTexture>("Input buffer", {.width=16, .height=32}); |
| 208 |
data.input = builder.read(data.input, FrameGraphTexture::Usage::SAMPLEABLE); |
| 209 |
data.input = builder.read(data.input, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 210 |
builder.sideEffect(); |
| 211 |
}, |
| 212 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 213 |
EXPECT_EQ(resources.getUsage(data.input), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 214 |
}); |
| 215 |
|
| 216 |
EXPECT_TRUE(fg.isAcyclic()); |
| 217 |
|
| 218 |
fg.compile(); |
| 219 |
|
| 220 |
EXPECT_FALSE(fg.isCulled(pass)); |
| 221 |
|
| 222 |
fg.execute(driverApi); |
| 223 |
} |
| 224 |
|
| 225 |
TEST_F(FrameGraphTest, WriteWrite) { |
| 226 |
struct PassData { |
| 227 |
FrameGraphId<FrameGraphTexture> output1; |
| 228 |
FrameGraphId<FrameGraphTexture> output2; |
| 229 |
}; |
| 230 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 231 |
data.output1 = builder.create<FrameGraphTexture>("Input buffer", {.width=16, .height=32}); |
| 232 |
data.output1 = builder.write(data.output1, FrameGraphTexture::Usage::UPLOADABLE); |
| 233 |
data.output2 = builder.write(data.output1, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 234 |
EXPECT_EQ(data.output1, data.output2); |
| 235 |
}, |
| 236 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 237 |
EXPECT_EQ(resources.getUsage(data.output2), FrameGraphTexture::Usage::UPLOADABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 238 |
}); |
| 239 |
|
| 240 |
fg.present(pass->output2); |
| 241 |
|
| 242 |
EXPECT_TRUE(fg.isAcyclic()); |
| 243 |
|
| 244 |
fg.compile(); |
| 245 |
|
| 246 |
EXPECT_FALSE(fg.isCulled(pass)); |
| 247 |
|
| 248 |
fg.execute(driverApi); |
| 249 |
} |
| 250 |
|
| 251 |
TEST_F(FrameGraphTest, WriteRead) { |
| 252 |
struct PassData { |
| 253 |
FrameGraphId<FrameGraphTexture> inout; |
| 254 |
}; |
| 255 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 256 |
data.inout = builder.create<FrameGraphTexture>("Inout buffer", {.width=16, .height=32}); |
| 257 |
data.inout = builder.write(data.inout); |
| 258 |
EXPECT_ANY_THROW( builder.read(data.inout) ); |
| 259 |
}, |
| 260 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 261 |
}); |
| 262 |
|
| 263 |
EXPECT_TRUE(fg.isAcyclic()); |
| 264 |
|
| 265 |
fg.compile(); |
| 266 |
|
| 267 |
EXPECT_TRUE(fg.isCulled(pass)); |
| 268 |
|
| 269 |
fg.execute(driverApi); |
| 270 |
} |
| 271 |
|
| 272 |
TEST_F(FrameGraphTest, ReadWrite) { |
| 273 |
FrameGraphTexture inputTexture{ .handle = Handle<HwTexture>{ 0x3141 }}; |
| 274 |
FrameGraphId<FrameGraphTexture> imported = fg.import("Imported input texture", |
| 275 |
FrameGraphTexture::Descriptor{ .width = 640, .height = 400 }, |
| 276 |
FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT, |
| 277 |
inputTexture); |
| 278 |
|
| 279 |
struct PassData { |
| 280 |
FrameGraphId<FrameGraphTexture> inout; |
| 281 |
FrameGraphId<FrameGraphTexture> imported; |
| 282 |
}; |
| 283 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 284 |
data.inout = builder.create<FrameGraphTexture>("Inout buffer", {.width=16, .height=32}); |
| 285 |
data.inout = builder.read(data.inout); |
| 286 |
data.inout = builder.write(data.inout); |
| 287 |
data.imported = builder.read(imported, FrameGraphTexture::Usage::SAMPLEABLE); |
| 288 |
data.imported = builder.write(data.imported, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 289 |
}, |
| 290 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 291 |
EXPECT_EQ(resources.getUsage(data.inout), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 292 |
EXPECT_EQ(resources.getUsage(data.imported), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 293 |
}); |
| 294 |
|
| 295 |
fg.present(pass->inout); |
| 296 |
|
| 297 |
EXPECT_TRUE(fg.isAcyclic()); |
| 298 |
|
| 299 |
fg.compile(); |
| 300 |
|
| 301 |
EXPECT_FALSE(fg.isCulled(pass)); |
| 302 |
|
| 303 |
fg.execute(driverApi); |
| 304 |
} |
| 305 |
|
| 306 |
TEST_F(FrameGraphTest, Culling1) { |
| 307 |
struct PassData { |
| 308 |
FrameGraphId<FrameGraphTexture> out0; |
| 309 |
FrameGraphId<FrameGraphTexture> out1; |
| 310 |
}; |
| 311 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 312 |
data.out0 = builder.create<FrameGraphTexture>("Out0 buffer", {.width=16, .height=32}); |
| 313 |
data.out0 = builder.write(data.out0, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 314 |
data.out1 = builder.create<FrameGraphTexture>("Out1 buffer", {.width=16, .height=32}); |
| 315 |
data.out1 = builder.write(data.out1, FrameGraphTexture::Usage::UPLOADABLE); |
| 316 |
}, |
| 317 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 318 |
// even if out1 is not consumed, we expect it to exist, because this pass needs it |
| 319 |
EXPECT_TRUE(resources.get(data.out0).handle); |
| 320 |
EXPECT_TRUE(resources.get(data.out1).handle); |
| 321 |
EXPECT_EQ(resources.getUsage(data.out0), FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 322 |
EXPECT_EQ(resources.getUsage(data.out1), FrameGraphTexture::Usage::UPLOADABLE); |
| 323 |
}); |
| 324 |
|
| 325 |
fg.present(pass->out0); |
| 326 |
|
| 327 |
EXPECT_TRUE(fg.isAcyclic()); |
| 328 |
|
| 329 |
fg.compile(); |
| 330 |
|
| 331 |
EXPECT_FALSE(fg.isCulled(pass)); |
| 332 |
|
| 333 |
fg.execute(driverApi); |
| 334 |
} |
| 335 |
|
| 336 |
TEST_F(FrameGraphTest, Basic) { |
| 337 |
struct DepthPassData { |
| 338 |
FrameGraphId<FrameGraphTexture> depth; |
| 339 |
}; |
| 340 |
auto& depthPass = fg.addPass<DepthPassData>("Depth pass", |
| 341 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 342 |
data.depth = builder.create<FrameGraphTexture>("Depth Buffer", {.width=16, .height=32}); |
| 343 |
data.depth = builder.write(data.depth, FrameGraphTexture::Usage::DEPTH_ATTACHMENT); |
| 344 |
builder.declareRenderPass("Depth target", { .attachments = { .depth = data.depth }}); |
| 345 |
EXPECT_TRUE(fg.isValid(data.depth)); |
| 346 |
}, |
| 347 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 348 |
FrameGraphTexture const& depth = resources.get(data.depth); |
| 349 |
EXPECT_TRUE((bool)depth.handle); |
| 350 |
auto rp = resources.getRenderPassInfo(); |
| 351 |
EXPECT_EQ(rp.params.flags.discardStart, TargetBufferFlags::DEPTH); |
| 352 |
EXPECT_EQ(rp.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 353 |
EXPECT_EQ(rp.params.viewport.width, 16); |
| 354 |
EXPECT_EQ(rp.params.viewport.height, 32); |
| 355 |
EXPECT_TRUE((bool)rp.target); |
| 356 |
}); |
| 357 |
|
| 358 |
struct GBufferPassData { |
| 359 |
FrameGraphId<FrameGraphTexture> depth; |
| 360 |
FrameGraphId<FrameGraphTexture> gbuf1; |
| 361 |
FrameGraphId<FrameGraphTexture> gbuf2; |
| 362 |
FrameGraphId<FrameGraphTexture> gbuf3; |
| 363 |
}; |
| 364 |
auto& gBufferPass = fg.addPass<GBufferPassData>("Gbuffer pass", |
| 365 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 366 |
data.depth = builder.read(depthPass->depth, FrameGraphTexture::Usage::DEPTH_ATTACHMENT); |
| 367 |
FrameGraphTexture::Descriptor desc = builder.getDescriptor(data.depth); |
| 368 |
data.gbuf1 = builder.create<FrameGraphTexture>("Gbuffer 1", desc); |
| 369 |
data.gbuf2 = builder.create<FrameGraphTexture>("Gbuffer 2", desc); |
| 370 |
data.gbuf3 = builder.create<FrameGraphTexture>("Gbuffer 3", desc); |
| 371 |
data.depth = builder.write(data.depth, FrameGraphTexture::Usage::DEPTH_ATTACHMENT); |
| 372 |
data.gbuf1 = builder.write(data.gbuf1, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 373 |
data.gbuf2 = builder.write(data.gbuf2, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 374 |
data.gbuf3 = builder.write(data.gbuf3, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 375 |
builder.declareRenderPass("Gbuffer target", { .attachments = { |
| 376 |
.color = { data.gbuf1, data.gbuf2, data.gbuf3 }, |
| 377 |
.depth = data.depth |
| 378 |
}}); |
| 379 |
|
| 380 |
EXPECT_TRUE(fg.isValid(data.depth)); |
| 381 |
}, |
| 382 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 383 |
FrameGraphTexture const& depth = resources.get(data.depth); |
| 384 |
FrameGraphTexture const& gbuf1 = resources.get(data.gbuf1); |
| 385 |
FrameGraphTexture const& gbuf2 = resources.get(data.gbuf2); |
| 386 |
FrameGraphTexture const& gbuf3 = resources.get(data.gbuf3); |
| 387 |
EXPECT_TRUE((bool)depth.handle); |
| 388 |
EXPECT_TRUE((bool)gbuf1.handle); |
| 389 |
EXPECT_TRUE((bool)gbuf2.handle); |
| 390 |
EXPECT_TRUE((bool)gbuf3.handle); |
| 391 |
auto rp = resources.getRenderPassInfo(); |
| 392 |
EXPECT_EQ(rp.params.flags.discardStart, |
| 393 |
TargetBufferFlags::COLOR0 |
| 394 |
| TargetBufferFlags::COLOR1 |
| 395 |
| TargetBufferFlags::COLOR2); |
| 396 |
EXPECT_EQ(rp.params.flags.discardEnd, TargetBufferFlags::COLOR0); |
| 397 |
EXPECT_EQ(rp.params.viewport.width, 16); |
| 398 |
EXPECT_EQ(rp.params.viewport.height, 32); |
| 399 |
EXPECT_TRUE((bool)rp.target); |
| 400 |
}); |
| 401 |
|
| 402 |
struct LightingPassData { |
| 403 |
FrameGraphId<FrameGraphTexture> lightingBuffer; |
| 404 |
FrameGraphId<FrameGraphTexture> depth; |
| 405 |
FrameGraphId<FrameGraphTexture> gbuf1; |
| 406 |
FrameGraphId<FrameGraphTexture> gbuf2; |
| 407 |
FrameGraphId<FrameGraphTexture> gbuf3; |
| 408 |
}; |
| 409 |
auto& lightingPass = fg.addPass<LightingPassData>("Lighting pass", |
| 410 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 411 |
data.depth = builder.read(gBufferPass->depth, FrameGraphTexture::Usage::SAMPLEABLE); |
| 412 |
data.gbuf1 = gBufferPass->gbuf1; |
| 413 |
data.gbuf2 = builder.read(gBufferPass->gbuf2, FrameGraphTexture::Usage::SAMPLEABLE); |
| 414 |
data.gbuf3 = builder.read(gBufferPass->gbuf3, FrameGraphTexture::Usage::SAMPLEABLE); |
| 415 |
FrameGraphTexture::Descriptor desc = builder.getDescriptor(data.depth); |
| 416 |
data.lightingBuffer = builder.create<FrameGraphTexture>("Lighting buffer", desc); |
| 417 |
data.lightingBuffer = builder.declareRenderPass(data.lightingBuffer); |
| 418 |
}, |
| 419 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 420 |
FrameGraphTexture const& lightingBuffer = resources.get(data.lightingBuffer); |
| 421 |
FrameGraphTexture const& depth = resources.get(data.depth); |
| 422 |
EXPECT_ANY_THROW(FrameGraphTexture const& gbuf1 = resources.get(data.gbuf1)); |
| 423 |
FrameGraphTexture const& gbuf2 = resources.get(data.gbuf2); |
| 424 |
FrameGraphTexture const& gbuf3 = resources.get(data.gbuf3); |
| 425 |
EXPECT_TRUE((bool)lightingBuffer.handle); |
| 426 |
EXPECT_TRUE((bool)depth.handle); |
| 427 |
EXPECT_TRUE((bool)gbuf2.handle); |
| 428 |
EXPECT_TRUE((bool)gbuf3.handle); |
| 429 |
auto rp = resources.getRenderPassInfo(); |
| 430 |
EXPECT_EQ(rp.params.flags.discardStart,TargetBufferFlags::COLOR0); |
| 431 |
EXPECT_EQ(rp.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 432 |
EXPECT_EQ(rp.params.viewport.width, 16); |
| 433 |
EXPECT_EQ(rp.params.viewport.height, 32); |
| 434 |
EXPECT_TRUE((bool)rp.target); |
| 435 |
}); |
| 436 |
|
| 437 |
struct DebugPass { |
| 438 |
FrameGraphId<FrameGraphTexture> debugBuffer; |
| 439 |
FrameGraphId<FrameGraphTexture> gbuf1; |
| 440 |
FrameGraphId<FrameGraphTexture> gbuf2; |
| 441 |
FrameGraphId<FrameGraphTexture> gbuf3; |
| 442 |
}; |
| 443 |
auto& culledPass = fg.addPass<DebugPass>("DebugPass pass", |
| 444 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 445 |
data.gbuf1 = builder.read(lightingPass->gbuf1, FrameGraphTexture::Usage::SAMPLEABLE); |
| 446 |
data.gbuf2 = builder.read(lightingPass->gbuf2, FrameGraphTexture::Usage::SAMPLEABLE); |
| 447 |
data.gbuf3 = builder.read(lightingPass->gbuf3, FrameGraphTexture::Usage::SAMPLEABLE); |
| 448 |
FrameGraphTexture::Descriptor desc = builder.getDescriptor(data.gbuf1); |
| 449 |
data.debugBuffer = builder.create<FrameGraphTexture>("Debug buffer", desc); |
| 450 |
data.debugBuffer = builder.declareRenderPass(data.debugBuffer); |
| 451 |
}, |
| 452 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 453 |
FrameGraphTexture const& debugBuffer = resources.get(data.debugBuffer); |
| 454 |
FrameGraphTexture const& gbuf1 = resources.get(data.gbuf1); |
| 455 |
FrameGraphTexture const& gbuf2 = resources.get(data.gbuf2); |
| 456 |
FrameGraphTexture const& gbuf3 = resources.get(data.gbuf3); |
| 457 |
EXPECT_FALSE((bool)debugBuffer.handle); |
| 458 |
EXPECT_TRUE((bool)gbuf1.handle); |
| 459 |
EXPECT_TRUE((bool)gbuf2.handle); |
| 460 |
EXPECT_TRUE((bool)gbuf3.handle); |
| 461 |
auto rp = resources.getRenderPassInfo(); |
| 462 |
EXPECT_FALSE((bool)rp.target); |
| 463 |
}); |
| 464 |
|
| 465 |
struct PostPassData { |
| 466 |
FrameGraphId<FrameGraphTexture> lightingBuffer; |
| 467 |
FrameGraphId<FrameGraphTexture> backBuffer; |
| 468 |
struct { |
| 469 |
FrameGraphId<FrameGraphTexture> depth; |
| 470 |
FrameGraphId<FrameGraphTexture> gbuf1; |
| 471 |
FrameGraphId<FrameGraphTexture> gbuf2; |
| 472 |
FrameGraphId<FrameGraphTexture> gbuf3; |
| 473 |
} destroyed; |
| 474 |
}; |
| 475 |
auto& postPass = fg.addPass<PostPassData>("Post pass", |
| 476 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 477 |
data.lightingBuffer = builder.read(lightingPass->lightingBuffer, FrameGraphTexture::Usage::SAMPLEABLE); |
| 478 |
FrameGraphTexture::Descriptor desc = builder.getDescriptor(data.lightingBuffer); |
| 479 |
data.backBuffer = builder.create<FrameGraphTexture>("Backbuffer", desc); |
| 480 |
data.backBuffer = builder.declareRenderPass(data.backBuffer); |
| 481 |
data.destroyed.depth = lightingPass->depth; |
| 482 |
data.destroyed.gbuf1 = lightingPass->gbuf1; |
| 483 |
data.destroyed.gbuf2 = lightingPass->gbuf2; |
| 484 |
data.destroyed.gbuf3 = lightingPass->gbuf3; |
| 485 |
}, |
| 486 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 487 |
FrameGraphTexture const& lightingBuffer = resources.get(data.lightingBuffer); |
| 488 |
FrameGraphTexture const& backBuffer = resources.get(data.backBuffer); |
| 489 |
EXPECT_TRUE((bool)lightingBuffer.handle); |
| 490 |
EXPECT_TRUE((bool)backBuffer.handle); |
| 491 |
|
| 492 |
EXPECT_EQ(resources.getUsage(data.lightingBuffer), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 493 |
EXPECT_EQ(resources.getUsage(data.backBuffer), FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 494 |
|
| 495 |
auto rp = resources.getRenderPassInfo(); |
| 496 |
EXPECT_EQ(rp.params.flags.discardStart,TargetBufferFlags::COLOR0); |
| 497 |
EXPECT_EQ(rp.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 498 |
EXPECT_EQ(rp.params.viewport.width, 16); |
| 499 |
EXPECT_EQ(rp.params.viewport.height, 32); |
| 500 |
EXPECT_TRUE((bool)rp.target); |
| 501 |
|
| 502 |
EXPECT_ANY_THROW(resources.get(data.destroyed.depth)); |
| 503 |
EXPECT_ANY_THROW(resources.get(data.destroyed.gbuf1)); |
| 504 |
EXPECT_ANY_THROW(resources.get(data.destroyed.gbuf2)); |
| 505 |
EXPECT_ANY_THROW(resources.get(data.destroyed.gbuf3)); |
| 506 |
|
| 507 |
// we need a backdoor to make those checks |
| 508 |
// EXPECT_FALSE((bool)resources.get(data.destroyed.depth).handle); |
| 509 |
// EXPECT_FALSE((bool)resources.get(data.destroyed.gbuf1).handle); |
| 510 |
// EXPECT_FALSE((bool)resources.get(data.destroyed.gbuf2).handle); |
| 511 |
// EXPECT_FALSE((bool)resources.get(data.destroyed.gbuf3).handle); |
| 512 |
// EXPECT_EQ(resources.getUsage(data.destroyed.depth), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::DEPTH_ATTACHMENT); |
| 513 |
// EXPECT_EQ(resources.getUsage(data.destroyed.gbuf1), FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 514 |
// EXPECT_EQ(resources.getUsage(data.destroyed.gbuf2), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 515 |
// EXPECT_EQ(resources.getUsage(data.destroyed.gbuf3), FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 516 |
}); |
| 517 |
|
| 518 |
fg.present(postPass->backBuffer); |
| 519 |
|
| 520 |
EXPECT_TRUE(fg.isAcyclic()); |
| 521 |
|
| 522 |
fg.compile(); |
| 523 |
|
| 524 |
EXPECT_TRUE(fg.isCulled(culledPass)); |
| 525 |
|
| 526 |
fg.execute(driverApi); |
| 527 |
} |
| 528 |
|
| 529 |
TEST_F(FrameGraphTest, ImportResource) { |
| 530 |
FrameGraphTexture outputTexture{ .handle = Handle<HwTexture>{ 0x1234 }}; |
| 531 |
FrameGraphTexture inputTexture{ .handle = Handle<HwTexture>{ 0x3141 }}; |
| 532 |
|
| 533 |
FrameGraphId<FrameGraphTexture> output = fg.import("Imported output texture", FrameGraphTexture::Descriptor{ |
| 534 |
.width = 320, .height = 200 }, FrameGraphTexture::Usage::COLOR_ATTACHMENT, outputTexture); |
| 535 |
FrameGraphId<FrameGraphTexture> input = fg.import("Imported input texture", FrameGraphTexture::Descriptor{ |
| 536 |
.width = 640, .height = 400 }, FrameGraphTexture::Usage::SAMPLEABLE, inputTexture); |
| 537 |
|
| 538 |
EXPECT_TRUE(fg.isValid(output)); |
| 539 |
EXPECT_TRUE(fg.isValid(input)); |
| 540 |
|
| 541 |
struct PassData { |
| 542 |
FrameGraphId<FrameGraphTexture> input; |
| 543 |
FrameGraphId<FrameGraphTexture> output; |
| 544 |
}; |
| 545 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 546 |
auto const& descOutput = builder.getDescriptor(output); |
| 547 |
EXPECT_EQ(descOutput.width, 320); |
| 548 |
EXPECT_EQ(descOutput.height, 200); |
| 549 |
|
| 550 |
auto const& descInput = builder.getDescriptor(input); |
| 551 |
EXPECT_EQ(descInput.width, 640); |
| 552 |
EXPECT_EQ(descInput.height, 400); |
| 553 |
|
| 554 |
EXPECT_ANY_THROW( output = builder.write(output, FrameGraphTexture::Usage::UPLOADABLE) ); |
| 555 |
|
| 556 |
data.output = builder.write(output, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 557 |
EXPECT_TRUE(fg.isValid(output)); // output is expect valid here because we never read before |
| 558 |
EXPECT_TRUE(fg.isValid(data.output)); |
| 559 |
|
| 560 |
data.input = builder.read(input, FrameGraphTexture::Usage::SAMPLEABLE); |
| 561 |
EXPECT_TRUE(fg.isValid(data.input)); |
| 562 |
}, |
| 563 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 564 |
FrameGraphTexture const& output = resources.get(data.output); |
| 565 |
FrameGraphTexture const& input = resources.get(data.input); |
| 566 |
EXPECT_EQ(output.handle.getId(), 0x1234); |
| 567 |
EXPECT_EQ(input.handle.getId(), 0x3141); |
| 568 |
}); |
| 569 |
|
| 570 |
fg.present(pass->output); |
| 571 |
|
| 572 |
EXPECT_TRUE(fg.isAcyclic()); |
| 573 |
|
| 574 |
fg.compile(); |
| 575 |
|
| 576 |
fg.execute(driverApi); |
| 577 |
} |
| 578 |
|
| 579 |
TEST_F(FrameGraphTest, ForwardResource) { |
| 580 |
|
| 581 |
struct LightingPassData { |
| 582 |
FrameGraphId<FrameGraphTexture> output; |
| 583 |
}; |
| 584 |
auto& lightingPass = fg.addPass<LightingPassData>("Lighting pass", |
| 585 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 586 |
data.output = builder.createTexture("lighting output", |
| 587 |
{ .width = 640, .height = 400 }); |
| 588 |
data.output = builder.write(data.output, FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 589 |
}, |
| 590 |
[=](FrameGraphResources const& resources, auto const& data, |
| 591 |
backend::DriverApi& driver) { |
| 592 |
auto const& desc = resources.getDescriptor(data.output); |
| 593 |
|
| 594 |
EXPECT_EQ(resources.getUsage(data.output), |
| 595 |
FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 596 |
|
| 597 |
EXPECT_EQ(desc.width, 32); |
| 598 |
|
| 599 |
EXPECT_EQ(desc.height, 64); |
| 600 |
}); |
| 601 |
|
| 602 |
|
| 603 |
struct DebugPassData { |
| 604 |
FrameGraphId<FrameGraphTexture> input; |
| 605 |
FrameGraphId<FrameGraphTexture> output; |
| 606 |
}; |
| 607 |
auto& debugPass = fg.addPass<DebugPassData>("Debug pass", |
| 608 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 609 |
data.input = builder.createTexture("debug input", { .width = 32, .height = 64 }); |
| 610 |
data.output = builder.createTexture("debug output", { .width = 640, .height = 400 }); |
| 611 |
data.input = builder.sample(data.input); |
| 612 |
data.output = builder.write(data.output); |
| 613 |
}, |
| 614 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 615 |
auto const& desc = resources.getDescriptor(data.input); |
| 616 |
|
| 617 |
EXPECT_EQ(resources.getUsage(data.input), |
| 618 |
FrameGraphTexture::Usage::SAMPLEABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 619 |
|
| 620 |
EXPECT_EQ(desc.width, 32); |
| 621 |
|
| 622 |
EXPECT_EQ(desc.height, 64); |
| 623 |
}); |
| 624 |
|
| 625 |
fg.present(debugPass->output); |
| 626 |
|
| 627 |
auto input = fg.forwardResource(debugPass->input, lightingPass->output); |
| 628 |
|
| 629 |
EXPECT_TRUE(fg.isValid(input)); |
| 630 |
|
| 631 |
EXPECT_FALSE(fg.isValid(lightingPass->output)); |
| 632 |
|
| 633 |
EXPECT_TRUE(fg.isAcyclic()); |
| 634 |
|
| 635 |
fg.compile(); |
| 636 |
|
| 637 |
EXPECT_TRUE(!fg.isCulled(lightingPass)); |
| 638 |
EXPECT_TRUE(!fg.isCulled(debugPass)); |
| 639 |
|
| 640 |
fg.execute(driverApi); |
| 641 |
} |
| 642 |
|
| 643 |
TEST_F(FrameGraphTest, ForwardImportedResource) { |
| 644 |
|
| 645 |
const Handle<HwRenderTarget> outputRenderTarget{ 0x1234 }; |
| 646 |
FrameGraphId<FrameGraphTexture> output = fg.import("outputRenderTarget", |
| 647 |
{ .viewport = { 0, 0, 320, 200 } }, outputRenderTarget); |
| 648 |
|
| 649 |
struct LightingPassData { |
| 650 |
FrameGraphId<FrameGraphTexture> output; |
| 651 |
}; |
| 652 |
auto& lightingPass = fg.addPass<LightingPassData>("Lighting pass", |
| 653 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 654 |
data.output = builder.createTexture("lighting output", |
| 655 |
{ .width = 32, .height = 32 }); |
| 656 |
data.output = builder.declareRenderPass(data.output); |
| 657 |
}, |
| 658 |
[=](FrameGraphResources const& resources, auto const& data, |
| 659 |
backend::DriverApi& driver) { |
| 660 |
auto const& desc = resources.getDescriptor(data.output); |
| 661 |
auto rp = resources.getRenderPassInfo(); |
| 662 |
|
| 663 |
EXPECT_EQ(rp.target.getId(), 0x1234); |
| 664 |
|
| 665 |
EXPECT_EQ(resources.getUsage(data.output), |
| 666 |
FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 667 |
|
| 668 |
EXPECT_EQ(desc.width, 320); |
| 669 |
|
| 670 |
EXPECT_EQ(desc.height, 200); |
| 671 |
}); |
| 672 |
|
| 673 |
fg.present(lightingPass->output); |
| 674 |
|
| 675 |
fg.forwardResource(output, lightingPass->output); |
| 676 |
|
| 677 |
EXPECT_FALSE(fg.isValid(lightingPass->output)); |
| 678 |
|
| 679 |
EXPECT_TRUE(fg.isAcyclic()); |
| 680 |
|
| 681 |
fg.compile(); |
| 682 |
|
| 683 |
EXPECT_TRUE(!fg.isCulled(lightingPass)); |
| 684 |
|
| 685 |
fg.execute(driverApi); |
| 686 |
} |
| 687 |
|
| 688 |
TEST_F(FrameGraphTest, SubResourcesWrite) { |
| 689 |
|
| 690 |
struct PassData { |
| 691 |
FrameGraphId<FrameGraphTexture> output; |
| 692 |
FrameGraphId<FrameGraphTexture> outputs[4]; |
| 693 |
}; |
| 694 |
auto& pass = fg.addPass<PassData>("Pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 695 |
data.output = builder.create<FrameGraphTexture>("Color buffer", {.width=16, .height=32, .levels=4}); |
| 696 |
for (int i = 0; i < 4; i++) { |
| 697 |
data.outputs[i] = builder.createSubresource(data.output, "Color mip", { .level=uint8_t(i) }); |
| 698 |
} |
| 699 |
EXPECT_TRUE(fg.isValid(data.output)); |
| 700 |
EXPECT_TRUE(fg.isValid(data.outputs[0])); |
| 701 |
EXPECT_TRUE(fg.isValid(data.outputs[1])); |
| 702 |
EXPECT_TRUE(fg.isValid(data.outputs[2])); |
| 703 |
EXPECT_TRUE(fg.isValid(data.outputs[3])); |
| 704 |
|
| 705 |
auto const& desc0 = builder.getDescriptor(data.output); |
| 706 |
for (int i = 0; i < 4; i++) { |
| 707 |
auto const& desc = builder.getDescriptor(data.outputs[i]); |
| 708 |
EXPECT_EQ(desc.width, FTexture::valueForLevel(i, desc0.width)); |
| 709 |
EXPECT_EQ(desc.height, FTexture::valueForLevel(i, desc0.height)); |
| 710 |
EXPECT_EQ(desc.levels, 1); |
| 711 |
} |
| 712 |
|
| 713 |
for (int i = 0; i < 4; i++) { |
| 714 |
data.outputs[i] = builder.write(data.outputs[i], FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 715 |
data.outputs[i] = builder.declareRenderPass(data.outputs[i]); |
| 716 |
} |
| 717 |
|
| 718 |
EXPECT_TRUE(fg.isValid(data.outputs[0])); |
| 719 |
EXPECT_TRUE(fg.isValid(data.outputs[1])); |
| 720 |
EXPECT_TRUE(fg.isValid(data.outputs[2])); |
| 721 |
EXPECT_TRUE(fg.isValid(data.outputs[3])); |
| 722 |
}, |
| 723 |
[=](FrameGraphResources const& resources, auto const& data, |
| 724 |
backend::DriverApi& driver) { |
| 725 |
EXPECT_ANY_THROW(resources.get(data.output)); |
| 726 |
|
| 727 |
EXPECT_TRUE(resources.get(data.outputs[0]).handle); |
| 728 |
EXPECT_TRUE(resources.get(data.outputs[1]).handle); |
| 729 |
EXPECT_TRUE(resources.get(data.outputs[2]).handle); |
| 730 |
EXPECT_TRUE(resources.get(data.outputs[3]).handle); |
| 731 |
|
| 732 |
EXPECT_TRUE(any(resources.getUsage(data.outputs[0]) & FrameGraphTexture::Usage::COLOR_ATTACHMENT)); |
| 733 |
EXPECT_TRUE(any(resources.getUsage(data.outputs[0]) & FrameGraphTexture::Usage::SAMPLEABLE)); |
| 734 |
|
| 735 |
EXPECT_EQ(resources.getUsage(data.outputs[1]), FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 736 |
EXPECT_EQ(resources.getUsage(data.outputs[2]), FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 737 |
EXPECT_EQ(resources.getUsage(data.outputs[3]), FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 738 |
|
| 739 |
auto rp0 = resources.getRenderPassInfo(0); |
| 740 |
auto rp1 = resources.getRenderPassInfo(1); |
| 741 |
auto rp2 = resources.getRenderPassInfo(2); |
| 742 |
auto rp3 = resources.getRenderPassInfo(3); |
| 743 |
EXPECT_TRUE(rp0.target); |
| 744 |
EXPECT_TRUE(rp1.target); |
| 745 |
EXPECT_TRUE(rp2.target); |
| 746 |
EXPECT_TRUE(rp3.target); |
| 747 |
|
| 748 |
EXPECT_EQ(resources.getSubResourceDescriptor(data.outputs[0]).level, 0); |
| 749 |
EXPECT_EQ(resources.getSubResourceDescriptor(data.outputs[1]).level, 1); |
| 750 |
EXPECT_EQ(resources.getSubResourceDescriptor(data.outputs[2]).level, 2); |
| 751 |
EXPECT_EQ(resources.getSubResourceDescriptor(data.outputs[3]).level, 3); |
| 752 |
|
| 753 |
EXPECT_EQ(rp0.params.flags.discardStart,TargetBufferFlags::COLOR0); |
| 754 |
EXPECT_EQ(rp1.params.flags.discardStart,TargetBufferFlags::COLOR0); |
| 755 |
EXPECT_EQ(rp2.params.flags.discardStart,TargetBufferFlags::COLOR0); |
| 756 |
EXPECT_EQ(rp3.params.flags.discardStart,TargetBufferFlags::COLOR0); |
| 757 |
|
| 758 |
EXPECT_EQ(rp0.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 759 |
EXPECT_EQ(rp1.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 760 |
EXPECT_EQ(rp2.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 761 |
EXPECT_EQ(rp2.params.flags.discardEnd, TargetBufferFlags::NONE); |
| 762 |
}); |
| 763 |
|
| 764 |
struct DebugPass { |
| 765 |
FrameGraphId<FrameGraphTexture> debugBuffer; |
| 766 |
FrameGraphId<FrameGraphTexture> subresource; |
| 767 |
FrameGraphId<FrameGraphTexture> out; |
| 768 |
}; |
| 769 |
auto& debugPass = fg.addPass<DebugPass>("DebugPass pass", |
| 770 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 771 |
data.subresource = builder.read(pass->outputs[0], FrameGraphTexture::Usage::SAMPLEABLE); |
| 772 |
data.out = builder.write(data.subresource, FrameGraphTexture::Usage::UPLOADABLE); |
| 773 |
FrameGraphTexture::Descriptor desc = builder.getDescriptor(data.subresource); |
| 774 |
data.debugBuffer = builder.create<FrameGraphTexture>("Debug buffer", desc); |
| 775 |
data.debugBuffer = builder.declareRenderPass(data.debugBuffer); |
| 776 |
}, |
| 777 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 778 |
FrameGraphTexture const& debugBuffer = resources.get(data.debugBuffer); |
| 779 |
FrameGraphTexture const& subresource = resources.get(data.subresource); |
| 780 |
EXPECT_TRUE((bool)debugBuffer.handle); |
| 781 |
EXPECT_TRUE((bool)subresource.handle); |
| 782 |
auto rp = resources.getRenderPassInfo(); |
| 783 |
EXPECT_TRUE((bool)rp.target); |
| 784 |
}); |
| 785 |
fg.present(debugPass->debugBuffer); |
| 786 |
fg.present(debugPass->out); |
| 787 |
|
| 788 |
fg.present(pass->output); |
| 789 |
|
| 790 |
EXPECT_TRUE(fg.isAcyclic()); |
| 791 |
|
| 792 |
fg.compile(); |
| 793 |
|
| 794 |
fg.execute(driverApi); |
| 795 |
} |
| 796 |
|
| 797 |
TEST_F(FrameGraphTest, SubResourcesRead) { |
| 798 |
|
| 799 |
struct UploadPassData { |
| 800 |
FrameGraphId<FrameGraphTexture> output; |
| 801 |
}; |
| 802 |
auto& uploadPass = fg.addPass<UploadPassData>("Upload pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 803 |
data.output = builder.create<FrameGraphTexture>("Color buffer", {.width=16, .height=32, .levels=4}); |
| 804 |
data.output = builder.write(data.output, FrameGraphTexture::Usage::UPLOADABLE); |
| 805 |
EXPECT_TRUE(fg.isValid(data.output)); |
| 806 |
}, |
| 807 |
[=](FrameGraphResources const& resources, auto const& data, |
| 808 |
backend::DriverApi& driver) { |
| 809 |
auto outputDesc = resources.getDescriptor(data.output); |
| 810 |
auto output = resources.get(data.output); |
| 811 |
EXPECT_TRUE(output.handle); |
| 812 |
EXPECT_EQ(resources.getUsage(data.output), |
| 813 |
FrameGraphTexture::Usage::UPLOADABLE | FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 814 |
}); |
| 815 |
|
| 816 |
struct BlitPassData { |
| 817 |
FrameGraphId<FrameGraphTexture> output; |
| 818 |
FrameGraphId<FrameGraphTexture> inputs[4]; |
| 819 |
}; |
| 820 |
auto& blitPass = fg.addPass<BlitPassData>("Blit pass", |
| 821 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 822 |
data.output = builder.create<FrameGraphTexture>("Output buffer", {.width=16, .height=32}); |
| 823 |
data.output = builder.declareRenderPass(data.output); |
| 824 |
for (int i = 0; i < 4; i++) { |
| 825 |
data.inputs[i] = builder.createSubresource(uploadPass->output, "Color mip", { .level=uint8_t(i) }); |
| 826 |
EXPECT_TRUE(fg.isValid(data.inputs[i])); |
| 827 |
data.inputs[i] = builder.read(data.inputs[i], FrameGraphTexture::Usage::COLOR_ATTACHMENT); |
| 828 |
EXPECT_TRUE(fg.isValid(data.inputs[i])); |
| 829 |
} |
| 830 |
}, |
| 831 |
[=](FrameGraphResources const& resources, auto const& data, backend::DriverApi& driver) { |
| 832 |
}); |
| 833 |
|
| 834 |
fg.present(blitPass->output); |
| 835 |
|
| 836 |
EXPECT_TRUE(fg.isAcyclic()); |
| 837 |
|
| 838 |
fg.compile(); |
| 839 |
|
| 840 |
fg.execute(driverApi); |
| 841 |
} |
| 842 |
|
| 843 |
TEST_F(FrameGraphTest, SubResourcesWriteRead) { |
| 844 |
|
| 845 |
struct UpstreamPassData { |
| 846 |
FrameGraphId<FrameGraphTexture> output; |
| 847 |
}; |
| 848 |
auto& upstreamPass = fg.addPass<UpstreamPassData>("Upstream pass", [&](FrameGraph::Builder& builder, auto& data) { |
| 849 |
data.output = builder.create<FrameGraphTexture>("Color buffer", {.width=16, .height=32, .levels=4}); |
| 850 |
data.output = builder.declareRenderPass(data.output); |
| 851 |
EXPECT_TRUE(fg.isValid(data.output)); |
| 852 |
}, |
| 853 |
[=](FrameGraphResources const& resources, auto const& data, |
| 854 |
backend::DriverApi& driver) { |
| 855 |
EXPECT_TRUE(resources.get(data.output).handle); |
| 856 |
EXPECT_TRUE(any(resources.getUsage(data.output) & FrameGraphTexture::Usage::COLOR_ATTACHMENT)); |
| 857 |
}); |
| 858 |
|
| 859 |
struct MipmapPassData { |
| 860 |
FrameGraphId<FrameGraphTexture> input; |
| 861 |
FrameGraphId<FrameGraphTexture> output; |
| 862 |
}; |
| 863 |
|
| 864 |
FrameGraphId<FrameGraphTexture> input = upstreamPass->output; |
| 865 |
for (int i = 1; i < 4; i++) { |
| 866 |
auto& mipmapPass = fg.addPass<MipmapPassData>("Mipmap pass", |
| 867 |
[&](FrameGraph::Builder& builder, auto& data) { |
| 868 |
data.input = builder.sample(input); |
| 869 |
data.output = builder.createSubresource(upstreamPass->output, "Mip level buffer", { .level=uint8_t(i) }); |
| 870 |
data.output = builder.declareRenderPass(data.output); |
| 871 |
EXPECT_TRUE(fg.isValid(data.output)); |
| 872 |
EXPECT_TRUE(fg.isValid(data.input)); |
| 873 |
}, |
| 874 |
[=](FrameGraphResources const& resources, auto const& data, |
| 875 |
backend::DriverApi& driver) { |
| 876 |
EXPECT_TRUE(resources.get(data.input).handle); |
| 877 |
EXPECT_TRUE(any(resources.getUsage(data.input) & FrameGraphTexture::Usage::SAMPLEABLE)); |
| 878 |
EXPECT_TRUE(resources.get(data.output).handle); |
| 879 |
EXPECT_TRUE(any(resources.getUsage(data.output) & FrameGraphTexture::Usage::COLOR_ATTACHMENT)); |
| 880 |
}); |
| 881 |
input = mipmapPass->output; |
| 882 |
} |
| 883 |
|
| 884 |
fg.present(upstreamPass->output); |
| 885 |
|
| 886 |
EXPECT_TRUE(fg.isAcyclic()); |
| 887 |
|
| 888 |
fg.compile(); |
| 889 |
|
| 890 |
fg.execute(driverApi); |
| 891 |
} |