PSYQo
primitives.hh
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2022 PCSX-Redux authors
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 
27 #pragma once
28 
29 #include <stdint.h>
30 
31 namespace psyqo {
32 
47 union Vertex {
48  struct {
49  union {
50  int16_t x, u, w;
51  };
52  union {
53  int16_t y, v, h;
54  };
55  };
56  int32_t packed;
57 };
58 static_assert(sizeof(Vertex) == sizeof(uint32_t), "Vertex is not 32 bits");
59 
74 struct Rect {
75  union {
76  Vertex a, pos;
77  };
78  union {
79  Vertex b, size;
80  };
81 };
82 static_assert(sizeof(Rect) == sizeof(uint64_t), "Rect is not 64 bits");
83 
90 union Color {
91  struct {
92  uint8_t r, g, b;
93  };
94  uint32_t packed;
95 };
96 static_assert(sizeof(Color) == sizeof(uint32_t), "Color is not 32 bits");
97 
98 namespace Prim {
99 
111 struct ClutIndex {
112  ClutIndex() {}
113  ClutIndex(Vertex v) : ClutIndex(v.x >> 4, v.y) {}
114  ClutIndex(uint16_t x, uint16_t y) : index((y << 6) | x) {}
115 
116  private:
117  uint16_t index = 0;
118 };
119 static_assert(sizeof(ClutIndex) == sizeof(uint16_t), "ClutIndex is not 16 bits");
120 
128 struct TexInfo {
129  uint8_t u;
130  uint8_t v;
131  ClutIndex clut;
132 };
133 static_assert(sizeof(TexInfo) == sizeof(uint32_t), "TexInfo is not 32 bits");
134 
145 struct FastFill {
146  FastFill() : command(0x02000000) {}
147  FastFill(Color c) : command(0x02000000 | c.packed) {}
148  void setColor(Color c) { command = 0x02000000 | c.packed; }
149 
150  private:
151  uint32_t command;
152 
153  public:
154  Rect rect;
155 };
156 static_assert(sizeof(FastFill) == (sizeof(uint32_t) * 3), "FastFill is not 96 bits");
157 
168 struct Sprite {
169  Sprite() : command(0b01100100'00000000'00000000'00000000) {}
170  Sprite(Color c) : command(0b01100100'00000000'00000000'00000000 | c.packed) {}
171  void setColor(Color c) { command = 0b01100100'00000000'00000000'00000000 | c.packed; }
172 
173  private:
174  uint32_t command;
175 
176  public:
177  Vertex position;
178  TexInfo texInfo;
179  Vertex size;
180 };
181 static_assert(sizeof(Sprite) == (sizeof(uint32_t) * 4), "Sprite is not 128 bits");
182 
191 struct Pixel {
192  Pixel() : command(0b01101000'00000000'00000000'00000000) {}
193  Pixel(Color c) : command(0b01101000'00000000'00000000'00000000 | c.packed) {}
194  void setColor(Color c) { command = 0b01101000'00000000'00000000'00000000 | c.packed; }
195 
196  private:
197  uint32_t command;
198 
199  public:
200  Vertex position;
201 };
202 static_assert(sizeof(Pixel) == (sizeof(uint64_t)), "Pixel is not 64 bits");
203 
212 struct FlushCache {
213  FlushCache() : command(0x01000000) {}
214 
215  private:
216  uint32_t command;
217 };
218 static_assert(sizeof(FlushCache) == sizeof(uint32_t), "FlushCache is not 32 bits");
219 
229  DrawingAreaStart(Vertex p) : command(0xe3000000 | p.x | (p.y << 10)) {}
230 
231  private:
232  uint32_t command;
233 };
234 static_assert(sizeof(DrawingAreaStart) == sizeof(uint32_t), "DrawingAreaStart is not 32 bits");
235 
245  DrawingAreaEnd(Vertex p) : command(0xe4000000 | (p.x - 1) | ((p.y - 1) << 10)) {}
246 
247  private:
248  uint32_t command;
249 };
250 static_assert(sizeof(DrawingAreaEnd) == sizeof(uint32_t), "DrawingAreaEnd is not 32 bits");
251 
261  DrawingOffset(Vertex p) : command(0xe5000000 | p.x | (p.y << 11)) {}
262 
263  private:
264  uint32_t command;
265 };
266 static_assert(sizeof(DrawingOffset) == sizeof(uint32_t), "DrawingOffset is not 32 bits");
267 
278 struct Scissor {
279  DrawingAreaStart start = DrawingAreaStart(Vertex{{.x = 0, .y = 0}});
280  DrawingAreaEnd end = DrawingAreaEnd(Vertex{{.x = 1024, .y = 512}});
281  DrawingOffset offset = DrawingOffset(Vertex{{.x = 0, .y = 0}});
282 };
283 
290 struct VRAMUpload {
291  VRAMUpload() : command(0xa0000000) {}
292 
293  private:
294  uint32_t command;
295 
296  public:
297  Rect region;
298 };
299 static_assert(sizeof(VRAMUpload) == (sizeof(uint32_t) * 3), "VRAMUpload is not 96 bits");
300 
301 } // namespace Prim
302 
303 } // namespace psyqo
A primitive's CLUT command.
Definition: primitives.hh:111
The DrawingAreaEnd primitive.
Definition: primitives.hh:244
The DrawingAreaStart primitive.
Definition: primitives.hh:228
The DrawingOffset primitive.
Definition: primitives.hh:260
The FastFill primitive.
Definition: primitives.hh:145
The FlushCache primitive.
Definition: primitives.hh:212
The Pixel primitive.
Definition: primitives.hh:191
A compounded Scissor primitive.
Definition: primitives.hh:278
The Sprite primitive.
Definition: primitives.hh:168
A primitive's texture information.
Definition: primitives.hh:128
Initiates a VRAM upload.
Definition: primitives.hh:290
The Rect struct.
Definition: primitives.hh:74
The Color struct.
Definition: primitives.hh:90
The Vertex struct.
Definition: primitives.hh:47