Better visual shader code generation

This commit is contained in:
Yuri Roubinsky 2020-01-27 12:10:51 +03:00
parent 4e7d75ccd3
commit bfec48abf1
3 changed files with 249 additions and 160 deletions

View File

@ -33,6 +33,10 @@
#include "core/vmap.h"
#include "servers/visual/shader_types.h"
bool VisualShaderNode::is_simple_decl() const {
return simple_decl;
}
void VisualShaderNode::set_output_port_for_preview(int p_index) {
port_preview = p_index;
@ -132,6 +136,7 @@ void VisualShaderNode::_bind_methods() {
VisualShaderNode::VisualShaderNode() {
port_preview = -1;
simple_decl = true;
}
/////////////////////////////////////////////////////////
@ -273,6 +278,7 @@ void VisualShaderNodeCustom::_bind_methods() {
}
VisualShaderNodeCustom::VisualShaderNodeCustom() {
simple_decl = false;
}
/////////////////////////////////////////////////////////
@ -1157,8 +1163,21 @@ Error VisualShader::_write_node(Type type, StringBuilder &global_code, StringBui
output_vars.resize(vsnode->get_output_port_count());
String *outputs = output_vars.ptrw();
if (vsnode->is_simple_decl()) { // less code to generate for some simple_decl nodes
for (int i = 0; i < output_count; i++) {
String var_name = "n_out" + itos(node) + "p" + itos(i);
switch (vsnode->get_output_port_type(i)) {
case VisualShaderNode::PORT_TYPE_SCALAR: outputs[i] = "float " + var_name; break;
case VisualShaderNode::PORT_TYPE_VECTOR: outputs[i] = "vec3 " + var_name; break;
case VisualShaderNode::PORT_TYPE_BOOLEAN: outputs[i] = "bool " + var_name; break;
case VisualShaderNode::PORT_TYPE_TRANSFORM: outputs[i] = "mat4 " + var_name; break;
default: {
}
}
}
} else {
for (int i = 0; i < output_count; i++) {
outputs[i] = "n_out" + itos(node) + "p" + itos(i);
switch (vsnode->get_output_port_type(i)) {
case VisualShaderNode::PORT_TYPE_SCALAR: code += String() + "\tfloat " + outputs[i] + ";\n"; break;
@ -1169,6 +1188,7 @@ Error VisualShader::_write_node(Type type, StringBuilder &global_code, StringBui
}
}
}
}
Vector<VisualShader::DefaultTextureParam> params = vsnode->get_default_texture_parameters(type, node);
for (int i = 0; i < params.size(); i++) {
@ -2514,6 +2534,7 @@ VisualShaderNodeGroupBase::VisualShaderNodeGroupBase() {
inputs = "";
outputs = "";
editable = false;
simple_decl = false;
}
////////////// Expression

View File

@ -176,6 +176,7 @@ class VisualShaderNode : public Resource {
Map<int, Variant> default_input_values;
protected:
bool simple_decl;
static void _bind_methods();
public:
@ -188,6 +189,8 @@ public:
PORT_TYPE_MAX,
};
bool is_simple_decl() const;
virtual String get_caption() const = 0;
virtual int get_input_port_count() const = 0;

View File

@ -440,7 +440,7 @@ String VisualShaderNodeTexture::generate_global(Shader::Mode p_mode, VisualShade
case TYPE_COLOR: u += " : hint_albedo"; break;
case TYPE_NORMALMAP: u += " : hint_normal"; break;
}
return u + ";";
return u + ";\n";
}
return String();
@ -621,6 +621,26 @@ String VisualShaderNodeTexture::generate_code(Shader::Mode p_mode, VisualShader:
void VisualShaderNodeTexture::set_source(Source p_source) {
source = p_source;
switch (source) {
case SOURCE_TEXTURE:
simple_decl = true;
break;
case SOURCE_SCREEN:
simple_decl = false;
break;
case SOURCE_2D_TEXTURE:
simple_decl = false;
break;
case SOURCE_2D_NORMAL:
simple_decl = false;
break;
case SOURCE_DEPTH:
simple_decl = false;
break;
case SOURCE_PORT:
simple_decl = false;
break;
}
emit_changed();
emit_signal("editor_refresh_request");
}
@ -913,6 +933,7 @@ void VisualShaderNodeCubeMap::_bind_methods() {
VisualShaderNodeCubeMap::VisualShaderNodeCubeMap() {
texture_type = TYPE_DATA;
source = SOURCE_TEXTURE;
simple_decl = false;
}
////////////// Scalar Op
@ -1217,6 +1238,35 @@ String VisualShaderNodeColorOp::generate_code(Shader::Mode p_mode, VisualShader:
void VisualShaderNodeColorOp::set_operator(Operator p_op) {
op = p_op;
switch (op) {
case OP_SCREEN:
simple_decl = true;
break;
case OP_DIFFERENCE:
simple_decl = true;
break;
case OP_DARKEN:
simple_decl = true;
break;
case OP_LIGHTEN:
simple_decl = true;
break;
case OP_OVERLAY:
simple_decl = false;
break;
case OP_DODGE:
simple_decl = true;
break;
case OP_BURN:
simple_decl = true;
break;
case OP_SOFT_LIGHT:
simple_decl = false;
break;
case OP_HARD_LIGHT:
simple_decl = false;
break;
}
emit_changed();
}
@ -1645,6 +1695,13 @@ String VisualShaderNodeVectorFunc::generate_code(Shader::Mode p_mode, VisualShad
void VisualShaderNodeVectorFunc::set_function(Function p_func) {
func = p_func;
if (func == FUNC_RGB2HSV) {
simple_decl = false;
} else if (func == FUNC_HSV2RGB) {
simple_decl = false;
} else {
simple_decl = true;
}
emit_changed();
}
@ -1797,6 +1854,7 @@ void VisualShaderNodeColorFunc::_bind_methods() {
VisualShaderNodeColorFunc::VisualShaderNodeColorFunc() {
func = FUNC_GRAYSCALE;
set_input_port_default_value(0, Vector3());
simple_decl = false;
}
////////////// Transform Func
@ -3309,6 +3367,7 @@ String VisualShaderNodeTextureUniform::get_input_port_default_hint(int p_port) c
VisualShaderNodeTextureUniform::VisualShaderNodeTextureUniform() {
texture_type = TYPE_DATA;
color_default = COLOR_DEFAULT_WHITE;
simple_decl = false;
}
////////////// Texture Uniform (Triplanar)
@ -3549,6 +3608,7 @@ VisualShaderNodeIf::VisualShaderNodeIf() {
set_input_port_default_value(3, Vector3(0.0, 0.0, 0.0));
set_input_port_default_value(4, Vector3(0.0, 0.0, 0.0));
set_input_port_default_value(5, Vector3(0.0, 0.0, 0.0));
simple_decl = false;
}
////////////// Switch
@ -3611,6 +3671,7 @@ VisualShaderNodeSwitch::VisualShaderNodeSwitch() {
set_input_port_default_value(0, false);
set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0));
set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
simple_decl = false;
}
////////////// Switch(scalar)
@ -3941,18 +4002,22 @@ void VisualShaderNodeCompare::set_comparison_type(ComparisonType p_type) {
case CTYPE_SCALAR:
set_input_port_default_value(0, 0.0);
set_input_port_default_value(1, 0.0);
simple_decl = true;
break;
case CTYPE_VECTOR:
set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
simple_decl = false;
break;
case CTYPE_BOOLEAN:
set_input_port_default_value(0, false);
set_input_port_default_value(1, false);
simple_decl = true;
break;
case CTYPE_TRANSFORM:
set_input_port_default_value(0, Transform());
set_input_port_default_value(1, Transform());
simple_decl = true;
break;
}
emit_changed();