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
@@ -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,12 +139,22 @@ namespace GlitchyEngine.Renderer
ID3DBlob* errorBlob = null;
shaderBlob = null;
var result = D3DCompiler.D3DCompile(code.Ptr, (.)code.Length, fileName?.ToScopeCStr!(), nativeMacros, ID3DInclude.StandardInclude, entryPoint, target, compileFlags, .None, &shaderBlob, &errorBlob);
if(result.Failed)
String directory = scope .();
Path.GetDirectoryPath(fileName.Value, directory);
using (ContentManagerInclude includer = .(contentManager, directory))
{
StringView str = StringView((char8*)errorBlob.GetBufferPointer(), (int)errorBlob.GetBufferSize());
Log.EngineLogger.Error($"Failed to compile Shader: Error Code({(int)result}): {result} | Error Message: {str}");
//ID3DInclude.StandardInclude
shaderBlob = null;
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");