From 788f5e89c29daab31f1099f081ca92d72e507bf1 Mon Sep 17 00:00:00 2001 From: Justin Weiss Date: Thu, 27 Feb 2020 23:42:43 -0800 Subject: WIP: Add ARM-assembly versions of lighting and blending --- plugins/gpu_unai/gpu_inner.h | 20 ++++++- plugins/gpu_unai/gpu_inner_blend.h | 2 +- plugins/gpu_unai/gpu_inner_blend_arm.h | 96 ++++++++++++++++++++++++++++++++++ plugins/gpu_unai/gpu_inner_light.h | 6 +-- plugins/gpu_unai/gpu_inner_light_arm.h | 79 ++++++++++++++++++++++++++++ 5 files changed, 198 insertions(+), 5 deletions(-) create mode 100644 plugins/gpu_unai/gpu_inner_blend_arm.h create mode 100644 plugins/gpu_unai/gpu_inner_light_arm.h (limited to 'plugins/gpu_unai') diff --git a/plugins/gpu_unai/gpu_inner.h b/plugins/gpu_unai/gpu_inner.h index 723e09f..197d7ad 100644 --- a/plugins/gpu_unai/gpu_inner.h +++ b/plugins/gpu_unai/gpu_inner.h @@ -48,12 +48,30 @@ //#include "gpu_inner_blend.h" //#endif -// TODO: use the arm-optimized gpu_inner_blends for arm builds #include "gpu_inner_blend.h" +#ifdef __arm__ +#include "gpu_inner_blend_arm.h" +#define gpuBlending gpuBlendingARM +#else +#define gpuBlending gpuBlendingGeneric +#endif + #include "gpu_inner_quantization.h" #include "gpu_inner_light.h" +#ifdef __arm__ +#include "gpu_inner_light_arm.h" +#define gpuLightingRGB gpuLightingRGBARM +#define gpuLightingTXT gpuLightingTXTARM +#define gpuLightingTXTGouraud gpuLightingTXTGouraudARM +#else +#define gpuLightingRGB gpuLightingRGBGeneric +#define gpuLightingTXT gpuLightingTXTGeneric +#define gpuLightingTXTGouraud gpuLightingTXTGouraudGeneric +#endif + + // If defined, Gouraud colors are fixed-point 5.11, otherwise they are 8.16 // This is only for debugging/verification of low-precision colors in C. // Low-precision Gouraud is intended for use by SIMD-optimized inner drivers diff --git a/plugins/gpu_unai/gpu_inner_blend.h b/plugins/gpu_unai/gpu_inner_blend.h index 93c268b..a469541 100644 --- a/plugins/gpu_unai/gpu_inner_blend.h +++ b/plugins/gpu_unai/gpu_inner_blend.h @@ -37,7 +37,7 @@ // Where '0' is zero-padding, and '-' is don't care //////////////////////////////////////////////////////////////////////////////// template -GPU_INLINE u16 gpuBlending(u16 uSrc, u16 uDst) +GPU_INLINE u16 gpuBlendingGeneric(u16 uSrc, u16 uDst) { // These use Blargg's bitwise modulo-clamping: // http://blargg.8bitalley.com/info/rgb_mixing.html diff --git a/plugins/gpu_unai/gpu_inner_blend_arm.h b/plugins/gpu_unai/gpu_inner_blend_arm.h new file mode 100644 index 0000000..4db105a --- /dev/null +++ b/plugins/gpu_unai/gpu_inner_blend_arm.h @@ -0,0 +1,96 @@ +#ifndef _OP_BLEND_ARM_H_ +#define _OP_BLEND_ARM_H_ + +//////////////////////////////////////////////////////////////////////////////// +// Blend bgr555 color in 'uSrc' (foreground) with bgr555 color +// in 'uDst' (background), returning resulting color. +// +// INPUT: +// 'uSrc','uDst' input: -bbbbbgggggrrrrr +// ^ bit 16 +// OUTPUT: +// u16 output: 0bbbbbgggggrrrrr +// ^ bit 16 +// RETURNS: +// Where '0' is zero-padding, and '-' is don't care +//////////////////////////////////////////////////////////////////////////////// +template +GPU_INLINE u16 gpuBlendingARM(u16 uSrc, u16 uDst) +{ + // These use Blargg's bitwise modulo-clamping: + // http://blargg.8bitalley.com/info/rgb_mixing.html + // http://blargg.8bitalley.com/info/rgb_clamped_add.html + // http://blargg.8bitalley.com/info/rgb_clamped_sub.html + + + u16 mix; + + asm ("bic %[uDst], %[uDst], #0x8000" : [uDst] "+r" (uDst)); + + if (BLENDMODE == 3) { + asm ("and %[uSrc], %[mask], %[uSrc], lsr #0x2" : [uSrc] "+r" (uSrc) : [mask] "r" (0x1ce7)); + } else if (!SKIP_USRC_MSB_MASK) { + asm ("bic %[uSrc], %[uSrc], #0x8000" : [uSrc] "+r" (uSrc)); + } + + + // 0.5 x Back + 0.5 x Forward + if (BLENDMODE==0) { + // mix = ((uSrc + uDst) - ((uSrc ^ uDst) & 0x0421)) >> 1; + asm ("eor %[mix], %[uSrc], %[uDst]\n\t" + "and %[mix], %[mix], %[mask]\n\t" + "sub %[mix], %[uDst], %[mix]\n\t" + "add %[mix], %[uSrc], %[mix]\n\t" + "mov %[mix], %[mix], lsr #0x1\n\t" + : [mix] "=&r" (mix) + : [uSrc] "r" (uSrc), [uDst] "r" (uDst), [mask] "r" (0x0421)); + } + + if (BLENDMODE == 1 || BLENDMODE == 3) { + // u32 sum = uSrc + uDst; + // u32 low_bits = (uSrc ^ uDst) & 0x0421; + // u32 carries = (sum - low_bits) & 0x8420; + // u32 modulo = sum - carries; + // u32 clamp = carries - (carries >> 5); + // mix = modulo | clamp; + + u32 sum; + + asm ("add %[sum], %[uSrc], %[uDst]\n\t" + "eor %[mix], %[uSrc], %[uDst]\n\t" + "and %[mix], %[mix], %[mask]\n\t" + "sub %[mix], %[sum], %[mix]\n\t" + "and %[mix], %[mix], %[mask], lsl #0x05\n\t" + "sub %[sum], %[sum], %[mix] \n\t" + "sub %[mix], %[mix], %[mix], lsr #0x05\n\t" + "orr %[mix], %[sum], %[mix]" + : [sum] "=&r" (sum), [mix] "=&r" (mix) + : [uSrc] "r" (uSrc), [uDst] "r" (uDst), [mask] "r" (0x0421)); + } + + // 1.0 x Back - 1.0 x Forward + if (BLENDMODE==2) { + u32 diff; + // u32 diff = uDst - uSrc + 0x8420; + // u32 low_bits = (uDst ^ uSrc) & 0x8420; + // u32 borrows = (diff - low_bits) & 0x8420; + // u32 modulo = diff - borrows; + // u32 clamp = borrows - (borrows >> 5); + // mix = modulo & clamp; + asm ("sub %[diff], %[uDst], %[uSrc]\n\t" + "add %[diff], %[diff], %[mask]\n\t" + "eor %[mix], %[uDst], %[uSrc]\n\t" + "and %[mix], %[mix], %[mask]\n\t" + "sub %[mix], %[diff], %[mix]\n\t" + "and %[mix], %[mix], %[mask]\n\t" + "sub %[diff], %[diff], %[mix]\n\t" + "sub %[mix], %[mix], %[mix], lsr #0x05\n\t" + "and %[mix], %[diff], %[mix]" + : [diff] "=&r" (diff), [mix] "=&r" (mix) + : [uSrc] "r" (uSrc), [uDst] "r" (uDst), [mask] "r" (0x8420)); + } + + return mix; +} + +#endif //_OP_BLEND_ARM_H_ diff --git a/plugins/gpu_unai/gpu_inner_light.h b/plugins/gpu_unai/gpu_inner_light.h index b041dc3..71d85b1 100644 --- a/plugins/gpu_unai/gpu_inner_light.h +++ b/plugins/gpu_unai/gpu_inner_light.h @@ -127,7 +127,7 @@ GPU_INLINE u32 gpuPackGouraudColInc(s32 dr, s32 dg, s32 db) // ^ bit 16 // Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '0' zero //////////////////////////////////////////////////////////////////////////////// -GPU_INLINE u16 gpuLightingRGB(u32 gCol) +GPU_INLINE u16 gpuLightingRGBGeneric(u32 gCol) { return ((gCol<< 5)&0x7C00) | ((gCol>>11)&0x03E0) | @@ -168,7 +168,7 @@ GPU_INLINE u32 gpuLightingRGB24(u32 gCol) // u16 output: 0bbbbbgggggrrrrr // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care //////////////////////////////////////////////////////////////////////////////// -GPU_INLINE u16 gpuLightingTXT(u16 uSrc, u8 r5, u8 g5, u8 b5) +GPU_INLINE u16 gpuLightingTXTGeneric(u16 uSrc, u8 r5, u8 g5, u8 b5) { return (gpu_unai.LightLUT[((uSrc&0x7C00)>>5) | b5] << 10) | (gpu_unai.LightLUT[ (uSrc&0x03E0) | g5] << 5) | @@ -190,7 +190,7 @@ GPU_INLINE u16 gpuLightingTXT(u16 uSrc, u8 r5, u8 g5, u8 b5) // u16 output: 0bbbbbgggggrrrrr // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care //////////////////////////////////////////////////////////////////////////////// -GPU_INLINE u16 gpuLightingTXTGouraud(u16 uSrc, u32 gCol) +GPU_INLINE u16 gpuLightingTXTGouraudGeneric(u16 uSrc, u32 gCol) { return (gpu_unai.LightLUT[((uSrc&0x7C00)>>5) | ((gCol>> 5)&0x1F)]<<10) | (gpu_unai.LightLUT[ (uSrc&0x03E0) | ((gCol>>16)&0x1F)]<< 5) | diff --git a/plugins/gpu_unai/gpu_inner_light_arm.h b/plugins/gpu_unai/gpu_inner_light_arm.h new file mode 100644 index 0000000..ac99cf3 --- /dev/null +++ b/plugins/gpu_unai/gpu_inner_light_arm.h @@ -0,0 +1,79 @@ +#ifndef _OP_LIGHT_ARM_H_ +#define _OP_LIGHT_ARM_H_ + +//////////////////////////////////////////////////////////////////////////////// +// Extract bgr555 color from Gouraud u32 fixed-pt 8.3:8.3:8.2 rgb triplet +// +// INPUT: +// 'gCol' input: rrrrrrrrXXXggggggggXXXbbbbbbbbXX +// ^ bit 31 +// RETURNS: +// u16 output: 0bbbbbgggggrrrrr +// ^ bit 16 +// Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '0' zero +//////////////////////////////////////////////////////////////////////////////// +GPU_INLINE u16 gpuLightingRGBARM(u32 gCol) +{ + u16 out = 0x03E0; // don't need the mask after starting to write output + u32 tmp; + + asm ("and %[tmp], %[gCol], %[out]\n\t" // tmp holds 0x000000bbbbb00000 + "and %[out], %[out], %[gCol], lsr #0x0B\n\t" // out holds 0x000000ggggg00000 + "orr %[tmp], %[out], %[tmp], lsl #0x05\n\t" // tmp holds 0x0bbbbbggggg00000 + "orr %[out], %[tmp], %[gCol], lsr #0x1B\n\t" // out holds 0x0bbbbbgggggrrrrr + : [out] "+&r" (out), [tmp] "=&r" (tmp) + : [gCol] "r" (gCol) + ); + + return out; +} + + +GPU_INLINE u16 gpuLightingTXTARM(u16 uSrc, u8 r5, u8 g5, u8 b5) +{ + u16 out = 0x03E0; + u32 db, dg; + asm ("and %[dg], %[out], %[src] \n\t" + "orr %[dg], %[dg], %[g5] \n\t" + "and %[db], %[out], %[src], lsr #0x05 \n\t" + "ldrb %[dg], [%[lut], %[dg]] \n\t" + "and %[out], %[out], %[src], lsl #0x05 \n\t" + "orr %[out], %[out], %[r5] \n\t" + "orr %[db], %[db], %[b5] \n\t" + "ldrb %[out], [%[lut], %[out]] \n\t" + "ldrb %[db], [%[lut], %[db]] \n\t" + "orr %[out], %[out], %[dg], lsl #0x05 \n\t" + "orr %[out], %[out], %[db], lsl #0x0A \n\t" + : [out] "=&r" (out), [db] "=&r" (db), [dg] "=&r" (dg) + : [r5] "r" (r5), [g5] "r" (g5), [b5] "r" (b5), + [lut] "r" (gpu_unai.LightLUT), [src] "r" (uSrc), "0" (out) + : "cc"); + return out; +} + +GPU_INLINE u16 gpuLightingTXTGouraudARM(u16 uSrc, u32 gCol) +{ + u16 out = 0x03E0; // don't need the mask after starting to write output + u32 db,dg,gtmp; + asm ("and %[dg], %[out], %[src] \n\t" + "and %[gtmp],%[out], %[gCol], lsr #0x0B \n\t" + "and %[db], %[out], %[src], lsr #0x05 \n\t" + "orr %[dg], %[dg], %[gtmp], lsr #0x05 \n\t" + "and %[gtmp],%[out], %[gCol] \n\t" + "ldrb %[dg], [%[lut], %[dg]] \n\t" + "and %[out], %[out], %[src], lsl #0x05 \n\t" + "orr %[out], %[out], %[gCol], lsr #0x1B \n\t" + "orr %[db], %[db], %[gtmp], lsr #0x05 \n\t" + "ldrb %[out], [%[lut], %[out]] \n\t" + "ldrb %[db], [%[lut], %[db]] \n\t" + "orr %[out], %[out], %[dg], lsl #0x05 \n\t" + "orr %[out], %[out], %[db], lsl #0x0A \n\t" + : [out] "=&r" (out), [db] "=&r" (db), [dg] "=&r" (dg), + [gtmp] "=&r" (gtmp) \ + : [gCol] "r" (gCol), [lut] "r" (gpu_unai.LightLUT), "0" (out), [src] "r" (uSrc) + : "cc"); + + return out; +} + +#endif //_OP_LIGHT_ARM_H_ -- cgit v1.2.3