diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 3f52acd18..8d2386257 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -178,7 +178,6 @@ set(SCENE_GRAPH_COMPONENT_FILES scene_graph/components/material.cpp scene_graph/components/mesh.cpp scene_graph/components/pbr_material.cpp - scene_graph/components/sampler.cpp scene_graph/components/sub_mesh.cpp scene_graph/components/texture.cpp scene_graph/components/transform.cpp diff --git a/framework/gltf_loader.cpp b/framework/gltf_loader.cpp index 445f472be..b7fc3859a 100644 --- a/framework/gltf_loader.cpp +++ b/framework/gltf_loader.cpp @@ -535,7 +535,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_ scene.set_components(std::move(light_components)); // Load samplers - std::vector> + std::vector> sampler_components(model.samplers.size()); for (size_t sampler_index = 0; sampler_index < model.samplers.size(); sampler_index++) @@ -625,7 +625,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_ // Load textures auto images = scene.get_components(); - auto samplers = scene.get_components(); + auto samplers = scene.get_components(); auto default_sampler_linear = create_default_sampler(TINYGLTF_TEXTURE_FILTER_LINEAR); auto default_sampler_nearest = create_default_sampler(TINYGLTF_TEXTURE_FILTER_NEAREST); bool used_nearest_sampler = false; @@ -1496,7 +1496,7 @@ std::unique_ptr GLTFLoader::parse_image(tinygltf::Image &gltf_image) return image; } -std::unique_ptr GLTFLoader::parse_sampler(const tinygltf::Sampler &gltf_sampler) const +std::unique_ptr GLTFLoader::parse_sampler(const tinygltf::Sampler &gltf_sampler) const { auto name = gltf_sampler.name; @@ -1521,7 +1521,7 @@ std::unique_ptr GLTFLoader::parse_sampler(const tinygltf::Sampler & core::Sampler vk_sampler{device, sampler_info}; vk_sampler.set_debug_name(gltf_sampler.name); - return std::make_unique(name, std::move(vk_sampler)); + return std::make_unique(name, std::move(vk_sampler)); } std::unique_ptr GLTFLoader::parse_texture(const tinygltf::Texture &gltf_texture) const @@ -1535,7 +1535,7 @@ std::unique_ptr GLTFLoader::create_default_material() return parse_material(gltf_material); } -std::unique_ptr GLTFLoader::create_default_sampler(int filter) +std::unique_ptr GLTFLoader::create_default_sampler(int filter) { tinygltf::Sampler gltf_sampler; diff --git a/framework/gltf_loader.h b/framework/gltf_loader.h index 6f25fe5d4..4176e96f5 100644 --- a/framework/gltf_loader.h +++ b/framework/gltf_loader.h @@ -27,6 +27,7 @@ #define TINYGLTF_NO_EXTERNAL_IMAGE #include +#include "scene_graph/components/sampler.h" #include "scene_graph/node.h" #include "timer.h" @@ -102,13 +103,13 @@ class GLTFLoader virtual std::unique_ptr parse_image(tinygltf::Image &gltf_image) const; - virtual std::unique_ptr parse_sampler(const tinygltf::Sampler &gltf_sampler) const; + virtual std::unique_ptr parse_sampler(const tinygltf::Sampler &gltf_sampler) const; virtual std::unique_ptr parse_texture(const tinygltf::Texture &gltf_texture) const; virtual std::unique_ptr create_default_material(); - virtual std::unique_ptr create_default_sampler(int filter); + virtual std::unique_ptr create_default_sampler(int filter); virtual std::unique_ptr create_default_camera(); diff --git a/framework/rendering/subpasses/geometry_subpass.cpp b/framework/rendering/subpasses/geometry_subpass.cpp index f39ed5257..89e4331bb 100644 --- a/framework/rendering/subpasses/geometry_subpass.cpp +++ b/framework/rendering/subpasses/geometry_subpass.cpp @@ -197,7 +197,7 @@ void GeometrySubpass::draw_submesh(vkb::core::CommandBufferC &command_buffer, sg if (auto layout_binding = descriptor_set_layout.get_layout_binding(texture.first)) { command_buffer.bind_image(texture.second->get_image()->get_vk_image_view(), - texture.second->get_sampler()->vk_sampler, + texture.second->get_sampler()->get_core_sampler(), 0, layout_binding->binding, 0); } } diff --git a/framework/scene_graph/components/sampler.cpp b/framework/scene_graph/components/sampler.cpp deleted file mode 100644 index 1ba1dd4ff..000000000 --- a/framework/scene_graph/components/sampler.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright (c) 2018-2019, Arm Limited and Contributors - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 the "License"; - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "sampler.h" - -namespace vkb -{ -namespace sg -{ -Sampler::Sampler(const std::string &name, core::Sampler &&vk_sampler) : - Component{name}, - vk_sampler{std::move(vk_sampler)} -{} - -std::type_index Sampler::get_type() -{ - return typeid(Sampler); -} -} // namespace sg -} // namespace vkb diff --git a/framework/scene_graph/components/sampler.h b/framework/scene_graph/components/sampler.h index 585ea42bc..411c41848 100644 --- a/framework/scene_graph/components/sampler.h +++ b/framework/scene_graph/components/sampler.h @@ -1,4 +1,5 @@ -/* Copyright (c) 2018-2019, Arm Limited and Contributors +/* Copyright (c) 2018-2025, Arm Limited and Contributors + * Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -17,30 +18,73 @@ #pragma once -#include -#include -#include -#include - +#include "core/hpp_sampler.h" #include "core/sampler.h" #include "scene_graph/component.h" namespace vkb { -namespace sg +namespace scene_graph +{ +namespace components { -class Sampler : public Component +template +class Sampler : public vkb::sg::Component { public: - Sampler(const std::string &name, core::Sampler &&vk_sampler); + using CoreSamplerType = typename std::conditional::type; + + public: + Sampler(std::string const &name, CoreSamplerType &&core_sampler); Sampler(Sampler &&other) = default; + virtual ~Sampler() = default; - virtual ~Sampler() = default; + CoreSamplerType const &get_core_sampler() const; virtual std::type_index get_type() override; - core::Sampler vk_sampler; + private: + vkb::core::HPPSampler core_sampler; }; -} // namespace sg + +using SamplerC = Sampler; +using SamplerCpp = Sampler; + +// Member function definitions + +template <> +inline Sampler::Sampler(std::string const &name, vkb::core::HPPSampler &&core_sampler_) : + Component{name}, + core_sampler{std::move(core_sampler_)} +{ +} + +template <> +inline Sampler::Sampler(std::string const &name, vkb::core::Sampler &&core_sampler_) : + Component{name}, + core_sampler{std::move(reinterpret_cast(core_sampler_))} +{ +} + +template +inline typename Sampler::CoreSamplerType const &Sampler::get_core_sampler() const +{ + if constexpr (bindingType == BindingType::Cpp) + { + return core_sampler; + } + else + { + return reinterpret_cast(core_sampler); + } +} + +template +inline std::type_index Sampler::get_type() +{ + return typeid(Sampler); +} +} // namespace components +} // namespace scene_graph } // namespace vkb diff --git a/framework/scene_graph/components/texture.cpp b/framework/scene_graph/components/texture.cpp index 0f2695936..ec64ff351 100644 --- a/framework/scene_graph/components/texture.cpp +++ b/framework/scene_graph/components/texture.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2018-2019, Arm Limited and Contributors +/* Copyright (c) 2018-2025, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * @@ -43,12 +43,12 @@ Image *Texture::get_image() return image; } -void Texture::set_sampler(Sampler &s) +void Texture::set_sampler(vkb::scene_graph::components::SamplerC &s) { sampler = &s; } -Sampler *Texture::get_sampler() +vkb::scene_graph::components::SamplerC *Texture::get_sampler() { assert(sampler && "Texture has no sampler"); return sampler; diff --git a/framework/scene_graph/components/texture.h b/framework/scene_graph/components/texture.h index bb6117710..835c76ae1 100644 --- a/framework/scene_graph/components/texture.h +++ b/framework/scene_graph/components/texture.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2018-2019, Arm Limited and Contributors +/* Copyright (c) 2018-2025, Arm Limited and Contributors * * SPDX-License-Identifier: Apache-2.0 * @@ -47,14 +47,14 @@ class Texture : public Component Image *get_image(); - void set_sampler(Sampler &sampler); + void set_sampler(vkb::scene_graph::components::SamplerC &sampler); - Sampler *get_sampler(); + vkb::scene_graph::components::SamplerC *get_sampler(); private: Image *image{nullptr}; - Sampler *sampler{nullptr}; + vkb::scene_graph::components::SamplerC *sampler = nullptr; }; } // namespace sg } // namespace vkb diff --git a/samples/extensions/dynamic_multisample_rasterization/dynamic_multisample_rasterization.cpp b/samples/extensions/dynamic_multisample_rasterization/dynamic_multisample_rasterization.cpp index 0788cd949..7399e8dbb 100644 --- a/samples/extensions/dynamic_multisample_rasterization/dynamic_multisample_rasterization.cpp +++ b/samples/extensions/dynamic_multisample_rasterization/dynamic_multisample_rasterization.cpp @@ -363,7 +363,7 @@ void DynamicMultisampleRasterization::load_assets() VkDescriptorImageInfo imageInfo; imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; imageInfo.imageView = image->get_vk_image_view().get_handle(); - imageInfo.sampler = texture->get_sampler()->vk_sampler.get_handle(); + imageInfo.sampler = texture->get_sampler()->get_core_sampler().get_handle(); image_infos.push_back(imageInfo); name_to_texture_id.emplace(name, static_cast(image_infos.size()) - 1); diff --git a/samples/extensions/fragment_density_map/fragment_density_map.cpp b/samples/extensions/fragment_density_map/fragment_density_map.cpp index e50e6deb4..a3cc01982 100644 --- a/samples/extensions/fragment_density_map/fragment_density_map.cpp +++ b/samples/extensions/fragment_density_map/fragment_density_map.cpp @@ -859,7 +859,7 @@ void FragmentDensityMap::setup_descriptor_set_main_pass() VkDescriptorBufferInfo buffer_descriptor = create_descriptor(*mesh_data.vertex_ubo); VkDescriptorImageInfo image_descriptor = vkb::initializers::descriptor_image_info( - mesh_data.base_color_texture->get_sampler()->vk_sampler.get_handle(), + mesh_data.base_color_texture->get_sampler()->get_core_sampler().get_handle(), mesh_data.base_color_texture->get_image()->get_vk_image_view().get_handle(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); std::array write_descriptor_sets = diff --git a/samples/extensions/ray_tracing_extended/ray_tracing_extended.cpp b/samples/extensions/ray_tracing_extended/ray_tracing_extended.cpp index 303cd66e5..68b9bca85 100644 --- a/samples/extensions/ray_tracing_extended/ray_tracing_extended.cpp +++ b/samples/extensions/ray_tracing_extended/ray_tracing_extended.cpp @@ -1519,7 +1519,7 @@ RaytracingExtended::RaytracingScene::RaytracingScene(vkb::core::DeviceC &device, VkDescriptorImageInfo imageInfo; imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; imageInfo.imageView = image->get_vk_image_view().get_handle(); - imageInfo.sampler = baseTextureIter->second->get_sampler()->vk_sampler.get_handle(); + imageInfo.sampler = baseTextureIter->second->get_sampler()->get_core_sampler().get_handle(); imageInfos.push_back(imageInfo); }