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 basic support for interface blocks #335

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ast.fs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ and Expr =

and TypeSpec =
| TypeName of string
| TypeStruct of string(*type*) * Ident option(*name*) * Decl list
// e.g. struct foo { float x; float y; }
| TypeBlock of string(*type*) * Ident option(*name*) * Decl list

and Type = {
name: TypeSpec // e.g. int
Expand All @@ -85,7 +86,7 @@ and Type = {
override t.ToString() =
let name = match t.name with
| TypeName n -> n
| TypeStruct _ -> $"{t.name}"
| TypeBlock _ -> $"{t.name}"
if t.typeQ.IsEmpty && t.arraySizes.IsEmpty
then $"{name}"
else $"<{t.typeQ} {name} {t.arraySizes}>"
Expand Down
2 changes: 1 addition & 1 deletion src/parse.fs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ type private ParseImpl() =
pipe2 name (between (ch '{') (ch '}') decls)
(fun n d ->
Option.iter (fun (i:Ast.Ident) -> forbiddenNames <- i.Name::forbiddenNames) n
Ast.TypeStruct(prefix, n, d))
Ast.TypeBlock(prefix, n, d))

let structSpecifier = parse {
let! str = keyword "struct"
Expand Down
2 changes: 1 addition & 1 deletion src/printer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ type PrinterImpl(outputFormat) =

and typeSpecToS = function
| TypeName s -> s
| TypeStruct(prefix, id, decls) -> structToS prefix id decls
| TypeBlock(prefix, id, decls) -> structToS prefix id decls

and typeToS (ty: Type) =
let get = function
Expand Down
10 changes: 10 additions & 0 deletions src/renamer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,18 @@ module private RenamerImpl =
renList env renCase cl |> ignore<Env>
env

// e.g. struct foo { int a; float b; }
// or uniform foo { int a; float b; }
let renTyBlock (env: Env) = function
| TypeBlock("struct", _, _) -> env
| TypeBlock(_, _, fields) ->
// treat the fields as if they were global variables
renList env (renDecl true) fields
| _ -> env

let rec renTopLevelName env = function
| TLDecl d -> renDecl true env d
| TypeDecl tyDecl -> renTyBlock env tyDecl
| Function(fct, _) -> renFctName env fct
| _ -> env

Expand Down
1 change: 1 addition & 0 deletions tests/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
--preprocess --no-remove-unused --no-renaming --format indented -o tests/unit/preprocess_if.frag.expected tests/unit/preprocess_if.frag
--no-remove-unused --no-renaming --format indented -o tests/unit/shadowing.frag.expected tests/unit/shadowing.frag
--no-remove-unused --no-renaming --format indented -o tests/unit/precedence.frag.expected tests/unit/precedence.frag
--no-remove-unused --format indented -o tests/unit/interface_block.frag.expected tests/unit/interface_block.frag

# Unused removal tests

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/interface_block.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
uniform MyBlock
{
vec3 color; // treated as a global variable
float alpha;
};

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
fragColor = vec4(color, alpha);
}
4 changes: 4 additions & 0 deletions tests/unit/interface_block.frag.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
uniform MyBlock{vec3 M;float v;};void mainImage(out vec4 m,vec2 B)
{
m=vec4(M,v);
}