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); }
void loadMaterials(glTFLoader ctx, DescriptorSetLayout layout, params AttachmentType[] attachments) { glTFLoader.Material[] mats = ctx.LoadMaterial(); materials = new Material[mats.Length]; descriptorSets = new DescriptorSet[mats.Length]; if (attachments.Length == 0) { throw new InvalidOperationException("At least one attachment is required for Model.WriteMaterialDescriptor"); } descriptorPool = new DescriptorPool(dev, (uint)materials.Length, new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler, (uint)(attachments.Length * materials.Length)) ); descriptorPool.SetName("descPool gltfTextures"); for (int i = 0; i < mats.Length; i++) { materials[i] = new Material { workflow = (float)mats[i].workflow, baseColorFactor = mats[i].baseColorFactor, emissiveFactor = mats[i].emissiveFactor, metallicFactor = mats[i].metallicFactor, roughnessFactor = mats[i].roughnessFactor, TexCoord0 = mats[i].availableAttachments, TexCoord1 = mats[i].availableAttachments1, alphaMask = 0f, alphaMaskCutoff = 0.0f, diffuseFactor = new Vector4(0), specularFactor = new Vector4(0) }; descriptorSets[i] = descriptorPool.Allocate(layout); descriptorSets[i].Handle.SetDebugMarkerName(dev, "descSet " + mats[i].Name); VkDescriptorSetLayoutBinding dslb = new VkDescriptorSetLayoutBinding(0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler); using (DescriptorSetWrites2 uboUpdate = new DescriptorSetWrites2(dev)) { for (uint a = 0; a < attachments.Length; a++) { dslb.binding = a; switch (attachments[a]) { case AttachmentType.None: break; case AttachmentType.Color: if (mats[i].availableAttachments.HasFlag(AttachmentType.Color)) { uboUpdate.AddWriteInfo(descriptorSets[i], dslb, textures[(int)mats[i].baseColorTexture].Descriptor); } break; case AttachmentType.Normal: if (mats[i].availableAttachments.HasFlag(AttachmentType.Normal)) { uboUpdate.AddWriteInfo(descriptorSets[i], dslb, textures[(int)mats[i].normalTexture].Descriptor); } break; case AttachmentType.AmbientOcclusion: if (mats[i].availableAttachments.HasFlag(AttachmentType.AmbientOcclusion)) { uboUpdate.AddWriteInfo(descriptorSets[i], dslb, textures[(int)mats[i].occlusionTexture].Descriptor); } break; case AttachmentType.PhysicalProps: if (mats[i].availableAttachments.HasFlag(AttachmentType.PhysicalProps)) { uboUpdate.AddWriteInfo(descriptorSets[i], dslb, textures[(int)mats[i].metallicRoughnessTexture].Descriptor); } break; case AttachmentType.Metal: break; case AttachmentType.Roughness: break; case AttachmentType.Emissive: if (mats[i].availableAttachments.HasFlag(AttachmentType.Emissive)) { uboUpdate.AddWriteInfo(descriptorSets[i], dslb, textures[(int)mats[i].emissiveTexture].Descriptor); } break; } } uboUpdate.Update(); } } }