• 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
Question by UnityPlum · Jan 20, 2022 at 10:32 PM · mesh3dcompute shaderpreformance

Compute Shader performance is dropping from a single boolean.

Hello, I am trying to make a custom 3d renderer with projection and rasterization, but I have a question about computeShaders. I have a compute shader that sees if a pixel is in a triangle, then draw it. My compute shader code:

 [numthreads(256,4,1)]
 void CSMain (int3 id : SV_DispatchThreadID)
 {
     float2 coords = float2 (
         id.x,
         id.y
     );
 
     float minValue = 1;
 
     bool shadeIt = false;
 
     for (int i = 0; i < numTriangles; i+=3)
     {
         float3 v0 = vertices[triangles[i+0]];
         float3 v1 = vertices[triangles[i+1]];
         float3 v2 = vertices[triangles[i+2]];
 
         float pointInside = isInside (
             v0.x, v0.y,
             v1.x, v1.y,
             v2.x, v2.y,
             coords.x, coords.y
         );
 
         if (pointInside < 0.1f){
             shadeIt = true;
         }
     }
 
     Result[id.xy] = (shadeIt ? float4(2, 1, 0, 1) : float4(0, 1, 2, 1));
 }

My C# code that runs the shader:

 vertexBuffer = new ComputeBuffer (mesh.vertices.Length, sizeof(float)*3);
         triangleBuffer = new ComputeBuffer (mesh.triangles.Length, sizeof(int));
 
         Vector3[] projectedPoints = new Vector3[mesh.vertices.Length];
         int i = 0;
         foreach (Vector3 vec in mesh.vertices)
         {
             projectedPoints[i] = (ProjectPoint (vec) * height) + new Vector3 (width/2, height/2, 0);
             projectedPoints[i].z = vec.z;
             i ++;
         }
 
         vertexBuffer.SetData (projectedPoints);
         triangleBuffer.SetData (mesh.triangles);
 
         renderer.SetTexture (0, "Result", output);
         renderer.SetBuffer (0, "vertices", vertexBuffer);
         renderer.SetBuffer (0, "triangles", triangleBuffer);
 
         renderer.SetInt ("width", width);
         renderer.SetInt ("height", height);
 
         renderer.SetInt ("numVertices", mesh.vertices.Length);
         renderer.SetInt ("numTriangles", mesh.triangles.Length);
 
         renderer.SetVector ("cameraPosition", camera.position);
         renderer.SetVector ("cameraAngles", camera.angles * (3.141f/180));
 
         renderer.Dispatch (0, threadGroups.x, threadGroups.y, threadGroups.z);
 
         vertexBuffer.Dispose();
         triangleBuffer.Dispose();

And this works, we can plug in any mesh and it renders it: alt text But as you can see, 1 FPS. Now, if I take out this bit of code (Which is important lol): shadeIt = true; Then obviously, the mesh disappears, but the FPS goes up to 60: alt text

Why is this happening and how can I fix it?

speal.png (25.9 kB)
shmemal.png (28.0 kB)
Comment

People who like this

0 Show 0
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

Answer by Tex5000 · Jan 20, 2022 at 10:58 PM

Hello,

You're not supposed to put if conditions in shader code. It's run on the GPU which is here to crush numbers (maths). You are supposed to find a way to put this "if" as a mathematical operation.

I didn't write shader code in ages but something like this should work:

 int colorA = (1.1f - pointInside);
 int colorB = 1 - colorA;
 
 Result[id.xy] = colorA * float4(2, 1, 0, 1) + colorB * float4(0, 1, 2, 1));

Note that ? is also a conditional operator so don't use it.

Comment

People who like this

0 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 UnityPlum · Jan 20, 2022 at 11:12 PM 0
Share

Tried it, did not work. Thanks for trying though. It is a real problem and is messing with me.

avatar image

Answer by esgnn · Jan 21, 2022 at 06:59 AM

I think second if in line 31 is redundant. Create a float4 outside the loop

 float4 resultVar = float4(0, 1, 2, 1);

then,

 if (pointInside < 0.1f){
          resultVar = float4(2, 1, 0, 1);
     }  

then,

 Result[id.xy] = resultVar;

Also I'd definitely move calculations in c# between lines 5-11 to compute shader as well. I am assuming you are converting points to screen space.

One other thing, check if unrolling for loop increases performance. (I am not an expert on compute shaders) I'd also play with numthreads numbers to see if there is a performance change.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

185 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 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

How to make asset with many objects one mesh? (3d) 1 Answer

Render a custom 2D shape within a 3D environment 0 Answers

Game Object's Vector3 changing threw script without reason 1 Answer

Drawing a 3D Mesh (javascript) 1 Answer

Advance mesh alteration: Whittling wood 0 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