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 }
167 command |= 0x02000000;
168 return *this;
169 }
170 bool isSemiTrans() const { return command & 0x02000000; }
172 pointA = v;
173 return *this;
174 }
176 pointB = v;
177 return *this;
178 }
180 pointC = v;
181 return *this;
182 }
183 template <Transparency transparency = Transparency::Auto>
184 void interpolateColors(const Color* a, const Color* b, const Color* c) {
185 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(&a->packed);
186 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(&b->packed);
187 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(&c->packed);
188 if constexpr (transparency == Transparency::Auto) {
189 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
190 } else if constexpr (transparency == Transparency::Opaque) {
191 GTE::write<GTE::Register::RGB, GTE::Safe>(0x30000000);
192 } else if constexpr (transparency == Transparency::SemiTransparent) {
193 GTE::write<GTE::Register::RGB, GTE::Safe>(0x32000000);
194 }
195 GTE::Kernels::dpct();
196 GTE::read<GTE::Register::RGB0>(&command);
197 GTE::read<GTE::Register::RGB1>(&colorB.packed);
198 GTE::read<GTE::Register::RGB2>(&colorC.packed);
199 }
200 template <Transparency transparency = Transparency::Auto>
202 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(a.packed);
203 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(b.packed);
204 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(c.packed);
205 if constexpr (transparency == Transparency::Auto) {
206 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
207 } else if constexpr (transparency == Transparency::Opaque) {
208 GTE::write<GTE::Register::RGB, GTE::Safe>(0x30000000);
209 } else if constexpr (transparency == Transparency::SemiTransparent) {
210 GTE::write<GTE::Register::RGB, GTE::Safe>(0x32000000);
211 }
212 GTE::Kernels::dpct();
213 GTE::read<GTE::Register::RGB0>(&command);
214 GTE::read<GTE::Register::RGB1>(&colorB.packed);
215 GTE::read<GTE::Register::RGB2>(&colorC.packed);
216 }
217
218 private:
219 uint32_t command;
220
221 public:
227};
228static_assert(sizeof(GouraudTriangle) == (sizeof(uint32_t) * 6), "GouraudTriangle is not 6 words");
229
245 GouraudTexturedTriangle() : command(0x34000000) {}
246 GouraudTexturedTriangle(Color c) : command(0x34000000 | (c.packed & 0x00ffffff)) {}
248 : command(other.command | (c.packed & 0x00ffffff)) {}
250 uint32_t wasSemiTrans = command & 0x02000000;
251 command = 0x34000000 | (c.packed & 0x00ffffff) | wasSemiTrans;
252 return *this;
253 }
255 colorB = c;
256 return *this;
257 }
259 colorC = c;
260 return *this;
261 }
262 Color getColorA() const { return Color{.packed = command & 0x00ffffff}; }
263 Color getColorB() const { return colorB; }
264 Color getColorC() const { return colorC; }
266 command &= ~0x02000000;
267 return *this;
268 }
270 command |= 0x02000000;
271 return *this;
272 }
273 bool isSemiTrans() const { return command & 0x02000000; }
274 template <Transparency transparency = Transparency::Auto>
275 void interpolateColors(const Color* a, const Color* b, const Color* c) {
276 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(&a->packed);
277 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(&b->packed);
278 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(&c->packed);
279 if constexpr (transparency == Transparency::Auto) {
280 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
281 } else if constexpr (transparency == Transparency::Opaque) {
282 GTE::write<GTE::Register::RGB, GTE::Safe>(0x34000000);
283 } else if constexpr (transparency == Transparency::SemiTransparent) {
284 GTE::write<GTE::Register::RGB, GTE::Safe>(0x36000000);
285 }
286 GTE::Kernels::dpct();
287 GTE::read<GTE::Register::RGB0>(&command);
288 GTE::read<GTE::Register::RGB1>(&colorB.packed);
289 GTE::read<GTE::Register::RGB2>(&colorC.packed);
290 }
291 template <Transparency transparency = Transparency::Auto>
293 GTE::write<GTE::Register::RGB0, GTE::Unsafe>(a.packed);
294 GTE::write<GTE::Register::RGB1, GTE::Unsafe>(b.packed);
295 GTE::write<GTE::Register::RGB2, GTE::Unsafe>(c.packed);
296 if constexpr (transparency == Transparency::Auto) {
297 GTE::write<GTE::Register::RGB, GTE::Safe>(&command);
298 } else if constexpr (transparency == Transparency::Opaque) {
299 GTE::write<GTE::Register::RGB, GTE::Safe>(0x34000000);
300 } else if constexpr (transparency == Transparency::SemiTransparent) {
301 GTE::write<GTE::Register::RGB, GTE::Safe>(0x36000000);
302 }
303 GTE::Kernels::dpct();
304 GTE::read<GTE::Register::RGB0>(&command);
305 GTE::read<GTE::Register::RGB1>(&colorB.packed);
306 GTE::read<GTE::Register::RGB2>(&colorC.packed);
307 }
308
309 private:
310 uint32_t command;
311
312 public:
323};
324static_assert(sizeof(GouraudTexturedTriangle) == (sizeof(uint32_t) * 9), "GouraudTexturedTriangle is not 9 words");
325
326} // namespace Prim
327
328} // namespace psyqo
Definition cdrom-loader.hh:39
char b[9]
Definition string.c:47
A primitive's CLUT command.
Definition common.hh:121
A primitive's tpage attribute.
Definition common.hh:158
A primitive's UV coordinates attribute.
Definition common.hh:233
A primitive's UV coordinates attribute.
Definition common.hh:222
A textured, blended triangle.
Definition triangles.hh:244
Vertex pointB
Definition triangles.hh:317
GouraudTexturedTriangle(const GouraudTexturedTriangle &other, Color c)
Definition triangles.hh:247
GouraudTexturedTriangle & setOpaque()
Definition triangles.hh:265
Vertex pointA
Definition triangles.hh:313
GouraudTexturedTriangle()
Definition triangles.hh:245
GouraudTexturedTriangle & setSemiTrans()
Definition triangles.hh:269
GouraudTexturedTriangle(Color c)
Definition triangles.hh:246
Color getColorB() const
Definition triangles.hh:263
PrimPieces::UVCoordsPadded uvC
Definition triangles.hh:322
Color colorB
Definition triangles.hh:316
GouraudTexturedTriangle & setColorB(Color c)
Definition triangles.hh:254
PrimPieces::TPageAttr tpage
Definition triangles.hh:319
PrimPieces::UVCoords uvA
Definition triangles.hh:314
PrimPieces::ClutIndex clutIndex
Definition triangles.hh:315
GouraudTexturedTriangle & setColorA(Color c)
Definition triangles.hh:249
Vertex pointC
Definition triangles.hh:321
Color getColorC() const
Definition triangles.hh:264
bool isSemiTrans() const
Definition triangles.hh:273
void interpolateColors(Color a, Color b, Color c)
Definition triangles.hh:292
Color getColorA() const
Definition triangles.hh:262
Color colorC
Definition triangles.hh:320
GouraudTexturedTriangle & setColorC(Color c)
Definition triangles.hh:258
PrimPieces::UVCoords uvB
Definition triangles.hh:318
void interpolateColors(const Color *a, const Color *b, const Color *c)
Definition triangles.hh:275
A gouraud-shaded triangle.
Definition triangles.hh:142
GouraudTriangle(const GouraudTriangle &other, Color c)
Definition triangles.hh:145
Vertex pointC
Definition triangles.hh:226
GouraudTriangle & setPointC(Vertex v)
Definition triangles.hh:179
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:222
Color colorC
Definition triangles.hh:225
GouraudTriangle & setColorB(Color c)
Definition triangles.hh:151
GouraudTriangle()
Definition triangles.hh:143
GouraudTriangle & setPointB(Vertex v)
Definition triangles.hh:175
bool isSemiTrans() const
Definition triangles.hh:170
GouraudTriangle(Color c)
Definition triangles.hh:144
GouraudTriangle & setOpaque()
Definition triangles.hh:162
GouraudTriangle & setSemiTrans()
Definition triangles.hh:166
void interpolateColors(Color a, Color b, Color c)
Definition triangles.hh:201
GouraudTriangle & setPointA(Vertex v)
Definition triangles.hh:171
void interpolateColors(const Color *a, const Color *b, const Color *c)
Definition triangles.hh:184
Color getColorB() const
Definition triangles.hh:160
Vertex pointB
Definition triangles.hh:224
GouraudTriangle & setColorC(Color c)
Definition triangles.hh:155
Color colorB
Definition triangles.hh:223
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:121
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