OpenGL Shading Language - Khronos Group

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

OpenGL Shading Language ... Shader stages: ... The OpenGL Shading Language (GLSL) is the principal shading language for OpenGL. While, thanks to ... OpenGLShadingLanguage FromOpenGLWiki Jumptonavigation Jumptosearch GLSL OpenGLShadingLanguage Shader Objects Compilation Introspection Thecorelanguage Variabletypes Typequalifiers Layoutqualifiers Uniformvariables Samplervariables Imagevariables Built-invariables Interfaceblocks Uniformblocks Shaderstorageblocks SPIR-V Shaderstages: VertexShader Tessellation GeometryShader FragmentShader ComputeShader Othershadinglanguages TheOpenGLShadingLanguage(GLSL)istheprincipalshadinglanguageforOpenGL.While,thankstoOpenGLExtensions,thereareseveralshadinglanguagesavailableforuseinOpenGL,GLSL(andSPIR-V)aresupporteddirectlybyOpenGLwithoutextensions. GLSLisaC-stylelanguage.Thelanguagehasundergoneanumberofversionchanges,anditsharesthedeprecationmodelofOpenGL.ThecurrentversionofGLSLis4.60. Contents 1Compilationmodel 1.1Terminology 2Language 2.1Standardlibrary 2.2Variabletypes 2.3Typequalifiers 2.4Interfaceblocks 2.5Predefinedvariables 3UsingGLSLshaders 3.1Buildingshaders 3.1.1Attributesanddrawbuffers 3.2Settinguniforms 3.2.1Settingsamplers 3.3ErrorChecking 4Seealso 5Externallinks Compilationmodel Mainarticle:ShaderCompilation GLSLisquiteuniqueamongshadinglanguagesduetoitscompilationmodel.It'scompilationmodelismorelikethestandardCparadigm.Compilationisoverseenbyanumberofobjecttypes.NotethatthesedonotfollowthestandardOpenGLObjectsparadigm. Terminology BecauseofGLSL'suniquecompilationmodel,GLSLusesuniqueterminology. AccordingtoGLSL'sstandardterminology,ashaderisjustacompiledsetofstringsforaparticularprogrammablestage;itdoesnotevenneedtohavethecompletecodeforthatstage.Aprogramisafullylinkedprogramthatcoversmultipleprogrammablestages. Forthesakeofclarity,wewilladjustthisslightly.Whenthetermshaderisused,itwillbesynonymouswiththeGLSLconceptofprogram.TorefertoaGLSLshader,thetermshaderobjectwillbeused. Language Mainarticle:CoreLanguage(GLSL) GLSLisalotlikeC/C++inmanyways.Itsupportsmostofthefamiliarstructuralcomponents(for-loops,if-statements,etc).Butithassomeimportantlanguagedifferences. Standardlibrary TheOpenGLShadingLanguagedefinesanumberofstandardfunctions.Somestandardfunctionsarespecifictocertainshaderstages,whilemostareavailableinanystage.Thereisreferencedocumentationforthesefunctionsavailablehere. Variabletypes Mainarticle:DataType(GLSL) Chasanumberofbasictypes.GLSLusessomeofthese,butaddsmanymore. Typequalifiers Mainarticle:TypeQualifier(GLSL) GLSL'susesalargenumberofqualifierstospecifywherethevaluesthatvariousvariablescontaincomefrom.Qualifiersalsomodifyhowthosevariablescanbeused. Interfaceblocks Mainarticle:InterfaceBlock(GLSL) Certainvariabledefinitionscanbegroupedintointerfaceblocks.Thesecanbeusedtomakecommunicationbetweendifferentshaderstageseasier,ortoallowstorageforvariablestocomefromabufferobject. Predefinedvariables Mainarticle:Built-inVariable(GLSL) Thedifferentshaderstageshaveanumberofpredefinedvariablesforthem.Theseareprovidedbythesystemforvarioussystem-specificuse. UsingGLSLshaders Buildingshaders Attributesanddrawbuffers Forthestagesatthestartandendofthepipeline(vertexandfragment,respectively),theinitialinputvaluesandfinaloutputvaluesdonotcomefromorgotoshaderstages.Theinputvaluestoavertexshadercomefromvertexdataspecifiedinavertexarrayobject,pulledfromvertexbufferobjectsduringVertexRendering.Theoutputvaluesofafragmentshaderarepipedtoparticularbuffersforthecurrentlyboundframebuffer;eitherthedefaultframebufferoraframebufferobject. Becauseofthis,thereisamappinglayerfortheprogram'sinputsandoutputs.Thevertexshader'sinputnamesaremappedtoattributeindices,whilethefragmentshader'soutputnamesaremappedtodrawbufferindices.Thismappingcanbecreatedbeforetheprogramislinked.Ifitisnot,orifthemappingdoesnotcoveralloftheinputsandoutputs,thenthelinkerwillautomaticallydefinewhatindicesaremappedtowhichunmappedinputoroutputnames.Thisauto-generatedmappingcanbequeriedbytheuseraftertheprogramislinked. Settinguniforms Mainarticle:GLSLUniform#Uniformmanagement UniformsinGLSLareshadervariablesthataresetfromusercode,butonlyareallowedtochangebetweendifferentglDraw*calls.Uniformscanbequeriedandsetbythecodeexternaltoaparticularshader.Uniformscanbearrangedintoblocks,andthedatastoragefortheseblockscancomefrombufferobjects. Settingsamplers Mainarticle:GLSLSampler#Bindingtexturestosamplers Samplersarespecialtypeswhichmustbedefinedasuniforms.TheyrepresentboundtexturesintheOpenGLcontext.Theyaresetlikeinteger,1Duniformvalues. ErrorChecking Thispieceofcodeshowstheprocessofloadingavertexandfragmentshaders.Thenitcompilesthemandalsochecksforerrors.TheideahereistoencouragenewcomerstoGLSLtoalwayscheckforerrors.ItisinC++butthatdoesn'tmatter. Notethattheprocessofloadingandcompilingshadershasn'tchangedmuchoverthedifferentGLversions. Fullcompile/linkofaVertexandFragmentShader. //Readourshadersintotheappropriatebuffers std::stringvertexSource=//Getsourcecodeforvertexshader. std::stringfragmentSource=//Getsourcecodeforfragmentshader. //Createanemptyvertexshaderhandle GLuintvertexShader=glCreateShader(GL_VERTEX_SHADER); //SendthevertexshadersourcecodetoGL //Notethatstd::string's.c_strisNULLcharacterterminated. constGLchar*source=(constGLchar*)vertexSource.c_str(); glShaderSource(vertexShader,1,&source,0); //Compilethevertexshader glCompileShader(vertexShader); GLintisCompiled=0; glGetShaderiv(vertexShader,GL_COMPILE_STATUS,&isCompiled); if(isCompiled==GL_FALSE) { GLintmaxLength=0; glGetShaderiv(vertexShader,GL_INFO_LOG_LENGTH,&maxLength); //ThemaxLengthincludestheNULLcharacter std::vectorinfoLog(maxLength); glGetShaderInfoLog(vertexShader,maxLength,&maxLength,&infoLog[0]); //Wedon'tneedtheshaderanymore. glDeleteShader(vertexShader); //UsetheinfoLogasyouseefit. //Inthissimpleprogram,we'lljustleave return; } //Createanemptyfragmentshaderhandle GLuintfragmentShader=glCreateShader(GL_FRAGMENT_SHADER); //SendthefragmentshadersourcecodetoGL //Notethatstd::string's.c_strisNULLcharacterterminated. source=(constGLchar*)fragmentSource.c_str(); glShaderSource(fragmentShader,1,&source,0); //Compilethefragmentshader glCompileShader(fragmentShader); glGetShaderiv(fragmentShader,GL_COMPILE_STATUS,&isCompiled); if(isCompiled==GL_FALSE) { GLintmaxLength=0; glGetShaderiv(fragmentShader,GL_INFO_LOG_LENGTH,&maxLength); //ThemaxLengthincludestheNULLcharacter std::vectorinfoLog(maxLength); glGetShaderInfoLog(fragmentShader,maxLength,&maxLength,&infoLog[0]); //Wedon'tneedtheshaderanymore. glDeleteShader(fragmentShader); //Eitherofthem.Don'tleakshaders. glDeleteShader(vertexShader); //UsetheinfoLogasyouseefit. //Inthissimpleprogram,we'lljustleave return; } //Vertexandfragmentshadersaresuccessfullycompiled. //Nowtimetolinkthemtogetherintoaprogram. //Getaprogramobject. GLuintprogram=glCreateProgram(); //Attachourshaderstoourprogram glAttachShader(program,vertexShader); glAttachShader(program,fragmentShader); //Linkourprogram glLinkProgram(program); //Notethedifferentfunctionshere:glGetProgram*insteadofglGetShader*. GLintisLinked=0; glGetProgramiv(program,GL_LINK_STATUS,(int*)&isLinked); if(isLinked==GL_FALSE) { GLintmaxLength=0; glGetProgramiv(program,GL_INFO_LOG_LENGTH,&maxLength); //ThemaxLengthincludestheNULLcharacter std::vectorinfoLog(maxLength); glGetProgramInfoLog(program,maxLength,&maxLength,&infoLog[0]); //Wedon'tneedtheprogramanymore. glDeleteProgram(program); //Don'tleakshaderseither. glDeleteShader(vertexShader); glDeleteShader(fragmentShader); //UsetheinfoLogasyouseefit. //Inthissimpleprogram,we'lljustleave return; } //Alwaysdetachshadersafterasuccessfullink. glDetachShader(program,vertexShader); glDetachShader(program,fragmentShader); Seealso GLSL Sampler(GLSL) Uniform(GLSL) UniformBufferObject GLSL :commonmistakes GLSL :nVidiaspecificfeatures Hardware_specifics:_NVidia#GLSL_shader_conformance HistoryofOpenGL Externallinks CurrentSpecificationfortheOpenGLShadingLanguage OpenGLRedBook OpenGLBlueBook RenderMonkeyShaderDevelopmentEnvironment Retrievedfrom"http://www.khronos.org/opengl/wiki_opengl/index.php?title=OpenGL_Shading_Language&oldid=14750" Categories:PagesusingdeprecatedsourcetagsOpenGLShadingLanguageShadingLanguages Navigationmenu Personaltools CreateaccountLogin Namespaces PageDiscussion Variants Views ReadViewsourceViewhistory More Search Navigation MainPageGettingStartedDownloadOpenGLRegistryReferencePagesReferenceGuideNewsCommunityForumsAboutOpenGL Help ContactUsPrivacyPolicyHelpEditingRecentchanges Tools WhatlinkshereRelatedchangesSpecialpagesPrintableversionPermanentlinkPageinformationCitethispage



請為這篇文章評分?