-
Notifications
You must be signed in to change notification settings - Fork 27
/
Ch24Sampler.diff
183 lines (162 loc) · 8.37 KB
/
Ch24Sampler.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch23TextureImage.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch24Sampler.java"
index 17a02f4..d7b4f7f 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch23TextureImage.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch24Sampler.java"
@@ -35,7 +35,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch23TextureImage {
+public class Ch24Sampler {
private static class HelloTriangleApplication {
@@ -227,6 +227,8 @@ public class Ch23TextureImage {
private long textureImage;
private long textureImageMemory;
+ private long textureImageView;
+ private long textureSampler;
private long vertexBuffer;
private long vertexBufferMemory;
@@ -292,6 +294,8 @@ public class Ch23TextureImage {
createLogicalDevice();
createCommandPool();
createTextureImage();
+ createTextureImageView();
+ createTextureSampler();
createVertexBuffer();
createIndexBuffer();
createDescriptorSetLayout();
@@ -336,6 +340,8 @@ public class Ch23TextureImage {
cleanupSwapChain();
+ vkDestroySampler(device, textureSampler, null);
+ vkDestroyImageView(device, textureImageView, null);
vkDestroyImage(device, textureImage, null);
vkFreeMemory(device, textureImageMemory, null);
@@ -541,6 +547,7 @@ public class Ch23TextureImage {
}
VkPhysicalDeviceFeatures deviceFeatures = VkPhysicalDeviceFeatures.calloc(stack);
+ deviceFeatures.samplerAnisotropy(true);
VkDeviceCreateInfo createInfo = VkDeviceCreateInfo.calloc(stack);
@@ -648,37 +655,8 @@ public class Ch23TextureImage {
swapChainImageViews = new ArrayList<>(swapChainImages.size());
- try(MemoryStack stack = stackPush()) {
-
- LongBuffer pImageView = stack.mallocLong(1);
-
- for(long swapChainImage : swapChainImages) {
-
- VkImageViewCreateInfo createInfo = VkImageViewCreateInfo.calloc(stack);
-
- createInfo.sType(VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
- createInfo.image(swapChainImage);
- createInfo.viewType(VK_IMAGE_VIEW_TYPE_2D);
- createInfo.format(swapChainImageFormat);
-
- createInfo.components().r(VK_COMPONENT_SWIZZLE_IDENTITY);
- createInfo.components().g(VK_COMPONENT_SWIZZLE_IDENTITY);
- createInfo.components().b(VK_COMPONENT_SWIZZLE_IDENTITY);
- createInfo.components().a(VK_COMPONENT_SWIZZLE_IDENTITY);
-
- createInfo.subresourceRange().aspectMask(VK_IMAGE_ASPECT_COLOR_BIT);
- createInfo.subresourceRange().baseMipLevel(0);
- createInfo.subresourceRange().levelCount(1);
- createInfo.subresourceRange().baseArrayLayer(0);
- createInfo.subresourceRange().layerCount(1);
-
- if (vkCreateImageView(device, createInfo, null, pImageView) != VK_SUCCESS) {
- throw new RuntimeException("Failed to create image views");
- }
-
- swapChainImageViews.add(pImageView.get(0));
- }
-
+ for(long swapChainImage : swapChainImages) {
+ swapChainImageViews.add(createImageView(swapChainImage, VK_FORMAT_R8G8B8A8_SRGB));
}
}
@@ -1012,6 +990,64 @@ public class Ch23TextureImage {
}
}
+ private void createTextureImageView() {
+ textureImageView = createImageView(textureImage, VK_FORMAT_R8G8B8A8_SRGB);
+ }
+
+ private void createTextureSampler() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkSamplerCreateInfo samplerInfo = VkSamplerCreateInfo.calloc(stack);
+ samplerInfo.sType(VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
+ samplerInfo.magFilter(VK_FILTER_LINEAR);
+ samplerInfo.minFilter(VK_FILTER_LINEAR);
+ samplerInfo.addressModeU(VK_SAMPLER_ADDRESS_MODE_REPEAT);
+ samplerInfo.addressModeV(VK_SAMPLER_ADDRESS_MODE_REPEAT);
+ samplerInfo.addressModeW(VK_SAMPLER_ADDRESS_MODE_REPEAT);
+ samplerInfo.anisotropyEnable(true);
+ samplerInfo.maxAnisotropy(16.0f);
+ samplerInfo.borderColor(VK_BORDER_COLOR_INT_OPAQUE_BLACK);
+ samplerInfo.unnormalizedCoordinates(false);
+ samplerInfo.compareEnable(false);
+ samplerInfo.compareOp(VK_COMPARE_OP_ALWAYS);
+ samplerInfo.mipmapMode(VK_SAMPLER_MIPMAP_MODE_LINEAR);
+
+ LongBuffer pTextureSampler = stack.mallocLong(1);
+
+ if(vkCreateSampler(device, samplerInfo, null, pTextureSampler) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create texture sampler");
+ }
+
+ textureSampler = pTextureSampler.get(0);
+ }
+ }
+
+ private long createImageView(long image, int format) {
+
+ try(MemoryStack stack = stackPush()) {
+
+ VkImageViewCreateInfo viewInfo = VkImageViewCreateInfo.calloc(stack);
+ viewInfo.sType(VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
+ viewInfo.image(image);
+ viewInfo.viewType(VK_IMAGE_VIEW_TYPE_2D);
+ viewInfo.format(format);
+ viewInfo.subresourceRange().aspectMask(VK_IMAGE_ASPECT_COLOR_BIT);
+ viewInfo.subresourceRange().baseMipLevel(0);
+ viewInfo.subresourceRange().levelCount(1);
+ viewInfo.subresourceRange().baseArrayLayer(0);
+ viewInfo.subresourceRange().layerCount(1);
+
+ LongBuffer pImageView = stack.mallocLong(1);
+
+ if(vkCreateImageView(device, viewInfo, null, pImageView) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create texture image view");
+ }
+
+ return pImageView.get(0);
+ }
+ }
+
private void createImage(int width, int height, int format, int tiling, int usage, int memProperties,
LongBuffer pTextureImage, LongBuffer pTextureImageMemory) {
@@ -1330,7 +1366,7 @@ public class Ch23TextureImage {
VkMemoryRequirements memRequirements = VkMemoryRequirements.malloc(stack);
vkGetBufferMemoryRequirements(device, pBuffer.get(0), memRequirements);
- VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.callocStack(stack);
+ VkMemoryAllocateInfo allocInfo = VkMemoryAllocateInfo.calloc(stack);
allocInfo.sType(VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
allocInfo.allocationSize(memRequirements.size());
allocInfo.memoryTypeIndex(findMemoryType(stack, memRequirements.memoryTypeBits(), properties));
@@ -1716,15 +1752,19 @@ public class Ch23TextureImage {
boolean extensionsSupported = checkDeviceExtensionSupport(device);
boolean swapChainAdequate = false;
+ boolean anisotropySupported = false;
if(extensionsSupported) {
try(MemoryStack stack = stackPush()) {
SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device, stack);
swapChainAdequate = swapChainSupport.formats.hasRemaining() && swapChainSupport.presentModes.hasRemaining();
+ VkPhysicalDeviceFeatures supportedFeatures = VkPhysicalDeviceFeatures.malloc(stack);
+ vkGetPhysicalDeviceFeatures(device, supportedFeatures);
+ anisotropySupported = supportedFeatures.samplerAnisotropy();
}
}
- return indices.isComplete() && extensionsSupported && swapChainAdequate;
+ return indices.isComplete() && extensionsSupported && swapChainAdequate && anisotropySupported;
}
private boolean checkDeviceExtensionSupport(VkPhysicalDevice device) {