-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathforward_render.pmfx
1368 lines (1117 loc) · 37.9 KB
/
forward_render.pmfx
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
#include "libs/lighting.pmfx"
#include "libs/skinning.pmfx"
#include "libs/globals.pmfx"
#include "libs/sdf.pmfx"
#include "libs/area_lights.pmfx"
// vs inputs
struct vs_input
{
float4 position : POSITION;
float4 normal : TEXCOORD0;
float4 texcoord : TEXCOORD1;
float4 tangent : TEXCOORD2;
float4 bitangent : TEXCOORD3;
};
struct vs_input_stencil_shadow
{
float4 position : POSITION;
float4 face_normal_0 : TEXCOORD0;
float4 face_normal_1 : TEXCOORD1;
};
struct vs_input_skinned
{
float4 position : POSITION;
float4 normal : TEXCOORD0;
float4 texcoord : TEXCOORD1;
float4 tangent : TEXCOORD2;
float4 bitangent : TEXCOORD3;
float4 blend_indices : TEXCOORD4;
float4 blend_weights : TEXCOORD5;
};
struct vs_instance_input
{
if:(INSTANCED)
{
float4 world_matrix_0 : TEXCOORD6;
float4 world_matrix_1 : TEXCOORD7;
float4 world_matrix_2 : TEXCOORD8;
float4 world_matrix_3 : TEXCOORD9;
float4 user_data : TEXCOORD10;
float4 user_data2 : TEXCOORD11;
}
};
struct vs_input_multi
{
float4 position : POSITION;
float4 normal : TEXCOORD0;
float4 texcoord : TEXCOORD1;
float4 tangent : TEXCOORD2;
float4 bitangent : TEXCOORD3;
if:(SKINNED)
{
float4 blend_indices : TEXCOORD4;
float4 blend_weights : TEXCOORD5;
}
};
struct vs_input_position_only
{
float4 position : POSITION;
if:(SKINNED)
{
float4 normal : TEXCOORD0;
float4 texcoord : TEXCOORD1;
float4 tangent : TEXCOORD2;
float4 bitangent : TEXCOORD3;
float4 blend_indices : TEXCOORD4;
float4 blend_weights : TEXCOORD5;
}
};
// vs outputs / ps inputs
struct vs_output
{
float4 position : SV_POSITION;
float4 world_pos : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 bitangent : TEXCOORD3;
float4 texcoord : TEXCOORD4;
float4 colour : TEXCOORD5;
};
struct vs_output_pre_skin
{
float4 vb_position : TEXCOORD0;
float4 normal : TEXCOORD1;
float4 texcoord : TEXCOORD2;
float4 tangent : TEXCOORD3;
float4 bitangent : TEXCOORD4;
};
struct vs_output_pre_skin_position
{
float4 vb_position : TEXCOORD0;
};
struct vs_output_zonly
{
float4 position : SV_POSITION;
};
struct vs_output_zonly_wp
{
float4 position : SV_POSITION;
float4 world_pos : TEXCOORD0;
};
struct vs_output_picking
{
float4 position : SV_POSITION;
float4 index : TEXCOORD0;
};
// ps output
struct ps_output
{
float4 colour : SV_Target;
};
struct ps_output_multi
{
float4 albedo : SV_Target0;
float4 normal : SV_Target1;
float4 world_pos : SV_Target2;
};
struct ps_output_depth
{
float depth : SV_Depth;
};
shader_resources
{
texture_2d( diffuse_texture, 0 );
texture_2d( normal_texture, 1 );
texture_2d( specular_texture, 2 );
if:(GI) {
texture_2d( blue_noise, 5 );
}
texture_3d( sdf_volume, 14 );
texture_2d( ltc_mat, 13 );
texture_2d( ltc_mag, 12 );
if:(PMFX_TEXTURE_CUBE_ARRAY) {
depth_cube_array( omni_shadow_texture, 10 );
}
texture_2d_array( area_light_textures, 11 );
texture_3d( volume_gi, 9 );
depth_2d( single_shadowmap_texture, 7 );
depth_2d_array( shadowmap_texture, 15 );
texture_2d( shadowmap_texture_sss, 8);
};
vs_output_zonly vs_main_zonly( vs_input_position_only input, vs_instance_input instance_input )
{
vs_output_zonly output;
float4x4 wvp;
if:(INSTANCED)
{
float4x4 instance_world_mat;
unpack_vb_instance_mat(
instance_world_mat,
instance_input.world_matrix_0,
instance_input.world_matrix_1,
instance_input.world_matrix_2,
instance_input.world_matrix_3
);
wvp = mul( instance_world_mat, vp_matrix );
}
else:
{
wvp = mul( world_matrix, vp_matrix );
}
if:(SKINNED)
{
float4 sp = skin_pos(input.position, input.blend_weights, input.blend_indices);
output.position = mul( sp, vp_matrix );
}
else:
{
output.position = mul( input.position, wvp );
}
return output;
}
vs_output_zonly_wp vs_main_zonly_wp( vs_input_position_only input, vs_instance_input instance_input )
{
vs_output_zonly_wp output;
float4x4 wvp = mul( world_matrix, vp_matrix );
float4x4 wm = world_matrix;
if:(INSTANCED)
{
float4x4 instance_world_mat;
unpack_vb_instance_mat(
instance_world_mat,
instance_input.world_matrix_0,
instance_input.world_matrix_1,
instance_input.world_matrix_2,
instance_input.world_matrix_3
);
wvp = mul( instance_world_mat, vp_matrix );
wm = instance_world_mat;
}
if:(SKINNED)
{
float4 sp = skin_pos(input.position, input.blend_weights, input.blend_indices);
output.position = mul( sp, vp_matrix );
output.world_pos = sp;
}
else:
{
output.position = mul( input.position, wvp );
output.world_pos = mul( input.position, wm );
}
return output;
}
vs_output vs_main_extrude( vs_input_stencil_shadow input )
{
vs_output output;
float4 wp = mul( input.position, world_matrix );
float3x3 wrm = to_3x3(world_matrix);
wrm[0] = normalize(wrm[0]);
wrm[1] = normalize(wrm[1]);
wrm[2] = normalize(wrm[2]);
float3 wn_0 = mul( input.face_normal_0.xyz, wrm );
float3 wn_1 = mul( input.face_normal_1.xyz, wrm );
// detect silhouette edges
float3 ld = wp.xyz - single_light.pos_radius.xyz;
float d0 = dot(ld, wn_0.xyz);
float d1 = dot(ld, wn_1.xyz);
if(d0 > 0.0 && d1 < 0.0)
wp.xyz += ld * 100.0;
output.position = mul( wp, vp_matrix);
return output;
}
vs_output_pre_skin vs_main_pre_skin( vs_input_skinned input )
{
vs_output_pre_skin output;
float4 sp = skin_pos(input.position, input.blend_weights, input.blend_indices);
output.vb_position = sp;
output.normal = input.normal;
output.tangent = input.tangent;
output.bitangent = input.bitangent;
output.texcoord = input.texcoord;
float3 t = input.tangent.xyz;
float3 b = input.bitangent.xyz;
float3 n = input.normal.xyz;
skin_tbn(t, b, n, input.blend_weights, input.blend_indices);
output.normal.xyz = n;
output.tangent.xyz = t;
output.bitangent.xyz = b;
return output;
}
vs_output_pre_skin_position vs_main_pre_skin_position( vs_input_skinned input )
{
vs_output_pre_skin_position output;
float4 sp = skin_pos(input.position, input.blend_weights, input.blend_indices);
output.vb_position = sp;
return output;
}
vs_output vs_main( vs_input_multi input, vs_instance_input instance_input )
{
vs_output output;
float4x4 wvp = mul( world_matrix, vp_matrix );
float4x4 wm = world_matrix;
output.texcoord = float4(input.texcoord.x, 1.0 - input.texcoord.y,
input.texcoord.z, 1.0 - input.texcoord.w );
if:(INSTANCED)
{
float4x4 instance_world_mat;
unpack_vb_instance_mat(
instance_world_mat,
instance_input.world_matrix_0,
instance_input.world_matrix_1,
instance_input.world_matrix_2,
instance_input.world_matrix_3
);
wvp = mul( instance_world_mat, vp_matrix );
wm = instance_world_mat;
output.colour = instance_input.user_data2;
}
else:
{
output.colour = m_albedo;
}
if:(SKINNED)
{
float4 sp = skin_pos(input.position, input.blend_weights, input.blend_indices);
output.tangent = input.tangent.xyz;
output.bitangent = input.bitangent.xyz;
output.normal = input.normal.xyz;
skin_tbn(output.tangent, output.bitangent, output.normal, input.blend_weights, input.blend_indices);
output.position = mul( sp, vp_matrix );
output.world_pos = sp;
}
else:
{
output.position = mul( input.position, wvp );
output.world_pos = mul( input.position, wm );
float3x3 wrm = to_3x3(wm);
wrm[0] = normalize(wrm[0]);
wrm[1] = normalize(wrm[1]);
wrm[2] = normalize(wrm[2]);
output.normal = mul( input.normal.xyz, wrm );
output.tangent = mul( input.tangent.xyz, wrm );
output.bitangent = mul( input.bitangent.xyz, wrm );
}
if:(UV_SCALE)
{
float3 scale = float3(length(world_matrix[0].xyz),
length(world_matrix[1].xyz),
length(world_matrix[2].xyz));
float xs = length(input.tangent.xyz * scale);
float ys = length(input.bitangent.xyz * scale);
output.texcoord *= float4(m_uv_scale.x * xs, m_uv_scale.y * ys, m_uv_scale.x, m_uv_scale.y);
}
return output;
}
float3 transform_ts_normal( float3 t, float3 b, float3 n, float3 ts_normal )
{
float3x3 tbn;
tbn[0] = float3(t.x, b.x, n.x);
tbn[1] = float3(t.y, b.y, n.y);
tbn[2] = float3(t.z, b.z, n.z);
return normalize( mul_tbn( tbn, ts_normal ) );
}
ps_output ps_single_light( vs_output input )
{
// this is for multi-pass lighting using stencil shadows.
float4 albedo = sample_texture( diffuse_texture, input.texcoord.xy );
float3 normal_sample = sample_texture( normal_texture, input.texcoord.xy ).rgb;
float4 ro_sample = sample_texture( specular_texture, input.texcoord.xy );
float4 specular_sample = float4(1.0, 1.0, 1.0, 1.0);
float reflectivity = m_reflectivity;
float roughness = ro_sample.x;
normal_sample = normal_sample * 2.0 - 1.0;
float3 n = transform_ts_normal(
input.tangent,
input.bitangent,
input.normal,
normal_sample );
float3 lit_colour = float3( 0.0, 0.0, 0.0 );
lit_colour += cook_torrence(
single_light.pos_radius,
single_light.colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
albedo.rgb,
specular_sample.rgb,
roughness,
reflectivity);
lit_colour += oren_nayar(
single_light.pos_radius,
single_light.colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
roughness,
albedo.rgb);
ps_output output;
output.colour.rgb = lit_colour;
output.colour.a = albedo.a;
return output;
}
float hash_12(vec2 p)
{
// Two typical hashes...
return fract(sin(dot(p, float2(12.9898, 78.233))) * 43758.5453);
}
float3 hash_33( float3 p )
{
p = float3(dot(p,float3(127.1,311.7, 74.7)),
dot(p,float3(269.5,183.3,246.1)),
dot(p,float3(113.5,271.9,124.6)));
return fract(sin(p)*43758.5453123) * 2.0 - 1.0;
}
ps_output ps_forward_lit( vs_output input )
{
ps_output output;
float4 albedo = sample_texture( diffuse_texture, input.texcoord.xy );
float3 normal_sample = sample_texture( normal_texture, input.texcoord.xy ).rgb;
float4 ro_sample = sample_texture( specular_texture, input.texcoord.xy );
float4 specular_sample = float4(1.0, 1.0, 1.0, 1.0);
normal_sample = normal_sample * 2.0 - 1.0;
float3 n = transform_ts_normal(
input.tangent,
input.bitangent,
input.normal,
normal_sample );
albedo *= input.colour;
float4 metalness = float4(0.0, 0.0, 0.0, 1.0);
float3 lit_colour = float3( 0.0, 0.0, 0.0 );
//todo these need to be passed from vs for instancing
float reflectivity = saturate(user_data.z);
float roughness = saturate(user_data.y);
reflectivity = m_reflectivity;
roughness = ro_sample.r;
if:(INSTANCED)
{
roughness = input.colour.a;
albedo.a = 1.0;
}
if:(SDF_SHADOW)
{
n = input.normal.rgb;
roughness = m_roughness;
float max_samples = 128.0;
float3x3 inv_rot = to_3x3(sdf_shadow.world_matrix_inv);
// point on surface ray origin in sdf space
float3 r1 = input.world_pos.xyz + input.normal.xyz * m_surface_offset; // offset slightly by normal to avoid self-shdow
float3 tr1 = mul( float4(r1, 1.0), sdf_shadow.world_matrix_inv ).xyz;
float3 scale = float3(length(sdf_shadow.world_matrix[0].xyz), length(sdf_shadow.world_matrix[1].xyz), length(sdf_shadow.world_matrix[2].xyz)) * 2.0;
// derivatives for texture grad
float3 vddx = ddx( r1 );
float3 vddy = ddy( r1 );
}
float t = 1.0;
//for directional lights
float3 lll = float3(0.0, 0.0, 0.0);
int shadow_map_index = 0;
_pmfx_loop
for( int i = 0; i < int(light_info.x); ++i )
{
float3 light_col = float3( 0.0, 0.0, 0.0 );
light_col += cook_torrence(
lights[i].pos_radius,
lights[i].colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
albedo.rgb,
metalness.rgb,
roughness,
reflectivity
);
light_col += oren_nayar(
lights[i].pos_radius,
lights[i].colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
roughness,
albedo.rgb
);
if:(SDF_SHADOW)
{
float s = sdf_shadow_trace(max_samples, lights[i].pos_radius.xyz, input.world_pos.xyz, scale, tr1, sdf_shadow.world_matrix_inv, inv_rot);
light_col *= smoothstep( 0.0, 0.1, s);
}
if( lights[i].colour.a == 0.0 )
{
lit_colour += light_col;
continue;
}
else
{
float shadow = 1.0;
float d = 1.0;
// shadow map
float4 offset_pos = float4(input.world_pos.xyz + n.xyz * 0.01, 1.0);
float4 sp = mul( offset_pos, shadow_matrix[i] );
sp.xyz /= sp.w;
sp.y *= -1.0;
sp.xy = sp.xy * 0.5 + 0.5;
sp.z = remap_depth(sp.z);
shadow = sample_shadow_array_pcf_9(float(shadow_map_index), sp.xyz);
lit_colour += light_col * shadow;
++shadow_map_index;
}
}
//for point lights
int point_start = int(light_info.x);
int point_end = int(light_info.x) + int(light_info.y);
int omni_shadow_index = 0;
_pmfx_loop
for( int i = point_start; i < point_end; ++i )
{
float3 light_col = float3( 0.0, 0.0, 0.0 );
light_col += cook_torrence(
lights[i].pos_radius,
lights[i].colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
albedo.rgb,
metalness.rgb,
roughness,
reflectivity
);
light_col += oren_nayar(
lights[i].pos_radius,
lights[i].colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
roughness,
albedo.rgb
);
float a = point_light_attenuation_cutoff( lights[i].pos_radius, input.world_pos.xyz );
light_col *= a;
if:(SDF_SHADOW)
{
float s = sdf_shadow_trace(max_samples, lights[i].pos_radius.xyz, input.world_pos.xyz, scale, tr1, sdf_shadow.world_matrix_inv, inv_rot);
light_col *= smoothstep( 0.0, 0.1, s);
}
if( lights[i].colour.a == 0.0)
{
lit_colour += light_col;
continue;
}
else
{
if:(PMFX_TEXTURE_CUBE_ARRAY)
{
// omni directional shadow
float3 to_light = (input.world_pos.xyz - lights[i].pos_radius.xyz);
float d = length(to_light) / 2.0; // omni shadow space far plane is radius * 2.0
float3 cv = normalize(to_light) * float3(1.0, 1.0, -1.0);
// add small epsilon and convert to 0-1
d /= lights[i].pos_radius.w;
d -= 0.00025f;
float ll = sample_depth_compare_cube_array(omni_shadow_texture, cv, float(omni_shadow_index), d);
lit_colour += light_col * ll;
++omni_shadow_index;
}
else:
{
lit_colour += light_col;
continue;
}
}
}
//for spot lights
int spot_start = point_end;
int spot_end = spot_start + int(light_info.z);
_pmfx_loop
for(int i = spot_start; i < spot_end; ++i )
{
float3 light_col = float3( 0.0, 0.0, 0.0 );
light_col += cook_torrence(
lights[i].pos_radius,
lights[i].colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
albedo.rgb,
metalness.rgb,
roughness,
reflectivity
);
light_col += oren_nayar(
lights[i].pos_radius,
lights[i].colour.rgb,
n,
input.world_pos.xyz,
camera_view_pos.xyz,
roughness,
albedo.rgb
);
float a = spot_light_attenuation(lights[i].pos_radius,
lights[i].dir_cutoff,
lights[i].data.x, // falloff
input.world_pos.xyz );
light_col *= a;
if:(SDF_SHADOW)
{
float s = sdf_shadow_trace(max_samples, lights[i].pos_radius.xyz, input.world_pos.xyz, scale, tr1, sdf_shadow.world_matrix_inv, inv_rot);
light_col *= smoothstep( 0.0, 0.1, s);
}
if( lights[i].colour.a == 0.0 )
{
lit_colour += light_col;
continue;
}
else
{
float shadow = 1.0;
float d = 1.0;
// shadow map
float4 offset_pos = float4(input.world_pos.xyz + n.xyz * 0.01, 1.0);
float4 sp = mul( offset_pos, shadow_matrix[shadow_map_index] );
sp.xyz /= sp.w;
sp.y *= -1.0;
sp.xy = sp.xy * 0.5 + 0.5;
sp.z = remap_depth(sp.z);
shadow = sample_shadow_array_pcf_9(float(shadow_map_index), sp.xyz);
lit_colour += light_col * shadow;
++shadow_map_index;
}
}
// area lights
{
// area lights constant colour
float pi = 3.14159265359;
int num_area_lights = int(area_light_info.x);
for(int i = 0; i < num_area_lights; ++i)
{
float3 v = -normalize(input.world_pos.xyz - camera_view_pos.xyz);
float3 pos = input.world_pos.xyz;
float3 points[4];
for(int j = 0; j < 4; ++j)
points[j] = area_lights[i].corners[j].xyz;
// diffuse
float diff_sum = area_light_diffuse(points, pos, n, v);
float3 diff = area_lights[i].colour.rgb * diff_sum;
// specular
float spec_sum = area_light_specular(points, pos, ro_sample.x, n, v);
float3 spec = area_lights[i].colour.rgb * spec_sum;
float3 light_col = (spec.rgb + diff.rgb) / (2.0 * pi);
lit_colour += light_col;
}
// area lights textured
int ts = num_area_lights;
int num_area_lights_textured = int(area_light_info.y);
for(int i = ts; i < ts + num_area_lights_textured; ++i)
{
float slice = area_lights[i].colour.w;
float levels = 8.0;
float2 inv_texel = float2(1.0/640.0, 1.0/480.0);
float2 inv_texel_x = float2(1.0, 1.0) - inv_texel;
float3 points[4];
for(int j = 0; j < 4; ++j)
points[j] = area_lights[i].corners[j].xyz;
float3 v = -normalize(input.world_pos.xyz - camera_view_pos.xyz);
float3 pos = input.world_pos.xyz;
// diffuse
float4 diff_uv = area_light_diffuse_uv(points, pos, n, v);
float2 duv = clamp(diff_uv.xy, inv_texel, inv_texel_x);
float3 diff = sample_texture_array_level( area_light_textures, duv, slice, diff_uv.z * levels).rgb * diff_uv.w;
// specular
float4 spec_uv = area_light_specular_uv(points, pos, ro_sample.x, n, v);
float2 suv = clamp(spec_uv.xy, inv_texel, inv_texel_x);
float3 spec = sample_texture_array_level(area_light_textures, suv, slice, spec_uv.z * levels).rgb * spec_uv.w;
float3 light_col = (spec.rgb + diff.rgb) / (2.0 * pi);
lit_colour += light_col;
}
}
output.colour.rgb = lit_colour.rgb * albedo.a;
output.colour.a = albedo.a;
// gi volume tracing..
if:(GI)
{
// geometry tb for casting rays
float3 gn = input.normal.xyz;
float3 gt = input.tangent.xyz;
float3 gb = input.bitangent.xyz;
// scene / volume dimensions
float3 dim = gi_scene_size.xyz;
float3 to_uvx = dim * 0.5;
// 16 rays on sphere surface
int num_rays = 16;
float3 rays[16];
rays[0] = float3(0.57735, 0.57735, 0.57735);
rays[1] = float3(0.57735, -0.57735, -0.57735);
rays[2] = float3(-0.57735, 0.57735, -0.57735);
rays[3] = float3(-0.57735, -0.57735, 0.57735);
rays[4] = float3(-0.903007, -0.182696, -0.388844);
rays[5] = float3(-0.903007, 0.182696, 0.388844);
rays[6] = float3(0.903007, -0.182696, 0.388844);
rays[7] = float3(0.903007, 0.182696, -0.388844);
rays[8] = float3(-0.388844, -0.903007, -0.182696);
rays[9] = float3(0.388844, -0.903007, 0.182696);
rays[10] = float3(0.388844, 0.903007, -0.182696);
rays[11] = float3(-0.388844, 0.903007, 0.182696);
rays[12] = float3(-0.182696, -0.388844, -0.903007);
rays[13] = float3(0.182696, 0.388844, -0.903007);
rays[14] = float3(-0.182696, 0.388844, 0.903007);
rays[15] = float3(0.182696, -0.388844, 0.903007);
float4 gi = float4(0.0, 0.0, 0.0, 0.0);
float4 sp = mul(input.world_pos, vp_matrix);
sp /= sp.w;
sp.x *= (1280.0/512.0);
sp.y *= (720.0/512.0);
// trace rays
for(int i = 0; i < num_rays; ++i)
{
float3 noise = (hash_33(input.world_pos.xyz + user_data.yyy));
float3 noise2 = (sample_texture_level(blue_noise, sp.xy + noise.xy, 0.0).rgb * 2.0 - 1.0);
// start outside occlusion
float3 tex_size = gi_volume_size.xyz;
float3 ray = chebyshev_normalize(noise2 + rays[i]);
float3 cn = chebyshev_normalize(n);
float3 step = (dim*2.0) / tex_size;
float3 sp = input.world_pos.xyz + (cn * step);
// ensure ray is pointing in normals hemisphere
ray *= dot(ray, gn) < 0.0 ? -1.0 : 1.0;
// gather gi
float4 ray_gi = float4(0.0, 0.0, 0.0, 0.0);
// sample each mip map level
// first 4 levels make the main contribution, after 4 its hard to notice any difference
for(int j = 0; j < 4; ++j)
{
// 2 steps per level to cover the distance of 1 texel in mip j+1
for(int k = 0; k < 2; ++k)
{
step = (dim*2.0) / tex_size;
float3 uvw = saturate((sp / to_uvx) * 0.5 + 0.5);
float4 g = sample_texture_level( volume_gi, uvw, float(j));
float d = length(input.world_pos.xyz - sp);
d = smoothstep(0.0, 8.0, d);
ray_gi.rgb = ray_gi.rgb + g.rgb * d;
ray_gi.a += g.a;
sp += ray * step.x;
}
tex_size /= 2.0;
// break if we reach 1 alpha but accumulate mip 0 for better coverage at contact points
if(ray_gi.a >= 1.0 && j > 1)
break;
}
gi += ray_gi;
}
gi /= float(num_rays);
// could multiply gi with albedo but additive gi gives a stronger effect
output.colour.rgb = gi.rgb * 2.0 * m_albedo.rgb + lit_colour.rgb;
}
if(albedo.a <= 0.0)
discard;
return output;
}
ps_output_multi ps_gbuffer( vs_output input )
{
ps_output_multi output;
float4 albedo = sample_texture(diffuse_texture, input.texcoord.xy);
float4 metalness = sample_texture(specular_texture, input.texcoord.xy);
float3 normal_sample = sample_texture( normal_texture, input.texcoord.xy ).rgb;
normal_sample = normal_sample * 2.0 - 1.0;
float4 ro_sample = sample_texture( specular_texture, input.texcoord.xy );
float3 n = transform_ts_normal(
input.tangent,
input.bitangent,
input.normal,
normal_sample );
float roughness = ro_sample.x;
float reflectivity = m_reflectivity;
//roughness = m_roughness;
output.albedo = float4(albedo.rgb * input.colour.rgb, roughness);
output.normal = float4(n, reflectivity);
output.world_pos = float4(input.world_pos.xyz, metalness.r);
return output;
}
void ps_null( vs_output_zonly input )
{
//stub
return;
}
ps_output ps_dbg_pos( vs_output_zonly input )
{
ps_output output;
output.colour = float4(1.0, 0.0, 1.0, 1.0);
return output;
}
ps_output ps_dbg( vs_output input )
{
ps_output output;
output.colour = float4(1.0, 0.0, 1.0, 1.0);
return output;
}
ps_output_depth ps_omni_shadow( vs_output_zonly_wp input )
{
ps_output_depth output;
float d = length(input.world_pos.xyz - single_light.pos_radius.xyz) / single_light.pos_radius.w;
output.depth = d / 2.0; // divide by 2 because the ortho far plane is light radius * 2.0
return output;
}
ps_output ps_albedo( vs_output_zonly input )
{
ps_output output;
output.colour = m_albedo;
output.colour.a = 1.0;
return output;
}
ps_output ps_single_light_diffuse( vs_output input )
{
ps_output output;
float3 l = normalize(single_light.pos_radius.xyz - input.world_pos.xyz);
float3 lc = single_light.colour.rgb;
output.colour.rgb = (m_albedo.rgb * dot(input.normal, l) * lc);
output.colour.rgb += m_albedo.rgb * 0.3;
output.colour.a = 1.0;
return output;
}
ps_output ps_per_pixel_velocity( vs_output input )
{
ps_output output;
output.colour = float4(1.0, 0.0, 0.0, 0.0);
return output;
}
ps_output ps_single_light_diffuse( vs_output input )
{
ps_output output;
float3 l = normalize(single_light.pos_radius.xyz - input.world_pos.xyz);
float3 lc = single_light.colour.rgb;
output.colour.rgb = (m_albedo.rgb * dot(input.normal, l) * lc);
output.colour.rgb += m_albedo.rgb * 0.3;
output.colour.a = 1.0;
return output;
}
ps_output ps_simple_lighting( vs_output input )
{
ps_output output;
float4 albedo = sample_texture( diffuse_texture, input.texcoord.xy );
float3 normal_sample = sample_texture( normal_texture, input.texcoord.xy ).rgb;
float4 ro_sample = sample_texture( specular_texture, input.texcoord.xy );
float4 specular_sample = float4(1.0, 1.0, 1.0, 1.0);
normal_sample = normal_sample * 2.0 - 1.0;
float3 n = transform_ts_normal(
input.tangent,
input.bitangent,
input.normal,
normal_sample );
albedo *= input.colour;
float4 metalness = float4(1.0, 1.0, 1.0, 1.0);
float3 lit_colour = float3( 0.0, 0.0, 0.0 );
float reflectivity = saturate(user_data.z);
float roughness = saturate(user_data.y);
reflectivity = m_reflectivity;
roughness = m_roughness;
if:(INSTANCED)
{
roughness = input.colour.a;
albedo.a = 1.0;
}