00001
00002
00003
00004
00005 #include <string.h>
00006
00007 #include "libgraph.h"
00008 #include "libdma.h"
00009 #include "libdev.h"
00010
00011 #include "GL/ps2gl.h"
00012
00013 #include "ps2s/ps2stuff.h"
00014 #include "ps2s/types.h"
00015 #include "ps2s/math.h"
00016 #include "ps2s/drawenv.h"
00017 #include "ps2s/displayenv.h"
00018 #include "ps2s/packet.h"
00019 #include "ps2s/gsmem.h"
00020 #include "ps2s/texture.h"
00021
00022 #include "ps2gl/glcontext.h"
00023 #include "ps2gl/matrix.h"
00024 #include "ps2gl/gmanager.h"
00025 #include "ps2gl/dlgmanager.h"
00026 #include "ps2gl/immgmanager.h"
00027 #include "ps2gl/lighting.h"
00028 #include "ps2gl/material.h"
00029 #include "ps2gl/dlist.h"
00030 #include "ps2gl/texture.h"
00031 #include "ps2gl/displaycontext.h"
00032 #include "ps2gl/drawcontext.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 CVifSCDmaPacket *CGLContext::CurPacket, *CGLContext::LastPacket;
00046
00047
00048
00049 extern "C" void Vu1PktStart();
00050 extern "C" void Vu1PktEnd();
00051
00052 CGLContext::CGLContext( int immBufferQwordSize )
00053 : StateChangesArePushed(false),
00054 IsCurrentFieldEven(true),
00055 CurrentFrameNumber(0)
00056 {
00057 CurPacket = new CVifSCDmaPacket( kDmaPacketMaxQwordLength, DMAC::Channels::vif1,
00058 Packet::kXferTags, Core::MemMappings::UncachedAccl );
00059 LastPacket = new CVifSCDmaPacket( kDmaPacketMaxQwordLength, DMAC::Channels::vif1,
00060 Packet::kXferTags, Core::MemMappings::UncachedAccl );
00061
00062 ImmGManager = new CImmGeomManager(*this, immBufferQwordSize);
00063 DListGManager = new CDListGeomManager(*this);
00064 CurGManager = ImmGManager;
00065
00066 ProjectionMatStack = new CImmMatrixStack(*this);
00067 ModelViewMatStack = new CImmMatrixStack(*this);
00068 DListMatStack = new CDListMatrixStack(*this);
00069 CurMatrixStack = ModelViewMatStack;
00070 SavedCurMatStack = NULL;
00071
00072 ImmLighting = new CImmLighting(*this);
00073 DListLighting = new CDListLighting(*this);
00074 CurLighting = ImmLighting;
00075
00076 CLight &light = ImmLighting->GetLight(0);
00077 light.SetDiffuse( cpu_vec_xyzw(1.0f, 1.0f, 1.0f, 1.0f) );
00078 light.SetSpecular( cpu_vec_xyzw(1.0f, 1.0f, 1.0f, 1.0f) );
00079
00080 MaterialManager = new CMaterialManager(*this);
00081 DListManager = new CDListManager;
00082 TexManager = new CTexManager(*this);
00083
00084 ImmDrawContext = new CImmDrawContext(*this);
00085 DListDrawContext = new CDListDrawContext(*this);
00086 CurDrawContext = ImmDrawContext;
00087
00088 DisplayContext = new CDisplayContext(*this);
00089
00090 SetVu1ContextChanged(true);
00091 SetGsContextChanged(true);
00092 SetRendererPropsChanged(true);
00093
00094
00095 sceDevVif0Reset();
00096 sceDevVu0Reset();
00097 sceDmaReset(1);
00098 sceGsResetPath();
00099 sceGsResetGraph(0, SCE_GS_INTERLACE, SCE_GS_NTSC, SCE_GS_FRAME);
00100 GS::Init();
00101 }
00102
00103 CGLContext::~CGLContext()
00104 {
00105 delete CurPacket;
00106 delete LastPacket;
00107
00108 delete ImmGManager;
00109 delete DListGManager;
00110
00111 delete ProjectionMatStack;
00112 delete ModelViewMatStack;
00113 delete DListMatStack;
00114
00115 delete ImmLighting;
00116 delete DListLighting;
00117
00118 delete MaterialManager;
00119 delete DListManager;
00120 delete TexManager;
00121
00122 delete ImmDrawContext;
00123 delete DListDrawContext;
00124
00125 delete DisplayContext;
00126 }
00127
00128 void
00129 CGLContext::BeginDListDef( unsigned int listID, GLenum mode )
00130 {
00131 DListManager->NewList( listID, mode );
00132
00133 PushStateChanges();
00134
00135 SetVu1ContextChanged(true);
00136 SetGsContextChanged(true);
00137 SetRendererPropsChanged(false);
00138
00139 MaterialManager->BeginDListDef();
00140 TexManager->BeginDListDef();
00141 CurLighting = DListLighting;
00142 CurGManager = DListGManager;
00143 ImmGManager->BeginDListDef();
00144 DListGManager->BeginDListDef();
00145 SavedCurMatStack = CurMatrixStack;
00146 CurMatrixStack = DListMatStack;
00147 }
00148
00149 void
00150 CGLContext::EndDListDef()
00151 {
00152 DListGManager->EndDListDef();
00153
00154 MaterialManager->EndDListDef();
00155 TexManager->EndDListDef();
00156 CurLighting = ImmLighting;
00157 CurGManager = ImmGManager;
00158 CurMatrixStack = SavedCurMatStack;
00159
00160 PopStateChanges();
00161
00162 DListManager->EndList();
00163 }
00164
00165 class CSetMatrixModeCmd : public CDListCmd {
00166 GLenum Mode;
00167 public:
00168 CSetMatrixModeCmd( GLenum mode ) : Mode(mode) {}
00169 CDListCmd* Play() { glMatrixMode( Mode ); return CDListCmd::GetNextCmd(this); }
00170 };
00171
00172 void
00173 CGLContext::SetMatrixMode( GLenum mode )
00174 {
00175 if ( InDListDef() ) {
00176 DListManager->GetOpenDList() += CSetMatrixModeCmd(mode);
00177 }
00178 else {
00179 switch (mode) {
00180 case GL_MODELVIEW:
00181 CurMatrixStack = ModelViewMatStack;
00182 break;
00183 case GL_PROJECTION:
00184 CurMatrixStack = ProjectionMatStack;
00185 break;
00186 default:
00187 mNotImplemented( );
00188 }
00189 }
00190 }
00191
00192 void
00193 CGLContext::WaitForVSync()
00194 {
00195
00196 IsCurrentFieldEven = Math::IsEven(sceGsSyncV(0));
00197 }
00198
00199 void
00200 CGLContext::SwapBuffers()
00201 {
00202
00203 GetDListManager().FrameHasEnded();
00204
00205
00206 GetImmGeomManager().Flush();
00207
00208
00209 CurPacket->End();
00210 CurPacket->Pad128();
00211 CurPacket->CloseTag();
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 CVifSCDmaPacket *tempPkt = CurPacket;
00225 CurPacket = LastPacket;
00226 LastPacket = tempPkt;
00227
00228 LastPacket->Send();
00229 CurPacket->Reset();
00230
00231 GetImmGeomManager().SwapBuffers();
00232 GetDisplayContext().SwapBuffers();
00233 GetImmDrawContext().SwapBuffers( IsCurrentFieldEven );
00234
00235 CurrentFrameNumber++;
00236 }
00237
00238
00239
00240
00241
00243 CGLContext *pGLContext = NULL;
00244
00266 int
00267 pglInit( int immBufferVertexSize )
00268 {
00269 ps2sInit();
00270 pGLContext = new CGLContext(immBufferVertexSize);
00271
00272 return true;
00273 }
00274
00279 int
00280 pglHasLibraryBeenInitted(void)
00281 {
00282 return (pGLContext != NULL);
00283 }
00284
00288 void
00289 pglFinish( void )
00290 {
00291 if (pGLContext) delete pGLContext;
00292 ps2sFinish();
00293 }
00294
00302 void
00303 pglWaitForVU1( void )
00304 {
00305
00306 *D_PCR = 0x2;
00307
00308 *D_STAT = 2;
00309
00310 asm volatile ("sync.l");
00311
00312 if ( *D1_CHCR & 0x100 ) {
00313
00314 asm volatile (
00315 ".set noreorder \n"
00316
00317 "0: \n"
00318
00319 "nop \n"
00320 "nop \n"
00321
00322 "nop \n"
00323 "nop \n"
00324
00325 "nop \n"
00326 "nop \n"
00327
00328 "nop \n"
00329 "nop \n"
00330
00331 "bc0f 0b \n"
00332 "nop \n"
00333
00334 ".set reorder \n"
00335 );
00336 }
00337 }
00338
00343 void
00344 pglWaitForVSync( void )
00345 {
00346 pGLContext->WaitForVSync();
00347 }
00348
00355 void
00356 pglSwapBuffers( void )
00357 {
00358 mErrorIf( pGLContext == NULL, "You need to call pglInit()" );
00359
00360 pGLContext->SwapBuffers();
00361 }
00362
00367
00368
00369
00370
00371 void glEnable( GLenum cap )
00372 {
00373 CLighting& lighting = pGLContext->GetLighting();
00374
00375 switch (cap) {
00376 case GL_LIGHT0:
00377 case GL_LIGHT1:
00378 case GL_LIGHT2:
00379 case GL_LIGHT3:
00380 case GL_LIGHT4:
00381 case GL_LIGHT5:
00382 case GL_LIGHT6:
00383 case GL_LIGHT7:
00384 lighting.GetLight(0x7 & cap).SetEnabled(true);
00385 break;
00386 case GL_LIGHTING:
00387 lighting.SetLightingEnabled(true);
00388 break;
00389
00390 case GL_BLEND:
00391 pGLContext->GetDrawContext().SetBlendEnabled(true);
00392 break;
00393
00394 case GL_COLOR_MATERIAL:
00395 pGLContext->GetMaterialManager().SetUseColorMaterial(true);
00396 break;
00397 case GL_RESCALE_NORMAL:
00398 pGLContext->GetDrawContext().SetRescaleNormals(true);
00399 break;
00400
00401 case GL_TEXTURE_2D:
00402 pGLContext->GetTexManager().SetTexEnabled(true);
00403 break;
00404
00405 case GL_NORMALIZE:
00406 pGLContext->GetGeomManager().SetDoNormalize(true);
00407 break;
00408
00409 case GL_CULL_FACE:
00410 pGLContext->GetDrawContext().SetDoCullFace(true);
00411 break;
00412
00413 case GL_ALPHA_TEST:
00414 pGLContext->GetDrawContext().SetAlphaTestEnabled(true);
00415 break;
00416
00417 case GL_DEPTH_TEST:
00418 default:
00419 mNotImplemented( );
00420 break;
00421 }
00422 }
00423
00424 void glDisable( GLenum cap )
00425 {
00426 switch (cap) {
00427 case GL_LIGHT0:
00428 case GL_LIGHT1:
00429 case GL_LIGHT2:
00430 case GL_LIGHT3:
00431 case GL_LIGHT4:
00432 case GL_LIGHT5:
00433 case GL_LIGHT6:
00434 case GL_LIGHT7:
00435 pGLContext->GetLighting().GetLight(0x7 & cap).SetEnabled(false);
00436 break;
00437 case GL_LIGHTING:
00438 pGLContext->GetLighting().SetLightingEnabled(false);
00439 break;
00440
00441 case GL_BLEND:
00442 pGLContext->GetDrawContext().SetBlendEnabled(false);
00443 break;
00444
00445 case GL_COLOR_MATERIAL:
00446 pGLContext->GetMaterialManager().SetUseColorMaterial(false);
00447 break;
00448 case GL_RESCALE_NORMAL:
00449 pGLContext->GetDrawContext().SetRescaleNormals(false);
00450 break;
00451
00452 case GL_TEXTURE_2D:
00453 pGLContext->GetTexManager().SetTexEnabled(false);
00454 break;
00455
00456 case GL_NORMALIZE:
00457 pGLContext->GetGeomManager().SetDoNormalize(false);
00458 break;
00459
00460 case GL_CULL_FACE:
00461 pGLContext->GetDrawContext().SetDoCullFace(false);
00462 break;
00463
00464 case GL_ALPHA_TEST:
00465 pGLContext->GetDrawContext().SetAlphaTestEnabled(false);
00466 break;
00467
00468 case GL_DEPTH_TEST:
00469 default:
00470 mNotImplemented( );
00471 }
00472 }
00473
00474 void glHint( GLenum target, GLenum mode )
00475 {
00476 mNotImplemented( );
00477 }
00478
00479 void glGetFloatv( GLenum pname, GLfloat *params )
00480 {
00481 switch (pname) {
00482 case GL_MODELVIEW_MATRIX:
00483 memcpy( params, & (pGLContext->GetModelViewStack().GetTop()), 16 * 4 );
00484 break;
00485 case GL_PROJECTION_MATRIX:
00486 memcpy( params, & (pGLContext->GetProjectionStack().GetTop()), 16 * 4 );
00487 break;
00488 default:
00489 mNotImplemented( "pname %d", pname );
00490 break;
00491 }
00492 }
00493
00494 const GLubyte *glGetString( GLenum name )
00495 {
00496 mNotImplemented( );
00497 return (GLubyte*)"not implemented";
00498 }