1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
struct VSInput { float2 position : POSITION; float2 texCoord : TEXCOORD0; };
struct VSOutput { float4 position : SV_Position; float2 texCoord : TEXCOORD0; };
[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
VSOutput output;
output.position = float4(input.position, 0.0, 1.0);
output.texCoord = input.texCoord;
return output;
}
struct Camera
{
float3 u_cam_pos; float _pad0;
float3 u_cam_right; float _pad1;
float3 u_cam_up; float _pad2;
float3 u_cam_forward; float _pad3;
float u_tan_half_fov;
float u_aspect;
bool u_moving;
int u_output_channel;
int u_raw_output;
};
ConstantBuffer<Camera> cam;
struct Disk
{
float u_inner_radius;
float u_outer_radius;
float u_turbulence;
float u_thickness;
float u_brightness;
float u_temperature;
};
ConstantBuffer<Disk> disk;
struct Objects
{
int u_num_objects;
float4 u_obj_pos_radius[16];
float4 u_obj_color[16];
float u_mass[16];
};
ConstantBuffer<Objects> obj;
struct Simulation
{
int u_max_steps_moving;
int u_max_steps_static;
float u_early_exit_distance;
float u_time;
};
ConstantBuffer<Simulation> sim;
SamplerCube u_HDRIEnvironment;
static const float SagA_rs = 1.269e10;
static const float D_LAMBDA = 1e7;
static const float ESCAPE_R = 1e30;
static const float R_ISCO = 3.0 * SagA_rs;
static const float R_PHOTON = 1.5 * SagA_rs;
static const float FLUX_PEAK = 0.0569;
static const int DEFAULT_MAX_STEPS_MOVING = 12000;
static const int DEFAULT_MAX_STEPS_STATIC = 8000;
static const float DEFAULT_EARLY_EXIT_DISTANCE = 2e12;
static const float MIN_STEP_SIZE = 1e6;
static const float MAX_STEP_SIZE = 2e10;
struct Hit
{
float4 objectColor;
float3 hitCenter;
float hitRadius;
};
float hash(float3 p)
{
p = frac(p * float3(0.1031, 0.1030, 0.0973));
p += dot(p, p.yxz + 33.33);
return frac((p.x + p.y) * p.z);
}
float noise(float3 x)
{
float3 i = floor(x);
float3 fr = frac(x);
float3 u = fr * fr * (3.0 - 2.0 * fr);
float a = hash(i);
float b = hash(i + float3(1.0, 0.0, 0.0));
float c = hash(i + float3(0.0, 1.0, 0.0));
float d = hash(i + float3(1.0, 1.0, 0.0));
float e = hash(i + float3(0.0, 0.0, 1.0));
float f = hash(i + float3(1.0, 0.0, 1.0));
float g = hash(i + float3(0.0, 1.0, 1.0));
float h = hash(i + float3(1.0, 1.0, 1.0));
return lerp(lerp(lerp(a, b, u.x), lerp(c, d, u.x), u.y),
lerp(lerp(e, f, u.x), lerp(g, h, u.x), u.y), u.z);
}
float fbm(float3 x, int octaves)
{
float v = 0.0;
float a = 0.5;
float3 shift = float3(100, 200, 300);
for (int i = 0; i < octaves; ++i)
{
v += a * noise(x);
x = x * 2.0 + shift;
a *= 0.5;
}
return v;
}
float3 Blackbody(float T)
{
T = clamp(T, 1000.0, 40000.0);
float t = T / 100.0;
float3 c;
c.r = (t <= 66.0) ? 1.0
: clamp(1.292936186 * pow(t - 60.0, -0.1332047592), 0.0, 1.0);
c.g = (t <= 66.0) ? clamp(0.3900815788 * log(t) - 0.6318414438, 0.0, 1.0)
: clamp(1.1298908609 * pow(t - 60.0, -0.0755148492), 0.0, 1.0);
c.b = (t >= 66.0) ? 1.0
: (t <= 19.0) ? 0.0
: clamp(0.5432067891 * log(t - 10.0) - 1.1962540891, 0.0, 1.0);
return c;
}
float3 DiskEmission(float3 P, float3 rayDir, out float outG, out float outTemit)
{
outG = 0.0; outTemit = 0.0;
float rc = length(float2(P.x, P.z));
float rin = max(disk.u_inner_radius, R_ISCO);
float rout = disk.u_outer_radius;
if (rc < rin || rc > rout)
return float3(0.0);
float xr = rc / rin;
float flux = max((1.0 - sqrt(1.0 / xr)) / (xr * xr * xr), 0.0);
float Tn = pow(flux / FLUX_PEAK, 0.25);
float Temit = disk.u_temperature * Tn;
float3 rhat = normalize(float3(P.x, 0.0, P.z));
float3 phiHat = normalize(cross(float3(0.0, 1.0, 0.0), rhat));
float v = sqrt((SagA_rs * 0.5) / max(rc - SagA_rs, 1.0));
float3 beta = v * phiHat;
float3 nhat = -normalize(rayDir);
float g = sqrt(max(1.0 - 1.5 * SagA_rs / rc, 0.0)) / max(1.0 - dot(beta, nhat), 1e-3);
outG = g; outTemit = Temit;
float Tobs = g * Temit;
float3 colour = Blackbody(Tobs);
float bright = pow(Tn, 2.0) * pow(g, 4.0);
float edge = smoothstep(rin, rin * 1.12, rc) * (1.0 - smoothstep(rout * 0.88, rout, rc));
bright *= edge;
if (disk.u_turbulence > 0.0)
{
float ang = sim.u_time * 0.3 / sqrt(xr);
float3 rp = float3(P.x * cos(ang) - P.z * sin(ang),
0.0,
P.x * sin(ang) + P.z * cos(ang)) * 1e-10;
float turb = 1.0 + disk.u_turbulence * (fbm(rp * 3.0, 3) - 0.5);
bright *= max(turb, 0.0);
}
return colour * bright * max(disk.u_brightness, 0.0);
}
struct Ray
{
float x, y, z;
float r, theta, phi;
float dr, dtheta, dphi;
float E, L;
};
Ray InitRay(float3 pos, float3 dir)
{
Ray ray;
ray.x = pos.x;
ray.y = pos.y;
ray.z = pos.z;
ray.r = length(pos);
ray.theta = acos(pos.z / ray.r);
ray.phi = atan2(pos.y, pos.x);
float dx = dir.x, dy = dir.y, dz = dir.z;
ray.dr = sin(ray.theta)*cos(ray.phi)*dx +
sin(ray.theta)*sin(ray.phi)*dy +
cos(ray.theta)*dz;
ray.dtheta = (cos(ray.theta)*cos(ray.phi)*dx +
cos(ray.theta)*sin(ray.phi)*dy -
sin(ray.theta)*dz) / ray.r;
ray.dphi = (-sin(ray.phi)*dx + cos(ray.phi)*dy) /
(ray.r * sin(ray.theta));
ray.L = ray.r * ray.r * sin(ray.theta) * ray.dphi;
float f = 1.0 - SagA_rs / ray.r;
float dt_dL = sqrt((ray.dr*ray.dr)/f +
ray.r*ray.r*(ray.dtheta*ray.dtheta +
sin(ray.theta)*sin(ray.theta)*ray.dphi*ray.dphi));
ray.E = f * dt_dL;
return ray;
}
bool Intercept(Ray ray, float rs) { return ray.r <= rs; }
bool InterceptObject(Ray ray, inout Hit hit)
{
float3 P = float3(ray.x, ray.y, ray.z);
for (int i = 0; i < obj.u_num_objects; ++i)
{
float3 center = obj.u_obj_pos_radius[i].xyz;
float radius = obj.u_obj_pos_radius[i].w;
float distSq = dot(P - center, P - center);
if (distSq > radius * radius * 4.0) continue;
if (distSq <= radius * radius)
{
hit.objectColor = obj.u_obj_color[i];
hit.hitCenter = center;
hit.hitRadius = radius;
return true;
}
}
return false;
}
void GeodesicRHS(Ray ray, out float3 d1, out float3 d2)
{
float r = ray.r;
float theta = ray.theta;
float dr = ray.dr;
float dtheta = ray.dtheta;
float dphi = ray.dphi;
float f = 1.0 - SagA_rs / r;
float dt_dL = ray.E / f;
d1 = float3(dr, dtheta, dphi);
d2.x = - (SagA_rs / (2.0 * r*r)) * f * dt_dL * dt_dL
+ (SagA_rs / (2.0 * r*r * f)) * dr * dr
+ r * (dtheta*dtheta + sin(theta)*sin(theta)*dphi*dphi);
d2.y = -2.0*dr*dtheta/r + sin(theta)*cos(theta)*dphi*dphi;
d2.z = -2.0*dr*dphi/r - 2.0*cos(theta)/(sin(theta)) * dtheta * dphi;
}
void RK4Step(inout Ray ray, float dL)
{
float3 k1a, k1b;
GeodesicRHS(ray, k1a, k1b);
ray.r += dL * k1a.x;
ray.theta += dL * k1a.y;
ray.phi += dL * k1a.z;
ray.dr += dL * k1b.x;
ray.dtheta += dL * k1b.y;
ray.dphi += dL * k1b.z;
ray.x = ray.r * sin(ray.theta) * cos(ray.phi);
ray.y = ray.r * sin(ray.theta) * sin(ray.phi);
ray.z = ray.r * cos(ray.theta);
}
float CalculateAdaptiveStepSize(Ray ray, float baseStepSize)
{
float step = 0.02 * max(ray.r - R_PHOTON, 0.0);
float rc = length(float2(ray.x, ray.z));
if (rc < disk.u_outer_radius * 3.0 && abs(ray.y) < disk.u_thickness * 8.0)
step = min(step, disk.u_thickness);
return clamp(step, MIN_STEP_SIZE, MAX_STEP_SIZE);
}
float3 ACESFilm(float3 x)
{
return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
}
float3 Falsecolor(float t)
{
t = clamp(t, 0.0, 1.0);
return clamp(float3(1.5 - abs(4.0 * t - 3.0),
1.5 - abs(4.0 * t - 2.0),
1.5 - abs(4.0 * t - 1.0)), 0.0, 1.0);
}
float3 TracePixel(float2 texCoord, out float outG, out float outTemit, out bool outHitDisk)
{
outG = 0.0; outTemit = 0.0; outHitDisk = false;
float u = (2.0 * texCoord.x - 1.0) * cam.u_aspect * cam.u_tan_half_fov;
float v = (1.0 - 2.0 * texCoord.y) * cam.u_tan_half_fov;
float3 dir = normalize(u * cam.u_cam_right - v * cam.u_cam_up + cam.u_cam_forward);
Ray ray = InitRay(cam.u_cam_pos, dir);
bool hitBlackHole = false;
bool hitObject = false;
Hit hit;
hit.objectColor = float4(0.0);
hit.hitCenter = float3(0.0);
hit.hitRadius = 0.0;
bool hitDisk = false;
float3 diskColor = float3(0.0);
int maxSteps = cam.u_moving ? sim.u_max_steps_moving : sim.u_max_steps_static;
if (maxSteps <= 0)
maxSteps = cam.u_moving ? DEFAULT_MAX_STEPS_MOVING : DEFAULT_MAX_STEPS_STATIC;
float exitDistance = sim.u_early_exit_distance > 0.0 ? sim.u_early_exit_distance : DEFAULT_EARLY_EXIT_DISTANCE;
int objectCheckInterval = 5;
for (int i = 0; i < maxSteps; ++i)
{
if (Intercept(ray, SagA_rs)) { hitBlackHole = true; break; }
if (ray.r > exitDistance || ray.r > ESCAPE_R) break;
float3 prevPos = float3(ray.x, ray.y, ray.z);
float stepSize = CalculateAdaptiveStepSize(ray, D_LAMBDA);
RK4Step(ray, stepSize);
float3 newPos = float3(ray.x, ray.y, ray.z);
{
float H = disk.u_thickness;
bool crossed = prevPos.y * newPos.y < 0.0;
bool inSlab = abs(newPos.y) <= H;
if (crossed || inSlab)
{
float3 hitP = crossed
? lerp(prevPos, newPos, prevPos.y / (prevPos.y - newPos.y))
: newPos;
float rc = length(float2(hitP.x, hitP.z));
if (rc >= max(disk.u_inner_radius, R_ISCO) && rc <= disk.u_outer_radius)
{
diskColor = DiskEmission(hitP, newPos - prevPos, outG, outTemit);
hitDisk = true;
outHitDisk = true;
break;
}
}
}
if (i % objectCheckInterval == 0 && InterceptObject(ray, hit)) { hitObject = true; break; }
if (ray.dr > 0.0 && ray.r > 50.0 * SagA_rs) break;
}
float3 rayDir = normalize(float3(ray.x, ray.y, ray.z) - cam.u_cam_pos);
float footprint = max(length(ddx(rayDir)), length(ddy(rayDir)));
float envLod = clamp(log2(max(footprint / 0.0015, 1.0)), 0.0, 10.0);
float3 shade;
if (hitDisk)
{
shade = diskColor;
}
else if (hitBlackHole)
{
shade = float3(0.0);
}
else if (hitObject)
{
float3 P = float3(ray.x, ray.y, ray.z);
float3 N = normalize(P - hit.hitCenter);
float3 V = normalize(cam.u_cam_pos - P);
float intensity = 0.1 + 0.9 * max(dot(N, V), 0.0);
shade = hit.objectColor.rgb * intensity;
}
else
{
shade = u_HDRIEnvironment.SampleLevel(rayDir, envLod).rgb;
}
return shade;
}
[shader("fragment")]
float4 fragmentMain(VSOutput input) : SV_Target
{
if (cam.u_output_channel != 0)
{
float g, Temit; bool hitDisk;
TracePixel(input.texCoord, g, Temit, hitDisk);
float value; float valid = 1.0;
if (cam.u_output_channel == 3)
{
float uu = (2.0 * input.texCoord.x - 1.0) * cam.u_aspect * cam.u_tan_half_fov;
float vv = (1.0 - 2.0 * input.texCoord.y) * cam.u_tan_half_fov;
float3 dir = normalize(uu * cam.u_cam_right - vv * cam.u_cam_up + cam.u_cam_forward);
value = length(cross(cam.u_cam_pos, dir)) / SagA_rs;
}
else
{
valid = hitDisk ? 1.0 : 0.0;
value = (cam.u_output_channel == 1) ? g : Temit;
}
if (cam.u_raw_output != 0)
return float4(value, value, value, valid);
if (valid < 0.5) return float4(0.0, 0.0, 0.0, 1.0);
float norm = (cam.u_output_channel == 1) ? value / 1.5
: (cam.u_output_channel == 2) ? value / 15000.0
: value / 30.0;
return float4(Falsecolor(norm), 1.0);
}
float _g, _t; bool _hd;
if (cam.u_raw_output != 0)
return float4(TracePixel(input.texCoord, _g, _t, _hd), 1.0);
if (cam.u_moving)
return float4(ACESFilm(TracePixel(input.texCoord, _g, _t, _hd)), 1.0);
float2 dUV = float2(ddx(input.texCoord.x), ddy(input.texCoord.y));
float2 offs[4] = {
float2( 0.125, 0.375), float2( 0.375, -0.125),
float2(-0.125, -0.375), float2(-0.375, 0.125),
};
float3 sum = float3(0.0);
for (int i = 0; i < 4; ++i)
sum += TracePixel(input.texCoord + offs[i] * dUV, _g, _t, _hd);
return float4(ACESFilm(sum * 0.25), 1.0);
}
|