From 44ca1062b0f321407a53856748fe5487749001de Mon Sep 17 00:00:00 2001 From: Stuart Carnie Date: Tue, 3 Jul 2018 22:31:51 -0700 Subject: [PATCH] formatting --- gfx/common/metal/Context.m | 12 +-- gfx/common/metal/Filter.h | 8 +- gfx/common/metal/Filter.m | 134 ++++++++++++++++-------------- gfx/common/metal/RendererCommon.h | 6 +- gfx/common/metal/RendererCommon.m | 22 ++--- gfx/common/metal/TexturedView.m | 61 ++++++++------ gfx/common/metal/View.h | 4 +- gfx/common/metal/View.m | 3 +- gfx/common/metal_common.h | 20 ++--- gfx/common/metal_common.m | 12 +-- 10 files changed, 154 insertions(+), 128 deletions(-) diff --git a/gfx/common/metal/Context.m b/gfx/common/metal/Context.m index 16e9d8142d..3e1ac87deb 100644 --- a/gfx/common/metal/Context.m +++ b/gfx/common/metal/Context.m @@ -63,28 +63,28 @@ _library = l; _commandQueue = [_device newCommandQueue]; _clearColor = MTLClearColorMake(0, 0, 0, 1); - + { MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; - + sd.label = @"NEAREST"; _samplers[TEXTURE_FILTER_NEAREST] = [d newSamplerStateWithDescriptor:sd]; - + sd.mipFilter = MTLSamplerMipFilterNearest; sd.label = @"MIPMAP_NEAREST"; _samplers[TEXTURE_FILTER_MIPMAP_NEAREST] = [d newSamplerStateWithDescriptor:sd]; - + sd.mipFilter = MTLSamplerMipFilterNotMipmapped; sd.minFilter = MTLSamplerMinMagFilterLinear; sd.magFilter = MTLSamplerMinMagFilterLinear; sd.label = @"LINEAR"; _samplers[TEXTURE_FILTER_LINEAR] = [d newSamplerStateWithDescriptor:sd]; - + sd.mipFilter = MTLSamplerMipFilterLinear; sd.label = @"MIPMAP_LINEAR"; _samplers[TEXTURE_FILTER_MIPMAP_LINEAR] = [d newSamplerStateWithDescriptor:sd]; } - + if (![self _initConversionFilters]) return nil; diff --git a/gfx/common/metal/Filter.h b/gfx/common/metal/Filter.h index d40f0b8798..82094ad67f 100644 --- a/gfx/common/metal/Filter.h +++ b/gfx/common/metal/Filter.h @@ -10,7 +10,7 @@ #import @protocol FilterDelegate --(void)configure:(id)encoder; +- (void)configure:(id)encoder; @end @interface Filter : NSObject @@ -18,9 +18,9 @@ @property (nonatomic, readwrite) id delegate; @property (nonatomic, readonly) id sampler; --(void)apply:(id)cb in:(id)tin out:(id)tout; --(void)apply:(id)cb inBuf:(id)tin outTex:(id)tout; +- (void)apply:(id)cb in:(id)tin out:(id)tout; +- (void)apply:(id)cb inBuf:(id)tin outTex:(id)tout; -+(instancetype)newFilterWithFunctionName:(NSString *)name device:(id)device library:(id)library error:(NSError **)error; ++ (instancetype)newFilterWithFunctionName:(NSString *)name device:(id)device library:(id)library error:(NSError **)error; @end diff --git a/gfx/common/metal/Filter.m b/gfx/common/metal/Filter.m index fa36811ae6..94490aff66 100644 --- a/gfx/common/metal/Filter.m +++ b/gfx/common/metal/Filter.m @@ -10,79 +10,87 @@ #import @interface Filter() --( instancetype)initWithKernel:(id)kernel sampler:(id)sampler; +- (instancetype)initWithKernel:(id)kernel sampler:(id)sampler; @end -@implementation Filter { - id _kernel; +@implementation Filter +{ + id _kernel; } -+(instancetype)newFilterWithFunctionName:(NSString *)name device:(id)device library:(id)library error:(NSError **)error { - id function = [library newFunctionWithName:name]; - id kernel = [device newComputePipelineStateWithFunction:function error:error]; - if (*error != nil) { - return nil; - } - - MTLSamplerDescriptor * sd = [MTLSamplerDescriptor new]; - sd.minFilter = MTLSamplerMinMagFilterNearest; - sd.magFilter = MTLSamplerMinMagFilterNearest; - sd.sAddressMode = MTLSamplerAddressModeClampToEdge; - sd.tAddressMode = MTLSamplerAddressModeClampToEdge; - sd.mipFilter = MTLSamplerMipFilterNotMipmapped; - id sampler = [device newSamplerStateWithDescriptor:sd]; - - return [[Filter alloc] initWithKernel:kernel sampler:sampler]; ++ (instancetype)newFilterWithFunctionName:(NSString *)name device:(id)device library:(id)library error:(NSError **)error +{ + id function = [library newFunctionWithName:name]; + id kernel = [device newComputePipelineStateWithFunction:function error:error]; + if (*error != nil) + { + return nil; + } + + MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; + sd.minFilter = MTLSamplerMinMagFilterNearest; + sd.magFilter = MTLSamplerMinMagFilterNearest; + sd.sAddressMode = MTLSamplerAddressModeClampToEdge; + sd.tAddressMode = MTLSamplerAddressModeClampToEdge; + sd.mipFilter = MTLSamplerMipFilterNotMipmapped; + id sampler = [device newSamplerStateWithDescriptor:sd]; + + return [[Filter alloc] initWithKernel:kernel sampler:sampler]; } --( instancetype)initWithKernel:(id)kernel sampler:(id)sampler { - if (self = [super init]) { - _kernel = kernel; - _sampler = sampler; - } - return self; +- (instancetype)initWithKernel:(id)kernel sampler:(id)sampler +{ + if (self = [super init]) + { + _kernel = kernel; + _sampler = sampler; + } + return self; } --(void)apply:(id)cb in:(id)tin out:(id)tout { - id ce = [cb computeCommandEncoder]; - ce.label = @"filter kernel"; - [ce pushDebugGroup:@"filter kernel"]; - - [ce setComputePipelineState:_kernel]; - - [ce setTexture:tin atIndex:0]; - [ce setTexture:tout atIndex:1]; - - [self.delegate configure:ce]; - - MTLSize size = MTLSizeMake(16, 16, 1); - MTLSize count = MTLSizeMake((tin.width + size.width + 1) / size.width, (tin.height + size.height + 1) / size.height, 1); - - [ce dispatchThreadgroups:count threadsPerThreadgroup:size]; - - [ce popDebugGroup]; - [ce endEncoding]; +- (void)apply:(id)cb in:(id)tin out:(id)tout +{ + id ce = [cb computeCommandEncoder]; + ce.label = @"filter kernel"; + [ce pushDebugGroup:@"filter kernel"]; + + [ce setComputePipelineState:_kernel]; + + [ce setTexture:tin atIndex:0]; + [ce setTexture:tout atIndex:1]; + + [self.delegate configure:ce]; + + MTLSize size = MTLSizeMake(16, 16, 1); + MTLSize count = MTLSizeMake((tin.width + size.width + 1) / size.width, (tin.height + size.height + 1) / size.height, + 1); + + [ce dispatchThreadgroups:count threadsPerThreadgroup:size]; + + [ce popDebugGroup]; + [ce endEncoding]; } --(void)apply:(id)cb inBuf:(id)tin outTex:(id)tout { - id ce = [cb computeCommandEncoder]; - ce.label = @"filter kernel"; - [ce pushDebugGroup:@"filter kernel"]; - - [ce setComputePipelineState:_kernel]; - - [ce setBuffer:tin offset:0 atIndex:0]; - [ce setTexture:tout atIndex:0]; - - [self.delegate configure:ce]; - - MTLSize size = MTLSizeMake(32, 1, 1); - MTLSize count = MTLSizeMake((tin.length + 00) / 32, 1, 1); - - [ce dispatchThreadgroups:count threadsPerThreadgroup:size]; - - [ce popDebugGroup]; - [ce endEncoding]; +- (void)apply:(id)cb inBuf:(id)tin outTex:(id)tout +{ + id ce = [cb computeCommandEncoder]; + ce.label = @"filter kernel"; + [ce pushDebugGroup:@"filter kernel"]; + + [ce setComputePipelineState:_kernel]; + + [ce setBuffer:tin offset:0 atIndex:0]; + [ce setTexture:tout atIndex:0]; + + [self.delegate configure:ce]; + + MTLSize size = MTLSizeMake(32, 1, 1); + MTLSize count = MTLSizeMake((tin.length + 00) / 32, 1, 1); + + [ce dispatchThreadgroups:count threadsPerThreadgroup:size]; + + [ce popDebugGroup]; + [ce endEncoding]; } @end diff --git a/gfx/common/metal/RendererCommon.h b/gfx/common/metal/RendererCommon.h index 2661ace833..49c3f74c66 100644 --- a/gfx/common/metal/RendererCommon.h +++ b/gfx/common/metal/RendererCommon.h @@ -27,7 +27,8 @@ #pragma mark - Pixel Formats -typedef NS_ENUM(NSUInteger, RPixelFormat) { +typedef NS_ENUM(NSUInteger, RPixelFormat) +{ RPixelFormatInvalid, @@ -44,7 +45,8 @@ typedef NS_ENUM(NSUInteger, RPixelFormat) { extern NSUInteger RPixelFormatToBPP(RPixelFormat format); extern NSString *NSStringFromRPixelFormat(RPixelFormat format); -typedef NS_ENUM(NSUInteger, RTextureFilter) { +typedef NS_ENUM(NSUInteger, RTextureFilter) +{ RTextureFilterNearest, RTextureFilterLinear, diff --git a/gfx/common/metal/RendererCommon.m b/gfx/common/metal/RendererCommon.m index 3751d20e2d..aca474a401 100644 --- a/gfx/common/metal/RendererCommon.m +++ b/gfx/common/metal/RendererCommon.m @@ -11,15 +11,16 @@ NSUInteger RPixelFormatToBPP(RPixelFormat format) { - switch (format) { + switch (format) + { case RPixelFormatBGRA8Unorm: case RPixelFormatBGRX8Unorm: return 4; - + case RPixelFormatB5G6R5Unorm: case RPixelFormatBGRA4Unorm: return 2; - + default: RARCH_ERR("[Metal]: unknown RPixel format: %d\n", format); return 4; @@ -40,13 +41,14 @@ NSString *NSStringFromRPixelFormat(RPixelFormat format) STRING(RPixelFormatBGRA8Unorm); STRING(RPixelFormatBGRX8Unorm); #undef STRING - + }); - - if (format >= RPixelFormatCount) { + + if (format >= RPixelFormatCount) + { format = RPixelFormatInvalid; } - + return RPixelStrings[format]; } @@ -54,19 +56,19 @@ matrix_float4x4 matrix_proj_ortho(float left, float right, float top, float bott { float near = 0; float far = 1; - + float sx = 2 / (right - left); float sy = 2 / (top - bottom); float sz = 1 / (far - near); float tx = (right + left) / (left - right); float ty = (top + bottom) / (bottom - top); float tz = near / (far - near); - + simd_float4 P = simd_make_float4(sx, 0, 0, 0); simd_float4 Q = simd_make_float4(0, sy, 0, 0); simd_float4 R = simd_make_float4(0, 0, sz, 0); simd_float4 S = simd_make_float4(tx, ty, tz, 1); - + matrix_float4x4 mat = {P, Q, R, S}; return mat; } diff --git a/gfx/common/metal/TexturedView.m b/gfx/common/metal/TexturedView.m index 830e8728b1..22ff31cf31 100644 --- a/gfx/common/metal/TexturedView.m +++ b/gfx/common/metal/TexturedView.m @@ -17,7 +17,7 @@ CGSize _size; // size of view in pixels CGRect _frame; NSUInteger _bpp; - + id _pixels; // frame buffer in _srcFmt bool _pixelsDirty; } @@ -25,15 +25,19 @@ - (instancetype)initWithDescriptor:(ViewDescriptor *)d context:(Context *)c { self = [super init]; - if (self) { + if (self) + { _format = d.format; _bpp = RPixelFormatToBPP(_format); _filter = d.filter; _context = c; _visible = YES; - if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) { + if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) + { _drawState = ViewDrawStateEncoder; - } else { + } + else + { _drawState = ViewDrawStateAll; } self.size = d.size; @@ -44,12 +48,13 @@ - (void)setSize:(CGSize)size { - if (CGSizeEqualToSize(_size, size)) { + if (CGSizeEqualToSize(_size, size)) + { return; } - + _size = size; - + // create new texture { MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm @@ -59,8 +64,9 @@ td.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite; _texture = [_context.device newTextureWithDescriptor:td]; } - - if (_format != RPixelFormatBGRA8Unorm && _format != RPixelFormatBGRX8Unorm) { + + if (_format != RPixelFormatBGRA8Unorm && _format != RPixelFormatBGRX8Unorm) + { _pixels = [_context.device newBufferWithLength:(NSUInteger)(size.width * size.height * 2) options:MTLResourceStorageModeManaged]; } @@ -73,21 +79,22 @@ - (void)setFrame:(CGRect)frame { - if (CGRectEqualToRect(_frame, frame)) { + if (CGRectEqualToRect(_frame, frame)) + { return; } - + _frame = frame; - + // update vertices CGPoint o = frame.origin; CGSize s = frame.size; - + float l = o.x; float t = o.y; float r = o.x + s.width; float b = o.y + s.height; - + Vertex v[4] = { {{l, b, 0}, {0, 1}}, {{r, b, 0}, {1, 1}}, @@ -106,15 +113,16 @@ { if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) return; - + if (!_pixelsDirty) return; - + [_context convertFormat:_format from:_pixels to:_texture]; _pixelsDirty = NO; } -- (void)drawWithContext:(Context *)ctx { +- (void)drawWithContext:(Context *)ctx +{ [self _convertFormat]; } @@ -127,27 +135,32 @@ - (void)updateFrame:(void const *)src pitch:(NSUInteger)pitch { - if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) { + if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) + { [_texture replaceRegion:MTLRegionMake2D(0, 0, (NSUInteger)_size.width, (NSUInteger)_size.height) mipmapLevel:0 withBytes:src bytesPerRow:(NSUInteger)(4 * pitch)]; } - else { + else + { void *dst = _pixels.contents; size_t len = (size_t)(_bpp * _size.width); assert(len <= pitch); // the length can't be larger? - - if (len < pitch) { - for (int i = 0; i < _size.height; i++) { + + if (len < pitch) + { + for (int i = 0; i < _size.height; i++) + { memcpy(dst, src, len); dst += len; src += pitch; } } - else { + else + { memcpy(dst, src, _pixels.length); } - + [_pixels didModifyRange:NSMakeRange(0, _pixels.length)]; _pixelsDirty = YES; } diff --git a/gfx/common/metal/View.h b/gfx/common/metal/View.h index b8f62439a1..ce1485a31a 100644 --- a/gfx/common/metal/View.h +++ b/gfx/common/metal/View.h @@ -12,11 +12,11 @@ typedef NS_ENUM(NSInteger, ViewDrawState) { - ViewDrawStateNone = 0x00, + ViewDrawStateNone = 0x00, ViewDrawStateContext = 0x01, ViewDrawStateEncoder = 0x02, - ViewDrawStateAll = 0x03, + ViewDrawStateAll = 0x03, }; @interface ViewDescriptor : NSObject diff --git a/gfx/common/metal/View.m b/gfx/common/metal/View.m index db7c29bb32..475983bc86 100644 --- a/gfx/common/metal/View.m +++ b/gfx/common/metal/View.m @@ -14,7 +14,8 @@ - (instancetype)init { self = [super init]; - if (self) { + if (self) + { _format = RPixelFormatBGRA8Unorm; } return self; diff --git a/gfx/common/metal_common.h b/gfx/common/metal_common.h index 7b4dacda0d..1ba4d73c77 100644 --- a/gfx/common/metal_common.h +++ b/gfx/common/metal_common.h @@ -34,8 +34,8 @@ extern MTLPixelFormat SelectOptimalPixelFormat(MTLPixelFormat fmt); @property (nonatomic, readwrite) CGRect frame; @property (nonatomic, readwrite) CGSize size; @property (nonatomic, readonly) ViewDrawState drawState; -@property (nonatomic, readonly) struct video_shader* shader; -@property (nonatomic, readwrite) uint64_t frameCount; +@property (nonatomic, readonly) struct video_shader *shader; +@property (nonatomic, readwrite) uint64_t frameCount; - (void)setFilteringIndex:(int)index smooth:(bool)smooth; - (BOOL)setShaderFromPath:(NSString *)path; @@ -60,14 +60,14 @@ extern MTLPixelFormat SelectOptimalPixelFormat(MTLPixelFormat fmt); @interface MetalDriver : NSObject -@property (nonatomic, readonly) video_viewport_t* viewport; -@property (nonatomic, readwrite) bool keepAspect; -@property (nonatomic, readonly) MetalMenu* menu; -@property (nonatomic, readonly) FrameView* frameView; -@property (nonatomic, readonly) MenuDisplay* display; -@property (nonatomic, readonly) Context* context; -@property (nonatomic, readonly) Uniforms* viewportMVP; -@property (nonatomic, readonly) Uniforms* viewportMVPNormalized; +@property (nonatomic, readonly) video_viewport_t *viewport; +@property (nonatomic, readwrite) bool keepAspect; +@property (nonatomic, readonly) MetalMenu *menu; +@property (nonatomic, readonly) FrameView *frameView; +@property (nonatomic, readonly) MenuDisplay *display; +@property (nonatomic, readonly) Context *context; +@property (nonatomic, readonly) Uniforms *viewportMVP; +@property (nonatomic, readonly) Uniforms *viewportMVPNormalized; - (instancetype)initWithVideo:(const video_info_t *)video input:(const input_driver_t **)input diff --git a/gfx/common/metal_common.m b/gfx/common/metal_common.m index 01fb722b8d..a975c06f7f 100644 --- a/gfx/common/metal_common.m +++ b/gfx/common/metal_common.m @@ -249,9 +249,9 @@ RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); return NO; } - + MTLFunctionConstantValues *vals; - + psd.label = @"snow_simple"; ca.blendingEnabled = YES; { @@ -272,7 +272,7 @@ RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); return NO; } - + psd.label = @"snow"; ca.blendingEnabled = YES; { @@ -293,7 +293,7 @@ RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); return NO; } - + psd.label = @"bokeh"; ca.blendingEnabled = YES; psd.fragmentFunction = [_library newFunctionWithName:@"bokeh_fragment"]; @@ -303,7 +303,7 @@ RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); return NO; } - + psd.label = @"snowflake"; ca.blendingEnabled = YES; psd.fragmentFunction = [_library newFunctionWithName:@"snowflake_fragment"]; @@ -313,7 +313,7 @@ RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); return NO; } - + psd.label = @"ribbon"; ca.blendingEnabled = NO; psd.vertexFunction = [_library newFunctionWithName:@"ribbon_vertex"];