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

namespace CSGL.Vulkan {
    public class RenderPassCreateInfo {
        public List<AttachmentDescription> attachments;
        public List<SubpassDescription> subpasses;
        public List<SubpassDependency> dependencies;
    }

    public class AttachmentDescription {
        public VkAttachmentDescriptionFlags flags;
        public VkFormat format;
        public VkSampleCountFlags samples;
        public VkAttachmentLoadOp loadOp;
        public VkAttachmentStoreOp storeOp;
        public VkAttachmentLoadOp stencilLoadOp;
        public VkAttachmentStoreOp stencilStoreOp;
        public VkImageLayout initialLayout;
        public VkImageLayout finalLayout;
    }

    public class AttachmentReference {
        public uint attachment;
        public VkImageLayout layout;
    }

    public class SubpassDescription {
        public VkPipelineBindPoint pipelineBindPoint;
        public List<AttachmentReference> inputAttachments;
        public List<AttachmentReference> colorAttachments;
        public List<AttachmentReference> resolveAttachments;
        public List<uint> preserveAttachments;
        public AttachmentReference depthStencilAttachment;
    }

    public class SubpassDependency {
        public uint srcSubpass;
        public uint dstSubpass;
        public VkPipelineStageFlags srcStageMask;
        public VkPipelineStageFlags dstStageMask;
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
        public VkDependencyFlags dependencyFlags;
    }

    public class RenderPass : IDisposable, INative<VkRenderPass> {
        VkRenderPass renderPass;
        bool disposed = false;
        VkExtent2D granularity;

        public Device Device { get; private set; }

        public VkRenderPass Native {
            get {
                return renderPass;
            }
        }

        public VkExtent2D Granularity {
            get {
                return granularity;
            }
        }

        public List<AttachmentDescription> Attachments { get; private set; }
        public List<SubpassDescription> Subpasses { get; private set; }
        public List<SubpassDependency> Dependencies { get; private set; }

        public RenderPass(Device device, RenderPassCreateInfo info) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (info == null) throw new ArgumentNullException(nameof(info));

            Device = device;

            CreateRenderPass(info);

            if (info.attachments != null) Attachments = new List<AttachmentDescription>(info.attachments);
            if (info.subpasses != null) Subpasses = new List<SubpassDescription>(info.subpasses);
            if (info.dependencies != null) Dependencies = new List<SubpassDependency>(info.dependencies);
        }

        void CreateRenderPass(RenderPassCreateInfo mInfo) {
            unsafe {
                var info = new VkRenderPassCreateInfo();
                info.sType = VkStructureType.RenderPassCreateInfo;

                //for CreateInfo
                int attachmentCount = 0;
                int subpassCount = 0;
                int dependencyCount = 0;

                //for CreateInfo.subpasses
                int totalInputAttachments = 0;
                int totalColorAttachments = 0;
                int totalResolveAttachments = 0;
                int totalDepthStencilAttachments = 0;
                int totalPreserveAttachments = 0;

                if (mInfo.attachments != null) attachmentCount = mInfo.attachments.Count;
                if (mInfo.subpasses != null) subpassCount = mInfo.subpasses.Count;
                if (mInfo.dependencies != null) dependencyCount = mInfo.dependencies.Count;
<commit_msg>Refactor RenderPass construction<commit_after>﻿using System;
using System.Collections.Generic;

namespace CSGL.Vulkan {
    public class RenderPassCreateInfo {
        public List<AttachmentDescription> attachments;
        public List<SubpassDescription> subpasses;
        public List<SubpassDependency> dependencies;
    }

    public class AttachmentDescription {
        public VkAttachmentDescriptionFlags flags;
        public VkFormat format;
        public VkSampleCountFlags samples;
        public VkAttachmentLoadOp loadOp;
        public VkAttachmentStoreOp storeOp;
        public VkAttachmentLoadOp stencilLoadOp;
        public VkAttachmentStoreOp stencilStoreOp;
        public VkImageLayout initialLayout;
        public VkImageLayout finalLayout;

        public AttachmentDescription() { }

        internal AttachmentDescription(AttachmentDescription other) {
            flags = other.flags;
            format = other.format;
            samples = other.samples;
            loadOp = other.loadOp;
            storeOp = other.storeOp;
            stencilLoadOp = other.stencilLoadOp;
            stencilStoreOp = other.stencilStoreOp;
            initialLayout = other.initialLayout;
            finalLayout = other.finalLayout;
        }
    }

    public class AttachmentReference {
        public uint attachment;
        public VkImageLayout layout;

        public AttachmentReference() { }

        internal AttachmentReference(AttachmentReference other) {
            attachment = other.attachment;
            layout = other.layout;
        }
    }

    public class SubpassDescription {
        public VkPipelineBindPoint pipelineBindPoint;
        public List<AttachmentReference> inputAttachments;
        public List<AttachmentReference> colorAttachments;
        public List<AttachmentReference> resolveAttachments;
        public List<uint> preserveAttachments;
        public AttachmentReference depthStencilAttachment;

        public SubpassDescription() { }

        internal SubpassDescription(SubpassDescription other) {
            pipelineBindPoint = other.pipelineBindPoint;
            if (other.inputAttachments != null) {
                inputAttachments = new List<AttachmentReference>(other.inputAttachments.Count);
                foreach (var input in other.inputAttachments) {
                    inputAttachments.Add(new AttachmentReference(input));
                }
            }
            if (other.colorAttachments != null) {
                colorAttachments = new List<AttachmentReference>(other.colorAttachments.Count);
                foreach (var color in other.colorAttachments) {
                    colorAttachments.Add(new AttachmentReference(color));
                }
            }
            if (other.resolveAttachments != null) {
                resolveAttachments = new List<AttachmentReference>(other.resolveAttachments);
                foreach (var resolve in other.resolveAttachments) {
                    resolveAttachments.Add(new AttachmentReference(resolve));
                }
            }
            if (other.preserveAttachments != null) preserveAttachments = new List<uint>(other.preserveAttachments);
            if (other.depthStencilAttachment != null) depthStencilAttachment = new AttachmentReference(other.depthStencilAttachment);
        }
    }

    public class SubpassDependency {
        public uint srcSubpass;
        public uint dstSubpass;
        public VkPipelineStageFlags srcStageMask;
        public VkPipelineStageFlags dstStageMask;
        public VkAccessFlags srcAccessMask;
        public VkAccessFlags dstAccessMask;
        public VkDependencyFlags dependencyFlags;

        public SubpassDependency() { }

        internal SubpassDependency(SubpassDependency other) {
            srcSubpass = other.srcSubpass;
            dstSubpass = other.dstSubpass;
            srcStageMask = other.srcStageMask;
            dstStageMask = other.dstStageMask;
            srcAccessMask = other.srcAccessMask;
            dstAccessMask = other.dstAccessMask;
            dependencyFlags = other.dependencyFlags;
        }
    }

    public class RenderPass : IDisposable, INative<VkRenderPass> {
        VkRenderPass renderPass;
        bool disposed = false;
        VkExtent2D granularity;

        public Device Device { get; private set; }

        public VkRenderPass Native {
            get {
                return renderPass;
            }
        }

        public VkExtent2D Granularity {
            get {
                return granularity;
            }
        }

        public List<AttachmentDescription> Attachments { get; private set; }
        public List<SubpassDescription> Subpasses { get; private set; }
        public List<SubpassDependency> Dependencies { get; private set; }

        public RenderPass(Device device, RenderPassCreateInfo info) {
            if (device == null) throw new ArgumentNullException(nameof(device));
            if (info == null) throw new ArgumentNullException(nameof(info));

            Device = device;

            CreateRenderPass(info);

            if (info.attachments != null) {
                Attachments = new List<AttachmentDescription>(info.attachments.Count);
                foreach (var attachment in info.attachments) {
                    Attachments.Add(new AttachmentDescription(attachment));
                }
            }
            if (info.subpasses != null) {
                Subpasses = new List<SubpassDescription>(info.subpasses.Count);
                foreach (var subpass in info.subpasses) {
                    Subpasses.Add(new SubpassDescription(subpass));
                }
            }
            if (info.dependencies != null) {
                Dependencies = new List<SubpassDependency>(info.dependencies.Count);
                foreach (var dependency in  info.dependencies) {
                    Dependencies.Add(new SubpassDependency(dependency));
                }
            }
        }

        void CreateRenderPass(RenderPassCreateInfo mInfo) {
            unsafe {
                var info = new VkRenderPassCreateInfo();
                info.sType = VkStructureType.RenderPassCreateInfo;

                //for CreateInfo
                int attachmentCount = 0;
                int subpassCount = 0;
                int dependencyCount = 0;

                //for CreateInfo.subpasses
                int totalInputAttachments = 0;
                int totalColorAttachments = 0;
                int totalResolveAttachments = 0;
                int totalDepthStencilAttachments = 0;
                int totalPreserveAttachments = 0;

                if (mInfo.attachments != null) attachmentCount = mInfo.attachments.Count;
                if (mInfo.subpasses != null) subpassCount = mInfo.subpasses.Count;
                if (mInfo.dependencies != null) dependencyCount = mInfo.dependencies.Count;

{'fraction_non_alphanumeric': 0.06361940298507462, 'fraction_numerical': 0.0018656716417910447, 'mean_word_length': 2.1690806976056756, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '7524693', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2571, 'n_words': 614}