VertexBuffer, VertexLayout and GeometryBinding improvements

- VertexBuffer: Added constructor that takes stride instead of type
- VertexLayout: Added Refcounting to VertexLayout and ownership parameters for VertexElements
- Fixed: GeometryBinding using stride and offset from buffer instead of buffer binding
This commit is contained in:
Simon Lübeß
2021-08-05 18:32:21 +02:00
parent e04d058381
commit 5c0ee3bae0
7 changed files with 65 additions and 32 deletions
@@ -40,8 +40,8 @@ namespace GlitchyEngine.Renderer
nativeBuffers[slot] = vertexBuffer.nativeBuffer..AddRef();
bufferStrides[slot] = vertexBuffer.Binding.Stride;
bufferOffsets[slot] = vertexBuffer.Binding.Offset;
bufferStrides[slot] = binding.Stride;
bufferOffsets[slot] = binding.Offset;
}
else
{
@@ -13,12 +13,13 @@ namespace GlitchyEngine.Renderer
public ID3DBlob* nativeShaderCode ~ _?.Release();
public this(GraphicsContext context, VertexElement[] elements, VertexShader vertexShader)
public this(GraphicsContext context, VertexElement[] elements, bool ownsElements, VertexShader vertexShader)
{
nativeShaderCode = vertexShader.nativeCode..AddRef();
_context = context..AddRef();
_elements = elements;
_ownsElements = ownsElements;
CreateNativeLayout();
}
@@ -8,8 +8,8 @@ namespace GlitchyEngine.Renderer
internal GraphicsContext _context ~ _?.ReleaseRef();
internal List<VertexBufferBinding> _vertexBuffers = new .() ~ delete _;
internal IndexBuffer _indexBuffer;
internal VertexLayout _vertexLayout;
internal IndexBuffer _indexBuffer ~ _?.ReleaseRef();
internal VertexLayout _vertexLayout ~ _?.ReleaseRef();
internal uint32 _indexByteOffset;
internal uint32 _indexCount;
internal uint32 _instanceCount;
@@ -26,8 +26,6 @@ namespace GlitchyEngine.Renderer
{
binding.Buffer?.ReleaseRef();
}
_indexBuffer?.ReleaseRef();
}
public VertexBufferBinding GetVertexBuffer(uint32 slot)
@@ -74,7 +72,8 @@ namespace GlitchyEngine.Renderer
public void SetVertexLayout(VertexLayout vertexLayout)
{
_vertexLayout = vertexLayout;
_vertexLayout?.ReleaseRef();
_vertexLayout = vertexLayout..AddRef();
PlatformSetVertexLayout(vertexLayout);
}
@@ -91,8 +90,7 @@ namespace GlitchyEngine.Renderer
public void SetIndexBuffer(IndexBuffer indexBuffer, uint32 byteOffset = 0, uint32 indexCount = (.)-1)
{
_indexBuffer?.ReleaseRef();
_indexBuffer = indexBuffer;
_indexBuffer.AddRef();
_indexBuffer = indexBuffer..AddRef();
_indexByteOffset = byteOffset;
+11 -13
View File
@@ -56,11 +56,9 @@ namespace GlitchyEngine.Renderer
private Vector2 _virtualResolution;
private Effect quadEffect ~ _?.ReleaseRef();
private VertexLayout layout ~ delete _;
private GeometryBinding quadBinding ~ _?.ReleaseRef();
private Effect instancingEffect ~ _?.ReleaseRef();
private VertexLayout instancingLayout ~ delete _;
private GeometryBinding instancingBinding ~ _?.ReleaseRef();
private VertexBuffer instanceBuffer ~ _?.ReleaseRef();
@@ -80,7 +78,7 @@ namespace GlitchyEngine.Renderer
{
quadEffect = effectLibrary.Load("content\\Shaders\\render2dShader.hlsl", "Renderer2D");
layout = new VertexLayout(_context, RenderVertex.VertexElements, quadEffect.VertexShader);
VertexLayout layout = new VertexLayout(_context, RenderVertex.VertexElements, false, quadEffect.VertexShader);
VertexBuffer quadVertices = new VertexBuffer(_context, typeof(RenderVertex), 4, .Immutable);
@@ -103,7 +101,7 @@ namespace GlitchyEngine.Renderer
quadIndices.SetData(indices);
quadBinding = new GeometryBinding(_context);
quadBinding.SetVertexLayout(layout);
quadBinding.SetVertexLayout(layout..ReleaseRefNoDelete());
quadBinding.SetPrimitiveTopology(.TriangleList);
quadBinding.SetVertexBufferSlot(quadVertices, 0);
quadBinding.SetIndexBuffer(quadIndices);
@@ -133,20 +131,20 @@ namespace GlitchyEngine.Renderer
instanceBuffer.SetData(0);
VertexElement[] vertexElements = scope .(
VertexElement(.R32G32_Float, "POSITION", 0, 0, 0, .PerVertexData, 0),
VertexElement(.R32G32_Float, "POSITION", false, 0, 0, 0, .PerVertexData, 0),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", 0, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", 1, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", 2, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", 3, 1, (.)-1, .PerInstanceData, 1),
VertexElement( .R8G8B8A8_UNorm, "COLOR", 0, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TEXCOORD", 0, 1, (.)-1, .PerInstanceData, 1)
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 0, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 1, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 2, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 3, 1, (.)-1, .PerInstanceData, 1),
VertexElement( .R8G8B8A8_UNorm, "COLOR", false, 0, 1, (.)-1, .PerInstanceData, 1),
VertexElement(.R32G32B32A32_Float, "TEXCOORD", false, 0, 1, (.)-1, .PerInstanceData, 1)
);
instancingLayout = new VertexLayout(_context, vertexElements, instancingEffect.VertexShader);
VertexLayout instancingLayout = new VertexLayout(_context, vertexElements, true, instancingEffect.VertexShader);
instancingBinding = new GeometryBinding(_context);
instancingBinding.SetVertexLayout(instancingLayout);
instancingBinding.SetVertexLayout(instancingLayout..ReleaseRefNoDelete());
instancingBinding.SetPrimitiveTopology(.TriangleList);
instancingBinding.SetVertexBufferSlot(quadBinding.GetVertexBuffer(0), 0);
instancingBinding.SetVertexBufferSlot(instanceBuffer, 1);
@@ -40,6 +40,21 @@ namespace GlitchyEngine.Renderer
_defaultBinding = .(this, (.)_vertexType.Stride, 0);
}
public this(GraphicsContext context, uint32 vertexStride, uint32 vertexCount, Usage usage = .Default, CPUAccessFlags cpuAccess = .None) : base(context)
{
_vertexType = null;
_description = .(){
Size = (vertexStride * vertexCount),
Usage = usage,
CPUAccess = cpuAccess,
BindFlags = .Vertex,
MiscFlags = .None
};
_defaultBinding = .(this, vertexStride, 0);
}
[Inline]
public static implicit operator VertexBufferBinding(Self buffer) => buffer._defaultBinding;
}
+25 -4
View File
@@ -24,6 +24,10 @@ namespace GlitchyEngine.Renderer
* The semantic associated with this element in a shader input-signature.
*/
public String SemanticName;
/**
* If set to True, the VertexLayout will delete SemanticName once it's reference count is 0.
*/
public bool OwnsName;
/**
* The semantic index for the element.
* A semantic index modifies a semantic, with an integer index number.
@@ -56,10 +60,11 @@ namespace GlitchyEngine.Renderer
public this() => this = default;
public this(Format format, String semanticName, uint32 semanticIndex = 0, uint32 inputSlot = 0, uint32 offset = (.)-1, InputClassification slotClass = .PerVertexData, uint32 instanceStepRate = 0)
public this(Format format, String semanticName, bool ownsName = false, uint32 semanticIndex = 0, uint32 inputSlot = 0, uint32 offset = (.)-1, InputClassification slotClass = .PerVertexData, uint32 instanceStepRate = 0)
{
Format = format;
SemanticName = semanticName;
OwnsName = ownsName;
SemanticIndex = semanticIndex;
InputSlot = inputSlot;
AlignedByteOffset = offset;
@@ -73,24 +78,40 @@ namespace GlitchyEngine.Renderer
public static readonly uint32 AppendAligned = 0xffffffff;
}
public class VertexLayout
public class VertexLayout : RefCounted
{
private GraphicsContext _context ~ _?.ReleaseRef();
private VertexElement[] _elements;
private bool _ownsElements;
public GraphicsContext Context => _context;
public VertexElement[] Elements => _elements;
public this(GraphicsContext context, VertexElement[] elements, VertexShader vertexShader)
public this(GraphicsContext context, VertexElement[] elements, bool ownsElements, VertexShader vertexShader)
{
_context = context..AddRef();
_elements = elements;
_ownsElements = ownsElements;
CreateNativeLayout();
}
public ~this()
{
if(_ownsElements)
{
for(var element in _elements)
{
if(element.OwnsName)
delete element.SemanticName;
}
delete _elements;
}
}
protected extern void CreateNativeLayout();
}
+5 -5
View File
@@ -50,8 +50,6 @@ namespace Sandbox
}
}
VertexLayout _vertexLayout ~ delete _;
GeometryBinding _geometryBinding ~ _?.ReleaseRef();
GeometryBinding _quadGeometryBinding ~ _?.ReleaseRef();
@@ -91,7 +89,7 @@ namespace Sandbox
// Create Input Layout
_vertexLayout = new VertexLayout(_context, VertexColorTexture.VertexElements, textureEffect.VertexShader);
VertexLayout vertexLayout = new VertexLayout(_context, VertexColorTexture.VertexElements, false, textureEffect.VertexShader);
textureEffect.ReleaseRef();
@@ -99,7 +97,7 @@ namespace Sandbox
{
_geometryBinding = new GeometryBinding(_context);
_geometryBinding.SetPrimitiveTopology(.TriangleList);
_geometryBinding.SetVertexLayout(_vertexLayout);
_geometryBinding.SetVertexLayout(vertexLayout);
float pO3 = Math.PI_f / 3.0f;
VertexColorTexture[?] vertices = .(
@@ -135,7 +133,7 @@ namespace Sandbox
{
_quadGeometryBinding = new GeometryBinding(_context);
_quadGeometryBinding.SetPrimitiveTopology(.TriangleList);
_quadGeometryBinding.SetVertexLayout(_vertexLayout);
_quadGeometryBinding.SetVertexLayout(vertexLayout);
VertexColorTexture[?] vertices = .(
VertexColorTexture(Vector3(-0.75f, 0.75f, 0), Color.White, .(0, 0)),
@@ -159,6 +157,8 @@ namespace Sandbox
qib.ReleaseRef();
}
vertexLayout.ReleaseRef();
// Create rasterizer state
RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
_rasterizerState = new RasterizerState(_context, rsDesc);