GLES2N64 (from mupen64plus-ae) plugin. Compile and run on the OpenPandora
[mupen64plus-pandora.git] / source / gles2n64 / src / VI.cpp
1 #include "Common.h"
2 #include "gles2N64.h"
3 #include "Types.h"
4 #include "VI.h"
5 #include "OpenGL.h"
6 #include "N64.h"
7 #include "gSP.h"
8 #include "gDP.h"
9 #include "RSP.h"
10 #include "Debug.h"
11 #include "Config.h"
12
13 VIInfo VI;
14
15 void VI_UpdateSize()
16 {
17
18     if (!config.video.force)
19     {
20         f32 xScale = _FIXED2FLOAT( _SHIFTR( *REG.VI_X_SCALE, 0, 12 ), 10 );
21         f32 xOffset = _FIXED2FLOAT( _SHIFTR( *REG.VI_X_SCALE, 16, 12 ), 10 );
22
23         f32 yScale = _FIXED2FLOAT( _SHIFTR( *REG.VI_Y_SCALE, 0, 12 ), 10 );
24         f32 yOffset = _FIXED2FLOAT( _SHIFTR( *REG.VI_Y_SCALE, 16, 12 ), 10 );
25
26         u32 hEnd = _SHIFTR( *REG.VI_H_START, 0, 10 );
27         u32 hStart = _SHIFTR( *REG.VI_H_START, 16, 10 );
28
29         // These are in half-lines, so shift an extra bit
30         u32 vEnd = _SHIFTR( *REG.VI_V_START, 1, 9 );
31         u32 vStart = _SHIFTR( *REG.VI_V_START, 17, 9 );
32
33         //Glide does this:
34         if (hEnd == hStart) hEnd = (u32)(*REG.VI_WIDTH / xScale);
35
36
37         VI.width = (hEnd - hStart) * xScale;
38         VI.height = (vEnd - vStart) * yScale * 1.0126582f;
39     }
40     else
41     {
42         VI.width = config.video.width;
43         VI.height = config.video.height;
44     }
45
46     if (VI.width == 0.0f) VI.width = 320.0f;
47     if (VI.height == 0.0f) VI.height = 240.0f;
48     VI.rwidth = 1.0f / VI.width;
49     VI.rheight = 1.0f / VI.height;
50
51
52     //add display buffer if doesn't exist
53     if (config.ignoreOffscreenRendering)
54     {
55         int i;
56         //int start = *REG.VI_ORIGIN;
57         u32 start = RSP_SegmentToPhysical(*REG.VI_ORIGIN) & 0x00FFFFFF;
58         u32 end = min(start + VI.width * VI.height * 4, RDRAMSize);
59         for(i = 0; i < VI.displayNum; i++)
60         {
61             if (VI.display[i].start <= end && VI.display[i].start >= start) break;
62             if (start <= VI.display[i].end && start >= VI.display[i].start) break;
63         }
64         if (i == VI.displayNum)
65         {
66             //printf("VI IMAGE=%i\n", o);
67             VI.display[i%16].start = start;
68             VI.display[i%16].end = end;
69             VI.displayNum = (VI.displayNum < 16) ? (VI.displayNum+1) : 16;
70         }
71     }
72
73 }
74
75 void VI_UpdateScreen()
76 {
77
78     switch(config.updateMode)
79     {
80
81         case SCREEN_UPDATE_AT_VI_CHANGE:
82             if (*REG.VI_ORIGIN != VI.lastOrigin)
83             {
84                 if (*REG.VI_ORIGIN < VI.lastOrigin || *REG.VI_ORIGIN > VI.lastOrigin+0x2000  )
85                     OGL_SwapBuffers();
86
87                 VI.lastOrigin = *REG.VI_ORIGIN;
88             }
89             break;
90
91         case SCREEN_UPDATE_AT_VI_UPDATE:
92             if (gSP.changed & CHANGED_COLORBUFFER)
93             {
94                 OGL_SwapBuffers();
95                 gSP.changed &= ~CHANGED_COLORBUFFER;
96             }
97             break;
98     }
99
100 }
101