1 /*
2 Copyright (C) 2002 Rice1964
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 
19 #ifndef _DECODEDMUX_H_
20 #define _DECODEDMUX_H_
21 
22 #include <string.h>
23 #include <stdio.h>
24 
25 #include "typedefs.h"
26 #include "CombinerDefs.h"
27 
28 #define CM_IGNORE 0
29 #define CM_IGNORE_BYTE 0xFF
30 
31 typedef enum
32 {
33     N64Cycle0RGB   = 0,
34     N64Cycle0Alpha = 1,
35     N64Cycle1RGB   = 2,
36     N64Cycle1Alpha = 3,
37 } N64StageNumberType;
38 
39 typedef union
40 {
41     struct
42     {
43         uint32_t dwMux0;
44         uint32_t dwMux1;
45     };
46     uint64_t Mux64;
47 } MuxType;
48 
49 typedef struct
50 {
51     MuxType ori_mux;
52     MuxType simple_mux;
53 } SimpleMuxMapType;
54 
55 class DecodedMux
56 {
57 public:
58     union
59     {
60         struct
61         {
62             uint8_t aRGB0;
63             uint8_t bRGB0;
64             uint8_t cRGB0;
65             uint8_t dRGB0;
66 
67             uint8_t aA0;
68             uint8_t bA0;
69             uint8_t cA0;
70             uint8_t dA0;
71 
72             uint8_t aRGB1;
73             uint8_t bRGB1;
74             uint8_t cRGB1;
75             uint8_t dRGB1;
76 
77             uint8_t aA1;
78             uint8_t bA1;
79             uint8_t cA1;
80             uint8_t dA1;
81         };
82         uint8_t  m_bytes[16];
83         uint32_t m_dWords[4];
84         N64CombinerType m_n64Combiners[4];
85     };
86 
87     union
88     {
89         struct
90         {
91             uint32_t m_dwMux0;
92             uint32_t m_dwMux1;
93         };
94         uint64_t m_u64Mux;
95     };
96 
97     CombinerFormatType splitType[4];
98     CombinerFormatType mType;
99 
100     uint32_t m_dwShadeColorChannelFlag;
101     uint32_t m_dwShadeAlphaChannelFlag;
102     uint32_t m_ColorTextureFlag[2];   // I may use a texture to represent a constant color
103                                     // when there are more constant colors are used than
104                                     // the system can support
105 
106     bool m_bShadeIsUsed[2];     // 0 for color channel, 1 for alpha channel
107     bool m_bTexel0IsUsed;
108     bool m_bTexel1IsUsed;
109 
110     int  m_maxConstants;    // OpenGL 1.1 does not really support a constant color in combiner
111                             // must use shade for constants;
112     int  m_maxTextures;     // 1 or 2
113 
114 
115     void Decode(uint32_t dwMux0, uint32_t dwMux1);
116     virtual void Hack(void);
117     bool IsUsed(uint8_t fac, uint8_t mask);
118     bool IsUsedInAlphaChannel(uint8_t fac, uint8_t mask);
119     bool IsUsedInColorChannel(uint8_t fac, uint8_t mask);
120     bool IsUsedInCycle(uint8_t fac, int cycle, CombineChannel channel, uint8_t mask);
121     bool IsUsedInCycle(uint8_t fac, int cycle, uint8_t mask);
122     uint32_t GetCycle(int cycle, CombineChannel channel);
123     uint32_t GetCycle(int cycle);
124     CombinerFormatType GetCombinerFormatType(uint32_t cycle);
125     void Display(bool simplified, FILE *fp);
126     static char* FormatStr(uint8_t val, char *buf);
127     void CheckCombineInCycle1(void);
128     virtual void Simplify(void);
129     virtual void Reformat(bool do_complement);
130     virtual void To_AB_Add_CD_Format(void); // Use by TNT,Geforce
131     virtual void To_AB_Add_C_Format(void);  // Use by ATI Radeon
132 
133     virtual void MergeShadeWithConstants(void);
134     virtual void MergeShadeWithConstantsInChannel(CombineChannel channel);
135     virtual void MergeConstants(void);
136     virtual void UseShadeForConstant(void);
137     virtual void UseTextureForConstant(void);
138 
139     void ConvertComplements();
140     int HowManyConstFactors();
141     int HowManyTextures();
142     void MergeConstFactors();
143     virtual void SplitComplexStages();  // Only used if the combiner supports more than 1 stages
144     void ConvertLODFracTo0();
145     void ReplaceVal(uint8_t val1, uint8_t val2, int cycle, uint8_t mask);
Replace1Val(uint8_t & val1,const uint8_t val2,uint8_t mask)146     void Replace1Val(uint8_t &val1, const uint8_t val2, uint8_t mask)
147     {
148         val1 &= (~mask);
149         val1 |= val2;
150     }
151     int CountTexels(void);
152     int Count(uint8_t val, int cycle, uint8_t mask);
153 
154 #ifdef DEBUGGER
155     void DisplayMuxString(const char *prompt);
156     void DisplaySimpliedMuxString(const char *prompt);
157     void DisplayConstantsWithShade(uint32_t flag,CombineChannel channel);
158 #else
DisplayMuxString(const char * prompt)159     void DisplayMuxString(const char *prompt) {}
DisplaySimpliedMuxString(const char * prompt)160     void DisplaySimpliedMuxString(const char *prompt){}
DisplayConstantsWithShade(uint32_t flag,CombineChannel channel)161     void DisplayConstantsWithShade(uint32_t flag,CombineChannel channel){}
162     void LogMuxString(const char *prompt, FILE *fp);
163     void LogSimpliedMuxString(const char *prompt, FILE *fp);
164     void LogConstantsWithShade(uint32_t flag,CombineChannel channel, FILE *fp);
165 #endif
166 
167     virtual DecodedMux& operator=(const DecodedMux& mux)
168     {
169         m_dWords[0] = mux.m_dWords[0];
170         m_dWords[1] = mux.m_dWords[1];
171         m_dWords[2] = mux.m_dWords[2];
172         m_dWords[3] = mux.m_dWords[3];
173         m_u64Mux = mux.m_u64Mux;
174         splitType[0] = mux.splitType[0];
175         splitType[1] = mux.splitType[1];
176         splitType[2] = mux.splitType[2];
177         splitType[3] = mux.splitType[3];
178         mType = mux.mType;
179 
180         m_dwShadeColorChannelFlag = mux.m_dwShadeColorChannelFlag;
181         m_dwShadeAlphaChannelFlag = mux.m_dwShadeAlphaChannelFlag;
182 
183         m_bShadeIsUsed[0] = mux.m_bShadeIsUsed[0];
184         m_bShadeIsUsed[1] = mux.m_bShadeIsUsed[1];
185         m_bTexel0IsUsed = mux.m_bTexel0IsUsed;
186         m_bTexel1IsUsed = mux.m_bTexel1IsUsed;
187 
188         m_maxConstants = mux.m_maxConstants;
189         m_maxTextures = mux.m_maxTextures;
190         m_ColorTextureFlag[0] = mux.m_ColorTextureFlag[0];
191         m_ColorTextureFlag[1] = mux.m_ColorTextureFlag[1];
192 
193         return *this;
194     }
195 
IsConstFactor(uint8_t val)196     static inline bool IsConstFactor(uint8_t val)
197     {
198         uint8_t v = val&MUX_MASK;
199         return( v == MUX_0 || v == MUX_1 || v == MUX_PRIM || v == MUX_ENV || v == MUX_LODFRAC || v == MUX_PRIMLODFRAC );
200     }
201 
DecodedMux()202     DecodedMux()
203     {
204         memset(m_bytes, 0, sizeof(m_bytes));
205         mType=CM_FMT_TYPE_NOT_CHECKED;
206         for( int i=0; i<4; i++ )
207         {
208             splitType[i] = CM_FMT_TYPE_NOT_CHECKED;
209         }
210         m_maxConstants = 1;
211         m_maxTextures = 2;
212     }
213 
~DecodedMux()214     virtual ~DecodedMux() {}
215 };
216 
217 class DecodedMuxForPixelShader : public DecodedMux
218 {
219 public:
220     virtual void Simplify(void);
SplitComplexStages()221     void SplitComplexStages() {};
222 };
223 
224 class DecodedMuxForSemiPixelShader : public DecodedMux
225 {
226 public:
227     void Reset(void);
228 };
229 
230 class DecodedMuxForOGL14V2 : public DecodedMuxForPixelShader
231 {
232 public:
233     virtual void Simplify(void);
234     void UseTextureForConstant(void);
235 };
236 
237 typedef struct
238 {
239     bool bFurtherFormatForOGL2;
240     bool bUseShadeForConstants;
241     bool bUseTextureForConstants;
242     bool bUseMoreThan2TextureForConstants;
243     bool bReformatToAB_CD;
244     bool bAllowHack;
245     bool bAllowComplimentary;
246     bool bCheckCombineInCycle1;
247     bool bSetLODFracTo0;
248     bool bMergeShadeWithConstants;
249     bool bSplitComplexStage;
250     bool bReformatAgainWithTwoTexels;
251 } MuxConverterOptions;
252 
253 #endif
254 
255 
256