Debian Codegraph @

src/scene/x3d/x3dnodes_standard_shaders.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2007-2022 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 {$ifdef read_interface}
18 TShaderPartNode = class;
19 TShaderProgramNode = class;
20
21 { Shader language used for @link(TAbstractShaderNode.Language) and @link(TEffectNode.Language). }
22 TShaderLanguage = (slDefault, slCG, slGLSL, slHLSL);
23
24 { Base type for all node types that specify a programmable shader. }
25 TAbstractShaderNode = class(TAbstractAppearanceChildNode)
26 {$I auto_generated_node_helpers/x3dnodes_x3dshadernode.inc}
27 end;
28
29 { Base type for all node types that specify
30 per-vertex attribute information to the shader. }
31 TAbstractVertexAttributeNode = class(TAbstractGeometricPropertyNode)
32 {$I auto_generated_node_helpers/x3dnodes_x3dvertexattributenode.inc}
33 end;
34
35 { OpenGL Shading Language (GLSL) shader container,
36 which should be composed from multiple @link(TShaderPartNode) instances. }
37 TComposedShaderNode = class(TAbstractShaderNode)
38 public
39 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
40 {$I auto_generated_node_helpers/x3dnodes_composedshader.inc}
41 end;
42
43 { Per-vertex single-precision floating point attributes, available to shaders. }
44 TFloatVertexAttributeNode = class(TAbstractVertexAttributeNode)
45 {$I auto_generated_node_helpers/x3dnodes_floatvertexattribute.inc}
46 end;
47
48 { Per-vertex 3x3 matrix attributes, available to shaders. }
49 TMatrix3VertexAttributeNode = class(TAbstractVertexAttributeNode)
50 {$I auto_generated_node_helpers/x3dnodes_matrix3vertexattribute.inc}
51 end;
52
53 { Per-vertex 4x4 matrix attributes, available to shaders. }
54 TMatrix4VertexAttributeNode = class(TAbstractVertexAttributeNode)
55 {$I auto_generated_node_helpers/x3dnodes_matrix4vertexattribute.inc}
56 end;
57
58 { Single file that may contain a number of shaders and combined effects.
59 @bold(Not implemented.) }
60 TPackagedShaderNode = class(TAbstractShaderNode)
61 public
62 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
63
64 {$I auto_generated_node_helpers/x3dnodes_packagedshader.inc}
65 end;
66
67 { Shader that can consist of one or more individually
68 programmable, self-contained pieces in @link(TShaderProgramNode),
69 like a Microsoft HLSL shader.
70 @bold(Not implemented.) }
71 TProgramShaderNode = class(TAbstractShaderNode)
72 {$I auto_generated_node_helpers/x3dnodes_programshader.inc}
73 end;
74
75 { OpenGL Shading Language (GLSL) shader part,
76 used inside @link(TComposedShaderNode). }
77 TShaderPartNode = class(TAbstractNode)
78 strict private
79 ContentsCached: boolean;
80 ContentsCache: String;
81 procedure EventUrlReceive(
82 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
83 function GetContents: String;
84 procedure SetContents(const Value: String);
85 public
86 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
87
88 { The shader code.
89
90 Getting this automatically loads the shader code pointed by the URL field
91 of this node. The shader code may be specified in an external file,
92 or inline using "data URI", see https://castle-engine.io/manual_network.php .
93 The shader code may also be specified using the XML "CDATA" in X3D XML encoding.
94 Returns '' of no valid URLs are present
95 (loading errors are reported to WritelnWarning).
96
97 Setting this overrides the URL field, to specify only the given shader code. }
98 property Contents: String read GetContents write SetContents;
99
100 {$I auto_generated_node_helpers/x3dnodes_shaderpart.inc}
101 end;
102
103 { Self-container shader part used with @link(TProgramShaderNode),
104 for example with Microsoft HLSL shader language.
105 @bold(Not implemented.) }
106 TShaderProgramNode = class(TAbstractNode)
107 public
108 constructor Create(const AX3DName: String = ''; const ABaseUrl: String = ''); override;
109 {$I auto_generated_node_helpers/x3dnodes_shaderprogram.inc}
110 end;
111 {$endif read_interface}
112
113 {$ifdef read_implementation}
114 const
115 ShaderLanguageNames: array [TShaderLanguage] of string =
116 ('', 'CG', 'GLSL', 'HLSL');
117
118 ShaderTypeNameX3D: array [TShaderType] of string =
119 ( 'VERTEX', 'GEOMETRY', 'FRAGMENT' );
120
121 UniformMissingNames: array [TUniformMissing] of String =
122 ( 'WARNING', 'IGNORE' );
123
124 { TComposedShaderNode -------------------------------------------------------- }
125
126 constructor TComposedShaderNode.Create(const AX3DName, ABaseUrl: String);
127 begin
128 inherited;
129
130 { Note that additional ComposedShader fields should have
131 ChangeAlways := chVisibleGeometry
132 because you want to redisplay when they change.
133
134 But CastleInternalRenderer takes care of it already. When shaders'
135 uniform variables change, we call Scene.VisibleChangeHere.
136 Implemented inside TX3DShaderProgram.EventReceive.
137
138 TODO: but when you don't use events, but directly change them
139 and call TX3DField.Changed and Scene.InternalChangedField,
140 we should do something here? }
141
142 HasInterfaceDeclarations := AllAccessTypes;
143 end;
144
145 { TPackagedShaderNode -------------------------------------------------------- }
146
147 constructor TPackagedShaderNode.Create(const AX3DName, ABaseUrl: String);
148 begin
149 inherited;
150 HasInterfaceDeclarations := AllAccessTypes;
151 AddFunctionality(TUrlFunctionality.Create(Self));
152 CDataField := FdUrl;
153 end;
154
155 { TShaderPartNode ------------------------------------------------------------ }
156
157 constructor TShaderPartNode.Create(const AX3DName, ABaseUrl: String);
158 begin
159 inherited;
160 AddFunctionality(TUrlFunctionality.Create(Self));
161 FdUrl.AddNotification({$ifdef FPC}@{$endif} EventUrlReceive);
162 CDataField := FdUrl;
163 end;
164
165 function ShaderLoadContents(const Node: TX3DNode; const FdUrl: TMFString): String;
166
167 function IsDirectShaderSource(const S: String): boolean;
168 { URL is recognized as direct shader source if it
169 doesn't start with any protocol *and*
170 has some newlines (needed to recognize relative URLs without
171 protocol correctly). }
172 begin
173 Result := (CharsPos([#10, #13], S) > 0) and (URIProtocol(S) = '');
174 end;
175
176 var
177 I: Integer;
178 FullUrl, MimeType: String;
179 begin
180 Result := '';
181
182 for I := 0 to FdUrl.Items.Count - 1 do
183 begin
184 if IsDirectShaderSource(FdUrl.Items[I]) then
185 begin
186 Result := FdUrl.Items[I];
187 Exit;
188 end else
189 begin
190 FullUrl := Node.PathFromBaseUrl(FdUrl.Items[I]);
191 try
192 Result := FileToString(FullUrl, MimeType);
193 if (MimeType <> '') and
194 (MimeType <> 'text/plain') then
195 WritelnWarning('VRML/X3D', Format('MIME type "%s" not recognized as shader source. Using anyway. If this is a valid shader source mime type that should be recognized, please submit a feature request.', [MimeType]));
196 Exit;
197 except
198 on E: Exception do
199 { Remember that WritelnWarning *may* raise an exception. }
200 WritelnWarning('VRML/X3D', Format(SLoadError,
201 [E.ClassName, 'shader source', UriDisplay(FullUrl), E.Message]));
202 end;
203 end;
204 end;
205 end;
206
207 function TShaderPartNode.GetContents: String;
208 begin
209 if not ContentsCached then
210 begin
211 ContentsCache := ShaderLoadContents(Self, FdUrl);
212 ContentsCached := true;
213 end;
214 Result := ContentsCache;
215 end;
216
217 procedure TShaderPartNode.SetContents(const Value: String);
218 begin
219 SetUrl(['data:text/plain,' + Value]);
220
221 //Assert(Value = GetContents);
222 // an optimized way to initialize ContentsCache
223 ContentsCache := Value;
224 ContentsCached := true;
225 end;
226
227 procedure TShaderPartNode.EventUrlReceive(
228 const Event: TX3DEvent; const Value: TX3DField; const Time: TX3DTime);
229 begin
230 ContentsCached := false;
231 end;
232
233 { TShaderProgramNode --------------------------------------------------------- }
234
235 constructor TShaderProgramNode.Create(const AX3DName, ABaseUrl: String);
236 begin
237 inherited;
238 AddFunctionality(TUrlFunctionality.Create(Self));
239 CDataField := FdUrl;
240 HasInterfaceDeclarations := AllAccessTypes;
241 end;
242
243 procedure RegisterShadersNodes;
244 begin
245 NodesManager.RegisterNodeClasses([
246 TComposedShaderNode,
247 TFloatVertexAttributeNode,
248 TMatrix3VertexAttributeNode,
249 TMatrix4VertexAttributeNode,
250 TPackagedShaderNode,
251 TProgramShaderNode,
252 TShaderPartNode,
253 TShaderProgramNode
254 ]);
255 end;
256 {$endif read_implementation}