private unsafe void CreateDepthBuffer() { var depthInfo = new ImageCreateInfo { SType = StructureType.ImageCreateInfo, ImageType = ImageType.ImageType2D, Format = DepthFormat, Extent = new Extent3D(this.SwapchainExtent.Width, this.SwapchainExtent.Height, 1), MipLevels = 1, ArrayLayers = 1, Samples = SampleCountFlags.SampleCount1Bit, InitialLayout = ImageLayout.Undefined, Usage = ImageUsageFlags.ImageUsageDepthStencilAttachmentBit, SharingMode = SharingMode.Exclusive }; var depthViewInfo = new ImageViewCreateInfo { SType = StructureType.ImageViewCreateInfo, Format = DepthFormat, Components = new ComponentMapping(ComponentSwizzle.R, ComponentSwizzle.G, ComponentSwizzle.B, ComponentSwizzle.A), SubresourceRange = new ImageSubresourceRange(aspectMask: ImageAspectFlags.ImageAspectDepthBit, levelCount: 1, layerCount: 1), ViewType = ImageViewType.ImageViewType2D }; var allocInfo = new AllocationCreateInfo(usage: MemoryUsage.GPU_Only); var image = this.Allocator.CreateImage(depthInfo, allocInfo, out Allocation alloc); depthViewInfo.Image = image; ImageView view; var res = VkApi.CreateImageView(this.Device, &depthViewInfo, null, &view); if (res != Result.Success) { throw new Exception("Unable to create depth image view!"); } this.DepthBuffer.Image = image; this.DepthBuffer.View = view; this.DepthBuffer.Allocation = alloc; }
private unsafe void CreateUniformBuffer() //Simpler setup from the Vertex buffer because there is no staging or device copying { BufferCreateInfo bufferInfo = new BufferCreateInfo { SType = StructureType.BufferCreateInfo, Size = this.UniformBufferSize, Usage = BufferUsageFlags.BufferUsageUniformBufferBit, SharingMode = SharingMode.Exclusive }; // Allow this to be updated every frame var allocInfo = new AllocationCreateInfo( usage: MemoryUsage.CPU_To_GPU, requiredFlags: MemoryPropertyFlags.MemoryPropertyHostVisibleBit); // Binds buffer to allocation for you var buffer = this.Allocator.CreateBuffer(in bufferInfo, in allocInfo, out var allocation); // Camera/MVP Matrix calculation Camera.LookAt(new Vector3(2f, 2f, -5f), new Vector3(0, 0, 0), new Vector3(0, 1, 0)); var radFov = MathF.PI / 180f * 45f; var aspect = (float)this.SwapchainExtent.Width / this.SwapchainExtent.Height; Camera.Perspective(radFov, aspect, 0.5f, 100f); Camera.UpdateMVP(); allocation.Map(); Matrix4x4 *ptr = (Matrix4x4 *)allocation.MappedData; ptr[0] = Camera.MVPMatrix; // Camera Matrix ptr[1] = Matrix4x4.Identity; // Model Matrix allocation.Unmap(); this.UniformBuffer = buffer; this.UniformAllocation = allocation; }