Skip to content

Commit 2b09f88

Browse files
authored
Merge pull request #18 from pseudophpt/develop
Merge version 1.4.0
2 parents 8539f49 + ad7f087 commit 2b09f88

File tree

6 files changed

+486
-91
lines changed

6 files changed

+486
-91
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
## [Unreleased](https://github.com/pseudophpt/pseultra/compare/master...develop)
44

5+
## [1.4.0](https://github.com/pseudophpt/pseultra/compare/v1.3.0...v1.4.0) - 2019-07-01
6+
7+
### Added
8+
- Shaded triangles
9+
- Ambient lighting
10+
- Diffuse lighting
11+
12+
### Fixed
13+
- Bug in which DzDx was written to DzDy
14+
515
## [1.3.0](https://github.com/pseudophpt/pseultra/compare/v1.2.0...v1.3.0) - 2019-06-09
616

717
### Added

n64/ucode/include/ucode/psm3d.h

+84-20
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,37 @@ typedef struct __attribute__((packed, aligned(8))) uPSM3DDispCmd_t {
3535
/** @brief Vertex for PSM3D */
3636
typedef struct __attribute__((packed, aligned(8))) uPSM3DVtx_t {
3737
/** @brief X-Coordinate Integral (s15.16 fixed point) */
38-
u16 xi;
38+
s16 xi;
3939
/** @brief Y-Coordinate Integral (s15.16 fixed point) */
40-
u16 yi;
40+
s16 yi;
4141
/** @brief Z-Coordinate Integral (s15.16 fixed point) */
42-
u16 zi;
42+
s16 zi;
4343
/** @brief Padding */
44-
u8 pad;
45-
/** @brief X-Normal (s.7 fixed point) */
46-
u8 xn;
44+
u16 pad;
4745
/** @brief X-Coordinate Fractional (s15.16 fixed point) */
48-
u16 xf;
46+
s16 xf;
4947
/** @brief Y-Coordinate Fractional (s15.16 fixed point) */
50-
u16 yf;
48+
s16 yf;
5149
/** @brief Z-Coordinate Fractional (s15.16 fixed point) */
52-
u16 zf;
53-
/** @brief Y-Normal (s.7 fixed point) */
54-
u8 yn;
55-
/** @brief Z-Normal (s.7 fixed point) */
56-
u8 zn;
50+
s16 zf;
51+
/** @brief Padding */
52+
u16 pad2;
5753
/** @brief S coordinate (s10.5) */
58-
u16 s;
54+
s16 s;
5955
/** @brief T coordinate (s10.5) */
60-
u16 t;
61-
/** @brief Padding */
62-
u32 pad2;
56+
s16 t;
6357
/** @brief Padding */
6458
u32 pad3;
59+
/** @brief X-Normal (s.7 fixed point) / R Shading */
60+
s8 xn;
61+
/** @brief Y-Normal (s.7 fixed point) / G Shading */
62+
s8 yn;
63+
/** @brief Z-Normal (s.7 fixed point) / B Shading */
64+
s8 zn;
65+
/** @brief Padding (this padding must be zero) */
66+
u8 pad4;
6567
/** @brief Padding */
66-
u32 pad4;
68+
u32 pad5;
6769
} uPSM3DVtx;
6870

6971
/** @brief Matrix for PSM3D */
@@ -81,11 +83,48 @@ typedef struct __attribute__((packed, aligned(8))) uPSM3DVp_t {
8183
/** @brief Half the viewport height */
8284
u16 hheight;
8385
/** @brief Viewport X offset */
84-
u16 xoff;
86+
s16 xoff;
8587
/** @brief Viewport Y offset */
86-
u16 yoff;
88+
s16 yoff;
8789
} uPSM3DVp;
8890

91+
/** @brief Ambient light structure for PSM3D */
92+
typedef struct __attribute((aligned(8))) uPSM3DAmbientLight_t {
93+
/** @brief Red value of ambient light color */
94+
u8 r;
95+
/** @brief Green value of ambient light color */
96+
u8 g;
97+
/** @brief Blue value of ambient light color */
98+
u8 b;
99+
} uPSM3DAmbientLight;
100+
101+
/** @brief Point light structure for PSM3D */
102+
typedef struct __attribute((aligned(8))) uPSM3DPointLight_t {
103+
/** @brief Red value of point light color */
104+
u8 r;
105+
/** @brief Green value of point light color */
106+
u8 g;
107+
/** @brief Blue value of point light color */
108+
u8 b;
109+
/** @brief Attenuation factor */
110+
u8 att;
111+
/** @brief X value of point light in current coordinate system (MODELVIEW) */
112+
s8 x;
113+
/** @brief Y value of point light in current coordinate system (MODELVIEW) */
114+
s8 y;
115+
/** @brief Z value of point light in current coordinate system (MODELVIEW) */
116+
s8 z;
117+
/** @brief Padding */
118+
u8 pad;
119+
} uPSM3DPointLight;
120+
121+
/** @brief Light structure for PSM3D */
122+
typedef struct __attribute__((packed, aligned(8))) uPSM3DLights_t {
123+
/** @brief Ambient light */
124+
uPSM3DAmbientLight ambient;
125+
/** @brief Point lights */
126+
uPSM3DPointLight point_lights [];
127+
} uPSM3DLights;
89128

90129
#endif
91130

@@ -107,6 +146,7 @@ typedef struct __attribute__((packed, aligned(8))) uPSM3DVp_t {
107146
#define UCODE_PSM3D_OP_TRI 0x07
108147
#define UCODE_PSM3D_OP_SET_VP 0x08
109148
#define UCODE_PSM3D_OP_POP_MTX 0x09
149+
#define UCODE_PSM3D_OP_LOAD_LIGHTS 0x0A
110150

111151
/*
112152
* Operation macros
@@ -138,6 +178,14 @@ typedef struct __attribute__((packed, aligned(8))) uPSM3DVp_t {
138178
#define UCODE_PSM3D_RSPMODE_ZBUF_OFF 0
139179
#define UCODE_PSM3D_RSPMODE_ZBUF_ON 0x400
140180

181+
#define UCODE_PSM3D_RSPMODE_LIGHT 0x800
182+
#define UCODE_PSM3D_RSPMODE_LIGHT_OFF 0
183+
#define UCODE_PSM3D_RSPMODE_LIGHT_ON 0x800
184+
185+
#define UCODE_PSM3D_RSPMODE_SHADE 0x1000
186+
#define UCODE_PSM3D_RSPMODE_SHADE_OFF 0
187+
#define UCODE_PSM3D_RSPMODE_SHADE_ON 0x1000
188+
141189
#define usPSM3DSetRSPMode(mask, data) \
142190
{\
143191
_FMT(UCODE_PSM3D_OP_SET_RSP_MODE, 24, 8) | _FMT(mask, 0, 24)\
@@ -293,6 +341,22 @@ typedef struct __attribute__((packed, aligned(8))) uPSM3DVp_t {
293341
_FMT(vp, 0, 24)\
294342
}
295343

344+
#define usPSM3DLoadLights(lights, count) \
345+
{\
346+
_FMT(UCODE_PSM3D_OP_LOAD_LIGHTS, 24, 8) |\
347+
_FMT(count, 0, 8),\
348+
\
349+
_FMT(lights, 0, 24)\
350+
}
351+
#define uPSM3DLoadLights(dl, lights, count) \
352+
*((dl) ++) = (uPSM3DDispCmd) {\
353+
_FMT(UCODE_PSM3D_OP_LOAD_LIGHTS, 24, 8) |\
354+
_FMT(count, 0, 8),\
355+
\
356+
_FMT(lights, 0, 24)\
357+
}
358+
359+
296360
#define usPSM3DPopMtx() { (UCODE_PSM3D_OP_POP_MTX << 24), 0 }
297361
#define uPSM3DPopMtx(dl) *((dl) ++) = (uPSM3DDispCmd) { _FMT(UCODE_PSM3D_OP_POP_MTX, 24, 8), 0 }
298362

n64/ucode/src/psm3d.sx

+19-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
#define PSM3D_VTX_SIZE 16
3535
#define PSM3D_VTX_COUNT 16
3636

37+
#define PSM3D_AMBIENT_LIGHT_SIZE 8
38+
39+
#define PSM3D_POINT_LIGHT_SIZE 16
40+
#define PSM3D_POINT_LIGHT_COUNT 8
41+
3742
#define v2nconst vc0 // Contains powers of 2
3843
#define v2nconst2 vc1 // Contains powers of 2
3944
#define nullconst vc2 // Contains 0
@@ -66,9 +71,15 @@
6671
#define PSM3D_MODE_CLOCK_CULL 0x200 // Clockwise culling?
6772
#define PSM3D_MODE_COUNTER_CULL 0x100 // Counter-clockwise culling?
6873
#define PSM3D_MODE_ZBUF 0x400 // Z buffering tri calculations enabled
74+
#define PSM3D_MODE_LIGHT 0x800 // Lighting enabled (Uses shading values as normals, not colors)
75+
#define PSM3D_MODE_SHADE 0x1000 // Shading enabled
6976

7077
#define rsp_mode r4
7178

79+
#define light_count r5
80+
81+
#define cmd_addr r6
82+
7283
#define op_ret b .ExecutionLoop
7384

7485
.global uPSM3DTextStart
@@ -144,6 +155,7 @@ uPSM3DTextStart:
144155

145156
.ScalarInit:
146157
or rsp_mode, r0, r0 // Mode = 0
158+
ori cmd_addr, r0, %lo(cmd) // Load command pointer
147159
//
148160
// Begin execution
149161
//
@@ -215,6 +227,7 @@ uPSM3DTextStart:
215227
#include "psm3d/tri.sx"
216228
#include "psm3d/setvp.sx"
217229
#include "psm3d/popmtx.sx"
230+
#include "psm3d/loadlights.sx"
218231

219232
#undef dl_dram_ptr
220233

@@ -231,13 +244,17 @@ uPSM3DTextEnd:
231244
.align 3
232245

233246
// Current display list command: also used as temp memory for subroutines
234-
.lcomm cmd, 0x80
247+
.lcomm cmd, 0x100
235248
// Vertex cache
236249
.lcomm vtx_cache, PSM3D_VTX_SIZE * PSM3D_VTX_COUNT
237250
// Projection matrix
238251
.lcomm mtx_proj, PSM3D_MTX_SIZE
239252
// Modeling matrix stack
240253
.lcomm mtx_stack, PSM3D_MTX_SIZE * PSM3D_MTX_COUNT
254+
// Ambient light
255+
.lcomm ambient_light, PSM3D_AMBIENT_LIGHT_SIZE
256+
// Point lights
257+
.lcomm point_lights, PSM3D_POINT_LIGHT_SIZE * PSM3D_POINT_LIGHT_COUNT
241258
// XBUS buffer
242259
.lcomm xbus_buffer, PSM3D_XBUS_BUFFER_SIZE
243260

@@ -268,6 +285,7 @@ jump_tbl:
268285
.word OPTri
269286
.word OPSetVp
270287
.word OPPopMtx
288+
.word OPLoadLights
271289

272290
.align 4
273291

n64/ucode/src/psm3d/loadlights.sx

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* pseultra/n64/ucode/src/psm3d/loadlights.sx
3+
* PSM3D microcode LoadLights function
4+
*
5+
* (C) pseudophpt 2019
6+
*/
7+
8+
//////////////////////////////////////////////////////////////////////////////
9+
// //
10+
// LoadLights //
11+
// //
12+
// Sets the lighting system lights //
13+
// //
14+
//////////////////////////////////////////////////////////////////////////////
15+
// //
16+
// FORMAT: //
17+
// //
18+
// 05 -- -- PP -- DD DD DD //
19+
// //
20+
// P: Number of point lights //
21+
// D: DRAM address of light struct //
22+
// //
23+
//////////////////////////////////////////////////////////////////////////////
24+
25+
26+
.global OPLoadLights
27+
28+
// Light format:
29+
// Xi, Yi, Zi, Zf, Xf, Yf, RG, BA
30+
31+
OPLoadLights:
32+
33+
lw r24, (%lo(cmd) + 4)($zero) // Load DRAM address
34+
lb light_count, (%lo(cmd) + 3)($zero) // Load number of point lights
35+
sll r16, light_count, 3 // 8 bytes to load for each point light
36+
sll r17, r16, 1 // 16 bytes for each point light as stored
37+
ori r25, $zero, %lo(ambient_light) // Load into light storage
38+
jal DMARead
39+
addiu r26, r16, 7 // Read ambient light info to size
40+
41+
addiu r16, %lo(point_lights) // Add to point light offset
42+
addiu r17, %lo(point_lights)
43+
44+
// Since this loops transforms lights from 8 byte form to 16 bytes, it goes backwards
45+
.TransformLoop:
46+
beq r16, r17, .TransformFinished // If we've translated all the point lights we are finished
47+
48+
lw r18, -8(r16) // Load RGBA
49+
sw r18, -4(r17) // Store RGBA
50+
51+
lpv vc19, 0, -8, r16 // Load position as signed fraction (pointer is aligned to end)
52+
53+
vmov vc19, 7, v2nconst2, v2n2_256 // Load 1 into w (256)
54+
55+
sdv vc19, 8, cmd, $zero // Copy second halves for matrix multiply
56+
ldv vc19, 0, cmd, $zero
57+
58+
vmudm vc8, vc19, v2nconst2, v2n2_256 // Multiply by 1/256 to get signed fraction as double-precision
59+
vmadn vc9, nullconst, nullconst, f // Accumulate fractional result
60+
61+
move r24, mtx_stack_ptr // Model matrix multiply to calculate transformed noral
62+
jal MTXApply4x4
63+
addiu r24, -PSM3D_MTX_SIZE
64+
65+
vmov vc14, 3, vc15, e2 // Move Zf next to Zi
66+
sdv vc14, 0, -16, r17 // Store coordinates
67+
slv vc15, 0, -8, r17
68+
69+
addiu r16, -8 // Go to next (previous) light
70+
b .TransformLoop
71+
addiu r17, -16
72+
73+
74+
.TransformFinished:
75+
76+
op_ret
77+
nop
78+

0 commit comments

Comments
 (0)