diff --git a/src/macosx/MacMiniFB.m b/src/macosx/MacMiniFB.m index 04866ba..823a606 100644 --- a/src/macosx/MacMiniFB.m +++ b/src/macosx/MacMiniFB.m @@ -131,11 +131,11 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) } #if defined(USE_METAL_API) - OSXViewDelegate *viewController = [[OSXViewDelegate alloc] initWithWindowData:window_data]; + window_data_osx->viewController = [[OSXViewDelegate alloc] initWithWindowData:window_data]; MTKView* view = [[MTKView alloc] initWithFrame:rectangle]; - view.device = viewController->metal_device; - view.delegate = viewController; + view.device = window_data_osx->viewController->metal_device; + view.delegate = window_data_osx->viewController; view.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; [window_data_osx->window.contentView addSubview:view]; @@ -229,6 +229,16 @@ mfb_update(struct mfb_window *window, void *buffer) { if(window == 0x0) { return STATE_INVALID_WINDOW; } + SWindowData *window_data = (SWindowData *) window; + return mfb_update_ex(window, buffer, window_data->buffer_width, window_data->buffer_height); +} + +//------------------------------------- +mfb_update_state +mfb_update_ex(struct mfb_window *window, void *buffer, unsigned width, unsigned height) { + if(window == 0x0) { + return STATE_INVALID_WINDOW; + } SWindowData *window_data = (SWindowData *) window; if(window_data->close) { @@ -240,8 +250,19 @@ mfb_update(struct mfb_window *window, void *buffer) { return STATE_INVALID_BUFFER; } + SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; + #if defined(USE_METAL_API) - memcpy(window_data->draw_buffer, buffer, window_data->buffer_width * window_data->buffer_height * 4); + if(window_data->buffer_width != width || window_data->buffer_height != height) { + window_data->buffer_width = width; + window_data->buffer_stride = width * 4; + window_data->buffer_height = height; + window_data->draw_buffer = realloc(window_data->draw_buffer, window_data->buffer_stride * window_data->buffer_height); + + [window_data_osx->viewController resizeTextures]; + } + + memcpy(window_data->draw_buffer, buffer, window_data->buffer_stride * window_data->buffer_height); #else window_data->draw_buffer = buffer; #endif @@ -252,7 +273,6 @@ mfb_update(struct mfb_window *window, void *buffer) { return STATE_EXIT; } - SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; [[window_data_osx->window contentView] setNeedsDisplay:YES]; return STATE_OK; diff --git a/src/macosx/OSXViewDelegate.h b/src/macosx/OSXViewDelegate.h index d4eb10a..33943fd 100644 --- a/src/macosx/OSXViewDelegate.h +++ b/src/macosx/OSXViewDelegate.h @@ -28,6 +28,7 @@ enum { MaxBuffersInFlight = 3 }; } - (id) initWithWindowData:(SWindowData *) windowData; +- (void) resizeTextures; @end diff --git a/src/macosx/OSXViewDelegate.m b/src/macosx/OSXViewDelegate.m index 35db248..c06aff7 100644 --- a/src/macosx/OSXViewDelegate.m +++ b/src/macosx/OSXViewDelegate.m @@ -140,6 +140,21 @@ NSString *g_shader_src = kShader( } } +//------------------------------------- +- (void) resizeTextures { + MTLTextureDescriptor *td; + td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm + width:window_data->buffer_width + height:window_data->buffer_height + mipmapped:false]; + + // Create the texture from the device by using the descriptor + for (size_t i = 0; i < MaxBuffersInFlight; ++i) { + [texture_buffers[i] release]; + texture_buffers[i] = [metal_device newTextureWithDescriptor:td]; + } +} + //------------------------------------- - (void) drawInMTKView:(nonnull MTKView *) view { // Wait to ensure only MaxBuffersInFlight number of frames are getting proccessed diff --git a/src/macosx/WindowData_OSX.h b/src/macosx/WindowData_OSX.h index 3c52e83..d3dee7b 100644 --- a/src/macosx/WindowData_OSX.h +++ b/src/macosx/WindowData_OSX.h @@ -8,6 +8,7 @@ #endif @class OSXWindow; +@class OSXViewDelegate; typedef struct Vertex { float x, y, z, w; @@ -15,6 +16,7 @@ typedef struct Vertex { typedef struct { OSXWindow *window; + OSXViewDelegate *viewController; struct mfb_timer *timer; #if defined(USE_METAL_API)