Moved pixel-/vertex shader to own files

This commit is contained in:
Simon Lübeß
2020-12-27 13:55:38 +01:00
parent 3a599fd21a
commit 567ef97ffb
7 changed files with 99 additions and 73 deletions
-2
View File
@@ -79,8 +79,6 @@ namespace GlitchyEngine
VertexShader _vertexShader ~ delete _;
PixelShader _pixelShader ~ delete _;
//ID3D11VertexShader* _vertexShader ~ _?.Release();
private Vector3 CircleCoord(float angle)
{
return .(Math.Cos(angle), Math.Sin(angle), 0);
@@ -8,33 +8,6 @@ using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension Shader
{
internal static void PlattformCompileShaderFromSource(String code, ShaderDefine[] macros, String entryPoint, String target, ShaderCompileFlags compileFlags, out ID3DBlob* shaderBlob)
{
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;
shaderBlob = null;
var result = D3DCompiler.D3DCompile(code.CStr(), (.)code.Length, null, nativeMacros, null, entryPoint, target, compileFlags, .None, &shaderBlob, &errorBlob);
if(result.Failed)
{
StringView str = StringView((char8*)errorBlob.GetBufferPointer(), errorBlob.GetBufferSize());
Log.EngineLogger.Error($"Failed to compile Shader: Error Code({(int)result}): {result} | Error Message: {str}");
}
}
}
extension PixelShader
{
internal ID3D11PixelShader* nativeShader ~ _?.Release();
@@ -82,32 +55,4 @@ namespace GlitchyEngine.Renderer
reflection.Release();
}
}
extension VertexShader
{
internal ID3D11VertexShader* nativeShader ~ _?.Release();
internal ID3DBlob* nativeCode ~ _?.Release();
public override void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null)
{
ShaderCompileFlags flags = .Default;
#if DEBUG
flags |= .Debug;
#else
flags |= .OptimizationLevel3;
#endif
Shader.PlattformCompileShaderFromSource(code, macros, entryPoint, "vs_5_0", flags, out nativeCode);
var result = _context.nativeDevice.CreateVertexShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, &nativeShader);
if(result.Failed)
{
Log.EngineLogger.Error($"Failed to create vertex shader: Message ({(int)result}): {result}");
}
//int i = nativeCode?.Release() ?? (uint32)-1;
}
}
}
@@ -0,0 +1,37 @@
using System;
using GlitchyEngine.Renderer;
using DirectX.Common;
using DirectX.D3D11;
using DirectX.D3DCompiler;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension Shader
{
internal static void PlattformCompileShaderFromSource(String code, ShaderDefine[] macros, String entryPoint, String target, ShaderCompileFlags compileFlags, out ID3DBlob* shaderBlob)
{
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;
shaderBlob = null;
var result = D3DCompiler.D3DCompile(code.CStr(), (.)code.Length, null, nativeMacros, null, entryPoint, target, compileFlags, .None, &shaderBlob, &errorBlob);
if(result.Failed)
{
StringView str = StringView((char8*)errorBlob.GetBufferPointer(), errorBlob.GetBufferSize());
Log.EngineLogger.Error($"Failed to compile Shader: Error Code({(int)result}): {result} | Error Message: {str}");
}
}
}
}
@@ -0,0 +1,38 @@
using System;
using GlitchyEngine.Renderer;
using DirectX.Common;
using DirectX.D3D11;
using DirectX.D3DCompiler;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension VertexShader
{
internal ID3D11VertexShader* nativeShader ~ _?.Release();
internal ID3DBlob* nativeCode ~ _?.Release();
public override void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null)
{
ShaderCompileFlags flags = .Default;
#if DEBUG
flags |= .Debug;
#else
flags |= .OptimizationLevel3;
#endif
Shader.PlattformCompileShaderFromSource(code, macros, entryPoint, "vs_5_0", flags, out nativeCode);
var result = _context.nativeDevice.CreateVertexShader(nativeCode.GetBufferPointer(), nativeCode.GetBufferSize(), null, &nativeShader);
if(result.Failed)
{
Log.EngineLogger.Error($"Failed to create vertex shader: Message ({(int)result}): {result}");
}
//int i = nativeCode?.Release() ?? (uint32)-1;
}
}
}
+12
View File
@@ -0,0 +1,12 @@
using System;
namespace GlitchyEngine.Renderer
{
public class PixelShader : Shader
{
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
: base(context, source, entryPoint, macros) { }
public override extern void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
}
}
-16
View File
@@ -43,20 +43,4 @@ namespace GlitchyEngine.Renderer
public abstract void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
}
public class PixelShader : Shader
{
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
: base(context, source, entryPoint, macros) { }
public override extern void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
}
public class VertexShader : Shader
{
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
: base(context, source, entryPoint, macros) { }
public override extern void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
}
}
@@ -0,0 +1,12 @@
using System;
namespace GlitchyEngine.Renderer
{
public class VertexShader : Shader
{
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
: base(context, source, entryPoint, macros) { }
public override extern void CompileFromSource(String code, String entryPoint, ShaderDefine[] macros = null);
}
}