diff --git a/tutorials/rendering/texture_streaming.rst b/tutorials/rendering/texture_streaming.rst index 248e28af372..2b07b38d240 100644 --- a/tutorials/rendering/texture_streaming.rst +++ b/tutorials/rendering/texture_streaming.rst @@ -300,11 +300,29 @@ Custom shaders -------------- Feedback is generated automatically for spatial shaders that use ``UV``, which -covers :ref:`class_BaseMaterial3D` and most custom shaders. If a -shader samples its streamed textures with coordinates that are not ``UV`` (for -instance, a triplanar or world-space projection), write the coordinates the -textures are actually sampled with to the ``STREAMING_UV`` built-in in -``fragment()``, and the streaming system will use those instead: +covers :ref:`class_BaseMaterial3D` and most custom shaders. The level requested +for each pixel is based on how fast ``UV`` changes between neighboring pixels. + +Shaders that sample streamed textures some other way (such as a world-space or +triplanar projection) produce no feedback; the textures will decay to +**Max LOD**. To avoid this, these shaders can write the level they need to ``STREAMING_LOD`` in +``fragment()`` instead. Once a shader writes ``STREAMING_LOD``, ``UV`` is no +longer used for its feedback. + +Choosing the level +~~~~~~~~~~~~~~~~~~ + +``STREAMING_LOD`` is the LOD level for a 4096×4096 texture. Smaller textures +subtract one level each time their size halves; for example, a value of ``3.0`` +loads level 2 of a 2048×2048 texture. The value is clamped between ``-32.0`` +and ``32.0``. + +A material reports one level for all of its streamed textures, so those +textures should share the same UV scale. The highest quality level written by +any pixel is used, and pixels that do not write ``STREAMING_LOD`` are ignored. + +To compute the level from other coordinates the same way the streaming system +does for ``UV``, take the screen-space derivatives of those coordinates: .. code-block:: glsl @@ -314,15 +332,18 @@ textures are actually sampled with to the ``STREAMING_UV`` built-in in uniform float scale = 0.25; void fragment() { - vec2 world_uv = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xz * scale; + vec2 world_uv = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xz * scale; ALBEDO = texture(albedo_texture, world_uv).rgb; - // Tell the streaming system which coordinates were sampled. - STREAMING_UV = world_uv; - } -Without this, a shader that only uses non-``UV`` coordinates produces no -feedback, and its textures decay to **Max LOD**. Shaders that write no UVs at -all and do not use streamed textures are unaffected. + vec2 uv_ddx = dFdx(world_uv); + vec2 uv_ddy = dFdy(world_uv); + float px_sq = dot(uv_ddx, uv_ddx); + float py_sq = dot(uv_ddy, uv_ddy); + // Use the shorter axis, limited to 16× anisotropy, like the streaming + // system does for UV. 0.5 * log2() of a squared length is log2() of the length, + // and 12.0 converts it to levels of the 4096×4096 reference texture. + STREAMING_LOD = 0.5 * log2(max(min(px_sq, py_sq), max(px_sq, py_sq) / 256.0)) + 12.0; + } Limitations ----------- @@ -342,6 +363,10 @@ Limitations even while a lower mipmap is resident, and :ref:`Texture2D.get_image() ` always reads the full-resolution image from disk. +- In shaders, streamed textures only contain the mipmaps currently loaded: + ``textureSize()`` returns the loaded size, and the level passed to + ``textureLod()`` counts from the finest loaded mipmap. The return values + of both functions will change as the texture LOD level changes. - Streaming reads from the imported ``.stex`` files during gameplay, so it trades VRAM for disk I/O. On storage with high latency (such as mechanical hard drives), expect textures to take longer to sharpen. diff --git a/tutorials/shaders/shader_reference/spatial_shader.rst b/tutorials/shaders/shader_reference/spatial_shader.rst index 29942789d2e..1b0c2cc55e3 100644 --- a/tutorials/shaders/shader_reference/spatial_shader.rst +++ b/tutorials/shaders/shader_reference/spatial_shader.rst @@ -531,10 +531,10 @@ these properties, and if you don't write to them, Godot will optimize away the c | out vec4 **IRRADIANCE** | If written to on any branch, blends environment map irradiance with ``IRRADIANCE.rgb`` based on | | | ``IRRADIANCE.a``. | +----------------------------------------+--------------------------------------------------------------------------------------------------+ -| out vec2 **STREAMING_UV** | The texture coordinates used to sample streamed textures, for | -| | :ref:`texture streaming ` feedback. Only needs to be written to if the | -| | shader samples streamed textures with coordinates other than ``UV``, such as a triplanar or | -| | world-space projection. Defaults to ``UV``. | +| out float **STREAMING_LOD** | If written to, sets the LOD level requested for | +| | :ref:`texture streaming ` instead of computing it from ``UV``. Expressed | +| | for a 4096×4096 texture. Use when sampling streamed textures with coordinates other than | +| | ``UV``, such as a triplanar or world-space projection. | +----------------------------------------+--------------------------------------------------------------------------------------------------+ .. note::