Skip to content

Commit 0659eaf

Browse files
committed
Add ISPC samples
1 parent 8a648b4 commit 0659eaf

File tree

3 files changed

+586
-0
lines changed

3 files changed

+586
-0
lines changed

samples/ISPC/VideoFunctions.ispc

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
export struct VideoData {
2+
uint16 width;
3+
uint16 height;
4+
uint16 buffer_width;
5+
uint16 buffer_height;
6+
int16 yoffset;
7+
int16 xoffset;
8+
int16 ytail;
9+
int16 xtail;
10+
bool transparentBlack;
11+
int16 transparentBlackLevel;
12+
int16 image_width;
13+
int16 image_height;
14+
int32 startx;
15+
int32 starty;
16+
int32 sampleSpacing;
17+
18+
int8 ch;
19+
uint8* image;
20+
void* bufferData;
21+
};
22+
23+
export void VideoEffectProcess(const uniform VideoData &data,
24+
uniform int startIdx, uniform int endIdx,
25+
uniform uint8<4> result[])
26+
{
27+
uniform int ch = data.ch;
28+
29+
30+
foreach (index = startIdx...endIdx) {
31+
float fidx = index;
32+
float y = (floor)(fidx / data.buffer_width);
33+
float x = fidx - (y * data.buffer_width);
34+
int newIndex = x + data.startx + ((y + data.starty) * data.buffer_width);
35+
36+
uint8* ptr = data.image + (data.image_height - 1 - y - data.yoffset) * (data.image_width * ch) + (data.xoffset * ch) + (x * ch);
37+
uint8<4> c;
38+
c.x = *(ptr);
39+
c.y = *(ptr + 1);
40+
c.z = *(ptr + 2);
41+
c.w = ch == 3 ? 255 : *(ptr + 3);
42+
uint16 level = (uint16)c[0] + (uint16)c[1] + (uint16)c[2];
43+
if (data.transparentBlack) {
44+
if (level > data.transparentBlackLevel) {
45+
result[newIndex] = c;
46+
}
47+
} else {
48+
result[newIndex] = c;
49+
}
50+
}
51+
}
52+
53+
export void VideoEffectProcessSample(const uniform VideoData &data,
54+
uniform int startIdx, uniform int endIdx,
55+
uniform uint8<4> result[]) {
56+
uniform float width = data.width;
57+
uniform float rszw = 1.0 / width;
58+
uniform float height = data.height;
59+
uniform int ch = data.ch;
60+
61+
foreach (index = startIdx ... endIdx) {
62+
float fidx = index;
63+
float y = (floor)(fidx * rszw + 0.00001);
64+
float x = fidx - (y * width);
65+
float newIndex = (y * width) + x;
66+
67+
int curx = data.startx + x * data.sampleSpacing;
68+
int cury = data.starty + y * data.sampleSpacing;
69+
70+
if (curx >= 0 && curx < data.image_width && cury >= 0 && cury < data.image_height) {
71+
72+
uint8* ptr = data.image + (data.image_height - 1 - cury) * data.image_width * ch + curx * ch;
73+
uint8<4> c;
74+
c.x = *(ptr);
75+
c.y = *(ptr + 1);
76+
c.z = *(ptr + 2);
77+
c.w = ch == 3 ? 255 : *(ptr + 3);
78+
if (data.transparentBlack) {
79+
uint16 level = (uint16)c[0] + (uint16)c[1] + (uint16)c[2];
80+
if (level > data.transparentBlackLevel) {
81+
result[newIndex] = c;
82+
}
83+
} else {
84+
result[newIndex] = c;
85+
}
86+
}
87+
}
88+
}

samples/ISPC/program_city.ispc

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
// https://www.shadertoy.com/view/XlsyWB
2+
3+
const float streetDistance = 0.6;
4+
const vec3 streetColor = { 4.0, 8.0, 10.0 };
5+
6+
const float fogDensity = 0.5;
7+
const float fogDistance = 4.0;
8+
const vec3 fogColor = { 0.34, 0.37, 0.4 };
9+
10+
const float windowSize = 0.1;
11+
const float windowDivergence = 0.2;
12+
const vec3 windowColor = { 0.1, 0.2, 0.5 };
13+
14+
const float beaconProb = 0.0003;
15+
const float beaconFreq = 0.6;
16+
const vec3 beaconColor = { 1.5, 0.2, 0.0 };
17+
18+
19+
const float tau = 6.283185;
20+
21+
float hash1(vec2 p2) {
22+
p2 = fract(p2 * vec2(5.3983, 5.4427));
23+
p2 += dot(p2.yx, p2.xy + vec2(21.5351, 14.3137));
24+
return fract(p2.x * p2.y * 95.4337);
25+
}
26+
27+
float hash1(vec2 p2, float p) {
28+
vec3 p3 = fract(vec3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p));
29+
p3 += dot(p3, p3.yzx + 19.19);
30+
return fract((p3.x + p3.y) * p3.z);
31+
}
32+
33+
vec2 hash2(vec2 p2) {
34+
vec3 p3 = fract(vec3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p2.x));
35+
p3 += dot(p3, p3.yzx + 19.19);
36+
return fract((p3.xx + p3.yz) * p3.zy);
37+
}
38+
39+
vec2 hash2(vec2 p2, float p) {
40+
vec3 p3 = fract(vec3(5.3983 * p2.x, 5.4427 * p2.y, 6.9371 * p));
41+
p3 += dot(p3, p3.yzx + 19.19);
42+
return fract((p3.xx + p3.yz) * p3.zy);
43+
}
44+
45+
vec3 hash3(vec2 p2) {
46+
vec3 p3 = fract(vec3(p2.xyx) * vec3(5.3983, 5.4427, 6.9371));
47+
p3 += dot(p3, p3.yxz + 19.19);
48+
return fract((p3.xxy + p3.yzz) * p3.zyx);
49+
}
50+
51+
float noise1(vec2 p) {
52+
vec2 i = floor(p);
53+
vec2 f = fract(p);
54+
vec2 u = f * f * (3.0 - 2.0 * f);
55+
return mix(mix(hash1(i + vec2(0.0, 0.0)),
56+
hash1(i + vec2(1.0, 0.0)), u.x),
57+
mix(hash1(i + vec2(0.0, 1.0)),
58+
hash1(i + vec2(1.0, 1.0)), u.x), u.y);
59+
}
60+
61+
vec4 castRay(vec3 eye, vec3 ray) {
62+
vec2 block = floor(eye.xy);
63+
vec3 ri = 1.0 / ray;
64+
vec3 rs = sign(ray);
65+
vec3 side = 0.5 + 0.5 * rs;
66+
vec2 ris = ri.xy * rs.xy;
67+
vec2 dis = (block - eye.xy + 0.5 + rs.xy * 0.5) * ri.xy;
68+
69+
float beacon = 0.0;
70+
71+
for (int i = 0; i < 200; ++i) {
72+
vec2 lo0 = vec2(block + 0.01);
73+
vec2 loX = vec2(0.3, 0.3);
74+
vec2 hi0 = vec2(block + 0.69);
75+
vec2 hiX = vec2(0.3, 0.3);
76+
float height = (0.5 + hash1(block)) * (2.0 + 4.0 * pow(noise1(0.1 * block), 2.5));
77+
78+
float dist = 500.0;
79+
float face = 0.0;
80+
for (int j = 0; j < 3; ++j) {
81+
float top = height * (1.0 - 0.1 * float(j));
82+
vec3 lo = vec3(lo0 + loX * hash2(block, float(j)), 0.0);
83+
vec3 hi = vec3(hi0 + hiX * hash2(block, float(j) + 0.5), top);
84+
85+
vec3 wall = mix(hi, lo, side);
86+
vec3 t = (wall - eye) * ri;
87+
88+
vec3 dim = step(t.zxy, t) * step(t.yzx, t);
89+
float maxT = dot(dim, t);
90+
float maxFace = 1.0 - dim.z;
91+
92+
vec3 p = eye + maxT * ray;
93+
dim += step(lo, p) * step(p, hi);
94+
if (dim.x * dim.y * dim.z > 0.5 && maxT < dist) {
95+
dist = maxT;
96+
face = maxFace;
97+
}
98+
}
99+
100+
float prob = beaconProb * pow(height, 3.0);
101+
vec2 h = hash2(block);
102+
if (h.x < prob) {
103+
vec3 center = vec3(block + 0.5, height + 0.2);
104+
float t = dot(center - eye, ray);
105+
if (t < dist) {
106+
vec3 p = eye + t * ray;
107+
float fog = (exp(-p.z / fogDistance) - exp(-eye.z / fogDistance)) / ray.z;
108+
fog = exp(fogDensity * fog);
109+
110+
t = distance(center, p);
111+
fog *= smoothstep(1.0, 0.5, cos(tau * (beaconFreq * iTime + h.y)));
112+
beacon += fog * pow(clamp(1.0 - 2.0 * t, 0.0, 1.0), 4.0);
113+
}
114+
}
115+
116+
if (dist < 400.0) {
117+
return vec4(dist, beacon, face, 1.0);
118+
}
119+
120+
float t = eye.z * ri.z;
121+
vec3 p = eye - t * ray;
122+
vec2 g = p.xy - block;
123+
if (g.x > 0.0 && g.x < 1.0 && g.y > 0.0 && g.y < 1.0) {
124+
return vec4(-t, beacon, 0.0, 1.0);
125+
}
126+
127+
vec2 dim = step(dis.xy, dis.yx);
128+
dis += dim * ris;
129+
block += dim * rs.xy;
130+
}
131+
132+
if (ray.z < 0.0) {
133+
return vec4(-eye.z * ri.z, beacon, 0.0, 1.0);
134+
}
135+
136+
return vec4(0.0, beacon, 0.0, 0.0);
137+
}
138+
139+
inline void mainImage(vec4& fragColor, vec2 fragCoord)
140+
{
141+
vec2 m = vec2(0.03 * iTime, 0.8);
142+
if (iMouse.z > 0.0)
143+
m = iMouse.xy / iResolution.xy;
144+
m *= tau * vec2(1.0, 0.25);
145+
146+
vec3 center = vec3(6.0 * iTime, 0.5, 3.0);
147+
float dist = 20.0;
148+
vec3 eye = center + vec3(dist * sin(m.x) * sin(m.y), dist * cos(m.x) * sin(m.y), dist * cos(m.y));
149+
float zoom = 3.0;
150+
151+
vec3 forward = normalize(center - eye);
152+
vec3 right = normalize(cross(forward, vec3(0.0, 0.0, 1.0)));
153+
vec3 up = cross(right, forward);
154+
vec2 xy = 2.0 * fragCoord - iResolution.xy;
155+
zoom *= iResolution.y;
156+
vec3 ray = normalize(xy.x * right + xy.y * up + zoom * forward);
157+
158+
vec4 res = castRay(eye, ray);
159+
vec3 p = eye + res.x * ray;
160+
161+
vec2 block = floor(p.xy);
162+
vec3 window = floor(p / windowSize);
163+
float x = hash1(block, window.x);
164+
float y = hash1(block, window.y);
165+
float z = hash1(block, window.z);
166+
vec3 color = windowColor + windowDivergence * (hash3(block) - 0.5);
167+
color *= smoothstep(0.1, 0.9, fract(2.5 * (x * y * z)));
168+
169+
vec3 streetLevel = streetColor * exp(-p.z / streetDistance);
170+
color += streetLevel;
171+
color = clamp(mix(0.25 * streetLevel, color, res.z), 0.0, 1.0);
172+
173+
float fog = (exp(-p.z / fogDistance) - exp(-eye.z / fogDistance)) / ray.z;
174+
fog = exp(fogDensity * fog);
175+
color = mix(fogColor, color, fog);
176+
177+
color = mix(fogColor, color, res.w);
178+
color += res.y * beaconColor;
179+
color += pow(res.y, 2.0);
180+
181+
fragColor = vec4(color, 1.0);
182+
}

0 commit comments

Comments
 (0)