Debian Codegraph @

src/scene/castlerendererinternalshader_hash.inc

raw
1 {%MainUnit castlerendererinternalshader.pas}
2 {
3 Copyright 2010-2024 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Maintain a hash of the shader code. }
18
19 {$ifdef read_interface}
20
21 //type
22 { Hash of the shader code.
23 Different hash -> different shader code, must link a new shader program.
24 Same hash -> can reuse the shader program with the same hash. }
25 TShaderCodeHash = record
26 strict private
27 Sum, XorValue: UInt32;
28 public
29 procedure AddString(const S: AnsiString; const Multiplier: UInt32);
30 procedure AddInteger(const I: Integer);
31 procedure AddFloat(const F: Single; const UniquePrimeNumber: Cardinal);
32 procedure AddPointer(Ptr: Pointer);
33 procedure AddEffects(Nodes: TX3DNodeList); overload;
34 procedure AddEffects(Nodes: TMFNode); overload;
35
36 function ToString: String;
37 procedure Clear;
38
39 class operator {$ifdef FPC}={$else}Equal{$endif} (const A, B: TShaderCodeHash): boolean;
40 end;
41
42 {$endif read_interface}
43
44 {$ifdef read_implementation}
45
46 { TShaderCodeHash ------------------------------------------------------------ }
47
48 {$include norqcheckbegin.inc}
49
50 procedure TShaderCodeHash.AddString(const S: AnsiString; const Multiplier: UInt32);
51 var
52 PS: PUInt32;
53 Last: UInt32;
54 I: Integer;
55 begin
56 PS := PUInt32(S);
57
58 for I := 1 to Length(S) div 4 do
59 begin
60 Sum := Sum + PS^ * Multiplier;
61 XorValue := XorValue xor PS^;
62 Inc(PS);
63 end;
64
65 if Length(S) mod 4 <> 0 then
66 begin
67 Last := 0;
68 Move(S[(Length(S) div 4) * 4 + 1], Last, Length(S) mod 4);
69 Sum := Sum + Last * Multiplier;
70 XorValue := XorValue xor Last;
71 end;
72 end;
73
74 procedure TShaderCodeHash.AddPointer(Ptr: Pointer);
75 begin
76 { This will cut the pointer on non-32bit processors.
77 But that's not a problem --- we just want it for hash,
78 taking the least significant 32 bits from pointer is OK for this. }
79 Sum := Sum + UInt32(PtrUInt(Ptr));
80 XorValue := XorValue xor UInt32(PtrUInt(Ptr));
81 end;
82
83 procedure TShaderCodeHash.AddInteger(const I: Integer);
84 begin
85 Sum := Sum + I;
86 end;
87
88 procedure TShaderCodeHash.AddFloat(const F: Single; const UniquePrimeNumber: Cardinal);
89 begin
90 Sum := Sum + (Round(F * 1000) + 1) * UniquePrimeNumber;
91 end;
92
93 {$include norqcheckend.inc}
94
95 procedure TShaderCodeHash.AddEffects(Nodes: TX3DNodeList);
96 var
97 I: Integer;
98 begin
99 { We add to hash actual Effect node references (pointers), this way ensuring
100 that to share the same shader, effect nodes must be the same.
101 Merely equal GLSL source code is not enough (because effects with equal
102 source code may still have different uniform values, and sharing them
103 would not be handled correctly here --- we set uniform values on change,
104 not every time before rendering shape). }
105 for I := 0 to Nodes.Count - 1 do
106 if (Nodes[I] is TEffectNode) and
107 TEffectNode(Nodes[I]).FdEnabled.Value then
108 AddPointer(Nodes[I]);
109 end;
110
111 procedure TShaderCodeHash.AddEffects(Nodes: TMFNode);
112 var
113 I: Integer;
114 begin
115 for I := 0 to Nodes.Count - 1 do
116 if (Nodes[I] is TEffectNode) and
117 TEffectNode(Nodes[I]).FdEnabled.Value then
118 AddPointer(Nodes[I]);
119 end;
120
121 function TShaderCodeHash.ToString: String;
122 begin
123 Result := IntToStr(Sum) + '/' + IntToStr(XorValue);
124 end;
125
126 procedure TShaderCodeHash.Clear;
127 begin
128 Sum := 0;
129 XorValue := 0;
130 end;
131
132 class operator TShaderCodeHash.{$ifdef FPC}={$else}Equal{$endif}(const A, B: TShaderCodeHash): boolean;
133 begin
134 Result := (A.Sum = B.Sum) and (A.XorValue = B.XorValue);
135 end;
136
137 {$endif read_implementation}