added mfb_update_ex - MacOS X - Metal version

This commit is contained in:
Carlos Aragones 2020-09-15 12:39:26 +02:00
parent 2cf75ce9ac
commit e98644cfbf
4 changed files with 43 additions and 5 deletions

View File

@ -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;

View File

@ -28,6 +28,7 @@ enum { MaxBuffersInFlight = 3 };
}
- (id) initWithWindowData:(SWindowData *) windowData;
- (void) resizeTextures;
@end

View File

@ -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

View File

@ -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)