Skip to content

Commit

Permalink
[Metal] Only use multiple viewports if it's supported in the current …
Browse files Browse the repository at this point in the history
…device.
  • Loading branch information
mellinoe committed Mar 22, 2018
1 parent 624f70d commit c0c7af0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/Veldrid.MetalBindings/MTLRenderCommandEncoder.cs
Expand Up @@ -89,9 +89,15 @@ public void drawPrimitives(MTLPrimitiveType primitiveType, MTLBuffer indirectBuf
indirectBuffer,
indirectBufferOffset);

public unsafe void setViewport(MTLViewport viewport)
=> objc_msgSend(NativePtr, sel_setViewport, viewport);

public unsafe void setViewports(MTLViewport* viewports, UIntPtr count)
=> objc_msgSend(NativePtr, sel_setViewports, viewports, count);

public unsafe void setScissorRect(MTLScissorRect scissorRect)
=> objc_msgSend(NativePtr, sel_setScissorRect, scissorRect);

public unsafe void setScissorRects(MTLScissorRect* scissorRects, UIntPtr count)
=> objc_msgSend(NativePtr, sel_setScissorRects, scissorRects, count);

Expand Down Expand Up @@ -130,7 +136,9 @@ public void setTriangleFillMode(MTLTriangleFillMode fillMode)
private static readonly Selector sel_drawIndexedPrimitives0 = "drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:";
private static readonly Selector sel_drawIndexedPrimitives1 = "drawIndexedPrimitives:indexCount:indexType:indexBuffer:indexBufferOffset:instanceCount:baseVertex:baseInstance:";
private static readonly Selector sel_drawIndexedPrimitives2 = "drawIndexedPrimitives:indexType:indexBuffer:indexBufferOffset:indirectBuffer:indirectBufferOffset:";
private static readonly Selector sel_setViewport = "setViewport:";
private static readonly Selector sel_setViewports = "setViewports:count:";
private static readonly Selector sel_setScissorRect = "setScissorRect:";
private static readonly Selector sel_setScissorRects = "setScissorRects:count:";
private static readonly Selector sel_setCullMode = "setCullMode:";
private static readonly Selector sel_setFrontFacingWinding = "setFrontFacingWinding:";
Expand Down
2 changes: 2 additions & 0 deletions src/Veldrid.MetalBindings/ObjectiveCRuntime.cs
Expand Up @@ -45,6 +45,8 @@ public static unsafe class ObjectiveCRuntime
[DllImport(ObjCLibrary, EntryPoint = "objc_msgSend")]
public static extern void objc_msgSend(IntPtr receiver, Selector selector, MTLViewport a);
[DllImport(ObjCLibrary, EntryPoint = "objc_msgSend")]
public static extern void objc_msgSend(IntPtr receiver, Selector selector, MTLScissorRect a);
[DllImport(ObjCLibrary, EntryPoint = "objc_msgSend")]
public static extern void objc_msgSend(IntPtr receiver, Selector selector, void* a, UIntPtr b);
[DllImport(ObjCLibrary, EntryPoint = "objc_msgSend")]
public static extern void objc_msgSend(IntPtr receiver, Selector selector, MTLPrimitiveType a, UIntPtr b, MTLIndexType c, IntPtr d, UIntPtr e, UIntPtr f);
Expand Down
40 changes: 32 additions & 8 deletions src/Veldrid/MTL/MTLCommandList.cs
Expand Up @@ -148,18 +148,12 @@ private bool PreDrawCommand()
{
if (_viewportsChanged)
{
fixed (MTLViewport* viewportsPtr = &_viewports[0])
{
_rce.setViewports(viewportsPtr, (UIntPtr)_viewportCount);
}
FlushViewports();
_viewportsChanged = false;
}
if (_scissorRectsChanged && _graphicsPipeline.ScissorTestEnabled)
{
fixed (MTLScissorRect* scissorRectsPtr = &_scissorRects[0])
{
_rce.setScissorRects(scissorRectsPtr, (UIntPtr)_viewportCount);
}
FlushScissorRects();
_scissorRectsChanged = false;
}
if (_graphicsPipelineChanged)
Expand Down Expand Up @@ -201,6 +195,36 @@ private bool PreDrawCommand()
}


private void FlushViewports()
{
if (_gd.Features.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v3))
{
fixed (MTLViewport* viewportsPtr = &_viewports[0])
{
_rce.setViewports(viewportsPtr, (UIntPtr)_viewportCount);
}
}
else
{
_rce.setViewport(_viewports[0]);
}
}

private void FlushScissorRects()
{
if (_gd.Features.IsSupported(MTLFeatureSet.macOS_GPUFamily1_v3))
{
fixed (MTLScissorRect* scissorRectsPtr = &_scissorRects[0])
{
_rce.setScissorRects(scissorRectsPtr, (UIntPtr)_viewportCount);
}
}
else
{
_rce.setScissorRect(_scissorRects[0]);
}
}

private void PreComputeCommand()
{
EnsureComputeEncoder();
Expand Down

0 comments on commit c0c7af0

Please sign in to comment.