2
0

GLState.pas 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLState;
  5. (* Tools for managing an application-side cache of OpenGL state. *)
  6. (*
  7. TODO: Proper client-side pushing + popping of state, in OpenGL 3+ contexts,
  8. rather than using glPushAttrib + glPopAttrib.
  9. TODO: Proper support for textures, taking into account that they probably
  10. won't be linked to texture units in some future version of OpenGL.
  11. TODO: Once more of GLScene is cache-aware, enable some of the checks before
  12. changing OpenGL state (where we will gain a speed increase).
  13. DONE: Cache some relevant legacy state
  14. TODO: improve binding objects to binding points
  15. TODO: decide how to implement the new Enable* options (without going above
  16. 32 elements in sets if possible, which would be slower in 32bit Delphi)
  17. DONE: remove stTexture1D, 2D, etc from TGLState if possible, since they are
  18. per texture-unit + also deprecated in OpenGL 3+
  19. *)
  20. interface
  21. {$I GLScene.inc}
  22. { .$DEFINE USE_CACHE_MISS_CHECK }
  23. uses
  24. System.Classes,
  25. System.SysUtils,
  26. OpenGLTokens,
  27. GLCrossPlatform,
  28. GLVectorTypes,
  29. GLVectorGeometry,
  30. GLTextureFormat;
  31. const
  32. GLS_VERTEX_ATTR_NUM = 16;
  33. type
  34. TGLStateType = (sttCurrent, sttPoint, sttLine, sttPolygon, sttPolygonStipple,
  35. sttPixelMode, sttLighting, sttFog, sttDepthBuffer, sttAccumBuffer,
  36. sttStencilBuffer, sttViewport, sttTransform, sttEnable, sttColorBuffer,
  37. sttHint, sttEval, sttList, sttTexture, sttScissor,
  38. sttMultisample);
  39. TGLStateTypes = set of TGLStateType;
  40. const
  41. cAllAttribBits = [low(TGLStateType) .. High(TGLStateType)];
  42. type
  43. TGLMeshPrimitive = (
  44. mpNOPRIMITIVE,
  45. mpTRIANGLES,
  46. mpTRIANGLE_STRIP,
  47. mpTRIANGLE_FAN,
  48. mpPOINTS,
  49. mpLINES,
  50. mpLINE_LOOP,
  51. mpLINE_STRIP,
  52. mpLINES_ADJACENCY,
  53. mpLINE_STRIP_ADJACENCY,
  54. mpTRIANGLES_ADJACENCY,
  55. mpTRIANGLE_STRIP_ADJACENCY,
  56. mpPATCHES
  57. );
  58. TGLMeshPrimitives = set of TGLMeshPrimitive;
  59. const
  60. cAllMeshPrimitive = [
  61. mpTRIANGLES,
  62. mpTRIANGLE_STRIP,
  63. mpTRIANGLE_FAN,
  64. mpPOINTS,
  65. mpLINES,
  66. mpLINE_LOOP,
  67. mpLINE_STRIP,
  68. mpLINES_ADJACENCY,
  69. mpLINE_STRIP_ADJACENCY,
  70. mpTRIANGLES_ADJACENCY,
  71. mpTRIANGLE_STRIP_ADJACENCY,
  72. mpPATCHES];
  73. type
  74. // Reflects all relevant (binary) states of OpenGL subsystem
  75. TGLState = (stAlphaTest, stAutoNormal,
  76. stBlend, stColorMaterial, stCullFace, stDepthTest, stDither,
  77. stFog, stLighting, stLineSmooth, stLineStipple,
  78. stIndexLogicOp, stColorLogicOp, stNormalize, stPointSmooth, stPointSprite,
  79. stPolygonSmooth, stPolygonStipple, stScissorTest, stStencilTest,
  80. stPolygonOffsetPoint, stPolygonOffsetLine, stPolygonOffsetFill,
  81. stDepthClamp);
  82. TGLStates = set of TGLState;
  83. TGLComparisonFunction = (cfNever, cfAlways, cfLess, cfLEqual, cfEqual,
  84. cfGreater, cfNotEqual, cfGEqual);
  85. TGLStencilFunction = TGLComparisonFunction;
  86. TGLDepthFunction = TGLComparisonFunction;
  87. TGLBlendFunction = (bfZero, bfOne,
  88. bfSrcColor, bfOneMinusSrcColor, bfDstColor, bfOneMinusDstColor,
  89. bfSrcAlpha, bfOneMinusSrcAlpha, bfDstAlpha, bfOneMinusDstAlpha,
  90. bfConstantColor, bfOneMinusConstantColor,
  91. bfConstantAlpha, bfOneMinusConstantAlpha,
  92. bfSrcAlphaSat);
  93. TGLDstBlendFunction = bfZero..bfOneMinusConstantAlpha;
  94. TGLBlendEquation = (beAdd, beSubtract, beReverseSubtract, beMin, beMax);
  95. TGLStencilOp = (soKeep, soZero, soReplace, soIncr, soDecr, soInvert, soIncrWrap,
  96. soDecrWrap);
  97. TGLLogicOp = (loClear, loAnd, loAndReverse, loCopy, loAndInverted, loNoOp,
  98. loXOr, loOr, loNor, loEquiv, loInvert, loOrReverse, loCopyInverted,
  99. loOrInverted, loNAnd, loSet);
  100. TGLQueryType = (
  101. qrySamplesPassed,
  102. qryPrimitivesGenerated,
  103. qryTransformFeedbackPrimitivesWritten,
  104. qryTimeElapsed,
  105. qryAnySamplesPassed);
  106. // Describe what kind of winding has a front face
  107. TGLFaceWinding = (fwCounterClockWise, fwClockWise);
  108. TGLPolygonMode = (pmFill, pmLines, pmPoints);
  109. TGLCullFaceMode = (cmFront, cmBack, cmFrontAndBack);
  110. // TSingleCullFaceMode = cmFront..cmBack;
  111. TGLColorComponent = (ccRed, ccGreen, ccBlue, ccAlpha);
  112. TGLColorMask = set of TGLColorComponent;
  113. const
  114. cAllColorComponents = [ccRed, ccGreen, ccBlue, ccAlpha];
  115. MAX_HARDWARE_LIGHT = 16;
  116. MAX_SHADER_LIGHT = 8;
  117. MAX_HARDWARE_TEXTURE_UNIT = 48;
  118. MAX_HARDWARE_UNIFORM_BUFFER_BINDING = 75;
  119. type
  120. TGLHintType = (hintDontCare, hintFastest, hintNicest);
  121. TGLLightSourceState = packed record
  122. Position: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  123. Ambient: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  124. Diffuse: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  125. Specular: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  126. SpotDirection: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  127. SpotCosCutoffExponent: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  128. Attenuation: array [0 .. MAX_HARDWARE_LIGHT - 1] of TVector;
  129. end;
  130. TGLShaderLightSourceState = packed record
  131. Position: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  132. Ambient: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  133. Diffuse: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  134. Specular: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  135. SpotDirection: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  136. SpotCosCutoffExponent: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  137. Attenuation: array [0 .. MAX_SHADER_LIGHT - 1] of TVector;
  138. end;
  139. TGLOnLightsChanged = procedure(Sender: TObject);
  140. TGLBufferBindingTarget = (bbtUniform, bbtTransformFeedBack);
  141. TUBOStates = record
  142. FUniformBufferBinding: Cardinal;
  143. FOffset: TGLintptr;
  144. FSize: TGLsizeiptr;
  145. end;
  146. TGLMaterialLevel = (mlAuto, mlFixedFunction, mlMultitexturing, mlSM3, mlSM4, mlSM5);
  147. (* Manages an application-side cache of OpenGL states and parameters.
  148. Purpose of this class is to eliminate redundant state and parameter
  149. changes, and there will typically be no more than one state cache per
  150. OpenGL context *)
  151. TGLStateCache = class
  152. strict private
  153. // Legacy state
  154. FFrontBackColors: array [0 .. 1, 0 .. 3] of TVector;
  155. FFrontBackShininess: array [0 .. 1] of Integer;
  156. FAlphaFunc: TGLComparisonFunction;
  157. FAlphaRef: Single;
  158. FPolygonBackMode: TGLPolygonMode; // Front + back have same polygon mode
  159. // Lighting state
  160. FMaxLights: Cardinal;
  161. FLightEnabling: array [0 .. MAX_HARDWARE_LIGHT - 1] of Boolean;
  162. FLightIndices: array [0 .. MAX_HARDWARE_LIGHT - 1] of TGLint;
  163. FLightNumber: Integer;
  164. FLightStates: TGLLightSourceState;
  165. FSpotCutoff: array [0 .. MAX_HARDWARE_LIGHT - 1] of Single;
  166. FShaderLightStates: TGLShaderLightSourceState;
  167. FShaderLightStatesChanged: Boolean;
  168. FColorWriting: Boolean; // TODO: change to per draw buffer (FColorWriteMask)
  169. FStates: TGLStates;
  170. FListStates: array of TGLStateTypes;
  171. FCurrentList: Cardinal;
  172. FTextureMatrixIsIdentity: array [0 .. 3] of Boolean;
  173. // FForwardContext: Boolean;
  174. FFFPLight: Boolean;
  175. // Vertex Array Data state
  176. FVertexArrayBinding: Cardinal;
  177. FArrayBufferBinding: Cardinal;
  178. FElementBufferBinding: Cardinal;
  179. FTextureBufferBinding: Cardinal;
  180. FEnablePrimitiveRestart: TGLboolean;
  181. FPrimitiveRestartIndex: Cardinal;
  182. // Transformation state
  183. FViewPort: TVector4i;
  184. FDepthRange: array [0 .. 1] of TGLclampd;
  185. FEnableClipDistance: array [0 .. 7] of TGLboolean;
  186. FEnableDepthClamp: TGLboolean;
  187. // Coloring state
  188. FClampReadColor: Cardinal; // GL_FIXED_ONLY
  189. FProvokingVertex: Cardinal; // GL_LAST_VERTEX_CONVENTION
  190. // Rasterization state
  191. FPointSize: TGLfloat;
  192. FPointFadeThresholdSize: TGLfloat;
  193. FPointSpriteCoordOrigin: Cardinal; // GL_UPPER_LEFT
  194. FLineWidth: Single;
  195. FLineStippleFactor: TGLint;
  196. FLineStipplePattern: TGLushort;
  197. FEnableLineSmooth: TGLboolean;
  198. FEnableCullFace: TGLboolean;
  199. FCullFaceMode: TGLCullFaceMode;
  200. FFrontFace: TGLFaceWinding;
  201. FEnablePolygonSmooth: TGLboolean;
  202. FPolygonMode: TGLPolygonMode;
  203. FPolygonOffsetFactor: TGLfloat;
  204. FPolygonOffsetUnits: TGLfloat;
  205. FEnablePolygonOffsetPoint: TGLboolean;
  206. FEnablePolygonOffsetLine: TGLboolean;
  207. FEnablePolygonOffsetFill: TGLboolean;
  208. // Multisample state
  209. FEnableMultisample: TGLboolean;
  210. FEnableSampleAlphaToCoverage: TGLboolean;
  211. FEnableSampleAlphaToOne: TGLboolean;
  212. FEnableSampleCoverage: TGLboolean;
  213. FSampleCoverageValue: TGLfloat;
  214. FSampleCoverageInvert: TGLboolean;
  215. FEnableSampleMask: TGLboolean;
  216. FSampleMaskValue: array [0 .. 15] of TGLbitfield;
  217. // Texture state
  218. FMaxTextureSize: Cardinal;
  219. FMax3DTextureSize: Cardinal;
  220. FMaxCubeTextureSize: Cardinal;
  221. FMaxArrayTextureSize: Cardinal;
  222. FMaxTextureImageUnits: Cardinal;
  223. FMaxTextureAnisotropy: Cardinal;
  224. FMaxSamples: Cardinal;
  225. FTextureBinding: array [0 .. MAX_HARDWARE_TEXTURE_UNIT - 1, TGLTextureTarget] of Cardinal;
  226. FTextureBindingTime: array [0 .. MAX_HARDWARE_TEXTURE_UNIT - 1, TGLTextureTarget] of Double;
  227. FSamplerBinding: array [0 .. MAX_HARDWARE_TEXTURE_UNIT - 1] of Cardinal;
  228. // Active texture state
  229. FActiveTexture: TGLint; // 0 .. Max_texture_units
  230. FActiveTextureEnabling: array [0 .. MAX_HARDWARE_TEXTURE_UNIT - 1, TGLTextureTarget] of Boolean;
  231. // Pixel operation state
  232. FEnableScissorTest: TGLboolean;
  233. FScissorBox: TVector4i;
  234. FEnableStencilTest: TGLboolean;
  235. FStencilFunc: TGLStencilFunction;
  236. FStencilValueMask: Cardinal;
  237. FStencilRef: TGLint;
  238. FStencilFail: TGLStencilOp;
  239. FStencilPassDepthFail: TGLStencilOp;
  240. FStencilPassDepthPass: TGLStencilOp;
  241. FStencilBackFunc: TGLStencilFunction;
  242. FStencilBackValueMask: Cardinal;
  243. FStencilBackRef: Cardinal;
  244. FStencilBackFail: TGLStencilOp;
  245. FStencilBackPassDepthPass: TGLStencilOp;
  246. FStencilBackPassDepthFail: TGLStencilOp;
  247. FEnableDepthTest: TGLboolean;
  248. FDepthFunc: TGLDepthFunction;
  249. FEnableBlend: array [0 .. 15] of TGLboolean;
  250. FBlendSrcRGB: TGLBlendFunction;
  251. FBlendSrcAlpha: TGLBlendFunction;
  252. FBlendDstRGB: TGLDstBlendFunction;
  253. FBlendDstAlpha: TGLDstBlendFunction;
  254. FBlendEquationRGB: TGLBlendEquation;
  255. FBlendEquationAlpha: TGLBlendEquation;
  256. FBlendColor: TVector;
  257. FEnableFramebufferSRGB: TGLboolean;
  258. FEnableDither: TGLboolean;
  259. FEnableColorLogicOp: TGLboolean;
  260. FLogicOpMode: TGLLogicOp;
  261. // Framebuffer control state
  262. FColorWriteMask: array [0 .. 15] of TGLColorMask;
  263. FDepthWriteMask: TGLboolean;
  264. FStencilWriteMask: Cardinal;
  265. FStencilBackWriteMask: Cardinal;
  266. FColorClearValue: TVector;
  267. FDepthClearValue: TGLfloat;
  268. FStencilClearValue: Cardinal;
  269. // Framebuffer state
  270. FDrawFrameBuffer: Cardinal;
  271. FReadFrameBuffer: Cardinal;
  272. // Renderbuffer state
  273. FRenderBuffer: Cardinal;
  274. // Pixels state
  275. FUnpackSwapBytes: TGLboolean;
  276. FUnpackLSBFirst: TGLboolean;
  277. FUnpackImageHeight: Cardinal;
  278. FUnpackSkipImages: Cardinal;
  279. FUnpackRowLength: Cardinal;
  280. FUnpackSkipRows: Cardinal;
  281. FUnpackSkipPixels: Cardinal;
  282. FUnpackAlignment: Cardinal;
  283. FPackSwapBytes: TGLboolean;
  284. FPackLSBFirst: TGLboolean;
  285. FPackImageHeight: Cardinal;
  286. FPackSkipImages: Cardinal;
  287. FPackRowLength: Cardinal;
  288. FPackSkipRows: Cardinal;
  289. FPackSkipPixels: Cardinal;
  290. FPackAlignment: Cardinal;
  291. FPixelPackBufferBinding: Cardinal;
  292. FPixelUnpackBufferBinding: Cardinal;
  293. // Program state
  294. FCurrentProgram: Cardinal;
  295. FMaxTextureUnits: Cardinal;
  296. FUniformBufferBinding: Cardinal;
  297. FUBOStates: array [TGLBufferBindingTarget, 0 .. MAX_HARDWARE_UNIFORM_BUFFER_BINDING - 1] of TUBOStates;
  298. // Vector + Geometry Shader state
  299. FCurrentVertexAttrib: array [0 .. 15] of TVector;
  300. FEnableProgramPointSize: TGLboolean;
  301. // Transform Feedback state
  302. FTransformFeedbackBufferBinding: Cardinal;
  303. // Hints state
  304. FTextureCompressionHint: TGLHintType;
  305. FPolygonSmoothHint: TGLHintType;
  306. FFragmentShaderDerivitiveHint: TGLHintType;
  307. FLineSmoothHint: TGLHintType;
  308. FMultisampleFilterHint: TGLHintType;
  309. // Misc state
  310. FCurrentQuery: array [TGLQueryType] of Cardinal;
  311. FCopyReadBufferBinding: Cardinal;
  312. FCopyWriteBufferBinding: Cardinal;
  313. FEnableTextureCubeMapSeamless: TGLboolean;
  314. FInsideList: Boolean;
  315. FOnLightsChanged: TGLOnLightsChanged;
  316. protected
  317. // Vertex Array Data state
  318. procedure SetVertexArrayBinding(const Value: Cardinal); inline;
  319. function GetArrayBufferBinding: Cardinal; inline;
  320. procedure SetArrayBufferBinding(const Value: Cardinal); inline;
  321. function GetElementBufferBinding: Cardinal; inline;
  322. procedure SetElementBufferBinding(const Value: Cardinal); inline;
  323. function GetEnablePrimitiveRestart: TGLboolean; inline;
  324. function GetPrimitiveRestartIndex: Cardinal; inline;
  325. procedure SetEnablePrimitiveRestart(const enabled: TGLboolean); inline;
  326. procedure SetPrimitiveRestartIndex(const index: Cardinal); inline;
  327. procedure SetTextureBufferBinding(const Value: Cardinal); inline;
  328. // Transformation state
  329. procedure SetViewPort(const Value: TVector4i); inline;
  330. function GetEnableClipDistance(ClipDistance: Cardinal): TGLboolean; inline;
  331. procedure SetEnableClipDistance(index: Cardinal; const Value: TGLboolean); inline;
  332. function GetDepthRangeFar: TGLclampd; inline;
  333. procedure SetDepthRangeFar(const Value: TGLclampd); inline;
  334. function GetDepthRangeNear: TGLclampd; inline;
  335. procedure SetDepthRangeNear(const Value: TGLclampd); inline;
  336. procedure SetEnableDepthClamp(const enabled: TGLboolean); inline;
  337. // Coloring state
  338. procedure SetClampReadColor(const Value: Cardinal); inline;
  339. procedure SetProvokingVertex(const Value: Cardinal); inline;
  340. // Rasterization state
  341. procedure SetPointSize(const Value: TGLfloat); inline;
  342. procedure SetPointFadeThresholdSize(const Value: TGLfloat); inline;
  343. procedure SetPointSpriteCoordOrigin(const Value: Cardinal); inline;
  344. procedure SetLineWidth(const Value: TGLfloat); inline;
  345. procedure SetLineStippleFactor(const Value: TGLint); inline;
  346. procedure SetLineStipplePattern(const Value: TGLushort); inline;
  347. procedure SetEnableLineSmooth(const Value: TGLboolean); inline;
  348. procedure SetEnableCullFace(const Value: TGLboolean); inline;
  349. procedure SetCullFaceMode(const Value: TGLCullFaceMode); inline;
  350. procedure SetFrontFace(const Value: TGLFaceWinding); inline;
  351. procedure SetEnablePolygonSmooth(const Value: TGLboolean); inline;
  352. procedure SetPolygonMode(const Value: TGLPolygonMode); inline;
  353. procedure SetPolygonOffsetFactor(const Value: TGLfloat); inline;
  354. procedure SetPolygonOffsetUnits(const Value: TGLfloat); inline;
  355. procedure SetEnablePolygonOffsetPoint(const Value: TGLboolean); inline;
  356. procedure SetEnablePolygonOffsetLine(const Value: TGLboolean); inline;
  357. procedure SetEnablePolygonOffsetFill(const Value: TGLboolean); inline;
  358. // Multisample state
  359. procedure SetEnableMultisample(const Value: TGLboolean); inline;
  360. procedure SetEnableSampleAlphaToCoverage(const Value: TGLboolean); inline;
  361. procedure SetEnableSampleAlphaToOne(const Value: TGLboolean); inline;
  362. procedure SetEnableSampleCoverage(const Value: TGLboolean); inline;
  363. procedure SetSampleCoverageValue(const Value: TGLfloat); inline;
  364. procedure SetSampleCoverageInvert(const Value: TGLboolean); inline;
  365. procedure SetEnableSampleMask(const Value: TGLboolean); inline;
  366. function GetSampleMaskValue(index: Integer): TGLbitfield; inline;
  367. procedure SetSampleMaskValue(index: Integer; const Value: TGLbitfield); inline;
  368. // Texture state
  369. function GetMaxTextureSize: Cardinal; inline;
  370. function GetMax3DTextureSize: Cardinal; inline;
  371. function GetMaxCubeTextureSize: Cardinal; inline;
  372. function GetMaxArrayTextureSize: Cardinal; inline;
  373. function GetMaxTextureImageUnits: Cardinal; inline;
  374. function GetMaxTextureAnisotropy: Cardinal; inline;
  375. function GetMaxSamples: Cardinal; inline;
  376. function GetTextureBinding(index: Integer; target: TGLTextureTarget): Cardinal; inline;
  377. function GetTextureBindingTime(index: Integer; target: TGLTextureTarget): Double; inline;
  378. procedure SetTextureBinding(index: Integer; target: TGLTextureTarget; const Value: Cardinal);
  379. function GetActiveTextureEnabled(target: TGLTextureTarget): Boolean; inline;
  380. procedure SetActiveTextureEnabled(target: TGLTextureTarget; const Value: Boolean); inline;
  381. function GetSamplerBinding(index: Cardinal): Cardinal; inline;
  382. procedure SetSamplerBinding(index: Cardinal; const Value: Cardinal); inline;
  383. // Active texture
  384. procedure SetActiveTexture(const Value: TGLint); inline;
  385. // Pixel operations
  386. procedure SetEnableScissorTest(const Value: TGLboolean); inline;
  387. procedure SetScissorBox(const Value: TVector4i); inline;
  388. procedure SetEnableStencilTest(const Value: TGLboolean); inline;
  389. procedure SetEnableDepthTest(const Value: TGLboolean); inline;
  390. procedure SetDepthFunc(const Value: TGLDepthFunction); inline;
  391. function GetEnableBlend(index: Integer): TGLboolean; inline;
  392. procedure SetEnableBlend(index: Integer; const Value: TGLboolean); inline;
  393. procedure SetBlendColor(const Value: TVector); inline;
  394. procedure SetEnableFramebufferSRGB(const Value: TGLboolean); inline;
  395. procedure SetEnableDither(const Value: TGLboolean); inline;
  396. procedure SetEnableColorLogicOp(const Value: TGLboolean); inline;
  397. procedure SetLogicOpMode(const Value: TGLLogicOp); inline;
  398. // Framebuffer control
  399. function GetColorWriteMask(index: Integer): TGLColorMask; inline;
  400. procedure SetColorWriteMask(index: Integer; const Value: TGLColorMask); inline;
  401. procedure SetDepthWriteMask(const Value: TGLboolean); inline;
  402. procedure SetStencilWriteMask(const Value: Cardinal); inline;
  403. procedure SetStencilBackWriteMask(const Value: Cardinal); inline;
  404. procedure SetColorClearValue(const Value: TVector); inline;
  405. procedure SetDepthClearValue(const Value: TGLfloat); inline;
  406. procedure SetStencilClearValue(const Value: Cardinal); inline;
  407. // Framebuffer
  408. procedure SetDrawFrameBuffer(const Value: Cardinal); inline;
  409. procedure SetReadFrameBuffer(const Value: Cardinal); inline;
  410. // Renderbuffer
  411. procedure SetRenderBuffer(const Value: Cardinal); inline;
  412. // Pixels
  413. procedure SetUnpackSwapBytes(const Value: TGLboolean); inline;
  414. procedure SetUnpackLSBFirst(const Value: TGLboolean); inline;
  415. procedure SetUnpackImageHeight(const Value: Cardinal); inline;
  416. procedure SetUnpackSkipImages(const Value: Cardinal); inline;
  417. procedure SetUnpackRowLength(const Value: Cardinal); inline;
  418. procedure SetUnpackSkipRows(const Value: Cardinal); inline;
  419. procedure SetUnpackSkipPixels(const Value: Cardinal); inline;
  420. procedure SetUnpackAlignment(const Value: Cardinal); inline;
  421. procedure SetPackSwapBytes(const Value: TGLboolean); inline;
  422. procedure SetPackLSBFirst(const Value: TGLboolean); inline;
  423. procedure SetPackImageHeight(const Value: Cardinal); inline;
  424. procedure SetPackSkipImages(const Value: Cardinal); inline;
  425. procedure SetPackRowLength(const Value: Cardinal); inline;
  426. procedure SetPackSkipRows(const Value: Cardinal); inline;
  427. procedure SetPackSkipPixels(const Value: Cardinal); inline;
  428. procedure SetPackAlignment(const Value: Cardinal); inline;
  429. procedure SetPixelPackBufferBinding(const Value: Cardinal);
  430. procedure SetPixelUnpackBufferBinding(const Value: Cardinal);
  431. // Program
  432. procedure SetCurrentProgram(const Value: Cardinal); inline;
  433. procedure SetUniformBufferBinding(const Value: Cardinal); inline;
  434. function GetMaxTextureUnits: Cardinal; inline;
  435. // Vector + Geometry Shader state
  436. function GetCurrentVertexAttrib(index: Integer): TVector; inline;
  437. procedure SetCurrentVertexAttrib(index: Integer; const Value: TVector); inline;
  438. procedure SetEnableProgramPointSize(const Value: TGLboolean); inline;
  439. // Transform Feedback state
  440. procedure SetTransformFeedbackBufferBinding(const Value: Cardinal); inline;
  441. // Hints
  442. procedure SetLineSmoothHint(const Value: TGLHintType); inline;
  443. procedure SetPolygonSmoothHint(const Value: TGLHintType); inline;
  444. procedure SetTextureCompressionHint(const Value: TGLHintType); inline;
  445. procedure SetFragmentShaderDerivitiveHint(const Value: TGLHintType); inline;
  446. procedure SetMultisampleFilterHint(const Value: TGLHintType); inline;
  447. // Misc
  448. function GetCurrentQuery(index: TGLQueryType): Cardinal; inline;
  449. // procedure SetCurrentQuery(Index: TQueryType; const Value: Cardinal);
  450. procedure SetCopyReadBufferBinding(const Value: Cardinal); inline;
  451. procedure SetCopyWriteBufferBinding(const Value: Cardinal); inline;
  452. procedure SetEnableTextureCubeMapSeamless(const Value: TGLboolean); inline;
  453. // Ligting
  454. procedure SetFFPLight(Value: Boolean); inline;
  455. function GetMaxLights: Integer; inline;
  456. function GetLightEnabling(I: Integer): Boolean; inline;
  457. procedure SetLightEnabling(I: Integer; Value: Boolean); inline;
  458. function GetLightPosition(I: Integer): TVector; inline;
  459. procedure SetLightPosition(I: Integer; const Value: TVector); inline;
  460. function GetLightSpotDirection(I: Integer): TAffineVector; inline;
  461. procedure SetLightSpotDirection(I: Integer; const Value: TAffineVector); inline;
  462. function GetLightAmbient(I: Integer): TVector; inline;
  463. procedure SetLightAmbient(I: Integer; const Value: TVector); inline;
  464. function GetLightDiffuse(I: Integer): TVector; inline;
  465. procedure SetLightDiffuse(I: Integer; const Value: TVector); inline;
  466. function GetLightSpecular(I: Integer): TVector; inline;
  467. procedure SetLightSpecular(I: Integer; const Value: TVector); inline;
  468. function GetSpotCutoff(I: Integer): Single; inline;
  469. procedure SetSpotCutoff(I: Integer; const Value: Single); inline;
  470. function GetSpotExponent(I: Integer): Single; inline;
  471. procedure SetSpotExponent(I: Integer; const Value: Single); inline;
  472. function GetConstantAtten(I: Integer): Single; inline;
  473. procedure SetConstantAtten(I: Integer; const Value: Single); inline;
  474. function GetLinearAtten(I: Integer): Single; inline;
  475. procedure SetLinearAtten(I: Integer; const Value: Single); inline;
  476. function GetQuadAtten(I: Integer): Single; inline;
  477. procedure SetQuadAtten(I: Integer; const Value: Single); inline;
  478. procedure SetForwardContext(Value: Boolean); inline;
  479. function GetMaterialAmbient(const aFace: TGLCullFaceMode): TVector; inline;
  480. function GetMaterialDiffuse(const aFace: TGLCullFaceMode): TVector; inline;
  481. function GetMaterialSpecular(const aFace: TGLCullFaceMode): TVector; inline;
  482. function GetMaterialEmission(const aFace: TGLCullFaceMode): TVector; inline;
  483. function GetMaterialShininess(const aFace: TGLCullFaceMode): Integer; inline;
  484. public
  485. constructor Create; virtual;
  486. destructor Destroy; override;
  487. procedure PushAttrib(const stateTypes: TGLStateTypes); inline;
  488. procedure PopAttrib(); inline;
  489. procedure Enable(const aState: TGLState);
  490. procedure Disable(const aState: TGLState);
  491. procedure PerformEnable(const aState: TGLState); inline;
  492. procedure PerformDisable(const aState: TGLState); inline;
  493. procedure SetGLState(const aState: TGLState); deprecated; inline;
  494. procedure UnSetGLState(const aState: TGLState); deprecated; inline;
  495. procedure ResetGLPolygonMode; deprecated; inline;
  496. procedure ResetGLMaterialColors; deprecated; inline;
  497. procedure ResetGLTexture(const TextureUnit: Integer); deprecated; inline;
  498. procedure ResetGLCurrentTexture; deprecated;
  499. procedure ResetGLFrontFace; deprecated;
  500. procedure SetGLFrontFaceCW; deprecated; inline;
  501. procedure ResetAll; deprecated; inline;
  502. // Adjusts material colors for a face
  503. procedure SetGLMaterialColors(const aFace: TGLCullFaceMode; const emission, Ambient, Diffuse, Specular: TVector;
  504. const shininess: Integer);
  505. property MaterialAmbient[const aFace: TGLCullFaceMode]: TVector read GetMaterialAmbient;
  506. property MaterialDiffuse[const aFace: TGLCullFaceMode]: TVector read GetMaterialDiffuse;
  507. property MaterialSpecular[const aFace: TGLCullFaceMode]: TVector read GetMaterialSpecular;
  508. property MaterialEmission[const aFace: TGLCullFaceMode]: TVector read GetMaterialEmission;
  509. property MaterialShininess[const aFace: TGLCullFaceMode]: Integer read GetMaterialShininess;
  510. // Adjusts material alpha channel for a face
  511. procedure SetGLMaterialAlphaChannel(const aFace: Cardinal; const alpha: TGLfloat);
  512. // Adjusts material diffuse color for a face
  513. procedure SetGLMaterialDiffuseColor(const aFace: Cardinal; const Diffuse: TVector);
  514. // Lighting states
  515. property FixedFunctionPipeLight: Boolean read FFFPLight write SetFFPLight;
  516. property MaxLights: Integer read GetMaxLights;
  517. property LightEnabling[Index: Integer]: Boolean read GetLightEnabling write SetLightEnabling;
  518. property LightPosition[Index: Integer]: TVector read GetLightPosition write SetLightPosition;
  519. property LightSpotDirection[Index: Integer]: TAffineVector read GetLightSpotDirection write SetLightSpotDirection;
  520. property LightAmbient[Index: Integer]: TVector read GetLightAmbient write SetLightAmbient;
  521. property LightDiffuse[Index: Integer]: TVector read GetLightDiffuse write SetLightDiffuse;
  522. property LightSpecular[Index: Integer]: TVector read GetLightSpecular write SetLightSpecular;
  523. property LightSpotCutoff[Index: Integer]: Single read GetSpotCutoff write SetSpotCutoff;
  524. property LightSpotExponent[Index: Integer]: Single read GetSpotExponent write SetSpotExponent;
  525. property LightConstantAtten[Index: Integer]: Single read GetConstantAtten write SetConstantAtten;
  526. property LightLinearAtten[Index: Integer]: Single read GetLinearAtten write SetLinearAtten;
  527. property LightQuadraticAtten[Index: Integer]: Single read GetQuadAtten write SetQuadAtten;
  528. function GetLightIndicesAsAddress: PGLInt;
  529. function GetLightStateAsAddress: Pointer;
  530. property LightNumber: Integer read FLightNumber;
  531. property OnLightsChanged: TGLOnLightsChanged read FOnLightsChanged write FOnLightsChanged;
  532. // Blending states
  533. procedure SetGLAlphaFunction(func: TGLComparisonFunction; ref: Single); inline;
  534. // Vertex Array Data state
  535. (* The currently bound array buffer (calling glVertexAttribPointer
  536. locks this buffer to the currently bound VBO). *)
  537. property VertexArrayBinding: Cardinal read FVertexArrayBinding write SetVertexArrayBinding;
  538. // The currently bound vertex buffer object (VAO)
  539. property ArrayBufferBinding: Cardinal read GetArrayBufferBinding write SetArrayBufferBinding;
  540. // The currently bound element buffer object (EBO)
  541. property ElementBufferBinding: Cardinal read GetElementBufferBinding write SetElementBufferBinding;
  542. // Determines whether primitive restart is turned on or off
  543. property EnablePrimitiveRestart: TGLboolean read GetEnablePrimitiveRestart write SetEnablePrimitiveRestart;
  544. // The index Value that causes a primitive restart
  545. property PrimitiveRestartIndex: Cardinal read GetPrimitiveRestartIndex write SetPrimitiveRestartIndex;
  546. // The currently bound texture buffer object (TBO)
  547. property TextureBufferBinding: Cardinal read FTextureBufferBinding write SetTextureBufferBinding;
  548. // Transformation state
  549. property ViewPort: TVector4i read FViewPort write SetViewPort;
  550. // Modifies the near + far clipping planes
  551. procedure SetDepthRange(const ZNear, ZFar: TGLclampd); inline;
  552. // The near clipping plane distance
  553. property DepthRangeNear: TGLclampd read GetDepthRangeNear write SetDepthRangeNear;
  554. // The far clipping plane distance
  555. property DepthRangeFar: TGLclampd read GetDepthRangeFar write SetDepthRangeFar;
  556. // Enables/Disables each of the clip distances, used in shaders
  557. property EnableClipDistance[Index: Cardinal]: TGLboolean read GetEnableClipDistance write SetEnableClipDistance;
  558. // Enables/Disables depth clamping
  559. property EnableDepthClamp: TGLboolean read FEnableDepthClamp write SetEnableDepthClamp;
  560. // Coloring state Controls read color clamping
  561. property ClampReadColor: Cardinal read FClampReadColor write SetClampReadColor;
  562. (* The provoking vertex used in flat shading. All the vertices of each
  563. primitive will the same value determined by this property. *)
  564. property ProvokingVertex: Cardinal read FProvokingVertex write SetProvokingVertex;
  565. // Rasterization state
  566. (* The default point size, used when EnableProgramPointSize = false *)
  567. property PointSize: TGLfloat read FPointSize write SetPointSize;
  568. // If multisampling is enabled, this can control when points are faded out
  569. property PointFadeThresholdSize: TGLfloat read FPointFadeThresholdSize write SetPointFadeThresholdSize;
  570. // The texture coordinate origin of point sprites
  571. property PointSpriteCoordOrigin: Cardinal read FPointSpriteCoordOrigin write SetPointSpriteCoordOrigin;
  572. // The line width
  573. property LineWidth: TGLfloat read FLineWidth write SetLineWidth;
  574. // The line stipple
  575. property LineStippleFactor: TGLint read FLineStippleFactor write SetLineStippleFactor;
  576. // The line stipple
  577. property LineStipplePattern: TGLushort read FLineStipplePattern write SetLineStipplePattern;
  578. // Enable/Disable line smoothing
  579. property EnableLineSmooth: TGLboolean read FEnableLineSmooth write SetEnableLineSmooth;
  580. // Enable/Disable face culling
  581. property EnableCullFace: TGLboolean read FEnableCullFace write SetEnableCullFace;
  582. // Selects which faces to cull: front, back or front+back
  583. property CullFaceMode: TGLCullFaceMode read FCullFaceMode write SetCullFaceMode;
  584. // The winding direction that indicates a front facing primitive
  585. property FrontFace: { Cardinal } TGLFaceWinding read FFrontFace write SetFrontFace;
  586. // Enables/Disables polygon smoothing.
  587. property EnablePolygonSmooth: TGLboolean read FEnablePolygonSmooth write SetEnablePolygonSmooth;
  588. // Whether polygons appear filled, lines or points
  589. property PolygonMode: TGLPolygonMode read FPolygonMode write SetPolygonMode;
  590. // Scales the maximum depth of the polygon
  591. property PolygonOffsetFactor: TGLfloat read FPolygonOffsetFactor write SetPolygonOffsetFactor;
  592. // Scales an implementation-dependent constant that relates to the usable resolution of the depth buffer
  593. property PolygonOffsetUnits: TGLfloat read FPolygonOffsetUnits write SetPolygonOffsetUnits;
  594. // Set polygon offset
  595. procedure SetPolygonOffset(const factor, units: TGLfloat);
  596. // Enable/Disable polygon offset for polygons in point mode
  597. property EnablePolygonOffsetPoint: TGLboolean read FEnablePolygonOffsetPoint write SetEnablePolygonOffsetPoint;
  598. // Enable/Disable polygon offset for polygons in line mode
  599. property EnablePolygonOffsetLine: TGLboolean read FEnablePolygonOffsetLine write SetEnablePolygonOffsetLine;
  600. // Enable/Disable polygon offset for polygons in fill mode
  601. property EnablePolygonOffsetFill: TGLboolean read FEnablePolygonOffsetFill write SetEnablePolygonOffsetFill;
  602. // Multisample state
  603. // Enable/Disable multisampling
  604. property EnableMultisample: TGLboolean read FEnableMultisample write SetEnableMultisample;
  605. // Enable/Disable sample alpha to coverage
  606. property EnableSampleAlphaToCoverage: TGLboolean read FEnableSampleAlphaToCoverage write SetEnableSampleAlphaToCoverage;
  607. // Enable/Disable sample alpha to one
  608. property EnableSampleAlphaToOne: TGLboolean read FEnableSampleAlphaToOne write SetEnableSampleAlphaToOne;
  609. // Enable/Disable sample coverage
  610. property EnableSampleCoverage: TGLboolean read FEnableSampleCoverage write SetEnableSampleCoverage;
  611. // Sample coverage Value
  612. property SampleCoverageValue: TGLfloat read FSampleCoverageValue write SetSampleCoverageValue;
  613. // Inverts sample coverage Value
  614. property SampleCoverageInvert: TGLboolean read FSampleCoverageInvert write SetSampleCoverageInvert;
  615. // Set sample coverage
  616. procedure SetSampleCoverage(const Value: TGLfloat; invert: TGLboolean);
  617. // Enable/Disable sample mask
  618. property EnableSampleMask: TGLboolean read FEnableSampleMask write SetEnableSampleMask;
  619. // Sample mask values
  620. property SampleMaskValue[Index: Integer]: TGLbitfield read GetSampleMaskValue write SetSampleMaskValue;
  621. // Textures
  622. // Textures bound to each texture unit + binding point.
  623. property TextureBinding[Index: Integer; target: TGLTextureTarget]: Cardinal read GetTextureBinding write SetTextureBinding;
  624. property TextureBindingTime[Index: Integer; target: TGLTextureTarget]: Double read GetTextureBindingTime;
  625. property ActiveTextureEnabled[target: TGLTextureTarget]: Boolean read GetActiveTextureEnabled write SetActiveTextureEnabled;
  626. property SamplerBinding[Index: Cardinal]: Cardinal read GetSamplerBinding write SetSamplerBinding;
  627. property MaxTextureSize: Cardinal read GetMaxTextureSize;
  628. property Max3DTextureSize: Cardinal read GetMax3DTextureSize;
  629. property MaxCubeTextureSize: Cardinal read GetMaxCubeTextureSize;
  630. property MaxArrayTextureSize: Cardinal read GetMaxArrayTextureSize;
  631. property MaxTextureImageUnits: Cardinal read GetMaxTextureImageUnits;
  632. property MaxTextureAnisotropy: Cardinal read GetMaxTextureAnisotropy;
  633. property MaxSamples: Cardinal read GetMaxSamples;
  634. // TODO: GL_TEXTURE_BUFFER_DATA_STORE_BINDING ?
  635. // Active texture
  636. (* The active texture unit. Valid values are 0 .. Max texture units. *)
  637. property ActiveTexture: TGLint read FActiveTexture write SetActiveTexture;
  638. // Pixel operations
  639. (* Enables/Disables scissor test. *)
  640. property EnableScissorTest: TGLboolean read FEnableScissorTest write SetEnableScissorTest;
  641. // The bounding box used in scissor test.
  642. property ScissorBox: TVector4i read FScissorBox write SetScissorBox;
  643. // Enables/Disables stencil test.
  644. property EnableStencilTest: TGLboolean read FEnableStencilTest write SetEnableStencilTest;
  645. (* The stencil function. Determines the comparison function to be used
  646. when comparing the reference + stored stencil values. *)
  647. property StencilFunc: TGLStencilFunction read FStencilFunc;
  648. // write SetStencilFunc;
  649. (* The stencil value mask. Masks both the reference + stored stencil values *)
  650. property StencilValueMask: Cardinal read FStencilValueMask;
  651. // write SetStencilValueMask;
  652. (* The stencil reference value. Clamped to 0..255 with an 8 bit stencil. *)
  653. property StencilRef: TGLint read FStencilRef; // write SetStencilRef;
  654. // The operation to perform when stencil test fails.
  655. property StencilFail: TGLStencilOp read FStencilFail; // write SetStencilFail;
  656. // The operation to perform when stencil test passes + depth test fails.
  657. property StencilPassDepthFail: TGLStencilOp read FStencilPassDepthFail;
  658. // write SetStencilPassDepthFail;
  659. (* The operation to perform when stencil test passes + depth test passes. *)
  660. property StencilPassDepthPass: TGLStencilOp read FStencilPassDepthPass;
  661. // write SetStencilPassDepthPass;
  662. (* The stencil back function. Determines the comparison function to be
  663. used when comparing the reference + stored stencil values on back facing primitives. *)
  664. property StencilBackFunc: TGLStencilFunction read FStencilBackFunc;
  665. // write SetStencilBackFunc;
  666. (* The stencil back value mask. Masks both the reference + stored stencil values. *)
  667. property StencilBackValueMask: Cardinal read FStencilBackValueMask;
  668. // write SetStencilBackValueMask;
  669. (* The stencil back reference value. Clamped to 0..255 with an 8 bit stencil. *)
  670. property StencilBackRef: Cardinal read FStencilBackRef;
  671. // write SetStencilBackRef;
  672. (* The operation to perform when stencil test fails on back facing primitives. *)
  673. property StencilBackFail: TGLStencilOp read FStencilBackFail;
  674. // write SetStencilBackFail;
  675. (* The operation to perform when stencil test passes + depth test fails on
  676. back facing primitives. *)
  677. property StencilBackPassDepthFail: TGLStencilOp read FStencilBackPassDepthFail;
  678. // write SetStencilBackPassDepthFail;
  679. (* The operation to perform when stencil test passes + depth test passes on
  680. back facing primitives. *)
  681. property StencilBackPassDepthPass: TGLStencilOp read FStencilBackPassDepthPass;
  682. // write SetStencilBackPassDepthPass;
  683. (* Used to set stencil Function, Reference + Mask values, for both front +
  684. back facing primitives. *)
  685. procedure SetStencilFunc(const func: TGLStencilFunction; const ref: TGLint; const mask: Cardinal); inline;
  686. (* Used to set stencil Function, Reference + Mask values for either the
  687. front or back facing primitives (or both, which is the same as calling
  688. SetStencilFunc). *)
  689. procedure SetStencilFuncSeparate(const face: TGLCullFaceMode; const func: TGLStencilFunction; const ref: TGLint;
  690. const mask: Cardinal); inline;
  691. // Used to set the StencilFail, StencilPassDepthFail + StencilPassDepthPass in one go.
  692. procedure SetStencilOp(const fail, zfail, zpass: TGLStencilOp); inline;
  693. (* Used to set the StencilFail, StencilPassDepthFail + StencilPassDepthPass
  694. in one go, for either front or back facing primitives. *)
  695. procedure SetStencilOpSeparate(const face: TGLCullFaceMode; const sfail, dpfail, dppass: TGLStencilOp); inline;
  696. // Enables/disables depth testing.
  697. property EnableDepthTest: TGLboolean read FEnableDepthTest write SetEnableDepthTest;
  698. (* The depth function. Used to determine whether to keep a fragment or
  699. discard it, depending on the current value stored in the depth buffer. *)
  700. property DepthFunc: TGLDepthFunction read FDepthFunc write SetDepthFunc;
  701. // Enables/disables blending for each draw buffer.
  702. property EnableBlend[Index: Integer]: TGLboolean read GetEnableBlend write SetEnableBlend;
  703. // The weighting factor used in blending equation, for source RGB.
  704. property BlendSrcRGB: TGLBlendFunction read FBlendSrcRGB;
  705. // write SetBlendSrcRGB;
  706. (* The weighting factor used in blending equation, for source alpha. *)
  707. property BlendSrcAlpha: TGLBlendFunction read FBlendSrcAlpha;
  708. // write SetBlendSrcAlpha;
  709. (* The weighting factor used in blending equation, for destination RGB. *)
  710. property BlendDstRGB: TGLDstBlendFunction read FBlendDstRGB;
  711. // write SetBlendDstRGB;
  712. (* The weighting factor used in blending equation, for destination alpha. *)
  713. property BlendDstAlpha: TGLDstBlendFunction read FBlendDstAlpha;
  714. // write SetBlendDstAlpha;
  715. (* Sets the weighting factors to be used by the blending equation, for both color + alpha *)
  716. procedure SetBlendFunc(const Src: TGLBlendFunction; const Dst: TGLDstBlendFunction); inline;
  717. (* Sets the weighting factors to be used by the blending equation, with
  718. separate values used for color + alpha components. *)
  719. procedure SetBlendFuncSeparate(const SrcRGB: TGLBlendFunction; const DstRGB: TGLDstBlendFunction;
  720. const SrcAlpha: TGLBlendFunction; const DstAlpha: TGLDstBlendFunction); inline;
  721. (* The blending equation. Determines how the incoming source fragment's
  722. RGB are combined with the destination RGB. *)
  723. property BlendEquationRGB: TGLBlendEquation read FBlendEquationRGB;
  724. // write SetBlendEquationRGB;
  725. (* The blending equation. Determines how the incoming source fragment's
  726. alpha values are combined with the destination alpha values. *)
  727. property BlendEquationAlpha: TGLBlendEquation read FBlendEquationAlpha;
  728. // write SetBlendEquationAlpha;
  729. // Sets the blend equation for RGB + alpha to the same value.
  730. procedure SetBlendEquation(const mode: TGLBlendEquation); inline;
  731. // Sets the blend equations for RGB + alpha separately.
  732. procedure SetBlendEquationSeparate(const modeRGB, modeAlpha: TGLBlendEquation); inline;
  733. // A constant blend color, that can be used in the blend equation.
  734. property BlendColor: TVector read FBlendColor write SetBlendColor;
  735. // Enables/disables framebuffer SRGB.
  736. property EnableFramebufferSRGB: TGLboolean read FEnableFramebufferSRGB write SetEnableFramebufferSRGB;
  737. // Enables/disables dithering.
  738. property EnableDither: TGLboolean read FEnableDither write SetEnableDither;
  739. // Enables/disables color logic op.
  740. property EnableColorLogicOp: TGLboolean read FEnableColorLogicOp write SetEnableColorLogicOp;
  741. // Logic op mode.
  742. property LogicOpMode: TGLLogicOp read FLogicOpMode write SetLogicOpMode;
  743. // The color write mask, for each draw buffer.
  744. property ColorWriteMask[Index: Integer]: TGLColorMask read GetColorWriteMask write SetColorWriteMask;
  745. // Set the color write mask for all draw buffers.
  746. procedure SetColorMask(mask: TGLColorMask); inline;
  747. // The depth write mask.
  748. property DepthWriteMask: TGLboolean read FDepthWriteMask write SetDepthWriteMask;
  749. // The stencil write mask.
  750. property StencilWriteMask: Cardinal read FStencilWriteMask write SetStencilWriteMask;
  751. // The stencil back write mask.
  752. property StencilBackWriteMask: Cardinal read FStencilBackWriteMask write SetStencilBackWriteMask;
  753. // The color clear value.
  754. property ColorClearValue: TVector read FColorClearValue write SetColorClearValue;
  755. // The depth clear value.
  756. property DepthClearValue: TGLfloat read FDepthClearValue write SetDepthClearValue;
  757. // The stencil clear value.
  758. property StencilClearValue: Cardinal read FStencilClearValue write SetStencilClearValue;
  759. // Framebuffer to be used for draw operations, 0 = default framebuffer.
  760. property DrawFrameBuffer: Cardinal read FDrawFrameBuffer write SetDrawFrameBuffer;
  761. // Framebuffer to be used for read operations, 0 = default framebuffer.
  762. property ReadFrameBuffer: Cardinal read FReadFrameBuffer write SetReadFrameBuffer;
  763. // set both draw + read framebuffer.
  764. procedure SetFrameBuffer(const Value: Cardinal); inline;
  765. // property FrameBuffer: Cardinal read FDrawFrameBuffer write SetFrameBuffer;
  766. // Renderbuffer currently bound render buffer.
  767. property RenderBuffer: Cardinal read FRenderBuffer write SetRenderBuffer;
  768. // Pixels
  769. (* Controls whether byte swapping occurs during pixel unpacking. *)
  770. property UnpackSwapBytes: TGLboolean read FUnpackSwapBytes write SetUnpackSwapBytes;
  771. // Whether unpacked data is required with LSB (least significant bit) first.
  772. property UnpackLSBFirst: TGLboolean read FUnpackLSBFirst write SetUnpackLSBFirst;
  773. // Unpack image height
  774. property UnpackImageHeight: Cardinal read FUnpackImageHeight write SetUnpackImageHeight;
  775. // Unpack skip images
  776. property UnpackSkipImages: Cardinal read FUnpackSkipImages write SetUnpackSkipImages;
  777. // Unpack row length
  778. property UnpackRowLength: Cardinal read FUnpackRowLength write SetUnpackRowLength;
  779. // Unpack skip rows
  780. property UnpackSkipRows: Cardinal read FUnpackSkipRows write SetUnpackSkipRows;
  781. // Unpack skip pixels
  782. property UnpackSkipPixels: Cardinal read FUnpackSkipPixels write SetUnpackSkipPixels;
  783. // Unpack alignment
  784. property UnpackAlignment: Cardinal read FUnpackAlignment write SetUnpackAlignment;
  785. // Controls whether byte swapping occurs during pixel packing
  786. property PackSwapBytes: TGLboolean read FPackSwapBytes write SetPackSwapBytes;
  787. // Whether packed data is required with LSB (least significant bit) first
  788. property PackLSBFirst: TGLboolean read FPackLSBFirst write SetPackLSBFirst;
  789. // Pack image height
  790. property PackImageHeight: Cardinal read FPackImageHeight write SetPackImageHeight;
  791. // Pack skip images
  792. property PackSkipImages: Cardinal read FPackSkipImages write SetPackSkipImages;
  793. // Pack row length
  794. property PackRowLength: Cardinal read FPackRowLength write SetPackRowLength;
  795. // Pack skip rows
  796. property PackSkipRows: Cardinal read FPackSkipRows write SetPackSkipRows;
  797. // Pack skip pixels
  798. property PackSkipPixels: Cardinal read FPackSkipPixels write SetPackSkipPixels;
  799. // Pack alignment
  800. property PackAlignment: Cardinal read FPackAlignment write SetPackAlignment;
  801. // Buffer bound for pixel packing (eg. ReadPixels)
  802. property PixelPackBufferBinding: Cardinal read FPixelPackBufferBinding write SetPixelPackBufferBinding;
  803. // Buffer bound for pixel unpacking (eg. Tex*Image)
  804. property PixelUnpackBufferBinding: Cardinal read FPixelUnpackBufferBinding write SetPixelUnpackBufferBinding;
  805. // Currently bound program
  806. property CurrentProgram: Cardinal read FCurrentProgram write SetCurrentProgram;
  807. property MaxTextureUnits: Cardinal read GetMaxTextureUnits;
  808. // Currently bound uniform buffer
  809. property UniformBufferBinding: Cardinal read FUniformBufferBinding write SetUniformBufferBinding;
  810. procedure SetBufferIndexedBinding(const Value: Cardinal; ATarget: TGLBufferBindingTarget; AIndex: Cardinal;
  811. ABufferSize: TGLsizeiptr); overload; inline;
  812. procedure SetBufferIndexedBinding(const Value: Cardinal; ATarget: TGLBufferBindingTarget; AIndex: Cardinal;
  813. AOffset: TGLintptr; ARangeSize: TGLsizeiptr); overload; inline;
  814. // Default values to be used when a vertex array is not used for that attribute
  815. property CurrentVertexAttrib[Index: Integer]: TVector read GetCurrentVertexAttrib write SetCurrentVertexAttrib;
  816. // Enables/disables program point size
  817. property EnableProgramPointSize: TGLboolean read FEnableProgramPointSize write SetEnableProgramPointSize;
  818. // Currently bound transform feedbac buffer
  819. property TransformFeedbackBufferBinding: Cardinal read FTransformFeedbackBufferBinding
  820. write SetTransformFeedbackBufferBinding;
  821. // Line smooth hint
  822. property LineSmoothHint: TGLHintType read FLineSmoothHint write SetLineSmoothHint;
  823. // Polygon smooth hint
  824. property PolygonSmoothHint: TGLHintType read FPolygonSmoothHint write SetPolygonSmoothHint;
  825. // Texture compression hint
  826. property TextureCompressionHint: TGLHintType read FTextureCompressionHint write SetTextureCompressionHint;
  827. // Fragment shader derivitive hint
  828. property FragmentShaderDerivitiveHint: TGLHintType read FFragmentShaderDerivitiveHint write SetFragmentShaderDerivitiveHint;
  829. property MultisampleFilterHint: TGLHintType read FMultisampleFilterHint write SetMultisampleFilterHint;
  830. // Current queries
  831. property CurrentQuery[Index: TGLQueryType]: Cardinal read GetCurrentQuery;
  832. // Begins a query of "Target" type. "Value" must be a valid query object
  833. procedure BeginQuery(const target: TGLQueryType; const Value: Cardinal); inline;
  834. // Ends current query of type "Target"
  835. procedure EndQuery(const target: TGLQueryType); inline;
  836. (* The buffer currently bound to the copy read buffer binding point, this
  837. is an extra binding point provided so that you don't need to overwrite
  838. other binding points to copy between buffers. *)
  839. property CopyReadBufferBinding: Cardinal read FCopyReadBufferBinding write SetCopyReadBufferBinding;
  840. (* The buffer currently bound to the copy write buffer binding point, this
  841. is an extra binding point provided so that you don't need to overwrite
  842. other binding points to copy between buffers. *)
  843. property CopyWriteBufferBinding: Cardinal read FCopyWriteBufferBinding write SetCopyWriteBufferBinding;
  844. // Enables/Disables seamless texture cube maps
  845. property EnableTextureCubeMapSeamless: TGLboolean read FEnableTextureCubeMapSeamless write SetEnableTextureCubeMapSeamless;
  846. // Indicates the current presence within the list
  847. property InsideList: Boolean read FInsideList;
  848. // Begin new display list
  849. procedure NewList(list: Cardinal; mode: Cardinal); inline;
  850. // End display list
  851. procedure EndList; inline;
  852. // Call display list
  853. procedure CallList(list: Cardinal); inline;
  854. // Defines the OpenGL texture matrix. Assumed texture mode is GL_MODELVIEW.
  855. procedure SetGLTextureMatrix(const matrix: TMatrix); inline;
  856. procedure ResetGLTextureMatrix; inline;
  857. procedure ResetAllGLTextureMatrix; inline;
  858. // note: needs to change to per draw-buffer
  859. procedure SetGLColorWriting(flag: Boolean); inline;
  860. // Inverts front face winding (CCW/CW)
  861. procedure InvertGLFrontFace; inline;
  862. // read only properties
  863. property States: TGLStates read FStates;
  864. // True for ignore deprecated and removed features in OpenGL 3x
  865. (* property ForwardContext: Boolean read FForwardContext write SetForwardContext; *)
  866. end;
  867. type
  868. TStateRecord = record
  869. GLConst: Cardinal;
  870. GLDeprecated: Boolean;
  871. end;
  872. const
  873. {$WARN SYMBOL_DEPRECATED OFF}
  874. cGLStateTypeToGLEnum: array [TGLStateType] of Cardinal = (GL_CURRENT_BIT, GL_POINT_BIT, GL_LINE_BIT, GL_POLYGON_BIT,
  875. GL_POLYGON_STIPPLE_BIT, GL_PIXEL_MODE_BIT, GL_LIGHTING_BIT, GL_FOG_BIT, GL_DEPTH_BUFFER_BIT, GL_ACCUM_BUFFER_BIT,
  876. GL_STENCIL_BUFFER_BIT, GL_VIEWPORT_BIT, GL_TRANSFORM_BIT, GL_ENABLE_BIT, GL_COLOR_BUFFER_BIT, GL_HINT_BIT, GL_EVAL_BIT,
  877. GL_LIST_BIT, GL_TEXTURE_BIT, GL_SCISSOR_BIT, GL_MULTISAMPLE_BIT);
  878. {$WARN SYMBOL_DEPRECATED ON}
  879. cGLStateToGLEnum: array[TGLState] of TStateRecord =
  880. ((GLConst: GL_ALPHA_TEST; GLDeprecated: True),
  881. (GLConst: GL_AUTO_NORMAL; GLDeprecated: True),
  882. (GLConst: GL_BLEND; GLDeprecated: False),
  883. (GLConst: GL_COLOR_MATERIAL; GLDeprecated: True),
  884. (GLConst: GL_CULL_FACE; GLDeprecated: False),
  885. (GLConst: GL_DEPTH_TEST; GLDeprecated: False),
  886. (GLConst: GL_DITHER; GLDeprecated: False),
  887. (GLConst: GL_FOG; GLDeprecated: True),
  888. (GLConst: GL_LIGHTING; GLDeprecated: True),
  889. (GLConst: GL_LINE_SMOOTH; GLDeprecated: True),
  890. (GLConst: GL_LINE_STIPPLE; GLDeprecated: True),
  891. (GLConst: GL_INDEX_LOGIC_OP; GLDeprecated: True),
  892. (GLConst: GL_COLOR_LOGIC_OP; GLDeprecated: False),
  893. (GLConst: GL_NORMALIZE; GLDeprecated: True),
  894. (GLConst: GL_POINT_SMOOTH; GLDeprecated: True),
  895. (GLConst: GL_POINT_SPRITE; GLDeprecated: True),
  896. (GLConst: GL_POLYGON_SMOOTH; GLDeprecated: True),
  897. (GLConst: GL_POLYGON_STIPPLE; GLDeprecated: True),
  898. (GLConst: GL_SCISSOR_TEST; GLDeprecated: False),
  899. (GLConst: GL_STENCIL_TEST; GLDeprecated: False),
  900. (GLConst: GL_POLYGON_OFFSET_POINT; GLDeprecated: False),
  901. (GLConst: GL_POLYGON_OFFSET_LINE; GLDeprecated: False),
  902. (GLConst: GL_POLYGON_OFFSET_FILL; GLDeprecated: False),
  903. (GLConst: GL_DEPTH_CLAMP; GLDeprecated: False)
  904. );
  905. cGLTexTypeToGLEnum: array[TGLTextureTarget] of Cardinal =
  906. (0, GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_1D_ARRAY,
  907. GL_TEXTURE_2D_ARRAY, GL_TEXTURE_RECTANGLE, GL_TEXTURE_BUFFER,
  908. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_2D_MULTISAMPLE,
  909. GL_TEXTURE_2D_MULTISAMPLE_ARRAY, GL_TEXTURE_CUBE_MAP_ARRAY);
  910. cGLQueryTypeToGLEnum: array[TGLQueryType] of Cardinal =
  911. (GL_SAMPLES_PASSED, GL_PRIMITIVES_GENERATED,
  912. GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN,
  913. GL_TIME_ELAPSED, GL_ANY_SAMPLES_PASSED);
  914. cGLStencilOpToGLEnum: array[TGLStencilOp] of Cardinal =
  915. (GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP,
  916. GL_DECR_WRAP);
  917. cGLLogicOpToGLEnum: array[TGLLogicOp] of Cardinal =
  918. (GL_CLEAR, GL_AND, GL_AND_REVERSE, GL_COPY, GL_AND_INVERTED, GL_NOOP,
  919. GL_XOR, GL_OR, GL_NOR, GL_EQUIV, GL_INVERT, GL_OR_REVERSE,
  920. GL_COPY_INVERTED, GL_OR_INVERTED, GL_NAND, GL_SET);
  921. cGLComparisonFunctionToGLEnum: array[TGLComparisonFunction] of Cardinal =
  922. (GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GREATER,
  923. GL_NOTEQUAL, GL_GEQUAL);
  924. cGLBlendFunctionToGLEnum: array[TGLBlendFunction] of Cardinal =
  925. (GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, GL_DST_COLOR,
  926. GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
  927. GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_CONSTANT_COLOR,
  928. GL_ONE_MINUS_CONSTANT_COLOR, GL_CONSTANT_ALPHA,
  929. GL_ONE_MINUS_CONSTANT_ALPHA, GL_SRC_ALPHA_SATURATE {valid for src only});
  930. cGLBlendEquationToGLEnum: array[TGLBlendEquation] of Cardinal =
  931. (GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT, GL_MIN,
  932. GL_MAX);
  933. cGLFaceWindingToGLEnum: array[TGLFaceWinding] of Cardinal =
  934. (GL_CCW, GL_CW);
  935. cGLPolygonModeToGLEnum: array[TGLPolygonMode] of Cardinal =
  936. (GL_FILL, GL_LINE, GL_POINT);
  937. cGLCullFaceModeToGLEnum: array[TGLCullFaceMode] of Cardinal =
  938. (GL_FRONT, GL_BACK, GL_FRONT_AND_BACK);
  939. cGLHintToGLEnum: array[TGLHintType] of Cardinal =
  940. (GL_DONT_CARE, GL_FASTEST, GL_NICEST);
  941. cGLBufferBindingTarget: array[TGLBufferBindingTarget] of Cardinal =
  942. (GL_UNIFORM_BUFFER, GL_TRANSFORM_FEEDBACK_BUFFER);
  943. //------------------------------------------------------
  944. implementation
  945. // ------------------------------------------------------
  946. uses
  947. GLContext,
  948. GLColor;
  949. // ------------------
  950. // ------------------ TGLStateCache ------------------
  951. // ------------------
  952. procedure TGLStateCache.BeginQuery(const target: TGLQueryType; const Value: Cardinal);
  953. begin
  954. Assert(FCurrentQuery[target] = 0, 'Can only have one query (of each type)' + ' running at a time');
  955. // Assert(glIsQuery(Value), 'Not a valid query');
  956. // if Value<>FCurrentQuery[Target] then
  957. begin
  958. FCurrentQuery[target] := Value;
  959. gl.BeginQuery(cGLQueryTypeToGLEnum[target], Value);
  960. end;
  961. end;
  962. constructor TGLStateCache.Create;
  963. var
  964. I: Integer;
  965. begin
  966. inherited;
  967. SetLength(FListStates, $FFFFF);
  968. FCurrentList := 0;
  969. // Material colors
  970. FFrontBackColors[0][0] := clrBlack;
  971. FFrontBackColors[0][1] := clrGray20;
  972. FFrontBackColors[0][2] := clrGray80;
  973. FFrontBackColors[0][3] := clrBlack;
  974. FFrontBackShininess[0] := 0;
  975. FFrontBackColors[1][0] := clrBlack;
  976. FFrontBackColors[1][1] := clrGray20;
  977. FFrontBackColors[1][2] := clrGray80;
  978. FFrontBackColors[1][3] := clrBlack;
  979. FFrontBackShininess[1] := 0;
  980. FAlphaFunc := cfAlways;
  981. // Lighting
  982. FFFPLight := True;
  983. FMaxLights := 0;
  984. FLightNumber := 0;
  985. for I := High(FLightEnabling) downto 0 do
  986. begin
  987. FLightEnabling[I] := False;
  988. FLightIndices[I] := 0;
  989. FLightStates.Position[I] := NullHmgVector;
  990. FLightStates.Ambient[I] := clrBlack;
  991. FLightStates.Diffuse[I] := clrBlack;
  992. FLightStates.Specular[I] := clrBlack;
  993. FLightStates.SpotDirection[I] := VectorMake(0.0, 0.0, -1.0, 0.0);
  994. FSpotCutoff[I] := 180.0;
  995. FLightStates.SpotCosCutoffExponent[I].X := -1;
  996. FLightStates.SpotCosCutoffExponent[I].Y := 0;
  997. FLightStates.Attenuation[I] := NullHmgVector;
  998. end;
  999. FLightStates.Diffuse[0] := clrWhite;
  1000. FLightStates.Specular[0] := clrWhite;
  1001. for I := High(FTextureMatrixIsIdentity) downto 0 do
  1002. FTextureMatrixIsIdentity[I] := False;
  1003. // FForwardContext := False;
  1004. // Vertex Array Data state
  1005. FVertexArrayBinding := 0;
  1006. FTextureBufferBinding := 0;
  1007. // Transformation state
  1008. // FViewPort := Rect(0,0,0,0); // (0, 0, Width, Height)
  1009. FDepthRange[0] := 0.0;
  1010. FDepthRange[1] := 1.0;
  1011. FillChar(FEnableClipDistance, sizeof(FEnableClipDistance), $00);
  1012. FEnableDepthClamp := False;
  1013. // Coloring state
  1014. FClampReadColor := GL_FIXED_ONLY;
  1015. FProvokingVertex := GL_LAST_VERTEX_CONVENTION;
  1016. // Rasterization state
  1017. FPointSize := 1.0;
  1018. FPointFadeThresholdSize := 1.0;
  1019. FPointSpriteCoordOrigin := GL_UPPER_LEFT;
  1020. FLineWidth := 1.0;
  1021. FLineStippleFactor := 1;
  1022. FLineStipplePattern := $FFFF;
  1023. FEnableLineSmooth := False;
  1024. FEnableCullFace := False;
  1025. FCullFaceMode := cmBack;
  1026. FFrontFace := fwCounterClockWise;
  1027. FEnablePolygonSmooth := False;
  1028. FPolygonMode := pmFill;
  1029. FPolygonOffsetFactor := 0.0;
  1030. FPolygonOffsetUnits := 0.0;
  1031. FEnablePolygonOffsetPoint := False;
  1032. FEnablePolygonOffsetLine := False;
  1033. FEnablePolygonOffsetFill := False;
  1034. // Multisample state
  1035. FEnableMultisample := True;
  1036. FEnableSampleAlphaToCoverage := False;
  1037. FEnableSampleAlphaToOne := False;
  1038. FEnableSampleCoverage := False;
  1039. FSampleCoverageValue := 1.0;
  1040. FSampleCoverageInvert := False;
  1041. FEnableSampleMask := False;
  1042. FillChar(FSampleMaskValue, sizeof(FSampleMaskValue), $FF);
  1043. // Texture state
  1044. FillChar(FTextureBinding, sizeof(FTextureBinding), $00);
  1045. FillChar(FActiveTextureEnabling, sizeof(FActiveTextureEnabling), $00);
  1046. // Active texture state
  1047. FActiveTexture := 0;
  1048. // Pixel operation state
  1049. FEnableScissorTest := False;
  1050. // FScissorBox := Rect(0, 0, Width, Height);
  1051. FEnableStencilTest := False;
  1052. FStencilFunc := cfAlways;
  1053. FStencilValueMask := $FFFFFFFF;
  1054. FStencilRef := 0;
  1055. FStencilFail := soKeep;
  1056. FStencilPassDepthFail := soKeep;
  1057. FStencilPassDepthPass := soKeep;
  1058. FStencilBackFunc := cfAlways;
  1059. FStencilBackValueMask := $FFFFFFFF;
  1060. FStencilBackRef := 0;
  1061. FStencilBackFail := soKeep;
  1062. FStencilBackPassDepthPass := soKeep;
  1063. FStencilBackPassDepthFail := soKeep;
  1064. FEnableDepthTest := False;
  1065. FDepthFunc := cfLess;
  1066. FillChar(FEnableBlend, sizeof(FEnableBlend), $0);
  1067. FBlendSrcRGB := bfOne;
  1068. FBlendSrcAlpha := bfOne;
  1069. FBlendDstRGB := bfZero;
  1070. FBlendDstAlpha := bfZero;
  1071. FBlendEquationRGB := beAdd;
  1072. FBlendEquationAlpha := beAdd;
  1073. FBlendColor := NullHmgVector;
  1074. FEnableFramebufferSRGB := False;
  1075. FEnableDither := True;
  1076. FEnableColorLogicOp := False;
  1077. FLogicOpMode := loCopy;
  1078. // Framebuffer control state
  1079. // for I := 0 to Length(FColorWriteMask) - 1 do
  1080. // FColorWriteMask[i] := [ccRed, ccGreen, ccBlue, ccAlpha];
  1081. FillChar(FColorWriteMask, sizeof(FColorWriteMask), $F);
  1082. FDepthWriteMask := True;
  1083. FStencilWriteMask := $FFFFFFFF;
  1084. FStencilBackWriteMask := $FFFFFFFF;
  1085. FColorClearValue := NullHmgVector;
  1086. FDepthClearValue := 1.0;
  1087. FStencilClearValue := 0;
  1088. // Framebuffer state
  1089. FDrawFrameBuffer := 0;
  1090. FReadFrameBuffer := 0;
  1091. // Renderbuffer state
  1092. FRenderBuffer := 0;
  1093. // Pixels state
  1094. FUnpackSwapBytes := False;
  1095. FUnpackLSBFirst := False;
  1096. FUnpackImageHeight := 0;
  1097. FUnpackSkipImages := 0;
  1098. FUnpackRowLength := 0;
  1099. FUnpackSkipRows := 0;
  1100. FUnpackSkipPixels := 0;
  1101. FUnpackAlignment := 4;
  1102. FPackSwapBytes := False;
  1103. FPackLSBFirst := False;
  1104. FPackImageHeight := 0;
  1105. FPackSkipImages := 0;
  1106. FPackRowLength := 0;
  1107. FPackSkipRows := 0;
  1108. FPackSkipPixels := 0;
  1109. FPackAlignment := 4;
  1110. FPixelPackBufferBinding := 0;
  1111. FPixelUnpackBufferBinding := 0;
  1112. // Program state
  1113. FCurrentProgram := 0;
  1114. FUniformBufferBinding := 0;
  1115. FillChar(FUBOStates[bbtUniform][0], sizeof(FUBOStates), $00);
  1116. // Vector + Geometry Shader state
  1117. for I := 0 to Length(FCurrentVertexAttrib) - 1 do
  1118. FCurrentVertexAttrib[I] := NullHmgPoint;
  1119. FEnableProgramPointSize := False;
  1120. // Transform Feedback state
  1121. FTransformFeedbackBufferBinding := 0;
  1122. // Hints state
  1123. FTextureCompressionHint := hintDontCare;
  1124. FPolygonSmoothHint := hintDontCare;
  1125. FFragmentShaderDerivitiveHint := hintDontCare;
  1126. FLineSmoothHint := hintDontCare;
  1127. // Misc state
  1128. FillChar(FCurrentQuery, sizeof(FCurrentQuery), $00);
  1129. FCopyReadBufferBinding := 0;
  1130. FCopyWriteBufferBinding := 0;
  1131. FEnableTextureCubeMapSeamless := False;
  1132. FInsideList := False;
  1133. end;
  1134. destructor TGLStateCache.Destroy;
  1135. begin
  1136. inherited;
  1137. end;
  1138. procedure TGLStateCache.EndQuery(const target: TGLQueryType);
  1139. begin
  1140. Assert(FCurrentQuery[target] <> 0, 'No query running');
  1141. FCurrentQuery[target] := 0;
  1142. gl.EndQuery(cGLQueryTypeToGLEnum[target]);
  1143. end;
  1144. procedure TGLStateCache.Enable(const aState: TGLState);
  1145. begin
  1146. { if cGLStateToGLEnum[aState].GLDeprecated and FForwardContext then
  1147. exit; }
  1148. if not(aState in FStates) or FInsideList then
  1149. begin
  1150. if FInsideList then
  1151. Include(FListStates[FCurrentList], sttEnable)
  1152. else
  1153. Include(FStates, aState);
  1154. {$IFDEF USE_CACHE_MISS_CHECK}
  1155. if gl.IsEnabled(cGLStateToGLEnum[aState].GLConst) then
  1156. GLSLogger.LogError(strStateCashMissing + 'Enable');
  1157. {$ENDIF}
  1158. gl.Enable(cGLStateToGLEnum[aState].GLConst);
  1159. end;
  1160. end;
  1161. procedure TGLStateCache.Disable(const aState: TGLState);
  1162. begin
  1163. { if cGLStateToGLEnum[aState].GLDeprecated and FForwardContext then
  1164. exit; }
  1165. if (aState in FStates) or FInsideList then
  1166. begin
  1167. if FInsideList then
  1168. Include(FListStates[FCurrentList], sttEnable)
  1169. else
  1170. Exclude(FStates, aState);
  1171. {$IFDEF USE_CACHE_MISS_CHECK}
  1172. if not gl.IsEnabled(cGLStateToGLEnum[aState].GLConst) then
  1173. GLSLogger.LogError(strStateCashMissing + 'Disable');
  1174. {$ENDIF}
  1175. gl.Disable(cGLStateToGLEnum[aState].GLConst);
  1176. if aState = stColorMaterial then
  1177. if FInsideList then
  1178. Include(FListStates[FCurrentList], sttLighting)
  1179. else
  1180. begin
  1181. gl.Materialfv(GL_FRONT, GL_EMISSION, @FFrontBackColors[0][0]);
  1182. gl.Materialfv(GL_FRONT, GL_AMBIENT, @FFrontBackColors[0][1]);
  1183. gl.Materialfv(GL_FRONT, GL_DIFFUSE, @FFrontBackColors[0][2]);
  1184. gl.Materialfv(GL_FRONT, GL_SPECULAR, @FFrontBackColors[0][3]);
  1185. gl.Materiali(GL_FRONT, GL_SHININESS, FFrontBackShininess[0]);
  1186. gl.Materialfv(GL_BACK, GL_EMISSION, @FFrontBackColors[1][0]);
  1187. gl.Materialfv(GL_BACK, GL_AMBIENT, @FFrontBackColors[1][1]);
  1188. gl.Materialfv(GL_BACK, GL_DIFFUSE, @FFrontBackColors[1][2]);
  1189. gl.Materialfv(GL_BACK, GL_SPECULAR, @FFrontBackColors[1][3]);
  1190. gl.Materiali(GL_BACK, GL_SHININESS, FFrontBackShininess[1]);
  1191. end;
  1192. end;
  1193. end;
  1194. procedure TGLStateCache.PerformEnable(const aState: TGLState);
  1195. begin
  1196. { if cGLStateToGLEnum[aState].GLDeprecated and FForwardContext then
  1197. exit; }
  1198. Include(FStates, aState);
  1199. gl.Enable(cGLStateToGLEnum[aState].GLConst);
  1200. end;
  1201. procedure TGLStateCache.PerformDisable(const aState: TGLState);
  1202. begin
  1203. { if cGLStateToGLEnum[aState].GLDeprecated and FForwardContext then
  1204. exit; }
  1205. Exclude(FStates, aState);
  1206. gl.Disable(cGLStateToGLEnum[aState].GLConst);
  1207. end;
  1208. procedure TGLStateCache.PopAttrib;
  1209. begin
  1210. // TODO: replace with proper client side push/pop
  1211. gl.PopAttrib();
  1212. end;
  1213. procedure TGLStateCache.PushAttrib(const stateTypes: TGLStateTypes);
  1214. var
  1215. tempFlag: Cardinal;
  1216. I: Integer;
  1217. begin
  1218. // TODO: replace with proper client side push/pop
  1219. tempFlag := 0;
  1220. for I := Integer(Low(TGLStateType)) to Integer(high(TGLStateType)) do
  1221. begin
  1222. if TGLStateType(I) in stateTypes then
  1223. begin
  1224. tempFlag := tempFlag or cGLStateTypeToGLEnum[TGLStateType(I)];
  1225. end;
  1226. end;
  1227. gl.PushAttrib(tempFlag);
  1228. end;
  1229. procedure TGLStateCache.SetGLMaterialColors(const aFace: TGLCullFaceMode;
  1230. const emission, ambient, diffuse, specular: TVector;
  1231. const shininess: Integer);
  1232. var
  1233. I: Integer;
  1234. currentFace: Cardinal;
  1235. begin
  1236. { if FForwardContext then
  1237. exit; }
  1238. Assert((aFace = cmFront) or (aFace = cmBack), 'Only cmFront or cmBack supported');
  1239. I := Integer(aFace);
  1240. currentFace := cGLCullFaceModeToGLEnum[aFace];
  1241. if FInsideList then
  1242. begin
  1243. gl.Materiali(currentFace, GL_SHININESS, shininess);
  1244. gl.Materialfv(currentFace, GL_EMISSION, @emission);
  1245. gl.Materialfv(currentFace, GL_AMBIENT, @Ambient);
  1246. gl.Materialfv(currentFace, GL_DIFFUSE, @Diffuse);
  1247. gl.Materialfv(currentFace, GL_SPECULAR, @Specular);
  1248. Include(FListStates[FCurrentList], sttLighting);
  1249. end
  1250. else
  1251. begin
  1252. if (FFrontBackShininess[I] <> shininess) then
  1253. begin
  1254. gl.Materiali(currentFace, GL_SHININESS, shininess);
  1255. FFrontBackShininess[I] := shininess;
  1256. end;
  1257. if not AffineVectorEquals(FFrontBackColors[I][0], emission) then
  1258. begin
  1259. gl.Materialfv(currentFace, GL_EMISSION, @emission);
  1260. SetVector(FFrontBackColors[I][0], emission);
  1261. end;
  1262. if not AffineVectorEquals(FFrontBackColors[I][1], Ambient) then
  1263. begin
  1264. gl.Materialfv(currentFace, GL_AMBIENT, @Ambient);
  1265. SetVector(FFrontBackColors[I][1], Ambient);
  1266. end;
  1267. if not VectorEquals(FFrontBackColors[I][2], Diffuse) then
  1268. begin
  1269. gl.Materialfv(currentFace, GL_DIFFUSE, @Diffuse);
  1270. SetVector(FFrontBackColors[I][2], Diffuse);
  1271. end;
  1272. if not AffineVectorEquals(FFrontBackColors[I][3], Specular) then
  1273. begin
  1274. gl.Materialfv(currentFace, GL_SPECULAR, @Specular);
  1275. SetVector(FFrontBackColors[I][3], Specular);
  1276. end;
  1277. end;
  1278. end;
  1279. procedure TGLStateCache.SetGLMaterialAlphaChannel(const aFace: Cardinal; const alpha: TGLfloat);
  1280. var
  1281. I: Integer;
  1282. color: TVector4f;
  1283. begin
  1284. { if FForwardContext then Exit; }
  1285. if not(stLighting in FStates) then
  1286. begin
  1287. // We need a temp variable, because FColor is cauched.
  1288. gl.GetFloatv(GL_CURRENT_COLOR, @color);
  1289. color.W := alpha;
  1290. gl.Color4fv(@color);
  1291. end
  1292. else
  1293. begin
  1294. I := aFace - GL_FRONT;
  1295. if (FFrontBackColors[I][2].W <> alpha) or FInsideList then
  1296. begin
  1297. if FInsideList then
  1298. begin
  1299. Include(FListStates[FCurrentList], sttLighting);
  1300. gl.Materialfv(aFace, GL_DIFFUSE, @FFrontBackColors[I][2]);
  1301. end
  1302. else
  1303. begin
  1304. FFrontBackColors[I][2].W := alpha;
  1305. gl.Materialfv(aFace, GL_DIFFUSE, @FFrontBackColors[I][2]);
  1306. end;
  1307. end;
  1308. end;
  1309. end;
  1310. procedure TGLStateCache.SetGLMaterialDiffuseColor(const aFace: Cardinal; const Diffuse: TVector);
  1311. var
  1312. I: Integer;
  1313. begin
  1314. { if FForwardContext then Exit; }
  1315. if not(stLighting in FStates) then
  1316. begin
  1317. gl.Color4fv(@Diffuse);
  1318. end
  1319. else
  1320. begin
  1321. //
  1322. I := aFace - GL_FRONT;
  1323. if (not VectorEquals(FFrontBackColors[I][2], Diffuse)) or FInsideList then
  1324. begin
  1325. if FInsideList then
  1326. begin
  1327. Include(FListStates[FCurrentList], sttLighting);
  1328. gl.Materialfv(aFace, GL_DIFFUSE, @FFrontBackColors[I][2]);
  1329. end
  1330. else
  1331. begin
  1332. FFrontBackColors[I][2] := Diffuse;
  1333. gl.Materialfv(aFace, GL_DIFFUSE, @Diffuse);
  1334. end;
  1335. end;
  1336. end;
  1337. end;
  1338. procedure TGLStateCache.SetActiveTexture(const Value: TGLint);
  1339. begin
  1340. if gl.ARB_multitexture then
  1341. if (Value <> FActiveTexture) or FInsideList then
  1342. begin
  1343. if FInsideList then
  1344. Include(FListStates[FCurrentList], sttTexture)
  1345. else
  1346. FActiveTexture := Value;
  1347. gl.ActiveTexture(GL_TEXTURE0 + Value);
  1348. end;
  1349. end;
  1350. procedure TGLStateCache.SetVertexArrayBinding(const Value: Cardinal);
  1351. begin
  1352. if Value <> FVertexArrayBinding then
  1353. begin
  1354. FVertexArrayBinding := Value;
  1355. gl.BindVertexArray(Value);
  1356. end;
  1357. end;
  1358. function TGLStateCache.GetArrayBufferBinding: Cardinal;
  1359. begin
  1360. Result := FArrayBufferBinding;
  1361. end;
  1362. procedure TGLStateCache.SetArrayBufferBinding(const Value: Cardinal);
  1363. begin
  1364. if (Value <> FArrayBufferBinding) or (FVertexArrayBinding <> 0) then
  1365. begin
  1366. FArrayBufferBinding := Value;
  1367. gl.BindBuffer(GL_ARRAY_BUFFER, Value);
  1368. end;
  1369. end;
  1370. function TGLStateCache.GetElementBufferBinding: Cardinal;
  1371. begin
  1372. Result := FElementBufferBinding
  1373. end;
  1374. procedure TGLStateCache.SetElementBufferBinding(const Value: Cardinal);
  1375. begin
  1376. if (Value <> FElementBufferBinding) or (FVertexArrayBinding <> 0) then
  1377. begin
  1378. FElementBufferBinding := Value;
  1379. gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, Value);
  1380. end;
  1381. end;
  1382. function TGLStateCache.GetEnablePrimitiveRestart: TGLboolean;
  1383. begin
  1384. Result := FEnablePrimitiveRestart;
  1385. end;
  1386. procedure TGLStateCache.SetEnablePrimitiveRestart(const enabled: TGLboolean);
  1387. begin
  1388. if enabled <> FEnablePrimitiveRestart then
  1389. begin
  1390. FEnablePrimitiveRestart := enabled;
  1391. if GL.NV_primitive_restart then
  1392. begin
  1393. if enabled then
  1394. gl.EnableClientState(GL_PRIMITIVE_RESTART_NV)
  1395. else
  1396. gl.DisableClientState(GL_PRIMITIVE_RESTART_NV);
  1397. end
  1398. else
  1399. begin
  1400. if enabled then
  1401. gl.Enable(GL_PRIMITIVE_RESTART)
  1402. else
  1403. gl.Disable(GL_PRIMITIVE_RESTART);
  1404. end;
  1405. end;
  1406. end;
  1407. function TGLStateCache.GetPrimitiveRestartIndex: Cardinal;
  1408. begin
  1409. Result := FPrimitiveRestartIndex;
  1410. end;
  1411. procedure TGLStateCache.SetPrimitiveRestartIndex(const index: Cardinal);
  1412. begin
  1413. if index <> FPrimitiveRestartIndex then
  1414. begin
  1415. if gl.NV_primitive_restart then
  1416. begin
  1417. FPrimitiveRestartIndex := index;
  1418. gl.PrimitiveRestartIndex(index)
  1419. end;
  1420. end;
  1421. end;
  1422. procedure TGLStateCache.SetEnableProgramPointSize(const Value: TGLboolean);
  1423. begin
  1424. if Value <> FEnableProgramPointSize then
  1425. begin
  1426. FEnableProgramPointSize := Value;
  1427. if Value then
  1428. gl.Enable(GL_PROGRAM_POINT_SIZE)
  1429. else
  1430. gl.Disable(GL_PROGRAM_POINT_SIZE);
  1431. end;
  1432. end;
  1433. procedure TGLStateCache.SetBlendColor(const Value: TVector);
  1434. begin
  1435. if not VectorEquals(Value, FBlendColor) or FInsideList then
  1436. begin
  1437. if FInsideList then
  1438. Include(FListStates[FCurrentList], sttColorBuffer)
  1439. else
  1440. FBlendColor := Value;
  1441. gl.BlendColor(Value.X, Value.Y, Value.Z, Value.W);
  1442. end;
  1443. end;
  1444. procedure TGLStateCache.SetBlendEquationSeparate(const modeRGB, modeAlpha: TGLBlendEquation);
  1445. begin
  1446. if (modeRGB <> FBlendEquationRGB) or (modeAlpha <> FBlendEquationAlpha) or FInsideList then
  1447. begin
  1448. FBlendEquationRGB := modeRGB;
  1449. FBlendEquationAlpha := modeAlpha;
  1450. gl.BlendEquationSeparate(cGLBlendEquationToGLEnum[modeRGB], cGLBlendEquationToGLEnum[modeAlpha]);
  1451. end;
  1452. if FInsideList then
  1453. Include(FListStates[FCurrentList], sttColorBuffer);
  1454. end;
  1455. procedure TGLStateCache.SetBlendEquation(const mode: TGLBlendEquation);
  1456. begin
  1457. if (mode <> FBlendEquationRGB) or (mode <> FBlendEquationAlpha) or FInsideList then
  1458. begin
  1459. if FInsideList then
  1460. Include(FListStates[FCurrentList], sttColorBuffer)
  1461. else
  1462. begin
  1463. FBlendEquationRGB := mode;
  1464. FBlendEquationAlpha := mode;
  1465. end;
  1466. gl.BlendEquation(cGLBlendEquationToGLEnum[mode]);
  1467. end;
  1468. end;
  1469. procedure TGLStateCache.SetBlendFunc(const Src: TGLBlendFunction; const Dst: TGLDstBlendFunction);
  1470. begin
  1471. if (Src <> FBlendSrcRGB) or (Dst <> FBlendDstRGB) or FInsideList then
  1472. begin
  1473. if FInsideList then
  1474. Include(FListStates[FCurrentList], sttColorBuffer)
  1475. else
  1476. begin
  1477. FBlendSrcRGB := Src;
  1478. FBlendDstRGB := Dst;
  1479. FBlendSrcAlpha := Src;
  1480. FBlendSrcAlpha := Dst;
  1481. end;
  1482. gl.BlendFunc(cGLBlendFunctionToGLEnum[Src], cGLBlendFunctionToGLEnum[Dst]);
  1483. end;
  1484. end;
  1485. procedure TGLStateCache.SetBlendFuncSeparate(const SrcRGB: TGLBlendFunction;
  1486. const DstRGB: TGLDstBlendFunction; const SrcAlpha: TGLBlendFunction; const DstAlpha: TGLDstBlendFunction);
  1487. begin
  1488. if (SrcRGB <> FBlendSrcRGB) or (DstRGB <> FBlendDstRGB) or
  1489. (SrcAlpha <> FBlendSrcAlpha) or (DstAlpha <> FBlendDstAlpha)
  1490. or FInsideList then
  1491. begin
  1492. if FInsideList then
  1493. Include(FListStates[FCurrentList], sttColorBuffer)
  1494. else
  1495. begin
  1496. FBlendSrcRGB := SrcRGB;
  1497. FBlendDstRGB := DstRGB;
  1498. FBlendSrcAlpha := SrcAlpha;
  1499. FBlendDstAlpha := DstAlpha;
  1500. end;
  1501. gl.BlendFuncSeparate(
  1502. cGLBlendFunctionToGLEnum[SrcRGB],
  1503. cGLBlendFunctionToGLEnum[DstRGB],
  1504. cGLBlendFunctionToGLEnum[SrcAlpha],
  1505. cGLBlendFunctionToGLEnum[DstAlpha]);
  1506. end;
  1507. end;
  1508. procedure TGLStateCache.SetClampReadColor(const Value: Cardinal);
  1509. begin
  1510. if (Value <> FClampReadColor) or FInsideList then
  1511. begin
  1512. if FInsideList then
  1513. Include(FListStates[FCurrentList], sttColorBuffer)
  1514. else
  1515. FClampReadColor := Value;
  1516. gl.ClampColor(GL_CLAMP_READ_COLOR, Value);
  1517. end;
  1518. end;
  1519. procedure TGLStateCache.SetColorWriteMask(index: Integer; const Value: TGLColorMask);
  1520. begin
  1521. if FColorWriteMask[Index] <> Value then
  1522. begin
  1523. FColorWriteMask[Index] := Value;
  1524. gl.ColorMaski(Index, ccRed in Value, ccGreen in Value, ccBlue in Value, ccAlpha in Value);
  1525. end;
  1526. end;
  1527. procedure TGLStateCache.SetCopyReadBufferBinding(const Value: Cardinal);
  1528. begin
  1529. if Value <> FCopyReadBufferBinding then
  1530. begin
  1531. FCopyReadBufferBinding := Value;
  1532. gl.BindBuffer(GL_COPY_READ_BUFFER, Value);
  1533. end;
  1534. end;
  1535. procedure TGLStateCache.SetCopyWriteBufferBinding(const Value: Cardinal);
  1536. begin
  1537. if Value <> FCopyWriteBufferBinding then
  1538. begin
  1539. FCopyWriteBufferBinding := Value;
  1540. gl.BindBuffer(GL_COPY_WRITE_BUFFER, Value);
  1541. end;
  1542. end;
  1543. procedure TGLStateCache.SetCullFaceMode(const Value: TGLCullFaceMode);
  1544. begin
  1545. if (Value <> FCullFaceMode) or FInsideList then
  1546. begin
  1547. if FInsideList then
  1548. Include(FListStates[FCurrentList], sttPolygon)
  1549. else
  1550. FCullFaceMode := Value;
  1551. gl.CullFace(cGLCullFaceModeToGLEnum[Value]);
  1552. end;
  1553. end;
  1554. procedure TGLStateCache.SetCurrentProgram(const Value: Cardinal);
  1555. begin
  1556. if Value <> FCurrentProgram then
  1557. begin
  1558. FCurrentProgram := Value;
  1559. gl.UseProgram(Value);
  1560. end;
  1561. end;
  1562. procedure TGLStateCache.SetTextureBufferBinding(const Value: Cardinal);
  1563. begin
  1564. if Value <> FTextureBufferBinding then
  1565. begin
  1566. FTextureBufferBinding := Value;
  1567. gl.BindBuffer(GL_TEXTURE_BUFFER, Value);
  1568. end;
  1569. end;
  1570. procedure TGLStateCache.SetCurrentVertexAttrib(index: Integer; const Value: TVector);
  1571. begin
  1572. if not VectorEquals(Value, FCurrentVertexAttrib[Index]) then
  1573. begin
  1574. FCurrentVertexAttrib[Index] := Value;
  1575. gl.VertexAttrib4fv(Index, @Value.X);
  1576. end;
  1577. end;
  1578. procedure TGLStateCache.SetDepthClearValue(const Value: TGLfloat);
  1579. begin
  1580. if (Value <> FDepthClearValue) or FInsideList then
  1581. begin
  1582. if FInsideList then
  1583. Include(FListStates[FCurrentList], sttDepthBuffer)
  1584. else
  1585. FDepthClearValue := Value;
  1586. gl.ClearDepth(Value);
  1587. end;
  1588. end;
  1589. procedure TGLStateCache.SetDepthFunc(const Value: TGLDepthFunction);
  1590. begin
  1591. if (Value <> FDepthFunc) or FInsideList then
  1592. begin
  1593. if FInsideList then
  1594. Include(FListStates[FCurrentList], sttDepthBuffer)
  1595. else
  1596. FDepthFunc := Value;
  1597. gl.DepthFunc(cGLComparisonFunctionToGLEnum[Value]);
  1598. end;
  1599. end;
  1600. procedure TGLStateCache.SetDepthRange(const ZNear, ZFar: TGLclampd);
  1601. begin
  1602. if (ZNear <> FDepthRange[0]) or (ZFar <> FDepthRange[1]) or FInsideList then
  1603. begin
  1604. if FInsideList then
  1605. Include(FListStates[FCurrentList], sttViewport)
  1606. else
  1607. begin
  1608. FDepthRange[0] := ZNear;
  1609. FDepthRange[1] := ZFar;
  1610. end;
  1611. gl.DepthRange(ZNear, ZFar);
  1612. end;
  1613. end;
  1614. procedure TGLStateCache.SetDepthRangeFar(const Value: TGLclampd);
  1615. begin
  1616. if (Value <> FDepthRange[1]) or FInsideList then
  1617. begin
  1618. if FInsideList then
  1619. Include(FListStates[FCurrentList], sttViewport)
  1620. else
  1621. FDepthRange[1] := Value;
  1622. gl.DepthRange(FDepthRange[0], Value);
  1623. end;
  1624. end;
  1625. procedure TGLStateCache.SetDepthRangeNear(const Value: TGLclampd);
  1626. begin
  1627. if (Value <> FDepthRange[0]) or FInsideList then
  1628. begin
  1629. if FInsideList then
  1630. Include(FListStates[FCurrentList], sttViewport)
  1631. else
  1632. FDepthRange[0] := Value;
  1633. gl.DepthRange(Value, FDepthRange[1]);
  1634. end;
  1635. end;
  1636. procedure TGLStateCache.SetDepthWriteMask(const Value: TGLboolean);
  1637. begin
  1638. if (Value <> FDepthWriteMask) or FInsideList then
  1639. begin
  1640. if FInsideList then
  1641. Include(FListStates[FCurrentList], sttDepthBuffer)
  1642. else
  1643. FDepthWriteMask := Value;
  1644. gl.DepthMask(Value);
  1645. end;
  1646. end;
  1647. procedure TGLStateCache.SetDrawFrameBuffer(const Value: Cardinal);
  1648. begin
  1649. if Value <> FDrawFrameBuffer then
  1650. begin
  1651. FDrawFrameBuffer := Value;
  1652. gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, Value);
  1653. end;
  1654. end;
  1655. procedure TGLStateCache.SetEnableBlend(index: Integer; const Value: TGLboolean);
  1656. begin
  1657. if FEnableBlend[Index] <> Value then
  1658. begin
  1659. FEnableBlend[Index] := Value;
  1660. if Value then
  1661. gl.Enablei(GL_BLEND, Index)
  1662. else
  1663. gl.Disablei(GL_BLEND, Index);
  1664. end;
  1665. end;
  1666. procedure TGLStateCache.SetEnableClipDistance(index: Cardinal; const Value: TGLboolean);
  1667. begin
  1668. if FEnableClipDistance[Index] <> Value then
  1669. begin
  1670. FEnableClipDistance[Index] := Value;
  1671. if Value then
  1672. gl.Enable(GL_CLIP_DISTANCE0 + Index)
  1673. else
  1674. gl.Disable(GL_CLIP_DISTANCE0 + Index);
  1675. end;
  1676. end;
  1677. procedure TGLStateCache.SetEnableColorLogicOp(const Value: TGLboolean);
  1678. begin
  1679. if Value <> FEnableColorLogicOp then
  1680. begin
  1681. FEnableColorLogicOp := Value;
  1682. if Value then
  1683. gl.Enable(GL_COLOR_LOGIC_OP)
  1684. else
  1685. gl.Disable(GL_COLOR_LOGIC_OP);
  1686. end;
  1687. end;
  1688. procedure TGLStateCache.SetEnableCullFace(const Value: TGLboolean);
  1689. begin
  1690. end;
  1691. procedure TGLStateCache.SetEnableDepthClamp(const enabled: TGLboolean);
  1692. begin
  1693. end;
  1694. procedure TGLStateCache.SetEnableDepthTest(const Value: TGLboolean);
  1695. begin
  1696. end;
  1697. procedure TGLStateCache.SetEnableDither(const Value: TGLboolean);
  1698. begin
  1699. end;
  1700. procedure TGLStateCache.SetEnableFramebufferSRGB(const Value: TGLboolean);
  1701. begin
  1702. end;
  1703. procedure TGLStateCache.SetEnableLineSmooth(const Value: TGLboolean);
  1704. begin
  1705. end;
  1706. procedure TGLStateCache.SetEnableMultisample(const Value: TGLboolean);
  1707. begin
  1708. end;
  1709. procedure TGLStateCache.SetEnablePolygonOffsetFill(const Value: TGLboolean);
  1710. begin
  1711. end;
  1712. procedure TGLStateCache.SetEnablePolygonOffsetLine(const Value: TGLboolean);
  1713. begin
  1714. end;
  1715. procedure TGLStateCache.SetEnablePolygonOffsetPoint(const Value: TGLboolean);
  1716. begin
  1717. end;
  1718. procedure TGLStateCache.SetEnablePolygonSmooth(const Value: TGLboolean);
  1719. begin
  1720. end;
  1721. procedure TGLStateCache.SetEnableSampleAlphaToCoverage(const Value: TGLboolean);
  1722. begin
  1723. if Value <> FEnableSampleAlphaToCoverage then
  1724. begin
  1725. FEnableSampleAlphaToCoverage := Value;
  1726. if Value then
  1727. gl.Enable(GL_SAMPLE_ALPHA_TO_COVERAGE)
  1728. else
  1729. gl.Disable(GL_SAMPLE_ALPHA_TO_COVERAGE);
  1730. end;
  1731. end;
  1732. procedure TGLStateCache.SetEnableSampleCoverage(const Value: TGLboolean);
  1733. begin
  1734. if Value <> FEnableSampleCoverage then
  1735. begin
  1736. FEnableSampleCoverage := Value;
  1737. if Value then
  1738. gl.Enable(GL_SAMPLE_COVERAGE)
  1739. else
  1740. gl.Disable(GL_SAMPLE_COVERAGE);
  1741. end;
  1742. end;
  1743. procedure TGLStateCache.SetEnableSampleMask(const Value: TGLboolean);
  1744. begin
  1745. if Value <> FEnableSampleMask then
  1746. begin
  1747. FEnableSampleMask := Value;
  1748. if Value then
  1749. gl.Enable(GL_SAMPLE_MASK)
  1750. else
  1751. gl.Disable(GL_SAMPLE_MASK);
  1752. end;
  1753. end;
  1754. procedure TGLStateCache.SetEnableSampleAlphaToOne(const Value: TGLboolean);
  1755. begin
  1756. if Value <> FEnableSampleAlphaToOne then
  1757. begin
  1758. FEnableSampleAlphaToOne := Value;
  1759. if Value then
  1760. gl.Enable(GL_SAMPLE_ALPHA_TO_ONE)
  1761. else
  1762. gl.Disable(GL_SAMPLE_ALPHA_TO_ONE);
  1763. end;
  1764. end;
  1765. procedure TGLStateCache.SetEnableScissorTest(const Value: TGLboolean);
  1766. begin
  1767. end;
  1768. procedure TGLStateCache.SetEnableStencilTest(const Value: TGLboolean);
  1769. begin
  1770. end;
  1771. procedure TGLStateCache.SetFragmentShaderDerivitiveHint(const Value: TGLHintType);
  1772. begin
  1773. if Value <> FFragmentShaderDerivitiveHint then
  1774. begin
  1775. if FInsideList then
  1776. Include(FListStates[FCurrentList], sttHint)
  1777. else
  1778. FFragmentShaderDerivitiveHint := Value;
  1779. gl.Hint(GL_FRAGMENT_SHADER_DERIVATIVE_HINT, cGLHintToGLEnum[Value]);
  1780. end;
  1781. end;
  1782. procedure TGLStateCache.SetMultisampleFilterHint(const Value: TGLHintType);
  1783. begin
  1784. if GL.NV_multisample_filter_hint then
  1785. if Value <> FMultisampleFilterHint then
  1786. begin
  1787. if FInsideList then
  1788. Include(FListStates[FCurrentList], sttHint)
  1789. else
  1790. FMultisampleFilterHint := Value;
  1791. gl.Hint(GL_MULTISAMPLE_FILTER_HINT_NV, cGLHintToGLEnum[Value]);
  1792. end;
  1793. end;
  1794. procedure TGLStateCache.SetFrameBuffer(const Value: Cardinal);
  1795. begin
  1796. if (Value <> FDrawFrameBuffer) or (Value <> FReadFrameBuffer) or FInsideList then
  1797. begin
  1798. FDrawFrameBuffer := Value;
  1799. FReadFrameBuffer := Value;
  1800. gl.BindFramebuffer(GL_FRAMEBUFFER, Value);
  1801. end;
  1802. end;
  1803. procedure TGLStateCache.SetFrontFace(const Value: TGLFaceWinding);
  1804. begin
  1805. if (Value <> FFrontFace) or FInsideList then
  1806. begin
  1807. if FInsideList then
  1808. Include(FListStates[FCurrentList], sttPolygon)
  1809. else
  1810. FFrontFace := Value;
  1811. gl.FrontFace(cGLFaceWindingToGLEnum[Value]);
  1812. end;
  1813. end;
  1814. procedure TGLStateCache.SetGLAlphaFunction(func: TGLComparisonFunction; ref: Single);
  1815. {$IFDEF USE_CACHE_MISS_CHECK}
  1816. var
  1817. I: Cardinal;
  1818. E: Single;
  1819. {$ENDIF}
  1820. begin
  1821. { if FForwardContext then
  1822. exit; }
  1823. {$IFDEF USE_CACHE_MISS_CHECK}
  1824. gl.GetIntegerv(GL_ALPHA_TEST_FUNC, @I);
  1825. if cGLComparisonFunctionToGLEnum[FAlphaFunc] <> I then
  1826. GLSLogger.LogError(strStateCashMissing + 'AlphaTest function');
  1827. gl.GetFloatv(GL_ALPHA_TEST_REF, @E);
  1828. if FAlphaRef <> E then
  1829. GLSLogger.LogError(strStateCashMissing + 'AlphaTest reference');
  1830. {$ENDIF}
  1831. if (FAlphaFunc <> func) or (FAlphaRef <> ref) or FInsideList then
  1832. begin
  1833. if FInsideList then
  1834. Include(FListStates[FCurrentList], sttColorBuffer)
  1835. else
  1836. begin
  1837. FAlphaFunc := func;
  1838. FAlphaRef := ref;
  1839. end;
  1840. gl.AlphaFunc(cGLComparisonFunctionToGLEnum[func], ref);
  1841. end;
  1842. end;
  1843. function TGLStateCache.GetColorWriteMask(index: Integer): TGLColorMask;
  1844. begin
  1845. Result := FColorWriteMask[Index];
  1846. end;
  1847. function TGLStateCache.GetCurrentQuery(index: TGLQueryType): Cardinal;
  1848. begin
  1849. Result := FCurrentQuery[Index];
  1850. end;
  1851. function TGLStateCache.GetCurrentVertexAttrib(index: Integer): TVector;
  1852. begin
  1853. Result := FCurrentVertexAttrib[Index];
  1854. end;
  1855. function TGLStateCache.GetDepthRangeFar: TGLclampd;
  1856. begin
  1857. Result := FDepthRange[1];
  1858. end;
  1859. function TGLStateCache.GetDepthRangeNear: TGLclampd;
  1860. begin
  1861. Result := FDepthRange[0];
  1862. end;
  1863. function TGLStateCache.GetEnableBlend(index: Integer): TGLboolean;
  1864. begin
  1865. Result := FEnableBlend[Index];
  1866. end;
  1867. function TGLStateCache.GetEnableClipDistance(ClipDistance: Cardinal): TGLboolean;
  1868. begin
  1869. Result := FEnableClipDistance[ClipDistance];
  1870. end;
  1871. function TGLStateCache.GetSampleMaskValue(index: Integer): TGLbitfield;
  1872. begin
  1873. Result := FSampleMaskValue[Index];
  1874. end;
  1875. function TGLStateCache.GetMaxTextureSize: Cardinal;
  1876. begin
  1877. if FMaxTextureSize = 0 then
  1878. gl.GetIntegerv(GL_MAX_TEXTURE_SIZE, @FMaxTextureSize);
  1879. Result := FMaxTextureSize;
  1880. end;
  1881. function TGLStateCache.GetMaterialAmbient(const aFace: TGLCullFaceMode): TVector;
  1882. begin
  1883. Result := FFrontBackColors[ord(aFace)][1];
  1884. end;
  1885. function TGLStateCache.GetMaterialDiffuse(const aFace: TGLCullFaceMode): TVector;
  1886. begin
  1887. Result := FFrontBackColors[ord(aFace)][2];
  1888. end;
  1889. function TGLStateCache.GetMaterialEmission(const aFace: TGLCullFaceMode): TVector;
  1890. begin
  1891. Result := FFrontBackColors[ord(aFace)][0];
  1892. end;
  1893. function TGLStateCache.GetMaterialShininess(const aFace: TGLCullFaceMode): Integer;
  1894. begin
  1895. Result := FFrontBackShininess[ord(aFace)];
  1896. end;
  1897. function TGLStateCache.GetMaterialSpecular(const aFace: TGLCullFaceMode): TVector;
  1898. begin
  1899. Result := FFrontBackColors[ord(aFace)][3];
  1900. end;
  1901. function TGLStateCache.GetMax3DTextureSize: Cardinal;
  1902. begin
  1903. if FMax3DTextureSize = 0 then
  1904. gl.GetIntegerv(GL_MAX_3D_TEXTURE_SIZE, @FMax3DTextureSize);
  1905. Result := FMax3DTextureSize;
  1906. end;
  1907. function TGLStateCache.GetMaxCubeTextureSize: Cardinal;
  1908. begin
  1909. if FMaxCubeTextureSize = 0 then
  1910. gl.GetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, @FMaxCubeTextureSize);
  1911. Result := FMaxCubeTextureSize;
  1912. end;
  1913. function TGLStateCache.GetMaxArrayTextureSize: Cardinal;
  1914. begin
  1915. if FMaxArrayTextureSize = 0 then
  1916. gl.GetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, @FMaxArrayTextureSize);
  1917. Result := FMaxArrayTextureSize;
  1918. end;
  1919. function TGLStateCache.GetMaxTextureImageUnits: Cardinal;
  1920. begin
  1921. if FMaxTextureImageUnits = 0 then
  1922. gl.GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, @FMaxTextureImageUnits);
  1923. Result := FMaxTextureImageUnits;
  1924. end;
  1925. function TGLStateCache.GetMaxTextureAnisotropy: Cardinal;
  1926. begin
  1927. if (FMaxTextureAnisotropy = 0) and GL.EXT_texture_filter_anisotropic then
  1928. gl.GetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, @FMaxTextureAnisotropy);
  1929. Result := FMaxTextureAnisotropy;
  1930. end;
  1931. function TGLStateCache.GetMaxSamples: Cardinal;
  1932. begin
  1933. if (FMaxSamples = 0) and GL.EXT_multisample then
  1934. gl.GetIntegerv(GL_MAX_SAMPLES, @FMaxSamples);
  1935. Result := FMaxSamples;
  1936. end;
  1937. function TGLStateCache.GetTextureBinding(index: Integer; target: TGLTextureTarget): Cardinal;
  1938. begin
  1939. Result := FTextureBinding[Index, target];
  1940. end;
  1941. function TGLStateCache.GetTextureBindingTime(index: Integer; target: TGLTextureTarget): Double;
  1942. begin
  1943. Result := FTextureBindingTime[Index, target];
  1944. end;
  1945. function TGLStateCache.GetSamplerBinding(index: Cardinal): Cardinal;
  1946. begin
  1947. Result := FSamplerBinding[Index];
  1948. end;
  1949. procedure TGLStateCache.SetSamplerBinding(index: Cardinal; const Value: Cardinal);
  1950. begin
  1951. if Index > High(FSamplerBinding) then
  1952. exit;
  1953. if (Value <> FSamplerBinding[Index]) or FInsideList then
  1954. begin
  1955. if FInsideList then
  1956. Include(FListStates[FCurrentList], sttTexture)
  1957. else
  1958. FSamplerBinding[Index] := Value;
  1959. gl.BindSampler(Index, Value);
  1960. end;
  1961. end;
  1962. procedure TGLStateCache.SetGLTextureMatrix(const matrix: TMatrix);
  1963. begin
  1964. { if FForwardContext then
  1965. exit; }
  1966. if FInsideList then
  1967. Include(FListStates[FCurrentList], sttTransform)
  1968. else
  1969. FTextureMatrixIsIdentity[ActiveTexture] := False;
  1970. gl.MatrixMode(GL_TEXTURE);
  1971. gl.LoadMatrixf(PGLFloat(@matrix.V[0].X));
  1972. gl.MatrixMode(GL_MODELVIEW);
  1973. end;
  1974. procedure TGLStateCache.ResetGLTextureMatrix;
  1975. begin
  1976. { if FForwardContext then
  1977. exit; }
  1978. gl.MatrixMode(GL_TEXTURE);
  1979. gl.LoadIdentity;
  1980. FTextureMatrixIsIdentity[ActiveTexture] := True;
  1981. gl.MatrixMode(GL_MODELVIEW);
  1982. end;
  1983. procedure TGLStateCache.ResetAllGLTextureMatrix;
  1984. var
  1985. I: Integer;
  1986. lastActiveTexture: Cardinal;
  1987. begin
  1988. { if FForwardContext then
  1989. exit; }
  1990. lastActiveTexture := ActiveTexture;
  1991. gl.MatrixMode(GL_TEXTURE);
  1992. for I := High(FTextureMatrixIsIdentity) downto 0 do
  1993. if not FTextureMatrixIsIdentity[I] then
  1994. begin
  1995. ActiveTexture := I;
  1996. gl.LoadIdentity;
  1997. FTextureMatrixIsIdentity[I] := True;
  1998. end;
  1999. gl.MatrixMode(GL_MODELVIEW);
  2000. ActiveTexture := lastActiveTexture;
  2001. end;
  2002. procedure TGLStateCache.SetLineSmoothHint(const Value: TGLHintType);
  2003. begin
  2004. if (Value <> FLineSmoothHint) or FInsideList then
  2005. begin
  2006. if FInsideList then
  2007. Include(FListStates[FCurrentList], sttHint)
  2008. else
  2009. FLineSmoothHint := Value;
  2010. gl.Hint(GL_LINE_SMOOTH_HINT, cGLHintToGLEnum[Value]);
  2011. end;
  2012. end;
  2013. procedure TGLStateCache.SetLineWidth(const Value: TGLfloat);
  2014. begin
  2015. // note: wide lines no longer deprecated (see OpenGL spec)
  2016. if (Value <> FLineWidth) or FInsideList then
  2017. begin
  2018. if FInsideList then
  2019. Include(FListStates[FCurrentList], sttLine)
  2020. else
  2021. FLineWidth := Value;
  2022. gl.LineWidth(Value);
  2023. end;
  2024. end;
  2025. procedure TGLStateCache.SetLineStippleFactor(const Value: TGLint);
  2026. begin
  2027. if (Value <> FLineStippleFactor) or FInsideList then
  2028. begin
  2029. if FInsideList then
  2030. Include(FListStates[FCurrentList], sttLine)
  2031. else
  2032. FLineStippleFactor := Value;
  2033. gl.LineStipple(Value, FLineStipplePattern);
  2034. end;
  2035. end;
  2036. procedure TGLStateCache.SetLineStipplePattern(const Value: TGLushort);
  2037. begin
  2038. if (Value <> FLineStipplePattern) or FInsideList then
  2039. begin
  2040. if FInsideList then
  2041. Include(FListStates[FCurrentList], sttLine)
  2042. else
  2043. FLineStipplePattern := Value;
  2044. gl.LineStipple(FLineStippleFactor, Value);
  2045. end;
  2046. end;
  2047. procedure TGLStateCache.SetLogicOpMode(const Value: TGLLogicOp);
  2048. begin
  2049. if (Value <> FLogicOpMode) or FInsideList then
  2050. begin
  2051. if FInsideList then
  2052. Include(FListStates[FCurrentList], sttColorBuffer)
  2053. else
  2054. FLogicOpMode := Value;
  2055. gl.LogicOp(cGLLogicOpToGLEnum[Value]);
  2056. end;
  2057. end;
  2058. procedure TGLStateCache.SetPackAlignment(const Value: Cardinal);
  2059. begin
  2060. if Value <> FPackAlignment then
  2061. begin
  2062. FPackAlignment := Value;
  2063. gl.PixelStoref(GL_PACK_ALIGNMENT, Value);
  2064. end;
  2065. end;
  2066. procedure TGLStateCache.SetPackImageHeight(const Value: Cardinal);
  2067. begin
  2068. if Value <> FPackImageHeight then
  2069. begin
  2070. FPackImageHeight := Value;
  2071. gl.PixelStoref(GL_PACK_IMAGE_HEIGHT, Value);
  2072. end;
  2073. end;
  2074. procedure TGLStateCache.SetPackLSBFirst(const Value: TGLboolean);
  2075. begin
  2076. if Value <> FPackLSBFirst then
  2077. begin
  2078. FPackLSBFirst := Value;
  2079. gl.PixelStorei(GL_PACK_LSB_FIRST, byte(Value));
  2080. end;
  2081. end;
  2082. procedure TGLStateCache.SetPackRowLength(const Value: Cardinal);
  2083. begin
  2084. if Value <> FPackRowLength then
  2085. begin
  2086. FPackRowLength := Value;
  2087. gl.PixelStoref(GL_PACK_ROW_LENGTH, Value);
  2088. end;
  2089. end;
  2090. procedure TGLStateCache.SetPackSkipImages(const Value: Cardinal);
  2091. begin
  2092. if Value <> FPackSkipImages then
  2093. begin
  2094. FPackSkipImages := Value;
  2095. gl.PixelStoref(GL_PACK_SKIP_IMAGES, Value);
  2096. end;
  2097. end;
  2098. procedure TGLStateCache.SetPackSkipPixels(const Value: Cardinal);
  2099. begin
  2100. if Value <> FPackSkipPixels then
  2101. begin
  2102. FPackSkipPixels := Value;
  2103. gl.PixelStoref(GL_PACK_SKIP_PIXELS, Value);
  2104. end;
  2105. end;
  2106. procedure TGLStateCache.SetPackSkipRows(const Value: Cardinal);
  2107. begin
  2108. if Value <> FPackSkipRows then
  2109. begin
  2110. FPackSkipRows := Value;
  2111. gl.PixelStoref(GL_PACK_SKIP_ROWS, Value);
  2112. end;
  2113. end;
  2114. procedure TGLStateCache.SetPackSwapBytes(const Value: TGLboolean);
  2115. begin
  2116. if Value <> FPackSwapBytes then
  2117. begin
  2118. FPackSwapBytes := Value;
  2119. gl.PixelStorei(GL_PACK_SWAP_BYTES, byte(Value));
  2120. end;
  2121. end;
  2122. procedure TGLStateCache.SetPixelPackBufferBinding(const Value: Cardinal);
  2123. begin
  2124. if Value <> FPixelPackBufferBinding then
  2125. begin
  2126. FPixelPackBufferBinding := Value;
  2127. gl.BindBuffer(GL_PIXEL_PACK_BUFFER, Value);
  2128. end;
  2129. end;
  2130. procedure TGLStateCache.SetPixelUnpackBufferBinding(const Value: Cardinal);
  2131. begin
  2132. if Value <> FPixelUnpackBufferBinding then
  2133. begin
  2134. FPixelUnpackBufferBinding := Value;
  2135. gl.BindBuffer(GL_PIXEL_UNPACK_BUFFER, Value);
  2136. end;
  2137. end;
  2138. procedure TGLStateCache.SetPointFadeThresholdSize(const Value: TGLfloat);
  2139. begin
  2140. if (Value <> FPointFadeThresholdSize) or FInsideList then
  2141. begin
  2142. if FInsideList then
  2143. Include(FListStates[FCurrentList], sttPoint)
  2144. else
  2145. FPointFadeThresholdSize := Value;
  2146. gl.PointParameterf(GL_POINT_FADE_THRESHOLD_SIZE, Value);
  2147. end;
  2148. end;
  2149. procedure TGLStateCache.SetPointSize(const Value: TGLfloat);
  2150. begin
  2151. if (Value <> FPointSize) or FInsideList then
  2152. begin
  2153. if FInsideList then
  2154. Include(FListStates[FCurrentList], sttPoint)
  2155. else
  2156. FPointSize := Value;
  2157. gl.PointSize(Value);
  2158. end;
  2159. end;
  2160. procedure TGLStateCache.SetPointSpriteCoordOrigin(const Value: Cardinal);
  2161. begin
  2162. if (Value <> FPointSpriteCoordOrigin) or FInsideList then
  2163. begin
  2164. if FInsideList then
  2165. Include(FListStates[FCurrentList], sttPoint)
  2166. else
  2167. FPointSpriteCoordOrigin := Value;
  2168. gl.PointParameterf(GL_POINT_SPRITE_COORD_ORIGIN, Value);
  2169. end;
  2170. end;
  2171. procedure TGLStateCache.SetPolygonMode(const Value: TGLPolygonMode);
  2172. begin
  2173. if (Value <> FPolygonMode) or FInsideList then
  2174. begin
  2175. if FInsideList then
  2176. Include(FListStates[FCurrentList], sttPolygon)
  2177. else
  2178. begin
  2179. FPolygonMode := Value;
  2180. FPolygonBackMode := Value;
  2181. end;
  2182. gl.PolygonMode(GL_FRONT_AND_BACK, cGLPolygonModeToGLEnum[Value]);
  2183. end;
  2184. end;
  2185. procedure TGLStateCache.SetPolygonOffset(const factor, units: TGLfloat);
  2186. begin
  2187. if (factor <> FPolygonOffsetFactor) or (units <> FPolygonOffsetUnits) or FInsideList then
  2188. begin
  2189. if FInsideList then
  2190. Include(FListStates[FCurrentList], sttPolygon)
  2191. else
  2192. begin
  2193. FPolygonOffsetFactor := factor;
  2194. FPolygonOffsetUnits := units;
  2195. end;
  2196. gl.PolygonOffset(factor, units);
  2197. end;
  2198. end;
  2199. procedure TGLStateCache.SetPolygonOffsetFactor(const Value: TGLfloat);
  2200. begin
  2201. if (Value <> FPolygonOffsetFactor) or FInsideList then
  2202. begin
  2203. if FInsideList then
  2204. Include(FListStates[FCurrentList], sttPolygon)
  2205. else
  2206. FPolygonOffsetFactor := Value;
  2207. gl.PolygonOffset(Value, FPolygonOffsetUnits);
  2208. end;
  2209. end;
  2210. procedure TGLStateCache.SetPolygonOffsetUnits(const Value: TGLfloat);
  2211. begin
  2212. if (Value <> FPolygonOffsetUnits) or FInsideList then
  2213. begin
  2214. if FInsideList then
  2215. Include(FListStates[FCurrentList], sttPolygon)
  2216. else
  2217. FPolygonOffsetUnits := Value;
  2218. gl.PolygonOffset(FPolygonOffsetFactor, Value);
  2219. end;
  2220. end;
  2221. procedure TGLStateCache.SetPolygonSmoothHint(const Value: TGLHintType);
  2222. begin
  2223. if (Value <> FPolygonSmoothHint) or FInsideList then
  2224. begin
  2225. if FInsideList then
  2226. Include(FListStates[FCurrentList], sttHint)
  2227. else
  2228. FPolygonSmoothHint := Value;
  2229. gl.Hint(GL_POLYGON_SMOOTH_HINT, cGLHintToGLEnum[Value]);
  2230. end;
  2231. end;
  2232. procedure TGLStateCache.SetProvokingVertex(const Value: Cardinal);
  2233. begin
  2234. if Value <> FProvokingVertex then
  2235. begin
  2236. FProvokingVertex := Value;
  2237. gl.ProvokingVertex(Value);
  2238. end;
  2239. end;
  2240. procedure TGLStateCache.SetReadFrameBuffer(const Value: Cardinal);
  2241. begin
  2242. if Value <> FReadFrameBuffer then
  2243. begin
  2244. FReadFrameBuffer := Value;
  2245. gl.BindFramebuffer(GL_READ_FRAMEBUFFER, Value);
  2246. end;
  2247. end;
  2248. procedure TGLStateCache.SetRenderBuffer(const Value: Cardinal);
  2249. begin
  2250. if Value <> FRenderBuffer then
  2251. begin
  2252. FRenderBuffer := Value;
  2253. gl.BindRenderbuffer(GL_RENDERBUFFER, Value);
  2254. end;
  2255. end;
  2256. procedure TGLStateCache.SetSampleCoverage(const Value: TGLfloat; invert: TGLboolean);
  2257. begin
  2258. if (Value <> FSampleCoverageValue) or (invert <> FSampleCoverageInvert) or FInsideList then
  2259. begin
  2260. if FInsideList then
  2261. Include(FListStates[FCurrentList], sttMultisample)
  2262. else
  2263. begin
  2264. FSampleCoverageValue := Value;
  2265. FSampleCoverageInvert := invert;
  2266. end;
  2267. gl.SampleCoverage(Value, invert);
  2268. end;
  2269. end;
  2270. procedure TGLStateCache.SetSampleCoverageInvert(const Value: TGLboolean);
  2271. begin
  2272. if (Value <> FSampleCoverageInvert) or FInsideList then
  2273. begin
  2274. if FInsideList then
  2275. Include(FListStates[FCurrentList], sttMultisample)
  2276. else
  2277. FSampleCoverageInvert := Value;
  2278. gl.SampleCoverage(FSampleCoverageValue, Value);
  2279. end;
  2280. end;
  2281. procedure TGLStateCache.SetSampleCoverageValue(const Value: TGLfloat);
  2282. begin
  2283. if (Value <> FSampleCoverageValue) or FInsideList then
  2284. begin
  2285. if FInsideList then
  2286. Include(FListStates[FCurrentList], sttMultisample)
  2287. else
  2288. FSampleCoverageValue := Value;
  2289. gl.SampleCoverage(Value, FSampleCoverageInvert);
  2290. end;
  2291. end;
  2292. procedure TGLStateCache.SetSampleMaskValue(index: Integer; const Value: TGLbitfield);
  2293. begin
  2294. if (FSampleMaskValue[Index] <> Value) or FInsideList then
  2295. begin
  2296. if FInsideList then
  2297. Include(FListStates[FCurrentList], sttMultisample)
  2298. else
  2299. FSampleMaskValue[Index] := Value;
  2300. gl.SampleMaski(Index, Value);
  2301. end;
  2302. end;
  2303. procedure TGLStateCache.SetScissorBox(const Value: TVector4i);
  2304. begin
  2305. if not VectorEquals(FScissorBox, Value) or FInsideList then
  2306. begin
  2307. if FInsideList then
  2308. Include(FListStates[FCurrentList], sttScissor)
  2309. else
  2310. FScissorBox := Value;
  2311. gl.Scissor(Value.X, Value.Y, Value.Z, Value.W);
  2312. end;
  2313. end;
  2314. procedure TGLStateCache.SetStencilBackWriteMask(const Value: Cardinal);
  2315. begin
  2316. if (Value <> FStencilBackWriteMask) or FInsideList then
  2317. begin
  2318. if FInsideList then
  2319. Include(FListStates[FCurrentList], sttStencilBuffer)
  2320. else
  2321. FStencilBackWriteMask := Value;
  2322. // DONE: ignore if unsupported
  2323. if gl.VERSION_2_0 then
  2324. gl.StencilMaskSeparate(GL_BACK, Value);
  2325. end;
  2326. end;
  2327. procedure TGLStateCache.SetStencilClearValue(const Value: Cardinal);
  2328. {$IFDEF USE_CACHE_MISS_CHECK}
  2329. var
  2330. I: Cardinal;
  2331. {$ENDIF}
  2332. begin
  2333. {$IFDEF USE_CACHE_MISS_CHECK}
  2334. gl.GetIntegerv(GL_STENCIL_CLEAR_VALUE, @I);
  2335. if FStencilClearValue <> I then
  2336. GLSLogger.LogError(strStateCashMissing + 'Stencil clear value');
  2337. {$ENDIF}
  2338. if (Value <> FStencilClearValue) or FInsideList then
  2339. begin
  2340. if FInsideList then
  2341. Include(FListStates[FCurrentList], sttStencilBuffer)
  2342. else
  2343. FStencilClearValue := Value;
  2344. gl.ClearStencil(Value);
  2345. end;
  2346. end;
  2347. procedure TGLStateCache.SetColorClearValue(const Value: TVector);
  2348. begin
  2349. if not VectorEquals(Value, FColorClearValue) or FInsideList then
  2350. begin
  2351. if FInsideList then
  2352. Include(FListStates[FCurrentList], sttColorBuffer)
  2353. else
  2354. FColorClearValue := Value;
  2355. gl.ClearColor(Value.X, Value.Y, Value.Z, Value.W);
  2356. end;
  2357. end;
  2358. procedure TGLStateCache.SetColorMask(mask: TGLColorMask);
  2359. var
  2360. I: Integer;
  2361. begin
  2362. // it might be faster to keep track of whether all draw buffers are same
  2363. // value or not, since using this is probably more common than setting
  2364. // the color write mask for individual draw buffers
  2365. if FInsideList then
  2366. Include(FListStates[FCurrentList], sttColorBuffer)
  2367. else
  2368. for I := low(FColorWriteMask) to high(FColorWriteMask) do
  2369. begin
  2370. FColorWriteMask[I] := mask;
  2371. end;
  2372. gl.ColorMask(ccRed in mask, ccGreen in mask, ccBlue in mask, ccAlpha in mask);
  2373. end;
  2374. procedure TGLStateCache.SetStencilFuncSeparate(const face: TGLCullFaceMode; const func: TGLStencilFunction; const ref: TGLint;
  2375. const mask: Cardinal);
  2376. {$IFDEF USE_CACHE_MISS_CHECK}
  2377. var
  2378. UI: Cardinal;
  2379. I: TGLint;
  2380. {$ENDIF}
  2381. begin
  2382. // if (func<>FStencilFunc) or (ref<>FStencilRef) or (mask<>FStencilValueMask)
  2383. // or FInsideList then
  2384. {$IFDEF USE_CACHE_MISS_CHECK}
  2385. gl.GetIntegerv(GL_STENCIL_FUNC, @UI);
  2386. if cGLComparisonFunctionToGLEnum[FStencilFunc] <> UI then
  2387. GLSLogger.LogError(strStateCashMissing + 'Stencil function');
  2388. gl.GetIntegerv(GL_STENCIL_REF, @I);
  2389. if FStencilRef <> I then
  2390. GLSLogger.LogError(strStateCashMissing + 'Stencil reference');
  2391. GLSLogger.LogError(strStateCashMissing + 'Stencil function');
  2392. gl.GetIntegerv(GL_STENCIL_VALUE_MASK, @UI);
  2393. if FStencilValueMask <> UI then
  2394. GLSLogger.LogError(strStateCashMissing + 'Stencil value mask');
  2395. {$ENDIF}
  2396. begin
  2397. if FInsideList then
  2398. Include(FListStates[FCurrentList], sttStencilBuffer)
  2399. else
  2400. case face of
  2401. cmFront:
  2402. begin
  2403. FStencilFunc := func;
  2404. FStencilRef := ref;
  2405. FStencilValueMask := mask;
  2406. end;
  2407. cmBack:
  2408. begin
  2409. FStencilBackFunc := func;
  2410. FStencilBackRef := ref;
  2411. FStencilBackValueMask := mask;
  2412. end;
  2413. cmFrontAndBack:
  2414. begin
  2415. FStencilFunc := func;
  2416. FStencilRef := ref;
  2417. FStencilValueMask := mask;
  2418. FStencilBackFunc := func;
  2419. FStencilBackRef := ref;
  2420. FStencilBackValueMask := mask;
  2421. end;
  2422. end;
  2423. gl.StencilFuncSeparate(cGLCullFaceModeToGLEnum[face], cGLComparisonFunctionToGLEnum[func], ref, mask);
  2424. end;
  2425. end;
  2426. procedure TGLStateCache.SetStencilFunc(const func: TGLStencilFunction; const ref: TGLint; const mask: Cardinal);
  2427. begin
  2428. if (func <> FStencilFunc) or (ref <> FStencilRef) or (mask <> FStencilValueMask) or FInsideList then
  2429. begin
  2430. if FInsideList then
  2431. Include(FListStates[FCurrentList], sttStencilBuffer)
  2432. else
  2433. begin
  2434. FStencilFunc := func;
  2435. FStencilRef := ref;
  2436. FStencilValueMask := mask;
  2437. end;
  2438. gl.StencilFunc(cGLComparisonFunctionToGLEnum[func], ref, mask);
  2439. end;
  2440. end;
  2441. procedure TGLStateCache.SetStencilOp(const fail, zfail, zpass: TGLStencilOp);
  2442. {$IFDEF USE_CACHE_MISS_CHECK}
  2443. var
  2444. I: Cardinal;
  2445. {$ENDIF}
  2446. begin
  2447. {$IFDEF USE_CACHE_MISS_CHECK}
  2448. gl.GetIntegerv(GL_STENCIL_FAIL, @I);
  2449. if cGLStencilOpToGLEnum[FStencilFail] <> I then
  2450. GLSLogger.LogError(strStateCashMissing + 'Stencil fail');
  2451. gl.GetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, @I);
  2452. if cGLStencilOpToGLEnum[FStencilPassDepthFail] <> I then
  2453. GLSLogger.LogError(strStateCashMissing + 'Stencil zfail');
  2454. gl.GetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, @I);
  2455. if cGLStencilOpToGLEnum[FStencilPassDepthPass] <> I then
  2456. GLSLogger.LogError(strStateCashMissing + 'Stencil zpass');
  2457. {$ENDIF}
  2458. if (fail <> FStencilFail) or (zfail <> FStencilPassDepthFail) or (zpass <> FStencilPassDepthPass) or FInsideList then
  2459. begin
  2460. if FInsideList then
  2461. Include(FListStates[FCurrentList], sttStencilBuffer)
  2462. else
  2463. begin
  2464. FStencilFail := fail;
  2465. FStencilPassDepthFail := zfail;
  2466. FStencilPassDepthPass := zpass;
  2467. end;
  2468. gl.StencilOp(cGLStencilOpToGLEnum[fail],
  2469. cGLStencilOpToGLEnum[zfail],
  2470. cGLStencilOpToGLEnum[zpass]);
  2471. end;
  2472. end;
  2473. procedure TGLStateCache.SetStencilOpSeparate(const face: TGLCullFaceMode; const sfail, dpfail, dppass: TGLStencilOp);
  2474. begin
  2475. if FInsideList then
  2476. Include(FListStates[FCurrentList], sttStencilBuffer)
  2477. else
  2478. case face of
  2479. cmFront:
  2480. begin
  2481. FStencilFail := sfail;
  2482. FStencilPassDepthFail := dpfail;
  2483. FStencilPassDepthPass := dppass;
  2484. end;
  2485. cmBack:
  2486. begin
  2487. FStencilBackFail := sfail;
  2488. FStencilBackPassDepthFail := dpfail;
  2489. FStencilBackPassDepthPass := dppass;
  2490. end;
  2491. cmFrontAndBack:
  2492. begin
  2493. FStencilFail := sfail;
  2494. FStencilPassDepthFail := dpfail;
  2495. FStencilPassDepthPass := dppass;
  2496. FStencilBackFail := sfail;
  2497. FStencilBackPassDepthFail := dpfail;
  2498. FStencilBackPassDepthPass := dppass;
  2499. end;
  2500. end;
  2501. gl.StencilOpSeparate(cGLCullFaceModeToGLEnum[face], cGLStencilOpToGLEnum[sfail], cGLStencilOpToGLEnum[dpfail],
  2502. cGLStencilOpToGLEnum[dppass]);
  2503. end;
  2504. procedure TGLStateCache.SetStencilWriteMask(const Value: Cardinal);
  2505. {$IFDEF USE_CACHE_MISS_CHECK}
  2506. var
  2507. I: Cardinal;
  2508. {$ENDIF}
  2509. begin
  2510. {$IFDEF USE_CACHE_MISS_CHECK}
  2511. gl.GetIntegerv(GL_STENCIL_WRITEMASK, @I);
  2512. if FStencilWriteMask <> I then
  2513. GLSLogger.LogError(strStateCashMissing + 'Stencil write mask');
  2514. {$ENDIF}
  2515. if (Value <> FStencilWriteMask) or FInsideList then
  2516. begin
  2517. if FInsideList then
  2518. Include(FListStates[FCurrentList], sttStencilBuffer)
  2519. else
  2520. FStencilWriteMask := Value;
  2521. gl.StencilMaskSeparate(GL_FRONT, Value);
  2522. end;
  2523. end;
  2524. procedure TGLStateCache.SetTextureBinding(index: Integer; target: TGLTextureTarget; const Value: Cardinal);
  2525. var
  2526. lastActiveTexture: Cardinal;
  2527. begin
  2528. if target = ttNoShape then
  2529. exit;
  2530. if (Value <> FTextureBinding[Index, target]) or FInsideList then
  2531. begin
  2532. if FInsideList then
  2533. Include(FListStates[FCurrentList], sttTexture)
  2534. else
  2535. FTextureBinding[Index, target] := Value;
  2536. lastActiveTexture := ActiveTexture;
  2537. ActiveTexture := Index;
  2538. gl.BindTexture(cGLTexTypeToGLEnum[target], Value);
  2539. ActiveTexture := lastActiveTexture;
  2540. end;
  2541. FTextureBindingTime[Index, target] := AppTime;
  2542. end;
  2543. function TGLStateCache.GetActiveTextureEnabled(target: TGLTextureTarget): Boolean;
  2544. begin
  2545. Result := FActiveTextureEnabling[FActiveTexture][target];
  2546. end;
  2547. procedure TGLStateCache.SetActiveTextureEnabled(target: TGLTextureTarget; const Value: Boolean);
  2548. var
  2549. glTarget: Cardinal;
  2550. begin
  2551. glTarget := DecodeTextureTarget(target);
  2552. if { FForwardContext or } not IsTargetSupported(glTarget) then
  2553. exit;
  2554. if (Value <> FActiveTextureEnabling[FActiveTexture][target]) or FInsideList then
  2555. begin
  2556. if FInsideList then
  2557. Include(FListStates[FCurrentList], sttEnable)
  2558. else
  2559. FActiveTextureEnabling[FActiveTexture][target] := Value;
  2560. if Value then
  2561. gl.Enable(glTarget)
  2562. else
  2563. gl.Disable(glTarget);
  2564. end;
  2565. end;
  2566. procedure TGLStateCache.SetTextureCompressionHint(const Value: TGLHintType);
  2567. begin
  2568. if (Value <> FTextureCompressionHint) or FInsideList then
  2569. begin
  2570. if FInsideList then
  2571. Include(FListStates[FCurrentList], sttHint)
  2572. else
  2573. FTextureCompressionHint := Value;
  2574. gl.Hint(GL_TEXTURE_COMPRESSION_HINT, cGLHintToGLEnum[Value]);
  2575. end;
  2576. end;
  2577. procedure TGLStateCache.SetTransformFeedbackBufferBinding(const Value: Cardinal);
  2578. begin
  2579. if (Value <> FTransformFeedbackBufferBinding) or FInsideList then
  2580. begin
  2581. FTransformFeedbackBufferBinding := Value;
  2582. gl.BindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, Value);
  2583. end;
  2584. end;
  2585. procedure TGLStateCache.SetEnableTextureCubeMapSeamless(const Value: TGLboolean);
  2586. begin
  2587. if Value <> FEnableTextureCubeMapSeamless then
  2588. begin
  2589. FEnableTextureCubeMapSeamless := Value;
  2590. if Value = True then
  2591. gl.Enable(GL_TEXTURE_CUBE_MAP_SEAMLESS)
  2592. else
  2593. gl.Disable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  2594. end;
  2595. end;
  2596. procedure TGLStateCache.NewList(list: Cardinal; mode: Cardinal);
  2597. var
  2598. I: Cardinal;
  2599. begin
  2600. Assert(mode = GL_COMPILE, 'Compile & executing not supported by TGLStateCache');
  2601. FCurrentList := list - 1;
  2602. // while High(FListStates) < Integer(FCurrentList) do
  2603. // SetLength(FListStates, 2 * Length(FListStates));
  2604. FListStates[FCurrentList] := [];
  2605. FInsideList := True;
  2606. // Reset VBO binding and client attribute
  2607. begin
  2608. if GL.ARB_vertex_buffer_object then
  2609. begin
  2610. ArrayBufferBinding := 0;
  2611. ElementBufferBinding := 0;
  2612. for I := 0 to 15 do
  2613. gl.DisableVertexAttribArray(I);
  2614. end;
  2615. gl.NewList(list, mode);
  2616. end;
  2617. end;
  2618. procedure TGLStateCache.EndList;
  2619. begin
  2620. gl.EndList;
  2621. FInsideList := False;
  2622. end;
  2623. procedure TGLStateCache.CallList(list: Cardinal);
  2624. begin
  2625. // while High(FListStates) < Integer(list) do
  2626. // SetLength(FListStates, 2 * Length(FListStates));
  2627. if FListStates[list - 1] <> [] then
  2628. begin
  2629. PushAttrib(FListStates[list - 1]);
  2630. gl.CallList(list);
  2631. PopAttrib;
  2632. end
  2633. else
  2634. gl.CallList(list);
  2635. end;
  2636. procedure TGLStateCache.SetUniformBufferBinding(const Value: Cardinal);
  2637. begin
  2638. Assert(not FInsideList);
  2639. if Value <> FUniformBufferBinding then
  2640. begin
  2641. FUniformBufferBinding := Value;
  2642. gl.BindBuffer(GL_UNIFORM_BUFFER, Value);
  2643. end;
  2644. end;
  2645. procedure TGLStateCache.SetBufferIndexedBinding(const Value: Cardinal;
  2646. ATarget: TGLBufferBindingTarget; AIndex: Cardinal; ABufferSize: TGLsizeiptr);
  2647. begin
  2648. Assert(not FInsideList);
  2649. if (FUBOStates[ATarget, AIndex].FUniformBufferBinding <> Value)
  2650. or (FUBOStates[ATarget, AIndex].FOffset > 0)
  2651. or (FUBOStates[ATarget, AIndex].FSize <> ABufferSize) then
  2652. begin
  2653. case ATarget of
  2654. bbtUniform:
  2655. FUniformBufferBinding := Value;
  2656. bbtTransformFeedBack:
  2657. FTransformFeedbackBufferBinding := Value;
  2658. end;
  2659. FUBOStates[ATarget, AIndex].FUniformBufferBinding := Value;
  2660. FUBOStates[ATarget, AIndex].FOffset := 0;
  2661. FUBOStates[ATarget, AIndex].FSize := ABufferSize;
  2662. gl.BindBufferBase(cGLBufferBindingTarget[ATarget], AIndex, Value);
  2663. end
  2664. else
  2665. case ATarget of
  2666. bbtUniform: SetUniformBufferBinding(Value);
  2667. bbtTransformFeedBack: SetTransformFeedbackBufferBinding(Value);
  2668. end;
  2669. end;
  2670. procedure TGLStateCache.SetBufferIndexedBinding(const Value: Cardinal; ATarget: TGLBufferBindingTarget; AIndex: Cardinal;
  2671. AOffset: TGLintptr; ARangeSize: TGLsizeiptr);
  2672. begin
  2673. Assert(not FInsideList);
  2674. if (FUBOStates[ATarget, AIndex].FUniformBufferBinding <> Value)
  2675. or (FUBOStates[ATarget, AIndex].FOffset <> AOffset)
  2676. or (FUBOStates[ATarget, AIndex].FSize <> ARangeSize) then
  2677. begin
  2678. case ATarget of
  2679. bbtUniform: FUniformBufferBinding := Value;
  2680. bbtTransformFeedBack: FTransformFeedbackBufferBinding := Value;
  2681. end;
  2682. FUBOStates[ATarget, AIndex].FUniformBufferBinding := Value;
  2683. FUBOStates[ATarget, AIndex].FOffset := AOffset;
  2684. FUBOStates[ATarget, AIndex].FSize := ARangeSize;
  2685. gl.BindBufferRange(cGLBufferBindingTarget[ATarget], AIndex, Value, AOffset, ARangeSize);
  2686. end;
  2687. end;
  2688. function TGLStateCache.GetMaxTextureUnits: Cardinal;
  2689. begin
  2690. if FMaxTextureUnits = 0 then
  2691. gl.GetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, @FMaxTextureUnits);
  2692. Result := FMaxTextureUnits;
  2693. end;
  2694. procedure TGLStateCache.SetUnpackAlignment(const Value: Cardinal);
  2695. begin
  2696. if Value <> FUnpackAlignment then
  2697. begin
  2698. FUnpackAlignment := Value;
  2699. gl.PixelStoref(GL_UNPACK_ALIGNMENT, Value);
  2700. end;
  2701. end;
  2702. procedure TGLStateCache.SetUnpackImageHeight(const Value: Cardinal);
  2703. begin
  2704. if Value <> FUnpackImageHeight then
  2705. begin
  2706. FUnpackImageHeight := Value;
  2707. gl.PixelStoref(GL_UNPACK_IMAGE_HEIGHT, Value);
  2708. end;
  2709. end;
  2710. procedure TGLStateCache.SetUnpackLSBFirst(const Value: TGLboolean);
  2711. begin
  2712. if Value <> FUnpackLSBFirst then
  2713. begin
  2714. FUnpackLSBFirst := Value;
  2715. gl.PixelStorei(GL_UNPACK_LSB_FIRST, byte(Value));
  2716. end;
  2717. end;
  2718. procedure TGLStateCache.SetUnpackRowLength(const Value: Cardinal);
  2719. begin
  2720. if Value <> FUnpackRowLength then
  2721. begin
  2722. FUnpackRowLength := Value;
  2723. gl.PixelStoref(GL_UNPACK_ROW_LENGTH, Value);
  2724. end;
  2725. end;
  2726. procedure TGLStateCache.SetUnpackSkipImages(const Value: Cardinal);
  2727. begin
  2728. if Value <> FUnpackSkipImages then
  2729. begin
  2730. FUnpackSkipImages := Value;
  2731. gl.PixelStoref(GL_UNPACK_SKIP_IMAGES, Value);
  2732. end;
  2733. end;
  2734. procedure TGLStateCache.SetUnpackSkipPixels(const Value: Cardinal);
  2735. begin
  2736. if Value <> FUnpackSkipPixels then
  2737. begin
  2738. FUnpackSkipPixels := Value;
  2739. gl.PixelStoref(GL_UNPACK_SKIP_PIXELS, Value);
  2740. end;
  2741. end;
  2742. procedure TGLStateCache.SetUnpackSkipRows(const Value: Cardinal);
  2743. begin
  2744. if Value <> FUnpackSkipRows then
  2745. begin
  2746. FUnpackSkipRows := Value;
  2747. gl.PixelStoref(GL_UNPACK_SKIP_ROWS, Value);
  2748. end;
  2749. end;
  2750. procedure TGLStateCache.SetUnpackSwapBytes(const Value: TGLboolean);
  2751. begin
  2752. if Value <> FUnpackSwapBytes then
  2753. begin
  2754. FUnpackSwapBytes := Value;
  2755. gl.PixelStorei(GL_UNPACK_SWAP_BYTES, byte(Value));
  2756. end;
  2757. end;
  2758. procedure TGLStateCache.SetViewPort(const Value: TVector4i);
  2759. begin
  2760. if not VectorEquals(Value, FViewPort) or FInsideList then
  2761. begin
  2762. if FInsideList then
  2763. Include(FListStates[FCurrentList], sttViewport)
  2764. else
  2765. FViewPort := Value;
  2766. gl.ViewPort(Value.X, Value.Y, Value.Z, Value.W);
  2767. end;
  2768. end;
  2769. procedure TGLStateCache.SetFFPLight(Value: Boolean);
  2770. begin
  2771. FFFPLight := Value { and not FForwardContext };
  2772. end;
  2773. function TGLStateCache.GetMaxLights: Integer;
  2774. begin
  2775. if FMaxLights = 0 then
  2776. { if FForwardContext then
  2777. FMaxLights := MAX_HARDWARE_LIGHT
  2778. else }
  2779. gl.GetIntegerv(GL_MAX_LIGHTS, @FMaxLights);
  2780. Result := FMaxLights;
  2781. end;
  2782. function TGLStateCache.GetLightEnabling(I: Integer): Boolean;
  2783. begin
  2784. Result := FLightEnabling[I];
  2785. end;
  2786. procedure TGLStateCache.SetLightEnabling(I: Integer; Value: Boolean);
  2787. var
  2788. J, K: Integer;
  2789. begin
  2790. if (FLightEnabling[I] <> Value) or FInsideList then
  2791. begin
  2792. if FInsideList then
  2793. Include(FListStates[FCurrentList], sttLighting)
  2794. else
  2795. FLightEnabling[I] := Value;
  2796. if FFFPLight then
  2797. begin
  2798. if Value then
  2799. gl.Enable(GL_LIGHT0 + I)
  2800. else
  2801. gl.Disable(GL_LIGHT0 + I);
  2802. end;
  2803. K := 0;
  2804. for J := 0 to MAX_HARDWARE_LIGHT - 1 do
  2805. if FLightEnabling[J] then
  2806. begin
  2807. FLightIndices[K] := J;
  2808. Inc(K);
  2809. end;
  2810. FLightNumber := K;
  2811. FShaderLightStatesChanged := True;
  2812. if Assigned(FOnLightsChanged) then
  2813. FOnLightsChanged(Self);
  2814. end;
  2815. end;
  2816. function TGLStateCache.GetLightIndicesAsAddress: PGLInt;
  2817. begin
  2818. Result := @FLightIndices[0];
  2819. end;
  2820. function TGLStateCache.GetLightStateAsAddress: Pointer;
  2821. var
  2822. I, J, C: Integer;
  2823. begin
  2824. C := MinInteger(FLightNumber, MAX_SHADER_LIGHT);
  2825. if FShaderLightStatesChanged then
  2826. begin
  2827. if C > 0 then
  2828. begin
  2829. if GL.VERSION_3_0 then
  2830. begin
  2831. Move(FLightStates.Position,
  2832. FShaderLightStates.Position,
  2833. SizeOf(FShaderLightStates.Position));
  2834. Move(FLightStates.Ambient,
  2835. FShaderLightStates.Ambient,
  2836. SizeOf(FShaderLightStates.Ambient));
  2837. Move(FLightStates.Diffuse,
  2838. FShaderLightStates.Diffuse,
  2839. SizeOf(FShaderLightStates.Diffuse));
  2840. Move(FLightStates.Specular,
  2841. FShaderLightStates.Specular,
  2842. SizeOf(FShaderLightStates.Specular));
  2843. Move(FLightStates.SpotDirection,
  2844. FShaderLightStates.SpotDirection,
  2845. SizeOf(FShaderLightStates.SpotDirection));
  2846. Move(FLightStates.SpotCosCutoffExponent,
  2847. FShaderLightStates.SpotCosCutoffExponent,
  2848. SizeOf(FShaderLightStates.SpotCosCutoffExponent));
  2849. Move(FLightStates.Attenuation,
  2850. FShaderLightStates.Attenuation,
  2851. SizeOf(FShaderLightStates.Attenuation));
  2852. end
  2853. else
  2854. begin
  2855. for I := C - 1 downto 0 do
  2856. begin
  2857. J := FLightIndices[I];
  2858. FShaderLightStates.Position[I] := FLightStates.Position[J];
  2859. FShaderLightStates.Ambient[I] := FLightStates.Ambient[J];
  2860. FShaderLightStates.Diffuse[I] := FLightStates.Diffuse[J];
  2861. FShaderLightStates.Specular[I] := FLightStates.Specular[J];
  2862. FShaderLightStates.SpotDirection[I] := FLightStates.SpotDirection[J];
  2863. FShaderLightStates.SpotCosCutoffExponent[I] := FLightStates.SpotCosCutoffExponent[J];
  2864. FShaderLightStates.Attenuation[I] := FLightStates.Attenuation[J];
  2865. end;
  2866. end;
  2867. end
  2868. else
  2869. FillChar(FShaderLightStatesChanged, SizeOf(FShaderLightStatesChanged), $00);
  2870. FShaderLightStatesChanged := False;
  2871. end;
  2872. Result := @FShaderLightStates;
  2873. end;
  2874. function TGLStateCache.GetLightPosition(I: Integer): TVector;
  2875. begin
  2876. Result := FLightStates.Position[I];
  2877. end;
  2878. procedure TGLStateCache.SetLightPosition(I: Integer; const Value: TVector);
  2879. begin
  2880. if not VectorEquals(Value, FLightStates.Position[I]) then
  2881. begin
  2882. FLightStates.Position[I] := Value;
  2883. FShaderLightStatesChanged := True;
  2884. if Assigned(FOnLightsChanged) then
  2885. FOnLightsChanged(Self);
  2886. end;
  2887. end;
  2888. function TGLStateCache.GetLightSpotDirection(I: Integer): TAffineVector;
  2889. begin
  2890. Result := AffineVectorMake(FLightStates.SpotDirection[I]);
  2891. end;
  2892. procedure TGLStateCache.SetLightSpotDirection(I: Integer; const Value: TAffineVector);
  2893. begin
  2894. if not VectorEquals(Value, AffineVectorMake(FLightStates.SpotDirection[I])) then
  2895. begin
  2896. FLightStates.SpotDirection[I] := VectorMake(Value);
  2897. FShaderLightStatesChanged := True;
  2898. if Assigned(FOnLightsChanged) then
  2899. FOnLightsChanged(Self);
  2900. end;
  2901. end;
  2902. function TGLStateCache.GetLightAmbient(I: Integer): TVector;
  2903. begin
  2904. Result := FLightStates.Ambient[I];
  2905. end;
  2906. procedure TGLStateCache.SetLightAmbient(I: Integer; const Value: TVector);
  2907. begin
  2908. if not VectorEquals(Value, FLightStates.Ambient[I]) or FInsideList then
  2909. begin
  2910. if FInsideList then
  2911. Include(FListStates[FCurrentList], sttLighting)
  2912. else
  2913. FLightStates.Ambient[I] := Value;
  2914. if FFFPLight then
  2915. gl.Lightfv(GL_LIGHT0 + I, GL_AMBIENT, @Value);
  2916. FShaderLightStatesChanged := True;
  2917. if Assigned(FOnLightsChanged) then
  2918. FOnLightsChanged(Self);
  2919. end;
  2920. end;
  2921. function TGLStateCache.GetLightDiffuse(I: Integer): TVector;
  2922. begin
  2923. Result := FLightStates.Diffuse[I];
  2924. end;
  2925. procedure TGLStateCache.SetLightDiffuse(I: Integer; const Value: TVector);
  2926. begin
  2927. if not VectorEquals(Value, FLightStates.Diffuse[I]) or FInsideList then
  2928. begin
  2929. if FInsideList then
  2930. Include(FListStates[FCurrentList], sttLighting)
  2931. else
  2932. FLightStates.Diffuse[I] := Value;
  2933. if FFFPLight then
  2934. gl.Lightfv(GL_LIGHT0 + I, GL_DIFFUSE, @Value);
  2935. FShaderLightStatesChanged := True;
  2936. if Assigned(FOnLightsChanged) then
  2937. FOnLightsChanged(Self);
  2938. end;
  2939. end;
  2940. function TGLStateCache.GetLightSpecular(I: Integer): TVector;
  2941. begin
  2942. Result := FLightStates.Specular[I];
  2943. end;
  2944. procedure TGLStateCache.SetLightSpecular(I: Integer; const Value: TVector);
  2945. begin
  2946. if not VectorEquals(Value, FLightStates.Specular[I]) or FInsideList then
  2947. begin
  2948. if FInsideList then
  2949. Include(FListStates[FCurrentList], sttLighting)
  2950. else
  2951. FLightStates.Specular[I] := Value;
  2952. if FFFPLight then
  2953. gl.Lightfv(GL_LIGHT0 + I, GL_SPECULAR, @Value);
  2954. FShaderLightStatesChanged := True;
  2955. if Assigned(FOnLightsChanged) then
  2956. FOnLightsChanged(Self);
  2957. end;
  2958. end;
  2959. function TGLStateCache.GetSpotCutoff(I: Integer): Single;
  2960. begin
  2961. Result := FSpotCutoff[I];
  2962. end;
  2963. procedure TGLStateCache.SetSpotCutoff(I: Integer; const Value: Single);
  2964. begin
  2965. if (Value <> FSpotCutoff[I]) or FInsideList then
  2966. begin
  2967. if FInsideList then
  2968. Include(FListStates[FCurrentList], sttLighting)
  2969. else
  2970. begin
  2971. FSpotCutoff[I] := Value;
  2972. FLightStates.SpotCosCutoffExponent[I].X := cos(DegToRadian(Value));
  2973. end;
  2974. if FFFPLight then
  2975. gl.Lightfv(GL_LIGHT0 + I, GL_SPOT_CUTOFF, @Value);
  2976. FShaderLightStatesChanged := True;
  2977. if Assigned(FOnLightsChanged) then
  2978. FOnLightsChanged(Self);
  2979. end;
  2980. end;
  2981. function TGLStateCache.GetSpotExponent(I: Integer): Single;
  2982. begin
  2983. Result := FLightStates.SpotCosCutoffExponent[I].Y;
  2984. end;
  2985. procedure TGLStateCache.SetSpotExponent(I: Integer; const Value: Single);
  2986. begin
  2987. if (Value <> FLightStates.SpotCosCutoffExponent[I].Y) or FInsideList then
  2988. begin
  2989. if FInsideList then
  2990. Include(FListStates[FCurrentList], sttLighting)
  2991. else
  2992. FLightStates.SpotCosCutoffExponent[I].Y := Value;
  2993. if FFFPLight then
  2994. gl.Lightfv(GL_LIGHT0 + I, GL_SPOT_EXPONENT, @Value);
  2995. FShaderLightStatesChanged := True;
  2996. if Assigned(FOnLightsChanged) then
  2997. FOnLightsChanged(Self);
  2998. end;
  2999. end;
  3000. function TGLStateCache.GetConstantAtten(I: Integer): Single;
  3001. begin
  3002. Result := FLightStates.Attenuation[I].X;
  3003. end;
  3004. procedure TGLStateCache.SetConstantAtten(I: Integer; const Value: Single);
  3005. begin
  3006. if (Value <> FLightStates.Attenuation[I].X) or FInsideList then
  3007. begin
  3008. if FInsideList then
  3009. Include(FListStates[FCurrentList], sttLighting)
  3010. else
  3011. FLightStates.Attenuation[I].X := Value;
  3012. if FFFPLight then
  3013. gl.Lightfv(GL_LIGHT0 + I, GL_CONSTANT_ATTENUATION, @Value);
  3014. FShaderLightStatesChanged := True;
  3015. if Assigned(FOnLightsChanged) then
  3016. FOnLightsChanged(Self);
  3017. end;
  3018. end;
  3019. function TGLStateCache.GetLinearAtten(I: Integer): Single;
  3020. begin
  3021. Result := FLightStates.Attenuation[I].Y;
  3022. end;
  3023. procedure TGLStateCache.SetLinearAtten(I: Integer; const Value: Single);
  3024. begin
  3025. if (Value <> FLightStates.Attenuation[I].Y) or FInsideList then
  3026. begin
  3027. if FInsideList then
  3028. Include(FListStates[FCurrentList], sttLighting)
  3029. else
  3030. FLightStates.Attenuation[I].Y := Value;
  3031. if FFFPLight then
  3032. gl.Lightfv(GL_LIGHT0 + I, GL_LINEAR_ATTENUATION, @Value);
  3033. FShaderLightStatesChanged := True;
  3034. if Assigned(FOnLightsChanged) then
  3035. FOnLightsChanged(Self);
  3036. end;
  3037. end;
  3038. function TGLStateCache.GetQuadAtten(I: Integer): Single;
  3039. begin
  3040. Result := FLightStates.Attenuation[I].Z;
  3041. end;
  3042. procedure TGLStateCache.SetQuadAtten(I: Integer; const Value: Single);
  3043. begin
  3044. if (Value <> FLightStates.Attenuation[I].Z) or FInsideList then
  3045. begin
  3046. if FInsideList then
  3047. Include(FListStates[FCurrentList], sttLighting)
  3048. else
  3049. FLightStates.Attenuation[I].Z := Value;
  3050. if FFFPLight then
  3051. gl.Lightfv(GL_LIGHT0 + I, GL_QUADRATIC_ATTENUATION, @Value);
  3052. FShaderLightStatesChanged := True;
  3053. if Assigned(FOnLightsChanged) then
  3054. FOnLightsChanged(Self);
  3055. end;
  3056. end;
  3057. procedure TGLStateCache.SetForwardContext(Value: Boolean);
  3058. begin
  3059. { if Value <> FForwardContext then
  3060. begin
  3061. FForwardContext := Value;
  3062. if Value then
  3063. begin
  3064. SetFFPlight(False);
  3065. end;
  3066. end;
  3067. }
  3068. end;
  3069. procedure TGLStateCache.SetGLColorWriting(flag: Boolean);
  3070. begin
  3071. if (FColorWriting <> flag) or FInsideList then
  3072. begin
  3073. if FInsideList then
  3074. Include(FListStates[FCurrentList], sttColorBuffer)
  3075. else
  3076. FColorWriting := flag;
  3077. gl.ColorMask(flag, flag, flag, flag);
  3078. end;
  3079. end;
  3080. procedure TGLStateCache.InvertGLFrontFace;
  3081. begin
  3082. if FFrontFace = fwCounterClockWise then
  3083. FrontFace := fwClockWise
  3084. else
  3085. FrontFace := fwCounterClockWise;
  3086. end;
  3087. procedure TGLStateCache.SetGLState(const aState: TGLState);
  3088. begin
  3089. Enable(aState);
  3090. end;
  3091. procedure TGLStateCache.UnSetGLState(const aState: TGLState);
  3092. begin
  3093. Disable(aState);
  3094. end;
  3095. procedure TGLStateCache.ResetGLPolygonMode;
  3096. begin
  3097. gl.PolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  3098. FPolygonMode := pmFill;
  3099. FPolygonBackMode := pmFill;
  3100. end;
  3101. procedure TGLStateCache.ResetGLMaterialColors;
  3102. begin
  3103. gl.Materialfv(GL_FRONT_AND_BACK, GL_AMBIENT, @clrGray20);
  3104. gl.Materialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, @clrGray80);
  3105. gl.Materialfv(GL_FRONT_AND_BACK, GL_SPECULAR, @clrBlack);
  3106. gl.Materialfv(GL_FRONT_AND_BACK, GL_EMISSION, @clrBlack);
  3107. gl.Materiali(GL_FRONT_AND_BACK, GL_SHININESS, 0);
  3108. FillChar(FFrontBackColors, SizeOf(FFrontBackColors), 127);
  3109. FFrontBackShininess[0] := 0;
  3110. FFrontBackShininess[1] := 0;
  3111. end;
  3112. procedure TGLStateCache.ResetGLTexture(const TextureUnit: Integer);
  3113. var
  3114. t: TGLTextureTarget;
  3115. glTarget: Cardinal;
  3116. begin
  3117. gl.ActiveTexture(GL_TEXTURE0 + TextureUnit);
  3118. for t := Low(TGLTextureTarget) to High(TGLTextureTarget) do
  3119. begin
  3120. glTarget := DecodeTextureTarget(t);
  3121. if IsTargetSupported(glTarget) then
  3122. begin
  3123. gl.BindTexture(glTarget, 0);
  3124. FTextureBinding[TextureUnit, t] := 0;
  3125. end;
  3126. end;
  3127. gl.ActiveTexture(GL_TEXTURE0);
  3128. FActiveTexture := 0;
  3129. end;
  3130. procedure TGLStateCache.ResetGLCurrentTexture;
  3131. var
  3132. a: TGLint;
  3133. t: TGLTextureTarget;
  3134. glTarget: Cardinal;
  3135. begin
  3136. if GL.ARB_multitexture then
  3137. begin
  3138. for a := MaxTextureImageUnits - 1 to 0 do
  3139. begin
  3140. gl.ActiveTexture(GL_TEXTURE0 + a);
  3141. for t := Low(TGLTextureTarget) to High(TGLTextureTarget) do
  3142. begin
  3143. glTarget := DecodeTextureTarget(t);
  3144. if IsTargetSupported(glTarget) then
  3145. begin
  3146. gl.BindTexture(glTarget, 0);
  3147. FTextureBinding[a, t] := 0;
  3148. end;
  3149. end;
  3150. end;
  3151. end
  3152. else
  3153. for t := Low(TGLTextureTarget) to High(TGLTextureTarget) do
  3154. begin
  3155. glTarget := DecodeTextureTarget(t);
  3156. if IsTargetSupported(glTarget) then
  3157. begin
  3158. gl.BindTexture(glTarget, 0);
  3159. FTextureBinding[0, t] := 0;
  3160. end;
  3161. end;
  3162. end;
  3163. procedure TGLStateCache.ResetGLFrontFace;
  3164. begin
  3165. gl.FrontFace(GL_CCW);
  3166. FFrontFace := fwCounterClockWise;
  3167. end;
  3168. procedure TGLStateCache.SetGLFrontFaceCW;
  3169. begin
  3170. if FFrontFace = fwCounterClockWise then
  3171. begin
  3172. gl.FrontFace(GL_CW);
  3173. FFrontFace := fwClockWise;
  3174. end;
  3175. end;
  3176. procedure TGLStateCache.ResetAll;
  3177. begin
  3178. {$WARN SYMBOL_DEPRECATED OFF}
  3179. ResetGLPolygonMode;
  3180. ResetGLMaterialColors;
  3181. ResetGLCurrentTexture;
  3182. ResetGLFrontFace;
  3183. {$WARN SYMBOL_DEPRECATED ON}
  3184. end;
  3185. end.