Repository: godot
Issue: Vulkan: Project crash on iOS - "Render Pipeline Descriptor Validation. No valid pixelFormats set"
Labels: bug,platform:ios,topic:rendering,topic:porting,confirmed,crash

___

***Bugsquad note:** This issue has been confirmed several times already. No need to confirm it further.*

___

### Godot version

4.0 alpha 14, master 

### System information

iPhone 7 Plus (iOS 15.4), Macbook Pro 2017 (macOS 12.3.1), Intel Iris Plus Graphics 640, Xcode 13.4.1

### Issue description

I am new to Godot and Github. I made a basic UI in Godot 4.0 alpha14. It worked on macOS then I exported for iOS. 

When I ran the exported Xcode project on iPhone 7 Plus the app crashed immediately after splash screen.

Xcode Debug Log::

```

2022-08-16 01:08:54.657731+0530 God[25323:790194] Vulkan API 1.1.162 - Using Vulkan Device #0: Apple - Apple A10 GPU
2022-08-16 01:08:54.662095+0530 God[25323:790194] fopen failed for data file: errno = 2 (No such file or directory)
2022-08-16 01:08:54.662213+0530 God[25323:790194] Errors found! Invalidating cache...
2022-08-16 01:08:55.260519+0530 God[25323:790194] USER ERROR: Condition "!classes.has(ti.inherits)" is true.
2022-08-16 01:08:55.260615+0530 God[25323:790194]    at: _add_class2 (core/object/class_db.cpp:402) - Condition "!classes.has(ti.inherits)" is true.
2022-08-16 01:08:55.261206+0530 God[25323:790194] USER ERROR: Condition "!classes.has(ti.inherits)" is true.
2022-08-16 01:08:55.261281+0530 God[25323:790194]    at: _add_class2 (core/object/class_db.cpp:402) - Condition "!classes.has(ti.inherits)" is true.
2022-08-16 01:08:55.923907+0530 God[25323:790444] fopen failed for data file: errno = 2 (No such file or directory)
2022-08-16 01:08:55.924072+0530 God[25323:790444] Errors found! Invalidating cache...
2022-08-16 01:09:05.711105+0530 God[25323:790433] validateWithDevice, line 3718: error 'Render Pipeline Descriptor Validation
No valid pixelFormats set.
'
validateWithDevice:3718: failed assertion `Render Pipeline Descriptor Validation
No valid pixelFormats set.
'
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/usr/lib/libMTLCapture.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
validateWithDevice:3718: failed assertion `Render Pipeline Descriptor Validation
No valid pixelFormats set.
```


**I have tried following solutions suggested for similar issues on Github but non of them worked.**


1. Disabled - Metal API Validation, GPU frame capture
2. Tried to run build in Debug and Release
3. Tried the same with Godot 4.0 alpha1. 
4. Tried to compile the master branch myself with different Vulkan SDK
       * Vulkan 1.3.216.0
       * Vulkan 1.2.148.1
5. Compiled custom iOS Export template
7. Tried to compile MoltenVK old version I think it was 1.2.148.1 and then master branch from Github

**Finally I tried to debug MoltenVK (master) code.** 

The crash is happening in (https://github.com/KhronosGroup/MoltenVK/tree/master/MoltenVK/MoltenVK/GPUObjects)/MVKPipeline.mm

```
id<MTLRenderPipelineState> MVKGraphicsPipeline::getOrCompilePipeline(MTLRenderPipelineDescriptor* plDesc,
																	 id<MTLRenderPipelineState>& plState) {
	if ( !plState ) {
		MVKRenderPipelineCompiler* plc = new MVKRenderPipelineCompiler(this);
		plState = plc->newMTLRenderPipelineState(plDesc);	// retained  <----------- Crash here
		plc->destroy();
		if ( !plState ) { _hasValidMTLPipelineStates = false; }
	}
	return plState;
}
```

After going through the code I added a few lines above the line where it was crashing. (Don't ask me to explain the code. I have no clue whatsoever)

```
id<MTLRenderPipelineState> MVKGraphicsPipeline::getOrCompilePipeline(MTLRenderPipelineDescriptor* plDesc,
                                                                     id<MTLRenderPipelineState>& plState) {
    if ( !plState ) {
        MVKRenderPipelineCompiler* plc = new MVKRenderPipelineCompiler(this);
        //My Code
        MTLRenderPipelineColorAttachmentDescriptor* colorDesc = plDesc.colorAttachments[0];
        colorDesc.pixelFormat = MTLPixelFormatRGBA8Unorm;//MTLPixelFormatBGRA8Unorm;
        colorDesc.writeMask = MTLColorWriteMaskAll;
        //My Code
        plState = plc->newMTLRenderPipelineState(plDesc);	// retained
        plc->destroy();
        if ( !plState ) { _hasValidMTLPipelineStates = false; }
    }
    return plState;
}
```

After two days of wandering in the desert I finally saw water.
I saw a world beyond splash screen.

Though it was running when not in debug, there was a new error when in debug.

Then I tried another demo project from Godot Github (https://github.com/godotengine/godot-demo-projects/releases/download/3.4-b0d4a7c/viewport_3d_scaling.zip)

I had to modify the GDScript in this project as Godot 4.0 has some changes (annotation and functions).

This project worked on macOS.
So exported for iOS.

Used the same MoltenVK library (libMoltenVK.dylib) I compiled with my changes.

This time it's neither working in debug nor without debug. (Splash Screen -> Crash)

The error is same in both projects.

Xcode Debug Log::

```
2022-08-16 02:32:39.776027+0530 God[25437:795656] 
2022-08-16 02:32:39.853738+0530 God[25437:795656] USER ERROR: Condition "!classes.has(ti.inherits)" is true.
2022-08-16 02:32:39.853896+0530 God[25437:795656]    at: _add_class2 (core/object/class_db.cpp:402) - Condition "!classes.has(ti.inherits)" is true.
-[MTLDebugRenderCommandEncoder setRenderPipelineState:]:1580: failed assertion `Set Render Pipeline State Validation
For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatRGBA8Unorm) does not match the framebuffer's pixelFormat (MTLPixelFormatBGRA8Unorm).
'
dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/GPUTools.framework/libglInterpose.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
-[MTLDebugRenderCommandEncoder setRenderPipelineState:]:1580: failed assertion `Set Render Pipeline State Validation
For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatRGBA8Unorm) does not match the framebuffer's pixelFormat (MTLPixelFormatBGRA8Unorm).
'
```

I believe this is because I hardcoded the pixelFormat to MTLPixelFormatRGBA8Unorm.

And I think the pixelFormat should be set from the function calling getOrCompilePipeline.

I was really excited to try Godot 4.0 on iOS but with this bug I am stuck.

If you need any other info please let me know.

### Steps to reproduce

Try to run (https://github.com/godotengine/godot-demo-projects/releases/download/3.4-b0d4a7c/viewport_3d_scaling.zip) on iOS.

### Minimal reproduction project

_No response_