diff options
Diffstat (limited to 'scene/resources/shader.cpp')
-rw-r--r-- | scene/resources/shader.cpp | 76 |
1 files changed, 32 insertions, 44 deletions
diff --git a/scene/resources/shader.cpp b/scene/resources/shader.cpp index 57e2470164..4ca8032d65 100644 --- a/scene/resources/shader.cpp +++ b/scene/resources/shader.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,50 +31,47 @@ #include "shader.h" #include "core/os/file_access.h" #include "scene/scene_string_names.h" -#include "servers/visual/shader_language.h" -#include "servers/visual_server.h" +#include "servers/rendering/shader_language.h" +#include "servers/rendering_server.h" #include "texture.h" Shader::Mode Shader::get_mode() const { - return mode; } void Shader::set_code(const String &p_code) { - String type = ShaderLanguage::get_shader_type(p_code); if (type == "canvas_item") { mode = MODE_CANVAS_ITEM; } else if (type == "particles") { mode = MODE_PARTICLES; + } else if (type == "sky") { + mode = MODE_SKY; } else { mode = MODE_SPATIAL; } - VisualServer::get_singleton()->shader_set_code(shader, p_code); + RenderingServer::get_singleton()->shader_set_code(shader, p_code); params_cache_dirty = true; emit_changed(); } String Shader::get_code() const { - _update_shader(); - return VisualServer::get_singleton()->shader_get_code(shader); + return RenderingServer::get_singleton()->shader_get_code(shader); } void Shader::get_param_list(List<PropertyInfo> *p_params) const { - _update_shader(); List<PropertyInfo> local; - VisualServer::get_singleton()->shader_get_param_list(shader, &local); + RenderingServer::get_singleton()->shader_get_param_list(shader, &local); params_cache.clear(); params_cache_dirty = false; for (List<PropertyInfo>::Element *E = local.front(); E; E = E->next()) { - PropertyInfo pi = E->get(); if (default_textures.has(pi.name)) { //do not show default textures continue; @@ -82,47 +79,43 @@ void Shader::get_param_list(List<PropertyInfo> *p_params) const { pi.name = "shader_param/" + pi.name; params_cache[pi.name] = E->get().name; if (p_params) { - //small little hack - if (pi.type == Variant::_RID) + if (pi.type == Variant::_RID) { pi.type = Variant::OBJECT; + } p_params->push_back(pi); } } } RID Shader::get_rid() const { - _update_shader(); return shader; } -void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture> &p_texture) { - +void Shader::set_default_texture_param(const StringName &p_param, const Ref<Texture2D> &p_texture) { if (p_texture.is_valid()) { default_textures[p_param] = p_texture; - VS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid()); + RS::get_singleton()->shader_set_default_texture_param(shader, p_param, p_texture->get_rid()); } else { default_textures.erase(p_param); - VS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID()); + RS::get_singleton()->shader_set_default_texture_param(shader, p_param, RID()); } emit_changed(); } -Ref<Texture> Shader::get_default_texture_param(const StringName &p_param) const { - - if (default_textures.has(p_param)) +Ref<Texture2D> Shader::get_default_texture_param(const StringName &p_param) const { + if (default_textures.has(p_param)) { return default_textures[p_param]; - else - return Ref<Texture>(); + } else { + return Ref<Texture2D>(); + } } void Shader::get_default_texture_param_list(List<StringName> *r_textures) const { - - for (const Map<StringName, Ref<Texture> >::Element *E = default_textures.front(); E; E = E->next()) { - + for (const Map<StringName, Ref<Texture2D>>::Element *E = default_textures.front(); E; E = E->next()) { r_textures->push_back(E->key()); } } @@ -132,7 +125,6 @@ bool Shader::is_text_shader() const { } bool Shader::has_param(const StringName &p_param) const { - return params_cache.has(p_param); } @@ -140,7 +132,6 @@ void Shader::_update_shader() const { } void Shader::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_mode"), &Shader::get_mode); ClassDB::bind_method(D_METHOD("set_code", "code"), &Shader::set_code); @@ -158,25 +149,25 @@ void Shader::_bind_methods() { BIND_ENUM_CONSTANT(MODE_SPATIAL); BIND_ENUM_CONSTANT(MODE_CANVAS_ITEM); BIND_ENUM_CONSTANT(MODE_PARTICLES); + BIND_ENUM_CONSTANT(MODE_SKY); } Shader::Shader() { - mode = MODE_SPATIAL; - shader = VisualServer::get_singleton()->shader_create(); + shader = RenderingServer::get_singleton()->shader_create(); params_cache_dirty = true; } Shader::~Shader() { - - VisualServer::get_singleton()->free(shader); + RenderingServer::get_singleton()->free(shader); } -//////////// -RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error) { +//////////// - if (r_error) +RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { + if (r_error) { *r_error = ERR_FILE_CANT_OPEN; + } Ref<Shader> shader; shader.instance(); @@ -188,32 +179,30 @@ RES ResourceFormatLoaderShader::load(const String &p_path, const String &p_origi shader->set_code(str); - if (r_error) + if (r_error) { *r_error = OK; + } return shader; } void ResourceFormatLoaderShader::get_recognized_extensions(List<String> *p_extensions) const { - p_extensions->push_back("shader"); } bool ResourceFormatLoaderShader::handles_type(const String &p_type) const { - return (p_type == "Shader"); } String ResourceFormatLoaderShader::get_resource_type(const String &p_path) const { - String el = p_path.get_extension().to_lower(); - if (el == "shader") + if (el == "shader") { return "Shader"; + } return ""; } Error ResourceFormatSaverShader::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - Ref<Shader> shader = p_resource; ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); @@ -236,14 +225,13 @@ Error ResourceFormatSaverShader::save(const String &p_path, const RES &p_resourc } void ResourceFormatSaverShader::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { - if (const Shader *shader = Object::cast_to<Shader>(*p_resource)) { if (shader->is_text_shader()) { p_extensions->push_back("shader"); } } } -bool ResourceFormatSaverShader::recognize(const RES &p_resource) const { +bool ResourceFormatSaverShader::recognize(const RES &p_resource) const { return p_resource->get_class_name() == "Shader"; //only shader, not inherited } |