SVT shaders copyright and formatting fixes

This commit is contained in:
Aleš Mlakar 2019-01-05 08:12:48 +01:00
parent 9f748b1b28
commit cf4d1ca28e
4 changed files with 50 additions and 32 deletions

View File

@ -5,14 +5,20 @@ $input v_texcoord0
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/ */
/*
* Reference(s):
* - Based on Virtual Texture Demo by Brad Blanchard
* http://web.archive.org/web/20190103162638/http://linedef.com/virtual-texture-demo.html
*/
#include "../common/common.sh" #include "../common/common.sh"
#include "virtualtexture.sh" #include "virtualtexture.sh"
void main() void main()
{ {
float mipCount = log2(PageTableSize); float mipCount = log2(PageTableSize);
float mip = floor(MipLevel(v_texcoord0.xy, VirtualTextureSize) - MipBias); float mip = floor(MipLevel(v_texcoord0.xy, VirtualTextureSize) - MipBias);
mip = clamp(mip, 0, mipCount); mip = clamp(mip, 0, mipCount);
vec2 offset = floor(v_texcoord0.xy * PageTableSize); vec2 offset = floor(v_texcoord0.xy * PageTableSize);
gl_FragColor = vec4(floor(vec3(offset / exp2(mip), mip)) / 255.0, 1.0); gl_FragColor = vec4(floor(vec3(offset / exp2(mip), mip)) / 255.0, 1.0);
} }

View File

@ -4,6 +4,12 @@ $input v_texcoord0
* Copyright 2011-2018 Branimir Karadzic. All rights reserved. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/ */
/*
* Reference(s):
* - Based on Virtual Texture Demo by Brad Blanchard
* http://web.archive.org/web/20190103162638/http://linedef.com/virtual-texture-demo.html
*/
#include "../common/common.sh" #include "../common/common.sh"
#include "virtualtexture.sh" #include "virtualtexture.sh"

View File

@ -3,6 +3,12 @@
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/ */
/*
* Reference(s):
* - Based on Virtual Texture Demo by Brad Blanchard
* http://web.archive.org/web/20190103162638/http://linedef.com/virtual-texture-demo.html
*/
uniform vec4 u_vt_settings_1; uniform vec4 u_vt_settings_1;
uniform vec4 u_vt_settings_2; uniform vec4 u_vt_settings_2;
@ -20,61 +26,61 @@ SAMPLER2D(s_vt_texture_atlas, 1);
// This function estimates mipmap levels // This function estimates mipmap levels
float MipLevel( vec2 uv, float size ) float MipLevel( vec2 uv, float size )
{ {
vec2 dx = dFdx( uv * size ); vec2 dx = dFdx( uv * size );
vec2 dy = dFdy( uv * size ); vec2 dy = dFdy( uv * size );
float d = max( dot( dx, dx ), dot( dy, dy ) ); float d = max( dot( dx, dx ), dot( dy, dy ) );
return max( 0.5 * log2( d ), 0 ); return max( 0.5 * log2( d ), 0 );
} }
// This function samples the page table and returns the page's // This function samples the page table and returns the page's
// position and mip level. // position and mip level.
vec3 SampleTable( vec2 uv, float mip ) vec3 SampleTable( vec2 uv, float mip )
{ {
vec2 offset = fract( uv * PageTableSize ) / PageTableSize; vec2 offset = fract( uv * PageTableSize ) / PageTableSize;
return texture2DLod( s_vt_page_table, uv - offset, mip ).xyz; return texture2DLod( s_vt_page_table, uv - offset, mip ).xyz;
} }
// This functions samples from the texture atlas and returns the final color // This functions samples from the texture atlas and returns the final color
vec4 SampleAtlas( vec3 page, vec2 uv ) vec4 SampleAtlas( vec3 page, vec2 uv )
{ {
float mipsize = exp2( floor( page.z * 255.0 + 0.5 ) ); float mipsize = exp2( floor( page.z * 255.0 + 0.5 ) );
uv = fract( uv * PageTableSize / mipsize ); uv = fract( uv * PageTableSize / mipsize );
uv *= BorderScale; uv *= BorderScale;
uv += BorderOffset; uv += BorderOffset;
vec2 offset = floor( page.xy * 255 + 0.5 ); vec2 offset = floor( page.xy * 255 + 0.5 );
return texture2D( s_vt_texture_atlas, ( offset + uv ) * AtlasScale ); return texture2D( s_vt_texture_atlas, ( offset + uv ) * AtlasScale );
} }
// Ugly brute force trilinear, look up twice and mix // Ugly brute force trilinear, look up twice and mix
vec4 VirtualTextureTrilinear( vec2 uv ) vec4 VirtualTextureTrilinear( vec2 uv )
{ {
float miplevel = MipLevel( uv, VirtualTextureSize ); float miplevel = MipLevel( uv, VirtualTextureSize );
miplevel = clamp( miplevel, 0, log2( PageTableSize )-1 ); miplevel = clamp( miplevel, 0, log2( PageTableSize )-1 );
float mip1 = floor( miplevel ); float mip1 = floor( miplevel );
float mip2 = mip1 + 1; float mip2 = mip1 + 1;
float mipfrac = miplevel - mip1; float mipfrac = miplevel - mip1;
vec3 page1 = SampleTable( uv, mip1 ); vec3 page1 = SampleTable( uv, mip1 );
vec3 page2 = SampleTable( uv, mip2 ); vec3 page2 = SampleTable( uv, mip2 );
vec4 sample1 = SampleAtlas( page1, uv ); vec4 sample1 = SampleAtlas( page1, uv );
vec4 sample2 = SampleAtlas( page2, uv ); vec4 sample2 = SampleAtlas( page2, uv );
return mix( sample1, sample2, mipfrac ); return mix( sample1, sample2, mipfrac );
} }
// Simple bilinear // Simple bilinear
vec4 VirtualTexture( vec2 uv ) vec4 VirtualTexture( vec2 uv )
{ {
float mip = floor( MipLevel( uv, VirtualTextureSize ) ); float mip = floor( MipLevel( uv, VirtualTextureSize ) );
mip = clamp( mip, 0, log2( PageTableSize ) ); mip = clamp( mip, 0, log2( PageTableSize ) );
vec3 page = SampleTable( uv, mip ); vec3 page = SampleTable( uv, mip );
return SampleAtlas( page, uv ); return SampleAtlas( page, uv );
} }

View File

@ -10,7 +10,7 @@ $output v_texcoord0
void main() void main()
{ {
vec3 wpos = mul(u_model[0], vec4(a_position, 1.0) ).xyz; vec3 wpos = mul(u_model[0], vec4(a_position, 1.0) ).xyz;
gl_Position = mul(u_viewProj, vec4(wpos, 1.0) ); gl_Position = mul(u_viewProj, vec4(wpos, 1.0) );
v_texcoord0 = a_texcoord0; v_texcoord0 = a_texcoord0;
} }