compute pass + additional validation

what_if_no_video_threads
cosmonaut 2024-02-23 10:43:39 -08:00
parent 0a5ec9e82d
commit ad97aed60f
1 changed files with 91 additions and 23 deletions

View File

@ -525,6 +525,19 @@ namespace MoonWorks.Graphics
#endif #endif
} }
public void BeginComputePass()
{
#if DEBUG
AssertNotInPass("Cannot begin compute pass while in another pass!");
computePassActive = true;
#endif
Refresh.Refresh_BeginComputePass(
Device.Handle,
Handle
);
}
/// <summary> /// <summary>
/// Binds a compute pipeline so that compute work may be dispatched. /// Binds a compute pipeline so that compute work may be dispatched.
/// </summary> /// </summary>
@ -534,6 +547,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute pipeline outside of compute pass!");
#endif #endif
Refresh.Refresh_BindComputePipeline( Refresh.Refresh_BindComputePipeline(
@ -556,6 +570,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute buffers outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeBufferCount(1); AssertComputeBufferCount(1);
#endif #endif
@ -581,6 +596,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute buffers outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeBufferCount(2); AssertComputeBufferCount(2);
#endif #endif
@ -609,6 +625,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute buffers outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeBufferCount(3); AssertComputeBufferCount(3);
#endif #endif
@ -640,6 +657,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute buffers outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeBufferCount(4); AssertComputeBufferCount(4);
#endif #endif
@ -666,6 +684,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute buffers outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeBufferCount(buffers.Length); AssertComputeBufferCount(buffers.Length);
#endif #endif
@ -693,6 +712,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute textures outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeTextureCount(1); AssertComputeTextureCount(1);
#endif #endif
@ -722,6 +742,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute textures outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeTextureCount(2); AssertComputeTextureCount(2);
#endif #endif
@ -755,6 +776,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute textures outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeTextureCount(3); AssertComputeTextureCount(3);
#endif #endif
@ -792,6 +814,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute textures outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeTextureCount(4); AssertComputeTextureCount(4);
#endif #endif
@ -825,6 +848,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot bind compute textures outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
AssertComputeTextureCount(bindings.Length); AssertComputeTextureCount(bindings.Length);
#endif #endif
@ -860,6 +884,7 @@ namespace MoonWorks.Graphics
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInComputePass("Cannot dispatch compute outside of compute pass!");
AssertComputePipelineBound(); AssertComputePipelineBound();
if (groupCountX < 1 || groupCountY < 1 || groupCountZ < 1) if (groupCountX < 1 || groupCountY < 1 || groupCountZ < 1)
@ -877,6 +902,19 @@ namespace MoonWorks.Graphics
); );
} }
public void EndComputePass()
{
#if DEBUG
AssertInComputePass("Cannot end compute pass while not in a compute pass!");
computePassActive = false;
#endif
Refresh.Refresh_EndComputePass(
Device.Handle,
Handle
);
}
/// <summary> /// <summary>
/// Binds a graphics pipeline so that rendering work may be performed. /// Binds a graphics pipeline so that rendering work may be performed.
/// </summary> /// </summary>
@ -1888,7 +1926,7 @@ namespace MoonWorks.Graphics
/// You MAY assume that the copy has finished for subsequent commands. /// You MAY assume that the copy has finished for subsequent commands.
/// </summary> /// </summary>
public void UploadToTexture( public void UploadToTexture(
TransferBuffer cpuBuffer, TransferBuffer transferBuffer,
in TextureSlice textureSlice, in TextureSlice textureSlice,
in BufferImageCopy copyParams in BufferImageCopy copyParams
) )
@ -1896,12 +1934,13 @@ namespace MoonWorks.Graphics
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot upload to texture outside of copy pass!"); AssertInCopyPass("Cannot upload to texture outside of copy pass!");
AssertBufferBoundsCheck(transferBuffer.Size, copyParams.BufferOffset, textureSlice.Size);
#endif #endif
Refresh.Refresh_UploadToTexture( Refresh.Refresh_UploadToTexture(
Device.Handle, Device.Handle,
Handle, Handle,
cpuBuffer.Handle, transferBuffer.Handle,
textureSlice.ToRefreshTextureSlice(), textureSlice.ToRefreshTextureSlice(),
copyParams.ToRefresh() copyParams.ToRefresh()
); );
@ -1911,11 +1950,11 @@ namespace MoonWorks.Graphics
/// Uploads the contents of an entire buffer to a texture with no mips. /// Uploads the contents of an entire buffer to a texture with no mips.
/// </summary> /// </summary>
public void UploadToTexture( public void UploadToTexture(
TransferBuffer cpuBuffer, TransferBuffer transferBuffer,
Texture texture Texture texture
) { ) {
UploadToTexture( UploadToTexture(
cpuBuffer, transferBuffer,
new TextureSlice(texture), new TextureSlice(texture),
new BufferImageCopy(0, 0, 0) new BufferImageCopy(0, 0, 0)
); );
@ -1931,19 +1970,21 @@ namespace MoonWorks.Graphics
/// You MAY assume that the copy has finished for subsequent commands. /// You MAY assume that the copy has finished for subsequent commands.
/// </summary> /// </summary>
public void UploadToBuffer( public void UploadToBuffer(
TransferBuffer cpuBuffer, TransferBuffer transferBuffer,
GpuBuffer gpuBuffer, GpuBuffer gpuBuffer,
in BufferCopy copyParams in BufferCopy copyParams
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot upload to texture outside of copy pass!"); AssertInCopyPass("Cannot upload to texture outside of copy pass!");
AssertBufferBoundsCheck(transferBuffer.Size, copyParams.SrcOffset, copyParams.Size);
AssertBufferBoundsCheck(gpuBuffer.Size, copyParams.DstOffset, copyParams.Size);
#endif #endif
Refresh.Refresh_UploadToBuffer( Refresh.Refresh_UploadToBuffer(
Device.Handle, Device.Handle,
Handle, Handle,
cpuBuffer.Handle, transferBuffer.Handle,
gpuBuffer.Handle, gpuBuffer.Handle,
copyParams.ToRefresh() copyParams.ToRefresh()
); );
@ -1953,19 +1994,13 @@ namespace MoonWorks.Graphics
/// Copies the entire contents of a CpuBuffer to a GpuBuffer. /// Copies the entire contents of a CpuBuffer to a GpuBuffer.
/// </summary> /// </summary>
public void UploadToBuffer( public void UploadToBuffer(
TransferBuffer cpuBuffer, TransferBuffer transferBuffer,
GpuBuffer gpuBuffer GpuBuffer gpuBuffer
) { ) {
#if DEBUG
if (cpuBuffer.Size > gpuBuffer.Size)
{
throw new InvalidOperationException("CpuBuffer copying to GpuBuffer is too large!");
}
#endif
UploadToBuffer( UploadToBuffer(
cpuBuffer, transferBuffer,
gpuBuffer, gpuBuffer,
new BufferCopy(0, 0, cpuBuffer.Size) new BufferCopy(0, 0, transferBuffer.Size)
); );
} }
@ -1978,19 +2013,20 @@ namespace MoonWorks.Graphics
/// </summary> /// </summary>
public void DownloadFromTexture( public void DownloadFromTexture(
in TextureSlice textureSlice, in TextureSlice textureSlice,
TransferBuffer cpuBuffer, TransferBuffer transferBuffer,
in BufferImageCopy copyParams in BufferImageCopy copyParams
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot download from texture outside of copy pass!"); AssertInCopyPass("Cannot download from texture outside of copy pass!");
AssertBufferBoundsCheck(transferBuffer.Size, copyParams.BufferOffset, textureSlice.Size);
#endif #endif
Refresh.Refresh_DownloadFromTexture( Refresh.Refresh_DownloadFromTexture(
Device.Handle, Device.Handle,
Handle, Handle,
textureSlice.ToRefreshTextureSlice(), textureSlice.ToRefreshTextureSlice(),
cpuBuffer.Handle, transferBuffer.Handle,
copyParams.ToRefresh() copyParams.ToRefresh()
); );
} }
@ -2000,11 +2036,11 @@ namespace MoonWorks.Graphics
/// </summary> /// </summary>
public void DownloadFromTexture( public void DownloadFromTexture(
Texture texture, Texture texture,
TransferBuffer cpuBuffer TransferBuffer transferBuffer
) { ) {
DownloadFromTexture( DownloadFromTexture(
new TextureSlice(texture), new TextureSlice(texture),
cpuBuffer, transferBuffer,
new BufferImageCopy(0, 0, 0) new BufferImageCopy(0, 0, 0)
); );
} }
@ -2018,23 +2054,42 @@ namespace MoonWorks.Graphics
/// </summary> /// </summary>
public void DownloadFromBuffer( public void DownloadFromBuffer(
GpuBuffer gpuBuffer, GpuBuffer gpuBuffer,
TransferBuffer cpuBuffer, TransferBuffer transferBuffer,
in BufferCopy copyParams in BufferCopy copyParams
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot download from texture outside of copy pass!"); AssertInCopyPass("Cannot download from texture outside of copy pass!");
AssertBufferBoundsCheck(transferBuffer.Size, copyParams.DstOffset, copyParams.Size);
#endif #endif
Refresh.Refresh_DownloadFromBuffer( Refresh.Refresh_DownloadFromBuffer(
Device.Handle, Device.Handle,
Handle, Handle,
gpuBuffer.Handle, gpuBuffer.Handle,
cpuBuffer.Handle, transferBuffer.Handle,
copyParams.ToRefresh() copyParams.ToRefresh()
); );
} }
/// <summary>
/// Downloads data from a GpuBuffer to a CpuBuffer.
/// This copy occurs on the GPU timeline.
///
/// You MAY NOT assume that the data in the CpuBuffer is
/// fully copied until the command buffer has finished execution.
/// </summary>
public void DownloadFromBuffer(
GpuBuffer gpuBuffer,
TransferBuffer transferBuffer
) {
DownloadFromBuffer(
gpuBuffer,
transferBuffer,
new BufferCopy(0, 0, gpuBuffer.Size)
);
}
/// <summary> /// <summary>
/// Copies the contents of a TextureSlice to another TextureSlice. /// Copies the contents of a TextureSlice to another TextureSlice.
/// The slices must have the same dimensions. /// The slices must have the same dimensions.
@ -2049,6 +2104,7 @@ namespace MoonWorks.Graphics
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot download from texture outside of copy pass!"); AssertInCopyPass("Cannot download from texture outside of copy pass!");
AssertTextureBoundsCheck(destination.Size, source.Size);
#endif #endif
Refresh.Refresh_CopyTextureToTexture( Refresh.Refresh_CopyTextureToTexture(
@ -2087,6 +2143,7 @@ namespace MoonWorks.Graphics
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot download from texture outside of copy pass!"); AssertInCopyPass("Cannot download from texture outside of copy pass!");
AssertBufferBoundsCheck(buffer.Size, copyParams.BufferOffset, textureSlice.Size);
#endif #endif
Refresh.Refresh_CopyTextureToBuffer( Refresh.Refresh_CopyTextureToBuffer(
@ -2119,19 +2176,20 @@ namespace MoonWorks.Graphics
/// You MAY assume that the copy has finished in subsequent commands. /// You MAY assume that the copy has finished in subsequent commands.
/// </summary> /// </summary>
public void CopyBufferToTexture( public void CopyBufferToTexture(
GpuBuffer buffer, GpuBuffer gpuBuffer,
in TextureSlice textureSlice, in TextureSlice textureSlice,
in BufferImageCopy copyParams in BufferImageCopy copyParams
) { ) {
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot download from texture outside of copy pass!"); AssertInCopyPass("Cannot download from texture outside of copy pass!");
AssertBufferBoundsCheck(gpuBuffer.Size, copyParams.BufferOffset, textureSlice.Size);
#endif #endif
Refresh.Refresh_CopyBufferToTexture( Refresh.Refresh_CopyBufferToTexture(
Device.Handle, Device.Handle,
Handle, Handle,
buffer.Handle, gpuBuffer.Handle,
textureSlice.ToRefreshTextureSlice(), textureSlice.ToRefreshTextureSlice(),
copyParams.ToRefresh() copyParams.ToRefresh()
); );
@ -2165,6 +2223,8 @@ namespace MoonWorks.Graphics
#if DEBUG #if DEBUG
AssertNotSubmitted(); AssertNotSubmitted();
AssertInCopyPass("Cannot download from texture outside of copy pass!"); AssertInCopyPass("Cannot download from texture outside of copy pass!");
AssertBufferBoundsCheck(source.Size, copyParams.SrcOffset, copyParams.Size);
AssertBufferBoundsCheck(destination.Size, copyParams.DstOffset, copyParams.Size);
#endif #endif
Refresh.Refresh_CopyBufferToBuffer( Refresh.Refresh_CopyBufferToBuffer(
@ -2421,6 +2481,14 @@ namespace MoonWorks.Graphics
} }
} }
private void AssertInComputePass(string message)
{
if (!computePassActive)
{
throw new System.InvalidOperationException(message);
}
}
private void AssertNotSubmitted() private void AssertNotSubmitted()
{ {
if (Submitted) if (Submitted)