Skip to content

Commit 76c215d

Browse files
committed
Fixed handling of image layers/cubemaps.
1 parent 3bc01a0 commit 76c215d

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

include/vsg/core/Data.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ namespace vsg
190190
virtual uint32_t height() const = 0;
191191
virtual uint32_t depth() const = 0;
192192

193-
/// return the {width, height, depth} pixel extents of an image accounting for blockWidth and any mipmapData assigned to image.
194-
std::tuple<uint32_t, uint32_t, uint32_t> pixelExtents() const;
193+
/// return the number of faces - uses properties.imageViewType and depth() value.
194+
std::pair<uint32_t, uint32_t> faceDepthAndCount() const;
195+
196+
/// return the {width, height, depth, layers} pixel extents of an image accounting for blockWidth and any mipmapData assigned to image.
197+
std::tuple<uint32_t, uint32_t, uint32_t, uint32_t> pixelExtents() const;
195198

196199
bool contiguous() const { return valueSize() == properties.stride; }
197200

src/vsg/core/Data.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
1616
#include <vsg/core/MipmapLayout.h>
1717
#include <vsg/io/Input.h>
1818
#include <vsg/io/Output.h>
19+
#include <vsg/io/Logger.h>
1920

2021
using namespace vsg;
2122

@@ -145,10 +146,28 @@ const MipmapLayout* Data::getMipmapLayout() const
145146
return getObject<MipmapLayout>("mipmapLayout");
146147
}
147148

149+
std::pair<uint32_t, uint32_t> Data::faceDepthAndCount() const
150+
{
151+
// Use properties.imageViewType to set the face count and face depth https://docs.vulkan.org/refpages/latest/refpages/source/VkImageViewType.html
152+
switch(properties.imageViewType)
153+
{
154+
case(VK_IMAGE_VIEW_TYPE_CUBE):
155+
case(VK_IMAGE_VIEW_TYPE_CUBE_ARRAY):
156+
return {depth()/6, 6};
157+
case(VK_IMAGE_VIEW_TYPE_1D_ARRAY):
158+
case(VK_IMAGE_VIEW_TYPE_2D_ARRAY):
159+
return {1, depth()};
160+
default:
161+
return {depth(), 1};
162+
}
163+
}
164+
148165
std::size_t Data::computeValueCountIncludingMipmaps() const
149166
{
150167
std::size_t count = 0;
151168

169+
auto [faceDepth, faceCount] = faceDepthAndCount();
170+
152171
if (auto mipmapLayout = getMipmapLayout())
153172
{
154173
for (const auto& mipmap : *mipmapLayout)
@@ -160,12 +179,13 @@ std::size_t Data::computeValueCountIncludingMipmaps() const
160179

161180
count += w * h * d;
162181
}
182+
count *= faceCount;
163183
}
164184
else
165185
{
166186
std::size_t x = width() * properties.blockWidth;
167187
std::size_t y = height() * properties.blockHeight;
168-
std::size_t z = depth() * properties.blockDepth;
188+
std::size_t z = faceDepth * properties.blockDepth;
169189

170190
auto mipLevels = std::max(properties.mipLevels, uint8_t(1));
171191
for (uint8_t level = 0; level < mipLevels; ++level)
@@ -181,16 +201,20 @@ std::size_t Data::computeValueCountIncludingMipmaps() const
181201
if (y > 1) y = y / 2;
182202
if (z > 1) z = z / 2;
183203
}
204+
205+
count *= faceCount;
184206
}
185207

186208
return count;
187209
}
188210

189-
std::tuple<uint32_t, uint32_t, uint32_t> Data::pixelExtents() const
211+
std::tuple<uint32_t, uint32_t, uint32_t, uint32_t> Data::pixelExtents() const
190212
{
213+
auto [faceDepth, faceCount] = faceDepthAndCount();
214+
191215
uint32_t w = width() * properties.blockWidth;
192216
uint32_t h = height() * properties.blockHeight;
193-
uint32_t d = depth() * properties.blockDepth;
217+
uint32_t d = faceDepth * properties.blockDepth;
194218

195219
if (auto mipmapLayout = getMipmapLayout())
196220
{
@@ -200,5 +224,5 @@ std::tuple<uint32_t, uint32_t, uint32_t> Data::pixelExtents() const
200224
d = mipmap.z;
201225
}
202226

203-
return {w, h, d};
227+
return {w, h, d, faceCount};
204228
}

src/vsg/state/Image.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,57 +40,43 @@ Image::Image(ref_ptr<Data> in_data) :
4040
auto properties = data->properties;
4141
auto dimensions = data->dimensions();
4242

43-
auto [width, height, depth] = data->pixelExtents();
43+
std::tie(extent.width, extent.height, extent.depth, arrayLayers) = data->pixelExtents();
4444

4545
switch (properties.imageViewType)
4646
{
4747
case (VK_IMAGE_VIEW_TYPE_1D):
4848
imageType = VK_IMAGE_TYPE_1D;
49-
arrayLayers = 1;
5049
break;
5150
case (VK_IMAGE_VIEW_TYPE_2D):
5251
imageType = VK_IMAGE_TYPE_2D;
53-
arrayLayers = 1;
5452
break;
5553
case (VK_IMAGE_VIEW_TYPE_3D):
5654
imageType = VK_IMAGE_TYPE_3D;
57-
arrayLayers = 1;
5855
break;
5956
case (VK_IMAGE_VIEW_TYPE_CUBE):
6057
imageType = VK_IMAGE_TYPE_2D;
6158
flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
62-
arrayLayers = depth;
63-
depth = 1;
6459
break;
6560
case (VK_IMAGE_VIEW_TYPE_1D_ARRAY):
6661
imageType = VK_IMAGE_TYPE_1D;
67-
arrayLayers = height * depth;
68-
height = 1;
69-
depth = 1;
7062
/* flags = VK_IMAGE_CREATE_1D_ARRAY_COMPATIBLE_BIT; // comment out as Vulkan headers don't yet provide this. */
7163
break;
7264
case (VK_IMAGE_VIEW_TYPE_2D_ARRAY):
7365
// imageType = VK_IMAGE_TYPE_3D;
7466
// flags = VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
7567
imageType = VK_IMAGE_TYPE_2D;
76-
arrayLayers = depth;
77-
depth = 1;
7868
break;
7969
case (VK_IMAGE_VIEW_TYPE_CUBE_ARRAY):
8070
imageType = VK_IMAGE_TYPE_2D;
8171
flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
82-
arrayLayers = depth;
83-
depth = 1;
8472
break;
8573
default:
8674
imageType = dimensions >= 3 ? VK_IMAGE_TYPE_3D : (dimensions == 2 ? VK_IMAGE_TYPE_2D : VK_IMAGE_TYPE_1D);
87-
arrayLayers = 1;
8875
break;
8976
}
9077

9178
format = properties.format;
9279
mipLevels = std::max(1u, static_cast<uint32_t>(data->properties.mipLevels));
93-
extent = VkExtent3D{width, height, depth};
9480

9581
// vsg::info("Image::Image(", data, ") mpipLevels = ", mipLevels);
9682

@@ -212,8 +198,6 @@ VkResult Image::compile(Device* device)
212198
info.pQueueFamilyIndices = queueFamilyIndices.data();
213199
info.initialLayout = initialLayout;
214200

215-
// vsg::info("Image::compile(), data = ",data, ", mipLevels = ", mipLevels, ", arrayLayers = ", arrayLayers, ", extent = {", extent.width, ", ", extent.height, ", ", extent.depth, "}");
216-
217201
vd.device = device;
218202

219203
vd.requiresDataCopy = data.valid();

src/vsg/state/ImageInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ uint32_t vsg::computeNumMipMapLevels(const Data* data, const Sampler* sampler)
331331
if (sampler)
332332
{
333333
// clamp the mipLevels so that it's no larger than what the data dimensions support
334-
auto [width, height, depth] = data->pixelExtents();
334+
auto [width, height, depth, numLayers] = data->pixelExtents();
335335
uint32_t maxDimension = std::max({width, height, depth});
336336
if (sampler->maxLod == VK_LOD_CLAMP_NONE)
337337
{

0 commit comments

Comments
 (0)