<commit_before>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CSGL.Vulkan {
    public class CommandBufferAllocateInfo {
        public VkCommandBufferLevel level;
        public uint commandBufferCount;
    }

    public class CommandBufferInheritanceInfo {
        public RenderPass renderPass;
        public uint subpass;
        public Framebuffer framebuffer;
        public bool occlusionQueryEnable;
        public VkQueryControlFlags queryFlags;
        public VkQueryPipelineStatisticFlags pipelineStatistics;

        internal VkCommandBufferInheritanceInfo GetNative() {
            VkCommandBufferInheritanceInfo info = new VkCommandBufferInheritanceInfo();
            info.sType = VkStructureType.CommandBufferInheritanceInfo;
            info.renderPass = renderPass.Native;
            info.subpass = subpass;

            var framebufferNative = VkFramebuffer.Null;
            if (framebuffer != null) framebufferNative = framebuffer.Native;
            info.framebuffer = framebufferNative;

            info.occlusionQueryEnable = occlusionQueryEnable ? 1u : 0u;
            info.queryFlags = queryFlags;
            info.pipelineStatistics = pipelineStatistics;

            return info;
        }
    }

    public class CommandBufferBeginInfo {
        public VkCommandBufferUsageFlags flags;
        public CommandBufferInheritanceInfo inheritanceInfo;

        internal VkCommandBufferBeginInfo GetNative(DisposableList<IDisposable> marshalled) {
            VkCommandBufferBeginInfo info = new VkCommandBufferBeginInfo();
            info.sType = VkStructureType.CommandBufferBeginInfo;
            info.flags = flags;

            if (inheritanceInfo != null) {
                var inheritanceInfoMarshalled = new Marshalled<VkCommandBufferInheritanceInfo>(inheritanceInfo.GetNative());
                info.pInheritanceInfo = inheritanceInfoMarshalled.Address;
                marshalled.Add(inheritanceInfoMarshalled);
            }

            return info;
        }
    }

    public class RenderPassBeginInfo {
        public RenderPass renderPass;
        public Framebuffer framebuffer;
        public VkRect2D renderArea;
        public IList<VkClearValue> clearValues;

        internal VkRenderPassBeginInfo GetNative(DisposableList<IDisposable> marshalled) {
            VkRenderPassBeginInfo info = new VkRenderPassBeginInfo();
            info.sType = VkStructureType.RenderPassBeginInfo;
            info.renderPass = renderPass.Native;
            info.framebuffer = framebuffer.Native;
            info.renderArea = renderArea;

            var clearValuesMarshalled = new MarshalledArray<VkClearValue>(clearValues);
            info.clearValueCount = (uint)clearValuesMarshalled.Count;
            info.pClearValues = clearValuesMarshalled.Address;

            marshalled.Add(clearValuesMarshalled);

            return info;
        }
    }

    public class MemoryBarrier {
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
    }

    public class BufferMemoryBarrier {
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
        public uint srcQueueFamilyIndex;
        public uint dstQueueFamilyIndex;
        public Buffer buffer;
        public ulong offset;
        public ulong size;
    }

    public class ImageMemoryBarrier {
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
        public VkImageLayout oldLayout;
        public VkImageLayout newLayout;
        public uint srcQueueFamilyIndex;
        public uint dstQueueFamilyIndex;
        public Image image;
        public VkImageSubresourceRange subresourceRange;
    }

    public class CommandBuffer : IDisposable, INative<VkCommandBuffer> {
        VkCommandBuffer commandBuffer;
        bool disposed;

        public VkCommandBuffer Native {
            get {
                return commandBuffer;
            }
        }

        public Device Device { get; private set; }
        public CommandPool Pool { get; private set; }
        public VkCommandBufferLevel Level { get; private set; }

        //set to false if pool is reset
        //prevents double free
        internal bool CanDispose { get; set; } = true;

        internal CommandBuffer(Device device, CommandPool pool, VkCommandBuffer commandBuffer, VkCommandBufferLevel level) {
            Device = device;
            Pool = pool;
            this.commandBuffer = commandBuffer;
            Level = level;
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        void Dispose(bool disposing) {
            if (disposed) return;

            if (CanDispose) {
                Pool.Free(this);
            }

            disposed = true;
        }

        ~CommandBuffer() {
            Dispose(false);
        }

        public void Begin(CommandBufferBeginInfo info) {
            using (var marshalled = new DisposableList<IDisposable>()) {
                var infoNative = info.GetNative(marshalled);

                var result = Device.Commands.beginCommandBuffer(commandBuffer, ref infoNative);
                if (result != VkResult.Success) throw new CommandBufferException(result, string.Format("Error beginning command buffer: {0}", result));<commit_msg>Refactor Begin<commit_after>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CSGL.Vulkan {
    public class CommandBufferAllocateInfo {
        public VkCommandBufferLevel level;
        public uint commandBufferCount;
    }

    public class CommandBufferInheritanceInfo {
        public RenderPass renderPass;
        public uint subpass;
        public Framebuffer framebuffer;
        public bool occlusionQueryEnable;
        public VkQueryControlFlags queryFlags;
        public VkQueryPipelineStatisticFlags pipelineStatistics;
    }

    public class CommandBufferBeginInfo {
        public VkCommandBufferUsageFlags flags;
        public CommandBufferInheritanceInfo inheritanceInfo;
    }

    public class RenderPassBeginInfo {
        public RenderPass renderPass;
        public Framebuffer framebuffer;
        public VkRect2D renderArea;
        public IList<VkClearValue> clearValues;

        internal VkRenderPassBeginInfo GetNative(DisposableList<IDisposable> marshalled) {
            VkRenderPassBeginInfo info = new VkRenderPassBeginInfo();
            info.sType = VkStructureType.RenderPassBeginInfo;
            info.renderPass = renderPass.Native;
            info.framebuffer = framebuffer.Native;
            info.renderArea = renderArea;

            var clearValuesMarshalled = new MarshalledArray<VkClearValue>(clearValues);
            info.clearValueCount = (uint)clearValuesMarshalled.Count;
            info.pClearValues = clearValuesMarshalled.Address;

            marshalled.Add(clearValuesMarshalled);

            return info;
        }
    }

    public class MemoryBarrier {
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
    }

    public class BufferMemoryBarrier {
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
        public uint srcQueueFamilyIndex;
        public uint dstQueueFamilyIndex;
        public Buffer buffer;
        public ulong offset;
        public ulong size;
    }

    public class ImageMemoryBarrier {
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
        public VkImageLayout oldLayout;
        public VkImageLayout newLayout;
        public uint srcQueueFamilyIndex;
        public uint dstQueueFamilyIndex;
        public Image image;
        public VkImageSubresourceRange subresourceRange;
    }

    public class CommandBuffer : IDisposable, INative<VkCommandBuffer> {
        VkCommandBuffer commandBuffer;
        bool disposed;

        public VkCommandBuffer Native {
            get {
                return commandBuffer;
            }
        }

        public Device Device { get; private set; }
        public CommandPool Pool { get; private set; }
        public VkCommandBufferLevel Level { get; private set; }

        //set to false if pool is reset
        //prevents double free
        internal bool CanDispose { get; set; } = true;

        internal CommandBuffer(Device device, CommandPool pool, VkCommandBuffer commandBuffer, VkCommandBufferLevel level) {
            Device = device;
            Pool = pool;
            this.commandBuffer = commandBuffer;
            Level = level;
        }

        public void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        void Dispose(bool disposing) {
            if (disposed) return;

            if (CanDispose) {
                Pool.Free(this);
            }

            disposed = true;
        }

        ~CommandBuffer() {
            Dispose(false);
        }

        public void Begin(CommandBufferBeginInfo info) {
            unsafe {
                var infoNative = new VkCommandBufferBeginInfo();
                infoNative.sType = VkStructureType.CommandBufferBeginInfo;
                infoNative.flags = info.flags;

                VkCommandBufferInheritanceInfo inheritanceNative = new VkCommandBufferInheritanceInfo();
                if (info.inheritanceInfo != null) {
                    inheritanceNative.sType = VkStructureType.CommandBufferInheritanceInfo;
                    inheritanceNative.renderPass = info.inheritanceInfo.renderPass.Native;
                    inheritanceNative.subpass = info.inheritanceInfo.subpass;

                    if (info.inheritanceInfo.framebuffer != null) inheritanceNative.framebuffer = info.inheritanceInfo.framebuffer.Native;

                    inheritanceNative.occlusionQueryEnable = info.inheritanceInfo.occlusionQueryEnable ? 1u : 0u;
                    inheritanceNative.queryFlags = info.inheritanceInfo.queryFlags;
                    inheritanceNative.pipelineStatistics = info.inheritanceInfo.pipelineStatistics;

                    infoNative.pInheritanceInfo = (IntPtr)(&inheritanceNative);
                }

                var result = Device.Commands.beginCommandBuffer(commandBuffer, ref infoNative);
                if (result != VkResult.Success) throw new CommandBufferException(result, string.Format("Error beginning command buffer: {0}", result));
{'fraction_non_alphanumeric': 0.05854867931724993, 'fraction_numerical': 0.0007628492419185658, 'mean_word_length': 2.332697807435653, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4093472', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2529, 'n_words': 610}