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