public SceneRenderer(GameSettingsAsset gameSettings) { if (gameSettings == null) throw new ArgumentNullException(nameof(gameSettings)); // Initialize services Services = new ServiceRegistry(); ContentManager = new ContentManager(Services); var renderingSettings = gameSettings.Get<RenderingSettings>(); GraphicsDevice = GraphicsDevice.New(DeviceCreationFlags.None, new[] { renderingSettings.DefaultGraphicsProfile }); var graphicsDeviceService = new GraphicsDeviceServiceLocal(Services, GraphicsDevice); EffectSystem = new EffectSystem(Services); GraphicsContext = new GraphicsContext(GraphicsDevice); Services.AddService(typeof(GraphicsContext), GraphicsContext); SceneSystem = new SceneSystem(Services); // Create game systems GameSystems = new GameSystemCollection(Services); GameSystems.Add(new GameFontSystem(Services)); GameSystems.Add(new UISystem(Services)); GameSystems.Add(EffectSystem); GameSystems.Add(SceneSystem); GameSystems.Initialize(); // Fake presenter // TODO GRAPHICS REFACTOR: This is needed be for render stage setup GraphicsDevice.Presenter = new RenderTargetGraphicsPresenter(GraphicsDevice, Texture.New2D(GraphicsDevice, renderingSettings.DefaultBackBufferWidth, renderingSettings.DefaultBackBufferHeight, renderingSettings.ColorSpace == ColorSpace.Linear ? PixelFormat.R8G8B8A8_UNorm_SRgb : PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource | TextureFlags.RenderTarget), PixelFormat.D24_UNorm_S8_UInt); SceneSystem.MainRenderFrame = RenderFrame.FromTexture(GraphicsDevice.Presenter.BackBuffer, GraphicsDevice.Presenter.DepthStencilBuffer); }
/// <summary> /// Update a dynamic effect instance based on its parameters. /// </summary> /// <param name="effectInstance">A dynmaic effect instance</param> /// <param name="passParameters">The pass parameters.</param> /// <returns><c>true</c> if the effect was recomiled on the effect instance, <c>false</c> otherwise.</returns> public bool Update(DynamicEffectInstance effectInstance, ParameterCollection passParameters) { bool effectChanged = false; var currentlyCompilingEffect = effectInstance.CurrentlyCompilingEffect; if (currentlyCompilingEffect != null) { if (currentlyCompilingEffect.IsCompleted) { if (currentlyCompilingEffect.IsFaulted) { var compilerParameters = new CompilerParameters(); effectInstance.CurrentlyCompilingUsedParameters.CopyTo(compilerParameters); SwitchFallbackEffect(FallbackEffectType.Error, effectInstance, passParameters, compilerParameters); } else { effectInstance.HasErrors = false; // Do not update effect right away: passParameters might have changed since last compilation; just try to go through a CreateEffect that will properly update the effect synchronously // TODO: This class (and maybe whole ParameterCollection system) need a complete rethink and rewrite with newest assumptions... //UpdateEffect(effectInstance, currentlyCompilingEffect.Result, effectInstance.CurrentlyCompilingUsedParameters, passParameters); } effectChanged = true; // Effect has been updated effectInstance.CurrentlyCompilingEffect = null; effectInstance.CurrentlyCompilingUsedParameters = null; } } if (effectChanged || // Check again, in case effect was just finished async compilation (effectInstance.Effect == null || !EffectSystem.IsValid(effectInstance.Effect) || HasCollectionChanged(effectInstance, passParameters) || effectInstance.HasErrors)) { if (effectInstance.HasErrors) { #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP var currentTime = DateTime.Now; if (currentTime < effectInstance.LastErrorCheck + ErrorCheckTimeSpan) { // Wait a regular interval before retrying to compile effect (i.e. every second) return(false); } // Update last check time effectInstance.LastErrorCheck = currentTime; #else // Other platforms: never try to recompile failed effects for now return(false); #endif } CreateEffect(effectInstance, passParameters); effectChanged = true; } return(effectChanged); }
private Effect GetOrCreatePickingSpriteEffect() { if (pickingSpriteEffect == null) { pickingSpriteEffect = EffectSystem.LoadEffect("SpritePicking").WaitForResult(); } return(pickingSpriteEffect); }
private Effect GetOrCreateSelectedSpriteEffect(ref Effect effect, bool isSRgb) { if (effect == null) { var compilerParameters = new CompilerParameters { [SpriteBaseKeys.ColorIsSRgb] = isSRgb }; effect = EffectSystem.LoadEffect("SelectedSprite", compilerParameters).WaitForResult(); } return(effect); }
public SkyboxGeneratorContext() { Services = new ServiceRegistry(); Assets = new AssetManager(Services); GraphicsDevice = GraphicsDevice.New(); GraphicsDeviceService = new GraphicsDeviceServiceLocal(Services, GraphicsDevice); EffectSystem = new EffectSystem(Services); EffectSystem.Initialize(); ((IContentable)EffectSystem).LoadContent(); ((EffectCompilerCache)EffectSystem.Compiler).CompileEffectAsynchronously = false; DrawEffectContext = RenderContext.GetShared(Services); }
internal static IEffectCompiler CreateEffectCompiler(EffectSystem effectSystem, Guid?packageId, EffectCompilationMode effectCompilationMode, bool recordEffectRequested, TaskSchedulerSelector taskSchedulerSelector = null) { EffectCompilerBase compiler = null; #if SILICONSTUDIO_XENKO_EFFECT_COMPILER if ((effectCompilationMode & EffectCompilationMode.Local) != 0) { // Local allowed and available, let's use that compiler = new EffectCompiler { SourceDirectories = { EffectCompilerBase.DefaultSourceShaderFolder }, }; } #endif // Nothing to do remotely bool needRemoteCompiler = (compiler == null && (effectCompilationMode & EffectCompilationMode.Remote) != 0); if (needRemoteCompiler || recordEffectRequested) { // Create the object that handles the connection var shaderCompilerTarget = new RemoteEffectCompilerClient(packageId); if (recordEffectRequested) { // Let's notify effect compiler server for each new effect requested effectSystem.EffectUsed += shaderCompilerTarget.NotifyEffectUsed; } // Use remote only if nothing else was found before (i.e. a local compiler) if (needRemoteCompiler) { // Create a remote compiler compiler = new RemoteEffectCompiler(shaderCompilerTarget); } else { // Otherwise, EffectSystem takes ownership of shaderCompilerTarget shaderCompilerTarget.DisposeBy(effectSystem); } } // Local not possible or allowed, and remote not allowed either => switch back to null compiler if (compiler == null) { compiler = new NullEffectCompiler(); } return(new EffectCompilerCache(compiler, taskSchedulerSelector)); }
public SkyboxGeneratorContext(SkyboxAsset skybox) { if (skybox == null) throw new ArgumentNullException(nameof(skybox)); Skybox = skybox; Services = new ServiceRegistry(); Content = new ContentManager(Services); GraphicsDevice = GraphicsDevice.New(); GraphicsDeviceService = new GraphicsDeviceServiceLocal(Services, GraphicsDevice); EffectSystem = new EffectSystem(Services); EffectSystem.Initialize(); ((IContentable)EffectSystem).LoadContent(); ((EffectCompilerCache)EffectSystem.Compiler).CompileEffectAsynchronously = false; RenderContext = RenderContext.GetShared(Services); RenderDrawContext = new RenderDrawContext(Services, RenderContext, new GraphicsContext(new CommandList(GraphicsDevice), new ResourceGroupAllocator(GraphicsDevice))); }
/// <summary> /// Initializes a new instance of the <see cref="XenkoRenderer"/> class. /// </summary> /// <param name="graphicsDeviceManager">The graphics device manager.</param> public XenkoRenderer(GraphicsDeviceManager graphicsDeviceManager, EffectSystem effectSystem) : base() { manager = graphicsDeviceManager; this.effectSystem = effectSystem; spriteBatch = new SpriteBatch(manager.GraphicsDevice); clipRectanges = new Stack<Rectangle>(); activeEffects = new Stack<EffectInstance>(); scissorRasterizerStateDescription = RasterizerStates.CullNone; scissorRasterizerStateDescription.ScissorTestEnable = true; // enables the scissor test geometryRasterizerStateDescription = RasterizerStates.CullNone; //geometryRasterizerStateDescription.FillMode = FillMode.Wireframe; geometryPipelineState = new MutablePipelineState(manager.GraphicsDevice); geometryPipelineState.State.DepthStencilState = DepthStencilStates.None; }
private void CreateEffect(DynamicEffectInstance effectInstance, ParameterCollection passParameters) { var compilerParameters = BuildCompilerParameters(effectInstance, passParameters); // Compile shader // possible exception in LoadEffect TaskOrResult <Effect> effect; ParameterCollection usedParameters; try { effect = EffectSystem.LoadEffect(EffectName, compilerParameters, out usedParameters); } catch (Exception) { SwitchFallbackEffect(FallbackEffectType.Error, effectInstance, passParameters, compilerParameters); return; } // Do we have an async compilation? if (asyncEffectCompiler && effect.Task != null) { effectInstance.CurrentlyCompilingEffect = effect.Task; effectInstance.CurrentlyCompilingUsedParameters = usedParameters; if (!effectInstance.HasErrors) // If there was an error, stay in that state (we don't want to switch between reloading and error states) { // Fallback to default effect var fallbackEffect = ComputeFallbackEffect(this, FallbackEffectType.Compiling, EffectName, compilerParameters); UpdateEffect(effectInstance, fallbackEffect.Effect, fallbackEffect.UsedParameters, passParameters); } return; } // TODO It throws an exception here when the compilation fails! var compiledEffect = effect.WaitForResult(); UpdateEffect(effectInstance, compiledEffect, usedParameters, passParameters); // Effect has been updated effectInstance.CurrentlyCompilingEffect = null; effectInstance.CurrentlyCompilingUsedParameters = null; }
/// <summary> /// Creates an effect. /// </summary> /// <param name="effectSystem">The effect system.</param> /// <param name="effectName">Name of the effect.</param> /// <returns>A new instance of an effect.</returns> public static TaskOrResult <Effect> LoadEffect(this EffectSystem effectSystem, string effectName) { var compilerParameters = new CompilerParameters(); return(effectSystem.LoadEffect(effectName, compilerParameters)); }
internal static IEffectCompiler CreateEffectCompiler(EffectSystem effectSystem, Guid? packageId, EffectCompilationMode effectCompilationMode, bool recordEffectRequested, TaskSchedulerSelector taskSchedulerSelector = null) { EffectCompilerBase compiler = null; #if SILICONSTUDIO_XENKO_EFFECT_COMPILER if ((effectCompilationMode & EffectCompilationMode.Local) != 0) { // Local allowed and available, let's use that compiler = new EffectCompiler { SourceDirectories = { EffectCompilerBase.DefaultSourceShaderFolder }, }; } #endif // Nothing to do remotely bool needRemoteCompiler = (compiler == null && (effectCompilationMode & EffectCompilationMode.Remote) != 0); if (needRemoteCompiler || recordEffectRequested) { // Create the object that handles the connection var shaderCompilerTarget = new RemoteEffectCompilerClient(packageId); if (recordEffectRequested) { // Let's notify effect compiler server for each new effect requested effectSystem.EffectUsed += shaderCompilerTarget.NotifyEffectUsed; } // Use remote only if nothing else was found before (i.e. a local compiler) if (needRemoteCompiler) { // Create a remote compiler compiler = new RemoteEffectCompiler(shaderCompilerTarget); } } // Local not possible or allowed, and remote not allowed either => switch back to null compiler if (compiler == null) { compiler = new NullEffectCompiler(); } return new EffectCompilerCache(compiler, taskSchedulerSelector); }
public void Initialize(IServiceRegistry services) { this.effectSystem = services.GetSafeServiceAs <EffectSystem>(); }
protected override void Initialize() { base.Initialize(); //now we probably are capable of detecting the gpu/cpu/etc so we confirm rendering settings if (AutoLoadDefaultSettings) { ConfirmRenderingSettings(); } // --------------------------------------------------------- // Add common GameSystems - Adding order is important // (Unless overriden by gameSystem.UpdateOrder) // --------------------------------------------------------- // Add the input manager Input = InputManagerFactory.NewInputManager(Services, Context); GameSystems.Add(Input); // Add the scheduler system // - Must be after Input, so that scripts are able to get latest input // - Must be before Entities/Camera/Audio/UI, so that scripts can apply // changes in the same frame they will be applied GameSystems.Add(Script); // Add the Audio System GameSystems.Add(Audio); // Add the Font system GameSystems.Add(gameFontSystem); //Add the sprite animation System GameSystems.Add(SpriteAnimation); GameSystems.Add(ProfilerSystem); EffectSystem = new EffectSystem(Services); // If requested in game settings, compile effects remotely and/or notify new shader requests if (Settings != null) { EffectSystem.Compiler = EffectSystem.CreateEffectCompiler(EffectSystem, Settings.PackageId, Settings.EffectCompilation, Settings.RecordUsedEffects); } GameSystems.Add(EffectSystem); GameSystems.Add(SceneSystem); // TODO: data-driven? Content.Serializer.RegisterSerializer(new ImageSerializer()); Content.Serializer.RegisterSerializer(new SoundEffectSerializer(Audio.AudioEngine)); Content.Serializer.RegisterSerializer(new SoundMusicSerializer(Audio.AudioEngine)); // enable multi-touch by default Input.MultiTouchEnabled = true; OnGameStarted(this); }
public void Initialize(IServiceRegistry services) { this.effectSystem = services.GetSafeServiceAs<EffectSystem>(); }
protected override void Initialize() { base.Initialize(); EffectSystem = new EffectSystem(Services); // If requested in game settings, compile effects remotely and/or notify new shader requests if (gameSettings != null) { EffectSystem.Compiler = EffectSystem.CreateEffectCompiler(EffectSystem, gameSettings.PackageId, gameSettings.EffectCompilation, gameSettings.RecordUsedEffects); } GameSystems.Add(EffectSystem); GameSystems.Add(SceneSystem); // TODO: data-driven? Asset.Serializer.RegisterSerializer(new ImageSerializer()); Asset.Serializer.RegisterSerializer(new SoundEffectSerializer(Audio.AudioEngine)); Asset.Serializer.RegisterSerializer(new SoundMusicSerializer(Audio.AudioEngine)); // enable multi-touch by default Input.MultiTouchEnabled = true; }
/// <summary> /// Initializes a new instance of the <see cref="XenkoEngine"/> class. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> /// <param name="nativeScreenWidth">Width of the native screen.</param> /// <param name="nativeScreenHeight">Height of the native screen.</param> public XenkoEngine(GraphicsDeviceManager manager, EffectSystem effectSystem) : base() { renderer = new XenkoRenderer(manager, effectSystem); }