PS2GL
OpenGL*-like API for the PS2
Loading...
Searching...
No Matches
glcontext.h
1/* Copyright (C) 2000,2001,2002 Sony Computer Entertainment America
2
3 This file is subject to the terms and conditions of the GNU Lesser
4 General Public License Version 2.1. See the file "COPYING" in the
5 main directory of this archive for more details. */
6
7#ifndef ps2gl_context_h
8#define ps2gl_context_h
9
10/********************************************
11 * includes
12 */
13
14#include "ps2s/gsmem.h"
15#include "ps2s/packet.h"
16
17#include "GL/gl.h"
18
19/********************************************
20 * state change flags
21 */
22
23namespace RendererCtxtFlags {
24static const int NumLights = 1 << 0;
25static const int LightPropChanged = 1 << 1;
26static const int GlobalAmb = 1 << 9;
27static const int CurMaterial = 1 << 10;
28static const int Xform = 1 << 11;
29static const int Prim = 1 << 12;
30static const int Shading = 1 << 13;
31static const int TexEnabled = 1 << 14;
32static const int LightingEnabled = 1 << 15;
33static const int AlphaBlending = 1 << 16;
34static const int CullFaceDir = 1 << 17;
35static const int ClippingEnabled = 1 << 18;
36}
37
38namespace GsCtxtFlags {
39static const int Texture = 1;
40static const int DrawEnv = Texture * 2;
41}
42
49static const int NumLights = 1;
50static const int TexEnabled = NumLights * 2;
51static const int LightingEnabled = TexEnabled * 2;
52static const int SpecularEnabled = LightingEnabled * 2;
53static const int PerVtxMaterial = SpecularEnabled * 2;
54static const int CullFaceEnabled = PerVtxMaterial * 2;
55static const int Prim = CullFaceEnabled * 2;
56static const int ArrayAccessType = Prim * 2;
57static const int ClippingEnabled = ArrayAccessType * 2;
58}
59
60/********************************************
61 * CGLContext
62 */
63
64class CImmMatrixStack;
66class CMatrixStack;
67
68class CImmLighting;
69class CDListLighting;
70class CLighting;
71
72class CImmGeomManager;
74class CGeomManager;
75
77class CDListManager;
78class CTexManager;
79
80class CImmDrawContext;
82class CDrawContext;
83
84class CDisplayContext;
85
87 CImmMatrixStack *ProjectionMatStack, *ModelViewMatStack;
88 CDListMatrixStack* DListMatStack;
89 CMatrixStack *CurMatrixStack, *SavedCurMatStack;
90
91 CImmLighting* ImmLighting;
92 CDListLighting* DListLighting;
93 CLighting* CurLighting;
94
95 CImmGeomManager* ImmGManager;
96 CDListGeomManager* DListGManager;
97 CGeomManager* CurGManager;
98
99 CMaterialManager* MaterialManager;
100 CDListManager* DListManager;
101 CTexManager* TexManager;
102
103 CImmDrawContext* ImmDrawContext;
104 CDListDrawContext* DListDrawContext;
105 CDrawContext* CurDrawContext;
106
107 CDisplayContext* DisplayContext;
108
109 // state changes
110
111 tU32 RendererContextChanged, SavedRendererContextChanges;
112 tU32 GsContextChanged, SavedGsContextChanges;
113 tU32 RendererPropsChanged, SavedRendererPropsChanges;
114 bool StateChangesArePushed;
115
116 inline void PushStateChanges()
117 {
118 mErrorIf(StateChangesArePushed, "Trying to push state changes when already pushed.");
119 SavedRendererContextChanges = RendererContextChanged;
120 SavedGsContextChanges = GsContextChanged;
121 SavedRendererPropsChanges = RendererPropsChanged;
122 StateChangesArePushed = true;
123 }
124 inline void PopStateChanges()
125 {
126 mErrorIf(!StateChangesArePushed,
127 "Trying to pop state changes that haven't been pushed.");
128 RendererContextChanged = SavedRendererContextChanges;
129 GsContextChanged = SavedGsContextChanges;
130 RendererPropsChanged = SavedRendererPropsChanges;
131 StateChangesArePushed = false;
132 }
133
134 // rendering loop management
135
136 bool IsCurrentFieldEven;
137 unsigned int CurrentFrameNumber;
138
139 // double-buffered dma packets for rendering use
140 static const int kDmaPacketMaxQwordLength = 65000;
141 static CVifSCDmaPacket *CurPacket, *LastPacket,
142 *Vif1Packet, *SavedVif1Packet,
143 *ImmVif1Packet;
144
145 // double-buffered list of draw environment ptrs so that
146 // the dma chains can be reused in different draw buffers,
147 // depth buffers, pixel formats, etc.
148 // If only they didn't pack so many logically distinct
149 // properties into the same registers I wouldn't have to
150 // do this!! Damn!
151 static const int kMaxDrawEnvChanges = 100;
152 void* DrawEnvPtrs0[kMaxDrawEnvChanges];
153 void* DrawEnvPtrs1[kMaxDrawEnvChanges];
154 void **CurDrawEnvPtrs, **LastDrawEnvPtrs;
155 int NumCurDrawEnvPtrs, NumLastDrawEnvPtrs;
156
157 // list of memory to free after this frame is finished
158 static const int kMaxBuffersToBeFreed = 1024;
159 int CurBuffer;
160 void* BuffersToBeFreed[2][kMaxBuffersToBeFreed];
161 int NumBuffersToBeFreed[2];
162
165 static const tU64 Ps2glSignalId = 0xffffffff00000000 | (tU32)'G' << 24 | (tU32)'L' << 16;
166
168 static int RenderingFinishedSemaId, ImmediateRenderingFinishedSemaId, VsyncSemaId;
169
170 static int GsIntHandler(int cause);
171
172 void FreeWaitingBuffersAndSwap();
173
174 void EndVif1Packet(unsigned short signalNum);
175
176 typedef void (*tRenderingFinishedCallback)(void);
177 static tRenderingFinishedCallback RenderingFinishedCallback;
178
179public:
180 CGLContext(int immBufferQwordSize, int immDrawBufferQwordSize);
181 ~CGLContext();
182
183 void SetMatrixMode(GLenum mode);
184 inline CMatrixStack& GetCurMatrixStack() { return *CurMatrixStack; }
185 inline CImmMatrixStack& GetModelViewStack() { return *ModelViewMatStack; }
186 inline CImmMatrixStack& GetProjectionStack() { return *ProjectionMatStack; }
187
188 inline CLighting& GetLighting() { return *CurLighting; }
189 inline CImmLighting& GetImmLighting() { return *ImmLighting; }
190 inline CDListLighting& GetDListLighting() { return *DListLighting; }
191
192 inline CGeomManager& GetGeomManager() { return *CurGManager; }
193 inline CImmGeomManager& GetImmGeomManager() { return *ImmGManager; }
194 inline CDListGeomManager& GetDListGeomManager() { return *DListGManager; }
195
196 inline CMaterialManager& GetMaterialManager() { return *MaterialManager; }
197
198 inline CDListManager& GetDListManager() { return *DListManager; }
199
200 inline CTexManager& GetTexManager() { return *TexManager; }
201
202 inline CDrawContext& GetDrawContext() { return *CurDrawContext; }
203 inline CImmDrawContext& GetImmDrawContext() { return *ImmDrawContext; }
204 inline CDListDrawContext& GetDListDrawContext() { return *DListDrawContext; }
205
206 inline CDisplayContext& GetDisplayContext() { return *DisplayContext; }
207
208 inline bool InDListDef() const { return CurGManager != (CGeomManager*)ImmGManager; }
209 void BeginDListDef(unsigned int listID, GLenum mode);
210 void EndDListDef();
211
212 void BeginImmediateGeometry();
213 void EndImmediateGeometry();
214 void RenderImmediateGeometry();
215 void FinishRenderingImmediateGeometry(bool forceImmediateStop);
216
217 void BeginGeometry();
218 void EndGeometry();
219 void RenderGeometry();
220 void FinishRenderingGeometry(bool forceImmediateStop);
221
222 void AddingDrawEnvToPacket(void* de)
223 {
224 mErrorIf(NumCurDrawEnvPtrs == kMaxDrawEnvChanges,
225 "Too many draw environment changes. Need to increase kMaxDrawEnvChanges");
226 CurDrawEnvPtrs[NumCurDrawEnvPtrs++] = de;
227 }
228 void** GetDrawEnvPtrs() { return LastDrawEnvPtrs; }
229 int GetNumDrawEnvPtrs() const { return NumLastDrawEnvPtrs; }
230
231 bool GetCurrentFieldIsEven() const { return IsCurrentFieldEven; }
232
237 static tU16 GetPs2glSignalId() { return (tU16)(Ps2glSignalId >> 16); }
238
243 inline void AddBufferToBeFreed(void* buf)
244 {
245 mAssert(NumBuffersToBeFreed[CurBuffer] < kMaxBuffersToBeFreed);
246 BuffersToBeFreed[CurBuffer][NumBuffersToBeFreed[CurBuffer]++] = buf;
247 }
248
249 inline static void SetRenderingFinishedCallback(tRenderingFinishedCallback cb)
250 {
251 RenderingFinishedCallback = cb;
252 }
253
254 // state updates
255
256 inline void NumLightsChanged()
257 {
258 RendererContextChanged |= RendererCtxtFlags::NumLights;
259 RendererPropsChanged |= RendererPropFlags::NumLights;
260 }
261 inline void LightPropChanged()
262 {
263 RendererContextChanged |= RendererCtxtFlags::LightPropChanged;
264 }
265 inline void GlobalAmbChanged()
266 {
267 RendererContextChanged |= RendererCtxtFlags::GlobalAmb;
268 }
269 inline void CurMaterialChanged()
270 {
271 RendererContextChanged |= RendererCtxtFlags::CurMaterial;
272 }
273 inline void XformChanged()
274 {
275 RendererContextChanged |= RendererCtxtFlags::Xform;
276 }
277 inline void PrimChanged()
278 {
279 RendererContextChanged |= RendererCtxtFlags::Prim;
280 RendererPropsChanged |= RendererPropFlags::Prim;
281 }
282 inline void ShadingChanged()
283 {
284 RendererContextChanged |= RendererCtxtFlags::Shading;
285 }
286 inline void TexEnabledChanged()
287 {
288 RendererContextChanged |= RendererCtxtFlags::TexEnabled;
289 RendererPropsChanged |= RendererPropFlags::TexEnabled;
290 GsContextChanged |= GsCtxtFlags::Texture;
291 }
292 inline void LightingEnabledChanged()
293 {
294 RendererContextChanged |= RendererCtxtFlags::LightingEnabled;
295 RendererPropsChanged |= RendererPropFlags::LightingEnabled;
296 }
297 inline void BlendEnabledChanged()
298 {
299 RendererContextChanged |= RendererCtxtFlags::AlphaBlending;
300 }
301 inline void DrawEnvChanged()
302 {
303 GsContextChanged |= GsCtxtFlags::DrawEnv;
304 }
305 inline void AlphaTestEnabledChanged()
306 {
307 GsContextChanged |= GsCtxtFlags::DrawEnv;
308 }
309 inline void DepthTestEnabledChanged()
310 {
311 GsContextChanged |= GsCtxtFlags::DrawEnv;
312 }
313 inline void DrawInterlacedChanged()
314 {
315 GsContextChanged |= GsCtxtFlags::DrawEnv;
316 }
317 inline void AlphaTestFuncChanged()
318 {
319 GsContextChanged |= GsCtxtFlags::DrawEnv;
320 }
321 inline void DepthTestFuncChanged()
322 {
323 GsContextChanged |= GsCtxtFlags::DrawEnv;
324 }
325 inline void DepthWriteEnabledChanged()
326 {
327 GsContextChanged |= GsCtxtFlags::DrawEnv;
328 }
329 inline void FrameBufferDrawMaskChanged()
330 {
331 GsContextChanged |= GsCtxtFlags::DrawEnv;
332 }
333 inline void SpecularEnabledChanged()
334 {
335 RendererPropsChanged |= RendererPropFlags::SpecularEnabled;
336 }
337 inline void TextureChanged()
338 {
339 GsContextChanged |= GsCtxtFlags::Texture;
340 }
341 inline void BlendModeChanged()
342 {
343 GsContextChanged |= GsCtxtFlags::DrawEnv;
344 }
345 inline void DrawBufferChanged()
346 {
347 GsContextChanged |= GsCtxtFlags::DrawEnv;
348 }
349 inline void PerVtxMaterialChanged()
350 {
351 RendererPropsChanged |= RendererPropFlags::PerVtxMaterial;
352 }
353 inline void ClippingEnabledChanged()
354 {
355 RendererPropsChanged |= RendererPropFlags::ClippingEnabled;
356 RendererContextChanged |= RendererCtxtFlags::ClippingEnabled;
357 }
358 inline void CullFaceEnabledChanged()
359 {
360 RendererPropsChanged |= RendererPropFlags::CullFaceEnabled;
361 }
362 inline void CullFaceDirChanged()
363 {
364 RendererContextChanged |= RendererCtxtFlags::CullFaceDir;
365 }
366 inline void ArrayAccessChanged()
367 {
368 RendererPropsChanged |= RendererPropFlags::ArrayAccessType;
369 }
370 inline void PolyModeChanged()
371 {
372 RendererContextChanged |= RendererCtxtFlags::Prim;
373 }
374
375 // ps2 rendering
376
377 inline tU32 GetRendererContextChanged() const { return RendererContextChanged; }
378 inline void SetRendererContextChanged(bool changed)
379 {
380 RendererContextChanged = (changed) ? 0xff : 0;
381 }
382
383 inline tU32 GetGsContextChanged() const { return GsContextChanged; }
384 inline void SetGsContextChanged(bool changed)
385 {
386 GsContextChanged = (changed) ? 0xff : 0;
387 }
388
389 inline tU32 GetRendererPropsChanged() const { return RendererPropsChanged; }
390 inline void SetRendererPropsChanged(bool changed)
391 {
392 RendererPropsChanged = (changed) ? 0xff : 0;
393 }
394
395 inline void PushVif1Packet()
396 {
397 mAssert(SavedVif1Packet == NULL);
398 SavedVif1Packet = Vif1Packet;
399 }
400 inline void PopVif1Packet()
401 {
402 mAssert(SavedVif1Packet != NULL);
403 Vif1Packet = SavedVif1Packet;
404 SavedVif1Packet = NULL;
405 }
406 inline void SetVif1Packet(CVifSCDmaPacket& packet) { Vif1Packet = &packet; }
407 inline CVifSCDmaPacket& GetVif1Packet() { return *Vif1Packet; }
408
409 void WaitForVSync();
410 void SwapBuffers();
411};
412
413// global pointer to the GLContext
414extern CGLContext* pGLContext;
415
416#endif // ps2gl_context_h
static tU16 GetPs2glSignalId()
Definition glcontext.h:237
void AddBufferToBeFreed(void *buf)
Definition glcontext.h:243
static int RenderingFinishedSemaId
Semaphores signaled by the gs int handler.
Definition glcontext.h:168
static const tU64 Ps2glSignalId
Definition glcontext.h:165