mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added Shader base and PixelShader
This commit is contained in:
@@ -75,8 +75,9 @@ namespace GlitchyEngine
|
|||||||
|
|
||||||
VertexLayout _vertexLayout ~ delete _;
|
VertexLayout _vertexLayout ~ delete _;
|
||||||
|
|
||||||
|
PixelShader _pixelShader ~ delete _;
|
||||||
|
|
||||||
ID3D11VertexShader* _vertexShader ~ _?.Release();
|
ID3D11VertexShader* _vertexShader ~ _?.Release();
|
||||||
ID3D11PixelShader* _pixelShader ~ _?.Release();
|
|
||||||
|
|
||||||
private Vector3 CircleCoord(float angle)
|
private Vector3 CircleCoord(float angle)
|
||||||
{
|
{
|
||||||
@@ -124,26 +125,7 @@ namespace GlitchyEngine
|
|||||||
// Load pixel shader
|
// Load pixel shader
|
||||||
//
|
//
|
||||||
|
|
||||||
ID3DBlob* psCode = null;
|
_pixelShader = Shader.FromFile!<PixelShader>(_window.Context, "content\\basicShader.hlsl", "PS");
|
||||||
|
|
||||||
result = D3DCompiler.D3DCompileFromFile("content\\basicShader.hlsl".ToScopedNativeWChar!(), null, .StandardInclude, "PS", "ps_5_0", .Debug, .None, &psCode, &errorBlob);
|
|
||||||
|
|
||||||
if(result.Failed || errorBlob != null)
|
|
||||||
{
|
|
||||||
Debug.Write("ERROR: Failed to compile Pixel Shader: {}", result);
|
|
||||||
//ErrorPrinter.PrintErrorBlob(errorBlob);
|
|
||||||
Runtime.FatalError("Failed to compile Pixel Shader");
|
|
||||||
}
|
|
||||||
|
|
||||||
result = Window.Context.[Friend]nativeDevice.CreatePixelShader(psCode.GetBufferPointer(), psCode.GetBufferSize(), null, &_pixelShader);
|
|
||||||
|
|
||||||
psCode.Release();
|
|
||||||
|
|
||||||
if(result.Failed)
|
|
||||||
{
|
|
||||||
Debug.Write("ERROR: Failed to create Pixel Shader: {}", result);
|
|
||||||
Runtime.FatalError("Failed to create Pixel Shader");
|
|
||||||
}
|
|
||||||
|
|
||||||
float pO3 = Math.PI_f / 3.0f;
|
float pO3 = Math.PI_f / 3.0f;
|
||||||
VertexColor[?] vertices = .(
|
VertexColor[?] vertices = .(
|
||||||
@@ -204,11 +186,8 @@ namespace GlitchyEngine
|
|||||||
|
|
||||||
_window.Context.ClearRenderTarget(null, .(0.2f, 0.2f, 0.2f));
|
_window.Context.ClearRenderTarget(null, .(0.2f, 0.2f, 0.2f));
|
||||||
|
|
||||||
for(Layer layer in _layerStack)
|
// Draw test geometry
|
||||||
layer.Update(_gameTime);
|
{
|
||||||
|
|
||||||
_window.Update();
|
|
||||||
|
|
||||||
_window.Context.SetRenderTarget(null);
|
_window.Context.SetRenderTarget(null);
|
||||||
_window.Context.BindRenderTargets();
|
_window.Context.BindRenderTargets();
|
||||||
|
|
||||||
@@ -227,9 +206,15 @@ namespace GlitchyEngine
|
|||||||
|
|
||||||
_window.Context.SetViewport(Window.Context.SwapChain.BackbufferViewport);
|
_window.Context.SetViewport(Window.Context.SwapChain.BackbufferViewport);
|
||||||
|
|
||||||
_immediateContext.PixelShader.SetShader(_pixelShader, null, 0);
|
_window.Context.SetPixelShader(_pixelShader);
|
||||||
|
|
||||||
_window.Context.DrawIndexed(3 * 6);
|
_window.Context.DrawIndexed(3 * 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(Layer layer in _layerStack)
|
||||||
|
layer.Update(_gameTime);
|
||||||
|
|
||||||
|
_window.Update();
|
||||||
|
|
||||||
_imGuiLayer.ImGuiRender();
|
_imGuiLayer.ImGuiRender();
|
||||||
|
|
||||||
|
|||||||
@@ -165,5 +165,13 @@ namespace GlitchyEngine.Renderer
|
|||||||
{
|
{
|
||||||
nativeContext.InputAssembler.SetPrimitiveTopology((DirectX.Common.PrimitiveTopology)primitiveTopology);
|
nativeContext.InputAssembler.SetPrimitiveTopology((DirectX.Common.PrimitiveTopology)primitiveTopology);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void SetPixelShader(PixelShader pixelShader)
|
||||||
|
{
|
||||||
|
//Todo: nativeContext.PixelShader.SetSamplers();
|
||||||
|
//Todo: nativeContext.PixelShader.SetConstantBuffers();
|
||||||
|
//Todo: nativeContext.PixelShader.SetShaderResources();
|
||||||
|
nativeContext.PixelShader.SetShader(pixelShader.nativeShader);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
using System;
|
||||||
|
using DirectX.D3D11;
|
||||||
|
using DirectX.D3DCompiler;
|
||||||
|
using DirectX.D3D11Shader;
|
||||||
|
using DirectX.Common;
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
Log.EngineLogger.Error($"Failed to compile Shader: Error Code({(int)result}): {result} | Error Message: {scope String((char8*)errorBlob.GetBufferPointer(), errorBlob.GetBufferSize())}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension PixelShader
|
||||||
|
{
|
||||||
|
internal ID3D11PixelShader* nativeShader ~ _?.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, "ps_5_0", flags, let shaderBlob);
|
||||||
|
|
||||||
|
var result = _context.nativeDevice.CreatePixelShader(shaderBlob.GetBufferPointer(), shaderBlob.GetBufferSize(), null, &nativeShader);
|
||||||
|
if(result.Failed)
|
||||||
|
{
|
||||||
|
Log.EngineLogger.Error($"Failed to create pixel shader: Message ({(int)result}): {result}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Reflect(shaderBlob);
|
||||||
|
|
||||||
|
shaderBlob?.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void Reflect(ID3DBlob* shaderCode)
|
||||||
|
{
|
||||||
|
ID3D11ShaderReflection* reflection = null;
|
||||||
|
var result = D3DCompiler.D3DReflect(shaderCode.GetBufferPointer(), shaderCode.GetBufferSize(), &reflection);
|
||||||
|
if(result.Failed)
|
||||||
|
{
|
||||||
|
Log.EngineLogger.Error($"Failed to reflect pixel shader: Message ({(int)result}): {result}");
|
||||||
|
}
|
||||||
|
|
||||||
|
reflection.GetDescription(let desc);
|
||||||
|
uint32 cBufferCount = desc.ConstantBuffers;
|
||||||
|
|
||||||
|
for(uint32 i < cBufferCount)
|
||||||
|
{
|
||||||
|
var bufferReflection = reflection.GetConstantBufferByIndex(i);
|
||||||
|
bufferReflection.GetDescription(let bufferDesc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -112,5 +112,7 @@ namespace GlitchyEngine.Renderer
|
|||||||
public extern void SetVertexLayout(VertexLayout vertexLayout);
|
public extern void SetVertexLayout(VertexLayout vertexLayout);
|
||||||
|
|
||||||
public extern void SetPrimitiveTopology(PrimitiveTopology primitiveTopology);
|
public extern void SetPrimitiveTopology(PrimitiveTopology primitiveTopology);
|
||||||
|
|
||||||
|
public extern void SetPixelShader(PixelShader pixelShader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace GlitchyEngine.Renderer
|
||||||
|
{
|
||||||
|
public struct ShaderDefine
|
||||||
|
{
|
||||||
|
public String Name;
|
||||||
|
public String Definition;
|
||||||
|
|
||||||
|
public this() => this = default;
|
||||||
|
|
||||||
|
public this(String name, String definition)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Definition = definition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class Shader
|
||||||
|
{
|
||||||
|
protected GraphicsContext _context;
|
||||||
|
|
||||||
|
public GraphicsContext Context => _context;
|
||||||
|
|
||||||
|
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
CompileFromSource(source, entryPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static mixin FromFile<T>(GraphicsContext context, String fileName, String entryPoint, ShaderDefine[] macros = null) where T : Shader
|
||||||
|
{
|
||||||
|
String fileContent = new String();
|
||||||
|
|
||||||
|
File.ReadAllText(fileName, fileContent, true);
|
||||||
|
T shader = new T(context, fileContent, entryPoint, macros);
|
||||||
|
|
||||||
|
delete fileContent;
|
||||||
|
|
||||||
|
shader
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+1
-1
Submodule GlitchyEngine/vendor/imgui-beef updated: 3afaf5b07e...d6159883e1
Reference in New Issue
Block a user