/
BaseTexture.cpp
341 lines (300 loc) · 13.4 KB
/
BaseTexture.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "BaseTexture.h"
#include "AlphaMode.h"
#include "DeviceMemMgr.h"
#include "Finally.h"
#include "LogicalDevice.h"
#include "TextureUtils.h"
#include "VkFormatUtils.h"
#include "VkFuncs.h"
BEGIN_NAMESPACE(vgl)
//------------------------------------------------------------------------------------------------------------------------------------------
// Creates an uninitialized base texture
//------------------------------------------------------------------------------------------------------------------------------------------
BaseTexture::BaseTexture() noexcept
: mbIsValid(false)
, mFormat(VK_FORMAT_UNDEFINED)
, mWidth(0)
, mHeight(0)
, mDepth(0)
, mNumLayers(0)
, mNumMipLevels(0)
, mNumSamples(0)
, mbIsCubemap(false)
, mAlphaMode(AlphaMode::UNSPECIFIED)
, mpDevice(nullptr)
, mVkImage(VK_NULL_HANDLE)
, mVkImageView(VK_NULL_HANDLE)
, mSizeInBytes(0)
, mDeviceMemAlloc()
{
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Move constructor: relocate a texture to this object
//------------------------------------------------------------------------------------------------------------------------------------------
BaseTexture::BaseTexture(BaseTexture&& other) noexcept
: mbIsValid(other.mbIsValid)
, mFormat(other.mFormat)
, mWidth(other.mWidth)
, mHeight(other.mHeight)
, mDepth(other.mDepth)
, mNumLayers(other.mNumLayers)
, mNumMipLevels(other.mNumMipLevels)
, mNumSamples(other.mNumSamples)
, mbIsCubemap(other.mbIsCubemap)
, mAlphaMode(other.mAlphaMode)
, mpDevice(other.mpDevice)
, mVkImage(other.mVkImage)
, mVkImageView(other.mVkImageView)
, mSizeInBytes(other.mSizeInBytes)
, mDeviceMemAlloc(other.mDeviceMemAlloc)
{
other.mbIsValid = false;
other.mFormat = VK_FORMAT_UNDEFINED;
other.mWidth = 0;
other.mHeight = 0;
other.mDepth = 0;
other.mNumLayers = 0;
other.mNumMipLevels = 0;
other.mNumSamples = 0;
other.mbIsCubemap = false;
other.mAlphaMode = AlphaMode::UNSPECIFIED;
other.mpDevice = nullptr;
other.mVkImage = VK_NULL_HANDLE;
other.mVkImageView = VK_NULL_HANDLE;
other.mSizeInBytes = 0;
other.mDeviceMemAlloc = {};
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Destructor: does nothing intentionally
//------------------------------------------------------------------------------------------------------------------------------------------
BaseTexture::~BaseTexture() noexcept {
// Note: not calling 'destroy()' here because it must be done the derived class destructor instead.
// This is so the derived class version of 'destroy()' can be called, so the object is fully cleaned up.
ASSERT_LOG((!mbIsValid), "Derived classes must call destroy() in their destructor!");
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Performs common destruction logic for the base texture class and frees up the texture resource
//------------------------------------------------------------------------------------------------------------------------------------------
void BaseTexture::destroy() noexcept {
ASSERT_LOG(((!mpDevice) || mpDevice->getVkDevice()), "Parent device must still be valid if defined!");
mbIsValid = false;
// Image cleanup
if (mVkImageView) {
ASSERT(mpDevice && mpDevice->getVkDevice());
const VkFuncs& vkFuncs = mpDevice->getVkFuncs();
vkFuncs.vkDestroyImageView(mpDevice->getVkDevice(), mVkImageView, nullptr);
mVkImageView = VK_NULL_HANDLE;
}
if (mVkImage) {
ASSERT(mpDevice && mpDevice->getVkDevice());
const VkFuncs& vkFuncs = mpDevice->getVkFuncs();
vkFuncs.vkDestroyImage(mpDevice->getVkDevice(), mVkImage, nullptr);
mVkImage = VK_NULL_HANDLE;
}
// Free up the device memory allocation for the image if there
if (mDeviceMemAlloc.size > 0) {
ASSERT(mpDevice);
DeviceMemMgr& deviceMemMgr = mpDevice->getDeviceMemMgr();
deviceMemMgr.dealloc(mDeviceMemAlloc);
}
// Cleanup other stuff
mSizeInBytes = 0;
mpDevice = nullptr;
mAlphaMode = AlphaMode::UNSPECIFIED;
mbIsCubemap = false;
mNumSamples = 0;
mNumMipLevels = 0;
mNumLayers = 0;
mDepth = 0;
mHeight = 0;
mWidth = 0;
mFormat = VK_FORMAT_UNDEFINED;
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Internal init implementation: allocates memory for the texture, creates image views and so on.
//------------------------------------------------------------------------------------------------------------------------------------------
bool BaseTexture::initInternal(
LogicalDevice& device,
const VkImageType vkImageType,
const VkImageViewType vkImageViewType,
const VkImageTiling vkImageTilingMode,
const VkImageUsageFlags vkImageUsageFlags,
const VkImageLayout vkInitialImageLayout,
const DeviceMemAllocMode deviceMemAllocMode,
const VkFormat textureFormat,
const uint32_t width,
const uint32_t height,
const uint32_t depth,
const uint32_t numLayers,
const uint32_t numMipLevels,
const uint32_t numSamples,
const bool bIsCubemap,
const AlphaMode alphaMode
) noexcept {
// Preconditions
ASSERT_LOG((!mbIsValid), "Must call destroy() before re-initializing!");
ASSERT(device.getVkDevice());
ASSERT(width >= 1);
ASSERT(height >= 1);
ASSERT(depth >= 1);
ASSERT(numLayers >= 1);
ASSERT(numMipLevels >= 1);
ASSERT(numSamples >= 1);
ASSERT_LOG((depth == 1) || (numLayers == 1), "Can't specify array layers for a 3D texture!");
// Verify valid alpha mode
ASSERT(
(alphaMode == AlphaMode::UNSPECIFIED) ||
(alphaMode == AlphaMode::PREMULTIPLIED) ||
(alphaMode == AlphaMode::STRAIGHT)
);
// Verify there are no mipmaps or the mip chain is complete
ASSERT(
(numMipLevels == 1) ||
(TextureUtils::getNumMipLevels(width, height, depth) == numMipLevels)
);
// If anything goes wrong, cleanup on exit - don't half initialize!
auto cleanupOnError = finally([&]{
if (!mbIsValid) {
destroy();
}
});
// Save for future reference
mFormat = textureFormat;
mWidth = width;
mHeight = height;
mDepth = depth;
mNumLayers = numLayers;
mNumMipLevels = numMipLevels;
mNumSamples = numSamples;
mbIsCubemap = bIsCubemap;
mAlphaMode = alphaMode;
mpDevice = &device;
// Get the image aspect flags for the texture format
const VkImageAspectFlags vkImageAspectFlags = VkFormatUtils::getVkImageAspectFlags(textureFormat);
// Create the following:
//
// (1) The Vulkan image object
// (2) The memory allocation for the image
// (3) The image view for the image
//
if (!createVkImage(vkImageType, textureFormat, vkImageTilingMode, vkImageUsageFlags, vkInitialImageLayout))
return false;
if (!createImageMemBuffer(deviceMemAllocMode))
return false;
// Note: image view creation is optional depending on the texture type
const bool bIsValidImageViewType = (
(vkImageViewType == VK_IMAGE_VIEW_TYPE_1D) ||
(vkImageViewType == VK_IMAGE_VIEW_TYPE_2D) ||
(vkImageViewType == VK_IMAGE_VIEW_TYPE_3D) ||
(vkImageViewType == VK_IMAGE_VIEW_TYPE_CUBE) ||
(vkImageViewType == VK_IMAGE_VIEW_TYPE_1D_ARRAY) ||
(vkImageViewType == VK_IMAGE_VIEW_TYPE_2D_ARRAY) ||
(vkImageViewType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
);
if (bIsValidImageViewType) {
if (!createImageView(vkImageViewType, textureFormat, vkImageAspectFlags))
return false;
}
// Success if we get to here!
mbIsValid = true;
return true;
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Creates the Vulkan image object for the texture
//------------------------------------------------------------------------------------------------------------------------------------------
bool BaseTexture::createVkImage(
const VkImageType vkImageType,
const VkFormat vkImageFormat,
const VkImageTiling vkImageTilingMode,
const VkImageUsageFlags vkImageUsageFlags,
const VkImageLayout vkInitialImageLayout
) noexcept {
// Preconditions: shouldn't call if the image already created!
ASSERT(mVkImage == VK_NULL_HANDLE);
// Populate the create info struct for the image and create it
VkImageCreateInfo imageInfo = {};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = vkImageType;
imageInfo.format = vkImageFormat;
imageInfo.extent.width = mWidth;
imageInfo.extent.height = mHeight;
imageInfo.extent.depth = mDepth;
imageInfo.mipLevels = mNumMipLevels;
imageInfo.arrayLayers = TextureUtils::getNumTexImages(mNumLayers, mbIsCubemap); // Note: Vulkan counts cube faces as layers so count is multiplied by 6 for cubemaps
imageInfo.samples = static_cast<VkSampleCountFlagBits>(mNumSamples);
imageInfo.tiling = vkImageTilingMode;
imageInfo.usage = vkImageUsageFlags;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.initialLayout = vkInitialImageLayout;
const VkFuncs& vkFuncs = mpDevice->getVkFuncs();
if (vkFuncs.vkCreateImage(mpDevice->getVkDevice(), &imageInfo, nullptr, &mVkImage) != VK_SUCCESS) {
ASSERT_FAIL("Failed to create a Vulkan image object!");
return false;
}
return true; // Succeeded!
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Creates the memory buffer for the texture and binds it to the created image
//------------------------------------------------------------------------------------------------------------------------------------------
bool BaseTexture::createImageMemBuffer(const DeviceMemAllocMode deviceMemAllocMode) noexcept {
// Preconditions
ASSERT(mpDevice && mpDevice->getVkDevice());
ASSERT_LOG(mVkImage, "Must create the Vulkan image first!");
// Nab the memory related requirements of the image, including size, required memory types and alignment
const VkDevice vkDevice = mpDevice->getVkDevice();
const VkFuncs& vkFuncs = mpDevice->getVkFuncs();
VkMemoryRequirements memRequirements = {};
vkFuncs.vkGetImageMemoryRequirements(vkDevice, mVkImage, &memRequirements);
mSizeInBytes = memRequirements.size;
// Get the device memory allocator and make sure it can support our alignment requirements
DeviceMemMgr& deviceMemMgr = mpDevice->getDeviceMemMgr();
if (deviceMemMgr.getMinAlignment() < memRequirements.alignment) {
ASSERT_FAIL("Unable to satisfy alignment requirements for the image!");
return false;
}
// Try to do a device memory alloc and if that fails (size <= 0) bail out
deviceMemMgr.alloc(memRequirements.size, memRequirements.memoryTypeBits, deviceMemAllocMode, mDeviceMemAlloc);
if (mDeviceMemAlloc.size <= 0)
return false;
// Okay, now that we have the buffer bind it to the image
if (vkFuncs.vkBindImageMemory(vkDevice, mVkImage, mDeviceMemAlloc.vkDeviceMemory, mDeviceMemAlloc.offset) != VK_SUCCESS) {
ASSERT_FAIL("Failed to associate the Vulkan image with it's allocated memory!");
return false;
}
return true; // Succeeded!
}
//------------------------------------------------------------------------------------------------------------------------------------------
// Creates the Vulkan image view for the texture
//------------------------------------------------------------------------------------------------------------------------------------------
bool BaseTexture::createImageView(
const VkImageViewType vkImageViewType,
const VkFormat vkImageFormat,
const VkImageAspectFlags vkImageAspectFlags
) noexcept {
// Preconditions: shouldn't call if the image view is already created!
ASSERT(mVkImageView == VK_NULL_HANDLE);
// Populate the create info struct for the view and create it
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = mVkImage;
createInfo.viewType = vkImageViewType;
createInfo.format = vkImageFormat;
createInfo.components = {
VK_COMPONENT_SWIZZLE_IDENTITY, // R
VK_COMPONENT_SWIZZLE_IDENTITY, // G
VK_COMPONENT_SWIZZLE_IDENTITY, // B
VK_COMPONENT_SWIZZLE_IDENTITY // A
};
createInfo.subresourceRange.aspectMask = vkImageAspectFlags;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = mNumMipLevels;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = TextureUtils::getNumTexImages(mNumLayers, mbIsCubemap); // Note: Vulkan counts cube faces as layers so count is multiplied by 6 for cubemaps
const VkFuncs& vkFuncs = mpDevice->getVkFuncs();
if (vkFuncs.vkCreateImageView(mpDevice->getVkDevice(), &createInfo, nullptr, &mVkImageView) != VK_SUCCESS) {
ASSERT_FAIL("Failed to create a Vulkan image view!");
return false;
}
return true; // Succeeded!
}
END_NAMESPACE(vgl)