From 1ce1aa2540292b30aa697d1a7724964b70579009 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Thu, 11 Jul 2024 14:54:57 +1000 Subject: [PATCH] Modify shader example to show using build in includes --- compute/post_shader/main.gd | 1 - compute/post_shader/main.tscn | 9 +++- compute/post_shader/post_process_shader.gd | 63 ++++++++++++++++++---- compute/post_shader/project.godot | 2 +- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/compute/post_shader/main.gd b/compute/post_shader/main.gd index 27f333b37df..5a39b20ac33 100644 --- a/compute/post_shader/main.gd +++ b/compute/post_shader/main.gd @@ -20,4 +20,3 @@ Shader effect: %s "Enabled" if compositor.compositor_effects[0].enabled else "Disabled", "Enabled" if compositor.compositor_effects[1].enabled else "Disabled", ] - diff --git a/compute/post_shader/main.tscn b/compute/post_shader/main.tscn index a0d16aa678e..c4c73021018 100644 --- a/compute/post_shader/main.tscn +++ b/compute/post_shader/main.tscn @@ -35,8 +35,13 @@ effect_callback_type = 4 needs_motion_vectors = false needs_normal_roughness = false script = ExtResource("1_rkpno") -shader_code = " // Invert color. - color.rgb = vec3(1.0 - color.r, 1.0 - color.g, 1.0 - color.b); +shader_code = "// Unproject +vec4 unproj = vec4(uv_norm * 2.0 - 1.0, depth, 1.0); +mat4 inv_projection_matrix = scene_data_block.data.inv_projection_matrix_view[view]; +vec4 vertex = inv_projection_matrix * unproj; +vertex.xyz = vertex.xyz / vertex.w; + +color.rgb = clamp(vec3(vertex.x/20.0, vertex.y/20.0, -vertex.z/20.0), 0.0, 1.0); " [sub_resource type="Compositor" id="Compositor_xxhi4"] diff --git a/compute/post_shader/post_process_shader.gd b/compute/post_shader/post_process_shader.gd index be6396382ec..024c473ff9c 100644 --- a/compute/post_shader/post_process_shader.gd +++ b/compute/post_shader/post_process_shader.gd @@ -4,28 +4,44 @@ class_name PostProcessShader const template_shader := """#version 450 +#define MAX_VIEWS 2 + +#include "scene_data_inc.glsl" + // Invocations in the (x, y, z) dimension. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; -layout(rgba16f, set = 0, binding = 0) uniform image2D color_image; +layout(set = 0, binding = 0, std140) uniform SceneDataBlock { + SceneData data; + SceneData prev_data; +} +scene_data_block; + +layout(rgba16f, set = 0, binding = 1) uniform image2D color_image; +layout(set = 0, binding = 2) uniform sampler2D depth_texture; // Our push constant. // Must be aligned to 16 bytes, just like the push constant we passed from the script. layout(push_constant, std430) uniform Params { vec2 raster_size; - vec2 pad; + float view; + float pad; } params; // The code we want to execute in each invocation. void main() { ivec2 uv = ivec2(gl_GlobalInvocationID.xy); ivec2 size = ivec2(params.raster_size); + int view = int(params.view); if (uv.x >= size.x || uv.y >= size.y) { return; } + vec2 uv_norm = vec2(uv) / params.raster_size; + vec4 color = imageLoad(color_image, uv); + float depth = texture(depth_texture, uv_norm).r; #COMPUTE_CODE @@ -42,6 +58,7 @@ void main() { var rd: RenderingDevice var shader: RID var pipeline: RID +var nearest_sampler : RID var mutex := Mutex.new() var shader_is_dirty := true @@ -59,6 +76,8 @@ func _notification(what: int) -> void: if shader.is_valid(): # Freeing our shader will also free any dependents such as the pipeline! RenderingServer.free_rid(shader) + if nearest_sampler.is_valid(): + rd.free_rid(nearest_sampler) #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: # Get our render scene buffers object, this gives us access to our render buffers. # Note that implementation differs per renderer hence the need for the cast. var render_scene_buffers := p_render_data.get_render_scene_buffers() - if render_scene_buffers: + var scene_data := p_render_data.get_render_scene_data() + if render_scene_buffers && scene_data: # Get our render size, this is the 3D render resolution! var size: Vector2i = render_scene_buffers.get_internal_size() if size.x == 0 and size.y == 0: @@ -137,18 +157,43 @@ func _render_callback(p_effect_callback_type: EffectCallbackType, p_render_data: 0.0, ]) + # Make sure we have a sampler + if not nearest_sampler.is_valid(): + var sampler_state : RDSamplerState = RDSamplerState.new() + sampler_state.min_filter = RenderingDevice.SAMPLER_FILTER_NEAREST + sampler_state.mag_filter = RenderingDevice.SAMPLER_FILTER_NEAREST + nearest_sampler = rd.sampler_create(sampler_state) + # Loop through views just in case we're doing stereo rendering. No extra cost if this is mono. var view_count: int = render_scene_buffers.get_view_count() for view in view_count: + # Get the RID for our scene data buffer + var scene_data_buffers: RID = scene_data.get_uniform_buffer() + # Get the RID for our color image, we will be reading from and writing to it. - var input_image: RID = render_scene_buffers.get_color_layer(view) + var color_image: RID = render_scene_buffers.get_color_layer(view) + + # Get the RID for our depth image, we will be reading from it. + var depth_image: RID = render_scene_buffers.get_depth_layer(view) # Create a uniform set, this will be cached, the cache will be cleared if our viewports configuration is changed. - var uniform := RDUniform.new() - uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_IMAGE - uniform.binding = 0 - uniform.add_id(input_image) - var uniform_set := UniformSetCacheRD.get_cache(shader, 0, [uniform]) + var scene_data_uniform := RDUniform.new() + scene_data_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_UNIFORM_BUFFER + scene_data_uniform.binding = 0 + scene_data_uniform.add_id(scene_data_buffers) + var color_uniform := RDUniform.new() + color_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_IMAGE + color_uniform.binding = 1 + color_uniform.add_id(color_image) + var depth_uniform := RDUniform.new() + depth_uniform.uniform_type = RenderingDevice.UNIFORM_TYPE_SAMPLER_WITH_TEXTURE + depth_uniform.binding = 2 + depth_uniform.add_id(nearest_sampler) + depth_uniform.add_id(depth_image) + var uniform_set := UniformSetCacheRD.get_cache(shader, 0, [scene_data_uniform, color_uniform, depth_uniform]) + + # Set our view + push_constant[2] = view # Run our compute shader. var compute_list := rd.compute_list_begin() diff --git a/compute/post_shader/project.godot b/compute/post_shader/project.godot index 2d9540b8980..a1e2faf45e9 100644 --- a/compute/post_shader/project.godot +++ b/compute/post_shader/project.godot @@ -12,7 +12,7 @@ config_version=5 config/name="Compositor Effects (Post-Processing)" run/main_scene="res://main.tscn" -config/features=PackedStringArray("4.3", "Forward Plus") +config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://icon.svg" [debug]