Arachnoid GLESv1.1 plugin. Compile and run (a bit glitchy and no frameskip) on the...
[mupen64plus-pandora.git] / source / mupen64plus-video-arachnoid / src / Combiner / CombinerStageCreator.cpp
1 /******************************************************************************
2  * Arachnoid Graphics Plugin for Mupen64Plus
3  * http://bitbucket.org/wahrhaft/mupen64plus-video-arachnoid/
4  *
5  * Copyright (C) 2007 Kristofer Karlsson, Rickard Niklasson
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  *****************************************************************************/
21
22 //*****************************************************************************
23 // 
24 // NOTE THAT THIS FILE IS BASED ON MATERIAL FROM glN64. 
25 // http://gln64.emulation64.com/
26 //
27 //*****************************************************************************
28
29 #include "CombinerStageCreator.h"
30
31 //-----------------------------------------------------------------------------
32 //* Set Stage
33 //! Function used to set combiner stage, tries to simplify and optimize as 
34 //! much as possible.
35 //! From glN64
36 //! @param[in] combineCycle Values to be added/subracted/multiplied and interpolated.
37 //! @param[out] stageOut Simplified combine cycle with combiner formula.
38 //-----------------------------------------------------------------------------
39 void setStage(CombineCycle* combineCycle, CombinerStage* stageOut)
40 {
41     // Load the first operand
42     stageOut->op[0].op = LOAD;
43     stageOut->op[0].param1 = combineCycle->loadValue;
44     stageOut->numOps = 1;
45
46     // If we're just subtracting zero, skip it
47     if (combineCycle->subValue != ZERO)
48     {
49         if (combineCycle->subValue == stageOut->op[0].param1)
50             stageOut->op[0].param1 = ZERO;
51         else
52         {
53             //Subract operation
54             stageOut->op[1].op = SUB;
55             stageOut->op[1].param1 = combineCycle->subValue;
56             stageOut->numOps++;
57         }
58     }
59
60     //Multiply operation
61     if ((stageOut->numOps > 1) || (stageOut->op[0].param1 != ZERO))
62     {
63         if (combineCycle->multValue == ZERO)
64         {
65             stageOut->numOps = 1;
66             stageOut->op[0].op = LOAD;
67             stageOut->op[0].param1 = ZERO;
68         }
69         else
70         {
71             if ( stageOut->numOps == 1 && stageOut->op[0].param1 == ONE )
72             {
73                 //LOAD
74                 stageOut->op[0].param1 = combineCycle->multValue;
75             }
76             else
77             {
78                 //MULT
79                 stageOut->op[stageOut->numOps].op = MUL;
80                 stageOut->op[stageOut->numOps].param1 = combineCycle->multValue;
81                 stageOut->numOps++;
82             }
83         }
84     }
85
86     //Don't bother adding zero
87     if (combineCycle->addValue != ZERO)
88     {
89             // If all we have so far is zero, then load this instead
90         if ((stageOut->numOps == 1) && (stageOut->op[0].param1 == ZERO))
91         {
92             stageOut->op[0].param1 = combineCycle->addValue;
93         }
94         else
95         {
96             stageOut->op[stageOut->numOps].op = ADD;
97             stageOut->op[stageOut->numOps].param1 = combineCycle->addValue;
98             stageOut->numOps++;
99         }
100     }
101
102     // Handle interpolation
103     if ((stageOut->numOps == 4) && (stageOut->op[1].param1 == stageOut->op[3].param1))
104     {
105         stageOut->numOps = 1;
106         stageOut->op[0].op = INTERPOLATE;
107         stageOut->op[0].param2 = stageOut->op[1].param1;
108         stageOut->op[0].param3 = stageOut->op[2].param1;
109     }
110 }