protected virtual void CreateResources() { query_count_ = (uint)QUERY_HSIZE * 2; for (int i = 0; i < 3; i++) { query_pool[i] = new VkQueryPool(VkQueryType.Timestamp, query_count_); } uint[] queue_families = null; VkSharingMode sharingMode = VkSharingMode.Exclusive; if (Device.QFGraphics != Device.QFCompute) { sharingMode = VkSharingMode.Concurrent; queue_families = new[] { Device.QFGraphics, Device.QFCompute }; } uboCluster = new SharedBuffer(VkBufferUsageFlags.UniformBuffer, VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent, (uint)Utilities.SizeOf <ClusterUBO>(), sharingMode, queue_families); uint max_grid_count = ((MAX_WIDTH - 1) / TILE_WIDTH + 1) * ((MAX_HEIGHT - 1) / TILE_HEIGHT + 1) * TILE_COUNT_Z; gridFlags = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, max_grid_count, VkFormat.R8UInt, sharingMode, queue_families); clusterLayout1 = new DescriptorSetLayout { new DescriptorSetLayoutBinding(0, VkDescriptorType.UniformBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(1, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), }; clusterSet1 = new DescriptorSet(clusterLayout1, uboCluster, gridFlags); }
public ImGUI() { IntPtr context = ImGui.CreateContext(); ImGui.SetCurrentContext(context); //ImGui.GetIO().Fonts.AddFontDefault(); File file = FileSystem.Instance.GetFile("fonts/arial.ttf"); var bytes = file.ReadAllBytes(); ImGui.GetIO().Fonts.AddFontFromMemoryTTF(Utilities.AsPointer(ref bytes[0]), 32, 15); CreateGraphicsResources(); RecreateFontDeviceTexture(); resourceSet = new DescriptorSet(resourceLayout, uniformBufferVS); resourceSetTex = new DescriptorSet(resourceLayoutTex, texture); ImGuiStylePtr style = ImGui.GetStyle(); style.WindowRounding = 2; SetOpenTKKeyMappings(); SetPerFrameImGuiData(1f / 60f); ImGui.NewFrame(); this.Subscribe <BeginFrame>((e) => Update()); this.Subscribe <DrawEvent>(RenderImDrawData); }
public Terrain() { batch = new TerrainBatch(); batches = new[] { batch }; material = new Material("shaders/Terrain.shader"); uboTess = new TessUBO { lightPos = new vec4(-48.0f, -40.0f, 46.0f, 0.0f), displacementFactor = 32.0f, tessellationFactor = 0.75f, tessellatedEdgeSize = 20.0f }; batch.material = material; ubTess = new SharedBuffer(VkBufferUsageFlags.UniformBuffer, (uint)Utilities.SizeOf <TessUBO>()); dsLayout = new DescriptorSetLayout { new DescriptorSetLayoutBinding(0, VkDescriptorType.UniformBuffer, VkShaderStageFlags.Fragment | VkShaderStageFlags.TessellationControl | VkShaderStageFlags.TessellationEvaluation), new DescriptorSetLayoutBinding(1, VkDescriptorType.CombinedImageSampler, VkShaderStageFlags.Fragment | VkShaderStageFlags.TessellationControl | VkShaderStageFlags.TessellationEvaluation), new DescriptorSetLayoutBinding(2, VkDescriptorType.CombinedImageSampler, VkShaderStageFlags.Fragment | VkShaderStageFlags.TessellationControl | VkShaderStageFlags.TessellationEvaluation), }; dsTess = new DescriptorSet(dsLayout); dsTess.Bind(0, ubTess); }
public IntPtr GetOrCreateImGuiBinding(ImageView textureView) { if (!_setsByView.TryGetValue(textureView, out ResourceSetInfo rsi)) { DescriptorSet resourceSet = new DescriptorSet(resourceLayoutTex, textureView); rsi = new ResourceSetInfo(GetNextImGuiBindingID(), resourceSet); _setsByView.Add(textureView, rsi); _viewsById.Add(rsi.ImGuiBinding, rsi); _ownedResources.Add(resourceSet); } return(rsi.ImGuiBinding); }
public void DrawBatchesMT(CommandBuffer cmd, FastList <SourceBatch> batches, DescriptorSet set0, Span <DescriptorSet> set1) { renderTasks.Clear(); int dpPerBatch = (int)Math.Ceiling(View.opaqueBatches.Count / (float)workCount); if (dpPerBatch < 200) { dpPerBatch = 200; } for (int i = 0; i < set1.Length; i++) { tempSets[i] = set1[i]; } ArraySegment <DescriptorSet> setSegment = new ArraySegment <DescriptorSet>(tempSets, 0, set1.Length); int idx = 0; for (int i = 0; i < batches.Count; i += dpPerBatch) { int from = i; int to = Math.Min(i + dpPerBatch, batches.Count); var cb = GetCmdBuffer(idx); secondCmdBuffers.Add(cb); var t = Task.Run(() => { cb.SetViewport(View.Viewport); cb.SetScissor(View.ViewRect); DrawBatches(cb, batches.AsSpan(from, to - from), set0, setSegment); cb.End(); }); renderTasks.Add(t); idx++; } Task.WaitAll(renderTasks.ToArray()); int workContext = Graphics.WorkContext; foreach (var c in secondCmdBuffers) { cmd.ExecuteCommand(c); } secondCmdBuffers.Clear(); tempSets.Clear(); }
public ShadowPass() : base(SubmitQueue.EarlyGraphics) { var depthFormat = Device.GetSupportedDepthFormat(); for (uint i = 0; i < SHADOW_MAP_CASCADE_COUNT; i++) { cascades[i].view = ImageView.Create(DepthRT.image, VkImageViewType.Image2DArray, depthFormat, VkImageAspectFlags.Depth, 0, 1, i, 1); } ubShadow = new SharedBuffer(VkBufferUsageFlags.UniformBuffer, (uint)(SHADOW_MAP_CASCADE_COUNT * Utilities.SizeOf <mat4>())); depthShader = Resources.Instance.Load <Shader>("shaders/shadow.shader"); vsSet = new DescriptorSet(depthShader.Main.GetResourceLayout(0), ubShadow, FrameGraph.TransformBuffer); }
public void DrawBatch(CommandBuffer cb, ulong passID, SourceBatch batch, Span <ConstBlock> pushConsts, DescriptorSet resourceSet, Span <DescriptorSet> resourceSet1) { var shader = batch.material.Shader; if ((passID & shader.passFlags) == 0) { return; } var pass = shader.GetPass(passID); var pipe = pass.GetGraphicsPipeline(FrameGraphPass.RenderPass, SubpassIndex, batch.geometry); cb.BindPipeline(VkPipelineBindPoint.Graphics, pipe); batch.Draw(cb, pushConsts, resourceSet, resourceSet1, pass); }
public virtual void Draw(CommandBuffer cb, Span <ConstBlock> pushConsts, DescriptorSet resourceSet, Span <DescriptorSet> resourceSet1, Pass pass) { cb.BindGraphicsResourceSet(pass.PipelineLayout, 0, resourceSet, offset); int firstSet = 1; foreach (var rs in resourceSet1) { if (firstSet < pass.PipelineLayout.ResourceLayout.Length && rs != null) { cb.BindGraphicsResourceSet(pass.PipelineLayout, firstSet, rs, -1); } firstSet++; } foreach (ConstBlock constBlock in pushConsts) { cb.PushConstants(pass.PipelineLayout, constBlock.range.stageFlags, constBlock.range.offset, constBlock.range.size, constBlock.data); } material.Bind(pass.passIndex, cb); geometry.Draw(cb); }
private void InitLightCompute() { uint[] queue_families = null; VkSharingMode sharingMode = VkSharingMode.Exclusive; if (Device.QFGraphics != Device.QFCompute) { sharingMode = VkSharingMode.Concurrent; queue_families = new[] { Device.QFGraphics, Device.QFCompute }; } uint max_grid_count = ((MAX_WIDTH - 1) / TILE_WIDTH + 1) * ((MAX_HEIGHT - 1) / TILE_HEIGHT + 1) * TILE_COUNT_Z; light_pos_ranges = new SharedBuffer(VkBufferUsageFlags.StorageTexelBuffer, VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent, MAX_NUM_LIGHTS * 4 * sizeof(float), VkSharingMode.Exclusive, queue_families); light_pos_ranges.CreateView(VkFormat.R32G32B32A32SFloat); light_colors = new SharedBuffer(VkBufferUsageFlags.StorageTexelBuffer, VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent, MAX_NUM_LIGHTS * sizeof(uint), sharingMode, queue_families); light_colors.CreateView(VkFormat.R8G8B8A8UNorm); lightBounds = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, MAX_NUM_LIGHTS * 6 * sizeof(uint), VkFormat.R32UInt, sharingMode, queue_families); // max tile count 1d (z 256) gridLightCounts = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, max_grid_count * sizeof(uint), VkFormat.R32UInt, sharingMode, queue_families); // light count / grid gridLightCountTotal = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, sizeof(uint), VkFormat.R32UInt, sharingMode, queue_families); // light count total * max grid count gridLightCountOffsets = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, max_grid_count * sizeof(uint), VkFormat.R32UInt, sharingMode, queue_families); // same as above lightList = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, 1024 * 1024 * sizeof(uint), VkFormat.R32UInt, sharingMode, queue_families); // light idx gridLightCountsCompare = Buffer.CreateTexelBuffer(VkBufferUsageFlags.TransferDst, max_grid_count * sizeof(uint), VkFormat.R32UInt, sharingMode, queue_families); // light count / grid resourceLayout0 = new DescriptorSetLayout(1) { new DescriptorSetLayoutBinding(0, VkDescriptorType.UniformBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(1, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(2, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), }; resourceLayout1 = new DescriptorSetLayout(2) { new DescriptorSetLayoutBinding(0, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(1, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(2, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(3, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(4, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(5, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), new DescriptorSetLayoutBinding(6, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Fragment), }; resourceSet1 = new DescriptorSet(resourceLayout0, uboCluster, light_pos_ranges, light_colors); resourceSet2 = new DescriptorSet(resourceLayout1, gridFlags, lightBounds, gridLightCounts, gridLightCountTotal, gridLightCountOffsets, lightList, gridLightCountsCompare); clusterLight = Resources.Instance.Load <Shader>("Shaders/ClusterLight.shader"); computeLayout0 = new DescriptorSetLayout { new DescriptorSetLayoutBinding(0, VkDescriptorType.UniformBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(1, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(2, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), }; computeLayout1 = new DescriptorSetLayout { new DescriptorSetLayoutBinding(0, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(1, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(2, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(3, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(4, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(5, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), new DescriptorSetLayoutBinding(6, VkDescriptorType.StorageTexelBuffer, VkShaderStageFlags.Compute), }; computeSet0 = new DescriptorSet(computeLayout0, uboCluster, light_pos_ranges, light_colors); computeSet1 = new DescriptorSet(computeLayout1, gridFlags, lightBounds, gridLightCounts, gridLightCountTotal, gridLightCountOffsets, lightList, gridLightCountsCompare); }
public ResourceSetInfo(IntPtr imGuiBinding, DescriptorSet resourceSet) { ImGuiBinding = imGuiBinding; ResourceSet = resourceSet; }
public void Init(PipelineLayout pipelineLayout) { this.pipelineLayout = pipelineLayout; int count = pipelineLayout.ResourceLayout.Length; ResourceSet = new DescriptorSet[count]; if (count <= 0) { return; } for (int i = 0; i < count; i++) { ResourceSet[i] = new DescriptorSet(pipelineLayout.ResourceLayout[i]); var resLayout = pipelineLayout.ResourceLayout[i]; foreach (var binding in resLayout.Bindings) { if (binding.IsInlineUniformBlock && binding.resourceInfo != null) { if (inlineUniformBlocks == null) { inlineUniformBlocks = new List <InlineUniformBlock>(); } var inlineUniformBlock = new InlineUniformBlock(binding.resourceInfo); inlineUniformBlock.descriptorSet = ResourceSet[i]; ResourceSet[i].Bind(binding.binding, inlineUniformBlock); inlineUniformBlocks.Add(inlineUniformBlock); } } } if (pipelineLayout.PushConstantNames != null) { for (int i = 0; i < pipelineLayout.PushConstantNames.Count; i++) { var constName = pipelineLayout.PushConstantNames[i]; if (constName.StartsWith("g_")) { continue; } var pushConst = pipelineLayout.PushConstant[i]; if (pushConst.offset + pushConst.size > Device.MaxPushConstantsSize) { Log.Error("PushConst out of range" + constName); continue; } if (pushConst.offset < minPushConstRange) { minPushConstRange = pushConst.offset; } if (pushConst.offset + pushConst.size > maxPushConstRange) { maxPushConstRange = pushConst.offset + pushConst.size; } } } }
public void DrawBatches(CommandBuffer commandBuffer, Span <SourceBatch> sourceBatches, DescriptorSet set0, Span <DescriptorSet> set1) { foreach (var batch in sourceBatches) { DrawBatch(commandBuffer, passID, batch, default, set0, set1);
public void RegResourceSet(StringID id, DescriptorSet resSet) { resourceSets[id] = resSet; }