diff --git a/modules/openxr/extensions/platform/openxr_vulkan_extension.cpp b/modules/openxr/extensions/platform/openxr_vulkan_extension.cpp index da613f8435ff..4bfaa79b3a8f 100644 --- a/modules/openxr/extensions/platform/openxr_vulkan_extension.cpp +++ b/modules/openxr/extensions/platform/openxr_vulkan_extension.cpp @@ -308,6 +308,10 @@ bool OpenXRVulkanExtension::get_swapchain_image_data(XrSwapchain p_swapchain, in format = RenderingDevice::DATA_FORMAT_B8G8R8A8_UINT; usage_flags |= RenderingDevice::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; break; + case VK_FORMAT_R16G16B16A16_SFLOAT: + format = RenderingDevice::DATA_FORMAT_R16G16B16A16_SFLOAT; + usage_flags |= RenderingDevice::TEXTURE_USAGE_COLOR_ATTACHMENT_BIT; + break; case VK_FORMAT_D32_SFLOAT: format = RenderingDevice::DATA_FORMAT_D32_SFLOAT; usage_flags |= RenderingDevice::TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; diff --git a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp index 20f8ef02c07d..7b6899f70db5 100644 --- a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp +++ b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp @@ -156,6 +156,34 @@ void RenderForwardMobile::RenderBufferDataForwardMobile::configure(RenderSceneBu ERR_FAIL_NULL(render_buffers); // Huh? really? } +RID RendererSceneRenderImplementation::RenderForwardMobile::RenderBufferDataForwardMobile::get_motion_vectors_fb() { + ERR_FAIL_NULL_V(render_buffers, RID()); + + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + ERR_FAIL_NULL_V(texture_storage, RID()); + + RID velocity = render_buffers->get_velocity_buffer(false); + RID velocity_depth = render_buffers->get_velocity_depth_buffer(); + + if (velocity.is_valid() && velocity_depth.is_valid()) { + Vector textures; + textures.push_back(velocity); + textures.push_back(velocity_depth); + + Vector passes; + RD::FramebufferPass pass; + pass.color_attachments.push_back(0); + pass.depth_attachment = 1; + passes.push_back(pass); + + uint32_t view_count = render_buffers->get_view_count(); + + return FramebufferCacheRD::get_singleton()->get_cache_multipass(textures, passes, view_count); + } + + return RID(); +} + RID RenderForwardMobile::RenderBufferDataForwardMobile::get_color_fbs(FramebufferConfigType p_config_type) { ERR_FAIL_NULL_V(render_buffers, RID()); @@ -754,19 +782,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color RENDER_TIMESTAMP("Setup 3D Scene"); - /* TODO - // check if we need motion vectors - if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_MOTION_VECTORS) { - p_render_data->scene_data->calculate_motion_vectors = true; - } else if (_compositor_effects_has_flag(p_render_data, RS::COMPOSITOR_EFFECT_FLAG_NEEDS_MOTION_VECTORS)) { - p_render_data->scene_data->calculate_motion_vectors = true; - } else if (render target has velocity override) { // TODO - p_render_data->scene_data->calculate_motion_vectors = true; - } else { - p_render_data->scene_data->calculate_motion_vectors = false; - } - */ - p_render_data->scene_data->calculate_motion_vectors = false; // for now, not yet supported... + p_render_data->scene_data->calculate_motion_vectors = RendererRD::TextureStorage::get_singleton()->render_target_get_override_velocity(rb->get_render_target()).is_valid(); p_render_data->scene_data->directional_light_count = 0; p_render_data->scene_data->opaque_prepass_threshold = 0.0; @@ -1050,6 +1066,25 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color breadcrumb = RDD::BreadcrumbMarker::REFLECTION_PROBES; } + if (rb_data.is_valid()) { + RID mv_fb = rb_data->get_motion_vectors_fb(); + + if (mv_fb.is_valid()) { + RENDER_TIMESTAMP("Render Motion Vectors"); + + Vector mv_pass_clear; + mv_pass_clear.push_back(Color(0, 0, 0, 0)); + + RD::get_singleton()->draw_command_begin_label("Render Motion Vectors"); + + RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, nullptr, RID(), samplers); + RenderListParameters render_list_params(render_list[RENDER_LIST_OPAQUE].elements.ptr(), render_list[RENDER_LIST_OPAQUE].element_info.ptr(), render_list[RENDER_LIST_OPAQUE].elements.size(), reverse_cull, PASS_MODE_MOTION_VECTORS, rp_uniform_set, base_specialization); + _render_list_with_draw_list(&render_list_params, mv_fb, RD::DRAW_CLEAR_ALL, mv_pass_clear); + + RD::get_singleton()->draw_command_end_label(); + } + } + // opaque pass RD::get_singleton()->draw_command_begin_label("Render Opaque"); @@ -1814,14 +1849,22 @@ void RenderForwardMobile::_fill_instance_data(RenderListType p_render_list, uint scene_state.instance_data[p_render_list].resize(p_offset + element_total); rl->element_info.resize(p_offset + element_total); + uint64_t frame = RSG::rasterizer->get_frame_number(); + for (uint32_t i = 0; i < element_total; i++) { GeometryInstanceSurfaceDataCache *surface = rl->elements[i + p_offset]; GeometryInstanceForwardMobile *inst = surface->owner; SceneState::InstanceData &instance_data = scene_state.instance_data[p_render_list][i + p_offset]; + if (inst->prev_transform_dirty && frame > inst->prev_transform_change_frame + 1 && inst->prev_transform_change_frame) { + inst->prev_transform = inst->transform; + inst->prev_transform_dirty = false; + } + if (inst->store_transform_cache) { RendererRD::MaterialStorage::store_transform(inst->transform, instance_data.transform); + RendererRD::MaterialStorage::store_transform(inst->prev_transform, instance_data.prev_transform); #ifdef REAL_T_IS_DOUBLE // Split the origin into two components, the float approximation and the missing precision. @@ -1832,6 +1875,7 @@ void RenderForwardMobile::_fill_instance_data(RenderListType p_render_list, uint #endif } else { RendererRD::MaterialStorage::store_transform(Transform3D(), instance_data.transform); + RendererRD::MaterialStorage::store_transform(Transform3D(), instance_data.prev_transform); } instance_data.flags = inst->flags_cache; @@ -2026,7 +2070,7 @@ void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, const } // ADD Element - if (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) { + if (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT || p_pass_mode == PASS_MODE_MOTION_VECTORS) { #ifdef DEBUG_ENABLED bool force_alpha = unlikely(get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_OVERDRAW); #else @@ -2115,6 +2159,9 @@ void RenderForwardMobile::_render_list(RenderingDevice::DrawListID p_draw_list, case PASS_MODE_DEPTH_MATERIAL: { _render_list_template(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); } break; + case PASS_MODE_MOTION_VECTORS: { + _render_list_template(p_draw_list, p_framebuffer_Format, p_params, p_from_element, p_to_element); + } } } @@ -2130,6 +2177,7 @@ void RenderForwardMobile::_render_list_with_draw_list(RenderListParameters *p_pa template void RenderForwardMobile::_render_list_template(RenderingDevice::DrawListID p_draw_list, RenderingDevice::FramebufferFormatID p_framebuffer_Format, RenderListParameters *p_params, uint32_t p_from_element, uint32_t p_to_element) { RendererRD::MeshStorage *mesh_storage = RendererRD::MeshStorage::get_singleton(); + RendererRD::ParticlesStorage *particles_storage = RendererRD::ParticlesStorage::get_singleton(); RD::DrawListID draw_list = p_draw_list; RD::FramebufferFormatID framebuffer_format = p_framebuffer_Format; @@ -2261,6 +2309,9 @@ void RenderForwardMobile::_render_list_template(RenderingDevice::DrawListID p_dr ERR_FAIL_COND_MSG(p_params->view_count > 1, "Multiview not supported for material pass"); pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL; } break; + case PASS_MODE_MOTION_VECTORS: { + pipeline_key.version = SceneShaderForwardMobile::SHADER_VERSION_MOTION_VECTORS; + } } pipeline_key.framebuffer_format_id = framebuffer_format; @@ -2279,9 +2330,9 @@ void RenderForwardMobile::_render_list_template(RenderingDevice::DrawListID p_dr // Skeleton and blend shape. uint64_t input_mask = shader->get_vertex_input_mask(pipeline_key.version, pipeline_key.ubershader); if (surf->owner->mesh_instance.is_valid()) { - mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, input_mask, false, vertex_array_rd, vertex_format); + mesh_storage->mesh_instance_surface_get_vertex_arrays_and_format(surf->owner->mesh_instance, surf->surface_index, input_mask, p_pass_mode == PASS_MODE_MOTION_VECTORS, vertex_array_rd, vertex_format); } else { - mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, input_mask, false, vertex_array_rd, vertex_format); + mesh_storage->mesh_surface_get_vertex_arrays_and_format(mesh_surface, input_mask, p_pass_mode == PASS_MODE_MOTION_VECTORS, vertex_array_rd, vertex_format); } index_array_rd = mesh_storage->mesh_surface_get_index_array(mesh_surface, element_info.lod_index); @@ -2349,6 +2400,15 @@ void RenderForwardMobile::_render_list_template(RenderingDevice::DrawListID p_dr prev_material_uniform_set = material_uniform_set; } + if (surf->owner->base_flags & INSTANCE_DATA_FLAG_PARTICLES) { + particles_storage->particles_get_instance_buffer_motion_vectors_offsets(surf->owner->data->base, push_constant.multimesh_motion_vectors_current_offset, push_constant.multimesh_motion_vectors_previous_offset); + } else if (surf->owner->base_flags & INSTANCE_DATA_FLAG_MULTIMESH) { + mesh_storage->_multimesh_get_motion_vectors_offsets(surf->owner->data->base, push_constant.multimesh_motion_vectors_current_offset, push_constant.multimesh_motion_vectors_previous_offset); + } else { + push_constant.multimesh_motion_vectors_current_offset = 0; + push_constant.multimesh_motion_vectors_previous_offset = 0; + } + size_t push_constant_size = 0; if (pipeline_key.ubershader) { push_constant_size = sizeof(SceneState::PushConstant); @@ -2396,6 +2456,17 @@ RenderGeometryInstance *RenderForwardMobile::geometry_instance_create(RID p_base return ginstance; } +void RendererSceneRenderImplementation::RenderForwardMobile::GeometryInstanceForwardMobile::set_transform(const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabb) { + uint64_t frame = RSG::rasterizer->get_frame_number(); + if (frame != prev_transform_change_frame) { + prev_transform = transform; + prev_transform_change_frame = frame; + prev_transform_dirty = true; + } + + RenderGeometryInstanceBase::set_transform(p_transform, p_aabb, p_transformed_aabb); +} + void RenderForwardMobile::GeometryInstanceForwardMobile::set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) { lightmap_instance = p_lightmap_instance; lightmap_uv_scale = p_lightmap_uv_scale; @@ -2800,6 +2871,7 @@ void RenderForwardMobile::_geometry_instance_update(RenderGeometryInstance *p_ge ginstance->transforms_uniform_set = mesh_storage->multimesh_get_3d_uniform_set(ginstance->data->base, scene_shader.default_shader_rd, TRANSFORMS_UNIFORM_SET); } else if (ginstance->data->base_type == RS::INSTANCE_PARTICLES) { + ginstance->base_flags |= INSTANCE_DATA_FLAG_PARTICLES; ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH; if (false) { // 2D particles ginstance->base_flags |= INSTANCE_DATA_FLAG_MULTIMESH_FORMAT_2D; diff --git a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h index 0969dd0b5082..0980ea7c6455 100644 --- a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h +++ b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.h @@ -92,6 +92,8 @@ class RenderForwardMobile : public RendererSceneRenderRD { virtual void free_data() override; virtual void configure(RenderSceneBuffersRD *p_render_buffers) override; + RID get_motion_vectors_fb(); + private: RenderSceneBuffersRD *render_buffers = nullptr; }; @@ -111,6 +113,7 @@ class RenderForwardMobile : public RendererSceneRenderRD { // PASS_MODE_DEPTH_NORMAL_ROUGHNESS_VOXEL_GI, PASS_MODE_DEPTH_MATERIAL, // PASS_MODE_SDF, + PASS_MODE_MOTION_VECTORS, }; struct RenderElementInfo; @@ -205,11 +208,16 @@ class RenderForwardMobile : public RendererSceneRenderRD { float uv_offset[2]; uint32_t base_index; uint32_t pad; + uint32_t multimesh_motion_vectors_current_offset; + uint32_t multimesh_motion_vectors_previous_offset; + uint32_t pad2; + uint32_t pad3; PushConstantUbershader ubershader; }; struct InstanceData { float transform[16]; + float prev_transform[16]; uint32_t flags; uint32_t instance_uniforms_ofs; // Base offset in global buffer for instance variables. uint32_t gi_offset; // GI information when using lightmapping (VCT or lightmap index). @@ -454,6 +462,10 @@ class RenderForwardMobile : public RendererSceneRenderRD { uint32_t instance_count = 0; uint32_t trail_steps = 1; + uint64_t prev_transform_change_frame = UINT_MAX; + bool prev_transform_dirty = true; + Transform3D prev_transform; + // lightmap uint32_t gi_offset_cache = 0; // !BAS! Should rename this to lightmap_offset_cache, in forward clustered this was shared between gi and lightmap RID lightmap_instance; @@ -481,6 +493,7 @@ class RenderForwardMobile : public RendererSceneRenderRD { virtual void _mark_dirty() override; + virtual void set_transform(const Transform3D &p_transform, const AABB &p_aabb, const AABB &p_transformed_aabb) override; virtual void set_use_lightmap(RID p_lightmap_instance, const Rect2 &p_lightmap_uv_scale, int p_lightmap_slice_index) override; virtual void set_lightmap_capture(const Color *p_sh9) override; diff --git a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp index eb2d25f2a7dd..d7ecc1a18528 100644 --- a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp +++ b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.cpp @@ -295,7 +295,7 @@ void SceneShaderForwardMobile::ShaderData::_create_pipeline(PipelineKey p_pipeli multisample_state.enable_alpha_to_one = true; } - if (p_pipeline_key.version == SHADER_VERSION_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW) { + if (p_pipeline_key.version == SHADER_VERSION_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_MOTION_VECTORS) { blend_state = blend_state_blend; if (depth_draw == DEPTH_DRAW_OPAQUE && !uses_alpha_clip) { // Alpha does not write to depth. @@ -310,7 +310,7 @@ void SceneShaderForwardMobile::ShaderData::_create_pipeline(PipelineKey p_pipeli // Do not use this version (error case). } } else { - if (p_pipeline_key.version == SHADER_VERSION_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW) { + if (p_pipeline_key.version == SHADER_VERSION_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS || p_pipeline_key.version == SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_MOTION_VECTORS) { blend_state = blend_state_opaque; } else if (p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS || p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS_MULTIVIEW || p_pipeline_key.version == SHADER_VERSION_SHADOW_PASS_DP) { // Contains nothing. @@ -490,6 +490,7 @@ void SceneShaderForwardMobile::init(const String p_defines) { shader_versions.push_back(base_define + "\n#define MODE_RENDER_DEPTH\n"); // SHADER_VERSION_SHADOW_PASS, should probably change this to MODE_RENDER_SHADOW because we don't have a depth pass here... shader_versions.push_back(base_define + "\n#define MODE_RENDER_DEPTH\n#define MODE_DUAL_PARABOLOID\n"); // SHADER_VERSION_SHADOW_PASS_DP shader_versions.push_back(base_define + "\n#define MODE_RENDER_DEPTH\n#define MODE_RENDER_MATERIAL\n"); // SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL + shader_versions.push_back(base_define + "\n#define MODE_RENDER_MOTION_VECTORS\n#define USE_MULTIVIEW\n"); // SHADER_VERSION_MOTION_VECTORS // Multiview versions of our shaders. shader_versions.push_back(base_define + "\n#define USE_MULTIVIEW\n"); // SHADER_VERSION_COLOR_PASS_MULTIVIEW diff --git a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h index a40c5446c74a..e693d466900c 100644 --- a/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h +++ b/servers/rendering/renderer_rd/forward_mobile/scene_shader_forward_mobile.h @@ -49,6 +49,7 @@ class SceneShaderForwardMobile { SHADER_VERSION_SHADOW_PASS, SHADER_VERSION_SHADOW_PASS_DP, SHADER_VERSION_DEPTH_PASS_WITH_MATERIAL, + SHADER_VERSION_MOTION_VECTORS, SHADER_VERSION_COLOR_PASS_MULTIVIEW, SHADER_VERSION_LIGHTMAP_COLOR_PASS_MULTIVIEW, diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl index a3c264b82366..c24226046430 100644 --- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl @@ -60,6 +60,15 @@ layout(location = 10) in uvec4 bone_attrib; layout(location = 11) in vec4 weight_attrib; #endif +#if defined(MODE_RENDER_MOTION_VECTORS) +layout(location = 12) in vec4 previous_vertex_attrib; + +#if defined(NORMAL_USED) || defined(TANGENT_USED) +layout(location = 13) in vec4 previous_normal_attrib; +#endif + +#endif // MODE_RENDER_MOTION_VECTORS + vec3 oct_to_vec3(vec2 e) { vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y)); float t = max(-v.z, 0.0); @@ -121,6 +130,11 @@ layout(location = 9) out highp float dp_clip; #endif +#if defined(MODE_RENDER_MOTION_VECTORS) +layout(location = 12) out highp vec4 screen_position; +layout(location = 13) out highp vec4 prev_screen_position; +#endif + #ifdef USE_MULTIVIEW #ifdef has_VK_KHR_multiview #define ViewIndex gl_ViewIndex @@ -185,15 +199,77 @@ uint multimesh_stride() { return stride; } -void main() { +void _unpack_vertex_attributes(vec4 p_vertex_in, vec3 p_compressed_aabb_position, vec3 p_compressed_aabb_size, +#if defined(NORMAL_USED) || defined(TANGENT_USED) + vec4 p_normal_in, +#ifdef NORMAL_USED + out vec3 r_normal, +#endif + out vec3 r_tangent, + out vec3 r_binormal, +#endif + out vec3 r_vertex) { + + r_vertex = p_vertex_in.xyz * p_compressed_aabb_size + p_compressed_aabb_position; +#ifdef NORMAL_USED + r_normal = oct_to_vec3(p_normal_in.xy * 2.0 - 1.0); +#endif + +#if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) + + float binormal_sign; + + // This works because the oct value (0, 1) maps onto (0, 0, -1) which encodes to (1, 1). + // Accordingly, if p_normal_in.z contains octahedral values, it won't equal (0, 1). + if (p_normal_in.z > 0.0 || p_normal_in.w < 1.0) { + // Uncompressed format. + vec2 signed_tangent_attrib = p_normal_in.zw * 2.0 - 1.0; + r_tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0)); + binormal_sign = sign(signed_tangent_attrib.y); + r_binormal = normalize(cross(r_normal, r_tangent) * binormal_sign); + } else { + // Compressed format. + float angle = p_vertex_in.w; + binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller. + angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably. + vec3 axis = r_normal; + axis_angle_to_tbn(axis, angle, r_tangent, r_binormal, r_normal); + r_binormal *= binormal_sign; + } +#endif +} + +void vertex_shader(in vec3 vertex, +#ifdef NORMAL_USED + in vec3 normal, +#endif +#if defined(NORMAL_USED) || defined(TANGENT_USED) + in vec3 tangent, + in vec3 binormal, +#endif + in uint instance_index, in uint multimesh_offset, in mat4 model_matrix, + in mat4 inv_view_matrix, +#ifdef USE_MULTIVIEW + in mat4 scene_projection_matrix_view, in mat4 scene_inv_projection_matrix_view, in vec4 scene_eye_offset, +#else + in mat4 scene_projection_matrix, in mat4 scene_inv_projection_matrix, +#endif + in mat4 scene_view_matrix, in vec2 scene_viewport_size, +#ifdef MODE_DUAL_PARABOLOID + in float scene_dual_paraboloid_side, in float scene_z_far, +#endif +#ifdef MODE_RENDER_DEPTH + in bool scene_pancake_shadows, +#endif +#ifdef MODE_RENDER_MATERIAL + in bool scene_material_uv2_mode, +#endif + out vec4 screen_position_output) { vec4 instance_custom = vec4(0.0); #if defined(COLOR_USED) color_interp = color_attrib; #endif - mat4 model_matrix = instances.data[draw_call.instance_index].transform; - mat4 inv_view_matrix = scene_data.inv_view_matrix; - #ifdef USE_DOUBLE_PRECISION vec3 model_precision = vec3(model_matrix[0][3], model_matrix[1][3], model_matrix[2][3]); model_matrix[0][3] = 0.0; @@ -206,7 +282,7 @@ void main() { #endif mat3 model_normal_matrix; - if (bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) { + if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) { model_normal_matrix = transpose(inverse(mat3(model_matrix))); } else { model_normal_matrix = mat3(model_matrix); @@ -219,7 +295,7 @@ void main() { //multimesh, instances are for it #ifdef USE_PARTICLE_TRAILS - uint trail_size = (instances.data[draw_call.instance_index].flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK; + uint trail_size = (instances.data[instance_index].flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK; uint stride = 3 + 1 + 1; //particles always uses this format uint offset = trail_size * stride * gl_InstanceIndex; @@ -264,7 +340,7 @@ void main() { #else uint stride = multimesh_stride(); - uint offset = stride * gl_InstanceIndex; + uint offset = stride * (gl_InstanceIndex + multimesh_offset); if (sc_multimesh_format_2d()) { matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)); @@ -300,33 +376,6 @@ void main() { model_normal_matrix = model_normal_matrix * mat3(matrix); } - vec3 vertex = vertex_angle_attrib.xyz * instances.data[draw_call.instance_index].compressed_aabb_size_pad.xyz + instances.data[draw_call.instance_index].compressed_aabb_position_pad.xyz; -#ifdef NORMAL_USED - vec3 normal = oct_to_vec3(axis_tangent_attrib.xy * 2.0 - 1.0); -#endif - -#if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) - - vec3 binormal; - float binormal_sign; - vec3 tangent; - if (axis_tangent_attrib.z > 0.0 || axis_tangent_attrib.w < 1.0) { - // Uncompressed format. - vec2 signed_tangent_attrib = axis_tangent_attrib.zw * 2.0 - 1.0; - tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0)); - binormal_sign = sign(signed_tangent_attrib.y); - binormal = normalize(cross(normal, tangent) * binormal_sign); - } else { - // Compressed format. - float angle = vertex_angle_attrib.w; - binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller. - angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably. - vec3 axis = normal; - axis_angle_to_tbn(axis, angle, tangent, binormal, normal); - binormal *= binormal_sign; - } -#endif - #ifdef UV_USED uv_interp = uv_attrib; #endif @@ -335,7 +384,7 @@ void main() { uv2_interp = uv2_attrib; #endif - vec4 uv_scale = instances.data[draw_call.instance_index].uv_scale; + vec4 uv_scale = instances.data[instance_index].uv_scale; if (uv_scale != vec4(0.0)) { // Compression enabled #ifdef UV_USED @@ -351,12 +400,12 @@ void main() { #endif #ifdef USE_MULTIVIEW - mat4 projection_matrix = scene_data.projection_matrix_view[ViewIndex]; - mat4 inv_projection_matrix = scene_data.inv_projection_matrix_view[ViewIndex]; - vec3 eye_offset = scene_data.eye_offset[ViewIndex].xyz; + mat4 projection_matrix = scene_projection_matrix_view; + mat4 inv_projection_matrix = scene_inv_projection_matrix_view; + vec3 eye_offset = scene_eye_offset.xyz; #else - mat4 projection_matrix = scene_data.projection_matrix; - mat4 inv_projection_matrix = scene_data.inv_projection_matrix; + mat4 projection_matrix = scene_projection_matrix; + mat4 inv_projection_matrix = scene_inv_projection_matrix; vec3 eye_offset = vec3(0.0, 0.0, 0.0); #endif //USE_MULTIVIEW @@ -379,10 +428,10 @@ void main() { float roughness = 1.0; - mat4 modelview = scene_data.view_matrix * model_matrix; - mat3 modelview_normal = mat3(scene_data.view_matrix) * model_normal_matrix; - mat4 read_view_matrix = scene_data.view_matrix; - vec2 read_viewport_size = scene_data.viewport_size; + mat4 modelview = scene_view_matrix * model_matrix; + mat3 modelview_normal = mat3(scene_view_matrix) * model_normal_matrix; + mat4 read_view_matrix = scene_view_matrix; + vec2 read_viewport_size = scene_viewport_size; { #CODE : VERTEX @@ -402,8 +451,8 @@ void main() { } vertex = mat3(inv_view_matrix * modelview) * vertex; vec3 temp_precision; - vertex += double_add_vec3(model_origin, model_precision, scene_data.inv_view_matrix[3].xyz, view_precision, temp_precision); - vertex = mat3(scene_data.view_matrix) * vertex; + vertex += double_add_vec3(model_origin, model_precision, inv_view_matrix[3].xyz, view_precision, temp_precision); + vertex = mat3(scene_view_matrix) * vertex; #else vertex = (modelview * vec4(vertex, 1.0)).xyz; #endif @@ -421,14 +470,14 @@ void main() { //using world coordinates #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - vertex = (scene_data.view_matrix * vec4(vertex, 1.0)).xyz; + vertex = (scene_view_matrix * vec4(vertex, 1.0)).xyz; #ifdef NORMAL_USED - normal = (scene_data.view_matrix * vec4(normal, 0.0)).xyz; + normal = (scene_view_matrix * vec4(normal, 0.0)).xyz; #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) - binormal = (scene_data.view_matrix * vec4(binormal, 0.0)).xyz; - tangent = (scene_data.view_matrix * vec4(tangent, 0.0)).xyz; + binormal = (scene_view_matrix * vec4(binormal, 0.0)).xyz; + tangent = (scene_view_matrix * vec4(tangent, 0.0)).xyz; #endif #endif @@ -453,13 +502,13 @@ void main() { diffuse_light_interp = vec4(0.0); specular_light_interp = vec4(0.0); - uvec2 omni_light_indices = instances.data[draw_call.instance_index].omni_lights; + uvec2 omni_light_indices = instances.data[instance_index].omni_lights; for (uint i = 0; i < sc_omni_lights(); i++) { uint light_index = (i > 3) ? ((omni_light_indices.y >> ((i - 4) * 8)) & 0xFF) : ((omni_light_indices.x >> (i * 8)) & 0xFF); light_process_omni_vertex(light_index, vertex, view, normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb); } - uvec2 spot_light_indices = instances.data[draw_call.instance_index].spot_lights; + uvec2 spot_light_indices = instances.data[instance_index].spot_lights; for (uint i = 0; i < sc_spot_lights(); i++) { uint light_index = (i > 3) ? ((spot_light_indices.y >> ((i - 4) * 8)) & 0xFF) : ((spot_light_indices.x >> (i * 8)) & 0xFF); light_process_spot_vertex(light_index, vertex, view, normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb); @@ -471,11 +520,11 @@ void main() { vec3 directional_specular = vec3(0.0); for (uint i = 0; i < sc_directional_lights(); i++) { - if (!bool(directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) { + if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) { continue; // Not masked, skip. } - if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { + if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { continue; // Statically baked light and object uses lightmap, skip. } if (i == 0) { @@ -521,7 +570,7 @@ void main() { #ifdef MODE_DUAL_PARABOLOID - vertex_interp.z *= scene_data.dual_paraboloid_side; + vertex_interp.z *= scene_dual_paraboloid_side; dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias @@ -531,7 +580,7 @@ void main() { float distance = length(vtx); vtx = normalize(vtx); vtx.xy /= 1.0 - vtx.z; - vtx.z = (distance / scene_data.z_far); + vtx.z = (distance / scene_z_far); vtx.z = vtx.z * 2.0 - 1.0; vertex_interp = vtx; @@ -540,27 +589,135 @@ void main() { #endif //MODE_RENDER_DEPTH #ifdef OVERRIDE_POSITION - gl_Position = position; + screen_position_output = position; #else - gl_Position = projection_matrix * vec4(vertex_interp, 1.0); + screen_position_output = projection_matrix * vec4(vertex_interp, 1.0); #endif // OVERRIDE_POSITION #ifdef MODE_RENDER_DEPTH - if (scene_data.pancake_shadows) { - if (gl_Position.z >= 0.9999) { - gl_Position.z = 0.9999; + if (scene_pancake_shadows) { + if (screen_position_output.z >= 0.9999) { + screen_position_output.z = 0.9999; } } #endif // MODE_RENDER_DEPTH #ifdef MODE_RENDER_MATERIAL - if (scene_data.material_uv2_mode) { - gl_Position.xy = (uv2_attrib.xy + draw_call.uv_offset) * 2.0 - 1.0; - gl_Position.z = 0.00001; - gl_Position.w = 1.0; + if (scene_material_uv2_mode) { + screen_position_output.xy = (uv2_attrib.xy + draw_call.uv_offset) * 2.0 - 1.0; + screen_position_output.z = 0.00001; + screen_position_output.w = 1.0; } #endif // MODE_RENDER_MATERIAL } +void main() { +#if defined(MODE_RENDER_MOTION_VECTORS) + vec3 prev_vertex; +#ifdef NORMAL_USED + vec3 prev_normal; +#endif +#if defined(NORMAL_USED) || defined(TANGENT_USED) + vec3 prev_tangent; + vec3 prev_binormal; +#endif + + _unpack_vertex_attributes( + previous_vertex_attrib, + instances.data[draw_call.instance_index].compressed_aabb_position_pad.xyz, + instances.data[draw_call.instance_index].compressed_aabb_size_pad.xyz, +#if defined(NORMAL_USED) || defined(TANGENT_USED) + previous_normal_attrib, +#ifdef NORMAL_USED + prev_normal, +#endif + prev_tangent, + prev_binormal, +#endif + prev_vertex); + + vertex_shader(prev_vertex, +#ifdef NORMAL_USED + prev_normal, +#endif +#if defined(NORMAL_USED) || defined(TANGENT_USED) + prev_tangent, + prev_binormal, +#endif + draw_call.instance_index, draw_call.multimesh_motion_vectors_previous_offset, instances.data[draw_call.instance_index].prev_transform, + scene_data_block.prev_data.inv_view_matrix, +#ifdef USE_MULTIVIEW + scene_data_block.prev_data.projection_matrix_view[ViewIndex], scene_data_block.prev_data.inv_projection_matrix_view[ViewIndex], scene_data_block.prev_data.eye_offset[ViewIndex], +#else + scene_data_block.prev_data.projection_matrix, scene_data_block.prev_data.inv_projection_matrix, +#endif + scene_data_block.prev_data.view_matrix, scene_data_block.prev_data.viewport_size, +#ifdef MODE_DUAL_PARABOLOID + scene_data_block.prev_data.dual_paraboloid_side, scene_data_block.prev_data.z_far, +#endif +#ifdef MODE_RENDER_DEPTH + scene_data_block.prev_data.pancake_shadows, +#endif +#ifdef MODE_RENDER_MATERIAL + scene_data_block.prev_data.material_uv2_mode, +#endif + prev_screen_position); +#else + vec4 screen_position; +#endif // MODE_RENDER_MOTION_VECTORS + + vec3 vertex; +#ifdef NORMAL_USED + vec3 normal; +#endif +#if defined(NORMAL_USED) || defined(TANGENT_USED) + vec3 tangent; + vec3 binormal; +#endif + + _unpack_vertex_attributes( + vertex_angle_attrib, + instances.data[draw_call.instance_index].compressed_aabb_position_pad.xyz, + instances.data[draw_call.instance_index].compressed_aabb_size_pad.xyz, +#if defined(NORMAL_USED) || defined(TANGENT_USED) + axis_tangent_attrib, +#ifdef NORMAL_USED + normal, +#endif + tangent, + binormal, +#endif + vertex); + + vertex_shader(vertex, +#ifdef NORMAL_USED + normal, +#endif +#if defined(NORMAL_USED) || defined(TANGENT_USED) + tangent, + binormal, +#endif + draw_call.instance_index, draw_call.multimesh_motion_vectors_current_offset, instances.data[draw_call.instance_index].transform, + scene_data_block.data.inv_view_matrix, +#ifdef USE_MULTIVIEW + scene_data_block.data.projection_matrix_view[ViewIndex], scene_data_block.data.inv_projection_matrix_view[ViewIndex], scene_data_block.data.eye_offset[ViewIndex], +#else + scene_data_block.data.projection_matrix, scene_data_block.data.inv_projection_matrix, +#endif + scene_data_block.data.view_matrix, scene_data_block.data.viewport_size, +#ifdef MODE_DUAL_PARABOLOID + scene_data_block.data.dual_paraboloid_side, scene_data_block.data.z_far, +#endif +#ifdef MODE_RENDER_DEPTH + scene_data_block.data.pancake_shadows, +#endif +#ifdef MODE_RENDER_MATERIAL + scene_data_block.data.material_uv2_mode, +#endif + screen_position); + + gl_Position = screen_position; +} + #[fragment] #version 450 @@ -609,6 +766,11 @@ layout(location = 9) highp in float dp_clip; #endif +#if defined(MODE_RENDER_MOTION_VECTORS) +layout(location = 12) in highp vec4 screen_position; +layout(location = 13) in highp vec4 prev_screen_position; +#endif + #ifdef USE_LIGHTMAP // w0, w1, w2, and w3 are the four cubic B-spline basis functions float w0(float a) { @@ -1830,4 +1992,12 @@ void main() { #endif //MODE_MULTIPLE_RENDER_TARGETS #endif //MODE_RENDER_DEPTH + +#ifdef MODE_RENDER_MOTION_VECTORS + vec3 ndc = screen_position.xyz / screen_position.w; + ndc.y = -ndc.y; + vec3 prev_ndc = prev_screen_position.xyz / prev_screen_position.w; + prev_ndc.y = -prev_ndc.y; + frag_color = vec4(ndc - prev_ndc, 0.0); +#endif } diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl index 49c8905dbf17..d099fb7feda5 100644 --- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl +++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile_inc.glsl @@ -20,6 +20,10 @@ layout(push_constant, std430) uniform DrawCall { vec2 uv_offset; uint instance_index; uint pad; + uint multimesh_motion_vectors_current_offset; + uint multimesh_motion_vectors_previous_offset; + uint pad2; + uint pad3; #ifdef UBERSHADER uint sc_packed_0; uint sc_packed_1; @@ -297,6 +301,7 @@ scene_data_block; struct InstanceData { highp mat4 transform; // 64 - 64 + highp mat4 prev_transform; uint flags; // 04 - 68 uint instance_uniforms_ofs; // Base offset in global buffer for instance variables. // 04 - 72 uint gi_offset; // GI information when using lightmapping (VCT or lightmap index). // 04 - 76 diff --git a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp index c140498f8476..115a29cfb97a 100644 --- a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp +++ b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.cpp @@ -676,6 +676,16 @@ RID RenderSceneBuffersRD::get_velocity_buffer(bool p_get_msaa, uint32_t p_layer) } } +RID RenderSceneBuffersRD::get_velocity_depth_buffer() { + RendererRD::TextureStorage *texture_storage = RendererRD::TextureStorage::get_singleton(); + RID velocity_depth = texture_storage->render_target_get_override_velocity_depth(render_target); + if (velocity_depth.is_valid()) { + return velocity_depth; + } else { + return RID(); + } +} + uint32_t RenderSceneBuffersRD::get_color_usage_bits(bool p_resolve, bool p_msaa, bool p_storage) { DEV_ASSERT((!p_resolve && !p_msaa) || (p_resolve != p_msaa)); diff --git a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h index 0af3a7527069..7d48b272e41b 100644 --- a/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h +++ b/servers/rendering/renderer_rd/storage_rd/render_scene_buffers_rd.h @@ -300,6 +300,8 @@ class RenderSceneBuffersRD : public RenderSceneBuffers { RID get_velocity_buffer(bool p_get_msaa); RID get_velocity_buffer(bool p_get_msaa, uint32_t p_layer); + RID get_velocity_depth_buffer(); + // Samplers adjusted with the mipmap bias that is best fit for the configuration of these render buffers. _FORCE_INLINE_ RendererRD::MaterialStorage::Samplers get_samplers() const { diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp index 163ad40d78b9..0be419025518 100644 --- a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp +++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp @@ -3454,6 +3454,7 @@ void TextureStorage::render_target_set_override(RID p_render_target, RID p_color rt->overridden.color = p_color_texture; rt->overridden.depth = p_depth_texture; rt->overridden.velocity = p_velocity_texture; + rt->overridden.velocity_depth = p_velocity_depth_texture; } RID TextureStorage::render_target_get_override_color(RID p_render_target) const { @@ -3515,6 +3516,13 @@ RID TextureStorage::render_target_get_override_velocity_slice(RID p_render_targe } } +RID TextureStorage::render_target_get_override_velocity_depth(RID p_render_target) const { + RenderTarget *rt = render_target_owner.get_or_null(p_render_target); + ERR_FAIL_NULL_V(rt, RID()); + + return rt->overridden.velocity_depth; +} + void TextureStorage::render_target_set_transparent(RID p_render_target, bool p_is_transparent) { RenderTarget *rt = render_target_owner.get_or_null(p_render_target); ERR_FAIL_NULL(rt); diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.h b/servers/rendering/renderer_rd/storage_rd/texture_storage.h index cfd85b28167f..e9d2e244e630 100644 --- a/servers/rendering/renderer_rd/storage_rd/texture_storage.h +++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.h @@ -394,6 +394,7 @@ class TextureStorage : public RendererTextureStorage { RID color; RID depth; RID velocity; + RID velocity_depth; // In a multiview scenario, which is the most likely where we // override our destination textures, we need to obtain slices @@ -785,7 +786,7 @@ class TextureStorage : public RendererTextureStorage { RID render_target_get_override_depth_slice(RID p_render_target, const uint32_t p_layer) const; virtual RID render_target_get_override_velocity(RID p_render_target) const override; RID render_target_get_override_velocity_slice(RID p_render_target, const uint32_t p_layer) const; - virtual RID render_target_get_override_velocity_depth(RID p_render_target) const override { return RID(); } + virtual RID render_target_get_override_velocity_depth(RID p_render_target) const override; virtual RID render_target_get_texture(RID p_render_target) override;