-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch28Mipmapping.diff
379 lines (330 loc) · 17.8 KB
/
Ch28Mipmapping.diff
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch27ModelLoading.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch28Mipmapping.java"
index ca38319..ba55d68 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch27ModelLoading.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch28Mipmapping.java"
@@ -1,17 +1,17 @@
package javavulkantutorial;
import javavulkantutorial.ShaderSPIRVUtils.SPIRV;
-import org.joml.*;
+import org.joml.Matrix4f;
+import org.joml.Vector2fc;
+import org.joml.Vector3f;
+import org.joml.Vector3fc;
import org.lwjgl.PointerBuffer;
-import org.lwjgl.assimp.AIScene;
-import org.lwjgl.assimp.Assimp;
import org.lwjgl.system.Configuration;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.Pointer;
import org.lwjgl.vulkan.*;
import java.io.File;
-import java.lang.Math;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
@@ -26,11 +26,12 @@ import static java.lang.ClassLoader.getSystemClassLoader;
import static java.util.stream.Collectors.toSet;
import static javavulkantutorial.AlignmentUtils.alignas;
import static javavulkantutorial.AlignmentUtils.alignof;
-import static javavulkantutorial.ModelLoader.*;
+import static javavulkantutorial.ModelLoader.Model;
import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.FRAGMENT_SHADER;
import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.VERTEX_SHADER;
import static javavulkantutorial.ShaderSPIRVUtils.compileShaderFile;
-import static org.lwjgl.assimp.Assimp.*;
+import static org.lwjgl.assimp.Assimp.aiProcess_DropNormals;
+import static org.lwjgl.assimp.Assimp.aiProcess_FlipUVs;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFWVulkan.glfwCreateWindowSurface;
import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions;
@@ -43,8 +44,9 @@ import static org.lwjgl.vulkan.EXTDebugUtils.*;
import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
+import static org.lwjgl.vulkan.VK11.VK_FORMAT_FEATURE_TRANSFER_DST_BIT;
-public class Ch27ModelLoading {
+public class Ch28Mipmapping {
private static class HelloTriangleApplication {
@@ -150,9 +152,9 @@ public class Ch27ModelLoading {
private static final int OFFSETOF_COLOR = 3 * Float.BYTES;
private static final int OFFSETOF_TEXTCOORDS = (3 + 3) * Float.BYTES;
- private Vector3fc pos;
- private Vector3fc color;
- private Vector2fc texCoords;
+ private final Vector3fc pos;
+ private final Vector3fc color;
+ private final Vector2fc texCoords;
public Vertex(Vector3fc pos, Vector3fc color, Vector2fc texCoords) {
this.pos = pos;
@@ -237,6 +239,7 @@ public class Ch27ModelLoading {
private long depthImageMemory;
private long depthImageView;
+ private int mipLevels;
private long textureImage;
private long textureImageMemory;
private long textureImageView;
@@ -676,7 +679,7 @@ public class Ch27ModelLoading {
swapChainImageViews = new ArrayList<>(swapChainImages.size());
for(long swapChainImage : swapChainImages) {
- swapChainImageViews.add(createImageView(swapChainImage, swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT));
+ swapChainImageViews.add(createImageView(swapChainImage, swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1));
}
}
@@ -699,8 +702,6 @@ public class Ch27ModelLoading {
colorAttachment.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
colorAttachment.finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
- int y = attachments.get(0).samples();
-
VkAttachmentReference colorAttachmentRef = attachmentRefs.get(0);
colorAttachmentRef.attachment(0);
colorAttachmentRef.layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
@@ -1000,6 +1001,7 @@ public class Ch27ModelLoading {
createImage(
swapChainExtent.width(), swapChainExtent.height(),
+ 1,
depthFormat,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
@@ -1010,11 +1012,12 @@ public class Ch27ModelLoading {
depthImage = pDepthImage.get(0);
depthImageMemory = pDepthImageMemory.get(0);
- depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
+ depthImageView = createImageView(depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
// Explicitly transitioning the depth image
transitionImageLayout(depthImage, depthFormat,
- VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
+ VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
+ 1);
}
}
@@ -1055,6 +1058,10 @@ public class Ch27ModelLoading {
return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
}
+ private double log2(double n) {
+ return Math.log(n) / Math.log(2);
+ }
+
private void createTextureImage() {
try(MemoryStack stack = stackPush()) {
@@ -1069,6 +1076,8 @@ public class Ch27ModelLoading {
long imageSize = pWidth.get(0) * pHeight.get(0) * 4; // pChannels.get(0);
+ mipLevels = (int) Math.floor(log2(Math.max(pWidth.get(0), pHeight.get(0)))) + 1;
+
if(pixels == null) {
throw new RuntimeException("Failed to load texture image " + filename);
}
@@ -1094,8 +1103,9 @@ public class Ch27ModelLoading {
LongBuffer pTextureImage = stack.mallocLong(1);
LongBuffer pTextureImageMemory = stack.mallocLong(1);
createImage(pWidth.get(0), pHeight.get(0),
+ mipLevels,
VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL,
- VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
+ VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
pTextureImage,
pTextureImageMemory);
@@ -1106,14 +1116,13 @@ public class Ch27ModelLoading {
transitionImageLayout(textureImage,
VK_FORMAT_R8G8B8A8_SRGB,
VK_IMAGE_LAYOUT_UNDEFINED,
- VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
+ VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ mipLevels);
copyBufferToImage(pStagingBuffer.get(0), textureImage, pWidth.get(0), pHeight.get(0));
- transitionImageLayout(textureImage,
- VK_FORMAT_R8G8B8A8_SRGB,
- VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
- VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
+ // Transitioned to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL while generating mipmaps
+ generateMipmaps(textureImage, VK_FORMAT_R8G8B8A8_SRGB, pWidth.get(0), pHeight.get(0), mipLevels);
vkDestroyBuffer(device, pStagingBuffer.get(0), null);
vkFreeMemory(device, pStagingBufferMemory.get(0), null);
@@ -1123,8 +1132,106 @@ public class Ch27ModelLoading {
}
}
+ private void generateMipmaps(long image, int imageFormat, int width, int height, int mipLevels) {
+
+ try(MemoryStack stack = stackPush()) {
+
+ // Check if image format supports linear blitting
+ VkFormatProperties formatProperties = VkFormatProperties.malloc(stack);
+ vkGetPhysicalDeviceFormatProperties(physicalDevice, imageFormat, formatProperties);
+
+ if((formatProperties.optimalTilingFeatures() & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT) == 0) {
+ throw new RuntimeException("Texture image format does not support linear blitting");
+ }
+
+ VkCommandBuffer commandBuffer = beginSingleTimeCommands();
+
+ VkImageMemoryBarrier.Buffer barrier = VkImageMemoryBarrier.calloc(1, stack);
+ barrier.sType(VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER);
+ barrier.image(image);
+ barrier.srcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED);
+ barrier.dstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED);
+ barrier.dstAccessMask(VK_QUEUE_FAMILY_IGNORED);
+ barrier.subresourceRange().aspectMask(VK_IMAGE_ASPECT_COLOR_BIT);
+ barrier.subresourceRange().baseArrayLayer(0);
+ barrier.subresourceRange().layerCount(1);
+ barrier.subresourceRange().levelCount(1);
+
+ int mipWidth = width;
+ int mipHeight = height;
+
+ for(int i = 1;i < mipLevels;i++) {
+
+ barrier.subresourceRange().baseMipLevel(i - 1);
+ barrier.oldLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
+ barrier.newLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
+ barrier.srcAccessMask(VK_ACCESS_TRANSFER_WRITE_BIT);
+ barrier.dstAccessMask(VK_ACCESS_TRANSFER_READ_BIT);
+
+ vkCmdPipelineBarrier(commandBuffer,
+ VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
+ null,
+ null,
+ barrier);
+
+ VkImageBlit.Buffer blit = VkImageBlit.calloc(1, stack);
+ blit.srcOffsets(0).set(0, 0, 0);
+ blit.srcOffsets(1).set(mipWidth, mipHeight, 1);
+ blit.srcSubresource().aspectMask(VK_IMAGE_ASPECT_COLOR_BIT);
+ blit.srcSubresource().mipLevel(i - 1);
+ blit.srcSubresource().baseArrayLayer(0);
+ blit.srcSubresource().layerCount(1);
+ blit.dstOffsets(0).set(0, 0, 0);
+ blit.dstOffsets(1).set(mipWidth > 1 ? mipWidth / 2 : 1, mipHeight > 1 ? mipHeight / 2 : 1, 1);
+ blit.dstSubresource().aspectMask(VK_IMAGE_ASPECT_COLOR_BIT);
+ blit.dstSubresource().mipLevel(i);
+ blit.dstSubresource().baseArrayLayer(0);
+ blit.dstSubresource().layerCount(1);
+
+ vkCmdBlitImage(commandBuffer,
+ image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
+ image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+ blit,
+ VK_FILTER_LINEAR);
+
+ barrier.oldLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
+ barrier.newLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
+ barrier.srcAccessMask(VK_ACCESS_TRANSFER_READ_BIT);
+ barrier.dstAccessMask(VK_ACCESS_SHADER_READ_BIT);
+
+ vkCmdPipelineBarrier(commandBuffer,
+ VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0,
+ null,
+ null,
+ barrier);
+
+ if(mipWidth > 1) {
+ mipWidth /= 2;
+ }
+
+ if(mipHeight > 1) {
+ mipHeight /= 2;
+ }
+ }
+
+ barrier.subresourceRange().baseMipLevel(mipLevels - 1);
+ barrier.oldLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
+ barrier.newLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
+ barrier.srcAccessMask(VK_ACCESS_TRANSFER_WRITE_BIT);
+ barrier.dstAccessMask(VK_ACCESS_SHADER_READ_BIT);
+
+ vkCmdPipelineBarrier(commandBuffer,
+ VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0,
+ null,
+ null,
+ barrier);
+
+ endSingleTimeCommands(commandBuffer);
+ }
+ }
+
private void createTextureImageView() {
- textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT);
+ textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, mipLevels);
}
private void createTextureSampler() {
@@ -1145,6 +1252,9 @@ public class Ch27ModelLoading {
samplerInfo.compareEnable(false);
samplerInfo.compareOp(VK_COMPARE_OP_ALWAYS);
samplerInfo.mipmapMode(VK_SAMPLER_MIPMAP_MODE_LINEAR);
+ samplerInfo.minLod(0); // Optional
+ samplerInfo.maxLod((float) mipLevels);
+ samplerInfo.mipLodBias(0); // Optional
LongBuffer pTextureSampler = stack.mallocLong(1);
@@ -1156,7 +1266,7 @@ public class Ch27ModelLoading {
}
}
- private long createImageView(long image, int format, int aspectFlags) {
+ private long createImageView(long image, int format, int aspectFlags, int mipLevels) {
try(MemoryStack stack = stackPush()) {
@@ -1167,7 +1277,7 @@ public class Ch27ModelLoading {
viewInfo.format(format);
viewInfo.subresourceRange().aspectMask(aspectFlags);
viewInfo.subresourceRange().baseMipLevel(0);
- viewInfo.subresourceRange().levelCount(1);
+ viewInfo.subresourceRange().levelCount(mipLevels);
viewInfo.subresourceRange().baseArrayLayer(0);
viewInfo.subresourceRange().layerCount(1);
@@ -1181,7 +1291,7 @@ public class Ch27ModelLoading {
}
}
- private void createImage(int width, int height, int format, int tiling, int usage, int memProperties,
+ private void createImage(int width, int height, int mipLevels, int format, int tiling, int usage, int memProperties,
LongBuffer pTextureImage, LongBuffer pTextureImageMemory) {
try(MemoryStack stack = stackPush()) {
@@ -1192,7 +1302,7 @@ public class Ch27ModelLoading {
imageInfo.extent().width(width);
imageInfo.extent().height(height);
imageInfo.extent().depth(1);
- imageInfo.mipLevels(1);
+ imageInfo.mipLevels(mipLevels);
imageInfo.arrayLayers(1);
imageInfo.format(format);
imageInfo.tiling(tiling);
@@ -1221,7 +1331,7 @@ public class Ch27ModelLoading {
}
}
- private void transitionImageLayout(long image, int format, int oldLayout, int newLayout) {
+ private void transitionImageLayout(long image, int format, int oldLayout, int newLayout, int mipLevels) {
try(MemoryStack stack = stackPush()) {
@@ -1234,7 +1344,7 @@ public class Ch27ModelLoading {
barrier.image(image);
barrier.subresourceRange().baseMipLevel(0);
- barrier.subresourceRange().levelCount(1);
+ barrier.subresourceRange().levelCount(mipLevels);
barrier.subresourceRange().baseArrayLayer(0);
barrier.subresourceRange().layerCount(1);
@@ -1310,7 +1420,7 @@ public class Ch27ModelLoading {
region.imageSubresource().baseArrayLayer(0);
region.imageSubresource().layerCount(1);
region.imageOffset().set(0, 0, 0);
- region.imageExtent(VkExtent3D.calloc(stack).set(width, height, 1));
+ region.imageExtent(VkExtent3D.callocStack(stack).set(width, height, 1));
vkCmdCopyBufferToImage(commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, region);
@@ -1461,7 +1571,7 @@ public class Ch27ModelLoading {
try(MemoryStack stack = stackPush()) {
- VkDescriptorPoolSize.Buffer poolSizes = VkDescriptorPoolSize.calloc(2, stack);
+ VkDescriptorPoolSize.Buffer poolSizes = VkDescriptorPoolSize.callocStack(2, stack);
VkDescriptorPoolSize uniformBufferPoolSize = poolSizes.get(0);
uniformBufferPoolSize.type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
@@ -1471,7 +1581,7 @@ public class Ch27ModelLoading {
textureSamplerPoolSize.type(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
textureSamplerPoolSize.descriptorCount(swapChainImages.size());
- VkDescriptorPoolCreateInfo poolInfo = VkDescriptorPoolCreateInfo.calloc(stack);
+ VkDescriptorPoolCreateInfo poolInfo = VkDescriptorPoolCreateInfo.callocStack(stack);
poolInfo.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO);
poolInfo.pPoolSizes(poolSizes);
poolInfo.maxSets(swapChainImages.size());
@@ -2054,6 +2164,7 @@ public class Ch27ModelLoading {
}
private PointerBuffer asPointerBuffer(MemoryStack stack, Collection<String> collection) {
+ if(collection == null) return null;
PointerBuffer buffer = stack.mallocPointer(collection.size());
@@ -2065,6 +2176,7 @@ public class Ch27ModelLoading {
}
private PointerBuffer asPointerBuffer(MemoryStack stack, List<? extends Pointer> list) {
+ if(list == null) return null;
PointerBuffer buffer = stack.mallocPointer(list.size());