RetroArch/gfx/common/metal/RendererCommon.m

86 lines
2.1 KiB
Mathematica
Raw Normal View History

//
// RendererCommon.m
// MetalRenderer
//
// Created by Stuart Carnie on 6/3/18.
// Copyright © 2018 Stuart Carnie. All rights reserved.
//
#import "RendererCommon.h"
#import <Metal/Metal.h>
NSUInteger RPixelFormatToBPP(RPixelFormat format)
{
2018-07-03 22:31:51 -07:00
switch (format)
{
case RPixelFormatBGRA8Unorm:
case RPixelFormatBGRX8Unorm:
return 4;
2018-07-03 22:31:51 -07:00
case RPixelFormatB5G6R5Unorm:
case RPixelFormatBGRA4Unorm:
return 2;
2018-07-03 22:31:51 -07:00
default:
RARCH_ERR("[Metal]: unknown RPixel format: %d\n", format);
return 4;
}
}
static NSString *RPixelStrings[RPixelFormatCount];
NSString *NSStringFromRPixelFormat(RPixelFormat format)
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
#define STRING(literal) RPixelStrings[literal] = @#literal
STRING(RPixelFormatInvalid);
STRING(RPixelFormatB5G6R5Unorm);
STRING(RPixelFormatBGRA4Unorm);
STRING(RPixelFormatBGRA8Unorm);
STRING(RPixelFormatBGRX8Unorm);
#undef STRING
2018-07-03 22:31:51 -07:00
});
2018-07-03 22:31:51 -07:00
if (format >= RPixelFormatCount)
{
format = RPixelFormatInvalid;
}
2018-07-03 22:31:51 -07:00
return RPixelStrings[format];
}
matrix_float4x4 make_matrix_float4x4(const float *v)
{
simd_float4 P = simd_make_float4(*v++, *v++, *v++, *v++);
simd_float4 Q = simd_make_float4(*v++, *v++, *v++, *v++);
simd_float4 R = simd_make_float4(*v++, *v++, *v++, *v++);
simd_float4 S = simd_make_float4(*v++, *v++, *v++, *v++);
matrix_float4x4 mat = {P, Q, R, S};
return mat;
}
matrix_float4x4 matrix_proj_ortho(float left, float right, float top, float bottom)
{
float near = 0;
float far = 1;
2018-07-03 22:31:51 -07:00
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);
2018-07-03 22:31:51 -07:00
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);
2018-07-03 22:31:51 -07:00
matrix_float4x4 mat = {P, Q, R, S};
return mat;
}