Page 1 of 1

Scene texture maps resize policy: MIPMAPMEM

Posted: Mon Jun 14, 2021 11:51 am
by Dade
Continuing from: viewtopic.php?f=5&t=3602#p30441

Policy: MIPMAPMEM

MIPMAPMEM image maps resize policy is a trade off between pre-processing speed and memory usage reduction. Instead of using exactly the estimated optimal size of the image, it will use the nearest mip map level size (i.e. for instance, with a 512x512 image, the mip map levels are 512x512, 256x256, 128x128, 64x64, 32x32, 16x16, 8x8, 4x4, 2x2, 1x1).
The huge advantage is that the policy uses a .tx file (and creates it if it doesn't already exist or is outdated) with all the mip map levels. So it works like a cache, the mip map levels are created only once and than reused from the existing .tx file avoiding any pre-processing time.

This is the policy to use in every day work. MINMEM instead is the last resort when you are trying to use every single bit of the RAM.

MIPMAPMEM policy is defined by:

Code: Select all

scene.images.resizepolicy.type = MIPMAPMEM
scene.images.resizepolicy.scale = 1.0
scene.images.resizepolicy.minsize = 64
This is an example of the policy working with usual test scene:

mipmap.jpg

Code: Select all

[SDL][0.001] Creating TX image for file:  scenes/randomizedtiling/pattern-3.jpg
[SDL][0.041] Probe ImageMap: scenes/randomizedtiling/pattern-3.jpg.tx [from 512x512 to 64x64]
[SDL][0.041] Reading texture map: scenes/randomizedtiling/pattern-3.jpg.tx
[SDL][0.041] Mip map available: [512x512][256x256][128x128][64x64][32x32][16x16][8x8][4x4][2x2][1x1]
[SDL][0.041] Reading mip map level: 3
[SDL][0.043] Texture definition: 140707101904392Color
[SDL][0.043] Creating TX image for file:  scenes/randomizedtiling/pattern-7.jpg
[SDL][0.087] Probe ImageMap: scenes/randomizedtiling/pattern-7.jpg.tx [from 512x512 to 64x64]
[SDL][0.087] Reading texture map: scenes/randomizedtiling/pattern-7.jpg.tx
[SDL][0.087] Mip map available: [512x512][256x256][128x128][64x64][32x32][16x16][8x8][4x4][2x2][1x1]
[SDL][0.087] Reading mip map level: 3
[SDL][0.088] Texture definition: 140707106223624Color
[SDL][0.088] Creating TX image for file:  scenes/randomizedtiling/pattern-9.jpg
[SDL][0.134] Probe ImageMap: scenes/randomizedtiling/pattern-9.jpg.tx [from 512x512 to 64x64]
[SDL][0.134] Reading texture map: scenes/randomizedtiling/pattern-9.jpg.tx
[SDL][0.134] Mip map available: [512x512][256x256][128x128][64x64][32x32][16x16][8x8][4x4][2x2][1x1]
[SDL][0.134] Reading mip map level: 3
[...]
[LuxCore][0.506] Optimal image map size preprocess thread count: 24
[SDL][0.816] Image maps "scenes/randomizedtiling/pattern-3.jpg" optimal resize: 512x512 => 533x533
[SDL][0.816] Image maps "scenes/randomizedtiling/pattern-7.jpg" optimal resize: 512x512 => 266x266
[SDL][0.816] Image maps "scenes/randomizedtiling/pattern-9.jpg" optimal resize: 512x512 => 133x133
[SDL][0.816] WARNING: image maps "scenes/randomizedtiling/pattern-3.jpg" too small !
[SDL][0.816] Reading texture map: scenes/randomizedtiling/pattern-3.jpg.tx
[SDL][0.817] Mip map available: [512x512][256x256][128x128][64x64][32x32][16x16][8x8][4x4][2x2][1x1]
[SDL][0.817] Reading mip map level: 0
[SDL][0.838] Image maps "scenes/randomizedtiling/pattern-3.jpg" scaled: 512x512 => 512x512
[SDL][0.838] Reading texture map: scenes/randomizedtiling/pattern-7.jpg.tx
[SDL][0.839] Mip map available: [512x512][256x256][128x128][64x64][32x32][16x16][8x8][4x4][2x2][1x1]
[SDL][0.839] Reading mip map level: 0
[SDL][0.859] Image maps "scenes/randomizedtiling/pattern-7.jpg" scaled: 512x512 => 512x512
[SDL][0.859] Reading texture map: scenes/randomizedtiling/pattern-9.jpg.tx
[SDL][0.860] Mip map available: [512x512][256x256][128x128][64x64][32x32][16x16][8x8][4x4][2x2][1x1]
[SDL][0.860] Reading mip map level: 1
[SDL][0.879] Image maps "scenes/randomizedtiling/pattern-9.jpg" scaled: 512x512 => 256x256
[SDL][0.879] Memory required for original Image maps: 2304Kbytes
[SDL][0.879] Memory required for MIPMAPMEM Image maps: 1728Kbytes
[SDL][0.879] Applying resize policy MIPMAPMEM time: 0.4secs
You can see the different result in term of memory usage of different policies:

- Memory required for original Image maps: 2,304Kbytes
- Memory required for MIPMAPMEM Image maps: 1,728Kbytes
- Memory required for MINMEM Image maps: 879Kbytes


A real world test

hallbench0.jpg

The results of rendering of HallBench scene with different policies are:

- Memory required for original Image maps: 272,710Kbytes
- Memory required for MIPMAPMEM Image maps: 156,862Kbytes
- Memory required for MINMEM Image maps: 113,532Kbytes


As you see, MIPMAPMEM still deliver a very significative memory usage reduction without most of the pre-processing time cost of MINMEM.

PyLuxCoreTool: MakeTx

There is a new command line PyLuxCoreTool called "MakeTx". It can be used if you want to pre-create the .tx files. For instance if you want to create all the files of a large library of images (instead of having it done on demand). For instance on Linux:

Code: Select all

python3 lib/pyluxcoretools.zip maketx scenes/randomizedtiling/pattern-3.jpg scenes/randomizedtiling/pattern-3.jpg.tx

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Mon Jun 14, 2021 4:11 pm
by Sharlybg
Great it isn't yet exposed in blendluxcore right ?
Anyway thanks for your hard work ;)

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Mon Jun 14, 2021 5:29 pm
by Dez!
I join in the thanks!

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Mon Jun 14, 2021 11:34 pm
by juangea
I’m eager to test this on BlendLuxCore, this would allow us to render impossible scenes for the GPU before.

I wonder, if we create the TX files before, what could be the impact on size disk?

Also this thx files would be useful just for one camera or frame or will it generate all the files to be picked depending on the requirements?

The large library case is our case hehe

Amazing job :D

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Tue Jun 15, 2021 9:54 am
by Dade
juangea wrote: Mon Jun 14, 2021 11:34 pm I wonder, if we create the TX files before, what could be the impact on size disk?
It is not small: a 1024x1024 PNG is translated in a TX with 1024x1024, 512x512, 256x256, etc. It is about a +50% in HD space used.
juangea wrote: Mon Jun 14, 2021 11:34 pm Also this thx files would be useful just for one camera or frame or will it generate all the files to be picked depending on the requirements?
It is the same TX file across any frame, scene, etc. It is created once and than good in any case, forever (i.e. or until when you don't modify the source file). It is like Arnold.

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Tue Jun 15, 2021 10:13 am
by B.Y.O.B.
Where are the .tx files saved?

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Tue Jun 15, 2021 10:27 am
by SteffenD
Nice progress. One question though:
Is this new feature comparable to a "real" texture cache (using tiled and mipmapped tx files) like the one used by Arnold or Redshift?

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Tue Jun 15, 2021 12:56 pm
by Dade
SteffenD wrote: Tue Jun 15, 2021 10:27 am Is this new feature comparable to a "real" texture cache (using tiled and mipmapped tx files) like the one used by Arnold or Redshift?
The end result is very similar however process is quite different: Arnold or Redshift picks the image mip map level and tile to use at run time each time they need to access the image. This solution has bit of runtime cost and has huge requirement from the coding point of view (i.e. it requires ray differentials and and support for them across a lot of code). It is extremely complex (and performance expansive) to run on GPUs.
On other side, Arnold or Redshift always uses the best mip map level for each sample because it is done at run time.

My solution is done at scene pre-processing, it has no rendering cost at all and no requirements (it work out of the box with GPUs). On other side, it may not always use the optimal mip map level.

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Tue Jun 15, 2021 1:04 pm
by SteffenD
Ah, thanks for clarification, Dade.
Sounds like a very clever solution.

Re: Scene texture maps resize policy: MIPMAPMEM

Posted: Tue Jun 15, 2021 1:12 pm
by Dade
SteffenD wrote: Tue Jun 15, 2021 1:04 pm Sounds like a very clever solution.
It is a trick but it should be a good one. Instead of have to rewrite half of the code for ray differentials, it is a solution that can work with any existing render.
As usual, the big deal is not the CPU but the GPUs: implementing and Arnold/Redshift solution for GPUs is complex and can have quite noticeable run time cost.