Skip to content

Commit 837a2e1

Browse files
Warr1024Zeno-
authored andcommittedMar 21, 2015
Fix composite textures with texture_min_size. Moved upscaling of textures to later in the process, when images are converted to textures, instead of right after image load, so the original image is unmodified for generateImagePart.
1 parent 709f4a5 commit 837a2e1

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed
 

‎src/client/tile.cpp

+44-32
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,42 @@ struct TextureInfo
182182
}
183183
};
184184

185+
/* Upscale textures to user's requested minimum size. This is a trick to make
186+
* filters look as good on low-res textures as on high-res ones, by making
187+
* low-res textures BECOME high-res ones. This is helpful for worlds that
188+
* mix high- and low-res textures, or for mods with least-common-denominator
189+
* textures that don't have the resources to offer high-res alternatives.
190+
*/
191+
video::IImage *textureMinSizeUpscale(video::IVideoDriver *driver, video::IImage *orig) {
192+
if(orig == NULL)
193+
return orig;
194+
s32 scaleto = g_settings->getS32("texture_min_size");
195+
if (scaleto > 0) {
196+
197+
/* Calculate scaling needed to make the shortest texture dimension
198+
* equal to the target minimum. If e.g. this is a vertical frames
199+
* animation, the short dimension will be the real size.
200+
*/
201+
const core::dimension2d<u32> dim = orig->getDimension();
202+
u32 xscale = scaleto / dim.Width;
203+
u32 yscale = scaleto / dim.Height;
204+
u32 scale = (xscale > yscale) ? xscale : yscale;
205+
206+
// Never downscale; only scale up by 2x or more.
207+
if (scale > 1) {
208+
u32 w = scale * dim.Width;
209+
u32 h = scale * dim.Height;
210+
const core::dimension2d<u32> newdim = core::dimension2d<u32>(w, h);
211+
video::IImage *newimg = driver->createImage(
212+
orig->getColorFormat(), newdim);
213+
orig->copyToScaling(newimg);
214+
return newimg;
215+
}
216+
}
217+
218+
return orig;
219+
}
220+
185221
/*
186222
SourceImageCache: A cache used for storing source images.
187223
*/
@@ -276,36 +312,6 @@ class SourceImageCache
276312
}
277313
}
278314

279-
/* Upscale textures to user's requested minimum size. This is a trick to make
280-
* filters look as good on low-res textures as on high-res ones, by making
281-
* low-res textures BECOME high-res ones. This is helpful for worlds that
282-
* mix high- and low-res textures, or for mods with least-common-denominator
283-
* textures that don't have the resources to offer high-res alternatives.
284-
*/
285-
s32 scaleto = g_settings->getS32("texture_min_size");
286-
if (scaleto > 0) {
287-
288-
/* Calculate scaling needed to make the shortest texture dimension
289-
* equal to the target minimum. If e.g. this is a vertical frames
290-
* animation, the short dimension will be the real size.
291-
*/
292-
const core::dimension2d<u32> dim = toadd->getDimension();
293-
u32 xscale = scaleto / dim.Width;
294-
u32 yscale = scaleto / dim.Height;
295-
u32 scale = (xscale > yscale) ? xscale : yscale;
296-
297-
// Never downscale; only scale up by 2x or more.
298-
if (scale > 1) {
299-
u32 w = scale * dim.Width;
300-
u32 h = scale * dim.Height;
301-
const core::dimension2d<u32> newdim = core::dimension2d<u32>(w, h);
302-
video::IImage *newimg = driver->createImage(
303-
toadd->getColorFormat(), newdim);
304-
toadd->copyToScaling(newimg);
305-
toadd = newimg;
306-
}
307-
}
308-
309315
if (need_to_grab)
310316
toadd->grab();
311317
m_images[name] = toadd;
@@ -682,7 +688,8 @@ u32 TextureSource::generateTexture(const std::string &name)
682688
video::IVideoDriver *driver = m_device->getVideoDriver();
683689
sanity_check(driver);
684690

685-
video::IImage *img = generateImage(name);
691+
video::IImage *origimg = generateImage(name);
692+
video::IImage *img = textureMinSizeUpscale(driver, origimg);
686693

687694
video::ITexture *tex = NULL;
688695

@@ -693,6 +700,8 @@ u32 TextureSource::generateTexture(const std::string &name)
693700
// Create texture from resulting image
694701
tex = driver->addTexture(name.c_str(), img);
695702
img->drop();
703+
if((origimg != NULL) && (img != origimg))
704+
origimg->drop();
696705
}
697706

698707
/*
@@ -783,7 +792,8 @@ void TextureSource::rebuildImagesAndTextures()
783792
// Recreate textures
784793
for (u32 i=0; i<m_textureinfo_cache.size(); i++){
785794
TextureInfo *ti = &m_textureinfo_cache[i];
786-
video::IImage *img = generateImage(ti->name);
795+
video::IImage *origimg = generateImage(ti->name);
796+
video::IImage *img = textureMinSizeUpscale(driver, origimg);
787797
#ifdef __ANDROID__
788798
img = Align2Npot2(img, driver);
789799
sanity_check(img->getDimension().Height == npot2(img->getDimension().Height));
@@ -794,6 +804,8 @@ void TextureSource::rebuildImagesAndTextures()
794804
if (img) {
795805
t = driver->addTexture(ti->name.c_str(), img);
796806
img->drop();
807+
if(origimg && (origimg != img))
808+
origimg->drop();
797809
}
798810
video::ITexture *t_old = ti->texture;
799811
// Replace texture

0 commit comments

Comments
 (0)