Debian Codegraph @

src/scene/castleinternalscreeneffects.pas

raw
1 {
2 Copyright 2010-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Internal screen effects stuff. }
17 unit CastleInternalScreenEffects;
18
19 interface
20
21 uses CastleGLShaders, CastleRenderOptions;
22
23 type
24 { GLSL shader program specialized for rendering screen effects.
25 See https://castle-engine.io/x3d_extensions_screen_effects.php
26 about screen effects.
27
28 Do not use the ancestor AttachVertexShader and AttachFragmentShader
29 methods, instead set the @link(ScreenEffectShader).
30 This way, the standard GLSL functionality of screen effects
31 will be attached to the vertex and fragment shader code automatically.
32 At link time, this looks at current OpenGL context multi-sampling capabilities,
33 and the @link(NeedsDepth) value, to link the correct shader code. }
34 TGLSLScreenEffect = class(TGLSLProgram)
35 private
36 FScreenEffectShader: string;
37 FNeedsDepth: boolean;
38 public
39 constructor Create;
40
41 property NeedsDepth: boolean read FNeedsDepth write FNeedsDepth default false;
42
43 { In this class, UniformMissing is by default umIgnore, since it's
44 normal that screen effect doesn't use some of it's uniform variables. }
45 property UniformMissing default umIgnore;
46
47 { Attach GLSL code for the screen effect (executed as part of fragment shader).
48 See https://castle-engine.io/x3d_extensions_screen_effects.php . }
49 property ScreenEffectShader: string read FScreenEffectShader write FScreenEffectShader;
50
51 procedure Link; override;
52 end;
53
54 { Standard GLSL vertex shader for screen effect. }
55 function ScreenEffectVertex: string;
56
57 (*Library of GLSL fragment shader functions useful for screen effects.
58 This looks at current OpenGL context multi-sampling capabilities
59 to return the correct shader code.
60
61 Note that to work with OpenGLES, we have to glue all fragment shaders,
62 and ScreenEffectFragment must be before the actual shader code.
63 The string returned by this function is guaranteed to end with a newline,
64 to make it easy.
65
66 So you usually want to create screen effect shaders like this:
67
68 @longCode(#
69 Shader := TGLSLProgram.Create;
70 Shader.AttachVertexShader(ScreenEffectVertex);
71 Shader.AttachFragmentShader(
72 ScreenEffectFragment(false { or true if you use depth }) +
73 '... my custom screen effect GLSL code ...');
74 Shader.Link;
75 { umIgnore is a good idea here, in case some uniform variable
76 from ScreenEffectFragment code may be left unused. }
77 Shader.UniformMissing := umIgnore;
78 #)
79 *)
80 function ScreenEffectFragment(const Depth: boolean): string;
81
82 implementation
83
84 uses SysUtils,
85 CastleGLUtils, CastleUtils, CastleLog;
86
87 { routines ------------------------------------------------------------------- }
88
89 function ScreenEffectVertex: string;
90 begin
91 Result := {$I screen_effect.vs.inc};
92 end;
93
94 function ScreenEffectFragment(const Depth: boolean): string;
95 begin
96 Result := '';
97 if Depth then
98 Result := Result + ('#define DEPTH' +NL);
99 if GLFeatures.FBOMultiSampling then
100 begin
101 if GLFeatures.CurrentMultiSampling > 1 then
102 Result := Result +
103 '#define MULTI_SAMPLING' +NL +
104 '#define MULTI_SAMPLING_' + IntToStr(GLFeatures.CurrentMultiSampling) +NL;
105 if not (GLFeatures.CurrentMultiSampling in [1, 2, 4, 8, 16]) then
106 WritelnWarning('Screen Effects', Format('Our GLSL library for screen effects is not prepared for your number of samples (anti-aliasing): %d. ' + 'This may indicate that your GPU is very new or very weird. Please submit this as a bug (see https://castle-engine.io/forum.php for links to forum, bug tracker and more), citing this message. For now, screen effects will not work.',
107 [GLFeatures.CurrentMultiSampling]));
108 end;
109 Result := Result + ({$I screen_effect_library.glsl.inc} + NL);
110 end;
111
112 { TGLSLScreenEffect ---------------------------------------------------------- }
113
114 constructor TGLSLScreenEffect.Create;
115 begin
116 inherited;
117 UniformMissing := umIgnore;
118 Name := 'TGLSLScreenEffect';
119 end;
120
121 procedure TGLSLScreenEffect.Link;
122 var
123 VS, FS: String;
124 begin
125 if FScreenEffectShader = '' then
126 raise EGLSLError.Create('TGLSLScreenEffect shader not assigned by AttachScreenEffectShader method');
127 VS := ScreenEffectVertex;
128 FS := ScreenEffectFragment(NeedsDepth) + FScreenEffectShader;
129 AttachVertexShader(VS);
130 AttachFragmentShader(FS);
131 inherited;
132 end;
133
134 end.