Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added compute shader support for vulkan #2685

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

johang88
Copy link
Contributor

PR Details

Vulkan graphics backend has been modified to support compute shaders, additional modifications were also made to the shader compiler so that correct glsl compute shaders can be generated.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My change requires a change to the documentation.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have built and run the editor to try this change out.

…ayer errors.

NoSampler was created using the default linear sampler, this caused validation errors
when using Texture.Load with Texture3d(r16b16f) as it does not support linear sampling.

The sampler was changed to use a point sampler instead which prevents the errors and should cause no issues.
Added locking to upload buffer creation and internal queue submit as 0
they are called from multiple threads if texture streaming is eanbled.
@johang88
Copy link
Contributor Author

I have only tested with a simple compute shader so far but seems to work fine.

Sample code, note that the sprite rendering can cause some validation errors if done after the forward renderer, there are no validation errors if the sprite rendering is removed, but then I have to validate the output in renderdoc :D

public class ComputeShaderRenderer : SceneRendererBase
{
    private ComputeEffectShader _shader;
    private Texture _texture;

    private SpriteBatch _sprite;

    protected override void DrawCore(RenderContext context, RenderDrawContext drawContext)
    {
        _shader ??= new(context) { ShaderSourceName = "TestShader" };
        _texture ??= Texture.New2D(context.GraphicsDevice, 64, 64, PixelFormat.R8G8B8A8_UNorm, TextureFlags.UnorderedAccess | TextureFlags.ShaderResource);
        _sprite ??= new(context.GraphicsDevice);

        drawContext.CommandList.ResourceBarrierTransition(_texture, GraphicsResourceState.UnorderedAccess);

        _shader.Parameters.Set(TestShaderKeys.TestTexture, _texture);
        _shader.ThreadGroupCounts = new Int3(64 / 8, 64 / 8, 1);
        _shader.ThreadNumbers = new Int3(8, 8, 1);

        _shader.Draw(drawContext, "TestShader");

        drawContext.CommandList.ResourceBarrierTransition(_texture, GraphicsResourceState.PixelShaderResource);

        _sprite.Begin(drawContext.GraphicsContext, SpriteSortMode.Immediate);
        _sprite.Draw(_texture, new(100, 100));
        _sprite.End();
    }
}

Shader:

shader TestShader : ComputeShaderBase
{
	rgroup Test
	{
		stage RWTexture2D<float4> TestTexture;
	}

	override void Compute()
	{
		TestTexture[streams.DispatchThreadId.xy] = float4((float)streams.DispatchThreadId.x / 64, (float)streams.DispatchThreadId.y / 64, 0, 1);
	}
};

Mandatory screenshot of the very fancy rectangle:
bild

@johang88 johang88 marked this pull request as draft March 22, 2025 10:59
Vulkan graphics backend has been modified to support compute shaders,
additional modifications were also made to the shader compiler so that correct
glsl compute shaders can be generated.
@johang88
Copy link
Contributor Author

johang88 commented Mar 22, 2025

Got my FFT shaders to work, it seems to work pretty well now. Note the commits from #2684 are currently included.

I got rid of the NoSampler sampler hack and am using GL_EXT_samplerless_texture_functions, not sure if it's a good idea or not but it looks like it should be supported pretty well and it's easier to manage.

I also noticed that the resource barrier api is a bit lacking as it wouldn't insert a barrier as my texture was already in UnorderedAccess, I had to insert an extra transition just to get the barrier in place. Not sure if we want to remove the check that skips the transition or maybe add an extra method argument that can force the barrier to be inserted? My hack works but it's not neat ...

context.CommandList.ResourceBarrierTransition(buffer, GraphicsResourceState.PixelShaderResource);
context.CommandList.ResourceBarrierTransition(buffer, GraphicsResourceState.UnorderedAccess);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant