2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* shader_compiler.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2019-11-05 12:01:00 +01:00
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
#include "shader_compiler.h"
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2019-07-21 16:31:30 +02:00
|
|
|
#include "core/os/os.h"
|
2022-10-03 13:01:46 +02:00
|
|
|
#include "servers/rendering/rendering_server_globals.h"
|
2022-01-10 19:46:41 +01:00
|
|
|
#include "servers/rendering/shader_types.h"
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
#define SL ShaderLanguage
|
|
|
|
|
|
|
|
static String _mktab(int p_level) {
|
2023-01-29 00:52:05 +01:00
|
|
|
return String("\t").repeat(p_level);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static String _typestr(SL::DataType p_type) {
|
|
|
|
String type = ShaderLanguage::get_datatype_name(p_type);
|
2022-04-30 00:34:01 +02:00
|
|
|
if (!RS::get_singleton()->is_low_end() && ShaderLanguage::is_sampler_type(p_type)) {
|
|
|
|
type = type.replace("sampler", "texture"); //we use textures instead of samplers in Vulkan GLSL
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _get_datatype_alignment(SL::DataType p_type) {
|
|
|
|
switch (p_type) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::TYPE_VOID:
|
|
|
|
return 0;
|
|
|
|
case SL::TYPE_BOOL:
|
|
|
|
return 4;
|
|
|
|
case SL::TYPE_BVEC2:
|
|
|
|
return 8;
|
|
|
|
case SL::TYPE_BVEC3:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_BVEC4:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_INT:
|
|
|
|
return 4;
|
|
|
|
case SL::TYPE_IVEC2:
|
|
|
|
return 8;
|
|
|
|
case SL::TYPE_IVEC3:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_IVEC4:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_UINT:
|
|
|
|
return 4;
|
|
|
|
case SL::TYPE_UVEC2:
|
|
|
|
return 8;
|
|
|
|
case SL::TYPE_UVEC3:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_UVEC4:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_FLOAT:
|
|
|
|
return 4;
|
|
|
|
case SL::TYPE_VEC2:
|
|
|
|
return 8;
|
|
|
|
case SL::TYPE_VEC3:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_VEC4:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_MAT2:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_MAT3:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_MAT4:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_SAMPLER2D:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_ISAMPLER2D:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_USAMPLER2D:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_SAMPLER2DARRAY:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_ISAMPLER2DARRAY:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_USAMPLER2DARRAY:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_SAMPLER3D:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_ISAMPLER3D:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_USAMPLER3D:
|
|
|
|
return 16;
|
|
|
|
case SL::TYPE_SAMPLERCUBE:
|
|
|
|
return 16;
|
2020-05-01 14:34:23 +02:00
|
|
|
case SL::TYPE_SAMPLERCUBEARRAY:
|
|
|
|
return 16;
|
2023-10-14 12:58:59 +02:00
|
|
|
case SL::TYPE_SAMPLEREXT:
|
|
|
|
return 16;
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::TYPE_STRUCT:
|
|
|
|
return 0;
|
2020-04-17 04:52:00 +02:00
|
|
|
case SL::TYPE_MAX: {
|
|
|
|
ERR_FAIL_V(0);
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_V(0);
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
static String _interpstr(SL::DataInterpolation p_interp) {
|
|
|
|
switch (p_interp) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::INTERPOLATION_FLAT:
|
|
|
|
return "flat ";
|
|
|
|
case SL::INTERPOLATION_SMOOTH:
|
|
|
|
return "";
|
2022-12-22 21:19:30 +01:00
|
|
|
case SL::INTERPOLATION_DEFAULT:
|
|
|
|
return "";
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-08-18 17:18:50 +02:00
|
|
|
static String _prestr(SL::DataPrecision p_pres, bool p_force_highp = false) {
|
2019-07-21 16:31:30 +02:00
|
|
|
switch (p_pres) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::PRECISION_LOWP:
|
|
|
|
return "lowp ";
|
|
|
|
case SL::PRECISION_MEDIUMP:
|
|
|
|
return "mediump ";
|
|
|
|
case SL::PRECISION_HIGHP:
|
|
|
|
return "highp ";
|
|
|
|
case SL::PRECISION_DEFAULT:
|
2021-08-18 17:18:50 +02:00
|
|
|
return p_force_highp ? "highp " : "";
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-11-19 09:25:23 +01:00
|
|
|
static String _constr(bool p_is_const) {
|
|
|
|
if (p_is_const) {
|
|
|
|
return "const ";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
static String _qualstr(SL::ArgumentQualifier p_qual) {
|
|
|
|
switch (p_qual) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::ARGUMENT_QUALIFIER_IN:
|
|
|
|
return "";
|
|
|
|
case SL::ARGUMENT_QUALIFIER_OUT:
|
|
|
|
return "out ";
|
|
|
|
case SL::ARGUMENT_QUALIFIER_INOUT:
|
|
|
|
return "inout ";
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static String _opstr(SL::Operator p_op) {
|
|
|
|
return SL::get_operator_text(p_op);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String _mkid(const String &p_id) {
|
2020-01-31 08:42:39 +01:00
|
|
|
String id = "m_" + p_id.replace("__", "_dus_");
|
2019-07-21 16:31:30 +02:00
|
|
|
return id.replace("__", "_dus_"); //doubleunderscore is reserved in glsl
|
|
|
|
}
|
|
|
|
|
|
|
|
static String f2sp0(float p_float) {
|
2023-07-03 11:48:33 +02:00
|
|
|
String num = rtos(p_float);
|
2022-02-03 17:03:38 +01:00
|
|
|
if (!num.contains(".") && !num.contains("e")) {
|
2019-07-21 16:31:30 +02:00
|
|
|
num += ".0";
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:49:09 +02:00
|
|
|
static String get_constant_text(SL::DataType p_type, const Vector<SL::Scalar> &p_values) {
|
2019-07-21 16:31:30 +02:00
|
|
|
switch (p_type) {
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::TYPE_BOOL:
|
|
|
|
return p_values[0].boolean ? "true" : "false";
|
2019-07-21 16:31:30 +02:00
|
|
|
case SL::TYPE_BVEC2:
|
|
|
|
case SL::TYPE_BVEC3:
|
|
|
|
case SL::TYPE_BVEC4: {
|
|
|
|
String text = "bvec" + itos(p_type - SL::TYPE_BOOL + 1) + "(";
|
|
|
|
for (int i = 0; i < p_values.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2019-07-21 16:31:30 +02:00
|
|
|
text += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
text += p_values[i].boolean ? "true" : "false";
|
|
|
|
}
|
|
|
|
text += ")";
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::TYPE_INT:
|
2024-09-12 15:49:09 +02:00
|
|
|
return itos(p_values[0].sint);
|
2019-07-21 16:31:30 +02:00
|
|
|
case SL::TYPE_IVEC2:
|
|
|
|
case SL::TYPE_IVEC3:
|
|
|
|
case SL::TYPE_IVEC4: {
|
|
|
|
String text = "ivec" + itos(p_type - SL::TYPE_INT + 1) + "(";
|
|
|
|
for (int i = 0; i < p_values.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2019-07-21 16:31:30 +02:00
|
|
|
text += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
text += itos(p_values[i].sint);
|
|
|
|
}
|
|
|
|
text += ")";
|
|
|
|
return text;
|
|
|
|
|
|
|
|
} break;
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::TYPE_UINT:
|
|
|
|
return itos(p_values[0].uint) + "u";
|
2019-07-21 16:31:30 +02:00
|
|
|
case SL::TYPE_UVEC2:
|
|
|
|
case SL::TYPE_UVEC3:
|
|
|
|
case SL::TYPE_UVEC4: {
|
|
|
|
String text = "uvec" + itos(p_type - SL::TYPE_UINT + 1) + "(";
|
|
|
|
for (int i = 0; i < p_values.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2019-07-21 16:31:30 +02:00
|
|
|
text += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
text += itos(p_values[i].uint) + "u";
|
|
|
|
}
|
|
|
|
text += ")";
|
|
|
|
return text;
|
|
|
|
} break;
|
2020-05-10 13:00:47 +02:00
|
|
|
case SL::TYPE_FLOAT:
|
2024-09-12 15:49:09 +02:00
|
|
|
return f2sp0(p_values[0].real);
|
2019-07-21 16:31:30 +02:00
|
|
|
case SL::TYPE_VEC2:
|
|
|
|
case SL::TYPE_VEC3:
|
|
|
|
case SL::TYPE_VEC4: {
|
|
|
|
String text = "vec" + itos(p_type - SL::TYPE_FLOAT + 1) + "(";
|
|
|
|
for (int i = 0; i < p_values.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2019-07-21 16:31:30 +02:00
|
|
|
text += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
text += f2sp0(p_values[i].real);
|
|
|
|
}
|
|
|
|
text += ")";
|
|
|
|
return text;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case SL::TYPE_MAT2:
|
|
|
|
case SL::TYPE_MAT3:
|
|
|
|
case SL::TYPE_MAT4: {
|
|
|
|
String text = "mat" + itos(p_type - SL::TYPE_MAT2 + 2) + "(";
|
|
|
|
for (int i = 0; i < p_values.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2019-07-21 16:31:30 +02:00
|
|
|
text += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
text += f2sp0(p_values[i].real);
|
|
|
|
}
|
|
|
|
text += ")";
|
|
|
|
return text;
|
|
|
|
|
|
|
|
} break;
|
2020-05-10 13:00:47 +02:00
|
|
|
default:
|
|
|
|
ERR_FAIL_V(String());
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
String ShaderCompiler::_get_sampler_name(ShaderLanguage::TextureFilter p_filter, ShaderLanguage::TextureRepeat p_repeat) {
|
2019-07-21 16:31:30 +02:00
|
|
|
if (p_filter == ShaderLanguage::FILTER_DEFAULT) {
|
|
|
|
ERR_FAIL_COND_V(actions.default_filter == ShaderLanguage::FILTER_DEFAULT, String());
|
|
|
|
p_filter = actions.default_filter;
|
|
|
|
}
|
|
|
|
if (p_repeat == ShaderLanguage::REPEAT_DEFAULT) {
|
|
|
|
ERR_FAIL_COND_V(actions.default_repeat == ShaderLanguage::REPEAT_DEFAULT, String());
|
|
|
|
p_repeat = actions.default_repeat;
|
|
|
|
}
|
2023-05-26 23:35:16 +02:00
|
|
|
constexpr const char *name_mapping[] = {
|
|
|
|
"SAMPLER_NEAREST_CLAMP",
|
|
|
|
"SAMPLER_LINEAR_CLAMP",
|
|
|
|
"SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP",
|
|
|
|
"SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP",
|
|
|
|
"SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_CLAMP",
|
|
|
|
"SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_CLAMP",
|
|
|
|
"SAMPLER_NEAREST_REPEAT",
|
|
|
|
"SAMPLER_LINEAR_REPEAT",
|
|
|
|
"SAMPLER_NEAREST_WITH_MIPMAPS_REPEAT",
|
|
|
|
"SAMPLER_LINEAR_WITH_MIPMAPS_REPEAT",
|
|
|
|
"SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_REPEAT",
|
|
|
|
"SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_REPEAT"
|
|
|
|
};
|
|
|
|
return String(name_mapping[p_filter + (p_repeat == ShaderLanguage::REPEAT_ENABLE ? ShaderLanguage::FILTER_DEFAULT : 0)]);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-19 17:00:06 +02:00
|
|
|
void ShaderCompiler::_dump_function_deps(const SL::ShaderNode *p_node, const StringName &p_for_func, const HashMap<StringName, String> &p_func_code, String &r_to_add, HashSet<StringName> &added) {
|
2019-07-21 16:31:30 +02:00
|
|
|
int fidx = -1;
|
|
|
|
|
2023-09-13 18:11:12 +02:00
|
|
|
for (int i = 0; i < p_node->vfunctions.size(); i++) {
|
|
|
|
if (p_node->vfunctions[i].name == p_for_func) {
|
2019-07-21 16:31:30 +02:00
|
|
|
fidx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(fidx == -1);
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
Vector<StringName> uses_functions;
|
|
|
|
|
2023-09-13 18:11:12 +02:00
|
|
|
for (const StringName &E : p_node->vfunctions[fidx].uses_function) {
|
2022-05-19 01:43:40 +02:00
|
|
|
uses_functions.push_back(E);
|
2021-05-25 02:25:11 +02:00
|
|
|
}
|
|
|
|
uses_functions.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
|
|
|
|
|
|
|
|
for (int k = 0; k < uses_functions.size(); k++) {
|
|
|
|
if (added.has(uses_functions[k])) {
|
2019-07-21 16:31:30 +02:00
|
|
|
continue; //was added already
|
|
|
|
}
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
_dump_function_deps(p_node, uses_functions[k], p_func_code, r_to_add, added);
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
SL::FunctionNode *fnode = nullptr;
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2023-09-13 18:11:12 +02:00
|
|
|
for (int i = 0; i < p_node->vfunctions.size(); i++) {
|
|
|
|
if (p_node->vfunctions[i].name == uses_functions[k]) {
|
|
|
|
fnode = p_node->vfunctions[i].function;
|
2019-07-21 16:31:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 17:04:18 +02:00
|
|
|
ERR_FAIL_NULL(fnode);
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
r_to_add += "\n";
|
|
|
|
|
|
|
|
String header;
|
2020-01-17 20:35:22 +01:00
|
|
|
if (fnode->return_type == SL::TYPE_STRUCT) {
|
2021-05-21 18:16:23 +02:00
|
|
|
header = _mkid(fnode->return_struct_name);
|
2020-01-17 20:35:22 +01:00
|
|
|
} else {
|
2021-05-21 18:16:23 +02:00
|
|
|
header = _typestr(fnode->return_type);
|
2020-01-17 20:35:22 +01:00
|
|
|
}
|
2021-05-21 18:16:23 +02:00
|
|
|
|
|
|
|
if (fnode->return_array_size > 0) {
|
|
|
|
header += "[";
|
|
|
|
header += itos(fnode->return_array_size);
|
|
|
|
header += "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
header += " ";
|
2024-05-27 21:16:54 +02:00
|
|
|
header += _mkid(fnode->rname);
|
2021-05-21 18:16:23 +02:00
|
|
|
header += "(";
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
for (int i = 0; i < fnode->arguments.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 0) {
|
2019-07-21 16:31:30 +02:00
|
|
|
header += ", ";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-11-19 09:25:23 +01:00
|
|
|
header += _constr(fnode->arguments[i].is_const);
|
2020-01-17 20:35:22 +01:00
|
|
|
if (fnode->arguments[i].type == SL::TYPE_STRUCT) {
|
2024-05-28 11:13:25 +02:00
|
|
|
header += _qualstr(fnode->arguments[i].qualifier) + _mkid(fnode->arguments[i].struct_name) + " " + _mkid(fnode->arguments[i].name);
|
2020-01-17 20:35:22 +01:00
|
|
|
} else {
|
|
|
|
header += _qualstr(fnode->arguments[i].qualifier) + _prestr(fnode->arguments[i].precision) + _typestr(fnode->arguments[i].type) + " " + _mkid(fnode->arguments[i].name);
|
|
|
|
}
|
2021-05-21 18:16:23 +02:00
|
|
|
if (fnode->arguments[i].array_size > 0) {
|
|
|
|
header += "[";
|
|
|
|
header += itos(fnode->arguments[i].array_size);
|
|
|
|
header += "]";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
header += ")\n";
|
|
|
|
r_to_add += header;
|
2021-05-25 02:25:11 +02:00
|
|
|
r_to_add += p_func_code[uses_functions[k]];
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
added.insert(uses_functions[k]);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:43 +02:00
|
|
|
static String _get_global_shader_uniform_from_type_and_index(const String &p_buffer, const String &p_index, ShaderLanguage::DataType p_type) {
|
2020-04-17 04:52:00 +02:00
|
|
|
switch (p_type) {
|
|
|
|
case ShaderLanguage::TYPE_BOOL: {
|
2022-10-29 08:33:44 +02:00
|
|
|
return "bool(floatBitsToUint(" + p_buffer + "[" + p_index + "].x))";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_BVEC2: {
|
2022-10-29 08:33:44 +02:00
|
|
|
return "bvec2(floatBitsToUint(" + p_buffer + "[" + p_index + "].xy))";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_BVEC3: {
|
2022-10-29 08:33:44 +02:00
|
|
|
return "bvec3(floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz))";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_BVEC4: {
|
2022-10-29 08:33:44 +02:00
|
|
|
return "bvec4(floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw))";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_INT: {
|
|
|
|
return "floatBitsToInt(" + p_buffer + "[" + p_index + "].x)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_IVEC2: {
|
|
|
|
return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xy)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_IVEC3: {
|
|
|
|
return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyz)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_IVEC4: {
|
|
|
|
return "floatBitsToInt(" + p_buffer + "[" + p_index + "].xyzw)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_UINT: {
|
2020-10-21 01:34:25 +02:00
|
|
|
return "floatBitsToUint(" + p_buffer + "[" + p_index + "].x)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_UVEC2: {
|
2020-10-21 01:34:25 +02:00
|
|
|
return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xy)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_UVEC3: {
|
2020-10-21 01:34:25 +02:00
|
|
|
return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyz)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_UVEC4: {
|
2020-10-21 01:34:25 +02:00
|
|
|
return "floatBitsToUint(" + p_buffer + "[" + p_index + "].xyzw)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_FLOAT: {
|
|
|
|
return "(" + p_buffer + "[" + p_index + "].x)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_VEC2: {
|
|
|
|
return "(" + p_buffer + "[" + p_index + "].xy)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_VEC3: {
|
|
|
|
return "(" + p_buffer + "[" + p_index + "].xyz)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_VEC4: {
|
|
|
|
return "(" + p_buffer + "[" + p_index + "].xyzw)";
|
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_MAT2: {
|
2023-09-09 17:43:49 +02:00
|
|
|
return "mat2(" + p_buffer + "[" + p_index + "].xy," + p_buffer + "[" + p_index + "+1u].xy)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_MAT3: {
|
2023-09-09 17:43:49 +02:00
|
|
|
return "mat3(" + p_buffer + "[" + p_index + "].xyz," + p_buffer + "[" + p_index + "+1u].xyz," + p_buffer + "[" + p_index + "+2u].xyz)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
case ShaderLanguage::TYPE_MAT4: {
|
2023-09-09 17:43:49 +02:00
|
|
|
return "mat4(" + p_buffer + "[" + p_index + "].xyzw," + p_buffer + "[" + p_index + "+1u].xyzw," + p_buffer + "[" + p_index + "+2u].xyzw," + p_buffer + "[" + p_index + "+3u].xyzw)";
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
ERR_FAIL_V("void");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:49:09 +02:00
|
|
|
String ShaderCompiler::_dump_node_code(const SL::Node *p_node, int p_level, GeneratedCode &r_gen_code, IdentifierActions &p_actions, const DefaultIdentifierActions &p_default_actions, bool p_assigning, bool p_use_scope) {
|
2019-07-21 16:31:30 +02:00
|
|
|
String code;
|
|
|
|
|
|
|
|
switch (p_node->type) {
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_SHADER: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::ShaderNode *pnode = (SL::ShaderNode *)p_node;
|
|
|
|
|
|
|
|
for (int i = 0; i < pnode->render_modes.size(); i++) {
|
|
|
|
if (p_default_actions.render_mode_defines.has(pnode->render_modes[i]) && !used_rmode_defines.has(pnode->render_modes[i])) {
|
|
|
|
r_gen_code.defines.push_back(p_default_actions.render_mode_defines[pnode->render_modes[i]]);
|
|
|
|
used_rmode_defines.insert(pnode->render_modes[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_actions.render_mode_flags.has(pnode->render_modes[i])) {
|
|
|
|
*p_actions.render_mode_flags[pnode->render_modes[i]] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_actions.render_mode_values.has(pnode->render_modes[i])) {
|
|
|
|
Pair<int *, int> &p = p_actions.render_mode_values[pnode->render_modes[i]];
|
|
|
|
*p.first = p.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 20:35:22 +01:00
|
|
|
// structs
|
|
|
|
|
|
|
|
for (int i = 0; i < pnode->vstructs.size(); i++) {
|
|
|
|
SL::StructNode *st = pnode->vstructs[i].shader_struct;
|
|
|
|
String struct_code;
|
|
|
|
|
|
|
|
struct_code += "struct ";
|
|
|
|
struct_code += _mkid(pnode->vstructs[i].name);
|
|
|
|
struct_code += " ";
|
|
|
|
struct_code += "{\n";
|
2024-04-15 15:18:34 +02:00
|
|
|
for (SL::MemberNode *m : st->members) {
|
2020-01-17 20:35:22 +01:00
|
|
|
if (m->datatype == SL::TYPE_STRUCT) {
|
|
|
|
struct_code += _mkid(m->struct_name);
|
|
|
|
} else {
|
|
|
|
struct_code += _prestr(m->precision);
|
|
|
|
struct_code += _typestr(m->datatype);
|
|
|
|
}
|
|
|
|
struct_code += " ";
|
|
|
|
struct_code += m->name;
|
2020-02-11 20:03:04 +01:00
|
|
|
if (m->array_size > 0) {
|
|
|
|
struct_code += "[";
|
|
|
|
struct_code += itos(m->array_size);
|
|
|
|
struct_code += "]";
|
|
|
|
}
|
2020-01-17 20:35:22 +01:00
|
|
|
struct_code += ";\n";
|
|
|
|
}
|
|
|
|
struct_code += "}";
|
|
|
|
struct_code += ";\n";
|
|
|
|
|
2021-04-13 22:01:43 +02:00
|
|
|
for (int j = 0; j < STAGE_MAX; j++) {
|
|
|
|
r_gen_code.stage_globals[j] += struct_code;
|
|
|
|
}
|
2020-01-17 20:35:22 +01:00
|
|
|
}
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
int max_texture_uniforms = 0;
|
|
|
|
int max_uniforms = 0;
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
|
|
|
|
if (SL::is_sampler_type(E.value.type)) {
|
2022-08-09 18:29:49 +02:00
|
|
|
if (E.value.hint == SL::ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
|
|
|
|
E.value.hint == SL::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
|
|
|
|
E.value.hint == SL::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
|
|
|
|
continue; // Don't create uniforms in the generated code for these.
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
max_texture_uniforms++;
|
2020-04-17 04:52:00 +02:00
|
|
|
} else {
|
2021-08-09 22:13:42 +02:00
|
|
|
if (E.value.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
|
Fix various typos with codespell
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
2021-07-07 17:17:32 +02:00
|
|
|
continue; // Instances are indexed directly, don't need index uniforms.
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
max_uniforms++;
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r_gen_code.texture_uniforms.resize(max_texture_uniforms);
|
|
|
|
|
|
|
|
Vector<int> uniform_sizes;
|
|
|
|
Vector<int> uniform_alignments;
|
|
|
|
Vector<StringName> uniform_defines;
|
|
|
|
uniform_sizes.resize(max_uniforms);
|
|
|
|
uniform_alignments.resize(max_uniforms);
|
|
|
|
uniform_defines.resize(max_uniforms);
|
|
|
|
bool uses_uniforms = false;
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
Vector<StringName> uniform_names;
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<StringName, SL::ShaderNode::Uniform> &E : pnode->uniforms) {
|
|
|
|
uniform_names.push_back(E.key);
|
2021-05-25 02:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uniform_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
|
|
|
|
|
|
|
|
for (int k = 0; k < uniform_names.size(); k++) {
|
2023-11-18 23:40:56 +01:00
|
|
|
const StringName &uniform_name = uniform_names[k];
|
2021-05-25 02:25:11 +02:00
|
|
|
const SL::ShaderNode::Uniform &uniform = pnode->uniforms[uniform_name];
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
String ucode;
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
if (uniform.scope == SL::ShaderNode::Uniform::SCOPE_INSTANCE) {
|
2020-04-17 04:52:00 +02:00
|
|
|
//insert, but don't generate any code.
|
2021-05-25 02:25:11 +02:00
|
|
|
p_actions.uniforms->insert(uniform_name, uniform);
|
Fix various typos with codespell
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
2021-07-07 17:17:32 +02:00
|
|
|
continue; // Instances are indexed directly, don't need index uniforms.
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
2022-08-09 18:29:49 +02:00
|
|
|
|
|
|
|
if (uniform.hint == SL::ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
|
|
|
|
uniform.hint == SL::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
|
|
|
|
uniform.hint == SL::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
|
|
|
|
continue; // Don't create uniforms in the generated code for these.
|
|
|
|
}
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
if (SL::is_sampler_type(uniform.type)) {
|
2022-04-30 00:34:01 +02:00
|
|
|
// Texture layouts are different for OpenGL GLSL and Vulkan GLSL
|
|
|
|
if (!RS::get_singleton()->is_low_end()) {
|
|
|
|
ucode = "layout(set = " + itos(actions.texture_layout_set) + ", binding = " + itos(actions.base_texture_binding_index + uniform.texture_binding) + ") ";
|
|
|
|
}
|
|
|
|
ucode += "uniform ";
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
bool is_buffer_global = !SL::is_sampler_type(uniform.type) && uniform.scope == SL::ShaderNode::Uniform::SCOPE_GLOBAL;
|
2020-04-17 04:52:00 +02:00
|
|
|
|
|
|
|
if (is_buffer_global) {
|
|
|
|
//this is an integer to index the global table
|
|
|
|
ucode += _typestr(ShaderLanguage::TYPE_UINT);
|
|
|
|
} else {
|
2021-08-18 17:18:50 +02:00
|
|
|
ucode += _prestr(uniform.precision, ShaderLanguage::is_float_type(uniform.type));
|
2021-05-25 02:25:11 +02:00
|
|
|
ucode += _typestr(uniform.type);
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
ucode += " " + _mkid(uniform_name);
|
2021-06-09 11:33:41 +02:00
|
|
|
if (uniform.array_size > 0) {
|
|
|
|
ucode += "[";
|
|
|
|
ucode += itos(uniform.array_size);
|
|
|
|
ucode += "]";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
ucode += ";\n";
|
2021-05-25 02:25:11 +02:00
|
|
|
if (SL::is_sampler_type(uniform.type)) {
|
2021-04-13 22:01:43 +02:00
|
|
|
for (int j = 0; j < STAGE_MAX; j++) {
|
|
|
|
r_gen_code.stage_globals[j] += ucode;
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
GeneratedCode::Texture texture;
|
2021-05-25 02:25:11 +02:00
|
|
|
texture.name = uniform_name;
|
|
|
|
texture.hint = uniform.hint;
|
|
|
|
texture.type = uniform.type;
|
2022-05-31 10:44:44 +02:00
|
|
|
texture.use_color = uniform.use_color;
|
2021-05-25 02:25:11 +02:00
|
|
|
texture.filter = uniform.filter;
|
|
|
|
texture.repeat = uniform.repeat;
|
|
|
|
texture.global = uniform.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL;
|
2021-06-09 11:33:41 +02:00
|
|
|
texture.array_size = uniform.array_size;
|
2020-04-17 04:52:00 +02:00
|
|
|
if (texture.global) {
|
|
|
|
r_gen_code.uses_global_textures = true;
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
r_gen_code.texture_uniforms.write[uniform.texture_order] = texture;
|
2019-07-21 16:31:30 +02:00
|
|
|
} else {
|
|
|
|
if (!uses_uniforms) {
|
|
|
|
uses_uniforms = true;
|
|
|
|
}
|
2021-05-25 02:25:11 +02:00
|
|
|
uniform_defines.write[uniform.order] = ucode;
|
2020-04-17 04:52:00 +02:00
|
|
|
if (is_buffer_global) {
|
|
|
|
//globals are indices into the global table
|
2021-12-22 08:15:12 +01:00
|
|
|
uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(ShaderLanguage::TYPE_UINT);
|
2021-05-25 02:25:11 +02:00
|
|
|
uniform_alignments.write[uniform.order] = _get_datatype_alignment(ShaderLanguage::TYPE_UINT);
|
2020-04-17 04:52:00 +02:00
|
|
|
} else {
|
2021-11-25 21:34:58 +01:00
|
|
|
// The following code enforces a 16-byte alignment of uniform arrays.
|
2021-06-09 11:33:41 +02:00
|
|
|
if (uniform.array_size > 0) {
|
2021-12-22 08:15:12 +01:00
|
|
|
int size = ShaderLanguage::get_datatype_size(uniform.type) * uniform.array_size;
|
2021-06-09 11:33:41 +02:00
|
|
|
int m = (16 * uniform.array_size);
|
|
|
|
if ((size % m) != 0) {
|
|
|
|
size += m - (size % m);
|
|
|
|
}
|
|
|
|
uniform_sizes.write[uniform.order] = size;
|
2021-11-25 21:34:58 +01:00
|
|
|
uniform_alignments.write[uniform.order] = 16;
|
2021-06-09 11:33:41 +02:00
|
|
|
} else {
|
2021-12-22 08:15:12 +01:00
|
|
|
uniform_sizes.write[uniform.order] = ShaderLanguage::get_datatype_size(uniform.type);
|
2021-11-25 21:34:58 +01:00
|
|
|
uniform_alignments.write[uniform.order] = _get_datatype_alignment(uniform.type);
|
2021-06-09 11:33:41 +02:00
|
|
|
}
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
p_actions.uniforms->insert(uniform_name, uniform);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < max_uniforms; i++) {
|
|
|
|
r_gen_code.uniforms += uniform_defines[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// add up
|
|
|
|
int offset = 0;
|
|
|
|
for (int i = 0; i < uniform_sizes.size(); i++) {
|
|
|
|
int align = offset % uniform_alignments[i];
|
|
|
|
|
|
|
|
if (align != 0) {
|
|
|
|
offset += uniform_alignments[i] - align;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_gen_code.uniform_offsets.push_back(offset);
|
|
|
|
|
|
|
|
offset += uniform_sizes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
r_gen_code.uniform_total_size = offset;
|
2020-02-13 18:50:07 +01:00
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
if (r_gen_code.uniform_total_size % 16 != 0) { //UBO sizes must be multiples of 16
|
2019-07-28 18:42:15 +02:00
|
|
|
r_gen_code.uniform_total_size += 16 - (r_gen_code.uniform_total_size % 16);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 06:01:52 +02:00
|
|
|
uint32_t index = p_default_actions.base_varying_index;
|
|
|
|
|
2020-12-26 12:26:19 +01:00
|
|
|
List<Pair<StringName, SL::ShaderNode::Varying>> var_frag_to_light;
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
Vector<StringName> varying_names;
|
|
|
|
|
2021-08-09 22:13:42 +02:00
|
|
|
for (const KeyValue<StringName, SL::ShaderNode::Varying> &E : pnode->varyings) {
|
|
|
|
varying_names.push_back(E.key);
|
2021-05-25 02:25:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
varying_names.sort_custom<StringName::AlphCompare>(); //ensure order is deterministic so the same shader is always produced
|
|
|
|
|
|
|
|
for (int k = 0; k < varying_names.size(); k++) {
|
2023-11-18 23:40:56 +01:00
|
|
|
const StringName &varying_name = varying_names[k];
|
2021-05-25 02:25:11 +02:00
|
|
|
const SL::ShaderNode::Varying &varying = pnode->varyings[varying_name];
|
|
|
|
|
|
|
|
if (varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT_TO_LIGHT || varying.stage == SL::ShaderNode::Varying::STAGE_FRAGMENT) {
|
|
|
|
var_frag_to_light.push_back(Pair<StringName, SL::ShaderNode::Varying>(varying_name, varying));
|
|
|
|
fragment_varyings.insert(varying_name);
|
2020-12-26 12:26:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-22 21:19:30 +01:00
|
|
|
if (varying.type < SL::TYPE_INT) {
|
|
|
|
continue; // Ignore boolean types to prevent crashing (if varying is just declared).
|
|
|
|
}
|
2020-12-26 12:26:19 +01:00
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
String vcode;
|
2021-05-25 02:25:11 +02:00
|
|
|
String interp_mode = _interpstr(varying.interpolation);
|
2021-08-18 17:18:50 +02:00
|
|
|
vcode += _prestr(varying.precision, ShaderLanguage::is_float_type(varying.type));
|
2021-05-25 02:25:11 +02:00
|
|
|
vcode += _typestr(varying.type);
|
|
|
|
vcode += " " + _mkid(varying_name);
|
2022-01-14 08:10:42 +01:00
|
|
|
uint32_t inc = 1U;
|
|
|
|
|
2021-05-25 02:25:11 +02:00
|
|
|
if (varying.array_size > 0) {
|
2022-01-14 08:10:42 +01:00
|
|
|
inc = (uint32_t)varying.array_size;
|
|
|
|
|
2019-09-23 16:22:55 +02:00
|
|
|
vcode += "[";
|
2021-05-25 02:25:11 +02:00
|
|
|
vcode += itos(varying.array_size);
|
2019-09-23 16:22:55 +02:00
|
|
|
vcode += "]";
|
|
|
|
}
|
2022-01-14 08:10:42 +01:00
|
|
|
|
|
|
|
switch (varying.type) {
|
|
|
|
case SL::TYPE_MAT2:
|
|
|
|
inc *= 2U;
|
|
|
|
break;
|
|
|
|
case SL::TYPE_MAT3:
|
|
|
|
inc *= 3U;
|
|
|
|
break;
|
|
|
|
case SL::TYPE_MAT4:
|
|
|
|
inc *= 4U;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
vcode += ";\n";
|
2022-05-03 22:28:15 +02:00
|
|
|
// GLSL ES 3.0 does not allow layout qualifiers for varyings
|
|
|
|
if (!RS::get_singleton()->is_low_end()) {
|
|
|
|
r_gen_code.stage_globals[STAGE_VERTEX] += "layout(location=" + itos(index) + ") ";
|
|
|
|
r_gen_code.stage_globals[STAGE_FRAGMENT] += "layout(location=" + itos(index) + ") ";
|
|
|
|
}
|
|
|
|
r_gen_code.stage_globals[STAGE_VERTEX] += interp_mode + "out " + vcode;
|
|
|
|
r_gen_code.stage_globals[STAGE_FRAGMENT] += interp_mode + "in " + vcode;
|
2021-04-13 22:01:43 +02:00
|
|
|
|
2022-01-14 08:10:42 +01:00
|
|
|
index += inc;
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2020-12-26 12:26:19 +01:00
|
|
|
if (var_frag_to_light.size() > 0) {
|
|
|
|
String gcode = "\n\nstruct {\n";
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const Pair<StringName, SL::ShaderNode::Varying> &E : var_frag_to_light) {
|
2021-07-16 05:45:57 +02:00
|
|
|
gcode += "\t" + _prestr(E.second.precision) + _typestr(E.second.type) + " " + _mkid(E.first);
|
|
|
|
if (E.second.array_size > 0) {
|
2020-12-26 12:26:19 +01:00
|
|
|
gcode += "[";
|
2021-07-16 05:45:57 +02:00
|
|
|
gcode += itos(E.second.array_size);
|
2020-12-26 12:26:19 +01:00
|
|
|
gcode += "]";
|
|
|
|
}
|
|
|
|
gcode += ";\n";
|
|
|
|
}
|
|
|
|
gcode += "} frag_to_light;\n";
|
2021-04-13 22:01:43 +02:00
|
|
|
r_gen_code.stage_globals[STAGE_FRAGMENT] += gcode;
|
2020-12-26 12:26:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 11:40:17 +02:00
|
|
|
for (int i = 0; i < pnode->vconstants.size(); i++) {
|
|
|
|
const SL::ShaderNode::Constant &cnode = pnode->vconstants[i];
|
2019-07-21 16:31:30 +02:00
|
|
|
String gcode;
|
2021-11-19 09:25:23 +01:00
|
|
|
gcode += _constr(true);
|
2021-08-18 17:18:50 +02:00
|
|
|
gcode += _prestr(cnode.precision, ShaderLanguage::is_float_type(cnode.type));
|
2020-04-11 11:40:17 +02:00
|
|
|
if (cnode.type == SL::TYPE_STRUCT) {
|
2024-05-28 11:13:25 +02:00
|
|
|
gcode += _mkid(cnode.struct_name);
|
2020-01-17 20:35:22 +01:00
|
|
|
} else {
|
2020-04-11 11:40:17 +02:00
|
|
|
gcode += _typestr(cnode.type);
|
2020-01-17 20:35:22 +01:00
|
|
|
}
|
2020-04-11 11:40:17 +02:00
|
|
|
gcode += " " + _mkid(String(cnode.name));
|
2019-10-11 19:43:42 +02:00
|
|
|
if (cnode.array_size > 0) {
|
|
|
|
gcode += "[";
|
|
|
|
gcode += itos(cnode.array_size);
|
|
|
|
gcode += "]";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
gcode += "=";
|
2020-04-11 11:40:17 +02:00
|
|
|
gcode += _dump_node_code(cnode.initializer, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
gcode += ";\n";
|
2021-04-13 22:01:43 +02:00
|
|
|
for (int j = 0; j < STAGE_MAX; j++) {
|
|
|
|
r_gen_code.stage_globals[j] += gcode;
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<StringName, String> function_code;
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
//code for functions
|
2023-09-13 18:11:12 +02:00
|
|
|
for (int i = 0; i < pnode->vfunctions.size(); i++) {
|
|
|
|
SL::FunctionNode *fnode = pnode->vfunctions[i].function;
|
2019-09-14 05:37:42 +02:00
|
|
|
function = fnode;
|
2019-07-21 16:31:30 +02:00
|
|
|
current_func_name = fnode->name;
|
|
|
|
function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2020-04-02 01:20:12 +02:00
|
|
|
function = nullptr;
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//place functions in actual code
|
|
|
|
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<StringName> added_funcs_per_stage[STAGE_MAX];
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2023-09-13 18:11:12 +02:00
|
|
|
for (int i = 0; i < pnode->vfunctions.size(); i++) {
|
|
|
|
SL::FunctionNode *fnode = pnode->vfunctions[i].function;
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
function = fnode;
|
|
|
|
|
|
|
|
current_func_name = fnode->name;
|
|
|
|
|
2021-04-13 22:01:43 +02:00
|
|
|
if (p_actions.entry_point_stages.has(fnode->name)) {
|
|
|
|
Stage stage = p_actions.entry_point_stages[fnode->name];
|
|
|
|
_dump_function_deps(pnode, fnode->name, function_code, r_gen_code.stage_globals[stage], added_funcs_per_stage[stage]);
|
|
|
|
r_gen_code.code[fnode->name] = function_code[fnode->name];
|
2020-08-19 15:38:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
function = nullptr;
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//code+=dump_node_code(pnode->body,p_level);
|
2020-01-17 20:35:22 +01:00
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_STRUCT: {
|
2019-07-21 16:31:30 +02:00
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_FUNCTION: {
|
2019-07-21 16:31:30 +02:00
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_BLOCK: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::BlockNode *bnode = (SL::BlockNode *)p_node;
|
|
|
|
|
|
|
|
//variables
|
|
|
|
if (!bnode->single_statement) {
|
|
|
|
code += _mktab(p_level - 1) + "{\n";
|
|
|
|
}
|
|
|
|
|
2024-04-15 15:18:34 +02:00
|
|
|
int i = 0;
|
|
|
|
for (List<ShaderLanguage::Node *>::ConstIterator itr = bnode->statements.begin(); itr != bnode->statements.end(); ++itr, ++i) {
|
|
|
|
String scode = _dump_node_code(*itr, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2024-04-15 15:18:34 +02:00
|
|
|
if ((*itr)->type == SL::Node::NODE_TYPE_CONTROL_FLOW || bnode->single_statement) {
|
2019-07-21 16:31:30 +02:00
|
|
|
code += scode; //use directly
|
2022-02-03 09:13:20 +01:00
|
|
|
if (bnode->use_comma_between_statements && i + 1 < bnode->statements.size()) {
|
|
|
|
code += ",";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
} else {
|
|
|
|
code += _mktab(p_level) + scode + ";\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!bnode->single_statement) {
|
|
|
|
code += _mktab(p_level - 1) + "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_VARIABLE_DECLARATION: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::VariableDeclarationNode *vdnode = (SL::VariableDeclarationNode *)p_node;
|
|
|
|
|
2020-01-17 20:35:22 +01:00
|
|
|
String declaration;
|
2021-11-19 09:25:23 +01:00
|
|
|
declaration += _constr(vdnode->is_const);
|
2020-01-17 20:35:22 +01:00
|
|
|
if (vdnode->datatype == SL::TYPE_STRUCT) {
|
|
|
|
declaration += _mkid(vdnode->struct_name);
|
|
|
|
} else {
|
|
|
|
declaration += _prestr(vdnode->precision) + _typestr(vdnode->datatype);
|
|
|
|
}
|
2022-01-18 08:40:41 +01:00
|
|
|
declaration += " ";
|
2019-07-21 16:31:30 +02:00
|
|
|
for (int i = 0; i < vdnode->declarations.size(); i++) {
|
2022-01-18 08:40:41 +01:00
|
|
|
bool is_array = vdnode->declarations[i].size > 0;
|
2019-07-21 16:31:30 +02:00
|
|
|
if (i > 0) {
|
|
|
|
declaration += ",";
|
|
|
|
}
|
|
|
|
declaration += _mkid(vdnode->declarations[i].name);
|
2022-01-18 08:40:41 +01:00
|
|
|
if (is_array) {
|
|
|
|
declaration += "[";
|
|
|
|
if (vdnode->declarations[i].size_expression != nullptr) {
|
|
|
|
declaration += _dump_node_code(vdnode->declarations[i].size_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
} else {
|
|
|
|
declaration += itos(vdnode->declarations[i].size);
|
|
|
|
}
|
|
|
|
declaration += "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array || vdnode->declarations[i].single_expression) {
|
|
|
|
if (!vdnode->declarations[i].initializer.is_empty()) {
|
|
|
|
declaration += "=";
|
|
|
|
declaration += _dump_node_code(vdnode->declarations[i].initializer[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int size = vdnode->declarations[i].initializer.size();
|
|
|
|
if (size > 0) {
|
|
|
|
declaration += "=";
|
|
|
|
if (vdnode->datatype == SL::TYPE_STRUCT) {
|
|
|
|
declaration += _mkid(vdnode->struct_name);
|
|
|
|
} else {
|
|
|
|
declaration += _typestr(vdnode->datatype);
|
|
|
|
}
|
|
|
|
declaration += "[";
|
|
|
|
declaration += itos(size);
|
|
|
|
declaration += "]";
|
|
|
|
declaration += "(";
|
|
|
|
for (int j = 0; j < size; j++) {
|
|
|
|
if (j > 0) {
|
|
|
|
declaration += ",";
|
|
|
|
}
|
|
|
|
declaration += _dump_node_code(vdnode->declarations[i].initializer[j], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
}
|
|
|
|
declaration += ")";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code += declaration;
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_VARIABLE: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::VariableNode *vnode = (SL::VariableNode *)p_node;
|
2020-12-26 12:26:19 +01:00
|
|
|
bool use_fragment_varying = false;
|
|
|
|
|
2021-08-13 07:12:04 +02:00
|
|
|
if (!vnode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
|
2020-12-26 12:26:19 +01:00
|
|
|
if (p_assigning) {
|
|
|
|
if (shader->varyings.has(vnode->name)) {
|
|
|
|
use_fragment_varying = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fragment_varyings.has(vnode->name)) {
|
|
|
|
use_fragment_varying = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
if (p_assigning && p_actions.write_flag_pointers.has(vnode->name)) {
|
|
|
|
*p_actions.write_flag_pointers[vnode->name] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_default_actions.usage_defines.has(vnode->name) && !used_name_defines.has(vnode->name)) {
|
|
|
|
String define = p_default_actions.usage_defines[vnode->name];
|
|
|
|
if (define.begins_with("@")) {
|
|
|
|
define = p_default_actions.usage_defines[define.substr(1, define.length())];
|
|
|
|
}
|
|
|
|
r_gen_code.defines.push_back(define);
|
|
|
|
used_name_defines.insert(vnode->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
|
|
|
|
*p_actions.usage_flag_pointers[vnode->name] = true;
|
|
|
|
used_flag_pointers.insert(vnode->name);
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_default_actions.renames.has(vnode->name)) {
|
2019-07-21 16:31:30 +02:00
|
|
|
code = p_default_actions.renames[vnode->name];
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2020-04-17 04:52:00 +02:00
|
|
|
if (shader->uniforms.has(vnode->name)) {
|
|
|
|
//its a uniform!
|
|
|
|
const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[vnode->name];
|
2024-07-26 11:19:13 +02:00
|
|
|
if (u.is_texture()) {
|
2023-06-09 17:00:50 +02:00
|
|
|
StringName name;
|
2022-08-09 18:29:49 +02:00
|
|
|
if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
|
2023-01-05 20:55:59 +01:00
|
|
|
name = "color_buffer";
|
2022-08-09 18:29:49 +02:00
|
|
|
if (u.filter >= ShaderLanguage::FILTER_NEAREST_MIPMAP) {
|
|
|
|
r_gen_code.uses_screen_texture_mipmaps = true;
|
|
|
|
}
|
2023-01-05 20:55:59 +01:00
|
|
|
r_gen_code.uses_screen_texture = true;
|
2022-08-09 18:29:49 +02:00
|
|
|
} else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
|
2023-01-05 20:55:59 +01:00
|
|
|
name = "normal_roughness_buffer";
|
|
|
|
r_gen_code.uses_normal_roughness_texture = true;
|
2022-08-09 18:29:49 +02:00
|
|
|
} else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
|
2023-01-05 20:55:59 +01:00
|
|
|
name = "depth_buffer";
|
|
|
|
r_gen_code.uses_depth_texture = true;
|
2022-08-09 18:29:49 +02:00
|
|
|
} else {
|
|
|
|
name = _mkid(vnode->name); //texture, use as is
|
|
|
|
}
|
|
|
|
|
2023-01-05 20:55:59 +01:00
|
|
|
code = name;
|
2020-04-17 04:52:00 +02:00
|
|
|
} else {
|
|
|
|
//a scalar or vector
|
|
|
|
if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
|
|
|
|
code = actions.base_uniform_string + _mkid(vnode->name); //texture, use as is
|
|
|
|
//global variable, this means the code points to an index to the global table
|
2022-04-03 18:56:43 +02:00
|
|
|
code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
|
2020-04-17 04:52:00 +02:00
|
|
|
} else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
|
|
|
|
//instance variable, index it as such
|
2024-09-10 20:21:37 +02:00
|
|
|
code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + "u)";
|
2022-04-03 18:56:43 +02:00
|
|
|
code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
|
2020-04-17 04:52:00 +02:00
|
|
|
} else {
|
|
|
|
//regular uniform, index from UBO
|
|
|
|
code = actions.base_uniform_string + _mkid(vnode->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2020-12-26 12:26:19 +01:00
|
|
|
if (use_fragment_varying) {
|
|
|
|
code = "frag_to_light.";
|
|
|
|
}
|
|
|
|
code += _mkid(vnode->name); //its something else (local var most likely) use as is
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vnode->name == time_name) {
|
2021-04-13 22:01:43 +02:00
|
|
|
if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
|
2019-07-21 16:31:30 +02:00
|
|
|
r_gen_code.uses_vertex_time = true;
|
|
|
|
}
|
2021-04-13 22:01:43 +02:00
|
|
|
if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
|
2019-07-21 16:31:30 +02:00
|
|
|
r_gen_code.uses_fragment_time = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 10:45:09 +02:00
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_ARRAY_CONSTRUCT: {
|
2020-02-11 20:03:04 +01:00
|
|
|
SL::ArrayConstructNode *acnode = (SL::ArrayConstructNode *)p_node;
|
|
|
|
int sz = acnode->initializer.size();
|
|
|
|
if (acnode->datatype == SL::TYPE_STRUCT) {
|
|
|
|
code += _mkid(acnode->struct_name);
|
|
|
|
} else {
|
|
|
|
code += _typestr(acnode->datatype);
|
|
|
|
}
|
|
|
|
code += "[";
|
|
|
|
code += itos(acnode->initializer.size());
|
|
|
|
code += "]";
|
|
|
|
code += "(";
|
|
|
|
for (int i = 0; i < sz; i++) {
|
|
|
|
code += _dump_node_code(acnode->initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
if (i != sz - 1) {
|
|
|
|
code += ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code += ")";
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_ARRAY: {
|
2019-08-21 10:45:09 +02:00
|
|
|
SL::ArrayNode *anode = (SL::ArrayNode *)p_node;
|
2020-12-26 12:26:19 +01:00
|
|
|
bool use_fragment_varying = false;
|
|
|
|
|
2021-08-13 07:12:04 +02:00
|
|
|
if (!anode->is_local && !(p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX)) {
|
2021-05-21 18:16:23 +02:00
|
|
|
if (anode->assign_expression != nullptr && shader->varyings.has(anode->name)) {
|
2020-12-26 12:26:19 +01:00
|
|
|
use_fragment_varying = true;
|
|
|
|
} else {
|
|
|
|
if (p_assigning) {
|
|
|
|
if (shader->varyings.has(anode->name)) {
|
|
|
|
use_fragment_varying = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fragment_varyings.has(anode->name)) {
|
|
|
|
use_fragment_varying = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-21 10:45:09 +02:00
|
|
|
|
|
|
|
if (p_assigning && p_actions.write_flag_pointers.has(anode->name)) {
|
|
|
|
*p_actions.write_flag_pointers[anode->name] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_default_actions.usage_defines.has(anode->name) && !used_name_defines.has(anode->name)) {
|
|
|
|
String define = p_default_actions.usage_defines[anode->name];
|
|
|
|
if (define.begins_with("@")) {
|
|
|
|
define = p_default_actions.usage_defines[define.substr(1, define.length())];
|
|
|
|
}
|
|
|
|
r_gen_code.defines.push_back(define);
|
|
|
|
used_name_defines.insert(anode->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_actions.usage_flag_pointers.has(anode->name) && !used_flag_pointers.has(anode->name)) {
|
|
|
|
*p_actions.usage_flag_pointers[anode->name] = true;
|
|
|
|
used_flag_pointers.insert(anode->name);
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (p_default_actions.renames.has(anode->name)) {
|
2019-08-21 10:45:09 +02:00
|
|
|
code = p_default_actions.renames[anode->name];
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2021-06-09 11:33:41 +02:00
|
|
|
if (shader->uniforms.has(anode->name)) {
|
|
|
|
//its a uniform!
|
|
|
|
const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[anode->name];
|
2024-07-26 11:19:13 +02:00
|
|
|
if (u.is_texture()) {
|
2021-06-09 11:33:41 +02:00
|
|
|
code = _mkid(anode->name); //texture, use as is
|
|
|
|
} else {
|
|
|
|
//a scalar or vector
|
|
|
|
if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
|
|
|
|
code = actions.base_uniform_string + _mkid(anode->name); //texture, use as is
|
|
|
|
//global variable, this means the code points to an index to the global table
|
2022-04-03 18:56:43 +02:00
|
|
|
code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
|
2021-06-09 11:33:41 +02:00
|
|
|
} else if (u.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
|
|
|
|
//instance variable, index it as such
|
2024-09-10 20:21:37 +02:00
|
|
|
code = "(" + p_default_actions.instance_uniform_index_variable + "+" + itos(u.instance_index) + "u)";
|
2022-04-03 18:56:43 +02:00
|
|
|
code = _get_global_shader_uniform_from_type_and_index(p_default_actions.global_buffer_array_variable, code, u.type);
|
2021-06-09 11:33:41 +02:00
|
|
|
} else {
|
|
|
|
//regular uniform, index from UBO
|
|
|
|
code = actions.base_uniform_string + _mkid(anode->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (use_fragment_varying) {
|
|
|
|
code = "frag_to_light.";
|
|
|
|
}
|
|
|
|
code += _mkid(anode->name);
|
2020-12-26 12:26:19 +01:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-08-21 10:45:09 +02:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
if (anode->call_expression != nullptr) {
|
2019-08-21 10:45:09 +02:00
|
|
|
code += ".";
|
2020-06-03 18:08:36 +02:00
|
|
|
code += _dump_node_code(anode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
|
2020-12-26 19:13:24 +01:00
|
|
|
} else if (anode->index_expression != nullptr) {
|
2019-08-21 10:45:09 +02:00
|
|
|
code += "[";
|
|
|
|
code += _dump_node_code(anode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
code += "]";
|
2020-12-26 19:13:24 +01:00
|
|
|
} else if (anode->assign_expression != nullptr) {
|
|
|
|
code += "=";
|
|
|
|
code += _dump_node_code(anode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
|
2019-08-21 10:45:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (anode->name == time_name) {
|
2021-04-13 22:01:43 +02:00
|
|
|
if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_VERTEX) {
|
2019-08-21 10:45:09 +02:00
|
|
|
r_gen_code.uses_vertex_time = true;
|
|
|
|
}
|
2021-04-13 22:01:43 +02:00
|
|
|
if (p_actions.entry_point_stages.has(current_func_name) && p_actions.entry_point_stages[current_func_name] == STAGE_FRAGMENT) {
|
2019-08-21 10:45:09 +02:00
|
|
|
r_gen_code.uses_fragment_time = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_CONSTANT: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::ConstantNode *cnode = (SL::ConstantNode *)p_node;
|
2019-10-11 19:43:42 +02:00
|
|
|
|
|
|
|
if (cnode->array_size == 0) {
|
2024-09-12 15:49:09 +02:00
|
|
|
return get_constant_text(cnode->datatype, cnode->values);
|
2019-10-11 19:43:42 +02:00
|
|
|
} else {
|
|
|
|
if (cnode->get_datatype() == SL::TYPE_STRUCT) {
|
|
|
|
code += _mkid(cnode->struct_name);
|
|
|
|
} else {
|
|
|
|
code += _typestr(cnode->datatype);
|
|
|
|
}
|
|
|
|
code += "[";
|
|
|
|
code += itos(cnode->array_size);
|
|
|
|
code += "]";
|
|
|
|
code += "(";
|
|
|
|
for (int i = 0; i < cnode->array_size; i++) {
|
|
|
|
if (i > 0) {
|
|
|
|
code += ",";
|
|
|
|
} else {
|
|
|
|
code += "";
|
|
|
|
}
|
|
|
|
code += _dump_node_code(cnode->array_declarations[0].initializer[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
}
|
|
|
|
code += ")";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_OPERATOR: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::OperatorNode *onode = (SL::OperatorNode *)p_node;
|
|
|
|
|
|
|
|
switch (onode->op) {
|
|
|
|
case SL::OP_ASSIGN:
|
|
|
|
case SL::OP_ASSIGN_ADD:
|
|
|
|
case SL::OP_ASSIGN_SUB:
|
|
|
|
case SL::OP_ASSIGN_MUL:
|
|
|
|
case SL::OP_ASSIGN_DIV:
|
|
|
|
case SL::OP_ASSIGN_SHIFT_LEFT:
|
|
|
|
case SL::OP_ASSIGN_SHIFT_RIGHT:
|
|
|
|
case SL::OP_ASSIGN_MOD:
|
|
|
|
case SL::OP_ASSIGN_BIT_AND:
|
|
|
|
case SL::OP_ASSIGN_BIT_OR:
|
|
|
|
case SL::OP_ASSIGN_BIT_XOR:
|
2024-09-12 15:49:09 +02:00
|
|
|
code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, true) + _opstr(onode->op) + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
break;
|
|
|
|
case SL::OP_BIT_INVERT:
|
|
|
|
case SL::OP_NEGATE:
|
|
|
|
case SL::OP_NOT:
|
|
|
|
case SL::OP_DECREMENT:
|
2024-09-12 15:49:09 +02:00
|
|
|
case SL::OP_INCREMENT: {
|
|
|
|
const String node_code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
|
|
|
|
if (onode->op == SL::OP_NEGATE && node_code.begins_with("-")) { // To prevent writing unary minus twice.
|
|
|
|
code = node_code;
|
|
|
|
} else {
|
|
|
|
code = _opstr(onode->op) + node_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2019-07-21 16:31:30 +02:00
|
|
|
case SL::OP_POST_DECREMENT:
|
|
|
|
case SL::OP_POST_INCREMENT:
|
2024-09-12 15:49:09 +02:00
|
|
|
code = _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + _opstr(onode->op);
|
2019-07-21 16:31:30 +02:00
|
|
|
break;
|
|
|
|
case SL::OP_CALL:
|
2020-01-17 20:35:22 +01:00
|
|
|
case SL::OP_STRUCT:
|
2019-07-21 16:31:30 +02:00
|
|
|
case SL::OP_CONSTRUCT: {
|
2023-05-11 12:32:23 +02:00
|
|
|
ERR_FAIL_COND_V(onode->arguments[0]->type != SL::Node::NODE_TYPE_VARIABLE, String());
|
2022-11-14 09:11:33 +01:00
|
|
|
const SL::VariableNode *vnode = static_cast<const SL::VariableNode *>(onode->arguments[0]);
|
|
|
|
const SL::FunctionNode *func = nullptr;
|
|
|
|
const bool is_internal_func = internal_functions.has(vnode->name);
|
|
|
|
|
|
|
|
if (!is_internal_func) {
|
2023-09-13 18:11:12 +02:00
|
|
|
for (int i = 0; i < shader->vfunctions.size(); i++) {
|
|
|
|
if (shader->vfunctions[i].name == vnode->name) {
|
|
|
|
func = shader->vfunctions[i].function;
|
2022-11-14 09:11:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
bool is_texture_func = false;
|
2021-07-26 13:31:15 +02:00
|
|
|
bool is_screen_texture = false;
|
2023-02-10 21:54:22 +01:00
|
|
|
bool texture_func_no_uv = false;
|
|
|
|
bool texture_func_returns_data = false;
|
|
|
|
|
2020-01-17 20:35:22 +01:00
|
|
|
if (onode->op == SL::OP_STRUCT) {
|
|
|
|
code += _mkid(vnode->name);
|
|
|
|
} else if (onode->op == SL::OP_CONSTRUCT) {
|
2019-07-21 16:31:30 +02:00
|
|
|
code += String(vnode->name);
|
|
|
|
} else {
|
2020-11-26 13:50:21 +01:00
|
|
|
if (p_actions.usage_flag_pointers.has(vnode->name) && !used_flag_pointers.has(vnode->name)) {
|
|
|
|
*p_actions.usage_flag_pointers[vnode->name] = true;
|
|
|
|
used_flag_pointers.insert(vnode->name);
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:11:33 +01:00
|
|
|
if (is_internal_func) {
|
2019-07-21 16:31:30 +02:00
|
|
|
code += vnode->name;
|
|
|
|
is_texture_func = texture_functions.has(vnode->name);
|
2023-02-10 21:54:22 +01:00
|
|
|
texture_func_no_uv = (vnode->name == "textureSize" || vnode->name == "textureQueryLevels");
|
|
|
|
texture_func_returns_data = texture_func_no_uv || vnode->name == "textureQueryLod";
|
2019-07-21 16:31:30 +02:00
|
|
|
} else if (p_default_actions.renames.has(vnode->name)) {
|
|
|
|
code += p_default_actions.renames[vnode->name];
|
|
|
|
} else {
|
2024-05-27 21:16:54 +02:00
|
|
|
code += _mkid(vnode->rname);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code += "(";
|
|
|
|
|
2023-01-15 06:07:59 +01:00
|
|
|
// if color backbuffer, depth backbuffer or normal roughness texture is used,
|
|
|
|
// we will add logic to automatically switch between
|
2023-01-09 20:47:14 +01:00
|
|
|
// sampler2D and sampler2D array and vec2 UV and vec3 UV.
|
2023-01-15 06:07:59 +01:00
|
|
|
bool multiview_uv_needed = false;
|
2023-12-18 23:35:26 +01:00
|
|
|
bool is_normal_roughness_texture = false;
|
2023-01-09 20:47:14 +01:00
|
|
|
|
2019-07-21 16:31:30 +02:00
|
|
|
for (int i = 1; i < onode->arguments.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i > 1) {
|
2019-07-21 16:31:30 +02:00
|
|
|
code += ", ";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-11-14 09:11:33 +01:00
|
|
|
|
|
|
|
bool is_out_qualifier = false;
|
|
|
|
if (is_internal_func) {
|
|
|
|
is_out_qualifier = SL::is_builtin_func_out_parameter(vnode->name, i - 1);
|
|
|
|
} else if (func != nullptr) {
|
|
|
|
const SL::ArgumentQualifier qualifier = func->arguments[i - 1].qualifier;
|
|
|
|
is_out_qualifier = qualifier == SL::ARGUMENT_QUALIFIER_OUT || qualifier == SL::ARGUMENT_QUALIFIER_INOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_out_qualifier) {
|
|
|
|
StringName name;
|
|
|
|
bool found = false;
|
|
|
|
{
|
|
|
|
const SL::Node *node = onode->arguments[i];
|
|
|
|
|
|
|
|
bool done = false;
|
|
|
|
do {
|
|
|
|
switch (node->type) {
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_VARIABLE: {
|
2022-11-14 09:11:33 +01:00
|
|
|
name = static_cast<const SL::VariableNode *>(node)->name;
|
|
|
|
done = true;
|
|
|
|
found = true;
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_MEMBER: {
|
2022-11-14 09:11:33 +01:00
|
|
|
node = static_cast<const SL::MemberNode *>(node)->owner;
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
done = true;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
} while (!done);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found && p_actions.write_flag_pointers.has(name)) {
|
|
|
|
*p_actions.write_flag_pointers[name] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:49:09 +02:00
|
|
|
String node_code = _dump_node_code(onode->arguments[i], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2023-01-15 06:07:59 +01:00
|
|
|
if (is_texture_func && i == 1) {
|
|
|
|
// If we're doing a texture lookup we need to check our texture argument
|
2021-06-09 11:33:41 +02:00
|
|
|
StringName texture_uniform;
|
|
|
|
bool correct_texture_uniform = false;
|
|
|
|
|
2021-10-06 21:07:50 +02:00
|
|
|
switch (onode->arguments[i]->type) {
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_VARIABLE: {
|
2021-10-06 21:07:50 +02:00
|
|
|
const SL::VariableNode *varnode = static_cast<const SL::VariableNode *>(onode->arguments[i]);
|
|
|
|
texture_uniform = varnode->name;
|
|
|
|
correct_texture_uniform = true;
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_ARRAY: {
|
2021-10-06 21:07:50 +02:00
|
|
|
const SL::ArrayNode *anode = static_cast<const SL::ArrayNode *>(onode->arguments[i]);
|
2021-06-09 11:33:41 +02:00
|
|
|
texture_uniform = anode->name;
|
|
|
|
correct_texture_uniform = true;
|
2021-10-06 21:07:50 +02:00
|
|
|
} break;
|
|
|
|
default:
|
|
|
|
break;
|
2021-06-09 11:33:41 +02:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2023-01-15 06:07:59 +01:00
|
|
|
if (correct_texture_uniform && !RS::get_singleton()->is_low_end()) {
|
|
|
|
// Need to map from texture to sampler in order to sample when using Vulkan GLSL.
|
2021-06-09 11:33:41 +02:00
|
|
|
String sampler_name;
|
2023-01-15 06:07:59 +01:00
|
|
|
bool is_depth_texture = false;
|
2019-07-21 16:31:30 +02:00
|
|
|
|
2021-06-09 11:33:41 +02:00
|
|
|
if (actions.custom_samplers.has(texture_uniform)) {
|
|
|
|
sampler_name = actions.custom_samplers[texture_uniform];
|
2019-07-21 16:31:30 +02:00
|
|
|
} else {
|
2021-06-09 11:33:41 +02:00
|
|
|
if (shader->uniforms.has(texture_uniform)) {
|
2023-01-05 20:55:59 +01:00
|
|
|
const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[texture_uniform];
|
|
|
|
if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
|
|
|
|
is_screen_texture = true;
|
2023-01-15 06:07:59 +01:00
|
|
|
} else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
|
|
|
|
is_depth_texture = true;
|
2023-01-05 20:55:59 +01:00
|
|
|
} else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
|
|
|
|
is_normal_roughness_texture = true;
|
|
|
|
}
|
|
|
|
sampler_name = _get_sampler_name(u.filter, u.repeat);
|
2021-06-09 11:33:41 +02:00
|
|
|
} else {
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (int j = 0; j < function->arguments.size(); j++) {
|
|
|
|
if (function->arguments[j].name == texture_uniform) {
|
|
|
|
if (function->arguments[j].tex_builtin_check) {
|
|
|
|
ERR_CONTINUE(!actions.custom_samplers.has(function->arguments[j].tex_builtin));
|
|
|
|
sampler_name = actions.custom_samplers[function->arguments[j].tex_builtin];
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (function->arguments[j].tex_argument_check) {
|
2024-07-26 19:45:36 +02:00
|
|
|
if (function->arguments[j].tex_hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
|
|
|
|
is_screen_texture = true;
|
|
|
|
} else if (function->arguments[j].tex_hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
|
|
|
|
is_depth_texture = true;
|
|
|
|
} else if (function->arguments[j].tex_hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
|
|
|
|
is_normal_roughness_texture = true;
|
|
|
|
}
|
2021-06-09 11:33:41 +02:00
|
|
|
sampler_name = _get_sampler_name(function->arguments[j].tex_argument_filter, function->arguments[j].tex_argument_repeat);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-09 11:33:41 +02:00
|
|
|
if (!found) {
|
|
|
|
//function was most likely unused, so use anything (compiler will remove it anyway)
|
|
|
|
sampler_name = _get_sampler_name(ShaderLanguage::FILTER_DEFAULT, ShaderLanguage::REPEAT_DEFAULT);
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 20:47:14 +01:00
|
|
|
String data_type_name = "";
|
2023-01-15 06:07:59 +01:00
|
|
|
if (actions.check_multiview_samplers && (is_screen_texture || is_depth_texture || is_normal_roughness_texture)) {
|
2023-01-09 20:47:14 +01:00
|
|
|
data_type_name = "multiviewSampler";
|
2023-01-15 06:07:59 +01:00
|
|
|
multiview_uv_needed = true;
|
2023-01-09 20:47:14 +01:00
|
|
|
} else {
|
|
|
|
data_type_name = ShaderLanguage::get_datatype_name(onode->arguments[i]->get_datatype());
|
|
|
|
}
|
|
|
|
|
|
|
|
code += data_type_name + "(" + node_code + ", " + sampler_name + ")";
|
2023-01-15 06:07:59 +01:00
|
|
|
} else if (actions.check_multiview_samplers && correct_texture_uniform && RS::get_singleton()->is_low_end()) {
|
|
|
|
// Texture function on low end hardware (i.e. OpenGL).
|
|
|
|
// We just need to know if the texture supports multiview.
|
|
|
|
|
|
|
|
if (shader->uniforms.has(texture_uniform)) {
|
|
|
|
const ShaderLanguage::ShaderNode::Uniform &u = shader->uniforms[texture_uniform];
|
|
|
|
if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE) {
|
|
|
|
multiview_uv_needed = true;
|
|
|
|
} else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
|
|
|
|
multiview_uv_needed = true;
|
|
|
|
} else if (u.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE) {
|
|
|
|
multiview_uv_needed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
code += node_code;
|
2021-06-09 11:33:41 +02:00
|
|
|
} else {
|
|
|
|
code += node_code;
|
|
|
|
}
|
2023-02-10 21:54:22 +01:00
|
|
|
} else if (multiview_uv_needed && !texture_func_no_uv && i == 2) {
|
2023-01-15 06:07:59 +01:00
|
|
|
// UV coordinate after using color, depth or normal roughness texture.
|
|
|
|
node_code = "multiview_uv(" + node_code + ".xy)";
|
2023-01-09 20:47:14 +01:00
|
|
|
|
2023-01-15 06:07:59 +01:00
|
|
|
code += node_code;
|
|
|
|
} else {
|
2019-07-21 16:31:30 +02:00
|
|
|
code += node_code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code += ")";
|
2023-02-10 21:54:22 +01:00
|
|
|
if (is_screen_texture && !texture_func_returns_data && actions.apply_luminance_multiplier) {
|
2024-03-15 18:13:31 +01:00
|
|
|
code = "(" + code + " * vec4(vec3(sc_luminance_multiplier()), 1.0))";
|
2021-07-26 13:31:15 +02:00
|
|
|
}
|
2024-01-15 10:22:03 +01:00
|
|
|
if (is_normal_roughness_texture && !texture_func_returns_data) {
|
2023-12-18 23:35:26 +01:00
|
|
|
code = "normal_roughness_compatibility(" + code + ")";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
} break;
|
|
|
|
case SL::OP_INDEX: {
|
2024-09-12 15:49:09 +02:00
|
|
|
code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
code += "[";
|
2024-09-12 15:49:09 +02:00
|
|
|
code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
code += "]";
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case SL::OP_SELECT_IF: {
|
2019-08-21 10:38:51 +02:00
|
|
|
code += "(";
|
2024-09-12 15:49:09 +02:00
|
|
|
code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
code += "?";
|
2024-09-12 15:49:09 +02:00
|
|
|
code += _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
code += ":";
|
2024-09-12 15:49:09 +02:00
|
|
|
code += _dump_node_code(onode->arguments[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-08-21 10:38:51 +02:00
|
|
|
code += ")";
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
} break;
|
2021-11-30 21:28:35 +01:00
|
|
|
case SL::OP_EMPTY: {
|
|
|
|
// Semicolon (or empty statement) - ignored.
|
|
|
|
} break;
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
default: {
|
2020-06-03 18:08:36 +02:00
|
|
|
if (p_use_scope) {
|
|
|
|
code += "(";
|
|
|
|
}
|
2024-09-12 15:49:09 +02:00
|
|
|
code += _dump_node_code(onode->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + " " + _opstr(onode->op) + " " + _dump_node_code(onode->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2020-06-03 18:08:36 +02:00
|
|
|
if (p_use_scope) {
|
|
|
|
code += ")";
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_CONTROL_FLOW: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::ControlFlowNode *cfnode = (SL::ControlFlowNode *)p_node;
|
|
|
|
if (cfnode->flow_op == SL::FLOW_OP_IF) {
|
|
|
|
code += _mktab(p_level) + "if (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
|
|
|
|
code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
if (cfnode->blocks.size() == 2) {
|
|
|
|
code += _mktab(p_level) + "else\n";
|
|
|
|
code += _dump_node_code(cfnode->blocks[1], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
}
|
2019-09-23 16:09:54 +02:00
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_SWITCH) {
|
|
|
|
code += _mktab(p_level) + "switch (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
|
|
|
|
code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_CASE) {
|
|
|
|
code += _mktab(p_level) + "case " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ":\n";
|
|
|
|
code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_DEFAULT) {
|
|
|
|
code += _mktab(p_level) + "default:\n";
|
|
|
|
code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-08-21 10:33:03 +02:00
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_DO) {
|
|
|
|
code += _mktab(p_level) + "do";
|
|
|
|
code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ");";
|
2019-07-21 16:31:30 +02:00
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_WHILE) {
|
|
|
|
code += _mktab(p_level) + "while (" + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ")\n";
|
|
|
|
code += _dump_node_code(cfnode->blocks[0], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_FOR) {
|
|
|
|
String left = _dump_node_code(cfnode->blocks[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2022-02-03 09:13:20 +01:00
|
|
|
String middle = _dump_node_code(cfnode->blocks[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
String right = _dump_node_code(cfnode->blocks[2], p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
code += _mktab(p_level) + "for (" + left + ";" + middle + ";" + right + ")\n";
|
2022-02-03 09:13:20 +01:00
|
|
|
code += _dump_node_code(cfnode->blocks[3], p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning);
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_RETURN) {
|
|
|
|
if (cfnode->expressions.size()) {
|
|
|
|
code = "return " + _dump_node_code(cfnode->expressions[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + ";";
|
|
|
|
} else {
|
|
|
|
code = "return;";
|
|
|
|
}
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_DISCARD) {
|
|
|
|
if (p_actions.usage_flag_pointers.has("DISCARD") && !used_flag_pointers.has("DISCARD")) {
|
|
|
|
*p_actions.usage_flag_pointers["DISCARD"] = true;
|
|
|
|
used_flag_pointers.insert("DISCARD");
|
|
|
|
}
|
|
|
|
|
|
|
|
code = "discard;";
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_CONTINUE) {
|
|
|
|
code = "continue;";
|
|
|
|
} else if (cfnode->flow_op == SL::FLOW_OP_BREAK) {
|
|
|
|
code = "break;";
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2023-05-11 12:32:23 +02:00
|
|
|
case SL::Node::NODE_TYPE_MEMBER: {
|
2019-07-21 16:31:30 +02:00
|
|
|
SL::MemberNode *mnode = (SL::MemberNode *)p_node;
|
|
|
|
code = _dump_node_code(mnode->owner, p_level, r_gen_code, p_actions, p_default_actions, p_assigning) + "." + mnode->name;
|
2020-04-02 01:20:12 +02:00
|
|
|
if (mnode->index_expression != nullptr) {
|
2020-02-11 20:03:04 +01:00
|
|
|
code += "[";
|
|
|
|
code += _dump_node_code(mnode->index_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning);
|
|
|
|
code += "]";
|
2020-12-26 19:13:24 +01:00
|
|
|
} else if (mnode->assign_expression != nullptr) {
|
|
|
|
code += "=";
|
|
|
|
code += _dump_node_code(mnode->assign_expression, p_level, r_gen_code, p_actions, p_default_actions, true, false);
|
2021-05-19 20:50:11 +02:00
|
|
|
} else if (mnode->call_expression != nullptr) {
|
|
|
|
code += ".";
|
|
|
|
code += _dump_node_code(mnode->call_expression, p_level, r_gen_code, p_actions, p_default_actions, p_assigning, false);
|
2020-02-11 20:03:04 +01:00
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
2022-10-03 13:01:46 +02:00
|
|
|
ShaderLanguage::DataType ShaderCompiler::_get_global_shader_uniform_type(const StringName &p_name) {
|
|
|
|
RS::GlobalShaderParameterType gvt = RSG::material_storage->global_shader_parameter_get_type(p_name);
|
2022-04-03 18:56:43 +02:00
|
|
|
return (ShaderLanguage::DataType)RS::global_shader_uniform_type_get_shader_datatype(gvt);
|
2020-04-17 04:52:00 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
Error ShaderCompiler::compile(RS::ShaderMode p_mode, const String &p_code, IdentifierActions *p_actions, const String &p_path, GeneratedCode &r_gen_code) {
|
2021-08-08 07:04:20 +02:00
|
|
|
SL::ShaderCompileInfo info;
|
|
|
|
info.functions = ShaderTypes::get_singleton()->get_functions(p_mode);
|
|
|
|
info.render_modes = ShaderTypes::get_singleton()->get_modes(p_mode);
|
|
|
|
info.shader_types = ShaderTypes::get_singleton()->get_types();
|
2022-10-03 13:01:46 +02:00
|
|
|
info.global_shader_uniform_type_func = _get_global_shader_uniform_type;
|
2021-08-08 07:04:20 +02:00
|
|
|
|
|
|
|
Error err = parser.compile(p_code, info);
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
if (err != OK) {
|
2022-06-29 11:31:18 +02:00
|
|
|
Vector<ShaderLanguage::FilePosition> include_positions = parser.get_include_positions();
|
|
|
|
|
|
|
|
String current;
|
|
|
|
HashMap<String, Vector<String>> includes;
|
|
|
|
includes[""] = Vector<String>();
|
|
|
|
Vector<String> include_stack;
|
2022-09-29 11:53:28 +02:00
|
|
|
Vector<String> shader_lines = p_code.split("\n");
|
2022-06-29 11:31:18 +02:00
|
|
|
|
|
|
|
// Reconstruct the files.
|
2022-09-29 11:53:28 +02:00
|
|
|
for (int i = 0; i < shader_lines.size(); i++) {
|
|
|
|
String l = shader_lines[i];
|
2022-06-29 11:31:18 +02:00
|
|
|
if (l.begins_with("@@>")) {
|
|
|
|
String inc_path = l.replace_first("@@>", "");
|
|
|
|
|
|
|
|
l = "#include \"" + inc_path + "\"";
|
|
|
|
includes[current].append("#include \"" + inc_path + "\""); // Restore the include directive
|
|
|
|
include_stack.push_back(current);
|
|
|
|
current = inc_path;
|
|
|
|
includes[inc_path] = Vector<String>();
|
|
|
|
|
|
|
|
} else if (l.begins_with("@@<")) {
|
|
|
|
if (include_stack.size()) {
|
|
|
|
current = include_stack[include_stack.size() - 1];
|
|
|
|
include_stack.resize(include_stack.size() - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
includes[current].push_back(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the files.
|
|
|
|
for (const KeyValue<String, Vector<String>> &E : includes) {
|
|
|
|
if (E.key.is_empty()) {
|
|
|
|
if (p_path == "") {
|
|
|
|
print_line("--Main Shader--");
|
|
|
|
} else {
|
|
|
|
print_line("--" + p_path + "--");
|
|
|
|
}
|
2021-07-17 03:05:11 +02:00
|
|
|
} else {
|
2022-06-29 11:31:18 +02:00
|
|
|
print_line("--" + E.key + "--");
|
2021-07-17 03:05:11 +02:00
|
|
|
}
|
2022-06-29 11:31:18 +02:00
|
|
|
int err_line = -1;
|
|
|
|
for (int i = 0; i < include_positions.size(); i++) {
|
|
|
|
if (include_positions[i].file == E.key) {
|
|
|
|
err_line = include_positions[i].line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const Vector<String> &V = E.value;
|
|
|
|
for (int i = 0; i < V.size(); i++) {
|
|
|
|
if (i == err_line - 1) {
|
|
|
|
// Mark the error line to be visible without having to look at
|
|
|
|
// the trace at the end.
|
|
|
|
print_line(vformat("E%4d-> %s", i + 1, V[i]));
|
|
|
|
} else {
|
|
|
|
print_line(vformat("%5d | %s", i + 1, V[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String file;
|
|
|
|
int line;
|
|
|
|
if (include_positions.size() > 1) {
|
|
|
|
file = include_positions[include_positions.size() - 1].file;
|
|
|
|
line = include_positions[include_positions.size() - 1].line;
|
|
|
|
} else {
|
|
|
|
file = p_path;
|
|
|
|
line = parser.get_error_line();
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 11:31:18 +02:00
|
|
|
_err_print_error(nullptr, file.utf8().get_data(), line, parser.get_error_text().utf8().get_data(), false, ERR_HANDLER_SHADER);
|
2019-07-21 16:31:30 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
r_gen_code.defines.clear();
|
2021-04-13 22:01:43 +02:00
|
|
|
r_gen_code.code.clear();
|
|
|
|
for (int i = 0; i < STAGE_MAX; i++) {
|
|
|
|
r_gen_code.stage_globals[i] = String();
|
|
|
|
}
|
2019-07-21 16:31:30 +02:00
|
|
|
r_gen_code.uses_fragment_time = false;
|
|
|
|
r_gen_code.uses_vertex_time = false;
|
2020-04-17 04:52:00 +02:00
|
|
|
r_gen_code.uses_global_textures = false;
|
2022-08-09 18:29:49 +02:00
|
|
|
r_gen_code.uses_screen_texture_mipmaps = false;
|
2023-01-05 20:55:59 +01:00
|
|
|
r_gen_code.uses_screen_texture = false;
|
|
|
|
r_gen_code.uses_depth_texture = false;
|
|
|
|
r_gen_code.uses_normal_roughness_texture = false;
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
used_name_defines.clear();
|
|
|
|
used_rmode_defines.clear();
|
|
|
|
used_flag_pointers.clear();
|
2020-12-26 12:26:19 +01:00
|
|
|
fragment_varyings.clear();
|
2019-07-21 16:31:30 +02:00
|
|
|
|
|
|
|
shader = parser.get_shader();
|
2020-04-02 01:20:12 +02:00
|
|
|
function = nullptr;
|
2019-07-21 16:31:30 +02:00
|
|
|
_dump_node_code(shader, 1, r_gen_code, *p_actions, actions, false);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
void ShaderCompiler::initialize(DefaultIdentifierActions p_actions) {
|
2019-07-21 16:31:30 +02:00
|
|
|
actions = p_actions;
|
|
|
|
|
|
|
|
time_name = "TIME";
|
|
|
|
|
|
|
|
List<String> func_list;
|
|
|
|
|
|
|
|
ShaderLanguage::get_builtin_funcs(&func_list);
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const String &E : func_list) {
|
2021-07-16 05:45:57 +02:00
|
|
|
internal_functions.insert(E);
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
texture_functions.insert("texture");
|
|
|
|
texture_functions.insert("textureProj");
|
|
|
|
texture_functions.insert("textureLod");
|
|
|
|
texture_functions.insert("textureProjLod");
|
|
|
|
texture_functions.insert("textureGrad");
|
2022-08-12 10:29:10 +02:00
|
|
|
texture_functions.insert("textureProjGrad");
|
2021-10-13 11:49:57 +02:00
|
|
|
texture_functions.insert("textureGather");
|
2020-10-21 16:45:40 +02:00
|
|
|
texture_functions.insert("textureSize");
|
2022-08-12 11:51:03 +02:00
|
|
|
texture_functions.insert("textureQueryLod");
|
|
|
|
texture_functions.insert("textureQueryLevels");
|
2020-10-21 16:45:40 +02:00
|
|
|
texture_functions.insert("texelFetch");
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:25:42 +01:00
|
|
|
ShaderCompiler::ShaderCompiler() {
|
2019-07-21 16:31:30 +02:00
|
|
|
}
|