Revert "use hash"

This reverts commit 8ee32e733e.
This commit is contained in:
troublemaker52025
2026-08-20 10:18:20 +08:00
parent 32ce04e333
commit 6c156997c2
3 changed files with 17 additions and 34 deletions
+5 -17
View File
@@ -39,14 +39,6 @@ export function instancingCompareFn (l: InstancedBuffer, r: InstancedBuffer): nu
return (ls.hash - rs.hash) || (ls.shaderId - rs.shaderId);
}
// Combines a running hash `seed` with `value` into a single 32-bit integer.
// Mirrors the native `ccstd::hash_combine` formula so merge keys can be plain
// numbers instead of formatted strings, avoiding per-merge string allocation.
function hashCombine (seed: number, value: number): number {
// eslint-disable-next-line no-bitwise
return (seed ^ (value + 0x9e3779b9 + (seed << 6) + (seed >> 2))) | 0;
}
export interface IInstancedItem {
count: number;
capacity: number;
@@ -74,7 +66,7 @@ export class InstancedBuffer {
public sortRender: IRenderPass;
private declare _passPool: RecyclePool<IRenderPass>;
private declare _device: Device;
private readonly _instanceMap = new Map<number, IInstancedItem[]>();
private readonly _instanceMap = new Map<string, IInstancedItem[]>();
constructor (pass: Pass) {
this._device = pass.device;
this.pass = pass;
@@ -118,13 +110,9 @@ export class InstancedBuffer {
this.sortRender.shaderId = shader.typedID;
this.sortRender.passIdx = passIdx;
let key = hashCombine(2, sourceIA.indexBuffer?.objectID ?? 0);
key = hashCombine(key, lightingMap.objectID);
key = hashCombine(key, useReflectionProbeType);
key = hashCombine(key, reflectionProbeCubemap.objectID);
key = hashCombine(key, reflectionProbePlanarMap.objectID);
key = hashCombine(key, ENABLE_PROBE_BLEND ? reflectionProbeBlendCubemap!.objectID : 0);
key = hashCombine(key, stride);
const key = `${sourceIA.indexBuffer?.objectID ?? 0}/${lightingMap.objectID}/${useReflectionProbeType}/`
+ `${reflectionProbeCubemap.objectID}/${reflectionProbePlanarMap.objectID}/`
+ `${ENABLE_PROBE_BLEND ? reflectionProbeBlendCubemap!.objectID : 0}/${stride}`;
const mappedInstances = this._instanceMap.get(key);
if (mappedInstances) {
for (let i = 0; i < mappedInstances.length; ++i) {
@@ -167,7 +155,7 @@ export class InstancedBuffer {
}
private _createInstance (
key: number,
key: string,
sourceIA: InputAssembler,
instancedAttributes: Attribute[],
instanceData: Uint8Array,
@@ -31,6 +31,7 @@
#include "gfx-base/GFXDescriptorSet.h"
#include "gfx-base/GFXDevice.h"
#include "gfx-base/GFXInputAssembler.h"
#include "base/StringUtil.h"
namespace cc {
namespace pipeline {
@@ -84,17 +85,14 @@ void InstancedBuffer::merge(scene::SubModel *subModel, uint32_t passIdx, gfx::Sh
_sortRender.hash = hash;
_sortRender.shaderID = shaderId;
_sortRender.passIndex = passIdx;
const ccstd::hash_t key = [&]() {
ccstd::hash_t seed = 2;
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(sourceIA->getIndexBuffer()));
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(lightingMap));
ccstd::hash_combine(seed, reflectionProbeType);
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(reflectionProbeCubemap));
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(reflectionProbePlanarMap));
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(reflectionProbeBlendCubemap));
ccstd::hash_combine(seed, stride);
return seed;
}();
const ccstd::string key = StringUtil::format("%u/%u/%u/%u/%u/%u/%u",
sourceIA->getIndexBuffer() ? sourceIA->getIndexBuffer()->getObjectID() : 0,
lightingMap ? lightingMap->getObjectID() : 0,
reflectionProbeType,
reflectionProbeCubemap ? reflectionProbeCubemap->getObjectID() : 0,
reflectionProbePlanarMap ? reflectionProbePlanarMap->getObjectID() : 0,
reflectionProbeBlendCubemap ? reflectionProbeBlendCubemap->getObjectID() : 0,
stride);
const auto iter = _instancesMap.find(key);
if (iter != _instancesMap.end()) {
for (size_t idx : iter->second) {
@@ -147,7 +145,7 @@ void InstancedBuffer::appendInstance(InstancedItem &instance,
_hasPendingModels = true;
}
void InstancedBuffer::createInstance(ccstd::hash_t key,
void InstancedBuffer::createInstance(const ccstd::string &key,
gfx::InputAssembler *sourceIA,
const ccstd::vector<gfx::Attribute> &attributes,
Uint8Array &buffer,
@@ -27,7 +27,6 @@
#include "Define.h"
#include "base/RefCounted.h"
#include "base/std/container/unordered_map.h"
#include "base/std/hash/hash.h"
#include "scene/Model.h"
#include "scene/Pass.h"
@@ -83,7 +82,7 @@ private:
Uint8Array &buffer,
gfx::Shader *shader,
gfx::DescriptorSet *descriptorSet);
void createInstance(ccstd::hash_t key,
void createInstance(const ccstd::string &key,
gfx::InputAssembler *sourceIA,
const ccstd::vector<gfx::Attribute> &attributes,
Uint8Array &buffer,
@@ -109,9 +108,7 @@ private:
// Maps a merge key to the indices (into `_instances`) of items sharing that key.
// Storing indices (not copies or references) keeps this in sync with `_instances`
// and remains valid across `_instances` reallocation, since indices don't dangle.
// The key is a combined hash (see `ccstd::hash_combine`) instead of a formatted
// string, avoiding per-merge string allocation/formatting overhead.
ccstd::unordered_map<ccstd::hash_t, ccstd::vector<size_t>> _instancesMap;
ccstd::unordered_map<ccstd::string, ccstd::vector<size_t>> _instancesMap;
};
} // namespace pipeline