From: Mikko Rasa Date: Thu, 12 Aug 2021 09:35:16 +0000 (+0300) Subject: Remove default sampler from Texture X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=dbc91b65728ab9c0e574bb1127cfe4d2da55de7f;p=libs%2Fgl.git Remove default sampler from Texture It was a bad idea and would not translate well to Vulkan. --- diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 2d85b10f..8a87011a 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -90,11 +90,10 @@ class MaterialExporter: if st: mat_res.statements.append(st) textures = [p.texture for p in material.properties if p.texture] - if textures and not all(t.default_filter for t in textures): + if textures: from .export_texture import SamplerExporter - sampler_tex = next(t for t in textures if not t.default_filter) sampler_export = SamplerExporter() - mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(sampler_tex)])) + mat_res.statements.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(textures[0])])) return mat_res @@ -103,11 +102,7 @@ class MaterialExporter: if prop.texture: tex_res = resources[prop.texture.image.name+".tex2d"] from .util import basename - fn = basename(prop.texture.image.filepath) - if prop.texture.default_filter and fn: - return Statement(prop.tex_keyword, fn) - else: - return mat_res.create_reference_statement(prop.tex_keyword, tex_res) + return mat_res.create_reference_statement(prop.tex_keyword, tex_res) elif not prop.keyword: return elif type(prop.value)==tuple: diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index 7299e636..1d6a06a0 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -140,7 +140,7 @@ class Material: sampler_settings = None for p in self.properties: if p.texture: - settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy) + settings = (p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy) if sampler_settings is None: sampler_settings = settings elif settings!=sampler_settings: diff --git a/blender/io_mspgl/properties.py b/blender/io_mspgl/properties.py index 236e79f3..744630f7 100644 --- a/blender/io_mspgl/properties.py +++ b/blender/io_mspgl/properties.py @@ -131,10 +131,8 @@ class MspGLTextureNodeProperties(bpy.types.Panel): if not node: return - self.layout.prop(node, "default_filter") - if not node.default_filter: - self.layout.prop(node, "use_mipmap") - self.layout.prop(node, "max_anisotropy") + self.layout.prop(node, "use_mipmap") + self.layout.prop(node, "max_anisotropy") class MspGLLightProperties(bpy.types.Panel): bl_idname = "LIGHT_PT_mspgl_properties" @@ -239,7 +237,6 @@ def register_properties(): bpy.types.Material.uniforms = bpy.props.CollectionProperty(type=MspGLUniform, name="Uniform", description="Uniform variables to add to the technique") bpy.types.Material.active_uniform_index = bpy.props.IntProperty("Active uniform index") - bpy.types.ShaderNodeTexImage.default_filter = bpy.props.BoolProperty(name="Default filter", description="Let the loading program determine filtering options") bpy.types.ShaderNodeTexImage.use_mipmap = bpy.props.BoolProperty(name="Use mipmaps", description="Use mipmaps (automatically generated) for the texture", default=True) bpy.types.ShaderNodeTexImage.max_anisotropy = bpy.props.FloatProperty(name="Maximum anisotropy", description="Maximum anisotropy to use in texture filtering", min=1, max=16, default=1) diff --git a/source/core/sampler.cpp b/source/core/sampler.cpp index 93b3b5c9..bb37b074 100644 --- a/source/core/sampler.cpp +++ b/source/core/sampler.cpp @@ -16,10 +16,17 @@ namespace Msp { namespace GL { Sampler::Sampler(): - owner(0) + min_filter(NEAREST_MIPMAP_LINEAR), + mag_filter(LINEAR), + max_anisotropy(1.0f), + wrap_s(REPEAT), + wrap_t(REPEAT), + wrap_r(REPEAT), + border_color(Color(0.0f, 0.0f, 0.0f, 0.0f)), + compare(false), + cmp_func(LEQUAL), + dirty_params(0) { - init(); - Require _req(ARB_sampler_objects); if(ARB_direct_state_access) glCreateSamplers(1, &id); @@ -27,105 +34,30 @@ Sampler::Sampler(): glGenSamplers(1, &id); } -Sampler::Sampler(const Texture &tex): - id(0), - owner(&tex) -{ - if(this!=&tex.get_default_sampler()) - throw invalid_argument("Sampler::Sampler"); - - init(); -} - -void Sampler::init() -{ - min_filter = NEAREST_MIPMAP_LINEAR; - mag_filter = LINEAR; - max_anisotropy = 1.0f; - wrap_s = REPEAT; - wrap_t = REPEAT; - wrap_r = REPEAT; - border_color = Color(0.0f, 0.0f, 0.0f, 0.0f); - compare = false; - cmp_func = LEQUAL; - dirty_params = 0; -} - void Sampler::update_parameter(int mask) const { - if(owner) - { - if(!owner->get_id()) - { - dirty_params |= mask; - return; - } - - if(!ARB_direct_state_access && TexUnit::current().get_texture()!=owner) - { - TexUnit *unit = TexUnit::find_unit(owner); - if(!unit) - { - dirty_params |= mask; - return; - } - - unit->bind(); - } - } - if(mask&MIN_FILTER) - set_parameter_i(GL_TEXTURE_MIN_FILTER, min_filter); + glSamplerParameteri(id, GL_TEXTURE_MIN_FILTER, min_filter); if(mask&MAG_FILTER) - set_parameter_i(GL_TEXTURE_MAG_FILTER, mag_filter); + glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, mag_filter); if(mask&MAX_ANISOTROPY) - set_parameter_f(GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy); + glSamplerParameterf(id, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_anisotropy); if(mask&WRAP_S) - set_parameter_i(GL_TEXTURE_WRAP_S, wrap_s); + glSamplerParameteri(id, GL_TEXTURE_WRAP_S, wrap_s); if(mask&WRAP_T) - set_parameter_i(GL_TEXTURE_WRAP_T, wrap_t); + glSamplerParameteri(id, GL_TEXTURE_WRAP_T, wrap_t); if(mask&WRAP_R) - set_parameter_i(GL_TEXTURE_WRAP_R, wrap_r); + glSamplerParameteri(id, GL_TEXTURE_WRAP_R, wrap_r); if(mask&BORDER_COLOR) - set_parameter_fv(GL_TEXTURE_BORDER_COLOR, &border_color.r); + glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, &border_color.r); if(mask&COMPARE) { - set_parameter_i(GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); + glSamplerParameteri(id, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); if(compare) - set_parameter_i(GL_TEXTURE_COMPARE_FUNC, cmp_func); + glSamplerParameteri(id, GL_TEXTURE_COMPARE_FUNC, cmp_func); } } -void Sampler::set_parameter_i(unsigned param, int value) const -{ - if(id) - glSamplerParameteri(id, param, value); - else if(ARB_direct_state_access) - glTextureParameteri(owner->get_id(), param, value); - else - glTexParameteri(owner->get_target(), param, value); -} - -void Sampler::set_parameter_f(unsigned param, float value) const -{ - if(id) - glSamplerParameterf(id, param, value); - else if(ARB_direct_state_access) - glTextureParameterf(owner->get_id(), param, value); - else - glTexParameterf(owner->get_target(), param, value); -} - -void Sampler::set_parameter_fv(unsigned param, const float *value) const -{ - if(id) - glSamplerParameterfv(id, param, value); - else if(ARB_direct_state_access) - glTextureParameterfv(owner->get_id(), param, value); - else - glTexParameterfv(owner->get_target(), param, value); -} - void Sampler::set_min_filter(TextureFilter f) { min_filter = f; @@ -199,20 +131,16 @@ void Sampler::set_compare(Predicate f) static Require _req(ARB_shadow); compare = true; cmp_func = f; - update_parameter(COMPARE); + dirty_params |= COMPARE; } void Sampler::bind_to(unsigned i) const { TexUnit &unit = TexUnit::get_unit(i); - if(owner && owner!=unit.get_texture()) - throw invalid_operation("Sampler::bind_to"); - const Sampler *cur = unit.get_sampler(); if(unit.set_sampler(this)) { - if(!owner || (cur && cur->id)) - glBindSampler(i, id); + glBindSampler(i, id); if(dirty_params) { @@ -230,32 +158,10 @@ const Sampler *Sampler::current(unsigned i) void Sampler::unbind_from(unsigned i) { TexUnit &unit = TexUnit::get_unit(i); - const Sampler *cur = unit.get_sampler(); - if(unit.set_sampler(0) && cur->id) + if(unit.set_sampler(0)) glBindSampler(i, 0); } -void Sampler::unload() -{ - if(owner && !owner->get_id()) - { - if(min_filter!=NEAREST_MIPMAP_LINEAR) - dirty_params |= MIN_FILTER; - if(mag_filter!=LINEAR) - dirty_params |= MAG_FILTER; - if(max_anisotropy!=1.0f) - dirty_params |= MAX_ANISOTROPY; - if(wrap_s!=REPEAT) - dirty_params |= WRAP_S; - if(wrap_t!=REPEAT) - dirty_params |= WRAP_T; - if(wrap_r!=REPEAT) - dirty_params |= WRAP_R; - if(compare || cmp_func!=LEQUAL) - dirty_params |= COMPARE; - } -} - void Sampler::set_debug_name(const string &name) { #ifdef DEBUG diff --git a/source/core/sampler.h b/source/core/sampler.h index f68f1a16..bb02269f 100644 --- a/source/core/sampler.h +++ b/source/core/sampler.h @@ -99,7 +99,6 @@ private: }; unsigned id; - const Texture *owner; TextureFilter min_filter; TextureFilter mag_filter; float max_anisotropy; @@ -113,10 +112,8 @@ private: public: Sampler(); - Sampler(const Texture &); -private: - void init(); +private: void update_parameter(int) const; void set_parameter_i(unsigned, int) const; void set_parameter_f(unsigned, float) const; @@ -165,8 +162,6 @@ public: static void unbind() { unbind_from(0); } static void unbind_from(unsigned); - void unload(); - void set_debug_name(const std::string &); }; diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 967a7fe3..ea503ed1 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -30,8 +30,7 @@ Texture::Texture(GLenum t, ResourceManager *m): storage_fmt(RGB8), swizzle(NO_SWIZZLE), use_srgb_format(false), - auto_gen_mipmap(false), - default_sampler(*this) + auto_gen_mipmap(false) { if(m) set_manager(m); @@ -128,46 +127,6 @@ void Texture::set_parameter_i(GLenum param, int value) const glTexParameteri(target, param, value); } -void Texture::set_min_filter(TextureFilter f) -{ - default_sampler.set_min_filter(f); -} - -void Texture::set_mag_filter(TextureFilter f) -{ - default_sampler.set_mag_filter(f); -} - -void Texture::set_filter(TextureFilter f) -{ - default_sampler.set_filter(f); -} - -void Texture::set_max_anisotropy(float a) -{ - default_sampler.set_max_anisotropy(a); -} - -void Texture::set_wrap(TextureWrap w) -{ - default_sampler.set_wrap(w); -} - -void Texture::set_wrap_s(TextureWrap w) -{ - default_sampler.set_wrap_s(w); -} - -void Texture::set_wrap_t(TextureWrap w) -{ - default_sampler.set_wrap_t(w); -} - -void Texture::set_wrap_r(TextureWrap w) -{ - default_sampler.set_wrap_r(w); -} - bool Texture::can_generate_mipmap() { return EXT_framebuffer_object; @@ -195,19 +154,6 @@ void Texture::set_auto_generate_mipmap(bool gm) auto_gen_mipmap = gm; } -void Texture::set_compare_enabled(bool c) -{ - if(c) - default_sampler.set_compare(default_sampler.get_compare_function()); - else - default_sampler.disable_compare(); -} - -void Texture::set_compare_func(Predicate f) -{ - default_sampler.set_compare(f); -} - void Texture::load_image(const string &fn, bool) { load_image(fn, 0U); @@ -240,7 +186,6 @@ void Texture::bind_to(unsigned i) const } TexUnit &unit = TexUnit::get_unit(i); - const Texture *cur = unit.get_texture(); if(unit.set_texture(this)) { if(manager) @@ -253,9 +198,6 @@ void Texture::bind_to(unsigned i) const unit.bind(); glBindTexture(target, id); } - - if(!unit.get_sampler() || unit.get_sampler()==&cur->default_sampler) - default_sampler.bind_to(i); } } @@ -277,9 +219,6 @@ void Texture::unbind_from(unsigned i) unit.bind(); glBindTexture(cur->target, 0); } - - if(unit.get_sampler()==&cur->default_sampler) - Sampler::unbind_from(i); } } @@ -313,27 +252,11 @@ void Texture::Loader::init() add("external_image", &Loader::external_image); add("external_image_srgb", &Loader::external_image_srgb); - add("filter", &Loader::filter); add("generate_mipmap", &Loader::generate_mipmap); add("image_data", &Loader::image_data); - add("mag_filter", &Loader::mag_filter); - add("max_anisotropy", &Loader::max_anisotropy); - add("min_filter", &Loader::min_filter); add("mipmap_levels", &Loader::mipmap_levels); - add("sampler", &Loader::sampler); - add("wrap", &Loader::wrap); - add("wrap_r", &Loader::wrap_r); - add("wrap_s", &Loader::wrap_s); - add("wrap_t", &Loader::wrap_t); -} - -unsigned Texture::Loader::get_levels() const -{ - return (is_mipmapped(obj.default_sampler.get_min_filter()) ? levels : 1); } -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn) { RefPtr io = get_collection().open_raw(fn); @@ -362,15 +285,10 @@ void Texture::Loader::external_image_common(const string &fn) { Graphics::Image img; load_external_image(img, fn); - obj.image(img, get_levels()); + obj.image(img, levels); } } -void Texture::Loader::filter(TextureFilter f) -{ - obj.set_filter(f); -} - void Texture::Loader::generate_mipmap(bool gm) { obj.set_auto_generate_mipmap(gm); @@ -389,22 +307,7 @@ void Texture::Loader::image_data(const string &data) IO::Memory mem(data.data(), data.size()); img.load_io(mem); - obj.image(img, get_levels()); -} - -void Texture::Loader::mag_filter(TextureFilter f) -{ - obj.set_mag_filter(f); -} - -void Texture::Loader::max_anisotropy(float a) -{ - obj.set_max_anisotropy(a); -} - -void Texture::Loader::min_filter(TextureFilter f) -{ - obj.set_min_filter(f); + obj.image(img, levels); } void Texture::Loader::mipmap_levels(unsigned l) @@ -412,31 +315,5 @@ void Texture::Loader::mipmap_levels(unsigned l) levels = l; } -void Texture::Loader::sampler() -{ - load_sub(obj.default_sampler); -} - -void Texture::Loader::wrap(TextureWrap w) -{ - obj.set_wrap(w); -} - -void Texture::Loader::wrap_r(TextureWrap w) -{ - obj.set_wrap_r(w); -} - -void Texture::Loader::wrap_s(TextureWrap w) -{ - obj.set_wrap_s(w); -} - -void Texture::Loader::wrap_t(TextureWrap w) -{ - obj.set_wrap_t(w); -} -#pragma GCC diagnostic pop - } // namespace GL } // namespace Msp diff --git a/source/core/texture.h b/source/core/texture.h index 4fa20c76..7fb41a62 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -37,7 +37,6 @@ protected: private: void init(); - unsigned get_levels() const; protected: void load_external_image(Graphics::Image &, const std::string &); @@ -45,18 +44,9 @@ protected: void external_image(const std::string &); void external_image_srgb(const std::string &); void external_image_common(const std::string &); - void filter(TextureFilter); void generate_mipmap(bool); void image_data(const std::string &); - void mag_filter(TextureFilter); - void max_anisotropy(float); - void min_filter(TextureFilter); void mipmap_levels(unsigned); - void sampler(); - void wrap(TextureWrap); - void wrap_r(TextureWrap); - void wrap_s(TextureWrap); - void wrap_t(TextureWrap); }; enum ParameterMask @@ -79,7 +69,6 @@ protected: FormatSwizzle swizzle; bool use_srgb_format; bool auto_gen_mipmap; - Sampler default_sampler; std::string debug_name; static int swizzle_orders[]; @@ -97,27 +86,6 @@ protected: void set_parameter_i(GLenum, int) const; public: - Sampler &get_default_sampler() { return default_sampler; } - const Sampler &get_default_sampler() const { return default_sampler; } - - DEPRECATED void set_min_filter(TextureFilter); - DEPRECATED void set_mag_filter(TextureFilter); - - /** Sets filter for both minification and magnification. Since mipmapping - is not applicable to magnification, LINEAR is used instead. */ - DEPRECATED void set_filter(TextureFilter); - - DEPRECATED void set_mipmap_levels(unsigned) { } - - DEPRECATED void set_max_anisotropy(float); - - /** Sets the wrapping mode for all coordinates. */ - DEPRECATED void set_wrap(TextureWrap); - - DEPRECATED void set_wrap_s(TextureWrap); - DEPRECATED void set_wrap_t(TextureWrap); - DEPRECATED void set_wrap_r(TextureWrap); - static bool can_generate_mipmap(); void generate_mipmap(); @@ -129,15 +97,6 @@ public: /// Deprecated. Use set_auto_generate_mipmap instead. DEPRECATED void set_generate_mipmap(bool g) { set_auto_generate_mipmap(g); } - /** Sets depth texture comparison. Has no effect on other formats. When - comparison is enabled, the third component of the texture coordinate is - compared against the texel value, and the result is returned as the texture - sample. */ - DEPRECATED void set_compare_enabled(bool); - - /** Sets the function to use for depth comparison. */ - DEPRECATED void set_compare_func(Predicate); - /// Loads a Graphics::Image from a file and uploads it to the texture. virtual void load_image(const std::string &, unsigned = 0); diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index c0a96a4e..1e9ec903 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -206,7 +206,6 @@ void Texture2D::unload() glDeleteTextures(1, &id); id = 0; allocated = 0; - default_sampler.unload(); } diff --git a/source/render/rendertarget.cpp b/source/render/rendertarget.cpp index 7b603252..7bdac670 100644 --- a/source/render/rendertarget.cpp +++ b/source/render/rendertarget.cpp @@ -148,9 +148,6 @@ void RenderTarget::init(unsigned w, unsigned h, unsigned s, const RenderTargetFo { tgt.texture = new Texture2D; tgt.texture->storage(pf, width, height, 1); - Sampler &sampler = tgt.texture->get_default_sampler(); - sampler.set_filter(NEAREST); - sampler.set_wrap(CLAMP_TO_EDGE); fbo.attach(att, *tgt.texture); } buffers.push_back(tgt); @@ -170,15 +167,6 @@ RenderTarget::~RenderTarget() } } -void RenderTarget::set_texture_filter(TextureFilter filt) -{ - if(!samples) - { - for(vector::iterator i=buffers.begin(); i!=buffers.end(); ++i) - i->texture->get_default_sampler().set_filter(filt); - } -} - const Texture2D &RenderTarget::get_target_texture(unsigned i) const { if(i>=buffers.size()) diff --git a/source/render/rendertarget.h b/source/render/rendertarget.h index cb6d6b97..6577cdcf 100644 --- a/source/render/rendertarget.h +++ b/source/render/rendertarget.h @@ -77,7 +77,6 @@ public: unsigned get_height() const { return height; } const RenderTargetFormat &get_format() const { return format; } Framebuffer &get_framebuffer() { return fbo; } - void set_texture_filter(TextureFilter); const Texture2D &get_target_texture(unsigned) const; const Texture2D &get_target_texture(RenderOutput) const; void blit_from(const RenderTarget &); diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 682acde2..8a7780cd 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -37,8 +37,6 @@ void init_builtin_data(DataFile::BuiltinSource &); Resources *Resources::global_resources = 0; Resources::Resources(bool set_as_global): - default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR), - default_tex_anisotropy(1.0f), srgb_conversion(false), resource_manager(0) { @@ -113,16 +111,6 @@ const DataFile::CollectionSource &Resources::get_builtins() return builtins; } -void Resources::set_default_texture_filter(TextureFilter tf) -{ - default_tex_filter = tf; -} - -void Resources::set_default_texture_anisotropy(float a) -{ - default_tex_anisotropy = a; -} - void Resources::set_srgb_conversion(bool c) { srgb_conversion = c; @@ -199,16 +187,6 @@ Texture2D *Resources::create_texture2d(const string &name) image.load_io(*io); tex = new Texture2D(resource_manager); - Sampler &samp = tex->get_default_sampler(); - if(is_mipmapped(default_tex_filter)) - { - tex->set_auto_generate_mipmap(true); - samp.set_mag_filter(LINEAR); - } - else - samp.set_mag_filter(default_tex_filter); - samp.set_min_filter(default_tex_filter); - samp.set_max_anisotropy(default_tex_anisotropy); if(resource_manager) resource_manager->set_resource_location(*tex, *this, name); diff --git a/source/resources/resources.h b/source/resources/resources.h index 82d2f4c6..37de5d8c 100644 --- a/source/resources/resources.h +++ b/source/resources/resources.h @@ -32,8 +32,6 @@ public: }; private: - TextureFilter default_tex_filter; - float default_tex_anisotropy; bool srgb_conversion; ResourceManager *resource_manager; @@ -46,9 +44,6 @@ public: static Resources &get_global(); static const DataFile::CollectionSource &get_builtins(); - void set_default_texture_filter(TextureFilter); - void set_default_texture_anisotropy(float); - /** Enables or disables sRGB conversion. If enabled, textures and material colors are converted from sRGB to linear color space when loaded. */ DEPRECATED void set_srgb_conversion(bool);