Path: blob/master/servers/rendering/dummy/storage/material_storage.cpp
20959 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 if (mode_string == "texture_blit") {181new_mode = RS::SHADER_TEXTURE_BLIT;182} else {183new_mode = RS::SHADER_MAX;184ERR_FAIL_MSG("Shader type " + mode_string + " not supported in Dummy renderer.");185}186ShaderCompiler::IdentifierActions actions;187actions.uniforms = &shader->uniforms;188ShaderCompiler::GeneratedCode gen_code;189190Error err = MaterialStorage::get_singleton()->dummy_compiler.compile(new_mode, p_code, &actions, "", gen_code);191ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");192}193194void MaterialStorage::get_shader_parameter_list(RID p_shader, List<PropertyInfo> *p_param_list) const {195DummyShader *shader = shader_owner.get_or_null(p_shader);196ERR_FAIL_NULL(shader);197198SortArray<Pair<StringName, int>, ShaderLanguage::UniformOrderComparator> sorter;199LocalVector<Pair<StringName, int>> filtered_uniforms;200201for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {202if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_LOCAL) {203continue;204}205filtered_uniforms.push_back(Pair<StringName, int>(E.key, E.value.prop_order));206}207int uniform_count = filtered_uniforms.size();208sorter.sort(filtered_uniforms.ptr(), uniform_count);209210String last_group;211for (int i = 0; i < uniform_count; i++) {212const StringName &uniform_name = filtered_uniforms[i].first;213const ShaderLanguage::ShaderNode::Uniform &uniform = shader->uniforms[uniform_name];214215String group = uniform.group;216if (!uniform.subgroup.is_empty()) {217group += "::" + uniform.subgroup;218}219220if (group != last_group) {221PropertyInfo pi;222pi.usage = PROPERTY_USAGE_GROUP;223pi.name = group;224p_param_list->push_back(pi);225226last_group = group;227}228229PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniform);230pi.name = uniform_name;231p_param_list->push_back(pi);232}233}234235RID MaterialStorage::material_allocate() {236return material_owner.allocate_rid();237}238239void MaterialStorage::material_initialize(RID p_rid) {240material_owner.initialize_rid(p_rid, DummyMaterial());241}242243void MaterialStorage::material_free(RID p_rid) {244DummyMaterial *material = material_owner.get_or_null(p_rid);245ERR_FAIL_NULL(material);246247material_owner.free(p_rid);248}249250void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {251DummyMaterial *material = material_owner.get_or_null(p_material);252ERR_FAIL_NULL(material);253254material->shader = p_shader;255}256257void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {258DummyMaterial *material = material_owner.get_or_null(p_material);259ERR_FAIL_NULL(material);260261material->next_pass = p_next_material;262}263264void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {265DummyMaterial *material = material_owner.get_or_null(p_material);266ERR_FAIL_NULL(material);267DummyShader *shader = shader_owner.get_or_null(material->shader);268269if (shader) {270for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : shader->uniforms) {271if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {272continue;273}274275RendererMaterialStorage::InstanceShaderParam p;276p.info = ShaderLanguage::uniform_to_property_info(E.value);277p.info.name = E.key; //supply name278p.index = E.value.instance_index;279p.default_value = ShaderLanguage::constant_value_to_variant(E.value.default_value, E.value.type, E.value.array_size, E.value.hint);280r_parameters->push_back(p);281}282}283if (material->next_pass.is_valid()) {284material_get_instance_shader_parameters(material->next_pass, r_parameters);285}286}287288289