Пример #1
0
        void setupDescriptorSet()
        {
            // Scene rendering
            {
                VkDescriptorSetLayout dsl = setLayoutScene;
                var info = VkDescriptorSetAllocateInfo.Alloc();
                info->setLayouts     = dsl;
                info->descriptorPool = descriptorPool;
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, info, &set);
                this.setScene = set;
            }
            {
                var descriptor0 = uniformBufferScene.descriptor;
                var descriptor1 = textures_gradient.descriptor;
                var writes      = VkWriteDescriptorSet.Alloc(2);
                {
                    // Binding 0: Vertex shader uniform buffer
                    writes[0].dstSet = setScene;
                    writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
                    writes[0].dstBinding          = 0;
                    writes[0].data.Set(descriptor0);
                    // Binding 1: Color gradient sampler
                    writes[1].dstSet = setScene;
                    writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
                    writes[1].dstBinding          = 1;
                    writes[1].data.Set(descriptor1);
                }
                vkUpdateDescriptorSets(device, 2, writes, 0, null);
            }

            // Fullscreen radial blur
            {
                VkDescriptorSetLayout dsl = setLayoutRadialBlur;
                var info = VkDescriptorSetAllocateInfo.Alloc();
                info->setLayouts     = dsl;
                info->descriptorPool = descriptorPool;
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, info, &set);
                this.setRadialBlur = set;
            }
            {
                VkDescriptorBufferInfo descriptor0 = uniformBufferBlurParams.descriptor;
                VkDescriptorImageInfo  descriptor1 = offscreenPass.descriptorImage;
                var writes = VkWriteDescriptorSet.Alloc(2);
                {
                    // Binding 0: Vertex shader uniform buffer
                    writes[0].dstSet = setRadialBlur;
                    writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
                    writes[0].dstBinding          = 0;
                    writes[0].data.Set(descriptor0);
                    // Binding 0: Fragment shader texture sampler
                    writes[1].dstSet = setRadialBlur;
                    writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
                    writes[1].dstBinding          = 1;
                    writes[1].data.Set(descriptor1);
                }
                vkUpdateDescriptorSets(device, 2, writes, 0, null);
            }
        }
Пример #2
0
        void setupDescriptorSet()
        {
            var dsl = descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo =
                Initializers.descriptorSetAllocateInfo(
                    descriptorPool,
                    &dsl,
                    1);

            Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSet));

            VkDescriptorImageInfo texDescriptor =
                Initializers.descriptorImageInfo(
                    textures_colorMap.sampler,
                    textures_colorMap.view,
                    VkImageLayout.General);

            var temp = uniformBuffers_scene.descriptor;
            FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>(
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSet,
                    VkDescriptorType.UniformBuffer,
                    0,
                    &temp),
                // Binding 1 : Color map
                Initializers.writeDescriptorSet(
                    descriptorSet,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &texDescriptor));

            vkUpdateDescriptorSets(device, (writeDescriptorSets.Count), ref writeDescriptorSets.First, 0, null);
        }
Пример #3
0
        public void Set(uint binding, uint idx, ImageView img, bool rw)
        {
            var img_info = new VkDescriptorImageInfo()
            {
                sampler     = IntPtr.Zero,
                imageView   = img.hndl,
                imageLayout = rw ? VkImageLayout.ImageLayoutGeneral : VkImageLayout.ImageLayoutShaderReadOnlyOptimal
            };
            var img_info_ptr = img_info.Pointer();

            var desc_write = new VkWriteDescriptorSet()
            {
                sType            = VkStructureType.StructureTypeWriteDescriptorSet,
                dstSet           = hndl,
                dstBinding       = binding,
                dstArrayElement  = idx,
                descriptorCount  = 1,
                pImageInfo       = img_info_ptr,
                pBufferInfo      = IntPtr.Zero,
                pTexelBufferView = null,
                descriptorType   = (VkDescriptorType)Layout.Layouts[(int)binding].Type
            };

            vkUpdateDescriptorSets(GraphicsDevice.GetDeviceInfo(devID).Device, 1, desc_write.Pointer(), 0, null);
        }
Пример #4
0
        void SetupDescriptorSet(Pipeline pipeline, GraphicsDevice device, UniformBuffer uniformBuffer, Texture2D texture_colorMap)
        {
            var dsl = pipeline.descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo =
                Initializers.descriptorSetAllocateInfo(
                    pipeline.descriptorPool,
                    &dsl,
                    1);

            Util.CheckResult(vkAllocateDescriptorSets(device.device, &allocInfo, out pipeline.descriptorSet));

            VkDescriptorImageInfo texDescriptor =
                Initializers.descriptorImageInfo(
                    texture_colorMap.sampler,
                    texture_colorMap.view,
                    VkImageLayout.General);

            VkDescriptorBufferInfo temp = uniformBuffer.GetVkDescriptor();

            FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>(
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    pipeline.descriptorSet,
                    VkDescriptorType.UniformBuffer,
                    uniformBuffer.location,
                    &temp),
                // Binding 1 : Color map
                Initializers.writeDescriptorSet(
                    pipeline.descriptorSet,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &texDescriptor));

            vkUpdateDescriptorSets(device.device, (writeDescriptorSets.Count), ref writeDescriptorSets.First, 0, null);
        }
Пример #5
0
        public unsafe void UpdateSampledImage(ImageView view, Sampler sampler, int binding)
        {
            var vulkanBinding       = _descriptorPool.Layout.Bindings[binding];
            var descriptorImageInfo = new VkDescriptorImageInfo
            {
                imageLayout = view.Image.Layout.First(),
                imageView   = view.Handle,
                sampler     = sampler.Handle
            };

            var writeDescriptorSet = new VkWriteDescriptorSet
            {
                sType           = VkStructureType.WriteDescriptorSet,
                dstSet          = _handle,
                dstBinding      = vulkanBinding.Index,
                dstArrayElement = 0,
                descriptorCount = vulkanBinding.DescriptorCounts,
                descriptorType  = vulkanBinding.DescriptorType,
                pImageInfo      = &descriptorImageInfo
            };

            VulkanNative.vkUpdateDescriptorSets(
                _device.Handle,
                1,
                &writeDescriptorSet,
                0,
                null
                );
        }
Пример #6
0
        void setupDescriptorSets()
        {
            // Image descriptor for the cube map texture
            VkDescriptorImageInfo textureDescriptor =
                Initializers.descriptorImageInfo(
                    cubeMap.sampler,
                    cubeMap.view,
                    cubeMap.imageLayout);

            var dsl = descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo =
                Initializers.descriptorSetAllocateInfo(
                    descriptorPool,
                    &dsl,
                    1);

            // 3D object descriptor set
            Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSets_object));

            var bufferInfo = uniformBuffers_object.descriptor;
            FixedArray2 <VkWriteDescriptorSet> writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>
                                                                     (
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSets_object,
                    VkDescriptorType.UniformBuffer,
                    0,
                    &bufferInfo),
                // Binding 1 : Fragment shader cubemap sampler
                Initializers.writeDescriptorSet(
                    descriptorSets_object,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &textureDescriptor));

            vkUpdateDescriptorSets(device, writeDescriptorSets.Count, ref writeDescriptorSets.First, 0, null);

            // Sky box descriptor set
            Util.CheckResult(vkAllocateDescriptorSets(device, &allocInfo, out descriptorSets_skybox));

            var descriptor = uniformBuffers_skybox.descriptor;

            writeDescriptorSets = new FixedArray2 <VkWriteDescriptorSet>
                                  (
                // Binding 0 : Vertex shader uniform buffer
                Initializers.writeDescriptorSet(
                    descriptorSets_skybox,
                    VkDescriptorType.UniformBuffer,
                    0,
                    &descriptor),
                // Binding 1 : Fragment shader cubemap sampler
                Initializers.writeDescriptorSet(
                    descriptorSets_skybox,
                    VkDescriptorType.CombinedImageSampler,
                    1,
                    &textureDescriptor));

            vkUpdateDescriptorSets(device, writeDescriptorSets.Count, ref writeDescriptorSets.First, 0, null);
        }
Пример #7
0
        public static VkDescriptorImageInfo descriptorImageInfo(VkSampler sampler, VkImageView imageView, VkImageLayout imageLayout)
        {
            VkDescriptorImageInfo descriptorImageInfo = new VkDescriptorImageInfo();

            descriptorImageInfo.sampler     = sampler;
            descriptorImageInfo.imageView   = imageView;
            descriptorImageInfo.imageLayout = imageLayout;
            return(descriptorImageInfo);
        }
Пример #8
0
        void CreateDescriptorSet()
        {
            var layouts = new List <VkDescriptorSetLayout> {
                descriptorSetLayout
            };
            var info = new VkDescriptorSetAllocateInfo();

            info.setLayouts = layouts;

            descriptorSet = descriptorPool.Allocate(info)[0];

            var bufferInfo = new VkDescriptorBufferInfo();

            bufferInfo.buffer = uniformBuffer;
            bufferInfo.offset = 0;
            bufferInfo.range  = Interop.SizeOf <UniformBufferObject>();

            var imageInfo = new VkDescriptorImageInfo();

            imageInfo.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
            imageInfo.imageView   = textureImageView;
            imageInfo.sampler     = textureSampler;

            var descriptorWrites = new List <VkWriteDescriptorSet>();

            descriptorWrites.Add(new VkWriteDescriptorSet());
            descriptorWrites[0].dstSet          = descriptorSet;
            descriptorWrites[0].dstBinding      = 0;
            descriptorWrites[0].dstArrayElement = 0;
            descriptorWrites[0].descriptorType  = VkDescriptorType.UniformBuffer;
            descriptorWrites[0].bufferInfo      = new List <VkDescriptorBufferInfo> {
                bufferInfo
            };

            descriptorWrites.Add(new VkWriteDescriptorSet());
            descriptorWrites[1].dstSet          = descriptorSet;
            descriptorWrites[1].dstBinding      = 1;
            descriptorWrites[1].dstArrayElement = 0;
            descriptorWrites[1].descriptorType  = VkDescriptorType.CombinedImageSampler;
            descriptorWrites[1].imageInfo       = new List <VkDescriptorImageInfo> {
                imageInfo
            };

            descriptorSet.Update(descriptorWrites, null);
        }
Пример #9
0
        public unsafe void UpdateSampledImages(List <ImageView> views, List <Sampler> samplers)
        {
            if (views.Count != _descriptorPool.Layout.Bindings.Count)
            {
                throw new InvalidOperationException("views length should match descriptor layout bindings");
            }
            if (samplers.Count != _descriptorPool.Layout.Bindings.Count)
            {
                throw new InvalidOperationException("samplers length should match descriptor layout bindings");
            }

            var descriptorImageInfos = new NativeList <VkDescriptorImageInfo>();
            var writeDescriptorSets  = new NativeList <VkWriteDescriptorSet>();

            for (int i = 0; i < _descriptorPool.Layout.Bindings.Count; i++)
            {
                var binding             = _descriptorPool.Layout.Bindings[i];
                var descriptorImageInfo = new VkDescriptorImageInfo
                {
                    imageLayout = views[i].Image.Layout.First(),
                    imageView   = views[i].Handle,
                    sampler     = samplers[i].Handle
                };

                var writeDescriptorSet = new VkWriteDescriptorSet
                {
                    sType           = VkStructureType.WriteDescriptorSet,
                    dstSet          = _handle,
                    dstBinding      = binding.Index,
                    dstArrayElement = 0,
                    descriptorCount = binding.DescriptorCounts,
                    descriptorType  = binding.DescriptorType,
                    pImageInfo      = &descriptorImageInfo
                };
                descriptorImageInfos.Add(descriptorImageInfo);
                writeDescriptorSets.Add(writeDescriptorSet);
            }
            VulkanNative.vkUpdateDescriptorSets(
                _device.Handle,
                writeDescriptorSets.Count,
                (VkWriteDescriptorSet *)writeDescriptorSets.Data.ToPointer(),
                0,
                null
                );
        }
Пример #10
0
        void setupDescriptorSet()
        {
            VkDescriptorSetLayout       dsl       = descriptorSetLayout;
            VkDescriptorSetAllocateInfo allocInfo = new VkDescriptorSetAllocateInfo();

            allocInfo.sType          = DescriptorSetAllocateInfo;
            allocInfo.descriptorPool = descriptorPool;
            allocInfo.setLayouts     = dsl;

            {
                VkDescriptorSet set;
                vkAllocateDescriptorSets(device, &allocInfo, &set);
                this.descriptorSet = set;
            }

            VkDescriptorImageInfo texDescriptor = new VkDescriptorImageInfo();

            texDescriptor.sampler     = textures_colorMap.sampler;
            texDescriptor.imageView   = textures_colorMap.view;
            texDescriptor.imageLayout = VkImageLayout.General;

            VkDescriptorBufferInfo temp = uniformBuffers_scene.descriptor;
            var writes = new VkWriteDescriptorSet[2];

            // Binding 0 : Vertex shader uniform buffer
            writes[0]        = new VkWriteDescriptorSet();
            writes[0].sType  = WriteDescriptorSet;
            writes[0].dstSet = descriptorSet;
            writes[0].data.descriptorType = VkDescriptorType.UniformBuffer;
            writes[0].dstBinding          = 0;
            writes[0].data.Set(temp);
            // Binding 1 : Color map
            writes[1]        = new VkWriteDescriptorSet();
            writes[1].sType  = WriteDescriptorSet;
            writes[1].dstSet = descriptorSet;
            writes[1].data.descriptorType = VkDescriptorType.CombinedImageSampler;
            writes[1].dstBinding          = 1;
            writes[1].data.Set(texDescriptor);

            fixed(VkWriteDescriptorSet *pointer = writes)
            {
                vkUpdateDescriptorSets(device, (UInt32)writes.Length, pointer, 0, null);
            }
        }
Пример #11
0
        private void PrepareDescriptor(VkDevice device)
        {
            // 今は定数バッファを1つ、サンプラーを1つを格納できるだけのディスクリプタプールを準備.
            VkDescriptorPoolSize descriptorPoolSize = new VkDescriptorPoolSize()
            {
                descriptorCount = 1,
                type            = VkDescriptorType.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
            };
            VkDescriptorPoolSize descriptorPoolSizeForSampler = new VkDescriptorPoolSize()
            {
                descriptorCount = 1,
                type            = VkDescriptorType.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
            };
            var descriptorPoolCreateInfo = new VkDescriptorPoolCreateInfo()
            {
                poolSizes = new[] { descriptorPoolSize, descriptorPoolSizeForSampler },
                maxSets   = 1,
                flags     = VkDescriptorPoolCreateFlags.VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT,
            };

            VulkanAPI.vkCreateDescriptorPool(device, ref descriptorPoolCreateInfo, out m_descriptorPool);

            // ディスクリプタセットレイアウトの作成.
            //  - 定数バッファを1つ
            //  - テクスチャサンプラ1つ
            var descLayoutBindingForUniform = new VkDescriptorSetLayoutBinding();

            descLayoutBindingForUniform.binding         = 0;
            descLayoutBindingForUniform.descriptorCount = 1;
            descLayoutBindingForUniform.descriptorType  = VkDescriptorType.VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
            descLayoutBindingForUniform.stageFlags      = VkShaderStageFlagBits.VK_SHADER_STAGE_VERTEX_BIT;
            var descLayoutBindingForSampler = new VkDescriptorSetLayoutBinding();

            descLayoutBindingForSampler.binding         = 1;
            descLayoutBindingForSampler.descriptorCount = 1;
            descLayoutBindingForSampler.descriptorType  = VkDescriptorType.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
            descLayoutBindingForSampler.stageFlags      = VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT;
            var descriptorSetLayoutCreateInfo = new VkDescriptorSetLayoutCreateInfo();

            descriptorSetLayoutCreateInfo.bindings = new[] {
                descLayoutBindingForUniform,
                descLayoutBindingForSampler
            };
            VulkanAPI.vkCreateDescriptorSetLayout(device, ref descriptorSetLayoutCreateInfo, out m_descriptorSetLayout);


            // ディスクリプタを作成&更新.
            VkDescriptorSet[] descriptorSets;
            var descriptorSetAllocateInfo = new VkDescriptorSetAllocateInfo(m_descriptorPool, new[] { m_descriptorSetLayout });

            VulkanAPI.vkAllocateDescriptorSets(device, ref descriptorSetAllocateInfo, out descriptorSets);
            m_descriptorSet = descriptorSets[0];

            var descUniform = new VkDescriptorBufferInfo()
            {
                buffer = m_uniformBuffer,
                range  = Marshal.SizeOf <Transform>(),
            };
            var descSampler = new VkDescriptorImageInfo()
            {
                imageView = m_imageView,
                sampler   = m_imageSampler,
            };
            var descForUniform   = SampleHelpers.CreateDescriptorFromUniformBuffer(0, descUniform, m_descriptorSet);
            var descForSampler   = SampleHelpers.CreateDescriptorFromImageSampler(1, descSampler, m_descriptorSet);
            var descriptorWrites = new[] { descForUniform, descForSampler };

            VulkanAPI.vkUpdateDescriptorSets(device, descriptorWrites, null);
        }
Пример #12
0
        /// <summary>
        /// テクスチャのためのディスクリプタの生成.
        /// </summary>
        /// <param name="destBinding"></param>
        /// <param name="imageView"></param>
        /// <param name="sampler"></param>
        /// <param name="descriptorSet"></param>
        /// <returns></returns>
        public static VkWriteDescriptorSet CreateDescriptorFromImageSampler(uint destBinding, VkDescriptorImageInfo descImageSampler, VkDescriptorSet descriptorSet)
        {
            var descriptorImageSampler = new VkWriteDescriptorSet()
            {
                descriptorCount = 1,
                descriptorType  = VkDescriptorType.VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
                pImageInfo      = new[] { descImageSampler },
                dstBinding      = destBinding,
                dstSet          = descriptorSet,
            };

            return(descriptorImageSampler);
        }
Пример #13
0
        public FGraphicsPipeline(VkDevice device, VkPhysicalDevice physicalDevice, VkPipelineCache pipelineCache, VkRenderPass renderPass,
                                 string shaderPath, uint swapchainImageCount, FTexture textureArray)
        {
            this.device = device;
            this.swapchainImageCount = swapchainImageCount;
            this.renderPass          = renderPass;

            desclayout     = CreateDescriptorLayout(device, 1);
            descriptorPool = CreateDescriptorPool(device, swapchainImageCount, 7);
            descriptorSets = AllocateDescriptorSets(device, desclayout, descriptorPool,
                                                    swapchainImageCount);

            sampler = CreateSampler(device, textureArray.MipLevels);
            for (int i = 0; i < swapchainImageCount; i++)
            {
                VkDescriptorImageInfo imageInfo = new VkDescriptorImageInfo();
                imageInfo.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
                imageInfo.imageView   = textureArray.imageView;
                imageInfo.sampler     = sampler;

                VkWriteDescriptorSet[] writes = new VkWriteDescriptorSet[1];
                writes[0].dstSet          = descriptorSets[i];
                writes[0].dstBinding      = 0;
                writes[0].dstArrayElement = 0;
                writes[0].descriptorType  = VkDescriptorType.CombinedImageSampler;
                writes[0].descriptorCount = 1;
                writes[0].pImageInfo      = &imageInfo;

                fixed(VkWriteDescriptorSet *ptr = writes)
                vkUpdateDescriptorSets(device, (uint)writes.Length, ptr, 0, null);
            }

            uniformdata = CreateUniformBuffer(device, physicalDevice, 1);

            VkShaderModule vs = LoadShader(device, $"{shaderPath}.vert.spv");
            VkShaderModule fs = LoadShader(device, $"{shaderPath}.frag.spv");


            VkGraphicsPipelineCreateInfo pCreateInfo = VkGraphicsPipelineCreateInfo.New();

            pCreateInfo.flags = VkPipelineCreateFlags.DisableOptimization;

            VkPipelineShaderStageCreateInfo[] shaderStages = new VkPipelineShaderStageCreateInfo[2];
            shaderStages[0]        = VkPipelineShaderStageCreateInfo.New();
            shaderStages[0].stage  = VkShaderStageFlags.Vertex;
            shaderStages[0].module = vs;

            byte[] vsFuncName = Encoding.UTF8.GetBytes("main" + char.MinValue);

            fixed(byte *ptr = &(vsFuncName[0]))
            shaderStages[0].pName = ptr;

            shaderStages[1]        = VkPipelineShaderStageCreateInfo.New();
            shaderStages[1].stage  = VkShaderStageFlags.Fragment;
            shaderStages[1].module = fs;
            byte[] fsFuncName = Encoding.UTF8.GetBytes("main" + char.MinValue);

            fixed(byte *ptr = &(fsFuncName[0]))
            shaderStages[1].pName = ptr;

            fixed(VkPipelineShaderStageCreateInfo *ptr = shaderStages)
            pCreateInfo.pStages = ptr;

            pCreateInfo.stageCount = 2;

            VkVertexInputBindingDescription[] bindings = new VkVertexInputBindingDescription[4];
            bindings[0]           = new VkVertexInputBindingDescription();
            bindings[0].binding   = 0;
            bindings[0].stride    = (uint)sizeof(VoxelRenderer.VoxelVertex);
            bindings[0].inputRate = VkVertexInputRate.Vertex;

            bindings[1]           = new VkVertexInputBindingDescription();
            bindings[1].binding   = 1;
            bindings[1].stride    = (uint)sizeof(VoxelRenderer.VoxelVertex);
            bindings[1].inputRate = VkVertexInputRate.Vertex;

            bindings[2]           = new VkVertexInputBindingDescription();
            bindings[2].binding   = 2;
            bindings[2].stride    = (uint)sizeof(VoxelRenderer.VoxelVertex);
            bindings[2].inputRate = VkVertexInputRate.Vertex;

            bindings[3]           = new VkVertexInputBindingDescription();
            bindings[3].binding   = 3;
            bindings[3].stride    = (uint)sizeof(VoxelRenderer.VoxelVertex);
            bindings[3].inputRate = VkVertexInputRate.Vertex;

            VkVertexInputAttributeDescription[] attribs = new VkVertexInputAttributeDescription[4];
            attribs[0].binding  = 0;
            attribs[0].location = 0;
            attribs[0].format   = VkFormat.R32g32b32Sfloat;
            attribs[0].offset   = 0;

            attribs[1].binding  = 1;
            attribs[1].location = 1;
            attribs[1].format   = VkFormat.R32g32b32Sfloat;
            attribs[1].offset   = 0;

            attribs[2].binding  = 2;
            attribs[2].location = 2;
            attribs[2].format   = VkFormat.R32g32b32Sfloat;
            attribs[2].offset   = 0;

            attribs[3].binding  = 3;
            attribs[3].location = 3;
            attribs[3].format   = VkFormat.R32Uint;
            attribs[3].offset   = 0;

            VkPipelineVertexInputStateCreateInfo vertexInput = VkPipelineVertexInputStateCreateInfo.New();

            fixed(VkVertexInputBindingDescription *ptr = bindings)
            vertexInput.pVertexBindingDescriptions = ptr;

            vertexInput.vertexBindingDescriptionCount = (uint)bindings.Length;

            fixed(VkVertexInputAttributeDescription *ptr = attribs)
            vertexInput.pVertexAttributeDescriptions = ptr;

            vertexInput.vertexAttributeDescriptionCount = (uint)attribs.Length;
            pCreateInfo.pVertexInputState = &vertexInput;

            VkPipelineInputAssemblyStateCreateInfo inputAssembly = VkPipelineInputAssemblyStateCreateInfo.New();

            inputAssembly.topology          = VkPrimitiveTopology.TriangleList;
            pCreateInfo.pInputAssemblyState = &inputAssembly;

            VkPipelineViewportStateCreateInfo viewportState = VkPipelineViewportStateCreateInfo.New();

            viewportState.viewportCount = 1;
            viewportState.scissorCount  = 1;
            pCreateInfo.pViewportState  = &viewportState;

            VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo.New();

            rasterizationState.lineWidth    = 1;
            rasterizationState.frontFace    = VkFrontFace.Clockwise;
            rasterizationState.cullMode     = VkCullModeFlags.Back;
            rasterizationState.polygonMode  = VkPolygonMode.Fill;//TODO add line debug render
            pCreateInfo.pRasterizationState = &rasterizationState;

            VkPipelineMultisampleStateCreateInfo multisampleState = VkPipelineMultisampleStateCreateInfo.New();

            multisampleState.rasterizationSamples = VkSampleCountFlags.Count1;
            pCreateInfo.pMultisampleState         = &multisampleState;

            VkPipelineDepthStencilStateCreateInfo depthState = VkPipelineDepthStencilStateCreateInfo.New();

            depthState.depthTestEnable     = VkBool32.True;
            depthState.depthWriteEnable    = VkBool32.True;
            depthState.depthCompareOp      = VkCompareOp.Less;
            pCreateInfo.pDepthStencilState = &depthState;

            VkPipelineColorBlendAttachmentState colourAttachment = new VkPipelineColorBlendAttachmentState();

            colourAttachment.colorWriteMask = VkColorComponentFlags.R | VkColorComponentFlags.G | VkColorComponentFlags.B | VkColorComponentFlags.A;

            VkPipelineColorBlendStateCreateInfo colourState = VkPipelineColorBlendStateCreateInfo.New();

            colourState.pAttachments     = &colourAttachment;
            colourState.attachmentCount  = 1;
            pCreateInfo.pColorBlendState = &colourState;

            VkDynamicState[] dynamicStates = new VkDynamicState[2];
            dynamicStates[0] = VkDynamicState.Viewport;
            dynamicStates[1] = VkDynamicState.Scissor;

            VkPipelineDynamicStateCreateInfo dynamicState = VkPipelineDynamicStateCreateInfo.New();

            dynamicState.dynamicStateCount = (uint)dynamicStates.Length;

            fixed(VkDynamicState *ptr = &(dynamicStates[0]))
            dynamicState.pDynamicStates = ptr;

            pCreateInfo.pDynamicState = &dynamicState;

            this.pipelineLayout    = CreatePipelineLayout(device, desclayout);
            pCreateInfo.layout     = this.pipelineLayout;
            pCreateInfo.renderPass = renderPass;
            pCreateInfo.subpass    = 0;


            VkPipeline pipeline = VkPipeline.Null;

            Assert(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pCreateInfo, null, &pipeline));
            this.pipeline = pipeline;
        }
Пример #14
0
        public void AddWriteInfo(DescriptorSet destSet, VkDescriptorSetLayoutBinding binding, VkDescriptorImageInfo descriptor)
        {
            if (!descriptors.Contains(descriptor))
            {
                descriptors.Add(descriptor);
            }
            VkWriteDescriptorSet wds = VkWriteDescriptorSet.New();

            wds.descriptorType  = binding.descriptorType;
            wds.descriptorCount = binding.descriptorCount;
            wds.dstBinding      = binding.binding;
            wds.dstSet          = destSet.handle;
            wds.pImageInfo      = descriptor.Pin();

            WriteDescriptorSets.Add(wds);
        }
Пример #15
0
        /// <inheritdoc cref="Draw(GraphicsPrimitive)" />
        public void Draw(VulkanGraphicsPrimitive graphicsPrimitive)
        {
            ThrowIfNull(graphicsPrimitive, nameof(graphicsPrimitive));

            var graphicsCommandBuffer     = VulkanCommandBuffer;
            var graphicsPipeline          = graphicsPrimitive.VulkanGraphicsPipeline;
            var graphicsPipelineSignature = graphicsPipeline.VulkanSignature;
            var vulkanPipeline            = graphicsPipeline.VulkanPipeline;
            var vertexBuffer             = graphicsPrimitive.VulkanVertexBuffer;
            var vulkanVertexBuffer       = vertexBuffer.VulkanBuffer;
            var vulkanVertexBufferOffset = 0ul;

            vkCmdBindPipeline(graphicsCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vulkanPipeline);
            vkCmdBindVertexBuffers(graphicsCommandBuffer, firstBinding: 0, bindingCount: 1, (ulong *)&vulkanVertexBuffer, &vulkanVertexBufferOffset);

            var vulkanDescriptorSet = graphicsPipelineSignature.VulkanDescriptorSet;

            if (vulkanDescriptorSet != VK_NULL_HANDLE)
            {
                var inputResources       = graphicsPrimitive.InputResources;
                var inputResourcesLength = inputResources.Length;

                for (var index = 0; index < inputResourcesLength; index++)
                {
                    var inputResource = inputResources[index];

                    VkWriteDescriptorSet writeDescriptorSet;

                    if (inputResource is VulkanGraphicsBuffer vulkanGraphicsBuffer)
                    {
                        var descriptorBufferInfo = new VkDescriptorBufferInfo {
                            buffer = vulkanGraphicsBuffer.VulkanBuffer,
                            offset = 0,
                            range  = vulkanGraphicsBuffer.Size,
                        };

                        writeDescriptorSet = new VkWriteDescriptorSet {
                            sType           = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
                            dstSet          = vulkanDescriptorSet,
                            dstBinding      = unchecked ((uint)index),
                            descriptorCount = 1,
                            descriptorType  = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
                            pBufferInfo     = &descriptorBufferInfo,
                        };
                    }
                    else if (inputResource is VulkanGraphicsTexture vulkanGraphicsTexture)
                    {
                        var descriptorImageInfo = new VkDescriptorImageInfo {
                            sampler     = vulkanGraphicsTexture.VulkanSampler,
                            imageView   = vulkanGraphicsTexture.VulkanImageView,
                            imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
                        };

                        writeDescriptorSet = new VkWriteDescriptorSet {
                            sType           = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
                            dstSet          = vulkanDescriptorSet,
                            dstBinding      = unchecked ((uint)index),
                            descriptorCount = 1,
                            descriptorType  = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
                            pImageInfo      = &descriptorImageInfo,
                        };
                    }

                    vkUpdateDescriptorSets(VulkanGraphicsDevice.VulkanDevice, 1, &writeDescriptorSet, 0, pDescriptorCopies: null);
                }

                vkCmdBindDescriptorSets(graphicsCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipelineSignature.VulkanPipelineLayout, firstSet: 0, 1, (ulong *)&vulkanDescriptorSet, dynamicOffsetCount: 0, pDynamicOffsets: null);
            }

            var indexBuffer = graphicsPrimitive.VulkanIndexBuffer;

            if (indexBuffer != null)
            {
                var indexBufferStride = indexBuffer.Stride;
                var indexType         = VK_INDEX_TYPE_UINT16;

                if (indexBufferStride != 2)
                {
                    Assert(indexBufferStride == 4, "Index Buffer has an unsupported stride.");
                    indexType = VK_INDEX_TYPE_UINT32;
                }
                vkCmdBindIndexBuffer(graphicsCommandBuffer, indexBuffer.VulkanBuffer, offset: 0, indexType);

                vkCmdDrawIndexed(graphicsCommandBuffer, indexCount: (uint)(indexBuffer.Size / indexBufferStride), instanceCount: 1, firstIndex: 0, vertexOffset: 0, firstInstance: 0);
            }
            else
            {
                vkCmdDraw(graphicsCommandBuffer, vertexCount: (uint)(vertexBuffer.Size / vertexBuffer.Stride), instanceCount: 1, firstVertex: 0, firstInstance: 0);
            }
        }
Пример #16
0
        void Preprocess()
        {
            Shader shader = Resources.Load <Shader>("shaders/brdf.shader");

            {
                Pass pass = shader.GetPass("SpMap");

                uint numMipTailLevels = (uint)kEnvMapLevels - 1;

                // Compute pre-filtered specular environment map.
                var specializationInfo = new SpecializationInfo(new VkSpecializationMapEntry(0, 0, sizeof(uint)));
                specializationInfo.Write(0, numMipTailLevels);
                pass.ComputeShader.SpecializationInfo = specializationInfo;
                DescriptorSetLayout resLayout = pass.GetResourceLayout(0);

                spSet = new DescriptorSet(resLayout);

                CommandBuffer commandBuffer = Graphics.BeginPrimaryCmd();

                // Copy base mipmap level into destination environment map.
                {
                    Span <VkImageMemoryBarrier> preCopyBarriers = stackalloc[]
                    {
                        new VkImageMemoryBarrier(cubeMap.image, 0, VkAccessFlags.TransferRead, VkImageLayout.ShaderReadOnlyOptimal, VkImageLayout.TransferSrcOptimal, VkImageAspectFlags.Color, 0, 1),
                        new VkImageMemoryBarrier(envMap.image, 0, VkAccessFlags.TransferWrite, VkImageLayout.Undefined, VkImageLayout.TransferDstOptimal),
                    };

                    Span <VkImageMemoryBarrier> postCopyBarriers = stackalloc[]
                    {
                        new VkImageMemoryBarrier(cubeMap.image, VkAccessFlags.TransferRead, VkAccessFlags.ShaderRead, VkImageLayout.TransferSrcOptimal, VkImageLayout.ShaderReadOnlyOptimal, VkImageAspectFlags.Color, 0, 1),
                        new VkImageMemoryBarrier(envMap.image, VkAccessFlags.TransferWrite, VkAccessFlags.ShaderWrite, VkImageLayout.TransferDstOptimal, VkImageLayout.General),
                    };

                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.TopOfPipe, VkPipelineStageFlags.Transfer, preCopyBarriers);

                    VkImageCopy copyRegion = new VkImageCopy
                    {
                        extent = new VkExtent3D(envMap.width, envMap.height, 1)
                    };

                    copyRegion.srcSubresource.aspectMask = VkImageAspectFlags.Color;
                    copyRegion.srcSubresource.layerCount = envMap.layers;
                    copyRegion.dstSubresource            = copyRegion.srcSubresource;

                    commandBuffer.CopyImage(cubeMap.image, VkImageLayout.TransferSrcOptimal, envMap.image, VkImageLayout.TransferDstOptimal, ref copyRegion);
                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.Transfer, VkPipelineStageFlags.ComputeShader, postCopyBarriers);

                    // Pre-filter rest of the mip-chain.
                    List <ImageView> envTextureMipTailViews = new List <ImageView>();

                    var inputTexture = new VkDescriptorImageInfo(computeSampler, cubeMap.imageView, VkImageLayout.ShaderReadOnlyOptimal);
                    spSet.Bind(0, ref inputTexture);

                    Span <VkDescriptorImageInfo> envTextureMipTailDescriptors = stackalloc VkDescriptorImageInfo[(int)numMipTailLevels];
                    for (uint level = 0; level < numMipTailLevels; ++level)
                    {
                        var view = ImageView.Create(envMap.image, VkImageViewType.ImageCube, VkFormat.R16G16B16A16SFloat, VkImageAspectFlags.Color, level + 1, 1, 0, envMap.image.arrayLayers);
                        envTextureMipTailViews.Add(view);
                        envTextureMipTailDescriptors[(int)level] = new VkDescriptorImageInfo(VkSampler.Null, view, VkImageLayout.General);
                    }

                    spSet.Bind(1, envTextureMipTailDescriptors);
                    spSet.UpdateSets();

                    commandBuffer.BindComputePipeline(pass);
                    commandBuffer.BindComputeResourceSet(pass.PipelineLayout, 0, spSet);

                    float deltaRoughness = 1.0f / Math.Max((float)numMipTailLevels, 1.0f);
                    for (uint level = 1, size = kEnvMapSize / 2; level < kEnvMapLevels; ++level, size /= 2)
                    {
                        uint numGroups = Math.Max(1, size / 32);

                        var pushConstants = new SpecularFilterPushConstants {
                            level = level - 1, roughness = level * deltaRoughness
                        };
                        commandBuffer.PushConstants(pass.PipelineLayout, VkShaderStageFlags.Compute, 0, ref pushConstants);
                        commandBuffer.Dispatch(numGroups, numGroups, 6);
                    }

                    var barrier = new VkImageMemoryBarrier(envMap.image, VkAccessFlags.ShaderWrite, 0, VkImageLayout.General, VkImageLayout.ShaderReadOnlyOptimal);
                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.ComputeShader, VkPipelineStageFlags.BottomOfPipe, ref barrier);
                }

                Graphics.EndPrimaryCmd(commandBuffer);
            }

            // Compute diffuse irradiance cubemap
            {
                Pass pass = shader.GetPass("IrMap");
                DescriptorSetLayout resLayout = pass.GetResourceLayout(0);
                irSet = new DescriptorSet(resLayout, cubeMap, irMap);

                CommandBuffer commandBuffer = Graphics.BeginPrimaryCmd();
                {
                    Span <VkImageMemoryBarrier> barriers = stackalloc [] { new VkImageMemoryBarrier(irMap.image, 0, VkAccessFlags.ShaderWrite, VkImageLayout.Undefined, VkImageLayout.General) };
                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.TopOfPipe, VkPipelineStageFlags.ComputeShader, barriers);

                    commandBuffer.BindComputePipeline(pass);
                    commandBuffer.BindComputeResourceSet(pass.PipelineLayout, 0, irSet);
                    commandBuffer.Dispatch(kIrradianceMapSize / 32, kIrradianceMapSize / 32, 6);

                    Span <VkImageMemoryBarrier> postDispatchBarrier = stackalloc [] { new VkImageMemoryBarrier(irMap.image, VkAccessFlags.ShaderWrite, 0, VkImageLayout.General, VkImageLayout.ShaderReadOnlyOptimal) };
                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.ComputeShader, VkPipelineStageFlags.BottomOfPipe, postDispatchBarrier);
                }

                Graphics.EndPrimaryCmd(commandBuffer);
            }

            // Compute Cook-Torrance BRDF 2D LUT for split-sum approximation.
            {
                var pass = shader.GetPass("BrdfLUT");
                DescriptorSetLayout resLayout = pass.GetResourceLayout(0);
                brdfLUTSet = new DescriptorSet(resLayout, brdfLUT);

                CommandBuffer commandBuffer = Graphics.BeginPrimaryCmd();
                {
                    Span <VkImageMemoryBarrier> barriers = stackalloc [] { new VkImageMemoryBarrier(brdfLUT.image, 0, VkAccessFlags.ShaderWrite, VkImageLayout.Undefined, VkImageLayout.General) };
                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.TopOfPipe, VkPipelineStageFlags.ComputeShader, barriers);

                    commandBuffer.BindComputePipeline(pass);
                    commandBuffer.BindComputeResourceSet(pass.PipelineLayout, 0, brdfLUTSet);
                    commandBuffer.Dispatch(kBRDF_LUT_Size / 32, kBRDF_LUT_Size / 32, 6);

                    Span <VkImageMemoryBarrier> postDispatchBarrier = stackalloc [] { new VkImageMemoryBarrier(brdfLUT.image, VkAccessFlags.ShaderWrite, 0, VkImageLayout.General, VkImageLayout.ShaderReadOnlyOptimal) };
                    commandBuffer.PipelineBarrier(VkPipelineStageFlags.ComputeShader, VkPipelineStageFlags.BottomOfPipe, postDispatchBarrier);
                }

                Graphics.EndPrimaryCmd(commandBuffer);
            }
        }

        int selected = 0;
Пример #17
0
        public static void Update(Device device, List <WriteDescriptorSet> writes)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            int totalBuffers     = 0;
            int totalImages      = 0;
            int totalBufferViews = 0;

            for (int i = 0; i < writes.Count; i++)
            {
                var write = writes[i];

                if (write.bufferInfo != null)
                {
                    totalBuffers += write.bufferInfo.Count;
                }
                if (write.imageInfo != null)
                {
                    totalImages += write.imageInfo.Count;
                }
                if (write.texelBufferView != null)
                {
                    totalBufferViews += write.texelBufferView.Count;
                }
            }

            unsafe
            {
                var bufferInfos = stackalloc VkDescriptorBufferInfo[totalBuffers];
                var imageInfos  = stackalloc VkDescriptorImageInfo[totalImages];
                var bufferViews = stackalloc VkBufferView[totalBufferViews];

                int bufferIndex     = 0;
                int imageIndex      = 0;
                int bufferViewIndex = 0;

                var writesNative = stackalloc VkWriteDescriptorSet[writes.Count];

                for (int i = 0; i < writes.Count; i++)
                {
                    var mWrite = writes[i];

                    writesNative[i].sType           = VkStructureType.WriteDescriptorSet;
                    writesNative[i].dstSet          = mWrite.dstSet.Native;
                    writesNative[i].dstBinding      = mWrite.dstBinding;
                    writesNative[i].dstArrayElement = mWrite.dstArrayElement;
                    writesNative[i].descriptorType  = mWrite.descriptorType;

                    if (mWrite.bufferInfo != null)
                    {
                        writesNative[i].descriptorCount = (uint)mWrite.bufferInfo.Count;
                        writesNative[i].pBufferInfo     = (IntPtr)(&bufferInfos[bufferViewIndex]);

                        for (int j = 0; j < writesNative[i].descriptorCount; j++)
                        {
                            VkDescriptorBufferInfo bufferInfo = new VkDescriptorBufferInfo();
                            bufferInfo.buffer = mWrite.bufferInfo[j].buffer.Native;
                            bufferInfo.offset = mWrite.bufferInfo[j].offset;
                            bufferInfo.range  = mWrite.bufferInfo[j].range;

                            bufferInfos[bufferIndex] = bufferInfo;
                            bufferIndex++;
                        }
                    }
                    else if (mWrite.imageInfo != null)
                    {
                        writesNative[i].descriptorCount = (uint)mWrite.imageInfo.Count;
                        writesNative[i].pImageInfo      = (IntPtr)(&imageInfos[imageIndex]);

                        for (int j = 0; j < writesNative[i].descriptorCount; j++)
                        {
                            VkDescriptorImageInfo imageInfo = new VkDescriptorImageInfo();
                            imageInfo.sampler   = VkSampler.Null;
                            imageInfo.imageView = VkImageView.Null;

                            if (mWrite.imageInfo[j].sampler != null)
                            {
                                imageInfo.sampler = mWrite.imageInfo[j].sampler.Native;
                            }

                            if (mWrite.imageInfo[j].imageView != null)
                            {
                                imageInfo.imageView = mWrite.imageInfo[j].imageView.Native;
                            }

                            imageInfo.imageLayout = mWrite.imageInfo[j].imageLayout;

                            imageInfos[imageIndex] = imageInfo;
                            imageIndex++;
                        }
                    }
                    else if (mWrite.texelBufferView != null)
                    {
                        writesNative[i].descriptorCount  = (uint)mWrite.texelBufferView.Count;
                        writesNative[i].pTexelBufferView = (IntPtr)(&bufferViews[bufferViewIndex]);

                        for (int j = 0; j < writesNative[i].descriptorCount; j++)
                        {
                            bufferViews[j] = mWrite.texelBufferView[j].Native;
                            bufferViewIndex++;
                        }
                    }
                }

                device.Commands.updateDescriptorSets(device.Native, (uint)writes.Count, (IntPtr)writesNative, 0, IntPtr.Zero);
            }
        }
        private VkDescriptorSet CreateDescriptorSet(VkDescriptorPool descriptorPool)
        {
            VkDescriptorSetLayout layout = _descriptorSetLayout;

            var allocInfo = new VkDescriptorSetAllocateInfo
            {
                sType              = VkStructureType.DescriptorSetAllocateInfo,
                pNext              = null,
                descriptorPool     = descriptorPool,
                descriptorSetCount = 1,
                pSetLayouts        = &layout
            };

            VkDescriptorSet descriptorSet;

            vkAllocateDescriptorSets(Context.Device, &allocInfo, &descriptorSet);

            VkBuffer uniformBuffer = _uniformBuffer.Buffer;
            // Update the descriptor set for the shader binding point.
            VkDescriptorBufferInfo bufferInfo1 = new VkDescriptorBufferInfo
            {
                buffer = _uniformBuffer.Buffer,
                offset = 0,
                range  = ulong.MaxValue
            };

            VkDescriptorBufferInfo bufferInfo = new VkDescriptorBufferInfo
            {
                buffer = _uniformBuffer.Buffer,
                offset = 0,
                range  = ulong.MaxValue
            };

            VkDescriptorImageInfo imageInfo = new VkDescriptorImageInfo
            {
                sampler     = _sampler,
                imageView   = _cubeTexture.View,
                imageLayout = VkImageLayout.ShaderReadOnlyOptimal
            };


            VkWriteDescriptorSet *writeDescriptorSetsPtr = stackalloc VkWriteDescriptorSet[2]
            {
                new VkWriteDescriptorSet
                {
                    sType           = VkStructureType.WriteDescriptorSet,
                    pNext           = null,
                    dstBinding      = 0,
                    descriptorCount = 1,
                    descriptorType  = VkDescriptorType.UniformBuffer,
                    pBufferInfo     = &bufferInfo,
                    dstSet          = descriptorSet
                },
                new VkWriteDescriptorSet
                {
                    sType           = VkStructureType.WriteDescriptorSet,
                    pNext           = null,
                    dstBinding      = 1,
                    descriptorCount = 1,
                    descriptorType  = VkDescriptorType.CombinedImageSampler,
                    pImageInfo      = &imageInfo,
                    dstSet          = descriptorSet,
                }
            };

            vkUpdateDescriptorSets(Context.Device, 2, writeDescriptorSetsPtr, 0, null);

            return(descriptorSet);
        }