2019-01-03 13:34:30 +03:00
|
|
|
/*
|
2021-01-15 02:53:49 +03:00
|
|
|
* Copyright 2011-2021 Branimir Karadzic. All rights reserved.
|
2019-01-03 13:34:30 +03:00
|
|
|
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
|
|
|
|
*/
|
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
/*
|
|
|
|
* Reference(s):
|
|
|
|
* - Based on Virtual Texture Demo by Brad Blanchard
|
|
|
|
* http://web.archive.org/web/20190103162638/http://linedef.com/virtual-texture-demo.html
|
|
|
|
*/
|
|
|
|
|
2019-01-03 13:34:30 +03:00
|
|
|
uniform vec4 u_vt_settings_1;
|
|
|
|
uniform vec4 u_vt_settings_2;
|
|
|
|
|
|
|
|
#define VirtualTextureSize u_vt_settings_1.x
|
|
|
|
#define AtlasScale u_vt_settings_1.y
|
|
|
|
#define BorderScale u_vt_settings_1.z
|
|
|
|
#define BorderOffset u_vt_settings_1.w
|
|
|
|
|
|
|
|
#define MipBias u_vt_settings_2.x
|
|
|
|
#define PageTableSize u_vt_settings_2.y
|
|
|
|
|
|
|
|
SAMPLER2D(s_vt_page_table, 0);
|
|
|
|
SAMPLER2D(s_vt_texture_atlas, 1);
|
|
|
|
|
|
|
|
// This function estimates mipmap levels
|
|
|
|
float MipLevel( vec2 uv, float size )
|
|
|
|
{
|
2019-01-05 10:12:48 +03:00
|
|
|
vec2 dx = dFdx( uv * size );
|
|
|
|
vec2 dy = dFdy( uv * size );
|
|
|
|
float d = max( dot( dx, dx ), dot( dy, dy ) );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
return max( 0.5 * log2( d ), 0 );
|
2019-01-03 13:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function samples the page table and returns the page's
|
|
|
|
// position and mip level.
|
|
|
|
vec3 SampleTable( vec2 uv, float mip )
|
|
|
|
{
|
2019-01-05 10:12:48 +03:00
|
|
|
vec2 offset = fract( uv * PageTableSize ) / PageTableSize;
|
|
|
|
return texture2DLod( s_vt_page_table, uv - offset, mip ).xyz;
|
2019-01-03 13:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// This functions samples from the texture atlas and returns the final color
|
|
|
|
vec4 SampleAtlas( vec3 page, vec2 uv )
|
|
|
|
{
|
2019-01-05 10:12:48 +03:00
|
|
|
float mipsize = exp2( floor( page.z * 255.0 + 0.5 ) );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
uv = fract( uv * PageTableSize / mipsize );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
uv *= BorderScale;
|
|
|
|
uv += BorderOffset;
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-23 00:31:41 +03:00
|
|
|
vec2 offset = floor( page.xy * 255.0 + 0.5 );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
return texture2D( s_vt_texture_atlas, ( offset + uv ) * AtlasScale );
|
2019-01-03 13:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ugly brute force trilinear, look up twice and mix
|
|
|
|
vec4 VirtualTextureTrilinear( vec2 uv )
|
|
|
|
{
|
2019-01-05 10:12:48 +03:00
|
|
|
float miplevel = MipLevel( uv, VirtualTextureSize );
|
2019-01-23 00:31:41 +03:00
|
|
|
miplevel = clamp( miplevel, 0.0, log2( PageTableSize )-1.0 );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
float mip1 = floor( miplevel );
|
2019-01-23 00:31:41 +03:00
|
|
|
float mip2 = mip1 + 1.0;
|
2019-01-05 10:12:48 +03:00
|
|
|
float mipfrac = miplevel - mip1;
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
vec3 page1 = SampleTable( uv, mip1 );
|
|
|
|
vec3 page2 = SampleTable( uv, mip2 );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
vec4 sample1 = SampleAtlas( page1, uv );
|
|
|
|
vec4 sample2 = SampleAtlas( page2, uv );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
return mix( sample1, sample2, mipfrac );
|
2019-01-03 13:34:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Simple bilinear
|
|
|
|
vec4 VirtualTexture( vec2 uv )
|
|
|
|
{
|
2019-01-05 10:12:48 +03:00
|
|
|
float mip = floor( MipLevel( uv, VirtualTextureSize ) );
|
|
|
|
mip = clamp( mip, 0, log2( PageTableSize ) );
|
2019-01-03 13:34:30 +03:00
|
|
|
|
2019-01-05 10:12:48 +03:00
|
|
|
vec3 page = SampleTable( uv, mip );
|
|
|
|
return SampleAtlas( page, uv );
|
2019-01-03 13:34:30 +03:00
|
|
|
}
|