Example #1
0
        protected override void CreateDescriptorSets()
        {
            var layouts = new DescriptorSetLayout[images.Length];

            for (int i = 0; i < layouts.Length; i++)
            {
                layouts[i] = descriptorSetLayout;
            }
            var allocInfo = new DescriptorSetAllocateInfo
            {
                DescriptorPool     = descriptorPool,
                DescriptorSetCount = (uint)images.Length,
                SetLayouts         = layouts
            };

            descriptorSets = device.AllocateDescriptorSets(allocInfo);
            for (int a = 0; a < descriptorSets.Length; a++)
            {
                var bufferInfo = new DescriptorBufferInfo
                {
                    Buffer = uniformBuffers[a],
                    Offset = 0,
                    Range  = (sizeof(float) * 16) * 3
                };

                var imageInfo = new DescriptorImageInfo
                {
                    ImageLayout = ImageLayout.ShaderReadOnlyOptimal,
                    ImageView   = textureImageView,
                    Sampler     = textureSampler
                };

                //TODO: Dispose this
                var descriptorWrites = new WriteDescriptorSet[]
                {
                    new WriteDescriptorSet
                    {
                        DstSet          = descriptorSets[a],
                        DstBinding      = 0,
                        DstArrayElement = 0,
                        DescriptorType  = DescriptorType.UniformBuffer,
                        DescriptorCount = 1,
                        BufferInfo      = new DescriptorBufferInfo[] { bufferInfo }
                    },
                    new WriteDescriptorSet
                    {
                        DstSet          = descriptorSets[a],
                        DstBinding      = 1,
                        DstArrayElement = 0,
                        DescriptorType  = DescriptorType.CombinedImageSampler,
                        DescriptorCount = 1,
                        ImageInfo       = new DescriptorImageInfo[] { imageInfo }
                    }
                };
                //device.UpdateDescriptorSet(descriptorWrites[0], null);
                //device.UpdateDescriptorSet(descriptorWrites[1], null);
                device.UpdateDescriptorSets(descriptorWrites, null);
            }
        }
        public unsafe void UpdateImage(int setIndex, int bindingIndex, DescriptorImageInfo imageInfo, DescriptorType type)
        {
            if (imageInfo.ImageView.Handle != 0UL)
            {
                var writeDescriptorSet = new WriteDescriptorSet
                {
                    SType           = StructureType.WriteDescriptorSet,
                    DstSet          = _descriptorSets[setIndex],
                    DstBinding      = (uint)bindingIndex,
                    DescriptorType  = type,
                    DescriptorCount = 1,
                    PImageInfo      = &imageInfo
                };

                _holder.Api.UpdateDescriptorSets(_holder.Device, 1, writeDescriptorSet, 0, null);
            }
        }
Example #3
0
        public void Write(string name, DescriptorImageInfo myInfo)
        {
            var Value = myIDsToItems[name];

            WriteDescriptorSet mySet = new WriteDescriptorSet();

            mySet.DescriptorType  = DescriptorType.UniformBuffer;
            mySet.DescriptorCount = Value.DescriptorCount;
            mySet.DstBinding      = Value.Binding;
            mySet.DstSet          = myDSet[Value.Binding];
            unsafe
            {
                mySet.ImageInfo = new DescriptorImageInfo[] { myInfo };
            }
            VulkanRenderer.SelectedLogicalDevice.UpdateDescriptorSet(mySet, null);
            mySet.Dispose();
        }
Example #4
0
        DescriptorSet CreateDescriptorSet(DescriptorPool pool, DescriptorSetLayout layout, ImageData imageData, UniformData uniformData)
        {
            var allocInfo           = new DescriptorSetAllocateInfo(pool, new[] { layout });
            var sets                = device.AllocateDescriptorSets(allocInfo);
            var descriptorSet       = sets.First();
            var texDescriptor       = new DescriptorImageInfo(imageData.Sampler, imageData.View, ImageLayout.General);
            var writeDescriptorSets = new[]
            {
                new WriteDescriptorSet(descriptorSet, 0, 0, DescriptorType.UniformBuffer, null, null, null),
                new WriteDescriptorSet(descriptorSet, 1, 0, DescriptorType.CombinedImageSampler, null, null, null),
            };

            writeDescriptorSets[0].BufferInfo = new[] { uniformData.Descriptor };
            writeDescriptorSets[1].ImageInfo  = new[] { texDescriptor };
            device.UpdateDescriptorSets(writeDescriptorSets, null);
            return(descriptorSet);
        }
Example #5
0
        public Tuple <string, DescriptorImageInfo>[] GetGBufferDescriptorImageInfo()
        {
            var myAttachments = this.myBackbuffer.GetAttachments();

            Tuple <string, DescriptorImageInfo>[] myDescriptorImages = new Tuple <string, DescriptorImageInfo> [myAttachments.Count];
            int lastInfoFilled = 0;

            foreach (FrameBufferAttachment A in myAttachments)
            {
                var myDescriptor = new DescriptorImageInfo();
                myDescriptor.ImageView             = A;
                myDescriptor.ImageLayout           = ImageLayout.ShaderReadOnlyOptimal;
                myDescriptor.Sampler               = mySample;
                myDescriptorImages[lastInfoFilled] = new Tuple <string, DescriptorImageInfo>(A.Name, myDescriptor);
                lastInfoFilled++;
            }
            return(myDescriptorImages);
        }
Example #6
0
        public void SetTextureAndSampler(int binding, ITexture texture, ISampler sampler)
        {
            if (texture == null)
            {
                return;
            }

            if (texture is TextureBuffer textureBuffer)
            {
                if (_bufferTextures.Length <= binding)
                {
                    Array.Resize(ref _bufferTextures, binding + 1);
                    Array.Resize(ref _bufferTextureRefs, binding + 1);
                }

                _bufferTextureRefs[binding] = textureBuffer;

                SignalDirty(DirtyFlags.BufferTexture);
            }
            else
            {
                if (sampler == null)
                {
                    return;
                }

                if (_textures.Length <= binding)
                {
                    Array.Resize(ref _textures, binding + 1);
                    Array.Resize(ref _textureRefs, binding + 1);
                    Array.Resize(ref _samplerRefs, binding + 1);
                }

                _textureRefs[binding] = ((TextureView)texture).GetImageView();
                _samplerRefs[binding] = ((SamplerHolder)sampler).GetSampler();

                _textures[binding] = new DescriptorImageInfo()
                {
                    ImageLayout = ImageLayout.General
                };

                SignalDirty(DirtyFlags.Texture);
            }
        }
        /// <summary>
        /// Sets a sampler state descriptor.
        /// </summary>
        /// <param name="slot">The slot.</param>
        /// <param name="samplerState">The sampler state.</param>
        public unsafe void SetSamplerState(int slot, SamplerState samplerState)
        {
            var imageInfo = new DescriptorImageInfo {
                Sampler = samplerState.NativeSampler
            };

            var write = new WriteDescriptorSet
            {
                sType                   = VkStructureType.WriteDescriptorSet,
                DescriptorCount         = 1,
                DestinationSet          = NativeDescriptorSet,
                DestinationBinding      = (uint)slot,
                DestinationArrayElement = 0,
                VkDescriptorType        = VkDescriptorType.Sampler,
                ImageInfo               = new IntPtr(&imageInfo),
            };

            GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
        }
Example #8
0
        private DescriptorSetCollection CreateImage(CommandBufferScoped cbs, ref HandleSet key)
        {
            var dsc = _gd.DescriptorSetManager.AllocateDescriptorSet(_gd.Api, _descriptorSetLayouts[PipelineBase.ImageSetIndex]).Get(cbs);

            Span <DescriptorImageInfo> imageInfos = stackalloc DescriptorImageInfo[key.Count];

            for (int i = 0; i < key.Count; i++)
            {
                imageInfos[i] = new DescriptorImageInfo()
                {
                    ImageLayout = ImageLayout.General,
                    ImageView   = new ImageView(key.Handles[i])
                };
            }

            dsc.UpdateImages(0, 0, imageInfos, DescriptorType.StorageImage);

            return(dsc);
        }
        /// <summary>
        /// Sets a shader resource view descriptor.
        /// </summary>
        /// <param name="slot">The slot.</param>
        /// <param name="shaderResourceView">The shader resource view.</param>
        public unsafe void SetShaderResourceView(int slot, GraphicsResource shaderResourceView)
        {
            var write = new WriteDescriptorSet
            {
                sType                   = VkStructureType.WriteDescriptorSet,
                DescriptorCount         = 1,
                DestinationSet          = NativeDescriptorSet,
                DestinationBinding      = (uint)slot,
                DestinationArrayElement = 0,
            };

            var texture = shaderResourceView as Texture;

            if (texture != null)
            {
                var imageInfo = new DescriptorImageInfo {
                    VkImageView = texture.NativeImageView, ImageLayout = VkImageLayout.ShaderReadOnlyOptimal
                };

                write.VkDescriptorType = VkDescriptorType.SampledImage;
                write.ImageInfo        = new IntPtr(&imageInfo);

                GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
            }
            else
            {
                var buffer = shaderResourceView as Buffer;
                if (buffer != null)
                {
                    var bufferViewCopy = buffer.NativeBufferView;

                    write.VkDescriptorType = VkDescriptorType.UniformTexelBuffer;
                    write.TexelBufferView  = new IntPtr(&bufferViewCopy);

                    GraphicsDevice.NativeDevice.UpdateDescriptorSets(1, &write, 0, null);
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
Example #10
0
        public void SetImage(int binding, ITexture image, GAL.Format imageFormat)
        {
            if (image == null)
            {
                return;
            }

            if (image is TextureBuffer imageBuffer)
            {
                if (_bufferImages.Length <= binding)
                {
                    Array.Resize(ref _bufferImages, binding + 1);
                    Array.Resize(ref _bufferImageRefs, binding + 1);
                }

                _bufferImageRefs[binding] = imageBuffer;

                SignalDirty(DirtyFlags.BufferImage);
            }
            else
            {
                if (_images.Length <= binding)
                {
                    Array.Resize(ref _images, binding + 1);
                    Array.Resize(ref _imageRefs, binding + 1);
                }

                if (image != null)
                {
                    _imageRefs[binding] = ((TextureView)image).GetIdentityImageView();
                    _images[binding]    = new DescriptorImageInfo()
                    {
                        ImageLayout = ImageLayout.General
                    };
                }

                SignalDirty(DirtyFlags.Image);
            }
        }
Example #11
0
        public unsafe RayTracingPipeline(
            Api api,
            SwapChain swapChain,
            TopLevelAccelerationStructure accelerationStructure,
            ImageView accumulationImageView,
            ImageView outputImageView,
            List <UniformBuffer> uniformBuffers,
            VulkanScene scene)
        {
            (_api, _swapChain) = (api, swapChain);

            // Create descriptor pool/sets.
            var descriptorBindings = new DescriptorBinding[]
            {
                // Top level acceleration structure.
                new DescriptorBinding(0, 1, DescriptorType.AccelerationStructureKhr, ShaderStageFlags.ShaderStageRaygenBitKhr),

                // Image accumulation & output
                new DescriptorBinding(1, 1, DescriptorType.StorageImage, ShaderStageFlags.ShaderStageRaygenBitKhr),
                new DescriptorBinding(2, 1, DescriptorType.StorageImage, ShaderStageFlags.ShaderStageRaygenBitKhr),

                // Camera information & co
                new DescriptorBinding(3, 1, DescriptorType.UniformBuffer, ShaderStageFlags.ShaderStageRaygenBitKhr | ShaderStageFlags.ShaderStageMissBitKhr),

                // Vertex buffer, Index buffer, Material buffer, Offset buffer, Transform buffer
                new DescriptorBinding(4, 1, DescriptorType.StorageBuffer, ShaderStageFlags.ShaderStageClosestHitBitKhr),
                new DescriptorBinding(5, 1, DescriptorType.StorageBuffer, ShaderStageFlags.ShaderStageClosestHitBitKhr),
                new DescriptorBinding(6, 1, DescriptorType.StorageBuffer, ShaderStageFlags.ShaderStageClosestHitBitKhr),
                new DescriptorBinding(7, 1, DescriptorType.StorageBuffer, ShaderStageFlags.ShaderStageClosestHitBitKhr),
                new DescriptorBinding(8, 1, DescriptorType.StorageBuffer, ShaderStageFlags.ShaderStageClosestHitBitKhr | ShaderStageFlags.ShaderStageIntersectionBitKhr),

                // Textures and image samplers
                new DescriptorBinding(9, (uint)scene.TextureSamplers.Count, DescriptorType.CombinedImageSampler, ShaderStageFlags.ShaderStageClosestHitBitKhr),

                // The Procedural buffer.
                new DescriptorBinding(10, 1, DescriptorType.StorageBuffer, ShaderStageFlags.ShaderStageClosestHitBitKhr | ShaderStageFlags.ShaderStageIntersectionBitKhr),
            };

            _descriptorSetManager = new DescriptorSetManager(_api, descriptorBindings, (ulong)uniformBuffers.Count);

            var descriptorSets = _descriptorSetManager.DescriptorSets;

            for (int i = 0; i != swapChain.VkImages.Count; i++)
            {
                // Top level acceleration structure.
                var _structureInfo = new WriteDescriptorSetAccelerationStructureKHR();
                _structureInfo.SType = StructureType.WriteDescriptorSetAccelerationStructureKhr;
                _structureInfo.PNext = null;
                _structureInfo.AccelerationStructureCount = 1;
                var accelerationStructurePinned = accelerationStructure.VkAccelerationStructure;
                _structureInfo.PAccelerationStructures = (AccelerationStructureKHR *)Unsafe.AsPointer(ref accelerationStructurePinned);

                // Accumulation image
                var _accumulationImageInfo = new DescriptorImageInfo();
                _accumulationImageInfo.ImageView   = accumulationImageView.VkImageView;
                _accumulationImageInfo.ImageLayout = ImageLayout.General;

                // Output image
                var _outputImageInfo = new DescriptorImageInfo();
                _outputImageInfo.ImageView   = outputImageView.VkImageView;
                _outputImageInfo.ImageLayout = ImageLayout.General;

                // Uniform buffer
                var _uniformBufferInfo = new DescriptorBufferInfo();
                _uniformBufferInfo.Buffer = uniformBuffers[i].Buffer.VkBuffer;
                _uniformBufferInfo.Range  = Vk.WholeSize;

                // Vertex buffer
                var _vertexBufferInfo = new DescriptorBufferInfo();
                _vertexBufferInfo.Buffer = scene.VertexBuffer.VkBuffer;
                _vertexBufferInfo.Range  = Vk.WholeSize;

                // Index buffer
                var _indexBufferInfo = new DescriptorBufferInfo();
                _indexBufferInfo.Buffer = scene.IndexBuffer.VkBuffer;
                _indexBufferInfo.Range  = Vk.WholeSize;

                // Material buffer
                var _materialBufferInfo = new DescriptorBufferInfo();
                _materialBufferInfo.Buffer = scene.MaterialBuffer.VkBuffer;
                _materialBufferInfo.Range  = Vk.WholeSize;

                // Offsets buffer
                var _offsetsBufferInfo = new DescriptorBufferInfo();
                _offsetsBufferInfo.Buffer = scene.OffsetsBuffer.VkBuffer;
                _offsetsBufferInfo.Range  = Vk.WholeSize;

                // Transforms buffer
                var _transformsBufferInfo = new DescriptorBufferInfo();
                _transformsBufferInfo.Buffer = scene.TransformsBuffer.VkBuffer;
                _transformsBufferInfo.Range  = Vk.WholeSize;

                // Image and texture samplers.
                var imageInfos = GC.AllocateArray <DescriptorImageInfo>(scene.TextureSamplers.Count, true);

                for (int t = 0; t != imageInfos.Length; ++t)
                {
                    var imageInfo = imageInfos[t];
                    imageInfo.ImageLayout = ImageLayout.ShaderReadOnlyOptimal;
                    imageInfo.ImageView   = scene.TextureImageViews[t];
                    imageInfo.Sampler     = scene.TextureSamplers[t];
                    imageInfos[t]         = imageInfo;
                }

                int descriptorWriteCt = scene.HasProcedurals ? 11 : 10;
#pragma warning disable CA2014 // Do not use stackalloc in loops
                Span <WriteDescriptorSet> descriptorWrites = stackalloc WriteDescriptorSet[descriptorWriteCt];
#pragma warning restore CA2014 // Do not use stackalloc in loops

                descriptorSets.Bind(i, 0, (WriteDescriptorSetAccelerationStructureKHR *)Unsafe.AsPointer(ref _structureInfo), out descriptorWrites[0]);
                descriptorSets.Bind(i, 1, (DescriptorImageInfo *)Unsafe.AsPointer(ref _accumulationImageInfo), out descriptorWrites[1]);
                descriptorSets.Bind(i, 2, (DescriptorImageInfo *)Unsafe.AsPointer(ref _outputImageInfo), out descriptorWrites[2]);
                descriptorSets.Bind(i, 3, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _uniformBufferInfo), out descriptorWrites[3]);
                descriptorSets.Bind(i, 4, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _vertexBufferInfo), out descriptorWrites[4]);
                descriptorSets.Bind(i, 5, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _indexBufferInfo), out descriptorWrites[5]);
                descriptorSets.Bind(i, 6, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _materialBufferInfo), out descriptorWrites[6]);
                descriptorSets.Bind(i, 7, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _offsetsBufferInfo), out descriptorWrites[7]);
                descriptorSets.Bind(i, 8, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _transformsBufferInfo), out descriptorWrites[8]);
                descriptorSets.Bind(i, 9, (DescriptorImageInfo *)Unsafe.AsPointer(ref imageInfos[0]), out descriptorWrites[9], (uint)imageInfos.Length);

                // Procedural buffer (optional)
                var _proceduralBufferInfo = new DescriptorBufferInfo();
                _proceduralBufferInfo.Buffer = scene.ProceduralBuffer.VkBuffer;
                _proceduralBufferInfo.Range  = Vk.WholeSize;

                if (scene.HasProcedurals)
                {
                    descriptorSets.Bind(i, 10, (DescriptorBufferInfo *)Unsafe.AsPointer(ref _proceduralBufferInfo), out descriptorWrites[10]);
                }

                descriptorSets.UpdateDescriptors(descriptorWrites);
            }

            _pipelineLayout = new PipelineLayout(_api, _descriptorSetManager.DescriptorSetLayout);

            // Load shaders.
            var rayGenShader                 = new ShaderModule(_api, "./assets/shaders/RayTracing.rgen.spv");
            var missShader                   = new ShaderModule(_api, "./assets/shaders/RayTracing.rmiss.spv");
            var closestHitShader             = new ShaderModule(_api, "./assets/shaders/RayTracing.rchit.spv");
            var proceduralClosestHitShader   = new ShaderModule(_api, "./assets/shaders/RayTracing.Procedural.rchit.spv");
            var proceduralIntersectionShader = new ShaderModule(_api, "./assets/shaders/RayTracing.Procedural.rint.spv");

            Span <PipelineShaderStageCreateInfo> shaderStages = stackalloc PipelineShaderStageCreateInfo[]
            {
                rayGenShader.CreateShaderStage(ShaderStageFlags.ShaderStageRaygenBitKhr),
                missShader.CreateShaderStage(ShaderStageFlags.ShaderStageMissBitKhr),
                closestHitShader.CreateShaderStage(ShaderStageFlags.ShaderStageClosestHitBitKhr),
                proceduralClosestHitShader.CreateShaderStage(ShaderStageFlags.ShaderStageClosestHitBitKhr),
                proceduralIntersectionShader.CreateShaderStage(ShaderStageFlags.ShaderStageIntersectionBitKhr)
            };

            const uint VK_SHADER_UNUSED_KHR = (~0U);

            // Shader groups
            var rayGenGroupInfo = new RayTracingShaderGroupCreateInfoKHR();
            rayGenGroupInfo.SType              = StructureType.RayTracingShaderGroupCreateInfoKhr;
            rayGenGroupInfo.PNext              = null;
            rayGenGroupInfo.Type               = RayTracingShaderGroupTypeKHR.RayTracingShaderGroupTypeGeneralKhr;
            rayGenGroupInfo.GeneralShader      = 0;
            rayGenGroupInfo.ClosestHitShader   = VK_SHADER_UNUSED_KHR;
            rayGenGroupInfo.AnyHitShader       = VK_SHADER_UNUSED_KHR;
            rayGenGroupInfo.IntersectionShader = VK_SHADER_UNUSED_KHR;
            _rayGenIndex = 0;

            var missGroupInfo = new RayTracingShaderGroupCreateInfoKHR();
            missGroupInfo.SType              = StructureType.RayTracingShaderGroupCreateInfoKhr;
            missGroupInfo.PNext              = null;
            missGroupInfo.Type               = RayTracingShaderGroupTypeKHR.RayTracingShaderGroupTypeGeneralKhr;
            missGroupInfo.GeneralShader      = 1;
            missGroupInfo.ClosestHitShader   = VK_SHADER_UNUSED_KHR;
            missGroupInfo.AnyHitShader       = VK_SHADER_UNUSED_KHR;
            missGroupInfo.IntersectionShader = VK_SHADER_UNUSED_KHR;
            _missIndex = 1;

            var triangleHitGroupInfo = new RayTracingShaderGroupCreateInfoKHR();
            triangleHitGroupInfo.SType              = StructureType.RayTracingShaderGroupCreateInfoKhr;
            triangleHitGroupInfo.PNext              = null;
            triangleHitGroupInfo.Type               = RayTracingShaderGroupTypeKHR.RayTracingShaderGroupTypeTrianglesHitGroupKhr;
            triangleHitGroupInfo.GeneralShader      = VK_SHADER_UNUSED_KHR;
            triangleHitGroupInfo.ClosestHitShader   = 2;
            triangleHitGroupInfo.AnyHitShader       = VK_SHADER_UNUSED_KHR;
            triangleHitGroupInfo.IntersectionShader = VK_SHADER_UNUSED_KHR;
            _triangleHitGroupIndex = 2;

            var proceduralHitGroupInfo = new RayTracingShaderGroupCreateInfoKHR();
            proceduralHitGroupInfo.SType              = StructureType.RayTracingShaderGroupCreateInfoKhr;
            proceduralHitGroupInfo.PNext              = null;
            proceduralHitGroupInfo.Type               = RayTracingShaderGroupTypeKHR.RayTracingShaderGroupTypeProceduralHitGroupKhr;
            proceduralHitGroupInfo.GeneralShader      = VK_SHADER_UNUSED_KHR;
            proceduralHitGroupInfo.ClosestHitShader   = 3;
            proceduralHitGroupInfo.AnyHitShader       = VK_SHADER_UNUSED_KHR;
            proceduralHitGroupInfo.IntersectionShader = 4;
            _proceduralHitGroupIndex = 3;

            Span <RayTracingShaderGroupCreateInfoKHR> groups = stackalloc RayTracingShaderGroupCreateInfoKHR[]
            {
                rayGenGroupInfo,
                missGroupInfo,
                triangleHitGroupInfo,
                proceduralHitGroupInfo,
            };

            // Create graphic pipeline
            var pipelineInfo = new RayTracingPipelineCreateInfoKHR();
            pipelineInfo.SType      = StructureType.RayTracingPipelineCreateInfoKhr;
            pipelineInfo.PNext      = null;
            pipelineInfo.Flags      = 0;
            pipelineInfo.StageCount = (uint)shaderStages.Length;
            pipelineInfo.PStages    = (PipelineShaderStageCreateInfo *)Unsafe.AsPointer(ref shaderStages[0]);
            pipelineInfo.GroupCount = (uint)groups.Length;
            pipelineInfo.PGroups    = (RayTracingShaderGroupCreateInfoKHR *)Unsafe.AsPointer(ref groups[0]);
            pipelineInfo.MaxPipelineRayRecursionDepth = 1;
            pipelineInfo.Layout             = _pipelineLayout.VkPipelineLayout;
            pipelineInfo.BasePipelineHandle = default;
            pipelineInfo.BasePipelineIndex  = 0;

            Util.Verify(
                _api.KhrRayTracingPipeline.CreateRayTracingPipelines(_api.Device.VkDevice, default, default, 1, pipelineInfo, default, out _pipeline),
Example #12
0
        public DescriptorSetUpdater(VulkanRenderer gd, PipelineBase pipeline)
        {
            _gd       = gd;
            _pipeline = pipeline;

            // Some of the bindings counts needs to be multiplied by 2 because we have buffer and
            // regular textures/images interleaved on the same descriptor set.

            _uniformBufferRefs  = new Auto <DisposableBuffer> [Constants.MaxUniformBufferBindings];
            _storageBufferRefs  = new Auto <DisposableBuffer> [Constants.MaxStorageBufferBindings];
            _textureRefs        = new Auto <DisposableImageView> [Constants.MaxTextureBindings * 2];
            _samplerRefs        = new Auto <DisposableSampler> [Constants.MaxTextureBindings * 2];
            _imageRefs          = new Auto <DisposableImageView> [Constants.MaxImageBindings * 2];
            _bufferTextureRefs  = new TextureBuffer[Constants.MaxTextureBindings * 2];
            _bufferImageRefs    = new TextureBuffer[Constants.MaxImageBindings * 2];
            _bufferImageFormats = new GAL.Format[Constants.MaxImageBindings * 2];

            _uniformBuffers = new DescriptorBufferInfo[Constants.MaxUniformBufferBindings];
            _storageBuffers = new DescriptorBufferInfo[Constants.MaxStorageBufferBindings];
            _textures       = new DescriptorImageInfo[Constants.MaxTexturesPerStage];
            _images         = new DescriptorImageInfo[Constants.MaxImagesPerStage];
            _bufferTextures = new BufferView[Constants.MaxTexturesPerStage];
            _bufferImages   = new BufferView[Constants.MaxImagesPerStage];

            var initialImageInfo = new DescriptorImageInfo()
            {
                ImageLayout = ImageLayout.General
            };

            _textures.AsSpan().Fill(initialImageInfo);
            _images.AsSpan().Fill(initialImageInfo);

            _uniformSet = new bool[Constants.MaxUniformBufferBindings];
            _storageSet = new bool[Constants.MaxStorageBufferBindings];

            if (gd.Capabilities.SupportsNullDescriptors)
            {
                // If null descriptors are supported, we can pass null as the handle.
                _dummyBuffer = null;
            }
            else
            {
                // If null descriptors are not supported, we need to pass the handle of a dummy buffer on unused bindings.
                _dummyBuffer = gd.BufferManager.Create(gd, 0x10000, forConditionalRendering: false, deviceLocal: true);
            }

            _dummyTexture = gd.CreateTextureView(new GAL.TextureCreateInfo(
                                                     1,
                                                     1,
                                                     1,
                                                     1,
                                                     1,
                                                     1,
                                                     1,
                                                     4,
                                                     GAL.Format.R8G8B8A8Unorm,
                                                     DepthStencilMode.Depth,
                                                     Target.Texture2D,
                                                     SwizzleComponent.Red,
                                                     SwizzleComponent.Green,
                                                     SwizzleComponent.Blue,
                                                     SwizzleComponent.Alpha), 1f);

            _dummySampler = (SamplerHolder)gd.CreateSampler(new GAL.SamplerCreateInfo(
                                                                MinFilter.Nearest,
                                                                MagFilter.Nearest,
                                                                false,
                                                                AddressMode.Repeat,
                                                                AddressMode.Repeat,
                                                                AddressMode.Repeat,
                                                                CompareMode.None,
                                                                GAL.CompareOp.Always,
                                                                new ColorF(0, 0, 0, 0),
                                                                0,
                                                                0,
                                                                0,
                                                                1f));
        }
Example #13
0
        public unsafe void ChangeTargetImages(
            Vector2D <uint> targetSize,
            ReadOnlyMemory <Image> targetImages,
            Format format,
            ColorSpaceKHR colorSpace)
        {
            _commandAllocs           = new Allocation[targetImages.Length];
            _commandBuffers          = new Buffer[targetImages.Length];
            _commandBufferCapacities = new int[targetImages.Length];
            _commandBufferCapacities.AsSpan().Fill(-1);

            if (_descriptorPool.Handle != 0)
            {
                _vk.DestroyDescriptorPool(_device, _descriptorPool, null);
            }

            _targetSize       = targetSize;
            _targetImages     = targetImages;
            _targetImageViews = new ImageView[targetImages.Length];

            var poolSizes = stackalloc  DescriptorPoolSize[]
            {
                new DescriptorPoolSize(DescriptorType.StorageImage, (uint)targetImages.Length),
                new DescriptorPoolSize(DescriptorType.StorageBuffer, (uint)targetImages.Length),
            };

            _vk.CreateDescriptorPool(_device,
                                     new DescriptorPoolCreateInfo(maxSets: (uint)targetImages.Length, poolSizeCount: 2,
                                                                  pPoolSizes: poolSizes), null, out _descriptorPool).ThrowCode();
            Name(ObjectType.DescriptorPool, _descriptorPool.Handle, $"Target Descriptor Pool");

            _targetDescriptorSets = new DescriptorSet[targetImages.Length];
            var setLayouts = stackalloc DescriptorSetLayout[targetImages.Length];

            new Span <DescriptorSetLayout>(setLayouts, targetImages.Length).Fill(_setLayout);
            var info = new DescriptorSetAllocateInfo(descriptorPool: _descriptorPool,
                                                     descriptorSetCount: (uint)targetImages.Length, pSetLayouts: setLayouts);

            _vk.AllocateDescriptorSets(_device, &info, _targetDescriptorSets.AsSpan()).ThrowCode();

            var imageInfos = stackalloc DescriptorImageInfo[targetImages.Length];
            Span <WriteDescriptorSet> writes = stackalloc WriteDescriptorSet[targetImages.Length];

            for (int i = 0; i < targetImages.Length; i++)
            {
                Name(ObjectType.Image, _targetImages.Span[i].Handle, $"Target Image {i}");

                // _vk.CreateImage(_device,
                //     new ImageCreateInfo(imageType: ImageType.ImageType2D, format: Format.R8G8B8A8Srgb,
                //         extent: new Extent3D(_targetSize.X, _targetSize.Y, 1), mipLevels: 1, arrayLayers: 1,
                //         samples: SampleCountFlags.SampleCount1Bit, tiling: ImageTiling.Optimal,
                //         initialLayout: ImageLayout.Undefined, usage: ImageUsageFlags.ImageUsageStorageBit,
                //         sharingMode: SharingMode.Exclusive, queueFamilyIndexCount: 0, pQueueFamilyIndices: null), null,
                //     out _ownImages[i]).ThrowCode();

                _vk.CreateImageView(_device,
                                    new ImageViewCreateInfo(image: _targetImages.Span[i], viewType: ImageViewType.ImageViewType2D,
                                                            format: format,
                                                            components: new ComponentMapping(ComponentSwizzle.Identity, ComponentSwizzle.Identity,
                                                                                             ComponentSwizzle.Identity, ComponentSwizzle.Identity),
                                                            subresourceRange: new ImageSubresourceRange(ImageAspectFlags.ImageAspectColorBit, 0, 1, 0, 1)),
                                    null, out _targetImageViews[i]).ThrowCode();
                Name(ObjectType.ImageView, _targetImageViews[i].Handle, $"Target Image View {i}");

                imageInfos[i] = new DescriptorImageInfo(imageView: _targetImageViews[i], imageLayout: ImageLayout.General);
                writes[i]     = new WriteDescriptorSet(dstSet: _targetDescriptorSets[i], dstBinding: 0, dstArrayElement: 0,
                                                       descriptorCount: 1, descriptorType: DescriptorType.StorageImage, pImageInfo: (imageInfos + i));

                Name(ObjectType.DescriptorSet, _targetDescriptorSets[i].Handle, $"Target Descriptor {i}");
            }

            _vk.UpdateDescriptorSets(_device, (uint)targetImages.Length, writes, 0, (CopyDescriptorSet *)null);
        }
Example #14
0
        public unsafe void SetUniforms(Frame frame, DescriptorSet descriptorSet, ulong uniformOffset, int image)
        {
            DescriptorBufferInfo vertexUniformBufferInfo = frame.VertexUniformBuffer.BufferInfo;

            vertexUniformBufferInfo.Offset = 0;
            DescriptorBufferInfo fragmentUniformBufferInfo = frame.FragmentUniformBuffer.BufferInfo;

            fragmentUniformBufferInfo.Offset = uniformOffset;
            fragmentUniformBufferInfo.Range  = (uint)Marshal.SizeOf <FragUniforms>();

            int id = _renderer.DummyTex;

            if (image != 0)
            {
                id = image;
            }
            DescriptorImageInfo fragmentImageInfo = _renderer.TextureManager.FindTexture(id).ImageInfo;

            Span <WriteDescriptorSet> descriptorWrites = stackalloc WriteDescriptorSet[]
            {
                new WriteDescriptorSet() // Vertex Uniform Buffer
                {
                    SType = StructureType.WriteDescriptorSet,

                    DstBinding      = 0,
                    DstArrayElement = 0,
                    DstSet          = descriptorSet,

                    DescriptorCount = 1,
                    DescriptorType  = DescriptorType.UniformBuffer,
                    PBufferInfo     = &vertexUniformBufferInfo
                },
                new WriteDescriptorSet() // Fragment Uniform Buffer
                {
                    SType = StructureType.WriteDescriptorSet,

                    DstBinding      = 1,
                    DstArrayElement = 0,
                    DstSet          = descriptorSet,

                    DescriptorCount = 1,
                    DescriptorType  = DescriptorType.UniformBuffer,
                    PBufferInfo     = &fragmentUniformBufferInfo
                },
                new WriteDescriptorSet() // Fragment Image Sampler(2D)
                {
                    SType = StructureType.WriteDescriptorSet,

                    DstBinding      = 2,
                    DstArrayElement = 0,
                    DstSet          = descriptorSet,

                    DescriptorCount = 1,
                    DescriptorType  = DescriptorType.CombinedImageSampler,
                    PImageInfo      = &fragmentImageInfo
                }
            };

            Device device = _renderer.Params.Device;
            Vk     vk     = _renderer.Vk;

            vk.UpdateDescriptorSets(device, (uint)descriptorWrites.Length, in descriptorWrites[0], 0, null);
        }