From: Jean-Philippe Bruyère Date: Thu, 3 Dec 2020 15:24:49 +0000 (+0100) Subject: pipeline config with IDisposable interface to dispose the shader modules created... X-Git-Tag: v0.2.0-beta~1 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=refs%2Fheads%2Fcodeclean;p=jp%2Fvke.net.git pipeline config with IDisposable interface to dispose the shader modules created during pipeline creation. --- diff --git a/addons/EnvironmentPipeline/EnvironmentPipeline.cs b/addons/EnvironmentPipeline/EnvironmentPipeline.cs index 017da48..87fc744 100644 --- a/addons/EnvironmentPipeline/EnvironmentPipeline.cs +++ b/addons/EnvironmentPipeline/EnvironmentPipeline.cs @@ -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); diff --git a/samples/Textured/main.cs b/samples/Textured/main.cs index 17cb1ca..3964cfc 100644 --- a/samples/Textured/main.cs +++ b/samples/Textured/main.cs @@ -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); diff --git a/samples/TexturedCube/main.cs b/samples/TexturedCube/main.cs index df79043..6c848d0 100644 --- a/samples/TexturedCube/main.cs +++ b/samples/TexturedCube/main.cs @@ -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 diff --git a/samples/Triangle/main.cs b/samples/Triangle/main.cs index f962cb6..34387f4 100644 --- a/samples/Triangle/main.cs +++ b/samples/Triangle/main.cs @@ -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 (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 (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. diff --git a/samples/deferred/DeferredPbrRenderer.cs b/samples/deferred/DeferredPbrRenderer.cs index 891a890..e9b1b65 100644 --- a/samples/deferred/DeferredPbrRenderer.cs +++ b/samples/deferred/DeferredPbrRenderer.cs @@ -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 ()), - 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 (); - - using (SpecializationInfo constants = new SpecializationInfo ( - new SpecializationConstant (0, nearPlane), - new SpecializationConstant (1, farPlane), - new SpecializationConstant (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 (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 ()), + 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 (); + + using (SpecializationInfo constants = new SpecializationInfo ( + new SpecializationConstant (0, nearPlane), + new SpecializationConstant (1, farPlane), + new SpecializationConstant (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 (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); diff --git a/samples/pbr/PbrPipeline.cs b/samples/pbr/PbrPipeline.cs index 352c305..80f02ac 100644 --- a/samples/pbr/PbrPipeline.cs +++ b/samples/pbr/PbrPipeline.cs @@ -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 ()), - new VkPushConstantRange (VkShaderStageFlags.Fragment, sizeof(int), 64) - ); - cfg.RenderPass = renderPass; - cfg.AddVertexBinding (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 ()), + new VkPushConstantRange (VkShaderStageFlags.Fragment, sizeof (int), 64) + ); + cfg.RenderPass = renderPass; + cfg.AddVertexBinding (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); diff --git a/samples/pbr/main.cs b/samples/pbr/main.cs index 034736c..7a5d01a 100644 --- a/samples/pbr/main.cs +++ b/samples/pbr/main.cs @@ -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 (); diff --git a/vke/src/base/DebuDrawPipeline.cs b/vke/src/base/DebuDrawPipeline.cs index 074a960..9259213 100644 --- a/vke/src/base/DebuDrawPipeline.cs +++ b/vke/src/base/DebuDrawPipeline.cs @@ -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() * 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 () * 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 (); diff --git a/vke/src/base/GraphicPipelineConfig.cs b/vke/src/base/GraphicPipelineConfig.cs index b899946..b576e7c 100644 --- a/vke/src/base/GraphicPipelineConfig.cs +++ b/vke/src/base/GraphicPipelineConfig.cs @@ -15,7 +15,7 @@ namespace vke { /// This class has some facilities for chaining multiple pipelines creations that have small differencies /// in their configurations. /// - public class GraphicPipelineConfig { + public class GraphicPipelineConfig : IDisposable { public uint SubpassIndex; /// /// 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 { } /// /// 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. /// /// ShaderInfo index in the Shaders list of this configuration object - /// the new Shader to use. + /// the new Shader to use or null to remove the list item. public void ReplaceShader (int shaderIndex, ShaderInfo info) { Shaders[shaderIndex].Dispose (); - Shaders[shaderIndex] = info; + if (info == null) + Shaders.RemoveAt (shaderIndex); + else + Shaders[shaderIndex] = info; } /// /// 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 (); - } - /// - /// Resets shaders in current config to ease reause of current 'GraphicPipelineConfig - /// for creating another similar pipeline with different shaders. - /// - 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; + } + } + /// + /// PipelineConfig may create shader modules that have to be disposed after the pipeline creation. + /// + 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 + } }