Updating buffers from within shaders in webgl - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

What is the logic of binding buffers in webgl? - Stack Overflow Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore Updatingbuffersfromwithinshadersinwebgl AskQuestion Asked 1year,10monthsago Modified 1year,10monthsago Viewed 521times 0 I'mabeginneratshadersandWebGLandI'vetakensomeshortcutstodevelopwhatIcurrentlyhave,sopleasebearwithme. IsthereawaytoupdateattributebufferdatawithintheGPUonly?BasicallywhatIwanttodoistosendinthreebufferst0,t1,t2intotheGPUrepresentingpointsandtheirpositionintime0,1,and2respectively.ThenIwishtoupdatetheirnewpositiontndependingonthepropertiesoft2,t1,andt0dependingonthevelocityofthepoints,turningangle,andsoon. MycurrentimplementationupdatesthepositionsinjavascriptandthencopiesthebuffersintoWebGLateverydraw.Butwhy?Thisseemsterriblyinefficienttome,andIdon'tseewhyIcouldn'tdoeverythingintheshadertoskipmovingdatafromCPU->GPUallthetime.Isthispossiblesomehow? Thisiscurrentvertexshaderwhichsetscoloronthepointdependingontheturndirectionandangleit'sturningat(tnisupdatedinJSatmbydebuggingfunctions): exportconstVsSource=` #defineM_PI3.1415926535897932384626433832795 attributevec4t0_pos; attributevec4t1_pos; attributevec4t2_pos; varyingvec4color; attributevec4r_texture; voidmain(){ floatdist=distance(t1_pos,t2_pos); vec4v=normalize(t1_pos-t0_pos); vec4u=normalize(t2_pos-t1_pos); floatangle=acos(dot(u,v)); floatintensinty=angle/M_PI*25.0; floatturnDirr=(t0_pos.y-t1_pos.y)*(t2_pos.x-t1_pos.x)+(t1_pos.x-t0_pos.x)*(t2_pos.y-t1_pos.y); if(turnDirr>0.000000001){ color=vec4(1.0,0.0,0.0,intensinty); }elseif(turnDirrt2,t2->t1,t1->t0toprepareforanothercycle,butallwithinthevertexshader(notonlyfortheefficiency,butalsoforsomeotherreasonswhichareunrelatedtothequestionbutrelatedtotheprojectI'mworkingon). javascriptglslwebgl Share Improvethisquestion Follow editedJul5,2020at21:18 Hamada 1,69822goldbadges1010silverbadges2323bronzebadges askedJul5,2020at18:07 baal_imagobaal_imago 12511silverbadge1010bronzebadges 1 1 Sidenote.ThebestfloatprecisionofwebGLshadersis32bits.Mostofthedefinedstring3.1415926535897932384626433832795islostastheclosestyoucangetis~3.141593 – Blindman67 Jul7,2020at18:01 Addacomment  |  1Answer 1 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 2 Note,yourquestionshouldprobablybeclosedasaduplicatesincehowtowriteoutputfromavertexshaderisalreadycoveredbutjusttoaddsomenotesrelevanttoyourquestion... InWebGL1isitnotpossibletoupdatebuffersintheGPU.Youcaninsteadstoreyourdatainatextureandupdateatexture.Still,youcannotupdateatexturefromitself pos=pos+vel//won'twork Butyoucanupdateanothertexture newPos=pos+vel//willwork ThennexttimepassthetexturecallednewPosasposandvisaversa InWebGL2youcanuse"transformFeedback"towritetheoutputavertexshader(thevaryings)toabuffer.Ithasthesameissuethatyoucannotwritebacktoabufferyouarereadingfrom. Thereisanexampleofwritingtoatextureandalsoanexampleofwritingtoabufferusingtransformfeedbackinthisanswer Alsoanexampleofputtingvertexdatainatexturehere ThereisanexampleofaparticlesystemusingtexturestoupdatethepositionsinthisQ&A Share Improvethisanswer Follow editedJul6,2020at8:18 answeredJul6,2020at1:22 gmangman 91.5k2929goldbadges230230silverbadges345345bronzebadges Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedjavascriptglslwebgloraskyourownquestion. TheOverflowBlog UnlockyourfullprogrammingpotentialwithTheKeyV2.0 Securityneedstoshiftleftintothesoftwaredevelopmentlifecycle FeaturedonMeta HowmighttheStagingGround&thenewAskWizardworkontheStackExchange... Overhaulingourcommunity'sclosurereasonsandguidance AnA/Btesthasgonelivefora"Trending"sortoptionforanswers Linked 2 passdatabetweenshaderprograms 1 Howtokeepcoordinationbetweenparticlesandwhichtexturepixelcontainseachone’sinformation? 0 Needhelprenderingpositionstoatextureusingframebuffersinvanillawebgl Related 6997 HowdoIremoveapropertyfromaJavaScriptobject? 0 GLSLshadersandWebGLproblem 10562 HowcanIremoveaspecificitemfromanarray? 5 Updatinginformationfromthevertexshader 6347 Howtoreturntheresponsefromanasynchronouscall 1 CreatingatrianglewithOpenGL&GLSL 3 WebGL:differencebetweenvertexshadertransformandapp/softwaretransform 2 WebGLVertexSpaceCoordinates HotNetworkQuestions Whyisone-timepadinformationallysecure? Whydon'tvirusesreachbroadconcentrationoutdoorsinacitylikeallergens? Whoarethese"MillennialFolk"beingreferredto? Whyarelongerantennasnotsuitabletopickhigherfrequencies? Mutualfundhistoricprices,seen@GoogleandYahoo,don'tmatchmyactivity WhyareSirHandelandPeterSamidenticaltoFalconandStuart? IsthereaDold-Kantheoremforcircleactions? Whenusingcattocreatefile,Ihavetotypectrl+dtwicetofinishtheinput,isthisexpected? BigpromotionofferbutamIhurtingmycareerwiththetitle? Couldahivemindworkforanon-insectoidspecies? Ubuntu22.04FreshinstallationFirefoxwillnoload Howtovetthird-partydeveloperpackages Weight&Balancewithmultiplestops Forceorphantobleedintomargin Aquestionaboutreturninglocalpointervariableinfunction Whatisthelinkbetweeng++andgcc-c++inFedora'sDNFrepositories? Whatisthedifferencebetween0.0.0.0/0and0.0.0.0/1 Shortestrestrictedsuperstring Liftinaclosedspace Willaddingwatertoagrillpantousetheremainsofpreviouslycookedfood(deglazing),damageit? Themoosemustflow,buthow? Mathematicianslearningfromengineersorfrom"engineers" What'swiththe"gettingrejectedfromtheFieldofReeds"dialogueinepisode1? Issigningafilebetterthanandrenderuselessaseparatechecksum? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. default Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?