Upscale Shader

I am trying to do upcaling of a texture (from 128 to 512) in a shader but I am having trouble finishing it. What I do is look at a pixel and its 3 neighbors in the 128 texture and decide what 4x4 pixels to replace it with. The 4x4 pixels I have in a static array(upscaleLT) and just need to look up depending on the UV. But I fail to get the offset within the new texel for what 4x4 value to use. Any ideas?

fixed4 frag (v2f i) : SV_Target
            {
                ...
                ...

                float2 targetRes = _MainTex_TexelSize.zw * 4.;
                float2 targetTexelSize = 1. / targetRes;
  -->           float2 targetTexelOffset = (i.uv % targetRes) % targetTexelSize / targetTexelSize;
             
                int pixelIndex = (targetTexelOffset.x + targetTexelOffset.y * 4.);

                fixed c = upscaleLT[upscaleIndex].pixels[pixelIndex];
        
                fixed4 col = fixed4(c, c, c, 1.);

                return col;
            }

The line with the arrow (line nr 8) is the culprit that I cant figure out. Thanks for any input/help!

I don’t think “i.uv % targetRes” will do something relevant, unless you have very big uv values : _MainTex_TexelSize.zw contains the texture pixel size, and you multiply by 4, so probably i.uv % targetRes = i.uv
I dont clearly get what you are doing, but could it be something like this :

float2 targetTexelOffset = floor( ( i.uv * _MainTex_texelSize.zw * 4 ) % 4 );

To have a targetTexelOffset value that are ints in the range [0;4[ ?

@Remy_Unity Yes, the “i.uv % targetRes” was part of desperate attempt to find a solution that I had meant to omit. The code change you proposed did exactly what I had in mind however. Many thanks for your reply, I was clearly overcomplicating the problem :slight_smile:

Thanks!