| Commit | Line | Data |
|---|---|---|
| 0bfe8d59 | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2016 PCSX4ALL Team * | |
| 3 | * Copyright (C) 2010 Unai * | |
| 4 | * * | |
| 5 | * This program is free software; you can redistribute it and/or modify * | |
| 6 | * it under the terms of the GNU General Public License as published by * | |
| 7 | * the Free Software Foundation; either version 2 of the License, or * | |
| 8 | * (at your option) any later version. * | |
| 9 | * * | |
| 10 | * This program is distributed in the hope that it will be useful, * | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
| 13 | * GNU General Public License for more details. * | |
| 14 | * * | |
| 15 | * You should have received a copy of the GNU General Public License * | |
| 16 | * along with this program; if not, write to the * | |
| 17 | * Free Software Foundation, Inc., * | |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. * | |
| 19 | ***************************************************************************/ | |
| 20 | ||
| 21 | #ifndef _OP_LIGHT_H_ | |
| 22 | #define _OP_LIGHT_H_ | |
| 23 | ||
| 24 | // GPU color operations for lighting calculations | |
| 25 | ||
| 26 | static void SetupLightLUT() | |
| 27 | { | |
| 28 | // 1024-entry lookup table that modulates 5-bit texture + 5-bit light value. | |
| 29 | // A light value of 15 does not modify the incoming texture color. | |
| 30 | // LightLUT[32*32] array is initialized to following values: | |
| 31 | // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
| 32 | // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
| 33 | // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, | |
| 34 | // 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, | |
| 35 | // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, | |
| 36 | // 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, | |
| 37 | // 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9,10,10,10,11,11, | |
| 38 | // 0, 0, 0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9,10,10,10,11,11,12,12,13,13, | |
| 39 | // 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10,11,11,12,12,13,13,14,14,15,15, | |
| 40 | // 0, 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 9, 9,10,10,11,11,12,12,13,14,14,15,15,16,16,17, | |
| 41 | // 0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9,10,10,11,11,12,13,13,14,15,15,16,16,17,18,18,19, | |
| 42 | // 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9,10,11,11,12,13,13,14,15,15,16,17,17,18,19,19,20,21, | |
| 43 | // 0, 0, 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9,10,11,12,12,13,14,15,15,16,17,18,18,19,20,21,21,22,23, | |
| 44 | // 0, 0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 8, 9,10,11,12,13,13,14,15,16,17,17,18,19,20,21,21,22,23,24,25, | |
| 45 | // 0, 0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9,10,11,12,13,14,14,15,16,17,18,19,20,21,21,22,23,24,25,26,27, | |
| 46 | // 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29, | |
| 47 | // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, | |
| 48 | // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,31, | |
| 49 | // 0, 1, 2, 3, 4, 5, 6, 7, 9,10,11,12,13,14,15,16,18,19,20,21,22,23,24,25,27,28,29,30,31,31,31,31, | |
| 50 | // 0, 1, 2, 3, 4, 5, 7, 8, 9,10,11,13,14,15,16,17,19,20,21,22,23,24,26,27,28,29,30,31,31,31,31,31, | |
| 51 | // 0, 1, 2, 3, 5, 6, 7, 8,10,11,12,13,15,16,17,18,20,21,22,23,25,26,27,28,30,31,31,31,31,31,31,31, | |
| 52 | // 0, 1, 2, 3, 5, 6, 7, 9,10,11,13,14,15,17,18,19,21,22,23,24,26,27,28,30,31,31,31,31,31,31,31,31, | |
| 53 | // 0, 1, 2, 4, 5, 6, 8, 9,11,12,13,15,16,17,19,20,22,23,24,26,27,28,30,31,31,31,31,31,31,31,31,31, | |
| 54 | // 0, 1, 2, 4, 5, 7, 8,10,11,12,14,15,17,18,20,21,23,24,25,27,28,30,31,31,31,31,31,31,31,31,31,31, | |
| 55 | // 0, 1, 3, 4, 6, 7, 9,10,12,13,15,16,18,19,21,22,24,25,27,28,30,31,31,31,31,31,31,31,31,31,31,31, | |
| 56 | // 0, 1, 3, 4, 6, 7, 9,10,12,14,15,17,18,20,21,23,25,26,28,29,31,31,31,31,31,31,31,31,31,31,31,31, | |
| 57 | // 0, 1, 3, 4, 6, 8, 9,11,13,14,16,17,19,21,22,24,26,27,29,30,31,31,31,31,31,31,31,31,31,31,31,31, | |
| 58 | // 0, 1, 3, 5, 6, 8,10,11,13,15,16,18,20,21,23,25,27,28,30,31,31,31,31,31,31,31,31,31,31,31,31,31, | |
| 59 | // 0, 1, 3, 5, 7, 8,10,12,14,15,17,19,21,22,24,26,28,29,31,31,31,31,31,31,31,31,31,31,31,31,31,31, | |
| 60 | // 0, 1, 3, 5, 7, 9,10,12,14,16,18,19,21,23,25,27,29,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31, | |
| 61 | // 0, 1, 3, 5, 7, 9,11,13,15,16,18,20,22,24,26,28,30,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31, | |
| 62 | // 0, 1, 3, 5, 7, 9,11,13,15,17,19,21,23,25,27,29,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31 | |
| 63 | ||
| 64 | for (int j=0; j < 32; ++j) { | |
| 65 | for (int i=0; i < 32; ++i) { | |
| 66 | int val = i * j / 16; | |
| 67 | if (val > 31) val = 31; | |
| e223fa15 | 68 | gpu_unai.LightLUT[(j*32) + i] = val; |
| 0bfe8d59 | 69 | } |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | ||
| 74 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 75 | // Create packed Gouraud fixed-pt 8.8 rgb triplet |
| 0bfe8d59 | 76 | // |
| 77 | // INPUT: | |
| 78 | // 'r','g','b' are 8.10 fixed-pt color components (r shown here) | |
| 79 | // 'r' input: --------------rrrrrrrrXXXXXXXXXX | |
| 80 | // ^ bit 31 | |
| 81 | // RETURNS: | |
| b3bce60e PC |
82 | // gcol_t output: ccccccccXXXXXXXX for c in [r, g, b] |
| 83 | // ^ bit 16 | |
| 0bfe8d59 | 84 | // Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '-' don't care |
| 85 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 86 | GPU_INLINE gcol_t gpuPackGouraudCol(u32 r, u32 g, u32 b) |
| 0bfe8d59 | 87 | { |
| b3bce60e PC |
88 | return (gcol_t){ |
| 89 | (u16)(r >> 2), | |
| 90 | (u16)(g >> 2), | |
| 91 | (u16)(b >> 2), | |
| 92 | }; | |
| 0bfe8d59 | 93 | } |
| 94 | ||
| 0bfe8d59 | 95 | //////////////////////////////////////////////////////////////////////////////// |
| b3bce60e | 96 | // Create packed increment for Gouraud fixed-pt 8.8 rgb triplet |
| 0bfe8d59 | 97 | // |
| 98 | // INPUT: | |
| 99 | // Sign-extended 8.10 fixed-pt r,g,b color increment values (only dr is shown) | |
| 100 | // 'dr' input: ssssssssssssssrrrrrrrrXXXXXXXXXX | |
| 101 | // ^ bit 31 | |
| 102 | // RETURNS: | |
| b3bce60e PC |
103 | // gcol_t output: ccccccccXXXXXXXX for c in [r, g, b] |
| 104 | // ^ bit 16 | |
| 0bfe8d59 | 105 | // Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and 's' sign bits |
| 106 | // | |
| 107 | // NOTE: The correctness of this code/method has not been fully verified, | |
| 108 | // having been merely factored out from original code in | |
| 109 | // poly-drawing functions. Feel free to check/improve it -senquack | |
| 110 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 111 | GPU_INLINE gcol_t gpuPackGouraudColInc(s32 dr, s32 dg, s32 db) |
| 0bfe8d59 | 112 | { |
| b3bce60e PC |
113 | return (gcol_t){ |
| 114 | (u16)((dr >> 2) + (dr < 0)), | |
| 115 | (u16)((dg >> 2) + (dg < 0)), | |
| 116 | (u16)((db >> 2) + (db < 0)), | |
| 117 | }; | |
| 0bfe8d59 | 118 | } |
| 119 | ||
| 0bfe8d59 | 120 | //////////////////////////////////////////////////////////////////////////////// |
| b3bce60e | 121 | // Extract bgr555 color from Gouraud u32 fixed-pt 8.8 rgb triplet |
| 0bfe8d59 | 122 | // |
| 123 | // INPUT: | |
| b3bce60e PC |
124 | // 'gCol' input: ccccccccXXXXXXXX for c in [r, g, b] |
| 125 | // ^ bit 16 | |
| 0bfe8d59 | 126 | // RETURNS: |
| 127 | // u16 output: 0bbbbbgggggrrrrr | |
| 128 | // ^ bit 16 | |
| 129 | // Where 'r,g,b' are integer bits of colors, 'X' fixed-pt, and '0' zero | |
| 130 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 131 | GPU_INLINE uint_fast16_t gpuLightingRGB(gcol_t gCol) |
| 0bfe8d59 | 132 | { |
| b3bce60e PC |
133 | return (gCol.c.r >> 11) | |
| 134 | ((gCol.c.g >> 6) & 0x3e0) | | |
| 135 | ((gCol.c.b >> 1) & 0x7c00); | |
| 0bfe8d59 | 136 | } |
| 137 | ||
| 0bfe8d59 | 138 | //////////////////////////////////////////////////////////////////////////////// |
| b3bce60e PC |
139 | // Convert packed Gouraud u32 fixed-pt 8.8 rgb triplet in 'gCol' |
| 140 | // to padded u32 5.4 bgr fixed-pt triplet, suitable for use | |
| 0bfe8d59 | 141 | // with HQ 24-bit lighting/quantization. |
| 142 | // | |
| 143 | // INPUT: | |
| b3bce60e PC |
144 | // 'gCol' input: ccccccccXXXXXXXX for c in [r, g, b] |
| 145 | // ^ bit 16 | |
| 0bfe8d59 | 146 | // RETURNS: |
| 147 | // u32 output: 000bbbbbXXXX0gggggXXXX0rrrrrXXXX | |
| 148 | // ^ bit 31 | |
| 149 | // Where 'X' are fixed-pt bits, '0' zero-padding, and '-' is don't care | |
| 150 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 151 | GPU_INLINE u32 gpuLightingRGB24(gcol_t gCol) |
| 0bfe8d59 | 152 | { |
| b3bce60e PC |
153 | return (gCol.c.r >> 7) |
| 154 | | ((gCol.c.g >> 7) << 10) | |
| 155 | | ((gCol.c.b >> 7) << 20); | |
| 0bfe8d59 | 156 | } |
| 157 | ||
| 0bfe8d59 | 158 | //////////////////////////////////////////////////////////////////////////////// |
| 159 | // Apply fast (low-precision) 5-bit lighting to bgr555 texture color: | |
| 160 | // | |
| 161 | // INPUT: | |
| 162 | // 'r5','g5','b5' are unsigned 5-bit color values, value of 15 | |
| 163 | // is midpoint that doesn't modify that component of texture | |
| 164 | // 'uSrc' input: -bbbbbgggggrrrrr | |
| 165 | // ^ bit 16 | |
| 166 | // RETURNS: | |
| 167 | // u16 output: 0bbbbbgggggrrrrr | |
| 168 | // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care | |
| 169 | //////////////////////////////////////////////////////////////////////////////// | |
| 170 | GPU_INLINE uint_fast16_t gpuLightingTXTGeneric(uint_fast16_t uSrc, u8 r5, u8 g5, u8 b5) | |
| 171 | { | |
| e223fa15 | 172 | return (gpu_unai.LightLUT[((uSrc&0x7C00)>>5) | b5] << 10) | |
| 173 | (gpu_unai.LightLUT[ (uSrc&0x03E0) | g5] << 5) | | |
| 846344d4 PC |
174 | (gpu_unai.LightLUT[((uSrc&0x001F)<<5) | r5] ) | |
| 175 | (uSrc & 0x8000); | |
| 0bfe8d59 | 176 | } |
| 177 | ||
| 178 | ||
| 179 | //////////////////////////////////////////////////////////////////////////////// | |
| 180 | // Apply fast (low-precision) 5-bit Gouraud lighting to bgr555 texture color: | |
| 181 | // | |
| 182 | // INPUT: | |
| b3bce60e PC |
183 | // 'gCol' is a Gouraud fixed-pt 8.8 rgb triplet |
| 184 | // 'gCol' input: ccccccccXXXXXXXX for c in [r, g, b] | |
| 185 | // ^ bit 16 | |
| 0bfe8d59 | 186 | // 'uSrc' input: -bbbbbgggggrrrrr |
| 187 | // ^ bit 16 | |
| 188 | // RETURNS: | |
| 189 | // u16 output: 0bbbbbgggggrrrrr | |
| 190 | // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care | |
| 191 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 192 | GPU_INLINE uint_fast16_t gpuLightingTXTGouraud(uint_fast16_t uSrc, gcol_t gCol) |
| 0bfe8d59 | 193 | { |
| b3bce60e PC |
194 | return (gpu_unai.LightLUT[((uSrc&0x7C00)>>5) | (gCol.c.b >> 11)] << 10) | |
| 195 | (gpu_unai.LightLUT[ (uSrc&0x03E0) | (gCol.c.g >> 11)] << 5) | | |
| 196 | (gpu_unai.LightLUT[((uSrc&0x001F)<<5) | (gCol.c.r >> 11)]) | | |
| 846344d4 | 197 | (uSrc & 0x8000); |
| 0bfe8d59 | 198 | } |
| 199 | ||
| 0bfe8d59 | 200 | //////////////////////////////////////////////////////////////////////////////// |
| 201 | // Apply high-precision 8-bit lighting to bgr555 texture color, | |
| 202 | // returning a padded u32 5.4:5.4:5.4 bgr fixed-pt triplet | |
| 203 | // suitable for use with HQ 24-bit lighting/quantization. | |
| 204 | // | |
| 205 | // INPUT: | |
| 206 | // 'r8','g8','b8' are unsigned 8-bit color component values, value of | |
| 207 | // 127 is midpoint that doesn't modify that component of texture | |
| 208 | // | |
| 209 | // uSrc input: -bbbbbgggggrrrrr | |
| 210 | // ^ bit 16 | |
| 211 | // RETURNS: | |
| 212 | // u32 output: 000bbbbbXXXX0gggggXXXX0rrrrrXXXX | |
| 213 | // ^ bit 31 | |
| 214 | // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care | |
| 215 | //////////////////////////////////////////////////////////////////////////////// | |
| 216 | GPU_INLINE u32 gpuLightingTXT24(uint_fast16_t uSrc, u8 r8, u8 g8, u8 b8) | |
| 217 | { | |
| 218 | uint_fast16_t r1 = uSrc&0x001F; | |
| 219 | uint_fast16_t g1 = uSrc&0x03E0; | |
| 220 | uint_fast16_t b1 = uSrc&0x7C00; | |
| 221 | ||
| 222 | uint_fast16_t r2 = r8; | |
| 223 | uint_fast16_t g2 = g8; | |
| 224 | uint_fast16_t b2 = b8; | |
| 225 | ||
| 226 | u32 r3 = r1 * r2; if (r3 & 0xFFFFF000) r3 = ~0xFFFFF000; | |
| 227 | u32 g3 = g1 * g2; if (g3 & 0xFFFE0000) g3 = ~0xFFFE0000; | |
| 228 | u32 b3 = b1 * b2; if (b3 & 0xFFC00000) b3 = ~0xFFC00000; | |
| 229 | ||
| 230 | return ((r3>> 3) ) | | |
| 231 | ((g3>> 8)<<10) | | |
| 232 | ((b3>>13)<<20); | |
| 233 | } | |
| 234 | ||
| 235 | ||
| 236 | //////////////////////////////////////////////////////////////////////////////// | |
| 237 | // Apply high-precision 8-bit lighting to bgr555 texture color in 'uSrc', | |
| 238 | // returning a padded u32 5.4:5.4:5.4 bgr fixed-pt triplet | |
| 239 | // suitable for use with HQ 24-bit lighting/quantization. | |
| 240 | // | |
| 241 | // INPUT: | |
| 242 | // 'uSrc' input: -bbbbbgggggrrrrr | |
| 243 | // ^ bit 16 | |
| b3bce60e PC |
244 | // 'gCol' input: ccccccccXXXXXXXX for c in [r, g, b] |
| 245 | // ^ bit 16 | |
| 0bfe8d59 | 246 | // RETURNS: |
| 247 | // u32 output: 000bbbbbXXXX0gggggXXXX0rrrrrXXXX | |
| 248 | // ^ bit 31 | |
| 249 | // Where 'X' are fixed-pt bits, '0' is zero-padding, and '-' is don't care | |
| 250 | //////////////////////////////////////////////////////////////////////////////// | |
| b3bce60e | 251 | GPU_INLINE u32 gpuLightingTXT24Gouraud(uint_fast16_t uSrc, gcol_t gCol) |
| 0bfe8d59 | 252 | { |
| 253 | uint_fast16_t r1 = uSrc&0x001F; | |
| 254 | uint_fast16_t g1 = uSrc&0x03E0; | |
| 255 | uint_fast16_t b1 = uSrc&0x7C00; | |
| 256 | ||
| b3bce60e PC |
257 | uint_fast16_t r2 = gCol.c.r >> 8; |
| 258 | uint_fast16_t g2 = gCol.c.g >> 8; | |
| 259 | uint_fast16_t b2 = gCol.c.b >> 8; | |
| 0bfe8d59 | 260 | |
| 261 | u32 r3 = r1 * r2; if (r3 & 0xFFFFF000) r3 = ~0xFFFFF000; | |
| 262 | u32 g3 = g1 * g2; if (g3 & 0xFFFE0000) g3 = ~0xFFFE0000; | |
| 263 | u32 b3 = b1 * b2; if (b3 & 0xFFC00000) b3 = ~0xFFC00000; | |
| 264 | ||
| 265 | return ((r3>> 3) ) | | |
| 266 | ((g3>> 8)<<10) | | |
| 267 | ((b3>>13)<<20); | |
| 268 | } | |
| 269 | ||
| 270 | #endif //_OP_LIGHT_H_ |