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"); }
public ThreadedShader(ThreadedRenderer renderer, ShaderStage stage, ShaderBindings bindings, byte[] code) { _renderer = renderer; _stage = stage; _bindings = bindings; _binaryCode = code; }
public ThreadedShader(ThreadedRenderer renderer, ShaderStage stage, ShaderBindings bindings, string code) { _renderer = renderer; _stage = stage; _bindings = bindings; _code = code; }
protected override void Render(ScriptableRenderContext context, Camera[] cameras) { ShaderBindings.SetPerFrameShaderVariables(context); foreach (Camera camera in cameras) { #if UNITY_EDITOR if (camera.cameraType == CameraType.SceneView) { ScriptableRenderContext.EmitWorldGeometryForSceneView(camera); } #endif CullingResults cullingResults = Cull(context, camera); ShaderBindings.SetPerCameraShaderVariables(context, camera); DrawCamera(context, cullingResults, camera); } }
public HelperShader(VulkanRenderer gd, Device device) { _pipeline = new PipelineHelperShader(gd, device); _pipeline.Initialize(); _samplerLinear = gd.CreateSampler(GAL.SamplerCreateInfo.Create(MinFilter.Linear, MagFilter.Linear)); _samplerNearest = gd.CreateSampler(GAL.SamplerCreateInfo.Create(MinFilter.Nearest, MagFilter.Nearest)); var vertexBindings = new ShaderBindings( new[] { 1 }, Array.Empty <int>(), Array.Empty <int>(), Array.Empty <int>()); var fragmentBindings = new ShaderBindings( Array.Empty <int>(), Array.Empty <int>(), new[] { 0 }, Array.Empty <int>()); _programColorBlit = gd.CreateProgramWithMinimalLayout(new[] { new ShaderSource(ShaderBinaries.ColorBlitVertexShaderSource, vertexBindings, ShaderStage.Vertex, TargetLanguage.Glsl), new ShaderSource(ShaderBinaries.ColorBlitFragmentShaderSource, fragmentBindings, ShaderStage.Fragment, TargetLanguage.Glsl), }); _programColorBlitClearAlpha = gd.CreateProgramWithMinimalLayout(new[] { new ShaderSource(ShaderBinaries.ColorBlitVertexShaderSource, vertexBindings, ShaderStage.Vertex, TargetLanguage.Glsl), new ShaderSource(ShaderBinaries.ColorBlitClearAlphaFragmentShaderSource, fragmentBindings, ShaderStage.Fragment, TargetLanguage.Glsl), }); var fragmentBindings2 = new ShaderBindings( Array.Empty <int>(), Array.Empty <int>(), Array.Empty <int>(), Array.Empty <int>()); _programColorClear = gd.CreateProgramWithMinimalLayout(new[] { new ShaderSource(ShaderBinaries.ColorClearVertexShaderSource, vertexBindings, ShaderStage.Vertex, TargetLanguage.Glsl), new ShaderSource(ShaderBinaries.ColorClearFragmentShaderSource, fragmentBindings2, ShaderStage.Fragment, TargetLanguage.Glsl), }); }
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"); }
public IShader CompileShader(ShaderStage stage, ShaderBindings bindings, byte[] code) { return(new Shader(stage, code)); }
public IShader CompileShader(ShaderStage stage, ShaderBindings bindings, byte[] code) { return(new Shader(Api, _device, stage, bindings, code)); }