{"record":{"id":"a987edea783d0ca6","repo":"bevyengine/bevy","slug":"binning-main-entity-when-it-was-already-binne","errorCode":null,"errorMessage":"Binning main entity {:?} when it was already binned. This should never happen.","messagePattern":"Binning main entity (.+?) when it was already binned\\. This should never happen\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bevy_render/src/render_phase/mod.rs","lineNumber":296,"sourceCode":"        // Create a `GpuRenderBinnedMeshInstance`.\n        let gpu_render_bin_entry = GpuRenderBinnedMeshInstance {\n            input_uniform_index: input_uniform_index.0,\n            bin_index: bin_index.0,\n        };\n\n        // Allocate a spot for this entity in the `render_mesh_instance_buffer`.\n        let render_binned_mesh_instance_buffer_index = RenderBinnedMeshInstanceIndex(\n            self.render_binned_mesh_instance_buffer\n                .push(GpuRenderBinnedMeshInstance::default()) as u32,\n        );\n        let maybe_previous_binned_mesh_instance_buffer_index = bin\n            .entity_to_binned_mesh_instance_index\n            .insert(main_entity, render_binned_mesh_instance_buffer_index);\n        // It's illegal to bin an entity that was already binned.\n        // If an entity is to change bins, it must first be removed from the bin\n        // it was previously in.\n        if maybe_previous_binned_mesh_instance_buffer_index.is_some() {\n            panic!(\n                \"Binning main entity {:?} when it was already binned. This should never happen.\",\n                main_entity\n            );\n        }\n\n        // Place the entry in the instance buffer at the proper spot.\n        self.render_binned_mesh_instance_buffer.values_mut()\n            [render_binned_mesh_instance_buffer_index.0 as usize] = gpu_render_bin_entry;\n\n        let bin_metadata_index =\n            self.bin_index_to_bin_metadata_index_buffer.values()[bin_index.0 as usize];\n        // There should never be a previous binned mesh buffer index, because\n        // it's illegal to bin a mesh instance that was already binned. But, if\n        // that error case *does* happen due to a bug, then at least don't\n        // corrupt the bins.\n        if maybe_previous_binned_mesh_instance_buffer_index.is_none() {\n            self.bin_metadata_buffer.values_mut()[bin_metadata_index as usize].instance_count += 1;\n        }","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/bevyengine/bevy/blob/221e52ae323febfd399bd7d067918fc43648cfb3/crates/bevy_render/src/render_phase/mod.rs#L278-L314","documentation":"This panic fires in RenderMultidrawableBin::insert when the entity_to_binned_mesh_instance_index map already contained an entry for the given MainEntity before the insert. That map is the authoritative record of which GPU instance slot an entity occupies; each entity must appear in exactly one bin, and moving an entity between bins requires an explicit remove first. Hitting this panic means a binning/rebinning code path (e.g. view or phase assignment logic) inserted the same entity twice without removing its prior entry, which would otherwise corrupt instance counts and leave a stale GPU instance slot. The input at fault is the duplicated MainEntity argument.","triggerScenarios":"Thrown at crates/bevy_render/src/render_phase/mod.rs:296 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the entity is removed from its previous bin (via the remove/swap_remove path) before inserting it into a new bin.","Check the caller's binning loop for re-processing the same MainEntity in one frame (duplicate phase entries or missing per-frame reset of entity_to_binned_mesh_instance_index).","If rebinning is intended, replace the plain insert with logic that detects the old index, decrements the old bin's instance_count, and frees the stale GPU slot instead of panicking."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"221e52ae323febfd399bd7d067918fc43648cfb3","analyzedAt":"2026-09-05T11:00:08.840Z","contentChangedAt":"2026-09-05T11:00:08.840Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}