Skip to content

Commit 5259c1a

Browse files
authored
Make vkb::sg::Sampler a unified class vkb::scene_graph::components::Sampler<bindingType> (#1439)
1 parent 6a80f80 commit 5259c1a

File tree

11 files changed

+75
-65
lines changed

11 files changed

+75
-65
lines changed

framework/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ set(SCENE_GRAPH_COMPONENT_FILES
178178
scene_graph/components/material.cpp
179179
scene_graph/components/mesh.cpp
180180
scene_graph/components/pbr_material.cpp
181-
scene_graph/components/sampler.cpp
182181
scene_graph/components/sub_mesh.cpp
183182
scene_graph/components/texture.cpp
184183
scene_graph/components/transform.cpp

framework/gltf_loader.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ sg::Scene GLTFLoader::load_scene(int scene_index, VkBufferUsageFlags additional_
535535
scene.set_components(std::move(light_components));
536536

537537
// Load samplers
538-
std::vector<std::unique_ptr<sg::Sampler>>
538+
std::vector<std::unique_ptr<vkb::scene_graph::components::SamplerC>>
539539
sampler_components(model.samplers.size());
540540

541541
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_
625625

626626
// Load textures
627627
auto images = scene.get_components<sg::Image>();
628-
auto samplers = scene.get_components<sg::Sampler>();
628+
auto samplers = scene.get_components<vkb::scene_graph::components::SamplerC>();
629629
auto default_sampler_linear = create_default_sampler(TINYGLTF_TEXTURE_FILTER_LINEAR);
630630
auto default_sampler_nearest = create_default_sampler(TINYGLTF_TEXTURE_FILTER_NEAREST);
631631
bool used_nearest_sampler = false;
@@ -1496,7 +1496,7 @@ std::unique_ptr<sg::Image> GLTFLoader::parse_image(tinygltf::Image &gltf_image)
14961496
return image;
14971497
}
14981498

1499-
std::unique_ptr<sg::Sampler> GLTFLoader::parse_sampler(const tinygltf::Sampler &gltf_sampler) const
1499+
std::unique_ptr<vkb::scene_graph::components::SamplerC> GLTFLoader::parse_sampler(const tinygltf::Sampler &gltf_sampler) const
15001500
{
15011501
auto name = gltf_sampler.name;
15021502

@@ -1521,7 +1521,7 @@ std::unique_ptr<sg::Sampler> GLTFLoader::parse_sampler(const tinygltf::Sampler &
15211521
core::Sampler vk_sampler{device, sampler_info};
15221522
vk_sampler.set_debug_name(gltf_sampler.name);
15231523

1524-
return std::make_unique<sg::Sampler>(name, std::move(vk_sampler));
1524+
return std::make_unique<vkb::scene_graph::components::SamplerC>(name, std::move(vk_sampler));
15251525
}
15261526

15271527
std::unique_ptr<sg::Texture> GLTFLoader::parse_texture(const tinygltf::Texture &gltf_texture) const
@@ -1535,7 +1535,7 @@ std::unique_ptr<sg::PBRMaterial> GLTFLoader::create_default_material()
15351535
return parse_material(gltf_material);
15361536
}
15371537

1538-
std::unique_ptr<sg::Sampler> GLTFLoader::create_default_sampler(int filter)
1538+
std::unique_ptr<vkb::scene_graph::components::SamplerC> GLTFLoader::create_default_sampler(int filter)
15391539
{
15401540
tinygltf::Sampler gltf_sampler;
15411541

framework/gltf_loader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define TINYGLTF_NO_EXTERNAL_IMAGE
2828
#include <tiny_gltf.h>
2929

30+
#include "scene_graph/components/sampler.h"
3031
#include "scene_graph/node.h"
3132
#include "timer.h"
3233

@@ -102,13 +103,13 @@ class GLTFLoader
102103

103104
virtual std::unique_ptr<sg::Image> parse_image(tinygltf::Image &gltf_image) const;
104105

105-
virtual std::unique_ptr<sg::Sampler> parse_sampler(const tinygltf::Sampler &gltf_sampler) const;
106+
virtual std::unique_ptr<vkb::scene_graph::components::SamplerC> parse_sampler(const tinygltf::Sampler &gltf_sampler) const;
106107

107108
virtual std::unique_ptr<sg::Texture> parse_texture(const tinygltf::Texture &gltf_texture) const;
108109

109110
virtual std::unique_ptr<sg::PBRMaterial> create_default_material();
110111

111-
virtual std::unique_ptr<sg::Sampler> create_default_sampler(int filter);
112+
virtual std::unique_ptr<vkb::scene_graph::components::SamplerC> create_default_sampler(int filter);
112113

113114
virtual std::unique_ptr<sg::Camera> create_default_camera();
114115

framework/rendering/subpasses/geometry_subpass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void GeometrySubpass::draw_submesh(vkb::core::CommandBufferC &command_buffer, sg
197197
if (auto layout_binding = descriptor_set_layout.get_layout_binding(texture.first))
198198
{
199199
command_buffer.bind_image(texture.second->get_image()->get_vk_image_view(),
200-
texture.second->get_sampler()->vk_sampler,
200+
texture.second->get_sampler()->get_core_sampler(),
201201
0, layout_binding->binding, 0);
202202
}
203203
}

framework/scene_graph/components/sampler.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/* Copyright (c) 2018-2019, Arm Limited and Contributors
1+
/* Copyright (c) 2018-2025, Arm Limited and Contributors
2+
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
23
*
34
* SPDX-License-Identifier: Apache-2.0
45
*
@@ -17,30 +18,73 @@
1718

1819
#pragma once
1920

20-
#include <memory>
21-
#include <string>
22-
#include <typeinfo>
23-
#include <vector>
24-
21+
#include "core/hpp_sampler.h"
2522
#include "core/sampler.h"
2623
#include "scene_graph/component.h"
2724

2825
namespace vkb
2926
{
30-
namespace sg
27+
namespace scene_graph
28+
{
29+
namespace components
3130
{
32-
class Sampler : public Component
31+
template <vkb::BindingType bindingType>
32+
class Sampler : public vkb::sg::Component
3333
{
3434
public:
35-
Sampler(const std::string &name, core::Sampler &&vk_sampler);
35+
using CoreSamplerType = typename std::conditional<bindingType == BindingType::Cpp, vkb::core::HPPSampler, vkb::core::Sampler>::type;
36+
37+
public:
38+
Sampler(std::string const &name, CoreSamplerType &&core_sampler);
3639

3740
Sampler(Sampler &&other) = default;
41+
virtual ~Sampler() = default;
3842

39-
virtual ~Sampler() = default;
43+
CoreSamplerType const &get_core_sampler() const;
4044

4145
virtual std::type_index get_type() override;
4246

43-
core::Sampler vk_sampler;
47+
private:
48+
vkb::core::HPPSampler core_sampler;
4449
};
45-
} // namespace sg
50+
51+
using SamplerC = Sampler<vkb::BindingType::C>;
52+
using SamplerCpp = Sampler<vkb::BindingType::Cpp>;
53+
54+
// Member function definitions
55+
56+
template <>
57+
inline Sampler<vkb::BindingType::Cpp>::Sampler(std::string const &name, vkb::core::HPPSampler &&core_sampler_) :
58+
Component{name},
59+
core_sampler{std::move(core_sampler_)}
60+
{
61+
}
62+
63+
template <>
64+
inline Sampler<vkb::BindingType::C>::Sampler(std::string const &name, vkb::core::Sampler &&core_sampler_) :
65+
Component{name},
66+
core_sampler{std::move(reinterpret_cast<vkb::core::HPPSampler &&>(core_sampler_))}
67+
{
68+
}
69+
70+
template <vkb::BindingType bindingType>
71+
inline typename Sampler<bindingType>::CoreSamplerType const &Sampler<bindingType>::get_core_sampler() const
72+
{
73+
if constexpr (bindingType == BindingType::Cpp)
74+
{
75+
return core_sampler;
76+
}
77+
else
78+
{
79+
return reinterpret_cast<vkb::core::Sampler const &>(core_sampler);
80+
}
81+
}
82+
83+
template <vkb::BindingType bindingType>
84+
inline std::type_index Sampler<bindingType>::get_type()
85+
{
86+
return typeid(Sampler<bindingType>);
87+
}
88+
} // namespace components
89+
} // namespace scene_graph
4690
} // namespace vkb

framework/scene_graph/components/texture.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2019, Arm Limited and Contributors
1+
/* Copyright (c) 2018-2025, Arm Limited and Contributors
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -43,12 +43,12 @@ Image *Texture::get_image()
4343
return image;
4444
}
4545

46-
void Texture::set_sampler(Sampler &s)
46+
void Texture::set_sampler(vkb::scene_graph::components::SamplerC &s)
4747
{
4848
sampler = &s;
4949
}
5050

51-
Sampler *Texture::get_sampler()
51+
vkb::scene_graph::components::SamplerC *Texture::get_sampler()
5252
{
5353
assert(sampler && "Texture has no sampler");
5454
return sampler;

framework/scene_graph/components/texture.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018-2019, Arm Limited and Contributors
1+
/* Copyright (c) 2018-2025, Arm Limited and Contributors
22
*
33
* SPDX-License-Identifier: Apache-2.0
44
*
@@ -47,14 +47,14 @@ class Texture : public Component
4747

4848
Image *get_image();
4949

50-
void set_sampler(Sampler &sampler);
50+
void set_sampler(vkb::scene_graph::components::SamplerC &sampler);
5151

52-
Sampler *get_sampler();
52+
vkb::scene_graph::components::SamplerC *get_sampler();
5353

5454
private:
5555
Image *image{nullptr};
5656

57-
Sampler *sampler{nullptr};
57+
vkb::scene_graph::components::SamplerC *sampler = nullptr;
5858
};
5959
} // namespace sg
6060
} // namespace vkb

samples/extensions/dynamic_multisample_rasterization/dynamic_multisample_rasterization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ void DynamicMultisampleRasterization::load_assets()
363363
VkDescriptorImageInfo imageInfo;
364364
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
365365
imageInfo.imageView = image->get_vk_image_view().get_handle();
366-
imageInfo.sampler = texture->get_sampler()->vk_sampler.get_handle();
366+
imageInfo.sampler = texture->get_sampler()->get_core_sampler().get_handle();
367367

368368
image_infos.push_back(imageInfo);
369369
name_to_texture_id.emplace(name, static_cast<int32_t>(image_infos.size()) - 1);

samples/extensions/fragment_density_map/fragment_density_map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ void FragmentDensityMap::setup_descriptor_set_main_pass()
859859

860860
VkDescriptorBufferInfo buffer_descriptor = create_descriptor(*mesh_data.vertex_ubo);
861861
VkDescriptorImageInfo image_descriptor = vkb::initializers::descriptor_image_info(
862-
mesh_data.base_color_texture->get_sampler()->vk_sampler.get_handle(),
862+
mesh_data.base_color_texture->get_sampler()->get_core_sampler().get_handle(),
863863
mesh_data.base_color_texture->get_image()->get_vk_image_view().get_handle(),
864864
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
865865
std::array<VkWriteDescriptorSet, 2> write_descriptor_sets =

0 commit comments

Comments
 (0)