//in the main vulkan thread void updateTextureSet() { nextTexture.CreateView(); nextTexture.CreateSampler(); nextTexture.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal; using (DescriptorSetWrites2 uboUpdate = new DescriptorSetWrites2(dev)) { uboUpdate.AddWriteInfo(descriptorSet, dsLayout.Bindings[1], nextTexture.Descriptor); uboUpdate.Update(); } texture?.Dispose(); texture = nextTexture; nextTexture = null; }
Program() : base() { vbo = new GPUBuffer <float> (presentQueue, cmdPool, VkBufferUsageFlags.VertexBuffer, vertices); ibo = new GPUBuffer <ushort> (presentQueue, cmdPool, VkBufferUsageFlags.IndexBuffer, indices); descriptorPool = new DescriptorPool(dev, 1, new VkDescriptorPoolSize(VkDescriptorType.UniformBuffer), new VkDescriptorPoolSize(VkDescriptorType.CombinedImageSampler) ); 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.SampleCount1); 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.AddShader(VkShaderStageFlags.Vertex, "shaders/main.vert.spv"); cfg.AddShader(VkShaderStageFlags.Fragment, "shaders/main.frag.spv"); pipeline = new GraphicPipeline(cfg); uboMats = new HostBuffer(dev, VkBufferUsageFlags.UniformBuffer, matrices); uboMats.Map(); //permanent map descriptorSet = descriptorPool.Allocate(dsLayout); using (DescriptorSetWrites2 uboUpdate = new DescriptorSetWrites2(dev)) { uboUpdate.AddWriteInfo(descriptorSet, dsLayout.Bindings[0], uboMats.Descriptor); uboUpdate.Update(); } loadTexture(imgPathes[currentImgIndex]); updateTextureSet(); }
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(); } } }