From 64e9cc16137b95e476249c66b8f759a2cf2a3d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Tue, 16 Feb 2021 18:11:34 +0100 Subject: [PATCH] Constants of Effects can now be accessed easily --- .../src/Renderer/BufferCollection.bf | 128 +++++++---- GlitchyEngine/src/Renderer/BufferVariable.bf | 156 +++++++++++++ GlitchyEngine/src/Renderer/ConstantBuffer.bf | 205 +++++------------- GlitchyEngine/src/Renderer/Effect.bf | 99 +++++++++ GlitchyEngine/src/Renderer/Renderer.bf | 28 ++- Sandbox/src/SandboxApp.bf | 17 +- 6 files changed, 414 insertions(+), 219 deletions(-) create mode 100644 GlitchyEngine/src/Renderer/BufferVariable.bf diff --git a/GlitchyEngine/src/Renderer/BufferCollection.bf b/GlitchyEngine/src/Renderer/BufferCollection.bf index 579aba4..05b1300 100644 --- a/GlitchyEngine/src/Renderer/BufferCollection.bf +++ b/GlitchyEngine/src/Renderer/BufferCollection.bf @@ -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 _buffers ~ DeleteBufferEntries!(_); - Dictionary _strToBuf ~ delete _; //delete:append _; - Dictionary _idxToBuf ~ delete _; //delete:append _; + Dictionary _strToBuf ~ delete _; //delete:append _; + Dictionary _idxToBuf ~ delete _; //delete:append _; [AllowAppend] public this() { // Todo: append allocate as soon as it's fixed let buffers = new List(); - let strToBuf = new Dictionary(); - let idxToBuf = new Dictionary(); + let strToBuf = new Dictionary(); + let idxToBuf = new Dictionary(); _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.Enumerator GetEnumerator() + { + return _buffers.GetEnumerator(); + } } } diff --git a/GlitchyEngine/src/Renderer/BufferVariable.bf b/GlitchyEngine/src/Renderer/BufferVariable.bf new file mode 100644 index 0000000..16bc74d --- /dev/null +++ b/GlitchyEngine/src/Renderer/BufferVariable.bf @@ -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); + } + } +} diff --git a/GlitchyEngine/src/Renderer/ConstantBuffer.bf b/GlitchyEngine/src/Renderer/ConstantBuffer.bf index 3573a5c..2879375 100644 --- a/GlitchyEngine/src/Renderer/ConstantBuffer.bf +++ b/GlitchyEngine/src/Renderer/ConstantBuffer.bf @@ -7,6 +7,52 @@ using internal GlitchyEngine.Renderer; namespace GlitchyEngine.Renderer { + public class BufferVariableCollection : IEnumerable + { + protected bool _ownsVariables = true; + protected List _variables = new .(); + protected Dictionary _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.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 _variables = new .() ~ DeleteContainerAndItems!(_); - protected Dictionary _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); - } - } - } diff --git a/GlitchyEngine/src/Renderer/Effect.bf b/GlitchyEngine/src/Renderer/Effect.bf index 62c08a2..ffd07aa 100644 --- a/GlitchyEngine/src/Renderer/Effect.bf +++ b/GlitchyEngine/src/Renderer/Effect.bf @@ -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 bufferNames = scope HashSet(); + + 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 bufferNames) + { + if(shader != null) + { + for(let buffer in shader.Buffers) + { + bufferNames.Add(buffer.Name); + } + } + } } } diff --git a/GlitchyEngine/src/Renderer/Renderer.bf b/GlitchyEngine/src/Renderer/Renderer.bf index 87928e0..11cf6cc 100644 --- a/GlitchyEngine/src/Renderer/Renderer.bf +++ b/GlitchyEngine/src/Renderer/Renderer.bf @@ -16,40 +16,48 @@ namespace GlitchyEngine.Renderer static GraphicsContext _context ~ _?.ReleaseRef(); - static Buffer _sceneConstants ~ _?.ReleaseRef(); + //static Buffer _sceneConstants ~ _?.ReleaseRef(); - static Buffer _objectConstants ~ _?.ReleaseRef(); + //static Buffer _objectConstants ~ _?.ReleaseRef(); + + static SceneConstants _sceneConstants; public static void Init(GraphicsContext context) { _context = context..AddRef(); + /* _sceneConstants = new Buffer(_context, .(0, .Constant, .Dynamic, .Write)); _sceneConstants.Update(); _objectConstants = new Buffer(_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); diff --git a/Sandbox/src/SandboxApp.bf b/Sandbox/src/SandboxApp.bf index 72e1258..046c4d5 100644 --- a/Sandbox/src/SandboxApp.bf +++ b/Sandbox/src/SandboxApp.bf @@ -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));