Skip to content

Commit 6c15699

Browse files
Revert "use hash"
This reverts commit 8ee32e7.
1 parent 32ce04e commit 6c15699

3 files changed

Lines changed: 17 additions & 34 deletions

File tree

cocos/rendering/instanced-buffer.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@ export function instancingCompareFn (l: InstancedBuffer, r: InstancedBuffer): nu
3939
return (ls.hash - rs.hash) || (ls.shaderId - rs.shaderId);
4040
}
4141

42-
// Combines a running hash `seed` with `value` into a single 32-bit integer.
43-
// Mirrors the native `ccstd::hash_combine` formula so merge keys can be plain
44-
// numbers instead of formatted strings, avoiding per-merge string allocation.
45-
function hashCombine (seed: number, value: number): number {
46-
// eslint-disable-next-line no-bitwise
47-
return (seed ^ (value + 0x9e3779b9 + (seed << 6) + (seed >> 2))) | 0;
48-
}
49-
5042
export interface IInstancedItem {
5143
count: number;
5244
capacity: number;
@@ -74,7 +66,7 @@ export class InstancedBuffer {
7466
public sortRender: IRenderPass;
7567
private declare _passPool: RecyclePool<IRenderPass>;
7668
private declare _device: Device;
77-
private readonly _instanceMap = new Map<number, IInstancedItem[]>();
69+
private readonly _instanceMap = new Map<string, IInstancedItem[]>();
7870
constructor (pass: Pass) {
7971
this._device = pass.device;
8072
this.pass = pass;
@@ -118,13 +110,9 @@ export class InstancedBuffer {
118110
this.sortRender.shaderId = shader.typedID;
119111
this.sortRender.passIdx = passIdx;
120112

121-
let key = hashCombine(2, sourceIA.indexBuffer?.objectID ?? 0);
122-
key = hashCombine(key, lightingMap.objectID);
123-
key = hashCombine(key, useReflectionProbeType);
124-
key = hashCombine(key, reflectionProbeCubemap.objectID);
125-
key = hashCombine(key, reflectionProbePlanarMap.objectID);
126-
key = hashCombine(key, ENABLE_PROBE_BLEND ? reflectionProbeBlendCubemap!.objectID : 0);
127-
key = hashCombine(key, stride);
113+
const key = `${sourceIA.indexBuffer?.objectID ?? 0}/${lightingMap.objectID}/${useReflectionProbeType}/`
114+
+ `${reflectionProbeCubemap.objectID}/${reflectionProbePlanarMap.objectID}/`
115+
+ `${ENABLE_PROBE_BLEND ? reflectionProbeBlendCubemap!.objectID : 0}/${stride}`;
128116
const mappedInstances = this._instanceMap.get(key);
129117
if (mappedInstances) {
130118
for (let i = 0; i < mappedInstances.length; ++i) {
@@ -167,7 +155,7 @@ export class InstancedBuffer {
167155
}
168156

169157
private _createInstance (
170-
key: number,
158+
key: string,
171159
sourceIA: InputAssembler,
172160
instancedAttributes: Attribute[],
173161
instanceData: Uint8Array,

native/cocos/renderer/pipeline/InstancedBuffer.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "gfx-base/GFXDescriptorSet.h"
3232
#include "gfx-base/GFXDevice.h"
3333
#include "gfx-base/GFXInputAssembler.h"
34+
#include "base/StringUtil.h"
3435

3536
namespace cc {
3637
namespace pipeline {
@@ -84,17 +85,14 @@ void InstancedBuffer::merge(scene::SubModel *subModel, uint32_t passIdx, gfx::Sh
8485
_sortRender.hash = hash;
8586
_sortRender.shaderID = shaderId;
8687
_sortRender.passIndex = passIdx;
87-
const ccstd::hash_t key = [&]() {
88-
ccstd::hash_t seed = 2;
89-
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(sourceIA->getIndexBuffer()));
90-
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(lightingMap));
91-
ccstd::hash_combine(seed, reflectionProbeType);
92-
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(reflectionProbeCubemap));
93-
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(reflectionProbePlanarMap));
94-
ccstd::hash_combine(seed, gfx::GFXObject::getObjectID(reflectionProbeBlendCubemap));
95-
ccstd::hash_combine(seed, stride);
96-
return seed;
97-
}();
88+
const ccstd::string key = StringUtil::format("%u/%u/%u/%u/%u/%u/%u",
89+
sourceIA->getIndexBuffer() ? sourceIA->getIndexBuffer()->getObjectID() : 0,
90+
lightingMap ? lightingMap->getObjectID() : 0,
91+
reflectionProbeType,
92+
reflectionProbeCubemap ? reflectionProbeCubemap->getObjectID() : 0,
93+
reflectionProbePlanarMap ? reflectionProbePlanarMap->getObjectID() : 0,
94+
reflectionProbeBlendCubemap ? reflectionProbeBlendCubemap->getObjectID() : 0,
95+
stride);
9896
const auto iter = _instancesMap.find(key);
9997
if (iter != _instancesMap.end()) {
10098
for (size_t idx : iter->second) {
@@ -147,7 +145,7 @@ void InstancedBuffer::appendInstance(InstancedItem &instance,
147145
_hasPendingModels = true;
148146
}
149147

150-
void InstancedBuffer::createInstance(ccstd::hash_t key,
148+
void InstancedBuffer::createInstance(const ccstd::string &key,
151149
gfx::InputAssembler *sourceIA,
152150
const ccstd::vector<gfx::Attribute> &attributes,
153151
Uint8Array &buffer,

native/cocos/renderer/pipeline/InstancedBuffer.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "Define.h"
2828
#include "base/RefCounted.h"
2929
#include "base/std/container/unordered_map.h"
30-
#include "base/std/hash/hash.h"
3130
#include "scene/Model.h"
3231
#include "scene/Pass.h"
3332

@@ -83,7 +82,7 @@ class InstancedBuffer : public RefCounted {
8382
Uint8Array &buffer,
8483
gfx::Shader *shader,
8584
gfx::DescriptorSet *descriptorSet);
86-
void createInstance(ccstd::hash_t key,
85+
void createInstance(const ccstd::string &key,
8786
gfx::InputAssembler *sourceIA,
8887
const ccstd::vector<gfx::Attribute> &attributes,
8988
Uint8Array &buffer,
@@ -109,9 +108,7 @@ class InstancedBuffer : public RefCounted {
109108
// Maps a merge key to the indices (into `_instances`) of items sharing that key.
110109
// Storing indices (not copies or references) keeps this in sync with `_instances`
111110
// and remains valid across `_instances` reallocation, since indices don't dangle.
112-
// The key is a combined hash (see `ccstd::hash_combine`) instead of a formatted
113-
// string, avoiding per-merge string allocation/formatting overhead.
114-
ccstd::unordered_map<ccstd::hash_t, ccstd::vector<size_t>> _instancesMap;
111+
ccstd::unordered_map<ccstd::string, ccstd::vector<size_t>> _instancesMap;
115112
};
116113

117114
} // namespace pipeline

0 commit comments

Comments
 (0)