Skip to content

Commit

Permalink
Implement 2D instance shader parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
huwpascoe committed Sep 25, 2024
1 parent 705ba68 commit 8687e49
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 283 deletions.
18 changes: 18 additions & 0 deletions doc/classes/CanvasItem.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,13 @@
Returns the transform from the local coordinate system of this [CanvasItem] to the [Viewport]s coordinate system.
</description>
</method>
<method name="get_instance_shader_parameter" qualifiers="const">
<return type="Variant" />
<param index="0" name="name" type="StringName" />
<description>
Get the value of a shader parameter as set on this instance.
</description>
</method>
<method name="get_local_mouse_position" qualifiers="const">
<return type="Vector2" />
<description>
Expand Down Expand Up @@ -558,6 +565,17 @@
Queues the [CanvasItem] to redraw. During idle time, if [CanvasItem] is visible, [constant NOTIFICATION_DRAW] is sent and [method _draw] is called. This only occurs [b]once[/b] per frame, even if this method has been called multiple times.
</description>
</method>
<method name="set_instance_shader_parameter">
<return type="void" />
<param index="0" name="name" type="StringName" />
<param index="1" name="value" type="Variant" />
<description>
Set the value of a shader uniform for this instance only ([url=$DOCS_URL/tutorials/shaders/shader_reference/shading_language.html#per-instance-uniforms]per-instance uniform[/url]). See also [method ShaderMaterial.set_shader_parameter] to assign a uniform on all instances using the same [ShaderMaterial].
[b]Note:[/b] For a shader uniform to be assignable on a per-instance basis, it [i]must[/i] be defined with [code]instance uniform ...[/code] rather than [code]uniform ...[/code] in the shader code.
[b]Note:[/b] [param name] is case-sensitive and must match the name of the uniform in the code exactly (not the capitalized name in the inspector).
[b]Note:[/b] Per-instance shader uniforms are currently only available for [code]canvas_item[/code] and [code]spatial[/code] shaders.
</description>
</method>
<method name="set_notify_local_transform">
<return type="void" />
<param index="0" name="enable" type="bool" />
Expand Down
33 changes: 33 additions & 0 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,30 @@
[b]Note:[/b] The equivalent node is [CanvasItem].
</description>
</method>
<method name="canvas_item_get_instance_shader_parameter" qualifiers="const">
<return type="Variant" />
<param index="0" name="instance" type="RID" />
<param index="1" name="parameter" type="StringName" />
<description>
Returns the value of the per-instance shader uniform from the specified canvas item instance. Equivalent to [method CanvasItem.get_instance_shader_parameter].
[b]Note:[/b] Per-instance shader parameter names are case-sensitive.
</description>
</method>
<method name="canvas_item_get_instance_shader_parameter_default_value" qualifiers="const">
<return type="Variant" />
<param index="0" name="instance" type="RID" />
<param index="1" name="parameter" type="StringName" />
<description>
Returns the default value of the per-instance shader uniform from the specified canvas item instance. Equivalent to [method CanvasItem.get_instance_shader_parameter].
</description>
</method>
<method name="canvas_item_get_instance_shader_parameter_list" qualifiers="const">
<return type="Dictionary[]" />
<param index="0" name="instance" type="RID" />
<description>
Returns a dictionary of per-instance shader uniform names of the per-instance shader uniform from the specified canvas item instance. The returned dictionary is in PropertyInfo format, with the keys [code]name[/code], [code]class_name[/code], [code]type[/code], [code]hint[/code], [code]hint_string[/code], and [code]usage[/code]. Equivalent to [method CanvasItem.get_instance_shader_parameter].
</description>
</method>
<method name="canvas_item_reset_physics_interpolation">
<return type="void" />
<param index="0" name="item" type="RID" />
Expand Down Expand Up @@ -524,6 +548,15 @@
Sets the index for the [CanvasItem].
</description>
</method>
<method name="canvas_item_set_instance_shader_parameter">
<return type="void" />
<param index="0" name="instance" type="RID" />
<param index="1" name="parameter" type="StringName" />
<param index="2" name="value" type="Variant" />
<description>
Sets the per-instance shader uniform on the specified canvas item instance. Equivalent to [method CanvasItem.set_instance_shader_parameter].
</description>
</method>
<method name="canvas_item_set_interpolated">
<return type="void" />
<param index="0" name="item" type="RID" />
Expand Down
19 changes: 11 additions & 8 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ int CanvasItem::get_light_mask() const {
return light_mask;
}

const StringName *CanvasItem::_instance_shader_parameter_get_remap(const StringName p_name) const {
const StringName *CanvasItem::_instance_shader_parameter_get_remap(const StringName &p_name) const {
StringName *r = instance_shader_parameter_property_remap.getptr(p_name);
if (!r) {
String s = p_name;
Expand Down Expand Up @@ -585,10 +585,10 @@ bool CanvasItem::_get(const StringName &p_name, Variant &r_ret) const {

void CanvasItem::_get_property_list(List<PropertyInfo> *p_list) const {
List<PropertyInfo> pinfo;
RS::get_singleton()->instance_item_get_shader_parameter_list(get_canvas_item(), &pinfo);
RS::get_singleton()->canvas_item_get_instance_shader_parameter_list(get_canvas_item(), &pinfo);
for (PropertyInfo &pi : pinfo) {
bool has_def_value = false;
Variant def_value = RS::get_singleton()->instance_item_get_shader_parameter_default_value(get_canvas_item(), pi.name);
Variant def_value = RS::get_singleton()->canvas_item_get_instance_shader_parameter_default_value(get_canvas_item(), pi.name);
if (def_value.get_type() != Variant::NIL) {
has_def_value = true;
}
Expand Down Expand Up @@ -1135,22 +1135,22 @@ void CanvasItem::set_use_parent_material(bool p_use_parent_material) {

void CanvasItem::set_instance_shader_parameter(const StringName &p_name, const Variant &p_value) {
if (p_value.get_type() == Variant::NIL) {
Variant def_value = RS::get_singleton()->instance_item_get_shader_parameter_default_value(get_canvas_item(), p_name);
RS::get_singleton()->instance_item_set_shader_parameter(get_canvas_item(), p_name, def_value);
Variant def_value = RS::get_singleton()->canvas_item_get_instance_shader_parameter_default_value(get_canvas_item(), p_name);
RS::get_singleton()->canvas_item_set_instance_shader_parameter(get_canvas_item(), p_name, def_value);
instance_shader_parameters.erase(p_value);
} else {
instance_shader_parameters[p_name] = p_value;
if (p_value.get_type() == Variant::OBJECT) {
RID tex_id = p_value;
RS::get_singleton()->instance_item_set_shader_parameter(get_canvas_item(), p_name, tex_id);
RS::get_singleton()->canvas_item_set_instance_shader_parameter(get_canvas_item(), p_name, tex_id);
} else {
RS::get_singleton()->instance_item_set_shader_parameter(get_canvas_item(), p_name, p_value);
RS::get_singleton()->canvas_item_set_instance_shader_parameter(get_canvas_item(), p_name, p_value);
}
}
}

Variant CanvasItem::get_instance_shader_parameter(const StringName &p_name) const {
return RS::get_singleton()->instance_item_get_shader_parameter(get_canvas_item(), p_name);
return RS::get_singleton()->canvas_item_get_instance_shader_parameter(get_canvas_item(), p_name);
}

bool CanvasItem::get_use_parent_material() const {
Expand Down Expand Up @@ -1315,6 +1315,9 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_material", "material"), &CanvasItem::set_material);
ClassDB::bind_method(D_METHOD("get_material"), &CanvasItem::get_material);

ClassDB::bind_method(D_METHOD("set_instance_shader_parameter", "name", "value"), &CanvasItem::set_instance_shader_parameter);
ClassDB::bind_method(D_METHOD("get_instance_shader_parameter", "name"), &CanvasItem::get_instance_shader_parameter);

ClassDB::bind_method(D_METHOD("set_use_parent_material", "enable"), &CanvasItem::set_use_parent_material);
ClassDB::bind_method(D_METHOD("get_use_parent_material"), &CanvasItem::get_use_parent_material);

Expand Down
2 changes: 1 addition & 1 deletion scene/main/canvas_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class CanvasItem : public Node {
void _update_texture_filter_changed(bool p_propagate);

void _notify_transform_deferred();
const StringName *_instance_shader_parameter_get_remap(const StringName p_name) const;
const StringName *_instance_shader_parameter_get_remap(const StringName &p_name) const;

protected:
bool _set(const StringName &p_name, const Variant &p_value);
Expand Down
185 changes: 185 additions & 0 deletions servers/rendering/instance_uniforms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/**************************************************************************/
/* instance_uniforms.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. */
/**************************************************************************/

#include "instance_uniforms.h"

#include "rendering_server_globals.h"

void InstanceUniforms::free(RID p_self) {
ERR_FAIL_COND(!p_self.is_valid());

if (is_allocated()) {
RSG::material_storage->global_shader_parameters_instance_free(p_self);
_location = -1;
}

_invalidate_items();
}

void InstanceUniforms::materials_start() {
_invalidate_items();
}

void InstanceUniforms::materials_append(RID p_material) {
ERR_FAIL_COND(!p_material.is_valid());

List<RendererMaterialStorage::InstanceShaderParam> params;
RSG::material_storage->material_get_instance_shader_parameters(p_material, &params);

for (RendererMaterialStorage::InstanceShaderParam const &srcp : params) {
StringName name = srcp.info.name;
if (Item *ptr = _parameters.getptr(name); ptr) {
if (!ptr->is_valid()) {
_init_param(*ptr, srcp);
} else if (ptr->index != srcp.index) {
WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +
"', but they do it with different indices. Only the first one (in order) will display correctly.");
} else if (ptr->info.type != srcp.info.type) {
WARN_PRINT("More than one material in instance export the same instance shader uniform '" + srcp.info.name +
"', but they do it with different data types. Only the first one (in order) will display correctly.");
}
} else {
Item i;
_init_param(i, srcp);
_parameters[name] = i;
}
}
}

bool InstanceUniforms::materials_finish(RID p_self) {
ERR_FAIL_COND_V(!p_self.is_valid(), false);

if (_parameters.is_empty()) {
if (is_allocated()) {
free(p_self);
}
return true;
}

bool const should_alloc = !is_allocated();

if (should_alloc) {
_location = RSG::material_storage->global_shader_parameters_instance_allocate(p_self);
}

for (KeyValue<StringName, Item> &kv : _parameters) {
Item &i = kv.value;
if (i.is_valid()) {
RSG::material_storage->global_shader_parameters_instance_update(p_self, i.index, i.value, i.flags);
}
}

return should_alloc;
}

Variant InstanceUniforms::get(StringName const &p_name) const {
if (Item const *ptr = _parameters.getptr(p_name); ptr) {
return ptr->value;
}
return Variant();
}

void InstanceUniforms::set(RID p_self, StringName const &p_name, Variant const &p_value) {
ERR_FAIL_COND(!p_self.is_valid());
ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);

if (Item *ptr = _parameters.getptr(p_name); ptr) {
ptr->value = p_value;
if (ptr->is_valid()) {
RSG::material_storage->global_shader_parameters_instance_update(p_self, ptr->index, ptr->value, ptr->flags);
}
} else {
Item i; // Initialize in materials_finish.
i.value = p_value;
_parameters[p_name] = i;
}
}

Variant InstanceUniforms::get_default(StringName const &p_name) const {
if (Item const *ptr = _parameters.getptr(p_name); ptr) {
return ptr->default_value;
}
return Variant();
}

void InstanceUniforms::get_property_list(List<PropertyInfo> &r_parameters) const {
Vector<StringName> names;

// Invalid items won't be saved, but will remain in memory incase of shader compliation failure.
for (KeyValue<StringName, Item> const &kv : _parameters) {
if (kv.value.is_valid()) {
names.push_back(kv.key);
}
}

names.sort_custom<StringName::AlphCompare>();

for (StringName const &n : names) {
PropertyInfo pinfo = _parameters[n].info;
r_parameters.push_back(pinfo);
}
}

void InstanceUniforms::_init_param(Item &r_item, RendererMaterialStorage::InstanceShaderParam const &p_param) const {
r_item.index = p_param.index;
r_item.flags = 0;
r_item.info = p_param.info;
r_item.default_value = p_param.default_value;

if (r_item.default_value.get_type() == Variant::NIL) {
Callable::CallError cerr;
Variant::construct(r_item.info.type, r_item.default_value, nullptr, 0, cerr);
}

if (r_item.value.get_type() == Variant::NIL) {
r_item.value = r_item.default_value;
}

if (r_item.info.hint == PROPERTY_HINT_FLAGS) {
// HACK: Detect boolean flags count and prevent overhead.
switch (r_item.info.hint_string.length()) {
case 3: // "x,y"
r_item.flags = 1;
break;
case 5: // "x,y,z"
r_item.flags = 2;
break;
case 7: // "x,y,z,w"
r_item.flags = 3;
break;
}
}
}

void InstanceUniforms::_invalidate_items() {
for (KeyValue<StringName, Item> &kv : _parameters) {
kv.value.index = -1;
}
}
Loading

0 comments on commit 8687e49

Please sign in to comment.