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

GLSL tutorial #19

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

jkwak-work
Copy link
Contributor

@jkwak-work jkwak-work commented Nov 21, 2024

This is a tutorial document for "migrating from glsl".

@jkwak-work jkwak-work self-assigned this Nov 21, 2024
@jkwak-work
Copy link
Contributor Author

Closes shader-slang/slang#5448

@jkwak-work
Copy link
Contributor Author

@jkwak-work jkwak-work changed the title [WIP] GLSL tutorial GLSL tutorial Dec 10, 2024
@jkwak-work jkwak-work marked this pull request as ready for review December 10, 2024 23:14
docs/coming-from-glsl.md Outdated Show resolved Hide resolved
docs/coming-from-glsl.md Show resolved Hide resolved

## How to use GLSL shaders as the input
By default, Slang doesn't recognize GLSL syntax. You need to explicitly enable it with an option, `-allow-glsl` or `-lang glsl`.
Copy link
Contributor

Choose a reason for hiding this comment

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

That's the options for slangc, maybe include the API options as well?

Curious, is it necessary to add -allow-glsl if your shader has "import glsl;"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could give a try and see what the actual behaviors are.
But my understanding is that import glsl just imports the GLSL functions top of what is available as Slang core modules.
It is not enough to allow the shader to use GLSL specific language syntax such as layout(location = 0).
These GLSL keywords are available only when -allow-glsl is used.

I am not clear on what -lang glsl actually does. I thought it is meant to includes -allow-glsl but when I tried last time, I still had to have -allow-glsl.
I will test a bit and update the document.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think you need -allow-glsl if you have #version 450 declaration in the source file.

Copy link
Contributor

Choose a reason for hiding this comment

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

-lang glsl does nothing right now so let’s not mention it.

docs/coming-from-glsl.md Outdated Show resolved Hide resolved
docs/coming-from-glsl.md Outdated Show resolved Hide resolved
docs/coming-from-glsl.md Outdated Show resolved Hide resolved
docs/coming-from-glsl.md Show resolved Hide resolved

## Layout rules
By default, Slang uses `std430` as the layout rule. You can explicitly specify to use `std140` whenever needed.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not true. Uniform buffer defaults to std140, shader storage buffers and structured buffers default to std430.

Copy link
Contributor

Choose a reason for hiding this comment

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

The example below is using HLSL syntax but this doc is for glsl users, so it seems inappropriate, unless we start with some explanation, such as “to explicitly specify the layout of individual buffers, you can use the Slang syntax when declaring the buffer.”

StructuredBuffer<T, Std140DataLayout> std140Layout;
StructuredBuffer<T, Std430DataLayout> std430Layout;
StructuredBuffer<T, ScalarDataLayout> scalarLayout;
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a case for specifying layout for a ConstantBuffer<>.

```

The layout rule can also be changed with options like `-force-glsl-scalar-layout` or `-fvk-use-scalar-layout`.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also -fvk-use-dx-layout now. See dxc documentation for what it means.


## Matrix
Even though GLSL claims to use "Column-major", it is mostly nomenclature when it comes to the shader-side implementation. For this reason, `matXxY` and `floatXxY` can be used interchangeably in Slang.
Copy link
Contributor

Choose a reason for hiding this comment

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

We can just say matNxM is a typedef to floatNxM.

GLSL requires an entry point to be named `main`. This requirement prevents a shader file from having more than one entry point, unlike other shading languages such as HLSL and Metal.

But this requirement doesn't apply when using Slang with the option `-allow-glsl`. In other words, you can define multiple entry points in the same shader file.
Copy link
Contributor

Choose a reason for hiding this comment

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

Talk about -fvk-use-EntryPoint-name here.

## Overview
Slang allows developers to use GLSL syntax as input shaders. It provides an easy transition from your existing GLSL-based system to the Slang system. This document provides information that users may want to know when migrating from a GLSL-based system to the Slang system.
Copy link
Contributor

Choose a reason for hiding this comment

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

… when migrating a glsl shader code base to Slang.


## How to use GLSL shaders as the input
By default, Slang doesn't recognize GLSL syntax. You need to explicitly enable it with an option, `-allow-glsl` or `-lang glsl`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think you need -allow-glsl if you have #version 450 declaration in the source file.


## How to use GLSL shaders as the input
By default, Slang doesn't recognize GLSL syntax. You need to explicitly enable it with an option, `-allow-glsl` or `-lang glsl`.
Copy link
Contributor

Choose a reason for hiding this comment

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

-lang glsl does nothing right now so let’s not mention it.

For people who are interested in more details of how GLSL functions are translated, please refer to [glsl.meta.slang](https://github.com/shader-slang/slang/blob/master/source/slang/glsl.meta.slang).

## How to use GLSL shaders as input
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a section on the support level of glsl input:

The support for GLSL input in the slang compiler is best-effort rather than spec-complete. The Slang compiler understands many but not all GLSL syntax. Slang is not a drop-in replacement for any existing GLSL compiler. Instead, the intention is to make transition from glsl to Slang easy by allowing mixing GLSL syntax and Slang syntax so the user does not need to modify every line of code before they can use Slang features. The user is still expected to migrate from unsupported GLSL syntax before they can be accepted by the Slang compiler.

Most of the unsupported areas of GLSL syntax are around global parameter or layout declarations. We expect most functions written in GLSL to just work in Slang.


For a more detailed explanation about the matrix layout, please check another document, [Handling Matrix Layout Differences on Different Platforms](https://shader-slang.com/slang/user-guide/a1-01-matrix-layout.html).

Copy link
Contributor

Choose a reason for hiding this comment

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

Also need a section to talk about the difference of operator ==, * between glsl and HLSL, and how -allow-glsl/#version/import glsl changes that default behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

And a section on how shader storage buffers are parsed and supported.

Show the glsl syntax and equivalent slang code for constant buffers, structured buffers and storage buffers, so people know how to migrate the code to slang if they want to do so.

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.

3 participants