2018-06-15 09:58:33 +00:00
|
|
|
#import "GBViewMetal.h"
|
2020-04-11 15:19:15 +00:00
|
|
|
#pragma clang diagnostic ignored "-Wpartial-availability"
|
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
|
|
|
|
static const vector_float2 rect[] =
|
|
|
|
{
|
|
|
|
{-1, -1},
|
|
|
|
{ 1, -1},
|
|
|
|
{-1, 1},
|
|
|
|
{ 1, 1},
|
|
|
|
};
|
|
|
|
|
|
|
|
@implementation GBViewMetal
|
|
|
|
{
|
|
|
|
id<MTLDevice> device;
|
|
|
|
id<MTLTexture> texture, previous_texture;
|
|
|
|
id<MTLBuffer> vertices;
|
|
|
|
id<MTLRenderPipelineState> pipeline_state;
|
|
|
|
id<MTLCommandQueue> command_queue;
|
2020-03-26 18:54:18 +00:00
|
|
|
id<MTLBuffer> frame_blending_mode_buffer;
|
2018-06-15 15:44:22 +00:00
|
|
|
id<MTLBuffer> output_resolution_buffer;
|
|
|
|
vector_float2 output_resolution;
|
2018-06-15 09:58:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 17:03:59 +00:00
|
|
|
+ (bool)isSupported
|
|
|
|
{
|
|
|
|
if (MTLCopyAllDevices) {
|
2020-03-27 14:26:51 +00:00
|
|
|
return [MTLCopyAllDevices() count];
|
2018-06-15 17:03:59 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:21:21 +00:00
|
|
|
- (void) allocateTextures
|
2018-06-15 09:58:33 +00:00
|
|
|
{
|
2018-11-14 22:21:21 +00:00
|
|
|
if (!device) return;
|
2018-06-15 09:58:33 +00:00
|
|
|
|
|
|
|
MTLTextureDescriptor *texture_descriptor = [[MTLTextureDescriptor alloc] init];
|
|
|
|
|
|
|
|
texture_descriptor.pixelFormat = MTLPixelFormatRGBA8Unorm;
|
|
|
|
|
2018-11-14 22:21:21 +00:00
|
|
|
texture_descriptor.width = GB_get_screen_width(self.gb);
|
|
|
|
texture_descriptor.height = GB_get_screen_height(self.gb);
|
2018-06-15 09:58:33 +00:00
|
|
|
|
|
|
|
texture = [device newTextureWithDescriptor:texture_descriptor];
|
|
|
|
previous_texture = [device newTextureWithDescriptor:texture_descriptor];
|
2018-11-14 22:21:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)createInternalView
|
|
|
|
{
|
|
|
|
MTKView *view = [[MTKView alloc] initWithFrame:self.frame device:(device = MTLCreateSystemDefaultDevice())];
|
|
|
|
view.delegate = self;
|
|
|
|
self.internalView = view;
|
|
|
|
view.paused = YES;
|
2019-07-19 20:59:25 +00:00
|
|
|
view.enableSetNeedsDisplay = YES;
|
2018-11-14 22:21:21 +00:00
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
vertices = [device newBufferWithBytes:rect
|
|
|
|
length:sizeof(rect)
|
|
|
|
options:MTLResourceStorageModeShared];
|
2018-06-15 10:58:50 +00:00
|
|
|
|
2020-03-26 18:54:18 +00:00
|
|
|
static const GB_frame_blending_mode_t default_blending_mode = GB_FRAME_BLENDING_MODE_DISABLED;
|
|
|
|
frame_blending_mode_buffer = [device newBufferWithBytes:&default_blending_mode
|
|
|
|
length:sizeof(default_blending_mode)
|
|
|
|
options:MTLResourceStorageModeShared];
|
2018-06-15 10:58:50 +00:00
|
|
|
|
2018-06-15 16:11:06 +00:00
|
|
|
output_resolution_buffer = [device newBufferWithBytes:&output_resolution
|
|
|
|
length:sizeof(output_resolution)
|
2018-06-15 15:44:22 +00:00
|
|
|
options:MTLResourceStorageModeShared];
|
|
|
|
|
2018-06-16 09:46:57 +00:00
|
|
|
output_resolution = (simd_float2){view.drawableSize.width, view.drawableSize.height};
|
2018-06-15 16:11:06 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadShader) name:@"GBFilterChanged" object:nil];
|
2018-06-15 09:58:33 +00:00
|
|
|
[self loadShader];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) loadShader
|
|
|
|
{
|
|
|
|
NSError *error = nil;
|
|
|
|
NSString *shader_source = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MasterShader"
|
|
|
|
ofType:@"metal"
|
|
|
|
inDirectory:@"Shaders"]
|
|
|
|
encoding:NSUTF8StringEncoding
|
|
|
|
error:nil];
|
2018-06-15 16:11:06 +00:00
|
|
|
|
|
|
|
NSString *shader_name = [[NSUserDefaults standardUserDefaults] objectForKey:@"GBFilter"];
|
|
|
|
NSString *scaler_source = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:shader_name
|
|
|
|
ofType:@"fsh"
|
|
|
|
inDirectory:@"Shaders"]
|
|
|
|
encoding:NSUTF8StringEncoding
|
|
|
|
error:nil];
|
|
|
|
|
|
|
|
shader_source = [shader_source stringByReplacingOccurrencesOfString:@"{filter}"
|
|
|
|
withString:scaler_source];
|
|
|
|
|
2018-06-15 21:08:24 +00:00
|
|
|
MTLCompileOptions *options = [[MTLCompileOptions alloc] init];
|
|
|
|
options.fastMathEnabled = YES;
|
2018-06-15 09:58:33 +00:00
|
|
|
id<MTLLibrary> library = [device newLibraryWithSource:shader_source
|
2018-06-15 21:08:24 +00:00
|
|
|
options:options
|
2018-06-15 09:58:33 +00:00
|
|
|
error:&error];
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Error: %@", error);
|
2018-06-15 16:11:06 +00:00
|
|
|
if (!library) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-15 09:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id<MTLFunction> vertex_function = [library newFunctionWithName:@"vertex_shader"];
|
|
|
|
id<MTLFunction> fragment_function = [library newFunctionWithName:@"fragment_shader"];
|
|
|
|
|
|
|
|
// Set up a descriptor for creating a pipeline state object
|
|
|
|
MTLRenderPipelineDescriptor *pipeline_state_descriptor = [[MTLRenderPipelineDescriptor alloc] init];
|
|
|
|
pipeline_state_descriptor.vertexFunction = vertex_function;
|
|
|
|
pipeline_state_descriptor.fragmentFunction = fragment_function;
|
|
|
|
pipeline_state_descriptor.colorAttachments[0].pixelFormat = ((MTKView *)self.internalView).colorPixelFormat;
|
|
|
|
|
|
|
|
error = nil;
|
|
|
|
pipeline_state = [device newRenderPipelineStateWithDescriptor:pipeline_state_descriptor
|
|
|
|
error:&error];
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Failed to created pipeline state, error %@", error);
|
2018-06-15 16:11:06 +00:00
|
|
|
return;
|
2018-06-15 09:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
command_queue = [device newCommandQueue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mtkView:(nonnull MTKView *)view drawableSizeWillChange:(CGSize)size
|
|
|
|
{
|
2018-06-15 15:44:22 +00:00
|
|
|
output_resolution = (vector_float2){size.width, size.height};
|
2018-10-11 15:43:31 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[(MTKView *)self.internalView draw];
|
|
|
|
});
|
2018-06-15 09:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawInMTKView:(nonnull MTKView *)view
|
|
|
|
{
|
2018-10-11 15:43:31 +00:00
|
|
|
if (!(view.window.occlusionState & NSWindowOcclusionStateVisible)) return;
|
2019-07-19 12:50:36 +00:00
|
|
|
if (!self.gb) return;
|
2018-11-23 17:59:15 +00:00
|
|
|
if (texture.width != GB_get_screen_width(self.gb) ||
|
|
|
|
texture.height != GB_get_screen_height(self.gb)) {
|
|
|
|
[self allocateTextures];
|
|
|
|
}
|
2018-11-14 22:21:21 +00:00
|
|
|
|
|
|
|
MTLRegion region = {
|
|
|
|
{0, 0, 0}, // MTLOrigin
|
|
|
|
{texture.width, texture.height, 1} // MTLSize
|
|
|
|
};
|
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
[texture replaceRegion:region
|
|
|
|
mipmapLevel:0
|
|
|
|
withBytes:[self currentBuffer]
|
2018-11-14 22:21:21 +00:00
|
|
|
bytesPerRow:texture.width * 4];
|
2020-03-26 18:54:18 +00:00
|
|
|
if ([self frameBlendingMode]) {
|
2018-06-15 10:58:50 +00:00
|
|
|
[previous_texture replaceRegion:region
|
|
|
|
mipmapLevel:0
|
|
|
|
withBytes:[self previousBuffer]
|
2018-11-14 22:21:21 +00:00
|
|
|
bytesPerRow:texture.width * 4];
|
2018-06-15 10:58:50 +00:00
|
|
|
}
|
2018-06-15 09:58:33 +00:00
|
|
|
|
|
|
|
MTLRenderPassDescriptor *render_pass_descriptor = view.currentRenderPassDescriptor;
|
|
|
|
id<MTLCommandBuffer> command_buffer = [command_queue commandBuffer];
|
|
|
|
|
2020-03-26 18:54:18 +00:00
|
|
|
if (render_pass_descriptor != nil)
|
2018-06-15 09:58:33 +00:00
|
|
|
{
|
2020-03-26 18:54:18 +00:00
|
|
|
*(GB_frame_blending_mode_t *)[frame_blending_mode_buffer contents] = [self frameBlendingMode];
|
2018-06-15 15:44:22 +00:00
|
|
|
*(vector_float2 *)[output_resolution_buffer contents] = output_resolution;
|
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
id<MTLRenderCommandEncoder> render_encoder =
|
|
|
|
[command_buffer renderCommandEncoderWithDescriptor:render_pass_descriptor];
|
|
|
|
|
|
|
|
[render_encoder setViewport:(MTLViewport){0.0, 0.0,
|
2018-06-15 15:44:22 +00:00
|
|
|
output_resolution.x,
|
|
|
|
output_resolution.y,
|
2018-06-15 09:58:33 +00:00
|
|
|
-1.0, 1.0}];
|
|
|
|
|
|
|
|
[render_encoder setRenderPipelineState:pipeline_state];
|
|
|
|
|
|
|
|
[render_encoder setVertexBuffer:vertices
|
|
|
|
offset:0
|
|
|
|
atIndex:0];
|
|
|
|
|
2020-03-26 18:54:18 +00:00
|
|
|
[render_encoder setFragmentBuffer:frame_blending_mode_buffer
|
2018-06-15 10:58:50 +00:00
|
|
|
offset:0
|
|
|
|
atIndex:0];
|
|
|
|
|
2018-06-15 15:44:22 +00:00
|
|
|
[render_encoder setFragmentBuffer:output_resolution_buffer
|
|
|
|
offset:0
|
|
|
|
atIndex:1];
|
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
[render_encoder setFragmentTexture:texture
|
|
|
|
atIndex:0];
|
|
|
|
|
2018-06-15 10:58:50 +00:00
|
|
|
[render_encoder setFragmentTexture:previous_texture
|
|
|
|
atIndex:1];
|
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
[render_encoder drawPrimitives:MTLPrimitiveTypeTriangleStrip
|
|
|
|
vertexStart:0
|
|
|
|
vertexCount:4];
|
|
|
|
|
|
|
|
[render_encoder endEncoding];
|
|
|
|
|
|
|
|
[command_buffer presentDrawable:view.currentDrawable];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[command_buffer commit];
|
|
|
|
}
|
2018-10-11 15:43:31 +00:00
|
|
|
|
|
|
|
- (void)flip
|
|
|
|
{
|
|
|
|
[super flip];
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2019-07-19 20:59:25 +00:00
|
|
|
[(MTKView *)self.internalView setNeedsDisplay:YES];
|
2018-10-11 15:43:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-15 09:58:33 +00:00
|
|
|
@end
|