Added Buffers to Shaders

This commit is contained in:
Simon Lübeß
2020-12-30 14:47:29 +01:00
parent f3c5ac3d60
commit 9a010f7c4a
8 changed files with 183 additions and 13 deletions
+4 -4
View File
@@ -100,12 +100,15 @@ namespace GlitchyEngine
//
_pixelShader = Shader.FromFile!<PixelShader>(_window.Context, "content\\basicShader.hlsl", "PS");
_pixelShader.[Friend]_buffers = new .[1];
//Todo: _pixelShader.[Friend]_buffers = new .[1];
_cBuffer = new Buffer<ColorRGBA>(_window.Context, .(0, .Constant, .Immutable, .None));
_cBuffer.Data = .White;
_cBuffer.Update();
_pixelShader.Buffers.ReplaceBuffer("Constants", _cBuffer);
float pO3 = Math.PI_f / 3.0f;
VertexColor[?] vertices = .(
VertexColor(.Zero, Color(255,255,255)),
@@ -171,15 +174,12 @@ namespace GlitchyEngine
_window.Context.SetPrimitiveTopology(.TriangleList);
var _immediateContext = _window.Context.[Friend]nativeContext;
_window.Context.SetVertexShader(_vertexShader);
_window.Context.SetRasterizerState(_rasterizerState);
_window.Context.SetViewport(Window.Context.SwapChain.BackbufferViewport);
_immediateContext.PixelShader.SetConstantBuffers(0, 1, &_cBuffer.[Friend]nativeBuffer);
_window.Context.SetPixelShader(_pixelShader);
_window.Context.DrawIndexed(3 * 6);
@@ -127,9 +127,4 @@ namespace GlitchyEngine.Renderer
return .Ok;
}
}
public extension VertexBuffer<T>
{
}
}
@@ -0,0 +1,19 @@
using DirectX.D3D11;
using internal GlitchyEngine.Renderer;
namespace GlitchyEngine.Renderer
{
extension BufferCollection
{
internal ID3D11Buffer*[DirectX.D3D11.D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT] nativeBuffers;
internal void PlatformFetchNativeBuffers()
{
for(let buffer in _buffers)
{
nativeBuffers[buffer.Index] = buffer.Buffer.nativeBuffer;
}
}
}
}
@@ -177,7 +177,8 @@ namespace GlitchyEngine.Renderer
public override void SetPixelShader(PixelShader pixelShader)
{
//Todo: nativeContext.PixelShader.SetSamplers();
//Todo: nativeContext.PixelShader.SetConstantBuffers();
pixelShader.Buffers.PlatformFetchNativeBuffers();
nativeContext.PixelShader.SetConstantBuffers(0, pixelShader.Buffers.nativeBuffers.Count, &pixelShader.Buffers.nativeBuffers);
//Todo: nativeContext.PixelShader.SetShaderResources();
nativeContext.PixelShader.SetShader(pixelShader.nativeShader);
}
@@ -3,6 +3,7 @@ using DirectX.D3D11;
using DirectX.D3DCompiler;
using DirectX.D3D11Shader;
using DirectX.Common;
using System.Diagnostics;
using internal GlitchyEngine.Renderer;
@@ -30,7 +31,7 @@ namespace GlitchyEngine.Renderer
Log.EngineLogger.Error($"Failed to create pixel shader: Message ({(int)result}): {result}");
}
//Reflect(shaderBlob);
Reflect(shaderBlob);
shaderBlob?.Release();
}
@@ -49,8 +50,19 @@ namespace GlitchyEngine.Renderer
for(uint32 i < cBufferCount)
{
reflection.GetResourceBindingDescription(i, let bindDesc);
var bufferReflection = reflection.GetConstantBufferByIndex(i);
bufferReflection.GetDescription(let bufferDesc);
// ConstantBuffer
if(bufferDesc.Type == .D3D11_CT_CBUFFER)
{
GlitchyEngine.Renderer.BufferDescription cBufferDesc = .(bufferDesc.Size, .Constant, .Dynamic, .Write);
_buffers.Add(bindDesc.BindPoint, StringView(bufferDesc.Name), new Buffer(_context, cBufferDesc), true);
}
}
reflection.Release();
}
+1 -1
View File
@@ -202,7 +202,7 @@ namespace GlitchyEngine.Renderer
// Todo: perhaps add a constructor that takes all parameters from description except Size
/**
* Uploads the date from @see Data to the GPU.
* Uploads the date to the GPU.
*/
[Inline]
public void Update()
@@ -0,0 +1,136 @@
using System;
using System.Collections;
namespace GlitchyEngine.Renderer
{
public class BufferCollection
{
typealias BufferEntry = (String Name, int Index, Buffer Buffer, bool OwnsBuffer);
List<BufferEntry> _buffers ~ delete _;
Dictionary<StringView, Buffer> _strToBuf ~ delete _;
Dictionary<int, Buffer> _idxToBuf ~ delete _;
//[AllowAppend]
public this()
{
/*List<BufferEntry> buffers = append .();
Dictionary<StringView, Buffer> strToBuf = append .();
Dictionary<int, Buffer> idxToBuf = append .();
_buffers = buffers;
_strToBuf = strToBuf;
_idxToBuf = idxToBuf;
*/
_buffers = new .();
_strToBuf = new .();
_idxToBuf = new .();
}
public ~this()
{
for(let entry in _buffers)
{
delete entry.Name;
if(entry.OwnsBuffer)
delete entry.Buffer;
}
}
public Buffer this[int idx] => _idxToBuf[idx];
public Buffer this[StringView name] => _strToBuf[name];
/**
* Replaces the buffer with the given index.
* @param idx The index (shader buffer register) of the buffer to replace.
* @param buffer The new buffer.
* @param If set to true, the Collection will take ownership of the buffer; if false, the ownership will remain with the caller.
*/
public void ReplaceBuffer(int idx, Buffer buffer, bool passOwnership = false)
{
if(_idxToBuf.TryGetValue(idx, let oldBuffer))
{
int index = GetIndexOfBuffer(oldBuffer);
ref BufferEntry bufferDesc = ref _buffers[index];
Log.EngineLogger.Assert(idx == bufferDesc.Index);
if(bufferDesc.OwnsBuffer)
delete bufferDesc.Buffer;
bufferDesc.Buffer = buffer;
bufferDesc.OwnsBuffer = passOwnership;
_strToBuf[bufferDesc.Name] = buffer;
_idxToBuf[bufferDesc.Index] = buffer;
}
else
{
Log.EngineLogger.Assert(false, "No buffer at the given index.");
}
}
/**
* Replaces the buffer with the given name.
* @param name The name of the buffer to replace.
* @param buffer The new buffer.
* @param If set to true, the Collection will take ownership of the buffer; if false, the ownership will remain with the caller.
*/
public void ReplaceBuffer(StringView name, Buffer buffer, bool passOwnership = false)
{
if(_strToBuf.TryGetValue(name, let oldBuffer))
{
int index = GetIndexOfBuffer(oldBuffer);
ref BufferEntry bufferDesc = ref _buffers[index];
Log.EngineLogger.Assert(name == bufferDesc.Name);
if(bufferDesc.OwnsBuffer)
delete bufferDesc.Buffer;
bufferDesc.Buffer = buffer;
bufferDesc.OwnsBuffer = passOwnership;
_strToBuf[bufferDesc.Name] = buffer;
_idxToBuf[bufferDesc.Index] = buffer;
}
else
{
Log.EngineLogger.Assert(false, "No buffer with the given name.");
}
}
public void Add(int index, StringView name, Buffer buffer, bool passOwnership = false)
{
String nameStr = new String(name);
BufferEntry entry = (nameStr, index, buffer, passOwnership);
_buffers.Add(entry);
_strToBuf.Add(entry.Name, entry.Buffer);
_idxToBuf.Add(entry.Index, entry.Buffer);
}
/**
* Returns the index of the given Buffer in the _buffer-List.
* @param The buffer to find the index of.
* @returns The index of the buffer in the _buffer-List, or -1 if it isn't in the list.
*/
int GetIndexOfBuffer(Buffer buffer)
{
for(int i < _buffers.Count)
{
// Only check for reference equality.
if(_buffers[i].Buffer === buffer)
{
return i;
}
}
return -1;
}
}
}
+7
View File
@@ -1,5 +1,7 @@
using System;
using System.IO;
using System.Collections;
using System.Diagnostics;
namespace GlitchyEngine.Renderer
{
@@ -21,10 +23,15 @@ namespace GlitchyEngine.Renderer
{
protected GraphicsContext _context;
protected BufferCollection _buffers ~ delete _;
public GraphicsContext Context => _context;
public BufferCollection Buffers => _buffers;
public this(GraphicsContext context, String source, String entryPoint, ShaderDefine[] macros = null)
{
_buffers = new BufferCollection();
_context = context;
CompileFromSource(source, entryPoint);
}