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 nug700 · Feb 09, 2017 at 02:28 AM · shadersprogramminghlsl

doubles and compute shaders

I have a compute shader I use to crunch the n-body gravity calculations for my project. when I use all floats in the shader, it runs fine and can process the gravity calculations of 10,000 objects in about 8 ms. However. I can't use floats because part the gravity equation ((G x mass1 x mass2) / d^2) can produce a number greater than what floats can hold with 2 sun sized masses. This leads me to need to use doubles for that part of the calculations. This wouldn't be a problem, except it seems to SEVERELY increase the time it takes to execute the shader for 8 ms to 130 ms. Any input is appreciated.

 [numthreads(256,1,1)]
 void GravityComp (uint3 id : SV_DispatchThreadID)
 {
     uint ind = id.x;
     float2 gravResult = float3(0, 0);
     for (uint i = 0; i < (uint)numAsteroids; i++) {
         if (ind == i)
             continue;
         float distance = Distance(dataIn[ind].xy, dataIn[i].xy);
         double G = (double)0.0000000000667408;
         double m1 = (double)dataIn[ind].z; // mass
         double m2 = (double)dataIn[i].z; // mass
         double newt = (G * m1 * m2) / (double)pow(distance, 2);
         float acc = (float)(newt / m1);
         float2 dir = -normalize(dataIn[ind].xy - dataIn[i].xy);
         float2 grav = dir.xy * acc;
         gravResult.xy = gravResult.xy + grav.xy;
     }
 
     dataOut[ind].xy = gravResult.xy;
 }
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

1 Reply

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

Answer by TheyLeftMe4Dead · Feb 09, 2017 at 02:46 AM

One solution to your problem would be to change your units. Ex : instead of 12" , 1'. Another idea is to store the end result in scientific notation. Ex: 3.16 x 10^8 . To do this, you could get the result as a double, split it into the 2 parts of your scientific notation (two variables), then cast it back into a float.

You could also cut the size of everything in your scene by half. (I dont know if that messes wih your formula)

Does your number need to be exact? If not, You could consider "trimming" your numbers down in some way.

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 nug700 · Feb 09, 2017 at 05:25 AM 0
Share

Converting my variables to scientific notation, and adding/subtracting the exponents and multiplying/dividing the mantissas worked (don't need to convert anything to doubles).

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

71 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

Related Questions

Multiple Cars not working 1 Answer

[Shader] Usage #ifdef DIRECTIONAL 0 Answers

Wavey Shaders, Where to start? 1 Answer

Replacing Shader At Runtime. 1 Answer

How to get Initial texture in Post-Processing Stack v2 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