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

[SUGGESTION] Remove ShaderBuiltins for VertexID, InstanceID, DispatchThreadID, GroupThreadID, IsFrontFace #67

Open
thargy opened this issue May 19, 2018 · 1 comment

Comments

@thargy
Copy link
Contributor

thargy commented May 19, 2018

Further to the extensive discussion in #65, a core goal would be to share shader code between the CPU and GPU, allowing ray-tracing, AI, etc. to be run on the CPU where compute shaders are not available, or even to maximise throughput by utilising both the CPU and GPU.

The ShaderBuiltins for VertexID, InstanceID, DispatchThreadID, GroupThreadID, IsFrontFace allow shader code to access these variables in an 'OpenGL style' as opposed to the 'Direct3D' style where the variables are passed into shaders as parameters.

For example DispatchThreadID is simply replaced with gl_GlobalInvocationID in OpenGL, but is added as an input parameter to the compute shader e.g.

[numthreads(1, 1, 1)]
void CS(uint3 _builtins_DispatchThreadID : SV_DispatchThreadID) { ... }

Although, this is a valid sylistic choice, as the aspiration is to be able to ultimately execute CS from the CPU it would be much easier to achieve that goal replacing code such as:

        // Current 'OpenGL' style
        [ComputeShader(1, 1, 1)]
        public void CS()
        {
            uint index = DispatchThreadID.X;
            ...
        }

with a pattern like:

        // Proposed 'Direct3D' style - with parameter name matching
        [ComputeShader(1, 1, 1)]
        public void CS(UInt3 dispatchThreadID)
        {
            uint index = dispatchThreadID.X;
            ...
        }

or, to future proof, prevent collisions, be less 'magic' and far more intelligible to consumers, allow a semantic attribute that supports parameter attachment:

        // Proposed 'Direct3D' style - with semantic attribute indicating which parameter is the dispatch id
        [ComputeShader(1, 1, 1)]
        public void CS([DispatchIDSemantic] UInt3 id)
        {
            uint index = id.X;
            ...
        }

This approach would make it much easier to call the compute shader directly from .NET code on the CPU. Currently ShaderBuiltins.DispatchID (et al) throw a ShaderBuiltinException on execution on the CPU. Technically, I could change them to pull out a value from the Thread context, however maintaining a thread context is notoriously annoying in an async world and impacts on performance, and compute shaders (in particular) are destined to be run asynchronously even on the CPU. Further, as the code can't easily tell which properties will be accessed in the code, all of them would need adding to the context immediately prior to execution, which is wasteful and slow. Anyone implementing a CPU executor for the shaders would likewise need to understand how to setup the contexts (possibly by adding methods such as ShaderBuiltins.InitialiseComputeContext(...), ShaderBuiltins.InitialiseVertexContext(...), etc.) but this is far less obvious and discoverable, and far less performant than adopting the Direct3D style.

@thargy
Copy link
Contributor Author

thargy commented May 19, 2018

FYI: as a workaround, I'm currently doing the following:

        // Current 'OpenGL' style
        [ComputeShader(1, 1, 1)]
        public void CS()
        {
            DoCS(DispatchThreadID);
        }

        public void DoCS(UInt3 dispatchThreadID)
        {
            uint index = dispatchThreadID.X;
            ...
        }

And calling DoCs(...) from the CPU, this doesn't allow for easy auto-plumbing, etc. (i.e. calling the known entry-point dynamically is not possible)

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

No branches or pull requests

1 participant