]> O.S.I.I.S - jp/vke.net.git/commitdiff
pipeline config with IDisposable interface to dispose the shader modules created... codeclean 6/head
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 3 Dec 2020 15:24:49 +0000 (16:24 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Thu, 3 Dec 2020 15:24:49 +0000 (16:24 +0100)
addons/EnvironmentPipeline/EnvironmentPipeline.cs
samples/Textured/main.cs
samples/TexturedCube/main.cs
samples/Triangle/main.cs
samples/deferred/DeferredPbrRenderer.cs
samples/pbr/PbrPipeline.cs
samples/pbr/main.cs
vke/src/base/DebuDrawPipeline.cs
vke/src/base/GraphicPipelineConfig.cs

index 017da48135b26fd0b799c9ea2d51969fb2f9272e..87fc744cb0117b71bdad539a01a6e60531c9bbb9 100644 (file)
@@ -28,23 +28,22 @@ namespace vke.Environment {
                                cubemap.SetName ("skybox Texture");
                                cubemap.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;
 
-                               GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, renderPass.Samples, false);
-                               cfg.RenderPass = renderPass;
-                               cfg.Layout = plLayout;
-                               cfg.AddVertexBinding (0, 3 * sizeof (float));
-                               cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat);
-                               cfg.AddShaders (
-                                       new ShaderInfo (Dev, VkShaderStageFlags.Vertex, "#EnvironmentPipeline.skybox.vert.spv"),
-                                       new ShaderInfo (Dev, VkShaderStageFlags.Fragment, STR_FRAG_PATH)
-                               );
+                               using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, renderPass.Samples, false)) {
+                                       cfg.RenderPass = renderPass;
+                                       cfg.Layout = plLayout;
+                                       cfg.AddVertexBinding (0, 3 * sizeof (float));
+                                       cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat);
+                                       cfg.AddShaders (
+                                               new ShaderInfo (Dev, VkShaderStageFlags.Vertex, "#EnvironmentPipeline.skybox.vert.spv"),
+                                               new ShaderInfo (Dev, VkShaderStageFlags.Fragment, STR_FRAG_PATH)
+                                       );
 
-                               cfg.multisampleState.rasterizationSamples = Samples;
+                                       cfg.multisampleState.rasterizationSamples = Samples;
 
-                               layout = cfg.Layout;
+                                       layout = cfg.Layout;
 
-                               init (cfg);
-
-                               cfg.DisposeShaders ();
+                                       init (cfg);
+                               }
 
                                generateBRDFLUT (staggingQ, cmdPool);
                                generateCubemaps (staggingQ, cmdPool);
@@ -129,7 +128,7 @@ namespace vke.Environment {
                                new ShaderInfo (Dev, VkShaderStageFlags.Fragment, "#EnvironmentPipeline.genbrdflut.frag.spv"));
 
                        using (GraphicPipeline pl = new GraphicPipeline (cfg)) {
-                               cfg.DisposeShaders ();
+                               cfg.Dispose ();
                                using (FrameBuffer fb = new FrameBuffer (cfg.RenderPass, dim, dim, lutBrdf)) {
                                        PrimaryCommandBuffer cmd = cmdPool.AllocateCommandBuffer ();
                                        cmd.Start (VkCommandBufferUsageFlags.OneTimeSubmit);
@@ -223,7 +222,7 @@ namespace vke.Environment {
                        VkImageSubresourceRange subRes = new VkImageSubresourceRange (VkImageAspectFlags.Color, 0, numMips, 0, 6);
 
                        using (GraphicPipeline pl = new GraphicPipeline (cfg)) {
-                               cfg.DisposeShaders ();
+                               cfg.Dispose ();
                                DescriptorSet dset = dsPool.Allocate (dsLayout);
                                DescriptorSetWrites dsUpdate = new DescriptorSetWrites (dsLayout);
                                dsUpdate.Write (Dev, dset, cubemap.Descriptor);
index 17cb1ca1b4a7b8253f65f936c647a97b848e4df2..3964cfce567b81cbe169a33d7c9e77186c9facff 100644 (file)
@@ -84,21 +84,20 @@ namespace Textured {
                        dsLayout = new DescriptorSetLayout (dev, 0,
                                new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer),
                                new VkDescriptorSetLayoutBinding (1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));
-                               
-                       GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount4);
 
-                       cfg.Layout = new PipelineLayout (dev, dsLayout);
-                       cfg.RenderPass = new RenderPass (dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat (), cfg.Samples);
+                       using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount4)) {
 
-                       cfg.AddVertexBinding (0, 5 * sizeof(float));
-                       cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);
+                               cfg.Layout = new PipelineLayout (dev, dsLayout);
+                               cfg.RenderPass = new RenderPass (dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat (), cfg.Samples);
 
-                       cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv");
-                       cfg.AddShader (dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv");
+                               cfg.AddVertexBinding (0, 5 * sizeof (float));
+                               cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);
 
-                       pipeline = new GraphicPipeline (cfg);
+                               cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv");
+                               cfg.AddShader (dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv");
 
-                       cfg.DisposeShaders ();
+                               pipeline = new GraphicPipeline (cfg);
+                       }
 
 
                        uboMats = new HostBuffer (dev, VkBufferUsageFlags.UniformBuffer, matrices);
index df790431aead39ba73503c75984d28264a4fb1fc..6c848d0955e2b0675636af207ee28abe55ec33cc 100644 (file)
@@ -114,20 +114,19 @@ namespace TextureCube {
                                new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer),
                                new VkDescriptorSetLayoutBinding (1, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler));
 
-                       GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, samples);
+                       using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, samples)) {
 
-                       cfg.Layout = new PipelineLayout (dev, dsLayout);
-                       cfg.RenderPass = new RenderPass (dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat (), cfg.Samples);
+                               cfg.Layout = new PipelineLayout (dev, dsLayout);
+                               cfg.RenderPass = new RenderPass (dev, swapChain.ColorFormat, dev.GetSuitableDepthFormat (), cfg.Samples);
 
-                       cfg.AddVertexBinding (0, 5 * sizeof (float));
-                       cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);
+                               cfg.AddVertexBinding (0, 5 * sizeof (float));
+                               cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat);
 
-                       cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#shaders.skybox.vert.spv");
-                       cfg.AddShader (dev, VkShaderStageFlags.Fragment, "#shaders.skybox.frag.spv");
+                               cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#shaders.skybox.vert.spv");
+                               cfg.AddShader (dev, VkShaderStageFlags.Fragment, "#shaders.skybox.frag.spv");
 
-                       pipeline = new GraphicPipeline (cfg);
-
-                       cfg.DisposeShaders ();
+                               pipeline = new GraphicPipeline (cfg);
+                       }
 
                        uboMats = new HostBuffer (dev, VkBufferUsageFlags.UniformBuffer, matrices);
                        uboMats.Map ();//permanent map
index f962cb638c49438695561d851906cf0ae7de87a0..34387f4845eb6ddf05c6547e94e3fd1a023951cf 100644 (file)
@@ -65,32 +65,28 @@ namespace Triangle {
                        descriptorPool = new DescriptorPool (dev, 1, new VkDescriptorPoolSize (VkDescriptorType.UniformBuffer));
 
                        //Graphic pipeline configuration are predefined by the GraphicPipelineConfig class, which ease sharing config for several pipelines having lots in common.
-                       GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1, false);
-                       //Create the pipeline layout, it will be automatically activated on pipeline creation, so that sharing layout among different pipelines will benefit
-                       //from the reference counting to automatically dispose unused layout on pipeline clean up. It's the same for DescriptorSetLayout.
-                       cfg.Layout = new PipelineLayout (dev,
-                               new DescriptorSetLayout (dev, new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer)));
-                       //create a default renderpass with just a color attachment for the swapchain image, a default subpass is automatically created and the renderpass activation
-                       //will follow the pipeline life cicle and will be automatically disposed when no longuer used.
-                       cfg.RenderPass = new RenderPass (dev, swapChain.ColorFormat, cfg.Samples);
-                       //configuration of vertex bindings and attributes
-                       cfg.AddVertexBinding<Vertex> (0);
-                       cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat);//position + color
-
-                       //shader are automatically compiled by SpirVTasks if added to the project. The resulting shaders are automatically embedded in the assembly.
-                       //To specifiy that the shader path is a resource name, put the '#' prefix. Else the path will be search on disk.
-                       cfg.AddShaders (
-                               new ShaderInfo (dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv"),
-                               new ShaderInfo (dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv")
-                       );
-
-                       //create and activate the pipeline with the configuration we've just done.
-                       pipeline = new GraphicPipeline (cfg);
-
-                       //ShaderInfo used in this configuration with create the VkShaderModule's used
-                       //for creating the pipeline. They have to be disposed to destroy those modules
-                       //used only during pipeline creation.
-                       cfg.DisposeShaders ();
+                       using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, VkSampleCountFlags.SampleCount1, false)) {
+                               //Create the pipeline layout, it will be automatically activated on pipeline creation, so that sharing layout among different pipelines will benefit
+                               //from the reference counting to automatically dispose unused layout on pipeline clean up. It's the same for DescriptorSetLayout.
+                               cfg.Layout = new PipelineLayout (dev,
+                                       new DescriptorSetLayout (dev, new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Vertex, VkDescriptorType.UniformBuffer)));
+                               //create a default renderpass with just a color attachment for the swapchain image, a default subpass is automatically created and the renderpass activation
+                               //will follow the pipeline life cicle and will be automatically disposed when no longuer used.
+                               cfg.RenderPass = new RenderPass (dev, swapChain.ColorFormat, cfg.Samples);
+                               //configuration of vertex bindings and attributes
+                               cfg.AddVertexBinding<Vertex> (0);
+                               cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat);//position + color
+
+                               //shader are automatically compiled by SpirVTasks if added to the project. The resulting shaders are automatically embedded in the assembly.
+                               //To specifiy that the shader path is a resource name, put the '#' prefix. Else the path will be search on disk.
+                               cfg.AddShaders (
+                                       new ShaderInfo (dev, VkShaderStageFlags.Vertex, "#shaders.main.vert.spv"),
+                                       new ShaderInfo (dev, VkShaderStageFlags.Fragment, "#shaders.main.frag.spv")
+                               );
+
+                               //create and activate the pipeline with the configuration we've just done.
+                               pipeline = new GraphicPipeline (cfg);
+                       }
 
                        //because descriptor layout used for a pipeline are only activated on pipeline activation, descriptor set must not be allocated before, except if the layout has been manually activated, 
                        //but in this case, layout will need also to be explicitly disposed.
index 891a890a9dd0b8a93e787d6ddc0525794cbb8ee7..e9b1b65c6744bf359c5f30c2d4f2c37d02120175 100644 (file)
@@ -234,74 +234,73 @@ namespace deferred {
 
 
 
-                       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);
+                       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.AddShader (dev, VkShaderStageFlags.Fragment, "#shaders.GBuffPbr.frag.spv", constants);
-
-                               gBuffPipeline = new GraphicPipeline (cfg);
-                       }
-                       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))) {
-                               cfg.AddShader (dev, VkShaderStageFlags.Vertex, "#vke.FullScreenQuad.vert.spv");
+                                       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);
+                                       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);
+                                       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);
                        }
-                       //DEBUG DRAW use subpass of compose
-                       cfg.Shaders[1].Dispose ();
-                       cfg.Shaders[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);
-
-                       cfg.DisposeShaders ();
 
                        dsMain = descriptorPool.Allocate (descLayoutMain);
                        dsGBuff = descriptorPool.Allocate (descLayoutGBuff);
index 352c3058a4dec03625138d9048a144c27b6fe372..80f02aca09c7ff2369482450d447bf64d3402067 100644 (file)
@@ -94,25 +94,24 @@ namespace pbrSample
                                new VkDescriptorSetLayoutBinding (3, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler),
                                new VkDescriptorSetLayoutBinding (4, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler)
                        );
-                               
-                       GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, renderPass.Samples);
-                       cfg.Layout = new PipelineLayout (Dev, descLayoutMain, descLayoutTextures);
-                       cfg.Layout.AddPushConstants (
-                               new VkPushConstantRange (VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf<Matrix4x4> ()),
-                               new VkPushConstantRange (VkShaderStageFlags.Fragment, sizeof(int), 64)
-                       );
-                       cfg.RenderPass = renderPass;
-                       cfg.AddVertexBinding<PbrModel.Vertex> (0);
-                       cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat);
 
-                       cfg.AddShader (Dev, VkShaderStageFlags.Vertex, "#shaders.pbr.vert.spv");
-                       cfg.AddShader (Dev, VkShaderStageFlags.Fragment, "#shaders.pbr_khr.frag.spv");
+                       using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.TriangleList, renderPass.Samples)) {
+                               cfg.Layout = new PipelineLayout (Dev, descLayoutMain, descLayoutTextures);
+                               cfg.Layout.AddPushConstants (
+                                       new VkPushConstantRange (VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf<Matrix4x4> ()),
+                                       new VkPushConstantRange (VkShaderStageFlags.Fragment, sizeof (int), 64)
+                               );
+                               cfg.RenderPass = renderPass;
+                               cfg.AddVertexBinding<PbrModel.Vertex> (0);
+                               cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat, VkFormat.R32g32Sfloat, VkFormat.R32g32Sfloat);
 
-                       layout = cfg.Layout;
+                               cfg.AddShader (Dev, VkShaderStageFlags.Vertex, "#shaders.pbr.vert.spv");
+                               cfg.AddShader (Dev, VkShaderStageFlags.Fragment, "#shaders.pbr_khr.frag.spv");
 
-                       init (cfg);
+                               layout = cfg.Layout;
 
-                       cfg.DisposeShaders ();
+                               init (cfg);
+                       }
 
                        dsMain = descriptorPool.Allocate (descLayoutMain);
 
index 034736c6b4f86c407473ac246676619b399c1784..7a5d01a79c503d9a12ece7fab1a10b4392a80bda 100644 (file)
@@ -46,7 +46,7 @@ namespace pbrSample {
                bool queryUpdatePrefilCube, showDebugImg;
 
                Vector4 lightPos = new Vector4 (1, 0, 0, 0);
-               uint curModelIndex = 0;
+               uint curModelIndex = 13;
 
                protected override void initVulkan () {
                        base.initVulkan ();
index 074a96080ec140f554f55a89145d9cdbf85f53e0..92592132e5af5f0fd065b3da0a7a4f2d1830bc1a 100644 (file)
@@ -18,24 +18,23 @@ namespace vke {
 
             vboLength = maxVertices * 6 * sizeof(float);
 
-            GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.LineList, samples, false);
-                       cfg.rasterizationState.lineWidth = 1.0f;
-                       cfg.RenderPass = RenderPass;
-                       cfg.Layout = new PipelineLayout(Dev, new VkPushConstantRange(VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf<Matrix4x4>() * 2));
-                       cfg.AddVertexBinding (0, 6 * sizeof(float));
-                       cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat);
-                       cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState (true);
-
-                       cfg.AddShaders (
-                               new ShaderInfo (Dev, VkShaderStageFlags.Vertex, "#vke.debug.vert.spv"),
-                               new ShaderInfo (Dev, VkShaderStageFlags.Fragment, "#vke.debug.frag.spv")
-                       );
-
-                       layout = cfg.Layout;
-
-                       init (cfg);
-
-                       cfg.DisposeShaders ();
+                       using (GraphicPipelineConfig cfg = GraphicPipelineConfig.CreateDefault (VkPrimitiveTopology.LineList, samples, false)) {
+                               cfg.rasterizationState.lineWidth = 1.0f;
+                               cfg.RenderPass = RenderPass;
+                               cfg.Layout = new PipelineLayout (Dev, new VkPushConstantRange (VkShaderStageFlags.Vertex, (uint)Marshal.SizeOf<Matrix4x4> () * 2));
+                               cfg.AddVertexBinding (0, 6 * sizeof (float));
+                               cfg.AddVertexAttributes (0, VkFormat.R32g32b32Sfloat, VkFormat.R32g32b32Sfloat);
+                               cfg.blendAttachments[0] = new VkPipelineColorBlendAttachmentState (true);
+
+                               cfg.AddShaders (
+                                       new ShaderInfo (Dev, VkShaderStageFlags.Vertex, "#vke.debug.vert.spv"),
+                                       new ShaderInfo (Dev, VkShaderStageFlags.Fragment, "#vke.debug.frag.spv")
+                               );
+
+                               layout = cfg.Layout;
+
+                               init (cfg);
+                       }
 
                        Vertices = new HostBuffer (Dev, VkBufferUsageFlags.VertexBuffer, vboLength);
                        Vertices.Map ();
index b899946477bd5658e90a09fa877efe14af951bce..b576e7c13eb0d6c53f29f6cfd1e6ec584dfdbdd5 100644 (file)
@@ -15,7 +15,7 @@ namespace vke {
        /// This class has some facilities for chaining multiple pipelines creations that have small differencies
        /// in their configurations.
        /// </summary>
-       public class GraphicPipelineConfig {
+       public class GraphicPipelineConfig : IDisposable {
                public uint SubpassIndex;
                /// <summary>
                /// Pipeline layout. Note that layout will not be activated (handle creation) until
@@ -114,7 +114,9 @@ namespace vke {
                }
 
                uint currentAttributeIndex = 0;
-               public void AddVertexAttributes (uint binding, params VkFormat[] attribsDesc) {
+        private bool disposedValue;
+
+        public void AddVertexAttributes (uint binding, params VkFormat[] attribsDesc) {
                        uint currentAttributeoffset = 0;
                        for (uint i = 0; i < attribsDesc.Length; i++) {
                                vertexAttributes.Add (new VkVertexInputAttributeDescription (binding, i + currentAttributeIndex, attribsDesc[i], currentAttributeoffset));
@@ -161,13 +163,16 @@ namespace vke {
                }
                /// <summary>
                /// Dispose ShaderInfo at index 'shaderIndex' in the Shaders list, and replace it
-               /// with the new 'ShaderInfo' given in arguments.
+               /// with the new 'ShaderInfo' given in arguments. If new `ShaderInfo` is null, the shader list element at the given index will be removed.
                /// </summary>
                /// <param name="shaderIndex">ShaderInfo index in the Shaders list of this configuration object</param>
-               /// <param name="info">the new Shader to use.</param>
+               /// <param name="info">the new Shader to use or null to remove the list item.</param>
                public void ReplaceShader (int shaderIndex, ShaderInfo info) {
                        Shaders[shaderIndex].Dispose ();
-                       Shaders[shaderIndex] = info;
+                       if (info == null)
+                               Shaders.RemoveAt (shaderIndex);
+                       else
+                               Shaders[shaderIndex] = info;
                }
                /// <summary>
                /// Dispose ShaderInfo at index 'shaderIndex' in the Shaders list, and replace it with a new one.
@@ -192,16 +197,31 @@ namespace vke {
                        currentAttributeIndex = 0;
                        vertexBindings.Clear ();
                        vertexAttributes.Clear ();
-                       DisposeShaders ();
-               }
-               /// <summary>
-               /// Resets shaders in current config to ease reause of current 'GraphicPipelineConfig
-               /// for creating another similar pipeline with different shaders.
-               /// </summary>
-               public void DisposeShaders () {
                        foreach (ShaderInfo shader in Shaders)
                                shader.Dispose ();
                        Shaders.Clear ();
                }
+
+        #region IDispable implementation
+        protected virtual void Dispose (bool disposing) {
+            if (!disposedValue) {
+                if (disposing) {
+                                       foreach (ShaderInfo shader in Shaders)
+                                               shader.Dispose ();
+                                       Shaders.Clear ();
+                               }
+                               disposedValue = true;
+            }
+        }
+               /// <summary>
+               /// PipelineConfig may create shader modules that have to be disposed after the pipeline creation.
+               /// </summary>
+        public void Dispose () {
+            // Ne changez pas ce code. Placez le code de nettoyage dans la méthode 'Dispose(bool disposing)'
+            Dispose (disposing: true);
+            GC.SuppressFinalize (this);
+        }
+               #endregion
+
        }
 }