Glide Plugin GLES2 port from mupen64plus-ae, but with special FrameSkip code
[mupen64plus-pandora.git] / source / gles2glide64 / src / Glide64 / TexModCI.h
1 /*
2 * Glide64 - Glide video plugin for Nintendo 64 emulators.
3 * Copyright (c) 2002  Dave2001
4 * Copyright (c) 2003-2009  Sergey 'Gonetz' Lipski
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 //****************************************************************
22 //
23 // Glide64 - Glide Plugin for Nintendo 64 emulators
24 // Project started on December 29th, 2001
25 //
26 // Authors:
27 // Dave2001, original author, founded the project in 2001, left it in 2002
28 // Gugaman, joined the project in 2002, left it in 2002
29 // Sergey 'Gonetz' Lipski, joined the project in 2002, main author since fall of 2002
30 // Hiroshi 'KoolSmoky' Morii, joined the project in 2007
31 //
32 //****************************************************************
33 //
34 // To modify Glide64:
35 // * Write your name and (optional)email, commented by your work, so I know who did it, and so that you can find which parts you modified when it comes time to send it to me.
36 // * Do NOT send me the whole project or file that you modified.  Take out your modified code sections, and tell me where to put them.  If people sent the whole thing, I would have many different versions, but no idea how to combine them all.
37 //
38 //****************************************************************
39
40 static void mod_tex_inter_color_using_factor_CI (wxUint32 color, wxUint32 factor)
41 {
42         float percent = factor / 255.0f;
43         float percent_i = 1 - percent;
44         wxUint8 cr, cg, cb;
45         wxUint16 col;
46         wxUint8 a, r, g, b;
47
48         cr = (wxUint8)((color >> 24) & 0xFF);
49         cg = (wxUint8)((color >> 16) & 0xFF);
50         cb = (wxUint8)((color >> 8) & 0xFF);
51
52         for (int i=0; i<256; i++)
53         {
54                 col = rdp.pal_8[i];
55                 a = (wxUint8)(col&0x0001);;
56                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
57         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
58         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
59                 r = (wxUint8)(min(255, percent_i * r + percent * cr));
60                 g = (wxUint8)(min(255, percent_i * g + percent * cg));
61                 b = (wxUint8)(min(255, percent_i * b + percent * cb));
62         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
63                           ((wxUint16)(g >> 3) << 6) |
64                           ((wxUint16)(b >> 3) << 1) |
65                                   ((wxUint16)(a ) << 0));
66         }
67 }
68
69 static void mod_tex_inter_col_using_col1_CI (wxUint32 color0, wxUint32 color1)
70 {
71         wxUint8 cr, cg, cb;
72         wxUint16 col;
73         wxUint8 a, r, g, b;
74
75         float percent_r = ((color1 >> 24) & 0xFF) / 255.0f;
76         float percent_g = ((color1 >> 16) & 0xFF) / 255.0f;
77         float percent_b = ((color1 >> 8)  & 0xFF) / 255.0f;
78         float percent_r_i = 1.0f - percent_r;
79         float percent_g_i = 1.0f - percent_g;
80         float percent_b_i = 1.0f - percent_b;
81
82         cr = (wxUint8)((color0 >> 24) & 0xFF);
83         cg = (wxUint8)((color0 >> 16) & 0xFF);
84         cb = (wxUint8)((color0 >> 8)  & 0xFF);
85
86         for (int i=0; i<256; i++)
87         {
88                 col = rdp.pal_8[i];
89                 a = (wxUint8)(col&0x0001);;
90                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
91         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
92         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
93                 r = (wxUint8)(min(255, percent_r_i * r + percent_r * cr));
94                 g = (wxUint8)(min(255, percent_g_i * g + percent_g * cg));
95                 b = (wxUint8)(min(255, percent_b_i * b + percent_b * cb));
96         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
97                           ((wxUint16)(g >> 3) << 6) |
98                           ((wxUint16)(b >> 3) << 1) |
99                                   ((wxUint16)(a ) << 0));
100         }
101 }
102
103 static void mod_full_color_sub_tex_CI (wxUint32 color)
104 {
105         wxUint8 cr, cg, cb, ca;
106         wxUint16 col;
107         wxUint8 a, r, g, b;
108
109         cr = (wxUint8)((color >> 24) & 0xFF);
110         cg = (wxUint8)((color >> 16) & 0xFF);
111         cb = (wxUint8)((color >> 8) & 0xFF);
112         ca = (wxUint8)(color & 0xFF);
113
114         for (int i=0; i<256; i++)
115         {
116                 col = rdp.pal_8[i];
117                 a = (wxUint8)(col&0x0001);;
118                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
119         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
120         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
121                 a = max(0, ca - a);
122                 r = max(0, cr - r);
123                 g = max(0, cg - g);
124                 b = max(0, cb - b);
125         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
126                           ((wxUint16)(g >> 3) << 6) |
127                           ((wxUint16)(b >> 3) << 1) |
128                                   ((wxUint16)(a ) << 0));
129         }
130 }
131
132 static void mod_col_inter_col1_using_tex_CI (wxUint32 color0, wxUint32 color1)
133 {
134         wxUint32 cr0, cg0, cb0, cr1, cg1, cb1;
135         wxUint16 col;
136         wxUint8 a, r, g, b;
137         float percent_r, percent_g, percent_b;
138
139         cr0 = (wxUint8)((color0 >> 24) & 0xFF);
140         cg0 = (wxUint8)((color0 >> 16) & 0xFF);
141         cb0 = (wxUint8)((color0 >> 8)  & 0xFF);
142         cr1 = (wxUint8)((color1 >> 24) & 0xFF);
143         cg1 = (wxUint8)((color1 >> 16) & 0xFF);
144         cb1 = (wxUint8)((color1 >> 8)  & 0xFF);
145
146         for (int i=0; i<256; i++)
147         {
148                 col = rdp.pal_8[i];
149                 a = (wxUint8)(col&0x0001);;
150                 percent_r = ((col&0xF800) >> 11) / 31.0f;
151                 percent_g = ((col&0x07C0) >> 6) / 31.0f;
152                 percent_b = ((col&0x003E) >> 1) / 31.0f;
153                 r = (wxUint8)(min((1.0f-percent_r) * cr0 + percent_r * cr1, 255));
154                 g = (wxUint8)(min((1.0f-percent_g) * cg0 + percent_g * cg1, 255));
155                 b = (wxUint8)(min((1.0f-percent_b) * cb0 + percent_b * cb1, 255));
156         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
157                           ((wxUint16)(g >> 3) << 6) |
158                           ((wxUint16)(b >> 3) << 1) |
159                                   ((wxUint16)(a ) << 0));
160         }
161 }
162
163
164
165 static void mod_tex_sub_col_mul_fac_add_tex_CI (wxUint32 color, wxUint32 factor)
166 {
167         float percent = factor / 255.0f;
168         wxUint8 cr, cg, cb, a;
169         wxUint16 col;
170         float r, g, b;
171
172         cr = (wxUint8)((color >> 24) & 0xFF);
173         cg = (wxUint8)((color >> 16) & 0xFF);
174         cb = (wxUint8)((color >> 8) & 0xFF);
175
176         for (int i=0; i<256; i++)
177         {
178                 col = rdp.pal_8[i];
179                 a = (wxUint8)(col&0x0001);;
180                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
181         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
182         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
183                 r = (r - cr) * percent + r;
184                 if (r > 255.0f) r = 255.0f;
185                 if (r < 0.0f) r = 0.0f;
186                 g = (g - cg) * percent + g;
187                 if (g > 255.0f) g = 255.0f;
188                 if (g < 0.0f) g = 0.0f;
189                 b = (b - cb) * percent + b;
190                 if (b > 255.0f) g = 255.0f;
191                 if (b < 0.0f) b = 0.0f;
192         rdp.pal_8[i] = (wxUint16)(((wxUint16)((wxUint8)(r) >> 3) << 11) |
193                           ((wxUint16)((wxUint8)(g) >> 3) << 6) |
194                           ((wxUint16)((wxUint8)(b) >> 3) << 1) |
195                                   (wxUint16)(a) );
196         }
197 }
198
199 static void mod_tex_scale_col_add_col_CI (wxUint32 color0, wxUint32 color1)
200 {
201         wxUint8 cr, cg, cb;
202         wxUint16 col;
203         wxUint8 a, r, g, b;
204
205         float percent_r = ((color0 >> 24) & 0xFF) / 255.0f;
206         float percent_g = ((color0 >> 16) & 0xFF) / 255.0f;
207         float percent_b = ((color0 >> 8)  & 0xFF) / 255.0f;
208         cr = (wxUint8)((color1 >> 24) & 0xFF);
209         cg = (wxUint8)((color1 >> 16) & 0xFF);
210         cb = (wxUint8)((color1 >> 8)  & 0xFF);
211
212         for (int i=0; i<256; i++)
213         {
214                 col = rdp.pal_8[i];
215                 a = (wxUint8)(col&0x0001);;
216                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
217         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
218         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
219                 r = (wxUint8)(min(255, percent_r * r + cr));
220                 g = (wxUint8)(min(255, percent_g * g + cg));
221                 b = (wxUint8)(min(255, percent_b * b + cb));
222         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
223                           ((wxUint16)(g >> 3) << 6) |
224                           ((wxUint16)(b >> 3) << 1) |
225                                   ((wxUint16)(a ) << 0));
226         }
227 }
228
229 static void mod_tex_add_col_CI (wxUint32 color)
230 {
231         wxUint8 cr, cg, cb;
232         wxUint16 col;
233         wxUint8 a, r, g, b;
234
235         cr = (wxUint8)((color >> 24) & 0xFF);
236         cg = (wxUint8)((color >> 16) & 0xFF);
237         cb = (wxUint8)((color >> 8) & 0xFF);
238
239         for (int i=0; i<256; i++)
240         {
241                 col = rdp.pal_8[i];
242                 a = (wxUint8)(col&0x0001);;
243                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
244         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
245         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
246                 r = min(cr + r, 255);
247                 g = min(cg + g, 255);
248                 b = min(cb + b, 255);
249         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
250                           ((wxUint16)(g >> 3) << 6) |
251                           ((wxUint16)(b >> 3) << 1) |
252                                   ((wxUint16)(a ) << 0));
253         }
254 }
255
256 static void mod_tex_sub_col_CI (wxUint32 color)
257 {
258         wxUint8 cr, cg, cb;
259         wxUint16 col;
260         wxUint8 a, r, g, b;
261
262         cr = (wxUint8)((color >> 24) & 0xFF);
263         cg = (wxUint8)((color >> 16) & 0xFF);
264         cb = (wxUint8)((color >> 8) & 0xFF);
265
266         for (int i=0; i<256; i++)
267         {
268                 col = rdp.pal_8[i];
269                 a = (wxUint8)(col&0x0001);;
270                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
271         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
272         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
273                 r = max(r - cr, 0);
274                 g = max(g - cg, 0);
275                 b = max(b - cb, 0);
276         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
277                           ((wxUint16)(g >> 3) << 6) |
278                           ((wxUint16)(b >> 3) << 1) |
279                                   ((wxUint16)(a ) << 0));
280         }
281 }
282
283 static void mod_tex_sub_col_mul_fac_CI (wxUint32 color, wxUint32 factor)
284 {
285         float percent = factor / 255.0f;
286         wxUint8 cr, cg, cb;
287         wxUint16 col;
288         wxUint8 a;
289         float r, g, b;
290
291         cr = (wxUint8)((color >> 24) & 0xFF);
292         cg = (wxUint8)((color >> 16) & 0xFF);
293         cb = (wxUint8)((color >> 8) & 0xFF);
294
295         for (int i=0; i<256; i++)
296         {
297                 col = rdp.pal_8[i];
298                 a = (wxUint8)(col&0x0001);
299                 r = (float)((col&0xF800) >> 11) / 31.0f * 255.0f;
300         g = (float)((col&0x07C0) >> 6) / 31.0f * 255.0f;
301         b = (float)((col&0x003E) >> 1) / 31.0f * 255.0f;
302                 r = (r - cr) * percent;
303                 if (r > 255.0f) r = 255.0f;
304                 if (r < 0.0f) r = 0.0f;
305                 g = (g - cg) * percent;
306                 if (g > 255.0f) g = 255.0f;
307                 if (g < 0.0f) g = 0.0f;
308                 b = (b - cb) * percent;
309                 if (b > 255.0f) g = 255.0f;
310                 if (b < 0.0f) b = 0.0f;
311
312         rdp.pal_8[i] = (wxUint16)(((wxUint16)((wxUint8)(r) >> 3) << 11) |
313                           ((wxUint16)((wxUint8)(g) >> 3) << 6) |
314                           ((wxUint16)((wxUint8)(b) >> 3) << 1) |
315                                   (wxUint16)(a) );
316         }
317 }
318
319 static void mod_col_inter_tex_using_col1_CI (wxUint32 color0, wxUint32 color1)
320 {
321         wxUint8 cr, cg, cb;
322         wxUint16 col;
323         wxUint8 a, r, g, b;
324
325         float percent_r = ((color1 >> 24) & 0xFF) / 255.0f;
326         float percent_g = ((color1 >> 16) & 0xFF) / 255.0f;
327         float percent_b = ((color1 >> 8)  & 0xFF) / 255.0f;
328         float percent_r_i = 1.0f - percent_r;
329         float percent_g_i = 1.0f - percent_g;
330         float percent_b_i = 1.0f - percent_b;
331
332         cr = (wxUint8)((color0 >> 24) & 0xFF);
333         cg = (wxUint8)((color0 >> 16) & 0xFF);
334         cb = (wxUint8)((color0 >> 8)  & 0xFF);
335
336         for (int i=0; i<256; i++)
337         {
338                 col = rdp.pal_8[i];
339                 a = (wxUint8)(col&0x0001);;
340                 r = (wxUint8)((float)((col&0xF800) >> 11) / 31.0f * 255.0f);
341         g = (wxUint8)((float)((col&0x07C0) >> 6) / 31.0f * 255.0f);
342         b = (wxUint8)((float)((col&0x003E) >> 1) / 31.0f * 255.0f);
343                 r = (wxUint8)(min(255, percent_r * r + percent_r_i * cr));
344                 g = (wxUint8)(min(255, percent_g * g + percent_g_i * cg));
345                 b = (wxUint8)(min(255, percent_b * b + percent_b_i * cb));
346         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
347                           ((wxUint16)(g >> 3) << 6) |
348                           ((wxUint16)(b >> 3) << 1) |
349                                   ((wxUint16)(a ) << 0));
350         }
351 }
352
353 static void mod_tex_inter_col_using_texa_CI (wxUint32 color)
354 {
355         wxUint8 a, r, g, b;
356
357         r = (wxUint8)((float)((color >> 24) & 0xFF) / 255.0f * 31.0f);
358     g = (wxUint8)((float)((color >> 16) & 0xFF) / 255.0f * 31.0f);
359     b = (wxUint8)((float)((color >> 8)  & 0xFF) / 255.0f * 31.0f);
360     a = (color&0xFF) ? 1 : 0;
361     wxUint16 col16 = (wxUint16)((r<<11)|(g<<6)|(b<<1)|a);
362
363         for (int i=0; i<256; i++)
364         {
365             if (rdp.pal_8[i]&1)
366           rdp.pal_8[i] = col16;
367         }
368 }
369
370 static void mod_tex_mul_col_CI (wxUint32 color)
371 {
372         wxUint8 a, r, g, b;
373         wxUint16 col;
374         float cr, cg, cb;
375
376         cr = (float)((color >> 24) & 0xFF) / 255.0f;
377     cg = (float)((color >> 16) & 0xFF) / 255.0f;
378     cb = (float)((color >> 8)  & 0xFF) / 255.0f;
379
380         for (int i=0; i<256; i++)
381         {
382                 col = rdp.pal_8[i];
383                 a = (wxUint8)(col&0x0001);;
384                 r = (wxUint8)((float)((col&0xF800) >> 11) * cr);
385         g = (wxUint8)((float)((col&0x07C0) >> 6) * cg);
386         b = (wxUint8)((float)((col&0x003E) >> 1) * cb);
387         rdp.pal_8[i] = (wxUint16)(((wxUint16)(r >> 3) << 11) |
388                           ((wxUint16)(g >> 3) << 6) |
389                           ((wxUint16)(b >> 3) << 1) |
390                                   ((wxUint16)(a ) << 0));
391         }
392 }
393
394 static void ModifyPalette(wxUint32 mod, wxUint32 modcolor, wxUint32 modcolor1, wxUint32 modfactor)
395 {
396                 switch (mod)
397                 {
398                 case TMOD_TEX_INTER_COLOR_USING_FACTOR:
399                         mod_tex_inter_color_using_factor_CI (modcolor, modfactor);
400                         break;
401                 case TMOD_TEX_INTER_COL_USING_COL1:
402                         mod_tex_inter_col_using_col1_CI (modcolor, modcolor1);
403                         break;
404                 case TMOD_FULL_COLOR_SUB_TEX:
405                         mod_full_color_sub_tex_CI (modcolor);
406                         break;
407                 case TMOD_COL_INTER_COL1_USING_TEX:
408                         mod_col_inter_col1_using_tex_CI (modcolor, modcolor1);
409                         break;
410                 case TMOD_TEX_SUB_COL_MUL_FAC_ADD_TEX:
411                         mod_tex_sub_col_mul_fac_add_tex_CI (modcolor, modfactor);
412                         break;
413                 case TMOD_TEX_SCALE_COL_ADD_COL:
414                         mod_tex_scale_col_add_col_CI (modcolor, modcolor1);
415                         break;
416                 case TMOD_TEX_ADD_COL:
417                         mod_tex_add_col_CI (modcolor);
418                         break;
419                 case TMOD_TEX_SUB_COL:
420                         mod_tex_sub_col_CI (modcolor);
421                         break;
422                 case TMOD_TEX_SUB_COL_MUL_FAC:
423                         mod_tex_sub_col_mul_fac_CI (modcolor, modfactor);
424                         break;
425                 case TMOD_COL_INTER_TEX_USING_COL1:
426                         mod_col_inter_tex_using_col1_CI (modcolor, modcolor1);
427                         break;
428         case TMOD_TEX_INTER_COL_USING_TEXA:
429             mod_tex_inter_col_using_texa_CI (modcolor);
430             break;
431         case TMOD_TEX_MUL_COL:
432             mod_tex_mul_col_CI (modcolor);
433             break;
434                 default:
435                         ;
436            }
437 }