@@ -4,28 +4,44 @@ class_name PostProcessShader
4
4
5
5
const template_shader := """#version 450
6
6
7
+ #define MAX_VIEWS 2
8
+
9
+ #include "scene_data_inc.glsl"
10
+
7
11
// Invocations in the (x, y, z) dimension.
8
12
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
9
13
10
- layout(rgba16f, set = 0, binding = 0) uniform image2D color_image;
14
+ layout(set = 0, binding = 0, std140) uniform SceneDataBlock {
15
+ SceneData data;
16
+ SceneData prev_data;
17
+ }
18
+ scene_data_block;
19
+
20
+ layout(rgba16f, set = 0, binding = 1) uniform image2D color_image;
21
+ layout(set = 0, binding = 2) uniform sampler2D depth_texture;
11
22
12
23
// Our push constant.
13
24
// Must be aligned to 16 bytes, just like the push constant we passed from the script.
14
25
layout(push_constant, std430) uniform Params {
15
26
vec2 raster_size;
16
- vec2 pad;
27
+ float view;
28
+ float pad;
17
29
} params;
18
30
19
31
// The code we want to execute in each invocation.
20
32
void main() {
21
33
ivec2 uv = ivec2(gl_GlobalInvocationID.xy);
22
34
ivec2 size = ivec2(params.raster_size);
35
+ int view = int(params.view);
23
36
24
37
if (uv.x >= size.x || uv.y >= size.y) {
25
38
return;
26
39
}
27
40
41
+ vec2 uv_norm = vec2(uv) / params.raster_size;
42
+
28
43
vec4 color = imageLoad(color_image, uv);
44
+ float depth = texture(depth_texture, uv_norm).r;
29
45
30
46
#COMPUTE_CODE
31
47
@@ -42,6 +58,7 @@ void main() {
42
58
var rd : RenderingDevice
43
59
var shader : RID
44
60
var pipeline : RID
61
+ var nearest_sampler : RID
45
62
46
63
var mutex := Mutex .new ()
47
64
var shader_is_dirty := true
@@ -59,6 +76,8 @@ func _notification(what: int) -> void:
59
76
if shader .is_valid ():
60
77
# Freeing our shader will also free any dependents such as the pipeline!
61
78
RenderingServer .free_rid (shader )
79
+ if nearest_sampler .is_valid ():
80
+ rd .free_rid (nearest_sampler )
62
81
63
82
64
83
#region Code in this region runs on the rendering thread.
@@ -115,7 +134,8 @@ func _render_callback(p_effect_callback_type: EffectCallbackType, p_render_data:
115
134
# Get our render scene buffers object, this gives us access to our render buffers.
116
135
# Note that implementation differs per renderer hence the need for the cast.
117
136
var render_scene_buffers := p_render_data .get_render_scene_buffers ()
118
- if render_scene_buffers :
137
+ var scene_data := p_render_data .get_render_scene_data ()
138
+ if render_scene_buffers && scene_data :
119
139
# Get our render size, this is the 3D render resolution!
120
140
var size : Vector2i = render_scene_buffers .get_internal_size ()
121
141
if size .x == 0 and size .y == 0 :
@@ -137,18 +157,43 @@ func _render_callback(p_effect_callback_type: EffectCallbackType, p_render_data:
137
157
0.0 ,
138
158
])
139
159
160
+ # Make sure we have a sampler
161
+ if not nearest_sampler .is_valid ():
162
+ var sampler_state : RDSamplerState = RDSamplerState .new ()
163
+ sampler_state .min_filter = RenderingDevice .SAMPLER_FILTER_NEAREST
164
+ sampler_state .mag_filter = RenderingDevice .SAMPLER_FILTER_NEAREST
165
+ nearest_sampler = rd .sampler_create (sampler_state )
166
+
140
167
# Loop through views just in case we're doing stereo rendering. No extra cost if this is mono.
141
168
var view_count : int = render_scene_buffers .get_view_count ()
142
169
for view in view_count :
170
+ # Get the RID for our scene data buffer
171
+ var scene_data_buffers : RID = scene_data .get_uniform_buffer ()
172
+
143
173
# Get the RID for our color image, we will be reading from and writing to it.
144
- var input_image : RID = render_scene_buffers .get_color_layer (view )
174
+ var color_image : RID = render_scene_buffers .get_color_layer (view )
175
+
176
+ # Get the RID for our depth image, we will be reading from it.
177
+ var depth_image : RID = render_scene_buffers .get_depth_layer (view )
145
178
146
179
# Create a uniform set, this will be cached, the cache will be cleared if our viewports configuration is changed.
147
- var uniform := RDUniform .new ()
148
- uniform .uniform_type = RenderingDevice .UNIFORM_TYPE_IMAGE
149
- uniform .binding = 0
150
- uniform .add_id (input_image )
151
- var uniform_set := UniformSetCacheRD .get_cache (shader , 0 , [uniform ])
180
+ var scene_data_uniform := RDUniform .new ()
181
+ scene_data_uniform .uniform_type = RenderingDevice .UNIFORM_TYPE_UNIFORM_BUFFER
182
+ scene_data_uniform .binding = 0
183
+ scene_data_uniform .add_id (scene_data_buffers )
184
+ var color_uniform := RDUniform .new ()
185
+ color_uniform .uniform_type = RenderingDevice .UNIFORM_TYPE_IMAGE
186
+ color_uniform .binding = 1
187
+ color_uniform .add_id (color_image )
188
+ var depth_uniform := RDUniform .new ()
189
+ depth_uniform .uniform_type = RenderingDevice .UNIFORM_TYPE_SAMPLER_WITH_TEXTURE
190
+ depth_uniform .binding = 2
191
+ depth_uniform .add_id (nearest_sampler )
192
+ depth_uniform .add_id (depth_image )
193
+ var uniform_set := UniformSetCacheRD .get_cache (shader , 0 , [scene_data_uniform , color_uniform , depth_uniform ])
194
+
195
+ # Set our view
196
+ push_constant [2 ] = view
152
197
153
198
# Run our compute shader.
154
199
var compute_list := rd .compute_list_begin ()
0 commit comments