Citro3d
Loading...
Searching...
No Matches
gas.c
Go to the documentation of this file.
1#include "internal.h"
2
3static inline u32 calc_diff(u32 a, u32 b, int pos)
4{
5 float fa = ((a>>pos)&0xFF)/255.0f;
6 float fb = ((b>>pos)&0xFF)/255.0f;
7 float x = fb-fa;
8 u32 diff = 0;
9 if (x < 0)
10 {
11 diff = 0x80;
12 x = -x;
13 }
14 diff |= (u32)(x*0x7F);
15 return diff<<pos;
16}
17
18static inline u32 conv_u8(float x, int pos)
19{
20 if (x < 0.0f) x = 0.0f;
21 else if (x > 1.0f) x = 1.0f;
22 return ((u32)x*255)<<pos;
23}
24
25static inline u32 color_diff(u32 a, u32 b)
26{
27 return calc_diff(a,b,0) | calc_diff(a,b,8) | calc_diff(a,b,16);
28}
29
30void GasLut_FromArray(C3D_GasLut* lut, const u32 data[9])
31{
32 int i;
33 for (i = 0; i <= 8; i ++)
34 {
35 if (i < 8)
36 lut->color[i] = data[i];
37 if (i > 0)
38 lut->diff[i-1] = color_diff(data[i-1], data[i]);
39 }
40}
41
43{
44 C3D_Context* ctx = C3Di_GetContext();
45
46 if (!(ctx->flags & C3DiF_Active))
47 return;
48
50}
51
52void C3D_GasDeltaZ(float value)
53{
54 C3D_Context* ctx = C3Di_GetContext();
55
56 if (!(ctx->flags & C3DiF_Active))
57 return;
58
59 ctx->flags |= C3DiF_Gas;
60 ctx->gasDeltaZ = (u32)(value*0x100);
62}
63
64void C3D_GasAccMax(float value)
65{
66 C3D_Context* ctx = C3Di_GetContext();
67
68 if (!(ctx->flags & C3DiF_Active))
69 return;
70
71 ctx->flags |= C3DiF_Gas;
72 ctx->gasAccMax = f32tof16(1.0f / value);
74}
75
76void C3D_GasAttn(float value)
77{
78 C3D_Context* ctx = C3Di_GetContext();
79
80 if (!(ctx->flags & C3DiF_Active))
81 return;
82
83 ctx->flags |= C3DiF_Gas;
84 ctx->gasAttn = f32tof16(value);
86}
87
88void C3D_GasLightPlanar(float min, float max, float attn)
89{
90 C3D_Context* ctx = C3Di_GetContext();
91
92 if (!(ctx->flags & C3DiF_Active))
93 return;
94
95 ctx->flags |= C3DiF_Gas;
96 ctx->gasLightXY = conv_u8(min,0) | conv_u8(max,8) | conv_u8(attn,16);
98}
99
100void C3D_GasLightView(float min, float max, float attn)
101{
102 C3D_Context* ctx = C3Di_GetContext();
103
104 if (!(ctx->flags & C3DiF_Active))
105 return;
106
107 ctx->flags |= C3DiF_Gas;
108 ctx->gasLightZ = conv_u8(min,0) | conv_u8(max,8) | conv_u8(attn,16);
110}
111
112void C3D_GasLightDirection(float dotp)
113{
114 C3D_Context* ctx = C3Di_GetContext();
115
116 if (!(ctx->flags & C3DiF_Active))
117 return;
118
119 ctx->flags |= C3DiF_Gas;
120 ctx->gasLightZColor &= ~0xFF;
121 ctx->gasLightZColor |= conv_u8(dotp,0);
123}
124
125void C3D_GasLutInput(GPU_GASLUTINPUT input)
126{
127 C3D_Context* ctx = C3Di_GetContext();
128
129 if (!(ctx->flags & C3DiF_Active))
130 return;
131
132 ctx->flags |= C3DiF_Gas;
133 ctx->gasLightZColor &= ~0x100;
134 ctx->gasLightZColor |= (input&1)<<8;
136}
137
138void C3D_GasLutBind(C3D_GasLut* lut)
139{
140 C3D_Context* ctx = C3Di_GetContext();
141
142 if (!(ctx->flags & C3DiF_Active))
143 return;
144
145 if (lut)
146 {
147 ctx->flags |= C3DiF_GasLut;
148 ctx->gasLut = lut;
149 } else
150 ctx->flags &= ~C3DiF_GasLut;
151}
152
154{
155 if (ctx->flags & C3DiF_Gas)
156 {
157 ctx->flags &= ~C3DiF_Gas;
158 u32 gasFlags = ctx->gasFlags;
159 ctx->gasFlags = 0;
160
161 if (gasFlags & C3DiG_BeginAcc)
162 GPUCMD_AddMaskedWrite(GPUREG_GAS_ACCMAX_FEEDBACK, 0x3, 0);
163 if (gasFlags & C3DiG_AccStage)
164 GPUCMD_AddMaskedWrite(GPUREG_GAS_DELTAZ_DEPTH, 0x7, ctx->gasDeltaZ);
165 if (gasFlags & C3DiG_SetAccMax)
166 GPUCMD_AddWrite(GPUREG_GAS_ACCMAX, ctx->gasAccMax);
167 if (gasFlags & C3DiG_RenderStage)
168 {
169 GPUCMD_AddWrite(GPUREG_GAS_ATTENUATION, ctx->gasAttn);
170 GPUCMD_AddWrite(GPUREG_GAS_LIGHT_XY, ctx->gasLightXY);
171 GPUCMD_AddWrite(GPUREG_GAS_LIGHT_Z, ctx->gasLightZ);
172 GPUCMD_AddWrite(GPUREG_GAS_LIGHT_Z_COLOR, ctx->gasLightZColor);
173 }
174 }
175 if (ctx->flags & C3DiF_GasLut)
176 {
177 ctx->flags &= ~C3DiF_GasLut;
178 if (ctx->gasLut)
179 {
180 GPUCMD_AddWrite(GPUREG_GAS_LUT_INDEX, 0);
181 GPUCMD_AddWrites(GPUREG_GAS_LUT_DATA, (u32*)ctx->gasLut, 16);
182 }
183 }
184}
void C3D_GasLutInput(GPU_GASLUTINPUT input)
Definition: gas.c:125
void C3D_GasLightPlanar(float min, float max, float attn)
Definition: gas.c:88
void C3D_GasDeltaZ(float value)
Definition: gas.c:52
void C3D_GasLightView(float min, float max, float attn)
Definition: gas.c:100
void C3D_GasAccMax(float value)
Definition: gas.c:64
void C3D_GasAttn(float value)
Definition: gas.c:76
void C3D_GasBeginAcc(void)
Definition: gas.c:42
void C3Di_GasUpdate(C3D_Context *ctx)
Definition: gas.c:153
void GasLut_FromArray(C3D_GasLut *lut, const u32 data[9])
Definition: gas.c:30
void C3D_GasLutBind(C3D_GasLut *lut)
Definition: gas.c:138
void C3D_GasLightDirection(float dotp)
Definition: gas.c:112
@ C3DiF_Gas
Definition: internal.h:92
@ C3DiF_Active
Definition: internal.h:75
@ C3DiF_GasLut
Definition: internal.h:93
@ C3DiG_BeginAcc
Definition: internal.h:105
@ C3DiG_RenderStage
Definition: internal.h:108
@ C3DiG_SetAccMax
Definition: internal.h:107
@ C3DiG_AccStage
Definition: internal.h:106
u32 flags
Definition: internal.h:38
u32 gasLightZColor
Definition: internal.h:56
u32 gasLightXY
Definition: internal.h:56
u16 gasAttn
Definition: internal.h:55
u16 gasAccMax
Definition: internal.h:55
u32 gasFlags
Definition: internal.h:58
u32 gasDeltaZ
Definition: internal.h:57
C3D_GasLut * gasLut
Definition: internal.h:59
u32 gasLightZ
Definition: internal.h:56
float24Uniform_s * data
Definition: uniforms.c:16