Skip to content

Commit

Permalink
[Rendering] EffectSystem: Moved CreateEffectCompiler in a separate st…
Browse files Browse the repository at this point in the history
…atic class outside of Rendering code
  • Loading branch information
xen2 committed Feb 15, 2019
1 parent 0cc2134 commit b07e779
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 55 deletions.
Expand Up @@ -57,7 +57,7 @@ public GameStudioBuilderService(SessionViewModel sessionViewModel, GameSettingsP
// so that database created with MountDatabase also have all the newest shaders.
taskScheduler = new EffectPriorityScheduler(ThreadPriority.BelowNormal, Math.Max(1, Environment.ProcessorCount / 2));
TaskSchedulerSelector taskSchedulerSelector = (mixinTree, compilerParameters) => taskScheduler.GetOrCreatePriorityGroup(compilerParameters?.TaskPriority ?? 0);
effectCompiler = (EffectCompilerBase)EffectSystem.CreateEffectCompiler(MicrothreadLocalDatabases.GetSharedDatabase(), taskSchedulerSelector: taskSchedulerSelector);
effectCompiler = (EffectCompilerBase)EffectCompilerFactory.CreateEffectCompiler(MicrothreadLocalDatabases.GetSharedDatabase(), taskSchedulerSelector: taskSchedulerSelector);

StartPushNotificationsTask();
}
Expand Down
Expand Up @@ -137,7 +137,7 @@ public ThumbnailGenerator(EffectCompilerBase effectCompiler)
EffectSystem.Initialize();

// Mount the same database for the cache
EffectSystem.Compiler = EffectSystem.CreateEffectCompiler(effectCompiler.FileProvider, EffectSystem);
EffectSystem.Compiler = EffectCompilerFactory.CreateEffectCompiler(effectCompiler.FileProvider, EffectSystem);

// Deactivate the asynchronous effect compilation
((EffectCompilerCache)EffectSystem.Compiler).CompileEffectAsynchronously = false;
Expand Down
2 changes: 1 addition & 1 deletion sources/engine/Xenko.Assets/Skyboxes/SkyboxGenerator.cs
Expand Up @@ -41,7 +41,7 @@ public SkyboxGeneratorContext(SkyboxAsset skybox, IDatabaseFileProviderService f
Services.AddService(graphicsContext);

EffectSystem = new EffectSystem(Services);
EffectSystem.Compiler = EffectSystem.CreateEffectCompiler(Content.FileProvider, EffectSystem);
EffectSystem.Compiler = EffectCompilerFactory.CreateEffectCompiler(Content.FileProvider, EffectSystem);

Services.AddService(EffectSystem);
EffectSystem.Initialize();
Expand Down
Expand Up @@ -7,8 +7,9 @@
namespace Xenko.Engine.Design
{
/// <summary>
/// Defines how <see cref="EffectSystem.CreateEffectCompiler"/> tries to create compiler.
/// Defines how <see cref="EffectCompilerFactory.CreateEffectCompiler"/> tries to create compiler.
/// </summary>
[Flags]
public enum EffectCompilationMode
{
/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion sources/engine/Xenko.Engine/Engine/Game.cs
Expand Up @@ -21,6 +21,7 @@
using Xenko.Rendering;
using Xenko.Rendering.Fonts;
using Xenko.Rendering.Sprites;
using Xenko.Shaders.Compiler;
using Xenko.Streaming;
using Xenko.VirtualReality;

Expand Down Expand Up @@ -370,7 +371,7 @@ protected override void Initialize()
Services.AddService(EffectSystem);

// If requested in game settings, compile effects remotely and/or notify new shader requests
EffectSystem.Compiler = EffectSystem.CreateEffectCompiler(Content.FileProvider, EffectSystem, Settings?.PackageName, Settings?.EffectCompilation ?? EffectCompilationMode.Local, Settings?.RecordUsedEffects ?? false);
EffectSystem.Compiler = EffectCompilerFactory.CreateEffectCompiler(Content.FileProvider, EffectSystem, Settings?.PackageName, Settings?.EffectCompilation ?? EffectCompilationMode.Local, Settings?.RecordUsedEffects ?? false);

// Setup shader compiler settings from a compilation mode.
// TODO: We might want to provide overrides on the GameSettings to specify debug and/or optim level specifically.
Expand Down
50 changes: 0 additions & 50 deletions sources/engine/Xenko.Engine/Rendering/EffectSystem.cs
Expand Up @@ -405,55 +405,5 @@ protected CompilerResults GetShaderFromParameters(string effectName, CompilerPar

return null;
}

public static IEffectCompiler CreateEffectCompiler(IVirtualFileProvider fileProvider, EffectSystem effectSystem = null, string packageName = null, EffectCompilationMode effectCompilationMode = EffectCompilationMode.Local, bool recordEffectRequested = false, TaskSchedulerSelector taskSchedulerSelector = null)
{
EffectCompilerBase compiler = null;

#if XENKO_EFFECT_COMPILER
if ((effectCompilationMode & EffectCompilationMode.Local) != 0)
{
// Local allowed and available, let's use that
compiler = new EffectCompiler(fileProvider)
{
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(packageName);

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(fileProvider, 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(fileProvider);
}

return new EffectCompilerCache(compiler, taskSchedulerSelector);
}
}
}
@@ -0,0 +1,65 @@
// Copyright (c) Xenko contributors (https://xenko.com)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Text;
using Xenko.Core;
using Xenko.Core.IO;
using Xenko.Engine.Design;
using Xenko.Rendering;

namespace Xenko.Shaders.Compiler
{
public static class EffectCompilerFactory
{
public static IEffectCompiler CreateEffectCompiler(IVirtualFileProvider fileProvider, EffectSystem effectSystem = null, string packageName = null, EffectCompilationMode effectCompilationMode = EffectCompilationMode.Local, bool recordEffectRequested = false, TaskSchedulerSelector taskSchedulerSelector = null)
{
EffectCompilerBase compiler = null;

#if XENKO_EFFECT_COMPILER
if ((effectCompilationMode & EffectCompilationMode.Local) != 0)
{
// Local allowed and available, let's use that
compiler = new EffectCompiler(fileProvider)
{
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(packageName);

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(fileProvider, 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(fileProvider);
}

return new EffectCompilerCache(compiler, taskSchedulerSelector);
}
}
}

0 comments on commit b07e779

Please sign in to comment.