Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by Peter G · Mar 19, 2011 at 06:50 PM · shadercg

Why are texcoords (uvs) stored in float4?

Essentially what the title says. When writing CG programs, the Unity docs say to create the texcoords in a structure as a float4. What is stored in the zw components?

//example
struct v2f {
    float4 texcoord : TEXCOORD0;
};

when most of the time I just use the xy components.

EDIT: I just looked at the CG documentation page, and it looks like it stores texcoords as a float2 in every example that I looked at.

Comment
Add comment · Show 2
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 loopyllama · Mar 19, 2011 at 08:13 PM 0
Share

I have no answer other than "for nothing", can you say where it forces a float 4? why can't you just use a float 2? I am sure there is a reason, else you would not have asked...

avatar image Jessy · Mar 19, 2011 at 08:31 PM 0
Share

I was pretty sure they did this so you could use your 4x4 transformation matrices on them without a hassle. But that's just a theory.

2 Replies

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

Answer by Owen-Reynolds · Mar 19, 2011 at 08:18 PM

Graphics cards use float4's to store and manipulate everything. They literally don't have simple variables in their chips or have simple math circuits. You ask one to add 4+7, it gets confused, then prints, "oh, you mean (4,0,0,0) plus (7,0,0,0)!!."

For UV's the 3rd is the 3rd dimension (if you have 64 textures, all 64x64, you can think of the 3rd as picking/blending which textures to use.) The 4th is the scale factor (backwards: 2 is one half size.) They are always (0,1.)

But, the lookup command is generally tex2D(texture, i.texcoords.xy); so the last two aren't even used.

I personally use float2s for UVs, but a float2 is really a float4 with the last two slots taped over, and saves 0 time over using a float4. Most likely someone just typed float4 way back and there was never a reason to change it.

Comment
Add comment · Show 10 · 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 Peter G · Mar 20, 2011 at 02:46 AM 0
Share

So, are all numerical data types created "implicitly" as 4 component vectors?

avatar image Owen-Reynolds · Mar 20, 2011 at 03:12 AM 0
Share

If you look at the Terrain shader, the blend weights of the 1st four textures are packed into one float4 ins$$anonymous$$d of being sent over in four floats. One of the tricks is that you use float2 animals; with x for cats and y for dogs, to save space. You can say animals+=float2(1,5); to add 1 cat and 5 dogs, and it runs faster than cats+=1; dogs+=5;.

avatar image Jessy · Mar 20, 2011 at 03:21 AM 0
Share

What about, say, multiplications of two vectors? Is it really not faster to multiply a vec2 rather than a vec4?

avatar image Owen-Reynolds · Mar 20, 2011 at 04:50 PM 0
Share

The GPU math circuits are made by welding four "normal" math units side-by-side. They take two float4's as input, and give a float4 as output. In a normal CPU, float2's don't really exist -- it adds them using two normal adds, one after another, so of course adding 4 is slower. Think of a car that seats 4.

avatar image Peter G · Mar 20, 2011 at 06:26 PM 0
Share

Thanks for the detailed explanation.

Show more comments
avatar image
1

Answer by Peter G · Mar 20, 2011 at 06:30 PM

After conducting a little more experimentation, Unity will let you create the texcoords as a float2.

//this is also valid:
struct v2f {
    float2 texcoord : TEXCOORD0;
};

Owenpat does a nice job explaining why the Unity docs recommends creating them as a float4 instead though.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Cg Language incompatibility Desktop VS GLSL Android 0 Answers

Can anyone help me with reflective shader with fall off property? 0 Answers

Sprite deformation in shader? 0 Answers

unity_LightmapMatrix in 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