Last active
September 17, 2025 13:03
-
-
Save w0wca7a/939e7251e7ec2dfe2d080b2950607e6e to your computer and use it in GitHub Desktop.
Async Shader Precompiler for Garaphics compositor in Stride game engine based on https://gist.github.com/Eideren/ef6be9508d8d3b0e460d8a6d15f0937b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) Stride contributors (https://stride3d.net). | |
| // Distributed under the MIT license. | |
| using Stride.Core.Mathematics; | |
| using System; | |
| using Stride.Rendering; | |
| using Stride.Rendering.Compositing; | |
| using Stride.Rendering.Materials.ComputeColors; | |
| using Stride.Rendering.Materials; | |
| using Stride.Shaders; | |
| using Stride.Graphics; | |
| using Stride.Shaders.Compiler; | |
| using Stride.Engine; | |
| namespace BlurUI; | |
| public class AsyncShaderPrecompiler : SceneRendererBase | |
| { | |
| private Material fallbackColorMaterial; | |
| private GraphicsCompositor compositor; | |
| protected override void InitializeCore() | |
| { | |
| base.InitializeCore(); | |
| compositor = this.Services.GetService<SceneSystem>().GraphicsCompositor; | |
| foreach (var feature in compositor.RenderFeatures) | |
| { | |
| if (feature is MeshRenderFeature meshRf) | |
| { | |
| meshRf.ComputeFallbackEffect += FallbackForAsyncCompilation; | |
| } | |
| } | |
| } | |
| protected override void OnAddReference() => base.OnAddReference(); | |
| protected override void DrawCore(RenderContext context, RenderDrawContext drawContext) { } | |
| // Like in https://gist.github.com/Eideren/ef6be9508d8d3b0e460d8a6d15f0937b | |
| Effect FallbackForAsyncCompilation(RenderObject renderObject, RenderEffect renderEffect, RenderEffectState renderEffectState) | |
| { | |
| try | |
| { | |
| var renderMesh = (RenderMesh)renderObject; | |
| fallbackColorMaterial ??= Material.New(GraphicsDevice, new MaterialDescriptor | |
| { | |
| Attributes = | |
| { | |
| Diffuse = new MaterialDiffuseMapFeature(new ComputeColor(new Color4(1f, 1f, 1f))), | |
| DiffuseModel = new MaterialDiffuseLambertModelFeature() | |
| } | |
| }); | |
| var compilerParameters = new CompilerParameters { EffectParameters = { TaskPriority = -1 } }; | |
| compilerParameters.Set(MaterialKeys.PixelStageSurfaceShaders, fallbackColorMaterial.Passes[0].Parameters.Get(MaterialKeys.PixelStageSurfaceShaders)); | |
| compilerParameters.Set(MaterialKeys.PixelStageStreamInitializer, fallbackColorMaterial.Passes[0].Parameters.Get(MaterialKeys.PixelStageStreamInitializer)); | |
| compilerParameters.Set(LightingKeys.EnvironmentLights, [new ShaderClassSource("LightConstantWhite")]); | |
| renderEffect.FallbackParameters = new ParameterCollection(renderMesh.MaterialPass.Parameters); | |
| if (renderEffectState == RenderEffectState.Error) | |
| { | |
| renderEffect.RetryTime = DateTime.UtcNow + TimeSpan.FromSeconds(5); | |
| } | |
| return EffectSystem.LoadEffect(renderEffect.EffectSelector.EffectName, compilerParameters).WaitForResult(); | |
| } | |
| catch (Exception e) | |
| { | |
| renderEffect.State = RenderEffectState.Error; | |
| Console.WriteLine(e.ToString()); | |
| return null; | |
| } | |
| } | |
| } |
Author
w0wca7a
commented
Sep 17, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment