Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tomix1024 · Jun 24, 2017 at 05:24 AM · shadertexture2dlod

textureQueryLod/CalculateLevelOfDetail in Fragment Shader

Hi,

I want to query the LOD, that would be used, when I sample a texture at a given location. I tried to use textureQueryLod directly with the OpenGL backend, however Unity fails to compile the shader.

Shader error in 'Custom/MySurfaceShader': undefined variable "textureQueryLod" at line 216 (on d3d9)

A solution working with either OpenGL or DX11 would be fine at this point.

Thanks, Tom

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by tomix1024 · Sep 11, 2017 at 03:09 PM

Adding the following definition in the CG shader code enabled me to query the LOD:

 #if defined(SHADER_API_D3D11) || defined(SHADER_API_GLCORE)
     #define UNITY_LOD_TEX2D(tex,coord) tex.CalculateLevelOfDetail (sampler##tex,coord)
 #else
     // Just match the type i.e. define as a float value
     #define UNITY_LOD_TEX2D(tex,coord) float(0)
 #endif

 // ...

 float lod = UNITY_LOD_TEX2D(_MainTex, uv);

Note that CalculateLevelOfDetail is a D3D function, but still works with the OpenGL backend here, while I had no success with the native textureQueryLod function.

However, if your intent is to use this LOD to sample from a texture again, I suggest that you use the SampleGrad method instead (a macro can be defined analogously). This preserves anisotropic filtering of the texture if it is enabled. The gradient of the uv coordinates in screen space can be computed using the built-in functions ddx(uv) and ddy(uv), respectively.

 #if defined(SHADER_API_D3D11) || defined(SHADER_API_GLCORE)
     #define UNITY_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) tex.SampleGrad (sampler##tex,coord,dx,dy)
 #else
     // Just match the type i.e. define as a float4 vector
     #define UNITY_SAMPLE_TEX2D_GRAD(tex,coord,dx,dy) float4(0,0,0,0)
 #endif

 // ...

 float4 color = UNITY_SAMPLE_TEX2D_GRAD(_MainTex, uv, ddx(uv), ddy(uv));
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by fox3fire · Jul 05, 2018 at 06:23 AM

Thanks, your answer is freaking awesome!

I defined a macro to use the SampleGrad function in my vert/frag shader. It worked as expected. When I switched to surface shader with the same macro, it didn't compile and gave me an "unexpected (" error even with #pragma target 5.0.

After adding the #if that you suggested, everything is fine now. It seems like when translating surface shader to vert/frag shader, Unity doesn't check for #pragma target 5.0 but rely on SHADER_API_D3D11 and SHADER_API_GLCORE.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Use RenderTexture as ShaderResource in DX11 native plug-in 1 Answer

Shader LOD settings. Tied to quality settings? 1 Answer

Combining 2 black-white textures 2 Answers

Change the color of a black sprite?,How can i change the color of a black sprite? 1 Answer

Converting a RenderTexture to a Texture2D for use in a shader 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges