00001 /* Copyright (C) 2001 Sony Computer Entertainment America 00002 All Rights Reserved 00003 SCEA Confidential */ 00004 00005 #ifndef ps2gl_context_h 00006 #define ps2gl_context_h 00007 00008 /******************************************** 00009 * includes 00010 */ 00011 00012 #include "ps2s/gsmem.h" 00013 #include "ps2s/packet.h" 00014 00015 #include "GL/gl.h" 00016 00017 /******************************************** 00018 * state change flags 00019 */ 00020 00021 namespace Vu1CtxtFlags { 00022 static const int NumLights = 1 << 0; 00023 static const int LightPropChanged = 1 << 1; 00024 static const int GlobalAmb = 1 << 9; 00025 static const int CurMaterial = 1 << 10; 00026 static const int Xform = 1 << 11; 00027 static const int Prim = 1 << 12; 00028 static const int Shading = 1 << 13; 00029 static const int TexEnabled = 1 << 14; 00030 static const int LightingEnabled = 1 << 15; 00031 static const int AlphaBlending = 1 << 16; 00032 static const int CullFaceDir = 1 << 17; 00033 } 00034 00035 namespace GsCtxtFlags { 00036 static const int Texture = 1; 00037 static const int DrawEnv = Texture * 2; 00038 } 00039 00040 namespace RendererPropFlags { 00041 static const int NumLights = 1; 00042 static const int TexEnabled = NumLights * 2; 00043 static const int LightingEnabled = TexEnabled * 2; 00044 static const int SpecularEnabled = LightingEnabled * 2; 00045 static const int PerVtxMaterial = SpecularEnabled * 2; 00046 static const int CullFaceEnabled = PerVtxMaterial * 2; 00047 static const int Prim = CullFaceEnabled * 2; 00048 } 00049 00050 /******************************************** 00051 * CGLContext 00052 */ 00053 00054 class CImmMatrixStack; 00055 class CDListMatrixStack; 00056 class CMatrixStack; 00057 00058 class CImmLighting; 00059 class CDListLighting; 00060 class CLighting; 00061 00062 class CImmGeomManager; 00063 class CDListGeomManager; 00064 class CGeomManager; 00065 00066 class CMaterialManager; 00067 class CDListManager; 00068 class CTexManager; 00069 00070 class CImmDrawContext; 00071 class CDListDrawContext; 00072 class CDrawContext; 00073 00074 class CDisplayContext; 00075 00076 class CGLContext { 00077 CImmMatrixStack *ProjectionMatStack, *ModelViewMatStack; 00078 CDListMatrixStack *DListMatStack; 00079 CMatrixStack *CurMatrixStack, *SavedCurMatStack; 00080 00081 CImmLighting *ImmLighting; 00082 CDListLighting *DListLighting; 00083 CLighting *CurLighting; 00084 00085 CImmGeomManager *ImmGManager; 00086 CDListGeomManager *DListGManager; 00087 CGeomManager *CurGManager; 00088 00089 CMaterialManager *MaterialManager; 00090 CDListManager *DListManager; 00091 CTexManager *TexManager; 00092 00093 CImmDrawContext *ImmDrawContext; 00094 CDListDrawContext *DListDrawContext; 00095 CDrawContext *CurDrawContext; 00096 00097 CDisplayContext *DisplayContext; 00098 00099 // state changes 00100 00101 tU32 Vu1ContextChanged, SavedVu1ContextChanges; 00102 tU32 GsContextChanged, SavedGsContextChanges; 00103 tU32 RendererPropsChanged, SavedRendererPropsChanges; 00104 bool StateChangesArePushed; 00105 00106 inline void PushStateChanges() { 00107 mErrorIf( StateChangesArePushed, "Trying to push state changes when already pushed." ); 00108 SavedVu1ContextChanges = Vu1ContextChanged; 00109 SavedGsContextChanges = GsContextChanged; 00110 SavedRendererPropsChanges = RendererPropsChanged; 00111 StateChangesArePushed = true; 00112 } 00113 inline void PopStateChanges() { 00114 mErrorIf( ! StateChangesArePushed, 00115 "Trying to pop state changes that haven't been pushed." ); 00116 Vu1ContextChanged = SavedVu1ContextChanges; 00117 GsContextChanged = SavedGsContextChanges; 00118 RendererPropsChanged = SavedRendererPropsChanges; 00119 StateChangesArePushed = false; 00120 } 00121 00122 // rendering loop management 00123 00124 bool IsCurrentFieldEven; 00125 unsigned int CurrentFrameNumber; 00126 00127 // double-buffered dma packets for rendering use 00128 static const int kDmaPacketMaxQwordLength = 65000; 00129 static CVifSCDmaPacket *CurPacket, *LastPacket; 00130 00131 public: 00132 CGLContext( int immBufferQwordSize ); 00133 ~CGLContext(); 00134 00135 void SetMatrixMode( GLenum mode ); 00136 inline CMatrixStack& GetCurMatrixStack() { return *CurMatrixStack; } 00137 inline CImmMatrixStack& GetModelViewStack() { return *ModelViewMatStack; } 00138 inline CImmMatrixStack& GetProjectionStack() { return *ProjectionMatStack; } 00139 00140 inline CLighting& GetLighting() { return *CurLighting; } 00141 inline CImmLighting& GetImmLighting() { return *ImmLighting; } 00142 inline CDListLighting& GetDListLighting() { return *DListLighting; } 00143 00144 inline CGeomManager& GetGeomManager() { return *CurGManager; } 00145 inline CImmGeomManager& GetImmGeomManager() { return *ImmGManager; } 00146 inline CDListGeomManager& GetDListGeomManager() { return *DListGManager; } 00147 00148 inline CMaterialManager& GetMaterialManager() { return *MaterialManager; } 00149 00150 inline CDListManager& GetDListManager() { return *DListManager; } 00151 00152 inline CTexManager& GetTexManager() { return *TexManager; } 00153 00154 inline CDrawContext& GetDrawContext() { return *CurDrawContext; } 00155 inline CImmDrawContext& GetImmDrawContext() { return *ImmDrawContext; } 00156 inline CDListDrawContext& GetDListDrawContext() { return *DListDrawContext; } 00157 00158 inline CDisplayContext& GetDisplayContext() { return *DisplayContext; } 00159 00160 inline bool InDListDef() const { return CurGManager != (CGeomManager*)ImmGManager; } 00161 void BeginDListDef( unsigned int listID, GLenum mode ); 00162 void EndDListDef(); 00163 00164 // state updates 00165 00166 inline void NumLightsChanged() { 00167 Vu1ContextChanged |= Vu1CtxtFlags::NumLights; 00168 RendererPropsChanged |= RendererPropFlags::NumLights; 00169 } 00170 inline void LightPropChanged() { 00171 Vu1ContextChanged |= Vu1CtxtFlags::LightPropChanged; 00172 } 00173 inline void GlobalAmbChanged() { 00174 Vu1ContextChanged |= Vu1CtxtFlags::GlobalAmb; 00175 } 00176 inline void CurMaterialChanged() { 00177 Vu1ContextChanged |= Vu1CtxtFlags::CurMaterial; 00178 } 00179 inline void XformChanged() { 00180 Vu1ContextChanged |= Vu1CtxtFlags::Xform; 00181 } 00182 inline void PrimChanged() { 00183 Vu1ContextChanged |= Vu1CtxtFlags::Prim; 00184 RendererPropsChanged |= RendererPropFlags::Prim; 00185 } 00186 inline void ShadingChanged() { 00187 Vu1ContextChanged |= Vu1CtxtFlags::Shading; 00188 } 00189 inline void TexEnabledChanged() { 00190 Vu1ContextChanged |= Vu1CtxtFlags::TexEnabled; 00191 RendererPropsChanged |= RendererPropFlags::TexEnabled; 00192 GsContextChanged |= GsCtxtFlags::Texture; 00193 } 00194 inline void LightingEnabledChanged() { 00195 Vu1ContextChanged |= Vu1CtxtFlags::LightingEnabled; 00196 RendererPropsChanged |= RendererPropFlags::LightingEnabled; 00197 } 00198 inline void BlendEnabledChanged() { 00199 Vu1ContextChanged |= Vu1CtxtFlags::AlphaBlending; 00200 } 00201 inline void AlphaTestEnabledChanged() { 00202 GsContextChanged |= GsCtxtFlags::DrawEnv; 00203 } 00204 inline void AlphaTestFuncChanged() { 00205 GsContextChanged |= GsCtxtFlags::DrawEnv; 00206 } 00207 inline void DepthWriteEnabledChanged() { 00208 GsContextChanged |= GsCtxtFlags::DrawEnv; 00209 } 00210 inline void SpecularEnabledChanged() { 00211 RendererPropsChanged |= RendererPropFlags::SpecularEnabled; 00212 } 00213 inline void TextureChanged() { 00214 GsContextChanged |= GsCtxtFlags::Texture; 00215 } 00216 inline void BlendModeChanged() { 00217 GsContextChanged |= GsCtxtFlags::DrawEnv; 00218 } 00219 inline void DrawBufferChanged() { 00220 GsContextChanged |= GsCtxtFlags::DrawEnv; 00221 } 00222 inline void PerVtxMaterialChanged() { 00223 RendererPropsChanged |= RendererPropFlags::PerVtxMaterial; 00224 } 00225 inline void CullFaceEnabledChanged() { 00226 RendererPropsChanged |= RendererPropFlags::CullFaceEnabled; 00227 } 00228 inline void CullFaceDirChanged() { 00229 Vu1ContextChanged |= Vu1CtxtFlags::CullFaceDir; 00230 } 00231 00232 // ps2 rendering 00233 00234 inline tU32 GetVu1ContextChanged() const { return Vu1ContextChanged; } 00235 inline void SetVu1ContextChanged( bool changed ) { 00236 Vu1ContextChanged = (changed) ? 0xff : 0; 00237 } 00238 00239 inline tU32 GetGsContextChanged() const { return GsContextChanged; } 00240 inline void SetGsContextChanged( bool changed ) { 00241 GsContextChanged = (changed) ? 0xff : 0; 00242 } 00243 00244 inline tU32 GetGeomManagerPropsChanged() const { return RendererPropsChanged; } 00245 inline void SetRendererPropsChanged( bool changed ) { 00246 RendererPropsChanged = (changed) ? 0xff : 0; 00247 } 00248 00249 inline CVifSCDmaPacket& GetVif1Packet() { return *CurPacket; } 00250 00251 void WaitForVSync(); 00252 void SwapBuffers(); 00253 }; 00254 00255 // global pointer to the GLContext 00256 extern CGLContext *pGLContext; 00257 00258 #endif // ps2gl_context_h