Group Group Group Group Group Group Group Group Group

Crash on iPhone after adding blit code in Chapter 14

I’ve tried working from the starter project forward and the final project backwards with the same result. After you add the blit command encoder the app will crash on my iPhone X with:

[MTLDebugBlitCommandEncoder internalValidateCopyFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:options:]:473: failed assertion `(sourceOrigin.x + sourceSize.width)(1125) must be <= width(375).'

Tracing this down, it seems like somehow MTKView is getting dimensions from the macOS side of things. Unfortunately, I can’t seem to figure out why that is.

BTW, the final project, after commenting out the blit code, works fine but is SUPER slow (i.e. 6 fps slow) on my iPhone X. It does not seem like that should be expected. It’s also super slow on my MBP but I’m not surprised with the NVIDIA GPU.

In draw(in:), try dividing the MTLSize width and height by 2. That should work.

Renderer is shared by both macOS and iOS.

Yes, the final project stresses the GPU - try reducing the number of lights in Renderer's init when you call createPointLights(). It runs at 30fps on my MacBook Pro and 17fps on my iPhone 11.

Dividing by two is not enough… 1125/2 = 562(.5). Dividing by three works but…

Kind of a cool effect but I don’t think it’s what I’m supposed to be seeing.

Huh. I divided by 2 and it worked

I went into final, and divided by 2 and ran it on my iPhone 11 and checked it in the GPU debugger

It’s hard to debug something that’s halfway through the chapter :smiley: - would you be able to zip up your project and post here please?

I’m too much of a NOOB to upload…

But here it is on Github:

The inset image looks to be 1/3 of the size of the screen which would make some sense I suppose.

The image that you posted above is actually what you’re supposed to be seeing.

The blit section is only there so that you can visualise what a texture is, and show you how you can render any texture to the screen. Later it gets taken out, because it’s only for demonstration.

I’ve not looked at this chapter for nearly a year, so I could be wrong, but I think I would prefer that size declaration to be:

let size = MTLSize(width: albedoTexture.width, height: albedoTexture.height, depth: 1)

albedoTexture is created in Renderer's drawableSizeWillChange() and the size gets updated when the view size changes.

Add this to the very end of Renderer's init:

 mtkView(metalView, drawableSizeWillChange: metalView.drawableSize)

and it should also now work on iOS.

Btw, to create a zip file, you should be able to right click on the project’s top directory and choose “Compress folder name”. Then drag it to the post that you’re writing.

Oh… I know how to create a zip file. Lol. I meant I’m too new on the forum to be allowed to upload anything.

Oh, I see now about the blit example. I was assuming the blit should fill the entire screen, which I suppose it would on macOS.

I was thinking about using a blit command encoder for another project I’m working on. Thus, I wanted to make sure I understood what was happening in your example.

Thank you for all the help.

1 Like