- Substance 3D home
- Home
- Getting Started
- Getting Started overview
- Activation and licenses
- System requirements
- Project creation
- Export
- Export overview
- Export window
- Export presets
- Export overview
- Glossary
- Performance
- Getting Started overview
- Interface
- Assets
- Substance 3D Assets
- Color picker
- Display settings
- History
- Layer stack
- Main menu
- Project configuration
- Properties
- Settings
- Shader settings
- Texture Set
- Toolbars
- Viewport
- Miscellaneous
- Assets
- Painting
- Painting overview
- Tool list
- Straight line
- Lazy mouse
- Symmetry
- Fill projections
- Presets
- Presets overview
- Creating and saving presets
- Creating particles presets
- Photoshop brush presets (ABR)
- Dynamic strokes
- Advanced channel painting
- Vector graphic (.svg & .ai)
- Text resource
- Effects
- Baking
- Content
- Creating custom effects
- Importing assets
- Creating custom effects
- Features
- Automatic UV Unwrapping
- Physical size
- Smart Materials and Masks
- Subsurface Scattering
- Dynamic Material Layering
- UV Reprojection
- UV Tiles
- Color Management
- Post Processing
- Iray Renderer
- Plugins
- Sparse Virtual Textures
- Custom Shaders
- SpaceMouse® by 3Dconnexion
- Universal Scene Description (USD)
- Send to
- Technical Support
- Performance Guidelines
- Configuring Pens and Tablets
- Exporting the log file
- Exporting a DXDiag
- Technical issues
- GPU Issues
- Crash when working with overclocked GPU
- Forcing the external GPU on Mac OS
- GPU drivers compatibility
- GPU drivers crash with long computations (TDR crash)
- GPU has outdated drivers
- GPU is not recognized
- GPU is not recognized and is mentionned as GDI Generic
- Issues with Nvidia GPUs on recent Mac OS versions
- Multi/Bi-GPU
- Running on integrated GPU
- Painter doesn't start on the right GPU
- Startup Issues
- Rendering Issues
- Stability Issues
- Miscellaneous Issues
- GPU Issues
- Workflow Issues
- Export Issues
- Tools Issues
- Project Issues
- Library Issues
- Viewport Issues
- Plugins Issues
- License Issues
- Pipeline and integration
- Installation and preferences
- Configuration
- Resource management
- Scripting and development
- Scripts and plugins
- Shader API Reference
- Shader API overview
- Changelog - Shader API
- Libraries - Shader API
- Lib Alpha - Shader API
- Lib Bayer - Shader API
- Lib Defines - Shader API
- Lib Emissive - Shader API
- Lib Env - Shader API
- Lib Normal - Shader API
- Lib PBR - Shader API
- Lib PBR Aniso - Shader API
- Lib Pom - Shader API
- Lib Random - Shader API
- Lib Sampler - Shader API
- Lib Sparse - Shader API
- Lib SSS - Shader API
- Lib Utils - Shader API
- Lib Vectors - Shader API
- Parameters - Shader API
- Shaders - Shader API
- Release notes
- Release notes overview
- All Changes
- Version 10.1
- Version 10.0
- Version 9.1
- Old versions
- Version 9.0
- Version 8.3
- Version 8.2
- Version 8.1
- Version 7.4
- Version 7.3
- Version 7.2
- Version 2021.1 (7.1.0)
- Version 2020.2 (6.2.0)
- Version 2020.1 (6.1.0)
- Version 2019.3
- Version 2019.2
- Version 2019.1
- Version 2018.3
- Version 2018.2
- Version 2018.1
- Version 2017.4
- Version 2017.3
- Version 2017.2
- Version 2017.1
- Version 2.6
- Version 2.5
- Version 2.4
- Version 2.3
- Version 2.2
Lib Sparse - Shader API
lib-sparse.glsl
This file provides useful functions to ensure sparse textures sampling correctness (ARB_sparse_texture). Allows to sample only part of textures really present in video memory.
Public Functions: getSparseCoord getSparseCoordLod0 textureSparseQueryLod textureSparse
Public Structures: SamplerSparse SparseCoord
The FEATURE_SPARSE_TEXTURE macro is defined only if the sparse virtual texture extension is enabled.
If enabled, process additional texture lookup checks to climb up mipmap pyramid if texels are missing.
#ifdef FEATURE_SPARSE_TEXTURE //: param auto material_lod_check_needed uniform bool material_lod_check_needed = false; //: param auto material_lod_mask uniform usampler2D material_lod_mask; #endif // FEATURE_SPARSE_TEXTURE //: param auto uvtile_reference_sampler uniform sampler2D uvtile_reference_sampler; //: param auto uvtile_size uniform vec2 uvtile_size; //: param auto uvtile_inverse_size uniform vec2 uvtile_inverse_size; //: param auto uvtile_lod_bias uniform float uvtile_lod_bias;
Sampler and sparse texture information structure
Used to query all sampler related uniforms with a single auto binding
struct SamplerSparse { sampler2D tex; vec4 size; // width, height, 1/width, 1/height bool is_set; // a boolean indicating whether the texture is in the texture set or not uvec3 lod_mask_select; // masking operations description allowing to retrieve loaded mipmaps information };
Sparse sampling coordinates
Store the UV coordinates & material-wise sparse LoD mask
struct SparseCoord { vec2 tex_coord; vec2 dfdx; vec2 dfdy; float lod; uint material_lod_mask; }; #if defined(SHADER_FRAGMENT)
Build texture coordinates structure used by textureSparse() sampling function (must be called from fragment shader)
Example: SparseCoord uv1coord = getSparseCoord(inputs.multi_tex_coord[1]);
SparseCoord getSparseCoord(vec2 tex_coord) { SparseCoord res; res.tex_coord = tex_coord; res.dfdx = dFdx(tex_coord); res.dfdy = dFdy(tex_coord); #ifdef FEATURE_SPARSE_TEXTURE res.material_lod_mask = material_lod_check_needed ? textureLod(material_lod_mask,tex_coord,0.0).r : 0u; res.lod = getLodFromReferenceSampler(tex_coord); #endif // FEATURE_SPARSE_TEXTURE return res; } #endif
Build texture coordinates structure used by textureSparse() sampling function Base level sampling version (can be used if outside fragment shader)
SparseCoord getSparseCoordLod0(vec2 tex_coord) { SparseCoord res; res.tex_coord = tex_coord; res.dfdx = vec2(0.0); res.dfdy = vec2(0.0); #ifdef FEATURE_SPARSE_TEXTURE res.material_lod_mask = material_lod_check_needed ? textureLod(material_lod_mask,tex_coord,0.0).r : 0u; res.lod = 0.0; #endif // FEATURE_SPARSE_TEXTURE return res; } #if defined(SHADER_FRAGMENT)
Compute the level-of-detail that would be used to sample from a sparse texture
Climb up mipmap pyramid if texels are missing Returns LoD BEFORE LoD bias applied
float textureSparseQueryLod(SamplerSparse sampler, SparseCoord coord) { #ifdef FEATURE_SPARSE_TEXTURE float lodfix = coord.lod; if (material_lod_check_needed) { lodfix = getFixedSparseLod(getTextureLodMask(sampler.lod_mask_select, coord.material_lod_mask), lodfix); } return lodfix-uvtile_lod_bias; #else // FEATURE_SPARSE_TEXTURE return textureQueryLod(sampler.tex, coord.tex_coord).y-uvtile_lod_bias; #endif // FEATURE_SPARSE_TEXTURE } #endif // SHADER_FRAGMENT
Compute the derivatives that would be used to sample from a sparse texture
Climb up mipmap pyramid if texels are missing
void textureSparseQueryGrad(out vec2 dfdx, out vec2 dfdy, SamplerSparse sampler, SparseCoord coord) { #ifdef FEATURE_SPARSE_TEXTURE if (material_lod_check_needed) { float lodfix = getFixedSparseLod(getTextureLodMask(sampler.lod_mask_select, coord.material_lod_mask), coord.lod); if (coord.lod!=lodfix) { // Fix dfdx dfdy, take account offset, no more anisotropy vec2 ddfix = exp2(lodfix-uvtile_lod_bias) * uvtile_inverse_size; dfdx = vec2(ddfix.x,0.0); dfdy = vec2(0.0,ddfix.y); return; } } #endif // FEATURE_SPARSE_TEXTURE dfdx = coord.dfdx; dfdy = coord.dfdy; }
Performs a texture lookup on a sparse texture, go up the mipmap levels if necessary
This function replaces the standard texture(sampler2D, vec2) to retrieve texels from a sparse texture
vec4 textureSparse(SamplerSparse sampler, SparseCoord coord) { vec2 dfdx,dfdy; textureSparseQueryGrad(dfdx, dfdy, sampler, coord); return textureGrad(sampler.tex, coord.tex_coord, dfdx, dfdy); }
Given a texture, performs an optimized multiple texture lookups with small offsets
We are providing alternatives versions of this helper for up to N=4
void textureSparseOffsets(SamplerSparse sampler, SparseCoord coord, vec2 offsets[N], out vec4 results[N]) { vec2 dfdx,dfdy; textureSparseQueryGrad(dfdx, dfdy, sampler, coord); for(int i = 0; i < N; ++i) { results[i] = textureGrad(sampler.tex, coord.tex_coord + offsets[i], dfdx, dfdy); } }