~random-stuff/random-stuff/reicast

« back to all changes in this revision

Viewing changes to core/rend/TexCache.h

  • Committer: Stefanos Kornilios Mitsis Poiitidis
  • Date: 2013-12-28 21:28:50 UTC
  • mfrom: (8.1.19)
  • Revision ID: git-v1:f5d29677b32441c3f7870a198a1d5a8b0bdb8b9c
Merge branch 'androidui'

Conflicts:
        core/hw/pvr/spg.cpp
        core/hw/sh4/interpr/sh4_interpreter.cpp
        reicast/android/src/com/reicast/emulator/MainActivity.java

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#pragma once
 
2
#include "oslib/oslib.h"
 
3
 
 
4
extern u8* vq_codebook;
 
5
extern u32 palette_index;
 
6
extern u32 palette_ram[1024];
 
7
extern bool pal_needs_update,fog_needs_update,KillTex;
 
8
extern u32 pal_rev_256[4];
 
9
extern u32 pal_rev_16[64];
 
10
extern u32 _pal_rev_256[4];
 
11
extern u32 _pal_rev_16[64];
 
12
 
 
13
extern u32 detwiddle[2][8][1024];
 
14
 
 
15
//Pixel buffer class (realy helpfull ;) )
 
16
struct PixelBuffer
 
17
{
 
18
        u16* p_buffer_start;
 
19
        u16* p_current_line;
 
20
        u16* p_current_pixel;
 
21
 
 
22
        u32 pixels_per_line;
 
23
 
 
24
        void init(void* data,u32 ppl_bytes)
 
25
        {
 
26
                p_buffer_start=p_current_line=p_current_pixel=(u16*)data;
 
27
                pixels_per_line=ppl_bytes/sizeof(u16);
 
28
        }
 
29
        __forceinline void prel(u32 x,u16 value)
 
30
        {
 
31
                p_current_pixel[x]=value;
 
32
        }
 
33
 
 
34
        __forceinline void prel(u32 x,u32 y,u16 value)
 
35
        {
 
36
                p_current_pixel[y*pixels_per_line+x]=value;
 
37
        }
 
38
 
 
39
        __forceinline void rmovex(u32 value)
 
40
        {
 
41
                p_current_pixel+=value;
 
42
        }
 
43
        __forceinline void rmovey(u32 value)
 
44
        {
 
45
                p_current_line+=pixels_per_line*value;
 
46
                p_current_pixel=p_current_line;
 
47
        }
 
48
        __forceinline void amove(u32 x_m,u32 y_m)
 
49
        {
 
50
                //p_current_pixel=p_buffer_start;
 
51
                p_current_line=p_buffer_start+pixels_per_line*y_m;
 
52
                p_current_pixel=p_current_line + x_m;
 
53
        }
 
54
};
 
55
 
 
56
void palette_update();
 
57
 
 
58
#define clamp(minv,maxv,x) min(maxv,max(minv,x))
 
59
 
 
60
#define ARGB1555( word )        ( ((word>>15)&1) | (((word>>10) & 0x1F)<<11)  | (((word>>5) & 0x1F)<<6)  | (((word>>0) & 0x1F)<<1) )
 
61
//      ARGB8888(unpack_1_to_8[(word>>15)&1],unpack_5_to_8[(word>>10) & 0x1F],  
 
62
//unpack_5_to_8[(word>>5) & 0x1F],unpack_5_to_8[word&0x1F])
 
63
 
 
64
#define ARGB565( word ) ( (((word>>0)&0x1F)<<0) | (((word>>5)&0x3F)<<5) | (((word>>11)&0x1F)<<11) )
 
65
        
 
66
//ARGB8888(0xFF,unpack_5_to_8[(word>>11) & 0x1F],       unpack_6_to_8[(word>>5) & 0x3F],unpack_5_to_8[word&0x1F])
 
67
//( 0xFF000000 | unpack_5_to_8[(word>>11) & 0x1F] | unpack_5_to_8[(word>>5) & 0x3F]<<8 | unpack_5_to_8[word&0x1F]<<16 )
 
68
 
 
69
#define ARGB4444( word ) ( (((word>>0)&0xF)<<4) | (((word>>4)&0xF)<<8) | (((word>>8)&0xF)<<12) | (((word>>12)&0xF)<<0) )
 
70
//ARGB8888( (word&0xF000)>>(12-4),(word&0xF00)>>(8-4),(word&0xF0)>>(4-4),(word&0xF)<<4 )
 
71
 
 
72
#define ARGB8888( word ) ( (((word>>4)&0xF)<<4) | (((word>>12)&0xF)<<8) | (((word>>20)&0xF)<<12) | (((word>>28)&0xF)<<0) )
 
73
 
 
74
template<class PixelPacker>
 
75
__forceinline u32 YUV422(s32 Y,s32 Yu,s32 Yv)
 
76
{
 
77
        Yu-=128;
 
78
        Yv-=128;
 
79
 
 
80
        //s32 B = (76283*(Y - 16) + 132252*(Yu - 128))>>16;
 
81
        //s32 G = (76283*(Y - 16) - 53281 *(Yv - 128) - 25624*(Yu - 128))>>16;
 
82
        //s32 R = (76283*(Y - 16) + 104595*(Yv - 128))>>16;
 
83
        
 
84
        s32 R = Y + Yv*11/8;            // Y + (Yv-128) * (11/8) ?
 
85
        s32 G = Y - (Yu*11 + Yv*22)/32; // Y - (Yu-128) * (11/8) * 0.25 - (Yv-128) * (11/8) * 0.5 ?
 
86
        s32 B = Y + Yu*110/64;          // Y + (Yu-128) * (11/8) * 1.25 ?
 
87
 
 
88
        return PixelPacker::packRGB(clamp(0,255,R),clamp(0,255,G),clamp(0,255,B));
 
89
}
 
90
 
 
91
#define twop(x,y,bcx,bcy) (detwiddle[0][bcy][x]+detwiddle[1][bcx][y])
 
92
 
 
93
//pixel packers !
 
94
struct pp_565
 
95
{
 
96
        __forceinline static u32 packRGB(u8 R,u8 G,u8 B)
 
97
        {
 
98
                R>>=3;
 
99
                G>>=2;
 
100
                B>>=3;
 
101
                return (R<<11) | (G<<5) | (B<<0);
 
102
        }
 
103
};
 
104
 
 
105
//pixel convertors !
 
106
#define pixelcvt_start(name,x,y) template<class PixelPacker> \
 
107
struct name \
 
108
{ \
 
109
        static const u32 xpp=x;\
 
110
        static const u32 ypp=y; \
 
111
        __forceinline static void Convert(PixelBuffer* pb,u8* data) \
 
112
{
 
113
 
 
114
#define pixelcvt_end } }
 
115
#define pixelcvt_next(name,x,y) pixelcvt_end;  pixelcvt_start(name,x,y)
 
116
//Non twiddled
 
117
pixelcvt_start(conv565_PL,4,1)
 
118
{
 
119
        //convert 4x1
 
120
        u16* p_in=(u16*)data;
 
121
        //0,0
 
122
        pb->prel(0,ARGB565(p_in[0]));
 
123
        //1,0
 
124
        pb->prel(1,ARGB565(p_in[1]));
 
125
        //2,0
 
126
        pb->prel(2,ARGB565(p_in[2]));
 
127
        //3,0
 
128
        pb->prel(3,ARGB565(p_in[3]));
 
129
}
 
130
pixelcvt_next(conv1555_PL,4,1)
 
131
{
 
132
        //convert 4x1
 
133
        u16* p_in=(u16*)data;
 
134
        //0,0
 
135
        pb->prel(0,ARGB1555(p_in[0]));
 
136
        //1,0
 
137
        pb->prel(1,ARGB1555(p_in[1]));
 
138
        //2,0
 
139
        pb->prel(2,ARGB1555(p_in[2]));
 
140
        //3,0
 
141
        pb->prel(3,ARGB1555(p_in[3]));
 
142
}
 
143
pixelcvt_next(conv4444_PL,4,1)
 
144
{
 
145
        //convert 4x1
 
146
        u16* p_in=(u16*)data;
 
147
        //0,0
 
148
        pb->prel(0,ARGB4444(p_in[0]));
 
149
        //1,0
 
150
        pb->prel(1,ARGB4444(p_in[1]));
 
151
        //2,0
 
152
        pb->prel(2,ARGB4444(p_in[2]));
 
153
        //3,0
 
154
        pb->prel(3,ARGB4444(p_in[3]));
 
155
}
 
156
pixelcvt_next(convYUV_PL,4,1)
 
157
{
 
158
        //convert 4x1 4444 to 4x1 8888
 
159
        u32* p_in=(u32*)data;
 
160
 
 
161
 
 
162
        s32 Y0 = (p_in[0]>>8) &255; //
 
163
        s32 Yu = (p_in[0]>>0) &255; //p_in[0]
 
164
        s32 Y1 = (p_in[0]>>24) &255; //p_in[3]
 
165
        s32 Yv = (p_in[0]>>16) &255; //p_in[2]
 
166
 
 
167
        //0,0
 
168
        pb->prel(0,YUV422<PixelPacker>(Y0,Yu,Yv));
 
169
        //1,0
 
170
        pb->prel(1,YUV422<PixelPacker>(Y1,Yu,Yv));
 
171
 
 
172
        //next 4 bytes
 
173
        p_in+=1;
 
174
 
 
175
        Y0 = (p_in[0]>>8) &255; //
 
176
        Yu = (p_in[0]>>0) &255; //p_in[0]
 
177
        Y1 = (p_in[0]>>24) &255; //p_in[3]
 
178
        Yv = (p_in[0]>>16) &255; //p_in[2]
 
179
 
 
180
        //0,0
 
181
        pb->prel(2,YUV422<PixelPacker>(Y0,Yu,Yv));
 
182
        //1,0
 
183
        pb->prel(3,YUV422<PixelPacker>(Y1,Yu,Yv));
 
184
}
 
185
pixelcvt_end;
 
186
//twiddled 
 
187
pixelcvt_start(conv565_TW,2,2)
 
188
{
 
189
        //convert 4x1 565 to 4x1 8888
 
190
        u16* p_in=(u16*)data;
 
191
        //0,0
 
192
        pb->prel(0,0,ARGB565(p_in[0]));
 
193
        //0,1
 
194
        pb->prel(0,1,ARGB565(p_in[1]));
 
195
        //1,0
 
196
        pb->prel(1,0,ARGB565(p_in[2]));
 
197
        //1,1
 
198
        pb->prel(1,1,ARGB565(p_in[3]));
 
199
}
 
200
pixelcvt_next(conv1555_TW,2,2)
 
201
{
 
202
        //convert 4x1 565 to 4x1 8888
 
203
        u16* p_in=(u16*)data;
 
204
        //0,0
 
205
        pb->prel(0,0,ARGB1555(p_in[0]));
 
206
        //0,1
 
207
        pb->prel(0,1,ARGB1555(p_in[1]));
 
208
        //1,0
 
209
        pb->prel(1,0,ARGB1555(p_in[2]));
 
210
        //1,1
 
211
        pb->prel(1,1,ARGB1555(p_in[3]));
 
212
}
 
213
pixelcvt_next(conv4444_TW,2,2)
 
214
{
 
215
        //convert 4x1 565 to 4x1 8888
 
216
        u16* p_in=(u16*)data;
 
217
        //0,0
 
218
        pb->prel(0,0,ARGB4444(p_in[0]));
 
219
        //0,1
 
220
        pb->prel(0,1,ARGB4444(p_in[1]));
 
221
        //1,0
 
222
        pb->prel(1,0,ARGB4444(p_in[2]));
 
223
        //1,1
 
224
        pb->prel(1,1,ARGB4444(p_in[3]));
 
225
}
 
226
pixelcvt_next(convYUV_TW,2,2)
 
227
{
 
228
        //convert 4x1 4444 to 4x1 8888
 
229
        u16* p_in=(u16*)data;
 
230
 
 
231
 
 
232
        s32 Y0 = (p_in[0]>>8) &255; //
 
233
        s32 Yu = (p_in[0]>>0) &255; //p_in[0]
 
234
        s32 Y1 = (p_in[2]>>8) &255; //p_in[3]
 
235
        s32 Yv = (p_in[2]>>0) &255; //p_in[2]
 
236
 
 
237
        //0,0
 
238
        pb->prel(0,0,YUV422<PixelPacker>(Y0,Yu,Yv));
 
239
        //1,0
 
240
        pb->prel(1,0,YUV422<PixelPacker>(Y1,Yu,Yv));
 
241
 
 
242
        //next 4 bytes
 
243
        //p_in+=2;
 
244
 
 
245
        Y0 = (p_in[1]>>8) &255; //
 
246
        Yu = (p_in[1]>>0) &255; //p_in[0]
 
247
        Y1 = (p_in[3]>>8) &255; //p_in[3]
 
248
        Yv = (p_in[3]>>0) &255; //p_in[2]
 
249
 
 
250
        //0,1
 
251
        pb->prel(0,1,YUV422<PixelPacker>(Y0,Yu,Yv));
 
252
        //1,1
 
253
        pb->prel(1,1,YUV422<PixelPacker>(Y1,Yu,Yv));
 
254
}
 
255
pixelcvt_end;
 
256
 
 
257
pixelcvt_start(convPAL4_TW,4,4)
 
258
{
 
259
        u8* p_in=(u8*)data;
 
260
        u32* pal=&palette_ram[palette_index];
 
261
 
 
262
        pb->prel(0,0,pal[p_in[0]&0xF]);
 
263
        pb->prel(0,1,pal[(p_in[0]>>4)&0xF]);p_in++;
 
264
        pb->prel(1,0,pal[p_in[0]&0xF]);
 
265
        pb->prel(1,1,pal[(p_in[0]>>4)&0xF]);p_in++;
 
266
 
 
267
        pb->prel(0,2,pal[p_in[0]&0xF]);
 
268
        pb->prel(0,3,pal[(p_in[0]>>4)&0xF]);p_in++;
 
269
        pb->prel(1,2,pal[p_in[0]&0xF]);
 
270
        pb->prel(1,3,pal[(p_in[0]>>4)&0xF]);p_in++;
 
271
 
 
272
        pb->prel(2,0,pal[p_in[0]&0xF]);
 
273
        pb->prel(2,1,pal[(p_in[0]>>4)&0xF]);p_in++;
 
274
        pb->prel(3,0,pal[p_in[0]&0xF]);
 
275
        pb->prel(3,1,pal[(p_in[0]>>4)&0xF]);p_in++;
 
276
 
 
277
        pb->prel(2,2,pal[p_in[0]&0xF]);
 
278
        pb->prel(2,3,pal[(p_in[0]>>4)&0xF]);p_in++;
 
279
        pb->prel(3,2,pal[p_in[0]&0xF]);
 
280
        pb->prel(3,3,pal[(p_in[0]>>4)&0xF]);p_in++;
 
281
}
 
282
pixelcvt_next(convPAL8_TW,2,4)
 
283
{
 
284
        u8* p_in=(u8*)data;
 
285
        u32* pal=&palette_ram[palette_index];
 
286
 
 
287
        pb->prel(0,0,pal[p_in[0]]);p_in++;
 
288
        pb->prel(0,1,pal[p_in[0]]);p_in++;
 
289
        pb->prel(1,0,pal[p_in[0]]);p_in++;
 
290
        pb->prel(1,1,pal[p_in[0]]);p_in++;
 
291
 
 
292
        pb->prel(0,2,pal[p_in[0]]);p_in++;
 
293
        pb->prel(0,3,pal[p_in[0]]);p_in++;
 
294
        pb->prel(1,2,pal[p_in[0]]);p_in++;
 
295
        pb->prel(1,3,pal[p_in[0]]);p_in++;
 
296
}
 
297
pixelcvt_end;
 
298
//handler functions
 
299
template<class PixelConvertor>
 
300
void texture_PL(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height)
 
301
{
 
302
        pb->amove(0,0);
 
303
 
 
304
        Height/=PixelConvertor::ypp;
 
305
        Width/=PixelConvertor::xpp;
 
306
 
 
307
        for (u32 y=0;y<Height;y++)
 
308
        {
 
309
                for (u32 x=0;x<Width;x++)
 
310
                {
 
311
                        u8* p = p_in;
 
312
                        PixelConvertor::Convert(pb,p);
 
313
                        p_in+=8;
 
314
 
 
315
                        pb->rmovex(PixelConvertor::xpp);
 
316
                }
 
317
                pb->rmovey(PixelConvertor::ypp);
 
318
        }
 
319
}
 
320
 
 
321
template<class PixelConvertor>
 
322
void texture_TW(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height)
 
323
{
 
324
        pb->amove(0,0);
 
325
 
 
326
        const u32 divider=PixelConvertor::xpp*PixelConvertor::ypp;
 
327
 
 
328
        unsigned long bcx_,bcy_;
 
329
        bcx_=bitscanrev(Width);
 
330
        bcy_=bitscanrev(Height);
 
331
        const u32 bcx=bcx_-3;
 
332
        const u32 bcy=bcy_-3;
 
333
 
 
334
        for (u32 y=0;y<Height;y+=PixelConvertor::ypp)
 
335
        {
 
336
                for (u32 x=0;x<Width;x+=PixelConvertor::xpp)
 
337
                {
 
338
                        u8* p = &p_in[(twop(x,y,bcx,bcy)/divider)<<3];
 
339
                        PixelConvertor::Convert(pb,p);
 
340
 
 
341
                        pb->rmovex(PixelConvertor::xpp);
 
342
                }
 
343
                pb->rmovey(PixelConvertor::ypp);
 
344
        }
 
345
}
 
346
 
 
347
template<class PixelConvertor>
 
348
void texture_VQ(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height)
 
349
{
 
350
        p_in+=256*4*2;
 
351
        pb->amove(0,0);
 
352
 
 
353
        const u32 divider=PixelConvertor::xpp*PixelConvertor::ypp;
 
354
        unsigned long bcx_,bcy_;
 
355
        bcx_=bitscanrev(Width);
 
356
        bcy_=bitscanrev(Height);
 
357
        const u32 bcx=bcx_-3;
 
358
        const u32 bcy=bcy_-3;
 
359
 
 
360
        for (u32 y=0;y<Height;y+=PixelConvertor::ypp)
 
361
        {
 
362
                for (u32 x=0;x<Width;x+=PixelConvertor::xpp)
 
363
                {
 
364
                        u8 p = p_in[twop(x,y,bcx,bcy)/divider];
 
365
                        PixelConvertor::Convert(pb,&vq_codebook[p*8]);
 
366
 
 
367
                        pb->rmovex(PixelConvertor::xpp);
 
368
                }
 
369
                pb->rmovey(PixelConvertor::ypp);
 
370
        }
 
371
}
 
372
 
 
373
//We ask the compiler to generate the templates here
 
374
//;)
 
375
//planar formats !
 
376
template void texture_PL<conv565_PL<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
377
template void texture_PL<conv1555_PL<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
378
template void texture_PL<conv4444_PL<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
379
template void texture_PL<convYUV_PL<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
380
 
 
381
//twiddled formats !
 
382
template void texture_TW<conv565_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
383
template void texture_TW<conv1555_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
384
template void texture_TW<conv4444_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
385
template void texture_TW<convYUV_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
386
 
 
387
template void texture_TW<convPAL4_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
388
template void texture_TW<convPAL8_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
389
 
 
390
//VQ formats !
 
391
template void texture_VQ<conv565_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
392
template void texture_VQ<conv1555_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
393
template void texture_VQ<conv4444_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
394
template void texture_VQ<convYUV_TW<pp_565> >(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
395
 
 
396
//Planar
 
397
#define tex565_PL texture_PL<conv565_PL<pp_565> >
 
398
#define tex1555_PL texture_PL<conv1555_PL<pp_565> >
 
399
#define tex4444_PL texture_PL<conv4444_PL<pp_565> >
 
400
#define texYUV422_PL texture_PL<convYUV_PL<pp_565> >
 
401
 
 
402
//Twiddle
 
403
#define tex565_TW texture_TW<conv565_TW<pp_565> >
 
404
#define tex1555_TW texture_TW<conv1555_TW<pp_565> >
 
405
#define tex4444_TW texture_TW<conv4444_TW<pp_565> >
 
406
#define texYUV422_TW texture_TW<convYUV_TW<pp_565> >
 
407
#define texPAL4_TW texture_TW<convPAL4_TW<pp_565> >
 
408
#define texPAL8_TW  texture_TW<convPAL8_TW<pp_565> >
 
409
 
 
410
//VQ
 
411
#define tex565_VQ texture_VQ<conv565_TW<pp_565> >
 
412
#define tex1555_VQ texture_VQ<conv1555_TW<pp_565> >
 
413
#define tex4444_VQ texture_VQ<conv4444_TW<pp_565> >
 
414
#define texYUV422_VQ texture_VQ<convYUV_TW<pp_565> >
 
415
 
 
416
void texture_PAL4(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
417
void texture_PAL8(PixelBuffer* pb,u8* p_in,u32 Width,u32 Height);
 
418
 
 
419
 
 
420
#define Is_64_Bit(addr) ((addr &0x1000000)==0)
 
421
 
 
422
//vram_block, vramLockCBFP on plugin headers
 
423
 
 
424
 
 
425
u32 vramlock_ConvAddrtoOffset64(u32 Address);
 
426
u32 vramlock_ConvOffset32toOffset64(u32 offset32);
 
427
 
 
428
void vramlock_Unlock_block(vram_block* block);
 
429
vram_block* vramlock_Lock_32(u32 start_offset32,u32 end_offset32,void* userdata);
 
430
vram_block* vramlock_Lock_64(u32 start_offset64,u32 end_offset64,void* userdata);
 
431
 
 
432
void vram_LockedWrite(u32 offset64);
 
 
b'\\ No newline at end of file'