@@ -13,6 +13,8 @@ public sealed unsafe class GpuComputePass : Poolable<GpuComputePass>
1313 internal SDL_GPUComputePass * Handle ;
1414#pragma warning restore SA1401
1515
16+ private bool _isPipelineBound ;
17+
1618 /// <summary>
1719 /// Gets the <see cref="GpuDevice" /> instance associated with the compute pass.
1820 /// </summary>
@@ -37,6 +39,7 @@ internal GpuComputePass(GpuDevice device)
3739 public void BindShader ( GpuComputeShader computeShader )
3840 {
3941 SDL_BindGPUComputePipeline ( Handle , computeShader . HandleTyped ) ;
42+ _isPipelineBound = true ;
4043 }
4144
4245 /// <summary>
@@ -50,12 +53,97 @@ public void Dispatch(
5053 int workGroupsCountY ,
5154 int workGroupsCountZ )
5255 {
56+ if ( ! _isPipelineBound )
57+ {
58+ throw new InvalidOperationException ( "A compute shader must be bound before dispatching." ) ;
59+ }
60+
5361 SDL_DispatchGPUCompute (
5462 Handle , ( uint ) workGroupsCountX , ( uint ) workGroupsCountY , ( uint ) workGroupsCountZ ) ;
5563 }
5664
5765 /// <summary>
58- /// Ends the compute pass.
66+ /// Dispatches compute work with parameters set from a buffer.
67+ /// </summary>
68+ /// <param name="buffer">The buffer containing dispatch parameters.</param>
69+ /// <param name="offset">the offset to start reading from the dispatch buffer.</param>
70+ public void DispatchIndirect ( GpuDataBuffer buffer , int offset )
71+ {
72+ if ( ! _isPipelineBound )
73+ {
74+ throw new InvalidOperationException ( "A compute shader must be bound before dispatching." ) ;
75+ }
76+
77+ SDL_DispatchGPUComputeIndirect ( Handle , buffer . HandleTyped , ( uint ) offset ) ;
78+ }
79+
80+ /// <summary>
81+ /// Binds storage textures as readonly for use on the compute pipeline.
82+ /// </summary>
83+ /// <param name="startIndex">Index of the slot to begin binding from.</param>
84+ /// <param name="textures">An array of <see cref="GpuTexture"/>s to bind.</param>
85+ public void BindStorageTextures ( int startIndex , params ReadOnlySpan < GpuTexture > textures )
86+ {
87+ var handles = stackalloc SDL_GPUTexture * [ textures . Length ] ;
88+ for ( var i = 0 ; i < textures . Length ; i ++ )
89+ {
90+ handles [ i ] = ( SDL_GPUTexture * ) textures [ i ] . Handle ;
91+ }
92+
93+ SDL_BindGPUComputeStorageTextures (
94+ Handle ,
95+ ( uint ) startIndex ,
96+ handles ,
97+ ( uint ) textures . Length ) ;
98+ }
99+
100+ /// <summary>
101+ /// Binds storage buffers as readonly for use on the compute pipeline.
102+ /// </summary>
103+ /// <param name="startIndex">Index of the slot to begin binding from.</param>
104+ /// <param name="buffers">An array of <see cref="GpuDataBuffer"/>s to bind.</param>
105+ public void BindStorageBuffers ( int startIndex , params ReadOnlySpan < GpuDataBuffer > buffers )
106+ {
107+ var handles = stackalloc SDL_GPUBuffer * [ buffers . Length ] ;
108+ for ( var i = 0 ; i < buffers . Length ; i ++ )
109+ {
110+ handles [ i ] = ( SDL_GPUBuffer * ) buffers [ i ] . Handle ;
111+ }
112+
113+ SDL_BindGPUComputeStorageBuffers (
114+ Handle ,
115+ ( uint ) startIndex ,
116+ handles ,
117+ ( uint ) buffers . Length ) ;
118+ }
119+
120+ /// <summary>
121+ /// Binds texture-sampler pairs for use on the compute shader pipeline.
122+ /// </summary>
123+ /// <param name="startIndex">Index of the slot to begin binding from.</param>
124+ /// <param name="samplers">An array of <see cref="GpuTexture"/> and <see cref="GpuSampler"/> pairs to bind.</param>
125+ public void BindSamplers (
126+ int startIndex ,
127+ params ReadOnlySpan < ( GpuTexture Texture , GpuSampler Sampler ) > samplers )
128+ {
129+ var bindings = stackalloc SDL_GPUTextureSamplerBinding [ samplers . Length ] ;
130+ for ( var i = 0 ; i < samplers . Length ; i ++ )
131+ {
132+ var src = samplers [ i ] ;
133+ ref var dst = ref bindings [ i ] ;
134+ dst . texture = ( SDL_GPUTexture * ) src . Texture . Handle ;
135+ dst . sampler = ( SDL_GPUSampler * ) src . Sampler . Handle ;
136+ }
137+
138+ SDL_BindGPUComputeSamplers (
139+ Handle ,
140+ ( uint ) startIndex ,
141+ bindings ,
142+ ( uint ) samplers . Length ) ;
143+ }
144+
145+ /// <summary>
146+ /// Ends a render pass.
59147 /// </summary>
60148 /// <exception cref="InvalidOperationException">The associated command buffer was submitted.</exception>
61149 public void End ( )
@@ -68,5 +156,6 @@ public void End()
68156 protected override void Reset ( )
69157 {
70158 Device . EndComputePassTryInternal ( this ) ;
159+ _isPipelineBound = false ;
71160 }
72161}
0 commit comments