Godot Engine – Multi-platform 2D and 3D game engine
renderer: fix MSAA resolve when driver lowers the sample count
Stuart Carnie9184614ef198
diff --git a/doc/classes/RenderingDevice.xml b/doc/classes/RenderingDevice.xmlindex 9930e4d7b85..1bc3212212b 100644--- a/doc/classes/RenderingDevice.xml+++ b/doc/classes/RenderingDevice.xml@@ -1222,6 +1222,13 @@[b]Note:[/b] This function returns a [code]uint64_t[/code] which internally maps to a [code]GLuint[/code] (OpenGL) or [code]VkImage[/code] (Vulkan).</description></method>+ <method name="texture_get_nearest_supported_samples">+ <return type="int" enum="RenderingDevice.TextureSamples" />+ <param index="0" name="format" type="RDTextureFormat" />+ <description>+ Returns the sample count that [method texture_create] will use for [param format]. This is [member RDTextureFormat.samples] if the driver supports that sample count for the format and usage, otherwise the nearest lower supported sample count. Use this to know the actual sample count in advance, for example when choosing a matching sample count for shaders or pipelines.+ </description>+ </method><method name="texture_is_discardable"><return type="bool" /><param index="0" name="texture" type="RID" />diff --git a/drivers/d3d12/rendering_device_driver_d3d12.cpp b/drivers/d3d12/rendering_device_driver_d3d12.cppindex c6ab936e4b5..db01f0b351b 100644--- a/drivers/d3d12/rendering_device_driver_d3d12.cpp+++ b/drivers/d3d12/rendering_device_driver_d3d12.cpp@@ -1130,7 +1130,8 @@ static const D3D12_UAV_DIMENSION RD_TEXTURE_TYPE_TO_D3D12_VIEW_DIMENSION_FOR_UAVD3D12_UAV_DIMENSION_TEXTURE2DARRAY,};-uint32_t RenderingDeviceDriverD3D12::_find_max_common_supported_sample_count(VectorView<DXGI_FORMAT> p_formats) {+// Bit n set means (1 << n) samples are supported, matching TextureSamples.+uint32_t RenderingDeviceDriverD3D12::_find_common_supported_sample_counts_mask(VectorView<DXGI_FORMAT> p_formats) {uint32_t common = UINT32_MAX;MutexLock lock(format_sample_counts_mask_cache_mutex);@@ -1154,11 +1155,14 @@ uint32_t RenderingDeviceDriverD3D12::_find_max_common_supported_sample_count(Veccommon &= mask;}}- if (common == UINT32_MAX) {- return 1;- } else {- return ((uint32_t)1 << Math::nearest_shift(common));- }+ // No formats: 1x only.+ return common == UINT32_MAX ? 1 : common;+}++uint32_t RenderingDeviceDriverD3D12::_find_max_common_supported_sample_count(VectorView<DXGI_FORMAT> p_formats) {+ uint32_t mask = _find_common_supported_sample_counts_mask(p_formats);+ // nearest_shift() returns the highest set bit index plus 1.+ return mask ? ((uint32_t)1 << (Math::nearest_shift(mask) - 1)) : 1;}UINT RenderingDeviceDriverD3D12::_compute_component_mapping(const RDD::TextureView &p_view) {@@ -1399,6 +1403,23 @@ bool RenderingDeviceDriverD3D12::_unordered_access_supported_by_format(DataForma}}+RDD::TextureSamples RenderingDeviceDriverD3D12::texture_get_nearest_supported_samples(const TextureFormat &p_format) {+ bool depth_stencil = p_format.usage_bits & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;+ bool unordered_access = (p_format.usage_bits & TEXTURE_USAGE_STORAGE_BIT) || (!(p_format.usage_bits & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) && (p_format.usage_bits & TEXTURE_USAGE_CAN_COPY_TO_BIT) && _unordered_access_supported_by_format(p_format.format));+ if (unordered_access) {+ // No MSAA in D3D12 if storage. May have become possible recently where supported, though.+ return TEXTURE_SAMPLES_1;+ }+ DXGI_FORMAT format_to_test = depth_stencil ? RD_TO_D3D12_FORMAT[p_format.format].dsv_format : RD_TO_D3D12_FORMAT[p_format.format].general_format;+ uint32_t mask = _find_common_supported_sample_counts_mask(format_to_test);+ for (int samples = p_format.samples; samples > TEXTURE_SAMPLES_1; samples--) {+ if (mask & (1u << samples)) {+ return TextureSamples(samples);+ }+ }+ return TEXTURE_SAMPLES_1;+}+RDD::TextureID RenderingDeviceDriverD3D12::texture_create(const TextureFormat &p_format, const TextureView &p_view) {// Using D3D12_RESOURCE_DESC1. Thanks to the layout, it's sliceable down to D3D12_RESOURCE_DESC if needed.CD3DX12_RESOURCE_DESC1 resource_desc = {};@@ -1483,16 +1504,9 @@ RDD::TextureID RenderingDeviceDriverD3D12::texture_create(const TextureFormat &presource_desc.Format = DXGI_FORMAT_R8_UINT;}+ ERR_FAIL_COND_V_MSG(texture_get_nearest_supported_samples(p_format) != p_format.samples, TextureID(), "Unsupported sample count.");resource_desc.SampleDesc = {};- DXGI_FORMAT format_to_test = (resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) ? RD_TO_D3D12_FORMAT[p_format.format].dsv_format : RD_TO_D3D12_FORMAT[p_format.format].general_format;- if (!(resource_desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) {- resource_desc.SampleDesc.Count = MIN(- _find_max_common_supported_sample_count(format_to_test),- TEXTURE_SAMPLES_COUNT[p_format.samples]);- } else {- // No MSAA in D3D12 if storage. May have become possible recently where supported, though.- resource_desc.SampleDesc.Count = 1;- }+ resource_desc.SampleDesc.Count = TEXTURE_SAMPLES_COUNT[p_format.samples];resource_desc.SampleDesc.Quality = resource_desc.SampleDesc.Count == 1 ? 0 : DXGI_STANDARD_MULTISAMPLE_QUALITY_PATTERN;// Create.diff --git a/drivers/d3d12/rendering_device_driver_d3d12.h b/drivers/d3d12/rendering_device_driver_d3d12.hindex fed929d5008..2a6da985b97 100644--- a/drivers/d3d12/rendering_device_driver_d3d12.h+++ b/drivers/d3d12/rendering_device_driver_d3d12.h@@ -323,6 +323,7 @@ private:HashMap<DXGI_FORMAT, uint32_t> format_sample_counts_mask_cache;Mutex format_sample_counts_mask_cache_mutex;+ uint32_t _find_common_supported_sample_counts_mask(VectorView<DXGI_FORMAT> p_formats);uint32_t _find_max_common_supported_sample_count(VectorView<DXGI_FORMAT> p_formats);UINT _compute_component_mapping(const TextureView &p_view);UINT _compute_plane_slice(DataFormat p_format, BitField<TextureAspectBits> p_aspect_bits);@@ -338,6 +339,7 @@ protected:public:virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;+ virtual TextureSamples texture_get_nearest_supported_samples(const TextureFormat &p_format) override final;virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) override final;virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;diff --git a/drivers/metal/rendering_device_driver_metal.cpp b/drivers/metal/rendering_device_driver_metal.cppindex 1d5efcb4a5b..ac0094ad1e0 100644--- a/drivers/metal/rendering_device_driver_metal.cpp+++ b/drivers/metal/rendering_device_driver_metal.cpp@@ -273,30 +273,23 @@ RDD::TextureID RenderingDeviceDriverMetal::texture_create(const TextureFormat &p// desc->setCompressionType(MTL::TextureCompressionTypeLossy);if (p_format.samples > TEXTURE_SAMPLES_1) {- SampleCount supported = (*device_properties).find_nearest_supported_sample_count(p_format.samples);-- if (supported > SampleCount1) {- bool ok = p_format.texture_type == TEXTURE_TYPE_2D || p_format.texture_type == TEXTURE_TYPE_2D_ARRAY;- if (ok) {- switch (p_format.texture_type) {- case TEXTURE_TYPE_2D:- desc->setTextureType(MTL::TextureType2DMultisample);- break;- case TEXTURE_TYPE_2D_ARRAY:- desc->setTextureType(MTL::TextureType2DMultisampleArray);- break;- default:- break;- }- desc->setSampleCount((NS::UInteger)supported);- if (p_format.mipmaps > 1) {- // For a buffer-backed or multi-sample texture, the value must be 1.- WARN_PRINT("mipmaps == 1 for multi-sample textures");- desc->setMipmapLevelCount(1);- }- } else {- WARN_PRINT("Unsupported multi-sample texture type; disabling multi-sample");- }+ ERR_FAIL_COND_V_MSG(texture_get_nearest_supported_samples(p_format) != p_format.samples, TextureID(), "Unsupported sample count.");++ switch (p_format.texture_type) {+ case TEXTURE_TYPE_2D:+ desc->setTextureType(MTL::TextureType2DMultisample);+ break;+ case TEXTURE_TYPE_2D_ARRAY:+ desc->setTextureType(MTL::TextureType2DMultisampleArray);+ break;+ default:+ ERR_FAIL_V_MSG(TextureID(), "Multisample textures must be 2D or 2D array.");+ }+ desc->setSampleCount(TEXTURE_SAMPLES_COUNT[p_format.samples]);+ if (p_format.mipmaps > 1) {+ // For a buffer-backed or multi-sample texture, the value must be 1.+ WARN_PRINT("mipmaps == 1 for multi-sample textures");+ desc->setMipmapLevelCount(1);}}@@ -430,6 +423,11 @@ RDD::TextureID RenderingDeviceDriverMetal::texture_create(const TextureFormat &preturn TextureID(tex_info);}+RDD::TextureSamples RenderingDeviceDriverMetal::texture_get_nearest_supported_samples(const TextureFormat &p_format) {+ SampleCount supported = (*device_properties).find_nearest_supported_sample_count(p_format.samples);+ return TextureSamples(Math::get_shift_from_power_of_2((uint32_t)supported));+}+RDD::TextureID RenderingDeviceDriverMetal::texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) {MTL::Texture *res = reinterpret_cast<MTL::Texture *>(p_native_texture);diff --git a/drivers/metal/rendering_device_driver_metal.h b/drivers/metal/rendering_device_driver_metal.hindex 60e58619dda..38ffbf1f0ce 100644--- a/drivers/metal/rendering_device_driver_metal.h+++ b/drivers/metal/rendering_device_driver_metal.h@@ -246,6 +246,7 @@ public:};virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;+ virtual TextureSamples texture_get_nearest_supported_samples(const TextureFormat &p_format) override final;virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) override final;virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;diff --git a/drivers/vulkan/rendering_device_driver_vulkan.cpp b/drivers/vulkan/rendering_device_driver_vulkan.cppindex 3bcff63c450..138adf0e91e 100644--- a/drivers/vulkan/rendering_device_driver_vulkan.cpp+++ b/drivers/vulkan/rendering_device_driver_vulkan.cpp@@ -2305,6 +2305,10 @@ VkSampleCountFlagBits RenderingDeviceDriverVulkan::_ensure_supported_sample_counreturn VK_SAMPLE_COUNT_1_BIT;}+RDD::TextureSamples RenderingDeviceDriverVulkan::texture_get_nearest_supported_samples(const TextureFormat &p_format) {+ return TextureSamples(Math::get_shift_from_power_of_2((uint32_t)_ensure_supported_sample_count(p_format.samples)));+}+RDD::TextureID RenderingDeviceDriverVulkan::texture_create(const TextureFormat &p_format, const TextureView &p_view) {VkImageCreateInfo create_info = {};create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;@@ -2355,7 +2359,8 @@ RDD::TextureID RenderingDeviceDriverVulkan::texture_create(const TextureFormat &create_info.mipLevels = p_format.mipmaps;create_info.arrayLayers = p_format.array_layers;- create_info.samples = _ensure_supported_sample_count(p_format.samples);+ ERR_FAIL_COND_V_MSG(texture_get_nearest_supported_samples(p_format) != p_format.samples, TextureID(), "Unsupported sample count.");+ create_info.samples = RD_TO_VK_SAMPLE_COUNT[p_format.samples];create_info.tiling = (p_format.usage_bits & TEXTURE_USAGE_CPU_READ_BIT) ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;// Usage.diff --git a/drivers/vulkan/rendering_device_driver_vulkan.h b/drivers/vulkan/rendering_device_driver_vulkan.hindex e7c129c744b..d123f312396 100644--- a/drivers/vulkan/rendering_device_driver_vulkan.h+++ b/drivers/vulkan/rendering_device_driver_vulkan.h@@ -296,6 +296,7 @@ public:public:virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) override final;+ virtual TextureSamples texture_get_nearest_supported_samples(const TextureFormat &p_format) override final;virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) override final;virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) override final;virtual TextureID texture_create_shared_from_slice(TextureID p_original_texture, const TextureView &p_view, TextureSliceType p_slice_type, uint32_t p_layer, uint32_t p_layers, uint32_t p_mipmap, uint32_t p_mipmaps) override final;diff --git a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cppindex b6be5545663..d8a0620034f 100644--- a/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp+++ b/servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp@@ -4286,7 +4286,7 @@ void RenderForwardClustered::_update_global_pipeline_data_requirements_from_projconst bool positional_shadow_16_bits = GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/positional_shadow/atlas_16_bits");global_pipeline_data_required.use_16_bit_shadows = directional_shadow_16_bits || positional_shadow_16_bits;global_pipeline_data_required.use_32_bit_shadows = !directional_shadow_16_bits || !positional_shadow_16_bits;- global_pipeline_data_required.texture_samples = RenderSceneBuffersRD::msaa_to_samples(RSE::ViewportMSAA(msaa_3d_mode));+ global_pipeline_data_required.texture_samples = RenderSceneBuffersRD::msaa_to_samples(RenderSceneBuffersRD::get_nearest_supported_msaa_3d(RSE::ViewportMSAA(msaa_3d_mode), _render_buffers_can_be_storage()));}void RenderForwardClustered::_update_global_pipeline_data_requirements_from_light_storage() {diff --git a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cppindex 77b26d2b132..870bdbd5d29 100644--- a/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp+++ b/servers/rendering/renderer_rd/forward_mobile/render_forward_mobile.cpp@@ -2917,8 +2917,8 @@ void RenderForwardMobile::_update_global_pipeline_data_requirements_from_projectconst bool positional_shadow_16_bits = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_16_bits");global_pipeline_data_required.use_16_bit_shadows = directional_shadow_16_bits || positional_shadow_16_bits;global_pipeline_data_required.use_32_bit_shadows = !directional_shadow_16_bits || !positional_shadow_16_bits;- global_pipeline_data_required.target_samples = RenderSceneBuffersRD::msaa_to_samples(RSE::ViewportMSAA(msaa_2d_mode));- global_pipeline_data_required.texture_samples = RenderSceneBuffersRD::msaa_to_samples(RSE::ViewportMSAA(msaa_3d_mode));+ global_pipeline_data_required.target_samples = RenderSceneBuffersRD::msaa_to_samples(RendererRD::TextureStorage::render_target_get_nearest_supported_msaa(RSE::ViewportMSAA(msaa_2d_mode), false));+ global_pipeline_data_required.texture_samples = RenderSceneBuffersRD::msaa_to_samples(RenderSceneBuffersRD::get_nearest_supported_msaa_3d(RSE::ViewportMSAA(msaa_3d_mode), _render_buffers_can_be_storage()));}void RenderForwardMobile::_update_global_pipeline_data_requirements_from_light_storage() {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.cppindex f3958072c9b..33153086f77 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@@ -163,7 +163,7 @@ void RenderSceneBuffersRD::configure(const RenderSceneBuffersConfiguration *p_coview_count = p_config->get_view_count();scaling_3d_mode = p_config->get_scaling_3d_mode();- msaa_3d = p_config->get_msaa_3d();+ msaa_3d = get_nearest_supported_msaa_3d(p_config->get_msaa_3d(), can_be_storage);screen_space_aa = p_config->get_screen_space_aa();fsr_sharpness = p_config->get_fsr_sharpness();@@ -822,6 +822,26 @@ RD::DataFormat RenderSceneBuffersRD::get_depth_format(bool p_resolve, bool p_msa}}+RSE::ViewportMSAA RenderSceneBuffersRD::get_nearest_supported_msaa(RSE::ViewportMSAA p_msaa, RD::DataFormat p_format, uint32_t p_usage_bits) {+ if (p_msaa == RSE::VIEWPORT_MSAA_DISABLED) {+ return p_msaa;+ }++ RD::TextureFormat tf;+ tf.format = p_format;+ tf.usage_bits = p_usage_bits;+ tf.samples = msaa_to_samples(p_msaa);+ RD::TextureSamples samples = RD::get_singleton()->texture_get_nearest_supported_samples(tf);+ if (samples != tf.samples && is_print_verbose_enabled()) {+ print_verbose(vformat("MSAA %dx is not supported by the current hardware. Falling back to %dx.", 1 << tf.samples, 1 << samples));+ }+ return samples_to_msaa(samples);+}++RSE::ViewportMSAA RenderSceneBuffersRD::get_nearest_supported_msaa_3d(RSE::ViewportMSAA p_msaa, bool p_storage) {+ return get_nearest_supported_msaa(p_msaa, get_depth_format(false, true, p_storage), get_depth_usage_bits(false, true, p_storage));+}+uint32_t RenderSceneBuffersRD::get_depth_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.hindex 77f306b0d62..48766aa9b3e 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@@ -350,8 +350,26 @@ public:}}+ _FORCE_INLINE_ static RSE::ViewportMSAA samples_to_msaa(RD::TextureSamples p_samples) {+ switch (p_samples) {+ case RD::TEXTURE_SAMPLES_1:+ return RSE::VIEWPORT_MSAA_DISABLED;+ case RD::TEXTURE_SAMPLES_2:+ return RSE::VIEWPORT_MSAA_2X;+ case RD::TEXTURE_SAMPLES_4:+ return RSE::VIEWPORT_MSAA_4X;+ case RD::TEXTURE_SAMPLES_8:+ return RSE::VIEWPORT_MSAA_8X;+ default:+ DEV_ASSERT(false && "Unknown sample count.");+ return RSE::VIEWPORT_MSAA_DISABLED;+ }+ }+static uint32_t get_color_usage_bits(bool p_resolve, bool p_msaa, bool p_storage);static RD::DataFormat get_depth_format(bool p_resolve, bool p_msaa, bool p_storage);+ static RSE::ViewportMSAA get_nearest_supported_msaa(RSE::ViewportMSAA p_msaa, RD::DataFormat p_format, uint32_t p_usage_bits);+ static RSE::ViewportMSAA get_nearest_supported_msaa_3d(RSE::ViewportMSAA p_msaa, bool p_storage);static uint32_t get_depth_usage_bits(bool p_resolve, bool p_msaa, bool p_storage);static RD::DataFormat get_velocity_format();static uint32_t get_velocity_usage_bits(bool p_resolve, bool p_msaa, bool p_storage);diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp b/servers/rendering/renderer_rd/storage_rd/texture_storage.cppindex d9570fb894a..a5157cbe50a 100644--- a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp+++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp@@ -4486,6 +4486,7 @@ void TextureStorage::_update_render_target(RenderTarget *rt) {rt->color_format = render_target_get_color_format(rt->use_hdr, false);rt->color_format_srgb = render_target_get_color_format(rt->use_hdr, true);+ rt->msaa = render_target_get_nearest_supported_msaa(rt->msaa, rt->use_hdr);if (rt->use_hdr) {rt->image_format = rt->is_transparent ? Image::FORMAT_RGBAH : Image::FORMAT_RGBH;@@ -5503,6 +5504,10 @@ uint32_t TextureStorage::render_target_get_color_usage_bits(bool p_msaa) {}}+RSE::ViewportMSAA TextureStorage::render_target_get_nearest_supported_msaa(RSE::ViewportMSAA p_msaa, bool p_use_hdr) {+ return RenderSceneBuffersRD::get_nearest_supported_msaa(p_msaa, render_target_get_color_format(p_use_hdr, false), render_target_get_color_usage_bits(true));+}+void TextureStorage::texture_2d_attach_streaming_state(RID p_texture, RID p_streaming_state) {Texture *tex = texture_owner.get_or_null(p_texture);ERR_FAIL_NULL_MSG(tex, "Invalid texture RID.");diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.h b/servers/rendering/renderer_rd/storage_rd/texture_storage.hindex d8f24c65a24..0e34563a219 100644--- a/servers/rendering/renderer_rd/storage_rd/texture_storage.h+++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.h@@ -941,6 +941,7 @@ public:static RD::DataFormat render_target_get_color_format(bool p_use_hdr, bool p_srgb);static uint32_t render_target_get_color_usage_bits(bool p_msaa);+ static RSE::ViewportMSAA render_target_get_nearest_supported_msaa(RSE::ViewportMSAA p_msaa, bool p_use_hdr);virtual void texture_2d_attach_streaming_state(RID p_texture, RID p_streaming_state) override;};diff --git a/servers/rendering/rendering_device.cpp b/servers/rendering/rendering_device.cppindex afff76e3b98..aa40a00c9ba 100644--- a/servers/rendering/rendering_device.cpp+++ b/servers/rendering/rendering_device.cpp@@ -1646,6 +1646,8 @@ RID RenderingDevice::texture_create(const TextureFormat &p_format, const Texture}ERR_FAIL_INDEX_V(format.samples, TEXTURE_SAMPLES_MAX, RID());+ ERR_FAIL_COND_V_MSG(format.samples > TEXTURE_SAMPLES_1 && format.texture_type != TEXTURE_TYPE_2D && format.texture_type != TEXTURE_TYPE_2D_ARRAY, RID(),+ "Multisample textures must be 2D or 2D array.");ERR_FAIL_COND_V_MSG(format.usage_bits == 0, RID(), "No usage bits specified (at least one is needed)");@@ -1763,6 +1765,7 @@ RID RenderingDevice::texture_create(const TextureFormat &p_format, const TextureTexture texture;format.usage_bits |= forced_usage_bits;+ format.samples = driver->texture_get_nearest_supported_samples(format);texture.driver_id = driver->texture_create(format, tv);ERR_FAIL_COND_V(!texture.driver_id, RID());texture.type = format.texture_type;@@ -2976,6 +2979,12 @@ bool RenderingDevice::texture_is_valid(RID p_texture) {return texture_owner.owns(p_texture);}+RD::TextureSamples RenderingDevice::texture_get_nearest_supported_samples(const TextureFormat &p_format) {+ ERR_RENDER_THREAD_GUARD_V(TEXTURE_SAMPLES_1);++ return driver->texture_get_nearest_supported_samples(p_format);+}+RD::TextureFormat RenderingDevice::texture_get_format(RID p_texture) {ERR_RENDER_THREAD_GUARD_V(TextureFormat());@@ -9330,6 +9339,7 @@ void RenderingDevice::_bind_methods() {ClassDB::bind_method(D_METHOD("texture_resolve_multisample", "from_texture", "to_texture"), &RenderingDevice::texture_resolve_multisample);ClassDB::bind_method(D_METHOD("texture_get_format", "texture"), &RenderingDevice::_texture_get_format);+ ClassDB::bind_method(D_METHOD("texture_get_nearest_supported_samples", "format"), &RenderingDevice::_texture_get_nearest_supported_samples);#ifndef DISABLE_DEPRECATEDClassDB::bind_method(D_METHOD("texture_get_native_handle", "texture"), &RenderingDevice::texture_get_native_handle);#endif@@ -10169,6 +10179,11 @@ RID RenderingDevice::_texture_create_shared_from_slice(const Ref<RDTextureView>return texture_create_shared_from_slice(p_view->base, p_with_texture, p_layer, p_mipmap, p_mipmaps, p_slice_type, p_layers);}+RD::TextureSamples RenderingDevice::_texture_get_nearest_supported_samples(const Ref<RDTextureFormat> &p_format) {+ ERR_FAIL_COND_V(p_format.is_null(), TEXTURE_SAMPLES_1);+ return texture_get_nearest_supported_samples(p_format->base);+}+Ref<RDTextureFormat> RenderingDevice::_texture_get_format(RID p_rd_texture) {Ref<RDTextureFormat> rtf;rtf.instantiate();diff --git a/servers/rendering/rendering_device.h b/servers/rendering/rendering_device.hindex f2b1085565d..966c0d2412a 100644--- a/servers/rendering/rendering_device.h+++ b/servers/rendering/rendering_device.h@@ -478,6 +478,7 @@ public:bool texture_is_shared(RID p_texture);bool texture_is_valid(RID p_texture);TextureFormat texture_get_format(RID p_texture);+ TextureSamples texture_get_nearest_supported_samples(const TextureFormat &p_format);Size2i texture_size(RID p_texture);#ifndef DISABLE_DEPRECATEDuint64_t texture_get_native_handle(RID p_texture);@@ -2008,6 +2009,7 @@ private:RID _texture_create_shared(const Ref<RDTextureView> &p_view, RID p_with_texture);RID _texture_create_shared_from_slice(const Ref<RDTextureView> &p_view, RID p_with_texture, uint32_t p_layer, uint32_t p_mipmap, uint32_t p_mipmaps = 1, TextureSliceType p_slice_type = TEXTURE_SLICE_2D, uint32_t p_layers = 0);Ref<RDTextureFormat> _texture_get_format(RID p_rd_texture);+ TextureSamples _texture_get_nearest_supported_samples(const Ref<RDTextureFormat> &p_format);FramebufferFormatID _framebuffer_format_create(const TypedArray<RDAttachmentFormat> &p_attachments, uint32_t p_view_count);FramebufferFormatID _framebuffer_format_create_multipass(const TypedArray<RDAttachmentFormat> &p_attachments, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_view_count);diff --git a/servers/rendering/rendering_device_driver.h b/servers/rendering/rendering_device_driver.hindex c9d566f215f..e6bb1dfd629 100644--- a/servers/rendering/rendering_device_driver.h+++ b/servers/rendering/rendering_device_driver.h@@ -279,6 +279,7 @@ public:};virtual TextureID texture_create(const TextureFormat &p_format, const TextureView &p_view) = 0;+ virtual TextureSamples texture_get_nearest_supported_samples(const TextureFormat &p_format) = 0;virtual TextureID texture_create_from_extension(uint64_t p_native_texture, TextureType p_type, DataFormat p_format, uint32_t p_array_layers, bool p_depth_stencil, uint32_t p_mipmaps) = 0;// texture_create_shared_*() can only use original, non-view textures as original. RenderingDevice is responsible for ensuring that.virtual TextureID texture_create_shared(TextureID p_original_texture, const TextureView &p_view) = 0;