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

namespace CSGL.Vulkan {
    public class PipelineCache : INative<VkPipelineCache>, IDisposable {
        bool disposed;
        VkPipelineCache pipelineCache;
        public Device Device { get; private set; }

        public VkPipelineCache Native {
            get {
                return pipelineCache;
            }
        }

        public PipelineCache(Device device, byte[] initialData) {
            Device = device;

            CreateCache(initialData);
        }

        void CreateCache(byte[] initialData) {
            var info = new VkPipelineCacheCreateInfo();
            info.sType = VkStructureType.PipelineCacheCreateInfo;
            info.initialDataSize = (IntPtr)initialData.Length;

            var initialDataMarshalled = new PinnedArray<byte>(initialData);
            info.pInitialData = initialDataMarshalled.Address;

            using (initialDataMarshalled) {
                var result = Device.Commands.createPipelineCache(Device.Native, ref info, Device.Instance.AllocationCallbacks, out pipelineCache);
                if (result != VkResult.Success) throw new PipelineCacheException(result, string.Format("Error creating pipeline cache: {0}", result));
            }
        }

        public byte[] GetData() {
            ulong length = 0;
            Device.Commands.getPipelineCacheData(Device.Native, pipelineCache, ref length, IntPtr.Zero);
            byte[] result = new byte[length];

            GCHandle handle = GCHandle.Alloc(result, GCHandleType.Pinned);
            Device.Commands.getPipelineCacheData(Device.Native, pipelineCache, ref length, handle.AddrOfPinnedObject());
            handle.Free();

            return result;
        }

        public void Merge(PipelineCache[] srcCaches) {
            unsafe {
                VkPipelineCache* srcNative = stackalloc VkPipelineCache[srcCaches.Length];
                Interop.Marshal<VkPipelineCache, PipelineCache>(srcCaches, srcNative);

                Device.Commands.mergePipelineCache(Device.Native, pipelineCache, (uint)srcCaches.Length, (IntPtr)srcNative);
            }
        }

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

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

            Device.Commands.destroyPipelineCache(Device.Native, pipelineCache, Device.Instance.AllocationCallbacks);

            disposed = true;
        }

        ~PipelineCache() {
            Dispose(false);
        }
    }

    public class PipelineCacheException : VulkanException {
        public PipelineCacheException(VkResult result, string message) : base(result, message) { }
    }
}
<commit_msg>Add PipelineCacheCreateInfo<commit_after>﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CSGL.Vulkan {
    public class PipelineCacheCreateInfo {
        public IList<byte> initialData;
    }

    public class PipelineCache : INative<VkPipelineCache>, IDisposable {
        bool disposed;
        VkPipelineCache pipelineCache;
        public Device Device { get; private set; }

        public VkPipelineCache Native {
            get {
                return pipelineCache;
            }
        }

        public PipelineCache(Device device, PipelineCacheCreateInfo info) {
            Device = device;

            CreateCache(info);
        }

        void CreateCache(PipelineCacheCreateInfo mInfo) {
            var info = new VkPipelineCacheCreateInfo();
            info.sType = VkStructureType.PipelineCacheCreateInfo;
            info.initialDataSize = (IntPtr)mInfo.initialData.Count;

            var initialDataMarshalled = new NativeArray<byte>(mInfo.initialData);
            info.pInitialData = initialDataMarshalled.Address;

            using (initialDataMarshalled) {
                var result = Device.Commands.createPipelineCache(Device.Native, ref info, Device.Instance.AllocationCallbacks, out pipelineCache);
                if (result != VkResult.Success) throw new PipelineCacheException(result, string.Format("Error creating pipeline cache: {0}", result));
            }
        }

        public byte[] GetData() {
            ulong length = 0;
            Device.Commands.getPipelineCacheData(Device.Native, pipelineCache, ref length, IntPtr.Zero);
            byte[] result = new byte[length];

            GCHandle handle = GCHandle.Alloc(result, GCHandleType.Pinned);
            Device.Commands.getPipelineCacheData(Device.Native, pipelineCache, ref length, handle.AddrOfPinnedObject());
            handle.Free();

            return result;
        }

        public void Merge(IList<PipelineCache> srcCaches) {
            unsafe {
                VkPipelineCache* srcNative = stackalloc VkPipelineCache[srcCaches.Count];
                Interop.Marshal<VkPipelineCache, PipelineCache>(srcCaches, srcNative);

                Device.Commands.mergePipelineCache(Device.Native, pipelineCache, (uint)srcCaches.Count, (IntPtr)srcNative);
            }
        }

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

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

            Device.Commands.destroyPipelineCache(Device.Native, pipelineCache, Device.Instance.AllocationCallbacks);

            disposed = true;
        }

        ~PipelineCache() {
            Dispose(false);
        }
    }

    public class PipelineCacheException : VulkanException {
        public PipelineCacheException(VkResult result, string message) : base(result, message) { }
    }
}

{'fraction_non_alphanumeric': 0.08473980309423347, 'fraction_numerical': 0.0007032348804500703, 'mean_word_length': 2.340575455079272, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '637101', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1440, 'n_words': 301}