Skip to content

Commit

Permalink
[Depends on #8332] Integrate fgviewer to the engine side (#8344)
Browse files Browse the repository at this point in the history
* Introduce FrameGraphInfo class

* Move the assignment into pimpl

* Make ctors explicit

* Add ctors to fg info structs

* Revert the macro change to align with existing

* Address the comments

* Remove pimpl and move func def to .cc

* Fix

* Convert the FrameGraph to FrameGraphInfo

* Initialize and update debug server on engine side

* Fix compile error

* Address the comments

* Update

* Use camelCase

* Use camelCase

* Update
  • Loading branch information
show50726 authored Jan 31, 2025
1 parent 9a2ca8d commit 793f2b4
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 2 deletions.
19 changes: 19 additions & 0 deletions filament/src/details/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,25 @@ int FEngine::loop() {
}
#endif

#if FILAMENT_ENABLE_FGVIEWER
#ifdef __ANDROID__
const char* fgviewerPortString = "8085";
#else
const char* fgviewerPortString = getenv("FILAMENT_FGVIEWER_PORT");
#endif
if (fgviewerPortString != nullptr) {
const int fgviewerPort = atoi(fgviewerPortString);
debug.fgviewerServer = new fgviewer::DebugServer(fgviewerPort);

// Sometimes the server can fail to spin up (e.g. if the above port is already in use).
// When this occurs, carry onward, developers can look at civetweb.txt for details.
if (!debug.fgviewerServer->isReady()) {
delete debug.fgviewerServer;
debug.fgviewerServer = nullptr;
}
}
#endif

while (true) {
if (!execute()) {
break;
Expand Down
9 changes: 9 additions & 0 deletions filament/src/details/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ using MaterialKey = uint32_t;
} // namespace filament::matdbg
#endif

#if FILAMENT_ENABLE_FGVIEWER
#include <fgviewer/DebugServer.h>
#else
namespace filament::fgviewer {
class DebugServer;
} // namespace filament::fgviewer
#endif

namespace filament {

class Renderer;
Expand Down Expand Up @@ -673,6 +681,7 @@ class FEngine : public Engine {
bool combine_multiview_images = false;
} stereo;
matdbg::DebugServer* server = nullptr;
fgviewer::DebugServer* fgviewerServer = nullptr;
} debug;

struct {
Expand Down
7 changes: 7 additions & 0 deletions filament/src/details/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,13 @@ void FRenderer::renderJob(RootArenaScope& rootArenaScope, FView& view) {

fg.compile();

#if FILAMENT_ENABLE_FGVIEWER
fgviewer::DebugServer* fgviewerServer = engine.debug.fgviewerServer;
if (UTILS_LIKELY(fgviewerServer)) {
fgviewerServer->update(view.getViewHandle(), fg.getFrameGraphInfo(view.getName()));
}
#endif

//fg.export_graphviz(slog.d, view.getName());

fg.execute(driver);
Expand Down
15 changes: 15 additions & 0 deletions filament/src/details/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ FView::FView(FEngine& engine)
}
#endif

#if FILAMENT_ENABLE_FGVIEWER
fgviewer::DebugServer* fgviewerServer = engine.debug.fgviewerServer;
if (UTILS_LIKELY(fgviewerServer)) {
mFrameGraphViewerViewHandle =
fgviewerServer->createView(utils::CString(getName()));
}
#endif

// allocate UBOs
mLightUbh = driver.createBufferObject(CONFIG_MAX_LIGHT_COUNT * sizeof(LightsUib),
BufferObjectBinding::UNIFORM, BufferUsage::DYNAMIC);
Expand Down Expand Up @@ -153,6 +161,13 @@ void FView::terminate(FEngine& engine) {
engine.getDebugRegistry().unregisterDataSource("d.view.frame_info");
}
#endif

#if FILAMENT_ENABLE_FGVIEWER
fgviewer::DebugServer* fgviewerServer = engine.debug.fgviewerServer;
if (UTILS_LIKELY(fgviewerServer)) {
fgviewerServer->destroyView(mFrameGraphViewerViewHandle);
}
#endif
}

void FView::setViewport(filament::Viewport const& viewport) noexcept {
Expand Down
14 changes: 14 additions & 0 deletions filament/src/details/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
#include <utils/Range.h>
#include <utils/Slice.h>

#if FILAMENT_ENABLE_FGVIEWER
#include <fgviewer/DebugServer.h>
#else
namespace filament::fgviewer {
using ViewHandle = uint32_t;
}
#endif

#include <math/scalar.h>
#include <math/mat4.h>

Expand Down Expand Up @@ -467,6 +475,10 @@ class FView : public View {
return mUniforms;
}

fgviewer::ViewHandle getViewHandle() const noexcept {
return mFrameGraphViewerViewHandle;
}

private:
struct FPickingQuery : public PickingQuery {
private:
Expand Down Expand Up @@ -600,6 +612,8 @@ class FView : public View {
{ 0, 0, 0, 1 },
}};

fgviewer::ViewHandle mFrameGraphViewerViewHandle;

#ifndef NDEBUG
struct DebugState {
std::unique_ptr<std::array<DebugRegistry::FrameHistory, 5*60>> debugFrameHistory{};
Expand Down
55 changes: 55 additions & 0 deletions filament/src/fg/FrameGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,61 @@ void FrameGraph::export_graphviz(utils::io::ostream& out, char const* name) {
mGraph.export_graphviz(out, name);
}

fgviewer::FrameGraphInfo FrameGraph::getFrameGraphInfo(const char *viewName) const {
#if FILAMENT_ENABLE_FGVIEWER
fgviewer::FrameGraphInfo info{utils::CString(viewName)};
std::vector<fgviewer::FrameGraphInfo::Pass> passes;

auto first = mPassNodes.begin();
const auto activePassNodesEnd = mActivePassNodesEnd;
while (first != activePassNodesEnd) {
PassNode *const pass = *first;
first++;

assert_invariant(!pass->isCulled());
std::vector<fgviewer::ResourceId> reads;
auto const &readEdges = mGraph.getIncomingEdges(pass);
for (auto const &edge: readEdges) {
// all incoming edges should be valid by construction
assert_invariant(mGraph.isEdgeValid(edge));
reads.push_back(edge->from);
}

std::vector<fgviewer::ResourceId> writes;
auto const &writeEdges = mGraph.getOutgoingEdges(pass);
for (auto const &edge: writeEdges) {
// It is possible that the node we're writing to has been culled.
// In this case we'd like to ignore the edge.
if (!mGraph.isEdgeValid(edge)) {
continue;
}
writes.push_back(edge->to);
}
passes.emplace_back(utils::CString(pass->getName()),
std::move(reads), std::move(writes));
}

std::unordered_map<fgviewer::ResourceId, fgviewer::FrameGraphInfo::Resource> resources;
for (const auto &resource: mResourceNodes) {
std::vector<fgviewer::FrameGraphInfo::Resource::Property> resourceProps;
// TODO: Fill in resource properties
fgviewer::ResourceId id = resource->getId();
resources.emplace(id, fgviewer::FrameGraphInfo::Resource(
id, utils::CString(resource->getName()),
std::move(resourceProps))
);
}

info.setResources(std::move(resources));
info.setPasses(std::move(passes));

return info;
#else
return fgviewer::FrameGraphInfo();
#endif
}


// ------------------------------------------------------------------------------------------------

/*
Expand Down
14 changes: 14 additions & 0 deletions filament/src/fg/FrameGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@

#include <functional>

#if FILAMENT_ENABLE_FGVIEWER
#include <fgviewer/FrameGraphInfo.h>
#else
namespace filament::fgviewer {
class FrameGraphInfo{};
} // namespace filament::fgviewer
#endif

namespace filament {

class ResourceAllocatorInterface;
Expand Down Expand Up @@ -434,6 +442,12 @@ class FrameGraph {
//! export a graphviz view of the graph
void export_graphviz(utils::io::ostream& out, const char* name = nullptr);

/**
* Export a fgviewer::FrameGraphInfo for current graph.
* Note that this function should be called after FrameGraph::compile().
*/
fgviewer::FrameGraphInfo getFrameGraphInfo(const char *viewName) const;

private:
friend class FrameGraphResources;
friend class PassNode;
Expand Down
2 changes: 0 additions & 2 deletions libs/fgviewer/include/fgviewer/DebugServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ namespace filament::fgviewer {

using ViewHandle = uint32_t;



/**
* Server-side frame graph debugger.
*
Expand Down

0 comments on commit 793f2b4

Please sign in to comment.