texture_size
Queries the dimensions of a texture mip level.
Test texture_size in a live shader
Open the playground, start from a visual preset, and wire texture_size into the fragment stage to see how it behaves with real values.
Open PlaygroundThe texture_size function returns the integer dimensions of a texture at a specific mip level. Use it when shader code needs texel-sized offsets, pixel-perfect addressing, or bounds-aware texture access.
Live Demo
Signature
texture_size :: (texture2D tex, int mip = 0) -> int2 {...}
texture_size :: (texture3D tex, int mip = 0) -> int3 {...}
texture_size :: (textureCube tex, int mip = 0) -> int2 {...}
The mip argument is optional and defaults to 0, so texture_size(resources.albedo) returns the base level's size.
Parameters
| Parameter | Type | Description |
|---|---|---|
tex | texture* | Texture to query |
mip | int | Mip level to query (optional, default 0) |
Return Value
Returns the width, height, and depth for the requested mip level. 2D and cube textures return int2; 3D textures return int3.
Known issue: 3D and array textures
The current compiler returns int2 for every texture type, so a texture3D's depth and a texture2DArray's layer count can't be queried yet. Until this is fixed, read the result into an int2. Tracked in BWSL#204.
Example
int2 size = texture_size(resources.albedoTexture, 0);
float2 texel = float2(1.0 / float(size.x), 1.0 / float(size.y));
float4 center = sample(resources.albedoTexture, resources.albedoSampler, input.uv);
float4 right = sample(resources.albedoTexture, resources.albedoSampler, input.uv + float2(texel.x, 0.0));Mip-Aware
Pass the mip level you intend to read. Higher mip levels are smaller, so their texel size is larger.
Compiled Output
When compiled to GLSL:
textureSize(tex, mip)
When compiled to HLSL:
tex.GetDimensions(...)
When compiled to Metal:
tex.get_width(mip), tex.get_height(mip)
See Also
- sample - Sample a texture
- load - Load an exact texel
- sample_lod - Sample at an explicit mip level