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

Add example showing how to use Slang/WASM #5996

Merged
merged 3 commits into from
Jan 7, 2025

Conversation

aleino-nv
Copy link
Collaborator

This closes #5656.

@aleino-nv aleino-nv requested a review from a team as a code owner January 3, 2025 10:06
@aleino-nv aleino-nv added the pr: non-breaking PRs without breaking changes label Jan 3, 2025
@aleino-nv aleino-nv enabled auto-merge (squash) January 3, 2025 13:35
@csyonghe csyonghe disabled auto-merge January 4, 2025 06:52

## Description

This is a simple example showing how WebGPU applications can usethe Slang API directly via the WebAssembly library.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

showing how WebGPU applications can use the slang-wasm library to compile slang shaders at runtime to wgsl.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@aleino-nv aleino-nv force-pushed the aleino/slang-wasm-example branch from b2831df to d8e4324 Compare January 5, 2025 08:23
@aleino-nv aleino-nv requested a review from csyonghe January 5, 2025 08:25
Copy link
Collaborator

@jkwak-work jkwak-work left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

I am wondering if it will be more helpful to add some comments, because the goal of this change is to tech how to use.
But too many comments may just bloat the line count.

throw new Error("Failed to compile WGSL shaders.");
}

render(wgslShaders);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is render() gets called only once?
Or does it get called every frame?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just called once, since there is no animation in the example.

@aleino-nv
Copy link
Collaborator Author

Looks good to me.

I am wondering if it will be more helpful to add some comments, because the goal of this change is to tech how to use. But too many comments may just bloat the line count.

Yeah I considered it, but most of the code is WebGPU -related.
The Slang-API code is the following, which I find is quite self-explanatory:

        var module = slangSession.loadModuleFromSource(
            slangCode, "shader", '/shader.slang'
        );
        var vertexEntryPoint = module.findAndCheckEntryPoint(
            "vertexMain", slang.constants.STAGE_VERTEX
        );
        var fragmentEntryPoint = module.findAndCheckEntryPoint(
            "fragmentMain", slang.constants.STAGE_FRAGMENT
        );
        var linkedProgram = slangSession.createCompositeComponentType([
            module, vertexEntryPoint, fragmentEntryPoint
        ]).link();
        wgslShaders = {
            vertex: linkedProgram.getEntryPointCode(
                0 /* entryPointIndex */, 0 /* targetIndex */
            ),
            fragment: linkedProgram.getEntryPointCode(
                1 /* entryPointIndex */, 0 /* targetIndex */
            ),
        };

@aleino-nv aleino-nv requested a review from jkwak-work January 7, 2025 08:13
@aleino-nv aleino-nv enabled auto-merge (squash) January 7, 2025 17:18
@aleino-nv aleino-nv merged commit 7e278c3 into shader-slang:master Jan 7, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[request] HTML5 example.
3 participants