Browse Source

Allow passing non-variable constant to const function param in shaders

Yuri Roubinsky 3 years ago
parent
commit
f4c0e90fd9

+ 12 - 6
drivers/gles3/shader_compiler_gles3.cpp

@@ -65,6 +65,13 @@ static String _prestr(SL::DataPrecision p_pres) {
 	return "";
 	return "";
 }
 }
 
 
+static String _constr(bool p_is_const) {
+	if (p_is_const) {
+		return "const ";
+	}
+	return "";
+}
+
 static String _qualstr(SL::ArgumentQualifier p_qual) {
 static String _qualstr(SL::ArgumentQualifier p_qual) {
 	switch (p_qual) {
 	switch (p_qual) {
 		case SL::ARGUMENT_QUALIFIER_IN:
 		case SL::ARGUMENT_QUALIFIER_IN:
@@ -246,9 +253,10 @@ void ShaderCompilerGLES3::_dump_function_deps(SL::ShaderNode *p_node, const Stri
 		header += "(";
 		header += "(";
 
 
 		for (int i = 0; i < fnode->arguments.size(); i++) {
 		for (int i = 0; i < fnode->arguments.size(); i++) {
-			if (i > 0)
+			if (i > 0) {
 				header += ", ";
 				header += ", ";
-
+			}
+			header += _constr(fnode->arguments[i].is_const);
 			header += _qualstr(fnode->arguments[i].qualifier);
 			header += _qualstr(fnode->arguments[i].qualifier);
 			header += _prestr(fnode->arguments[i].precision);
 			header += _prestr(fnode->arguments[i].precision);
 			header += _typestr(fnode->arguments[i].type);
 			header += _typestr(fnode->arguments[i].type);
@@ -365,7 +373,7 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener
 
 
 			for (int i = 0; i < snode->vconstants.size(); i++) {
 			for (int i = 0; i < snode->vconstants.size(); i++) {
 				String gcode;
 				String gcode;
-				gcode += "const ";
+				gcode += _constr(true);
 				gcode += _prestr(snode->vconstants[i].precision);
 				gcode += _prestr(snode->vconstants[i].precision);
 				gcode += _typestr(snode->vconstants[i].type);
 				gcode += _typestr(snode->vconstants[i].type);
 				gcode += " " + _mkid(String(snode->vconstants[i].name));
 				gcode += " " + _mkid(String(snode->vconstants[i].name));
@@ -446,9 +454,7 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener
 			SL::VariableDeclarationNode *var_dec_node = (SL::VariableDeclarationNode *)p_node;
 			SL::VariableDeclarationNode *var_dec_node = (SL::VariableDeclarationNode *)p_node;
 
 
 			StringBuffer<> declaration;
 			StringBuffer<> declaration;
-			if (var_dec_node->is_const) {
-				declaration += "const ";
-			}
+			declaration += _constr(var_dec_node->is_const);
 			declaration += _prestr(var_dec_node->precision);
 			declaration += _prestr(var_dec_node->precision);
 			declaration += _typestr(var_dec_node->datatype);
 			declaration += _typestr(var_dec_node->datatype);
 
 

+ 11 - 10
servers/rendering/renderer_rd/shader_compiler_rd.cpp

@@ -227,6 +227,13 @@ static String _prestr(SL::DataPrecision p_pres, bool p_force_highp = false) {
 	return "";
 	return "";
 }
 }
 
 
+static String _constr(bool p_is_const) {
+	if (p_is_const) {
+		return "const ";
+	}
+	return "";
+}
+
 static String _qualstr(SL::ArgumentQualifier p_qual) {
 static String _qualstr(SL::ArgumentQualifier p_qual) {
 	switch (p_qual) {
 	switch (p_qual) {
 		case SL::ARGUMENT_QUALIFIER_IN:
 		case SL::ARGUMENT_QUALIFIER_IN:
@@ -417,9 +424,7 @@ void ShaderCompilerRD::_dump_function_deps(const SL::ShaderNode *p_node, const S
 			if (i > 0) {
 			if (i > 0) {
 				header += ", ";
 				header += ", ";
 			}
 			}
-			if (fnode->arguments[i].is_const) {
-				header += "const ";
-			}
+			header += _constr(fnode->arguments[i].is_const);
 			if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
 			if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
 				header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
 				header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].type_str) + " " + _mkid(fnode->arguments[i].name);
 			} else {
 			} else {
@@ -791,7 +796,7 @@ String ShaderCompilerRD::_dump_node_code(const SL::Node *p_node, int p_level, Ge
 			for (int i = 0; i < pnode->vconstants.size(); i++) {
 			for (int i = 0; i < pnode->vconstants.size(); i++) {
 				const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
 				const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
 				String gcode;
 				String gcode;
-				gcode += "const ";
+				gcode += _constr(true);
 				gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
 				gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
 				if (cnode.type == SL::TYPE_STRUCT) {
 				if (cnode.type == SL::TYPE_STRUCT) {
 					gcode += _mkid(cnode.type_str);
 					gcode += _mkid(cnode.type_str);
@@ -875,9 +880,7 @@ String ShaderCompilerRD::_dump_node_code(const SL::Node *p_node, int p_level, Ge
 			SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
 			SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
 
 
 			String declaration;
 			String declaration;
-			if (vdnode->is_const) {
-				declaration += "const ";
-			}
+			declaration += _constr(vdnode->is_const);
 			if (vdnode->datatype == SL::TYPE_STRUCT) {
 			if (vdnode->datatype == SL::TYPE_STRUCT) {
 				declaration += _mkid(vdnode->struct_name);
 				declaration += _mkid(vdnode->struct_name);
 			} else {
 			} else {
@@ -997,9 +1000,7 @@ String ShaderCompilerRD::_dump_node_code(const SL::Node *p_node, int p_level, Ge
 		case SL::Node::TYPE_ARRAY_DECLARATION: {
 		case SL::Node::TYPE_ARRAY_DECLARATION: {
 			SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
 			SL::ArrayDeclarationNode *adnode = (SL::ArrayDeclarationNode *)p_node;
 			String declaration;
 			String declaration;
-			if (adnode->is_const) {
-				declaration += "const ";
-			}
+			declaration += _constr(adnode->is_const);
 			if (adnode->datatype == SL::TYPE_STRUCT) {
 			if (adnode->datatype == SL::TYPE_STRUCT) {
 				declaration += _mkid(adnode->struct_name);
 				declaration += _mkid(adnode->struct_name);
 			} else {
 			} else {

+ 3 - 1
servers/rendering/shader_language.cpp

@@ -4920,7 +4920,9 @@ ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, cons
 										bool error = false;
 										bool error = false;
 										Node *n = func->arguments[argidx];
 										Node *n = func->arguments[argidx];
 										if (n->type == Node::TYPE_CONSTANT || n->type == Node::TYPE_OPERATOR) {
 										if (n->type == Node::TYPE_CONSTANT || n->type == Node::TYPE_OPERATOR) {
-											error = true;
+											if (!call_function->arguments[i].is_const) {
+												error = true;
+											}
 										} else if (n->type == Node::TYPE_ARRAY) {
 										} else if (n->type == Node::TYPE_ARRAY) {
 											ArrayNode *an = static_cast<ArrayNode *>(n);
 											ArrayNode *an = static_cast<ArrayNode *>(n);
 											if (an->call_expression != nullptr || an->is_const) {
 											if (an->call_expression != nullptr || an->is_const) {