From 07036ac48f0d2005f77d966aaf5991a9aea52d4c Mon Sep 17 00:00:00 2001 From: Richard Harrison Date: Sun, 20 Jan 2019 18:10:20 +0100 Subject: [PATCH] Add load origin hint to options This is to allow the DDS cache to handle the image load differently depending on the origin. --- simgear/scene/material/TextureBuilder.cxx | 28 +++++++++++++++++-- simgear/scene/material/mipmap.hxx | 20 ++++++++------ simgear/scene/util/SGReaderWriterOptions.hxx | 29 +++++++++++++++++--- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/simgear/scene/material/TextureBuilder.cxx b/simgear/scene/material/TextureBuilder.cxx index 5634d966..0d2a60c7 100644 --- a/simgear/scene/material/TextureBuilder.cxx +++ b/simgear/scene/material/TextureBuilder.cxx @@ -76,7 +76,7 @@ osg::Texture* TextureBuilder::buildFromType(Effect* effect, Pass* pass, const st typedef boost::tuple TexTuple; + string, MipMapTuple, ImageInternalFormat> TexTuple; EffectNameValue texEnvModesInit[] = { @@ -239,6 +239,17 @@ TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props, } } + const SGPropertyNode* pInternalFormat = getEffectPropertyChild(effect, props, "internal-format"); + pInternalFormat = props->getChild("internal-format"); + ImageInternalFormat iformat = ImageInternalFormat::Unspecified; + if (pInternalFormat) { + std::string internalFormat = pInternalFormat->getStringValue(); + if (internalFormat == "normalized") { + iformat = ImageInternalFormat::Normalized; + SG_LOG(SG_INPUT, SG_ALERT, "internal-format normalized '" << imageName << "'"); + } + } + const SGPropertyNode* pMipmapControl = getEffectPropertyChild(effect, props, "mipmap-control"); MipMapTuple mipmapFunctions( AUTOMATIC, AUTOMATIC, AUTOMATIC, AUTOMATIC ); @@ -246,7 +257,7 @@ TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props, mipmapFunctions = makeMipMapTuple(effect, pMipmapControl, options); return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap, - texType, mipmapFunctions); + texType, mipmapFunctions, iformat); } bool setAttrs(const TexTuple& attrs, Texture* tex, @@ -258,7 +269,14 @@ bool setAttrs(const TexTuple& attrs, Texture* tex, osgDB::ReaderWriter::ReadResult result; + // load texture for effect + SGReaderWriterOptions::LoadOriginHint origLOH = options->getLoadOriginHint(); + if(attrs.get<8>() == ImageInternalFormat::Normalized) + options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS_NORMALIZED); + else + options->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS); result = osgDB::readRefImageFile(imageName, options); + options->setLoadOriginHint(origLOH); osg::ref_ptr image; if (result.success()) image = result.getImage(); @@ -586,7 +604,10 @@ Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE); osgDB::ReaderWriter::ReadResult result; - result = osgDB::readImageFile(_tuple.get<0>(), options); + SGReaderWriterOptions* wOpts = (SGReaderWriterOptions*)options; + SGReaderWriterOptions::LoadOriginHint origLOH = wOpts->getLoadOriginHint(); + wOpts->setLoadOriginHint(SGReaderWriterOptions::LoadOriginHint::ORIGIN_EFFECTS); + result = osgDB::readRefImageFile(_tuple.get<0>(), options); if(result.success()) { osg::Image* image = result.getImage(); cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image); @@ -616,6 +637,7 @@ Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* osg::Image* image = result.getImage(); cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image); } + wOpts->setLoadOriginHint(origLOH); if (itr == _cubemaps.end()) _cubemaps[_tuple] = cubeTexture; diff --git a/simgear/scene/material/mipmap.hxx b/simgear/scene/material/mipmap.hxx index 0a908c59..75b29231 100644 --- a/simgear/scene/material/mipmap.hxx +++ b/simgear/scene/material/mipmap.hxx @@ -31,14 +31,18 @@ class Effect; class SGReaderWriterOptions; namespace effect { -enum MipMapFunction { - AUTOMATIC, - AVERAGE, - SUM, - PRODUCT, - MIN, - MAX -}; + enum MipMapFunction { + AUTOMATIC, + AVERAGE, + SUM, + PRODUCT, + MIN, + MAX + }; + enum ImageInternalFormat { + Unspecified, + Normalized, + }; typedef boost::tuple MipMapTuple; diff --git a/simgear/scene/util/SGReaderWriterOptions.hxx b/simgear/scene/util/SGReaderWriterOptions.hxx index 2760df90..17183944 100644 --- a/simgear/scene/util/SGReaderWriterOptions.hxx +++ b/simgear/scene/util/SGReaderWriterOptions.hxx @@ -38,12 +38,22 @@ namespace simgear class SGReaderWriterOptions : public osgDB::Options { public: + enum LoadOriginHint + { + ORIGIN_MODEL, + ORIGIN_EFFECTS, + ORIGIN_EFFECTS_NORMALIZED, + }; + + //SGReaderWriterOptions* cloneOptions(const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY) const { return static_cast(clone(copyop)); } + SGReaderWriterOptions() : _materialLib(0), _load_panel(0), _model_data(0), _instantiateEffects(false), - _instantiateMaterialEffects(false) + _instantiateMaterialEffects(false), + _LoadOriginHint(ORIGIN_MODEL) { } SGReaderWriterOptions(const std::string& str) : osgDB::Options(str), @@ -51,7 +61,8 @@ public: _load_panel(0), _model_data(0), _instantiateEffects(false), - _instantiateMaterialEffects(false) + _instantiateMaterialEffects(false), + _LoadOriginHint(ORIGIN_MODEL) { } SGReaderWriterOptions(const osgDB::Options& options, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY) : @@ -60,7 +71,8 @@ public: _load_panel(0), _model_data(0), _instantiateEffects(false), - _instantiateMaterialEffects(false) + _instantiateMaterialEffects(false), + _LoadOriginHint(ORIGIN_MODEL) { } SGReaderWriterOptions(const SGReaderWriterOptions& options, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY) : @@ -75,7 +87,8 @@ public: _instantiateEffects(options._instantiateEffects), _instantiateMaterialEffects(options._instantiateMaterialEffects), _materialName(options._materialName), - _sceneryPathSuffixes(options._sceneryPathSuffixes) + _sceneryPathSuffixes(options._sceneryPathSuffixes), + _LoadOriginHint(ORIGIN_MODEL) { } META_Object(simgear, SGReaderWriterOptions); @@ -139,6 +152,13 @@ public: const SGGeod& getLocation() const { return _geod; } + // the load origin defines where the load request has come from. + // example usage; to allow the DDS Texture Cache (DTC) to ignore + // any texture that is used in a shader, as these often have special values + // encoded into the channels that aren't suitable for conversion. + void setLoadOriginHint(LoadOriginHint _v) const { _LoadOriginHint = _v; } + LoadOriginHint getLoadOriginHint() const { return _LoadOriginHint; } + protected: virtual ~SGReaderWriterOptions(); @@ -157,6 +177,7 @@ private: string _materialName; string_list _sceneryPathSuffixes; SGGeod _geod; + mutable LoadOriginHint _LoadOriginHint; }; }