mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
perf(pipeline): reduce render queue sort to two packed key compares (native platform).
This commit is contained in:
@@ -149,6 +149,23 @@ CC_FORCE_INLINE Tgt bit_cast(const Src &src) { // NOLINT(readability-identifier-
|
||||
return tgt;
|
||||
}
|
||||
|
||||
// Map a float to a uint32 whose unsigned-integer ordering matches the float's
|
||||
// numeric ordering (the classic radix-sort "flip"):
|
||||
// * non-negative: flip the sign bit
|
||||
// * negative: complement all bits
|
||||
// This maps [-inf, -0.0] to [0, 2^31) and [+0.0, +inf] to [2^31, 2^32), so the
|
||||
// unsigned comparison orders floats correctly. NaN must be excluded by the caller.
|
||||
//
|
||||
// Requires IEEE 754 binary32 (32-bit) floats, which every Cocos platform has.
|
||||
static_assert(sizeof(float) == sizeof(uint32_t), "float must be 32-bit");
|
||||
static_assert(std::numeric_limits<float>::is_iec559, "float must be IEEE 754");
|
||||
|
||||
CC_FORCE_INLINE uint32_t floatToSortableUint(float value) {
|
||||
constexpr uint32_t signBit = 0x80000000U;
|
||||
const auto bits = bit_cast<uint32_t>(value);
|
||||
return (bits & signBit) ? ~bits : (bits ^ signBit);
|
||||
}
|
||||
|
||||
} // namespace numext
|
||||
|
||||
// Following the Arm ACLE arm_neon.h should also include arm_fp16.h but not all
|
||||
@@ -160,7 +177,7 @@ CC_FORCE_INLINE Tgt bit_cast(const Src &src) { // NOLINT(readability-identifier-
|
||||
|
||||
// Code from https://gitlab.com/libeigen/eigen/-/blob/master/Eigen/src/Core/arch/Default/Half.h#L586
|
||||
struct HalfRaw {
|
||||
constexpr HalfRaw() : x(0) {}
|
||||
constexpr HalfRaw() : x(0) {} // NOLINT(modernize-use-default-member-init)
|
||||
#if defined(CC_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
||||
explicit HalfRaw(uint16_t raw) : x(numext::bit_cast<__fp16>(raw)) {
|
||||
}
|
||||
|
||||
@@ -73,6 +73,15 @@ struct CC_DLL RenderPass {
|
||||
uint32_t hash = 0;
|
||||
float depth = 0.0F;
|
||||
uint32_t shaderID = 0;
|
||||
/**
|
||||
* Packed sort keys, computed once in RenderQueue::insertRenderPass so the
|
||||
* comparator degenerates to a single uint64 compare.
|
||||
* opaque: primaryKey = hash, secondaryKey = depthKey << 32 | shaderID
|
||||
* transparent: primaryKey = priority << 32 | hash, secondaryKey = ~depthKey << 32 | shaderID
|
||||
* depthKey is the order-preserving uint32 mapping of depth.
|
||||
*/
|
||||
uint64_t primaryKey = 0;
|
||||
uint64_t secondaryKey = 0;
|
||||
uint32_t passIndex = 0;
|
||||
const scene::SubModel *subModel = nullptr;
|
||||
};
|
||||
@@ -175,35 +184,17 @@ using RenderQueueDescList = ccstd::vector<IntrusivePtr<RenderQueueDesc>>;
|
||||
uint32_t getPhaseID(const ccstd::string &phase);
|
||||
|
||||
inline bool opaqueCompareFn(const RenderPass &a, const RenderPass &b) {
|
||||
if (a.hash != b.hash) {
|
||||
return a.hash < b.hash;
|
||||
if (a.primaryKey != b.primaryKey) {
|
||||
return a.primaryKey < b.primaryKey;
|
||||
}
|
||||
|
||||
CC_ASSERT(!std::isnan(a.depth) && !std::isnan(b.depth));
|
||||
|
||||
if (a.depth != b.depth) {
|
||||
return a.depth < b.depth;
|
||||
}
|
||||
|
||||
return a.shaderID < b.shaderID;
|
||||
return a.secondaryKey < b.secondaryKey;
|
||||
}
|
||||
|
||||
inline bool transparentCompareFn(const RenderPass &a, const RenderPass &b) {
|
||||
if (a.priority != b.priority) {
|
||||
return a.priority < b.priority;
|
||||
if (a.primaryKey != b.primaryKey) {
|
||||
return a.primaryKey < b.primaryKey;
|
||||
}
|
||||
|
||||
if (a.hash != b.hash) {
|
||||
return a.hash < b.hash;
|
||||
}
|
||||
|
||||
CC_ASSERT(!std::isnan(a.depth) && !std::isnan(b.depth));
|
||||
|
||||
if (a.depth != b.depth) {
|
||||
return b.depth < a.depth;
|
||||
}
|
||||
|
||||
return a.shaderID < b.shaderID;
|
||||
return a.secondaryKey < b.secondaryKey;
|
||||
}
|
||||
|
||||
inline uint32_t convertPhase(const ccstd::vector<ccstd::string> &stages) {
|
||||
|
||||
@@ -25,6 +25,8 @@
|
||||
#include "RenderQueue.h"
|
||||
|
||||
#include <utility>
|
||||
#include <cmath>
|
||||
#include "base/Utils.h"
|
||||
#include "PipelineSceneData.h"
|
||||
#include "PipelineStateManager.h"
|
||||
#include "RenderPipeline.h"
|
||||
@@ -60,7 +62,28 @@ bool RenderQueue::insertRenderPass(const RenderObject &renderObj, uint32_t subMo
|
||||
auto shaderId = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(subModel->getShader(passIdx)));
|
||||
const auto hash = (0 << 30) | (passPriority << 16) | (modelPriority << 8) | passIdx;
|
||||
const auto priority = renderObj.model->getPriority();
|
||||
RenderPass renderPass = {priority, hash, renderObj.depth, shaderId, passIdx, subModel};
|
||||
RenderPass renderPass{};
|
||||
renderPass.priority = priority;
|
||||
renderPass.hash = hash;
|
||||
renderPass.depth = renderObj.depth;
|
||||
renderPass.shaderID = shaderId;
|
||||
renderPass.passIndex = passIdx;
|
||||
renderPass.subModel = subModel;
|
||||
|
||||
/**
|
||||
* Pack the sort keys once here so the comparator only compares uint64 keys.
|
||||
* secondaryKey: depthKey << 32 | shaderID (transparent inverts depth for back-to-front).
|
||||
*/
|
||||
CC_ASSERT(!std::isnan(renderObj.depth));
|
||||
const uint32_t depthKey = utils::numext::floatToSortableUint(renderObj.depth);
|
||||
if (isTransparent) {
|
||||
const uint32_t invertedDepthKey = ~depthKey;
|
||||
renderPass.primaryKey = (static_cast<uint64_t>(priority) << 32) | hash;
|
||||
renderPass.secondaryKey = (static_cast<uint64_t>(invertedDepthKey) << 32) | shaderId;
|
||||
} else {
|
||||
renderPass.primaryKey = hash;
|
||||
renderPass.secondaryKey = (static_cast<uint64_t>(depthKey) << 32) | shaderId;
|
||||
}
|
||||
_queue.emplace_back(renderPass);
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user