public static ShaderModule LoadShaderModule(IVulkanAppHost host, VulkanContext ctx, string path) { const int defaultBufferSize = 4096; using (Stream stream = host.Open(path)) using (var ms = new MemoryStream()) { stream.CopyTo(ms, defaultBufferSize); return(ctx.Device.CreateShaderModule(new ShaderModuleCreateInfo(ms.ToArray()))); } }
public static VkShaderModule LoadShaderModule(IVulkanAppHost host, VulkanContext ctx, string path) { const int defaultBufferSize = 4096; using (Stream stream = host.Open(path)) using (var ms = new MemoryStream()) { stream.CopyTo(ms, defaultBufferSize); byte[] bytes = ms.ToArray(); // Create a new shader module that will be used for Pipeline creation VkShaderModule shaderModule; vkCreateShaderModule(ctx.Device, bytes, null, out shaderModule); return(shaderModule); } }
public static VulkanImage LoadKtxVulkanImage(IVulkanAppHost host, VulkanContext ctx, string path) { using (var reader = new BinaryReader(host.Open(path))) { byte[] identifier = reader.ReadBytes(12); if (!identifier.SequenceEqual(KtxIdentifier)) { throw new InvalidOperationException("File is not in Khronos Texture format."); } int endienness = reader.ReadInt32(); int glType = reader.ReadInt32(); int glTypeSize = reader.ReadInt32(); int glFormat = reader.ReadInt32(); int glInternalFormat = reader.ReadInt32(); int glBaseInternalFormat = reader.ReadInt32(); int pixelWidth = reader.ReadInt32(); int pixelHeight = reader.ReadInt32(); int pixelDepth = reader.ReadInt32(); int numberOfArrayElements = reader.ReadInt32(); int numberOfFaces = reader.ReadInt32(); int numberOfMipmapLevels = reader.ReadInt32(); int bytesOfKeyValueData = reader.ReadInt32(); // Skip key-value data. reader.ReadBytes(bytesOfKeyValueData); // Some of the values may be 0 - ensure at least 1. pixelWidth = Math.Max(pixelWidth, 1); pixelHeight = Math.Max(pixelHeight, 1); pixelDepth = Math.Max(pixelDepth, 1); numberOfArrayElements = Math.Max(numberOfArrayElements, 1); numberOfFaces = Math.Max(numberOfFaces, 1); numberOfMipmapLevels = Math.Max(numberOfMipmapLevels, 1); int numberOfSlices = Math.Max(numberOfFaces, numberOfArrayElements); if (!_glInternalFormatToVkFormat.TryGetValue(glInternalFormat, out Format format)) { throw new NotImplementedException("glInternalFormat not mapped to VkFormat."); } var data = new TextureData { Mipmaps = new TextureData.Mipmap[numberOfMipmapLevels], Format = format }; for (int i = 0; i < numberOfMipmapLevels; i++) { var mipmap = new TextureData.Mipmap(); mipmap.Size = reader.ReadInt32(); mipmap.Extent = new Extent3D(pixelWidth, pixelHeight, pixelDepth); mipmap.Data = reader.ReadBytes(mipmap.Size); data.Mipmaps[i] = mipmap; break; // TODO: impl //for (int j = 0; j < numberOfArrayElements; j++) //{ // for (int k = 0; k < numberOfFaces; k++) // { // for (int l = 0; l < pixelDepth; l++) // { // //for (int row = 0; // // row < ) // } // } //} } return(VulkanImage.Texture2D(ctx, data)); } }