• 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 jeffpk · Jun 22, 2012 at 10:32 PM · flashshader writing

How to reduce temporary register use in shader?

Okay, for you cg shader gurus out there

I am trying to write a simple unlit transparent shader that supports a clipping rectangle. This is my code...

Shader "Custom/ClippingUnlitTransparent" {

Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _cliprect ("Clip Rectangle", Vector) = (-1,-1,-1,-1) } SubShader { Tags { "RenderType"="Opaque" } LOD 200
CGPROGRAM #pragma surface surf Unlit #include "UnityCG.cginc"
half4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten) { half4 c; c.rgb = s.Albedo; c.a = s.Alpha; return c; } sampler2D _MainTex; float4 _cliprect;
struct Input { float2 uv_MainTex; float4 screenPos; }; void surf (Input IN, inout SurfaceOutput o) { float2 screenPosition = IN.screenPos.xy / IN.screenPos.w; screenPosition.xy = ((screenPosition.xy*0.5)+0.5) * _ScreenParams.xy; if ((_cliprect[0]<0) _cliprect[0]=0; if (_cliprect[1]<0) _cliprect[1]=0; if (_cliprect[2]<0) _cliprect[2] = _ScreenParams.x; if (_cliprect[3]<0) _cliprect[3]= _ScreenParams.y; if (screenPosition.x>=_cliprect[0])&& (screenPosition.x<=_cliprect[2])&& (screenPosition.y>=_cliprect[1])&& (screenPosition.y<=_cliprect[3])){ half4 c = tex2D (_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; } else{ o.Albedo = (0,0,0); o.Alpha = 0; } } ENDCG } FallBack "Diffuse" } It compiled fine until I put the final if check in

if (screenPosition.x>=_cliprect[0])&&

(screenPosition.x<=_cliprect[2])&& (screenPosition.y>=_cliprect[1])&& (screenPosition.y<=_cliprect[3])){ Now it gives me the error "Not enough registers, needs 9 (compiling for flash) at line 11" What can I do to fix this?
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 jeffpk · Jun 22, 2012 at 11:28 PM 0
Share

I cut out the if that sets the rect and ist happier now. will just have to be less idiot proof I guess

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 23, 2012 at 12:34 AM

The GPU works like is a DSP (Digital signal processor) in a pipeline mode. It's designed to perform the same actions on a huge stream of data. Branches are a bad idea in a shader. Try to avoid them. It's way better to transform your "condition" into a value that can be computed and used as multiplier.

Those:

 if ((_cliprect[0]<0) _cliprect[0]=0;
 if (_cliprect[1]<0) _cliprect[1]=0;
 if (_cliprect[2]<0) _cliprect[2] = _ScreenParams.x;
 if (_cliprect[3]<0) _cliprect[3]= _ScreenParams.y;

really should be done by your program and not by the shader for every pixel!! You should take care of valid shader parameters from outside.

Comment
Add comment · Show 2 · 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 jeffpk · Jun 23, 2012 at 04:03 AM 0
Share

I did remove that and it solved the register issue. Just goes contrary to 25 years of defensive program$$anonymous$$g instinct ;)

avatar image Bunny83 · Jun 23, 2012 at 11:18 AM 0
Share

But shaders work way different to normal x86 code ;) I'm not a shader expert at all, but the basic concept of a CPU and GPU are very different since they have different targets.

A pipeline processor has a fix pipeline of operations and the data is pushed through the pipeline and get processed. A GPU is optimised for 32-bit floating point arithmetic. It's not a logic processor.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Linearising the inversion of camera depth effect 1 Answer

Possible for a shader to have two decal slots? 2 Answers

Shadow distance rendering in a custom shader 1 Answer

How to calculate UV coordinates of the outer edge of a texture mask? 0 Answers

How to make a plane to sphere using vertex displacement shader graph, with respect to the camera position? 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