コード例 #1
0
        public BindlessDescriptorSet(Vk vk, Device device, int pushConstantSize, int numSampledImages = 512 * 1024, int numStorageImages = 64 * 1024, int numSamplers = 4 * 1024)
        {
            _vk               = vk;
            _device           = device;
            _pushConstantSize = pushConstantSize;
            _numSampledImages = numSampledImages;
            _numStorageImages = numStorageImages;
            _numSamplers      = numSamplers;

            var pBindings = stackalloc DescriptorSetLayoutBinding[]
            {
                new DescriptorSetLayoutBinding(SampledImageBinding, DescriptorType.SampledImage, (uint)numSampledImages, ShaderStageFlags.ShaderStageAll),
                new DescriptorSetLayoutBinding(StorageImageBinding, DescriptorType.StorageImage, (uint)numStorageImages, ShaderStageFlags.ShaderStageAll),
                new DescriptorSetLayoutBinding(SamplerBinding, DescriptorType.Sampler, (uint)numSamplers, ShaderStageFlags.ShaderStageAll),
            };

            _vk.CreateDescriptorSetLayout(_device, new DescriptorSetLayoutCreateInfo(flags: DescriptorSetLayoutCreateFlags.DescriptorSetLayoutCreateUpdateAfterBindPoolBit,
                                                                                     bindingCount: 3, pBindings: pBindings), null, out var layout).ThrowCode();
            _descriptorSetLayout = layout;

            if (pushConstantSize > 0)
            {
                var pushConstantRange =
                    new PushConstantRange(ShaderStageFlags.ShaderStageAll, 0, (uint)pushConstantSize);
                _vk.CreatePipelineLayout(_device,
                                         new PipelineLayoutCreateInfo(setLayoutCount: 1, pSetLayouts: &layout, pushConstantRangeCount: 1,
                                                                      pPushConstantRanges: &pushConstantRange), null, out _pipelineLayout).ThrowCode();
            }
            else
            {
                _vk.CreatePipelineLayout(_device,
                                         new PipelineLayoutCreateInfo(setLayoutCount: 1, pSetLayouts: &layout, pushConstantRangeCount: 0,
                                                                      pPushConstantRanges: null), null, out _pipelineLayout).ThrowCode();
            }
        }
コード例 #2
0
        public unsafe void Initialize(
            int maxFrames,
            QueueManager queueManager,
            Vk vk,
            Instance instance,
            Device device,
            PhysicalDevice physicalDevice
#if DEBUG
            , ExtDebugUtils debugUtils
#endif
            )
        {
#if DEBUG
            _debugUtils = debugUtils;
#endif
            _vma          = new VulkanMemoryAllocator(new VulkanMemoryAllocatorCreateInfo(new Version32(1, 1, 0), vk, instance, physicalDevice, device, AllocatorCreateFlags.BufferDeviceAddress));
            _frames       = new FrameData[maxFrames];
            _queueManager = queueManager;
            _vk           = vk;
            _instance     = instance;
            _device       = device;
            // if (!_vk.TryGetDeviceExtension(_instance, _device, out _khrSynchronization2))
            //     throw new Exception($"{KhrSynchronization2.ExtensionName} not found!");
            Console.WriteLine("Initializing Render Graph");

            (QueueFamilyIndex, Queue) = _queueManager.GetQueue(true, true, false, true);

            Console.WriteLine("Creating Command Pools");
            for (int i = 0; i < _frames.Length; i++)
            {
                _vk.CreateCommandPool(_device, new CommandPoolCreateInfo(queueFamilyIndex: QueueFamilyIndex), null,
                                      out var commandPool).ThrowCode();

                _frames[i] = new FrameData(commandPool);
            }

            Console.WriteLine("Creating Descriptor Layout");
            var bindings = stackalloc DescriptorSetLayoutBinding[]
            {
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageImage, 1,
                                               ShaderStageFlags.ShaderStageComputeBit, null),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1,
                                               ShaderStageFlags.ShaderStageComputeBit, null),
            };
            _vk.CreateDescriptorSetLayout(_device,
                                          new DescriptorSetLayoutCreateInfo(bindingCount: 2, pBindings: bindings), null, out var setLayout).ThrowCode();
            _setLayout = setLayout;
            Name(ObjectType.DescriptorSetLayout, _setLayout.Handle, "Primary Set Layout");

            var pushConstantRange = new PushConstantRange(ShaderStageFlags.ShaderStageComputeBit, 0, sizeof(uint) * 2);
            _vk.CreatePipelineLayout(_device,
                                     new PipelineLayoutCreateInfo(setLayoutCount: 1, pSetLayouts: &setLayout, pushConstantRangeCount: 1,
                                                                  pPushConstantRanges: &pushConstantRange), null, out _pipelineLayout).ThrowCode();

            Name(ObjectType.PipelineLayout, _pipelineLayout.Handle, "Primary Pipeline Layout");

            Console.WriteLine("Loading Primary Compute");
            _computeShader = new ComputeShader(vk, instance, device, physicalDevice, _pipelineLayout,
                                               (ReadOnlySpan <byte>)File.ReadAllBytes("./shaders/compute.spv"), "main", null);

            _vk.GetPhysicalDeviceMemoryProperties(physicalDevice, out var memoryProperties);
            _memoryTypes = new MemoryType[32];
            new Span <MemoryType>(&memoryProperties.MemoryTypes.Element0, 32).CopyTo(_memoryTypes);

            EngineContext = new EngineContext();
            Console.WriteLine("Done Initializing Render Graph");
        }