From 7156c64692b7919b487d7fe2d7aa75430a2e53bb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 7 Apr 2021 00:17:02 +0300 Subject: [PATCH] Disallow certain types from appearing on variables --- source/glsl/validate.cpp | 8 ++++++++ tests/glsl/conditional_multiple_emitvertex.glsl | 16 ++++++++-------- tests/glsl/disallowed_types.glsl | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 tests/glsl/disallowed_types.glsl diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index 6ce2ee51..67d456a8 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -251,8 +251,12 @@ void DeclarationValidator::visit(VariableDeclaration &var) } TypeDeclaration *type = var.type_declaration; + BasicTypeDeclaration::Kind kind = BasicTypeDeclaration::ALIAS; while(BasicTypeDeclaration *basic = dynamic_cast(type)) + { + kind = basic->kind; type = basic->base_type; + } if(dynamic_cast(type)) { if(scope!=GLOBAL && scope!=FUNCTION_PARAM) @@ -260,6 +264,10 @@ void DeclarationValidator::visit(VariableDeclaration &var) else if(scope==GLOBAL && var.interface!="uniform") error(var, format("Type '%s' only allowed with uniform interface", type->name)); } + else if(kind==BasicTypeDeclaration::VOID) + error(var, "Type 'void' not allowed on variable"); + else if(kind==BasicTypeDeclaration::BOOL && !var.interface.empty() && var.source!=BUILTIN_SOURCE) + error(var, "Type 'bool' not allowed on interface variable"); if(var.init_expression) { diff --git a/tests/glsl/conditional_multiple_emitvertex.glsl b/tests/glsl/conditional_multiple_emitvertex.glsl index 2e4f32b1..2d0cb5a5 100644 --- a/tests/glsl/conditional_multiple_emitvertex.glsl +++ b/tests/glsl/conditional_multiple_emitvertex.glsl @@ -1,4 +1,4 @@ -uniform bool flag; +uniform int flag; uniform sampler2D tex; #pragma MSP stage(vertex) @@ -15,13 +15,13 @@ layout(triangle_strip, max_vertices=3) out; void main() { passthrough[0]; - if(flag) + if(flag!=0) EmitVertex(); passthrough[1]; - if(flag) + if(flag!=0) EmitVertex(); passthrough[2]; - if(flag) + if(flag!=0) EmitVertex(); } @@ -43,7 +43,7 @@ void main() */ /* Expected output: geometry -layout(location=0) uniform bool flag; +layout(location=0) uniform int flag; layout(triangles) in; layout(triangle_strip, max_vertices=3) out; layout(location=0) in vec2 texcoord[]; @@ -52,15 +52,15 @@ void main() { gl_Position = gl_in[0].gl_Position; _gs_out_texcoord = texcoord[0]; - if(flag) + if(flag!=0) EmitVertex(); gl_Position = gl_in[1].gl_Position; _gs_out_texcoord = texcoord[1]; - if(flag) + if(flag!=0) EmitVertex(); gl_Position = gl_in[2].gl_Position; _gs_out_texcoord = texcoord[2]; - if(flag) + if(flag!=0) EmitVertex(); } */ diff --git a/tests/glsl/disallowed_types.glsl b/tests/glsl/disallowed_types.glsl new file mode 100644 index 00000000..6835dbb6 --- /dev/null +++ b/tests/glsl/disallowed_types.glsl @@ -0,0 +1,14 @@ +uniform bool u_bool; + +#pragma MSP stage(vertex) +void void_var; +layout(location=0) in vec4 position; +void main() +{ + gl_Position = position; +} + +/* Expected error: +:1: Type 'bool' not allowed on interface variable +:4: Type 'void' not allowed on variable +*/ -- 2.45.2