7#include "ps2s/packet.h"
9#include "ps2gl/glcontext.h"
10#include "ps2gl/metrics.h"
11#include "ps2gl/renderermanager.h"
13#include "ps2gl/indexed_renderer.h"
14#include "ps2gl/linear_renderer.h"
16#include "vu1_mem_linear.h"
17#include "vu1renderers.h"
19using namespace RendererProps;
25CRendererManager::CRendererManager(
CGLContext& context)
27 , RendererReqsHaveChanged(false)
29 , CurUserPrimReqMask(~0)
30 , NumDefaultRenderers(0)
32 , CurrentRenderer(NULL)
35 RendererRequirements.PrimType = 0;
36 RendererRequirements.Lighting = 0;
37 RendererRequirements.NumDirLights = 0;
38 RendererRequirements.NumPtLights = 0;
39 RendererRequirements.Texture = 0;
40 RendererRequirements.Specular = 0;
41 RendererRequirements.PerVtxMaterial = kNoMaterial;
42 RendererRequirements.Clipping = kClipped;
43 RendererRequirements.CullFace = 0;
44 RendererRequirements.TwoSidedLighting = 0;
45 RendererRequirements.ArrayAccess = 0;
46 RendererRequirements.UserProps = 0;
54 PrimType : kPtsLinesStripsFans,
56 NumDirLights : k3DirLights | k8DirLights,
57 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
60 PerVtxMaterial : kNoMaterial,
61 Clipping : kNonClipped | kClipped,
64 ArrayAccess : kIndexed
67 RegisterDefaultRenderer(
new CIndexedRenderer(mVsmAddr(Indexed), mVsmSize(Indexed), capabilities, no_reqs, 3, 3,
74 PrimType : kPtsLinesStripsFans,
76 NumDirLights : k3DirLights,
80 PerVtxMaterial : kNoMaterial,
81 Clipping : kNonClipped,
87 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(FastNoLights), mVsmSize(FastNoLights), capabilities, no_reqs, 3, 3,
88 kInputStart, kInputBufSize - kInputStart,
94 PrimType : kPtsLinesStripsFans,
96 NumDirLights : k3DirLights,
100 PerVtxMaterial : kNoMaterial,
101 Clipping : kNonClipped,
103 TwoSidedLighting : 0,
104 ArrayAccess : kLinear
107 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(Fast), mVsmSize(Fast), capabilities, no_reqs, 3, 3,
108 kInputStart, kInputBufSize - kInputStart,
115 PrimType : kPtsLinesStripsFans,
117 NumDirLights : k3DirLights,
121 PerVtxMaterial : kNoMaterial,
124 TwoSidedLighting : 0,
125 ArrayAccess : kLinear
128 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(SCEI), mVsmSize(SCEI), capabilities, no_reqs, 3, 3,
129 kInputStart, kInputBufSize - kInputStart,
137 PrimType : kPtsLinesStripsFans,
139 NumDirLights : k3DirLights | k8DirLights,
140 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
143 PerVtxMaterial : kNoMaterial,
144 Clipping : kNonClipped | kClipped,
146 TwoSidedLighting : 0,
147 ArrayAccess : kLinear
150 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralNoSpec), mVsmSize(GeneralNoSpec), capabilities, no_reqs, 3, 3,
151 kInputStart, kInputBufSize - kInputStart,
152 "linear, no specular"));
156 PrimType : kTriangles,
158 NumDirLights : k3DirLights | k8DirLights,
159 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
162 PerVtxMaterial : kNoMaterial,
163 Clipping : kNonClipped | kClipped,
165 TwoSidedLighting : 0,
166 ArrayAccess : kLinear
169 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralNoSpecTri), mVsmSize(GeneralNoSpecTri), capabilities, no_reqs, 3, 3,
170 kInputStart, kInputBufSize - kInputStart,
171 "linear, tris, no specular"));
177 NumDirLights : k3DirLights | k8DirLights,
178 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
181 PerVtxMaterial : kNoMaterial,
182 Clipping : kNonClipped | kClipped,
184 TwoSidedLighting : 0,
185 ArrayAccess : kLinear
188 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralNoSpecQuad), mVsmSize(GeneralNoSpecQuad), capabilities, no_reqs, 3, 3,
189 kInputStart, kInputBufSize - kInputStart,
190 "linear, quads, no specular"));
197 PrimType : kPtsLinesStripsFans,
199 NumDirLights : k3DirLights | k8DirLights,
200 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
203 PerVtxMaterial : kNoMaterial,
204 Clipping : kNonClipped | kClipped,
206 TwoSidedLighting : 0,
207 ArrayAccess : kLinear
210 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(General), mVsmSize(General), capabilities, no_reqs, 3, 3,
211 kInputStart, kInputBufSize - kInputStart,
218 NumDirLights : k3DirLights | k8DirLights,
219 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
222 PerVtxMaterial : kNoMaterial,
223 Clipping : kNonClipped | kClipped,
225 TwoSidedLighting : 0,
226 ArrayAccess : kLinear
229 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralQuad), mVsmSize(GeneralQuad), capabilities, no_reqs, 3, 3,
230 kInputStart, kInputBufSize - kInputStart,
235 PrimType : kTriangles,
237 NumDirLights : k3DirLights | k8DirLights,
238 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
241 PerVtxMaterial : kNoMaterial,
242 Clipping : kNonClipped | kClipped,
244 TwoSidedLighting : 0,
245 ArrayAccess : kLinear
248 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralTri), mVsmSize(GeneralTri), capabilities, no_reqs, 3, 3,
249 kInputStart, kInputBufSize - kInputStart,
257 PrimType : kPtsLinesStripsFans,
259 NumDirLights : k3DirLights | k8DirLights,
260 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
263 PerVtxMaterial : kDiffuse,
264 Clipping : kNonClipped | kClipped,
266 TwoSidedLighting : 0,
267 ArrayAccess : kLinear
270 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralPVDiff), mVsmSize(GeneralPVDiff), capabilities, no_reqs, 4, 3,
271 kInputStart, kInputBufSize - kInputStart,
276 PrimType : kTriangles,
278 NumDirLights : k3DirLights | k8DirLights,
279 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
282 PerVtxMaterial : kDiffuse,
283 Clipping : kNonClipped | kClipped,
285 TwoSidedLighting : 0,
286 ArrayAccess : kLinear
289 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralPVDiffTri), mVsmSize(GeneralPVDiffTri), capabilities, no_reqs, 4, 3,
290 kInputStart, kInputBufSize - kInputStart,
291 "linear, pvc, tris"));
297 NumDirLights : k3DirLights | k8DirLights,
298 NumPtLights : k1PtLight | k2PtLights | k8PtLights,
301 PerVtxMaterial : kDiffuse,
302 Clipping : kNonClipped | kClipped,
304 TwoSidedLighting : 0,
305 ArrayAccess : kLinear
308 RegisterDefaultRenderer(
new CLinearRenderer(mVsmAddr(GeneralPVDiffQuad), mVsmSize(GeneralPVDiffQuad), capabilities, no_reqs, 4, 3,
309 kInputStart, kInputBufSize - kInputStart,
310 "linear, pvc, quads"));
315 CurrentRenderer = &DefaultRenderers[0];
318void CRendererManager::RegisterDefaultRenderer(
CRenderer* renderer)
320 mErrorIf(NumDefaultRenderers == kMaxDefaultRenderers,
321 "Trying to register too many renderers; adjust the limit");
323 tRenderer newEntry = { renderer->GetCapabilities(), renderer->GetRequirements(), renderer };
324 DefaultRenderers[NumDefaultRenderers++] = newEntry;
327void CRendererManager::RegisterUserRenderer(
CRenderer* renderer)
329 mErrorIf(renderer == NULL,
330 "Trying to register a null renderer is not playing fair...");
332 mErrorIf(NumUserRenderers == kMaxUserRenderers,
333 "Trying to register too many renderers; adjust the limit");
335 tRenderer newEntry = { renderer->GetCapabilities(), renderer->GetRequirements(), renderer };
336 UserRenderers[NumUserRenderers++] = newEntry;
341void CRendererManager::EnableCustom(tU64 flag)
343 tU64 newState = RendererRequirements;
346 if (newState != (tU64)RendererRequirements)
347 RendererReqsHaveChanged =
true;
349 RendererRequirements = newState;
352void CRendererManager::DisableCustom(tU64 flag)
354 tU64 newState = RendererRequirements;
357 if (newState != (tU64)RendererRequirements)
358 RendererReqsHaveChanged =
true;
360 RendererRequirements = newState;
363void CRendererManager::NumLightsChanged(tLightType type,
int num)
370 newState.NumDirLights = 0;
371 else if (0 < num && num <= 3)
372 newState.NumDirLights = k3DirLights;
374 newState.NumDirLights = k8DirLights;
376 if (RendererRequirements != newState) {
377 RendererRequirements = newState;
378 RendererReqsHaveChanged =
true;
384 newState.NumPtLights = 0;
386 newState.NumPtLights = k1PtLight;
388 newState.NumPtLights = k2PtLights;
390 newState.NumPtLights = k8PtLights;
392 if (RendererRequirements != newState) {
393 RendererRequirements = newState;
394 RendererReqsHaveChanged =
true;
403void CRendererManager::PrimChanged(
unsigned int prim)
406 if (CGeomManager::IsUserPrimType(prim)) {
409 tU64 newState = RendererRequirements;
412 newState &= ~CurUserPrimReqs;
417 CurUserPrimReqs = CGeomManager::GetUserPrimRequirements(prim);
418 newState |= CurUserPrimReqs;
420 CurUserPrimReqMask = CGeomManager::GetUserPrimReqMask(prim);
422 if ((tU64)RendererRequirements != newState) {
423 RendererRequirements = newState;
424 RendererReqsHaveChanged =
true;
431 if (CurUserPrimReqs) {
432 RendererReqsHaveChanged =
true;
434 newState = (tU64)newState & ~CurUserPrimReqs;
438 CurUserPrimReqMask = ~(tU32)0;
440 if (prim <= GL_LINE_STRIP)
441 newState.PrimType = kPtsLinesStripsFans;
442 else if (prim == GL_TRIANGLE_STRIP
443 || prim == GL_TRIANGLE_FAN
444 || prim == GL_POLYGON)
445 newState.PrimType = kPtsLinesStripsFans;
446 else if (prim == GL_QUAD_STRIP)
447 newState.PrimType = kPtsLinesStripsFans;
448 else if (prim == GL_TRIANGLES)
449 newState.PrimType = kTriangles;
450 else if (prim == GL_QUADS)
451 newState.PrimType = kQuads;
453 mError(
"shouldn't get here");
456 if (RendererRequirements != newState) {
457 RendererRequirements = newState;
458 RendererReqsHaveChanged =
true;
463void CRendererManager::TexEnabledChanged(
bool enabled)
468 newState.Texture = 1;
470 newState.Texture = 0;
472 if (RendererRequirements != newState) {
473 RendererRequirements = newState;
474 RendererReqsHaveChanged =
true;
478void CRendererManager::LightingEnabledChanged(
bool enabled)
483 newState.Lighting = 1;
485 newState.Lighting = 0;
487 if (RendererRequirements != newState) {
488 RendererRequirements = newState;
489 RendererReqsHaveChanged =
true;
493void CRendererManager::SpecularEnabledChanged(
bool enabled)
498 newState.Specular = 1;
500 newState.Specular = 0;
502 if (RendererRequirements != newState) {
503 RendererRequirements = newState;
504 RendererReqsHaveChanged =
true;
508void CRendererManager::PerVtxMaterialChanged(RendererProps::tPerVtxMaterial matType)
510 if (RendererRequirements.PerVtxMaterial != (
unsigned int)matType) {
511 RendererRequirements.PerVtxMaterial = matType;
512 RendererReqsHaveChanged =
true;
516void CRendererManager::ClippingEnabledChanged(
bool enabled)
518 tClipping clipping = (enabled) ? kClipped : kNonClipped;
519 if (RendererRequirements.Clipping != (
unsigned int)clipping) {
520 RendererRequirements.Clipping = clipping;
521 RendererReqsHaveChanged =
true;
525void CRendererManager::CullFaceEnabledChanged(
bool enabled)
527 if (RendererRequirements.CullFace != enabled) {
528 RendererRequirements.CullFace = enabled;
529 RendererReqsHaveChanged =
true;
533void CRendererManager::ArrayAccessChanged(RendererProps::tArrayAccess accessType)
535 if (RendererRequirements.ArrayAccess != (
unsigned int)accessType) {
536 RendererRequirements.ArrayAccess = accessType;
537 RendererReqsHaveChanged =
true;
549 bool rendererChanged =
false;
551 if (RendererReqsHaveChanged) {
554 if (!rreqs.Lighting) {
557 rreqs.TwoSidedLighting = 0;
560 rreqs = (tU64)rreqs & CurUserPrimReqMask;
565 bool userRendererFound =
false;
567 for (i = 0; i < NumUserRenderers; i++)
568 if (rreqs == (rreqs & UserRenderers[i].capabilities)
569 && UserRenderers[i].requirements == (rreqs & UserRenderers[i].requirements))
573 if (i < NumUserRenderers) {
574 userRendererFound =
true;
576 NewRenderer = &UserRenderers[i];
578 if (CurrentRenderer != &UserRenderers[i]) {
579 rendererChanged =
true;
585 if (!userRendererFound) {
586 for (i = 0; i < NumDefaultRenderers; i++)
587 if (rreqs == (rreqs & DefaultRenderers[i].capabilities)
588 && DefaultRenderers[i].requirements == (rreqs & DefaultRenderers[i].requirements))
591 mErrorIf(i == NumDefaultRenderers,
592 "Couldn't find a suitable renderer..\n"
593 "state reqs = 0x%08x 0x%08x, mask = %08x %08x\n",
594 (tU32)((tU64)rreqs >> 32),
596 (tU32)((tU64)CurUserPrimReqMask >> 32),
597 (tU32)((tU64)CurUserPrimReqMask));
599 NewRenderer = &DefaultRenderers[i];
601 if (CurrentRenderer != &DefaultRenderers[i]) {
604 rendererChanged =
true;
609 RendererReqsHaveChanged =
false;
611 return rendererChanged;
614void CRendererManager::MakeNewRendererCurrent()
616 mAssert(NewRenderer != NULL);
617 CurrentRenderer = NewRenderer;
621void CRendererManager::LoadRenderer(CVifSCDmaPacket& packet)
623 mAssert(CurrentRenderer != NULL);
625 mDebugPrint(
"Loading renderer: %s\n", CurrentRenderer->renderer->
GetName());
627 CurrentRenderer->renderer->
Load();
629 pglAddToMetric(kMetricsRendererUpload);
707 pGLContext->GetImmGeomManager().GetRendererManager().RegisterUserRenderer(newRenderer);
727 return pGLContext->GetImmGeomManager().GetRendererManager().GetCurRenderer().
GetName();
virtual void Load()=0
Load the renderer into vu0/vu1 memory.
virtual const char * GetName()=0
Return a pointer to the text name of this renderer.
void pglRegisterRenderer(void *renderer)
const char * pglGetCurRendererName()
void pglEndRendererDefs()
void pglBeginRendererDefs()