Skip to content

fix(rendering): use lightIdx instead of loop index l in GPU instancing forward-add light path - #19185

Closed
UC-RX0 wants to merge 1 commit into
cocos:v3.8.8from
UC-RX0:fix/instancing-light-index
Closed

fix(rendering): use lightIdx instead of loop index l in GPU instancing forward-add light path#19185
UC-RX0 wants to merge 1 commit into
cocos:v3.8.8from
UC-RX0:fix/instancing-light-index

Conversation

@UC-RX0

@UC-RX0 UC-RX0 commented Jun 26, 2026

Copy link
Copy Markdown

Problem

In _addRenderQueue of render-additive-light-queue.ts, the BatchingSchemes.INSTANCING branch was using the local loop index l for getInstancedBuffer(l) and _instancedQueues[l] instead of lightIdx (the global light index from validPunctualLights).

This caused mismatched indexing between:

  • The instancing path (used local per-model light indices l)
  • recordCommandBuffer / gatherLightPasses (use global light indices lightIdx)

Result: wrong UBO/descriptor bindings for additional lights when different models have different light culling results under GPU instancing. The non-instancing default branch already correctly uses lightIdx.

Fix

Changed 3 occurrences of l to lightIdx in the BatchingSchemes.INSTANCING case:

  • pass.getInstancedBuffer(l)pass.getInstancedBuffer(lightIdx)
  • this._instancedQueues[l]this._instancedQueues[lightIdx] (2 occurrences)

Reproduce Conditions

  • Scene has multiple valid additional lights (>= 2 validPunctualLights)
  • Material uses GPU Instancing (BatchingSchemes.INSTANCING)
  • Different models have different _lightCulling results (non-uniform _lightIndices per model)

Fixes: #19170

Changelog

  • Fixed incorrect light indexing in GPU instancing forward-add render queue

@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown

Too many files changed for review. (1573 files found, 100 file limit)

@UC-RX0
UC-RX0 force-pushed the fix/instancing-light-index branch from 0a629f3 to 20536d0 Compare June 26, 2026 03:29
@
fix(rendering): use lightIdx instead of loop index l in GPU instancing forward-add light path

In _addRenderQueue, the BatchingSchemes.INSTANCING branch was using
the local loop index l for getInstancedBuffer() and _instancedQueues[]
instead of lightIdx (the global light index from validPunctualLights).

This caused mismatched indexing between the instancing path (which
used local per-model light indices) and recordCommandBuffer /
gatherLightPasses (which use global light indices), resulting in
wrong UBO/descriptor bindings for additional lights when different
models have different light culling results under GPU instancing.

Also added:
- Null check in recordCommandBuffer for sparse _instancedQueues entries
- lightIdx multiplier on dynamicOffsets for consistency with
  non-instancing default branch and _instancedLightPassPool convention

Fixes: cocos#19170
@
@UC-RX0
UC-RX0 force-pushed the fix/instancing-light-index branch from 20536d0 to 33e4363 Compare June 29, 2026 02:36

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@github-actions

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1009738 bytes 1009754 bytes ⚠️ +16 bytes
2D All (legacy pipeline) 2675347 bytes 2675363 bytes ⚠️ +16 bytes
2D All (new pipeline) 2767083 bytes 2767083 bytes ✅ 0 bytes
(2D + 3D) All 10024081 bytes 10024097 bytes ⚠️ +16 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16843143 bytes 16843159 bytes ⚠️ +16 bytes

Interface Check Report

This pull request does not change any public interfaces !

@github-actions

Copy link
Copy Markdown

@UC-RX0 ❗ There was an error during the execution of the tasks. Please check the logs for more details.

@UC-RX0
UC-RX0 deleted the fix/instancing-light-index branch July 16, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

【Bug】附加光 + GPU 合批时,_addRenderQueue 中 instancing 路径使用循环索引 l 应改为真实灯索引 lightIdx

2 participants