Load effects via content manager + virtual InitContentManager in App

This commit is contained in:
Simon Lübeß
2023-01-08 00:17:04 +01:00
parent 3da60355df
commit 4589030da6
36 changed files with 1092 additions and 743 deletions
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -0,0 +1,4 @@
{
AssetLoader = "EffectAssetLoader",
Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}
}
@@ -4,13 +4,15 @@
_generateMipMaps = true,
_isSrgb = true,
_samplerStateDescription = {
MinFilter = .Linear,
MinFilter = .Anisotropic,
MagFilter = .Anisotropic,
MipFilter = .Anisotropic,
ComparisonFunction = .Never,
AddressModeU = .Wrap,
AddressModeV = .Border,
AddressModeW = .Clamp,
MipMaxLOD = 160,
MaxAnisotropy = 5,
MaxAnisotropy = 16,
BorderColor = {
R = 0.756863,
G = 0.2,
@@ -1,6 +1,7 @@
{
AssetLoader = "EditorTextureAssetLoader",
Config = (GlitchyEditor.Assets.EditorTextureAssetLoaderConfig){
_isSrgb = true,
_samplerStateDescription = {
MinFilter = .Linear,
MagFilter = .Linear,
@@ -0,0 +1,101 @@
using Bon;
using GlitchyEngine.Content;
using System;
using System.Collections;
using System.IO;
using GlitchyEngine;
using GlitchyEngine.Renderer;
namespace GlitchyEditor.Assets;
class EffectAssetPropertiesEditor : AssetPropertiesEditor
{
public this(AssetFile asset) : base(asset)
{
}
public override void ShowEditor()
{
}
public static AssetPropertiesEditor Factory(AssetFile assetFile)
{
return new Self(assetFile);
}
}
[BonTarget, BonPolyRegister]
class EffectAssetLoaderConfig : AssetLoaderConfig
{
}
class EffectAssetLoader : IAssetLoader //, IReloadingAssetLoader
{
private static readonly List<StringView> _fileExtensions = new .(){".hlsl"} ~ delete _;
public static List<StringView> FileExtensions => _fileExtensions;
public AssetLoaderConfig GetDefaultConfig()
{
return new EffectAssetLoaderConfig();
}
public Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView assetIdentifier, StringView? subAsset, IContentManager contentManager)
{
Effect effect = new Effect(file, assetIdentifier, contentManager);
return effect;
/*StreamReader reader = scope .(file);
String text = scope .();
reader.ReadToEnd(text);
MaterialFile materialFile = scope .();
var result = Bon.Deserialize<MaterialFile>(ref materialFile, text);
if (result case .Err)
{
Log.EngineLogger.Error("Failed to load material.");
return null;
// TODO: return error material
}
Effect fx = new Effect(materialFile.Effect);
Material material = new Material(fx);
for (let (slotName, textureIdentifier) in materialFile.Textures)
{
Texture texture = contentManager.LoadAsset(textureIdentifier) as Texture;
if (texture == null)
{
Log.EngineLogger.Error("Failed to load texture.");
// TODO: LoadAsset should return an error texture.
}
material.SetTexture(slotName, texture);
}
fx.ReleaseRef();
/*for (let (slotName, textureIdentifier) in materialFile.Variables)
{
if (texture == null)
{
Log.EngineLogger.Error("Failed to load texture.");
// TODO: LoadAsset should return an error texture.
}
material.SetVariable(slotName, );
}*/
return material; //ModelLoader.LoadMesh(file, subAsset.Value, 0);*/
}
}
@@ -52,7 +52,7 @@ class MaterialAssetLoader : IAssetLoader //, IReloadingAssetLoader
return new ModelAssetLoaderConfig();
}
public Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView? subAsset, IContentManager contentManager)
public Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView assetIdentifier, StringView? subAsset, IContentManager contentManager)
{
StreamReader reader = scope .(file);
+1 -1
View File
@@ -42,7 +42,7 @@ class ModelAssetLoader : IAssetLoader //, IReloadingAssetLoader
return new ModelAssetLoaderConfig();
}
public Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView? subAsset, IContentManager contentManager)
public Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView assetIdentifier, StringView? subAsset, IContentManager contentManager)
{
Log.EngineLogger.Assert(subAsset != null);
@@ -175,7 +175,7 @@ class EditorTextureAssetLoader : IAssetLoader, IReloadingAssetLoader
return new EditorTextureAssetLoaderConfig();
}
public Asset LoadAsset(Stream data, AssetLoaderConfig config, StringView? subAsset, IContentManager contentManager)
public Asset LoadAsset(Stream data, AssetLoaderConfig config, StringView assetIdentifier, StringView? subAsset, IContentManager contentManager)
{
var config;
+29 -1
View File
@@ -1,13 +1,41 @@
using System;
using GlitchyEngine;
using GlitchyEngine.Content;
using GlitchyEditor.Assets;
namespace GlitchyEditor
{
class EditorApp : Application
{
EditorContentManager _contentManager;
public this()
{
PushLayer(new EditorLayer());
PushLayer(new EditorLayer(_contentManager));
}
protected override IContentManager InitContentManager()
{
_contentManager = new EditorContentManager();
_contentManager.RegisterAssetLoader<EditorTextureAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<EditorTextureAssetLoader>(".png", ".dds");
_contentManager.SetAssetPropertiesEditor<EditorTextureAssetLoader>(=> TextureAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<ModelAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<ModelAssetLoader>(".glb", ".gltf");
_contentManager.SetAssetPropertiesEditor<ModelAssetLoader>(=> ModelAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<MaterialAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<MaterialAssetLoader>(".mat");
_contentManager.SetAssetPropertiesEditor<MaterialAssetLoader>(=> MaterialAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<EffectAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<EffectAssetLoader>(".hlsl");
_contentManager.SetAssetPropertiesEditor<EffectAssetLoader>(=> EffectAssetPropertiesEditor.Factory);
_contentManager.SetContentDirectory("./content");
return _contentManager;
}
[Export, LinkName("CreateApplication")]
+15 -2
View File
@@ -511,7 +511,7 @@ class EditorContentManager : IContentManager
_supportedExtensions.Add(new String(ext));
}
public void SetAsDefaultAssetLoader<T>(params StringView[] fileExtensions) where T : IAssetLoader
public void SetAsDefaultAssetLoader<T>(params Span<StringView> fileExtensions) where T : IAssetLoader
{
for (var ext in fileExtensions)
{
@@ -616,7 +616,7 @@ class EditorContentManager : IContentManager
Stream stream = GetStream(filePath);
Asset loadedAsset = assetLoader.LoadAsset(stream, file.AssetConfig.Config, subassetName, this);
Asset loadedAsset = assetLoader.LoadAsset(stream, file.AssetConfig.Config, resourceName, subassetName, this);
delete stream;
@@ -637,9 +637,22 @@ class EditorContentManager : IContentManager
// TODO: probably not needed
public Stream GetStream(StringView assetIdentifier)
{
var assetIdentifier;
if (!assetIdentifier.StartsWith(_contentDirectory))
{
String filePath = scope:: String(assetIdentifier.Length + _contentDirectory.Length + 2);
Path.Combine(filePath, _contentDirectory, assetIdentifier);
assetIdentifier = filePath;
}
FileStream fs = new FileStream();
var result = fs.Open(assetIdentifier, .Open, .Read, .ReadWrite);
if (result case .Err)
return null;
return fs;
}
+12 -5
View File
@@ -27,7 +27,7 @@ namespace GlitchyEditor
BlendState _opaqueBlendState ~ _.ReleaseRef();
DepthStencilState _depthStencilState ~ _.ReleaseRef();
Scene _scene = new Scene() ~ delete _;
Scene _scene ~ delete _;
String _sceneFilePath = new String() ~ delete _;
public String SceneFilePath
@@ -69,14 +69,17 @@ namespace GlitchyEditor
SceneState _sceneState = .Edit;
public this() : base("Example")
public this(EditorContentManager contentManager) : base("Example")
{
Application.Get().Window.IsVSync = false;
InitContentManager();
//InitContentManager();
_contentManager = contentManager;
InitGraphics();
_scene = new Scene();
_camera = EditorCamera(Vector3(3.5f, 1.25f, 2.75f), Quaternion.FromEulerAngles(MathHelper.ToRadians(40), MathHelper.ToRadians(25), 0), MathHelper.ToRadians(75), 0.1f, 1);
_camera.RenderTarget = _cameraTarget;
@@ -85,7 +88,7 @@ namespace GlitchyEditor
NewScene();
}
private void InitContentManager()
/*private void InitContentManager()
{
_contentManager = new EditorContentManager();
_contentManager.RegisterAssetLoader<EditorTextureAssetLoader>();
@@ -100,11 +103,15 @@ namespace GlitchyEditor
_contentManager.SetAsDefaultAssetLoader<MaterialAssetLoader>(".mat");
_contentManager.SetAssetPropertiesEditor<MaterialAssetLoader>(=> MaterialAssetPropertiesEditor.Factory);
_contentManager.RegisterAssetLoader<EffectAssetLoader>();
_contentManager.SetAsDefaultAssetLoader<EffectAssetLoader>(".hlsl");
_contentManager.SetAssetPropertiesEditor<EffectAssetLoader>(=> EffectAssetPropertiesEditor.Factory);
_contentManager.SetContentDirectory("./content");
// Todo: Sketchy...
Application.Get().[Friend]_contentManager = _contentManager;
}
}*/
private void InitGraphics()
{
+13 -11
View File
@@ -7,13 +7,12 @@ using GlitchyEngine.Content;
namespace GlitchyEngine
{
public class Application
public abstract class Application
{
static Application s_Instance = null;
private Window _window;
private RendererAPI _rendererApi;
private EffectLibrary _effectLibrary;
private bool _running = true;
private bool _isMinimized = false;
@@ -31,8 +30,6 @@ namespace GlitchyEngine
public bool IsRunning => _running;
public Window Window => _window;
public EffectLibrary EffectLibrary => _effectLibrary;
public IContentManager ContentManager => _contentManager;
public bool IsMinimized => _isMinimized;
@@ -57,18 +54,18 @@ namespace GlitchyEngine
Input.Init();
_contentManager = InitContentManager();
// TODO: RenderAPI in RenderCommand initialisieren?
_rendererApi = new RendererAPI();
_rendererApi.Context = _window.Context;
//_contentManager = new ContentManager("./content");
SamplerStateManager.Init();
// TODO: Rendercommmand in Renderer initialisieren?
RenderCommand.RendererAPI = _rendererApi;
_effectLibrary = new EffectLibrary();
Renderer.Init(_effectLibrary);
Renderer.Init();
#if IMGUI
_imGuiLayer = new ImGuiLayer();
@@ -79,6 +76,13 @@ namespace GlitchyEngine
Settings.Apply();
}
/// Initializes the content manager.
protected abstract IContentManager InitContentManager();
//{
// TODO: init default content manager?
//_contentManager = new ContentManager("./content");
//}
public ~this()
{
Profiler.ProfileFunction!();
@@ -86,8 +90,6 @@ namespace GlitchyEngine
SamplerStateManager.Uninit();
Renderer.Deinit();
delete _effectLibrary;
delete _contentManager;
delete _rendererApi;
+12 -7
View File
@@ -56,24 +56,24 @@ namespace GlitchyEngine.Content
/// @param config The configuration which specifies the settings used to load the asset.
/// @param contentManager The content manager used to load the asset.
/// @returns The loaded asset.
Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView? subAsset, IContentManager contentManager);
Asset LoadAsset(Stream file, AssetLoaderConfig config, StringView assetIdentifier, StringView? subAsset, IContentManager contentManager);
}
static
{
/// Loads the specified asset with the given contentManager or the current applications content manager.
public static T LoadAsset<T>(StringView assetIdentifier, IContentManager contentManager = null) where T : IRefCounted
public static T LoadAsset<T>(StringView assetIdentifier, IContentManager contentManager = null) where T : Asset
{
var contentManager;
if (contentManager == null)
contentManager = Application.Get().ContentManager;
Object o = contentManager.LoadAsset(assetIdentifier);
Asset asset = contentManager.LoadAsset(assetIdentifier);
Log.EngineLogger.AssertDebug(o is T);
Log.EngineLogger.AssertDebug(asset is T);
return (T)o;
return (T)asset;
}
}
@@ -96,7 +96,7 @@ namespace GlitchyEngine.Content
Stream GetStream(StringView assetIdentifier);
void RegisterAssetLoader<T>() where T : new, class, IAssetLoader;
void SetAsDefaultAssetLoader<T>(params StringView[] fileExtensions) where T : IAssetLoader;
void SetAsDefaultAssetLoader<T>(params Span<StringView> fileExtensions) where T : IAssetLoader;
//void GetFilePath(String outFilename, String filename);
//Stream GetFile(String filename);
@@ -104,6 +104,11 @@ namespace GlitchyEngine.Content
class RuntimeContentManager : IContentManager
{
public this()
{
Runtime.NotImplemented();
}
public Asset LoadAsset(StringView assetIdentifier)
{
Runtime.NotImplemented();
@@ -134,7 +139,7 @@ namespace GlitchyEngine.Content
Runtime.NotImplemented();
}
public void SetAsDefaultAssetLoader<T>(params StringView[] fileExtensions) where T : IAssetLoader
public void SetAsDefaultAssetLoader<T>(params Span<StringView> fileExtensions) where T : IAssetLoader
{
Runtime.NotImplemented();
}
@@ -4,6 +4,7 @@ using System;
using DirectX.D3D11;
using DirectX.D3DCompiler;
using GlitchyEngine.Platform.DX11;
using GlitchyEngine.Content;
using internal GlitchyEngine.Renderer;
using internal GlitchyEngine.Platform.DX11;
@@ -12,11 +13,11 @@ namespace GlitchyEngine.Renderer
{
extension PixelShader
{
public override void CompileFromSource(StringView code, StringView? fileName, String entryPoint, ShaderDefine[] macros = null)
public override void CompileFromSource(StringView code, StringView? fileName, String entryPoint, IContentManager contentManager, ShaderDefine[] macros = null)
{
Debug.Profiler.ProfileRendererFunction!();
Shader.PlattformCompileShaderFromSource(code, fileName, macros, entryPoint, "ps_5_0", DefaultCompileFlags, out nativeCode);
Shader.PlattformCompileShaderFromSource(code, fileName, macros, entryPoint, "ps_5_0", DefaultCompileFlags, contentManager, out nativeCode);
{
Debug.Profiler.ProfileResourceScope!("CreateNativePixelShader");
@@ -6,11 +6,106 @@ using DirectX.Common;
using DirectX.D3D11;
using DirectX.D3DCompiler;
using DirectX.D3D11Shader;
using System.IO;
using GlitchyEngine.Content;
using System.Collections;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
struct ContentManagerInclude : ID3DInclude, IDisposable
{
private VTable _vTable;
private IContentManager _contentManager;
private Dictionary<char8*, (String FileContent, void* Data, uint32 Length)> _loadedFiles;
private String _parentFileDirectory;
public this(IContentManager contentManager, String parentFileDirectory)
{
_contentManager = contentManager;
_parentFileDirectory = parentFileDirectory;
_loadedFiles = new Dictionary<char8*, (String FileContent, void* Data, uint32 Length)>();
_vTable.Open = => Open;
_vTable.Close = => Close;
_vt = &_vTable;
}
public void Dispose()
{
delete _loadedFiles;
}
public static HResult Open(ID3DInclude* self, IncludeType includeType, char8* fileName, void* parentData, void** data, uint32* bytes)
{
ContentManagerInclude* includer = (.)self;
if (includer._loadedFiles.TryGetValue(fileName, let value))
{
*data = value.Data;
*bytes = value.Length;
return .S_OK;
}
String pathNextToParent = scope .();
Path.Combine(pathNextToParent, includer._parentFileDirectory, StringView(fileName));
Stream fileStream = Application.Get().ContentManager.GetStream(pathNextToParent);
if (fileStream == null)
{
fileStream = Application.Get().ContentManager.GetStream(StringView(fileName));
}
if (fileStream == null)
{
Log.EngineLogger.Error($"Failed to include file \"{fileName}\"");
return .E_FILENOTFOUND;
}
String fileContent = new String();
{
StreamReader reader = scope .(fileStream);
reader.ReadToEnd(fileContent);
includer._loadedFiles.Add(fileName, (fileContent, fileContent.Ptr, (uint32)fileContent.Length));
}
delete fileStream;
*data = (void*)fileContent.Ptr;
*bytes = (uint32)fileContent.Length;
return .S_OK;
}
public static HResult Close(ID3DInclude* self, void** data)
{
ContentManagerInclude* includer = (.)self;
for (var v in includer._loadedFiles)
{
if (v.value.Data == data)
{
delete v.value.FileContent;
includer._loadedFiles.Remove(v.key);
}
}
return .S_OK;
}
}
extension Shader
{
internal ID3D11DeviceChild* nativeShader ~ _?.Release();
@@ -27,7 +122,7 @@ namespace GlitchyEngine.Renderer
.OptimizationLevel3;
#endif
internal static void PlattformCompileShaderFromSource(StringView code, StringView? fileName, ShaderDefine[] macros, String entryPoint, String target, ShaderCompileFlags compileFlags, out ID3DBlob* shaderBlob)
internal static void PlattformCompileShaderFromSource(StringView code, StringView? fileName, ShaderDefine[] macros, String entryPoint, String target, ShaderCompileFlags compileFlags, IContentManager contentManager, out ID3DBlob* shaderBlob)
{
Debug.Profiler.ProfileResourceFunction!();
@@ -44,13 +139,23 @@ namespace GlitchyEngine.Renderer
ID3DBlob* errorBlob = null;
String directory = scope .();
Path.GetDirectoryPath(fileName.Value, directory);
using (ContentManagerInclude includer = .(contentManager, directory))
{
//ID3DInclude.StandardInclude
shaderBlob = null;
var result = D3DCompiler.D3DCompile(code.Ptr, (.)code.Length, fileName?.ToScopeCStr!(), nativeMacros, ID3DInclude.StandardInclude, entryPoint, target, compileFlags, .None, &shaderBlob, &errorBlob);
var result = D3DCompiler.D3DCompile(code.Ptr, (.)code.Length, fileName?.ToScopeCStr!(), nativeMacros, &includer, entryPoint, target, compileFlags, .None, &shaderBlob, &errorBlob);
if(result.Failed)
{
StringView str = StringView((char8*)errorBlob.GetBufferPointer(), (int)errorBlob.GetBufferSize());
Log.EngineLogger.Error($"Failed to compile Shader: Error Code({(int)result}): {result} | Error Message: {str}");
}
}
Log.EngineLogger.Assert(shaderBlob != null, "Shader compilation failed.");
}
@@ -5,6 +5,7 @@ using GlitchyEngine.Renderer;
using DirectX.D3D11;
using DirectX.D3DCompiler;
using GlitchyEngine.Platform.DX11;
using GlitchyEngine.Content;
using internal GlitchyEngine.Renderer;
using internal GlitchyEngine.Platform.DX11;
@@ -13,11 +14,11 @@ namespace GlitchyEngine.Renderer
{
extension VertexShader
{
public override void CompileFromSource(StringView code, StringView? fileName, String entryPoint, ShaderDefine[] macros = null)
public override void CompileFromSource(StringView code, StringView? fileName, String entryPoint, IContentManager contentManager = null, ShaderDefine[] macros = null)
{
Debug.Profiler.ProfileResourceFunction!();
Shader.PlattformCompileShaderFromSource(code, fileName, macros, entryPoint, "vs_5_0", DefaultCompileFlags, out nativeCode);
Shader.PlattformCompileShaderFromSource(code, fileName, macros, entryPoint, "vs_5_0", DefaultCompileFlags, contentManager, out nativeCode);
{
Debug.Profiler.ProfileResourceScope!("CreateNativeVertexShader");
+46 -28
View File
@@ -3,9 +3,11 @@ using System.IO;
using System.Collections;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
using GlitchyEngine.Content;
namespace GlitchyEngine.Renderer
{
namespace GlitchyEngine.Renderer;
/*/// Obsolete because of the ContentManager?
public class EffectLibrary
{
private Dictionary<String, Effect> _effects = new .() ~ delete _;
@@ -100,17 +102,16 @@ namespace GlitchyEngine.Renderer
}
public bool Exists(String effectName) => _effects.ContainsKey(effectName);
}
}*/
public class Effect : RefCounter
public class Effect : Asset
{
internal VertexShader _vs ~ _?.ReleaseRef();
internal PixelShader _ps ~ _?.ReleaseRef();
protected String _name ~ delete _;
typealias VariableDesc = Dictionary<String, Dictionary<String, Variant>>;
protected VariableDesc _variableDescriptions ~ {
protected VariableDesc _variableDescriptions = new .() ~ {
for (var (key, value) in _)
{
delete key;
@@ -127,7 +128,7 @@ namespace GlitchyEngine.Renderer
delete _;
};
protected Dictionary<String, String> _engineBuffers ~ DeleteDictionaryAndKeysAndValues!(_);
protected Dictionary<String, String> _engineBuffers = new .() ~ DeleteDictionaryAndKeysAndValues!(_);
BufferCollection _bufferCollection ~ _.ReleaseRef();
@@ -153,9 +154,8 @@ namespace GlitchyEngine.Renderer
public BufferCollection Buffers => _bufferCollection;
public BufferVariableCollection Variables => _variables;
public String Name => _name;
public this(String filename, String shaderName = null)
[Obsolete("", false)]
public this(String filename)
{
Debug.Profiler.ProfileResourceFunction!();
@@ -163,24 +163,26 @@ namespace GlitchyEngine.Renderer
String vsName = scope String();
String psName = scope String();
_variableDescriptions = new VariableDesc();
_engineBuffers = new Dictionary<String, String>();
ProcessFile(filename, fileContent, vsName, psName, _variableDescriptions, _engineBuffers);
Compile(fileContent, filename, vsName, psName);
MergeResources();
}
if(shaderName == null)
public this(Stream data, StringView assetIdentifier, IContentManager contentManager)
{
_name = new String();
Path.GetFileNameWithoutExtension(filename, _name);
}
else
{
_name = new String(shaderName);
}
Debug.Profiler.ProfileResourceFunction!();
String fileContent = scope String();
String vsName = scope String();
String psName = scope String();
ProcessStream(data, fileContent, vsName, psName, _variableDescriptions, _engineBuffers);
Compile(fileContent, assetIdentifier, vsName, psName, contentManager);
MergeResources();
}
public ~this()
@@ -273,7 +275,7 @@ namespace GlitchyEngine.Renderer
RenderCommand.BindPixelShader(_ps);
}
private void CompileFromFile(String filename, String vsEntry, String psEntry)
/*private void CompileFromFile(String filename, String vsEntry, String psEntry)
{
Debug.Profiler.ProfileResourceFunction!();
@@ -283,17 +285,17 @@ namespace GlitchyEngine.Renderer
let ps = Shader.FromFile!<PixelShader>(filename, psEntry);
PixelShader = ps;
ps.ReleaseRef();
}
}*/
private void Compile(String fileContent, String fileName, String vsEntry, String psEntry)
private void Compile(String fileContent, StringView fileName, String vsEntry, String psEntry, IContentManager contentManager = null)
{
Debug.Profiler.ProfileResourceFunction!();
// TODO: vsEntry and psEntry could be empty (which is a valid case.)
let vs = new VertexShader(fileContent, (StringView)fileName, vsEntry);
let vs = new VertexShader(fileContent, fileName, vsEntry, contentManager);
VertexShader = vs;
vs.ReleaseRef();
let ps = new PixelShader(fileContent, (StringView)fileName, psEntry);
let ps = new PixelShader(fileContent, fileName, psEntry, contentManager);
PixelShader = ps;
ps.ReleaseRef();
}
@@ -381,6 +383,18 @@ namespace GlitchyEngine.Renderer
return .Ok((startOfLine, endOfLine));
}
private static void ProcessStream(Stream rawData, String fileContent, String outVsName, String outPsName, VariableDesc outVarDescs, Dictionary<String, String> outEngineBuffers)
{
Debug.Profiler.ProfileResourceFunction!();
StreamReader streamReader = scope .(rawData);
streamReader.ReadToEnd(fileContent);
// append line ending just in case the file doesn't end with one.
fileContent.Append('\n');
ProcessFileContent(fileContent, outVsName, outPsName, outVarDescs, outEngineBuffers);
}
/**
* Loads the effect file and extracts the names of the vertex- and pixel-shader.
* @param filename The path of the effect file.
@@ -397,7 +411,13 @@ namespace GlitchyEngine.Renderer
// append line ending just in case the file doesn't end with one.
fileContent.Append('\n');
ProcessFileContent(fileContent, outVsName, outPsName, outVarDescs, outEngineBuffers);
}
private static void ProcessFileContent(String fileContent, String outVsName, String outPsName, VariableDesc outVarDescs, Dictionary<String, String> outEngineBuffers)
{
Debug.Profiler.ProfileResourceFunction!();
Dictionary<StringView, StringView> arguments = scope .();
int index = 0;
@@ -439,7 +459,6 @@ namespace GlitchyEngine.Renderer
}
}
}
}
private static void ProcessEngineBuffer(Dictionary<StringView, StringView> arguments, Dictionary<String, String> outEngineBuffers)
{
@@ -732,4 +751,3 @@ namespace GlitchyEngine.Renderer
}
}
}
}
+3 -2
View File
@@ -1,11 +1,12 @@
using System;
using GlitchyEngine.Content;
namespace GlitchyEngine.Renderer
{
public class PixelShader : Shader
{
[AllowAppend]
public this(StringView code, StringView? fileName, String entryPoint, ShaderDefine[] macros = null)
: base(code, fileName, entryPoint, macros) { }
public this(StringView code, StringView? fileName, String entryPoint, IContentManager contentManager, ShaderDefine[] macros = null)
: base(code, fileName, entryPoint, contentManager, macros) { }
}
}
+8 -8
View File
@@ -104,7 +104,7 @@ namespace GlitchyEngine.Renderer
private Vector3 _padding;
}
public static void Init(EffectLibrary effectLibrary)
public static void Init()
{
Debug.Profiler.ProfileFunction!();
@@ -112,8 +112,8 @@ namespace GlitchyEngine.Renderer
Renderer2D.Init();
FullscreenQuad.Init();
InitLineRenderer(effectLibrary);
InitDeferredRenderer(effectLibrary);
InitLineRenderer();
InitDeferredRenderer();
}
public static void Deinit()
@@ -128,11 +128,11 @@ namespace GlitchyEngine.Renderer
Renderer2D.Deinit();
}
static void InitLineRenderer(EffectLibrary effectLibrary)
static void InitLineRenderer()
{
Debug.Profiler.ProfileFunction!();
LineEffect = effectLibrary.Load("content\\Shaders\\lineShader.hlsl");
LineEffect = Content.LoadAsset<Effect>("Shaders\\lineShader.hlsl");
LineGeometry = new GeometryBinding();
LineGeometry.SetPrimitiveTopology(.LineList);
@@ -161,10 +161,10 @@ namespace GlitchyEngine.Renderer
LineEffect.ReleaseRef();
}
static void InitDeferredRenderer(EffectLibrary effectLibrary)
static void InitDeferredRenderer()
{
TestFullscreenEffect = effectLibrary.Load("content\\Shaders\\simpleLight.hlsl");
s_tonemappingEffect = effectLibrary.Load("content\\Shaders\\SimpleTonemapping.hlsl");
TestFullscreenEffect = Content.LoadAsset<Effect>("Shaders\\simpleLight.hlsl");
s_tonemappingEffect = Content.LoadAsset<Effect>("Shaders\\SimpleTonemapping.hlsl");
_gBuffer = new GBuffer();
BlendStateDescription gBufferBlendDesc = .Default;
+7 -6
View File
@@ -2,6 +2,7 @@ using System;
using System.IO;
using System.Collections;
using GlitchyEngine.Core;
using GlitchyEngine.Content;
namespace GlitchyEngine.Renderer
{
@@ -30,7 +31,7 @@ namespace GlitchyEngine.Renderer
public ShaderTextureCollection Textures => _textures;
[AllowAppend]
public this(StringView code, StringView? fileName, String entryPoint, ShaderDefine[] macros = null)
public this(StringView code, StringView? fileName, String entryPoint, IContentManager contentManager, ShaderDefine[] macros = null)
{
Debug.Profiler.ProfileResourceFunction!();
@@ -39,7 +40,7 @@ namespace GlitchyEngine.Renderer
_buffers = new BufferCollection();
_textures = new ShaderTextureCollection();
CompileFromSource(code, fileName, entryPoint);
CompileFromSource(code, fileName, entryPoint, contentManager);
}
public ~this()
@@ -47,19 +48,19 @@ namespace GlitchyEngine.Renderer
Debug.Profiler.ProfileResourceFunction!();
}
public static mixin FromFile<T>(String fileName, String entryPoint, ShaderDefine[] macros = null) where T : Shader
/*public static mixin FromFile<T>(String fileName, String entryPoint, IContentManager contentManager, ShaderDefine[] macros = null) where T : Shader
{
Debug.Profiler.ProfileResourceFunction!();
String fileContent = new String();
File.ReadAllText(fileName, fileContent, true);
T shader = new T(fileContent, (StringView)fileName, entryPoint, macros);
T shader = new T(fileContent, (StringView)fileName, contentManager, entryPoint, macros);
delete fileContent;
shader
}
}*/
public abstract void CompileFromSource(StringView code, StringView? fileName, String entryPoint, ShaderDefine[] macros = null);
public abstract void CompileFromSource(StringView code, StringView? fileName, String entryPoint, IContentManager contentManager, ShaderDefine[] macros = null);
}
}
+3 -2
View File
@@ -1,11 +1,12 @@
using System;
using GlitchyEngine.Content;
namespace GlitchyEngine.Renderer
{
public class VertexShader : Shader
{
[AllowAppend]
public this(StringView code, StringView? fileName, String entryPoint, ShaderDefine[] macros = null)
: base(code, fileName, entryPoint, macros) { }
public this(StringView code, StringView? fileName, String entryPoint, IContentManager contentManager = null, ShaderDefine[] macros = null)
: base(code, fileName, entryPoint, contentManager, macros) { }
}
}
+2 -1
View File
@@ -4,6 +4,7 @@ using System;
using System.Collections;
using Box2D;
using GlitchyEngine.Core;
using GlitchyEngine.Content;
namespace GlitchyEngine.World
{
@@ -75,7 +76,7 @@ namespace GlitchyEngine.World
DepthTargetDescription = .(.D24_UNorm_S8_UInt)
});
_gammaCorrectEffect = Application.Get().EffectLibrary.Load("content/Shaders/GammaCorrect.hlsl");
_gammaCorrectEffect = Content.LoadAsset<Effect>("Shaders/GammaCorrect.hlsl");//Application.Get().EffectLibrary.Load("content/Shaders/GammaCorrect.hlsl");
}
public ~this()
+7 -7
View File
@@ -85,13 +85,13 @@ namespace Sandbox
_context = Application.Get().Window.Context..AddRef();
var effectLibrary = Application.Get().EffectLibrary;
//var effectLibrary = Application.Get().EffectLibrary;
effectLibrary.LoadNoRefInc("content\\Shaders\\basicShader.hlsl");
//effectLibrary.LoadNoRefInc("content\\Shaders\\basicShader.hlsl");
effectLibrary.LoadNoRefInc("content\\Shaders\\testShader.hlsl");
//effectLibrary.LoadNoRefInc("content\\Shaders\\testShader.hlsl");
var textureEffect = effectLibrary.Load("content\\Shaders\\textureShader.hlsl");
var textureEffect = Content.LoadAsset<Effect>("Shaders\\textureShader.hlsl");
_depthTarget = new DepthStencilTarget(_context.SwapChain.Width, _context.SwapChain.Height);
@@ -214,7 +214,7 @@ namespace Sandbox
void TestLoadModel()
{
var testEffect = Application.Get().EffectLibrary.Get("testShader");
var testEffect = Content.LoadAsset<Effect>("Shaders\\testShader.hlsl");//Application.Get().EffectLibrary.Get("testShader");
var materialTestMaterial = new Material(testEffect);
animationMat = materialTestMaterial;
@@ -266,7 +266,7 @@ namespace Sandbox
_world.Register<CameraComponent>();
_world.Register<AnimationComponent>();
var basicEffect = Application.Get().EffectLibrary.Get("basicShader");
var basicEffect = Content.LoadAsset<Effect>("Shaders\\basicShader.hlsl");//Application.Get().EffectLibrary.Get("basicShader");
testMaterial1 = new Material(basicEffect);
testMaterial1.SetVariable("BaseColor", _squareColor0);
@@ -352,7 +352,7 @@ namespace Sandbox
RenderCommand.SetBlendState(_opaqueBlendState);
var basicEffect = Application.Get().EffectLibrary.Get("basicShader");
var basicEffect = Content.LoadAsset<Effect>("Shaders\\basicShader.hlsl");//Application.Get().EffectLibrary.Get("basicShader");
RenderCommand.SetRasterizerState(_rasterizerState);
+5
View File
@@ -32,5 +32,10 @@ namespace Sandbox
{
return new SandboxApp();
}
protected override IContentManager InitContentManager()
{
Runtime.NotImplemented();
}
}
}