Subject: [PATCH] black at first frame

From: tom &lt;[email-masked]&gt;
Date: Tue, 9 Nov 2021 20:05:59 +0800

---
 Application/AAPLMetalViewController.m      | 70 +++++++++++-----------
 Application/AAPLOpenGLViewController.m     | 13 +++-
 Application/iOS/Base.lproj/Main.storyboard | 10 ++--
 Renderer/Metal/AAPLMetalRenderer.m         | 51 +++++++++++++---
 Renderer/OpenGL/AAPLOpenGLRenderer.m       |  4 +-
 5 files changed, 98 insertions(+), 50 deletions(-)

diff --git a/Application/AAPLMetalViewController.m b/Application/AAPLMetalViewController.m
index b472faa..bb0da2b 100644
--- a/Application/AAPLMetalViewController.m
+++ b/Application/AAPLMetalViewController.m
@@ -26,41 +26,41 @@ static const MTLPixelFormat AAPLMetalViewInteropPixelFormat = MTLPixelFormatBGRA
 {
     [super viewDidLoad];
 
-    // Set the view to use the default device
-    _view = (MTKView *)self.view;
-    _view.device = MTLCreateSystemDefaultDevice();
-    _view.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
-
-    _metalRenderer = [[AAPLMetalRenderer alloc] initWithDevice:_view.device
-                                              colorPixelFormat:_view.colorPixelFormat];
-
-    NSAssert(_metalRenderer, @&#34;Metal Renderer failed initialization&#34;);
-
-    // Initialize  metal renderer with the view size
-    [_metalRenderer resize:_view.drawableSize];
-
-    [self createOpenGLContext];
-
-    // After a Metal device has been retrieved and an OpenGL context has been created and made
-    // current, a interop texture can be created
-    _interopTexture = [[AAPLOpenGLMetalInteropTexture alloc] initWithMetalDevice:_view.device
-                                                                   openGLContext:_openGLContext
-                                                                metalPixelFormat:AAPLMetalViewInteropPixelFormat
-                                                                            size:AAPLInteropTextureSize];
-
-    // Initialize OpenGL renderer to render into the interop texture
-    [self intializeOpenGLRendererWithInteropTexture:_interopTexture];
-
-    _view.delegate = self;
-
-    // Set initial Metal rendering size to the view size
-    [_metalRenderer resize:_view.drawableSize];
-
-    // Set initial OpenGL rendering size to interop texture size
-    [_openGLRenderer resize:AAPLInteropTextureSize];
-
-    // Set interop texture as texture to sample from
-    [_metalRenderer useInteropTextureAsBaseMap:_interopTexture.metalTexture];
+//    // Set the view to use the default device
+//    _view = (MTKView *)self.view;
+//    _view.device = MTLCreateSystemDefaultDevice();
+//    _view.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
+//
+//    _metalRenderer = [[AAPLMetalRenderer alloc] initWithDevice:_view.device
+//                                              colorPixelFormat:_view.colorPixelFormat];
+//
+//    NSAssert(_metalRenderer, @&#34;Metal Renderer failed initialization&#34;);
+//
+//    // Initialize  metal renderer with the view size
+//    [_metalRenderer resize:_view.drawableSize];
+//
+//    [self createOpenGLContext];
+//
+//    // After a Metal device has been retrieved and an OpenGL context has been created and made
+//    // current, a interop texture can be created
+//    _interopTexture = [[AAPLOpenGLMetalInteropTexture alloc] initWithMetalDevice:_view.device
+//                                                                   openGLContext:_openGLContext
+//                                                                metalPixelFormat:AAPLMetalViewInteropPixelFormat
+//                                                                            size:AAPLInteropTextureSize];
+//
+//    // Initialize OpenGL renderer to render into the interop texture
+//    [self intializeOpenGLRendererWithInteropTexture:_interopTexture];
+//
+//    _view.delegate = self;
+//
+//    // Set initial Metal rendering size to the view size
+//    [_metalRenderer resize:_view.drawableSize];
+//
+//    // Set initial OpenGL rendering size to interop texture size
+//    [_openGLRenderer resize:AAPLInteropTextureSize];
+//
+//    // Set interop texture as texture to sample from
+//    [_metalRenderer useInteropTextureAsBaseMap:_interopTexture.metalTexture];
 }
 
 - (void)createOpenGLContext
diff --git a/Application/AAPLOpenGLViewController.m b/Application/AAPLOpenGLViewController.m
index 813fcc9..30ed222 100644
--- a/Application/AAPLOpenGLViewController.m
+++ b/Application/AAPLOpenGLViewController.m
@@ -195,6 +195,17 @@ static CVReturn OpenGLDisplayLinkCallback(CVDisplayLinkRef displayLink,
 - (void)draw:(id)sender
 {
     [EAGLContext setCurrentContext:_context];
+	
+//	if (_metalRenderer == nil)
+//	{
+//		_metalRenderer = [[AAPLMetalRenderer alloc] initWithDevice:_metalDevice
+//		                                         colorPixelFormat:AAPLOpenGLViewInteropPixelFormat];
+//
+//		[_metalRenderer useTextureFromFileAsBaseMap];
+//
+//		[_metalRenderer resize:AAPLInteropTextureSize];
+//	}
+	
     [_metalRenderer drawToInteropTexture:_interopTexture.metalTexture];
     [_openGLRenderer draw];
 
@@ -242,7 +253,7 @@ static CVReturn OpenGLDisplayLinkCallback(CVDisplayLinkRef displayLink,
     // Create the display link so that we render at 60FPS
     _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(draw:)];
 
-    _displayLink.preferredFramesPerSecond = 60;
+    _displayLink.preferredFramesPerSecond = 1;
 
     // Have the display link run on the default runn loop (and the main thread)
     [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
diff --git a/Application/iOS/Base.lproj/Main.storyboard b/Application/iOS/Base.lproj/Main.storyboard
index e6cde90..367de67 100644
--- a/Application/iOS/Base.lproj/Main.storyboard
+++ b/Application/iOS/Base.lproj/Main.storyboard
@@ -1,11 +1,9 @@
 &lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&gt;
-&lt;document type=&#34;com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB&#34; version=&#34;3.0&#34; toolsVersion=&#34;14750&#34; targetRuntime=&#34;iOS.CocoaTouch&#34; propertyAccessControl=&#34;none&#34; useAutolayout=&#34;YES&#34; useTraitCollections=&#34;YES&#34; colorMatched=&#34;YES&#34; initialViewController=&#34;BV1-FR-VrT&#34;&gt;
-    &lt;device id=&#34;retina4_7&#34; orientation=&#34;portrait&#34;&gt;
-        &lt;adaptation id=&#34;fullscreen&#34;/&gt;
-    &lt;/device&gt;
+&lt;document type=&#34;com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB&#34; version=&#34;3.0&#34; toolsVersion=&#34;17701&#34; targetRuntime=&#34;iOS.CocoaTouch&#34; propertyAccessControl=&#34;none&#34; useAutolayout=&#34;YES&#34; useTraitCollections=&#34;YES&#34; colorMatched=&#34;YES&#34; initialViewController=&#34;BV1-FR-VrT&#34;&gt;
+    &lt;device id=&#34;retina4_7&#34; orientation=&#34;portrait&#34; appearance=&#34;light&#34;/&gt;
     &lt;dependencies&gt;
         &lt;deployment identifier=&#34;iOS&#34;/&gt;
-        &lt;plugIn identifier=&#34;com.apple.InterfaceBuilder.IBCocoaTouchPlugin&#34; version=&#34;14710.3&#34;/&gt;
+        &lt;plugIn identifier=&#34;com.apple.InterfaceBuilder.IBCocoaTouchPlugin&#34; version=&#34;17703&#34;/&gt;
         &lt;capability name=&#34;documents saved in the Xcode 8 format&#34; minToolsVersion=&#34;8.0&#34;/&gt;
     &lt;/dependencies&gt;
     &lt;scenes&gt;
@@ -60,7 +58,7 @@
                         &lt;viewControllerLayoutGuide type=&#34;top&#34; id=&#34;LO4-tw-VqD&#34;/&gt;
                         &lt;viewControllerLayoutGuide type=&#34;bottom&#34; id=&#34;Efi-Wa-aUd&#34;/&gt;
                     &lt;/layoutGuides&gt;
-                    &lt;view key=&#34;view&#34; contentMode=&#34;scaleToFill&#34; id=&#34;Qf4-Bk-0DP&#34; customClass=&#34;MTKView&#34;&gt;
+                    &lt;view key=&#34;view&#34; contentMode=&#34;scaleToFill&#34; id=&#34;Qf4-Bk-0DP&#34;&gt;
                         &lt;rect key=&#34;frame&#34; x=&#34;0.0&#34; y=&#34;0.0&#34; width=&#34;375&#34; height=&#34;330&#34;/&gt;
                         &lt;autoresizingMask key=&#34;autoresizingMask&#34; widthSizable=&#34;YES&#34; flexibleMinY=&#34;YES&#34; heightSizable=&#34;YES&#34;/&gt;
                         &lt;color key=&#34;backgroundColor&#34; white=&#34;1&#34; alpha=&#34;1&#34; colorSpace=&#34;custom&#34; customColorSpace=&#34;genericGamma22GrayColorSpace&#34;/&gt;
diff --git a/Renderer/Metal/AAPLMetalRenderer.m b/Renderer/Metal/AAPLMetalRenderer.m
index 406bfd4..5ba4149 100644
--- a/Renderer/Metal/AAPLMetalRenderer.m
+++ b/Renderer/Metal/AAPLMetalRenderer.m
@@ -16,6 +16,7 @@ Implementation of renderer class which performs Metal setup and per frame render
 
 // The max number of command buffers in flight
 static const NSUInteger AAPLMaxBuffersInFlight = 3;
+static const float STEP = 0.05;
 
 // Main class performing the rendering
 @implementation AAPLMetalRenderer
@@ -46,6 +47,8 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
     float _rotationIncrement;
 
     MTLRenderPassDescriptor *_renderPassDescriptor;
+	
+	id&lt;MTLTexture&gt; intenalTexture ;
 }
 
 /// Initialize with a Metal device and the pixel format of the render target
@@ -115,7 +118,7 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
         _renderPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
         _renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
 
-        _rotationIncrement = 0.01;
+        _rotationIncrement = STEP;
     }
 
     return self;
@@ -134,7 +137,7 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
 
     NSAssert(_labelMap, @&#34;Error loading Metal texture from file %@: %@&#34;, labelMapURL.absoluteString, error);
 
-    _rotationIncrement = 0.01;
+	_rotationIncrement = STEP;
 }
 
 - (void)useTextureFromFileAsBaseMap
@@ -155,7 +158,7 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
 
     NSAssert(_labelMap, @&#34;Error loading Metal texture from file %@: %@&#34;, labelMapURL.absoluteString, error);
     
-    _rotationIncrement = -0.01;
+    _rotationIncrement = -STEP;
 }
 
 /// Called whenever view changes orientation or layout is changed
@@ -170,15 +173,15 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
 {
     if(_rotation &gt; 30*(M_PI/180.0f))
     {
-        _rotationIncrement = -0.01;
+        _rotationIncrement = -STEP;
     }
     else if(_rotation &lt; -30*(M_PI/180.0f))
     {
-        _rotationIncrement = 0.01;
+        _rotationIncrement = STEP;
     }
     _rotation += _rotationIncrement;
 
-    matrix_float4x4 rotation = matrix4x4_rotation(_rotation, 0.0, 1.0, 0.0);
+    matrix_float4x4 rotation = matrix4x4_rotation(_rotation, 0.0, 0.0, 1.0);
     matrix_float4x4 translation = matrix4x4_translation(0.0, 0.0, -2.0);
     matrix_float4x4 modelView = matrix_multiply(translation, rotation);
 
@@ -195,6 +198,21 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
     dispatch_semaphore_wait(_inFlightSemaphore, DISPATCH_TIME_FOREVER);
 
     [self updateState];
+	
+	
+	if (intenalTexture == nil)
+	{
+		MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];
+		textureDescriptor.pixelFormat =  MTLPixelFormatBGRA8Unorm;
+		textureDescriptor.textureType = MTLTextureType2D;
+		textureDescriptor.width = texture.width;
+		textureDescriptor.height = texture.height;
+		textureDescriptor.storageMode = MTLResourceStorageModePrivate;
+		textureDescriptor.usage = MTLTextureUsageShaderRead|MTLTextureUsageRenderTarget;
+		intenalTexture = [_device newTextureWithDescriptor:textureDescriptor];
+	}
+	
+	
 
     // Create a new command buffer for each renderpass to the current drawable
     id&lt;MTLCommandBuffer&gt; commandBuffer = [_commandQueue commandBuffer];
@@ -211,7 +229,7 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
      }];

-    _renderPassDescriptor.colorAttachments[0].texture = texture;
+    _renderPassDescriptor.colorAttachments[0].texture = intenalTexture;
 
     // If a renderPassDescriptor has been obtained, render to the drawable, otherwise skip
     // any rendering this frame because there is no drawable to draw to
@@ -257,6 +275,25 @@ static const NSUInteger AAPLMaxBuffersInFlight = 3;
 
     // Done encoding commands
     [renderEncoder endEncoding];
+	
+	
+	/// blit
+
+	id&lt;MTLBlitCommandEncoder&gt; blitCommandEncoder = [commandBuffer blitCommandEncoder];
+	
+	[blitCommandEncoder copyFromTexture:intenalTexture
+							sourceSlice:0
+							sourceLevel:0
+						   sourceOrigin:MTLOriginMake(0,0,0)
+							 sourceSize:MTLSizeMake(texture.width, texture.height, 1)
+							  toTexture:texture
+					   destinationSlice:0
+					   destinationLevel:0
+					  destinationOrigin:MTLOriginMake(0,0,0)];
+
+
+	[blitCommandEncoder endEncoding];
+	
 
     return commandBuffer;
 }
diff --git a/Renderer/OpenGL/AAPLOpenGLRenderer.m b/Renderer/OpenGL/AAPLOpenGLRenderer.m
index beedae2..92b1ad4 100644
--- a/Renderer/OpenGL/AAPLOpenGLRenderer.m
+++ b/Renderer/OpenGL/AAPLOpenGLRenderer.m
@@ -445,8 +445,10 @@ enum {
     {
         _rotationIncrement = 0.01;
     }
-    _rotation += _rotationIncrement;
+    //_rotation += _rotationIncrement;
 
+	_rotation = 0 ;
+	
     matrix_float4x4 rotation = matrix4x4_rotation(_rotation, 0.0, 1.0, 0.0);
     matrix_float4x4 translation = matrix4x4_translation(0.0, 0.0, -2.0);
     matrix_float4x4 modelView = matrix_multiply(translation, rotation);
-- 
2.23.0