#!/bin/sh

MAX_JOBS=7
TEXCONV=~/texconv.exe

find . -iname '*.dds' -print0 | while read -d $'\0' file; do
	if (file "$file" | grep -E '4096|8192' > /dev/null); then
		dir=$(dirname "$file")
		windir=$(cygpath -w "$dir")
		basename=$(basename "$file")
		echo "4k+: $file"
		mkdir -p "2k/$dir"
		# Best approach would be to discard the most detailed mip and
		# promote all the remaining mips without recompressing
		# anything, but texconv can't do that AFAICT. Next approach is
		# to resize the texture, but texconv can't do that if it has
		# mips - so we do two passes. First, resize the texture to 2k
		# and discard mip maps, then run a second pass to generate new
		# mip maps:
		(
			$TEXCONV -w 2048 -h 2048 -m 1 -sepalpha -o "2k\\$windir" "$windir\\$basename" > /dev/null
			$TEXCONV -y -sepalpha -o "2k\\$windir" "2k\\$windir\\$basename" > /dev/null
		) &
		while [ $(jobs | wc -l) -ge $MAX_JOBS ]; do
			jobs
			sleep 1
		done
	else
		echo "Not 4k: $file"
	fi
done
echo Waiting for remaining jobs to complete...
while [ $(jobs | wc -l) -ne 0 ]; do
	jobs
	sleep 1
done

Add script to downscale 4k/8k textures for reduced VRAM usage in SkyrimVR
Add script to downscale 4k/8k textures for reduced VRAM usage in SkyrimVR

Lots of Skyrim mods add unnecessarily detailed textures, that just eat
up a bunch of VRAM and cause micro-stutters while looking around as the
driver has to constantly page textures in and out of the GPU once the
VRAM limit is reached.

This is a dead-simple cygwin script intended to be run from the
Data/textures directory in Skyrim that will locate any 4K / 8K loose
textures and run Microsoft texconv to downscale them to a much more
reasonable 2K.

The downsized textures are placed in a 2K directory that is intended to
then be turned into a mod and added to your mod organiser of choice to
allow it to handle overriding the 4K textures rather than just outright
overwriting the mod added textures directly, though the downside is that
switching mods to others using different textures would then be using
the wrong ones... This is for advanced users only who know what they are
doing.

Microsoft texcov can be downloaded from here and by default the script
expects it to be placed/linked in the cygwin home directory:

    https://github.com/microsoft/DirectXTex/releases
