8#include "ps2s/packet.h"
10#include "ps2gl/dlgmanager.h"
11#include "ps2gl/dlist.h"
12#include "ps2gl/glcontext.h"
13#include "ps2gl/immgmanager.h"
14#include "ps2gl/lighting.h"
15#include "ps2gl/material.h"
16#include "ps2gl/renderer.h"
17#include "ps2gl/texture.h"
19using namespace ArrayType;
25CDListGeomManager::CDListGeomManager(
CGLContext& context)
28 , RendererMayHaveChanged(false)
32void CDListGeomManager::BeginDListDef()
34 Prim = GL_INVALID_VALUE;
37 CDList& dlist = GLContext.GetDListManager().GetOpenDList();
40 LastArrayAccessIsValid =
false;
43void CDListGeomManager::EndDListDef()
49 CurTexCoordBuf = NULL;
55void CDListGeomManager::Flush()
57 if (Geometry.IsPending()) {
67void CDListGeomManager::BeginGeom(GLenum mode)
72 CDList& dlist = GLContext.GetDListManager().GetOpenDList();
73 CurNormalBuf = &dlist.GetNormalBuf();
74 CurVertexBuf = &dlist.GetVertexBuf();
75 CurTexCoordBuf = &dlist.GetTexCoordBuf();
76 CurColorBuf = &dlist.GetColorBuf();
78 Geometry.SetPrimType(mode);
79 Geometry.SetArrayType(kLinear);
80 Geometry.SetNormals(CurNormalBuf->GetNextPtr());
81 Geometry.SetVertices(CurVertexBuf->GetNextPtr());
82 Geometry.SetTexCoords(CurTexCoordBuf->GetNextPtr());
83 Geometry.SetColors(CurColorBuf->GetNextPtr());
85 InsideBeginEnd =
true;
88void CDListGeomManager::Vertex(cpu_vec_xyzw newVert)
90 *CurVertexBuf += newVert;
92 Geometry.AddVertices();
105 pGLContext->GetImmGeomManager().Normal(Normal);
106 return CDListCmd::GetNextCmd(
this);
110void CDListGeomManager::Normal(cpu_vec_xyz normal)
115 if (InsideBeginEnd) {
118 *CurNormalBuf += normal(0);
119 *CurNormalBuf += normal(1);
120 *CurNormalBuf += normal(2);
122 Geometry.AddNormals();
133 cpu_vec_xyzw CurColor;
142 pGLContext->GetMaterialManager().Color(CurColor);
143 return CDListCmd::GetNextCmd(
this);
147void CDListGeomManager::Color(cpu_vec_xyzw color)
149 if (InsideBeginEnd) {
150 *CurColorBuf += color;
152 Geometry.AddColors();
154 CDList& dlist = GLContext.GetDListManager().GetOpenDList();
156 GLContext.CurMaterialChanged();
171 pGLContext->GetImmGeomManager().TexCoord(U, V);
172 return CDListCmd::GetNextCmd(
this);
176void CDListGeomManager::TexCoord(
float u,
float v)
178 if (InsideBeginEnd) {
182 *CurTexCoordBuf += u;
183 *CurTexCoordBuf += v;
185 Geometry.AddTexCoords();
192void CDListGeomManager::EndGeom()
194 InsideBeginEnd =
false;
196 Geometry.SetWordsPerVertex(4);
197 Geometry.SetWordsPerNormal(3);
198 Geometry.SetWordsPerTexCoord(2);
199 Geometry.SetWordsPerColor(4);
201 Geometry.SetVerticesAreValid(
true);
207 Geometry.SetNormalsAreValid(
false);
208 if (Geometry.GetNumNewNormals() > 0) {
212 if (Geometry.GetNumNewNormals() > 1) {
213 mErrorIf(Geometry.GetNumNewVertices() != Geometry.GetNumNewNormals(),
214 "Sorry, but in display lists you need to specify either one normal, "
215 "or a normal for each vertex given.");
216 Geometry.SetNormalsAreValid(
true);
221 Geometry.SetTexCoordsAreValid(
false);
222 if (Geometry.GetNumNewTexCoords() > 0) {
226 mErrorIf(Geometry.GetNumNewVertices() != Geometry.GetNumNewTexCoords(),
227 "Sorry, but in display lists you need to specify either one "
228 "texture coord for each vertex given, or zero.");
229 Geometry.SetTexCoordsAreValid(
true);
233 if (Geometry.GetNumNewColors() > 0) {
234 mErrorIf(Geometry.GetNumNewVertices() != Geometry.GetNumNewColors(),
235 "Sorry, but in display lists inside glBegin/glEnd you need "
236 "to specify either one color for each vertex given, or none.");
237 Geometry.SetColorsAreValid(
true);
239 Geometry.SetColorsAreValid(
false);
240 Geometry.SetColors(NULL);
250void CDListGeomManager::DrawArrays(GLenum mode,
int first,
int count)
255 Geometry.SetPrimType(mode);
256 Geometry.SetArrayType(kLinear);
258 Geometry.SetVertices(VertArray->GetVertices());
259 Geometry.SetNormals(VertArray->GetNormals());
260 Geometry.SetTexCoords(VertArray->GetTexCoords());
261 Geometry.SetColors(VertArray->GetColors());
263 Geometry.SetVerticesAreValid(VertArray->GetVerticesAreValid());
264 Geometry.SetNormalsAreValid(VertArray->GetNormalsAreValid());
265 Geometry.SetTexCoordsAreValid(VertArray->GetTexCoordsAreValid());
266 Geometry.SetColorsAreValid(VertArray->GetColorsAreValid());
268 Geometry.SetWordsPerVertex(VertArray->GetWordsPerVertex());
269 Geometry.SetWordsPerNormal(VertArray->GetWordsPerNormal());
270 Geometry.SetWordsPerTexCoord(VertArray->GetWordsPerTexCoord());
271 Geometry.SetWordsPerColor(VertArray->GetWordsPerColor());
273 Geometry.AddVertices(count);
274 Geometry.AddNormals(count);
275 Geometry.AddTexCoords(count);
276 Geometry.AddColors(count);
278 Geometry.AdjustNewGeomPtrs(first);
283void CDListGeomManager::DrawIndexedArrays(GLenum primType,
284 int numIndices,
const unsigned char* indices,
287 if (Prim != primType)
288 PrimChanged(primType);
290 Geometry.SetPrimType(primType);
291 Geometry.SetArrayType(kIndexed);
293 Geometry.SetVertices(VertArray->GetVertices());
294 Geometry.SetNormals(VertArray->GetNormals());
295 Geometry.SetTexCoords(VertArray->GetTexCoords());
296 Geometry.SetColors(VertArray->GetColors());
298 Geometry.SetVerticesAreValid(VertArray->GetVerticesAreValid());
299 Geometry.SetNormalsAreValid(VertArray->GetNormalsAreValid());
300 Geometry.SetTexCoordsAreValid(VertArray->GetTexCoordsAreValid());
301 Geometry.SetColorsAreValid(VertArray->GetColorsAreValid());
303 Geometry.SetWordsPerVertex(VertArray->GetWordsPerVertex());
304 Geometry.SetWordsPerNormal(VertArray->GetWordsPerNormal());
305 Geometry.SetWordsPerTexCoord(VertArray->GetWordsPerTexCoord());
306 Geometry.SetWordsPerColor(VertArray->GetWordsPerColor());
308 Geometry.AddVertices(numVertices);
309 Geometry.AddNormals(numVertices);
310 Geometry.AddTexCoords(numVertices);
311 Geometry.AddColors(numVertices);
313 Geometry.SetNumIndices(numIndices);
314 Geometry.SetIndices(indices);
315 Geometry.SetIStripLengths(NULL);
335 gmanager.SyncRendererContext(PrimType);
336 return CDListCmd::GetNextCmd(
this);
350 pGLContext->GetImmGeomManager().GetRendererManager().PrimChanged(Prim);
351 return CDListCmd::GetNextCmd(
this);
355void CDListGeomManager::PrimChanged(GLenum prim)
357 GLContext.PrimChanged();
358 GLContext.GetDListManager().GetOpenDList() +=
CUpdatePrimCmd(prim);
363 tArrayType ArrayType;
367 : PerVtxColors(pvColors)
374 gmanager.SyncColorMaterial(PerVtxColors);
375 gmanager.SyncArrayType(ArrayType);
376 gmanager.SyncRenderer();
377 return CDListCmd::GetNextCmd(
this);
385 pGLContext->GetImmGeomManager().SyncGsContext();
386 return CDListCmd::GetNextCmd(
this);
394void CDListGeomManager::DrawingLinearArray()
396 if (!LastArrayAccessIsValid || LastArrayAccessWasIndexed) {
397 GLContext.ArrayAccessChanged();
398 LastArrayAccessIsValid =
true;
400 LastArrayAccessWasIndexed =
false;
403void CDListGeomManager::DrawingIndexedArray()
405 if (!LastArrayAccessIsValid || !LastArrayAccessWasIndexed) {
406 GLContext.ArrayAccessChanged();
407 LastArrayAccessIsValid =
true;
409 LastArrayAccessWasIndexed =
true;
412void CDListGeomManager::CommitNewGeom()
415 if (Geometry.GetNewArrayType() == kLinear)
416 DrawingLinearArray();
418 DrawingIndexedArray();
422 if (Geometry.IsPending()) {
425 if (GLContext.GetRendererContextChanged() == 0
426 && GLContext.GetGsContextChanged() == 0
427 && !UserRenderContextChanged
428 && GLContext.GetRendererPropsChanged() == 0
429 && Geometry.MergeNew())
438 Geometry.MakeNewValuesCurrent();
441 if (GLContext.GetRendererPropsChanged()) {
442 *CurDList +=
CUpdateRendererCmd(Geometry.GetColorsAreValid(), Geometry.GetArrayType());
443 GLContext.SetRendererPropsChanged(
false);
444 GLContext.SetRendererContextChanged(
true);
446 if (GLContext.GetRendererContextChanged()) {
448 GLContext.SetRendererContextChanged(
false);
449 Prim = Geometry.GetPrimType();
451 if (GLContext.GetGsContextChanged()) {
453 GLContext.SetGsContextChanged(
false);
455 if (UserRenderContextChanged) {
459 UserRenderContextChanged =
false;
468 CVifSCDmaPacket* RenderPacket;
469 tU64 RenderContextDependencies, RenderContextDepMask;
477 , RenderContextDependencies(0)
478 , RenderContextDepMask(0)
495 tU64 curRenderContext = (tU64)pGLContext->GetImmGeomManager().GetRendererManager().GetRendererReqs();
496 tU64 curRenderContextDeps = curRenderContext & RenderContextDepMask;
497 bool rebuildPacket = (curRenderContextDeps != RenderContextDependencies);
501 if (!IsCached || rebuildPacket) {
507 CRenderer& renderer = gmanager.GetRendererManager().GetCurRenderer();
512 RenderContextDependencies = RenderContextDepMask & curRenderContext;
521 RenderPacket =
new CVifSCDmaPacket(qwords, DMAC::Channels::vif1,
523 Core::MemMappings::UncachedAccl);
524 DList.RegisterNewPacket(RenderPacket);
531 void* newBuf = CDmaPacket::AllocBuffer(qwords,
532 Core::MemMappings::UncachedAccl);
533 void* oldBuf = RenderPacket->SwapOutBuffer(newBuf);
537 RenderPacket->Reset();
542 RenderPacket = &pGLContext->GetVif1Packet();
545 pGLContext->PushVif1Packet();
547 pGLContext->SetVif1Packet(*RenderPacket);
550 pGLContext->PopVif1Packet();
555 RenderPacket->Pad128();
556 RenderPacket->CloseTag();
562 pGLContext->GetVif1Packet().Call(*RenderPacket);
563 pGLContext->GetVif1Packet().Pad128();
564 pGLContext->GetVif1Packet().CloseTag();
570 return CDListCmd::GetNextCmd(
this);
578 CVifSCDmaPacket* RenderPacket;
579 bool IsTexEnabled, IsLightingEnabled;
587 , IsTexEnabled(
false)
588 , IsLightingEnabled(
false)
598 bool texEnabled = pGLContext->GetTexManager().GetTexEnabled();
599 bool lEnabled = pGLContext->GetImmLighting().GetLightingEnabled();
602 bool dontCache = (lEnabled && !Geometry.GetNormalsAreValid());
606 || texEnabled != IsTexEnabled
607 || lEnabled != IsLightingEnabled) {
615 int qwords = Math::Max(Geometry.GetNumArrays(), 1) * 100;
616 RenderPacket =
new CVifSCDmaPacket(qwords, DMAC::Channels::vif1,
618 Core::MemMappings::UncachedAccl);
619 DList.RegisterNewPacket(RenderPacket);
626 int qwords = Math::Max(Geometry.GetNumArrays(), 1) * 100;
627 void* newBuf = CDmaPacket::AllocBuffer(qwords,
628 Core::MemMappings::UncachedAccl);
629 void* oldBuf = RenderPacket->SwapOutBuffer(newBuf);
633 RenderPacket->Reset();
638 RenderPacket = &pGLContext->GetVif1Packet();
641 IsTexEnabled = texEnabled;
642 IsLightingEnabled = lEnabled;
644 pGLContext->PushVif1Packet();
646 pGLContext->SetVif1Packet(*RenderPacket);
648 CRenderer& renderer = gmanager.GetRendererManager().GetCurRenderer();
651 pGLContext->PopVif1Packet();
655 RenderPacket->Pad128();
656 RenderPacket->CloseTag();
661 pGLContext->GetVif1Packet().Call(*RenderPacket);
662 pGLContext->GetVif1Packet().Pad128();
663 pGLContext->GetVif1Packet().CloseTag();
668 return CDListCmd::GetNextCmd(
this);
674 if (block.GetArrayType() == kLinear)
690 pGLContext->GetImmGeomManager().GetRendererManager().EnableCustom(Flag);
691 return CDListCmd::GetNextCmd(
this);
695void CDListGeomManager::EnableCustom(tU64 flag)
710 pGLContext->GetImmGeomManager().GetRendererManager().DisableCustom(Flag);
711 return CDListCmd::GetNextCmd(
this);
715void CDListGeomManager::DisableCustom(tU64 flag)
void AddBufferToBeFreed(void *buf)
virtual bool GetCachePackets(const CGeometryBlock &geometry)=0
virtual void DrawIndexedArrays(CGeometryBlock &block)
Draw arrays of vertices that are accessed by index (i.e., glDrawElements)
virtual void DrawLinearArrays(CGeometryBlock &block)
Draw arrays of vertices that are accessed linearly (i.e., glDrawArrays)
virtual int GetPacketQwordSize(const CGeometryBlock &geometry)=0
virtual CRendererProps GetRenderContextDeps()=0