Removed old shader loading stuff

This commit is contained in:
Simon Lübeß
2024-08-22 23:42:16 +02:00
parent 73a6cc0854
commit ff1c6db222
10 changed files with 2 additions and 982 deletions
@@ -1,94 +0,0 @@
#if GE_GRAPHICS_DX11
using DirectX.Common;
using DirectX.D3D11Shader;
using System;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension BufferVariable
{
internal this(ConstantBuffer constantBuffer, ID3D11ShaderReflectionVariable* variableReflection)
{
Debug.Profiler.ProfileResourceFunction!();
_constantBuffer = constantBuffer;
HResult result = variableReflection.GetDescription(let variableDescription);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to get variable description. Error({(int)result}): {result}");
_name = new String(variableDescription.Name);
_offset = variableDescription.StartOffset;
_sizeInBytes = variableDescription.Size;
_isUsed = variableDescription.uFlags.HasFlag(.Used);
let variableType = variableReflection.GetVariableType();
result = variableType.GetDescription(let shaderTypeDescription);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to get variable type description. Error({(int)result}): {result}");
switch(shaderTypeDescription.Type)
{
case .Bool:
_type = .Bool;
case .Float:
_type = .Float;
case .Int:
_type = .Int;
case .UInt:
_type = .UInt;
default:
Log.EngineLogger.Assert(false, scope $"Unhandled shader variable type: {shaderTypeDescription.Type}");
}
_columns = shaderTypeDescription.Columns;
_rows = shaderTypeDescription.Rows;
_elements = shaderTypeDescription.Elements;
SetRawData(variableDescription.DefaultValue);
}
}
extension ConstantBuffer
{
internal this(ID3D11ShaderReflectionConstantBuffer* bufferReflection)
{
Debug.Profiler.ProfileResourceFunction!();
Reflect(bufferReflection);
ConstructBuffer();
Update();
}
private void Reflect(ID3D11ShaderReflectionConstantBuffer* bufferReflection)
{
Debug.Profiler.ProfileResourceFunction!();
HResult result = bufferReflection.GetDescription(let bufferDescription);
Log.EngineLogger.Assert(result.Succeeded, scope $"Failed to get buffer description. Error({(int)result}): {result}");
Log.EngineLogger.AssertDebug(bufferDescription.Type == .D3D_CT_CBUFFER, "The buffer is not of type \"D3D_CT_CBUFFER\"");
_name = new String(bufferDescription.Name);
rawData = new uint8[bufferDescription.Size];
// Flags seem to be irrelevant for us here
for(uint32 v = 0; v < bufferDescription.Variables; v++)
{
ID3D11ShaderReflectionVariable* variableReflection = bufferReflection.GetVariableByIndex(v);
BufferVariable variable = new BufferVariable(this, variableReflection);
AddVariable(variable);
}
}
}
}
#endif
@@ -1,37 +0,0 @@
#if GE_GRAPHICS_DX11
using System;
using DirectX.D3D11;
using DirectX.D3DCompiler;
using GlitchyEngine.Platform.DX11;
using GlitchyEngine.Content;
using internal GlitchyEngine.Renderer;
using internal GlitchyEngine.Platform.DX11;
namespace GlitchyEngine.Renderer
{
extension PixelShader
{
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, contentManager, out nativeCode);
{
Debug.Profiler.ProfileResourceScope!("CreateNativePixelShader");
var result = NativeDevice.CreatePixelShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, (ID3D11PixelShader**)&nativeShader);
if(result.Failed)
{
Log.EngineLogger.Error($"Failed to create pixel shader: Message ({(int)result}): {result}");
}
}
Reflect(nativeCode);
}
}
}
#endif
@@ -26,98 +26,6 @@ using internal GlitchyEngine.Platform.DX11;
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.Instance.ContentManager.GetStream(pathNextToParent);
if (fileStream == null)
{
fileStream = Application.Instance.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();
@@ -160,130 +68,6 @@ namespace GlitchyEngine.Renderer
return .Ok;
}
protected const ShaderCompileFlags DefaultCompileFlags = .EnableStrictness |
#if DEBUG
.Debug;
#else
.OptimizationLevel3;
#endif
internal static void PlattformCompileShaderFromSource(StringView code, StringView? fileName, ShaderDefine[] macros, String entryPoint, String target, ShaderCompileFlags compileFlags, IContentManager contentManager, out ID3DBlob* shaderBlob)
{
Debug.Profiler.ProfileResourceFunction!();
ShaderMacro* nativeMacros = macros == null ? null : new:ScopedAlloc! ShaderMacro[macros.Count]*;
for(int i < macros?.Count ?? 0)
{
nativeMacros[i].Name = macros[i].Name.ToScopedNativeWChar!();
nativeMacros[i].Definition = macros[i].Definition.ToScopedNativeWChar!();
}
// Todo: sourceName, includes,
// Todo: variable shader target?
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, &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.");
}
protected internal void Reflect(ID3DBlob* shaderCode)
{
Debug.Profiler.ProfileResourceFunction!();
ID3D11ShaderReflection* reflection = null;
{
Debug.Profiler.ProfileResourceScope!("D3DReflect");
var result = D3DCompiler.D3DReflect(shaderCode.GetBufferPointer(), shaderCode.GetBufferSize(), &reflection);
if(result.Failed)
{
Log.EngineLogger.Error($"Failed to reflect shader: Message ({(int)result}): {result}");
}
}
reflection.GetDescription(let desc);
uint32 resourceCount = desc.BoundResources;
for (uint32 i < resourceCount)
{
var res = reflection.GetResourceBindingDescription(i, let bindDesc);
if (res.Failed)
{
Log.EngineLogger.Error($"Error({(int)res}) {res}: Failed to get resource binding desc for resource {i}");
continue;
}
switch(bindDesc.Type)
{
case .ConstantBuffer:
var bufferReflection = reflection.GetConstantBufferByName(bindDesc.Name);
bufferReflection.GetDescription(let bufferDesc);
// ConstantBuffer
if(bufferDesc.Type == .D3D11_CT_CBUFFER)
{
let buffer = new ConstantBuffer(bufferReflection);
_buffers.Add(bindDesc.BindPoint, buffer.Name, buffer);
buffer.ReleaseRef();
}
case .Texture:
TextureDimension texDim;
switch (bindDesc.Dimension)
{
case .Texture1D:
texDim = .Texture1D;
case .Texture1DArray:
texDim = .Texture1DArray;
case .Texture2D:
texDim = .Texture2D;
case .Texture2DArray:
texDim = .Texture2DArray;
case .Texture3D:
texDim = .Texture3D;
case .TextureCube:
texDim = .TextureCube;
case .TextureCubeArray:
texDim = .TextureCubeArray;
default:
texDim = .Unknown;
}
_textures.Add(scope String(bindDesc.Name), bindDesc.BindPoint, TextureViewBinding(null, null), texDim);
case .Sampler:
// TODO: do we have to do something for samplers?
default:
Log.EngineLogger.Warning($"Unhandled shader resource type: \"{bindDesc.Type}\"");
}
}
reflection.Release();
}
}
}
@@ -1,38 +0,0 @@
#if GE_GRAPHICS_DX11
using System;
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;
namespace GlitchyEngine.Renderer
{
extension VertexShader
{
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, contentManager, out nativeCode);
{
Debug.Profiler.ProfileResourceScope!("CreateNativeVertexShader");
var result = NativeDevice.CreateVertexShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, (ID3D11VertexShader**)&nativeShader);
if(result.Failed)
{
Log.EngineLogger.Error($"Failed to create vertex shader: Message ({(int)result}): {result}");
}
}
Reflect(nativeCode);
}
}
}
#endif