Blender V5.0
vk_sampler.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "vk_sampler.hh"
10#include "vk_backend.hh"
11#include "vk_context.hh"
12
13#include "DNA_userdef_types.h"
14
15namespace blender::gpu {
20
21void VKSampler::create(const GPUSamplerState &sampler_state)
22{
24 BLI_assert(vk_sampler_ == VK_NULL_HANDLE);
25
26 const VKDevice &device = VKBackend::get().device;
27
28 VkSamplerCreateInfo sampler_info = {};
29 sampler_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
30 /* Extend */
31 sampler_info.addressModeU = to_vk_sampler_address_mode(sampler_state.extend_x);
32 sampler_info.addressModeV = sampler_info.addressModeW = to_vk_sampler_address_mode(
33 sampler_state.extend_yz);
34 sampler_info.minLod = 0;
35 sampler_info.maxLod = 0;
36
37 if (sampler_state.type == GPU_SAMPLER_STATE_TYPE_PARAMETERS) {
38 /* Apply filtering. */
39 if (sampler_state.filtering & GPU_SAMPLER_FILTERING_LINEAR) {
40 sampler_info.magFilter = VK_FILTER_LINEAR;
41 sampler_info.minFilter = VK_FILTER_LINEAR;
42 }
43 if (sampler_state.filtering & GPU_SAMPLER_FILTERING_MIPMAP) {
44 sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
45 sampler_info.minLod = 0;
46 sampler_info.maxLod = 1000;
47 }
48 if ((sampler_state.filtering & GPU_SAMPLER_FILTERING_ANISOTROPIC) &&
49 (U.anisotropic_filter > 1) &&
50 (device.physical_device_features_get().samplerAnisotropy == VK_TRUE))
51 {
52 sampler_info.anisotropyEnable = VK_TRUE;
53 sampler_info.maxAnisotropy = U.anisotropic_filter;
54 }
55 }
56 else if (sampler_state.type == GPU_SAMPLER_STATE_TYPE_CUSTOM) {
57 if (sampler_state.custom_type == GPU_SAMPLER_CUSTOM_ICON) {
58 sampler_info.magFilter = VK_FILTER_LINEAR;
59 sampler_info.minFilter = VK_FILTER_LINEAR;
60 sampler_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
61 sampler_info.minLod = 0;
62 sampler_info.maxLod = 1;
63 }
64 else if (sampler_state.custom_type == GPU_SAMPLER_CUSTOM_COMPARE) {
65 sampler_info.magFilter = VK_FILTER_LINEAR;
66 sampler_info.minFilter = VK_FILTER_LINEAR;
67 sampler_info.compareEnable = VK_TRUE;
68 sampler_info.compareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
69 }
70 }
71
72 vkCreateSampler(device.vk_handle(), &sampler_info, nullptr, &vk_sampler_);
73 debug::object_label(vk_sampler_, sampler_state.to_string().c_str());
74}
75
77{
78
79 if (vk_sampler_ != VK_NULL_HANDLE) {
80 const VKDevice &device = VKBackend::get().device;
81 if (device.vk_handle() != VK_NULL_HANDLE) {
82 vkDestroySampler(device.vk_handle(), vk_sampler_, nullptr);
83 }
84 vk_sampler_ = VK_NULL_HANDLE;
85 }
86}
87
88} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:46
@ GPU_SAMPLER_CUSTOM_ICON
@ GPU_SAMPLER_CUSTOM_COMPARE
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_PARAMETERS
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
@ GPU_SAMPLER_FILTERING_MIPMAP
@ GPU_SAMPLER_FILTERING_ANISOTROPIC
@ GPU_SAMPLER_FILTERING_LINEAR
#define U
static VKBackend & get()
Definition vk_backend.hh:91
const VkPhysicalDeviceFeatures & physical_device_features_get() const
Definition vk_device.hh:291
VkDevice vk_handle() const
Definition vk_device.hh:311
void create(const GPUSamplerState &sampler_state)
Definition vk_sampler.cc:21
void object_label(GLenum type, GLuint object, const char *name)
Definition gl_debug.cc:329
VkSamplerAddressMode to_vk_sampler_address_mode(const GPUSamplerExtendMode extend_mode)
Definition vk_common.cc:693
GPUSamplerCustomType custom_type
GPUSamplerExtendMode extend_yz
GPUSamplerFiltering filtering
std::string to_string() const
GPUSamplerExtendMode extend_x
GPUSamplerStateType type