Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added a WebGpuView to make WebGpu accessible in SwiftUI#14

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
SoylentGraham wants to merge1 commit intohenrybetts:main
base:main
Choose a base branch
Loading
fromNewChromantics:WebGpuView

Conversation

@SoylentGraham
Copy link

@SoylentGrahamSoylentGraham commentedNov 6, 2024
edited
Loading

Very quick implementation of a metal view where we can get access to the layer, and then use the layer + WebGpu to render WebGpu pipelines to the view surface.
So far only tested on macos with the triangle demo code

Usage

// allocate a global/persistent webgpu-rendererlet webGpuRenderer = WebGPU.WebGpuRenderer()// class that contains persistent objects for democlass TriangleDemoInstance{//todo: need to save these per-devicevar pipeline : WebGPU.RenderPipeline?var vertexBuffer : WebGPU.Buffer?//can we get this from the surface view?var windowTextureFormat = TextureFormat.bgra8Unormfunc InitResources(device:Device){... create pipeline, vertex buffer, if not already allocated...}func Render(device:Device,encoder:CommandEncoder,surfaceView:TextureView){InitResources(device: device)guard let pipeline else{return}let renderPass = encoder.beginRenderPass(descriptor: RenderPassDescriptor(colorAttachments: [RenderPassColorAttachment(view: surfaceView,loadOp: .clear,storeOp: .store,clearValue: WebGPU.Color(r: 0, g: 1, b: 1, a: 1))]))renderPass.setPipeline(pipeline)renderPass.setVertexBuffer(slot: 0, buffer: vertexBuffer!)renderPass.draw(vertexCount: self.vertexCount!)renderPass.end()}}// instance of our persistent application rendererlet triangleDemo = TriangleDemoInstance()public struct TriangleView : View, WebGPU.ContentRenderer{public var body : some View{WebGpuView(contentRenderer: self).frame(maxWidth: .infinity, maxHeight: .infinity).background(.red) // debug}public func Render(contentRect: CGRect,layer:CAMetalLayer){do{try webGpuRenderer.Render(metalLayer: layer, getCommands:triangleDemo.Render )}catch let Error{print("Render error; \(Error.localizedDescription)")}}}

Todo

@SoylentGraham
Copy link
Author

There are more changes to this swiftviewhttps://github.com/NewChromantics/swift-webgpu/tree/LockingRender

  • lock to avoid multithread access to dawn (blows up!)
  • improvements to layer setup
  • Added a high level view to easily add debug (eg. fps counter) at package level, but also a "direct" webgpu-view for the original mostly-direct access
  • Vsync implemented for macos

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@SoylentGraham

[8]ページ先頭

©2009-2025 Movatter.jp