aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Visualization/vis_avs/r_blit.cpp
blob: 901df213e35f403ff16748f448c61dc41aa9551c (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
  LICENSE
  -------
Copyright 2005 Nullsoft, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer. 

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution. 

  * Neither the name of Nullsoft nor the names of its contributors may be used to 
    endorse or promote products derived from this software without specific prior written permission. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
// alphachannel safe 11/21/99
// highly optimized on 10/10/00 JF. MMX.

#include <windows.h>
#include <commctrl.h>
#include "r_defs.h"
#include "resource.h"

#include "timing.h"
#include "../Agave/Language/api_language.h"

#ifndef LASER


#define C_THISCLASS C_BlitClass
#define MOD_NAME "Trans / Blitter Feedback"

static const unsigned int revn[2]={0xff00ff,0xff00ff};//{0x1000100,0x1000100}; <<- this is actually more correct, but we're going for consistency vs. the non-mmx ver-jf
static const int zero=0;



class C_THISCLASS : public C_RBASE {
	protected:
	public:
		C_THISCLASS();
		virtual ~C_THISCLASS();
		virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
		virtual char *get_desc() { static char desc[128]; return (!desc[0]?WASABI_API_LNGSTRING_BUF(IDS_TRANS_BLITTER_FEEDBACK,desc,128):desc); }
		virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
		virtual void load_config(unsigned char *data, int len);
		virtual int  save_config(unsigned char *data);

		int scale, scale2, blend,beatch;
		int fpos;
    int subpixel;


  int blitter_normal(int *framebuffer, int *fbout, int w, int h, int f_val);
  int blitter_out(int *framebuffer, int *fbout, int w, int h, int f_val);
};


#define PUT_INT(y) data[pos]=(y)&255; data[pos+1]=(y>>8)&255; data[pos+2]=(y>>16)&255; data[pos+3]=(y>>24)&255
#define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
void C_THISCLASS::load_config(unsigned char *data, int len)
{
	int pos=0;
	if (len-pos >= 4) { scale=GET_INT(); pos+=4; }
	if (len-pos >= 4) { scale2=GET_INT(); pos+=4; }
	if (len-pos >= 4) { blend=GET_INT(); pos+=4; }
	if (len-pos >= 4) { beatch=GET_INT(); pos+=4; }
	if (len-pos >= 4) { subpixel=GET_INT(); pos+=4; }
  else subpixel=0;

	fpos=scale;
	
}
int  C_THISCLASS::save_config(unsigned char *data)
{
	int pos=0;
	PUT_INT(scale); pos+=4;
	PUT_INT(scale2); pos+=4;
	PUT_INT(blend); pos+=4;
	PUT_INT(beatch); pos+=4;
  PUT_INT(subpixel); pos+=4;
	return pos;
}


C_THISCLASS::C_THISCLASS()
{
	scale=30;
	scale2=30;
	blend=0;
	fpos=scale;
	beatch=0;
  subpixel=1;
}

C_THISCLASS::~C_THISCLASS()
{
}

int C_THISCLASS::blitter_out(int *framebuffer, int *fbout, int w, int h, int f_val)
{
	const int adj=7;
	int ds_x = ((f_val+(1<<adj)-32)<<(16-adj));	
	int x_len=((w<<16)/ds_x)&~3;
	int y_len=(h<<16)/ds_x;

  if (x_len >= w || y_len >= h) return 0;


	int start_x=(w-x_len)/2;
	int start_y=(h-y_len)/2;
	int s_y=32768;

	int *dest=(int *)framebuffer+start_y*w + start_x;
	int *src=(int *)fbout+start_y*w + start_x;
  int y;

	fbout += start_y*w;
	for (y = 0; y < y_len; y ++)
	{
		int s_x=32768;
		unsigned int *src = ((unsigned int *) framebuffer)+(s_y>>16)*w;
		unsigned int *old_dest=((unsigned int *)fbout) + start_x;
		s_y+=ds_x;
    if (!blend) 
		{
			int x=x_len/4;
			while (x--)
			{ 
				old_dest[0]=src[s_x>>16];
				s_x += ds_x;
				old_dest[1]=src[s_x>>16];
				s_x += ds_x;
				old_dest[2]=src[s_x>>16];
				s_x += ds_x;
				old_dest[3]=src[s_x>>16];
				s_x += ds_x;
				old_dest+=4;
			}
		}
		else
		{
			unsigned int *s2=(unsigned int *)framebuffer+((y+start_y)*w)+start_x;
			int x=x_len/4;
			while (x--)
			{ 
				old_dest[0]=BLEND_AVG(s2[0],src[s_x>>16]);
				s_x += ds_x;
				old_dest[1]=BLEND_AVG(s2[1],src[s_x>>16]);
				s_x += ds_x;
				old_dest[2]=BLEND_AVG(s2[2],src[s_x>>16]);
				s_x += ds_x;
				old_dest[3]=BLEND_AVG(s2[3],src[s_x>>16]);
				s2+=4;
				old_dest+=4;
				s_x += ds_x;
			}
 		}
		fbout += w;
	}
	for (y = 0; y < y_len; y ++)
	{
		memcpy(dest,src,x_len*sizeof(int));
		dest+=w;
		src+=w;
	}

  return 0;
}



int C_THISCLASS::blitter_normal(int *framebuffer, int *fbout, int w, int h, int f_val)
{
	int ds_x = ((f_val+32)<<16)/64;
	int isx=(((w<<16)-((ds_x*w)))/2);
	int s_y=(((h<<16)-((ds_x*h)))/2);

  if (subpixel)
  {
    int y;
	  for (y = 0; y < h; y ++)
	  {
		  int s_x=isx;
		  unsigned int *src = ((unsigned int *) framebuffer)+(s_y>>16)*w;
      int ypart=(s_y>>8)&0xff;
		  s_y+=ds_x;
#ifdef NO_MMX
      {
        ypart=(ypart*255)>>8;
    		int x=w/4;
		  	while (x--)
			  {
          fbout[0]=BLEND4(src+(s_x>>16),w,(s_x>>8)&0xff,ypart);
					s_x += ds_x;
          fbout[1]=BLEND4(src+(s_x>>16),w,(s_x>>8)&0xff,ypart);
					s_x += ds_x;
          fbout[2]=BLEND4(src+(s_x>>16),w,(s_x>>8)&0xff,ypart);
					s_x += ds_x;
          fbout[3]=BLEND4(src+(s_x>>16),w,(s_x>>8)&0xff,ypart);
					s_x += ds_x;
          fbout+=4;
				}
      }
#else
      {
          __int64 mem5,mem7;
          __asm
          {
            mov edi, fbout         
            mov esi, w

            movd mm7, ypart
            punpcklwd mm7,mm7

            movq mm5, revn
            punpckldq mm7,mm7

            psubw mm5, mm7         
            mov ecx, esi

            movq mem5,mm5
            movq mem7,mm7

            shr ecx, 1
            mov edx, s_x

            align 16
      mmx_scale_loop1:

            mov ebx, edx
            mov eax, edx

            shr eax, 14
            add edx, ds_x

            shr ebx, 8
            and eax, ~3
            
            add eax, src
            and ebx, 0xff

            movd mm6, ebx
            //

            movq mm4, revn
            punpcklwd mm6,mm6

            movd mm0, [eax]
            punpckldq mm6,mm6

            movd mm1, [eax+4]
            psubw mm4, mm6

            movd mm2, [eax+esi*4]
            punpcklbw mm0, [zero]

            movd mm3, [eax+esi*4+4]
            pmullw mm0, mm4         // mm0 used in divide for 3 cycles

            punpcklbw mm1, [zero]
            mov eax, edx

            pmullw mm1, mm6         // mm1 used in divide for 3 cycles
            punpcklbw mm2, [zero]

            pmullw mm2, mm4         // mm2 used in divide for 3 cycles
            punpcklbw mm3, [zero]

            pmullw mm3, mm6         // mm3 used in divide for 3 cycles
            shr eax, 14

            mov ebx, edx
            and eax, ~3

            paddw mm0, mm1
            shr ebx, 8

            psrlw mm0, 8
            add eax, src            

            paddw mm2, mm3
            and ebx, 0xff

            pmullw mm0, mem5
            psrlw mm2, 8
                        
            pmullw mm2, mem7
            add edx, ds_x


            movd mm6, ebx
            // 

            movq mm4, revn
            punpcklwd mm6,mm6


            movd mm1, [eax+4]
            //

            movd mm7, [eax+esi*4]
            paddw mm0, mm2

            movd mm5, [eax]
            psrlw mm0, 8

            movd mm3, [eax+esi*4+4]
            packuswb mm0, mm0

            movd [edi], mm0
            punpckldq mm6,mm6

            //poop

            punpcklbw mm5, [zero]
            psubw mm4, mm6

            punpcklbw mm1, [zero]
            pmullw mm5, mm4

            punpcklbw mm7, [zero]
            pmullw mm1, mm6

            punpcklbw mm3, [zero]
            pmullw mm7, mm4

            //cycle
            //cycle
            
            paddw mm5, mm1
            //

            pmullw mm3, mm6
            psrlw mm5, 8

            pmullw mm5, mem5
            add edi, 8

            // cycle
            // cycle

            paddw mm7, mm3
            //
            
            psrlw mm7, 8
            //

            pmullw mm7, mem7
            dec ecx

            // cycle
            // cycle
            // cycle

            paddw mm5, mm7

            psrlw mm5, 8

            packuswb mm5, mm5

            movd [edi-4], mm5

            
            jnz mmx_scale_loop1
            mov fbout, edi
          }
      } // end mmx
    __asm emms;
#endif
      if (blend) // reblend this scanline with the original
      {
        fbout-=w;
        int x=w/4;
        unsigned int *s2=(unsigned int *)framebuffer+y*w;
        while (x--)
        {
          fbout[0]=BLEND_AVG(fbout[0],s2[0]);
          fbout[1]=BLEND_AVG(fbout[1],s2[1]);
          fbout[2]=BLEND_AVG(fbout[2],s2[2]);
          fbout[3]=BLEND_AVG(fbout[3],s2[3]);
          fbout+=4;
          s2+=4;
        }
      }
    } // end subpixel y loop
  }
  else // !subpixel
  {
    int y;
	  for (y = 0; y < h; y ++)
	  {
		  int s_x=isx;
		  unsigned int *src = ((unsigned int *) framebuffer)+(s_y>>16)*w;
      int ypart=(s_y>>8)&0xff;
		  s_y+=ds_x;
		  if (!blend) 
		  {
    	  int x=w/4;
			  while (x--)
			  { 
				  fbout[0]=src[s_x>>16];
				  s_x += ds_x;
				  fbout[1]=src[s_x>>16];
				  s_x += ds_x;
				  fbout[2]=src[s_x>>16];
				  s_x += ds_x;
				  fbout[3]=src[s_x>>16];
				  s_x += ds_x;
				  fbout+=4;
			  }
		  }
		  else
		  {
			  unsigned int *s2=(unsigned int *)framebuffer+(y*w);
    	  int x=w/4;
			  while (x--)
			  { 
				  fbout[0]=BLEND_AVG(s2[0],src[s_x>>16]);
				  s_x += ds_x;
				  fbout[1]=BLEND_AVG(s2[1],src[s_x>>16]);
				  s_x += ds_x;
				  fbout[2]=BLEND_AVG(s2[2],src[s_x>>16]);
				  s_x += ds_x;
				  fbout[3]=BLEND_AVG(s2[3],src[s_x>>16]);
				  s2+=4;
				  fbout+=4;
				  s_x += ds_x;
			  }
 		  }
	  }
  }
  return 1;
}

	
int C_THISCLASS::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
{
  if (isBeat&0x80000000) return 0;
	int f_val;
	unsigned int *dest=(unsigned int *) fbout;

	if (isBeat && beatch) 
	{
		fpos=scale2;
	}
	
	if (scale < scale2) 
	{
		f_val=(fpos > scale? fpos:scale);
		fpos -= 3;
	}
	else 
	{
		f_val=(fpos < scale? fpos :scale);
		fpos+=3;
	}

	if (f_val<0) f_val=0;

  if (f_val < 32) return blitter_normal(framebuffer,fbout,w,h,f_val);
  if (f_val > 32) return blitter_out(framebuffer,fbout,w,h,f_val);
  return 0;
}

C_RBASE *R_BlitterFB(char *desc)
{
	if (desc) { strcpy(desc,MOD_NAME); return NULL; }
	return (C_RBASE *) new C_THISCLASS();
}

static C_THISCLASS *g_this;

static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
{
	int *a=NULL;
	switch (uMsg)
	{
		case WM_INITDIALOG:
			SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETRANGEMIN,0,0);
			SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETRANGEMAX,0,256);
			SendDlgItemMessage(hwndDlg,IDC_SLIDER1,TBM_SETPOS,1,g_this->scale);
			SendDlgItemMessage(hwndDlg,IDC_SLIDER2,TBM_SETRANGEMIN,0,0);
			SendDlgItemMessage(hwndDlg,IDC_SLIDER2,TBM_SETRANGEMAX,0,256);
			SendDlgItemMessage(hwndDlg,IDC_SLIDER2,TBM_SETPOS,1,g_this->scale2);
			if (g_this->blend==1) CheckDlgButton(hwndDlg,IDC_BLEND,BST_CHECKED);			
      if (g_this->subpixel) CheckDlgButton(hwndDlg,IDC_CHECK2,BST_CHECKED);
			if (g_this->beatch==1) CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
			return 1;
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_BLEND:
					if (IsDlgButtonChecked(hwndDlg,IDC_BLEND))
          {
						g_this->blend=1;
          }
					else g_this->blend=0;
				break;
				case IDC_CHECK1:
					if (IsDlgButtonChecked(hwndDlg,IDC_CHECK1))
						g_this->beatch=1;
					else g_this->beatch=0;
				break;
        case IDC_CHECK2:
					if (IsDlgButtonChecked(hwndDlg,IDC_CHECK2))
          {
						g_this->subpixel=1;
          }
					else g_this->subpixel=0;
        break;
			}
		return 0;

		case WM_HSCROLL:
			{
				HWND swnd = (HWND) lParam;
				int t = (int) SendMessage(swnd,TBM_GETPOS,0,0);
				if (swnd == GetDlgItem(hwndDlg,IDC_SLIDER1))
				{
					g_this->scale=t;
					g_this->fpos=t;
				}
				if (swnd == GetDlgItem(hwndDlg,IDC_SLIDER2))
				{
					g_this->scale2=t;
					g_this->fpos=t;
				}
			}
	}
	return 0;
}


HWND C_THISCLASS::conf(HINSTANCE hInstance, HWND hwndParent)
{
	g_this = this;
	return WASABI_API_CREATEDIALOG(IDD_CFG_BLT,hwndParent,g_DlgProc);
}

#else
C_RBASE *R_BlitterFB(char *desc) { return NULL; }
#endif