Skip to content

[wgpu-core] split command encoders from command buffers #7979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

teoxoy
Copy link
Member

@teoxoy teoxoy commented Jul 21, 2025

Fixes #7812 and partially Bug 1972949.

@teoxoy teoxoy requested review from crowlKats and a team as code owners July 21, 2025 13:43
@teoxoy teoxoy force-pushed the split-cmd-enc-buf branch 2 times, most recently from 043df3d to 4faa0c5 Compare July 21, 2025 14:22
Copy link
Contributor

@andyleiserson andyleiserson left a comment

Choose a reason for hiding this comment

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

Nice, thank you for taking care of this.

I left some minor comments but nothing that I see as blocking. I pushed a branch with all of my CommandEncoderStatus::Consumed changes here.

Comment on lines +18 to +20
command_buffer_id_manager: &mut wgc::identity::IdentityManager<
wgc::id::markers::CommandBuffer,
>,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since the type names are kind of unwieldy and are repeated a few times, maybe it's worth importing the components or defining type aliases for the two identity manager types?

Comment on lines +148 to +150
Self::Error(CommandEncoderError::State(EncoderStateError::Ended)) => {
Err(EncoderStateError::Ended)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it make sense to add a new CommandEncoderStatus::Consumed for the status of an encoder that has been finished? (vs. Finished, which after this change is the CommandEncoderStatus of a valid command buffer). Distinguishing it as a separate state makes it easier to distinguish "there's a bug in wgpu, I didn't expect to find this state here" from an application doing something illegal that results in objects being invalid.

st @ Self::Error(CommandEncoderError::State(EncoderStateError::Ended)) => {
*self = st;
Err(EncoderStateError::Ended)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually a comment about CommandEncoderStatus::finish, below. We could change the API to reflect that the operation is no longer a mutation of the common state of encoders/buffers, but is more like an update to the state of a command encoder that also returns some information that will be given to the new command buffer. (Eventually, we could even split the buffer states out of CommandEncoderStatus into CommandBufferStatus, but that's a bigger refactoring that is probably better not to combine with this PR.)

Maybe something like:

    fn finish(&mut self) -> Self {
        // Replace our state with `Consumed`, and return either the inner
        // state or an error, to be transferred to the command buffer.
        match mem::replace(self, Self::Consumed) {
            Self::Recording(mut inner) => {
                if let Err(err) = inner.encoder.close_if_open() {
                    Self::Error(err.into())
                } else {
                    // Note: if we want to stop tracking the swapchain texture view,
                    // this is the place to do it.
                    Self::Finished(inner)
                }
            }
            Self::Consumed | Self::Finished(_) => Self::Error(EncoderStateError::Ended.into()),
            Self::Locked(_) => Self::Error(EncoderStateError::Locked.into()),
            st @ Self::Error(_) => st,
            Self::Transitioning => unreachable!(),
        }
    }

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.

[deno] Calling CommandEncoder.finish multiple times should raise a validation error
2 participants