Skip to content

Commit 3d42d5e

Browse files
Merge branch 'findSimilarInstancesOpt' into 'main'
[REMIX-3744] Small optimization to early out on exact transform match. See merge request lightspeedrtx/dxvk-remix-nv!1185
2 parents b4ca5f6 + fd7a965 commit 3d42d5e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/dxvk/rtx_render/rtx_instance_manager.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -652,17 +652,24 @@ namespace dxvk {
652652

653653
// Search the BLAS for an instance matching ours
654654
{
655-
const auto adjacentCells = blas.getSpatialMap().getDataNearPos(worldPosition);
656-
for (const std::vector<const RtInstance*>* cellPtr : adjacentCells){
657-
for (const RtInstance* instance : *cellPtr) {
658-
if (instance->m_frameLastUpdated == currentFrameIdx) {
659-
// If the transform is an exact match and the instance has already been touched this frame,
660-
// then this is a second draw call on a single mesh.
655+
// Search for an exact match
656+
const std::vector<const RtInstance*>* matchingCell = blas.getSpatialMap().getDataAtPos(worldPosition);
657+
if (matchingCell != nullptr) {
658+
for (const RtInstance* instance : *matchingCell) {
661659
const Matrix4 instanceTransform = instance->getTransform();
660+
// Note: this may be the second call this frame targetting the same instance.
662661
if (memcmp(&transform, &instanceTransform, sizeof(instanceTransform)) == 0) {
663662
return const_cast<RtInstance*>(instance);
664663
}
665-
} else if (instance->m_materialHash == material.getHash()) {
664+
}
665+
}
666+
667+
// No exact match, so find the closest match in the region
668+
// (need to check a 2x2x2 patch of cells to account for positions close to a border)
669+
const auto adjacentCells = blas.getSpatialMap().getDataNearPos(worldPosition);
670+
for (const std::vector<const RtInstance*>* cellPtr : adjacentCells){
671+
for (const RtInstance* instance : *cellPtr) {
672+
if (instance->m_frameLastUpdated != currentFrameIdx && instance->m_materialHash == material.getHash()) {
666673
// Instance hasn't been touched yet this frame.
667674

668675
const Vector3& prevInstanceWorldPosition = instance->getSpatialCachePosition();

src/util/util_spatial_map.h

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ namespace dxvk {
4545
return *this;
4646
}
4747

48+
// returns the cell closest to `position`
49+
const std::vector<T>* getDataAtPos(const Vector3& position) const {
50+
auto pair = m_cache.find(getCellPos(position));
51+
if ( pair != m_cache.end()) {
52+
return &pair->second;
53+
}
54+
return nullptr;
55+
}
56+
4857
// returns the 8 cells closest to `position`
4958
const std::vector<const std::vector<T>*> getDataNearPos(const Vector3& position) const {
5059
static const std::array kOffsets{

0 commit comments

Comments
 (0)