public ShaderInfo(VkShaderStageFlags _stageFlags, string _spirvPath, SpecializationInfo specializationInfo = null, string _entryPoint = "main") { StageFlags = _stageFlags; SpirvPath = _spirvPath; EntryPoint = new FixedUtf8String(_entryPoint); this.SpecializationInfo = specializationInfo; }
public void PushConstants <T>(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, T[] data) where T : struct { GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)(data.Length * Interop.SizeOf <T>()), handle.AddrOfPinnedObject()); handle.Free(); }
public ShaderModule(VkShaderStageFlags stage, byte[] code) { Stage = stage; Code = code; Build(); }
public void PushConstants(CommandBuffer cmd) { int size = maxPushConstRange - minPushConstRange; if (size > 0) { VkShaderStageFlags shaderStage = VkShaderStageFlags.None; int minRange = minPushConstRange; int currentSize = 0; for (int i = 0; i < pipelineLayout.PushConstant.Length; i++) { if (i == 0) { shaderStage = pipelineLayout.PushConstant[0].stageFlags; } currentSize += pipelineLayout.PushConstant[i].size; if ((pipelineLayout.PushConstant[i].stageFlags != shaderStage) || (i == pipelineLayout.PushConstant.Length - 1)) { cmd.PushConstants(pipelineLayout, shaderStage, minRange, currentSize, pushConstBuffer + minRange); shaderStage = pipelineLayout.PushConstant[i].stageFlags; minRange += currentSize; currentSize = 0; } } } }
internal static VkShaderStageFlags VdToVkShaderStages(ShaderStages stage) { VkShaderStageFlags ret = VkShaderStageFlags.None; if ((stage & ShaderStages.Vertex) == ShaderStages.Vertex) { ret |= VkShaderStageFlags.Vertex; } if ((stage & ShaderStages.Geometry) == ShaderStages.Geometry) { ret |= VkShaderStageFlags.Geometry; } if ((stage & ShaderStages.TessellationControl) == ShaderStages.TessellationControl) { ret |= VkShaderStageFlags.TessellationControl; } if ((stage & ShaderStages.TessellationEvaluation) == ShaderStages.TessellationEvaluation) { ret |= VkShaderStageFlags.TessellationEvaluation; } if ((stage & ShaderStages.Fragment) == ShaderStages.Fragment) { ret |= VkShaderStageFlags.Fragment; } return(ret); }
public override void RenderNode(CommandBuffer cmd, PipelineLayout pipelineLayout, Node node, Matrix4x4 currentTransform, bool shadowPass = false) { Matrix4x4 localMat = node.localMatrix * currentTransform; VkShaderStageFlags matStage = shadowPass ? VkShaderStageFlags.Geometry : VkShaderStageFlags.Vertex; cmd.PushConstant(pipelineLayout, matStage, localMat); if (node.Mesh != null) { foreach (Primitive p in node.Mesh.Primitives) { if (!shadowPass) { cmd.PushConstant(pipelineLayout, VkShaderStageFlags.Fragment, (int)p.material, (uint)Marshal.SizeOf <Matrix4x4> ()); if (descriptorSets[p.material] != null) { cmd.BindDescriptorSet(pipelineLayout, descriptorSets[p.material], 2); } } cmd.DrawIndexed(p.indexCount, 1, p.indexBase, p.vertexBase, 0); } } if (node.Children == null) { return; } foreach (Node child in node.Children) { RenderNode(cmd, pipelineLayout, child, localMat, shadowPass); } }
ShaderModule LoadShaderModelFromFile(VkShaderStageFlags shaderStage, string file, string[] defs) { using (File stream = FileSystem.Instance.GetFile(file)) { return(LoadShaderModel(shaderStage, stream.ReadAllText(), defs)); } }
public static extern void CmdPushConstants( VkCommandBuffer commandBuffer, VkPipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, uint size, IntPtr pValues );
public VkDescriptorSetLayoutBinding(uint _binding, VkShaderStageFlags _stageFlags, VkDescriptorType _descriptorType, uint _descriptorCount = 1) { binding = _binding; this._descriptorType = (int)_descriptorType; descriptorCount = _descriptorCount; this._stageFlags = (uint)_stageFlags; _pImmutableSamplers = IntPtr.Zero; }
public VkDescriptorSetLayoutBinding(uint _binding, VkShaderStageFlags _stageFlags, VkDescriptorType _descriptorType, uint _descriptorCount = 1) { binding = _binding; descriptorType = _descriptorType; descriptorCount = _descriptorCount; stageFlags = _stageFlags; pImmutableSamplers = (IntPtr)0; }
public DescriptorSetLayoutBinding(uint binding, VkDescriptorType type, VkShaderStageFlags stageFlags, uint descriptorCount = 1) { descriptorType = type; this.binding = binding; this.descriptorCount = descriptorCount; this.stageFlags = stageFlags; pImmutableSamplers = null; }
public void PushConstants <T>(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, T data) where T : struct { unsafe { //can't take address of generic variable in c# IntPtr native = (IntPtr)Unsafe.AsPointer(ref data); Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)Interop.SizeOf <T>(), native); } }
/// <summary> /// Create a new ShaderInfo object by providing directly a VkShaderModule. Note /// that this module will not be own by this ShaderInfo, and so will not be /// destroyed on Dispose. /// </summary> public ShaderInfo(VkShaderStageFlags stageFlags, VkShaderModule module, SpecializationInfo specializationInfo = null, string entryPoint = "main") { EntryPoint = new FixedUtf8String(entryPoint); info.stage = stageFlags; info.pName = EntryPoint; info.module = module; info.pSpecializationInfo = (specializationInfo == null) ? IntPtr.Zero : specializationInfo.InfosPtr; }
public void PushConstants <T>(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, List <T> data) where T : struct { unsafe { int size = (int)Interop.SizeOf <T>(); byte *native = stackalloc byte[size * data.Count]; Interop.Copy(data, (IntPtr)native); Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)(size * data.Count), (IntPtr)native); } }
public void PushConstants(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, byte[] data) { unsafe { fixed(byte *ptr = data) { Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)data.Length, (IntPtr)ptr); } } }
public static ShaderModule CreateShaderModule(VkShaderStageFlags stage, string fileName, string funcName = "main") { using (File stream = FileSystem.Instance.GetFile(fileName)) { var source = stream.ReadAllText(); var shaderModule = ShaderReader.CreateShaderModule(stage, source, fileName); shaderModule.Build(); return(shaderModule); } }
protected VkPipelineShaderStageCreateInfo loadShader(string fileName, VkShaderStageFlags stage) { VkPipelineShaderStageCreateInfo shaderStage = VkPipelineShaderStageCreateInfo.New(); shaderStage.stage = stage; shaderStage.module = Tools.loadShader(fileName, device, stage); shaderStage.pName = Strings.main; // todo : make param Debug.Assert(shaderStage.module.Handle != 0); shaderModules.Add(shaderStage.module); return(shaderStage); }
public static VkPushConstantRange pushConstantRange( VkShaderStageFlags stageFlags, uint size, uint offset) { VkPushConstantRange pushConstantRange = new VkPushConstantRange(); pushConstantRange.stageFlags = stageFlags; pushConstantRange.offset = offset; pushConstantRange.size = size; return(pushConstantRange); }
public static ShaderInfo CreateShaderInfo(this shaderc.Compiler comp, Device dev, string shaderPath, shaderc.ShaderKind shaderKind, SpecializationInfo specializationInfo = null, string entryPoint = "main") { using (shaderc.Result res = comp.Compile(shaderPath, shaderKind)) { if (res.Status != shaderc.Status.Success) { throw new Exception($"Shaderc compilation failure: {res.ErrorMessage}"); } VkShaderStageFlags stageFlags = Utils.ShaderKindToStageFlag(shaderKind); return(new ShaderInfo(dev, stageFlags, res.CodePointer, (UIntPtr)res.CodeLength, specializationInfo, entryPoint)); } }
public DescriptorBindingInfo( uint index, VkDescriptorType descriptorType, uint descriptorCounts, VkShaderStageFlags shaderStageFlags ) { Index = index; DescriptorType = descriptorType; DescriptorCounts = descriptorCounts; ShaderStageFlags = shaderStageFlags; }
public static VkDescriptorSetLayoutBinding descriptorSetLayoutBinding( VkDescriptorType type, VkShaderStageFlags stageFlags, uint binding, uint descriptorCount = 1) { VkDescriptorSetLayoutBinding setLayoutBinding = new VkDescriptorSetLayoutBinding(); setLayoutBinding.descriptorType = type; setLayoutBinding.stageFlags = stageFlags; setLayoutBinding.binding = binding; setLayoutBinding.descriptorCount = descriptorCount; return(setLayoutBinding); }
public ShaderModule(VkShaderStageFlags stage, IntPtr CodePointer, uint CodeLength) { Stage = stage; unsafe { var sm = new VkShaderModuleCreateInfo { sType = VkStructureType.ShaderModuleCreateInfo, pCode = (uint *)CodePointer, codeSize = new UIntPtr(CodeLength), }; handle = Device.CreateShaderModule(ref sm); } }
private VkShaderStageFlags MapStageFlags(ShaderStages stages) { VkShaderStageFlags flags = VkShaderStageFlags.None; if ((stages & ShaderStages.Vertex) == ShaderStages.Vertex) { flags |= VkShaderStageFlags.Vertex; } if ((stages & ShaderStages.Geometry) == ShaderStages.Geometry) { flags |= VkShaderStageFlags.Geometry; } if ((stages & ShaderStages.Fragment) == ShaderStages.Fragment) { flags |= VkShaderStageFlags.Fragment; } return(flags); }
public VkShaderStatisticsInfoAMD( VkShaderStageFlags shaderStageMask = default, VkShaderResourceUsageAMD resourceUsage = default, uint numPhysicalVgprs = default, uint numPhysicalSgprs = default, uint numAvailableVgprs = default, uint numAvailableSgprs = default, uint computeWorkGroupSize_0 = default, uint computeWorkGroupSize_1 = default, uint computeWorkGroupSize_2 = default ) { ShaderStageMask = shaderStageMask; ResourceUsage = resourceUsage; NumPhysicalVgprs = numPhysicalVgprs; NumPhysicalSgprs = numPhysicalSgprs; NumAvailableVgprs = numAvailableVgprs; NumAvailableSgprs = numAvailableSgprs; ComputeWorkGroupSize[0] = computeWorkGroupSize_0; ComputeWorkGroupSize[1] = computeWorkGroupSize_1; ComputeWorkGroupSize[2] = computeWorkGroupSize_2; }
public static VkShaderModule loadShader(string fileName, VkDevice device, VkShaderStageFlags stage) { using (var fs = File.OpenRead(fileName)) { var length = fs.Length; } byte[] shaderCode = File.ReadAllBytes(fileName); ulong shaderSize = (ulong)shaderCode.Length; fixed(byte *scPtr = shaderCode) { // Create a new shader module that will be used for Pipeline creation VkShaderModuleCreateInfo moduleCreateInfo = VkShaderModuleCreateInfo.New(); moduleCreateInfo.codeSize = new UIntPtr(shaderSize); moduleCreateInfo.pCode = (uint *)scPtr; Util.CheckResult(vkCreateShaderModule(device, ref moduleCreateInfo, null, out VkShaderModule shaderModule)); return(shaderModule); } }
internal static VkShaderStageFlags VdToVkShaderStages(ShaderStages stage) { VkShaderStageFlags ret = VkShaderStageFlags.None; if ((stage & ShaderStages.Vertex) == ShaderStages.Vertex) ret |= VkShaderStageFlags.Vertex; if ((stage & ShaderStages.Geometry) == ShaderStages.Geometry) ret |= VkShaderStageFlags.Geometry; if ((stage & ShaderStages.TessellationControl) == ShaderStages.TessellationControl) ret |= VkShaderStageFlags.TessellationControl; if ((stage & ShaderStages.TessellationEvaluation) == ShaderStages.TessellationEvaluation) ret |= VkShaderStageFlags.TessellationEvaluation; if ((stage & ShaderStages.Fragment) == ShaderStages.Fragment) ret |= VkShaderStageFlags.Fragment; if ((stage & ShaderStages.Compute) == ShaderStages.Compute) ret |= VkShaderStageFlags.Compute; return ret; }
public static ShaderModule CreateShaderModule(VkShaderStageFlags shaderStage, string code, string includeFile) { #if SHARP_SHADER_COMPILER ShaderCompiler.Stage stage = ShaderCompiler.Stage.Vertex; switch (shaderStage) { case VkShaderStageFlags.Vertex: stage = ShaderCompiler.Stage.Vertex; break; case VkShaderStageFlags.Fragment: stage = ShaderCompiler.Stage.Fragment; break; case VkShaderStageFlags.Geometry: stage = ShaderCompiler.Stage.Geometry; break; case VkShaderStageFlags.Compute: stage = ShaderCompiler.Stage.Compute; break; case VkShaderStageFlags.TessellationControl: stage = ShaderCompiler.Stage.TessControl; break; case VkShaderStageFlags.TessellationEvaluation: stage = ShaderCompiler.Stage.TessEvaluation; break; } var c = new ShaderCompiler(); var o = new CompileOptions(IncludeHandler) { Language = CompileOptions.InputLanguage.GLSL, Target = CompileOptions.Environment.Vulkan, GenerateDebug = true, }; var r = c.Preprocess(code, stage, o, "main"); if (r.NumberOfErrors > 0) { Log.Error(r.ErrorMessage); } if (r.CompileStatus != CompileResult.Status.Success) { return(null); } var source = r.GetString(); var res = c.Compile(source, stage, o, includeFile, "main"); if (res.NumberOfErrors > 0) { Log.Error(res.ErrorMessage); } if (res.CompileStatus != CompileResult.Status.Success) { return(null); } uint len = res.GetCode(out var codePointer); var refl = ReflectionShaderModule(source, codePointer, len / 4); var shaderModule = new ShaderModule(shaderStage, codePointer, len) { ShaderReflection = refl }; #else shaderc.ShaderKind stage = shaderc.ShaderKind.VertexShader; switch (shaderStage) { case VkShaderStageFlags.Vertex: stage = shaderc.ShaderKind.VertexShader; break; case VkShaderStageFlags.Fragment: stage = shaderc.ShaderKind.FragmentShader; break; case VkShaderStageFlags.Geometry: stage = shaderc.ShaderKind.GeometryShader; break; case VkShaderStageFlags.Compute: stage = shaderc.ShaderKind.ComputeShader; break; case VkShaderStageFlags.TessControl: stage = shaderc.ShaderKind.TessControlShader; break; case VkShaderStageFlags.TessEvaluation: stage = shaderc.ShaderKind.TessEvaluationShader; break; } shaderc.Compiler.GetSpvVersion(out shaderc.SpirVVersion version, out uint revision); shaderc.Options o = new shaderc.Options() //new ShadercOptions() { //SourceLanguage = shaderc.SourceLanguage.Glsl, //TargetSpirVVersion = new shaderc.SpirVVersion(1,5), //Optimization = shaderc.OptimizationLevel.Performance }; o.EnableDebugInfo(); o.IncludeDirectories.Add(FileSystem.WorkSpace + "data/shaders/common"); o.IncludeDirectories.Add(FileSystem.WorkSpace + "data/shaders/glsl"); var c = new shaderc.Compiler(o); var res = c.Compile(code, includeFile, stage); if (res.ErrorCount > 0) { Log.Error(res.ErrorMessage); } if (res.Status != Status.Success) { return(null); } var refl = ReflectionShaderModule(code, res.CodePointer, res.CodeLength / 4); var shaderModule = new ShaderModule(shaderStage, res.CodePointer, res.CodeLength) { ShaderReflection = refl }; #endif return(shaderModule); }
ShaderModule LoadShaderModel(VkShaderStageFlags shaderStage, string code, string[] defs) { List <string> saveLines = new List <string>(); string ver = ""; string includeFile = ""; StringReader reader = new StringReader(code); while (true) { string line = reader.ReadLine(); if (line == null) { break; } string str = line.TrimStart(); if (str.StartsWith("#version")) { ver = line; break; } if (str.StartsWith("#include")) { var incFile = str.Substring(8); if (string.IsNullOrEmpty(includeFile)) { includeFile = incFile; } if (ReadInclude(incFile, saveLines, out ver)) { break; } } else { saveLines.Add(line); } } StringBuilder sb = new StringBuilder(); sb.AppendLine(ver); if (defs != null) { foreach (var def in defs) { sb.Append("#define ").Append(def); } } foreach (var line in saveLines) { sb.AppendLine(line); } sb.Append(reader.ReadToEnd()); code = sb.ToString(); return(CreateShaderModule(shaderStage, code, includeFile)); }
internal extern static void vkCmdPushConstants(IntPtr commandBuffer, UInt64 layout, VkShaderStageFlags stageFlags, UInt32 offset, UInt32 size, IntPtr pValues);
public void PushConstants(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, uint size, IntPtr data) { Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, size, data); }