-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcl2.cl
16930 lines (11670 loc) · 484 KB
/
cl2.cl
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#pragma OPENCL EXTENSION cl_khr_3d_image_writes : enable
#pragma OPENCL FP_CONTRACT ON
#define MIP_LEVELS 4
#ifndef FOV_CONST
#define FOV_CONST 500.0f
#endif
#define LFOV_CONST (LIGHTBUFFERDIM/2.0f)
//#ifndef M_PI
#define M_PI 3.1415927f
//#endif // M_PI
#define depth_far 350000.0f
#define mulint UINT_MAX
#ifndef depth_icutoff
#define depth_icutoff 20
#endif // depth_icutoff
//#define depth_fcutoff depth_icutoff ## .f
#define depth_no_clear (mulint-1)
#undef supports_3d_writes
//#define LEAP
//#define IX(i,j,k) ((i) + (width*(j)) + (width*height*(k)))
struct interp_container;
float rerror(float a, float b)
{
return fabs(a-b);
}
float min3(float x, float y, float z)
{
return min(min(x,y),z);
}
float max3(float x, float y, float z)
{
return max(max(x,y),z);
}
int imin3(int x, int y, int z)
{
return min(min(x,y),z);
}
int imax3(int x, int y, int z)
{
return max(max(x,y),z);
}
///mad
float calc_area(float3 x, float3 y)
{
return fabs((x.x*(y.y - y.z) + x.y*(y.z - y.x) + x.z*(y.x - y.y)) * 0.5f);
}
///soa vs aos
struct voxel
{
int offset;
uchar valid_mask;
uchar leaf_mask;
uchar2 pad;
};
struct light
{
float4 pos;
float4 col;
uint shadow;
float brightness;
float radius;
float diffuse;
float godray_intensity;
int is_static;
};
///Don't use me as an actual type see
enum object_feature_flag
{
FEATURE_FLAG_SS_REFLECTIVE = 1,
FEATURE_FLAG_TWO_SIDED = 2,
FEATURE_FLAG_OUTLINE = 4,
FEATURE_FLAG_IS_STATIC = 8,
FEATURE_FLAG_DOES_NOT_RECEIVE_DYNAMIC_SHADOWS = 16,
};
struct obj_g_descriptor
{
float4 world_pos; ///w is 0
//float4 world_rot; ///w is 0
float4 world_rot_quat;
///motion blur
float4 old_world_pos_1;
float4 old_world_pos_2;
float4 old_world_rot_quat_1;
float4 old_world_rot_quat_2;
float scale;
uint tid; ///texture id
uint rid; ///normal map id
uint ssid; ///screenspace map id, does NOT imply is_ss_reflective
//uint mip_start;
uint has_bump;
float specular;
float spec_mult;
float diffuse;
//uint two_sided;
int buffer_offset; ///0 == first buffer, 1 == second etc
//uint is_ss_reflective; ///0 no screenspace reflections, 1 is screenspace reflections
int feature_flag;
};
bool has_feature(int feature_flag, enum object_feature_flag flag)
{
return (feature_flag & flag) > 0;
}
struct vertex
{
float4 pos;
float4 normal;
float2 vt;
uint object_id;
uint vertex_col;
};
/*struct vertex
{
float x, y, z;
uint object_id;
ushort2 normal;
uint h_vt;
};*/
struct triangle
{
struct vertex vertices[3];
};
float3 vertex_pos(__global struct vertex* v)
{
//return (float3)(v->x, v->y, v->z);
return v->pos.xyz;
}
void set_tri_vertex(__global struct triangle* T, int i, float3 pos)
{
/*T->vertices[i].x = pos.x;
T->vertices[i].y = pos.y;
T->vertices[i].z = pos.z;*/
T->vertices[i].pos.xyz = pos;
}
float3 reflect(float3 d, float3 n)
{
n = fast_normalize(n);
float3 r = d - 2 * dot(d, n) * n;
return r;
}
/*float calc_third_areas_i(float x1, float x2, float x3, float y1, float y2, float y3, float x, float y)
{
return (fabs(x2*y-x*y2+x3*y2-x2*y3+x*y3-x3*y) + fabs(x*y1-x1*y+x3*y-x*y3+x1*y3-x3*y1) + fabs(x2*y1-x1*y2+x*y2-x2*y+x1*y-x*y1)) * 0.5f;
}*/
float calc_third_areas_i(float x1, float x2, float x3, float y1, float y2, float y3, float x, float y)
{
return (
fabs(mad(x2,y,mad(x3,y2,x*y3)-mad(x3,y,mad(x,y2,x2*y3)))) +
fabs(mad(x,y1,mad(x3,y,x1*y3)-mad(x3,y1,mad(x1,y,x*y3)))) +
fabs(mad(x2,y1,mad(x,y2,x1*y)-mad(x,y1,mad(x1,y2,x2*y))))) * 0.5f;
}
/*float calc_third_areas_i(float x1, float x2, float x3, float y1, float y2, float y3, float x, float y)
{
return (fabs(x2*y-mad(x, y2, x3*y2)+mad(-x2, y3, x*y3)-x3*y) + fabs(x*y1-x1*y+x3*y-x*y3+x1*y3-x3*y1) + fabs(x2*y1-x1*y2+x*y2-x2*y+x1*y-x*y1)) * 0.5f;
}*/
/*float get_third_areas(float x1, float x2, float x3, float y1, float y2, float y3, float x, float y, float* a1, float* a2, float* a3)
{
*a1 = x2*y-x*y2+x3*y2-x2*y3+x*y3-x3*y;
*a2 = x*y1-x1*y+x3*y-x*y3+x1*y3-x3*y1;
*a3 = x2*y1-x1*y2+x*y2-x2*y+x1*y-x*y1;
}*/
/*float calc_third_areas_get(float x1, float x2, float x3, float y1, float y2, float y3, float x, float y, float* A, float* B, float* C)
{
*A = fabs(x2*y-x*y2+x3*y2-x2*y3+x*y3-x3*y) * 0.5f;
*B = fabs(x*y1-x1*y+x3*y-x*y3+x1*y3-x3*y1) * 0.5f;
*C = fabs(x2*y1-x1*y2+x*y2-x2*y+x1*y-x*y1) * 0.5f;
}*/
///wikipedia is wrong, this is the XYZ rotation
///http://wims.unice.fr/~wims/wims.cgi?session=JQDCD8CAA0.5&lang=en&cmd=reply&module=tool%2Flinear%2Fmatmult.en&matA=c2%2C0%2C-s2%0D%0As2*s1%2Cc1%2Cc2*s1%0D%0As2*c1%2C-s1%2Cc2*c1&matB=c3%2C+s3%2C+0%0D%0A-s3%2C+c3%2C+0%0D%0A0%2C+0%2C+1&show=A*B
/*c3*c2,s3*c2,-s2
(c3*s1*s2-s3*c1),(s3*s1*s2+c3*c1),s1*c2
(c3*c1*s2+s3*s1),(s3*c1*s2-c3*s1),c1*c2*/
///intrinsic xyz (extrinsic zyx)
///rotates point about camera
///no, extrinsic xyz
float3 rot(const float3 point, const float3 c_pos, const float3 c_rot)
{
float3 c = native_cos(c_rot);
float3 s = native_sin(c_rot);
float3 rel = point - c_pos;
float3 ret;
///this is correct
//ret.x = c.y * (s.z * rel.y + c.z*rel.x) - s.y*rel.z;
//ret.y = s.x * (c.y * rel.z + s.y*(s.z*rel.y + c.z*rel.x)) + c.x*(c.z*rel.y - s.z*rel.x);
//ret.z = c.x * (c.y * rel.z + s.y*(s.z*rel.y + c.z*rel.x)) - s.x*(c.z*rel.y - s.z*rel.x);
ret.x = mad(c.y, (mad(s.z, rel.y, c.z*rel.x)), - s.y*rel.z);
ret.y = mad(s.x, (mad(c.y, rel.z, s.y*(mad(s.z, rel.y, c.z*rel.x)))), c.x*(mad(c.z, rel.y, - s.z*rel.x)));
ret.z = mad(c.x, (mad(c.y, rel.z, s.y*(mad(s.z, rel.y, c.z*rel.x)))), - s.x*(mad(c.z, rel.y, - s.z*rel.x)));
/*cl_float4 cos_rot;
cos_rot.x = cos(c_rot.x);
cos_rot.y = cos(c_rot.y);
cos_rot.z = cos(c_rot.z);
cl_float4 sin_rot;
sin_rot.x = sin(c_rot.x);
sin_rot.y = sin(c_rot.y);
sin_rot.z = sin(c_rot.z);
cl_float4 ret;
ret.x= cos_rot.y*(sin_rot.z+cos_rot.z*(point.x-c_pos.x))-sin_rot.y*(point.z-c_pos.z);
ret.y= sin_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))+cos_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
ret.z= cos_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))-sin_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
ret.w = 0;*/
//float3 ret;
//ret.x = cos_rot.y*(sin_rot.z+cos_rot.z*(point.x-c_pos.x))-sin_rot.y*(point.z-c_pos.z);
//ret.y = sin_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))+cos_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
//ret.z = cos_rot.x*(cos_rot.y*(point.z-c_pos.z)+sin_rot.y*(sin_rot.z*(point.y-c_pos.y)+cos_rot.z*(point.x-c_pos.x)))-sin_rot.x*(cos_rot.z*(point.y-c_pos.y)-sin_rot.z*(point.x-c_pos.x));
///? this seems correct, though backwards
/*float3 r1 = {cos_rot.y*cos_rot.z, -cos_rot.y*sin_rot.z, sin_rot.y};
float3 r2 = {cos_rot.x*sin_rot.z + cos_rot.z*sin_rot.x*sin_rot.y, cos_rot.x*cos_rot.z - sin_rot.x*sin_rot.y*sin_rot.z, -cos_rot.y*sin_rot.x};
float3 r3 = {sin_rot.x*sin_rot.z - cos_rot.x*cos_rot.z*sin_rot.y, cos_rot.z*sin_rot.x + cos_rot.x*sin_rot.y*sin_rot.z, cos_rot.y*cos_rot.x};
*/
/*ret.x = c.y * (s.z * rel.y + c.z*rel.x) - s.y*rel.z;
ret.y = s.x * (c.y * rel.z + s.y*(s.z*rel.y + c.z*rel.x)) + c.x*(c.z*rel.y - s.z*rel.x);
ret.z = c.x * (c.y * rel.z + s.y*(s.z*rel.y + c.z*rel.x)) - s.x*(c.z*rel.y - s.z*rel.x);*/
return ret;
}
///the inverse of a rotation matrix is its transpose
float3 back_rot(const float3 point, const float3 c_pos, const float3 c_rot)
{
float3 c = native_cos(c_rot);
float3 s = native_sin(c_rot);
///so the forward rotation matrix is
/*c3*c2, s3*c2, -s2
(c3*s1*s2-s3*c1),(s3*s1*s2+c3*c1),s1*c2
(c3*c1*s2+s3*s1),(s3*c1*s2-c3*s1),c1*c*/
///so the transpose is
/*c2*c3, s1*s2*c3-c1*s3, c1*s2*c3+s1*s3
s3*c2, c1*c3+s1*s2*s3, -s1*c3+c1*s2*s3
-s2, s1*c2, c1*c2*/
float3 rel = point - c_pos;
float3 ret;
///transpose, simple form
/*ret.x = c.y * c.z * rel.x + (s.x * s.y * c.z - c.x * s.z) * rel.y + (c.x * s.y * c.z + s.x * s.z) * rel.z;
ret.y = (s.z * c.y) * rel.x + (c.x * c.z + s.x * s.y * s.z) * rel.y + (-s.x * c.z + c.x * s.y * s.z) * rel.z;
ret.z = -s.y * rel.x + (s.x * c.y) * rel.y + (c.x * c.y) * rel.z;*/
///mad(a, b, c) = a*b + c
///transpose, factorised
/*ret.x =
c.z * (
c.y * rel.x +
(s.x * s.y) * rel.y +
(c.x * s.y) * rel.z)
+ s.z *
(s.x * rel.z
- c.x * rel.y);
ret.y =
(s.z * c.y) * rel.x +
(c.x * c.z + s.x * s.y * s.z) * rel.y +
(-s.x * c.z + c.x * s.y * s.z) * rel.z;
ret.z =
-s.y * rel.x +
c.y *
(s.x * rel.y +
c.x * rel.z);*/
///transposed, with mads
ret.x =
c.z * (
mad(c.y, rel.x,
mad(s.x, s.y * rel.y,
c.x * s.y * rel.z)))
+ s.z *
(s.x * rel.z
- c.x * rel.y);
ret.y =
mad(s.z, c.y * rel.x,
mad(mad(c.x, c.z, s.x * s.y * s.z), rel.y,
(mad(-s.x, c.z, c.x * s.y * s.z) * rel.z)));
ret.z =
mad(-s.y, rel.x,
c.y *
mad(s.x, rel.y,
c.x * rel.z));
return ret;
}
float3 rot_quat(const float3 point, float4 quat)
{
quat = fast_normalize(quat);
float3 t = 2.f * cross(quat.xyz, point);
return point + quat.w * t + cross(quat.xyz, t);
}
float3 back_rot_quat(const float3 point, const float4 quat)
{
float4 conj = quat;
conj.xyz = -conj.xyz;
//float len = fast_length(conj);
float len_sq = dot(conj, conj);
return rot_quat(point, conj / len_sq);
}
///a rot then a back rot 'cancel' out
/*float3 back_rot(const float3 point, const float3 c_pos, const float3 c_rot)
{
float3 pos = rot(point, c_pos, (float3){-c_rot.x, 0, 0});
pos = rot(pos, c_pos, (float3){0, -c_rot.y, 0});
pos = rot(pos, c_pos, (float3){0, 0, -c_rot.z});
return pos;
}*/
/*float3 optirot(float3 point, float3 c_pos, float3 sin_c_rot, float3 cos_c_rot)
{
float3 ret;
ret.x = cos_c_rot.y*(sin_c_rot.z + cos_c_rot.z*(point.x-c_pos.x)) - sin_c_rot.y*(point.z-c_pos.z);
ret.y = sin_c_rot.x*(cos_c_rot.y*(point.z-c_pos.z)+sin_c_rot.y*(sin_c_rot.z*(point.y-c_pos.y)+cos_c_rot.z*(point.x-c_pos.x)))+cos_c_rot.x*(cos_c_rot.z*(point.y-c_pos.y)-sin_c_rot.z*(point.x-c_pos.x));
ret.z = cos_c_rot.x*(cos_c_rot.y*(point.z-c_pos.z)+sin_c_rot.y*(sin_c_rot.z*(point.y-c_pos.y)+cos_c_rot.z*(point.x-c_pos.x)))-sin_c_rot.x*(cos_c_rot.z*(point.y-c_pos.y)-sin_c_rot.z*(point.x-c_pos.x));
return ret;
}*/
float dcalc(float value)
{
//value=(log(value + 1)/(log(depth_far + 1)));
return native_divide(value, depth_far);
}
float idcalc(float value)
{
return value * depth_far;
}
float calc_rconstant(float x[3], float y[3])
{
return native_recip(x[1]*y[2]+x[0]*(y[1]-y[2])-x[2]*y[1]+(x[2]-x[1])*y[0]);
}
float calc_rconstant_v(const float3 x, const float3 y)
{
return native_recip(x.y*y.z+x.x*(y.y-y.z)-x.z*y.y+(x.z-x.y)*y.x);
}
void interpolate_get_const(float3 f, float3 x, float3 y, float rconstant, float* A, float* B, float* C)
{
*A = ((f.y*y.z+f.x*(y.y-y.z)-f.z*y.y+(f.z-f.y)*y.x) * rconstant);
*B = (-(f.y*x.z+f.x*(x.y-x.z)-f.z*x.y+(f.z-f.y)*x.x) * rconstant);
*C = f.x-(*A)*x.x - (*B)*y.x;
}
void calc_min_max(float3 points[3], float width, float height, float ret[4])
{
float x[3];
float y[3];
for(int i=0; i<3; i++)
{
x[i] = round(points[i].x);
y[i] = round(points[i].y);
}
ret[0] = min3(x[0], x[1], x[2]) - 1;
ret[1] = max3(x[0], x[1], x[2]);
ret[2] = min3(y[0], y[1], y[2]) - 1;
ret[3] = max3(y[0], y[1], y[2]);
ret[0] = clamp(ret[0], 0.0f, width-1);
ret[1] = clamp(ret[1], 0.0f, width-1);
ret[2] = clamp(ret[2], 0.0f, height-1);
ret[3] = clamp(ret[3], 0.0f, height-1);
}
float4 calc_min_max_p(float3 p0, float3 p1, float3 p2, float2 s)
{
p0 = round(p0);
p1 = round(p1);
p2 = round(p2);
float4 ret;
ret.x = min3(p0.x, p1.x, p2.x) - 1.f;
ret.y = max3(p0.x, p1.x, p2.x);
ret.z = min3(p0.y, p1.y, p2.y) - 1.f;
ret.w = max3(p0.y, p1.y, p2.y);
ret = clamp(ret, 0.f, s.xxyy - 1.f);
return ret;
}
void calc_min_max_oc(float3 points[3], float mx, float my, float width, float height, float ret[4])
{
float x[3];
float y[3];
for(int i=0; i<3; i++)
{
x[i] = round(points[i].x);
y[i] = round(points[i].y);
}
ret[0] = min3(x[0], x[1], x[2]) - 1;
ret[1] = max3(x[0], x[1], x[2]);
ret[2] = min3(y[0], y[1], y[2]) - 1;
ret[3] = max3(y[0], y[1], y[2]);
ret[0] = clamp(ret[0], mx, width-1);
ret[1] = clamp(ret[1], mx, width-1);
ret[2] = clamp(ret[2], my, height-1);
ret[3] = clamp(ret[3], my, height-1);
}
float3 get_flat_normal(float3 p0, float3 p1, float3 p2)
{
return cross(p1-p0, p2-p0);
}
///small holes are not this fault
///cannot be float as || float is not valid on some platforms
int backface_cull_expanded(float3 p0, float3 p1, float3 p2)
{
return cross((p1-p0), (p2-p0)).z < 0.f;
}
float3 rot_with_offset(const float3 pos, const float3 c_pos, const float3 c_rot, const float3 offset, const float3 rotation_offset)
{
float3 intermediate = rot(pos, 0, rotation_offset);
return rot(intermediate + offset, c_pos, c_rot);
}
float3 rot_quat_with_offset(float3 pos, float3 c_pos, float3 c_rot, float3 offset, float4 rotation_offset)
{
float3 intermediate = rot_quat(pos, rotation_offset);
return rot(intermediate + offset, c_pos, c_rot);
}
void rot_3(__global struct triangle *triangle, const float3 c_pos, const float3 c_rot, const float3 offset, const float3 rotation_offset, float3 ret[3])
{
ret[0] = rot(vertex_pos(&triangle->vertices[0]), 0, rotation_offset);
ret[1] = rot(vertex_pos(&triangle->vertices[1]), 0, rotation_offset);
ret[2] = rot(vertex_pos(&triangle->vertices[2]), 0, rotation_offset);
ret[0] = rot(ret[0] + offset, c_pos, c_rot);
ret[1] = rot(ret[1] + offset, c_pos, c_rot);
ret[2] = rot(ret[2] + offset, c_pos, c_rot);
}
void rot_3_raw(const float3 raw[3], const float3 rotation, float3 ret[3])
{
ret[0]=rot(raw[0], 0, rotation);
ret[1]=rot(raw[1], 0, rotation);
ret[2]=rot(raw[2], 0, rotation);
}
void rot_3_pos(const float3 raw[3], const float3 pos, const float3 rotation, float3 ret[3])
{
ret[0]=rot(raw[0], pos, rotation);
ret[1]=rot(raw[1], pos, rotation);
ret[2]=rot(raw[2], pos, rotation);
}
void depth_project(float3 rotated[3], float width, float height, float fovc, float3 ret[3])
{
for(int i=0; i<3; i++)
{
float2 rxy = mad(rotated[i].xy, fovc / rotated[i].z, (float2){width/2.f, height/2.f});
ret[i].xy = rxy;
ret[i].z = rotated[i].z;
}
}
float3 depth_project_singular(float3 rotated, float width, float height, float fovc)
{
/*float rx;
rx=(rotated.x) * (fovc/rotated.z);
float ry;
ry=(rotated.y) * (fovc/rotated.z);
rx+=width/2.f;
ry+=height/2.f;
float3 ret;
ret.x = rx;
ret.y = ry;
ret.z = rotated.z;*/
float2 rxy = mad(rotated.xy, fovc / rotated.z, (float2){width/2.f, height/2.f});
float3 ret;
ret.xy = rxy;
ret.z = rotated.z;
return ret;
}
///this clips with the near plane, but do we want to clip with the screen instead?
///could be generating huge triangles that fragment massively
///so, i think its all the branching and random array accesses that make this super slow
///evaluate projection pyramid thing instead of using depth_icutoff, get depth at point
///no. we need to clip properly ;_;
void generate_new_triangles(float3 points[3], int *num, float3 ret[2][3])
{
int id_valid;
int ids_behind[3];
int n_behind = 0;
for(int i=0; i<3; i++)
{
///will cause odd effects as tri crosses far clipping plane
if(points[i].z <= depth_icutoff || points[i].z > depth_far)
{
ids_behind[n_behind] = i;
n_behind++;
}
else
{
id_valid = i;
}
}
if(n_behind > 2)
{
*num = 0;
return;
}
float3 p1, p2, c1, c2;
///we really want to avoid a copy
if(n_behind == 0)
{
ret[0][0] = points[0]; ///copy nothing?
ret[0][1] = points[1];
ret[0][2] = points[2];
*num = 1;
return;
}
int g1, g2, g3;
if(n_behind==1)
{
int id = ids_behind[0];
///n0, v1, v2
g1 = id;
//g2 = (ids_behind[0] + 1) % 3;
//g3 = (ids_behind[0] + 2) % 3;
g2 = (id + 1) >= 3 ? id - 2 : id + 1;
g3 = (id + 2) >= 3 ? id - 1 : id + 2;
}
if(n_behind==2)
{
g2 = ids_behind[0];
g3 = ids_behind[1];
g1 = id_valid;
}
p1 = points[g2] + native_divide((depth_icutoff - points[g2].z)*(points[g1] - points[g2]), points[g1].z - points[g2].z);
p2 = points[g3] + native_divide((depth_icutoff - points[g3].z)*(points[g1] - points[g3]), points[g1].z - points[g3].z);
if(n_behind==1)
{
c1 = points[g2];
c2 = points[g3];
ret[0][0] = p1;
ret[0][1] = c1;
ret[0][2] = c2;
ret[1][0] = p1;
ret[1][1] = c2;
ret[1][2] = p2;
*num = 2;
}
else
{
c1 = points[g1];
ret[0][ids_behind[0]] = p1;
ret[0][ids_behind[1]] = p2;
ret[0][id_valid] = c1;
*num = 1;
}
}
void full_rotate_n_extra(float3 v1, float3 v2, float3 v3, float3 passback[2][3], int* num, const float3 c_pos, const float3 c_rot, const float3 offset, const float3 rotation_offset, const float fovc, const float width, const float height)
{
///void rot_3(__global struct triangle *triangle, float3 c_pos, float4 c_rot, float4 ret[3])
///void generate_new_triangles(float4 points[3], int ids[3], float lconst[2], int *num, float4 ret[2][3])
///void depth_project(float4 rotated[3], int width, int height, float fovc, float4 ret[3])
float3 tris[2][3];
float3 pr[3];
//rot_3(triangle, c_pos, c_rot, offset, rotation_offset, pr);
pr[0] = rot_with_offset(v1, c_pos, c_rot, offset, rotation_offset);
pr[1] = rot_with_offset(v2, c_pos, c_rot, offset, rotation_offset);
pr[2] = rot_with_offset(v3, c_pos, c_rot, offset, rotation_offset);
int n = 0;
generate_new_triangles(pr, &n, tris);
*num = n;
if(n == 0)
{
return;
}
depth_project(tris[0], width, height, fovc, passback[0]);
if(n == 2)
{
depth_project(tris[1], width, height, fovc, passback[1]);
}
}
void full_rotate_quat(float3 v1, float3 v2, float3 v3, float3 passback[2][3], int* num, float3 c_pos, float3 c_rot, float3 offset, float4 rotation_offset, float scale, float fovc, float width, float height)
{
///void depth_project(float4 rotated[3], int width, int height, float fovc, float4 ret[3])
float3 tris[2][3];
float3 pr[3];
pr[0] = rot_quat_with_offset(v1 * scale, c_pos, c_rot, offset, rotation_offset);
pr[1] = rot_quat_with_offset(v2 * scale, c_pos, c_rot, offset, rotation_offset);
pr[2] = rot_quat_with_offset(v3 * scale, c_pos, c_rot, offset, rotation_offset);
int n = 0;
generate_new_triangles(pr, &n, tris);
*num = n;
if(n == 0)
{
return;
}
depth_project(tris[0], width, height, fovc, passback[0]);
if(n == 2)
{
depth_project(tris[1], width, height, fovc, passback[1]);
}
}
#ifdef supports_3d_writes
typedef __write_only image3d_t image_3d_write;
#else
typedef __global uchar4* image_3d_write;
#endif
#ifdef supports_3d_writes
typedef __read_only image3d_t image_3d_read;
#else
typedef __global uchar4* image_3d_read;
#endif
#ifdef supports_3d_writes
void write_image_3d_hardware(int4 coord, __write_only image3d_t array, uint4 to_write, int width, int height)
#else
void write_image_3d_hardware(int4 coord, __global uchar4* array, uint4 to_write, int width, int height)
#endif
{
#ifdef supports_3d_writes
write_imageui(array, convert_int4(coord), to_write);
#else
//if(coord.x >= width || coord.y >= height || coord.x < 0 || coord.y < 0)
// return;
array[coord.z * width * height + mul24(coord.y, width) + coord.x] = convert_uchar4(to_write);
#endif
}
#ifdef supports_3d_writes
uint4 read_image_3d_hardware(int4 coord, __read_only image3d_t array, int width, int height)
#else
uint4 read_image_3d_hardware(int4 coord, __global uchar4* array, int width, int height)
#endif
{
#ifdef supports_3d_writes
sampler_t sam = CLK_NORMALIZED_COORDS_FALSE |
CLK_ADDRESS_NONE |
CLK_FILTER_NEAREST;
return read_imageui(array, sam, coord);
#else
//if(coord.x >= width || coord.y >= height || coord.x < 0 || coord.y < 0)
// return 0;
return convert_uint4(array[coord.z * width * height + mul24(coord.y, width) + coord.x]);
#endif
}
///reads a coordinate from the texture with id tid, num is and sizes are descriptors for the array
///fixme
///this should under no circumstances have to index two global arrays just to have to read from a damn texture
///replace nums and sizes with texture start in pixels. Maybe remove tid as well, directly pass in?
float4 read_tex_array(float2 coords, uint tid, global uint *num, global uint *size, image_3d_read array)
{
//cannot do linear interpolation on uchars
int nv = num[tid];
int slice = nv >> 16;
int which = nv & 0x0000FFFF;
const float max_tex_size = 2048;
float width = size[slice];
float hnum = floor(native_divide(max_tex_size, width));
///max horizontal and vertical nums
float tnumy = floor(native_divide(which, hnum));
float tnumx = mad(-tnumy, hnum, which);//which - tnumy * hnum;
coords = clamp(coords, 0.001f, width - 0.001f);
float2 res = mad((float2){tnumx, tnumy}, width, coords);
///width - fixes bug
///remember to add 0.5f to this
int4 coord;
coord.xy = convert_int2(res);
coord.z = slice;
uint4 col;
col = read_image_3d_hardware(coord, array, max_tex_size, max_tex_size);
float4 t = convert_float4(col);
return t;
}
float4 read_tex_array_all_precalculated(float2 coord_absolute_coordinates, int which, int slice, float width, image_3d_read array)
{
float2 coords = coord_absolute_coordinates;
const float max_tex_size = 2048;
const float imax_tex_size = 1.f/2048;
float ihnum = width * imax_tex_size;
float tnumy = floor(which * ihnum);
float tnumx = which - tnumy / ihnum;
coords = clamp(coords, 0.001f, width - 0.001f);
float2 res = mad((float2){tnumx, tnumy}, width, coords);
///width - fixes bug
///remember to add 0.5f to this
int4 coord;
coord.xy = convert_int2(res);
coord.z = slice;
uint4 col;
col = read_image_3d_hardware(coord, array, max_tex_size, max_tex_size);
float4 t = convert_float4(col);
return t;
}
///0 -> 255
///this is gpu_id weird combination
void write_tex_array(uint4 to_write, float2 coords, uint tid, __global uint* num, __global uint* size, image_3d_write array)
{
//cannot do linear interpolation on uchars
int nv = num[tid];
int slice = nv >> 16;
int which = nv & 0x0000FFFF;
const float max_tex_size = 2048;
float width = size[slice];
float hnum = floor(native_divide(max_tex_size, width));
///max horizontal and vertical nums
float tnumy = floor(native_divide(which, hnum));
float tnumx = mad(-tnumy, hnum, which);//which - tnumy * hnum;
float tx = tnumx*width;
float ty = tnumy*width;
coords = fmod(coords, width);
coords = clamp(coords, 0.001f, width - 0.001f);
///width - fixes bug
///remember to add 0.5f to this
int4 coord = {tx + coords.x, ty + coords.y, slice, 0};
//write_imageui(array, convert_int4(coord), to_write);
write_image_3d_hardware(coord, array, to_write, max_tex_size, max_tex_size);
}
///will only work if image_3d_write is not the array tex type
#ifndef supports_3d_writes
void blend_tex_array(uint4 to_write, float2 coords, uint tid, __global uint* num, __global uint* size, image_3d_write array)
{
float4 ccol = read_tex_array(coords, tid, num, size, array) / 255.f;
float4 b1 = convert_float4(to_write) / 255.f;
float4 b2 = convert_float4(ccol) / 255.f;
float3 pre1 = b1.xyz * b1.w;
float3 pre2 = b2.xyz * b2.w;
float4 res = (float4)(pre1.xyz + pre2 * (1.f - b1.w), 1.f) * 255.f;
write_tex_array(convert_uint4(res), coords, tid, num, size, array);
}
#endif
__kernel void write_col_to_tex(uint tex_id, __global uint* nums, __global uint* sizes, image_3d_write array, int x, int y, float4 col)
{
if(get_global_id(0) > 1)
return;
uint4 ucol = convert_uint4(col * 255);
int slice = nums[tex_id] >> 16;
float width = sizes[slice];
write_tex_array(ucol, (float2){x, y}, tex_id, nums, sizes, array);
}
///why is the texture actually floats, not 32bit rgba? surface format optimisation?
__kernel void update_gpu_tex(__read_only image2d_t tex, uint tex_id, uint mipmap_start, __global uint* nums, __global uint* sizes, image_3d_write array, int flip)
{
int x = get_global_id(0);
int y = get_global_id(1);
///I seem to remember this is hideously slow
int2 dim = get_image_dim(tex);
if(x >= dim.x || y >= dim.y)
return;
sampler_t sam = CLK_NORMALIZED_COORDS_FALSE |
CLK_ADDRESS_NONE |
CLK_FILTER_NEAREST;
///for some reason, even though sfml is 32bit RGBA internally,
///I have to use read_imagef to read the texture format
float4 col = read_imagef(tex, sam, (int2){x, y});
col *= 255.f;
uint4 ucol = convert_uint4(col);
int slice = nums[tex_id] >> 16;
float width = sizes[slice];
if(flip)
y = width - y;
write_tex_array(ucol, (float2){x, y}, tex_id, nums, sizes, array);
}
__kernel
void update_gpu_tex_colour(float4 col, uint tex_id, uint mipmap_start, __global uint* nums, __global uint* sizes, image_3d_write array)
{
int x = get_global_id(0);
int y = get_global_id(1);
int slice = nums[tex_id] >> 16;
float width = sizes[slice];
if(x >= width || y >= width)
return;
uint4 ucol = convert_uint4(col);
write_tex_array(ucol, (float2){x, y}, tex_id, nums, sizes, array);
for(int i=0; i<MIP_LEVELS; i++)
{
///is this just.. wrong?
///how on earth has this ever worked???
///tex_id is some completely random property
int mtexid = tex_id * MIP_LEVELS + mipmap_start + i;
int w2 = nums[mtexid] >> 16;
float nwidth = sizes[w2];
write_tex_array(ucol, ((float2){x, y} / width) * nwidth, mtexid, nums, sizes, array);
}
}
/*float noise_2d(int x, int y)
{
int n=x*271 + y*1999;
n=(n<<13)^n;
int nn=(n*(n*n*41333 +53307781)+1376312589)&0x7fffffff;
return ((1.0-((float)nn/1073741824.0)));// + noise1(x) + noise1(y) + noise1(z) + noise1(w))/5.0;
}*/
float noise(int x)
{
int n=x*271;
n=(n<<13)^n;