OpenGL Shading Language - Khronos Group
文章推薦指數: 80 %
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::vector
延伸文章資訊
- 1GLSL - 维基百科,自由的百科全书
GLSL - OpenGL Shading Language 也稱作GLslang,是一個以C語言為基礎的高階著色語言。它是由OpenGL ARB 所建立,提供開發者對繪圖管線更多的直接控制,而...
- 2OpenGL Shading Language - Khronos Group
OpenGL Shading Language ... Shader stages: ... The OpenGL Shading Language (GLSL) is the principa...
- 3Core Language (GLSL) - OpenGL Wiki - Khronos Group
Core Language (GLSL) ... Shader stages: ... The OpenGL Shading Language is a C-style language, so...
- 4着色器
着色器是使用一种叫GLSL的类C语言写成的。GLSL是为图形计算量身定制的,它包含一些针对向量和矩阵操作的有用特性。 着色器的开头总是要 ...
- 5GLSL_百度百科
GLSL(GL Shading Language)的着色器代碼分成2個部分:Vertex Shader(頂點着色器)和Fragment(片斷着色 ... 它可以得到當前OpenGL 中的狀態,G...