internal static ShaderModule LoadShader(string filePath, Device dv) { if (!File.Exists(filePath)) { Console.WriteLine("Bad file path: " + filePath + " in LoadShader()"); return(null); } FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (fs == null) { Console.WriteLine("Couldn't open shader: " + filePath + " in LoadShader()"); return(null); } BinaryReader br = new BinaryReader(fs); if (br == null) { Console.WriteLine("Couldn't open shader: " + filePath + " in LoadShader()"); return(null); } byte [] bytes = br.ReadBytes((int)fs.Length); br.Close(); fs.Close(); ShaderModuleCreateInfo smci = new ShaderModuleCreateInfo(bytes); ShaderModule sm = dv.CreateShaderModule(smci); return(sm); }
ShaderModule CreateShaderModule(string filename) { byte[] stream; using (var code = System.IO.File.OpenRead(filename)) { stream = new byte[code.Length]; code.Read(stream, 0, stream.Length); } fixed(byte *ptr = stream) { var shader_module_create_info = new ShaderModuleCreateInfo { sType = StructureType.ShaderModuleCreateInfo, // VkStructureType sType pNext = IntPtr.Zero, // const void *pNext flags = 0, // VkShaderModuleCreateFlagBits flags codeSize = stream.Length, // size_t codeSize pCode = (uint *)ptr // const uint32_t *pCode }; ShaderModule shader_module; vk.CreateShaderModule(GetDevice, ref shader_module_create_info, (AllocationCallbacks *)0, out shader_module).CheckError(); return(shader_module); } }
public void CreateShaderModule() { var createInfo = new ShaderModuleCreateInfo(ReadAllBytes("Shader.vert.spv")); using (Device.CreateShaderModule(createInfo)) { } using (Device.CreateShaderModule(createInfo, CustomAllocator)) { } }
public unsafe Shader(Vk api, Device device, ShaderStage stage, ShaderBindings bindings, string glsl) { _api = api; _device = device; Bindings = bindings; glsl = glsl.Replace("gl_VertexID", "gl_VertexIndex"); glsl = glsl.Replace("gl_InstanceID", "gl_InstanceIndex"); glsl = glsl.Replace("gl_SubGroupInvocationARB", "gl_SubgroupInvocationID"); glsl = glsl.Replace("unpackUint2x32(gl_SubGroupEqMaskARB).x", "gl_SubgroupEqMask.x"); glsl = glsl.Replace("unpackUint2x32(gl_SubGroupGeMaskARB).x", "gl_SubgroupGeMask.x"); glsl = glsl.Replace("unpackUint2x32(gl_SubGroupGtMaskARB).x", "gl_SubgroupGtMask.x"); glsl = glsl.Replace("unpackUint2x32(gl_SubGroupLeMaskARB).x", "gl_SubgroupLeMask.x"); glsl = glsl.Replace("unpackUint2x32(gl_SubGroupLtMaskARB).x", "gl_SubgroupLtMask.x"); glsl = glsl.Replace("unpackUint2x32(ballotARB", "(subgroupBallot"); glsl = glsl.Replace("readInvocationARB", "subgroupBroadcast"); glsl = glsl.Replace("#extension GL_ARB_shader_ballot : enable", "#extension GL_KHR_shader_subgroup_basic : enable\n#extension GL_KHR_shader_subgroup_ballot : enable"); // System.Console.WriteLine(glsl); Options options = new Options(false) { SourceLanguage = SourceLanguage.Glsl, TargetSpirVVersion = new SpirVVersion(1, 5) }; options.SetTargetEnvironment(TargetEnvironment.Vulkan, EnvironmentVersion.Vulkan_1_2); Compiler compiler = new Compiler(options); var scr = compiler.Compile(glsl, "Ryu", GetShaderCShaderStage(stage)); if (scr.Status != Status.Success) { Logger.Error?.Print(LogClass.Gpu, $"Shader compilation error: {scr.Status} {scr.ErrorMessage}"); return; } Valid = true; var spirvBytes = new Span <byte>((void *)scr.CodePointer, (int)scr.CodeLength); uint[] code = new uint[(scr.CodeLength + 3) / 4]; spirvBytes.CopyTo(MemoryMarshal.Cast <uint, byte>(new Span <uint>(code)).Slice(0, (int)scr.CodeLength)); fixed(uint *pCode = code) { var shaderModuleCreateInfo = new ShaderModuleCreateInfo() { SType = StructureType.ShaderModuleCreateInfo, CodeSize = scr.CodeLength, PCode = pCode }; api.CreateShaderModule(device, shaderModuleCreateInfo, null, out _module).ThrowOnError(); } _stage = stage.Convert(); _entryPointName = Marshal.StringToHGlobalAnsi("main"); }
protected ShaderModule CreateShaderModule(byte[] code) { var shaderInfo = new ShaderModuleCreateInfo { CodeSize = (UIntPtr)code.Length, CodeBytes = code }; return(device.CreateShaderModule(shaderInfo)); }
ShaderModule CreateShaderModule(byte[] shaderCode) { // Shader modules contain shader code and one or more entry points. Shaders are selected // from a shader module by specifying an entry point as part of pipeline creation. The // stages of a pipeline can use shaders that come from different modules. The shader code // defining a shader module must be in the SPIR-V format, as described by the 'Vulkan // Environment for SPIR-V' specification. var createInfo = new ShaderModuleCreateInfo(shaderCode); return(device.CreateShaderModule(createInfo)); }
void CreateComputePipeline() { /* * We create a compute pipeline here. */ /* * Create a shader module. A shader module basically just encapsulates some shader code. */ // the code in comp.spv was created by running the command: // glslangValidator.exe -V shader.comp byte[] code = ReadFile("shaders/shader.comp.spv"); ShaderModuleCreateInfo createInfo = new ShaderModuleCreateInfo() { Code = code }; computeShaderModule = device.CreateShaderModule(createInfo); /* * Now let us actually create the compute pipeline. * A compute pipeline is very simple compared to a graphics pipeline. * It only consists of a single stage with a compute shader. * So first we specify the compute shader stage, and it's entry point(main). */ PipelineShaderStageCreateInfo shaderStageCreateInfo = new PipelineShaderStageCreateInfo() { Stage = ShaderStages.Compute,// VK_SHADER_STAGE_COMPUTE_BIT; Module = computeShaderModule, Name = "main" }; /* * The pipeline layout allows the pipeline to access descriptor sets. * So we just specify the descriptor set layout we created earlier. */ PipelineLayoutCreateInfo pipelineLayoutCreateInfo = new PipelineLayoutCreateInfo(new[] { descriptorSetLayout }); pipelineLayout = device.CreatePipelineLayout(pipelineLayoutCreateInfo); ComputePipelineCreateInfo pipelineCreateInfo = new ComputePipelineCreateInfo() { Stage = shaderStageCreateInfo, Layout = pipelineLayout }; /* * Now, we finally create the compute pipeline. */ ComputePipelineCreateInfo[] ci = { pipelineCreateInfo }; pipelines = device.CreateComputePipelines(ci); }
public unsafe ShaderModule(Api api, byte[] code) { _api = api; fixed(byte *pCode = &code[0]) { var createInfo = new ShaderModuleCreateInfo(); createInfo.SType = StructureType.ShaderModuleCreateInfo; createInfo.CodeSize = (uint)code.Length; createInfo.PCode = (uint *)pCode; Util.Verify(_api.Vk.CreateShaderModule(_api.Device.VkDevice, createInfo, default, out _vkShaderModule), $"{nameof(ShaderModule)}: Unable to create shader module");
private ShaderModule CreateShaderModule(byte[] shaderCode) { fixed(byte *codePointer = &shaderCode[0]) { var createInfo = new ShaderModuleCreateInfo { StructureType = StructureType.ShaderModuleCreateInfo, CodeSize = shaderCode.Length, Code = new IntPtr(codePointer) }; return(device.CreateShaderModule(ref createInfo)); } }
public unsafe Shader(Vk api, Device device, ShaderSource shaderSource) { _api = api; _device = device; Bindings = shaderSource.Bindings; CompileStatus = ProgramLinkStatus.Incomplete; _stage = shaderSource.Stage.Convert(); _entryPointName = Marshal.StringToHGlobalAnsi("main"); CompileTask = Task.Run(() => { byte[] spirv = shaderSource.BinaryCode; if (spirv == null) { spirv = GlslToSpirv(shaderSource.Code, shaderSource.Stage); if (spirv == null) { CompileStatus = ProgramLinkStatus.Failure; return; } } fixed(byte *pCode = spirv) { var shaderModuleCreateInfo = new ShaderModuleCreateInfo() { SType = StructureType.ShaderModuleCreateInfo, CodeSize = (uint)spirv.Length, PCode = (uint *)pCode }; api.CreateShaderModule(device, shaderModuleCreateInfo, null, out _module).ThrowOnError(); } CompileStatus = ProgramLinkStatus.Success; }); }
private unsafe PipelineShaderStageCreateInfo[] CreateShaderStages(PipelineStateDescription pipelineStateDescription, out Dictionary <int, string> inputAttributeNames) { var stages = pipelineStateDescription.EffectBytecode.Stages; var nativeStages = new PipelineShaderStageCreateInfo[stages.Length]; inputAttributeNames = null; // GLSL converter always outputs entry point main() var entryPoint = Encoding.UTF8.GetBytes("main\0"); for (int i = 0; i < stages.Length; i++) { var shaderBytecode = BinarySerialization.Read <ShaderInputBytecode>(stages[i].Data); if (stages[i].Stage == ShaderStage.Vertex) inputAttributeNames = shaderBytecode.InputAttributeNames; fixed(byte *entryPointPointer = &entryPoint[0]) fixed(byte *codePointer = &shaderBytecode.Data[0]) { // Create shader module var moduleCreateInfo = new ShaderModuleCreateInfo { StructureType = StructureType.ShaderModuleCreateInfo, Code = new IntPtr(codePointer), CodeSize = shaderBytecode.Data.Length }; // Create stage nativeStages[i] = new PipelineShaderStageCreateInfo { StructureType = StructureType.PipelineShaderStageCreateInfo, Stage = VulkanConvertExtensions.Convert(stages[i].Stage), Name = new IntPtr(entryPointPointer), Module = GraphicsDevice.NativeDevice.CreateShaderModule(ref moduleCreateInfo) }; } } ; return(nativeStages); }
public unsafe Shader(Vk api, Device device, ShaderStage stage, ShaderBindings bindings, byte[] spirv) { _api = api; _device = device; Bindings = bindings; Valid = true; fixed(byte *pCode = spirv) { var shaderModuleCreateInfo = new ShaderModuleCreateInfo() { SType = StructureType.ShaderModuleCreateInfo, CodeSize = (uint)spirv.Length, PCode = (uint *)pCode }; api.CreateShaderModule(device, shaderModuleCreateInfo, null, out _module).ThrowOnError(); } _stage = stage.Convert(); _entryPointName = Marshal.StringToHGlobalAnsi("main"); }
private ShaderModule LoadShaderModule(string filename) { byte[] data = File.ReadAllBytes(filename); fixed(byte *pData = data) { ShaderModuleCreateInfo createInfo = new ShaderModuleCreateInfo { SType = StructureType.ShaderModuleCreateInfo, CodeSize = new UIntPtr((uint)data.Length), PCode = (uint *)pData }; ShaderModule module; var res = VkApi.CreateShaderModule(this.Device, &createInfo, null, &module); if (res != Result.Success) { throw new VMASharp.VulkanResultException("Failed to create Shader Module!", res); } return(module); } }
private unsafe PipelineShaderStageCreateInfo[] CreateShaderStages(PipelineStateDescription pipelineStateDescription, out Dictionary<int, string> inputAttributeNames) { var stages = pipelineStateDescription.EffectBytecode.Stages; var nativeStages = new PipelineShaderStageCreateInfo[stages.Length]; inputAttributeNames = null; // GLSL converter always outputs entry point main() var entryPoint = Encoding.UTF8.GetBytes("main\0"); for (int i = 0; i < stages.Length; i++) { var shaderBytecode = BinarySerialization.Read<ShaderInputBytecode>(stages[i].Data); if (stages[i].Stage == ShaderStage.Vertex) inputAttributeNames = shaderBytecode.InputAttributeNames; fixed (byte* entryPointPointer = &entryPoint[0]) fixed (byte* codePointer = &shaderBytecode.Data[0]) { // Create shader module var moduleCreateInfo = new ShaderModuleCreateInfo { StructureType = StructureType.ShaderModuleCreateInfo, Code = new IntPtr(codePointer), CodeSize = shaderBytecode.Data.Length }; // Create stage nativeStages[i] = new PipelineShaderStageCreateInfo { StructureType = StructureType.PipelineShaderStageCreateInfo, Stage = VulkanConvertExtensions.Convert(stages[i].Stage), Name = new IntPtr(entryPointPointer), Module = GraphicsDevice.NativeDevice.CreateShaderModule(ref moduleCreateInfo) }; } }; return nativeStages; }
internal static unsafe extern Result vkCreateShaderModule(Device device, ShaderModuleCreateInfo* createInfo, AllocationCallbacks* allocator, ShaderModule* shaderModule);
public unsafe ShaderModule CreateShaderModule(ref ShaderModuleCreateInfo createInfo, AllocationCallbacks* allocator = null) { ShaderModule shaderModule; fixed (ShaderModuleCreateInfo* __createInfo__ = &createInfo) { vkCreateShaderModule(this, __createInfo__, allocator, &shaderModule).CheckError(); } return shaderModule; }
private ShaderModule CreateShaderModule(byte[] shaderCode) { fixed (byte* codePointer = &shaderCode[0]) { var createInfo = new ShaderModuleCreateInfo { StructureType = StructureType.ShaderModuleCreateInfo, CodeSize = shaderCode.Length, Code = new IntPtr(codePointer) }; return device.CreateShaderModule(ref createInfo); } }
protected ShaderModule CreateShaderModule(byte[] shaderCode) { var createInfo = new ShaderModuleCreateInfo(shaderCode); return(device.CreateShaderModule(createInfo)); }