15#include "ps2s/displayenv.h"
16#include "ps2s/drawenv.h"
17#include "ps2s/gsmem.h"
19#include "ps2s/packet.h"
20#include "ps2s/ps2stuff.h"
21#include "ps2s/texture.h"
22#include "ps2s/types.h"
24#include "ps2gl/displaycontext.h"
25#include "ps2gl/dlgmanager.h"
26#include "ps2gl/dlist.h"
27#include "ps2gl/drawcontext.h"
28#include "ps2gl/glcontext.h"
29#include "ps2gl/gmanager.h"
30#include "ps2gl/immgmanager.h"
31#include "ps2gl/lighting.h"
32#include "ps2gl/material.h"
33#include "ps2gl/matrix.h"
34#include "ps2gl/texture.h"
46CVifSCDmaPacket *CGLContext::CurPacket, *CGLContext::LastPacket,
47 *CGLContext::Vif1Packet = NULL, *CGLContext::SavedVif1Packet = NULL,
48 *CGLContext::ImmVif1Packet;
51int CGLContext::ImmediateRenderingFinishedSemaId = -1;
52int CGLContext::VsyncSemaId = -1;
54CGLContext::tRenderingFinishedCallback CGLContext::RenderingFinishedCallback = NULL;
56CGLContext::CGLContext(
int immBufferQwordSize,
int immDrawBufferQwordSize)
57 : StateChangesArePushed(false)
58 , IsCurrentFieldEven(true)
59 , CurrentFrameNumber(0)
62 CurPacket =
new CVifSCDmaPacket(kDmaPacketMaxQwordLength, DMAC::Channels::vif1,
63 Packet::kXferTags, Core::MemMappings::UncachedAccl);
64 LastPacket =
new CVifSCDmaPacket(kDmaPacketMaxQwordLength, DMAC::Channels::vif1,
65 Packet::kXferTags, Core::MemMappings::UncachedAccl);
66 Vif1Packet = CurPacket;
68 ImmVif1Packet =
new CVifSCDmaPacket(immDrawBufferQwordSize, DMAC::Channels::vif1,
69 Packet::kXferTags, Core::MemMappings::UncachedAccl);
71 CurDrawEnvPtrs = DrawEnvPtrs0;
72 LastDrawEnvPtrs = DrawEnvPtrs1;
73 NumCurDrawEnvPtrs = 0;
74 NumLastDrawEnvPtrs = 0;
78 CurGManager = ImmGManager;
83 CurMatrixStack = ModelViewMatStack;
84 SavedCurMatStack = NULL;
88 CurLighting = ImmLighting;
90 CLight& light = ImmLighting->GetLight(0);
91 light.SetDiffuse(cpu_vec_xyzw(1.0f, 1.0f, 1.0f, 1.0f));
92 light.SetSpecular(cpu_vec_xyzw(1.0f, 1.0f, 1.0f, 1.0f));
100 CurDrawContext = ImmDrawContext;
104 SetRendererContextChanged(
true);
105 SetGsContextChanged(
true);
106 SetRendererPropsChanged(
true);
109 NumBuffersToBeFreed[0] = NumBuffersToBeFreed[1] = 0;
115 struct t_ee_sema newSemaphore = { 0, 1, 0 };
116 VsyncSemaId = CreateSema(&newSemaphore);
117 RenderingFinishedSemaId = CreateSema(&newSemaphore);
118 ImmediateRenderingFinishedSemaId = CreateSema(&newSemaphore);
119 mErrorIf(VsyncSemaId == -1
120 || RenderingFinishedSemaId == -1
121 || ImmediateRenderingFinishedSemaId == -1,
122 "Failed to create ps2gl semaphores.");
126 AddIntcHandler(INTC_GS, CGLContext::GsIntHandler, 0 );
129 *(
volatile unsigned int*)GS::ControlRegs::csr = 9;
131 *(
volatile unsigned int*)GS::ControlRegs::imr = 0x7600;
134CGLContext::~CGLContext()
140 delete DListGManager;
142 delete ProjectionMatStack;
143 delete ModelViewMatStack;
144 delete DListMatStack;
147 delete DListLighting;
149 delete MaterialManager;
153 delete ImmDrawContext;
154 delete DListDrawContext;
156 delete DisplayContext;
163void CGLContext::BeginDListDef(
unsigned int listID, GLenum mode)
165 DListManager->NewList(listID, mode);
170 SetRendererContextChanged(
true);
171 SetGsContextChanged(
true);
173 SetRendererPropsChanged(
true);
175 MaterialManager->BeginDListDef();
176 TexManager->BeginDListDef();
177 DListGManager->BeginDListDef();
179 CurLighting = DListLighting;
180 CurGManager = DListGManager;
181 SavedCurMatStack = CurMatrixStack;
182 CurMatrixStack = DListMatStack;
183 CurDrawContext = DListDrawContext;
186void CGLContext::EndDListDef()
188 DListGManager->EndDListDef();
189 MaterialManager->EndDListDef();
190 TexManager->EndDListDef();
192 CurLighting = ImmLighting;
193 CurGManager = ImmGManager;
194 CurMatrixStack = SavedCurMatStack;
195 CurDrawContext = ImmDrawContext;
199 DListManager->EndList();
217 return CDListCmd::GetNextCmd(
this);
221void CGLContext::SetMatrixMode(GLenum mode)
228 CurMatrixStack = ModelViewMatStack;
231 CurMatrixStack = ProjectionMatStack;
243void CGLContext::BeginImmediateGeometry()
249 GetImmGeomManager().Flush();
252 SetVif1Packet(*ImmVif1Packet);
254 ImmVif1Packet->Reset();
257void CGLContext::EndImmediateGeometry()
259 mAssert(Vif1Packet == ImmVif1Packet);
266void CGLContext::RenderImmediateGeometry()
268 ImmVif1Packet->End();
269 ImmVif1Packet->Pad128();
270 ImmVif1Packet->CloseTag();
272 ImmVif1Packet->Send();
275void CGLContext::FinishRenderingImmediateGeometry(
bool forceImmediateStop)
277 mWarnIf(forceImmediateStop,
"Interrupting currently rendering dma chain not supported yet");
285void CGLContext::BeginGeometry()
292void CGLContext::EndGeometry()
297void CGLContext::EndVif1Packet(
unsigned short signalNum)
302 GetImmGeomManager().Flush();
318 Vif1Packet->OpenDirect();
320 *Vif1Packet += giftag;
322 *Vif1Packet += (tU64)0x60;
324 Vif1Packet->CloseDirect();
325 Vif1Packet->CloseTag();
328void CGLContext::RenderGeometry()
339int CGLContext::GsIntHandler(
int cause)
343 tU32 csr = *(
volatile tU32*)GS::ControlRegs::csr;
347 tU64 sigLblId = *(
volatile tU64*)GS::ControlRegs::siglblid;
349 switch (sigLblId & 0xffff) {
352 if (RenderingFinishedCallback != NULL)
353 RenderingFinishedCallback();
356 iSignalSema(ImmediateRenderingFinishedSemaId);
359 mError(
"Unknown signal");
363 sigLblId &= ~0xffffffff;
364 *(
volatile tU64*)GS::ControlRegs::siglblid = sigLblId;
366 *(
volatile unsigned int*)GS::ControlRegs::csr = 1;
373 iSignalSema(VsyncSemaId);
375 *(
volatile unsigned int*)GS::ControlRegs::csr = 8;
387 *(
volatile unsigned int*)GS::ControlRegs::imr = 0x7f00;
388 *(
volatile unsigned int*)GS::ControlRegs::imr = 0x7600;
394void CGLContext::FinishRenderingGeometry(
bool forceImmediateStop)
398 mWarnIf(forceImmediateStop,
"Interrupting currently rendering dma chain not supported yet");
402void CGLContext::WaitForVSync()
407 WaitSema(VsyncSemaId);
412 while (PollSema(VsyncSemaId) != -1)
415 tU32 csr = *(
volatile tU32*)GS::ControlRegs::csr;
416 IsCurrentFieldEven = (bool)((csr >> 13) & 1);
419void CGLContext::SwapBuffers()
425 CVifSCDmaPacket* tempPkt = CurPacket;
426 CurPacket = LastPacket;
427 LastPacket = tempPkt;
428 Vif1Packet = CurPacket;
432 void** tempDEPtrs = CurDrawEnvPtrs;
433 CurDrawEnvPtrs = LastDrawEnvPtrs;
434 LastDrawEnvPtrs = tempDEPtrs;
435 NumLastDrawEnvPtrs = NumCurDrawEnvPtrs;
436 NumCurDrawEnvPtrs = 0;
440 GetImmGeomManager().SwapBuffers();
441 GetDListManager().SwapBuffers();
442 GetDisplayContext().SwapBuffers();
443 GetImmDrawContext().SwapBuffers(IsCurrentFieldEven);
446 FreeWaitingBuffersAndSwap();
448 CurrentFrameNumber++;
451void CGLContext::FreeWaitingBuffersAndSwap()
455 CurBuffer = 1 - CurBuffer;
457 for (
int i = 0; i < NumBuffersToBeFreed[CurBuffer]; i++) {
458 free(BuffersToBeFreed[CurBuffer][i]);
461 NumBuffersToBeFreed[CurBuffer] = 0;
495int pglInit(
int immBufferVertexSize,
int immDrawBufferQwordSize)
498 pGLContext =
new CGLContext(immBufferVertexSize, immDrawBufferQwordSize);
509 return (pGLContext != NULL);
531 dma_channel_wait(DMAC::Channels::vif1, 1000000);
540 pGLContext->WaitForVSync();
551 mErrorIf(pGLContext == NULL,
"You need to call pglInit()");
553 pGLContext->SwapBuffers();
563 pGLContext->SetRenderingFinishedCallback(cb);
570void pglBeginImmediateGeometry(
void)
572 pGLContext->BeginImmediateGeometry();
574void pglEndImmediateGeometry(
void)
576 pGLContext->EndImmediateGeometry();
578void pglRenderImmediateGeometry(
void)
580 pGLContext->RenderImmediateGeometry();
582void pglFinishRenderingImmediateGeometry(
int forceImmediateStop)
584 pGLContext->FinishRenderingImmediateGeometry((
bool)forceImmediateStop);
591void pglBeginGeometry(
void)
593 pGLContext->BeginGeometry();
595void pglEndGeometry(
void)
597 pGLContext->EndGeometry();
599void pglRenderGeometry(
void)
601 pGLContext->RenderGeometry();
603void pglFinishRenderingGeometry(
int forceImmediateStop)
605 pGLContext->FinishRenderingGeometry((
bool)forceImmediateStop);
612void pglEnable(GLenum cap)
616 pGLContext->GetDrawContext().SetDoClipping(
true);
619 mError(
"Unknown option passed to pglEnable()");
623void pglDisable(GLenum cap)
627 pGLContext->GetDrawContext().SetDoClipping(
false);
630 mError(
"Unknown option passed to pglDisable()");
642void glEnable(GLenum cap)
644 GL_FUNC_DEBUG(
"%s(0x%x)\n", __FUNCTION__, cap);
646 CLighting& lighting = pGLContext->GetLighting();
657 lighting.GetLight(0x7 & cap).SetEnabled(
true);
660 lighting.SetLightingEnabled(
true);
664 pGLContext->GetDrawContext().SetBlendEnabled(
true);
667 case GL_COLOR_MATERIAL:
668 pGLContext->GetMaterialManager().SetUseColorMaterial(
true);
670 case GL_RESCALE_NORMAL:
671 pGLContext->GetDrawContext().SetRescaleNormals(
true);
675 pGLContext->GetTexManager().SetTexEnabled(
true);
679 pGLContext->GetGeomManager().SetDoNormalize(
true);
683 pGLContext->GetDrawContext().SetDoCullFace(
true);
687 pGLContext->GetDrawContext().SetAlphaTestEnabled(
true);
691 pGLContext->GetDrawContext().SetDepthTestEnabled(
true);
700void glDisable(GLenum cap)
702 GL_FUNC_DEBUG(
"%s(0x%x)\n", __FUNCTION__, cap);
713 pGLContext->GetLighting().GetLight(0x7 & cap).SetEnabled(
false);
716 pGLContext->GetLighting().SetLightingEnabled(
false);
720 pGLContext->GetDrawContext().SetBlendEnabled(
false);
723 case GL_COLOR_MATERIAL:
724 pGLContext->GetMaterialManager().SetUseColorMaterial(
false);
726 case GL_RESCALE_NORMAL:
727 pGLContext->GetDrawContext().SetRescaleNormals(
false);
731 pGLContext->GetTexManager().SetTexEnabled(
false);
735 pGLContext->GetGeomManager().SetDoNormalize(
false);
739 pGLContext->GetDrawContext().SetDoCullFace(
false);
743 pGLContext->GetDrawContext().SetAlphaTestEnabled(
false);
747 pGLContext->GetDrawContext().SetDepthTestEnabled(
false);
755void glHint(GLenum target, GLenum mode)
757 GL_FUNC_DEBUG(
"%s(0x%x,0x%x)\n", __FUNCTION__, target, mode);
762void glGetFloatv(GLenum pname, GLfloat* params)
764 GL_FUNC_DEBUG(
"%s(0x%x,...)\n", __FUNCTION__, pname);
767 case GL_MODELVIEW_MATRIX:
768 memcpy(params, &(pGLContext->GetModelViewStack().GetTop()), 16 * 4);
770 case GL_PROJECTION_MATRIX:
771 memcpy(params, &(pGLContext->GetProjectionStack().GetTop()), 16 * 4);
774 mNotImplemented(
"pname %d", pname);
779void glGetIntegerv(GLenum pname,
int* params)
781 GL_FUNC_DEBUG(
"%s(0x%x,...)\n", __FUNCTION__, pname);
786GLenum glGetError(
void)
788 GL_FUNC_DEBUG(
"%s()\n", __FUNCTION__);
790 mWarn(
"glGetError does nothing");
795const GLubyte* glGetString(GLenum name)
797 GL_FUNC_DEBUG(
"%s(0x%x)\n", __FUNCTION__, name);
800 return (GLubyte*)
"not implemented";
static tU16 GetPs2glSignalId()
static int RenderingFinishedSemaId
Semaphores signaled by the gs int handler.
static const tU64 Ps2glSignalId
void pglSetRenderingFinishedCallback(void(*cb)(void))
void pglSwapBuffers(void)
int pglHasLibraryBeenInitted(void)
void pglWaitForVSync(void)
int pglInit(int immBufferVertexSize, int immDrawBufferQwordSize)