remove params from BeginRenderPass and PushUniforms

pull/35/head
cosmonaut 2022-11-17 00:12:50 -08:00
parent 970517c3c2
commit 99a7064e2e
1 changed files with 371 additions and 93 deletions

View File

@ -29,40 +29,164 @@ namespace MoonWorks.Graphics
currentSampleCount = SampleCount.One;
}
// FIXME: we can probably use the NativeMemory functions to not have to generate arrays here
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="colorAttachmentInfo">The color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
ColorAttachmentInfo colorAttachmentInfo
)
{
#if DEBUG
AssertTextureNotNull(colorAttachmentInfo);
AssertColorTarget(colorAttachmentInfo);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[1];
refreshColorAttachmentInfos[0] = colorAttachmentInfo.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
(IntPtr) refreshColorAttachmentInfos,
1,
IntPtr.Zero
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="colorAttachmentInfos">The color attachments to use in the render pass.</param>
/// <param name="colorAttachmentInfoOne">The first color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoTwo">The second color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
params ColorAttachmentInfo[] colorAttachmentInfos
ColorAttachmentInfo colorAttachmentInfoOne,
ColorAttachmentInfo colorAttachmentInfoTwo
)
{
#if DEBUG
AssertValidColorAttachments(colorAttachmentInfos, true);
AssertTextureNotNull(colorAttachmentInfoOne);
AssertColorTarget(colorAttachmentInfoOne);
AssertTextureNotNull(colorAttachmentInfoTwo);
AssertColorTarget(colorAttachmentInfoTwo);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
#endif
var refreshColorAttachmentInfos = new Refresh.ColorAttachmentInfo[colorAttachmentInfos.Length];
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[2];
refreshColorAttachmentInfos[0] = colorAttachmentInfoOne.ToRefresh();
refreshColorAttachmentInfos[1] = colorAttachmentInfoTwo.ToRefresh();
for (var i = 0; i < colorAttachmentInfos.Length; i += 1)
{
refreshColorAttachmentInfos[i] = colorAttachmentInfos[i].ToRefresh();
}
fixed (Refresh.ColorAttachmentInfo* pColorAttachmentInfos = refreshColorAttachmentInfos)
{
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
(IntPtr) pColorAttachmentInfos,
(uint) colorAttachmentInfos.Length,
(IntPtr) refreshColorAttachmentInfos,
2,
IntPtr.Zero
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="colorAttachmentInfoOne">The first color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoTwo">The second color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoThree">The third color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
ColorAttachmentInfo colorAttachmentInfoOne,
ColorAttachmentInfo colorAttachmentInfoTwo,
ColorAttachmentInfo colorAttachmentInfoThree
)
{
#if DEBUG
AssertTextureNotNull(colorAttachmentInfoOne);
AssertColorTarget(colorAttachmentInfoOne);
AssertTextureNotNull(colorAttachmentInfoTwo);
AssertColorTarget(colorAttachmentInfoTwo);
AssertTextureNotNull(colorAttachmentInfoThree);
AssertColorTarget(colorAttachmentInfoThree);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoThree);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[3];
refreshColorAttachmentInfos[0] = colorAttachmentInfoOne.ToRefresh();
refreshColorAttachmentInfos[1] = colorAttachmentInfoTwo.ToRefresh();
refreshColorAttachmentInfos[2] = colorAttachmentInfoThree.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
(IntPtr) refreshColorAttachmentInfos,
3,
IntPtr.Zero
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="colorAttachmentInfoOne">The first color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoTwo">The second color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoThree">The third color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoFour">The four color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
ColorAttachmentInfo colorAttachmentInfoOne,
ColorAttachmentInfo colorAttachmentInfoTwo,
ColorAttachmentInfo colorAttachmentInfoThree,
ColorAttachmentInfo colorAttachmentInfoFour
)
{
#if DEBUG
AssertTextureNotNull(colorAttachmentInfoOne);
AssertColorTarget(colorAttachmentInfoOne);
AssertTextureNotNull(colorAttachmentInfoTwo);
AssertColorTarget(colorAttachmentInfoTwo);
AssertTextureNotNull(colorAttachmentInfoThree);
AssertColorTarget(colorAttachmentInfoThree);
AssertTextureNotNull(colorAttachmentInfoFour);
AssertColorTarget(colorAttachmentInfoFour);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoThree);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoFour);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[4];
refreshColorAttachmentInfos[0] = colorAttachmentInfoOne.ToRefresh();
refreshColorAttachmentInfos[1] = colorAttachmentInfoTwo.ToRefresh();
refreshColorAttachmentInfos[2] = colorAttachmentInfoThree.ToRefresh();
refreshColorAttachmentInfos[3] = colorAttachmentInfoFour.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
(IntPtr) refreshColorAttachmentInfos,
4,
IntPtr.Zero
);
renderPassActive = true;
}
@ -72,37 +196,209 @@ namespace MoonWorks.Graphics
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="depthStencilAttachmentInfo">The depth stencil attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfos">The color attachments to use in the render pass.</param>
public unsafe void BeginRenderPass(
DepthStencilAttachmentInfo depthStencilAttachmentInfo,
params ColorAttachmentInfo[] colorAttachmentInfos
DepthStencilAttachmentInfo depthStencilAttachmentInfo
)
{
#if DEBUG
AssertValidDepthAttachment(depthStencilAttachmentInfo);
AssertValidColorAttachments(colorAttachmentInfos, false);
#endif
var refreshColorAttachmentInfos = new Refresh.ColorAttachmentInfo[colorAttachmentInfos.Length];
for (var i = 0; i < colorAttachmentInfos.Length; i += 1)
{
refreshColorAttachmentInfos[i] = colorAttachmentInfos[i].ToRefresh();
}
var refreshDepthStencilAttachmentInfo = depthStencilAttachmentInfo.ToRefresh();
fixed (Refresh.ColorAttachmentInfo* pColorAttachmentInfos = refreshColorAttachmentInfos)
{
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
pColorAttachmentInfos,
(uint) colorAttachmentInfos.Length,
(Refresh.ColorAttachmentInfo*) IntPtr.Zero,
0,
&refreshDepthStencilAttachmentInfo
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="depthStencilAttachmentInfo">The depth stencil attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfo">The color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
DepthStencilAttachmentInfo depthStencilAttachmentInfo,
ColorAttachmentInfo colorAttachmentInfo
)
{
#if DEBUG
AssertValidDepthAttachment(depthStencilAttachmentInfo);
AssertTextureNotNull(colorAttachmentInfo);
AssertColorTarget(colorAttachmentInfo);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[1];
refreshColorAttachmentInfos[0] = colorAttachmentInfo.ToRefresh();
var refreshDepthStencilAttachmentInfo = depthStencilAttachmentInfo.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
refreshColorAttachmentInfos,
1,
&refreshDepthStencilAttachmentInfo
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="depthStencilAttachmentInfo">The depth stencil attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoOne">The first color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoTwo">The second color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
DepthStencilAttachmentInfo depthStencilAttachmentInfo,
ColorAttachmentInfo colorAttachmentInfoOne,
ColorAttachmentInfo colorAttachmentInfoTwo
)
{
#if DEBUG
AssertValidDepthAttachment(depthStencilAttachmentInfo);
AssertTextureNotNull(colorAttachmentInfoOne);
AssertColorTarget(colorAttachmentInfoOne);
AssertTextureNotNull(colorAttachmentInfoTwo);
AssertColorTarget(colorAttachmentInfoTwo);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[2];
refreshColorAttachmentInfos[0] = colorAttachmentInfoOne.ToRefresh();
refreshColorAttachmentInfos[1] = colorAttachmentInfoTwo.ToRefresh();
var refreshDepthStencilAttachmentInfo = depthStencilAttachmentInfo.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
refreshColorAttachmentInfos,
2,
&refreshDepthStencilAttachmentInfo
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="depthStencilAttachmentInfo">The depth stencil attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoOne">The first color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoTwo">The second color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoThree">The third color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
DepthStencilAttachmentInfo depthStencilAttachmentInfo,
ColorAttachmentInfo colorAttachmentInfoOne,
ColorAttachmentInfo colorAttachmentInfoTwo,
ColorAttachmentInfo colorAttachmentInfoThree
)
{
#if DEBUG
AssertValidDepthAttachment(depthStencilAttachmentInfo);
AssertTextureNotNull(colorAttachmentInfoOne);
AssertColorTarget(colorAttachmentInfoOne);
AssertTextureNotNull(colorAttachmentInfoTwo);
AssertColorTarget(colorAttachmentInfoTwo);
AssertTextureNotNull(colorAttachmentInfoThree);
AssertColorTarget(colorAttachmentInfoThree);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[3];
refreshColorAttachmentInfos[0] = colorAttachmentInfoOne.ToRefresh();
refreshColorAttachmentInfos[1] = colorAttachmentInfoTwo.ToRefresh();
refreshColorAttachmentInfos[2] = colorAttachmentInfoThree.ToRefresh();
var refreshDepthStencilAttachmentInfo = depthStencilAttachmentInfo.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
refreshColorAttachmentInfos,
3,
&refreshDepthStencilAttachmentInfo
);
renderPassActive = true;
}
/// <summary>
/// Begins a render pass.
/// All render state, resource binding, and draw commands must be made within a render pass.
/// It is an error to call this after calling BeginRenderPass but before calling EndRenderPass.
/// </summary>
/// <param name="depthStencilAttachmentInfo">The depth stencil attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoOne">The first color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoTwo">The second color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoThree">The third color attachment to use in the render pass.</param>
/// <param name="colorAttachmentInfoFour">The four color attachment to use in the render pass.</param>
public unsafe void BeginRenderPass(
DepthStencilAttachmentInfo depthStencilAttachmentInfo,
ColorAttachmentInfo colorAttachmentInfoOne,
ColorAttachmentInfo colorAttachmentInfoTwo,
ColorAttachmentInfo colorAttachmentInfoThree,
ColorAttachmentInfo colorAttachmentInfoFour
)
{
#if DEBUG
AssertValidDepthAttachment(depthStencilAttachmentInfo);
AssertTextureNotNull(colorAttachmentInfoOne);
AssertColorTarget(colorAttachmentInfoOne);
AssertTextureNotNull(colorAttachmentInfoTwo);
AssertColorTarget(colorAttachmentInfoTwo);
AssertTextureNotNull(colorAttachmentInfoThree);
AssertColorTarget(colorAttachmentInfoThree);
AssertTextureNotNull(colorAttachmentInfoFour);
AssertColorTarget(colorAttachmentInfoFour);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoTwo);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoThree);
AssertSameSampleCount(colorAttachmentInfoOne, colorAttachmentInfoFour);
#endif
var refreshColorAttachmentInfos = stackalloc Refresh.ColorAttachmentInfo[4];
refreshColorAttachmentInfos[0] = colorAttachmentInfoOne.ToRefresh();
refreshColorAttachmentInfos[1] = colorAttachmentInfoTwo.ToRefresh();
refreshColorAttachmentInfos[2] = colorAttachmentInfoThree.ToRefresh();
refreshColorAttachmentInfos[3] = colorAttachmentInfoFour.ToRefresh();
var refreshDepthStencilAttachmentInfo = depthStencilAttachmentInfo.ToRefresh();
Refresh.Refresh_BeginRenderPass(
Device.Handle,
Handle,
refreshColorAttachmentInfos,
4,
&refreshDepthStencilAttachmentInfo
);
renderPassActive = true;
}
@ -478,7 +774,7 @@ namespace MoonWorks.Graphics
/// </summary>
/// <returns>A starting offset value to be used with draw calls.</returns>
public unsafe uint PushVertexShaderUniforms<T>(
params T[] uniforms
T uniforms
) where T : unmanaged
{
#if DEBUG
@ -490,23 +786,20 @@ namespace MoonWorks.Graphics
}
#endif
fixed (T* ptr = &uniforms[0])
{
return Refresh.Refresh_PushVertexShaderUniforms(
Device.Handle,
Handle,
(IntPtr) ptr,
(uint) (uniforms.Length * sizeof(T))
(IntPtr) (&uniforms),
(uint) sizeof(T)
);
}
}
/// <summary>
/// Pushes fragment shader uniforms to the device.
/// </summary>
/// <returns>A starting offset to be used with draw calls.</returns>
public unsafe uint PushFragmentShaderUniforms<T>(
params T[] uniforms
T uniforms
) where T : unmanaged
{
#if DEBUG
@ -518,23 +811,20 @@ namespace MoonWorks.Graphics
}
#endif
fixed (T* ptr = &uniforms[0])
{
return Refresh.Refresh_PushFragmentShaderUniforms(
Device.Handle,
Handle,
(IntPtr) ptr,
(uint) (uniforms.Length * sizeof(T))
(IntPtr) (&uniforms),
(uint) sizeof(T)
);
}
}
/// <summary>
/// Pushes compute shader uniforms to the device.
/// </summary>
/// <returns>A starting offset to be used with dispatch calls.</returns>
public unsafe uint PushComputeShaderUniforms<T>(
params T[] uniforms
T uniforms
) where T : unmanaged
{
#if DEBUG
@ -546,15 +836,13 @@ namespace MoonWorks.Graphics
}
#endif
fixed (T* ptr = &uniforms[0])
{
return Refresh.Refresh_PushComputeShaderUniforms(
Device.Handle,
Handle,
(IntPtr) ptr,
(uint) (uniforms.Length * sizeof(T))
(IntPtr) (&uniforms),
(uint) sizeof(T)
);
}
}
/// <summary>
@ -1019,39 +1307,29 @@ namespace MoonWorks.Graphics
}
}
private void AssertValidColorAttachments(ColorAttachmentInfo[] colorAttachmentInfos, bool atLeastOneRequired)
private void AssertTextureNotNull(ColorAttachmentInfo colorAttachmentInfo)
{
if (atLeastOneRequired && colorAttachmentInfos.Length == 0)
{
throw new System.ArgumentException("Render pass must contain at least one attachment!");
}
currentSampleCount = (colorAttachmentInfos.Length > 0) ? colorAttachmentInfos[0].SampleCount : SampleCount.One;
if (colorAttachmentInfos.Length > 4)
{
throw new System.ArgumentException("Render pass cannot have more than 4 color attachments!");
}
for (int i = 0; i < colorAttachmentInfos.Length; i += 1)
{
if (colorAttachmentInfos[i].Texture == null ||
colorAttachmentInfos[i].Texture.Handle == IntPtr.Zero)
if (colorAttachmentInfo.Texture == null || colorAttachmentInfo.Texture.Handle == IntPtr.Zero)
{
throw new System.ArgumentException("Render pass color attachment Texture cannot be null!");
}
}
if ((colorAttachmentInfos[i].Texture.UsageFlags & TextureUsageFlags.ColorTarget) == 0)
private void AssertColorTarget(ColorAttachmentInfo colorAttachmentInfo)
{
if ((colorAttachmentInfo.Texture.UsageFlags & TextureUsageFlags.ColorTarget) == 0)
{
throw new System.ArgumentException("Render pass color attachment UsageFlags must include TextureUsageFlags.ColorTarget!");
}
}
if (colorAttachmentInfos[i].SampleCount != currentSampleCount)
private void AssertSameSampleCount(ColorAttachmentInfo a, ColorAttachmentInfo b)
{
if (a.SampleCount != b.SampleCount)
{
throw new System.ArgumentException("All color attachments in a render pass must have the same SampleCount!");
}
}
}
private void AssertValidDepthAttachment(DepthStencilAttachmentInfo depthStencilAttachmentInfo)
{