Atlas - D3D9_PixelShader_Palette.hlsli

Home / ext / SDL / src / render / direct3d Lines: 1 | Size: 1415 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1 2cbuffer Constants 3{ 4 float4 texel_size; 5}; 6 7uniform sampler2D image; 8uniform sampler1D palette; 9 10struct PixelShaderInput 11{ 12 float4 pos : SV_POSITION; 13 float2 tex : TEXCOORD0; 14 float4 color : COLOR0; 15}; 16 17static const float TEXTURETYPE_NONE = 0; 18static const float TEXTURETYPE_PALETTE_NEAREST = 1; 19static const float TEXTURETYPE_PALETTE_LINEAR = 2; 20 21float4 SamplePaletteNearest(float2 uv) 22{ 23 float index = tex2D(image, uv).r * 255; 24 return tex1D(palette, (index + 0.5) / 256); 25} 26 27// Implementation with thanks from bgolus: 28// https://discussions.unity.com/t/how-to-make-data-shader-support-bilinear-trilinear/598639/8 29float4 SamplePaletteLinear(float2 uv) 30{ 31 // scale & offset uvs to integer values at texel centers 32 float2 uv_texels = uv * texel_size.zw + 0.5; 33 34 // get uvs for the center of the 4 surrounding texels by flooring 35 float4 uv_min_max = float4((floor(uv_texels) - 0.5) * texel_size.xy, (floor(uv_texels) + 0.5) * texel_size.xy); 36 37 // blend factor 38 float2 uv_frac = frac(uv_texels); 39 40 // sample all 4 texels 41 float4 texelA = SamplePaletteNearest(uv_min_max.xy); 42 float4 texelB = SamplePaletteNearest(uv_min_max.xw); 43 float4 texelC = SamplePaletteNearest(uv_min_max.zy); 44 float4 texelD = SamplePaletteNearest(uv_min_max.zw); 45 46 // bilinear interpolation 47 return lerp(lerp(texelA, texelB, uv_frac.y), lerp(texelC, texelD, uv_frac.y), uv_frac.x); 48} 49 50
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.