public void CreateDevice() { /* * We create the logical device in this function. */ /* * When creating the device, we also specify what queues it has. */ // Store memory properties of the physical device. PhysicalDeviceMemoryProperties MemoryProperties = physicalDevice.GetMemoryProperties(); PhysicalDeviceFeatures Features = physicalDevice.GetFeatures(); PhysicalDeviceProperties Properties = physicalDevice.GetProperties(); // Create a logical device. var queueCreateInfos = new DeviceQueueCreateInfo[1]; queueCreateInfos[0] = new DeviceQueueCreateInfo(computeQueueFamilyIndex, 1, 1.0f); var deviceCreateInfo = new DeviceCreateInfo( queueCreateInfos, new[] { Constant.DeviceExtension.NVExternalMemory }, Features); device = physicalDevice.CreateDevice(deviceCreateInfo); // Get queue(s). queue = device.GetQueue(computeQueueFamilyIndex); // Create command pool(s). //commandPool = device.CreateCommandPool(new CommandPoolCreateInfo(computeQueueFamilyIndex)); }
private DeviceMemory AllocateMappableMemory(int size) { PhysicalDeviceMemoryProperties memoryProperties = DefaultPhysicalDevice.GetMemoryProperties(); int memoryTypeIndex = memoryProperties.MemoryTypes.IndexOf(~0, MemoryProperties.HostVisible | MemoryProperties.HostCoherent); return(DefaultDevice.AllocateMemory(new MemoryAllocateInfo(size, memoryTypeIndex))); }
void AppGpuDumpMemoryProps(AppGpu gpu, StreamWriter output) { PhysicalDeviceMemoryProperties props = gpu.MemoryProps; output.WriteLine("VkPhysicalDeviceMemoryProperties:"); output.WriteLine("================================="); output.WriteLine("\tmemoryTypeCount = {0}", props.MemoryTypeCount); for (int i = 0; i < props.MemoryTypeCount; i++) { MemoryType memoryType = props.MemoryTypes[i]; output.WriteLine("\tmemoryTypes[{0}] : ", i); output.WriteLine("\t\tpropertyFlags = {0}", memoryType.PropertyFlags); output.WriteLine("\t\theapIndex = {0}", memoryType.HeapIndex); } output.WriteLine("\tmemoryHeapCount = {0}", props.MemoryHeapCount); for (int i = 0; i < props.MemoryHeapCount; i++) { MemoryHeap memoryHeap = props.MemoryHeaps[i]; output.WriteLine("\tmemoryHeaps[{0}] : ", i); output.WriteLine("\t\tsize = {0}", memoryHeap.Size); } }
public void BindMemoryAndCreateView() { using (Image image = CreateImage()) { PhysicalDeviceMemoryProperties deviceMemProps = PhysicalDevice.GetMemoryProperties(); MemoryRequirements memReq = image.GetMemoryRequirements(); using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo( memReq.Size, deviceMemProps.MemoryTypes.IndexOf(memReq.MemoryTypeBits, 0)))) { image.BindMemory(memory); var createInfo = new ImageViewCreateInfo { Format = Format.B8G8R8A8UNorm, ViewType = ImageViewType.Image2D, SubresourceRange = new ImageSubresourceRange { AspectMask = ImageAspects.Color, LayerCount = 1, LevelCount = 1 } }; using (image.CreateView(createInfo)) { } using (image.CreateView(createInfo, CustomAllocator)) { } } } }
public bool TryCreateDevice(string[] extensionNames) { foreach (PhysicalDevice gpu in gpuList) { ExtensionProperties[] extensions = gpu.EnumerateDeviceExtensionProperties(""); if (!ValidateExtentsions(extensionNames, extensions)) { continue; } List <DeviceQueueCreateInfo> queueCreateInfo = new List <DeviceQueueCreateInfo>(); List <QueueInfo> queueInfoList = new List <QueueInfo>(); QueueFamilyProperties[] queueFamilys = gpu.GetQueueFamilyProperties(); if (queueFamilys == null || queueFamilys.Length <= 0) { continue; } else { for (uint i = 0; i < queueFamilys.Length; i++) { uint queueCount = queueFamilys[i].QueueCount; queueCreateInfo.Add(new DeviceQueueCreateInfo { QueueFamilyIndex = i, QueueCount = queueCount, QueuePriorities = new float[queueCount], }); for (uint j = 0; j < queueCount; j++) { queueInfoList.Add(new QueueInfo { queueFamilyIndex = i, queueIndex = j, flags = (QueueFlags)queueFamilys[i].QueueFlags, }); } } } DeviceCreateInfo info = new DeviceCreateInfo() { // LayerNames is "deprecated and ignore" in vulkan 1.0 spec QueueCreateInfos = queueCreateInfo.ToArray(), EnabledExtensionNames = extensionNames, }; Device = gpu.CreateDevice(info, null); Gpu = gpu; this.queueInfoList = queueInfoList.ToArray(); gpuMemoryProperties = gpu.GetMemoryProperties(); return(true); } return(false); }
public void GetMemoryProperties() { PhysicalDeviceMemoryProperties properties = PhysicalDevice.GetMemoryProperties(); Assert.True(properties.MemoryHeaps.Length > 0); Assert.True(properties.MemoryHeaps.All(x => x.Size > 0)); Assert.True(properties.MemoryTypes.Length > 0); Assert.True(properties.MemoryTypes.All(x => x.HeapIndex >= 0 && x.HeapIndex < properties.MemoryHeaps.Length)); }
public PhysicalDeviceInfo(PhysicalDevice physicalDevice) { PhysicalDevice = physicalDevice; Properties = PhysicalDevice.GetProperties(); QueueFamilyProperties = PhysicalDevice.GetQueueFamilyProperties(); MemoryProperties = PhysicalDevice.GetMemoryProperties(); Features = PhysicalDevice.GetFeatures(); GraphicsQFamilies = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.Graphics)); ComputeQFamilies = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.Compute)); TransferQFamilies = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.Transfer)); SparseBindingQFamilies = Array.AsReadOnly(QueueFamiliesWithFlag(QueueFlags.SparseBinding)); }
public void InvalidateMappedMemoryRange() { PhysicalDeviceMemoryProperties memoryProperties = PhysicalDevice.GetMemoryProperties(); int memoryTypeIndex = memoryProperties.MemoryTypes.IndexOf(~0, MemoryProperties.HostVisible); const int size = 1024; using (var memory = Device.AllocateMemory(new MemoryAllocateInfo(size, memoryTypeIndex))) { memory.Map(0, size); Device.InvalidateMappedMemoryRange(new MappedMemoryRange(memory, 0, size)); Device.InvalidateMappedMemoryRanges(new MappedMemoryRange(memory, 0, size)); memory.Unmap(); } }
public void BindMemoryAndCreateBufferView() { using (Buffer buffer = CreateBuffer()) { PhysicalDeviceMemoryProperties deviceMemProps = PhysicalDevice.GetMemoryProperties(); MemoryRequirements memReq = buffer.GetMemoryRequirements(); var memoryAllocateInfo = new MemoryAllocateInfo( memReq.Size, deviceMemProps.MemoryTypes.IndexOf(memReq.MemoryTypeBits, 0)); using (DeviceMemory memory = Device.AllocateMemory(memoryAllocateInfo)) { buffer.BindMemory(memory); var bufferViewCreateInfo = new BufferViewCreateInfo(Format.R32UInt); using (buffer.CreateView(bufferViewCreateInfo)) { } using (buffer.CreateView(bufferViewCreateInfo, CustomAllocator)) { } } } }
internal HostDevice( PhysicalDevice vulkanPhysicaldevice, SurfaceType surfaceType, Logger logger = null) { if (vulkanPhysicaldevice == null) { throw new ArgumentNullException(nameof(vulkanPhysicaldevice)); } this.physicalDevice = vulkanPhysicaldevice; this.surfaceType = surfaceType; this.logger = logger; this.properties = vulkanPhysicaldevice.GetProperties(); this.deviceMemoryProperties = vulkanPhysicaldevice.GetMemoryProperties(); this.supportedFeatures = vulkanPhysicaldevice.GetFeatures(); this.availableExtensions = vulkanPhysicaldevice.EnumerateExtensionProperties(); this.queueFamilies = vulkanPhysicaldevice.GetQueueFamilyProperties(); logger?.Log(nameof(HostDevice), $"Found device: {Name}"); logger?.LogList(nameof(HostDevice), $"{Name} available extensions:", availableExtensions); }
public static uint GetMemoryTypeIndex(uint memoryTypeBits, MemoryPropertyFlags desiredMemoryFlags) { uint memoryTypeIndex = 0; PhysicalDeviceMemoryProperties physicalDeviceMemoryProperties = VContext.Instance.physicalDevice.GetMemoryProperties(); for (uint i = 0; i < physicalDeviceMemoryProperties.MemoryTypeCount; i++) { if ((memoryTypeBits & 1) == 1) { MemoryType memoryType = physicalDeviceMemoryProperties.MemoryTypes[i]; if ((memoryType.PropertyFlags & desiredMemoryFlags) == desiredMemoryFlags) { memoryTypeIndex = i; break; } } memoryTypeBits >>= 1; } return(memoryTypeIndex); }
internal static unsafe extern void vkGetPhysicalDeviceMemoryProperties(PhysicalDevice physicalDevice, PhysicalDeviceMemoryProperties* memoryProperties);
//from the vulkan tutorial int FindMemoryType(int typeFilter, MemoryProperties memProps) { PhysicalDeviceMemoryProperties pdmp = mLogical.Parent.GetMemoryProperties(); return(pdmp.MemoryTypes.IndexOf(typeFilter, memProps)); }
void PhysicalDeviceMemoryProperties(PhysicalDeviceMemoryProperties physicalDeviceMemoryProperties) { WriteLine($"MemoryTypeCount = {physicalDeviceMemoryProperties.MemoryTypeCount}"); }
// find memory type with desired properties. int FindMemoryType(int memoryTypeBits, MemoryProperties properties) { PhysicalDeviceMemoryProperties memoryProperties = physicalDevice.GetMemoryProperties(); return(memoryProperties.MemoryTypes.IndexOf(memoryTypeBits, properties)); }
public ExampleRenderToDisk() { // The goal of this example is to: // // - Initialize Vulkan // - Create an empty image of size `imageWidth` x `imageHeight` // - Use that image as a render target for a triangle // - Save the render to disk // - Shutdown Vulkan // const string OutputFilename = "./out.bmp"; const Format ImageFormat = Format.R8g8b8a8Unorm; uint imageWidth = 800; uint imageHeight = 600; Instance instance; PhysicalDevice[] physDevices; PhysicalDevice physDevice; QueueFamilyProperties[] queueFamilies; //Device device; Queue queue; CommandPool cmdPool; instance = CreateInstance(); // Create a new Vulkan Instance physDevices = EnumeratePhysicalDevices(instance); // Discover the physical devices attached to the system physDevice = physDevices[0]; // Select the first physical device queueFamilies = physDevice.GetQueueFamilyProperties(); // Get properties about the queues on the physical device physDeviceMem = physDevice.GetMemoryProperties(); // Get properties about the memory on the physical device device = CreateDevice(physDevice, 0); // Create a device from the physical device queue = GetQueue(physDevice, 0); // Get an execution queue from the physical device cmdPool = CreateCommandPool(0); // Create a command pool from which command buffers are created // Now that we have a command pool, we can begin creating command buffers // and recording commands. You will find however that you can't do much of // anything without first initializing a few more dependencies. VertexData vertexData; ImageData imageData; Buffer imageBuffer; RenderPass renderPass; PipelineLayout pipelineLayout; Pipeline[] pipelines; Pipeline pipeline; Framebuffer framebuffer; // This exercise would be pointless if we had nothing to // render, so lets create that data now. vertexData = CreateVertexData(); // Since we didn't enable any extensions, we don't have access to // any display surfaces to render too. Instead we'll create an image // in memory and have Vulkan use it as the render target imageData = new ImageData(); imageData.Width = imageWidth; imageData.Height = imageHeight; imageData.Image = CreateImage(ImageFormat, imageWidth, imageHeight); imageData.Memory = BindImage(imageData.Image); imageData.View = CreateImageView(imageData.Image, ImageFormat); // Allocate device memory to the image so that it can be read from and written to var memRequirements = device.GetImageMemoryRequirements(imageData.Image); imageBuffer = CreateBuffer(memRequirements.Size, BufferUsageFlags.TransferSrc | BufferUsageFlags.TransferDst); var memoryIndex = FindMemoryIndex(MemoryPropertyFlags.HostVisible); var memAlloc = new MemoryAllocateInfo(memRequirements.Size, memoryIndex); var bufferMem = BindBuffer(imageBuffer, memAlloc); // Load shaders from disk and set them up to be passed to `CreatePipeline` var shaderStageCreateInfos = new[] { GetShaderStageCreateInfo(ShaderStageFlags.Vertex, "triangle.vert.spv"), GetShaderStageCreateInfo(ShaderStageFlags.Fragment, "triangle.frag.spv"), }; // Create the render dependencies renderPass = CreateRenderPass(ImageFormat); pipelineLayout = CreatePipelineLayout(); pipelines = CreatePipelines(pipelineLayout, renderPass, shaderStageCreateInfos, vertexData); pipeline = pipelines.First(); framebuffer = CreateFramebuffer(renderPass, imageData); // Render the triangle to the image Render(queue, cmdPool, vertexData, imageData, imageBuffer, renderPass, pipeline, framebuffer); var renderData = CopyBufferToArray(bufferMem, memRequirements); WriteBitmap(renderData, OutputFilename, (int)imageWidth, (int)imageHeight); Console.WriteLine($"Render written to {OutputFilename}"); #region Shutdown // Destroy Vulkan handles in reverse order of creation (roughly) device.DestroyShaderModule(shaderStageCreateInfos[0].Module); device.DestroyShaderModule(shaderStageCreateInfos[1].Module); device.DestroyBuffer(imageBuffer); device.FreeMemory(bufferMem); device.DestroyImageView(imageData.View); device.DestroyImage(imageData.Image); device.FreeMemory(imageData.Memory); device.DestroyBuffer(vertexData.Buffer); device.FreeMemory(vertexData.DeviceMemory); device.DestroyFramebuffer(framebuffer); device.DestroyPipeline(pipeline); device.DestroyPipelineLayout(pipelineLayout); device.DestroyRenderPass(renderPass); device.DestroyCommandPool(cmdPool); device.Destroy(); DebugUtils.DestroyDebugReportCallback(instance, debugCallback); instance.Destroy(); #endregion }
public Context(GameWindow window) { Window = window; Instance = ToDispose(VKHelper.CreateInstance()); DebugReportCallback = ToDispose(VKHelper.CreateDebugReportCallback(Instance)); Surface = ToDispose(VKHelper.CreateSurface(Instance, Window.Handle)); foreach (PhysicalDevice physicalDevice in Instance.EnumeratePhysicalDevices()) { QueueFamilyProperties[] queueFamilyProperties = physicalDevice.GetQueueFamilyProperties(); for (int i = 0; i < queueFamilyProperties.Length; i++) { if (queueFamilyProperties[i].QueueFlags.HasFlag(Queues.Graphics)) { if (GraphicsQueueFamilyIndex == -1) { GraphicsQueueFamilyIndex = i; } if (ComputeQueueFamilyIndex == -1) { ComputeQueueFamilyIndex = i; } if (physicalDevice.GetSurfaceSupportKhr(i, Surface) && VKHelper.GetPresentationSupport(physicalDevice, i)) { PresentQueueFamilyIndex = i; } if (GraphicsQueueFamilyIndex != -1 && ComputeQueueFamilyIndex != -1 && PresentQueueFamilyIndex != -1) { PhysicalDevice = physicalDevice; break; } } } if (PhysicalDevice != null) { break; } } if (PhysicalDevice == null) { throw new InvalidOperationException("No suitable physical device found."); } GenerateDepthStencilFormat(); // Store memory properties of the physical device. MemoryProperties = PhysicalDevice.GetMemoryProperties(); Features = PhysicalDevice.GetFeatures(); Properties = PhysicalDevice.GetProperties(); // Create a logical device. bool sameGraphicsAndPresent = GraphicsQueueFamilyIndex == PresentQueueFamilyIndex; var queueCreateInfos = new DeviceQueueCreateInfo[sameGraphicsAndPresent ? 1 : 2]; queueCreateInfos[0] = new DeviceQueueCreateInfo(GraphicsQueueFamilyIndex, 1, 1.0f); if (!sameGraphicsAndPresent) { queueCreateInfos[1] = new DeviceQueueCreateInfo(PresentQueueFamilyIndex, 1, 1.0f); } var deviceCreateInfo = new DeviceCreateInfo( queueCreateInfos, new[] { Constant.DeviceExtension.KhrSwapchain, Constant.DeviceExtension.KhrMaintenance1 }, Features); Device = PhysicalDevice.CreateDevice(deviceCreateInfo); // Get queue(s). GraphicsQueue = Device.GetQueue(GraphicsQueueFamilyIndex); ComputeQueue = ComputeQueueFamilyIndex == GraphicsQueueFamilyIndex ? GraphicsQueue : Device.GetQueue(ComputeQueueFamilyIndex); PresentQueue = PresentQueueFamilyIndex == GraphicsQueueFamilyIndex ? GraphicsQueue : Device.GetQueue(PresentQueueFamilyIndex); Content = new Content(this); GraphicsCommandPool = ToDispose(Device.CreateCommandPool(new CommandPoolCreateInfo(GraphicsQueueFamilyIndex, CommandPoolCreateFlags.ResetCommandBuffer))); ComputeCommandPool = ToDispose(Device.CreateCommandPool(new CommandPoolCreateInfo(ComputeQueueFamilyIndex))); Graphics = ToDispose(new Graphics(this)); Build(); }
public unsafe void GetMemoryProperties(out PhysicalDeviceMemoryProperties memoryProperties) { fixed (PhysicalDeviceMemoryProperties* __memoryProperties__ = &memoryProperties) { vkGetPhysicalDeviceMemoryProperties(this, __memoryProperties__); } }