From: Not Zed Date: Wed, 14 Sep 2022 05:09:48 +0000 (+0930) Subject: Updated to Index Buffer chapter (27) X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=a6270f2c283460e039dfc8a061a8150d70f0aec9;p=vulkanz Updated to Index Buffer chapter (27) --- diff --git a/src/notzed.vulkan.test/classes/vulkan/test/Demo.java b/src/notzed.vulkan.test/classes/vulkan/test/Demo.java index 45ef9bb..ff3cfc5 100644 --- a/src/notzed.vulkan.test/classes/vulkan/test/Demo.java +++ b/src/notzed.vulkan.test/classes/vulkan/test/Demo.java @@ -720,7 +720,7 @@ public class Demo { } // TODO: memory allocator? - limited memory blocks? - public static Buffer createBuffer(DemoDevice device, long size, int usage, int props, int[] sharedFamilies, MemorySegment src, ResourceScope scope) { + public static Buffer createBuffer(DemoDevice device, long size, int usage, int props, int[] sharedFamilies, ResourceScope scope) { try ( Frame frame = Frame.frame()) { IntArray families = sharedFamilies != null ? IntArray.create(frame, sharedFamilies) : null; VkBufferCreateInfo create = VkBufferCreateInfo.create(0, size, usage, VK_SHARING_MODE_CONCURRENT, sharedFamilies != null ? sharedFamilies.length : 0, families, frame); @@ -740,14 +740,22 @@ public class Demo { device.device.vkBindBufferMemory(buffer, memory, 0); - if (src != null) { - MemorySegment mem = device.device.vkMapMemory(memory, 0, size, 0, frame.scope()); + return new Buffer(buffer, memory, size, 0); + } + } - mem.copyFrom(src); - device.device.vkUnmapMemory(memory); - } + public static Buffer createBuffer(DemoDevice device, MemorySegment src, int usage, int props, int[] sharedFamilies, ResourceScope scope) { + try ( Frame frame = Frame.frame()) { + Buffer target = createBuffer(device, src.byteSize(), usage | Vulkan.VK_BUFFER_USAGE_TRANSFER_DST_BIT, props, sharedFamilies, scope); + Buffer source = createBuffer(device, src.byteSize(), Vulkan.VK_BUFFER_USAGE_TRANSFER_SRC_BIT, Vulkan.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, sharedFamilies, frame.scope()); - return new Buffer(buffer, memory, size, 0); + MemorySegment mem = device.device.vkMapMemory(source.memory, source.offset, source.size, 0, frame.scope()); + mem.copyFrom(src); + device.device.vkUnmapMemory(source.memory()); + copyBuffer(device, source, target); + source.close(device.device); + + return target; } } diff --git a/src/notzed.vulkan.test/classes/vulkan/test/Tutorial.java b/src/notzed.vulkan.test/classes/vulkan/test/Tutorial.java index 09d77b7..6612674 100644 --- a/src/notzed.vulkan.test/classes/vulkan/test/Tutorial.java +++ b/src/notzed.vulkan.test/classes/vulkan/test/Tutorial.java @@ -65,9 +65,14 @@ public class Tutorial { }; static final float[] vertices = { - 0.0f, -0.5f, 1, 0, 0, - 0.5f, 0.5f, 0, 1, 0, - -0.5f, 0.5f, 0, 0, 1 + -0.5f, -0.5f, 1, 0, 0, + 0.5f, -0.5f, 0, 1, 0, + 0.5f, 0.5f, 0, 0, 1, + -0.5f, 0.5f, 1, 1, 1 + }; + + static final short[] indices = { + 0, 1, 2, 2, 3, 0 }; public static record RenderInfo( @@ -169,16 +174,18 @@ public class Tutorial { VkViewport viewports = VkViewport.create(0, 0, chain.extWidth(), chain.extHeight(), 0, 1, frame); VkRect2D scissors = VkRect2D.create(0, 0, chain.extWidth(), chain.extHeight(), frame); - Demo.Buffer vertexStaging = Demo.createBuffer(device, vertices.length * 4, Vulkan.VK_BUFFER_USAGE_TRANSFER_SRC_BIT, Vulkan.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, device.queueFamilies(), MemorySegment.ofArray(vertices), scope); - Demo.Buffer vertexBuffer = Demo.createBuffer(device, vertices.length * 4, Vulkan.VK_BUFFER_USAGE_TRANSFER_DST_BIT | Vulkan.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, Vulkan.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, device.queueFamilies(), null, scope); + Demo.Buffer vertexBuffer = Demo.createBuffer(device, MemorySegment.ofArray(vertices), Vulkan.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, Vulkan.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, device.queueFamilies(), scope); + Demo.Buffer indexBuffer = Demo.createBuffer(device, MemorySegment.ofArray(indices), Vulkan.VK_BUFFER_USAGE_INDEX_BUFFER_BIT, Vulkan.VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, device.queueFamilies(), scope); + HandleArray buffers = VkBuffer.createArray(1, alloc, scope); LongArray buffersOffset = LongArray.createArray(1, alloc); buffers.set(0, vertexBuffer.buffer()); - Demo.copyBuffer(device, vertexStaging, vertexBuffer); - vertexStaging.close(device.device()); - cleanup.add(() -> vertexBuffer.close(device.device())); + cleanup.add(() -> { + indexBuffer.close(device.device()); + vertexBuffer.close(device.device()); + }); for (int i = 0; i < commandBuffers.size(); i++) { VkCommandBuffer cmd = commandBuffers.get(i); @@ -193,7 +200,8 @@ public class Tutorial { cmd.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); cmd.vkCmdBindVertexBuffers(0, 1, buffers, buffersOffset); - cmd.vkCmdDraw(3, 1, 0, 0); + cmd.vkCmdBindIndexBuffer(indexBuffer.buffer(), indexBuffer.offset(), Vulkan.VK_INDEX_TYPE_UINT16); + cmd.vkCmdDrawIndexed(indices.length, 1, 0, 0, 0); cmd.vkCmdEndRenderPass(); cmd.vkEndCommandBuffer();