Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 N1nja · May 27, 2010 at 05:03 PM · shaderfor-loopcgperlin

Cg and "for" statements, what's the deal?

I have this code:

float cnoise(float3 p)
{
    float noise = 0;
    float freq = _Frequency;
    float amp = _Amplitude;
    for (int i = 0; i < 2; i++)
    {
        noise += inoise(p * freq) * amp;
        freq  *= 2;
        amp   *= _Persistence;
    }
    return clamp(abs(noise), 0, 1);
}

Which works, don't get me wrong, though where you see the integer 2, in the expression i < 2; you should see _Octaves, or i < _Octaves.

It seems that if I add that variables, I get problems where the "for" statement is unsupported, and can not be unrolled.

Well, that's weird indeed.. Any insight on this problem, and how I might fix it? =]

Comment
Add comment · Show 1
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 reefwirrax · Sep 30, 2013 at 05:12 PM 0
Share

good question. this works:

 // fractal sum, range -1.0 - 1.0
             float fBm(float3 p, int octaves)
             {
                 float freq = _Frequency, amp = 0.5;
                 float sum = 0;    
                 for(int i = 0; i < octaves; i++) 
                 {
                     sum += inoise(p * freq) * amp;
                     freq *= _Lacunarity;
                     amp *= _Gain;
                 }
                 return sum;
             }
             

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Murcho · May 28, 2010 at 12:43 AM

Without seeing the entire shader I can't be certain, but I have a feeling it may have something to do with Instruction limits. Here is a list of the different shader models under Direct X and the limits within a shader.

Basically, if you're adding a variable to the for loop limit, the for loop could cause the shader to go over the available amount of instructions if set to a high value, however when set to 2 it knows how many instructions will be executed. You may be able to get around the compilation error by placing a clamp on the variable or something like that.

Comment
Add comment · Show 1 · 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 Cyclops · May 28, 2010 at 02:16 PM 0
Share

Interesting. I've never worked with shaders, didn't realize they had limitations like that. Guess it makes sense, given they run on every pixel.

avatar image
0

Answer by reefwirrax · Sep 30, 2013 at 05:29 PM

Proper looping in shaders (dynamic branching) is only possible with Shader Model 4.0 (OpenGL 3 or DirectX 10). In previous shader models, things like for loops are "unrolled": that is, the compiler looks at the loop, sees how many times it will run, and actually copies out - or unrolls - the whole loop so it's as if you wrote them down one at a time. As for passing in an arbitrary number of textures... I think you'd at least have to define a maximum number, and then just use as many as you need. But yeah, you need a SM4-capable card to even get off the ground with dynamic branching. curses his 7300gt

Comment
Add comment · Show 1 · 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 reefwirrax · Sep 30, 2013 at 05:37 PM 0
Share

You need to declare octaves as a static int for this to work. When unrolling a loop, the iteration count has to be a compile-time constant; without the static on there, the compiler has to assume you might change the value of the parameter at runtime.

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

1 Person is following this question.

avatar image

Related Questions

Profile does not support "for" statements and "for" could not be unrolled. 1 Answer

How to force the compilation of a shader in Unity? 5 Answers

Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime? 0 Answers

How to use array in cg shader 1 Answer

Problem with custom shader (ShaderLab + Cg) 1 Answer


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