Skip to content
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

[WebGPU] Implement dynamic buffer offsets for render bundles #19025

Conversation

mwyrzykowski
Copy link
Contributor

@mwyrzykowski mwyrzykowski commented Oct 12, 2023

32f0226

[WebGPU] Implement dynamic buffer offsets for render bundles
https://bugs.webkit.org/show_bug.cgi?id=262208
<radar://116137950>

Reviewed by Dan Glastonbury.

Support dynamic offsets in RenderBundles / ICBs like they are supported in the RenderPassEncoder
and ComputePassEncoder.

* Source/WebCore/Modules/WebGPU/Implementation/WebGPURenderBundleEncoderImpl.cpp:
(WebCore::WebGPU::RenderBundleEncoderImpl::setBindGroup):
* Source/WebGPU/WebGPU/RenderBundleEncoder.h:
* Source/WebGPU/WebGPU/RenderBundleEncoder.mm:
(WebGPU::addResource):
(WebGPU::RenderBundleEncoder::executePreDrawCommands):
(WebGPU::RenderBundleEncoder::draw):
(WebGPU::RenderBundleEncoder::drawIndexed):
(WebGPU::RenderBundleEncoder::drawIndexedIndirect):
(WebGPU::RenderBundleEncoder::drawIndirect):
(WebGPU::RenderBundleEncoder::finish):
(WebGPU::RenderBundleEncoder::setBindGroup):
(WebGPU::RenderBundleEncoder::setPipeline):
(wgpuRenderBundleEncoderSetBindGroup):
(wgpuRenderBundleEncoderSetBindGroupWithDynamicOffsets):
* Source/WebGPU/WebGPU/WebGPUExt.h:

Canonical link: https://commits.webkit.org/269548@main

99a537c

Misc iOS, tvOS & watchOS macOS Linux Windows
βœ… πŸ§ͺ style βœ… πŸ›  ios βœ… πŸ›  mac βœ… πŸ›  wpe βœ… πŸ›  wincairo
βœ… πŸ§ͺ bindings βœ… πŸ›  ios-sim βœ… πŸ›  mac-AS-debug βœ… πŸ§ͺ wpe-wk2
βœ… πŸ§ͺ webkitperl βœ… πŸ§ͺ ios-wk2 βœ… πŸ§ͺ api-mac βœ… πŸ›  gtk
βœ… πŸ§ͺ ios-wk2-wpt βœ… πŸ§ͺ mac-wk1 βœ… πŸ§ͺ gtk-wk2
βœ… πŸ§ͺ api-ios βœ… πŸ§ͺ mac-wk2 βœ… πŸ§ͺ api-gtk
βœ… πŸ›  tv βœ… πŸ§ͺ mac-AS-debug-wk2
βœ… πŸ›  tv-sim
βœ… πŸ›  πŸ§ͺ merge βœ… πŸ›  watch
βœ… πŸ›  watch-sim

@mwyrzykowski mwyrzykowski self-assigned this Oct 12, 2023
@mwyrzykowski mwyrzykowski added the WebGPU For bugs in WebGPU label Oct 12, 2023
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Oct 13, 2023
@mwyrzykowski mwyrzykowski removed the merging-blocked Applied to prevent a change from being merged label Oct 13, 2023
@mwyrzykowski mwyrzykowski changed the title WebGPU] Implement dynamic buffer offsets for render bundles [WebGPU] Implement dynamic buffer offsets for render bundles Oct 13, 2023
@mwyrzykowski mwyrzykowski force-pushed the eng/WebGPU-Implement-dynamic-buffer-offsets-for-render-bundles branch from 8edcd4c to a31d465 Compare October 13, 2023 00:24
Copy link
Contributor

@djg djg left a comment

Choose a reason for hiding this comment

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

:shipit:


m_bindGroupDynamicOffsets.clear();
}

void RenderBundleEncoder::draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
{
if (id<MTLIndirectRenderCommand> icbCommand = currentRenderCommand()) {
executePreDrawCommands();
executePreDrawCommands();
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be computed for every draw, or can the calculations be cached?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it needs to happen before every draw because the vertex buffers can change between draws.

And Metal ICBs appear to require all buffers in use to call setVertexBuffer: for each command, whereas WebGPU maintains a persistent state across commands

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which is best illustrated through https://webgpu.github.io/webgpu-samples/samples/renderBundles

    passEncoder.setPipeline(pipeline);
    passEncoder.setBindGroup(0, frameBindGroup);

    // Loop through every renderable object and draw them individually.
    // (Because many of these meshes are repeated, with only the transforms
    // differing, instancing would be highly effective here. This sample
    // intentionally avoids using instancing in order to emulate a more complex
    // scene, which helps demonstrate the potential time savings a render bundle
    // can provide.)
    let count = 0;
    for (const renderable of renderables) {
      passEncoder.setBindGroup(1, renderable.bindGroup);
      passEncoder.setVertexBuffer(0, renderable.vertices);
      passEncoder.setIndexBuffer(renderable.indices, 'uint16');
      passEncoder.drawIndexed(renderable.indexCount);

      if (++count > settings.asteroidCount) {
        break;
      }
    }

setBindGroup(0 is only called once and the expectation is that bind group is persistent throughout the draw calls

auto commandCount = m_currentCommandIndex;
m_currentCommandIndex = 0;

if (!m_indirectCommandBuffer) {
m_icbDescriptor.maxVertexBufferBindCount = m_device->maxBuffersPlusVertexBuffersForVertexStage();
m_icbDescriptor.maxVertexBufferBindCount = m_device->maxBuffersPlusVertexBuffersForVertexStage() + 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you a comment here why + 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh because the dynamic offsets get set to index = m_device->maxBuffersPlusVertexBuffersForVertexStage() and if I set them to m_device->maxBuffersPlusVertexBuffersForVertexStage() - 1 then I need to change the WGSL compiler.

But that is kind of ugly, maybe I can clean that up

@mwyrzykowski mwyrzykowski force-pushed the eng/WebGPU-Implement-dynamic-buffer-offsets-for-render-bundles branch from a31d465 to 99a537c Compare October 18, 2023 16:13
@webkit-early-warning-system
Copy link
Collaborator

Starting EWS tests for 99a537c. Live statuses available at the PR page, #19025

@mwyrzykowski mwyrzykowski added the merge-queue Applied to send a pull request to merge-queue label Oct 19, 2023
https://bugs.webkit.org/show_bug.cgi?id=262208
<radar://116137950>

Reviewed by Dan Glastonbury.

Support dynamic offsets in RenderBundles / ICBs like they are supported in the RenderPassEncoder
and ComputePassEncoder.

* Source/WebCore/Modules/WebGPU/Implementation/WebGPURenderBundleEncoderImpl.cpp:
(WebCore::WebGPU::RenderBundleEncoderImpl::setBindGroup):
* Source/WebGPU/WebGPU/RenderBundleEncoder.h:
* Source/WebGPU/WebGPU/RenderBundleEncoder.mm:
(WebGPU::addResource):
(WebGPU::RenderBundleEncoder::executePreDrawCommands):
(WebGPU::RenderBundleEncoder::draw):
(WebGPU::RenderBundleEncoder::drawIndexed):
(WebGPU::RenderBundleEncoder::drawIndexedIndirect):
(WebGPU::RenderBundleEncoder::drawIndirect):
(WebGPU::RenderBundleEncoder::finish):
(WebGPU::RenderBundleEncoder::setBindGroup):
(WebGPU::RenderBundleEncoder::setPipeline):
(wgpuRenderBundleEncoderSetBindGroup):
(wgpuRenderBundleEncoderSetBindGroupWithDynamicOffsets):
* Source/WebGPU/WebGPU/WebGPUExt.h:

Canonical link: https://commits.webkit.org/269548@main
@webkit-commit-queue webkit-commit-queue force-pushed the eng/WebGPU-Implement-dynamic-buffer-offsets-for-render-bundles branch from 99a537c to 32f0226 Compare October 19, 2023 22:15
@webkit-commit-queue
Copy link
Collaborator

Committed 269548@main (32f0226): https://commits.webkit.org/269548@main

Reviewed commits have been landed. Closing PR #19025 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit 32f0226 into WebKit:main Oct 19, 2023
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WebGPU For bugs in WebGPU
Projects
None yet
5 participants