void init(float nearPlane, float farPlane, string cubemapPath)
        {
            init_renderpass();

            descLayoutMain = new DescriptorSetLayout(dev,
                                                     new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer), //matrices and params
                                                     new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),                      //irradiance
                                                     new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),                      //prefil
                                                     new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),                      //lut brdf
                                                     new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),                             //lights
                                                     new VkDescriptorSetLayoutBinding(5, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),                             //materials
                                                     new VkDescriptorSetLayoutBinding(6, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),                      //shadow map
                                                     new VkDescriptorSetLayoutBinding(7, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),                      //texture array
                                                     new VkDescriptorSetLayoutBinding(8, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));                     //uiImage

            descLayoutGBuff = new DescriptorSetLayout(dev,
                                                      new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),  //color + roughness
                                                      new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),  //emit + metal
                                                      new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),  //normals + AO
                                                      new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment),  //Pos + depth
                                                      new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.InputAttachment)); //hdr

            descLayoutMain.SetName("main");
            descLayoutGBuff.SetName("GBuff");

            using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, NUM_SAMPLES)) {
                cfg.rasterizationState.cullMode = VkCullModeFlags.Back;
                if (NUM_SAMPLES != VkSampleCountFlags.SampleCount1)
                {
                    cfg.multisampleState.sampleShadingEnable = true;
                    cfg.multisampleState.minSampleShading    = 0.5f;
                }
                cfg.Cache  = pipelineCache;
                cfg.Layout = new PipelineLayout(dev,
                                                new VkPushConstantRange[] {
                    new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                    new VkPushConstantRange(VkShaderStageFlags.Fragment, sizeof(int), 64)
                },
                                                descLayoutMain, descLayoutGBuff);

                cfg.RenderPass = renderPass;
                //cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));

                cfg.AddVertexBinding <PbrModelTexArray.Vertex> (0);
                cfg.AddVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat);

                cfg.AddVertexBinding <VkChess.InstanceData> (1, VkVertexInputRate.Instance);
                cfg.AddVertexAttributes(1, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat, VkFormat.R32g32b32a32Sfloat);

                using (SpecializationInfo constants = new SpecializationInfo(
                           new SpecializationConstant <float> (0, nearPlane),
                           new SpecializationConstant <float> (1, farPlane),
                           new SpecializationConstant <int> (2, MAX_MATERIAL_COUNT))) {
                    cfg.AddShader(dev, VkShaderStageFlags.Vertex, "#vkChess.net.GBuffPbrInstanced.vert.spv");

                    cfg.SubpassIndex     = SP_DEPTH_PREPASS;
                    depthPrepassPipeline = new GraphicPipeline(cfg, "Depth prepass");

                    cfg.depthStencilState.depthCompareOp = VkCompareOp.Equal;
                    cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
                    cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
                    cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));

                    cfg.SubpassIndex = SP_MODELS;

                    cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#vkChess.net.GBuffPbrTexArray.frag.spv", constants);

                    gBuffPipeline = new GraphicPipeline(cfg, "GBuff");
                }
                cfg.rasterizationState.cullMode = VkCullModeFlags.Front;
                //COMPOSE PIPELINE
                cfg.blendAttachments.Clear();
                cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));

                cfg.ResetShadersAndVerticesInfos();

                cfg.SubpassIndex = SP_COMPOSE;
                cfg.Layout       = gBuffPipeline.Layout;
                cfg.depthStencilState.depthTestEnable  = false;
                cfg.depthStencilState.depthWriteEnable = false;
                using (SpecializationInfo constants = new SpecializationInfo(
                           new SpecializationConstant <uint> (0, (uint)lights.Length),
                           new SpecializationConstant <float> (1, 0.25f))) {
                    cfg.AddShader(dev, VkShaderStageFlags.Vertex, "#vke.FullScreenQuad.vert.spv");
                    cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#vkChess.net.compose_with_shadows.frag.spv", constants);
                    composePipeline = new GraphicPipeline(cfg, "compose");
                }
                //DEBUG DRAW use subpass of compose
                cfg.ReplaceShader(1, new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#vkChess.net.show_gbuff.frag.spv"));
                cfg.SubpassIndex = SP_COMPOSE;
                debugPipeline    = new GraphicPipeline(cfg, "debug");
                //TONE MAPPING
                cfg.ReplaceShader(1, new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#vkChess.net.tone_mapping.frag.spv"));
                cfg.SubpassIndex    = SP_TONE_MAPPING;
                toneMappingPipeline = new GraphicPipeline(cfg, "tonne mapping");
            }

            dsMain  = descriptorPool.Allocate(descLayoutMain);
            dsGBuff = descriptorPool.Allocate(descLayoutGBuff);

            envCube = new vke.Environment.EnvironmentCube(cubemapPath, gBuffPipeline.Layout, presentQueue, renderPass);

            matrices.prefilteredCubeMipLevels = envCube.prefilterCube.CreateInfo.mipLevels;

            DescriptorSetWrites dsMainWrite = new DescriptorSetWrites(dsMain, descLayoutMain.Bindings.GetRange(0, 5).ToArray());

            dsMainWrite.Write(dev,
                              uboMatrices.Descriptor,
                              envCube.irradianceCube.Descriptor,
                              envCube.prefilterCube.Descriptor,
                              envCube.lutBrdf.Descriptor,
                              uboLights.Descriptor);

            dsMainWrite = new DescriptorSetWrites(dsMain, descLayoutMain.Bindings[6]);
            dsMainWrite.Write(dev, shadowMapRenderer.shadowMap.Descriptor);
        }
示例#2
0
        void init(VkSampleCountFlags samples = VkSampleCountFlags.SampleCount4)
        {
            descriptorPool = new DescriptorPool(dev, 2,
                                                new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer),
                                                new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler)
                                                );

            descLayoutMatrix = new DescriptorSetLayout(dev,
                                                       new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Vertex | VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),
                                                       new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                       );

            descLayoutTextures = new DescriptorSetLayout(dev,
                                                         new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                         new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                                                         );

            dsMats = descriptorPool.Allocate(descLayoutMatrix);

            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault(VkPrimitiveTopology.TriangleList, samples);

            cfg.Layout = new PipelineLayout(dev, descLayoutMatrix, descLayoutTextures);
            cfg.Layout.AddPushConstants(
                new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                new VkPushConstantRange(VkShaderStageFlags.Fragment, (uint)Marshal.SizeOf <Model.PbrMaterial> (), 64)
                );
            cfg.RenderPass = new RenderPass(dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat(), samples);

            cfg.AddVertexBinding <Model.Vertex> (0);
            cfg.SetVertexAttributes(0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);

            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/pbrtest.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/pbrtest.frag.spv");

            pipeline = new GraphicPipeline(cfg);

            cfg.ResetShadersAndVerticesInfos();
            cfg.AddShader(VkShaderStageFlags.Vertex, "shaders/FullScreenQuad.vert.spv");
            cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/simpletexture.frag.spv");

            cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState(true);

            uiPipeline = new GraphicPipeline(cfg);

            uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, (ulong)Marshal.SizeOf <Matrices>());
            uboMats.Map();             //permanent map

            DescriptorSetWrites uboUpdate = new DescriptorSetWrites(dsMats, descLayoutMatrix.Bindings[0]);

            uboUpdate.Write(dev, uboMats.Descriptor);

            cfg.Layout.SetName("Main Pipeline layout");
            uboMats.SetName("uboMats");
            descriptorPool.SetName("main pool");
            descLayoutTextures.SetName("descLayoutTextures");

            statPool = new PipelineStatisticsQueryPool(dev,
                                                       VkQueryPipelineStatisticFlags.InputAssemblyVertices |
                                                       VkQueryPipelineStatisticFlags.InputAssemblyPrimitives |
                                                       VkQueryPipelineStatisticFlags.ClippingInvocations |
                                                       VkQueryPipelineStatisticFlags.ClippingPrimitives |
                                                       VkQueryPipelineStatisticFlags.FragmentShaderInvocations);

            timestampQPool = new TimestampQueryPool(dev);
        }