-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch25TextureMapping.diff
291 lines (240 loc) · 15.1 KB
/
Ch25TextureMapping.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch24Sampler.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch25TextureMapping.java"
index d7b4f7f..faac5b9 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch24Sampler.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch25TextureMapping.java"
@@ -8,9 +8,12 @@ import org.lwjgl.system.Pointer;
import org.lwjgl.vulkan.*;
import java.lang.Math;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
+import java.nio.file.Paths;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@@ -35,7 +38,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch24Sampler {
+public class Ch25TextureMapping {
private static class HelloTriangleApplication {
@@ -136,16 +139,19 @@ public class Ch24Sampler {
private static class Vertex {
- private static final int SIZEOF = (2 + 3) * Float.BYTES;
+ private static final int SIZEOF = (2 + 3 + 2) * Float.BYTES;
private static final int OFFSETOF_POS = 0;
private static final int OFFSETOF_COLOR = 2 * Float.BYTES;
+ private static final int OFFSETOF_TEXTCOORDS = 5 * Float.BYTES;
private Vector2fc pos;
private Vector3fc color;
+ private Vector2fc texCoords;
- public Vertex(Vector2fc pos, Vector3fc color) {
+ public Vertex(Vector2fc pos, Vector3fc color, Vector2fc texCoords) {
this.pos = pos;
this.color = color;
+ this.texCoords = texCoords;
}
private static VkVertexInputBindingDescription.Buffer getBindingDescription(MemoryStack stack) {
@@ -163,7 +169,7 @@ public class Ch24Sampler {
private static VkVertexInputAttributeDescription.Buffer getAttributeDescriptions(MemoryStack stack) {
VkVertexInputAttributeDescription.Buffer attributeDescriptions =
- VkVertexInputAttributeDescription.calloc(2);
+ VkVertexInputAttributeDescription.calloc(3, stack);
// Position
VkVertexInputAttributeDescription posDescription = attributeDescriptions.get(0);
@@ -179,16 +185,23 @@ public class Ch24Sampler {
colorDescription.format(VK_FORMAT_R32G32B32_SFLOAT);
colorDescription.offset(OFFSETOF_COLOR);
+ // Texture coordinates
+ VkVertexInputAttributeDescription texCoordsDescription = attributeDescriptions.get(2);
+ texCoordsDescription.binding(0);
+ texCoordsDescription.location(2);
+ texCoordsDescription.format(VK_FORMAT_R32G32_SFLOAT);
+ texCoordsDescription.offset(OFFSETOF_TEXTCOORDS);
+
return attributeDescriptions.rewind();
}
}
private static final Vertex[] VERTICES = {
- new Vertex(new Vector2f(-0.5f, -0.5f), new Vector3f(1.0f, 0.0f, 0.0f)),
- new Vertex(new Vector2f(0.5f, -0.5f), new Vector3f(0.0f, 1.0f, 0.0f)),
- new Vertex(new Vector2f(0.5f, 0.5f), new Vector3f(0.0f, 0.0f, 1.0f)),
- new Vertex(new Vector2f(-0.5f, 0.5f), new Vector3f(1.0f, 1.0f, 1.0f))
+ new Vertex(new Vector2f(-0.5f, -0.5f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector2f(1.0f, 0.0f)),
+ new Vertex(new Vector2f(0.5f, -0.5f), new Vector3f(0.0f, 1.0f, 0.0f), new Vector2f(0.0f, 0.0f)),
+ new Vertex(new Vector2f(0.5f, 0.5f), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(0.0f, 1.0f)),
+ new Vertex(new Vector2f(-0.5f, 0.5f), new Vector3f(1.0f, 1.0f, 1.0f), new Vector2f(1.0f, 1.0f))
};
private static final /*uint16_t*/ short[] INDICES = {
@@ -656,7 +669,7 @@ public class Ch24Sampler {
swapChainImageViews = new ArrayList<>(swapChainImages.size());
for(long swapChainImage : swapChainImages) {
- swapChainImageViews.add(createImageView(swapChainImage, VK_FORMAT_R8G8B8A8_SRGB));
+ swapChainImageViews.add(createImageView(swapChainImage, swapChainImageFormat));
}
}
@@ -711,16 +724,25 @@ public class Ch24Sampler {
try(MemoryStack stack = stackPush()) {
- VkDescriptorSetLayoutBinding.Buffer uboLayoutBinding = VkDescriptorSetLayoutBinding.calloc(1, stack);
+ VkDescriptorSetLayoutBinding.Buffer bindings = VkDescriptorSetLayoutBinding.calloc(2, stack);
+
+ VkDescriptorSetLayoutBinding uboLayoutBinding = bindings.get(0);
uboLayoutBinding.binding(0);
uboLayoutBinding.descriptorCount(1);
uboLayoutBinding.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
uboLayoutBinding.pImmutableSamplers(null);
uboLayoutBinding.stageFlags(VK_SHADER_STAGE_VERTEX_BIT);
+ VkDescriptorSetLayoutBinding samplerLayoutBinding = bindings.get(1);
+ samplerLayoutBinding.binding(1);
+ samplerLayoutBinding.descriptorCount(1);
+ samplerLayoutBinding.descriptorType(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
+ samplerLayoutBinding.pImmutableSamplers(null);
+ samplerLayoutBinding.stageFlags(VK_SHADER_STAGE_FRAGMENT_BIT);
+
VkDescriptorSetLayoutCreateInfo layoutInfo = VkDescriptorSetLayoutCreateInfo.calloc(stack);
layoutInfo.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO);
- layoutInfo.pBindings(uboLayoutBinding);
+ layoutInfo.pBindings(bindings);
LongBuffer pDescriptorSetLayout = stack.mallocLong(1);
@@ -737,8 +759,8 @@ public class Ch24Sampler {
// Let's compile the GLSL shaders into SPIR-V at runtime using the shaderc library
// Check ShaderSPIRVUtils class to see how it can be done
- SPIRV vertShaderSPIRV = compileShaderFile("shaders/21_shader_ubo.vert", VERTEX_SHADER);
- SPIRV fragShaderSPIRV = compileShaderFile("shaders/21_shader_ubo.frag", FRAGMENT_SHADER);
+ SPIRV vertShaderSPIRV = compileShaderFile("shaders/25_shader_textures.vert", VERTEX_SHADER);
+ SPIRV fragShaderSPIRV = compileShaderFile("shaders/25_shader_textures.frag", FRAGMENT_SHADER);
long vertShaderModule = createShaderModule(vertShaderSPIRV.bytecode());
long fragShaderModule = createShaderModule(fragShaderSPIRV.bytecode());
@@ -929,7 +951,7 @@ public class Ch24Sampler {
try(MemoryStack stack = stackPush()) {
- String filename = getSystemClassLoader().getResource("textures/texture.jpg").toExternalForm();
+ String filename = Paths.get(new URI(getSystemClassLoader().getResource("textures/texture.jpg").toExternalForm())).toString();
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
@@ -937,7 +959,7 @@ public class Ch24Sampler {
ByteBuffer pixels = stbi_load(filename, pWidth, pHeight, pChannels, STBI_rgb_alpha);
- long imageSize = pWidth.get(0) * pHeight.get(0) * /*always 4 due to STBI_rgb_alpha*/pChannels.get(0);
+ long imageSize = pWidth.get(0) * pHeight.get(0) * 4; // pChannels.get(0);
if(pixels == null) {
throw new RuntimeException("Failed to load texture image " + filename);
@@ -955,7 +977,7 @@ public class Ch24Sampler {
PointerBuffer data = stack.mallocPointer(1);
vkMapMemory(device, pStagingBufferMemory.get(0), 0, imageSize, 0, data);
{
- memcpy(data.getByteBuffer(0, (int) imageSize), pixels, imageSize);
+ memcpy(data.getByteBuffer(0, (int)imageSize), pixels, imageSize);
}
vkUnmapMemory(device, pStagingBufferMemory.get(0));
@@ -987,6 +1009,9 @@ public class Ch24Sampler {
vkDestroyBuffer(device, pStagingBuffer.get(0), null);
vkFreeMemory(device, pStagingBufferMemory.get(0), null);
+
+ } catch (URISyntaxException e) {
+ e.printStackTrace();
}
}
@@ -1281,13 +1306,19 @@ public class Ch24Sampler {
try(MemoryStack stack = stackPush()) {
- VkDescriptorPoolSize.Buffer poolSize = VkDescriptorPoolSize.calloc(1, stack);
- poolSize.type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
- poolSize.descriptorCount(swapChainImages.size());
+ VkDescriptorPoolSize.Buffer poolSizes = VkDescriptorPoolSize.calloc(2, stack);
+
+ VkDescriptorPoolSize uniformBufferPoolSize = poolSizes.get(0);
+ uniformBufferPoolSize.type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
+ uniformBufferPoolSize.descriptorCount(swapChainImages.size());
+
+ VkDescriptorPoolSize textureSamplerPoolSize = poolSizes.get(1);
+ textureSamplerPoolSize.type(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
+ textureSamplerPoolSize.descriptorCount(swapChainImages.size());
VkDescriptorPoolCreateInfo poolInfo = VkDescriptorPoolCreateInfo.calloc(stack);
poolInfo.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO);
- poolInfo.pPoolSizes(poolSize);
+ poolInfo.pPoolSizes(poolSizes);
poolInfo.maxSets(swapChainImages.size());
LongBuffer pDescriptorPool = stack.mallocLong(1);
@@ -1326,13 +1357,28 @@ public class Ch24Sampler {
bufferInfo.offset(0);
bufferInfo.range(UniformBufferObject.SIZEOF);
- VkWriteDescriptorSet.Buffer descriptorWrite = VkWriteDescriptorSet.calloc(1, stack);
- descriptorWrite.sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);
- descriptorWrite.dstBinding(0);
- descriptorWrite.dstArrayElement(0);
- descriptorWrite.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
- descriptorWrite.descriptorCount(1);
- descriptorWrite.pBufferInfo(bufferInfo);
+ VkDescriptorImageInfo.Buffer imageInfo = VkDescriptorImageInfo.calloc(1, stack);
+ imageInfo.imageLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
+ imageInfo.imageView(textureImageView);
+ imageInfo.sampler(textureSampler);
+
+ VkWriteDescriptorSet.Buffer descriptorWrites = VkWriteDescriptorSet.calloc(2, stack);
+
+ VkWriteDescriptorSet uboDescriptorWrite = descriptorWrites.get(0);
+ uboDescriptorWrite.sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);
+ uboDescriptorWrite.dstBinding(0);
+ uboDescriptorWrite.dstArrayElement(0);
+ uboDescriptorWrite.descriptorType(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
+ uboDescriptorWrite.descriptorCount(1);
+ uboDescriptorWrite.pBufferInfo(bufferInfo);
+
+ VkWriteDescriptorSet samplerDescriptorWrite = descriptorWrites.get(1);
+ samplerDescriptorWrite.sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET);
+ samplerDescriptorWrite.dstBinding(1);
+ samplerDescriptorWrite.dstArrayElement(0);
+ samplerDescriptorWrite.descriptorType(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
+ samplerDescriptorWrite.descriptorCount(1);
+ samplerDescriptorWrite.pImageInfo(imageInfo);
for(int i = 0;i < pDescriptorSets.capacity();i++) {
@@ -1340,9 +1386,10 @@ public class Ch24Sampler {
bufferInfo.buffer(uniformBuffers.get(i));
- descriptorWrite.dstSet(descriptorSet);
+ uboDescriptorWrite.dstSet(descriptorSet);
+ samplerDescriptorWrite.dstSet(descriptorSet);
- vkUpdateDescriptorSets(device, descriptorWrite, null);
+ vkUpdateDescriptorSets(device, descriptorWrites, null);
descriptorSets.add(descriptorSet);
}
@@ -1353,7 +1400,7 @@ public class Ch24Sampler {
try(MemoryStack stack = stackPush()) {
- VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.calloc(stack);
+ VkBufferCreateInfo bufferInfo = VkBufferCreateInfo.callocStack(stack);
bufferInfo.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
bufferInfo.size(size);
bufferInfo.usage(usage);
@@ -1366,7 +1413,7 @@ public class Ch24Sampler {
VkMemoryRequirements memRequirements = VkMemoryRequirements.malloc(stack);
vkGetBufferMemoryRequirements(device, pBuffer.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(), properties));
@@ -1443,6 +1490,9 @@ public class Ch24Sampler {
buffer.putFloat(vertex.color.x());
buffer.putFloat(vertex.color.y());
buffer.putFloat(vertex.color.z());
+
+ buffer.putFloat(vertex.texCoords.x());
+ buffer.putFloat(vertex.texCoords.y());
}
}
@@ -1578,8 +1628,8 @@ public class Ch24Sampler {
for(int i = 0;i < MAX_FRAMES_IN_FLIGHT;i++) {
if(vkCreateSemaphore(device, semaphoreInfo, null, pImageAvailableSemaphore) != VK_SUCCESS
- || vkCreateSemaphore(device, semaphoreInfo, null, pRenderFinishedSemaphore) != VK_SUCCESS
- || vkCreateFence(device, fenceInfo, null, pFence) != VK_SUCCESS) {
+ || vkCreateSemaphore(device, semaphoreInfo, null, pRenderFinishedSemaphore) != VK_SUCCESS
+ || vkCreateFence(device, fenceInfo, null, pFence) != VK_SUCCESS) {
throw new RuntimeException("Failed to create synchronization objects for the frame " + i);
}
@@ -1703,7 +1753,7 @@ public class Ch24Sampler {
private VkSurfaceFormatKHR chooseSwapSurfaceFormat(VkSurfaceFormatKHR.Buffer availableFormats) {
return availableFormats.stream()
- .filter(availableFormat -> availableFormat.format() == VK_FORMAT_B8G8R8_UNORM)
+ .filter(availableFormat -> availableFormat.format() == VK_FORMAT_B8G8R8_SRGB)
.filter(availableFormat -> availableFormat.colorSpace() == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
.findAny()
.orElse(availableFormats.get(0));