This repository was archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShader.cpp
1707 lines (1609 loc) · 69.5 KB
/
Shader.cpp
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 "stdafx.h"
#include "Shader.h"
#include "typedefs3D.h"
#include "RenderDevice.h"
#ifdef ENABLE_SDL
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <inc/robin_hood.h>
#include <regex>
static ShaderTechniques m_bound_technique = ShaderTechniques::SHADER_TECHNIQUE_INVALID;
#endif
#if DEBUG_LEVEL_LOG == 0
#define LOG(a,b,c)
#endif
#define SHADER_TECHNIQUE(name) #name
const string Shader::shaderTechniqueNames[SHADER_TECHNIQUE_COUNT]
{
SHADER_TECHNIQUE(RenderBall),
SHADER_TECHNIQUE(RenderBall_DecalMode),
SHADER_TECHNIQUE(RenderBall_CabMode),
SHADER_TECHNIQUE(RenderBall_CabMode_DecalMode),
SHADER_TECHNIQUE(RenderBallTrail),
SHADER_TECHNIQUE(basic_without_texture),
SHADER_TECHNIQUE(basic_with_texture),
SHADER_TECHNIQUE(basic_with_texture_normal),
SHADER_TECHNIQUE(basic_without_texture_isMetal),
SHADER_TECHNIQUE(basic_with_texture_isMetal),
SHADER_TECHNIQUE(basic_with_texture_normal_isMetal),
SHADER_TECHNIQUE(playfield_without_texture),
SHADER_TECHNIQUE(playfield_with_texture),
SHADER_TECHNIQUE(playfield_with_texture_normal),
SHADER_TECHNIQUE(playfield_without_texture_isMetal),
SHADER_TECHNIQUE(playfield_with_texture_isMetal),
SHADER_TECHNIQUE(playfield_with_texture_normal_isMetal),
SHADER_TECHNIQUE(playfield_refl_without_texture),
SHADER_TECHNIQUE(playfield_refl_with_texture),
SHADER_TECHNIQUE(basic_depth_only_without_texture),
SHADER_TECHNIQUE(basic_depth_only_with_texture),
SHADER_TECHNIQUE(bg_decal_without_texture),
SHADER_TECHNIQUE(bg_decal_with_texture),
SHADER_TECHNIQUE(kickerBoolean),
SHADER_TECHNIQUE(kickerBoolean_isMetal),
SHADER_TECHNIQUE(light_with_texture),
SHADER_TECHNIQUE(light_with_texture_isMetal),
SHADER_TECHNIQUE(light_without_texture),
SHADER_TECHNIQUE(light_without_texture_isMetal),
SHADER_TECHNIQUE(basic_DMD),
SHADER_TECHNIQUE(basic_DMD_ext),
SHADER_TECHNIQUE(basic_DMD_world),
SHADER_TECHNIQUE(basic_DMD_world_ext),
SHADER_TECHNIQUE(basic_noDMD),
SHADER_TECHNIQUE(basic_noDMD_world),
SHADER_TECHNIQUE(basic_noDMD_notex),
SHADER_TECHNIQUE(AO),
SHADER_TECHNIQUE(NFAA),
SHADER_TECHNIQUE(DLAA_edge),
SHADER_TECHNIQUE(DLAA),
SHADER_TECHNIQUE(FXAA1),
SHADER_TECHNIQUE(FXAA2),
SHADER_TECHNIQUE(FXAA3),
SHADER_TECHNIQUE(fb_tonemap),
SHADER_TECHNIQUE(fb_bloom),
SHADER_TECHNIQUE(fb_AO),
SHADER_TECHNIQUE(fb_tonemap_AO),
SHADER_TECHNIQUE(fb_tonemap_AO_static),
SHADER_TECHNIQUE(fb_tonemap_no_filterRGB),
SHADER_TECHNIQUE(fb_tonemap_no_filterRG),
SHADER_TECHNIQUE(fb_tonemap_no_filterR),
SHADER_TECHNIQUE(fb_tonemap_AO_no_filter),
SHADER_TECHNIQUE(fb_tonemap_AO_no_filter_static),
SHADER_TECHNIQUE(fb_bloom_horiz9x9),
SHADER_TECHNIQUE(fb_bloom_vert9x9),
SHADER_TECHNIQUE(fb_bloom_horiz19x19),
SHADER_TECHNIQUE(fb_bloom_vert19x19),
SHADER_TECHNIQUE(fb_bloom_horiz19x19h),
SHADER_TECHNIQUE(fb_bloom_vert19x19h),
SHADER_TECHNIQUE(fb_bloom_horiz39x39),
SHADER_TECHNIQUE(fb_bloom_vert39x39),
SHADER_TECHNIQUE(fb_mirror),
SHADER_TECHNIQUE(CAS),
SHADER_TECHNIQUE(BilateralSharp_CAS),
SHADER_TECHNIQUE(SSReflection),
SHADER_TECHNIQUE(basic_noLight),
SHADER_TECHNIQUE(bulb_light),
SHADER_TECHNIQUE(SMAA_ColorEdgeDetection),
SHADER_TECHNIQUE(SMAA_BlendWeightCalculation),
SHADER_TECHNIQUE(SMAA_NeighborhoodBlending),
SHADER_TECHNIQUE(stereo),
SHADER_TECHNIQUE(stereo_Int),
SHADER_TECHNIQUE(stereo_Flipped_Int),
SHADER_TECHNIQUE(stereo_anaglyph),
SHADER_TECHNIQUE(stereo_AMD_DEBUG),
};
#undef SHADER_TECHNIQUE
ShaderTechniques Shader::getTechniqueByName(const string& name)
{
for (int i = 0; i < SHADER_TECHNIQUE_COUNT; ++i)
if (name == shaderTechniqueNames[i])
return ShaderTechniques(i);
LOG(1, m_shaderCodeName, string("getTechniqueByName Could not find technique ").append(name).append(" in shaderTechniqueNames."));
return SHADER_TECHNIQUE_INVALID;
}
#define SHADER_UNIFORM(type, name) { type, #name, ""s, SA_UNDEFINED, SA_UNDEFINED, SF_UNDEFINED }
#define SHADER_SAMPLER(name, tex_name, default_clampu, default_clampv, default_filter) { SUT_Sampler, #name, #tex_name, default_clampu, default_clampv, default_filter }
Shader::ShaderUniform Shader::shaderUniformNames[SHADER_UNIFORM_COUNT] {
// -- Matrices --
SHADER_UNIFORM(SUT_DataBlock, matrixBlock), // OpenGL only, matrices as a float block
SHADER_UNIFORM(SUT_Float4x4, matWorldViewProj), // DX9 only
SHADER_UNIFORM(SUT_Float4x4, matWorldView), // DX9 only
SHADER_UNIFORM(SUT_Float4x3, matWorldViewInverse), // DX9 only
SHADER_UNIFORM(SUT_Float3x4, matWorldViewInverseTranspose), // DX9 only
SHADER_UNIFORM(SUT_Float4x3, matView), // DX9 only
SHADER_UNIFORM(SUT_Float4x3, orientation),
// -- Floats --
SHADER_UNIFORM(SUT_Float, RenderBall),
SHADER_UNIFORM(SUT_Float, blend_modulate_vs_add),
SHADER_UNIFORM(SUT_Float, alphaTestValue),
SHADER_UNIFORM(SUT_Float, eye),
SHADER_UNIFORM(SUT_Float, fKickerScale),
SHADER_UNIFORM(SUT_Float, fSceneScale),
SHADER_UNIFORM(SUT_Float, mirrorFactor),
// -- Vectors and Float Arrays --
SHADER_UNIFORM(SUT_Float4, Roughness_WrapL_Edge_Thickness),
SHADER_UNIFORM(SUT_Float4, cBase_Alpha),
SHADER_UNIFORM(SUT_Float4, lightCenter_maxRange),
SHADER_UNIFORM(SUT_Float4, lightColor2_falloff_power),
SHADER_UNIFORM(SUT_Float4, lightColor_intensity),
SHADER_UNIFORM(SUT_Float2, fenvEmissionScale_TexWidth),
SHADER_UNIFORM(SUT_Float4, invTableRes_playfield_height_reflection),
SHADER_UNIFORM(SUT_Float4, cAmbient_LightRange),
SHADER_UNIFORM(SUT_Float4, cClearcoat_EdgeAlpha),
SHADER_UNIFORM(SUT_Float4, cGlossy_ImageLerp),
SHADER_UNIFORM(SUT_Float2, fDisableLighting_top_below),
SHADER_UNIFORM(SUT_Float4, backBoxSize),
SHADER_UNIFORM(SUT_Float4, vColor_Intensity),
SHADER_UNIFORM(SUT_Float4, w_h_height),
SHADER_UNIFORM(SUT_Float4, alphaTestValueAB_filterMode_addBlend),
SHADER_UNIFORM(SUT_Float3, amount_blend_modulate_vs_add_flasherMode),
SHADER_UNIFORM(SUT_Float4, staticColor_Alpha),
SHADER_UNIFORM(SUT_Float4, ms_zpd_ya_td),
SHADER_UNIFORM(SUT_Float2, Anaglyph_DeSaturation_Contrast),
SHADER_UNIFORM(SUT_Float4, vRes_Alpha_time),
SHADER_UNIFORM(SUT_Float4, SSR_bumpHeight_fresnelRefl_scale_FS),
SHADER_UNIFORM(SUT_Float2, AO_scale_timeblur),
SHADER_UNIFORM(SUT_Float4, cWidth_Height_MirrorAmount),
SHADER_UNIFORM(SUT_Float4v, clip_planes), // OpenGL only
SHADER_UNIFORM(SUT_Float4v, lightEmission), // OpenGL only
SHADER_UNIFORM(SUT_Float4v, lightPos), // OpenGL only
SHADER_UNIFORM(SUT_Float4v, packedLights), // DX9 only
// -- Integer and Bool --
SHADER_UNIFORM(SUT_Bool, ignoreStereo),
SHADER_UNIFORM(SUT_Bool, disableLighting),
SHADER_UNIFORM(SUT_Int, lightSources),
SHADER_UNIFORM(SUT_Bool, doNormalMapping),
SHADER_UNIFORM(SUT_Bool, is_metal),
SHADER_UNIFORM(SUT_Bool, color_grade),
SHADER_UNIFORM(SUT_Bool, do_bloom),
SHADER_UNIFORM(SUT_Bool, objectSpaceNormalMap),
SHADER_UNIFORM(SUT_Bool, do_dither),
SHADER_UNIFORM(SUT_Bool, lightingOff), // DX9 only
SHADER_UNIFORM(SUT_Float2, imageBackglassMode), // OpenGL only
// -- Samplers (a texture reference with sampling configuration) --
// DMD shader
SHADER_SAMPLER(tex_dmd, Texture0, SA_CLAMP, SA_CLAMP, SF_NONE), // DMD
SHADER_SAMPLER(tex_sprite, Texture0, SA_MIRROR, SA_MIRROR, SF_TRILINEAR), // Sprite
// Flasher shader
SHADER_SAMPLER(tex_flasher_A, Texture0, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // base texture
SHADER_SAMPLER(tex_flasher_B, Texture1, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // texB
// FB shader
SHADER_SAMPLER(tex_fb_unfiltered, Texture0, SA_CLAMP, SA_CLAMP, SF_NONE), // Framebuffer (unfiltered)
SHADER_SAMPLER(tex_fb_filtered, Texture0, SA_CLAMP, SA_CLAMP, SF_BILINEAR), // Framebuffer (filtered)
SHADER_SAMPLER(tex_mirror, Texture0, SA_CLAMP, SA_CLAMP, SF_BILINEAR), // base mirror texture
SHADER_SAMPLER(tex_bloom, Texture1, SA_CLAMP, SA_CLAMP, SF_BILINEAR), // Bloom
SHADER_SAMPLER(tex_ao, Texture3, SA_CLAMP, SA_CLAMP, SF_BILINEAR), // AO Result
SHADER_SAMPLER(tex_depth, Texture3, SA_CLAMP, SA_CLAMP, SF_NONE), // Depth
SHADER_SAMPLER(tex_color_lut, Texture4, SA_CLAMP, SA_CLAMP, SF_BILINEAR), // Color grade LUT
SHADER_SAMPLER(tex_ao_dither, Texture4, SA_REPEAT, SA_REPEAT, SF_NONE), // AO dither
// Shared material samplers for Ball, Basic and Classic light shaders
SHADER_SAMPLER(tex_env, Texture1, SA_REPEAT, SA_CLAMP, SF_TRILINEAR), // environment
SHADER_SAMPLER(tex_diffuse_env, Texture2, SA_REPEAT, SA_CLAMP, SF_BILINEAR), // diffuse environment contribution/radiance
// Ball shader
SHADER_SAMPLER(tex_ball_color, Texture0, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // base texture
SHADER_SAMPLER(tex_ball_playfield, Texture4, SA_CLAMP, SA_CLAMP, SF_TRILINEAR), // playfield
SHADER_SAMPLER(tex_ball_decal, Texture3, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // ball decal
// Basic shader
SHADER_SAMPLER(tex_base_color, Texture0, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // base texture
SHADER_SAMPLER(tex_base_transmission, Texture3, SA_CLAMP, SA_CLAMP, SF_BILINEAR), // bulb light/transmission buffer texture
SHADER_SAMPLER(tex_playfield_reflection, Texture3, SA_CLAMP, SA_CLAMP, SF_NONE), // playfield reflection
SHADER_SAMPLER(tex_base_normalmap, Texture4, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // normal map texture
// Classic light shader
SHADER_SAMPLER(tex_light_color, Texture0, SA_REPEAT, SA_REPEAT, SF_TRILINEAR), // base texture
// Stereo shader (VPVR only, combine the 2 rendered eyes into a single one)
SHADER_SAMPLER(tex_stereo_fb, Undefined, SA_CLAMP, SA_CLAMP, SF_NONE), // Framebuffer (unfiltered)
// SMAA shader
SHADER_SAMPLER(colorTex, colorTex, SA_CLAMP, SA_CLAMP, SF_BILINEAR),
SHADER_SAMPLER(colorGammaTex, colorGammaTex, SA_CLAMP, SA_CLAMP, SF_BILINEAR),
SHADER_SAMPLER(edgesTex2D, edgesTex2D, SA_CLAMP, SA_CLAMP, SF_BILINEAR),
SHADER_SAMPLER(blendTex2D, blendTex2D, SA_CLAMP, SA_CLAMP, SF_BILINEAR),
SHADER_SAMPLER(areaTex2D, areaTex2D, SA_CLAMP, SA_CLAMP, SF_BILINEAR),
SHADER_SAMPLER(searchTex2D, searchTex2D, SA_CLAMP, SA_CLAMP, SF_NONE), // Note that this should have a w address mode set to clamp as well
};
#undef SHADER_UNIFORM
#undef SHADER_SAMPLER
ShaderUniforms Shader::getUniformByName(const string& name)
{
for (int i = 0; i < SHADER_UNIFORM_COUNT; ++i)
if (name == shaderUniformNames[i].name)
return (ShaderUniforms)i;
LOG(1, m_shaderCodeName, string("getUniformByName Could not find uniform ").append(name).append(" in shaderUniformNames."));
return SHADER_UNIFORM_INVALID;
}
void Shader::SetDefaultSamplerFilter(const ShaderUniforms sampler, const SamplerFilter sf)
{
Shader::shaderUniformNames[sampler].default_filter = sf;
}
// When changed, this list must also be copied unchanged to Shader.cpp (for its implementation)
#define SHADER_ATTRIBUTE(name, shader_name) #shader_name
const string Shader::shaderAttributeNames[SHADER_ATTRIBUTE_COUNT]
{
SHADER_ATTRIBUTE(POS, vPosition),
SHADER_ATTRIBUTE(NORM, vNormal),
SHADER_ATTRIBUTE(TC, tc),
SHADER_ATTRIBUTE(TEX, tex0),
};
#undef SHADER_ATTRIBUTE
ShaderAttributes Shader::getAttributeByName(const string& name)
{
for (int i = 0; i < SHADER_ATTRIBUTE_COUNT; ++i)
if (name == shaderAttributeNames[i])
return ShaderAttributes(i);
LOG(1, m_shaderCodeName, string("getAttributeByName Could not find attribute ").append(name).append(" in shaderAttributeNames."));
return SHADER_ATTRIBUTE_INVALID;
}
Shader* Shader::current_shader = nullptr;
Shader* Shader::GetCurrentShader() { return current_shader; }
Shader::Shader(RenderDevice* renderDevice)
: currentMaterial(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX, 0xCCCCCCCC, 0xCCCCCCCC, 0xCCCCCCCC, false, false, -FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX)
{
m_renderDevice = renderDevice;
m_technique = SHADER_TECHNIQUE_INVALID;
memset(m_uniformCache, 0, sizeof(UniformCache) * SHADER_UNIFORM_COUNT * (SHADER_TECHNIQUE_COUNT + 1));
memset(m_isCacheValid, 0, sizeof(bool) * SHADER_TECHNIQUE_COUNT);
#ifdef ENABLE_SDL
logFile = nullptr;
memset(m_techniques, 0, sizeof(ShaderTechnique*) * SHADER_TECHNIQUE_COUNT);
#else
m_shader = nullptr;
memset(m_texture_cache, 0, sizeof(IDirect3DTexture9*) * TEXTURESET_STATE_CACHE_SIZE);
memset(m_bound_texture, 0, sizeof(IDirect3DTexture9*) * TEXTURESET_STATE_CACHE_SIZE);
#endif
currentAlphaTestValue = -FLT_MAX;
currentDisableLighting = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentFlasherData = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentFlasherData2 = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentFlasherColor = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightColor = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightColor2 = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightData = vec4(-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX);
currentLightImageMode = ~0u;
currentLightBackglassMode = ~0u;
}
Shader::~Shader()
{
for (int j = 0; j <= SHADER_TECHNIQUE_COUNT; ++j)
{
for (int i = 0; i < SHADER_UNIFORM_COUNT; ++i)
{
if (m_uniformCache[j][i].count > 0 && m_uniformCache[j][i].val.data)
free(m_uniformCache[j][i].val.data);
}
#ifdef ENABLE_SDL
if (j < SHADER_TECHNIQUE_COUNT && m_techniques[j] != nullptr)
{
glDeleteProgram(m_techniques[j]->program);
delete m_techniques[j];
m_techniques[j] = nullptr;
}
#endif
}
#ifndef ENABLE_SDL
SAFE_RELEASE(m_shader);
#endif
}
void Shader::Begin()
{
assert(current_shader == nullptr);
current_shader = this;
assert(m_technique != SHADER_TECHNIQUE_INVALID);
if (m_bound_technique != m_technique)
{
m_renderDevice->m_curTechniqueChanges++;
m_bound_technique = m_technique;
#ifdef ENABLE_SDL
glUseProgram(m_techniques[m_technique]->program);
#else
CHECKD3D(m_shader->SetTechnique((D3DXHANDLE)shaderTechniqueNames[m_technique].c_str()));
#endif
}
for (auto uniformName : m_uniforms[m_technique])
ApplyUniform(uniformName);
m_isCacheValid[m_technique] = true;
#ifndef ENABLE_SDL
unsigned int cPasses;
CHECKD3D(m_shader->Begin(&cPasses, 0));
CHECKD3D(m_shader->BeginPass(0));
#endif
}
void Shader::End()
{
assert(current_shader == this);
current_shader = nullptr;
#ifndef ENABLE_SDL
CHECKD3D(m_shader->EndPass());
CHECKD3D(m_shader->End());
#endif
}
void Shader::SetMaterial(const Material* const mat, const bool has_alpha)
{
COLORREF cBase, cGlossy, cClearcoat;
float fWrapLighting, fRoughness, fGlossyImageLerp, fThickness, fEdge, fEdgeAlpha, fOpacity;
bool bIsMetal, bOpacityActive;
if (mat)
{
fWrapLighting = mat->m_fWrapLighting;
fRoughness = exp2f(10.0f * mat->m_fRoughness + 1.0f); // map from 0..1 to 2..2048
fGlossyImageLerp = mat->m_fGlossyImageLerp;
fThickness = mat->m_fThickness;
fEdge = mat->m_fEdge;
fEdgeAlpha = mat->m_fEdgeAlpha;
fOpacity = mat->m_fOpacity;
cBase = mat->m_cBase;
cGlossy = mat->m_cGlossy;
cClearcoat = mat->m_cClearcoat;
bIsMetal = mat->m_bIsMetal;
bOpacityActive = mat->m_bOpacityActive;
}
else
{
fWrapLighting = 0.0f;
fRoughness = exp2f(10.0f * 0.0f + 1.0f); // map from 0..1 to 2..2048
fGlossyImageLerp = 1.0f;
fThickness = 0.05f;
fEdge = 1.0f;
fEdgeAlpha = 1.0f;
fOpacity = 1.0f;
cBase = g_pvp->m_dummyMaterial.m_cBase;
cGlossy = 0;
cClearcoat = 0;
bIsMetal = false;
bOpacityActive = false;
}
// bIsMetal is nowadays handled via a separate technique! (so not in here)
if (fRoughness != currentMaterial.m_fRoughness || fEdge != currentMaterial.m_fEdge || fWrapLighting != currentMaterial.m_fWrapLighting || fThickness != currentMaterial.m_fThickness)
{
const vec4 rwem(fRoughness, fWrapLighting, fEdge, fThickness);
SetVector(SHADER_Roughness_WrapL_Edge_Thickness, &rwem);
currentMaterial.m_fRoughness = fRoughness;
currentMaterial.m_fWrapLighting = fWrapLighting;
currentMaterial.m_fEdge = fEdge;
currentMaterial.m_fThickness = fThickness;
}
const float alpha = bOpacityActive ? fOpacity : 1.0f;
if (cBase != currentMaterial.m_cBase || alpha != currentMaterial.m_fOpacity)
{
const vec4 cBaseF = convertColor(cBase, alpha);
SetVector(SHADER_cBase_Alpha, &cBaseF);
currentMaterial.m_cBase = cBase;
currentMaterial.m_fOpacity = alpha;
}
if (!bIsMetal) // Metal has no glossy
if (cGlossy != currentMaterial.m_cGlossy || fGlossyImageLerp != currentMaterial.m_fGlossyImageLerp)
{
const vec4 cGlossyF = convertColor(cGlossy, fGlossyImageLerp);
SetVector(SHADER_cGlossy_ImageLerp, &cGlossyF);
currentMaterial.m_cGlossy = cGlossy;
currentMaterial.m_fGlossyImageLerp = fGlossyImageLerp;
}
if (cClearcoat != currentMaterial.m_cClearcoat || (bOpacityActive && fEdgeAlpha != currentMaterial.m_fEdgeAlpha))
{
const vec4 cClearcoatF = convertColor(cClearcoat, fEdgeAlpha);
SetVector(SHADER_cClearcoat_EdgeAlpha, &cClearcoatF);
currentMaterial.m_cClearcoat = cClearcoat;
currentMaterial.m_fEdgeAlpha = fEdgeAlpha;
}
if (bOpacityActive && (has_alpha || alpha < 1.0f))
g_pplayer->m_pin3d.EnableAlphaBlend(false);
else
g_pplayer->m_pin3d.m_pd3dPrimaryDevice->SetRenderState(RenderDevice::ALPHABLENDENABLE, RenderDevice::RS_FALSE);
}
void Shader::SetDisableLighting(const float value) // only set top
{
if (currentDisableLighting.x != value || currentDisableLighting.y != 0.f)
{
currentDisableLighting.x = value;
currentDisableLighting.y = 0.f;
currentDisableLighting.z = 0.f;
currentDisableLighting.w = 0.f;
SetVector(SHADER_fDisableLighting_top_below, ¤tDisableLighting);
}
}
void Shader::SetDisableLighting(const vec4& value) // sets the two top and below lighting flags, z and w unused
{
if (currentDisableLighting.x != value.x || currentDisableLighting.y != value.y)
{
currentDisableLighting = value;
SetVector(SHADER_fDisableLighting_top_below, &value);
}
}
void Shader::SetAlphaTestValue(const float value)
{
if (currentAlphaTestValue != value)
{
currentAlphaTestValue = value;
SetFloat(SHADER_alphaTestValue, value);
}
}
void Shader::SetFlasherColorAlpha(const vec4& color)
{
if (currentFlasherColor.x != color.x || currentFlasherColor.y != color.y || currentFlasherColor.z != color.z || currentFlasherColor.w != color.w)
{
currentFlasherColor = color;
SetVector(SHADER_staticColor_Alpha, &color);
}
}
vec4 Shader::GetCurrentFlasherColorAlpha() const
{
return currentFlasherColor;
}
void Shader::SetFlasherData(const vec4& c1, const vec4& c2)
{
if (currentFlasherData.x != c1.x || currentFlasherData.y != c1.y || currentFlasherData.z != c1.z || currentFlasherData.w != c1.w)
{
currentFlasherData = c1;
SetVector(SHADER_alphaTestValueAB_filterMode_addBlend, &c1);
}
if (currentFlasherData2.x != c2.x || currentFlasherData2.y != c2.y || currentFlasherData2.z != c2.z || currentFlasherData2.w != c2.w)
{
currentFlasherData2 = c2;
SetVector(SHADER_amount_blend_modulate_vs_add_flasherMode, &c2);
}
}
void Shader::SetLightColorIntensity(const vec4& color)
{
if (currentLightColor.x != color.x || currentLightColor.y != color.y || currentLightColor.z != color.z || currentLightColor.w != color.w)
{
currentLightColor = color;
SetVector(SHADER_lightColor_intensity, &color);
}
}
void Shader::SetLightColor2FalloffPower(const vec4& color)
{
if (currentLightColor2.x != color.x || currentLightColor2.y != color.y || currentLightColor2.z != color.z || currentLightColor2.w != color.w)
{
currentLightColor2 = color;
SetVector(SHADER_lightColor2_falloff_power, &color);
}
}
void Shader::SetLightData(const vec4& color)
{
if (currentLightData.x != color.x || currentLightData.y != color.y || currentLightData.z != color.z || currentLightData.w != color.w)
{
currentLightData = color;
SetVector(SHADER_lightCenter_maxRange, &color);
}
}
void Shader::SetLightImageBackglassMode(const bool imageMode, const bool backglassMode)
{
if (currentLightImageMode != (unsigned int)imageMode || currentLightBackglassMode != (unsigned int)backglassMode)
{
currentLightImageMode = (unsigned int)imageMode;
currentLightBackglassMode = (unsigned int)backglassMode;
#ifdef ENABLE_SDL
SetVector(SHADER_imageBackglassMode, imageMode ? 1.0f : 0.0f, backglassMode ? 1.0f : 0.0f, 0.0f, 0.0f);
#else
SetBool(SHADER_lightingOff, imageMode || backglassMode); // at the moment can be combined into a single bool due to what the shader actually does in the end
#endif
}
}
void Shader::SetTechnique(ShaderTechniques technique)
{
assert(current_shader != this); // Changing the technique of a used shader is not allowed (between Begin/End)
assert(0 <= technique && technique < SHADER_TECHNIQUE_COUNT);
#ifdef ENABLE_SDL
if (m_techniques[technique] == nullptr)
{
m_technique = SHADER_TECHNIQUE_INVALID;
ShowError("Fatal Error: Could not find shader technique " + shaderTechniqueNames[technique]);
exit(-1);
}
#endif
m_technique = technique;
}
void Shader::SetTechniqueMetal(ShaderTechniques technique, const bool isMetal)
{
#ifdef ENABLE_SDL
SetTechnique(technique);
SetBool(SHADER_is_metal, isMetal);
#else
if (isMetal)
{
switch (technique)
{
case SHADER_TECHNIQUE_basic_with_texture: SetTechnique(SHADER_TECHNIQUE_basic_with_texture_isMetal); break;
case SHADER_TECHNIQUE_basic_with_texture_normal: SetTechnique(SHADER_TECHNIQUE_basic_with_texture_normal_isMetal); break;
case SHADER_TECHNIQUE_basic_without_texture: SetTechnique(SHADER_TECHNIQUE_basic_without_texture_isMetal); break;
case SHADER_TECHNIQUE_playfield_with_texture: SetTechnique(SHADER_TECHNIQUE_playfield_with_texture_isMetal); break;
case SHADER_TECHNIQUE_playfield_with_texture_normal: SetTechnique(SHADER_TECHNIQUE_playfield_with_texture_normal_isMetal); break;
case SHADER_TECHNIQUE_playfield_without_texture: SetTechnique(SHADER_TECHNIQUE_playfield_without_texture_isMetal); break;
case SHADER_TECHNIQUE_kickerBoolean: SetTechnique(SHADER_TECHNIQUE_kickerBoolean_isMetal); break;
case SHADER_TECHNIQUE_light_with_texture: SetTechnique(SHADER_TECHNIQUE_light_with_texture_isMetal); break;
case SHADER_TECHNIQUE_light_without_texture: SetTechnique(SHADER_TECHNIQUE_light_without_texture_isMetal); break;
default: assert(false); // Invalid technique: no metal shading variant
}
}
else
{
SetTechnique(technique);
}
#endif
}
uint32_t Shader::CopyUniformCache(const bool copyTo, const ShaderTechniques technique, UniformCache (&uniformCache)[SHADER_UNIFORM_COUNT])
{
UniformCache* src_cache = copyTo ? m_uniformCache[SHADER_TECHNIQUE_COUNT] : uniformCache;
UniformCache* dst_cache = copyTo ? uniformCache : m_uniformCache[SHADER_TECHNIQUE_COUNT];
unsigned long sampler_hash = 0L;
for (auto uniformName : m_uniforms[technique])
{
#ifdef ENABLE_SDL
// For OpenGL uniform binding state is per technique (i.e. program)
UniformDesc desc = m_techniques[technique]->uniform_desc[uniformName];
#else
// For DX9 Effect framework uniform binding state is per shader, so we only use the first array
UniformDesc desc = m_uniform_desc[uniformName];
#endif
UniformCache* src = &(src_cache[uniformName]);
UniformCache* dst = &(dst_cache[uniformName]);
if (src->count > 0)
{
if (dst->count != src->count)
{
if (dst->count > 0)
free(dst->val.data);
dst->count = src->count;
dst->val.data = malloc(dst->count);
}
memcpy(dst->val.data, src->val.data, src->count);
}
else
{
memcpy(&(dst->val), &(src->val), sizeof(UniformCache::UniformValue));
}
if (shaderUniformNames[uniformName].type == ShaderUniformType::SUT_Sampler)
sampler_hash += (unsigned long) src->val.sampler;
}
return sampler_hash;
}
void Shader::SetMatrix(const ShaderUniforms hParameter, const Matrix3D* pMatrix)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
memcpy(elem->val.fv, pMatrix->m, 16 * sizeof(float));
ApplyUniform(hParameter);
}
void Shader::SetMatrix(const ShaderUniforms hParameter, const D3DXMATRIX* pMatrix)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
memcpy(elem->val.fv, pMatrix->m, 16 * sizeof(float));
ApplyUniform(hParameter);
}
void Shader::SetVector(const ShaderUniforms hParameter, const vec4* pVector)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
memcpy(elem->val.fv, pVector, 4 * sizeof(float));
ApplyUniform(hParameter);
}
void Shader::SetVector(const ShaderUniforms hParameter, const float x, const float y, const float z, const float w)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
elem->val.fv[0] = x;
elem->val.fv[1] = y;
elem->val.fv[2] = z;
elem->val.fv[3] = w;
ApplyUniform(hParameter);
}
void Shader::SetFloat(const ShaderUniforms hParameter, const float f)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
elem->val.f = f;
ApplyUniform(hParameter);
}
void Shader::SetInt(const ShaderUniforms hParameter, const int i)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
elem->val.i = i;
ApplyUniform(hParameter);
}
void Shader::SetBool(const ShaderUniforms hParameter, const bool b)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
assert(elem->count == 0);
elem->val.i = b ? 1 : 0;
ApplyUniform(hParameter);
}
void Shader::SetFloat4v(const ShaderUniforms hParameter, const vec4* pData, const unsigned int count)
{
assert(0 <= hParameter && hParameter < SHADER_UNIFORM_COUNT);
UniformCache* elem = &m_uniformCache[SHADER_TECHNIQUE_COUNT][hParameter];
if (elem->count == 0)
{
elem->count = count * 4 * sizeof(float);
elem->val.data = malloc(elem->count);
}
memcpy(elem->val.data, pData, elem->count);
ApplyUniform(hParameter);
}
void Shader::SetTextureNull(const ShaderUniforms texelName)
{
SetTexture(texelName, (Sampler*)nullptr);
}
void Shader::SetTexture(const ShaderUniforms texelName, Texture* texel, const SamplerFilter filter, const SamplerAddressMode clampU, const SamplerAddressMode clampV, const bool force_linear_rgb)
{
SetTexture(texelName, texel->m_pdsBuffer, filter, clampU, clampV, force_linear_rgb);
}
void Shader::SetTexture(const ShaderUniforms texelName, BaseTexture* texel, const SamplerFilter filter, const SamplerAddressMode clampU, const SamplerAddressMode clampV, const bool force_linear_rgb)
{
if (!texel)
SetTexture(texelName, (Sampler*)nullptr);
else
SetTexture(texelName, m_renderDevice->m_texMan.LoadTexture(texel, filter, clampU, clampV, force_linear_rgb));
}
void Shader::SetTexture(const ShaderUniforms uniformName, Sampler* texel)
{
#ifdef ENABLE_SDL
m_uniformCache[SHADER_TECHNIQUE_COUNT][uniformName].val.sampler = texel;
ApplyUniform(uniformName);
#else
// Since DirectX effect framework manages the samplers, we only care about the texture here
m_texture_cache[m_uniform_desc[uniformName].sampler] = texel;
#endif
}
void Shader::ApplyUniform(const ShaderUniforms uniformName)
{
if (current_shader != this)
return;
bool isCacheInvalid = !m_isCacheValid[m_technique];
UniformCache* src = &(m_uniformCache[SHADER_TECHNIQUE_COUNT][uniformName]);
#ifdef ENABLE_SDL
// For OpenGL uniform binding state is per technique (i.e. program)
auto desc = m_techniques[m_technique]->uniform_desc[uniformName];
UniformCache* dst = &(m_uniformCache[m_technique][uniformName]);
if (desc.location < 0)
return;
#else
// For DX9 Effect framework uniform binding state is per shader, so we only use the first array
auto desc = m_uniform_desc[uniformName];
UniformCache* dst = &(m_uniformCache[0][uniformName]);
#endif
switch (desc.uniform.type)
{
case SUT_DataBlock: // Uniform blocks
#ifdef ENABLE_SDL
assert(src->count == 0 || src->count == desc.count);
if ((src->count != 0) && (isCacheInvalid || dst->count == 0 || memcmp(src->val.data, dst->val.data, src->count) != 0))
{
if (dst->count == 0)
{
dst->count = src->count;
dst->val.data = malloc(src->count);
}
memcpy(dst->val.data, src->val.data, src->count);
glBindBuffer(GL_UNIFORM_BUFFER, desc.blockBuffer);
glBufferData(GL_UNIFORM_BUFFER, src->count, src->val.data, GL_STREAM_DRAW);
m_renderDevice->m_curParameterChanges++;
}
glUniformBlockBinding(m_techniques[m_technique]->program, desc.location, 0);
glBindBufferRange(GL_UNIFORM_BUFFER, 0, desc.blockBuffer, 0, src->count);
#else
assert(false); // Unsupported on DX9
#endif
break;
case SUT_Bool:
assert(src->count == 0);
if (isCacheInvalid || dst->val.i != src->val.i)
{
dst->val.i = src->val.i;
#ifdef ENABLE_SDL
glUniform1i(desc.location, src->val.i);
#else
CHECKD3D(m_shader->SetBool(desc.handle, src->val.i));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Int:
assert(src->count == 0);
if (isCacheInvalid || dst->val.i != src->val.i)
{
dst->val.i = src->val.i;
#ifdef ENABLE_SDL
glUniform1i(desc.location, src->val.i);
#else
CHECKD3D(m_shader->SetInt(desc.handle, src->val.i));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Float:
assert(src->count == 0);
if (isCacheInvalid || dst->val.f != src->val.f)
{
dst->val.f = src->val.f;
#ifdef ENABLE_SDL
glUniform1f(desc.location, src->val.f);
#else
CHECKD3D(m_shader->SetFloat(desc.handle, src->val.f));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Float2:
assert(src->count == 0);
if (isCacheInvalid || memcmp(src->val.fv, dst->val.fv, 2 * sizeof(float)) != 0)
{
memcpy(dst->val.fv, src->val.fv, 2 * sizeof(float));
#ifdef ENABLE_SDL
glUniform2fv(desc.location, 1, src->val.fv);
#else
CHECKD3D(m_shader->SetVector(desc.handle, (D3DXVECTOR4*)src->val.fv));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Float3:
assert(src->count == 0);
if (isCacheInvalid || memcmp(src->val.fv, dst->val.fv, 3 * sizeof(float)) != 0)
{
memcpy(dst->val.fv, src->val.fv, 3 * sizeof(float));
#ifdef ENABLE_SDL
glUniform3fv(desc.location, 1, src->val.fv);
#else
CHECKD3D(m_shader->SetVector(desc.handle, (D3DXVECTOR4*)src->val.fv));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Float4:
assert(src->count == 0);
if (isCacheInvalid || memcmp(src->val.fv, dst->val.fv, 4 * sizeof(float)) != 0)
{
memcpy(dst->val.fv, src->val.fv, 4 * sizeof(float));
#ifdef ENABLE_SDL
glUniform4fv(desc.location, 1, src->val.fv);
#else
CHECKD3D(m_shader->SetVector(desc.handle, (D3DXVECTOR4*)src->val.fv));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Float4v:
assert(src->count == 0 || src->count == desc.count * 4 * sizeof(float));
if ((src->count != 0) && (isCacheInvalid || dst->count == 0 || memcmp(src->val.data, dst->val.data, src->count) != 0))
{
if (dst->count == 0)
{
dst->count = src->count;
dst->val.data = malloc(src->count);
}
memcpy(dst->val.data, src->val.data, src->count);
#ifdef ENABLE_SDL
glUniform4fv(desc.location, desc.count, (float*) src->val.data);
#else
CHECKD3D(m_shader->SetFloatArray(desc.handle, (float*) src->val.data, desc.count));
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
case SUT_Float3x4:
case SUT_Float4x3:
case SUT_Float4x4:
assert(src->count == 0);
if (isCacheInvalid || memcmp(src->val.fv, dst->val.fv, 4 * 4 * sizeof(float)) != 0)
{
memcpy(dst->val.fv, src->val.fv, 4 * 4 * sizeof(float));
#ifdef ENABLE_SDL
glUniformMatrix4fv(desc.location, 1, GL_FALSE, src->val.fv);
#else
/*CHECKD3D(*/ m_shader->SetMatrix(desc.handle, (D3DXMATRIX*) src->val.fv) /*)*/; // leads to invalid calls when setting some of the matrices (as hlsl compiler optimizes some down to less than 4x4)
#endif
m_renderDevice->m_curParameterChanges++;
}
break;
#ifndef ENABLE_SDL
case SUT_Sampler:
{
// A sampler bind performs 3 things:
// - bind the texture to a texture stage (done by DirectX effect framework)
// - adjust the sampling state (filter, wrapping, ...) of the choosen texture stage (partly done by DirectX effect framework which only applies the ones defined in the effect file)
// - set the shader constant buffer to point to the selected texture stage (done by DirectX effect framework)
// So, for DirectX, we simply fetch the Texture, DirectX will then use the texture for one or more samplers, applying there default states if any
int unit = desc.sampler;
// Bind the texture to the shader
Sampler* tex = m_texture_cache[unit];
IDirect3DTexture9* bounded = m_bound_texture[unit] ? m_bound_texture[unit]->GetCoreTexture() : nullptr;
IDirect3DTexture9* tobound = tex ? tex->GetCoreTexture() : nullptr;
if (bounded != tobound)
{
CHECKD3D(m_shader->SetTexture(desc.tex_handle, tobound));
m_bound_texture[unit] = tex;
m_renderDevice->m_curTextureChanges++;
}
// Apply the texture sampling states
if (tex)
{
SamplerFilter filter = tex->GetFilter();
SamplerAddressMode clampu = tex->GetClampU();
SamplerAddressMode clampv = tex->GetClampV();
if (filter == SF_UNDEFINED)
{
filter = shaderUniformNames[uniformName].default_filter;
if (filter == SF_UNDEFINED) filter = SF_NONE;
}
if (clampu == SA_UNDEFINED)
{
clampu = shaderUniformNames[uniformName].default_clampu;
if (clampu == SA_UNDEFINED) clampu = SA_CLAMP;
}
if (clampv == SA_UNDEFINED)
{
clampv = shaderUniformNames[uniformName].default_clampv;
if (clampv == SA_UNDEFINED) clampv = SA_CLAMP;
}
m_renderDevice->SetSamplerState(unit, filter, clampu, clampv);
}
}
break;
#else
case SUT_Sampler:
{
// DX9 implementation uses preaffected texture units, not samplers, so these can not be used for OpenGL. This would cause some collisions.
Sampler* texel = m_uniformCache[SHADER_TECHNIQUE_COUNT][uniformName].val.sampler;
SamplerBinding* tex_unit = nullptr;
if (texel == nullptr)
{ // For null texture, use OpenGL texture 0 which is a predefined texture that always returns (0, 0, 0, 1)
for (auto binding : m_renderDevice->m_samplerBindings)
{
if (binding->sampler == nullptr)
{
tex_unit = binding;
break;
}
}
if (tex_unit == nullptr)
{
tex_unit = m_renderDevice->m_samplerBindings.back();
if (tex_unit->sampler != nullptr)
tex_unit->sampler->m_bindings.erase(tex_unit);
tex_unit->sampler = nullptr;
glActiveTexture(GL_TEXTURE0 + tex_unit->unit);
glBindTexture(GL_TEXTURE_2D, 0);
m_renderDevice->m_curTextureChanges++;
}
}
else
{
SamplerFilter filter = texel->GetFilter();
SamplerAddressMode clampu = texel->GetClampU();
SamplerAddressMode clampv = texel->GetClampV();
if (filter == SF_UNDEFINED)
{
filter = shaderUniformNames[uniformName].default_filter;
if (filter == SF_UNDEFINED) filter = SF_NONE;
}
if (clampu == SA_UNDEFINED)
{
clampu = shaderUniformNames[uniformName].default_clampu;
if (clampu == SA_UNDEFINED) clampu = SA_CLAMP;
}
if (clampv == SA_UNDEFINED)
{
clampv = shaderUniformNames[uniformName].default_clampv;
if (clampv == SA_UNDEFINED) clampv = SA_CLAMP;
}
for (auto binding : texel->m_bindings)
{
if (binding->filter == filter && binding->clamp_u == clampu && binding->clamp_v == clampv)
{
tex_unit = binding;
break;
}
}
if (tex_unit == nullptr)
{
tex_unit = m_renderDevice->m_samplerBindings.back();
if (tex_unit->sampler != nullptr)
tex_unit->sampler->m_bindings.erase(tex_unit);
tex_unit->sampler = texel;
tex_unit->filter = filter;
tex_unit->clamp_u = clampu;
tex_unit->clamp_v = clampv;
texel->m_bindings.insert(tex_unit);
glActiveTexture(GL_TEXTURE0 + tex_unit->unit);
glBindTexture(GL_TEXTURE_2D, texel->GetCoreTexture());
m_renderDevice->m_curTextureChanges++;
m_renderDevice->SetSamplerState(tex_unit->unit, filter, clampu, clampv);
}
}
// Bind the sampler
glUniform1i(desc.location, tex_unit->unit);
m_renderDevice->m_curParameterChanges++;
// Mark this texture unit as the last used one, and age all the others
for (int i = tex_unit->use_rank - 1; i >= 0; i--)
{
m_renderDevice->m_samplerBindings[i]->use_rank++;
m_renderDevice->m_samplerBindings[i + 1] = m_renderDevice->m_samplerBindings[i];
}
tex_unit->use_rank = 0;
m_renderDevice->m_samplerBindings[0] = tex_unit;
break;
}
default:
{
char msg[256];
sprintf_s(msg, sizeof(msg), "Unknown uniform type 0x%0002X for %s in %s", desc.uniform.type, shaderUniformNames[uniformName].name.c_str(), m_techniques[m_technique]->name.c_str());
ShowError(msg);
break;
}
#endif
}
}
#if DEBUG_LEVEL_LOG > 0
void Shader::LOG(const int level, const string& fileNameRoot, const string& message) {