Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        void init(float nearPlane, float farPlane)
        {
            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),
                                                     new VkDescriptorSetLayoutBinding(2, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                                     new VkDescriptorSetLayoutBinding(4, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer),  //lights
                                                     new VkDescriptorSetLayoutBinding(5, VkShaderStageFlags.Fragment, VkDescriptorType.UniformBuffer)); //materials
#if WITH_SHADOWS
            descLayoutMain.Bindings.Add(new VkDescriptorSetLayoutBinding(6, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));
#endif

            if (TEXTURE_ARRAY)
            {
                descLayoutMain.Bindings.Add(new VkDescriptorSetLayoutBinding(7, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));                  //texture array
            }
            else
            {
                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)
                                                             );
            }

            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



            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;
                if (TEXTURE_ARRAY)
                {
                    cfg.Layout = new PipelineLayout(dev, descLayoutMain, descLayoutGBuff);
                }
                else
                {
                    cfg.Layout = new PipelineLayout(dev, descLayoutMain, descLayoutGBuff, descLayoutTextures);
                }

                cfg.Layout.AddPushConstants(
                    new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf <Matrix4x4> ()),
                    new VkPushConstantRange(VkShaderStageFlags.Fragment, sizeof(int), 64)
                    );
                cfg.RenderPass   = renderPass;
                cfg.SubpassIndex = SP_MODELS;
                cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
                cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
                cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
                //cfg.blendAttachments.Add (new VkPipelineColorBlendAttachmentState (false));

                cfg.AddVertex <PbrModelTexArray.Vertex> ();

                using (SpecializationInfo constants = new SpecializationInfo(
                           new SpecializationConstant <float> (0, nearPlane),
                           new SpecializationConstant <float> (1, farPlane),
                           new SpecializationConstant <float> (2, MAX_MATERIAL_COUNT))) {
                    cfg.AddShader(dev, VkShaderStageFlags.Vertex, "#shaders.GBuffPbr.vert.spv");
                    if (TEXTURE_ARRAY)
                    {
                        cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#shaders.GBuffPbrTexArray.frag.spv", constants);
                    }
                    else
                    {
                        cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#shaders.GBuffPbr.frag.spv", constants);
                    }

                    gBuffPipeline = new GraphicPipeline(cfg);
                }
                cfg.ResetShadersAndVerticesInfos();

                cfg.rasterizationState.cullMode = VkCullModeFlags.Front;
                //COMPOSE PIPELINE
                cfg.blendAttachments.Clear();
                cfg.blendAttachments.Add(new VkPipelineColorBlendAttachmentState(false));
                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))) {
                    cfg.AddShader(dev, VkShaderStageFlags.Vertex, "#vke.FullScreenQuad.vert.spv");
#if WITH_SHADOWS
                    cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#shaders.compose_with_shadows.frag.spv", constants);
#else
                    cfg.AddShader(dev, VkShaderStageFlags.Fragment, "#shaders.compose.frag.spv", constants);
#endif
                    composePipeline = new GraphicPipeline(cfg);
                }
                //DEBUG DRAW use subpass of compose
                cfg.ReplaceShader(1, new ShaderInfo(dev, VkShaderStageFlags.Fragment, "#shaders.show_gbuff.frag.spv"));
                cfg.SubpassIndex = SP_COMPOSE;
                debugPipeline    = new GraphicPipeline(cfg);
                ////TONE MAPPING
                //cfg.shaders[1] = new ShaderInfo (VkShaderStageFlags.Fragment, "#shaders.tone_mapping.frag.spv");
                //cfg.SubpassIndex = SP_TONE_MAPPING;
                //toneMappingPipeline = new GraphicPipeline (cfg);
            }

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

            envCube = new EnvironmentCube(cubemapPath, gBuffPipeline.Layout, gQueue, 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);

#if WITH_SHADOWS
            dsMainWrite = new DescriptorSetWrites(dsMain, descLayoutMain.Bindings[6]);
            dsMainWrite.Write(dev, shadowMapRenderer.shadowMap.Descriptor);
#endif
        }