From 9964a1f5ad379b274a216ab5a745e8fa00b3de3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 6 Dec 2020 00:08:55 +0100 Subject: [PATCH] Added generic Buffer --- GlitchyEngine/src/Application.bf | 10 ++++-- .../Platform/DX11/Renderer/Dx11PixelShader.bf | 3 +- GlitchyEngine/src/Renderer/Buffer.bf | 31 +++++++++++++++++++ Sandbox/content/basicShader.hlsl | 7 ++++- 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/GlitchyEngine/src/Application.bf b/GlitchyEngine/src/Application.bf index cb0a648..c7d25df 100644 --- a/GlitchyEngine/src/Application.bf +++ b/GlitchyEngine/src/Application.bf @@ -70,6 +70,7 @@ namespace GlitchyEngine VertexBuffer _vertexBuffer ~ delete _; IndexBuffer _indexBuffer ~ delete _; + Buffer _cBuffer ~ delete _; RasterizerState _rasterizerState ~ delete _; @@ -94,7 +95,6 @@ namespace GlitchyEngine if(result.Failed || errorBlob != null) { Debug.Write("ERROR: Failed to compile Vertex Shader: {}", result); - //ErrorPrinter.PrintErrorBlob(errorBlob); Runtime.FatalError("Failed to compile Vertex Shader"); } @@ -126,7 +126,12 @@ namespace GlitchyEngine // _pixelShader = Shader.FromFile!(_window.Context, "content\\basicShader.hlsl", "PS"); - + _pixelShader.[Friend]_buffers = new .[1]; + + _cBuffer = new Buffer(_window.Context, .(0, .Constant, .Immutable, .None)); + _cBuffer.Data = .White; + _cBuffer.Update(); + float pO3 = Math.PI_f / 3.0f; VertexColor[?] vertices = .( VertexColor(.Zero, Color(255,255,255)), @@ -206,6 +211,7 @@ namespace GlitchyEngine _window.Context.SetViewport(Window.Context.SwapChain.BackbufferViewport); + _immediateContext.PixelShader.SetConstantBuffers(0, 1, &_cBuffer.[Friend]nativeBuffer); _window.Context.SetPixelShader(_pixelShader); _window.Context.DrawIndexed(3 * 6); diff --git a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11PixelShader.bf b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11PixelShader.bf index 65f6b51..ad6a168 100644 --- a/GlitchyEngine/src/Platform/DX11/Renderer/Dx11PixelShader.bf +++ b/GlitchyEngine/src/Platform/DX11/Renderer/Dx11PixelShader.bf @@ -56,7 +56,7 @@ namespace GlitchyEngine.Renderer Log.EngineLogger.Error($"Failed to create pixel shader: Message ({(int)result}): {result}"); } - Reflect(shaderBlob); + //Reflect(shaderBlob); shaderBlob?.Release(); } @@ -78,6 +78,7 @@ namespace GlitchyEngine.Renderer var bufferReflection = reflection.GetConstantBufferByIndex(i); bufferReflection.GetDescription(let bufferDesc); } + reflection.Release(); } } } diff --git a/GlitchyEngine/src/Renderer/Buffer.bf b/GlitchyEngine/src/Renderer/Buffer.bf index 53f03fd..5520046 100644 --- a/GlitchyEngine/src/Renderer/Buffer.bf +++ b/GlitchyEngine/src/Renderer/Buffer.bf @@ -179,4 +179,35 @@ namespace GlitchyEngine.Renderer */ protected extern Result PlatformSetData(void* data, uint32 byteLength, uint32 dstByteOffset, MapType mapType); } + + /** + * A buffer containing an array of a struct on the GPU. + * @param T The type of the struct represented by this buffer. + */ + public class Buffer : Buffer where T : struct + { + public T Data; + + /** + * Creates a new instance of a Buffer + * @param context The graphics context. + * @param description Describes the Buffer. Note: description.Size is ignored, as it will always be sizeof(T). + */ + public this(GraphicsContext context, BufferDescription description) : base(context) + { + _description = description; + _description.Size = (uint32)sizeof(T); + } + + // Todo: perhaps add a constructor that takes all parameters from description except Size + + /** + * Uploads the date from @see Data to the GPU. + */ + [Inline] + public void Update() + { + PlatformSetData(&Data, (uint32)sizeof(T), 0, .WriteDiscard); + } + } } diff --git a/Sandbox/content/basicShader.hlsl b/Sandbox/content/basicShader.hlsl index 72c8200..5ece317 100644 --- a/Sandbox/content/basicShader.hlsl +++ b/Sandbox/content/basicShader.hlsl @@ -1,3 +1,8 @@ +cbuffer Constants +{ + float4 BaseColor; +} + struct VS_IN { float3 Position : POSITION; @@ -21,5 +26,5 @@ PS_IN VS(VS_IN input) float4 PS(PS_IN input) : SV_TARGET { - return input.Color; + return input.Color * BaseColor; } \ No newline at end of file