# The limit of "for" in shader?

This topic is 4421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all: I try to do computation in shader. First, I do a FBO object and attatch 4 color + 1 depth texture my sizes are 725 x 401 Second, I do a simple loop in fragment . int b =0; for(int i = 0 ; i<n ;i++) b++; gl_FragColor = vec4(b,b,b,1.0); if n is 280 , I got 24 n is 270 14 n is 260 4 n 250 250 is right! Everyone konw what's wrong with me? Please tell me some tips or relative site. Thank you very much!

##### Share on other sites
Where are you taking the output value from?
It obviously clamps the value to 256 and wraps up back again, 280-256=24,270-256=14 etc..
Either way, unless you're using floating point target, you shouldn't be storing that big numbers!

ch.

##### Share on other sites
Quote:
 Original post by ARB1130gl_FragColor = vec4(b,b,b,1.0);

try
gl_FragColor = frac(vec4(b,b,b,1.0)*vec4(1,256,65536,1)); 
 0 
 Share this post Link to post Share on other sites 
 
 
 ARB1130    122 ARB1130 Member 122 Posted September 14, 2006 Quote:Original post by rapsoinstead ofQuote:Original post by ARB1130gl_FragColor = vec4(b,b,b,1.0);trygl_FragColor = frac(vec4(b,b,b,1.0)*vec4(1,256,65536,1));Thanks your responds!Your approach is packing a float into a texture.I got 0 ^_^"And I got the values by glReadPixels. 0 Share this post Link to post Share on other sites ARB1130    122 ARB1130 Member 122 Posted September 14, 2006 Quote:Original post by christian hWhere are you taking the output value from?It obviously clamps the value to 256 and wraps up back again, 280-256=24,270-256=14 etc..Either way, unless you're using floating point target, you shouldn't be storing that big numbers!ch.Thanks to your idea! ^_^ 256Yes, I use the GL_RGBA32F_ARB and GL_TEXTURE_RECTANGLE_NV 0 Share this post Link to post Share on other sites ARB1130    122 ARB1130 Member 122 Posted September 14, 2006 Quote:Original post by ARB1130Hi all:I try to do computation in shader.First, I do a FBO object and attatch 4 color + 1 depth texture my sizes are 725 x 401Second, I do a simple loop in fragment .int b =0; for(int i = 0 ; i<n ;i++) b++; gl_FragColor = vec4(b,b,b,1.0);if n is 280 , I got 24 n is 270 14 n is 260 4 n 250 250 is right! Everyone konw what's wrong with me?Please tell me some tips or relative site.Thank you very much!I found a interesting phenomenon.I used 2 "for loop" and I can get the right value.ex:int b =0; for(int i =0;i<20;i++) for(int j =0;j<20;j++) b++;I still got the values by glReadPixels ->4e+002.But the loop became bigger and bigger.Something wrong!EX:250*250I got 6.25e+004 and it is right!251*251 is 2.173e+004252*252 2.173e+004252*252 2.173e+004I don't know whats happened.Please tell me ! Thanks! 0 Share this post Link to post Share on other sites ARB1130    122 ARB1130 Member 122 Posted September 14, 2006 Quote:Original post by ARB1130Hi all:I try to do computation in shader.First, I do a FBO object and attatch 4 color + 1 depth texture my sizes are 725 x 401Second, I do a simple loop in fragment .int b =0; for(int i = 0 ; i<n ;i++) b++; gl_FragColor = vec4(b,b,b,1.0);if n is 280 , I got 24 n is 270 14 n is 260 4 n 250 250 is right! Everyone konw what's wrong with me?Please tell me some tips or relative site.Thank you very much!I found a strange problem!In one "for loop",the max value is 256In two "for loop",the max value >256Does somebody know the problem?Thanks to responds! ^_^ 0 Share this post Link to post Share on other sites 
 Sign in to follow this   Followers 0 
 Go To Topic Listing Graphics and GPU Programming Advertisement 
 Advertisement Popular Tags 2D 3D Advice Algorithm C# C++ Concept Design DX11 Education GameMaker Gameplay General HTML5 Java Javascript Learning Mobile Music OpenGL PC SFX Unity Unreal Visual Studio Popular Contributors Week Month Year All Time 1 Rutin 44 2 lawnjelly 31 3 Hodgman 26 4 jbadams 25 5 Septopus 21 Show More Advertisement Popular Now 11 Being Hired as Co-Writer for an Indie Studio - Question about Fixed + Revenue Share By Raze23Started 5 hours ago 9 Requesting a code review By KuurdeStarted Yesterday at 07:49 AM 12 Help: Solo programmer: What to do about Assets? By PlotnusStarted Saturday at 07:00 AM 10 Allow to slide rectangle avoiding corner collision By AsierStarted Friday at 10:26 PM 13 3D artist looking for experience and motivation By supermikhailStarted Friday at 12:28 PM Forum Statistics Total Topics 632984 Total Posts 3009712 Who's Online (See full list) There are no registered users currently online GameDev.net GameDev.net Articles GameDev.net Event Coverage GameDev.net Forums GameDev.net Blogs GameDev.net Gallery GameDev.net News GameDev.net Projects GDNet Chat All Activity Search In Everywhere This Forum This Topic More options... Find results that contain... All of my search term words Any of my search term words Find results in... Content titles and body Content titles only Home Forums Programming Graphics and GPU Programming The limit of "for" in shader? 
 
 
 × Existing user? Sign In Sign Up Browse Back Articles & Tutorials Back All Categories Audio Business Game Design Industry Programming Visual Arts Columns Back GameDev Unboxed Event Coverage Back All Events Game Developers Conference Power Up Digital Games Conference GameDev.Market Links News Podcasts Back All Podcasts Game Dev Loadout Archive Community Back Beginners Back Beginners Group Beginners Forum Beginners Resources Blogs Calendar Chat Forums Back All Forums Audio Business Game Design Programming Visual Arts Community GameDev Challenges Affiliates Topical Workshops Gallery Groups Back For Beginners GameDev Challenges All Groups Projects Back All Projects Games Game Assets Game Mods Developer Tools Store Forums Back All Forums For Beginners Audio Back Music and Sound FX Games Career Development Business Back Games Career Development Production and Management Games Business and Law Game Design Back Game Design and Theory Writing for Games Programming Back Artificial Intelligence Engines and Middleware General and Gameplay Programming Graphics and GPU Programming Math and Physics Networking and Multiplayer Visual Arts Back 2D and 3D Art Critique and Feedback Community Back GameDev Challenges GDNet Lounge GDNet Comments, Suggestions, and Ideas Coding Horrors Your Announcements Hobby Project Classifieds Indie Showcase Affiliates Back NeHe Productions AngelCode Topical Workshops Careers Back Contractors Hobby Projects Game Jobs Back Browse on GameDev.Jobs Post a Job Members Back Subscriptions Chat Guidelines Leaderboard Online Users Awards Search Back All Activity My Activity Streams Back Latest Topics Featured Blogs Search var ipsDebug = false; var CKEDITOR_BASEPATH = '//www.gamedev.net/applications/core/interface/ckeditor/ckeditor/'; var ipsSettings = { cookie_path: "/", cookie_prefix: "ips4_", cookie_ssl: true, upload_imgURL: "", message_imgURL: "", notification_imgURL: "", baseURL: "//www.gamedev.net/", jsURL: "//www.gamedev.net/applications/core/interface/js/js.php", csrfKey: "59e6215ac2819b14669692a84e624c7b", antiCache: "14de000c45", disableNotificationSounds: false, useCompiledFiles: true, links_external: true, memberID: 0, analyticsProvider: "ga", viewProfiles: true, mapProvider: 'google', mapApiKey: "AIzaSyAeT7tk3vnWWmbgVISkLpbhkQvekG19rHM", }; ips.setSetting( 'date_format', jQuery.parseJSON('"mm\/dd\/yy"') ); ips.setSetting( 'date_first_day', jQuery.parseJSON('0') ); ips.setSetting( 'remote_image_proxy', jQuery.parseJSON('1') ); ips.setSetting( 'ipb_url_filter_option', jQuery.parseJSON('"none"') ); ips.setSetting( 'url_filter_any_action', jQuery.parseJSON('"allow"') ); ips.setSetting( 'bypass_profanity', jQuery.parseJSON('0') ); ips.setSetting( 'emoji_style', jQuery.parseJSON('"native"') ); ips.setSetting( 'emoji_shortcodes', jQuery.parseJSON('"1"') ); ips.setSetting( 'emoji_ascii', jQuery.parseJSON('"1"') ); ips.setSetting( 'emoji_cache', jQuery.parseJSON('"1"') ); ips.setSetting( 'quickSearchDefault', jQuery.parseJSON('"all"') ); ips.setSetting( 'quickSearchMinimum', jQuery.parseJSON('3') ); ips.setSetting( 'quickSearchShowAdv', jQuery.parseJSON('true') ); ips.setSetting( 'quickSearchIn', jQuery.parseJSON('"title"') ); { "@context": "http://schema.org", "@type": "DiscussionForumPosting", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/", "discussionUrl": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/", "name": "The limit of \"for\" in shader?", "headline": "The limit of \"for\" in shader?", "text": "Hi all:\n\nI try to do computation in shader.\n\nFirst, I do a FBO object and attatch 4 color + 1 depth texture\n\n my sizes are 725 x 401\n\nSecond, I do a simple loop in fragment .\n\nint b =0;\n\t\nfor(int i = 0 ; i\u0026lt;n ;i++)\n\t\tb++;\n\t\t \ngl_FragColor = vec4(b,b,b,1.0);\n\n\nif n is 280 , I got 24\n\n n is 270 14\n\n n is 260 4\n\n n 250 250 is right! \n\nEveryone konw what\u0027s wrong with me?\n\nPlease tell me some tips or relative site.\n\nThank you very much!", "dateCreated": "2006-09-14T02:09:14+0000", "datePublished": "2006-09-14T02:09:14+0000", "pageStart": 1, "pageEnd": 1, "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "author": { "@type": "Person", "name": "ARB1130", "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "url": "https://www.gamedev.net/profile/101582-arb1130/" }, "interactionStatistic": [ { "@type": "InteractionCounter", "interactionType": "http://schema.org/ViewAction", "userInteractionCount": 646 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/CommentAction", "userInteractionCount": 6 }, { "@type": "InteractionCounter", "interactionType": "http://schema.org/FollowAction", "userInteractionCount": 23 } ], "comment": [ { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=414579", "author": { "@type": "Person", "name": "ARB1130", "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "url": "https://www.gamedev.net/profile/101582-arb1130/" }, "dateCreated": "2006-09-14T02:09:14+0000", "text": "Hi all:\n\nI try to do computation in shader.\n\nFirst, I do a FBO object and attatch 4 color + 1 depth texture\n\n my sizes are 725 x 401\n\nSecond, I do a simple loop in fragment .\n\nint b =0;\n\t\nfor(int i = 0 ; i\u0026lt;n ;i++)\n\t\tb++;\n\t\t \ngl_FragColor = vec4(b,b,b,1.0);\n\n\nif n is 280 , I got 24\n\n n is 270 14\n\n n is 260 4\n\n n 250 250 is right! \n\nEveryone konw what\u0027s wrong with me?\n\nPlease tell me some tips or relative site.\n\nThank you very much!", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=3760862", "author": { "@type": "Person", "name": "christian h", "image": "https://secure.gravatar.com/avatar/21b6caa3e465ebf8963c0eb04192901b?d=https://www.gamedev.net/uploads/monthly_2017_08/C.png.9d85848426caa01d5d5fde47e94cc6c1.png", "url": "https://www.gamedev.net/profile/69816-christian-h/" }, "dateCreated": "2006-09-14T03:12:33+0000", "text": "Where are you taking the output value from?It obviously clamps the value to 256 and wraps up back again, 280-256=24,270-256=14 etc..Either way, unless you\u0027re using floating point target, you shouldn\u0027t be storing that big numbers!ch.", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=3760876", "author": { "@type": "Person", "name": "rapso", "image": "https://secure.gravatar.com/avatar/28a7b591e211d5b02d0891b5051d31b2?d=https://www.gamedev.net/uploads/monthly_2017_08/R.png.c6e545c455760f1a87896c147e18ef27.png", "url": "https://www.gamedev.net/profile/10159-rapso/" }, "dateCreated": "2006-09-14T03:34:56+0000", "text": "instead of\ntrygl_FragColor = frac(vec4(b,b,b,1.0)*vec4(1,256,65536,1));\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=3760958", "author": { "@type": "Person", "name": "ARB1130", "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "url": "https://www.gamedev.net/profile/101582-arb1130/" }, "dateCreated": "2006-09-14T06:33:41+0000", "text": "Thanks your responds!Your approach is packing a float into a texture.I got 0 ^_^\"And I got the values by glReadPixels. \n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=3760964", "author": { "@type": "Person", "name": "ARB1130", "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "url": "https://www.gamedev.net/profile/101582-arb1130/" }, "dateCreated": "2006-09-14T06:40:43+0000", "text": "Thanks to your idea! ^_^ 256Yes, I use the GL_RGBA32F_ARB and GL_TEXTURE_RECTANGLE_NV\n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=3760973", "author": { "@type": "Person", "name": "ARB1130", "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "url": "https://www.gamedev.net/profile/101582-arb1130/" }, "dateCreated": "2006-09-14T06:55:56+0000", "text": "I found a interesting phenomenon.I used 2 \"for loop\" and I can get the right value.ex:int b =0; for(int i =0;i\u0026lt;20;i++) for(int j =0;j\u0026lt;20;j++) b++;I still got the values by glReadPixels -\u0026gt;4e+002.But the loop became bigger and bigger.Something wrong!EX:250*250I got 6.25e+004 and it is right!251*251 is 2.173e+004252*252 2.173e+004252*252 2.173e+004I don\u0027t know whats happened.Please tell me ! Thanks! \n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" }, { "@type": "Comment", "url": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/?do=findComment\u0026comment=3761893", "author": { "@type": "Person", "name": "ARB1130", "image": "https://secure.gravatar.com/avatar/d0501e30a8107a517136ce726aeb2c5d?d=https://www.gamedev.net/uploads/monthly_2017_08/A.png.748ce1425a46e1b3dbbde749950cdab5.png", "url": "https://www.gamedev.net/profile/101582-arb1130/" }, "dateCreated": "2006-09-14T23:52:13+0000", "text": "I found a strange problem!In one \"for loop\",the max value is 256In two \"for loop\",the max value \u0026gt;256Does somebody know the problem?Thanks to responds! ^_^ \n", "mainEntityOfPage": "https://www.gamedev.net/forums/topic/414579-the-limit--of-for-in-shader/" } ] } { "@context": "http://www.schema.org", "@type": "WebSite", "name": "GameDev.net", "url": "https://www.gamedev.net/", "potentialAction": { "type": "SearchAction", "query-input": "required name=query", "target": "https://www.gamedev.net/search/?q={query}" }, "inLanguage": [ { "@type": "Language", "name": "English (USA)", "alternateName": "en-US" } ] } { "@context": "http://www.schema.org", "@type": "Organization", "name": "GameDev.net", "url": "https://www.gamedev.net/", "address": { "@type": "PostalAddress", "streetAddress": "", "addressLocality": null, "addressRegion": null, "postalCode": null, "addressCountry": null } } { "@context": "http://schema.org", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": 1, "item": { "@id": "https://www.gamedev.net/forums/", "name": "Forums" } }, { "@type": "ListItem", "position": 2, "item": { "@id": "https://www.gamedev.net/forums/forum/4-programming/", "name": "Programming" } }, { "@type": "ListItem", "position": 3, "item": { "@id": "https://www.gamedev.net/forums/forum/5-graphics-and-gpu-programming/", "name": "Graphics and GPU Programming" } } ] } { "@context": "http://schema.org", "@type": "ContactPage", "url": "https://www.gamedev.net/contact/" } Important Information By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.   I accept We are the game development community. Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up! Sign me up!