{"record":{"id":"b1e444c47a84eaeb","repo":"bevyengine/bevy","slug":"createbindgroupdirectly","errorCode":"CreateBindGroupDirectly","errorMessage":"Create the bind group via `as_bind_group()` instead","messagePattern":"Create the bind group via `as_bind_group\\(\\)` instead","errorType":"error_code","errorClass":"AsBindGroupError","httpStatus":null,"severity":"error","filePath":"crates/bevy_render/src/render_resource/bind_group.rs","lineNumber":644,"sourceCode":"    fn bind_group_layout_entries(\n        render_device: &RenderDevice,\n        force_no_bindless: bool,\n    ) -> Vec<BindGroupLayoutEntry>\n    where\n        Self: Sized;\n\n    fn bindless_descriptor() -> Option<BindlessDescriptor> {\n        None\n    }\n}\n\n/// An error that occurs during [`AsBindGroup::as_bind_group`] calls.\n#[derive(Debug, Error)]\npub enum AsBindGroupError {\n    /// The bind group could not be generated. Try again next frame.\n    #[error(\"The bind group could not be generated\")]\n    RetryNextUpdate,\n    #[error(\"Create the bind group via `as_bind_group()` instead\")]\n    CreateBindGroupDirectly,\n    #[error(\"At binding index {0}, the provided image sampler `{1}` does not match the required sampler type(s) `{2}`.\")]\n    InvalidSamplerType(u32, String, String),\n}\n\n/// A prepared bind group returned as a result of [`AsBindGroup::as_bind_group`].\npub struct PreparedBindGroup {\n    pub bindings: BindingResources,\n    pub bind_group: BindGroup,\n}\n\nimpl PreparedBindGroup {\n    pub(crate) fn unprepare(&self) -> BindGroupBuilder {\n        let mut data_buffer = vec![];\n        BindGroupBuilder {\n            binding_resources: UnpreparedBindingResources(\n                self.bindings\n                    .iter()","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_render/src/render_resource/bind_group.rs#L626-L662","documentation":"AsBindGroupError::CreateBindGroupDirectly is returned by AsBindGroup paths other than as_bind_group() for implementations that must be created through as_bind_group() (bind_group.rs:580 documents this contract). It tells the caller the attempted creation route (e.g. the unprepared/allocator path) is not supported for this type and the bind group must come from as_bind_group().","triggerScenarios":"Calling unprepared_bind_group (or letting the MaterialBindGroupAllocator create the group from unprepared bindings) on a type whose implementation rejects that route and returns CreateBindGroupDirectly.","commonSituations":"Custom materials that take full control of bind group creation; bindless (descriptor-indexing) materials; migration between Bevy versions where material bind group creation paths changed; hand-rolled material preparation bypassing the default Material systems.","solutions":["Create the group via as_bind_group() as the message says, and use the returned PreparedBindGroup.","If you own the implementation, only return this variant from paths you do not support and make sure your material plugin uses as_bind_group.","Prefer the default Material preparation pipeline, which already handles this variant correctly."],"exampleFix":"// before: unsupported creation path\nlet unprepared = material.unprepared_bind_group(layout_id, &layout, render_device)?; // Err(CreateBindGroupDirectly)\n\n// after\nlet prepared = material.as_bind_group(layout_id, &layout, render_device, render_queue, &mut param)?;\nlet bind_group = prepared.bind_group;","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"let group = match material.unprepared_bind_group(layout_id, &layout, render_device) {\n    Ok(unprepared) => build_from(unprepared),\n    Err(AsBindGroupError::CreateBindGroupDirectly) => {\n        material.as_bind_group(layout_id, &layout, render_device, render_queue, &mut param)?.bind_group\n    }\n    Err(e) => return Err(e.into()),\n};","preventionTips":["Read the type's AsBindGroup docs/impl before choosing a creation path.","Prefer the standard material preparation systems over hand-rolled creation.","When implementing AsBindGroup, document which creation routes you support."],"tags":["bevy","bind-group","api-misuse","material"],"backgroundTag":"use-factory-method","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-09T01:17:15.007Z"}