-
Notifications
You must be signed in to change notification settings - Fork 27
/
Ch29Multisampling.diff
353 lines (302 loc) · 17 KB
/
Ch29Multisampling.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch28Mipmapping.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch29Multisampling.java"
index ba55d68..f53e108 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch28Mipmapping.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch29Multisampling.java"
@@ -6,7 +6,6 @@ import org.joml.Vector2fc;
import org.joml.Vector3f;
import org.joml.Vector3fc;
import org.lwjgl.PointerBuffer;
-import org.lwjgl.system.Configuration;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.Pointer;
import org.lwjgl.vulkan.*;
@@ -44,9 +43,8 @@ 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 Ch28Mipmapping {
+public class Ch29Multisampling {
private static class HelloTriangleApplication {
@@ -152,9 +150,9 @@ public class Ch28Mipmapping {
private static final int OFFSETOF_COLOR = 3 * Float.BYTES;
private static final int OFFSETOF_TEXTCOORDS = (3 + 3) * Float.BYTES;
- private final Vector3fc pos;
- private final Vector3fc color;
- private final Vector2fc texCoords;
+ private Vector3fc pos;
+ private Vector3fc color;
+ private Vector2fc texCoords;
public Vertex(Vector3fc pos, Vector3fc color, Vector2fc texCoords) {
this.pos = pos;
@@ -214,6 +212,7 @@ public class Ch28Mipmapping {
private long surface;
private VkPhysicalDevice physicalDevice;
+ private int msaaSamples = VK_SAMPLE_COUNT_1_BIT;
private VkDevice device;
private VkQueue graphicsQueue;
@@ -235,6 +234,10 @@ public class Ch28Mipmapping {
private long commandPool;
+ private long colorImage;
+ private long colorImageMemory;
+ private long colorImageView;
+
private long depthImage;
private long depthImageMemory;
private long depthImageView;
@@ -334,6 +337,10 @@ public class Ch28Mipmapping {
private void cleanupSwapChain() {
+ vkDestroyImageView(device, colorImageView, null);
+ vkDestroyImage(device, colorImage, null);
+ vkFreeMemory(device, colorImageMemory, null);
+
vkDestroyImageView(device, depthImageView, null);
vkDestroyImage(device, depthImage, null);
vkFreeMemory(device, depthImageMemory, null);
@@ -425,6 +432,7 @@ public class Ch28Mipmapping {
createImageViews();
createRenderPass();
createGraphicsPipeline();
+ createColorResources();
createDepthResources();
createFramebuffers();
createUniformBuffers();
@@ -544,6 +552,7 @@ public class Ch28Mipmapping {
if(isDeviceSuitable(device)) {
physicalDevice = device;
+ msaaSamples = getMaxUsableSampleCount();
return;
}
}
@@ -571,6 +580,7 @@ public class Ch28Mipmapping {
VkPhysicalDeviceFeatures deviceFeatures = VkPhysicalDeviceFeatures.calloc(stack);
deviceFeatures.samplerAnisotropy(true);
+ deviceFeatures.sampleRateShading(true); // Enable sample shading feature for the device
VkDeviceCreateInfo createInfo = VkDeviceCreateInfo.calloc(stack);
@@ -687,30 +697,47 @@ public class Ch28Mipmapping {
try(MemoryStack stack = stackPush()) {
- VkAttachmentDescription.Buffer attachments = VkAttachmentDescription.calloc(2, stack);
- VkAttachmentReference.Buffer attachmentRefs = VkAttachmentReference.calloc(2, stack);
+ VkAttachmentDescription.Buffer attachments = VkAttachmentDescription.calloc(3, stack);
+ VkAttachmentReference.Buffer attachmentRefs = VkAttachmentReference.calloc(3, stack);
// Color attachments
+ // MSAA Image
VkAttachmentDescription colorAttachment = attachments.get(0);
colorAttachment.format(swapChainImageFormat);
- colorAttachment.samples(VK_SAMPLE_COUNT_1_BIT);
+ colorAttachment.samples(msaaSamples);
colorAttachment.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR);
colorAttachment.storeOp(VK_ATTACHMENT_STORE_OP_STORE);
colorAttachment.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
colorAttachment.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
colorAttachment.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
- colorAttachment.finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
+ colorAttachment.finalLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkAttachmentReference colorAttachmentRef = attachmentRefs.get(0);
colorAttachmentRef.attachment(0);
colorAttachmentRef.layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
+ // Present Image
+ VkAttachmentDescription colorAttachmentResolve = attachments.get(2);
+ colorAttachmentResolve.format(swapChainImageFormat);
+ colorAttachmentResolve.samples(VK_SAMPLE_COUNT_1_BIT);
+ colorAttachmentResolve.loadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
+ colorAttachmentResolve.storeOp(VK_ATTACHMENT_STORE_OP_STORE);
+ colorAttachmentResolve.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
+ colorAttachmentResolve.stencilStoreOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
+ colorAttachmentResolve.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
+ colorAttachmentResolve.finalLayout(VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
+
+ VkAttachmentReference colorAttachmentResolveRef = attachmentRefs.get(2);
+ colorAttachmentResolveRef.attachment(2);
+ colorAttachmentResolveRef.layout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
+
+
// Depth-Stencil attachments
VkAttachmentDescription depthAttachment = attachments.get(1);
depthAttachment.format(findDepthFormat());
- depthAttachment.samples(VK_SAMPLE_COUNT_1_BIT);
+ depthAttachment.samples(msaaSamples);
depthAttachment.loadOp(VK_ATTACHMENT_LOAD_OP_CLEAR);
depthAttachment.storeOp(VK_ATTACHMENT_STORE_OP_DONT_CARE);
depthAttachment.stencilLoadOp(VK_ATTACHMENT_LOAD_OP_DONT_CARE);
@@ -727,6 +754,7 @@ public class Ch28Mipmapping {
subpass.colorAttachmentCount(1);
subpass.pColorAttachments(VkAttachmentReference.calloc(1, stack).put(0, colorAttachmentRef));
subpass.pDepthStencilAttachment(depthAttachmentRef);
+ subpass.pResolveAttachments(VkAttachmentReference.calloc(1, stack).put(0, colorAttachmentResolveRef));
VkSubpassDependency.Buffer dependency = VkSubpassDependency.calloc(1, stack);
dependency.srcSubpass(VK_SUBPASS_EXTERNAL);
@@ -864,8 +892,9 @@ public class Ch28Mipmapping {
VkPipelineMultisampleStateCreateInfo multisampling = VkPipelineMultisampleStateCreateInfo.calloc(stack);
multisampling.sType(VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO);
- multisampling.sampleShadingEnable(false);
- multisampling.rasterizationSamples(VK_SAMPLE_COUNT_1_BIT);
+ multisampling.sampleShadingEnable(true);
+ multisampling.minSampleShading(0.2f); // Enable sample shading in the pipeline
+ multisampling.rasterizationSamples(msaaSamples); // Min fraction for sample shading; closer to one is smoother
VkPipelineDepthStencilStateCreateInfo depthStencil = VkPipelineDepthStencilStateCreateInfo.calloc(stack);
depthStencil.sType(VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO);
@@ -944,7 +973,7 @@ public class Ch28Mipmapping {
try(MemoryStack stack = stackPush()) {
- LongBuffer attachments = stack.longs(VK_NULL_HANDLE, depthImageView);
+ LongBuffer attachments = stack.longs(colorImageView, depthImageView, VK_NULL_HANDLE);
LongBuffer pFramebuffer = stack.mallocLong(1);
// Lets allocate the create info struct once and just update the pAttachments field each iteration
@@ -957,7 +986,7 @@ public class Ch28Mipmapping {
for(long imageView : swapChainImageViews) {
- attachments.put(0, imageView);
+ attachments.put(2, imageView);
framebufferInfo.pAttachments(attachments);
@@ -990,6 +1019,32 @@ public class Ch28Mipmapping {
}
}
+ private void createColorResources() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ LongBuffer pColorImage = stack.mallocLong(1);
+ LongBuffer pColorImageMemory = stack.mallocLong(1);
+
+ createImage(swapChainExtent.width(), swapChainExtent.height(),
+ 1,
+ msaaSamples,
+ swapChainImageFormat,
+ VK_IMAGE_TILING_OPTIMAL,
+ VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
+ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
+ pColorImage,
+ pColorImageMemory);
+
+ colorImage = pColorImage.get(0);
+ colorImageMemory = pColorImageMemory.get(0);
+
+ colorImageView = createImageView(colorImage, swapChainImageFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
+
+ transitionImageLayout(colorImage, swapChainImageFormat, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, 1);
+ }
+ }
+
private void createDepthResources() {
try(MemoryStack stack = stackPush()) {
@@ -1002,6 +1057,7 @@ public class Ch28Mipmapping {
createImage(
swapChainExtent.width(), swapChainExtent.height(),
1,
+ msaaSamples,
depthFormat,
VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT,
@@ -1104,7 +1160,7 @@ public class Ch28Mipmapping {
LongBuffer pTextureImageMemory = stack.mallocLong(1);
createImage(pWidth.get(0), pHeight.get(0),
mipLevels,
- VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL,
+ VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
pTextureImage,
@@ -1230,6 +1286,39 @@ public class Ch28Mipmapping {
}
}
+ private int getMaxUsableSampleCount() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkPhysicalDeviceProperties physicalDeviceProperties = VkPhysicalDeviceProperties.malloc(stack);
+ vkGetPhysicalDeviceProperties(physicalDevice, physicalDeviceProperties);
+
+ int sampleCountFlags = physicalDeviceProperties.limits().framebufferColorSampleCounts()
+ & physicalDeviceProperties.limits().framebufferDepthSampleCounts();
+
+ if((sampleCountFlags & VK_SAMPLE_COUNT_64_BIT) != 0) {
+ return VK_SAMPLE_COUNT_64_BIT;
+ }
+ if((sampleCountFlags & VK_SAMPLE_COUNT_32_BIT) != 0) {
+ return VK_SAMPLE_COUNT_32_BIT;
+ }
+ if((sampleCountFlags & VK_SAMPLE_COUNT_16_BIT) != 0) {
+ return VK_SAMPLE_COUNT_16_BIT;
+ }
+ if((sampleCountFlags & VK_SAMPLE_COUNT_8_BIT) != 0) {
+ return VK_SAMPLE_COUNT_8_BIT;
+ }
+ if((sampleCountFlags & VK_SAMPLE_COUNT_4_BIT) != 0) {
+ return VK_SAMPLE_COUNT_4_BIT;
+ }
+ if((sampleCountFlags & VK_SAMPLE_COUNT_2_BIT) != 0) {
+ return VK_SAMPLE_COUNT_2_BIT;
+ }
+
+ return VK_SAMPLE_COUNT_1_BIT;
+ }
+ }
+
private void createTextureImageView() {
textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, mipLevels);
}
@@ -1291,12 +1380,12 @@ public class Ch28Mipmapping {
}
}
- private void createImage(int width, int height, int mipLevels, int format, int tiling, int usage, int memProperties,
+ private void createImage(int width, int height, int mipLevels, int numSamples, int format, int tiling, int usage, int memProperties,
LongBuffer pTextureImage, LongBuffer pTextureImageMemory) {
try(MemoryStack stack = stackPush()) {
- VkImageCreateInfo imageInfo = VkImageCreateInfo.calloc(stack);
+ VkImageCreateInfo imageInfo = VkImageCreateInfo.callocStack(stack);
imageInfo.sType(VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
imageInfo.imageType(VK_IMAGE_TYPE_2D);
imageInfo.extent().width(width);
@@ -1308,7 +1397,7 @@ public class Ch28Mipmapping {
imageInfo.tiling(tiling);
imageInfo.initialLayout(VK_IMAGE_LAYOUT_UNDEFINED);
imageInfo.usage(usage);
- imageInfo.samples(VK_SAMPLE_COUNT_1_BIT);
+ imageInfo.samples(numSamples);
imageInfo.sharingMode(VK_SHARING_MODE_EXCLUSIVE);
if(vkCreateImage(device, imageInfo, null, pTextureImage) != VK_SUCCESS) {
@@ -1318,7 +1407,7 @@ public class Ch28Mipmapping {
VkMemoryRequirements memRequirements = VkMemoryRequirements.malloc(stack);
vkGetImageMemoryRequirements(device, pTextureImage.get(0), memRequirements);
- VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.calloc(stack);
+ VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.callocStack(stack);
allocInfo.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
allocInfo.allocationSize(memRequirements.size());
allocInfo.memoryTypeIndex(findMemoryType(stack, memRequirements.memoryTypeBits(), memProperties));
@@ -1335,7 +1424,7 @@ public class Ch28Mipmapping {
try(MemoryStack stack = stackPush()) {
- VkImageMemoryBarrier.Buffer barrier = VkImageMemoryBarrier.calloc(1, stack);
+ VkImageMemoryBarrier.Buffer barrier = VkImageMemoryBarrier.callocStack(1, stack);
barrier.sType(VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER);
barrier.oldLayout(oldLayout);
barrier.newLayout(newLayout);
@@ -1388,6 +1477,14 @@ public class Ch28Mipmapping {
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
+ } else if(oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL) {
+
+ barrier.srcAccessMask(0);
+ barrier.dstAccessMask(VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT);
+
+ sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
+ destinationStage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
+
} else {
throw new IllegalArgumentException("Unsupported layout transition");
}
@@ -1411,7 +1508,7 @@ public class Ch28Mipmapping {
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
- VkBufferImageCopy.Buffer region = VkBufferImageCopy.calloc(1, stack);
+ VkBufferImageCopy.Buffer region = VkBufferImageCopy.callocStack(1, stack);
region.bufferOffset(0);
region.bufferRowLength(0); // Tightly packed
region.bufferImageHeight(0); // Tightly packed
@@ -2164,7 +2261,6 @@ public class Ch28Mipmapping {
}
private PointerBuffer asPointerBuffer(MemoryStack stack, Collection<String> collection) {
- if(collection == null) return null;
PointerBuffer buffer = stack.mallocPointer(collection.size());
@@ -2176,7 +2272,6 @@ public class Ch28Mipmapping {
}
private PointerBuffer asPointerBuffer(MemoryStack stack, List<? extends Pointer> list) {
- if(list == null) return null;
PointerBuffer buffer = stack.mallocPointer(list.size());