mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Constants of Effects can now be accessed easily
This commit is contained in:
@@ -3,22 +3,22 @@ using System.Collections;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public class BufferCollection
|
||||
public class BufferCollection : IEnumerable<(String Name, int Index, Buffer Buffer)>
|
||||
{
|
||||
typealias BufferEntry = (String Name, int Index, Buffer Buffer);
|
||||
public typealias BufferEntry = (String Name, int Index, Buffer Buffer);
|
||||
|
||||
List<BufferEntry> _buffers ~ DeleteBufferEntries!(_);
|
||||
|
||||
Dictionary<String, Buffer> _strToBuf ~ delete _; //delete:append _;
|
||||
Dictionary<int, Buffer> _idxToBuf ~ delete _; //delete:append _;
|
||||
Dictionary<String, BufferEntry*> _strToBuf ~ delete _; //delete:append _;
|
||||
Dictionary<int, BufferEntry*> _idxToBuf ~ delete _; //delete:append _;
|
||||
|
||||
[AllowAppend]
|
||||
public this()
|
||||
{
|
||||
// Todo: append allocate as soon as it's fixed
|
||||
let buffers = new List<BufferEntry>();
|
||||
let strToBuf = new Dictionary<String, Buffer>();
|
||||
let idxToBuf = new Dictionary<int, Buffer>();
|
||||
let strToBuf = new Dictionary<String, BufferEntry*>();
|
||||
let idxToBuf = new Dictionary<int, BufferEntry*>();
|
||||
|
||||
_buffers = buffers;
|
||||
_strToBuf = strToBuf;
|
||||
@@ -39,35 +39,61 @@ namespace GlitchyEngine.Renderer
|
||||
delete entries;
|
||||
}
|
||||
|
||||
public Buffer this[int idx] => _idxToBuf[idx];
|
||||
public Buffer this[String name] => _strToBuf[name];
|
||||
public Buffer this[int idx] => _idxToBuf[idx].Buffer;
|
||||
public Buffer this[String name] => _strToBuf[name].Buffer;
|
||||
|
||||
public Buffer TryGetBuffer(String name)
|
||||
{
|
||||
return TryGetBufferEntry(name)?.Buffer;
|
||||
}
|
||||
|
||||
public Buffer TryGetBuffer(int index)
|
||||
{
|
||||
return TryGetBufferEntry(index)?.Buffer;
|
||||
}
|
||||
|
||||
public BufferEntry* TryGetBufferEntry(String name)
|
||||
{
|
||||
if(_strToBuf.TryGetValue(name, let buffer))
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public BufferEntry* TryGetBufferEntry(int index)
|
||||
{
|
||||
if(_idxToBuf.TryGetValue(index, let buffer))
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the buffer with the given index.
|
||||
* @param idx The index (shader buffer register) of the buffer to replace.
|
||||
* @param buffer The new buffer.
|
||||
* @returns True, if the buffer was replaced successfully; false, otherwise.
|
||||
*/
|
||||
public void ReplaceBuffer(int idx, Buffer buffer)
|
||||
public bool TryReplaceBuffer(int idx, Buffer buffer)
|
||||
{
|
||||
if(_idxToBuf.TryGetValue(idx, let oldBuffer))
|
||||
if(_idxToBuf.TryGetValue(idx, let bufferEntry))
|
||||
{
|
||||
int index = GetIndexOfBuffer(oldBuffer);
|
||||
Log.EngineLogger.Assert(idx == bufferEntry.Index);
|
||||
|
||||
ref BufferEntry bufferDesc = ref _buffers[index];
|
||||
|
||||
Log.EngineLogger.Assert(idx == bufferDesc.Index);
|
||||
|
||||
oldBuffer.ReleaseRef();
|
||||
bufferEntry.Buffer.ReleaseRef();
|
||||
|
||||
buffer.AddRef();
|
||||
bufferDesc.Buffer = buffer;
|
||||
bufferEntry.Buffer = buffer;
|
||||
|
||||
_strToBuf[bufferDesc.Name] = buffer;
|
||||
_idxToBuf[bufferDesc.Index] = buffer;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.EngineLogger.Assert(false, "No buffer at the given index.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,22 +105,14 @@ namespace GlitchyEngine.Renderer
|
||||
*/
|
||||
public bool TryReplaceBuffer(String name, Buffer buffer)
|
||||
{
|
||||
if(_strToBuf.TryGetValue(name, let oldBuffer))
|
||||
if(_strToBuf.TryGetValue(name, let bufferEntry))
|
||||
{
|
||||
int index = GetIndexOfBuffer(oldBuffer);
|
||||
Log.EngineLogger.AssertDebug(name == bufferEntry.Name);
|
||||
|
||||
ref BufferEntry bufferDesc = ref _buffers[index];
|
||||
|
||||
// If the names don't match something went spectactularly wrong.
|
||||
Log.EngineLogger.AssertDebug(name == bufferDesc.Name);
|
||||
|
||||
oldBuffer?.ReleaseRef();
|
||||
bufferEntry.Buffer.ReleaseRef();
|
||||
|
||||
buffer?.AddRef();
|
||||
bufferDesc.Buffer = buffer;
|
||||
|
||||
_strToBuf[bufferDesc.Name] = buffer;
|
||||
_idxToBuf[bufferDesc.Index] = buffer;
|
||||
buffer.AddRef();
|
||||
bufferEntry.Buffer = buffer;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -106,19 +124,25 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
public void Add(int index, String name, Buffer buffer)
|
||||
{
|
||||
String nameStr = new String(name);
|
||||
BufferEntry entry = (nameStr, index, buffer);
|
||||
Add((name, index, buffer));
|
||||
}
|
||||
|
||||
buffer.AddRef();
|
||||
_buffers.Add(entry);
|
||||
_strToBuf.Add(entry.Name, entry.Buffer);
|
||||
_idxToBuf.Add(entry.Index, entry.Buffer);
|
||||
public void Add(BufferEntry entry)
|
||||
{
|
||||
BufferEntry copy = (new String(entry.Name), entry.Index, entry.Buffer..AddRef());
|
||||
|
||||
_buffers.Add(copy);
|
||||
|
||||
BufferEntry* copyRef = &_buffers.Back;
|
||||
|
||||
_strToBuf.Add(copy.Name, copyRef);
|
||||
_idxToBuf.Add(copy.Index, copyRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @returns The index of the buffer, or null if it isn't in this collection.
|
||||
*/
|
||||
int GetIndexOfBuffer(Buffer buffer)
|
||||
{
|
||||
@@ -133,5 +157,29 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the given Buffer in the _buffer-List.
|
||||
* @param The buffer to find the index of.
|
||||
* @returns The name of the buffer, or null if it isn't in this collection.
|
||||
*/
|
||||
String GetNameOfBuffer(Buffer buffer)
|
||||
{
|
||||
for(int i < _buffers.Count)
|
||||
{
|
||||
// Only check for reference equality.
|
||||
if(_buffers[i].Buffer === buffer)
|
||||
{
|
||||
return _buffers[i].Name;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<BufferEntry>.Enumerator GetEnumerator()
|
||||
{
|
||||
return _buffers.GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
using System;
|
||||
using GlitchyEngine.Math;
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
public class BufferVariable
|
||||
{
|
||||
private ConstantBuffer _constantBuffer;
|
||||
|
||||
private String _name ~ delete _;
|
||||
|
||||
private ShaderVariableType _type;
|
||||
|
||||
private uint32 _columns;
|
||||
private uint32 _rows;
|
||||
private uint32 _offset;
|
||||
private uint32 _sizeInBytes;
|
||||
|
||||
private bool _isUsed;
|
||||
|
||||
public ConstantBuffer ConstantBuffer => _constantBuffer;
|
||||
|
||||
public ShaderVariableType Type => _type;
|
||||
|
||||
public String Name => _name;
|
||||
|
||||
/**
|
||||
* Gets a pointer to the start of the variable in the constant buffers backing data.
|
||||
*/
|
||||
[Inline]
|
||||
internal uint8* firstByte => _constantBuffer.rawData.CArray() + _offset;
|
||||
|
||||
public this(ConstantBuffer constantBuffer, ShaderVariableType type, uint32 columns, uint32 rows, uint32 offset, uint32 sizeInBytes, bool isUsed)
|
||||
{
|
||||
_constantBuffer = constantBuffer..AddRef();
|
||||
_type = type;
|
||||
_columns = columns;
|
||||
_rows = rows;
|
||||
_offset = offset;
|
||||
_sizeInBytes = sizeInBytes;
|
||||
_isUsed = isUsed;
|
||||
}
|
||||
|
||||
public void EnsureTypeMatch(int rows, int cols, ShaderVariableType type)
|
||||
{
|
||||
#if GE_ERROR_SHADER_MATRIX_MISMATCH
|
||||
Log.EngineLogger.Assert(rows == _rows || cols == _columns, scope $"The matrix-dimensions do not match: Expected {_rows} rows and {_rows} columns but Received {rows} rows and {cols} columns instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#elif GE_WARN_SHADER_MATRIX_MISMATCH
|
||||
if (rows != _rows || cols != _columns)
|
||||
Log.EngineLogger.Warning($"The matrix-dimensions do not match: Expected {_rows} rows and {_rows} columns but Received {rows} rows and {cols} columns instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#endif
|
||||
|
||||
#if GE_ERROR_SHADER_VAR_TYPE_MISMATCH
|
||||
Log.EngineLogger.Assert(type == _type, scope $"The types do not match: Expected \"{_type}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#elif GE_WARN_SHADER_VAR_TYPE_MISMATCH
|
||||
if (type != _type)
|
||||
Log.EngineLogger.Warning($"The types do not match: Expected \"{_type}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetData(float value)
|
||||
{
|
||||
EnsureTypeMatch(1, 1, .Float);
|
||||
|
||||
*(float*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Vector2 value)
|
||||
{
|
||||
EnsureTypeMatch(1, 2, .Float);
|
||||
|
||||
*(Vector2*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Vector3 value)
|
||||
{
|
||||
EnsureTypeMatch(1, 3, .Float);
|
||||
|
||||
*(Vector3*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Vector4 value)
|
||||
{
|
||||
EnsureTypeMatch(1, 4, .Float);
|
||||
|
||||
*(Vector4*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Matrix4x3 value)
|
||||
{
|
||||
// I think this is right
|
||||
EnsureTypeMatch(4, 3, .Float);
|
||||
|
||||
*(Matrix4x3*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Matrix3x3 value)
|
||||
{
|
||||
EnsureTypeMatch(3, 3, .Float);
|
||||
|
||||
*(Matrix4x3*)firstByte = Matrix4x3(value);
|
||||
|
||||
// Todo: maybe manual copy
|
||||
}
|
||||
|
||||
public void SetData(Matrix value)
|
||||
{
|
||||
EnsureTypeMatch(4, 4, .Float);
|
||||
|
||||
*(Matrix*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(ColorRGB value)
|
||||
{
|
||||
EnsureTypeMatch(1, 3, .Float);
|
||||
|
||||
*(ColorRGB*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(ColorRGBA value)
|
||||
{
|
||||
EnsureTypeMatch(1, 4, .Float);
|
||||
|
||||
*(ColorRGBA*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Color value)
|
||||
{
|
||||
EnsureTypeMatch(1, 4, .Float);
|
||||
|
||||
*(ColorRGBA*)firstByte = (ColorRGBA)value;
|
||||
}
|
||||
|
||||
// Todo: add all the other SetData-Methods
|
||||
|
||||
/**
|
||||
* Sets the raw data of the variable.
|
||||
* @param rawData The pointer to the raw data. If rawData is null the raw data will be set to zero.
|
||||
*/
|
||||
internal void SetRawData(void* rawData)
|
||||
{
|
||||
#if DEBUG && !GE_IGNORE_UNUSED_VARIABLE
|
||||
if(!_isUsed)
|
||||
{
|
||||
Log.EngineLogger.Warning($"Setting data for unused Variable \"{_name}\" of constant buffer \"{_constantBuffer.Name}\".");
|
||||
}
|
||||
#endif
|
||||
|
||||
if(rawData != null)
|
||||
Internal.MemCpy(firstByte, rawData, _sizeInBytes);
|
||||
else
|
||||
Internal.MemSet(firstByte, 0, _sizeInBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,52 @@ using internal GlitchyEngine.Renderer;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
public class BufferVariableCollection : IEnumerable<BufferVariable>
|
||||
{
|
||||
protected bool _ownsVariables = true;
|
||||
protected List<BufferVariable> _variables = new .();
|
||||
protected Dictionary<String, BufferVariable> _nameToVariable = new .() ~ delete _;
|
||||
|
||||
public this(bool ownsVariables = true)
|
||||
{
|
||||
_ownsVariables = ownsVariables;
|
||||
}
|
||||
|
||||
public ~this()
|
||||
{
|
||||
if(_ownsVariables)
|
||||
DeleteContainerAndItems!(_variables);
|
||||
else
|
||||
delete _variables;
|
||||
}
|
||||
|
||||
public void Add(BufferVariable ownVariable)
|
||||
{
|
||||
_variables.Add(ownVariable);
|
||||
_nameToVariable.Add(ownVariable.Name, ownVariable);
|
||||
}
|
||||
|
||||
public bool TryAdd(BufferVariable ownVariable)
|
||||
{
|
||||
if(_nameToVariable.TryAdd(ownVariable.Name, ownVariable))
|
||||
{
|
||||
_variables.Add(ownVariable);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public BufferVariable this[String name] => _nameToVariable[name];
|
||||
|
||||
public List<BufferVariable>.Enumerator GetEnumerator()
|
||||
{
|
||||
return _variables.GetEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
public class ConstantBuffer : Buffer
|
||||
{
|
||||
protected String _name ~ delete _;
|
||||
@@ -16,20 +62,18 @@ namespace GlitchyEngine.Renderer
|
||||
*/
|
||||
protected internal uint8[] rawData ~ delete _;
|
||||
|
||||
protected List<BufferVariable> _variables = new .() ~ DeleteContainerAndItems!(_);
|
||||
protected Dictionary<String, BufferVariable> _nameToVariable = new .() ~ delete _;
|
||||
protected BufferVariableCollection _variables = new BufferVariableCollection() ~ delete _;
|
||||
|
||||
/// Gets the name of the constant buffer.
|
||||
public String Name => _name;
|
||||
|
||||
protected this(GraphicsContext context) : base(context) {}
|
||||
public BufferVariableCollection Variables => _variables;
|
||||
|
||||
public BufferVariable this[String name] => _nameToVariable[name];
|
||||
protected this(GraphicsContext context) : base(context) {}
|
||||
|
||||
protected internal void AddVariable(BufferVariable ownVariable)
|
||||
{
|
||||
_variables.Add(ownVariable);
|
||||
_nameToVariable.Add(ownVariable.Name, ownVariable);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,155 +102,4 @@ namespace GlitchyEngine.Renderer
|
||||
Float,
|
||||
// todo
|
||||
}
|
||||
|
||||
public class BufferVariable
|
||||
{
|
||||
private ConstantBuffer _constantBuffer;
|
||||
|
||||
private String _name ~ delete _;
|
||||
|
||||
private ShaderVariableType _type;
|
||||
|
||||
private uint32 _columns;
|
||||
private uint32 _rows;
|
||||
private uint32 _offset;
|
||||
private uint32 _sizeInBytes;
|
||||
|
||||
private bool _isUsed;
|
||||
|
||||
public ConstantBuffer ConstantBuffer => _constantBuffer;
|
||||
|
||||
public ShaderVariableType Type => _type;
|
||||
|
||||
public String Name => _name;
|
||||
|
||||
/**
|
||||
* Gets a pointer to the start of the variable in the constant buffers backing data.
|
||||
*/
|
||||
[Inline]
|
||||
internal uint8* firstByte => _constantBuffer.rawData.CArray() + _offset;
|
||||
|
||||
public this(ConstantBuffer constantBuffer, ShaderVariableType type, uint32 columns, uint32 rows, uint32 offset, uint32 sizeInBytes, bool isUsed)
|
||||
{
|
||||
_constantBuffer = constantBuffer..AddRef();
|
||||
_type = type;
|
||||
_columns = columns;
|
||||
_rows = rows;
|
||||
_offset = offset;
|
||||
_sizeInBytes = sizeInBytes;
|
||||
_isUsed = isUsed;
|
||||
}
|
||||
|
||||
public void EnsureTypeMatch(int rows, int cols, ShaderVariableType type)
|
||||
{
|
||||
#if GE_ERROR_SHADER_MATRIX_MISMATCH
|
||||
Log.EngineLogger.Assert(rows == _rows || cols == _columns, scope $"The matrix-dimensions do not match: Expected {_rows} rows and {_rows} columns but Received {rows} rows and {cols} columns instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#elif GE_WARN_SHADER_MATRIX_MISMATCH
|
||||
if (rows != _rows || cols != _columns)
|
||||
Log.EngineLogger.Warning($"The matrix-dimensions do not match: Expected {_rows} rows and {_rows} columns but Received {rows} rows and {cols} columns instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#endif
|
||||
|
||||
#if GE_ERROR_SHADER_VAR_TYPE_MISMATCH
|
||||
Log.EngineLogger.Assert(type == _type, scope $"The types do not match: Expected \"{_type}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#elif GE_WARN_SHADER_VAR_TYPE_MISMATCH
|
||||
if (type != _type)
|
||||
Log.EngineLogger.Warning($"The types do not match: Expected \"{_type}\" but Received \"{type}\" instead. Variable: \"{_name}\" of buffer: \"{_constantBuffer.Name}\"");
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SetData(float value)
|
||||
{
|
||||
EnsureTypeMatch(1, 1, .Float);
|
||||
|
||||
*(float*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Vector2 value)
|
||||
{
|
||||
EnsureTypeMatch(1, 2, .Float);
|
||||
|
||||
*(Vector2*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Vector3 value)
|
||||
{
|
||||
EnsureTypeMatch(1, 3, .Float);
|
||||
|
||||
*(Vector3*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Vector4 value)
|
||||
{
|
||||
EnsureTypeMatch(1, 4, .Float);
|
||||
|
||||
*(Vector4*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Matrix4x3 value)
|
||||
{
|
||||
// I think this is right
|
||||
EnsureTypeMatch(4, 3, .Float);
|
||||
|
||||
*(Matrix4x3*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Matrix3x3 value)
|
||||
{
|
||||
EnsureTypeMatch(3, 3, .Float);
|
||||
|
||||
*(Matrix4x3*)firstByte = Matrix4x3(value);
|
||||
|
||||
// Todo: maybe manual copy
|
||||
}
|
||||
|
||||
public void SetData(Matrix value)
|
||||
{
|
||||
EnsureTypeMatch(4, 4, .Float);
|
||||
|
||||
*(Matrix*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(ColorRGB value)
|
||||
{
|
||||
EnsureTypeMatch(1, 3, .Float);
|
||||
|
||||
*(ColorRGB*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(ColorRGBA value)
|
||||
{
|
||||
EnsureTypeMatch(1, 4, .Float);
|
||||
|
||||
*(ColorRGBA*)firstByte = value;
|
||||
}
|
||||
|
||||
public void SetData(Color value)
|
||||
{
|
||||
EnsureTypeMatch(1, 4, .Float);
|
||||
|
||||
*(ColorRGBA*)firstByte = (ColorRGBA)value;
|
||||
}
|
||||
|
||||
// Todo: add all the other SetData-Methods
|
||||
|
||||
/**
|
||||
* Sets the raw data of the variable.
|
||||
* @param rawData The pointer to the raw data. If rawData is null the raw data will be set to zero.
|
||||
*/
|
||||
internal void SetRawData(void* rawData)
|
||||
{
|
||||
#if DEBUG && !GE_IGNORE_UNUSED_VARIABLE
|
||||
if(!_isUsed)
|
||||
{
|
||||
Log.EngineLogger.Warning($"Setting data for unused Variable \"{_name}\" of constant buffer \"{_constantBuffer.Name}\".");
|
||||
}
|
||||
#endif
|
||||
|
||||
if(rawData != null)
|
||||
Internal.MemCpy(firstByte, rawData, _sizeInBytes);
|
||||
else
|
||||
Internal.MemSet(firstByte, 0, _sizeInBytes);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
@@ -9,6 +10,10 @@ namespace GlitchyEngine.Renderer
|
||||
internal VertexShader _vs ~ _?.ReleaseRef();
|
||||
internal PixelShader _ps ~ _?.ReleaseRef();
|
||||
|
||||
BufferCollection _bufferCollection ~ delete _;
|
||||
|
||||
BufferVariableCollection _variables ~ delete _;
|
||||
|
||||
public GraphicsContext Context => _context;
|
||||
|
||||
public VertexShader VertexShader
|
||||
@@ -33,8 +38,24 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
|
||||
public BufferCollection Buffers => _bufferCollection;
|
||||
public BufferVariableCollection Variables => _variables;
|
||||
|
||||
public void ApplyChanges()
|
||||
{
|
||||
for(let buffer in _bufferCollection)
|
||||
{
|
||||
if(let cbuffer = buffer.Buffer as ConstantBuffer)
|
||||
{
|
||||
cbuffer.Update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind(GraphicsContext context)
|
||||
{
|
||||
ApplyChanges();
|
||||
|
||||
context.SetVertexShader(_vs);
|
||||
context.SetPixelShader(_ps);
|
||||
}
|
||||
@@ -60,6 +81,8 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
ProcessFile(filename, fileContent, vsName, psName);
|
||||
Compile(fileContent, vsName, psName);
|
||||
|
||||
MergeResources();
|
||||
}
|
||||
|
||||
public this(String vsPath, String vsEntry, String psPath, String psEntry)
|
||||
@@ -155,5 +178,81 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
|
||||
protected extern void Compile(String vsPath, String vsEntry, String psPath, String psEntry);
|
||||
|
||||
private void MergeResources()
|
||||
{
|
||||
MergeConstantBuffers();
|
||||
MergeBufferVariables();
|
||||
}
|
||||
|
||||
private void MergeConstantBuffers()
|
||||
{
|
||||
_bufferCollection = new BufferCollection();
|
||||
|
||||
HashSet<String> bufferNames = scope HashSet<String>();
|
||||
|
||||
AddShaderBuffers(_vs, bufferNames);
|
||||
AddShaderBuffers(_ps, bufferNames);
|
||||
|
||||
int internalIndex = 0;
|
||||
|
||||
for(String bufferName in bufferNames)
|
||||
{
|
||||
let vsBuffer = _vs.Buffers.TryGetBufferEntry(bufferName);
|
||||
let psBuffer = _ps.Buffers.TryGetBufferEntry(bufferName);
|
||||
|
||||
if(vsBuffer != null && psBuffer != null)
|
||||
{
|
||||
BufferCollection.BufferEntry* fxBuffer = null;
|
||||
// choose the larger of the two
|
||||
if(psBuffer.Buffer.Description.Size > vsBuffer.Buffer.Description.Size)
|
||||
fxBuffer = psBuffer;
|
||||
else
|
||||
fxBuffer = vsBuffer;
|
||||
|
||||
_bufferCollection.Add(internalIndex, bufferName, fxBuffer.Buffer);
|
||||
|
||||
_vs.Buffers.TryReplaceBuffer(vsBuffer.Index, fxBuffer.Buffer);
|
||||
_ps.Buffers.TryReplaceBuffer(psBuffer.Index, fxBuffer.Buffer);
|
||||
}
|
||||
else if(vsBuffer != null)
|
||||
{
|
||||
_bufferCollection.Add(internalIndex, bufferName, vsBuffer.Buffer);
|
||||
}
|
||||
else if(psBuffer != null)
|
||||
{
|
||||
_bufferCollection.Add(internalIndex, bufferName, psBuffer.Buffer);
|
||||
}
|
||||
|
||||
internalIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
private void MergeBufferVariables()
|
||||
{
|
||||
_variables = new BufferVariableCollection(false);
|
||||
|
||||
for(let buffer in _bufferCollection)
|
||||
{
|
||||
if(let cbuffer = buffer.Buffer as ConstantBuffer)
|
||||
{
|
||||
for(let variable in cbuffer.Variables)
|
||||
{
|
||||
_variables.TryAdd(variable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddShaderBuffers(Shader shader, HashSet<String> bufferNames)
|
||||
{
|
||||
if(shader != null)
|
||||
{
|
||||
for(let buffer in shader.Buffers)
|
||||
{
|
||||
bufferNames.Add(buffer.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,40 +16,48 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
static GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
static Buffer<SceneConstants> _sceneConstants ~ _?.ReleaseRef();
|
||||
//static Buffer<SceneConstants> _sceneConstants ~ _?.ReleaseRef();
|
||||
|
||||
static Buffer<ObjectConstants> _objectConstants ~ _?.ReleaseRef();
|
||||
//static Buffer<ObjectConstants> _objectConstants ~ _?.ReleaseRef();
|
||||
|
||||
static SceneConstants _sceneConstants;
|
||||
|
||||
public static void Init(GraphicsContext context)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
/*
|
||||
_sceneConstants = new Buffer<SceneConstants>(_context, .(0, .Constant, .Dynamic, .Write));
|
||||
_sceneConstants.Update();
|
||||
|
||||
_objectConstants = new Buffer<ObjectConstants>(_context, .(0, .Constant, .Dynamic, .Write));
|
||||
_objectConstants.Update();
|
||||
*/
|
||||
|
||||
RenderCommand.Init();
|
||||
}
|
||||
|
||||
public static void BeginScene(Camera camera)
|
||||
{
|
||||
_sceneConstants.Data.ViewProjection = camera.ViewProjection;
|
||||
_sceneConstants.Update();
|
||||
_sceneConstants.ViewProjection = camera.ViewProjection;
|
||||
//_sceneConstants.Data.ViewProjection = camera.ViewProjection;
|
||||
//_sceneConstants.Update();
|
||||
}
|
||||
|
||||
public static void EndScene(){}
|
||||
|
||||
public static void Submit(GeometryBinding geometry, Effect effect, Matrix transform = .Identity)
|
||||
{
|
||||
effect.PixelShader?.Buffers.TryReplaceBuffer("SceneConstants", _sceneConstants);
|
||||
effect.VertexShader?.Buffers.TryReplaceBuffer("SceneConstants", _sceneConstants);
|
||||
//effect.PixelShader?.Buffers.TryReplaceBuffer("SceneConstants", _sceneConstants);
|
||||
//effect.VertexShader?.Buffers.TryReplaceBuffer("SceneConstants", _sceneConstants);
|
||||
|
||||
_objectConstants.Data.Transform = transform;
|
||||
_objectConstants.Update();
|
||||
//_objectConstants.Data.Transform = transform;
|
||||
//_objectConstants.Update();
|
||||
|
||||
//effect.PixelShader?.Buffers.TryReplaceBuffer("ObjectConstants", _objectConstants);
|
||||
//effect.VertexShader?.Buffers.TryReplaceBuffer("ObjectConstants", _objectConstants);
|
||||
|
||||
effect.PixelShader?.Buffers.TryReplaceBuffer("ObjectConstants", _objectConstants);
|
||||
effect.VertexShader?.Buffers.TryReplaceBuffer("ObjectConstants", _objectConstants);
|
||||
effect.Variables["ViewProjection"].SetData(_sceneConstants.ViewProjection);
|
||||
effect.Variables["Transform"].SetData(transform);
|
||||
|
||||
effect.Bind(_context);
|
||||
|
||||
|
||||
@@ -66,8 +66,6 @@ namespace Sandbox
|
||||
Effect _effect ~ _?.ReleaseRef();
|
||||
Effect _textureEffect ~ _?.ReleaseRef();
|
||||
|
||||
ConstantBuffer _cBuffer ~ _?.ReleaseRef();
|
||||
|
||||
GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
Texture2D _texture ~ _?.ReleaseRef();
|
||||
@@ -94,10 +92,6 @@ namespace Sandbox
|
||||
|
||||
_vertexLayout = new VertexLayout(_context, VertexColorTexture.VertexElements, _textureEffect.VertexShader);
|
||||
|
||||
|
||||
_cBuffer = _effect.PixelShader.Buffers["Constants"] as ConstantBuffer;
|
||||
_cBuffer.AddRef();
|
||||
|
||||
// Create hexagon
|
||||
{
|
||||
_geometryBinding = new GeometryBinding(_context);
|
||||
@@ -249,19 +243,16 @@ namespace Sandbox
|
||||
for(int y < 20)
|
||||
{
|
||||
if((x + y) % 2 == 0)
|
||||
_cBuffer["BaseColor"].SetData(_squareColor0);
|
||||
_effect.Variables["BaseColor"].SetData(_squareColor0);
|
||||
else
|
||||
_cBuffer["BaseColor"].SetData(_squareColor1);
|
||||
|
||||
_cBuffer.Update();
|
||||
_effect.Variables["BaseColor"].SetData(_squareColor1);
|
||||
|
||||
Matrix transform = Matrix.Translation(x * 0.2f, y * 0.2f, 0) * Matrix.Scaling(0.1f);
|
||||
Renderer.Submit(_quadGeometryBinding, _effect, transform);
|
||||
}
|
||||
|
||||
_cBuffer["BaseColor"].SetData(ColorRGBA.White);
|
||||
_cBuffer.Update();
|
||||
|
||||
_effect.Variables["BaseColor"].SetData(_squareColor1);
|
||||
|
||||
_texture.Bind();
|
||||
Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user