forked from baldavenger/DCTLs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArri_LogC_to_Rec709_with_Grain.dctl
345 lines (283 loc) · 11 KB
/
Arri_LogC_to_Rec709_with_Grain.dctl
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
// Arri LogC to Rec.709 plus Grain DCTL
__CONSTANT__ bool Add_Grain = true; // Add Grain (true or false)
__CONSTANT__ bool Coloured = false; // Coloured noise (true or false)
__CONSTANT__ float Scale = 1.0f; // Grain plate resolution
__CONSTANT__ float Intensity = 2.0f; // Grain contrast
__CONSTANT__ float PermTexUnit = 1.0f / 256.0f; // Perm texture texel-size
__CONSTANT__ float PermTexUnitHalf = 0.5f / 256.0f; // Half perm texture texel-size
__DEVICE__ float3 add(float3 A, float B)
{
float3 C;
C.x = A.x + B;
C.y = A.y + B;
C.z = A.z + B;
return C;
}
__DEVICE__ float2 add(float2 A, float2 B)
{
float2 C;
C.x = A.x + B.x;
C.y = A.y + B.y;
return C;
}
__DEVICE__ float3 add(float3 A, float3 B)
{
float3 C;
C.x = A.x + B.x;
C.y = A.y + B.y;
C.z = A.z + B.z;
return C;
}
__DEVICE__ float dot(float2 A, float2 B)
{
float C = A.x * B.x + A.y * B.y;
return C;
}
__DEVICE__ float dot(float3 A, float3 B)
{
float C = A.x * B.x + A.y * B.y + A.z * B.z;
return C;
}
__DEVICE__ float fract(float A)
{
float B;
B = A - floor(A);
return B;
}
__DEVICE__ float3 fract(float3 A)
{
float3 B;
B.x = A.x - _floor(A.x);
B.y = A.y - _floor(A.y);
B.z = A.z - _floor(A.z);
return B;
}
__DEVICE__ float3 floor(float3 A)
{
float3 B;
B.x = _floor(A.x);
B.y = _floor(A.y);
B.z = _floor(A.z);
return B;
}
__DEVICE__ float3 Make_Float3(float A)
{
float3 B;
B.x = A;
B.y = A;
B.z = A;
return B;
}
__DEVICE__ float3 Make_Float3(float2 A, float B)
{
float3 C;
C.x = A.x;
C.y = A.y;
C.z = B;
return C;
}
__DEVICE__ float3 minus(float3 A, float B)
{
float3 C;
C.x = A.x - B;
C.y = A.y - B;
C.z = A.z - B;
return C;
}
__DEVICE__ float3 minus(float3 A, float3 B)
{
float3 C;
C.x = A.x - B.x;
C.y = A.y - B.y;
C.z = A.z - B.z;
return C;
}
__DEVICE__ float mix(float A, float B, float C)
{
float D = A * (1.0f - C) + B * C;
return D;
}
__DEVICE__ float2 mix(float2 A, float2 B, float C)
{
float2 D;
D.x = A.x * (1.0f - C) + B.x * C;
D.y = A.y * (1.0f - C) + B.y * C;
return D;
}
__DEVICE__ float3 mix(float3 A, float3 B, float C)
{
float3 D;
D.x = A.x * (1.0f - C) + B.x * C;
D.y = A.y * (1.0f - C) + B.y * C;
D.z = A.z * (1.0f - C) + B.z * C;
return D;
}
__DEVICE__ float4 mix(float4 A, float4 B, float C)
{
float4 D;
D.x = A.x * (1.0f - C) + B.x * C;
D.y = A.y * (1.0f - C) + B.y * C;
D.z = A.z * (1.0f - C) + B.z * C;
D.w = A.w * (1.0f - C) + B.w * C;
return D;
}
__DEVICE__ float2 multi(float2 A, float B)
{
float2 C;
C.x = A.x * B;
C.y = A.y * B;
return C;
}
__DEVICE__ float2 multi(float2 A, float2 B)
{
float2 C;
C.x = A.x * B.x;
C.y = A.y * B.y;
return C;
}
__DEVICE__ float3 multi(float3 A, float B)
{
float3 C;
C.x = A.x * B;
C.y = A.y * B;
C.z = A.z * B;
return C;
}
__DEVICE__ float smoothstep(float edge0, float edge1, float x)
{
float t = _saturatef((x - edge0) / (edge1 - edge0));
return t * t * (3.0f - 2.0f * t);
}
//a random texture generator, but you can also use a pre-computed perturbation texture
__DEVICE__ float4 rnm(float2 tc, float timer)
{
float noise = _sinf(dot(add(tc, make_float2(timer,timer)), make_float2(12.9898f, 78.233f))) * 43758.5453f;
float noiseR = fract(noise) * 2.0f - 1.0f;
float noiseG = fract(noise * 1.2154f) * 2.0f - 1.0f;
float noiseB = fract(noise * 1.3453f) * 2.0f - 1.0f;
float noiseA = fract(noise * 1.3647f) * 2.0f - 1.0f;
return make_float4(noiseR, noiseG, noiseB, noiseA);
}
__DEVICE__ float rand(float2 co)
{
float a = 12.9898f;
float b = 78.233f;
float c = 43758.5453f;
float dt = dot(co, make_float2(a,b));
float sn = _fmod(dt, 3.14f);
return fract(_sinf(sn) * c);
}
//good for large clumps of smooth looking noise, but too repetitive
//for small grains
__DEVICE__ float fastNoise(float2 n)
{
const float2 d = make_float2(0.0f, 1.0f);
float2 b, f;
b.x = _floor(n.x);
b.y = _floor(n.y);
f.x = smoothstep(0.0f, 1.0f, fract(n.x));
f.y = smoothstep(0.0f, 1.0f, fract(n.y));
float D1 = mix(mix(rand(b), rand(add(b, make_float2(d.y, d.x))), f.x), mix(rand(add(b, d)), rand(add(b, make_float2(d.y, d.y))), f.x), f.y);
return D1;
}
__DEVICE__ float fade(float t)
{
return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
}
__DEVICE__ float pnoise3D(float3 p, float timer)
{
float3 pi = add(multi(floor(p), PermTexUnit), PermTexUnitHalf); // Integer part, scaled so +1 moves PermTexUnit texel
// and offset 1/2 texel to sample texel centers
float3 pf = fract(p); // Fractional part for interpolation
// Noise contributions from (x=0, y=0), z=0 and z=1
float perm00 = rnm(make_float2(pi.x, pi.y), timer).w ;
float3 grad000 = minus(multi(make_float3(rnm(make_float2(perm00, pi.z), timer).x, rnm(make_float2(perm00, pi.z), timer).y, rnm(make_float2(perm00, pi.z), timer).z), 4.0f), 1.0f);
float n000 = dot(grad000, pf);
float3 grad001 = minus(multi(make_float3(rnm(make_float2(perm00, pi.z + PermTexUnit), timer).x, rnm(make_float2(perm00, pi.z + PermTexUnit), timer).y, rnm(make_float2(perm00, pi.z + PermTexUnit), timer).z), 4.0f), 1.0f);
float n001 = dot(grad001, minus(pf, make_float3(0.0f, 0.0f, 1.0f)));
// Noise contributions from (x=0, y=1), z=0 and z=1
float perm01 = rnm(add(make_float2(pi.x, pi.y), make_float2(0.0f, PermTexUnit)), timer).w ;
float3 grad010 = minus(multi(make_float3(rnm(make_float2(perm01, pi.z), timer).x, rnm(make_float2(perm01, pi.z), timer).y, rnm(make_float2(perm01, pi.z), timer).z), 4.0f), 1.0f);
float n010 = dot(grad010, minus(pf, make_float3(0.0f, 1.0f, 0.0f)));
float3 grad011 = minus(multi(make_float3(rnm(make_float2(perm01, pi.z + PermTexUnit), timer).x, rnm(make_float2(perm01, pi.z + PermTexUnit), timer).y, rnm(make_float2(perm01, pi.z + PermTexUnit), timer).z), 4.0f), 1.0f);
float n011 = dot(grad011, minus(pf, make_float3(0.0f, 1.0f, 1.0f)));
// Noise contributions from (x=1, y=0), z=0 and z=1
float perm10 = rnm(add(make_float2(pi.x, pi.y), make_float2(PermTexUnit, 0.0f)), timer).w;
float3 grad100 = minus(multi(make_float3(rnm(make_float2(perm10, pi.z), timer).x, rnm(make_float2(perm10, pi.z), timer).y, rnm(make_float2(perm10, pi.z), timer).z), 4.0f), 1.0f);
float n100 = dot(grad100, minus(pf, make_float3(1.0f, 0.0f, 0.0f)));
float3 grad101 = minus(multi(make_float3(rnm(make_float2(perm10, pi.z + PermTexUnit), timer).x, rnm(make_float2(perm10, pi.z + PermTexUnit), timer).y, rnm(make_float2(perm10, pi.z + PermTexUnit), timer).z), 4.0f), 1.0f);
float n101 = dot(grad101, minus(pf, make_float3(1.0f, 0.0f, 1.0f)));
// Noise contributions from (x=1, y=1), z=0 and z=1
float perm11 = rnm(add(make_float2(pi.x, pi.y), make_float2(PermTexUnit, PermTexUnit)), timer).w;
float3 grad110 = minus(multi(make_float3(rnm(make_float2(perm11, pi.z), timer).x, rnm(make_float2(perm11, pi.z), timer).y, rnm(make_float2(perm11, pi.z), timer).z), 4.0f), 1.0f);
float n110 = dot(grad110, minus(pf, make_float3(1.0f, 1.0f, 0.0f)));
float3 grad111 = minus(multi(make_float3(rnm(make_float2(perm11, pi.z + PermTexUnit), timer).x, rnm(make_float2(perm11, pi.z + PermTexUnit), timer).y, rnm(make_float2(perm11, pi.z + PermTexUnit), timer).z), 4.0f), 1.0f);
float n111 = dot(grad111, minus(pf, make_float3(1.0f, 1.0f, 1.0f)));
// Blend contributions along x
float4 n_x = mix(make_float4(n000, n001, n010, n011), make_float4(n100, n101, n110, n111), fade(pf.x));
// Blend contributions along y
float2 n_xy = mix(make_float2(n_x.x, n_x.y), make_float2(n_x.z, n_x.w), fade(pf.y));
// Blend contributions along z
float n_xyz = mix(n_xy.x, n_xy.y, fade(pf.z));
// We're done, return the final noise value.
return n_xyz;
}
//2d coordinate orientation thing
__DEVICE__ float2 coordRot(float2 tc, float angle, float width, float height)
{
float aspect = width/height;
float rotX = ((tc.x * 2.0f - 1.0f) * aspect * _cosf(angle)) - ((tc.y * 2.0f - 1.0f) * _sinf(angle));
float rotY = ((tc.y * 2.0f - 1.0f) * _cosf(angle)) + ((tc.x * 2.0f - 1.0f) * aspect * _sinf(angle));
rotX = ((rotX / aspect) * 0.5f + 0.5f);
rotY = rotY * 0.5f + 0.5f;
return make_float2(rotX, rotY);
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, __TEXTURE__ p_TexR, __TEXTURE__ p_TexG, __TEXTURE__ p_TexB)
{
float grainsize = 1.0f;
float Grainamount = 2.0f;
float Colour_Amount = 0.6f;
float Luma_Amount = 1.0f;
float Eu = 2.718281828459045f;
float iTime = _tex2D(p_TexR, 500, 500) / 24.0f;
float timer = iTime * 0.05f;
float Y = p_Height - p_Y;
float width = (float)p_Width;
float height = (float)p_Height;
float2 texCoord = make_float2(p_X/width, Y/height);
texCoord = multi(texCoord, 10.0f / Scale);
float3 rotOffset = make_float3(1.425f, 3.892f, 5.835f); //rotation offset values
float2 rotCoordsR = coordRot(texCoord, timer + rotOffset.x, width, height);
float3 noise = Make_Float3(pnoise3D(Make_Float3(multi(rotCoordsR, make_float2(width/grainsize, height/grainsize)), 0.0f), timer));
if (Coloured)
{
float2 rotCoordsG = coordRot(texCoord, timer + rotOffset.y, width, height);
float2 rotCoordsB = coordRot(texCoord, timer + rotOffset.z, width, height);
noise.y = mix(noise.x, pnoise3D(Make_Float3(multi(rotCoordsG, make_float2(width/grainsize, height/grainsize)), 1.0f), timer), Colour_Amount);
noise.z = mix(noise.x, pnoise3D(Make_Float3(multi(rotCoordsB, make_float2(width/grainsize, height/grainsize)), 2.0f), timer), Colour_Amount);
}
float3 Grain = Make_Float3(0.5f);
Grain = add(Grain, multi(noise, 0.025f * Grainamount));
Grain = add(multi(minus(Grain, 0.5f), Intensity), 0.5f);
float3 col = make_float3(_tex2D(p_TexR, p_X, p_Y), _tex2D(p_TexG, p_X, p_Y), _tex2D(p_TexB, p_X, p_Y));
col.x = col.x > 0.1496582f ? (_powf(10.0f, (col.x - 0.385537f) / 0.2471896f) - 0.052272f) / 5.555556f : (col.x - 0.092809f) / 5.367655f;
col.y = col.y > 0.1496582f ? (_powf(10.0f, (col.y - 0.385537f) / 0.2471896f) - 0.052272f) / 5.555556f : (col.y - 0.092809f) / 5.367655f;
col.z = col.z > 0.1496582f ? (_powf(10.0f, (col.z - 0.385537f) / 0.2471896f) - 0.052272f) / 5.555556f : (col.z - 0.092809f) / 5.367655f;
float Ar = col.x * 1.617523f + col.y * -0.537287f + col.z * -0.080237f;
float Ag = col.x * -0.070573f + col.y * 1.334613f + col.z * -0.26404f;
float Ab = col.x * -0.021102f + col.y * -0.226954f + col.z * 1.248056f;
col = make_float3(Ar, Ag, Ab);
if (Add_Grain)
{
col.x = Grain.x < 0.5f ? 2.0f * col.x * Grain.x + (col.x * col.x) * (1.0f - 2.0f * Grain.x) : 2.0f * col.x * (1.0f - Grain.x) + _sqrtf(col.x) * (2.0f * Grain.x - 1.0f);
col.y = Grain.y < 0.5f ? 2.0f * col.y * Grain.y + (col.y * col.y) * (1.0f - 2.0f * Grain.y) : 2.0f * col.y * (1.0f - Grain.y) + _sqrtf(col.y) * (2.0f * Grain.y - 1.0f);
col.z = Grain.z < 0.5f ? 2.0f * col.z * Grain.z + (col.z * col.z) * (1.0f - 2.0f * Grain.z) : 2.0f * col.z * (1.0f - Grain.z) + _sqrtf(col.z) * (2.0f * Grain.z - 1.0f);
}
col.x = col.x > 0.010591f ? 0.247190f * _log10f(5.555556f * col.x + 0.052272f) + 0.385537f : 5.367655f * col.x + 0.092809f;
col.y = col.y > 0.010591f ? 0.247190f * _log10f(5.555556f * col.y + 0.052272f) + 0.385537f : 5.367655f * col.y + 0.092809f;
col.z = col.z > 0.010591f ? 0.247190f * _log10f(5.555556f * col.z + 0.052272f) + 0.385537f : 5.367655f * col.z + 0.092809f;
col.x = 1.0f / (1.0f + _powf(Eu, -8.9f * (col.x - 0.435f)));
col.y = 1.0f / (1.0f + _powf(Eu, -8.9f * (col.y - 0.435f)));
col.z = 1.0f / (1.0f + _powf(Eu, -8.9f * (col.z - 0.435f)));
return col;
}