PS2GL
OpenGL*-like API for the PS2
Loading...
Searching...
No Matches
indexed_renderer.cpp
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#include <stdio.h>
8
9#include "ps2s/cpu_matrix.h"
10#include "ps2s/math.h"
11
12#include "ps2gl/glcontext.h"
13#include "ps2gl/indexed_renderer.h"
14#include "ps2gl/lighting.h"
15#include "ps2gl/material.h"
16#include "ps2gl/texture.h"
17
18#include "vu1_mem_indexed.h"
19
20CIndexedRenderer::CIndexedRenderer(void* packet, int packetSize, CRendererProps caps, CRendererProps reqs,
21 int inQuadsPerVert, int outQuadsPerVert,
22 const char* name)
23 : CBaseRenderer(packet, packetSize, caps, reqs,
24 inQuadsPerVert, outQuadsPerVert, kInputGeomStart, name)
25{
26 printf("Max number of vertices in indexed array is %d\n", (kInputBufSize - 4) / 3 - 3);
27 printf("giftag = 0x%04x, context length = 0x%04x (%d)\n",
28 kGifTag, kContextLength, kContextLength);
29}
30
31void CIndexedRenderer::InitContext(GLenum primType, tU32 rcChanges, bool userRcChanged)
32{
33 CGLContext& glContext = *pGLContext;
34 CVifSCDmaPacket& packet = glContext.GetVif1Packet();
35
36 packet.Cnt();
37 {
38 AddVu1RendererContext(packet, primType, kContextStart);
39
40 packet.Mscal(0);
41 packet.Flushe();
42
43 packet.Base(kDoubleBufBase);
44 packet.Offset(kDoubleBufOffset);
45 }
46 packet.CloseTag();
47
48 CacheRendererState();
49
50 // more caching: calculate and save the current constant vertex color
51
52 CImmLighting& lighting = glContext.GetImmLighting();
53 bool doLighting = lighting.GetLightingEnabled();
54
55 float maxColorValue = GetMaxColorValue(XferTexCoords);
56 cpu_vec_4 globalAmb;
57 if (doLighting)
58 globalAmb = lighting.GetGlobalAmbient() * maxColorValue;
59 else
60 globalAmb.set(0, 0, 0, 0);
61
62 CImmMaterial& material = glContext.GetMaterialManager().GetImmMaterial();
63 cpu_vec_4 materialAmb = material.GetAmbient();
64
65 cpu_vec_4 materialEmm;
66 if (doLighting)
67 materialEmm = material.GetEmission() * maxColorValue;
68 else
69 materialEmm = glContext.GetMaterialManager().GetCurColor() * maxColorValue;
70
71 ConstantVertColor = materialAmb * globalAmb + materialEmm;
72}
73
75{
76 // FIXME: this is a pitiful hack for allocating enough memory
77 return Math::Max(geometry.GetTotalVertices() / 70, 1) * 1000;
78}
79
82{
83 CRendererProps deps;
84 deps = (tU64)0;
85 deps.Lighting = 1;
86 deps.Texture = 1;
87 deps.PerVtxMaterial = 1;
88
89 return deps;
90}
91
93{
94 return !(pGLContext->GetImmLighting().GetLightingEnabled()
95 && !geometry.GetNormalsAreValid());
96}
97
99{
100 // TODO:
101 // - transfer strip lengths into temp area w's and set strip adc's
102 // - test with real data..
103
104 // transfer the arrays
105
106 CVifSCDmaPacket& packet = pGLContext->GetVif1Packet();
107
108 int wordsPerVert = block.GetWordsPerVertex();
109 int wordsPerNormal = (block.GetNormalsAreValid()) ? block.GetWordsPerNormal() : 0;
110 int wordsPerTex = (block.GetTexCoordsAreValid()) ? block.GetWordsPerTexCoord() : 0;
111 int wordsPerColor = (block.GetColorsAreValid()) ? block.GetWordsPerColor() : 0;
112
113 InitXferBlock(packet, wordsPerVert, wordsPerNormal, wordsPerTex, wordsPerColor);
114
115 for (int curArray = 0; curArray < block.GetNumArrays(); curArray++) {
116
117 const void *normals, *vertices, *texCoords, *colors;
118 vertices = block.GetVerticesAreValid() ? block.GetVertices(curArray) : NULL;
119 normals = block.GetNormalsAreValid() ? block.GetNormals(curArray) : NULL;
120 texCoords = block.GetTexCoordsAreValid() ? block.GetTexCoords(curArray) : NULL;
121 colors = block.GetColorsAreValid() ? block.GetColors(curArray) : NULL;
122
123 packet.Cnt();
124 packet.Stcycl(1, 3).Nop();
125 packet.CloseTag();
126
127 int numIndices = block.GetNumIndices(curArray);
128 const void* indices = block.GetIndices(curArray);
129 int numVertices = block.GetArrayLength(curArray);
130
131 XferBlock(packet,
132 vertices, normals, texCoords, colors,
133 kInputGeomStart,
134 0, numVertices);
135
136 // transfer the indices
137
138 packet.Cnt();
139 {
140 packet.Stmod(Vifs::AddModes::kOffset);
141 // don't write over the xyz's
142 Vifs::tMask mask = { 3, 3, 3, 0,
143 3, 3, 3, 0,
144 3, 3, 3, 0,
145 3, 3, 3, 0 };
146 packet.Stmask(mask);
147 static const float row[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
148 packet.Strow(row);
149 packet.Pad128();
150 }
151 packet.CloseTag();
152
153 int numIndexQwords = numIndices / 16 + (numIndices % 16 > 0);
154 packet.Ref(Core::MakePtrNormal((const unsigned int*)indices), numIndexQwords);
155 {
156 packet.Stcycl(1, 1);
157 packet.OpenUnpack(Vifs::UnpackModes::s_16, kInputGeomStart,
158 Packet::kDoubleBuff, Packet::kMasked);
159 // packet.CloseUnpack( numIndices/2 );
160 packet.CloseUnpack(numIndexQwords * 8);
161 }
162
163 // transfer a buffer header & start renderer
164
165 packet.Cnt();
166 {
167 // buffer header
168
169 packet.Stmod(Vifs::AddModes::kNone);
170 packet.OpenUnpack(Vifs::UnpackModes::v4_32, 0, Packet::kDoubleBuff);
171 {
172 packet += numVertices;
173 packet += numIndices / 2 + (numIndices & 1);
174 packet += numIndices;
175 packet += 0;
176 }
177 packet.CloseUnpack();
178
179 // constant color of each vertex
180
181 packet.Strow(&ConstantVertColor);
182 packet.Stcycl(numVertices, 0);
183 Vifs::tMask mask = { 1, 1, 1, 3,
184 1, 1, 1, 3,
185 1, 1, 1, 3,
186 1, 1, 1, 3 };
187 packet.Stmask(mask);
188 packet.OpenUnpack(Vifs::UnpackModes::v4_32, kTempAreaStart,
189 Packet::kDoubleBuff, Packet::kMasked);
190 packet.CloseUnpack(numVertices);
191
192 // start renderer
193
194 packet.Mscnt();
195 packet.Pad128();
196 }
197 packet.CloseTag();
198 }
199}
void XferBlock(CVifSCDmaPacket &packet, const void *vertices, const void *normals, const void *texCoords, const void *colors, int vu1Offset, int firstElement, int numToAdd)
void InitXferBlock(CVifSCDmaPacket &packet, int wordsPerVertex, int wordsPerNormal, int wordsPerTex, int wordsPerColor)
virtual int GetPacketQwordSize(const CGeometryBlock &geometry)
virtual void DrawIndexedArrays(CGeometryBlock &block)
Draw arrays of vertices that are accessed by index (i.e., glDrawElements)
virtual void InitContext(GLenum primType, tU32 rcChanges, bool userRcChanged)
virtual CRendererProps GetRenderContextDeps()
virtual bool GetCachePackets(const CGeometryBlock &geometry)