(Metal) Small cleanups

This commit is contained in:
reallibretroadmin 2023-01-08 05:34:05 +01:00
parent 2b0c86f1f5
commit 29f165026a
3 changed files with 156 additions and 219 deletions

View File

@ -24,7 +24,6 @@
#include "metal_shader_types.h" #include "metal_shader_types.h"
#include "../../gfx_display.h" #include "../../gfx_display.h"
#include "../../../retroarch.h"
/* TODO/FIXME: implement triple buffering */ /* TODO/FIXME: implement triple buffering */
/*! @brief maximum inflight frames */ /*! @brief maximum inflight frames */

View File

@ -37,20 +37,10 @@ static NSString *RPixelStrings[RPixelFormatCount];
NSUInteger RPixelFormatToBPP(RPixelFormat format) NSUInteger RPixelFormatToBPP(RPixelFormat format)
{ {
switch (format) if ( format == RPixelFormatB5G6R5Unorm
{ || format == RPixelFormatBGRA4Unorm )
case RPixelFormatBGRA8Unorm: return 2;
case RPixelFormatBGRX8Unorm: return 4;
return 4;
case RPixelFormatB5G6R5Unorm:
case RPixelFormatBGRA4Unorm:
return 2;
default:
RARCH_ERR("[Metal]: unknown RPixel format: %d\n", format);
return 4;
}
} }
static NSString *NSStringFromRPixelFormat(RPixelFormat format) static NSString *NSStringFromRPixelFormat(RPixelFormat format)
@ -69,44 +59,48 @@ static NSString *NSStringFromRPixelFormat(RPixelFormat format)
}); });
if (format >= RPixelFormatCount) if (format >= RPixelFormatCount)
{
format = RPixelFormatInvalid; format = RPixelFormatInvalid;
}
return RPixelStrings[format]; return RPixelStrings[format];
} }
matrix_float4x4 make_matrix_float4x4(const float *v) matrix_float4x4 make_matrix_float4x4(const float *v)
{ {
simd_float4 P = simd_make_float4(v[0], v[1], v[2], v[3]); simd_float4 P = simd_make_float4(v[0], v[1], v[2], v[3]);
v += 4; v += 4;
simd_float4 Q = simd_make_float4(v[0], v[1], v[2], v[3]); simd_float4 Q = simd_make_float4(v[0], v[1], v[2], v[3]);
v += 4; v += 4;
simd_float4 R = simd_make_float4(v[0], v[1], v[2], v[3]); simd_float4 R = simd_make_float4(v[0], v[1], v[2], v[3]);
v += 4; v += 4;
simd_float4 S = simd_make_float4(v[0], v[1], v[2], v[3]); simd_float4 S = simd_make_float4(v[0], v[1], v[2], v[3]);
matrix_float4x4 mat = {P, Q, R, S}; matrix_float4x4 mat = {P, Q, R, S};
return mat; return mat;
} }
matrix_float4x4 matrix_proj_ortho(float left, float right, float top, float bottom) matrix_float4x4 matrix_proj_ortho(float left, float right, float top, float bottom)
{ {
float near = 0; #if 0
float far = 1; float near = 0;
float far = 1;
float sx = 2 / (right - left); float sx = 2 / (right - left);
float sy = 2 / (top - bottom); float sy = 2 / (top - bottom);
float sz = 1 / (far - near); float sz = 1 / (far - near);
float tx = (right + left) / (left - right); float tx = (right + left) / (left - right);
float ty = (top + bottom) / (bottom - top); float ty = (top + bottom) / (bottom - top);
float tz = near / (far - near); float tz = near / (far - near);
simd_float4 P = simd_make_float4(sx, 0, 0, 0);
simd_float4 P = simd_make_float4(sx, 0, 0, 0); simd_float4 Q = simd_make_float4(0, sy, 0, 0);
simd_float4 Q = simd_make_float4(0, sy, 0, 0); simd_float4 R = simd_make_float4(0, 0, sz, 0);
simd_float4 R = simd_make_float4(0, 0, sz, 0); simd_float4 S = simd_make_float4(tx, ty, tz, 1);
simd_float4 S = simd_make_float4(tx, ty, tz, 1); #else
float sx = 2 / (right - left);
float sy = 2 / (top - bottom);
float tx = (right + left) / (left - right);
float ty = (top + bottom) / (bottom - top);
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, 1, 0);
simd_float4 S = simd_make_float4(tx, ty, 0, 1);
#endif
matrix_float4x4 mat = {P, Q, R, S}; matrix_float4x4 mat = {P, Q, R, S};
return mat; return mat;
} }
@ -190,6 +184,8 @@ matrix_float4x4 matrix_rotate_z(float rot)
{ {
if (self = [super init]) if (self = [super init])
{ {
int i;
_inflightSemaphore = dispatch_semaphore_create(MAX_INFLIGHT); _inflightSemaphore = dispatch_semaphore_create(MAX_INFLIGHT);
_device = d; _device = d;
_layer = layer; _layer = layer;
@ -237,10 +233,8 @@ matrix_float4x4 matrix_rotate_z(float rot)
if (![self _initMenuStates]) if (![self _initMenuStates])
return nil; return nil;
for (int i = 0; i < CHAIN_LENGTH; i++) for (i = 0; i < CHAIN_LENGTH; i++)
{
_chain[i] = [[BufferChain alloc] initWithDevice:_device blockLen:65536]; _chain[i] = [[BufferChain alloc] initWithDevice:_device blockLen:65536];
}
} }
return self; return self;
} }
@ -252,7 +246,7 @@ matrix_float4x4 matrix_rotate_z(float rot)
- (void)setViewport:(video_viewport_t *)viewport - (void)setViewport:(video_viewport_t *)viewport
{ {
_viewport = *viewport; _viewport = *viewport;
_uniforms.outputSize = simd_make_float2(_viewport.full_width, _viewport.full_height); _uniforms.outputSize = simd_make_float2(_viewport.full_width, _viewport.full_height);
} }
@ -263,11 +257,9 @@ matrix_float4x4 matrix_rotate_z(float rot)
- (void)setRotation:(unsigned)rotation - (void)setRotation:(unsigned)rotation
{ {
_rotation = 270 * rotation; _rotation = 270 * rotation;
/* Calculate projection. */ /* Calculate projection. */
_mvp_no_rot = matrix_proj_ortho(0, 1, 0, 1); _mvp_no_rot = matrix_proj_ortho(0, 1, 0, 1);
bool allow_rotate = true; bool allow_rotate = true;
if (!allow_rotate) if (!allow_rotate)
{ {
@ -276,9 +268,8 @@ matrix_float4x4 matrix_rotate_z(float rot)
} }
matrix_float4x4 rot = matrix_rotate_z((float)(M_PI * _rotation / 180.0f)); matrix_float4x4 rot = matrix_rotate_z((float)(M_PI * _rotation / 180.0f));
_mvp = simd_mul(rot, _mvp_no_rot); _mvp = simd_mul(rot, _mvp_no_rot);
_uniforms.projectionMatrix = _mvp;
_uniforms.projectionMatrix = _mvp;
_uniformsNoRotate.projectionMatrix = _mvp_no_rot; _uniformsNoRotate.projectionMatrix = _mvp_no_rot;
} }
@ -331,24 +322,24 @@ matrix_float4x4 matrix_rotate_z(float rot)
vd.attributes[1].format = MTLVertexFormatFloat2; vd.attributes[1].format = MTLVertexFormatFloat2;
vd.attributes[2].offset = offsetof(SpriteVertex, color); vd.attributes[2].offset = offsetof(SpriteVertex, color);
vd.attributes[2].format = MTLVertexFormatFloat4; vd.attributes[2].format = MTLVertexFormatFloat4;
vd.layouts[0].stride = sizeof(SpriteVertex); vd.layouts[0].stride = sizeof(SpriteVertex);
return vd; return vd;
} }
- (bool)_initClearState - (bool)_initClearState
{ {
MTLVertexDescriptor *vd = [self _spriteVertexDescriptor]; NSError *err;
MTLVertexDescriptor *vd = [self _spriteVertexDescriptor];
MTLRenderPipelineDescriptor *psd = [MTLRenderPipelineDescriptor new]; MTLRenderPipelineDescriptor *psd = [MTLRenderPipelineDescriptor new];
psd.label = @"clear_state"; psd.label = @"clear_state";
MTLRenderPipelineColorAttachmentDescriptor *ca = psd.colorAttachments[0]; MTLRenderPipelineColorAttachmentDescriptor *ca = psd.colorAttachments[0];
ca.pixelFormat = _layer.pixelFormat; ca.pixelFormat = _layer.pixelFormat;
psd.vertexDescriptor = vd; psd.vertexDescriptor = vd;
psd.vertexFunction = [_library newFunctionWithName:@"stock_vertex"]; psd.vertexFunction = [_library newFunctionWithName:@"stock_vertex"];
psd.fragmentFunction = [_library newFunctionWithName:@"stock_fragment_color"]; psd.fragmentFunction = [_library newFunctionWithName:@"stock_fragment_color"];
NSError *err;
_clearState = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _clearState = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -361,24 +352,24 @@ matrix_float4x4 matrix_rotate_z(float rot)
- (bool)_initMenuStates - (bool)_initMenuStates
{ {
MTLVertexDescriptor *vd = [self _spriteVertexDescriptor]; NSError *err;
MTLVertexDescriptor *vd = [self _spriteVertexDescriptor];
MTLRenderPipelineDescriptor *psd = [MTLRenderPipelineDescriptor new]; MTLRenderPipelineDescriptor *psd = [MTLRenderPipelineDescriptor new];
psd.label = @"stock"; psd.label = @"stock";
MTLRenderPipelineColorAttachmentDescriptor *ca = psd.colorAttachments[0]; MTLRenderPipelineColorAttachmentDescriptor *ca = psd.colorAttachments[0];
ca.pixelFormat = _layer.pixelFormat; ca.pixelFormat = _layer.pixelFormat;
ca.blendingEnabled = NO; ca.blendingEnabled = NO;
ca.sourceRGBBlendFactor = MTLBlendFactorSourceAlpha; ca.sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
ca.destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha; ca.destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
ca.sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha; ca.sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
ca.destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha; ca.destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
psd.sampleCount = 1; psd.sampleCount = 1;
psd.vertexDescriptor = vd; psd.vertexDescriptor = vd;
psd.vertexFunction = [_library newFunctionWithName:@"stock_vertex"]; psd.vertexFunction = [_library newFunctionWithName:@"stock_vertex"];
psd.fragmentFunction = [_library newFunctionWithName:@"stock_fragment"]; psd.fragmentFunction = [_library newFunctionWithName:@"stock_fragment"];
NSError *err;
_states[VIDEO_SHADER_STOCK_BLEND][0] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_STOCK_BLEND][0] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -386,8 +377,8 @@ matrix_float4x4 matrix_rotate_z(float rot)
return NO; return NO;
} }
psd.label = @"stock_blend"; psd.label = @"stock_blend";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
_states[VIDEO_SHADER_STOCK_BLEND][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_STOCK_BLEND][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -397,10 +388,10 @@ matrix_float4x4 matrix_rotate_z(float rot)
MTLFunctionConstantValues *vals; MTLFunctionConstantValues *vals;
psd.label = @"snow_simple"; psd.label = @"snow_simple";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
{ {
vals = [MTLFunctionConstantValues new]; vals = [MTLFunctionConstantValues new];
float values[3] = { float values[3] = {
1.25f, /* baseScale */ 1.25f, /* baseScale */
0.50f, /* density */ 0.50f, /* density */
@ -418,10 +409,10 @@ matrix_float4x4 matrix_rotate_z(float rot)
return NO; return NO;
} }
psd.label = @"snow"; psd.label = @"snow";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
{ {
vals = [MTLFunctionConstantValues new]; vals = [MTLFunctionConstantValues new];
float values[3] = { float values[3] = {
3.50f, /* baseScale */ 3.50f, /* baseScale */
0.70f, /* density */ 0.70f, /* density */
@ -439,9 +430,9 @@ matrix_float4x4 matrix_rotate_z(float rot)
return NO; return NO;
} }
psd.label = @"bokeh"; psd.label = @"bokeh";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
psd.fragmentFunction = [_library newFunctionWithName:@"bokeh_fragment"]; psd.fragmentFunction = [_library newFunctionWithName:@"bokeh_fragment"];
_states[VIDEO_SHADER_MENU_5][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_MENU_5][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -449,9 +440,9 @@ matrix_float4x4 matrix_rotate_z(float rot)
return NO; return NO;
} }
psd.label = @"snowflake"; psd.label = @"snowflake";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
psd.fragmentFunction = [_library newFunctionWithName:@"snowflake_fragment"]; psd.fragmentFunction = [_library newFunctionWithName:@"snowflake_fragment"];
_states[VIDEO_SHADER_MENU_6][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_MENU_6][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -459,32 +450,32 @@ matrix_float4x4 matrix_rotate_z(float rot)
return NO; return NO;
} }
psd.label = @"ribbon"; psd.label = @"ribbon";
ca.blendingEnabled = NO; ca.blendingEnabled = NO;
psd.vertexFunction = [_library newFunctionWithName:@"ribbon_vertex"]; psd.vertexFunction = [_library newFunctionWithName:@"ribbon_vertex"];
psd.fragmentFunction = [_library newFunctionWithName:@"ribbon_fragment"]; psd.fragmentFunction = [_library newFunctionWithName:@"ribbon_fragment"];
_states[VIDEO_SHADER_MENU][0] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_MENU][0] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String);
return NO; return NO;
} }
psd.label = @"ribbon_blend"; psd.label = @"ribbon_blend";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
ca.sourceRGBBlendFactor = MTLBlendFactorOne; ca.sourceRGBBlendFactor = MTLBlendFactorOne;
ca.destinationRGBBlendFactor = MTLBlendFactorOne; ca.destinationRGBBlendFactor = MTLBlendFactorOne;
_states[VIDEO_SHADER_MENU][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_MENU][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String); RARCH_ERR("[Metal]: error creating pipeline state %s\n", err.localizedDescription.UTF8String);
return NO; return NO;
} }
psd.label = @"ribbon_simple"; psd.label = @"ribbon_simple";
ca.blendingEnabled = NO; ca.blendingEnabled = NO;
psd.vertexFunction = [_library newFunctionWithName:@"ribbon_simple_vertex"]; psd.vertexFunction = [_library newFunctionWithName:@"ribbon_simple_vertex"];
psd.fragmentFunction = [_library newFunctionWithName:@"ribbon_simple_fragment"]; psd.fragmentFunction = [_library newFunctionWithName:@"ribbon_simple_fragment"];
_states[VIDEO_SHADER_MENU_2][0] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_MENU_2][0] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -492,10 +483,10 @@ matrix_float4x4 matrix_rotate_z(float rot)
return NO; return NO;
} }
psd.label = @"ribbon_simple_blend"; psd.label = @"ribbon_simple_blend";
ca.blendingEnabled = YES; ca.blendingEnabled = YES;
ca.sourceRGBBlendFactor = MTLBlendFactorOne; ca.sourceRGBBlendFactor = MTLBlendFactorOne;
ca.destinationRGBBlendFactor = MTLBlendFactorOne; ca.destinationRGBBlendFactor = MTLBlendFactorOne;
_states[VIDEO_SHADER_MENU_2][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err]; _states[VIDEO_SHADER_MENU_2][1] = [_device newRenderPipelineStateWithDescriptor:psd error:&err];
if (err != nil) if (err != nil)
{ {
@ -510,9 +501,9 @@ matrix_float4x4 matrix_rotate_z(float rot)
{ {
NSError *err = nil; NSError *err = nil;
_filters[RPixelFormatBGRA4Unorm] = [Filter newFilterWithFunctionName:@"convert_bgra4444_to_bgra8888" _filters[RPixelFormatBGRA4Unorm] = [Filter newFilterWithFunctionName:@"convert_bgra4444_to_bgra8888"
device:_device device:_device
library:_library library:_library
error:&err]; error:&err];
if (err) if (err)
{ {
RARCH_LOG("[Metal]: unable to create 'convert_bgra4444_to_bgra8888' conversion filter: %s\n", RARCH_LOG("[Metal]: unable to create 'convert_bgra4444_to_bgra8888' conversion filter: %s\n",
@ -521,9 +512,9 @@ matrix_float4x4 matrix_rotate_z(float rot)
} }
_filters[RPixelFormatB5G6R5Unorm] = [Filter newFilterWithFunctionName:@"convert_rgb565_to_bgra8888" _filters[RPixelFormatB5G6R5Unorm] = [Filter newFilterWithFunctionName:@"convert_rgb565_to_bgra8888"
device:_device device:_device
library:_library library:_library
error:&err]; error:&err];
if (err) if (err)
{ {
RARCH_LOG("[Metal]: unable to create 'convert_rgb565_to_bgra8888' conversion filter: %s\n", RARCH_LOG("[Metal]: unable to create 'convert_rgb565_to_bgra8888' conversion filter: %s\n",
@ -563,25 +554,23 @@ matrix_float4x4 matrix_rotate_z(float rot)
} }
BOOL mipmapped = filter == TEXTURE_FILTER_MIPMAP_LINEAR || filter == TEXTURE_FILTER_MIPMAP_NEAREST; BOOL mipmapped = filter == TEXTURE_FILTER_MIPMAP_LINEAR || filter == TEXTURE_FILTER_MIPMAP_NEAREST;
Texture *tex = [Texture new];
Texture *tex = [Texture new]; tex.texture = [self newTexture:image mipmapped:mipmapped];
tex.texture = [self newTexture:image mipmapped:mipmapped]; tex.sampler = _samplers[filter];
tex.sampler = _samplers[filter];
return tex; return tex;
} }
- (id<MTLTexture>)newTexture:(struct texture_image)image mipmapped:(bool)mipmapped - (id<MTLTexture>)newTexture:(struct texture_image)image mipmapped:(bool)mipmapped
{ {
MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
width:image.width width:image.width
height:image.height height:image.height
mipmapped:mipmapped]; mipmapped:mipmapped];
id<MTLTexture> t = [_device newTextureWithDescriptor:td]; id<MTLTexture> t = [_device newTextureWithDescriptor:td];
[t replaceRegion:MTLRegionMake2D(0, 0, image.width, image.height) [t replaceRegion:MTLRegionMake2D(0, 0, image.width, image.height)
mipmapLevel:0 mipmapLevel:0
withBytes:image.pixels withBytes:image.pixels
bytesPerRow:4 * image.width]; bytesPerRow:4 * image.width];
if (mipmapped) if (mipmapped)
@ -598,9 +587,7 @@ matrix_float4x4 matrix_rotate_z(float rot)
- (id<CAMetalDrawable>)nextDrawable - (id<CAMetalDrawable>)nextDrawable
{ {
if (_drawable == nil) if (_drawable == nil)
{
_drawable = _layer.nextDrawable; _drawable = _layer.nextDrawable;
}
return _drawable; return _drawable;
} }
@ -615,8 +602,9 @@ matrix_float4x4 matrix_rotate_z(float rot)
- (id<MTLCommandBuffer>)blitCommandBuffer - (id<MTLCommandBuffer>)blitCommandBuffer
{ {
if (!_blitCommandBuffer) { if (!_blitCommandBuffer)
_blitCommandBuffer = [_commandQueue commandBuffer]; {
_blitCommandBuffer = [_commandQueue commandBuffer];
_blitCommandBuffer.label = @"Blit command buffer"; _blitCommandBuffer.label = @"Blit command buffer";
} }
return _blitCommandBuffer; return _blitCommandBuffer;
@ -663,10 +651,10 @@ matrix_float4x4 matrix_rotate_z(float rot)
mipmapLevel:0]; mipmapLevel:0];
NSUInteger srcStride = _backBuffer.width * 4; NSUInteger srcStride = _backBuffer.width * 4;
uint8_t const *src = tmp + (_viewport.y * srcStride); uint8_t const *src = tmp + (_viewport.y * srcStride);
NSUInteger dstStride = _viewport.width * 3; NSUInteger dstStride = _viewport.width * 3;
uint8_t *dst = buffer + (_viewport.height - 1) * dstStride; uint8_t *dst = buffer + (_viewport.height - 1) * dstStride;
for (int y = 0; y < _viewport.height; y++, src += srcStride, dst -= dstStride) for (int y = 0; y < _viewport.height; y++, src += srcStride, dst -= dstStride)
{ {
@ -702,10 +690,8 @@ matrix_float4x4 matrix_rotate_z(float rot)
rpd.colorAttachments[0].loadAction = MTLLoadActionClear; rpd.colorAttachments[0].loadAction = MTLLoadActionClear;
rpd.colorAttachments[0].texture = self.nextDrawable.texture; rpd.colorAttachments[0].texture = self.nextDrawable.texture;
if (_captureEnabled) if (_captureEnabled)
{
_backBuffer = self.nextDrawable.texture; _backBuffer = self.nextDrawable.texture;
} _rce = [_commandBuffer renderCommandEncoderWithDescriptor:rpd];
_rce = [_commandBuffer renderCommandEncoderWithDescriptor:rpd];
_rce.label = @"Frame command encoder"; _rce.label = @"Frame command encoder";
} }
return _rce; return _rce;
@ -818,9 +804,7 @@ matrix_float4x4 matrix_rotate_z(float rot)
- (instancetype)initWithBuffer:(id<MTLBuffer>)src - (instancetype)initWithBuffer:(id<MTLBuffer>)src
{ {
if (self = [super init]) if (self = [super init])
{
_src = src; _src = src;
}
return self; return self;
} }
@ -848,7 +832,7 @@ static const NSUInteger kConstantAlignment = 4;
{ {
if (self = [super init]) if (self = [super init])
{ {
_device = device; _device = device;
_blockLen = blockLen; _blockLen = blockLen;
} }
return self; return self;
@ -880,16 +864,15 @@ static const NSUInteger kConstantAlignment = 4;
- (bool)allocRange:(BufferRange *)range length:(NSUInteger)length - (bool)allocRange:(BufferRange *)range length:(NSUInteger)length
{ {
MTLResourceOptions opts; MTLResourceOptions opts = PLATFORM_METAL_RESOURCE_STORAGE_MODE;
opts = PLATFORM_METAL_RESOURCE_STORAGE_MODE;
memset(range, 0, sizeof(*range)); memset(range, 0, sizeof(*range));
if (!_head) if (!_head)
{ {
_head = [[BufferNode alloc] initWithBuffer:[_device newBufferWithLength:_blockLen options:opts]]; _head = [[BufferNode alloc] initWithBuffer:[_device newBufferWithLength:_blockLen options:opts]];
_length += _blockLen; _length += _blockLen;
_current = _head; _current = _head;
_offset = 0; _offset = 0;
} }
if ([self _subAllocRange:range length:length]) if ([self _subAllocRange:range length:length])
@ -920,20 +903,20 @@ static const NSUInteger kConstantAlignment = 4;
- (void)_nextNode - (void)_nextNode
{ {
_current = _current.next; _current = _current.next;
_offset = 0; _offset = 0;
} }
- (BOOL)_subAllocRange:(BufferRange *)range length:(NSUInteger)length - (BOOL)_subAllocRange:(BufferRange *)range length:(NSUInteger)length
{ {
NSUInteger nextOffset = _offset + length; NSUInteger nextOffset = _offset + length;
if (nextOffset <= _current.src.length) if (nextOffset <= _current.src.length)
{ {
_current.allocated = nextOffset; _current.allocated = nextOffset;
_allocated += length; _allocated += length;
range->data = _current.src.contents + _offset; range->data = _current.src.contents + _offset;
range->buffer = _current.src; range->buffer = _current.src;
range->offset = _offset; range->offset = _offset;
_offset = MTL_ALIGN_BUFFER(nextOffset); _offset = MTL_ALIGN_BUFFER(nextOffset);
return YES; return YES;
} }
return NO; return NO;
@ -959,16 +942,14 @@ static const NSUInteger kConstantAlignment = 4;
id<MTLFunction> function = [library newFunctionWithName:name]; id<MTLFunction> function = [library newFunctionWithName:name];
id<MTLComputePipelineState> kernel = [device newComputePipelineStateWithFunction:function error:error]; id<MTLComputePipelineState> kernel = [device newComputePipelineStateWithFunction:function error:error];
if (*error != nil) if (*error != nil)
{
return nil; return nil;
}
MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new];
sd.minFilter = MTLSamplerMinMagFilterNearest; sd.minFilter = MTLSamplerMinMagFilterNearest;
sd.magFilter = MTLSamplerMinMagFilterNearest; sd.magFilter = MTLSamplerMinMagFilterNearest;
sd.sAddressMode = MTLSamplerAddressModeClampToEdge; sd.sAddressMode = MTLSamplerAddressModeClampToEdge;
sd.tAddressMode = MTLSamplerAddressModeClampToEdge; sd.tAddressMode = MTLSamplerAddressModeClampToEdge;
sd.mipFilter = MTLSamplerMipFilterNotMipmapped; sd.mipFilter = MTLSamplerMipFilterNotMipmapped;
id<MTLSamplerState> sampler = [device newSamplerStateWithDescriptor:sd]; id<MTLSamplerState> sampler = [device newSamplerStateWithDescriptor:sd];
return [[Filter alloc] initWithKernel:kernel sampler:sampler]; return [[Filter alloc] initWithKernel:kernel sampler:sampler];
@ -978,7 +959,7 @@ static const NSUInteger kConstantAlignment = 4;
{ {
if (self = [super init]) if (self = [super init])
{ {
_kernel = kernel; _kernel = kernel;
_sampler = sampler; _sampler = sampler;
} }
return self; return self;
@ -996,12 +977,11 @@ static const NSUInteger kConstantAlignment = 4;
[self.delegate configure:ce]; [self.delegate configure:ce];
MTLSize size = MTLSizeMake(16, 16, 1); MTLSize size = MTLSizeMake(16, 16, 1);
MTLSize count = MTLSizeMake((tin.width + size.width + 1) / size.width, (tin.height + size.height + 1) / size.height, MTLSize count = MTLSizeMake((tin.width + size.width + 1) / size.width, (tin.height + size.height + 1) / size.height,
1); 1);
[ce dispatchThreadgroups:count threadsPerThreadgroup:size]; [ce dispatchThreadgroups:count threadsPerThreadgroup:size];
[ce endEncoding]; [ce endEncoding];
} }
@ -1017,11 +997,10 @@ static const NSUInteger kConstantAlignment = 4;
[self.delegate configure:ce]; [self.delegate configure:ce];
MTLSize size = MTLSizeMake(32, 1, 1); MTLSize size = MTLSizeMake(32, 1, 1);
MTLSize count = MTLSizeMake((tin.length + 00) / 32, 1, 1); MTLSize count = MTLSizeMake((tin.length + 00) / 32, 1, 1);
[ce dispatchThreadgroups:count threadsPerThreadgroup:size]; [ce dispatchThreadgroups:count threadsPerThreadgroup:size];
[ce endEncoding]; [ce endEncoding];
} }
@ -1108,30 +1087,19 @@ static const NSUInteger kConstantAlignment = 4;
- (MTLPrimitiveType)_toPrimitiveType:(enum gfx_display_prim_type)prim - (MTLPrimitiveType)_toPrimitiveType:(enum gfx_display_prim_type)prim
{ {
switch (prim) if (prim == GFX_DISPLAY_PRIM_TRIANGLESTRIP)
{ return MTLPrimitiveTypeTriangleStrip;
case GFX_DISPLAY_PRIM_TRIANGLESTRIP:
return MTLPrimitiveTypeTriangleStrip;
case GFX_DISPLAY_PRIM_TRIANGLES:
default:
/* Unexpected primitive type, defaulting to triangle */
break;
}
return MTLPrimitiveTypeTriangle; return MTLPrimitiveTypeTriangle;
} }
- (void)drawPipeline:(gfx_display_ctx_draw_t *)draw - (void)drawPipeline:(gfx_display_ctx_draw_t *)draw
{ {
static struct video_coords blank_coords; static struct video_coords blank_coords;
draw->x = 0;
draw->x = 0; draw->y = 0;
draw->y = 0; draw->matrix_data = NULL;
draw->matrix_data = NULL; _uniforms.outputSize = simd_make_float2(_context.viewport->full_width, _context.viewport->full_height);
draw->backend_data = &_uniforms;
_uniforms.outputSize = simd_make_float2(_context.viewport->full_width, _context.viewport->full_height);
draw->backend_data = &_uniforms;
draw->backend_data_size = sizeof(_uniforms); draw->backend_data_size = sizeof(_uniforms);
switch (draw->pipeline_id) switch (draw->pipeline_id)
@ -1264,22 +1232,19 @@ static const NSUInteger kConstantAlignment = 4;
{ {
self = [super init]; self = [super init];
if (self) if (self)
{
_format = RPixelFormatBGRA8Unorm; _format = RPixelFormatBGRA8Unorm;
}
return self; return self;
} }
- (NSString *)debugDescription - (NSString *)debugDescription
{ {
#if defined(HAVE_COCOATOUCH) #if defined(HAVE_COCOATOUCH)
NSString *sizeDesc = [NSString stringWithFormat:@"width: %f, height: %f",_size.width,_size.height]; NSString *sizeDesc = [NSString stringWithFormat:@"width: %f, height: %f",_size.width,_size.height];
#else #else
NSString *sizeDesc = NSStringFromSize(_size); NSString *sizeDesc = NSStringFromSize(_size);
#endif #endif
return [NSString stringWithFormat:@"( format = %@, frame = %@ )", return [NSString stringWithFormat:@"( format = %@, frame = %@ )",
NSStringFromRPixelFormat(_format), NSStringFromRPixelFormat(_format), sizeDesc];
sizeDesc];
} }
@end @end
@ -1292,7 +1257,6 @@ static const NSUInteger kConstantAlignment = 4;
CGSize _size; /* Size of view in pixels */ CGSize _size; /* Size of view in pixels */
CGRect _frame; CGRect _frame;
NSUInteger _bpp; NSUInteger _bpp;
id<MTLTexture> _src; /* Source texture */ id<MTLTexture> _src; /* Source texture */
bool _srcDirty; bool _srcDirty;
} }
@ -1302,21 +1266,17 @@ static const NSUInteger kConstantAlignment = 4;
self = [super init]; self = [super init];
if (self) if (self)
{ {
_format = d.format; _format = d.format;
_bpp = RPixelFormatToBPP(_format); _bpp = RPixelFormatToBPP(_format);
_filter = d.filter; _filter = d.filter;
_context = c; _context = c;
_visible = YES; _visible = YES;
if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm)
{
_drawState = ViewDrawStateEncoder; _drawState = ViewDrawStateEncoder;
}
else else
{
_drawState = ViewDrawStateAll; _drawState = ViewDrawStateAll;
} self.size = d.size;
self.size = d.size; self.frame = CGRectMake(0, 0, 1, 1);
self.frame = CGRectMake(0, 0, 1, 1);
} }
return self; return self;
} }
@ -1324,27 +1284,26 @@ static const NSUInteger kConstantAlignment = 4;
- (void)setSize:(CGSize)size - (void)setSize:(CGSize)size
{ {
if (CGSizeEqualToSize(_size, size)) if (CGSizeEqualToSize(_size, size))
{
return; return;
}
_size = size; _size = size;
{ {
MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm
width:(NSUInteger)size.width width: (NSUInteger)size.width
height:(NSUInteger)size.height height:(NSUInteger)size.height
mipmapped:NO]; mipmapped:NO];
td.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite; td.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
_texture = [_context.device newTextureWithDescriptor:td]; _texture = [_context.device newTextureWithDescriptor:td];
} }
if (_format != RPixelFormatBGRA8Unorm && _format != RPixelFormatBGRX8Unorm) if ( _format != RPixelFormatBGRA8Unorm
&& _format != RPixelFormatBGRX8Unorm)
{ {
MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR16Uint MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR16Uint
width:(NSUInteger)size.width width:(NSUInteger)size.width
height:(NSUInteger)size.height height:(NSUInteger)size.height
mipmapped:NO]; mipmapped:NO];
_src = [_context.device newTextureWithDescriptor:td]; _src = [_context.device newTextureWithDescriptor:td];
} }
} }
@ -1357,16 +1316,14 @@ static const NSUInteger kConstantAlignment = 4;
- (void)setFrame:(CGRect)frame - (void)setFrame:(CGRect)frame
{ {
if (CGRectEqualToRect(_frame, frame)) if (CGRectEqualToRect(_frame, frame))
{
return; return;
}
_frame = frame; _frame = frame;
float l = (float)CGRectGetMinX(frame); float l = (float)CGRectGetMinX(frame);
float t = (float)CGRectGetMinY(frame); float t = (float)CGRectGetMinY(frame);
float r = (float)CGRectGetMaxX(frame); float r = (float)CGRectGetMaxX(frame);
float b = (float)CGRectGetMaxY(frame); float b = (float)CGRectGetMaxY(frame);
Vertex v[4] = { Vertex v[4] = {
{simd_make_float3(l, b, 0), simd_make_float2(0, 1)}, {simd_make_float3(l, b, 0), simd_make_float2(0, 1)},
@ -1384,7 +1341,8 @@ static const NSUInteger kConstantAlignment = 4;
- (void)_convertFormat - (void)_convertFormat
{ {
if (_format == RPixelFormatBGRA8Unorm || _format == RPixelFormatBGRX8Unorm) if ( _format == RPixelFormatBGRA8Unorm
|| _format == RPixelFormatBGRX8Unorm)
return; return;
if (!_srcDirty) if (!_srcDirty)

View File

@ -375,7 +375,6 @@
05A8C75E20DB72F100FF7857 /* d3d_common.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = d3d_common.c; sourceTree = "<group>"; }; 05A8C75E20DB72F100FF7857 /* d3d_common.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = d3d_common.c; sourceTree = "<group>"; };
05A8C76320DB72F100FF7857 /* d3d10_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = d3d10_common.h; sourceTree = "<group>"; }; 05A8C76320DB72F100FF7857 /* d3d10_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = d3d10_common.h; sourceTree = "<group>"; };
05A8C76820DB72F100FF7857 /* vulkan_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = vulkan_common.h; sourceTree = "<group>"; }; 05A8C76820DB72F100FF7857 /* vulkan_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = vulkan_common.h; sourceTree = "<group>"; };
05A8C76920DB72F100FF7857 /* metal_common.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = metal_common.m; sourceTree = "<group>"; };
05A8C76F20DB72F100FF7857 /* gl_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gl_common.h; sourceTree = "<group>"; }; 05A8C76F20DB72F100FF7857 /* gl_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = gl_common.h; sourceTree = "<group>"; };
05A8C77020DB72F100FF7857 /* d3d_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = d3d_common.h; sourceTree = "<group>"; }; 05A8C77020DB72F100FF7857 /* d3d_common.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = d3d_common.h; sourceTree = "<group>"; };
05A8C77720DB72F100FF7857 /* d3d10_common.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = d3d10_common.c; sourceTree = "<group>"; }; 05A8C77720DB72F100FF7857 /* d3d10_common.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = d3d10_common.c; sourceTree = "<group>"; };
@ -402,11 +401,6 @@
05A8C79E20DB72F100FF7857 /* video_shader_parse.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = video_shader_parse.c; sourceTree = "<group>"; }; 05A8C79E20DB72F100FF7857 /* video_shader_parse.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = video_shader_parse.c; sourceTree = "<group>"; };
05A8C79F20DB72F100FF7857 /* video_driver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = video_driver.h; sourceTree = "<group>"; }; 05A8C79F20DB72F100FF7857 /* video_driver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = video_driver.h; sourceTree = "<group>"; };
05A8C7A020DB72F100FF7857 /* video_thread_wrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = video_thread_wrapper.h; sourceTree = "<group>"; }; 05A8C7A020DB72F100FF7857 /* video_thread_wrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = video_thread_wrapper.h; sourceTree = "<group>"; };
05A8C7A220DB72F100FF7857 /* d3d9_renderchain.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = d3d9_renderchain.h; sourceTree = "<group>"; };
05A8C7A320DB72F100FF7857 /* gl2_renderchain.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = gl2_renderchain.c; sourceTree = "<group>"; };
05A8C7A420DB72F100FF7857 /* d3d9_cg_renderchain.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = d3d9_cg_renderchain.c; sourceTree = "<group>"; };
05A8C7A520DB72F100FF7857 /* d3d9_hlsl_renderchain.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = d3d9_hlsl_renderchain.c; sourceTree = "<group>"; };
05A8C7A620DB72F100FF7857 /* gl1_renderchain.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = gl1_renderchain.c; sourceTree = "<group>"; };
05A8C7A720DB72F100FF7857 /* font_driver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = font_driver.h; sourceTree = "<group>"; }; 05A8C7A720DB72F100FF7857 /* font_driver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = font_driver.h; sourceTree = "<group>"; };
05A8C7A820DB72F100FF7857 /* video_defines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = video_defines.h; sourceTree = "<group>"; }; 05A8C7A820DB72F100FF7857 /* video_defines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = video_defines.h; sourceTree = "<group>"; };
05A8C7A920DB72F100FF7857 /* video_coord_array.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = video_coord_array.c; sourceTree = "<group>"; }; 05A8C7A920DB72F100FF7857 /* video_coord_array.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = video_coord_array.c; sourceTree = "<group>"; };
@ -950,7 +944,6 @@
05A8C5D620DB72F000FF7857 /* drivers_context */, 05A8C5D620DB72F000FF7857 /* drivers_context */,
05A8C77C20DB72F100FF7857 /* drivers_font */, 05A8C77C20DB72F100FF7857 /* drivers_font */,
05A8C7AA20DB72F100FF7857 /* drivers_font_renderer */, 05A8C7AA20DB72F100FF7857 /* drivers_font_renderer */,
05A8C7A120DB72F100FF7857 /* drivers_renderchain */,
05A8C78E20DB72F100FF7857 /* drivers_shader */, 05A8C78E20DB72F100FF7857 /* drivers_shader */,
05A8C73920DB72F100FF7857 /* font_driver.c */, 05A8C73920DB72F100FF7857 /* font_driver.c */,
05A8C7A720DB72F100FF7857 /* font_driver.h */, 05A8C7A720DB72F100FF7857 /* font_driver.h */,
@ -1100,7 +1093,6 @@
05A8C75D20DB72F100FF7857 /* gl_common.c */, 05A8C75D20DB72F100FF7857 /* gl_common.c */,
05A8C76F20DB72F100FF7857 /* gl_common.h */, 05A8C76F20DB72F100FF7857 /* gl_common.h */,
05A8C74420DB72F100FF7857 /* metal_common.h */, 05A8C74420DB72F100FF7857 /* metal_common.h */,
05A8C76920DB72F100FF7857 /* metal_common.m */,
05A8C73C20DB72F100FF7857 /* vulkan_common.c */, 05A8C73C20DB72F100FF7857 /* vulkan_common.c */,
05A8C76820DB72F100FF7857 /* vulkan_common.h */, 05A8C76820DB72F100FF7857 /* vulkan_common.h */,
); );
@ -1149,18 +1141,6 @@
path = drivers_shader; path = drivers_shader;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
05A8C7A120DB72F100FF7857 /* drivers_renderchain */ = {
isa = PBXGroup;
children = (
05A8C7A220DB72F100FF7857 /* d3d9_renderchain.h */,
05A8C7A320DB72F100FF7857 /* gl2_renderchain.c */,
05A8C7A420DB72F100FF7857 /* d3d9_cg_renderchain.c */,
05A8C7A520DB72F100FF7857 /* d3d9_hlsl_renderchain.c */,
05A8C7A620DB72F100FF7857 /* gl1_renderchain.c */,
);
path = drivers_renderchain;
sourceTree = "<group>";
};
05A8C7AA20DB72F100FF7857 /* drivers_font_renderer */ = { 05A8C7AA20DB72F100FF7857 /* drivers_font_renderer */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (