Skip to content

Commit

Permalink
Pipeline Profiler [pipeline statistics].
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesky013 committed Oct 10, 2023
1 parent 269b839 commit 4ba04b8
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 39 deletions.
4 changes: 3 additions & 1 deletion native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,8 @@ cocos_source_files(
cocos/renderer/pipeline/custom/details/Utility.h
cocos/renderer/pipeline/profile/GPUTimeQuery.cpp
cocos/renderer/pipeline/profile/GPUTimeQuery.h
cocos/renderer/pipeline/profile/GPUStatisticsQuery.cpp
cocos/renderer/pipeline/profile/GPUStatisticsQuery.h
cocos/renderer/pipeline/profile/PipelineProfiler.cpp
cocos/renderer/pipeline/profile/PipelineProfiler.h
)
Expand Down Expand Up @@ -3354,7 +3356,7 @@ set(COCOS_SOURCE_LIST_EXCLUDE_GENRATED ${COCOS_SOURCE_LIST})
set(COCOS_GENERATED_LIST)
foreach(src IN LISTS COCOS_SOURCE_LIST_EXCLUDE_GENRATED)
get_source_file_property(IS_GENERATED ${src} GENERATED)
if(IS_GENERATED)
if(IS_GENERATED)
list(REMOVE_ITEM COCOS_SOURCE_LIST_EXCLUDE_GENRATED ${src})
list(APPEND COCOS_GENERATED_LIST ${src})
endif()
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-vulkan/VKGPUContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace {

constexpr uint32_t FORCE_MINOR_VERSION = 0; // 0 for default version, otherwise minorVersion = (FORCE_MINOR_VERSION - 1)

#define FORCE_ENABLE_VALIDATION 0
#define FORCE_ENABLE_VALIDATION 1
#define FORCE_DISABLE_VALIDATION 1

using ccstd::vector;
Expand Down
71 changes: 44 additions & 27 deletions native/cocos/renderer/pipeline/custom/NativeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include "details/GslUtils.h"
#include "details/Range.h"

#define CC_PIPELINE_PROFILER 1

namespace cc {

namespace render {
Expand Down Expand Up @@ -209,17 +211,6 @@ PersistentRenderPassAndFramebuffer& fetchOrCreateFramebuffer(
return iter->second;
}

std::string getPassName(const RenderGraph& g, RenderGraph::vertex_descriptor v) noexcept {
std::stringstream ss;
const auto &name = get(RenderGraph::NameTag{}, g, v);
if (name.empty()) {
ss << "GraphPass_" << v;
return ss.str();
}

return name.c_str();
}

struct RenderGraphFilter {
bool operator()(RenderGraph::vertex_descriptor u) const {
return validPasses->operator[](u);
Expand Down Expand Up @@ -1140,8 +1131,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
#if CC_DEBUG
ctx.cmdBuff->beginMarker(makeMarkerInfo(get(RenderGraph::NameTag{}, ctx.g, vertID).c_str(), RASTER_COLOR));
#endif
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);

#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.beginScope(ctx.cmdBuff, vertID);
#endif
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
if (!renderData.custom.empty()) {
const auto& passes = ctx.ppl->custom.renderPasses;
Expand Down Expand Up @@ -1182,8 +1174,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
#if CC_DEBUG
ctx.cmdBuff->insertMarker(makeMarkerInfo(get(RenderGraph::NameTag{}, ctx.g, vertID).c_str(), RASTER_COLOR));
#endif
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);

#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.beginScope(ctx.cmdBuff, vertID);
#endif
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
if (!renderData.custom.empty()) {
const auto& subpasses = ctx.ppl->custom.renderSubpasses;
Expand All @@ -1207,8 +1200,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
#if CC_DEBUG
ctx.cmdBuff->insertMarker(makeMarkerInfo(get(RenderGraph::NameTag{}, ctx.g, vertID).c_str(), RASTER_COLOR));
#endif
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);

#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.beginScope(ctx.cmdBuff, vertID);
#endif
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
if (!renderData.custom.empty()) {
const auto& subpasses = ctx.ppl->custom.computeSubpasses;
Expand All @@ -1227,8 +1221,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
#if CC_DEBUG
ctx.cmdBuff->beginMarker(makeMarkerInfo(get(RenderGraph::NameTag{}, ctx.g, vertID).c_str(), COMPUTE_COLOR));
#endif
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);

#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.beginScope(ctx.cmdBuff, vertID);
#endif
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
if (!renderData.custom.empty()) {
const auto& passes = ctx.ppl->custom.computePasses;
Expand Down Expand Up @@ -1380,8 +1375,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
#if CC_DEBUG
ctx.cmdBuff->beginMarker(makeMarkerInfo(get(RenderGraph::NameTag{}, ctx.g, vertID).c_str(), RENDER_QUEUE_COLOR));
#endif
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);

#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.beginScope(ctx.cmdBuff, vertID);
#endif
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
if (!renderData.custom.empty()) {
const auto& queues = ctx.ppl->custom.renderQueues;
Expand Down Expand Up @@ -1421,6 +1417,12 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
}
}
void begin(const Blit& blit, RenderGraph::vertex_descriptor vertID) const {
#if CC_DEBUG
ctx.cmdBuff->beginMarker(makeMarkerInfo(get(RenderGraph::NameTag{}, ctx.g, vertID).c_str(), RENDER_QUEUE_COLOR));
#endif
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.beginScope(ctx.cmdBuff, vertID);
#endif
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
if (!renderData.custom.empty()) {
const auto& commands = ctx.ppl->custom.renderCommands;
Expand Down Expand Up @@ -1503,8 +1505,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
ctx.cmdBuff->endRenderPass();
ctx.currentPass = nullptr;
ctx.currentPassLayoutID = LayoutGraphData::null_vertex();

ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.endScope(ctx.cmdBuff, vertID);
#endif
#if CC_DEBUG
ctx.cmdBuff->endMarker();
#endif
Expand All @@ -1524,7 +1527,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
std::ignore = vertID;
ctx.subpassIndex = 0;
// noop
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.endScope(ctx.cmdBuff, vertID);
#endif
}
void end(const ComputeSubpass& subpass, RenderGraph::vertex_descriptor vertID) const { // NOLINT(readability-convert-member-functions-to-static)
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
Expand All @@ -1540,7 +1545,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
std::ignore = subpass;
std::ignore = vertID;
// noop
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.endScope(ctx.cmdBuff, vertID);
#endif
}
void end(const ComputePass& pass, RenderGraph::vertex_descriptor vertID) const {
const auto& renderData = get(RenderGraph::DataTag{}, ctx.g, vertID);
Expand All @@ -1552,7 +1559,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
return;
}
}
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.endScope(ctx.cmdBuff, vertID);
#endif
#if CC_DEBUG
ctx.cmdBuff->endMarker();
#endif
Expand Down Expand Up @@ -1580,7 +1589,9 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
return;
}
}
ctx.context.pipelineProfiler.writeGpuTimeStamp(ctx.cmdBuff, vertID);
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.endScope(ctx.cmdBuff, vertID);
#endif
#if CC_DEBUG
ctx.cmdBuff->endMarker();
#endif
Expand All @@ -1598,6 +1609,12 @@ struct RenderGraphVisitor : boost::dfs_visitor<> {
return;
}
}
#if CC_PIPELINE_PROFILER
ctx.context.pipelineProfiler.endScope(ctx.cmdBuff, vertID);
#endif
#if CC_DEBUG
ctx.cmdBuff->endMarker();
#endif
std::ignore = pass;
}
void end(const Dispatch& pass, RenderGraph::vertex_descriptor vertID) const {
Expand Down
144 changes: 144 additions & 0 deletions native/cocos/renderer/pipeline/profile/GPUStatisticsQuery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/****************************************************************************
Copyright (c) 2023-2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#include "GPUStatisticsQuery.h"
#include "gfx-base/GFXDevice.h"
#include "base/TypeDef.h"

namespace cc::render {

static constexpr uint32_t MAX_FRAME_INFLIGHT = 2; // from device agent

namespace {

uint32_t getStatsCount(const gfx::PipelineStatisticFlags &flags) {
uint32_t res = 0;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::IA_VERTICES)) ++res;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::IA_PRIMITIVES)) ++res;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::VS_INVOCATIONS)) ++res;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::CLIP_INVOCATIONS)) ++res;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::CLIP_PRIMITIVES)) ++res;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::FS_INVOCATIONS)) ++res;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::CS_INVOCATIONS)) ++res;
return res;
}

} // namespace

const uint64_t* GPUStatisticsQuery::getReadBuffer() const {
const auto* ptr = reinterpret_cast<const uint8_t *>(_results.data());
const uint64_t readIndex = (_frameIndex + MAX_FRAME_INFLIGHT - 1) % MAX_FRAME_INFLIGHT;
return reinterpret_cast<const uint64_t *>(ptr + readIndex * _dataSize);
}

GPUPipelineStats GPUStatisticsQuery::resolveData(uint32_t id) const {
GPUPipelineStats stats = {};

const uint64_t *ptr = getReadBuffer() + id * _dataCount;
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::IA_VERTICES)) {
stats.iaVertices = *ptr;
ptr++;
}
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::IA_PRIMITIVES)) {
stats.iaPrimitives = *ptr;
ptr++;
}
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::VS_INVOCATIONS)) {
stats.vsInvocations = *ptr;
ptr++;
}
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::CLIP_INVOCATIONS)) {
stats.clipInvocations = *ptr;
ptr++;
}
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::CLIP_PRIMITIVES)) {
stats.clipPrimitives = *ptr;
ptr++;
}
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::FS_INVOCATIONS)) {
stats.fsInvocations = *ptr;
ptr++;
}
if (hasFlag(flags, gfx::PipelineStatisticFlagBit::CS_INVOCATIONS)) {
stats.csInvocations = *ptr;
}
return stats;
}

void GPUStatisticsQuery::resize(uint32_t size) {
if (size <= _capacity) {
return;
}

_capacity = size;

auto *device = gfx::Device::getInstance();

device->getSupportedPipelineStatisticFlags(gfx::PipelineStatisticFlagBit::ALL, flags);
_dataCount = getStatsCount(flags);
_dataSize = _capacity * _dataCount * sizeof(uint64_t);

gfx::QueryPoolInfo poolInfo = {};
poolInfo.type = gfx::QueryType::PIPELINE_STATISTICS;
poolInfo.maxQueryObjects = _capacity;
poolInfo.pipelineStatisticFlags = flags;

_queryPool = device->createQueryPool(poolInfo);

gfx::BufferInfo bufferInfo = {};
bufferInfo.usage = gfx::BufferUsageBit::TRANSFER_DST;
bufferInfo.memUsage = gfx::MemoryUsageBit::HOST | gfx::MemoryUsageBit::DEVICE;
bufferInfo.stride = _dataCount * sizeof(uint64_t);
bufferInfo.size = _dataSize;

_results.resize(bufferInfo.size * MAX_FRAME_INFLIGHT);
_readBackBuffer = device->createBuffer(bufferInfo);
}

void GPUStatisticsQuery::reset(gfx::CommandBuffer *cmdBuffer) {
cmdBuffer->resetQueryPool(_queryPool, 0, _capacity);
_count = 0;
idMap.clear();
}

void GPUStatisticsQuery::begin(gfx::CommandBuffer *cmdBuffer, uint32_t key) {
idMap[key] = _count;
cmdBuffer->beginQuery(_queryPool, _count);
++_count;
}

void GPUStatisticsQuery::end(gfx::CommandBuffer *cmdBuffer, uint32_t key) {
cmdBuffer->endQuery(_queryPool, idMap[key]);
}

void GPUStatisticsQuery::copyResult(gfx::CommandBuffer *cmdBuffer) {
cmdBuffer->copyQueryResult(_queryPool, _readBackBuffer, 0, _dataCount * sizeof(uint64_t), 0, _count);
_frameIndex = (_frameIndex + 1) % MAX_FRAME_INFLIGHT;

auto* ptr = reinterpret_cast<uint8_t *>(_results.data());
uint8_t* writeBuffer = ptr + static_cast<uint64_t>(_frameIndex) * _dataSize;
_readBackBuffer->readBack(writeBuffer, 0, _dataSize);
}

} // namespace cc::render
Loading

0 comments on commit 4ba04b8

Please sign in to comment.