Shader:DX11 get texture dimensions inside a shader

Hi everybody,

I would love to have a float2 containing a texture’s mipmap0-dimensions. But I also rather not want to expose a shader property to achieve that.

DX11 Texture Objects provide the means to do that: Texture2D.GetDimensions()

But - well, the docs are pretty rare on that - how would I implement such a call in my fragment shader? Has anybody already done that successfully?

If I declared a texture/sampler pair via UNITY_DECLARE_TEX2D(name), how would I be able to access the corresponding Texture Object / call GetDimensions on it?

Thanks a lot in advance,

In order to access DX11 specific language features, you can wrap calls in #if defined(SHADER_API_D3D11). When using UNITY_DECLARE_TEX2D(name), a Texture2D variable with ‘name’ is created on DX11.

So to use Texture2D.GetDimensions(), the following code would work:

#if defined(SHADER_API_D3D11)
			uint width;
			uint height;
			uint levels;
			_MainTex.GetDimensions(0, width, height, levels);

To see the implementation of UNITY_DECLARE_TEX2D see HLSLSSupport.cginc