Path: blob/master/servers/rendering/dummy/storage/material_storage.cpp
10279 views
/**************************************************************************/1/* material_storage.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "material_storage.h"3132#include "core/config/project_settings.h"3334using namespace RendererDummy;3536MaterialStorage *MaterialStorage::singleton = nullptr;3738MaterialStorage::MaterialStorage() {39singleton = this;40ShaderCompiler::DefaultIdentifierActions actions;41dummy_compiler.initialize(actions);42}4344MaterialStorage::~MaterialStorage() {45singleton = nullptr;46global_shader_variables.clear();47}4849void MaterialStorage::global_shader_parameter_add(const StringName &p_name, RS::GlobalShaderParameterType p_type, const Variant &p_value) {50ERR_FAIL_COND(global_shader_variables.has(p_name));5152global_shader_variables[p_name] = p_type;53}5455void MaterialStorage::global_shader_parameter_remove(const StringName &p_name) {56if (!global_shader_variables.has(p_name)) {57return;58}5960global_shader_variables.erase(p_name);61}6263Vector<StringName> MaterialStorage::global_shader_parameter_get_list() const {64Vector<StringName> names;65for (const KeyValue<StringName, RS::GlobalShaderParameterType> &E : global_shader_variables) {66names.push_back(E.key);67}68names.sort_custom<StringName::AlphCompare>();69return names;70}7172RS::GlobalShaderParameterType MaterialStorage::global_shader_parameter_get_type(const StringName &p_name) const {73if (!global_shader_variables.has(p_name)) {74print_line("don't have name, sorry");75return RS::GLOBAL_VAR_TYPE_MAX;76}7778return global_shader_variables[p_name];79}8081void MaterialStorage::global_shader_parameters_load_settings(bool p_load_textures) {82List<PropertyInfo> settings;83ProjectSettings::get_singleton()->get_property_list(&settings);8485for (const PropertyInfo &E : settings) {86if (E.name.begins_with("shader_globals/")) {87StringName name = E.name.get_slicec('/', 1);88Dictionary d = GLOBAL_GET(E.name);8990ERR_CONTINUE(!d.has("type"));91ERR_CONTINUE(!d.has("value"));9293String type = d["type"];9495static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {96"bool",97"bvec2",98"bvec3",99"bvec4",100"int",101"ivec2",102"ivec3",103"ivec4",104"rect2i",105"uint",106"uvec2",107"uvec3",108"uvec4",109"float",110"vec2",111"vec3",112"vec4",113"color",114"rect2",115"mat2",116"mat3",117"mat4",118"transform_2d",119"transform",120"sampler2D",121"sampler2DArray",122"sampler3D",123"samplerCube",124"samplerExternalOES"125};126127RS::GlobalShaderParameterType gvtype = RS::GLOBAL_VAR_TYPE_MAX;128129for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {130if (global_var_type_names[i] == type) {131gvtype = RS::GlobalShaderParameterType(i);132break;133}134}135136ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid137138if (!global_shader_variables.has(name)) {139global_shader_parameter_add(name, gvtype, Variant());140}141}142}143}144145RID MaterialStorage::shader_allocate() {146return shader_owner.allocate_rid();147}148149void MaterialStorage::shader_initialize(RID p_rid, bool p_embedded) {150shader_owner.initialize_rid(p_rid, DummyShader());151}152153void MaterialStorage::shader_free(RID p_rid) {154DummyShader *shader = shader_owner.get_or_null(p_rid);155ERR_FAIL_NULL(shader);156157shader_owner.free(p_rid);158}159160void MaterialStorage::shader_set_code(RID p_shader, const String &p_code) {161DummyShader *shader = shader_owner.get_or_null(p_shader);162ERR_FAIL_NULL(shader);163if (p_code.is_empty()) {164return;165}166167String mode_string = ShaderLanguage::get_shader_type(p_code);168169RS::ShaderMode new_mode;170if (mode_string == "canvas_item") {171new_mode = RS::SHADER_CANVAS_ITEM;172} else if (mode_string == "particles") {173new_mode = RS::SHADER_PARTICLES;174} else if (mode_string == "spatial") {175new_mode = RS::SHADER_SPATIAL;176} else if (mode_string == "sky") {177new_mode = RS::SHADER_SKY;178} else if (mode_string == "fog") {179new_mode = RS::SHADER_FOG;180} else {181new_mode = RS::SHADER_MAX;182ERR_FAIL_MSG("Shader type " + mode_string + " not supported in Dummy renderer.");183}184ShaderCompiler::IdentifierActions actions;185actions.uniforms = &shader->uniforms;186ShaderCompiler::GeneratedCode gen_code;187188Error err = MaterialStorage::get_singleton()->dummy_compiler.compile(new_mode, p_code, &actions, "", gen_code);189ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");190}191192void MaterialStorage::get_shader_parameter_list(RID p_shader, List<PropertyInfo> *p_param_list) const {193DummyShader *shader = shader_owner.get_or_null(p_shader);194ERR_FAIL_NULL(shader);195196SortArray<Pair<StringName, int>, ShaderLanguage::UniformOrderComparator> sorter;197LocalVector<Pair<StringName, int>> filtered_uniforms;198199for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {200if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_LOCAL) {201continue;202}203filtered_uniforms.push_back(Pair<StringName, int>(E.key, E.value.prop_order));204}205int uniform_count = filtered_uniforms.size();206sorter.sort(filtered_uniforms.ptr(), uniform_count);207208String last_group;209for (int i = 0; i < uniform_count; i++) {210const StringName &uniform_name = filtered_uniforms[i].first;211const ShaderLanguage::ShaderNode::Uniform &uniform = shader->uniforms[uniform_name];212213String group = uniform.group;214if (!uniform.subgroup.is_empty()) {215group += "::" + uniform.subgroup;216}217218if (group != last_group) {219PropertyInfo pi;220pi.usage = PROPERTY_USAGE_GROUP;221pi.name = group;222p_param_list->push_back(pi);223224last_group = group;225}226227PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniform);228pi.name = uniform_name;229p_param_list->push_back(pi);230}231}232233RID MaterialStorage::material_allocate() {234return material_owner.allocate_rid();235}236237void MaterialStorage::material_initialize(RID p_rid) {238material_owner.initialize_rid(p_rid, DummyMaterial());239}240241void MaterialStorage::material_free(RID p_rid) {242DummyMaterial *material = material_owner.get_or_null(p_rid);243ERR_FAIL_NULL(material);244245material_owner.free(p_rid);246}247248void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {249DummyMaterial *material = material_owner.get_or_null(p_material);250ERR_FAIL_NULL(material);251252material->shader = p_shader;253}254255void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {256DummyMaterial *material = material_owner.get_or_null(p_material);257ERR_FAIL_NULL(material);258259material->next_pass = p_next_material;260}261262void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {263DummyMaterial *material = material_owner.get_or_null(p_material);264ERR_FAIL_NULL(material);265DummyShader *shader = shader_owner.get_or_null(material->shader);266267if (shader) {268for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {269if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {270continue;271}272273RendererMaterialStorage::InstanceShaderParam p;274p.info = ShaderLanguage::uniform_to_property_info(E.value);275p.info.name = E.key; //supply name276p.index = E.value.instance_index;277p.default_value = ShaderLanguage::constant_value_to_variant(E.value.default_value, E.value.type, E.value.array_size, E.value.hint);278r_parameters->push_back(p);279}280}281if (material->next_pass.is_valid()) {282material_get_instance_shader_parameters(material->next_pass, r_parameters);283}284}285286287