00001
00002
00003 This file is subject to the terms and conditions of the GNU Lesser
00004 General Public License Version 2.1. See the file "COPYING" in the
00005 main directory of this archive for more details. */
00006
00007 #ifndef ps2gl_context_h
00008 #define ps2gl_context_h
00009
00010
00011 * includes
00012 */
00013
00014 #include "ps2s/packet.h"
00015 #include "ps2s/gsmem.h"
00016
00017 #include "GL/gl.h"
00018
00019
00020 * state change flags
00021 */
00022
00023 namespace RendererCtxtFlags {
00024 static const int NumLights = 1 << 0;
00025 static const int LightPropChanged = 1 << 1;
00026 static const int GlobalAmb = 1 << 9;
00027 static const int CurMaterial = 1 << 10;
00028 static const int Xform = 1 << 11;
00029 static const int Prim = 1 << 12;
00030 static const int Shading = 1 << 13;
00031 static const int TexEnabled = 1 << 14;
00032 static const int LightingEnabled = 1 << 15;
00033 static const int AlphaBlending = 1 << 16;
00034 static const int CullFaceDir = 1 << 17;
00035 static const int ClippingEnabled = 1 << 18;
00036 }
00037
00038 namespace GsCtxtFlags {
00039 static const int Texture = 1;
00040 static const int DrawEnv = Texture * 2;
00041 }
00042
00048 namespace RendererPropFlags {
00049 static const int NumLights = 1;
00050 static const int TexEnabled = NumLights * 2;
00051 static const int LightingEnabled = TexEnabled * 2;
00052 static const int SpecularEnabled = LightingEnabled * 2;
00053 static const int PerVtxMaterial = SpecularEnabled * 2;
00054 static const int CullFaceEnabled = PerVtxMaterial * 2;
00055 static const int Prim = CullFaceEnabled * 2;
00056 static const int ArrayAccessType = Prim * 2;
00057 static const int ClippingEnabled = ArrayAccessType * 2;
00058 }
00059
00060
00061 * CGLContext
00062 */
00063
00064 class CImmMatrixStack;
00065 class CDListMatrixStack;
00066 class CMatrixStack;
00067
00068 class CImmLighting;
00069 class CDListLighting;
00070 class CLighting;
00071
00072 class CImmGeomManager;
00073 class CDListGeomManager;
00074 class CGeomManager;
00075
00076 class CMaterialManager;
00077 class CDListManager;
00078 class CTexManager;
00079
00080 class CImmDrawContext;
00081 class CDListDrawContext;
00082 class CDrawContext;
00083
00084 class CDisplayContext;
00085
00086 class CGLContext {
00087 CImmMatrixStack *ProjectionMatStack, *ModelViewMatStack;
00088 CDListMatrixStack *DListMatStack;
00089 CMatrixStack *CurMatrixStack, *SavedCurMatStack;
00090
00091 CImmLighting *ImmLighting;
00092 CDListLighting *DListLighting;
00093 CLighting *CurLighting;
00094
00095 CImmGeomManager *ImmGManager;
00096 CDListGeomManager *DListGManager;
00097 CGeomManager *CurGManager;
00098
00099 CMaterialManager *MaterialManager;
00100 CDListManager *DListManager;
00101 CTexManager *TexManager;
00102
00103 CImmDrawContext *ImmDrawContext;
00104 CDListDrawContext *DListDrawContext;
00105 CDrawContext *CurDrawContext;
00106
00107 CDisplayContext *DisplayContext;
00108
00109
00110
00111 tU32 RendererContextChanged, SavedRendererContextChanges;
00112 tU32 GsContextChanged, SavedGsContextChanges;
00113 tU32 RendererPropsChanged, SavedRendererPropsChanges;
00114 bool StateChangesArePushed;
00115
00116 inline void PushStateChanges() {
00117 mErrorIf( StateChangesArePushed, "Trying to push state changes when already pushed." );
00118 SavedRendererContextChanges = RendererContextChanged;
00119 SavedGsContextChanges = GsContextChanged;
00120 SavedRendererPropsChanges = RendererPropsChanged;
00121 StateChangesArePushed = true;
00122 }
00123 inline void PopStateChanges() {
00124 mErrorIf( ! StateChangesArePushed,
00125 "Trying to pop state changes that haven't been pushed." );
00126 RendererContextChanged = SavedRendererContextChanges;
00127 GsContextChanged = SavedGsContextChanges;
00128 RendererPropsChanged = SavedRendererPropsChanges;
00129 StateChangesArePushed = false;
00130 }
00131
00132
00133
00134 bool IsCurrentFieldEven;
00135 unsigned int CurrentFrameNumber;
00136
00137
00138 static const int kDmaPacketMaxQwordLength = 65000;
00139 static CVifSCDmaPacket *CurPacket, *LastPacket,
00140 *Vif1Packet, *SavedVif1Packet,
00141 *ImmVif1Packet;
00142
00143
00144
00145
00146
00147
00148
00149 static const int kMaxDrawEnvChanges = 100;
00150 void *DrawEnvPtrs0[kMaxDrawEnvChanges];
00151 void *DrawEnvPtrs1[kMaxDrawEnvChanges];
00152 void **CurDrawEnvPtrs, **LastDrawEnvPtrs;
00153 int NumCurDrawEnvPtrs, NumLastDrawEnvPtrs;
00154
00155
00156 static const int kMaxBuffersToBeFreed = 1024;
00157 int CurBuffer;
00158 void *BuffersToBeFreed[2][kMaxBuffersToBeFreed];
00159 int NumBuffersToBeFreed[2];
00160
00163 #ifndef PS2_LINUX
00164 static const tU64 Ps2glSignalId = 0xffffffff00000000 | (tU32)'G' << 24 | (tU32)'L' << 16;
00165 #else
00166 static const tU64 Ps2glSignalId = (tU32)'G' << 24 | (tU32)'L' << 16;
00167 #endif
00168
00170 static int RenderingFinishedSemaId, ImmediateRenderingFinishedSemaId, VsyncSemaId;
00171
00172 static int GsIntHandler( int cause );
00173
00174 void FreeWaitingBuffersAndSwap();
00175
00176 void EndVif1Packet( unsigned short signalNum );
00177
00178 typedef void (* tRenderingFinishedCallback)(void);
00179 static tRenderingFinishedCallback RenderingFinishedCallback;
00180
00181 public:
00182 CGLContext( int immBufferQwordSize, int immDrawBufferQwordSize );
00183 ~CGLContext();
00184
00185 void SetMatrixMode( GLenum mode );
00186 inline CMatrixStack& GetCurMatrixStack() { return *CurMatrixStack; }
00187 inline CImmMatrixStack& GetModelViewStack() { return *ModelViewMatStack; }
00188 inline CImmMatrixStack& GetProjectionStack() { return *ProjectionMatStack; }
00189
00190 inline CLighting& GetLighting() { return *CurLighting; }
00191 inline CImmLighting& GetImmLighting() { return *ImmLighting; }
00192 inline CDListLighting& GetDListLighting() { return *DListLighting; }
00193
00194 inline CGeomManager& GetGeomManager() { return *CurGManager; }
00195 inline CImmGeomManager& GetImmGeomManager() { return *ImmGManager; }
00196 inline CDListGeomManager& GetDListGeomManager() { return *DListGManager; }
00197
00198 inline CMaterialManager& GetMaterialManager() { return *MaterialManager; }
00199
00200 inline CDListManager& GetDListManager() { return *DListManager; }
00201
00202 inline CTexManager& GetTexManager() { return *TexManager; }
00203
00204 inline CDrawContext& GetDrawContext() { return *CurDrawContext; }
00205 inline CImmDrawContext& GetImmDrawContext() { return *ImmDrawContext; }
00206 inline CDListDrawContext& GetDListDrawContext() { return *DListDrawContext; }
00207
00208 inline CDisplayContext& GetDisplayContext() { return *DisplayContext; }
00209
00210 inline bool InDListDef() const { return CurGManager != (CGeomManager*)ImmGManager; }
00211 void BeginDListDef( unsigned int listID, GLenum mode );
00212 void EndDListDef();
00213
00214 void BeginImmediateGeometry();
00215 void EndImmediateGeometry();
00216 void RenderImmediateGeometry();
00217 void FinishRenderingImmediateGeometry( bool forceImmediateStop );
00218
00219 void BeginGeometry();
00220 void EndGeometry();
00221 void RenderGeometry();
00222 void FinishRenderingGeometry( bool forceImmediateStop );
00223
00224 void AddingDrawEnvToPacket( void *de ) {
00225 mErrorIf( NumCurDrawEnvPtrs == kMaxDrawEnvChanges,
00226 "Too many draw environment changes. Need to increase kMaxDrawEnvChanges" );
00227 CurDrawEnvPtrs[NumCurDrawEnvPtrs++] = de;
00228 }
00229 void** GetDrawEnvPtrs() { return LastDrawEnvPtrs; }
00230 int GetNumDrawEnvPtrs() const { return NumLastDrawEnvPtrs; }
00231
00232 bool GetCurrentFieldIsEven() const { return IsCurrentFieldEven; }
00233
00238 static tU16 GetPs2glSignalId() { return (tU16)(Ps2glSignalId >> 16); }
00239
00244 inline void AddBufferToBeFreed( void *buf ) {
00245 mAssert( NumBuffersToBeFreed[CurBuffer] < kMaxBuffersToBeFreed );
00246 BuffersToBeFreed[CurBuffer][NumBuffersToBeFreed[CurBuffer]++] = buf;
00247 }
00248
00249 inline static void SetRenderingFinishedCallback( tRenderingFinishedCallback cb ) {
00250 RenderingFinishedCallback = cb;
00251 }
00252
00253
00254
00255 inline void NumLightsChanged() {
00256 RendererContextChanged |= RendererCtxtFlags::NumLights;
00257 RendererPropsChanged |= RendererPropFlags::NumLights;
00258 }
00259 inline void LightPropChanged() {
00260 RendererContextChanged |= RendererCtxtFlags::LightPropChanged;
00261 }
00262 inline void GlobalAmbChanged() {
00263 RendererContextChanged |= RendererCtxtFlags::GlobalAmb;
00264 }
00265 inline void CurMaterialChanged() {
00266 RendererContextChanged |= RendererCtxtFlags::CurMaterial;
00267 }
00268 inline void XformChanged() {
00269 RendererContextChanged |= RendererCtxtFlags::Xform;
00270 }
00271 inline void PrimChanged() {
00272 RendererContextChanged |= RendererCtxtFlags::Prim;
00273 RendererPropsChanged |= RendererPropFlags::Prim;
00274 }
00275 inline void ShadingChanged() {
00276 RendererContextChanged |= RendererCtxtFlags::Shading;
00277 }
00278 inline void TexEnabledChanged() {
00279 RendererContextChanged |= RendererCtxtFlags::TexEnabled;
00280 RendererPropsChanged |= RendererPropFlags::TexEnabled;
00281 GsContextChanged |= GsCtxtFlags::Texture;
00282 }
00283 inline void LightingEnabledChanged() {
00284 RendererContextChanged |= RendererCtxtFlags::LightingEnabled;
00285 RendererPropsChanged |= RendererPropFlags::LightingEnabled;
00286 }
00287 inline void BlendEnabledChanged() {
00288 RendererContextChanged |= RendererCtxtFlags::AlphaBlending;
00289 }
00290 inline void DrawEnvChanged() {
00291 GsContextChanged |= GsCtxtFlags::DrawEnv;
00292 }
00293 inline void AlphaTestEnabledChanged() {
00294 GsContextChanged |= GsCtxtFlags::DrawEnv;
00295 }
00296 inline void DrawInterlacedChanged() {
00297 GsContextChanged |= GsCtxtFlags::DrawEnv;
00298 }
00299 inline void AlphaTestFuncChanged() {
00300 GsContextChanged |= GsCtxtFlags::DrawEnv;
00301 }
00302 inline void DepthWriteEnabledChanged() {
00303 GsContextChanged |= GsCtxtFlags::DrawEnv;
00304 }
00305 inline void FrameBufferDrawMaskChanged() {
00306 GsContextChanged |= GsCtxtFlags::DrawEnv;
00307 }
00308 inline void SpecularEnabledChanged() {
00309 RendererPropsChanged |= RendererPropFlags::SpecularEnabled;
00310 }
00311 inline void TextureChanged() {
00312 GsContextChanged |= GsCtxtFlags::Texture;
00313 }
00314 inline void BlendModeChanged() {
00315 GsContextChanged |= GsCtxtFlags::DrawEnv;
00316 }
00317 inline void DrawBufferChanged() {
00318 GsContextChanged |= GsCtxtFlags::DrawEnv;
00319 }
00320 inline void PerVtxMaterialChanged() {
00321 RendererPropsChanged |= RendererPropFlags::PerVtxMaterial;
00322 }
00323 inline void ClippingEnabledChanged() {
00324 RendererPropsChanged |= RendererPropFlags::ClippingEnabled;
00325 RendererContextChanged |= RendererCtxtFlags::ClippingEnabled;
00326 }
00327 inline void CullFaceEnabledChanged() {
00328 RendererPropsChanged |= RendererPropFlags::CullFaceEnabled;
00329 }
00330 inline void CullFaceDirChanged() {
00331 RendererContextChanged |= RendererCtxtFlags::CullFaceDir;
00332 }
00333 inline void ArrayAccessChanged() {
00334 RendererPropsChanged |= RendererPropFlags::ArrayAccessType;
00335 }
00336 inline void PolyModeChanged() {
00337 RendererContextChanged |= RendererCtxtFlags::Prim;
00338 }
00339
00340
00341
00342 inline tU32 GetRendererContextChanged() const { return RendererContextChanged; }
00343 inline void SetRendererContextChanged( bool changed ) {
00344 RendererContextChanged = (changed) ? 0xff : 0;
00345 }
00346
00347 inline tU32 GetGsContextChanged() const { return GsContextChanged; }
00348 inline void SetGsContextChanged( bool changed ) {
00349 GsContextChanged = (changed) ? 0xff : 0;
00350 }
00351
00352 inline tU32 GetRendererPropsChanged() const { return RendererPropsChanged; }
00353 inline void SetRendererPropsChanged( bool changed ) {
00354 RendererPropsChanged = (changed) ? 0xff : 0;
00355 }
00356
00357 inline void PushVif1Packet() {
00358 mAssert(SavedVif1Packet == NULL);
00359 SavedVif1Packet = Vif1Packet;
00360 }
00361 inline void PopVif1Packet() {
00362 mAssert(SavedVif1Packet != NULL);
00363 Vif1Packet = SavedVif1Packet;
00364 SavedVif1Packet = NULL;
00365 }
00366 inline void SetVif1Packet( CVifSCDmaPacket &packet ) { Vif1Packet = &packet; }
00367 inline CVifSCDmaPacket& GetVif1Packet() { return *Vif1Packet; }
00368
00369 void WaitForVSync();
00370 void SwapBuffers();
00371 };
00372
00373
00374 extern CGLContext *pGLContext;
00375
00376 #endif // ps2gl_context_h