public VkDeviceBuffer( VkRenderContext rc, ulong size, VkBufferUsageFlags usage, VkMemoryPropertyFlags memoryProperties, bool dynamic) { _rc = rc; usage |= VkBufferUsageFlags.TransferSrc; _usage = usage; _memoryProperties = memoryProperties; _isDynamic = dynamic; VkBufferCreateInfo bufferCI = VkBufferCreateInfo.New(); bufferCI.size = size; bufferCI.usage = _usage; VkResult result = vkCreateBuffer(rc.Device, ref bufferCI, null, out _buffer); CheckResult(result); vkGetBufferMemoryRequirements(rc.Device, _buffer, out _bufferMemoryRequirements); _bufferCapacity = _bufferMemoryRequirements.size; uint memoryType = FindMemoryType(rc.PhysicalDevice, _bufferMemoryRequirements.memoryTypeBits, memoryProperties); VkMemoryBlock memoryToken = rc.MemoryManager.Allocate( memoryType, _bufferMemoryRequirements.size, _bufferMemoryRequirements.alignment); _memory = memoryToken; vkBindBufferMemory(rc.Device, _buffer, _memory.DeviceMemory, _memory.Offset); }
public VkVertexBuffer( VkRenderContext rc, ulong size, VkMemoryPropertyFlags memoryProperties, bool dynamic) : base(rc, size, VkBufferUsageFlags.VertexBuffer, memoryProperties, dynamic) { }
public VkResourceFactory(VkRenderContext rc) { RenderContext = rc; _device = rc.Device; _physicalDevice = rc.PhysicalDevice; VkCommandPoolCreateInfo commandPoolCI = VkCommandPoolCreateInfo.New(); commandPoolCI.flags = VkCommandPoolCreateFlags.None; commandPoolCI.queueFamilyIndex = rc.GraphicsQueueIndex; }
public VkCubemapTexture( VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceMemoryManager memoryManager, VkRenderContext rc, IntPtr pixelsFront, IntPtr pixelsBack, IntPtr pixelsLeft, IntPtr pixelsRight, IntPtr pixelsTop, IntPtr pixelsBottom, int width, int height, PixelFormat format) { _device = device; _physicalDevice = physicalDevice; _rc = rc; _memoryManager = memoryManager; Width = width; Height = height; _format = format; _vkFormat = VkFormats.VeldridToVkPixelFormat(_format); vkGetPhysicalDeviceImageFormatProperties( _physicalDevice, _vkFormat, VkImageType.Image2D, VkImageTiling.Linear, VkImageUsageFlags.Sampled, VkImageCreateFlags.CubeCompatible, out VkImageFormatProperties linearProps); vkGetPhysicalDeviceImageFormatProperties( _physicalDevice, _vkFormat, VkImageType.Image2D, VkImageTiling.Optimal, VkImageUsageFlags.Sampled, VkImageCreateFlags.CubeCompatible, out VkImageFormatProperties optimalProps); bool useSingleStagingBuffer = linearProps.maxArrayLayers >= 6; VkImageCreateInfo imageCI = VkImageCreateInfo.New(); imageCI.imageType = VkImageType.Image2D; imageCI.flags = VkImageCreateFlags.CubeCompatible; imageCI.format = _vkFormat; imageCI.extent.width = (uint)width; imageCI.extent.height = (uint)height; imageCI.extent.depth = 1; imageCI.mipLevels = 1; imageCI.arrayLayers = 6; imageCI.samples = VkSampleCountFlags.Count1; imageCI.tiling = useSingleStagingBuffer ? VkImageTiling.Linear : VkImageTiling.Optimal; imageCI.usage = VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferDst; imageCI.initialLayout = VkImageLayout.Preinitialized; VkResult result = vkCreateImage(_device, ref imageCI, null, out _image); CheckResult(result); vkGetImageMemoryRequirements(_device, _image, out VkMemoryRequirements memReqs); uint memoryType = FindMemoryType(_physicalDevice, memReqs.memoryTypeBits, VkMemoryPropertyFlags.DeviceLocal); _memory = memoryManager.Allocate(memoryType, memReqs.size, memReqs.alignment); vkBindImageMemory(_device, _image, _memory.DeviceMemory, _memory.Offset); if (useSingleStagingBuffer) { CopyDataSingleStagingBuffer(pixelsFront, pixelsBack, pixelsLeft, pixelsRight, pixelsTop, pixelsBottom, memReqs); } else { CopyDataMultiImage(pixelsFront, pixelsBack, pixelsLeft, pixelsRight, pixelsTop, pixelsBottom); } TransitionImageLayout(_image, (uint)MipLevels, 0, 6, VkImageLayout.TransferDstOptimal, VkImageLayout.ShaderReadOnlyOptimal); _imageLayout = VkImageLayout.ShaderReadOnlyOptimal; }
public VkTexture2D( VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceMemoryManager memoryManager, VkRenderContext rc, int mipLevels, int width, int height, PixelFormat veldridFormat, DeviceTextureCreateOptions createOptions) { _device = device; _physicalDevice = physicalDevice; _memoryManager = memoryManager; _rc = rc; MipLevels = mipLevels; _width = width; _height = height; _createOptions = createOptions; if (createOptions == DeviceTextureCreateOptions.DepthStencil) { Format = VkFormat.D16Unorm; } else { Format = VkFormats.VeldridToVkPixelFormat(veldridFormat); } _veldridFormat = veldridFormat; VkImageCreateInfo imageCI = VkImageCreateInfo.New(); imageCI.mipLevels = (uint)mipLevels; imageCI.arrayLayers = 1; imageCI.imageType = VkImageType.Image2D; imageCI.extent.width = (uint)width; imageCI.extent.height = (uint)height; imageCI.extent.depth = 1; imageCI.initialLayout = VkImageLayout.Preinitialized; // TODO: Use proper VkImageLayout values and transitions. imageCI.usage = VkImageUsageFlags.TransferDst | VkImageUsageFlags.Sampled; if (createOptions == DeviceTextureCreateOptions.RenderTarget) { imageCI.usage |= VkImageUsageFlags.ColorAttachment; } else if (createOptions == DeviceTextureCreateOptions.DepthStencil) { imageCI.usage |= VkImageUsageFlags.DepthStencilAttachment; } imageCI.tiling = createOptions == DeviceTextureCreateOptions.DepthStencil ? VkImageTiling.Optimal : VkImageTiling.Optimal; imageCI.format = Format; imageCI.samples = VkSampleCountFlags.Count1; VkResult result = vkCreateImage(device, ref imageCI, null, out _image); CheckResult(result); vkGetImageMemoryRequirements(_device, _image, out VkMemoryRequirements memoryRequirements); VkMemoryBlock memoryToken = memoryManager.Allocate( FindMemoryType( _physicalDevice, memoryRequirements.memoryTypeBits, VkMemoryPropertyFlags.DeviceLocal), memoryRequirements.size, memoryRequirements.alignment); _memory = memoryToken; vkBindImageMemory(_device, _image, _memory.DeviceMemory, _memory.Offset); }