7edb8fb050a05197d5d8e3041f9bbc4deaca7e7c
[pcsx_rearmed.git] / plugins / gpu_unai / gpu_inner_light_arm.h
1 #ifndef _OP_LIGHT_ARM_H_
2 #define _OP_LIGHT_ARM_H_
3
4 #include "arm_features.h"
5
6 ////////////////////////////////////////////////////////////////////////////////
7 // Extract bgr555 color from Gouraud u32 fixed-pt 8.3:8.3:8.2 rgb triplet
8 //
9 // INPUT:
10 //  'gCol' input:  rrrrrrrrXXXggggggggXXXbbbbbbbbXX
11 //                 ^ bit 31
12 // RETURNS:
13 //    u16 output:  0bbbbbgggggrrrrr
14 //                 ^ bit 16
15 // Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '0' zero
16 ////////////////////////////////////////////////////////////////////////////////
17 GPU_INLINE uint_fast16_t gpuLightingRGBARM(u32 gCol)
18 {
19         uint_fast16_t out = 0x03E0; // don't need the mask after starting to write output
20         u32 tmp;
21   
22         asm ("and %[tmp], %[gCol], %[out]\n\t"              // tmp holds 0x000000bbbbb00000
23              "and %[out], %[out],  %[gCol], lsr #0x0B\n\t"  // out holds 0x000000ggggg00000
24              "orr %[tmp], %[out],  %[tmp],  lsl #0x05\n\t"  // tmp holds 0x0bbbbbggggg00000
25              "orr %[out], %[tmp],  %[gCol], lsr #0x1B\n\t"  // out holds 0x0bbbbbgggggrrrrr
26              : [out] "+&r" (out), [tmp] "=&r" (tmp)
27              : [gCol] "r"  (gCol)
28              );
29
30         return out;
31 }
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // Apply fast (low-precision) 5-bit lighting to bgr555 texture color:
35 //
36 // INPUT:
37 //        'r5','g5','b5' are unsigned 5-bit color values, value of 15
38 //          is midpoint that doesn't modify that component of texture
39 //        'uSrc' input:  mbbbbbgggggrrrrr
40 //                       ^ bit 16
41 // RETURNS:
42 //          u16 output:  mbbbbbgggggrrrrr
43 // Where 'X' are fixed-pt bits.
44 ////////////////////////////////////////////////////////////////////////////////
45 #ifdef HAVE_ARMV6
46 // clang uses smulbb but not gcc, so we need this
47 GPU_INLINE int_fast16_t smulbb(int_fast16_t a, int_fast16_t b)
48 {
49         int_fast16_t r;
50         asm("smulbb %0, %1, %2" : "=r"(r) : "r"(a), "r"(b));
51         return r;
52 }
53
54 GPU_INLINE uint_fast16_t gpuLightingTXTARM(uint_fast16_t uSrc, u8 r5, u8 g5, u8 b5)
55 {
56         // on v6 we have single-cycle mul and sat which is better than the lut
57         int_fast16_t r = smulbb(uSrc & 0x001f, r5);
58         int_fast16_t g = smulbb(uSrc & 0x03e0, g5);
59         int_fast16_t b = smulbb(uSrc & 0x7c00, b5);
60         asm volatile("usat %0, #5, %0, asr #4"  : "=r"(r) : "0"(r));
61         asm volatile("usat %0, #5, %0, asr #9"  : "=r"(g) : "0"(g));
62         asm volatile("usat %0, #5, %0, asr #14" : "=r"(b) : "0"(b));
63         return (uSrc & 0x8000) | (b << 10) | (g << 5) | r;
64 }
65 #else
66 GPU_INLINE uint_fast16_t gpuLightingTXTARM(uint_fast16_t uSrc, u8 r5, u8 g5, u8 b5)
67 {
68         uint_fast16_t out = 0x03E0;
69         u32 db, dg;
70
71         // Using `g` for src, `G` for dest
72         asm ("and    %[dg],  %[out],    %[src]  \n\t"             // dg holds 0x000000ggggg00000
73              "orr    %[dg],  %[dg],     %[g5]   \n\t"             // dg holds 0x000000gggggGGGGG
74              "and    %[db],  %[out],    %[src], lsr #0x05 \n\t"   // db holds 0x000000bbbbb00000
75              "ldrb   %[dg],  [%[lut],   %[dg]]  \n\t"             // dg holds result 0x00000000000ggggg
76              "and    %[out], %[out],    %[src], lsl #0x05 \n\t"   // out holds 0x000000rrrrr00000
77              "orr    %[out], %[out],    %[r5]   \n\t"             // out holds 0x000000rrrrrRRRRR
78              "orr    %[db],  %[db],     %[b5]   \n\t"             // db holds 0x000000bbbbbBBBBB
79              "ldrb   %[out], [%[lut],   %[out]] \n\t"             // out holds result 0x00000000000rrrrr
80              "ldrb   %[db],  [%[lut],   %[db]]  \n\t"             // db holds result 0x00000000000bbbbb
81              "tst    %[src], #0x8000\n\t"                         // check whether msb was set on uSrc
82              "orr    %[out], %[out],    %[dg],  lsl #0x05   \n\t" // out holds 0x000000gggggrrrrr
83              "orrne  %[out], %[out],    #0x8000\n\t"              // add msb to out if set on uSrc
84              "orr    %[out], %[out],    %[db],  lsl #0x0A   \n\t" // out holds 0xmbbbbbgggggrrrrr
85              : [out] "=&r" (out), [db] "=&r" (db), [dg] "=&r" (dg)
86              : [r5] "r" (r5), [g5] "r" (g5),  [b5] "r" (b5),
87                [lut] "r" (gpu_unai.LightLUT), [src] "r" (uSrc), "0" (out)
88              : "cc");
89         return out;
90 }
91 #endif
92
93 ////////////////////////////////////////////////////////////////////////////////
94 // Apply fast (low-precision) 5-bit Gouraud lighting to bgr555 texture color:
95 //
96 // INPUT:
97 //  'gCol' is a packed Gouraud u32 fixed-pt 8.3:8.3:8.2 rgb triplet, value of
98 //     15.0 is midpoint that does not modify color of texture
99 //         gCol input :  rrrrrXXXXXXgggggXXXXXXbbbbbXXXXX
100 //                       ^ bit 31
101 //        'uSrc' input:  mbbbbbgggggrrrrr
102 //                       ^ bit 16
103 // RETURNS:
104 //          u16 output:  mbbbbbgggggrrrrr
105 // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care
106 ////////////////////////////////////////////////////////////////////////////////
107 GPU_INLINE uint_fast16_t gpuLightingTXTGouraudARM(uint_fast16_t uSrc, u32 gCol)
108 {
109         uint_fast16_t out = 0x03E0; // don't need the mask after starting to write output
110         u32 db,dg,gtmp;
111
112         // Using `g` for src, `G` for dest
113         asm ("and    %[dg],  %[out],  %[src]   \n\t"           // dg holds 0x000000ggggg00000
114              "and    %[gtmp],%[out],  %[gCol], lsr #0x0B \n\t" // gtmp holds 0x000000GGGGG00000
115              "and    %[db],  %[out],  %[src],  lsr #0x05 \n\t" // db holds 0x000000bbbbb00000
116              "orr    %[dg],  %[dg],   %[gtmp], lsr #0x05 \n\t" // dg holds 0x000000gggggGGGGG
117              "and    %[gtmp],%[out],  %[gCol]  \n\t"           // gtmp holds 0x000000BBBBB00000
118              "ldrb   %[dg],  [%[lut], %[dg]]   \n\t"           // dg holds result 0x00000000000ggggg
119              "and    %[out], %[out],  %[src],  lsl #0x05 \n\t" // out holds 0x000000rrrrr00000
120              "orr    %[out], %[out],  %[gCol], lsr #0x1B \n\t" // out holds 0x000000rrrrrRRRRR
121              "orr    %[db],  %[db],   %[gtmp], lsr #0x05 \n\t" // db holds 0x000000bbbbbBBBBB
122              "ldrb   %[out], [%[lut], %[out]]  \n\t"           // out holds result 0x00000000000rrrrr
123              "ldrb   %[db],  [%[lut], %[db]]   \n\t"           // db holds result 0x00000000000bbbbb
124              "tst    %[src], #0x8000\n\t"                      // check whether msb was set on uSrc
125              "orr    %[out], %[out],  %[dg],   lsl #0x05 \n\t" // out holds 0x000000gggggrrrrr
126              "orrne  %[out], %[out],  #0x8000\n\t"             // add msb to out if set on uSrc
127              "orr    %[out], %[out],  %[db],   lsl #0x0A \n\t" // out holds 0xmbbbbbgggggrrrrr
128              : [out] "=&r" (out), [db] "=&r" (db), [dg] "=&r" (dg),
129                [gtmp] "=&r" (gtmp) \
130              : [gCol] "r" (gCol), [lut] "r" (gpu_unai.LightLUT), "0" (out), [src] "r" (uSrc)
131              : "cc");
132
133         return out;
134 }
135
136 #endif  //_OP_LIGHT_ARM_H_