Nugget
Loading...
Searching...
No Matches
triangles.hh
Go to the documentation of this file.
1/*
2
3MIT License
4
5Copyright (c) 2022 PCSX-Redux authors
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26
27#pragma once
28
29#include <stdint.h>
30
31#include "psyqo/gte-kernels.hh"
34
35namespace psyqo {
36
37namespace Prim {
38
45struct Triangle {
46 Triangle() : command(0x20000000) {}
47 Triangle(Color c) : command(0x20000000 | (c.packed & 0x00ffffff)) {}
48 Triangle(const Triangle& other, Color c) : command(other.command | (c.packed & 0x00ffffff)) {}
50 uint32_t wasSemiTrans = command & 0x02000000;
51 command = 0x20000000 | (c.packed & 0x00ffffff) | wasSemiTrans;
52 return *this;
53 }
54 Color getColor() const { return Color{.packed = command & 0x00ffffff}; }
56 command &= ~0x02000000;
57 return *this;
58 }
60 command |= 0x02000000;
61 return *this;
62 }
63 bool isSemiTrans() const { return command & 0x02000000; }
65 pointA = v;
66 return *this;
67 }
69 pointB = v;
70 return *this;
71 }
73 pointC = v;
74 return *this;
75 }
76
77 private:
78 uint32_t command;
79
80 public:
84};
85static_assert(sizeof(Triangle) == (sizeof(uint32_t) * 4), "Triangle is not 4 words");
86
100 TexturedTriangle() : command(0x24808080) {}
101 TexturedTriangle(Color c) : command(0x24000000 | (c.packed & 0x00ffffff)) {}
102 TexturedTriangle(const TexturedTriangle& other, Color c) : command(other.command | (c.packed & 0x00ffffff)) {}
104 uint32_t wasSemiTrans = command & 0x02000000;
105 command = 0x24000000 | (c.packed & 0x00ffffff) | wasSemiTrans;
106 return *this;
107 }
108 Color getColor() const { return Color{.packed = command & 0x00ffffff}; }
110 command &= ~0x02000000;
111 return *this;
112 }
114 command |= 0x02000000;
115 return *this;
116 }
117 bool isSemiTrans() const { return command & 0x02000000; }
118
119 private:
120 uint32_t command;
121
122 public:
131};
132static_assert(sizeof(TexturedTriangle) == (sizeof(uint32_t) * 7), "TexturedTriangle is not 7 words");
133
143 GouraudTriangle() : command(0x30000000) {}
144 GouraudTriangle(Color c) : command(0x30000000 | (c.packed & 0x00ffffff)) {}
145 GouraudTriangle(const GouraudTriangle& other, Color c) : command(other.command | (c.packed & 0x00ffffff)) {}
147 uint32_t wasSemiTrans = command & 0x02000000;
148 command = 0x30000000 | (c.packed & 0x00ffffff) | wasSemiTrans;
149 return *this;
150 }
152 colorB = c;
153 return *this;
154 }
156 colorC = c;
157 return *this;
158 }
159 Color getColorA() const { return Color{.packed = command & 0x00ffffff}; }
160 Color getColorB() const { return colorB; }
161 Color getColorC() const { return colorC; }
163 command &= ~0x02000000;
164 return *this;
165 }
173 uint32_t getCommandWord() const { return command & 0xff000000; }
182 command = packed;
183 return *this;
184 }
186 command |= 0x02000000;
187 return *this;
188 }
189 bool isSemiTrans() const { return command & 0x02000000; }
191 pointA = v;
192 return *this;
193 }
195 pointB = v;
196 return *this;
197 }
199 pointC = v;
200 return *this;
201 }
202 template <Transparency transparency = Transparency::Auto>
203 void interpolateColors(const Color* a, const Color* b, const Color* c) {
204 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(&a->packed);
205 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(&b->packed);
206 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(&c->packed);
207 if constexpr (transparency == Transparency::Auto) {
208 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
209 } else if constexpr (transparency == Transparency::Opaque) {
210 GTE::write<GTE::Register::RGB, GTE::Safe>(0x30000000);
211 } else if constexpr (transparency == Transparency::SemiTransparent) {
212 GTE::write<GTE::Register::RGB, GTE::Safe>(0x32000000);
213 }
214 GTE::Kernels::dpct();
215 GTE::read<GTE::Register::RGB0>(&command);
216 GTE::read<GTE::Register::RGB1>(&colorB.packed);
217 GTE::read<GTE::Register::RGB2>(&colorC.packed);
218 }
219 template <Transparency transparency = Transparency::Auto>
221 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(a.packed);
222 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(b.packed);
223 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(c.packed);
224 if constexpr (transparency == Transparency::Auto) {
225 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
226 } else if constexpr (transparency == Transparency::Opaque) {
227 GTE::write<GTE::Register::RGB, GTE::Safe>(0x30000000);
228 } else if constexpr (transparency == Transparency::SemiTransparent) {
229 GTE::write<GTE::Register::RGB, GTE::Safe>(0x32000000);
230 }
231 GTE::Kernels::dpct();
232 GTE::read<GTE::Register::RGB0>(&command);
233 GTE::read<GTE::Register::RGB1>(&colorB.packed);
234 GTE::read<GTE::Register::RGB2>(&colorC.packed);
235 }
236
237 private:
238 uint32_t command;
239
240 public:
246};
247static_assert(sizeof(GouraudTriangle) == (sizeof(uint32_t) * 6), "GouraudTriangle is not 6 words");
248
264 GouraudTexturedTriangle() : command(0x34000000) {}
265 GouraudTexturedTriangle(Color c) : command(0x34000000 | (c.packed & 0x00ffffff)) {}
267 : command(other.command | (c.packed & 0x00ffffff)) {}
269 uint32_t wasSemiTrans = command & 0x02000000;
270 command = 0x34000000 | (c.packed & 0x00ffffff) | wasSemiTrans;
271 return *this;
272 }
274 colorB = c;
275 return *this;
276 }
278 colorC = c;
279 return *this;
280 }
281 Color getColorA() const { return Color{.packed = command & 0x00ffffff}; }
282 Color getColorB() const { return colorB; }
283 Color getColorC() const { return colorC; }
285 command &= ~0x02000000;
286 return *this;
287 }
295 uint32_t getCommandWord() const { return command & 0xff000000; }
304 command = packed;
305 return *this;
306 }
308 command |= 0x02000000;
309 return *this;
310 }
311 bool isSemiTrans() const { return command & 0x02000000; }
312 template <Transparency transparency = Transparency::Auto>
313 void interpolateColors(const Color* a, const Color* b, const Color* c) {
314 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(&a->packed);
315 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(&b->packed);
316 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(&c->packed);
317 if constexpr (transparency == Transparency::Auto) {
318 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
319 } else if constexpr (transparency == Transparency::Opaque) {
320 GTE::write<GTE::Register::RGB, GTE::Safe>(0x34000000);
321 } else if constexpr (transparency == Transparency::SemiTransparent) {
322 GTE::write<GTE::Register::RGB, GTE::Safe>(0x36000000);
323 }
324 GTE::Kernels::dpct();
325 GTE::read<GTE::Register::RGB0>(&command);
326 GTE::read<GTE::Register::RGB1>(&colorB.packed);
327 GTE::read<GTE::Register::RGB2>(&colorC.packed);
328 }
329 template <Transparency transparency = Transparency::Auto>
331 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(a.packed);
332 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(b.packed);
333 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(c.packed);
334 if constexpr (transparency == Transparency::Auto) {
335 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
336 } else if constexpr (transparency == Transparency::Opaque) {
337 GTE::write<GTE::Register::RGB, GTE::Safe>(0x34000000);
338 } else if constexpr (transparency == Transparency::SemiTransparent) {
339 GTE::write<GTE::Register::RGB, GTE::Safe>(0x36000000);
340 }
341 GTE::Kernels::dpct();
342 GTE::read<GTE::Register::RGB0>(&command);
343 GTE::read<GTE::Register::RGB1>(&colorB.packed);
344 GTE::read<GTE::Register::RGB2>(&colorC.packed);
345 }
346
347 private:
348 uint32_t command;
349
350 public:
361};
362static_assert(sizeof(GouraudTexturedTriangle) == (sizeof(uint32_t) * 9), "GouraudTexturedTriangle is not 9 words");
363
364} // namespace Prim
365
366} // namespace psyqo
uint8_t b
Definition gte-depthcue.c:39
Definition lua.hh:38
A primitive's CLUT command.
Definition common.hh:121
A primitive's tpage attribute.
Definition common.hh:188
A primitive's UV coordinates attribute.
Definition common.hh:295
A primitive's UV coordinates attribute.
Definition common.hh:284
A textured, blended triangle.
Definition triangles.hh:263
Vertex pointB
Definition triangles.hh:355
GouraudTexturedTriangle(const GouraudTexturedTriangle &other, Color c)
Definition triangles.hh:266
GouraudTexturedTriangle & setOpaque()
Definition triangles.hh:284
Vertex pointA
Definition triangles.hh:351
GouraudTexturedTriangle()
Definition triangles.hh:264
GouraudTexturedTriangle & setSemiTrans()
Definition triangles.hh:307
GouraudTexturedTriangle(Color c)
Definition triangles.hh:265
Color getColorB() const
Definition triangles.hh:282
PrimPieces::UVCoordsPadded uvC
Definition triangles.hh:360
Color colorB
Definition triangles.hh:354
GouraudTexturedTriangle & setColorB(Color c)
Definition triangles.hh:273
PrimPieces::TPageAttr tpage
Definition triangles.hh:357
PrimPieces::UVCoords uvA
Definition triangles.hh:352
PrimPieces::ClutIndex clutIndex
Definition triangles.hh:353
GouraudTexturedTriangle & setColorA(Color c)
Definition triangles.hh:268
Vertex pointC
Definition triangles.hh:359
GouraudTexturedTriangle & setColorAPacked(uint32_t packed)
Sets the command word and vertex A's colour in one go.
Definition triangles.hh:303
Color getColorC() const
Definition triangles.hh:283
bool isSemiTrans() const
Definition triangles.hh:311
void interpolateColors(Color a, Color b, Color c)
Definition triangles.hh:330
Color getColorA() const
Definition triangles.hh:281
Color colorC
Definition triangles.hh:358
uint32_t getCommandWord() const
The GP0 command word, minus any colour.
Definition triangles.hh:295
GouraudTexturedTriangle & setColorC(Color c)
Definition triangles.hh:277
PrimPieces::UVCoords uvB
Definition triangles.hh:356
void interpolateColors(const Color *a, const Color *b, const Color *c)
Definition triangles.hh:313
A gouraud-shaded triangle.
Definition triangles.hh:142
GouraudTriangle(const GouraudTriangle &other, Color c)
Definition triangles.hh:145
Vertex pointC
Definition triangles.hh:245
GouraudTriangle & setPointC(Vertex v)
Definition triangles.hh:198
uint32_t getCommandWord() const
The GP0 command word, minus any colour.
Definition triangles.hh:173
Color getColorC() const
Definition triangles.hh:161
GouraudTriangle & setColorA(Color c)
Definition triangles.hh:146
Color getColorA() const
Definition triangles.hh:159
Vertex pointA
Definition triangles.hh:241
Color colorC
Definition triangles.hh:244
GouraudTriangle & setColorB(Color c)
Definition triangles.hh:151
GouraudTriangle()
Definition triangles.hh:143
GouraudTriangle & setPointB(Vertex v)
Definition triangles.hh:194
bool isSemiTrans() const
Definition triangles.hh:189
GouraudTriangle(Color c)
Definition triangles.hh:144
GouraudTriangle & setOpaque()
Definition triangles.hh:162
GouraudTriangle & setSemiTrans()
Definition triangles.hh:185
void interpolateColors(Color a, Color b, Color c)
Definition triangles.hh:220
GouraudTriangle & setPointA(Vertex v)
Definition triangles.hh:190
GouraudTriangle & setColorAPacked(uint32_t packed)
Sets the command word and vertex A's colour in one go.
Definition triangles.hh:181
void interpolateColors(const Color *a, const Color *b, const Color *c)
Definition triangles.hh:203
Color getColorB() const
Definition triangles.hh:160
Vertex pointB
Definition triangles.hh:243
GouraudTriangle & setColorC(Color c)
Definition triangles.hh:155
Color colorB
Definition triangles.hh:242
A textured triangle.
Definition triangles.hh:99
Vertex pointB
Definition triangles.hh:126
Color getColor() const
Definition triangles.hh:108
TexturedTriangle(const TexturedTriangle &other, Color c)
Definition triangles.hh:102
PrimPieces::ClutIndex clutIndex
Definition triangles.hh:125
PrimPieces::TPageAttr tpage
Definition triangles.hh:128
TexturedTriangle & setSemiTrans()
Definition triangles.hh:113
PrimPieces::UVCoords uvA
Definition triangles.hh:124
PrimPieces::UVCoords uvB
Definition triangles.hh:127
bool isSemiTrans() const
Definition triangles.hh:117
Vertex pointA
Definition triangles.hh:123
TexturedTriangle & setColor(Color c)
Definition triangles.hh:103
TexturedTriangle(Color c)
Definition triangles.hh:101
Vertex pointC
Definition triangles.hh:129
PrimPieces::UVCoordsPadded uvC
Definition triangles.hh:130
TexturedTriangle()
Definition triangles.hh:100
TexturedTriangle & setOpaque()
Definition triangles.hh:109
A flat-colored triangle.
Definition triangles.hh:45
Triangle & setSemiTrans()
Definition triangles.hh:59
Triangle & setPointA(Vertex v)
Definition triangles.hh:64
Triangle()
Definition triangles.hh:46
Color getColor() const
Definition triangles.hh:54
Vertex pointC
Definition triangles.hh:83
Triangle(const Triangle &other, Color c)
Definition triangles.hh:48
Triangle & setPointB(Vertex v)
Definition triangles.hh:68
Triangle(Color c)
Definition triangles.hh:47
Vertex pointB
Definition triangles.hh:82
bool isSemiTrans() const
Definition triangles.hh:63
Triangle & setOpaque()
Definition triangles.hh:55
Vertex pointA
Definition triangles.hh:81
Triangle & setPointC(Vertex v)
Definition triangles.hh:72
Triangle & setColor(Color c)
Definition triangles.hh:49
static int c
Definition syscalls.h:122
void uint32_t(classId, spec)
The Color struct.
Definition common.hh:91
uint32_t packed
Definition common.hh:95
The Vertex struct.
Definition common.hh:47