1 // Copyright (c) 2015- PPSSPP Project.
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
11
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18 #pragma once
19
20 #include "ppsspp_config.h"
21 #include "Common/CommonTypes.h"
22 #include "Common/Data/Convert/ColorConvNEON.h"
23
24 void SetupColorConv();
25
Convert4To8(u8 v)26 inline u8 Convert4To8(u8 v) {
27 // Swizzle bits: 00001234 -> 12341234
28 return (v << 4) | (v);
29 }
30
Convert5To8(u8 v)31 inline u8 Convert5To8(u8 v) {
32 // Swizzle bits: 00012345 -> 12345123
33 return (v << 3) | (v >> 2);
34 }
35
Convert6To8(u8 v)36 inline u8 Convert6To8(u8 v) {
37 // Swizzle bits: 00123456 -> 12345612
38 return (v << 2) | (v >> 4);
39 }
40
RGBA4444ToRGBA8888(u16 src)41 inline u32 RGBA4444ToRGBA8888(u16 src) {
42 const u32 r = (src & 0x000F) << 0;
43 const u32 g = (src & 0x00F0) << 4;
44 const u32 b = (src & 0x0F00) << 8;
45 const u32 a = (src & 0xF000) << 12;
46
47 const u32 c = r | g | b | a;
48 return c | (c << 4);
49 }
50
RGBA5551ToRGBA8888(u16 src)51 inline u32 RGBA5551ToRGBA8888(u16 src) {
52 u8 r = Convert5To8((src >> 0) & 0x1F);
53 u8 g = Convert5To8((src >> 5) & 0x1F);
54 u8 b = Convert5To8((src >> 10) & 0x1F);
55 u8 a = (src >> 15) & 0x1;
56 a = (a) ? 0xff : 0;
57 return (a << 24) | (b << 16) | (g << 8) | r;
58 }
59
RGB565ToRGBA8888(u16 src)60 inline u32 RGB565ToRGBA8888(u16 src) {
61 u8 r = Convert5To8((src >> 0) & 0x1F);
62 u8 g = Convert6To8((src >> 5) & 0x3F);
63 u8 b = Convert5To8((src >> 11) & 0x1F);
64 u8 a = 0xFF;
65 return (a << 24) | (b << 16) | (g << 8) | r;
66 }
67
RGBA8888ToRGB565(u32 value)68 inline u16 RGBA8888ToRGB565(u32 value) {
69 u32 r = (value >> 3) & 0x1F;
70 u32 g = (value >> 5) & (0x3F << 5);
71 u32 b = (value >> 8) & (0x1F << 11);
72 return (u16)(r | g | b);
73 }
74
RGBA8888ToRGBA5551(u32 value)75 inline u16 RGBA8888ToRGBA5551(u32 value) {
76 u32 r = (value >> 3) & 0x1F;
77 u32 g = (value >> 6) & (0x1F << 5);
78 u32 b = (value >> 9) & (0x1F << 10);
79 u32 a = (value >> 16) & 0x8000;
80 return (u16)(r | g | b | a);
81 }
82
RGBA8888ToRGBA4444(u32 value)83 inline u16 RGBA8888ToRGBA4444(u32 value) {
84 const u32 c = value >> 4;
85 const u16 r = (c >> 0) & 0x000F;
86 const u16 g = (c >> 4) & 0x00F0;
87 const u16 b = (c >> 8) & 0x0F00;
88 const u16 a = (c >> 12) & 0xF000;
89 return r | g | b | a;
90 }
91
92 // convert image to 8888, parallelizable
93 // TODO: Implement these in terms of the conversion functions below.
94 void convert4444_gl(u16* data, u32* out, int width, int l, int u);
95 void convert565_gl(u16* data, u32* out, int width, int l, int u);
96 void convert5551_gl(u16* data, u32* out, int width, int l, int u);
97 void convert4444_dx9(u16* data, u32* out, int width, int l, int u);
98 void convert565_dx9(u16* data, u32* out, int width, int l, int u);
99 void convert5551_dx9(u16* data, u32* out, int width, int l, int u);
100
101 // "Complete" set of color conversion functions between the usual formats.
102
103 // TODO: Need to revisit the naming convention of these. Seems totally backwards
104 // now that we've standardized on Draw::DataFormat.
105
106 typedef void (*Convert16bppTo16bppFunc)(u16 *dst, const u16 *src, u32 numPixels);
107 typedef void (*Convert16bppTo32bppFunc)(u32 *dst, const u16 *src, u32 numPixels);
108 typedef void (*Convert32bppTo16bppFunc)(u16 *dst, const u32 *src, u32 numPixels);
109 typedef void (*Convert32bppTo32bppFunc)(u32 *dst, const u32 *src, u32 numPixels);
110
111 void ConvertBGRA8888ToRGBA8888(u32 *dst, const u32 *src, u32 numPixels);
112 #define ConvertRGBA8888ToBGRA8888 ConvertBGRA8888ToRGBA8888
113 void ConvertBGRA8888ToRGB888(u8 *dst, const u32 *src, u32 numPixels);
114
115 void ConvertRGBA8888ToRGBA5551(u16 *dst, const u32 *src, u32 numPixels);
116 void ConvertRGBA8888ToRGB565(u16 *dst, const u32 *src, u32 numPixels);
117 void ConvertRGBA8888ToRGBA4444(u16 *dst, const u32 *src, u32 numPixels);
118 void ConvertRGBA8888ToRGB888(u8 *dst, const u32 *src, u32 numPixels);
119
120 void ConvertBGRA8888ToRGBA5551(u16 *dst, const u32 *src, u32 numPixels);
121 void ConvertBGRA8888ToRGB565(u16 *dst, const u32 *src, u32 numPixels);
122 void ConvertBGRA8888ToRGBA4444(u16 *dst, const u32 *src, u32 numPixels);
123
124 void ConvertRGB565ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
125 void ConvertRGBA5551ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
126 void ConvertRGBA4444ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
127
128 void ConvertBGR565ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
129 void ConvertABGR1555ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
130 void ConvertABGR4444ToRGBA8888(u32 *dst, const u16 *src, u32 numPixels);
131
132 void ConvertRGBA4444ToBGRA8888(u32 *dst, const u16 *src, u32 numPixels);
133 void ConvertRGBA5551ToBGRA8888(u32 *dst, const u16 *src, u32 numPixels);
134 void ConvertRGB565ToBGRA8888(u32 *dst, const u16 *src, u32 numPixels);
135
136 void ConvertRGBA4444ToABGR4444Basic(u16 *dst, const u16 *src, u32 numPixels);
137 void ConvertRGBA5551ToABGR1555Basic(u16 *dst, const u16 *src, u32 numPixels);
138 void ConvertRGB565ToBGR565Basic(u16 *dst, const u16 *src, u32 numPixels);
139 void ConvertBGRA5551ToABGR1555(u16 *dst, const u16 *src, u32 numPixels);
140
141 #if PPSSPP_ARCH(ARM64)
142 #define ConvertRGBA4444ToABGR4444 ConvertRGBA4444ToABGR4444NEON
143 #elif !PPSSPP_ARCH(ARM)
144 #define ConvertRGBA4444ToABGR4444 ConvertRGBA4444ToABGR4444Basic
145 #else
146 extern Convert16bppTo16bppFunc ConvertRGBA4444ToABGR4444;
147 #endif
148
149 #if PPSSPP_ARCH(ARM64)
150 #define ConvertRGBA5551ToABGR1555 ConvertRGBA5551ToABGR1555NEON
151 #elif !PPSSPP_ARCH(ARM)
152 #define ConvertRGBA5551ToABGR1555 ConvertRGBA5551ToABGR1555Basic
153 #else
154 extern Convert16bppTo16bppFunc ConvertRGBA5551ToABGR1555;
155 #endif
156
157 #if PPSSPP_ARCH(ARM64)
158 #define ConvertRGB565ToBGR565 ConvertRGB565ToBGR565NEON
159 #elif !PPSSPP_ARCH(ARM)
160 #define ConvertRGB565ToBGR565 ConvertRGB565ToBGR565Basic
161 #else
162 extern Convert16bppTo16bppFunc ConvertRGB565ToBGR565;
163 #endif
164