Skip to content

Commit 62af78b

Browse files
Merge pull request #199 from JackPilley/ImageViewFix
Several fixes for 01_Image_view_and_sampler.adoc
2 parents 298a423 + 9ca13c3 commit 62af78b

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

en/06_Texture_mapping/01_Image_view_and_sampler.adoc

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The only two changes you have to make are the `format` and the `image`:
3838

3939
[,c++]
4040
----
41-
vk::ImageViewCreateInfo viewInfo({}, image, vk::ImageViewType::e2D, format, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 });
41+
vk::ImageViewCreateInfo viewInfo{ .image = image, .viewType = vk::ImageViewType::e2D, .format = format, .subresourceRange = { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 }};
4242
----
4343

4444
I've left out the explicit `viewInfo.components` initialization, because `VK_COMPONENT_SWIZZLE_IDENTITY` is defined as `0` anyway.
@@ -54,7 +54,8 @@ Because so much of the logic is duplicated from `createImageViews`, you may wish
5454
[,c++]
5555
----
5656
vk::raii::ImageView createImageView(vk::raii::Image& image, vk::Format format) {
57-
vk::ImageViewCreateInfo viewInfo({}, image, vk::ImageViewType::e2D, format, {}, { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 });
57+
vk::ImageViewCreateInfo viewInfo{ .image = image, .viewType = vk::ImageViewType::e2D,
58+
.format = format, .subresourceRange = { vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 } };
5859
return vk::raii::ImageView( device, viewInfo );
5960
}
6061
----
@@ -134,7 +135,7 @@ Samplers are configured through a `VkSamplerCreateInfo` structure, which specifi
134135

135136
[,c++]
136137
----
137-
vk::SamplerCreateInfo samplerInfo( {}, vk::Filter::eLinear, vk::Filter::eLinear);
138+
vk::SamplerCreateInfo samplerInfo{.magFilter = vk::Filter::eLinear, .minFilter = vk::Filter::eLinear};
138139
----
139140

140141
The `magFilter` and `minFilter` fields specify how to interpolate texels that are magnified or minified.
@@ -143,8 +144,8 @@ The choices are `VK_FILTER_NEAREST` and `VK_FILTER_LINEAR`, corresponding to the
143144

144145
[,c++]
145146
----
146-
vk::SamplerCreateInfo samplerInfo( {}, vk::Filter::eLinear, vk::Filter::eLinear, vk::SamplerMipmapMode::eLinear, vk::SamplerAddressMode::eRepeat,
147-
vk::SamplerAddressMode::eRepeat);
147+
vk::SamplerCreateInfo samplerInfo{.magFilter = vk::Filter::eLinear, .minFilter = vk::Filter::eLinear, .mipmapMode = vk::SamplerMipmapMode::eLinear,
148+
.addressModeU = vk::SamplerAddressMode::eRepeat, .addressModeV = vk::SamplerAddressMode::eRepeat };
148149
----
149150

150151
The addressing mode can be specified per axis using the `addressMode` fields.
@@ -165,11 +166,12 @@ However, the repeat mode is probably the most common mode, because it can be use
165166
[,c++]
166167
----
167168
vk::PhysicalDeviceProperties properties = physicalDevice.getProperties();
168-
vk::SamplerCreateInfo samplerInfo( {}, vk::Filter::eLinear, vk::Filter::eLinear, vk::SamplerMipmapMode::eLinear, vk::SamplerAddressMode::eRepeat,
169-
vk::SamplerAddressMode::eRepeat, vk::SamplerAddressMode::eRepeat, 0);
169+
vk::SamplerCreateInfo samplerInfo{.magFilter = vk::Filter::eLinear, .minFilter = vk::Filter::eLinear, .mipmapMode = vk::SamplerMipmapMode::eLinear,
170+
.addressModeU = vk::SamplerAddressMode::eRepeat, .addressModeV = vk::SamplerAddressMode::eRepeat, .addressModeW = vk::SamplerAddressMode::eRepeat,
171+
.anisotropyEnable = vk::True, .maxAnisotropy = properties.limits.maxSamplerAnisotropy};
170172
----
171173

172-
These two fields specify if anisotropic filtering should be used.
174+
The `anisotropyEnable` field specifies if anisotropic filtering should be used.
173175
There is no reason not to use this unless performance is a concern.
174176
The `maxAnisotropy` field limits the number of texel samples that can be used to calculate the final color.
175177
A lower value results in better performance, but lower quality results.
@@ -187,9 +189,10 @@ If we want to go for maximum quality, we can simply use that value directly:
187189
[,c++]
188190
----
189191
vk::PhysicalDeviceProperties properties = physicalDevice.getProperties();
190-
vk::SamplerCreateInfo samplerInfo( {}, vk::Filter::eLinear, vk::Filter::eLinear, vk::SamplerMipmapMode::eLinear, vk::SamplerAddressMode::eRepeat,
191-
vk::SamplerAddressMode::eRepeat, vk::SamplerAddressMode::eRepeat, 0, 1,
192-
properties.limits.maxSamplerAnisotropy, vk::False, vk::CompareOp::eAlways);
192+
vk::SamplerCreateInfo samplerInfo{.magFilter = vk::Filter::eLinear, .minFilter = vk::Filter::eLinear, .mipmapMode = vk::SamplerMipmapMode::eLinear,
193+
.addressModeU = vk::SamplerAddressMode::eRepeat, .addressModeV = vk::SamplerAddressMode::eRepeat, .addressModeW = vk::SamplerAddressMode::eRepeat,
194+
.anisotropyEnable = vk::True, .maxAnisotropy = properties.limits.maxSamplerAnisotropy,
195+
.compareEnable = vk::False, .compareOp = vk::CompareOp::eAlways};
193196
----
194197

195198
You can either query the properties at the beginning of your program and pass them around to the functions that need them, or query them in the `createTextureSampler` function itself.
@@ -267,8 +270,11 @@ We need to update the `createLogicalDevice` function to request it:
267270

268271
[,c++]
269272
----
270-
vk::PhysicalDeviceFeatures deviceFeatures;
271-
deviceFeatures.samplerAnisotropy = vk::True;
273+
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
274+
{.features = {.samplerAnisotropy = true } }, // vk::PhysicalDeviceFeatures2
275+
{.synchronization2 = true, .dynamicRendering = true }, // vk::PhysicalDeviceVulkan13Features
276+
{.extendedDynamicState = true } // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
277+
};
272278
----
273279

274280
And even though it is very unlikely that a modern graphics card will not support it, we should update `isDeviceSuitable` to check if it is available:

0 commit comments

Comments
 (0)