1 /*
2 Copyright (C) 2003 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 
20 #ifndef _RICE_RENDER_H
21 #define _RICE_RENDER_H
22 
23 #include "Blender.h"
24 #include "Combiner.h"
25 #include "Config.h"
26 #include "Debugger.h"
27 #include "RenderBase.h"
28 #include "RSP_Parser.h"
29 #include "RSP_S2DEX.h"
30 
31 enum TextureChannel
32 {
33     TXT_RGB,
34     TXT_ALPHA,
35     TXT_RGBA,
36 };
37 
38 class CRender
39 {
40 protected:
41     CRender();
42 
43     TextureUVFlag TileUFlags[8];
44     TextureUVFlag TileVFlags[8];
45 
46 public:
47 
48     float m_fScreenViewportMultX;
49     float m_fScreenViewportMultY;
50 
51 
52     uint32_t  m_dwTexturePerspective;
53     bool    m_bAlphaTestEnable;
54 
55     bool    m_bZUpdate;
56     bool    m_bZCompare;
57     uint32_t  m_dwZBias;
58 
59     TextureFilter   m_dwMinFilter;
60     TextureFilter   m_dwMagFilter;
61 
62     uint32_t  m_dwAlpha;
63 
64     uint64_t      m_Mux;
65     bool    m_bBlendModeValid;
66 
67     CColorCombiner *m_pColorCombiner;
68     CBlender *m_pAlphaBlender;
69 
70 
71     virtual ~CRender();
72 
IsTexel0Enable()73     inline bool IsTexel0Enable() {return m_pColorCombiner->m_bTex0Enabled;}
IsTexel1Enable()74     inline bool IsTexel1Enable() {return m_pColorCombiner->m_bTex1Enabled;}
IsTextureEnabled()75     inline bool IsTextureEnabled() { return (m_pColorCombiner->m_bTex0Enabled||m_pColorCombiner->m_bTex1Enabled); }
76 
GetCurrentTexture()77     inline RenderTexture& GetCurrentTexture() { return g_textures[gRSP.curTile]; }
GetTexture(uint32_t dwTile)78     inline RenderTexture& GetTexture(uint32_t dwTile) { return g_textures[dwTile]; }
79     void SetViewport(int nLeft, int nTop, int nRight, int nBottom, int maxZ);
SetViewportRender()80     virtual void SetViewportRender() {}
81     virtual void SetClipRatio(uint32_t type, uint32_t value);
UpdateScissor()82     virtual void UpdateScissor() {}
ApplyRDPScissor(bool force)83     virtual void ApplyRDPScissor(bool force) {}
84     virtual void UpdateClipRectangle();
85     virtual void UpdateScissorWithClipRatio();
ApplyScissorWithClipRatio(bool force)86     virtual void ApplyScissorWithClipRatio(bool force) {}
87 
88     void SetTextureEnableAndScale(int dwTile, bool enable, float fScaleX, float fScaleY);
89 
SetFogEnable(bool bEnable)90     virtual void SetFogEnable(bool bEnable)
91     {
92         gRSP.bFogEnabled = bEnable;
93     }
94     virtual void SetFogMinMax(float fMin, float fMax) = 0;
95     virtual void TurnFogOnOff(bool flag)=0;
96 
SetFogColor(uint32_t r,uint32_t g,uint32_t b,uint32_t a)97     virtual void SetFogColor(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
98     {
99         gRDP.fogColor = COLOR_RGBA(r, g, b, a);
100     }
GetFogColor()101     uint32_t GetFogColor() { return gRDP.fogColor; }
102 
103     void SetProjection(const Matrix & mat, bool bPush, bool bReplace);
104     void SetWorldView(const Matrix & mat, bool bPush, bool bReplace);
GetProjectMatrixLevel(void)105     inline int GetProjectMatrixLevel(void) { return gRSP.projectionMtxTop; }
GetWorldViewMatrixLevel(void)106     inline int GetWorldViewMatrixLevel(void) { return gRSP.modelViewMtxTop; }
107 
PopProjection()108     inline void PopProjection()
109     {
110         if (gRSP.projectionMtxTop > 0)
111             gRSP.projectionMtxTop--;
112         else
113             TRACE0("Popping past projection stack limits");
114     }
115 
116     void PopWorldView();
117     Matrix & GetWorldProjectMatrix(void);
118     void SetWorldProjectMatrix(Matrix &mtx);
119 
120     void ResetMatrices();
121 
GetShadeMode()122     inline RenderShadeMode GetShadeMode() { return gRSP.shadeMode; }
123 
SetVtxTextureCoord(uint32_t dwV,float tu,float tv)124     void SetVtxTextureCoord(uint32_t dwV, float tu, float tv)
125     {
126         g_fVtxTxtCoords[dwV].x = tu;
127         g_fVtxTxtCoords[dwV].y = tv;
128     }
129 
130     virtual void RenderReset();
131     virtual void SetCombinerAndBlender();
132     virtual void SetMux(uint32_t dwMux0, uint32_t dwMux1);
SetCullMode(bool bCullFront,bool bCullBack)133     virtual void SetCullMode(bool bCullFront, bool bCullBack) { gRSP.bCullFront = bCullFront; gRSP.bCullBack = bCullBack; }
134 
BeginRendering(void)135     virtual void BeginRendering(void) {CRender::gRenderReferenceCount++;}       // For DirectX only
EndRendering(void)136     virtual void EndRendering(void)
137     {
138         if( CRender::gRenderReferenceCount > 0 )
139             CRender::gRenderReferenceCount--;
140     }
141 
142     virtual void ClearBuffer(bool cbuffer, bool zbuffer)=0;
143     virtual void ClearZBuffer(float depth)=0;
ClearBuffer(bool cbuffer,bool zbuffer,COORDRECT & rect)144     virtual void ClearBuffer(bool cbuffer, bool zbuffer, COORDRECT &rect)
145     {
146         ClearBuffer(cbuffer, zbuffer);
147     }
148     virtual void ZBufferEnable(bool bZBuffer)=0;
149     virtual void SetZCompare(bool bZCompare)=0;
150     virtual void SetZUpdate(bool bZUpdate)=0;
151     virtual void SetZBias(int bias)=0;
152     virtual void SetAlphaTestEnable(bool bAlphaTestEnable)=0;
153 
154     void SetTextureFilter(uint32_t dwFilter);
ApplyTextureFilter()155     virtual void ApplyTextureFilter() {}
156 
157     virtual void SetShadeMode(RenderShadeMode mode)=0;
158 
159     virtual void SetAlphaRef(uint32_t dwAlpha)=0;
160     virtual void ForceAlphaRef(uint32_t dwAlpha)=0;
161 
162     virtual void InitOtherModes(void);
163 
164     void SetVertexTextureUVCoord(TLITVERTEX &v, float fTex0S, float fTex0T, float fTex1S, float fTex1T);
165     void SetVertexTextureUVCoord(TLITVERTEX &v, float fTex0S, float fTex0T);
166     void SetVertexTextureUVCoord(TLITVERTEX &v, const TexCord &fTex0, const TexCord &fTex1);
167     void SetVertexTextureUVCoord(TLITVERTEX &v, const TexCord &fTex0);
168     virtual COLOR PostProcessDiffuseColor(COLOR curDiffuseColor)=0;
169     virtual COLOR PostProcessSpecularColor()=0;
170 
171     bool DrawTriangles();
172     virtual bool RenderFlushTris()=0;
173 
174     bool TexRect(int nX0, int nY0, int nX1, int nY1, float fS0, float fT0, float fScaleS, float fScaleT, bool colorFlag, uint32_t difcolor);
175     bool TexRectFlip(int nX0, int nY0, int nX1, int nY1, float fS0, float fT0, float fS1, float fT1);
176     bool FillRect(int nX0, int nY0, int nX1, int nY1, uint32_t dwColor);
177     bool Line3D(uint32_t dwV0, uint32_t dwV1, uint32_t dwWidth);
178 
179     virtual void SetAddressUAllStages(uint32_t dwTile, TextureUVFlag dwFlag); // For DirectX only, fix me
180     virtual void SetAddressVAllStages(uint32_t dwTile, TextureUVFlag dwFlag); // For DirectX only, fix me
181     virtual void SetTextureUFlag(TextureUVFlag dwFlag, uint32_t tile)=0;
182     virtual void SetTextureVFlag(TextureUVFlag dwFlag, uint32_t tile)=0;
183     virtual void SetTexelRepeatFlags(uint32_t dwTile);
184     virtual void SetAllTexelRepeatFlag();
185 
186     virtual bool SetCurrentTexture(int tile, TxtrCacheEntry *pTextureEntry)=0;
187     virtual bool SetCurrentTexture(int tile, CTexture *handler, uint32_t dwTileWidth, uint32_t dwTileHeight, TxtrCacheEntry *pTextureEntry) = 0;
188 
189     virtual bool InitDeviceObjects()=0;
190     virtual bool ClearDeviceObjects()=0;
191     virtual void Initialize(void);
192     virtual void CleanUp(void);
193 
194     virtual void SetFillMode(FillMode mode)=0;
195 
196     void LoadSprite2D(Sprite2DInfo &info, uint32_t ucode);
197     void LoadObjBGCopy(uObjBg &info);
198     void LoadObjBG1CYC(uObjScaleBg &info);
199     void LoadObjSprite(uObjTxSprite &info, bool useTIAddr);
200 
201     void LoadFrameBuffer(bool useVIreg, uint32_t left, uint32_t top, uint32_t width, uint32_t height);
202     void LoadTextureFromMemory(void *buf, uint32_t left, uint32_t top, uint32_t width, uint32_t height, uint32_t pitch, uint32_t format);
203     void LoadTxtrBufIntoTexture(void);
204     void DrawSprite2D(Sprite2DInfo &info, uint32_t ucode);
205     void DrawSpriteR(uObjTxSprite &sprite, bool initCombiner, uint32_t tile, uint32_t left, uint32_t top, uint32_t width, uint32_t height);
206     void DrawSprite(uObjTxSprite &sprite, bool rectR);
207     void DrawObjBGCopy(uObjBg &info);
DrawSpriteR_Render()208     virtual void DrawSpriteR_Render(){};
209     virtual void DrawSimple2DTexture(float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, COLOR dif, COLOR spe, float z, float rhw)=0;
210     void DrawFrameBuffer(bool useVIreg, uint32_t left, uint32_t top, uint32_t width, uint32_t height);
211     void DrawObjBG1CYC(uObjScaleBg &bg, bool scaled);
212 
213     static CRender * g_pRender;
214     static int gRenderReferenceCount;
215     static CRender * GetRender(void);
216     static bool IsAvailable();
217 
218 
219 protected:
220     bool            m_savedZBufferFlag;
221     uint32_t          m_savedMinFilter;
222     uint32_t          m_savedMagFilter;
223 
224     // FillRect
225     virtual bool    RenderFillRect(uint32_t dwColor, float depth)=0;
226     VECTOR2         m_fillRectVtx[2];
227 
228     // Line3D
229     virtual bool    RenderLine3D()=0;
230 
231     LITVERTEX       m_line3DVtx[2];
232     VECTOR2         m_line3DVector[4];
233 
234     // TexRect
235     virtual bool    RenderTexRect()=0;
236 
237     TexCord         m_texRectTex1UV[2];
238     TexCord         m_texRectTex2UV[2];
239 
240     // DrawSimple2DTexture
241     virtual void    StartDrawSimple2DTexture(float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1, COLOR dif, COLOR spe, float z, float rhw);
242 
243     // DrawSimpleRect
244     virtual void    StartDrawSimpleRect(int nX0, int nY0, int nX1, int nY1, uint32_t dwColor, float depth, float rhw);
245     VECTOR2         m_simpleRectVtx[2];
246 
247     bool            RemapTextureCoordinate(float s0, float s1, uint32_t tileWidth, uint32_t mask, float textureWidth,
248                                             float &u0, float &u1);
249 
250 };
251 
252 #define ffloor(a) (((int(a))<=(a))?(float)(int(a)):((float)(int(a))-1))
253 
254 #endif  //_RICE_RENDER_H
255 
256