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 support for GLSL interface blocks #6351

Merged
merged 18 commits into from
Feb 14, 2025

Conversation

cheneym2
Copy link
Collaborator

This adds support for GLSL style syntax for shader inputs, e.g.:

in VertexData
{
vec2 texcoord_0;
vec2 texcoord_1;
} vd;

@csyonghe
Copy link
Collaborator

In slang-ir-translate-global-var.cpp:97, instead of creating a new field type layout for the global var, there should already be a layout created on input (stored in a IRLayoutDecoration), we should just use that as fieldTypeLayout instead of building it.

@cheneym2
Copy link
Collaborator Author

In slang-ir-translate-global-var.cpp:97, instead of creating a new field type layout for the global var, there should already be a layout created on input (stored in a IRLayoutDecoration), we should just use that as fieldTypeLayout instead of building it.

Okay, thanks, that seems to be working, though I don't know what to do about the call to "addResourceUsage" on the fieldTypeLayout builder which would go unused in the case that I reuse a layout from IRLayoutDecoration.

@csyonghe
Copy link
Collaborator

varLayoutBuilder takes a TypeLayout to initialize, so you just delete the existing fieldTypeLayout completely, and pass the type layout you get from input's layout decoration to initailize varLayoutBuilder.

@cheneym2
Copy link
Collaborator Author

varLayoutBuilder takes a TypeLayout to initialize, so you just delete the existing fieldTypeLayout completely, and pass the type layout you get from input's layout decoration to initailize varLayoutBuilder.

That's what I think I'm doing, and it mostly works. I'll just upload a commit to explain.

LayoutSize(1));
if (!hasExistingLayout)
{
fieldTypeLayoutBuilder.addResourceUsage(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The doubt I was having was about this call to addResourceUsage. I think it doesn't make sense if I'm pulling fieldTypeLayout from IRLayoutDecoration.

@cheneym2
Copy link
Collaborator Author

cheneym2 commented Feb 13, 2025

glsl-intrinsic/fragment-processing/fragment-processing.slang is failing due to the missing shadowed globals replacement optimization. I believe the output of the test is still correct, but doesn't match expect output for that reason.

@cheneym2 cheneym2 added the pr: non-breaking PRs without breaking changes label Feb 13, 2025
@cheneym2
Copy link
Collaborator Author

With the change to use tryReplaceUsesofStageInput for the case of nested input, compilation of the test case outputs this:

#version 450
layout(row_major) uniform;
layout(row_major) buffer;

#line 31 0
vec4 _S1;


#line 31
layout(location = 0)
out vec4 entryPointParam_fragmentMain_out1_0;


#line 1974 1
layout(location = 0)
in vec2 vd_texcoord_0_0;


#line 1974
layout(location = 1)
in vec2 vd_texcoord_1_0;


#line 1974
layout(location = 2)
in vec4 vd_inner_texcoord_2_0;


#line 19 0
struct innerData_0
{
    vec4 texcoord_2_0;
};


#line 33
void main()
{

#line 33
    innerData_0 _S2 = { vd_inner_texcoord_2_0 };

#line 33
    _S3.texcoord_0_0 = vd_texcoord_0_0;

#line 33
    _S3.texcoord_1_0 = vd_texcoord_1_0;

#line 33
    _S3.inner_0 = _S2;

    vec4 _S4 = vec4(vd_texcoord_0_0, vd_texcoord_1_0.x, vd_inner_texcoord_2_0.y);

#line 35
    _S1 = _S4;

#line 35
    entryPointParam_fragmentMain_out1_0 = _S4;

#line 35
    return;
}

@csyonghe
Copy link
Collaborator

I guess the left over global variable _S3 can be removed if we add an optimization pass that removes non-output global variables that has only writes but no reads.

// input, and it makes no sense to store values into such real
// input locations.
traverseUses(
globalVar,
Copy link
Collaborator

Choose a reason for hiding this comment

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

we can also follow uses of any FieldAddr on the globalVar and remove the stores to those field addrs to get rid of the store to _S3 in the test case.

csyonghe
csyonghe previously approved these changes Feb 13, 2025
@cheneym2 cheneym2 marked this pull request as ready for review February 13, 2025 22:28
@cheneym2 cheneym2 requested a review from a team as a code owner February 13, 2025 22:28
@cheneym2
Copy link
Collaborator Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

@cheneym2 cheneym2 merged commit 944c19b into shader-slang:master Feb 14, 2025
16 checks passed
@cheneym2 cheneym2 deleted the cheneym2/materialx branch February 14, 2025 14:41
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.

3 participants