private void FreeAllHandles() { int currentBlockIndex = 0; EntryStorageBlock block = _blocks[currentBlockIndex]; uint currentOffset = 0; for (uint i = 0; i < _totalEntries; i++) { if (currentOffset == block.TotalSize) { currentBlockIndex += 1; block = _blocks[currentBlockIndex]; currentOffset = 0; } uint id = Unsafe.Read <byte>(block.BasePtr + currentOffset); if (id == 0) { currentBlockIndex += 1; block = _blocks[currentBlockIndex]; currentOffset = 0; id = Unsafe.Read <byte>(block.BasePtr + currentOffset); } Debug.Assert(id != 0); currentOffset += 1; byte *entryBasePtr = block.BasePtr + currentOffset; switch (id) { case BeginEntryID: currentOffset += BeginEntrySize; break; case ClearColorTargetID: currentOffset += ClearColorTargetEntrySize; break; case ClearDepthTargetID: currentOffset += ClearDepthTargetEntrySize; break; case DrawEntryID: currentOffset += DrawEntrySize; break; case DrawIndexedEntryID: currentOffset += DrawIndexedEntrySize; break; case DrawIndirectEntryID: ref NoAllocDrawIndirectEntry drawIndirectEntry = ref Unsafe.AsRef <NoAllocDrawIndirectEntry>(entryBasePtr); drawIndirectEntry.IndirectBuffer.Free(); currentOffset += DrawIndirectEntrySize; break; case DrawIndexedIndirectEntryID: ref NoAllocDrawIndexedIndirectEntry diie = ref Unsafe.AsRef <NoAllocDrawIndexedIndirectEntry>(entryBasePtr); diie.IndirectBuffer.Free(); currentOffset += DrawIndexedIndirectEntrySize; break; case DispatchEntryID: currentOffset += DispatchEntrySize; break; case DispatchIndirectEntryID: ref NoAllocDispatchIndirectEntry dispatchIndirect = ref Unsafe.AsRef <NoAllocDispatchIndirectEntry>(entryBasePtr); dispatchIndirect.IndirectBuffer.Free(); currentOffset += DispatchIndirectEntrySize; break; case EndEntryID: currentOffset += EndEntrySize; break; case SetFramebufferEntryID: ref NoAllocSetFramebufferEntry sfbe = ref Unsafe.AsRef <NoAllocSetFramebufferEntry>(entryBasePtr); sfbe.Framebuffer.Free(); currentOffset += SetFramebufferEntrySize; break; case SetIndexBufferEntryID: ref NoAllocSetIndexBufferEntry sibe = ref Unsafe.AsRef <NoAllocSetIndexBufferEntry>(entryBasePtr); sibe.Buffer.Free(); currentOffset += SetIndexBufferEntrySize; break; case SetPipelineEntryID: ref NoAllocSetPipelineEntry spe = ref Unsafe.AsRef <NoAllocSetPipelineEntry>(entryBasePtr); spe.Pipeline.Free(); currentOffset += SetPipelineEntrySize; break; case SetGraphicsResourceSetEntryID: ref NoAllocSetGraphicsResourceSetEntry sgrse = ref Unsafe.AsRef <NoAllocSetGraphicsResourceSetEntry>(entryBasePtr); sgrse.ResourceSet.Free(); currentOffset += SetGraphicsResourceSetEntrySize; break; case SetComputeResourceSetEntryID: ref NoAllocSetComputeResourceSetEntry scrse = ref Unsafe.AsRef <NoAllocSetComputeResourceSetEntry>(entryBasePtr); scrse.ResourceSet.Free(); currentOffset += SetComputeResourceSetEntrySize; break; case SetScissorRectEntryID: currentOffset += SetScissorRectEntrySize; break; case SetVertexBufferEntryID: ref NoAllocSetVertexBufferEntry svbe = ref Unsafe.AsRef <NoAllocSetVertexBufferEntry>(entryBasePtr); svbe.Buffer.Free(); currentOffset += SetVertexBufferEntrySize; break; case SetViewportEntryID: currentOffset += SetViewportEntrySize; break; case UpdateBufferEntryID: ref NoAllocUpdateBufferEntry ube = ref Unsafe.AsRef <NoAllocUpdateBufferEntry>(entryBasePtr); ube.Buffer.Free(); ube.StagingBlock.GCHandle.Free(); currentOffset += UpdateBufferEntrySize; break; case CopyBufferEntryID: ref NoAllocCopyBufferEntry cbe = ref Unsafe.AsRef <NoAllocCopyBufferEntry>(entryBasePtr); cbe.Source.Free(); cbe.Destination.Free(); currentOffset += CopyBufferEntrySize; break; case CopyTextureEntryID: ref NoAllocCopyTextureEntry cte = ref Unsafe.AsRef <NoAllocCopyTextureEntry>(entryBasePtr); cte.Source.Free(); cte.Destination.Free(); currentOffset += CopyTextureEntrySize; break; case ResolveTextureEntryID: ref NoAllocResolveTextureEntry rte = ref Unsafe.AsRef <NoAllocResolveTextureEntry>(entryBasePtr); rte.Source.Free(); rte.Destination.Free(); currentOffset += ResolveTextureEntrySize; break; default: throw new InvalidOperationException("Invalid entry ID: " + id); } } }
public void SetGraphicsResourceSet(uint slot, ResourceSet rs) { NoAllocSetGraphicsResourceSetEntry entry = new NoAllocSetGraphicsResourceSetEntry(slot, rs); AddEntry(SetGraphicsResourceSetEntryID, ref entry); }
public void ExecuteAll(OpenGLCommandExecutor executor) { int currentBlockIndex = 0; EntryStorageBlock block = _blocks[currentBlockIndex]; uint currentOffset = 0; for (uint i = 0; i < _totalEntries; i++) { if (currentOffset == block.TotalSize) { currentBlockIndex += 1; block = _blocks[currentBlockIndex]; currentOffset = 0; } uint id = Unsafe.Read <byte>(block.BasePtr + currentOffset); if (id == 0) { currentBlockIndex += 1; block = _blocks[currentBlockIndex]; currentOffset = 0; id = Unsafe.Read <byte>(block.BasePtr + currentOffset); } Debug.Assert(id != 0); currentOffset += 1; byte *entryBasePtr = block.BasePtr + currentOffset; switch (id) { case BeginEntryID: executor.Begin(); currentOffset += BeginEntrySize; break; case ClearColorTargetID: ref NoAllocClearColorTargetEntry ccte = ref Unsafe.AsRef <NoAllocClearColorTargetEntry>(entryBasePtr); executor.ClearColorTarget(ccte.Index, ccte.ClearColor); currentOffset += ClearColorTargetEntrySize; break; case ClearDepthTargetID: ref NoAllocClearDepthTargetEntry cdte = ref Unsafe.AsRef <NoAllocClearDepthTargetEntry>(entryBasePtr); executor.ClearDepthStencil(cdte.Depth, cdte.Stencil); currentOffset += ClearDepthTargetEntrySize; break; case DrawEntryID: ref NoAllocDrawEntry de = ref Unsafe.AsRef <NoAllocDrawEntry>(entryBasePtr); executor.Draw(de.VertexCount, de.InstanceCount, de.VertexStart, de.InstanceStart); currentOffset += DrawEntrySize; break; case DrawIndexedEntryID: ref NoAllocDrawIndexedEntry die = ref Unsafe.AsRef <NoAllocDrawIndexedEntry>(entryBasePtr); executor.DrawIndexed(die.IndexCount, die.InstanceCount, die.IndexStart, die.VertexOffset, die.InstanceCount); currentOffset += DrawIndexedEntrySize; break; case DrawIndirectEntryID: ref NoAllocDrawIndirectEntry drawIndirectEntry = ref Unsafe.AsRef <NoAllocDrawIndirectEntry>(entryBasePtr); executor.DrawIndirect( drawIndirectEntry.IndirectBuffer, drawIndirectEntry.Offset, drawIndirectEntry.DrawCount, drawIndirectEntry.Stride); currentOffset += DrawIndirectEntrySize; break; case DrawIndexedIndirectEntryID: ref NoAllocDrawIndexedIndirectEntry diie = ref Unsafe.AsRef <NoAllocDrawIndexedIndirectEntry>(entryBasePtr); executor.DrawIndexedIndirect(diie.IndirectBuffer, diie.Offset, diie.DrawCount, diie.Stride); currentOffset += DrawIndexedIndirectEntrySize; break; case DispatchEntryID: ref NoAllocDispatchEntry dispatchEntry = ref Unsafe.AsRef <NoAllocDispatchEntry>(entryBasePtr); executor.Dispatch(dispatchEntry.GroupCountX, dispatchEntry.GroupCountY, dispatchEntry.GroupCountZ); currentOffset += DispatchEntrySize; break; case DispatchIndirectEntryID: ref NoAllocDispatchIndirectEntry dispatchIndir = ref Unsafe.AsRef <NoAllocDispatchIndirectEntry>(entryBasePtr); executor.DispatchIndirect(dispatchIndir.IndirectBuffer, dispatchIndir.Offset); currentOffset += DispatchIndirectEntrySize; break; case EndEntryID: executor.End(); currentOffset += EndEntrySize; break; case SetFramebufferEntryID: ref NoAllocSetFramebufferEntry sfbe = ref Unsafe.AsRef <NoAllocSetFramebufferEntry>(entryBasePtr); executor.SetFramebuffer(sfbe.Framebuffer); currentOffset += SetFramebufferEntrySize; break; case SetIndexBufferEntryID: ref NoAllocSetIndexBufferEntry sibe = ref Unsafe.AsRef <NoAllocSetIndexBufferEntry>(entryBasePtr); executor.SetIndexBuffer(sibe.Buffer.Item, sibe.Format); currentOffset += SetIndexBufferEntrySize; break; case SetPipelineEntryID: ref NoAllocSetPipelineEntry spe = ref Unsafe.AsRef <NoAllocSetPipelineEntry>(entryBasePtr); executor.SetPipeline(spe.Pipeline); currentOffset += SetPipelineEntrySize; break; case SetGraphicsResourceSetEntryID: ref NoAllocSetGraphicsResourceSetEntry sgrse = ref Unsafe.AsRef <NoAllocSetGraphicsResourceSetEntry>(entryBasePtr); executor.SetGraphicsResourceSet(sgrse.Slot, sgrse.ResourceSet); currentOffset += SetGraphicsResourceSetEntrySize; break; case SetComputeResourceSetEntryID: ref NoAllocSetComputeResourceSetEntry scrse = ref Unsafe.AsRef <NoAllocSetComputeResourceSetEntry>(entryBasePtr); executor.SetComputeResourceSet(scrse.Slot, scrse.ResourceSet); currentOffset += SetComputeResourceSetEntrySize; break; case SetScissorRectEntryID: ref NoAllocSetScissorRectEntry ssre = ref Unsafe.AsRef <NoAllocSetScissorRectEntry>(entryBasePtr); executor.SetScissorRect(ssre.Index, ssre.X, ssre.Y, ssre.Width, ssre.Height); currentOffset += SetScissorRectEntrySize; break; case SetVertexBufferEntryID: ref NoAllocSetVertexBufferEntry svbe = ref Unsafe.AsRef <NoAllocSetVertexBufferEntry>(entryBasePtr); executor.SetVertexBuffer(svbe.Index, svbe.Buffer.Item); currentOffset += SetVertexBufferEntrySize; break; case SetViewportEntryID: ref NoAllocSetViewportEntry svpe = ref Unsafe.AsRef <NoAllocSetViewportEntry>(entryBasePtr); executor.SetViewport(svpe.Index, ref svpe.Viewport); currentOffset += SetViewportEntrySize; break; case UpdateBufferEntryID: ref NoAllocUpdateBufferEntry ube = ref Unsafe.AsRef <NoAllocUpdateBufferEntry>(entryBasePtr); fixed(byte *dataPtr = &ube.StagingBlock.Array[0]) { executor.UpdateBuffer( ube.Buffer.Item, ube.BufferOffsetInBytes, (IntPtr)dataPtr, ube.StagingBlock.SizeInBytes); } currentOffset += UpdateBufferEntrySize; break; case CopyBufferEntryID: ref NoAllocCopyBufferEntry cbe = ref Unsafe.AsRef <NoAllocCopyBufferEntry>(entryBasePtr); executor.CopyBuffer( cbe.Source, cbe.SourceOffset, cbe.Destination, cbe.DestinationOffset, cbe.SizeInBytes); currentOffset += CopyBufferEntrySize; break; case CopyTextureEntryID: ref NoAllocCopyTextureEntry cte = ref Unsafe.AsRef <NoAllocCopyTextureEntry>(entryBasePtr); executor.CopyTexture( cte.Source, cte.SrcX, cte.SrcY, cte.SrcZ, cte.SrcMipLevel, cte.SrcBaseArrayLayer, cte.Destination, cte.DstX, cte.DstY, cte.DstZ, cte.DstMipLevel, cte.DstBaseArrayLayer, cte.Width, cte.Height, cte.Depth, cte.LayerCount); currentOffset += CopyTextureEntrySize; break; case ResolveTextureEntryID: ref NoAllocResolveTextureEntry rte = ref Unsafe.AsRef <NoAllocResolveTextureEntry>(entryBasePtr); executor.ResolveTexture(rte.Source.Item, rte.Destination.Item); currentOffset += ResolveTextureEntrySize; break; default: throw new InvalidOperationException("Invalid entry ID: " + id); } } }