Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

ios - Off Screen Rendering Metal

 func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
        print("current drawable size:(view.drawableSize)")
    }

    func draw(in view: MTKView) {


        guard let drawable = view.currentDrawable else { return }

        let textureDescriptor = MTLTextureDescriptor()
        textureDescriptor.textureType = MTLTextureType.type2D
        textureDescriptor.width = drawable.texture.width
        textureDescriptor.height = drawable.texture.height
        textureDescriptor.pixelFormat = .bgra8Unorm
        textureDescriptor.storageMode = .shared
        textureDescriptor.usage = .renderTarget

        let sampleTexture = device.makeTexture(descriptor: textureDescriptor)


        let renderPass = MTLRenderPassDescriptor()
        renderPass.colorAttachments[0].texture = sampleTexture
        renderPass.colorAttachments[0].loadAction = .clear
        renderPass.colorAttachments[0].clearColor =
            MTLClearColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
        renderPass.colorAttachments[0].storeAction = .store

        let commandBuffer = commandQueue.makeCommandBuffer()
        var commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: renderPass)


        let deltaTime = 1 / Float(view.preferredFramesPerSecond)


        for scene in scenes{
            scene.render(commandEncoder: commandEncoder!, deltaTime: deltaTime)
        }

        commandEncoder?.endEncoding()


        let descriptor = view.currentRenderPassDescriptor
        commandEncoder = commandBuffer?.makeRenderCommandEncoder(descriptor: descriptor!)



       for canvasScene in canvasScenes{
          canvasScene.updateCanvas(texture: sampleTexture!)
          canvasScene.render(commandEncoder: commandEncoder!, deltaTime: deltaTime)
       }

        commandEncoder?.endEncoding()


        commandBuffer?.present(drawable)
        commandBuffer?.commit()
    }

I am new to Off screen rendering. I hope I am doing it correct. can any one suggest whether it is correct or not. I am drawing it in sample Texture in first and then setting the sample texture to the quad(plane) which is drawing in the current drawable. Is it the optimized way of doing off screen rendering.

This code runs when i only disable Metal API validation. otherwise i got following issue

Fragment Function(fragment_shader_texture): Shader reads texture (texture[0]) whose usage (0x04) doesn't specify MTLTextureUsageShaderRead (0x01)

Suggestions please to optimize

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

off-screen texture's usage to .renderTarget, you should use [.renderTarget, .shaderRead].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...