123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831 |
- //
- // The multimedia graphics platform GLScene https://github.com/glscene
- //
- unit GLS.Context;
- (* Prototypes and base implementation of TGLContext *)
- interface
- {$I GLScene.inc}
- uses
- Winapi.OpenGL,
- Winapi.OpenGLext,
- Winapi.Windows,
- System.Classes,
- System.SysUtils,
- System.Types,
- System.SyncObjs,
- VCL.Forms,
- VCL.Controls,
- VCL.Consts,
- {$IFDEF USE_SERVICE_CONTEXT}
- GLS.Generics,
- {$ENDIF}
- GLS.OpenGLTokens,
- GLS.OpenGLAdapter,
- GLS.XOpenGL,
- GLS.VectorGeometry,
- GLS.Strings,
- GLS.VectorTypes,
- GLS.State,
- GLS.PipelineTransformation,
- GLS.TextureFormat,
- GLS.Logger;
- // Buffer ID's for Multiple-Render-Targets (using GL_ATI_draw_buffers)
- const
- MRT_BUFFERS: array [0 .. 3] of TGLuint = (GL_FRONT_LEFT, GL_AUX0, GL_AUX1, GL_AUX2);
- type
- TGLRCOption = (rcoDoubleBuffered, rcoStereo, rcoDebug, rcoOGL_ES);
- TGLRCOptions = set of TGLRCOption;
- TGLContextLayer = (clUnderlay2, clUnderlay1, clMainPlane, clOverlay1, clOverlay2);
- TFinishTaskEvent = class(TEvent)
- public
- constructor Create; reintroduce;
- end;
- TTaskProcedure = procedure of object; stdcall;
- TServiceContextTask = record
- Task: TTaskProcedure;
- Event: TFinishTaskEvent;
- end;
- {$IFDEF USE_SERVICE_CONTEXT}
- TServiceContextTaskList = {$IFDEF USE_GENERIC_PREFIX} specialize {$ENDIF}
- GThreadList<TServiceContextTask>;
- {$ENDIF USE_SERVICE_CONTEXT}
- TGLContextManager = class;
- TGLContextAcceleration = (chaUnknown, chaHardware, chaSoftware);
- TGLAntiAliasing = (
- // Multisample Antialiasing
- aaDefault, aaNone, aa2x, aa2xHQ, aa4x, aa4xHQ, aa6x, aa8x, aa16x,
- // Coverage Sampling Antialiasing
- csa8x, csa8xHQ, csa16x, csa16xHQ);
- TGLVSyncMode = (vsmSync, vsmNoSync);
- (* Wrapper around an OpenGL rendering context.
- The aim of this class is to offer platform-independant
- initialization, activation and management of OpenGL
- rendering context. The class also offers notifications
- event and error/problems detection.
- This is a virtual abstract a class, and platform-specific
- subclasses must be used. All rendering context share the same lists *)
- TGLContext = class
- private
- FColorBits, FAlphaBits: Integer;
- FDepthBits: Integer;
- FStencilBits: Integer;
- FAccumBits: Integer;
- FAuxBuffers: Integer;
- FAntiAliasing: TGLAntiAliasing;
- FOptions: TGLRCOptions;
- FOnDestroyContext: TNotifyEvent;
- FManager: TGLContextManager;
- FActivationCount: Integer;
- FOwnedHandlesCount: Integer;
- FIsPraparationNeed: Boolean;
- procedure SetColorBits(const aColorBits: Integer); inline;
- procedure SetAlphaBits(const aAlphaBits: Integer); inline;
- procedure SetDepthBits(const val: Integer); inline;
- procedure SetStencilBits(const aStencilBits: Integer); inline;
- procedure SetAccumBits(const aAccumBits: Integer); inline;
- procedure SetAuxBuffers(const aAuxBuffers: Integer); inline;
- procedure SetOptions(const aOptions: TGLRCOptions); inline;
- procedure SetAntiAliasing(const val: TGLAntiAliasing); inline;
- procedure SetAcceleration(const val: TGLContextAcceleration); inline;
- function GetActive: Boolean; inline;
- procedure SetActive(const aActive: Boolean); inline;
- procedure SetLayer(const Value: TGLContextLayer); inline;
- protected
- Fgl: TGLExtensionsAndEntryPoints;
- Fxgl: TGLMultitextureCoordinator;
- FGLStates: TGLStateCache;
- FTransformation: TGLTransformation;
- FAcceleration: TGLContextAcceleration;
- FLayer: TGLContextLayer;
- {$IFNDEF USE_MULTITHREAD}
- FSharedContexts: TList;
- {$ELSE}
- FSharedContexts: TThreadList;
- FLock: TCriticalSection;
- {$ENDIF}
- procedure PropagateSharedContext;
- procedure DoCreateContext(ADeviceHandle: HDC); virtual; abstract;
- procedure DoCreateMemoryContext(outputDevice: HWND; width, height: Integer; BufferCount: Integer = 1); virtual; abstract;
- function DoShareLists(aContext: TGLContext): Boolean; virtual; abstract;
- procedure DoDestroyContext; virtual; abstract;
- procedure DoActivate; virtual; abstract;
- procedure DoDeactivate; virtual; abstract;
- class function ServiceContext: TGLContext;
- procedure MakeGLCurrent;
- public
- constructor Create; virtual;
- destructor Destroy; override;
- // An application-side cache of global per-context OpenGL states and parameters
- property GLStates: TGLStateCache read FGLStates;
- property PipelineTransformation: TGLTransformation read FTransformation;
- // Context manager reference
- property Manager: TGLContextManager read FManager;
- // Color bits for the rendering context
- property ColorBits: Integer read FColorBits write SetColorBits;
- // Alpha bits for the rendering context
- property AlphaBits: Integer read FAlphaBits write SetAlphaBits;
- // Depth bits for the rendering context
- property DepthBits: Integer read FDepthBits write SetDepthBits;
- // Stencil bits for the rendering context
- property StencilBits: Integer read FStencilBits write SetStencilBits;
- // Accumulation buffer bits for the rendering context
- property AccumBits: Integer read FAccumBits write SetAccumBits;
- // Auxiliary buffers bits for the rendering context
- property AuxBuffers: Integer read FAuxBuffers write SetAuxBuffers;
- // AntiAliasing option. Ignored if not hardware supported, currently based on ARB_multisample
- property AntiAliasing: TGLAntiAliasing read FAntiAliasing write SetAntiAliasing;
- // Specifies the layer plane that the rendering context is bound to
- property Layer: TGLContextLayer read FLayer write SetLayer;
- // Rendering context options
- property Options: TGLRCOptions read FOptions write SetOptions;
- (* Allows reading and defining the activity for the context.
- The methods of this property are just wrappers around calls to Activate and Deactivate *)
- property Active: Boolean read GetActive write SetActive;
- // Indicates if the context is hardware-accelerated
- property Acceleration: TGLContextAcceleration read FAcceleration write SetAcceleration;
- (* Triggered whenever the context is destroyed.
- This events happens *before* the context has been
- actually destroyed, OpenGL resource cleanup can still occur here *)
- property OnDestroyContext: TNotifyEvent read FOnDestroyContext write FOnDestroyContext;
- // Creates the context. This method must be invoked before the context can be used
- procedure CreateContext(ADeviceHandle: HDC); overload;
- (* Creates an in-memory context.
- The function should fail if no hardware-accelerated memory context
- can be created (the CreateContext method can handle software OpenGL contexts) *)
- procedure CreateMemoryContext(outputDevice: HWND; width, height: Integer; BufferCount: Integer = 1);
- (* Setup display list sharing between two rendering contexts.
- Both contexts must have the same pixel format *)
- procedure ShareLists(aContext: TGLContext);
- (* Destroy the context. Will fail if no context has been created.
- The method will first invoke the OnDestroyContext
- event, then attempts to deactivate the context
- (if it is active) before destroying it *)
- procedure DestroyContext;
- (* Activates the context.
- A context can be activated multiple times (and must be
- deactivated the same number of times), but this function
- will fail if another context is already active. *)
- procedure Activate;
- (* Deactivates the context. Will fail if the context is not active or another
- context has been activated *)
- procedure Deactivate;
- // Call OnPrepare for all handles
- procedure PrepareHandlesData;
- (* Returns true if the context is valid.
- A context is valid from the time it has been successfully
- created to the time of its destruction. *)
- function IsValid: Boolean; virtual; abstract;
- // Request to swap front and back buffers if they were defined
- procedure SwapBuffers; virtual; abstract;
- // Returns the first compatible context that isn't self in the shares
- function FindCompatibleContext: TGLContext;
- procedure DestroyAllHandles;
- function RenderOutputDevice: Pointer; virtual; abstract;
- // Access to OpenGL command and extension
- property GL: TGLExtensionsAndEntryPoints read Fgl;
- property MultitextureCoordinator: TGLMultitextureCoordinator read Fxgl;
- property IsPraparationNeed: Boolean read FIsPraparationNeed;
- end;
- TGLContextClass = class of TGLContext;
- (* A TGLContext with screen control property and methods.
- This variety of contexts is for drivers that access windows and OpenGL
- through an intermediate opaque cross-platform API.
- TGLSceneViewer won't use them, TGLMemoryViewer may be able to use them,
- but most of the time they will be accessed through a specific viewer
- class/subclass *)
- TGLScreenControlingContext = class(TGLContext)
- strict private
- FWidth, FHeight: Integer;
- FFullScreen: Boolean;
- public
- property Width: Integer read FWidth write FWidth;
- property Height: Integer read FHeight write FHeight;
- property FullScreen: Boolean read FFullScreen write FFullScreen;
- end;
- PGLRCHandle = ^TGLRCHandle;
- TGLRCHandle = record
- FRenderingContext: TGLContext;
- FHandle: TGLuint;
- FChanged: Boolean;
- end;
- TOnPrepareHandleData = procedure(aContext: TGLContext) of object;
- (* Wrapper around an OpenGL context handle.
- This wrapper also takes care of context registrations and data releases
- related to context releases an cleanups. This is an abstract class,
- use the TGLListHandle and TGLTextureHandle subclasses *)
- TGLContextHandle = class
- private
- FHandles: TList;
- FLastHandle: PGLRCHandle;
- FOnPrepare: TOnPrepareHandleData;
- function GetHandle: TGLuint; inline;
- function GetContext: TGLContext;
- function SearchRC(aContext: TGLContext): PGLRCHandle;
- function RCItem(AIndex: Integer): PGLRCHandle; inline;
- procedure CheckCurrentRC;
- protected
- // Invoked by when there is no compatible context left for relocation
- procedure ContextDestroying;
- // Specifies if the handle can be transfered across shared contexts
- class function Transferable: Boolean; virtual;
- class function IsValid(const ID: TGLuint): Boolean; virtual;
- function DoAllocateHandle: TGLuint; virtual; abstract;
- procedure DoDestroyHandle(var AHandle: TGLuint); virtual; abstract;
- public
- constructor Create; virtual;
- constructor CreateAndAllocate(failIfAllocationFailed: Boolean = True);
- destructor Destroy; override;
- // Return OpenGL identifier in current context
- property Handle: TGLuint read GetHandle;
- (* Return current rendering context if handle is allocated in it
- or first context where handle is allocated. *)
- property RenderingContext: TGLContext read GetContext;
- // Return True is data need update in current context
- function IsDataNeedUpdate: Boolean; inline;
- // Return True if data updated in all contexts
- function IsDataComplitelyUpdated: Boolean;
- // Notify the data was updated in current context
- procedure NotifyDataUpdated;
- // Notify the data was changed through all context
- procedure NotifyChangesOfData;
- // Checks if required extensions / OpenGL version are met
- class function IsSupported: Boolean; virtual;
- function IsAllocatedForContext(aContext: TGLContext = nil): Boolean;
- function IsShared: Boolean;
- function AllocateHandle: TGLuint;
- procedure DestroyHandle;
- property OnPrapare: TOnPrepareHandleData read FOnPrepare write FOnPrepare;
- end;
- TGLVirtualHandle = class;
- TGLVirtualHandleEvent = procedure(Sender: TGLVirtualHandle; var Handle: TGLuint) of object;
- // A context handle with event-based handle allocation and destruction
- TGLVirtualHandle = class(TGLContextHandle)
- private
- FOnAllocate, FOnDestroy: TGLVirtualHandleEvent;
- FTag: Integer;
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function Transferable: Boolean; override;
- public
- property OnAllocate: TGLVirtualHandleEvent read FOnAllocate write FOnAllocate;
- property OnDestroy: TGLVirtualHandleEvent read FOnDestroy write FOnDestroy;
- property Tag: Integer read FTag write FTag;
- end;
- // Transferable virtual handle
- TGLVirtualHandleTransf = class(TGLVirtualHandle)
- protected
- class function Transferable: Boolean; override;
- end;
- // Manages a handle to a display list
- TGLListHandle = class(TGLContextHandle)
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- procedure NewList(mode: TGLuint); inline;
- procedure EndList; inline;
- procedure CallList; inline;
- end;
- // Manages a handle to a texture
- TGLTextureHandle = class(TGLContextHandle)
- private
- FTarget: TGLTextureTarget;
- procedure SetTarget(ATarget: TGLTextureTarget);
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- property Target: TGLTextureTarget read FTarget write SetTarget;
- end;
- // Manages a handle to a sampler
- TGLSamplerHandle = class(TGLContextHandle)
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a query.
- Do not use this class directly, use one of its subclasses instead. *)
- TGLQueryHandle = class(TGLContextHandle)
- private
- FActive: Boolean;
- protected
- class function Transferable: Boolean; override;
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- function GetTarget: TGLuint; virtual; abstract;
- function GetQueryType: TGLQueryType; virtual; abstract;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- procedure BeginQuery;
- procedure EndQuery;
- // Check if result is available from the query. Result may not be available
- // immediately after ending the query
- function IsResultAvailable: Boolean;
- // Number of bits used to store the query result. eg. 32/64 bit
- function CounterBits: Integer;
- // Retrieve query result, may cause a stall if the result is not available yet
- function QueryResultInt: TGLint;
- function QueryResultUInt: TGLUInt;
- function QueryResultInt64: TGLint64EXT;
- function QueryResultUInt64: TGLuint64EXT;
- function QueryResultBool: TGLboolean;
- property Target: TGLuint read GetTarget;
- property QueryType: TGLQueryType read GetQueryType;
- // True if within a Begin/EndQuery.
- property Active: Boolean read FActive;
- end;
- (* Manages a handle to an occlusion query.
- Requires OpenGL 1.5+
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user. *)
- TGLOcclusionQueryHandle = class(TGLQueryHandle)
- protected
- function GetTarget: TGLuint; override;
- function GetQueryType: TGLQueryType; override;
- public
- class function IsSupported: Boolean; override;
- // Number of samples (pixels) drawn during the query, some pixels may
- // be drawn to several times in the same query
- function PixelCount: Integer;
- end;
- TGLBooleanOcclusionQueryHandle = class(TGLQueryHandle)
- protected
- function GetTarget: TGLuint; override;
- function GetQueryType: TGLQueryType; override;
- public
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a timer query.
- Requires GL_EXT_timer_query extension.
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user. *)
- TGLTimerQueryHandle = class(TGLQueryHandle)
- protected
- function GetTarget: TGLuint; override;
- function GetQueryType: TGLQueryType; override;
- public
- class function IsSupported: Boolean; override;
- // Time, in nanoseconds (1 ns = 10^-9 s) between starting + ending the query.
- // with 32 bit integer can measure up to approximately 4 seconds, use
- // QueryResultUInt64 if you may need longer
- function Time: Integer;
- end;
- (* Manages a handle to a primitive query.
- Requires OpenGL 3.0+
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user. *)
- TGLPrimitiveQueryHandle = class(TGLQueryHandle)
- protected
- function GetTarget: TGLuint; override;
- function GetQueryType: TGLQueryType; override;
- public
- class function IsSupported: Boolean; override;
- // Number of primitives (eg. Points, Triangles etc.) drawn whilst the
- // query was active
- function PrimitivesGenerated: Integer;
- end;
- (* Manages a handle to a Buffer Object.
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user. *)
- TGLBufferObjectHandle = class(TGLContextHandle)
- private
- FSize: Integer;
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- function GetTarget: TGLuint; virtual; abstract;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- // Creates the buffer object buffer and initializes it.
- constructor CreateFromData(p: Pointer; size: Integer; bufferUsage: TGLuint);
- procedure Bind; virtual; abstract;
- // Note that it is not necessary to UnBind before Binding another buffer.
- procedure UnBind; virtual; abstract;
- (* Bind a buffer object to an indexed target, used by transform feedback
- buffer objects and uniform buffer objects. (OpenGL 3.0+) *)
- procedure BindRange(index: TGLuint; offset: TGLintptr; size: TGLsizeiptr); virtual;
- // Equivalent to calling BindRange with offset = 0, and size = the size of buffer.
- procedure BindBase(index: TGLuint); virtual;
- procedure UnBindBase(index: TGLuint); virtual;
- (* Specifies buffer content.
- Common bufferUsage values are GL_STATIC_DRAW_ARB for data that will
- change rarely, but be used often, GL_STREAM_DRAW_ARB for data specified
- once but used only a few times, and GL_DYNAMIC_DRAW_ARB for data
- that is re-specified very often. Valid only if the buffer has been bound. *)
- procedure BufferData(p: Pointer; size: Integer; bufferUsage: TGLuint);
- // Invokes Bind then BufferData
- procedure BindBufferData(p: Pointer; size: Integer; bufferUsage: TGLuint);
- (* Updates part of an already existing buffer.
- offset and size indicate which part of the data in the buffer is
- to bo modified and p where the data should be taken from. *)
- procedure BufferSubData(offset, size: Integer; p: Pointer);
- (* Map buffer content to memory.
- Values for access are GL_READ_ONLY_ARB, GL_WRITE_ONLY_ARB and
- GL_READ_WRITE_ARB.
- Valid only if the buffer has been bound, must be followed by
- an UnmapBuffer, only one buffer may be mapped at a time. *)
- function MapBuffer(access: TGLuint): Pointer;
- function MapBufferRange(offset: TGLint; len: TGLsizei; access: TGLbitfield): Pointer;
- procedure Flush(offset: TGLint; len: TGLsizei);
- (* Unmap buffer content from memory.
- Must follow a MapBuffer, and happen before the buffer is unbound. *)
- function UnmapBuffer: Boolean;
- class function IsSupported: Boolean; override;
- property Target: TGLuint read GetTarget;
- property BufferSize: Integer read FSize;
- end;
- (* Manages a handle to an Vertex Buffer Object, VBO.
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user.
- Do not use this class directly, use one of its subclasses instead. *)
- TGLVBOHandle = class(TGLBufferObjectHandle)
- private
- function GetVBOTarget: TGLuint;
- public
- property VBOTarget: TGLuint read GetVBOTarget;
- end;
- // Manages a handle to VBO Array Buffer. Typically used to store vertices, normals, texcoords, etc.
- TGLVBOArrayBufferHandle = class(TGLVBOHandle)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- end;
- // Manages a handle to VBO Element Array Buffer. Typically used to store vertex indices.
- TGLVBOElementArrayHandle = class(TGLVBOHandle)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- end;
- (* Manages a handle to PBO Pixel Pack Buffer.
- When bound, commands such as ReadPixels write their data into a buffer object. *)
- TGLPackPBOHandle = class(TGLBufferObjectHandle)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to PBO Pixel Unpack Buffer.
- When bound, commands such as DrawPixels read
- their data from a buffer object *)
- TGLUnpackPBOHandle = class(TGLBufferObjectHandle)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a Transform Feedback Buffer Object.
- Transform feedback buffers can be used to capture vertex data from the
- vertex or geometry shader stage to perform further processing without
- going on to the fragment shader stage. *)
- TGLTransformFeedbackBufferHandle = class(TGLBufferObjectHandle)
- // FTransformFeedbackBufferBuffer: array[0..15] of Cardinal; // (0, 0, 0, ...)
- // FTransformFeedbackBufferStart: array[0..15] of TGLuint64; // (0, 0, 0, ...)
- // FTransformFeedbackBufferSize: array[0..15] of TGLuint64; // (0, 0, 0, ...)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- procedure BeginTransformFeedback(primitiveMode: TGLuint);
- procedure EndTransformFeedback();
- procedure BindRange(index: TGLuint; offset: TGLintptr; size: TGLsizeiptr); override;
- procedure BindBase(index: TGLuint); override;
- procedure UnBindBase(index: TGLuint); override;
- class function IsSupported: Boolean; override;
- end;
- // Manages a handle to a Buffer Texture. (TBO)
- TGLTextureBufferHandle = class(TGLBufferObjectHandle)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a Uniform Buffer Object (UBO).
- Uniform buffer objects store "uniform blocks"; groups of uniforms
- that can be passed as a group into a GLSL program *)
- TGLUniformBufferHandle = class(TGLBufferObjectHandle)
- /// FUniformBufferBuffer: array[0..15] of GLuint; // (0, 0, 0, ...)
- /// FUniformBufferStart: array[0..15] of TGLuint64; // (0, 0, 0, ...)
- /// FUniformBufferSize: array[0..15] of TGLuint64; // (0, 0, 0, ...)
- protected
- function GetTarget: TGLuint; override;
- public
- procedure Bind; override;
- procedure UnBind; override;
- procedure BindRange(index: TGLuint; offset: TGLintptr; size: TGLsizeiptr); override;
- procedure BindBase(index: TGLuint); override;
- procedure UnBindBase(index: TGLuint); override;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a Vertex Array Object (VAO). Vertex array objects are used
- to rapidly switch between large sets of array state *)
- TGLVertexArrayHandle = class(TGLContextHandle)
- protected
- class function Transferable: Boolean; override;
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- procedure Bind;
- procedure UnBind;
- class function IsSupported: Boolean; override;
- end;
- TGLFramebufferStatus = (fsComplete, fsIncompleteAttachment, fsIncompleteMissingAttachment, fsIncompleteDuplicateAttachment,
- fsIncompleteDimensions, fsIncompleteFormats, fsIncompleteDrawBuffer, fsIncompleteReadBuffer, fsUnsupported,
- fsIncompleteMultisample, fsStatusError);
- (* Manages a handle to a Framebuffer Object (FBO).
- Framebuffer objects provide a way of drawing to rendering
- destinations other than the buffers provided to the GL by the
- window-system. One or more "framebuffer-attachable images" can be attached
- to a Framebuffer for uses such as: offscreen rendering, "render to texture" +
- "multiple render targets" (MRT).
- There are several types of framebuffer-attachable images:
- - The image of a renderbuffer object, which is always 2D.
- - A single level of a 1D texture, which is treated as a 2D image with a height of one.
- - A single level of a 2D or rectangle texture.
- - A single face of a cube map texture level, which is treated as a 2D image.
- - A single layer of a 1D or 2D array texture or 3D texture, which is treated as a 2D image.
- Additionally, an entire level of a 3D texture, cube map texture,
- or 1D or 2D array texture can be attached to an attachment point.
- Such attachments are treated as an array of 2D images, arranged in
- layers, and the corresponding attachment point is considered to be layered *)
- TGLFramebufferHandle = class(TGLContextHandle)
- protected
- class function Transferable: Boolean; override;
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- // Bind framebuffer for both drawing + reading
- procedure Bind;
- // Bind framebuffer for drawing
- procedure BindForDrawing;
- // Bind framebuffer for reading
- procedure BindForReading;
- { Note that it is not necessary to unbind before binding another framebuffer. }
- procedure UnBind;
- procedure UnBindForDrawing;
- procedure UnBindForReading;
- // target = GL_DRAW_FRAMEBUFFER, GL_READ_FRAMEBUFFER, GL_FRAMEBUFFER (attach to both READ + DRAW)
- // attachment = COLOR_ATTACHMENTi, DEPTH_ATTACHMENT, STENCIL_ATTACHMENT, DEPTH_STENCIL_ATTACHMENT
- procedure Attach1DTexture(Target: TGLuint; attachment: TGLuint; textarget: TGLuint; texture: TGLuint; level: TGLint);
- procedure Attach2DTexture(Target: TGLuint; attachment: TGLuint; textarget: TGLuint; texture: TGLuint; level: TGLint);
- procedure Attach3DTexture(Target: TGLuint; attachment: TGLuint; textarget: TGLuint; texture: TGLuint; level: TGLint; Layer: TGLint);
- procedure AttachLayer(Target: TGLuint; attachment: TGLuint; texture: TGLuint; level: TGLint; Layer: TGLint);
- procedure AttachRenderBuffer(Target: TGLuint; attachment: TGLuint; renderbuffertarget: TGLuint; renderbuffer: TGLuint);
- (* OpenGL 3.2+ only.
- If texture is the name of a three-dimensional texture, cube map texture, one-or
- two-dimensional array texture, or two-dimensional multisample array texture, the
- texture level attached to the framebuffer attachment point is an array of images,
- and the framebuffer attachment is considered layered *)
- procedure AttachTexture(Target: TGLuint; attachment: TGLuint; texture: TGLuint; level: TGLint);
- // OpenGL 3.2+ only
- procedure AttachTextureLayer(Target: TGLuint; attachment: TGLuint; texture: TGLuint; level: TGLint; Layer: TGLint);
- // copy rect from bound read framebuffer to bound draw framebuffer
- procedure Blit(srcX0: TGLint; srcY0: TGLint; srcX1: TGLint; srcY1: TGLint; dstX0: TGLint; dstY0: TGLint; dstX1: TGLint;
- dstY1: TGLint; mask: TGLbitfield; filter: TGLuint);
- (* target = GL_DRAW_FRAMEBUFFER, GL_READ_FRAMEBUFFER, GL_FRAMEBUFFER (equivalent to GL_DRAW_FRAMEBUFFER)
- If default framebuffer (0) is bound:
- attachment = GL_FRONT_LEFT, GL_FRONT_RIGHT, GL_BACK_LEFT, or GL_BACK_RIGHT, GL_DEPTH, GL_STENCIL
- if a framebuffer object is bound:
- attachment = GL_COLOR_ATTACHMENTi, GL_DEPTH_ATTACHMENT, GL_STENCIL_ATTACHMENT, GL_DEPTH_STENCIL_ATTACHMENT
- param = GL_FRAMEBUFFER_ATTACHMENT_(OBJECT_TYPE, OBJECT_NAME,
- RED_SIZE, GREEN_SIZE, BLUE_SIZE, ALPHA_SIZE, DEPTH_SIZE, STENCIL_SIZE,
- COMPONENT_TYPE, COLOR_ENCODING, TEXTURE_LEVEL, LAYERED, TEXTURE_CUBE_MAP_FACE, TEXTURE_LAYER *)
- function GetAttachmentParameter(Target: TGLuint; attachment: TGLuint; pname: TGLuint): TGLint;
- (* Returns the type of object bound to attachment point:
- GL_NONE, GL_FRAMEBUFFER_DEFAULT, GL_TEXTURE, or GL_RENDERBUFFER *)
- function GetAttachmentObjectType(Target: TGLuint; attachment: TGLuint): TGLint;
- // Returns the name (ID) of the texture or renderbuffer attached to attachment point
- function GetAttachmentObjectName(Target: TGLuint; attachment: TGLuint): TGLint;
- function GetStatus: TGLFramebufferStatus;
- function GetStringStatus(out clarification: string): TGLFramebufferStatus;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a Renderbuffer Object.
- A Renderbuffer is a "framebuffer-attachable image" for generalized offscreen
- rendering and it also provides a means to support rendering to GL logical
- buffer types which have no corresponding texture format (stencil, accum, etc). *)
- TGLRenderbufferHandle = class(TGLContextHandle)
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- procedure Bind;
- procedure UnBind;
- procedure SetStorage(internalformat: TGLuint; width, height: TGLsizei);
- procedure SetStorageMultisample(internalformat: TGLuint; samples: TGLsizei; width, height: TGLsizei);
- class function IsSupported: Boolean; override;
- end;
- TGLARBProgramHandle = class(TGLContextHandle)
- private
- FReady: Boolean;
- FInfoLog: string;
- protected
- function DoAllocateHandle: TGLuint; override;
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- class function GetTarget: TGLuint; virtual; abstract;
- public
- procedure LoadARBProgram(const AText: string);
- procedure Enable;
- procedure Disable;
- procedure Bind;
- property Ready: Boolean read FReady;
- property InfoLog: string read FInfoLog;
- end;
- TGLARBVertexProgramHandle = class(TGLARBProgramHandle)
- protected
- class function GetTarget: TGLuint; override;
- public
- class function IsSupported: Boolean; override;
- end;
- TGLARBFragmentProgramHandle = class(TGLARBProgramHandle)
- protected
- class function GetTarget: TGLuint; override;
- public
- class function IsSupported: Boolean; override;
- end;
- TGLARBGeometryProgramHandle = class(TGLARBProgramHandle)
- protected
- class function GetTarget: TGLuint; override;
- public
- class function IsSupported: Boolean; override;
- end;
- (* Base class for GLSL handles (programs and shaders).
- Do not use this class directly, use one of its subclasses instead *)
- TGLSLHandle = class(TGLContextHandle)
- protected
- procedure DoDestroyHandle(var AHandle: TGLuint); override;
- public
- function InfoLog: string;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a handle to a Shader Object.
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user.
- Do not use this class directly, use one of its subclasses instead *)
- TGLShaderHandle = class(TGLSLHandle)
- private
- FShaderType: TGLuint;
- protected
- function DoAllocateHandle: TGLuint; override;
- class function IsValid(const ID: TGLuint): Boolean; override;
- public
- procedure ShaderSource(const source: AnsiString); overload;
- // Returns True if compilation sucessful
- function CompileShader: Boolean;
- property ShaderType: TGLuint read FShaderType;
- end;
- TGLShaderHandleClass = class of TGLShaderHandle;
- // Manages a handle to a Vertex Shader Object
- TGLVertexShaderHandle = class(TGLShaderHandle)
- public
- constructor Create; override;
- class function IsSupported: Boolean; override;
- end;
- // Manages a handle to a Geometry Shader Object
- TGLGeometryShaderHandle = class(TGLShaderHandle)
- public
- constructor Create; override;
- class function IsSupported: Boolean; override;
- end;
- // Manages a handle to a Fragment Shader Object
- TGLFragmentShaderHandle = class(TGLShaderHandle)
- public
- constructor Create; override;
- class function IsSupported: Boolean; override;
- end;
- // Manages a handle to a Tessellation Control Shader Object
- TGLTessControlShaderHandle = class(TGLShaderHandle)
- public
- constructor Create; override;
- class function IsSupported: Boolean; override;
- end;
- // Manages a handle to a Tessellation Evaluation Shader Object
- TGLTessEvaluationShaderHandle = class(TGLShaderHandle)
- public
- constructor Create; override;
- class function IsSupported: Boolean; override;
- end;
- (* Manages a GLSL Program Object.
- Does *NOT* check for extension availability, this is assumed to have been
- checked by the user *)
- TGLProgramHandle = class(TGLSLHandle)
- public
- class function IsValid(const ID: TGLuint): Boolean; override;
- private
- FName: string;
- function GetUniform1i(const index: string): Integer;
- procedure SetUniform1i(const index: string; val: Integer);
- function GetUniform2i(const index: string): TVector2i;
- procedure SetUniform2i(const index: string; const Value: TVector2i);
- function GetUniform3i(const index: string): TVector3i;
- procedure SetUniform3i(const index: string; const Value: TVector3i);
- function GetUniform4i(const index: string): TVector4i;
- procedure SetUniform4i(const index: string; const Value: TVector4i);
- function GetUniform1f(const index: string): Single;
- procedure SetUniform1f(const index: string; val: Single);
- function GetUniform2f(const index: string): TVector2f;
- procedure SetUniform2f(const index: string; const val: TVector2f);
- function GetUniform3f(const index: string): TAffineVector;
- procedure SetUniform3f(const index: string; const val: TAffineVector);
- function GetUniform4f(const index: string): TGLVector;
- procedure SetUniform4f(const index: string; const val: TGLVector);
- function GetUniformMatrix2fv(const index: string): TMatrix2f;
- procedure SetUniformMatrix2fv(const index: string; const val: TMatrix2f);
- function GetUniformMatrix3fv(const index: string): TMatrix3f;
- procedure SetUniformMatrix3fv(const index: string; const val: TMatrix3f);
- function GetUniformMatrix4fv(const index: string): TGLMatrix;
- procedure SetUniformMatrix4fv(const index: string; const val: TGLMatrix);
- function GetUniformTextureHandle(const Index: string; const TextureIndex: Integer; const TextureTarget: TGLTextureTarget)
- : TGLuint;
- procedure SetUniformTextureHandle(const Index: string; const TextureIndex: Integer; const TextureTarget: TGLTextureTarget;
- const Value: TGLuint);
- procedure SetUniformBuffer(const Index: string; Value: TGLUniformBufferHandle);
- protected
- function DoAllocateHandle: TGLuint; override;
- public
- property Name: string read FName write FName;
- constructor Create; override;
- (* Compile and attach a new shader.
- Raises an EGLShader exception in case of failure. *)
- procedure AddShader(ShaderType: TGLShaderHandleClass; const ShaderSource: string;
- treatWarningsAsErrors: Boolean = False);
- procedure AttachObject(shader: TGLShaderHandle);
- procedure DetachAllObject;
- procedure BindAttribLocation(index: Integer; const aName: string);
- procedure BindFragDataLocation(index: Integer; const aName: string);
- function LinkProgram: Boolean;
- function ValidateProgram: Boolean;
- function GetAttribLocation(const aName: string): Integer;
- function GetUniformLocation(const aName: string): Integer;
- function GetUniformOffset(const aName: string): PGLInt;
- function GetUniformBlockIndex(const aName: string): Integer;
- function GetVaryingLocation(const aName: string): Integer;
- // Currently, NVidia-specific.
- procedure AddActiveVarying(const aName: string);
- // Currently, NVidia-specific.
- function GetUniformBufferSize(const aName: string): Integer;
- procedure UseProgramObject;
- procedure EndUseProgramObject;
- procedure SetUniformi(const index: string; const val: Integer); overload;
- procedure SetUniformi(const index: string; const val: TVector2i); overload;
- procedure SetUniformi(const index: string; const val: TVector3i); overload;
- procedure SetUniformi(const index: string; const val: TVector4i); overload;
- procedure SetUniformf(const index: string; const val: Single); overload;
- procedure SetUniformf(const index: string; const val: TVector2f); overload;
- procedure SetUniformf(const index: string; const val: TVector3f); overload;
- procedure SetUniformf(const index: string; const val: TVector4f); overload;
- // Shader parameters.
- property Uniform1i[const index: string]: Integer read GetUniform1i write SetUniform1i;
- property Uniform2i[const index: string]: TVector2i read GetUniform2i write SetUniform2i;
- property Uniform3i[const index: string]: TVector3i read GetUniform3i write SetUniform3i;
- property Uniform4i[const index: string]: TVector4i read GetUniform4i write SetUniform4i;
- property Uniform1f[const index: string]: Single read GetUniform1f write SetUniform1f;
- property Uniform2f[const index: string]: TVector2f read GetUniform2f write SetUniform2f;
- property Uniform3f[const index: string]: TAffineVector read GetUniform3f write SetUniform3f;
- property Uniform4f[const index: string]: TGLVector read GetUniform4f write SetUniform4f;
- property UniformMatrix2fv[const index: string]: TMatrix2f read GetUniformMatrix2fv write SetUniformMatrix2fv;
- property UniformMatrix3fv[const index: string]: TMatrix3f read GetUniformMatrix3fv write SetUniformMatrix3fv;
- property UniformMatrix4fv[const index: string]: TGLMatrix read GetUniformMatrix4fv write SetUniformMatrix4fv;
- property UniformTextureHandle[const index: string; const TextureIndex: Integer; const TextureTarget: TGLTextureTarget]
- : TGLuint read GetUniformTextureHandle write SetUniformTextureHandle;
- property UniformBuffer[const index: string]: TGLUniformBufferHandle write SetUniformBuffer;
- end;
- TGLContextNotification = record
- obj: TObject;
- Event: TNotifyEvent;
- end;
- // Stores and manages all the TGLContext objects.
- TGLContextManager = class
- private
- FList: TThreadList;
- FTerminated: Boolean;
- FNotifications: array of TGLContextNotification;
- FCreatedRCCount: Integer;
- {$IFDEF USE_MULTITHREAD}
- FHandles: TThreadList;
- {$ELSE}
- FHandles: TList;
- {$ENDIF USE_MULTITHREAD}
- {$IFDEF USE_SERVICE_CONTEXT}
- FThread: TThread;
- FServiceStarter: TEvent;
- FThreadTask: TServiceContextTaskList;
- {$ENDIF}
- FServiceContext: TGLContext;
- protected
- procedure Lock;
- procedure UnLock;
- procedure RegisterContext(aContext: TGLContext);
- procedure UnRegisterContext(aContext: TGLContext);
- procedure ContextCreatedBy(aContext: TGLContext);
- procedure DestroyingContextBy(aContext: TGLContext);
- {$IFDEF USE_SERVICE_CONTEXT}
- // Create a special service and resource-keeper context.
- procedure CreateServiceContext;
- procedure QueueTaskDepleted;
- property ServiceStarter: TEvent read FServiceStarter;
- {$ENDIF}
- property ServiceContext: TGLContext read FServiceContext;
- public
- constructor Create;
- destructor Destroy; override;
- (* Returns an appropriate, ready-to use context.
- The returned context should be freed by caller. *)
- function CreateContext(AClass: TGLContextClass = nil): TGLContext;
- (* Returns the number of TGLContext object.
- This is *not* the number of OpenGL rendering contexts! *)
- function ContextCount: Integer;
- (* Registers a new object to notify when the last context is destroyed.
- When the last rendering context is destroyed, the 'anEvent' will
- be invoked with 'anObject' as parameter.
- Note that the registration is kept until the notification is triggered
- or a RemoveNotification on 'anObject' is issued. *)
- procedure LastContextDestroyNotification(anObject: TObject; anEvent: TNotifyEvent);
- // Unregisters an object from the notification lists.
- procedure RemoveNotification(anObject: TObject);
- // Marks the context manager for termination
- procedure Terminate;
- // Request all contexts to destroy all their handles.
- procedure DestroyAllHandles;
- // Notify all contexts about necessity of handles preparation.
- procedure NotifyPreparationNeed;
- end;
- EGLContext = class(Exception);
- EGLShader = class(EGLContext);
- EPBuffer = class(Exception);
- // Drivers should register themselves via this function.
- procedure RegisterGLContextClass(aGLContextClass: TGLContextClass);
- (* The TGLContext that is the currently active context, if any.
- Returns nil if no context is active. *)
- function CurrentGLContext: TGLContext; inline;
- function SafeCurrentGLContext: TGLContext; inline;
- function IsMainThread: Boolean;
- function IsServiceContextAvaible: Boolean;
- function GetServiceWindow: TForm;
- {$IFDEF USE_SERVICE_CONTEXT}
- procedure AddTaskForServiceContext(ATask: TTaskProcedure; FinishEvent: TFinishTaskEvent = nil);
- {$ENDIF}
- var
- GLContextManager: TGLContextManager;
- vIgnoreOpenGLErrors: Boolean = False;
- vContextActivationFailureOccurred: Boolean = False;
- {$IFDEF USE_MULTITHREAD}
- threadvar
- {$ELSE}
- var
- {$ENDIF}
- vCurrentGLContext: TGLContext;
- GL: TGLExtensionsAndEntryPoints;
- xgl: TGLMultitextureCoordinator;
- vMainThread: Boolean;
- GLwithoutContext: TGLExtensionsAndEntryPoints;
- // ------------------------------------------------------------------
- implementation
- // ------------------------------------------------------------------
- {$IFDEF USE_SERVICE_CONTEXT}
- type
- TServiceContextThread = class(TThread)
- private
- FDC: HDC;
- FWindow: TForm;
- FLastTaskStartTime: Double;
- FReported: Boolean;
- protected
- procedure Execute; override;
- procedure DoCreateServiceContext; stdcall;
- public
- constructor Create;
- destructor Destroy; override;
- end;
- {$ENDIF}
- var
- vContextClasses: TList;
- vServiceWindow: TForm;
- {$IFDEF USE_SERVICE_CONTEXT}
- OldInitProc: Pointer;
- {$ENDIF}
- function CurrentGLContext: TGLContext; inline;
- begin
- Result := vCurrentGLContext;
- end;
- function SafeCurrentGLContext: TGLContext; inline;
- begin
- Result := CurrentGLContext;
- if not Assigned(Result) then
- begin
- {$IFDEF USE_LOGGING}
- LogError(strNoActiveRC);
- {$ENDIF}
- Abort;
- end;
- end;
- function IsMainThread: Boolean;
- begin
- Result := vMainThread;
- end;
- function IsServiceContextAvaible: Boolean;
- begin
- Result := GLContextManager.ServiceContext <> nil;
- end;
- function GetServiceWindow: TForm;
- begin
- Result := vServiceWindow;
- end;
- procedure RegisterGLContextClass(aGLContextClass: TGLContextClass);
- begin
- if not Assigned(vContextClasses) then
- vContextClasses := TList.Create;
- vContextClasses.Add(aGLContextClass);
- end;
- // ------------------
- // ------------------ TGLContext ------------------
- // ------------------
- constructor TGLContext.Create;
- begin
- inherited Create;
- {$IFDEF USE_MULTITHREAD}
- FLock := TCriticalSection.Create;
- {$ENDIF}
- FColorBits := 32;
- FStencilBits := 0;
- FAccumBits := 0;
- FAuxBuffers := 0;
- FLayer := clMainPlane;
- FOptions := [];
- {$IFNDEF USE_MULTITHREAD}
- FSharedContexts := TList.Create;
- {$ELSE}
- FSharedContexts := TThreadList.Create;
- {$ENDIF}
- FSharedContexts.Add(Self);
- FAcceleration := chaUnknown;
- FGLStates := TGLStateCache.Create;
- FGL := TGLExtensionsAndEntryPoints.Create;
- FTransformation := TGLTransformation.Create;
- FTransformation.LoadMatricesEnabled := True;
- GLContextManager.RegisterContext(Self);
- FIsPraparationNeed := True;
- FXGL := TGLMultitextureCoordinator.Create;
- end;
- destructor TGLContext.Destroy;
- begin
- if IsValid then
- DestroyContext;
- GLContextManager.UnRegisterContext(Self);
- FGLStates.Free;
- FGL.Free;
- FXGL.Free;
- FTransformation.Free;
- FSharedContexts.Free;
- {$IFDEF USE_MULTITHREAD}
- FLock.Free;
- {$ENDIF}
- inherited Destroy;
- end;
- procedure TGLContext.SetColorBits(const aColorBits: Integer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FColorBits := aColorBits;
- end;
- procedure TGLContext.SetAlphaBits(const aAlphaBits: Integer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FAlphaBits := aAlphaBits;
- end;
- procedure TGLContext.SetDepthBits(const val: Integer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FDepthBits := val;
- end;
- procedure TGLContext.SetLayer(const Value: TGLContextLayer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FLayer := Value;
- end;
- procedure TGLContext.SetStencilBits(const aStencilBits: Integer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FStencilBits := aStencilBits;
- end;
- procedure TGLContext.SetAccumBits(const aAccumBits: Integer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FAccumBits := aAccumBits;
- end;
- procedure TGLContext.SetAuxBuffers(const aAuxBuffers: Integer);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FAuxBuffers := aAuxBuffers;
- end;
- procedure TGLContext.SetOptions(const aOptions: TGLRCOptions);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FOptions := aOptions;
- end;
- procedure TGLContext.SetAntiAliasing(const val: TGLAntiAliasing);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FAntiAliasing := val;
- end;
- procedure TGLContext.SetAcceleration(const val: TGLContextAcceleration);
- begin
- if Active then
- raise EGLContext.Create(strCannotAlterAnActiveContext)
- else
- FAcceleration := val;
- end;
- function TGLContext.GetActive: Boolean;
- begin
- Result := (FActivationCount > 0);
- end;
- procedure TGLContext.SetActive(const aActive: Boolean);
- begin
- // activation/deactivation can be nested...
- while aActive <> Active do
- begin
- if aActive then
- Activate
- else
- Deactivate;
- end;
- end;
- procedure TGLContext.CreateContext(ADeviceHandle: HDC);
- begin
- if IsValid then
- raise EGLContext.Create(strContextAlreadyCreated);
- DoCreateContext(ADeviceHandle);
- Manager.ContextCreatedBy(Self);
- end;
- procedure TGLContext.CreateMemoryContext(outputDevice: HWND; Width, Height: Integer; BufferCount: Integer);
- begin
- if IsValid then
- raise EGLContext.Create(strContextAlreadyCreated);
- DoCreateMemoryContext(outputDevice, width, height, BufferCount);
- Manager.ContextCreatedBy(Self);
- end;
- procedure TGLContext.PrepareHandlesData;
- var
- I: Integer;
- LHandle: TGLContextHandle;
- begin
- if vCurrentGLContext = Self then
- begin
- {$IFNDEF USE_MULTITHREAD}
- for I := Manager.FHandles.Count - 1 downto 0 do
- begin
- LHandle := TGLContextHandle(Manager.FHandles[I]);
- if Assigned(LHandle.FOnPrepare) then
- LHandle.FOnPrepare(Self);
- end;
- {$ELSE}
- with Manager.FHandles.LockList do
- try
- for I := Count - 1 downto 0 do
- begin
- LHandle := TGLContextHandle(Items[I]);
- if Assigned(LHandle.FOnPrepare) then
- LHandle.FOnPrepare(Self);
- end;
- finally
- Manager.FHandles.UnlockList;
- end;
- {$ENDIF}
- FIsPraparationNeed := False;
- end;
- end;
- procedure TGLContext.PropagateSharedContext;
- var
- I, j: Integer;
- otherContext: TGLContext;
- otherList: TList;
- begin
- {$IFNDEF USE_MULTITHREAD}
- with FSharedContexts do
- begin
- for I := 1 to Count - 1 do
- begin
- otherContext := TGLContext(Items[I]);
- otherList := otherContext.FSharedContexts;
- for j := 0 to otherList.Count - 1 do
- if IndexOf(otherList[j]) < 0 then
- Add(otherList[j]);
- end;
- for I := 1 to Count - 1 do
- begin
- otherContext := TGLContext(Items[I]);
- otherList := otherContext.FSharedContexts;
- if otherList.IndexOf(Self) < 0 then
- otherList.Add(Self);
- end;
- end;
- {$ELSE}
- with FSharedContexts.LockList do
- try
- for I := 1 to Count - 1 do
- begin
- otherContext := TGLContext(Items[I]);
- otherList := otherContext.FSharedContexts.LockList;
- for j := 0 to otherList.Count - 1 do
- if IndexOf(otherList[j]) < 0 then
- Add(otherList[j]);
- otherContext.FSharedContexts.UnlockList;
- end;
- for I := 1 to Count - 1 do
- begin
- otherContext := TGLContext(Items[I]);
- otherList := otherContext.FSharedContexts.LockList;
- if otherList.IndexOf(Self) < 0 then
- otherList.Add(Self);
- otherContext.FSharedContexts.UnlockList;
- end;
- finally
- FSharedContexts.UnlockList;
- end;
- {$ENDIF}
- end;
- procedure TGLContext.ShareLists(aContext: TGLContext);
- begin
- {$IFNDEF USE_MULTITHREAD}
- if FSharedContexts.IndexOf(aContext) < 0 then
- begin
- if DoShareLists(aContext) then
- begin
- FSharedContexts.Add(aContext);
- PropagateSharedContext;
- end;
- end;
- {$ELSE}
- with FSharedContexts.LockList do
- try
- if IndexOf(aContext) < 0 then
- begin
- if DoShareLists(aContext) then
- begin
- Add(aContext);
- PropagateSharedContext;
- end;
- end;
- finally
- FSharedContexts.UnlockList;
- end;
- {$ENDIF}
- end;
- procedure TGLContext.DestroyAllHandles;
- var
- I: Integer;
- begin
- Activate;
- try
- {$IFNDEF USE_MULTITHREAD}
- for I := Manager.FHandles.Count - 1 downto 0 do
- TGLContextHandle(Manager.FHandles[I]).ContextDestroying;
- {$ELSE}
- with Manager.FHandles.LockList do
- try
- for I := Count - 1 downto 0 do
- TGLContextHandle(Items[I]).ContextDestroying;
- finally
- Manager.FHandles.UnlockList;
- end;
- {$ENDIF}
- finally
- Deactivate;
- end;
- end;
- procedure TGLContext.DestroyContext;
- var
- I: Integer;
- oldContext, otherContext: TGLContext;
- contextHandle: TGLContextHandle;
- aList: TList;
- begin
- if vCurrentGLContext <> Self then
- begin
- oldContext := vCurrentGLContext;
- if Assigned(oldContext) then
- oldContext.Deactivate;
- end
- else
- oldContext := nil;
- Activate;
- try
- {$IFNDEF USE_MULTITHREAD}
- for I := Manager.FHandles.Count - 1 downto 0 do
- begin
- contextHandle := TGLContextHandle(Manager.FHandles[I]);
- contextHandle.ContextDestroying;
- end;
- {$ELSE}
- aList := Manager.FHandles.LockList;
- try
- for I := aList.Count - 1 downto 0 do
- begin
- contextHandle := TGLContextHandle(aList[I]);
- contextHandle.ContextDestroying;
- end;
- finally
- Manager.FHandles.UnlockList;
- end;
- {$ENDIF}
- Manager.DestroyingContextBy(Self);
- {$IFDEF USE_MULTITHREAD}
- aList := FSharedContexts.LockList;
- {$ELSE}
- aList := FSharedContexts;
- {$ENDIF}
- for I := 1 to aList.Count - 1 do
- begin
- otherContext := TGLContext(aList[I]);
- otherContext.FSharedContexts.Remove(Self);
- end;
- FSharedContexts.Clear;
- FSharedContexts.Add(Self);
- {$IFDEF USE_MULTITHREAD}
- FSharedContexts.UnlockList;
- {$ENDIF}
- Active := False;
- DoDestroyContext;
- finally
- if Assigned(oldContext) then
- oldContext.Activate;
- end;
- FAcceleration := chaUnknown;
- FGL.Close;
- end;
- procedure TGLContext.Activate;
- begin
- {$IFDEF USE_MULTITHREAD}
- FLock.Enter;
- {$ENDIF}
- if FActivationCount = 0 then
- begin
- if not IsValid then
- raise EGLContext.Create(strContextNotCreated);
- vContextActivationFailureOccurred := False;
- try
- DoActivate;
- except
- vContextActivationFailureOccurred := True;
- end;
- GLS.Context.GL := FGL;
- xgl := FXGL;
- vCurrentGLContext := Self;
- end
- else
- Assert(vCurrentGLContext = Self, 'vCurrentGLContext <> Self');
- Inc(FActivationCount);
- end;
- procedure TGLContext.Deactivate;
- begin
- Assert(vCurrentGLContext = Self);
- Dec(FActivationCount);
- if FActivationCount = 0 then
- begin
- if not IsValid then
- raise EGLContext.Create(strContextNotCreated);
- if not vContextActivationFailureOccurred then
- DoDeactivate;
- vCurrentGLContext := nil;
- GLS.Context.GL := GLwithoutContext;
- xgl := nil;
- end
- else if FActivationCount < 0 then
- raise EGLContext.Create(strUnbalancedContexActivations);
- {$IFDEF USE_MULTITHREAD}
- FLock.Leave;
- {$ENDIF}
- end;
- function TGLContext.FindCompatibleContext: TGLContext;
- var
- I: Integer;
- begin
- Result := nil;
- {$IFNDEF USE_MULTITHREAD}
- for I := 0 to FSharedContexts.Count - 1 do
- if TGLContext(FSharedContexts[I]) <> Self then
- begin
- Result := TGLContext(FSharedContexts[I]);
- Break;
- end;
- {$ELSE}
- with FSharedContexts.LockList do
- try
- for I := 0 to Count - 1 do
- if TGLContext(Items[I]) <> Self then
- begin
- Result := TGLContext(Items[I]);
- Break;
- end;
- finally
- FSharedContexts.UnlockList;
- end;
- {$ENDIF}
- end;
- class function TGLContext.ServiceContext: TGLContext;
- begin
- Result := GLContextManager.FServiceContext;
- end;
- procedure TGLContext.MakeGLCurrent;
- begin
- GLS.Context.GL := FGL;
- end;
- // ------------------
- // ------------------ TGLContextHandle ------------------
- // ------------------
- constructor TGLContextHandle.Create;
- begin
- inherited Create;
- FHandles := TList.Create;
- // first is a dummy record
- new(FLastHandle);
- FillChar(FLastHandle^, sizeof(FLastHandle^), 0);
- FHandles.Add(FLastHandle);
- GLContextManager.FHandles.Add(Self);
- end;
- constructor TGLContextHandle.CreateAndAllocate(failIfAllocationFailed: Boolean = True);
- begin
- Create;
- AllocateHandle;
- if failIfAllocationFailed and (Handle = 0) then
- raise EGLContext.Create('Auto-allocation failed');
- end;
- destructor TGLContextHandle.Destroy;
- var
- I: Integer;
- begin
- DestroyHandle;
- for I := 0 to FHandles.Count - 1 do
- Dispose(RCItem(I));
- FHandles.Free;
- if Assigned(GLContextManager) then
- GLContextManager.FHandles.Remove(Self);
- inherited Destroy;
- end;
- function TGLContextHandle.AllocateHandle: Cardinal;
- var
- I: Integer;
- bSucces: Boolean;
- aList: TList;
- p: PGLRCHandle;
- begin
- // if handle aready allocated in current context
- Result := GetHandle;
- if Result <> 0 then
- exit;
- if vCurrentGLContext = nil then
- begin
- {$IFDEF USE_LOGGING}
- GLSLogger.LogError('Failed to allocate OpenGL identifier - no active rendering context!');
- {$ENDIF}
- exit;
- end;
- // add entry
- new(FLastHandle);
- FillChar(FLastHandle^, sizeof(FLastHandle^), 0);
- FHandles.Add(FLastHandle);
- FLastHandle.FRenderingContext := vCurrentGLContext;
- bSucces := False;
- if Transferable then
- begin
- {$IFNDEF USE_MULTITHREAD}
- aList := vCurrentGLContext.FSharedContexts;
- {$ELSE}
- aList := vCurrentGLContext.FSharedContexts.LockList;
- try
- {$ENDIF}
- for I := aList.Count - 1 downto 0 do
- begin
- p := SearchRC(aList[I]);
- if (p.FHandle > 0) then
- begin
- // Copy shared handle
- // FLastHandle.FRenderingContext := vCurrentGLContext;
- FLastHandle.FHandle := p.FHandle;
- FLastHandle.FChanged := p.FChanged;
- Inc(vCurrentGLContext.FOwnedHandlesCount);
- bSucces := True;
- Break;
- end;
- end;
- {$IFNDEF USE_MULTITHREAD}
- {$ELSE}
- finally
- vCurrentGLContext.FSharedContexts.UnlockList;
- end;
- {$ENDIF}
- end;
- if not bSucces then
- begin
- // Allocate handle in current context
- FLastHandle.FHandle := DoAllocateHandle;
- bSucces := FLastHandle.FHandle <> 0;
- FLastHandle.FChanged := bSucces;
- if bSucces then
- Inc(vCurrentGLContext.FOwnedHandlesCount);
- end;
- Result := FLastHandle.FHandle;
- if not bSucces then
- GLSLogger.LogError(strNoActiveRC)
- else if Assigned(FOnPrepare) then
- GLContextManager.NotifyPreparationNeed;
- end;
- function TGLContextHandle.IsAllocatedForContext(aContext: TGLContext = nil): Boolean;
- begin
- Result := SearchRC(aContext).FHandle > 0;
- end;
- function TGLContextHandle.SearchRC(aContext: TGLContext): PGLRCHandle;
- var
- I: Integer;
- begin
- if aContext = nil then
- aContext := vCurrentGLContext;
- if aContext = FLastHandle.FRenderingContext then
- begin
- Result := FLastHandle;
- exit;
- end;
- for I := 1 to FHandles.Count - 1 do
- if RCItem(I).FRenderingContext = aContext then
- begin
- Result := RCItem(I);
- exit;
- end;
- // first handle is always a dummy
- Result := FHandles[0];
- end;
- procedure TGLContextHandle.CheckCurrentRC;
- begin
- if vCurrentGLContext <> FLastHandle.FRenderingContext then
- FLastHandle := SearchRC(vCurrentGLContext);
- end;
- function TGLContextHandle.GetHandle: Cardinal;
- begin
- // CheckCurrentRC;
- // inline doesn't always work... so optimize it here
- if vCurrentGLContext <> FLastHandle.FRenderingContext then
- FLastHandle := SearchRC(vCurrentGLContext);
- Result := FLastHandle.FHandle;
- end;
- procedure TGLContextHandle.DestroyHandle;
- var
- oldContext: TGLContext;
- p: PGLRCHandle;
- I: Integer;
- begin
- oldContext := vCurrentGLContext;
- if Assigned(oldContext) then
- oldContext.Deactivate;
- try
- for I := FHandles.Count - 1 downto 1 do
- begin
- p := FHandles[I];
- if p.FHandle > 0 then
- begin
- p.FRenderingContext.Activate;
- if IsValid(p.FHandle) then
- DoDestroyHandle(p.FHandle);
- Dec(p.FRenderingContext.FOwnedHandlesCount);
- p.FRenderingContext.Deactivate;
- p.FRenderingContext := nil;
- p.FHandle := 0;
- p.FChanged := True;
- end;
- Dispose(p);
- end;
- FHandles.Count := 1; // delete all in 1 step
- FLastHandle := FHandles[0];
- finally
- if Assigned(vCurrentGLContext) then
- vCurrentGLContext.Deactivate;
- if Assigned(oldContext) then
- oldContext.Activate;
- end;
- end;
- procedure TGLContextHandle.ContextDestroying;
- var
- I: Integer;
- p: PGLRCHandle;
- aList: TList;
- bShared: Boolean;
- begin
- if Assigned(vCurrentGLContext) then
- begin
- bShared := False;
- if Transferable then
- begin
- {$IFNDEF USE_MULTITHREAD}
- aList := vCurrentGLContext.FSharedContexts;
- {$ELSE}
- aList := vCurrentGLContext.FSharedContexts.LockList;
- try
- {$ENDIF USE_MULTITHREAD}
- for I := FHandles.Count - 1 downto 1 do
- begin
- p := RCItem(I);
- if (p.FRenderingContext <> vCurrentGLContext) and (p.FHandle <> 0) and
- (aList.IndexOf(p.FRenderingContext) > -1) then
- begin
- bShared := True;
- Break;
- end;
- end;
- {$IFDEF USE_MULTITHREAD}
- finally
- vCurrentGLContext.FSharedContexts.UnlockList;
- end;
- {$ENDIF USE_MULTITHREAD}
- end;
- for I := FHandles.Count - 1 downto 1 do
- begin
- p := RCItem(I);
- if (p.FRenderingContext = vCurrentGLContext) and (p.FHandle <> 0) then
- begin
- if not bShared then
- if IsValid(p.FHandle) then
- DoDestroyHandle(p.FHandle);
- Dec(p.FRenderingContext.FOwnedHandlesCount);
- p.FHandle := 0;
- p.FRenderingContext := nil;
- p.FChanged := True;
- Dispose(p);
- FHandles.Delete(I);
- if FLastHandle = p then
- FLastHandle := FHandles[0];
- exit;
- end;
- end;
- end;
- end;
- function TGLContextHandle.GetContext: TGLContext;
- var
- I: Integer;
- p: PGLRCHandle;
- begin
- Result := nil;
- // Return first context where handle is allocated
- for I := FHandles.Count - 1 downto 1 do
- begin
- p := RCItem(I);
- if (p.FRenderingContext <> nil) and (p.FHandle <> 0) then
- begin
- Result := p.FRenderingContext;
- // If handle allocated in active context - return it
- if (Result = vCurrentGLContext) then
- exit;
- end;
- end;
- end;
- function TGLContextHandle.IsDataNeedUpdate: Boolean;
- begin
- if GetHandle = 0 then
- CheckCurrentRC;
- Result := (FLastHandle.FHandle = 0) or FLastHandle.FChanged;
- end;
- function TGLContextHandle.IsDataComplitelyUpdated: Boolean;
- var
- I: Integer;
- begin
- Result := False;
- for I := FHandles.Count - 1 downto 1 do
- begin
- with RCItem(I)^ do
- if (FRenderingContext <> nil) and (FHandle <> 0) and FChanged then
- exit;
- end;
- Result := True;
- end;
- procedure TGLContextHandle.NotifyDataUpdated;
- var
- I: Integer;
- aList: TList;
- begin
- if Assigned(vCurrentGLContext) then
- begin
- if not Transferable then
- begin
- CheckCurrentRC();
- if FLastHandle.FHandle <> 0 then
- begin
- FLastHandle.FChanged := False;
- exit;
- end;
- end
- else
- begin
- {$IFNDEF USE_MULTITHREAD}
- aList := vCurrentGLContext.FSharedContexts;
- {$ELSE}
- aList := vCurrentGLContext.FSharedContexts.LockList;
- try
- {$ENDIF}
- for I := 0 to aList.Count - 1 do
- begin
- with SearchRC(aList[I])^ do
- if (FHandle <> 0) then
- FChanged := False;
- end;
- {$IFDEF USE_MULTITHREAD}
- finally
- vCurrentGLContext.FSharedContexts.UnlockList;
- end;
- {$ENDIF}
- end;
- end
- {$IFDEF USE_LOGGING}
- else
- GLSLogger.LogError(strNoActiveRC);
- {$ENDIF}
- end;
- function TGLContextHandle.RCItem(AIndex: Integer): PGLRCHandle;
- begin
- Result := FHandles[AIndex];
- end;
- procedure TGLContextHandle.NotifyChangesOfData;
- var
- I: Integer;
- begin
- for I := FHandles.Count - 1 downto 1 do
- RCItem(I).FChanged := True;
- if Assigned(FOnPrepare) then
- GLContextManager.NotifyPreparationNeed;
- end;
- function TGLContextHandle.IsShared: Boolean;
- var
- I: Integer;
- vContext: TGLContext;
- aList: TList;
- begin
- Result := False;
- // untransferable handles can't be shared
- if not Transferable then
- exit;
- Result := True;
- {$IFNDEF USE_MULTITHREAD}
- aList := vCurrentGLContext.FSharedContexts;
- {$ELSE}
- aList := vCurrentGLContext.FSharedContexts.LockList;
- try
- {$ENDIF}
- for I := 0 to aList.Count - 1 do
- begin
- vContext := aList[I];
- if (vContext <> vCurrentGLContext) and
- // at least one context is friendly
- (SearchRC(vContext).FHandle <> 0) then
- exit;
- end;
- {$IFDEF USE_MULTITHREAD}
- finally
- vCurrentGLContext.FSharedContexts.UnlockList;
- end;
- {$ENDIF}
- Result := False;
- end;
- class function TGLContextHandle.Transferable: Boolean;
- begin
- Result := True;
- end;
- class function TGLContextHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := True;
- end;
- class function TGLContextHandle.IsSupported: Boolean;
- begin
- Result := True;
- end;
- // ------------------
- // ------------------ TGLVirtualHandle ------------------
- // ------------------
- function TGLVirtualHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- if Assigned(FOnAllocate) then
- FOnAllocate(Self, Result);
- end;
- procedure TGLVirtualHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.ClearError;
- if Assigned(FOnDestroy) then
- FOnDestroy(Self, AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLVirtualHandle.Transferable: Boolean;
- begin
- Result := False;
- end;
- // ------------------
- // TGLVirtualHandleTransf
- // ------------------
- class function TGLVirtualHandleTransf.Transferable: Boolean;
- begin
- Result := True;
- end;
- // ------------------
- // ------------------ TGLListHandle ------------------
- // ------------------
- function TGLListHandle.DoAllocateHandle: Cardinal;
- begin
- Result := gl.GenLists(1);
- end;
- procedure TGLListHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.ClearError;
- gl.DeleteLists(AHandle, 1);
- gl.CheckError;
- end;
- end;
- class function TGLListHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsList(ID);
- end;
- procedure TGLListHandle.NewList(mode: Cardinal);
- begin
- vCurrentGLContext.GLStates.NewList(GetHandle, mode);
- end;
- procedure TGLListHandle.EndList;
- begin
- vCurrentGLContext.GLStates.EndList;
- end;
- procedure TGLListHandle.CallList;
- begin
- vCurrentGLContext.GLStates.CallList(GetHandle);
- end;
- // ------------------
- // ------------------ TGLTextureHandle ------------------
- // ------------------
- function TGLTextureHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenTextures(1, @Result);
- FTarget := ttNoShape;
- end;
- procedure TGLTextureHandle.DoDestroyHandle(var AHandle: Cardinal);
- var
- a: TGLInt;
- t: TGLTextureTarget;
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- // Unbind identifier from all image selectors.
- if gl.ARB_multitexture then
- begin
- with GetContext.GLStates do
- begin
- for a := 0 to MaxTextureImageUnits - 1 do
- for t := Low(TGLTextureTarget) to High(TGLTextureTarget) do
- if TextureBinding[a, t] = AHandle then
- TextureBinding[a, t] := 0;
- end
- end
- else
- with GetContext.GLStates do
- for t := Low(TGLTextureTarget) to High(TGLTextureTarget) do
- if TextureBinding[0, t] = AHandle then
- TextureBinding[0, t] := 0;
- gl.DeleteTextures(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLTextureHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsTexture(ID);
- end;
- procedure TGLTextureHandle.SetTarget(ATarget: TGLTextureTarget);
- begin
- if FTarget = ttNoShape then
- FTarget := ATarget;
- end;
- // ------------------
- // ------------------ TGLSamplerHandle ------------------
- // ------------------
- function TGLSamplerHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenSamplers(1, @Result);
- end;
- procedure TGLSamplerHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- gl.DeleteSamplers(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLSamplerHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_sampler_objects;
- end;
- class function TGLSamplerHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsSampler(ID);
- end;
- // ------------------
- // ------------------ TGLQueryHandle ------------------
- // ------------------
- procedure TGLQueryHandle.BeginQuery;
- begin
- if vCurrentGLContext.GLStates.CurrentQuery[QueryType] = 0 then
- vCurrentGLContext.GLStates.BeginQuery(QueryType, GetHandle);
- FActive := True;
- end;
- function TGLQueryHandle.CounterBits: Integer;
- begin
- gl.GetQueryiv(Target, GL_QUERY_COUNTER_BITS, @Result);
- end;
- function TGLQueryHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenQueries(1, @Result);
- end;
- procedure TGLQueryHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- gl.DeleteQueries(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLQueryHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsQuery(ID);
- end;
- procedure TGLQueryHandle.EndQuery;
- begin
- Assert(FActive = True, 'Cannot end a query before it begins');
- FActive := False;
- Assert(Handle <> 0);
- // glEndQuery(Target);
- vCurrentGLContext.GLStates.EndQuery(QueryType);
- end;
- function TGLQueryHandle.IsResultAvailable: Boolean;
- begin
- gl.GetQueryObjectiv(Handle, GL_QUERY_RESULT_AVAILABLE, @Result);
- end;
- function TGLQueryHandle.QueryResultInt: TGLInt;
- begin
- gl.GetQueryObjectiv(Handle, GL_QUERY_RESULT, @Result);
- end;
- function TGLQueryHandle.QueryResultInt64: TGLint64EXT;
- begin
- gl.GetQueryObjecti64v(Handle, GL_QUERY_RESULT, @Result);
- end;
- function TGLQueryHandle.QueryResultUInt: Cardinal;
- begin
- gl.GetQueryObjectuiv(Handle, GL_QUERY_RESULT, @Result);
- end;
- function TGLQueryHandle.QueryResultUInt64: TGLuint64EXT;
- begin
- gl.GetQueryObjectui64v(Handle, GL_QUERY_RESULT, @Result);
- end;
- function TGLQueryHandle.QueryResultBool: TGLboolean;
- var
- I: Cardinal;
- begin
- gl.GetQueryObjectuiv(Handle, GL_QUERY_RESULT, @I);
- Result := I > 0;
- end;
- class function TGLQueryHandle.Transferable: Boolean;
- begin
- Result := False;
- end;
- // ------------------
- // ------------------ TGLOcclusionQueryHandle ------------------
- // ------------------
- function TGLOcclusionQueryHandle.GetQueryType: TGLQueryType;
- begin
- Result := qrySamplesPassed;
- end;
- function TGLOcclusionQueryHandle.GetTarget: Cardinal;
- begin
- Result := GL_SAMPLES_PASSED;
- end;
- class function TGLOcclusionQueryHandle.IsSupported: Boolean;
- begin
- Result := gl.VERSION_1_5;
- end;
- function TGLOcclusionQueryHandle.PixelCount: Integer;
- begin
- Result := QueryResultUInt;
- end;
- // ------------------
- // ------------------ TGLBooleanOcclusionQueryHandle ------------------
- // ------------------
- function TGLBooleanOcclusionQueryHandle.GetQueryType: TGLQueryType;
- begin
- Result := qryAnySamplesPassed;
- end;
- function TGLBooleanOcclusionQueryHandle.GetTarget: Cardinal;
- begin
- Result := GL_ANY_SAMPLES_PASSED;
- end;
- class function TGLBooleanOcclusionQueryHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_occlusion_query2;
- end;
- // ------------------
- // ------------------ TGLTimerQueryHandle ------------------
- // ------------------
- function TGLTimerQueryHandle.GetQueryType: TGLQueryType;
- begin
- Result := qryTimeElapsed;
- end;
- function TGLTimerQueryHandle.GetTarget: Cardinal;
- begin
- Result := GL_TIME_ELAPSED;
- end;
- class function TGLTimerQueryHandle.IsSupported: Boolean;
- begin
- Result := gl.EXT_timer_query or gl.ARB_timer_query;
- end;
- function TGLTimerQueryHandle.Time: Integer;
- begin
- Result := QueryResultUInt;
- end;
- // ------------------
- // ------------------ TGLPrimitiveQueryHandle ------------------
- // ------------------
- function TGLPrimitiveQueryHandle.GetQueryType: TGLQueryType;
- begin
- Result := qryPrimitivesGenerated;
- end;
- function TGLPrimitiveQueryHandle.GetTarget: Cardinal;
- begin
- Result := GL_PRIMITIVES_GENERATED;
- end;
- class function TGLPrimitiveQueryHandle.IsSupported: Boolean;
- begin
- Result := gl.VERSION_3_0;
- end;
- function TGLPrimitiveQueryHandle.PrimitivesGenerated: Integer;
- begin
- Result := QueryResultUInt;
- end;
- // ------------------
- // ------------------ TGLBufferObjectHandle ------------------
- // ------------------
- constructor TGLBufferObjectHandle.CreateFromData(p: Pointer; size: Integer; bufferUsage: Cardinal);
- begin
- Create;
- AllocateHandle;
- Bind;
- BufferData(p, size, bufferUsage);
- UnBind;
- end;
- function TGLBufferObjectHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenBuffers(1, @Result);
- end;
- procedure TGLBufferObjectHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- UnBind;
- gl.DeleteBuffers(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLBufferObjectHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsBuffer(ID);
- end;
- class function TGLBufferObjectHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_vertex_buffer_object;
- end;
- procedure TGLBufferObjectHandle.BindRange(index: Cardinal; offset: TGLintptr; size: TGLsizeiptr);
- begin
- Assert(False, 'BindRange only XBO and UBO');
- end;
- procedure TGLBufferObjectHandle.BindBase(index: Cardinal);
- begin
- Assert(False, 'BindRange only XBO and UBO');
- end;
- procedure TGLBufferObjectHandle.UnBindBase(index: Cardinal);
- begin
- Assert(False, 'BindRange only XBO and UBO');
- end;
- procedure TGLBufferObjectHandle.BufferData(p: Pointer; size: Integer; bufferUsage: Cardinal);
- begin
- FSize := size;
- gl.BufferData(Target, size, p, bufferUsage);
- end;
- procedure TGLBufferObjectHandle.BindBufferData(p: Pointer; size: Integer; bufferUsage: Cardinal);
- begin
- Bind;
- FSize := size;
- gl.BufferData(Target, size, p, bufferUsage);
- end;
- procedure TGLBufferObjectHandle.BufferSubData(offset, size: Integer; p: Pointer);
- begin
- Assert(offset + size <= FSize);
- gl.BufferSubData(Target, offset, size, p);
- end;
- function TGLBufferObjectHandle.MapBuffer(access: Cardinal): Pointer;
- begin
- Result := gl.MapBuffer(Target, access);
- end;
- function TGLBufferObjectHandle.MapBufferRange(offset: TGLInt; len: TGLsizei; access: TGLbitfield): Pointer;
- begin
- Result := gl.MapBufferRange(Target, offset, len, access);
- end;
- procedure TGLBufferObjectHandle.Flush(offset: TGLInt; len: TGLsizei);
- begin
- gl.FlushMappedBufferRange(Target, offset, len);
- end;
- function TGLBufferObjectHandle.UnmapBuffer: Boolean;
- begin
- Result := gl.UnmapBuffer(Target);
- end;
- // ------------------
- // ------------------ TGLVBOHandle ------------------
- // ------------------
- function TGLVBOHandle.GetVBOTarget: Cardinal;
- begin
- Result := Target;
- end;
- // ------------------
- // ------------------ TGLVBOArrayBufferHandle ------------------
- // ------------------
- procedure TGLVBOArrayBufferHandle.Bind;
- begin
- vCurrentGLContext.GLStates.ArrayBufferBinding := Handle;
- end;
- procedure TGLVBOArrayBufferHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.ArrayBufferBinding := 0;
- end;
- function TGLVBOArrayBufferHandle.GetTarget: Cardinal;
- begin
- Result := GL_ARRAY_BUFFER;
- end;
- // ------------------
- // ------------------ TGLVBOElementArrayHandle ------------------
- // ------------------
- procedure TGLVBOElementArrayHandle.Bind;
- begin
- vCurrentGLContext.GLStates.ElementBufferBinding := Handle;
- end;
- procedure TGLVBOElementArrayHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.ElementBufferBinding := 0;
- end;
- function TGLVBOElementArrayHandle.GetTarget: TGLuint;
- begin
- Result := GL_ELEMENT_ARRAY_BUFFER;
- end;
- // ------------------
- // ------------------ TGLPackPBOHandle ------------------
- // ------------------
- procedure TGLPackPBOHandle.Bind;
- begin
- vCurrentGLContext.GLStates.PixelPackBufferBinding := Handle;
- end;
- procedure TGLPackPBOHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.PixelPackBufferBinding := 0;
- end;
- function TGLPackPBOHandle.GetTarget: TGLuint;
- begin
- Result := GL_PIXEL_PACK_BUFFER;
- end;
- class function TGLPackPBOHandle.IsSupported: Boolean;
- begin
- Result := GL.ARB_pixel_buffer_object;
- end;
- // ------------------
- // ------------------ TGLUnpackPBOHandle ------------------
- // ------------------
- procedure TGLUnpackPBOHandle.Bind;
- begin
- vCurrentGLContext.GLStates.PixelUnpackBufferBinding := Handle;
- end;
- procedure TGLUnpackPBOHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.PixelUnpackBufferBinding := 0;
- end;
- function TGLUnpackPBOHandle.GetTarget: Cardinal;
- begin
- Result := GL_PIXEL_UNPACK_BUFFER;
- end;
- class function TGLUnpackPBOHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_pixel_buffer_object;
- end;
- // ------------------
- // ------------------ TGLTransformFeedbackBufferHandle ------------------
- // ------------------
- procedure TGLTransformFeedbackBufferHandle.Bind;
- begin
- vCurrentGLContext.GLStates.TransformFeedbackBufferBinding := Handle;
- end;
- procedure TGLTransformFeedbackBufferHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.TransformFeedbackBufferBinding := 0;
- end;
- function TGLTransformFeedbackBufferHandle.GetTarget: TGLuint;
- begin
- Result := GL_TRANSFORM_FEEDBACK_BUFFER;
- end;
- procedure TGLTransformFeedbackBufferHandle.BeginTransformFeedback
- (primitiveMode: TGLuint);
- begin
- gl.BeginTransformFeedback(primitiveMode);
- end;
- procedure TGLTransformFeedbackBufferHandle.EndTransformFeedback();
- begin
- gl.EndTransformFeedback();
- end;
- procedure TGLTransformFeedbackBufferHandle.BindRange(index: Cardinal; offset: TGLintptr; size: TGLsizeiptr);
- begin
- vCurrentGLContext.GLStates.SetBufferIndexedBinding(Handle, bbtTransformFeedBack, index, offset, size);
- end;
- procedure TGLTransformFeedbackBufferHandle.BindBase(index: Cardinal);
- begin
- vCurrentGLContext.GLStates.SetBufferIndexedBinding(Handle, bbtTransformFeedBack, index, BufferSize);
- end;
- procedure TGLTransformFeedbackBufferHandle.UnBindBase(index: Cardinal);
- begin
- vCurrentGLContext.GLStates.SetBufferIndexedBinding(0, bbtTransformFeedBack, index, 0);
- end;
- class function TGLTransformFeedbackBufferHandle.IsSupported: Boolean;
- begin
- Result := GL.EXT_transform_feedback;
- end;
- // ------------------
- // ------------------ TGLTextureBufferHandle ------------------
- // ------------------
- procedure TGLTextureBufferHandle.Bind;
- begin
- vCurrentGLContext.GLStates.TextureBufferBinding := Handle;
- end;
- procedure TGLTextureBufferHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.TextureBufferBinding := 0;
- end;
- function TGLTextureBufferHandle.GetTarget: Cardinal;
- begin
- Result := GL_TEXTURE_BUFFER;
- end;
- class function TGLTextureBufferHandle.IsSupported: Boolean;
- begin
- Result := gl.EXT_texture_buffer_object or gl.ARB_texture_buffer_object or gl.VERSION_3_1;
- end;
- // ------------------
- // ------------------ TGLUniformBufferHandle ------------------
- // ------------------
- procedure TGLUniformBufferHandle.Bind;
- begin
- vCurrentGLContext.GLStates.UniformBufferBinding := Handle;
- end;
- procedure TGLUniformBufferHandle.UnBind;
- begin
- vCurrentGLContext.GLStates.UniformBufferBinding := 0;
- end;
- procedure TGLUniformBufferHandle.BindRange(index: Cardinal; offset: TGLintptr; size: TGLsizeiptr);
- begin
- vCurrentGLContext.GLStates.SetBufferIndexedBinding(Handle, bbtUniform, index, offset, size);
- end;
- procedure TGLUniformBufferHandle.BindBase(index: Cardinal);
- begin
- vCurrentGLContext.GLStates.SetBufferIndexedBinding(Handle, bbtUniform, index, BufferSize);
- end;
- procedure TGLUniformBufferHandle.UnBindBase(index: Cardinal);
- begin
- vCurrentGLContext.GLStates.SetBufferIndexedBinding(0, bbtUniform, index, 0);
- end;
- function TGLUniformBufferHandle.GetTarget: Cardinal;
- begin
- Result := GL_UNIFORM_BUFFER;
- end;
- class function TGLUniformBufferHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_uniform_buffer_object;
- end;
- // ------------------
- // ------------------ TGLVertexArrayHandle ------------------
- // ------------------
- function TGLVertexArrayHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenVertexArrays(1, @Result);
- end;
- procedure TGLVertexArrayHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- gl.DeleteVertexArrays(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLVertexArrayHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsVertexArray(ID);
- end;
- procedure TGLVertexArrayHandle.Bind;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.VertexArrayBinding := Handle;
- end;
- procedure TGLVertexArrayHandle.UnBind;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.VertexArrayBinding := 0;
- end;
- class function TGLVertexArrayHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_vertex_array_object;
- end;
- class function TGLVertexArrayHandle.Transferable: Boolean;
- begin
- Result := False;
- end;
- // ------------------
- // ------------------ TGLFramebufferHandle ------------------
- // ------------------
- function TGLFramebufferHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenFramebuffers(1, @Result)
- end;
- procedure TGLFramebufferHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- gl.DeleteFramebuffers(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLFramebufferHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsFramebuffer(ID);
- end;
- procedure TGLFramebufferHandle.Bind;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.SetFrameBuffer(Handle);
- end;
- procedure TGLFramebufferHandle.BindForDrawing;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.DrawFrameBuffer := Handle;
- end;
- procedure TGLFramebufferHandle.BindForReading;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.ReadFrameBuffer := Handle;
- end;
- procedure TGLFramebufferHandle.UnBind;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.SetFrameBuffer(0);
- end;
- procedure TGLFramebufferHandle.UnBindForDrawing;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.DrawFrameBuffer := 0;
- end;
- procedure TGLFramebufferHandle.UnBindForReading;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.ReadFrameBuffer := 0;
- end;
- procedure TGLFramebufferHandle.Attach1DTexture(Target: Cardinal; attachment: Cardinal; textarget: Cardinal; texture: Cardinal;
- level: TGLInt);
- begin
- gl.FramebufferTexture1D(Target, attachment, textarget, texture, level);
- end;
- procedure TGLFramebufferHandle.Attach2DTexture(Target: Cardinal; attachment: Cardinal; textarget: Cardinal; texture: Cardinal;
- level: TGLInt);
- begin
- gl.FramebufferTexture2D(Target, attachment, textarget, texture, level);
- end;
- procedure TGLFramebufferHandle.Attach3DTexture(Target: Cardinal; attachment: Cardinal; textarget: Cardinal; texture: Cardinal;
- level: TGLInt; Layer: TGLInt);
- begin
- gl.FramebufferTexture3D(Target, attachment, textarget, texture, level, Layer);
- end;
- procedure TGLFramebufferHandle.AttachLayer(Target: Cardinal; attachment: Cardinal; texture: Cardinal; level: TGLInt;
- Layer: TGLInt);
- begin
- gl.FramebufferTextureLayer(Target, attachment, texture, level, Layer);
- end;
- procedure TGLFramebufferHandle.AttachRenderBuffer(Target: Cardinal; attachment: Cardinal; renderbuffertarget: Cardinal;
- renderbuffer: Cardinal);
- begin
- gl.FramebufferRenderbuffer(Target, attachment, renderbuffertarget, renderbuffer);
- end;
- procedure TGLFramebufferHandle.AttachTexture(Target: Cardinal; attachment: Cardinal; texture: Cardinal; level: TGLInt);
- begin
- gl.FramebufferTexture(Target, attachment, texture, level);
- end;
- procedure TGLFramebufferHandle.AttachTextureLayer(Target: Cardinal; attachment: Cardinal; texture: Cardinal; level: TGLInt;
- Layer: TGLInt);
- begin
- gl.FramebufferTextureLayer(Target, attachment, texture, level, Layer);
- end;
- procedure TGLFramebufferHandle.Blit(srcX0: TGLInt; srcY0: TGLInt; srcX1: TGLInt; srcY1: TGLInt; dstX0: TGLInt; dstY0: TGLInt;
- dstX1: TGLInt; dstY1: TGLInt; mask: TGLbitfield; filter: Cardinal);
- begin
- gl.BlitFramebuffer(srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
- end;
- function TGLFramebufferHandle.GetAttachmentParameter(Target: Cardinal; attachment: Cardinal; pname: Cardinal): TGLInt;
- begin
- gl.GetFramebufferAttachmentParameteriv(Target, attachment, pname, @Result)
- end;
- function TGLFramebufferHandle.GetAttachmentObjectType(Target: Cardinal; attachment: Cardinal): TGLInt;
- begin
- gl.GetFramebufferAttachmentParameteriv(Target, attachment, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, @Result);
- end;
- function TGLFramebufferHandle.GetAttachmentObjectName(Target: Cardinal; attachment: Cardinal): TGLInt;
- begin
- gl.GetFramebufferAttachmentParameteriv(Target, attachment, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, @Result);
- end;
- function TGLFramebufferHandle.GetStatus: TGLFramebufferStatus;
- var
- Status: TGLuint;
- begin
- Status := gl.CheckFramebufferStatus(GL_FRAMEBUFFER);
- case Status of
- GL_FRAMEBUFFER_COMPLETE_EXT:
- Result := fsComplete;
- GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
- Result := fsIncompleteAttachment;
- GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
- Result := fsIncompleteMissingAttachment;
- GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
- Result := fsIncompleteDuplicateAttachment;
- GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
- Result := fsIncompleteDimensions;
- GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
- Result := fsIncompleteFormats;
- GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
- Result := fsIncompleteDrawBuffer;
- GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
- Result := fsIncompleteReadBuffer;
- GL_FRAMEBUFFER_UNSUPPORTED_EXT:
- Result := fsUnsupported;
- GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
- Result := fsIncompleteMultisample;
- else
- Result := fsStatusError;
- end;
- end;
- function TGLFramebufferHandle.GetStringStatus(out clarification: string): TGLFramebufferStatus;
- const
- cFBOStatus: array [TGLFramebufferStatus] of string = ('Complete', 'Incomplete attachment', 'Incomplete missing attachment',
- 'Incomplete duplicate attachment', 'Incomplete dimensions', 'Incomplete formats', 'Incomplete draw buffer',
- 'Incomplete read buffer', 'Unsupported', 'Incomplite multisample', 'Status Error');
- begin
- Result := GetStatus;
- clarification := cFBOStatus[Result];
- end;
- class function TGLFramebufferHandle.IsSupported: Boolean;
- begin
- Result := gl.EXT_framebuffer_object or gl.ARB_framebuffer_object;
- end;
- class function TGLFramebufferHandle.Transferable: Boolean;
- begin
- Result := False;
- end;
- // ------------------
- // ------------------ TGLRenderbufferObject ------------------
- // ------------------
- function TGLRenderbufferHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenRenderbuffers(1, @Result);
- end;
- procedure TGLRenderbufferHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- gl.DeleteRenderbuffers(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLRenderbufferHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsRenderbuffer(ID);
- end;
- procedure TGLRenderbufferHandle.Bind;
- begin
- vCurrentGLContext.GLStates.renderbuffer := GetHandle;
- end;
- procedure TGLRenderbufferHandle.UnBind;
- begin
- if vCurrentGLContext <> nil then
- vCurrentGLContext.GLStates.renderbuffer := 0;
- end;
- procedure TGLRenderbufferHandle.SetStorage(internalformat: Cardinal; width, height: TGLsizei);
- begin
- gl.RenderbufferStorage(GL_RENDERBUFFER, internalformat, width, height);
- end;
- procedure TGLRenderbufferHandle.SetStorageMultisample(internalformat: Cardinal; samples: TGLsizei; width, height: TGLsizei);
- begin
- gl.RenderbufferStorageMultisample(GL_RENDERBUFFER, samples, internalformat, width, height);
- end;
- class function TGLRenderbufferHandle.IsSupported: Boolean;
- begin
- Result := gl.EXT_framebuffer_object or gl.ARB_framebuffer_object;
- end;
- // ------------------
- // ------------------ TGLARBProgramHandle ------------------
- // ------------------
- function TGLARBProgramHandle.DoAllocateHandle: Cardinal;
- begin
- Result := 0;
- gl.GenPrograms(1, @Result);
- FReady := False;
- end;
- procedure TGLARBProgramHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.GetError;
- gl.DeletePrograms(1, @AHandle);
- gl.CheckError;
- end;
- end;
- class function TGLARBProgramHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsProgram(ID);
- end;
- procedure TGLARBProgramHandle.LoadARBProgram(const AText: string);
- const
- cProgType: array [0 .. 2] of string = ('ARB vertex', 'ARB fragment', 'NV geometry');
- var
- errPos, p: Integer;
- begin
- Bind;
- gl.ProgramString(GetTarget, GL_PROGRAM_FORMAT_ASCII_ARB, Length(AText), PAnsiChar(AnsiString(AText)));
- gl.GetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, @errPos);
- if errPos > -1 then
- begin
- FInfoLog := string(gl.GetString(GL_PROGRAM_ERROR_STRING_ARB));
- case GetTarget of
- GL_VERTEX_PROGRAM_ARB:
- p := 0;
- GL_FRAGMENT_PROGRAM_ARB:
- p := 1;
- else
- p := 2;
- end;
- GLSLogger.LogError(Format('%s Program Error - [Pos: %d][Error %s]', [cProgType[p], errPos, FInfoLog]));
- FReady := False;
- end
- else
- begin
- FReady := True;
- FInfoLog := '';
- end;
- end;
- procedure TGLARBProgramHandle.Enable;
- begin
- if FReady then
- gl.Enable(GetTarget)
- else
- Abort;
- end;
- procedure TGLARBProgramHandle.Disable;
- begin
- gl.Disable(GetTarget);
- end;
- procedure TGLARBProgramHandle.Bind;
- begin
- gl.BindProgram(GetTarget, Handle);
- end;
- class function TGLARBVertexProgramHandle.GetTarget: Cardinal;
- begin
- Result := GL_VERTEX_PROGRAM_ARB;
- end;
- class function TGLARBVertexProgramHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_vertex_program;
- end;
- class function TGLARBFragmentProgramHandle.GetTarget: Cardinal;
- begin
- Result := GL_FRAGMENT_PROGRAM_ARB;
- end;
- class function TGLARBFragmentProgramHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_vertex_program;
- end;
- class function TGLARBGeometryProgramHandle.GetTarget: Cardinal;
- begin
- Result := GL_GEOMETRY_PROGRAM_NV;
- end;
- class function TGLARBGeometryProgramHandle.IsSupported: Boolean;
- begin
- Result := gl.NV_geometry_program4;
- end;
- // ------------------
- // ------------------ TGLSLHandle ------------------
- // ------------------
- procedure TGLSLHandle.DoDestroyHandle(var AHandle: Cardinal);
- begin
- if not vContextActivationFailureOccurred then
- begin
- gl.ClearError;
- gl.DeleteObject(AHandle);
- gl.CheckError;
- end;
- end;
- function TGLSLHandle.InfoLog: string;
- var
- maxLength: Integer;
- log: AnsiString;
- begin
- maxLength := 0;
- gl.GetObjectParameteriv(GetHandle, GL_OBJECT_INFO_LOG_LENGTH_ARB, @maxLength);
- SetLength(log, maxLength);
- if maxLength > 0 then
- begin
- gl.GetInfoLog(GetHandle, maxLength, @maxLength, @log[1]);
- SetLength(log, maxLength);
- end;
- Result := string(log);
- end;
- class function TGLSLHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_shader_objects;
- end;
- // ------------------
- // ------------------ TGLShaderHandle ------------------
- // ------------------
- function TGLShaderHandle.DoAllocateHandle: Cardinal;
- begin
- Result := gl.CreateShader(FShaderType)
- end;
- class function TGLShaderHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsShader(ID);
- end;
- procedure TGLShaderHandle.ShaderSource(const source: AnsiString);
- var
- p: PAnsiChar;
- begin
- p := PAnsiChar(AnsiString(source));
- gl.ShaderSource(GetHandle, 1, @p, nil);
- end;
- function TGLShaderHandle.CompileShader: Boolean;
- var
- compiled: Integer;
- glH: Cardinal;
- begin
- glH := GetHandle;
- gl.CompileShader(glH);
- compiled := 0;
- gl.GetShaderiv(glH, GL_COMPILE_STATUS, @compiled);
- Result := (compiled <> 0);
- end;
- // ------------------
- // ------------------ TGLVertexShaderHandle ------------------
- // ------------------
- constructor TGLVertexShaderHandle.Create;
- begin
- FShaderType := GL_VERTEX_SHADER_ARB;
- inherited;
- end;
- class function TGLVertexShaderHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_vertex_shader;
- end;
- // ------------------
- // ------------------ TGLGeometryShaderHandle ------------------
- // ------------------
- constructor TGLGeometryShaderHandle.Create;
- begin
- FShaderType := GL_GEOMETRY_SHADER_EXT;
- inherited;
- end;
- class function TGLGeometryShaderHandle.IsSupported: Boolean;
- begin
- Result := gl.EXT_geometry_shader4;
- end;
- // ------------------
- // ------------------ TGLFragmentShaderHandle ------------------
- // ------------------
- constructor TGLFragmentShaderHandle.Create;
- begin
- FShaderType := GL_FRAGMENT_SHADER_ARB;
- inherited;
- end;
- class function TGLFragmentShaderHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_fragment_shader;
- end;
- // ------------------
- // ------------------ TGLTessControlShaderHandle ------------------
- // ------------------
- constructor TGLTessControlShaderHandle.Create;
- begin
- FShaderType := GL_TESS_CONTROL_SHADER;
- inherited;
- end;
- class function TGLTessControlShaderHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_tessellation_shader;
- end;
- // ------------------
- // ------------------ TGLTessEvaluationShaderHandle ------------------
- // ------------------
- constructor TGLTessEvaluationShaderHandle.Create;
- begin
- FShaderType := GL_TESS_EVALUATION_SHADER;
- inherited;
- end;
- class function TGLTessEvaluationShaderHandle.IsSupported: Boolean;
- begin
- Result := gl.ARB_tessellation_shader;
- end;
- // ------------------
- // ------------------ TGLProgramHandle ------------------
- // ------------------
- function TGLProgramHandle.DoAllocateHandle: Cardinal;
- begin
- Result := gl.CreateProgram();
- end;
- class function TGLProgramHandle.IsValid(const ID: Cardinal): Boolean;
- begin
- Result := gl.IsProgram(ID);
- end;
- procedure TGLProgramHandle.AddShader(ShaderType: TGLShaderHandleClass; const ShaderSource: string;
- treatWarningsAsErrors: Boolean = False);
- var
- shader: TGLShaderHandle;
- begin
- shader := ShaderType.CreateAndAllocate;
- try
- if shader.Handle = 0 then
- raise EGLShader.Create('Couldn''t allocate ' + ShaderType.ClassName);
- shader.ShaderSource(AnsiString(ShaderSource));
- if (not shader.CompileShader) or (treatWarningsAsErrors and (Pos('warning', LowerCase(shader.InfoLog)) > 0)) then
- raise EGLShader.Create(FName + ' (' + shader.ClassName + '): '#13#10 + shader.InfoLog);
- AttachObject(shader);
- finally
- shader.Free;
- end;
- gl.CheckError;
- end;
- procedure TGLProgramHandle.AttachObject(shader: TGLShaderHandle);
- begin
- gl.AttachShader(GetHandle, shader.Handle);
- end;
- procedure TGLProgramHandle.DetachAllObject;
- var
- glH: Cardinal;
- I: Integer;
- Count: TGLsizei;
- buffer: array [0 .. 255] of Cardinal;
- begin
- glH := GetHandle;
- if glH > 0 then
- begin
- gl.GetAttachedShaders(glH, Length(buffer), @Count, @buffer[0]);
- Count := MinInteger(Count, Length(buffer));
- for I := 0 to Count - 1 do
- gl.DetachShader(glH, buffer[I]);
- NotifyChangesOfData;
- end;
- end;
- procedure TGLProgramHandle.BindAttribLocation(index: Integer; const aName: string);
- begin
- gl.BindAttribLocation(GetHandle, index, PAnsiChar(AnsiString(aName)));
- end;
- procedure TGLProgramHandle.BindFragDataLocation(index: Integer; const aName: string);
- begin
- gl.BindFragDataLocation(GetHandle, index, PAnsiChar(AnsiString(name)));
- end;
- function TGLProgramHandle.LinkProgram: Boolean;
- var
- Status: Integer;
- glH: Cardinal;
- begin
- glH := GetHandle;
- gl.LinkProgram(glH);
- Status := 0;
- gl.GetProgramiv(glH, GL_LINK_STATUS, @Status);
- Result := (Status <> 0);
- end;
- function TGLProgramHandle.ValidateProgram: Boolean;
- var
- validated: Integer;
- h: Cardinal;
- begin
- h := GetHandle;
- gl.ValidateProgram(h);
- validated := 0;
- gl.GetProgramiv(h, GL_VALIDATE_STATUS, @validated);
- Result := (validated <> 0);
- end;
- function TGLProgramHandle.GetAttribLocation(const aName: string): Integer;
- begin
- Result := gl.GetAttribLocation(GetHandle, PAnsiChar(AnsiString(aName)));
- Assert(Result >= 0, Format(strUnknownParam, ['attrib', aName, Name]));
- end;
- function TGLProgramHandle.GetUniformLocation(const aName: string): Integer;
- begin
- Result := gl.GetUniformLocation(GetHandle, PAnsiChar(AnsiString(aName)));
- Assert(Result >= 0, Format(strUnknownParam, ['uniform', aName, Name]));
- end;
- function TGLProgramHandle.GetVaryingLocation(const aName: string): Integer;
- begin
- Result := gl.GetVaryingLocation(GetHandle, PAnsiChar(AnsiString(aName)));
- Assert(Result >= 0, Format(strUnknownParam, ['varying', aName, Name]));
- end;
- procedure TGLProgramHandle.AddActiveVarying(const aName: string);
- begin
- gl.ActiveVarying(GetHandle, PAnsiChar(AnsiString(aName)));
- end;
- procedure TGLProgramHandle.UseProgramObject;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.CurrentProgram := Handle;
- end;
- procedure TGLProgramHandle.EndUseProgramObject;
- begin
- Assert(vCurrentGLContext <> nil);
- vCurrentGLContext.GLStates.CurrentProgram := 0;
- end;
- function TGLProgramHandle.GetUniform1i(const index: string): Integer;
- begin
- gl.GetUniformiv(GetHandle, GetUniformLocation(index), @Result);
- end;
- function TGLProgramHandle.GetUniform2i(const index: string): TVector2i;
- begin
- gl.GetUniformiv(GetHandle, GetUniformLocation(index), @Result);
- end;
- function TGLProgramHandle.GetUniform3i(const index: string): TVector3i;
- begin
- gl.GetUniformiv(GetHandle, GetUniformLocation(index), @Result);
- end;
- function TGLProgramHandle.GetUniform4i(const index: string): TVector4i;
- begin
- gl.GetUniformiv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniform1f(const index: string; val: Single);
- begin
- gl.Uniform1f(GetUniformLocation(index), val);
- end;
- function TGLProgramHandle.GetUniform1f(const index: string): Single;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniform1i(const index: string; val: Integer);
- begin
- gl.Uniform1i(GetUniformLocation(index), val);
- end;
- procedure TGLProgramHandle.SetUniform2i(const index: string; const Value: TVector2i);
- begin
- gl.Uniform2i(GetUniformLocation(index), Value.X, Value.Y);
- end;
- procedure TGLProgramHandle.SetUniform3i(const index: string; const Value: TVector3i);
- begin
- gl.Uniform3i(GetUniformLocation(index), Value.X, Value.Y, Value.Z);
- end;
- procedure TGLProgramHandle.SetUniform4i(const index: string; const Value: TVector4i);
- begin
- gl.Uniform4i(GetUniformLocation(index), Value.X, Value.Y, Value.Z, Value.W);
- end;
- function TGLProgramHandle.GetUniform2f(const index: string): TVector2f;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniform2f(const index: string; const val: TVector2f);
- begin
- gl.Uniform2f(GetUniformLocation(index), val.X, val.Y);
- end;
- function TGLProgramHandle.GetUniform3f(const index: string): TAffineVector;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniform3f(const index: string; const val: TAffineVector);
- begin
- gl.Uniform3f(GetUniformLocation(index), val.X, val.Y, val.Z);
- end;
- function TGLProgramHandle.GetUniform4f(const index: string): TGLVector;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniform4f(const index: string; const val: TGLVector);
- begin
- gl.Uniform4f(GetUniformLocation(index), val.X, val.Y, val.Z, val.W);
- end;
- function TGLProgramHandle.GetUniformMatrix2fv(const index: string): TMatrix2f;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniformMatrix2fv(const index: string; const val: TMatrix2f);
- begin
- gl.UniformMatrix2fv(GetUniformLocation(index), 1, False, @val);
- end;
- function TGLProgramHandle.GetUniformMatrix3fv(const index: string): TMatrix3f;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniformMatrix3fv(const index: string; const val: TMatrix3f);
- begin
- gl.UniformMatrix3fv(GetUniformLocation(index), 1, False, @val);
- end;
- function TGLProgramHandle.GetUniformMatrix4fv(const index: string): TGLMatrix;
- begin
- gl.GetUniformfv(GetHandle, GetUniformLocation(index), @Result);
- end;
- procedure TGLProgramHandle.SetUniformMatrix4fv(const index: string; const val: TGLMatrix);
- begin
- gl.UniformMatrix4fv(GetUniformLocation(index), 1, False, @val);
- end;
- procedure TGLProgramHandle.SetUniformf(const index: string; const val: Single);
- begin
- SetUniform1f(index, val);
- end;
- procedure TGLProgramHandle.SetUniformf(const index: string; const val: TVector2f);
- begin
- SetUniform2f(index, val);
- end;
- procedure TGLProgramHandle.SetUniformf(const index: string; const val: TVector3f);
- begin
- SetUniform3f(index, val);
- end;
- procedure TGLProgramHandle.SetUniformf(const index: string; const val: TVector4f);
- begin
- SetUniform4f(index, val);
- end;
- procedure TGLProgramHandle.SetUniformi(const index: string; const val: Integer);
- begin
- SetUniform1f(index, val);
- end;
- procedure TGLProgramHandle.SetUniformi(const index: string; const val: TVector2i);
- begin
- SetUniform2i(index, val);
- end;
- procedure TGLProgramHandle.SetUniformi(const index: string; const val: TVector3i);
- begin
- SetUniform3i(index, val);
- end;
- procedure TGLProgramHandle.SetUniformi(const index: string; const val: TVector4i);
- begin
- SetUniform4i(index, val);
- end;
- function TGLProgramHandle.GetUniformTextureHandle(const index: string; const TextureIndex: Integer;
- const TextureTarget: TGLTextureTarget): Cardinal;
- begin
- Result := GetUniform1i(index);
- end;
- procedure TGLProgramHandle.SetUniformTextureHandle(const index: string; const TextureIndex: Integer;
- const TextureTarget: TGLTextureTarget; const Value: Cardinal);
- begin
- vCurrentGLContext.GLStates.TextureBinding[0, TextureTarget] := Value;
- SetUniform1i(index, TextureIndex);
- end;
- procedure TGLProgramHandle.SetUniformBuffer(const index: string; Value: TGLUniformBufferHandle);
- begin
- gl.UniformBuffer(Handle, GetUniformLocation(index), Value.Handle);
- end;
- function TGLProgramHandle.GetUniformBufferSize(const aName: string): Integer;
- begin
- Result := gl.GetUniformBufferSize(Handle, GetUniformLocation(aName));
- end;
- function TGLProgramHandle.GetUniformOffset(const aName: string): PGLInt;
- begin
- Result := gl.GetUniformOffset(Handle, GetUniformLocation(aName));
- end;
- function TGLProgramHandle.GetUniformBlockIndex(const aName: string): Integer;
- begin
- Result := gl.GetUniformBlockIndex(Handle, PAnsiChar(AnsiString(aName)));
- Assert(Result >= 0, Format(strUnknownParam, ['uniform block', aName, Name]));
- end;
- constructor TGLProgramHandle.Create;
- begin
- inherited Create;
- FName := 'DefaultShaderName';
- end;
- // ------------------
- // ------------------ TGLContextManager ------------------
- // ------------------
- {$IFDEF USE_SERVICE_CONTEXT}
- procedure OnApplicationInitialize;
- begin
- InitProc := OldInitProc;
- Application.Initialize;
- GLContextManager.CreateServiceContext;
- end;
- {$ENDIF}
- constructor TGLContextManager.Create;
- begin
- inherited Create;
- {$IFNDEF USE_MULTITHREAD}
- FHandles := TList.Create;
- {$ELSE}
- FHandles := TThreadList.Create;
- {$ENDIF USE_MULTITHREAD}
- FList := TThreadList.Create;
- end;
- destructor TGLContextManager.Destroy;
- begin
- FHandles.Free;
- FList.Free;
- inherited Destroy;
- end;
- function TGLContextManager.CreateContext(AClass: TGLContextClass): TGLContext;
- begin
- if Assigned(AClass) then
- begin
- Result := AClass.Create;
- Result.FManager := Self;
- end
- else if Assigned(vContextClasses) and (vContextClasses.Count > 0) then
- begin
- Result := TGLContextClass(vContextClasses.Last).Create;
- Result.FManager := Self;
- end
- else
- Result := nil;
- end;
- {$IFDEF USE_SERVICE_CONTEXT}
- procedure TGLContextManager.CreateServiceContext;
- begin
- FServiceContext := CreateContext;
- FThreadTask := TServiceContextTaskList.Create;
- FServiceStarter := TFinishTaskEvent.Create;
- FThread := TServiceContextThread.Create;
- AddTaskForServiceContext(TServiceContextThread(FThread).DoCreateServiceContext);
- end;
- procedure TGLContextManager.QueueTaskDepleted;
- var
- TaskRec: TServiceContextTask;
- I: Integer;
- nowTime: Double;
- begin
- with FThreadTask.LockList do
- try
- for I := 0 to Count - 1 do
- begin
- TaskRec := Items[I];
- if Assigned(TaskRec.Task) then
- begin
- FThreadTask.UnlockList;
- // Task queue not empty
- FServiceStarter.SetEvent;
- exit;
- end;
- end;
- finally
- FThreadTask.UnlockList;
- end;
- FServiceStarter.ResetEvent;
- FThreadTask.Clear;
- nowTime := Now;
- with TServiceContextThread(FThread) do
- if (nowTime - FLastTaskStartTime > 30000) and not FReported then
- begin
- FReported := True;
- GLSLogger.LogInfo('Service context queue task depleted');
- end;
- end;
- {$ENDIF USE_SERVICE_CONTEXT}
- procedure TGLContextManager.Lock;
- begin
- FList.LockList;
- end;
- procedure TGLContextManager.NotifyPreparationNeed;
- var
- I: Integer;
- LList: TList;
- begin
- LList := FList.LockList;
- try
- for I := LList.Count - 1 downto 0 do
- TGLContext(LList[I]).FIsPraparationNeed := True;
- finally
- FList.UnlockList;
- end;
- end;
- procedure TGLContextManager.UnLock;
- begin
- FList.UnlockList;
- end;
- function TGLContextManager.ContextCount: Integer;
- begin
- // try..finally just a waste of CPU here, if Count fails, the list is amok,
- // and so is the lock...
- Result := FList.LockList.Count;
- FList.UnlockList;
- end;
- procedure TGLContextManager.RegisterContext(aContext: TGLContext);
- begin
- with FList.LockList do
- try
- if IndexOf(aContext) >= 0 then
- raise EGLContext.Create(strInvalidContextRegistration)
- else
- Add(aContext);
- finally
- FList.UnlockList;
- end;
- end;
- procedure TGLContextManager.UnRegisterContext(aContext: TGLContext);
- begin
- with FList.LockList do
- try
- if IndexOf(aContext) < 0 then
- raise EGLContext.Create(strInvalidContextRegistration)
- else
- Remove(aContext);
- finally
- FList.UnlockList;
- end;
- end;
- procedure TGLContextManager.ContextCreatedBy(aContext: TGLContext);
- begin
- Lock;
- try
- Inc(FCreatedRCCount);
- finally
- UnLock;
- end;
- end;
- procedure TGLContextManager.DestroyingContextBy(aContext: TGLContext);
- var
- cn: TGLContextNotification;
- begin
- Lock;
- try
- Dec(FCreatedRCCount);
- if FCreatedRCCount = 0 then
- begin
- // yes, slow and bulky, but allows for the triggered event to
- // cascade-remove notifications safely
- while Length(FNotifications) > 0 do
- begin
- cn := FNotifications[High(FNotifications)];
- SetLength(FNotifications, Length(FNotifications) - 1);
- cn.Event(cn.obj);
- end;
- end;
- finally
- UnLock;
- end;
- end;
- procedure TGLContextManager.LastContextDestroyNotification(anObject: TObject; anEvent: TNotifyEvent);
- begin
- Lock;
- try
- SetLength(FNotifications, Length(FNotifications) + 1);
- with FNotifications[High(FNotifications)] do
- begin
- obj := anObject;
- Event := anEvent;
- end;
- finally
- UnLock;
- end;
- end;
- procedure TGLContextManager.RemoveNotification(anObject: TObject);
- var
- I: Integer;
- found: Boolean;
- begin
- Lock;
- try
- found := False;
- I := Low(FNotifications);
- while I <= High(FNotifications) do
- begin
- if FNotifications[I].obj = anObject then
- begin
- found := True;
- while I <= High(FNotifications) do
- begin
- FNotifications[I] := FNotifications[I + 1];
- Inc(I);
- end;
- SetLength(FNotifications, Length(FNotifications) - 1);
- Break;
- end;
- Inc(I);
- end;
- if not found then
- raise EGLContext.Create(strInvalidNotificationRemoval);
- finally
- UnLock;
- end;
- end;
- procedure TGLContextManager.Terminate;
- begin
- FTerminated := True;
- {$IFDEF USE_SERVICE_CONTEXT}
- // Sevice context may not be created becouse Application.Initialize not happened
- if Assigned(FServiceContext) then
- begin
- CheckSynchronize;
- FThread.Terminate;
- FServiceStarter.SetEvent;
- FThread.WaitFor;
- FThread.Destroy;
- GLSLogger.LogDebug('Service thread destroyed');
- FServiceStarter.Destroy;
- FThreadTask.Destroy;
- end;
- {$ENDIF}
- if ContextCount = 0 then
- begin
- GLContextManager := nil;
- Free;
- end;
- end;
- procedure TGLContextManager.DestroyAllHandles;
- var
- I: Integer;
- begin
- with FList.LockList do
- try
- for I := Count - 1 downto 0 do
- TGLContext(Items[I]).DestroyAllHandles;
- finally
- FList.UnlockList;
- end;
- end;
- {$IFDEF USE_SERVICE_CONTEXT}
- constructor TServiceContextThread.Create;
- begin
- FWindow := TForm.CreateNew(nil);
- FWindow.Hide;
- FWindow.Position := poScreenCenter;
- FWindow.width := 1;
- FWindow.height := 1;
- FWindow.BorderStyle := bsNone;
- FWindow.FormStyle := fsStayOnTop;
- FWindow.Color := 0;
- vServiceWindow := FWindow;
- {$IFDEF MSWINDOWS}
- FDC := GetDC(FWindow.Handle);
- {$ENDIF}
- {$IFDEF LINUX}
- FDC := FWindow.Handle;
- {$ENDIF}
- inherited Create(False);
- end;
- destructor TServiceContextThread.Destroy;
- begin
- ReleaseDC(FWindow.Handle, FDC);
- FWindow.Free;
- inherited;
- end;
- procedure TServiceContextThread.DoCreateServiceContext; stdcall;
- procedure Fail;
- begin
- GLSLogger.LogError(Format('%s: can''t initialize rendering context', [ClassName]));
- FWindow.Destroy;
- vServiceWindow := nil;
- end;
- begin
- try
- GLContextManager.ServiceContext.Acceleration := chaHardware;
- GLContextManager.ServiceContext.CreateMemoryContext(FDC, 1, 1, 1);
- except
- on EGLContext do
- begin
- Fail;
- exit;
- end;
- on EPBuffer do
- begin
- GLSLogger.LogWarning(Format('%s: can''t initialize memory rendering context. Try initialize common context.',
- [ClassName]));
- try
- GLContextManager.ServiceContext.CreateContext(FDC);
- except
- Fail;
- exit;
- end;
- end;
- end;
- GLSLogger.LogNotice('Service context successfuly initialized');
- GLContextManager.ServiceContext.Activate;
- FWindow.Hide;
- vServiceWindow := nil;
- end;
- procedure TServiceContextThread.Execute;
- var
- TaskRec: TServiceContextTask;
- procedure NextTask;
- const
- NullTask: TServiceContextTask = (Task: nil; Event: nil);
- var
- I: Integer;
- begin
- TaskRec.Task := nil;
- with GLContextManager.FThreadTask.LockList do
- try
- for I := 0 to Count - 1 do
- begin
- TaskRec := Items[I];
- if Assigned(TaskRec.Task) then
- begin
- Items[I] := NullTask;
- Break;
- end;
- end;
- finally
- GLContextManager.FThreadTask.UnlockList;
- end;
- end;
- begin
- with GLContextManager do
- begin
- vMainThread := False;
- GLSLogger.LogNotice('Service thread started');
- Sleep(100);
- try
- while not Terminated do
- begin
- NextTask;
- if Assigned(TaskRec.Task) then
- begin
- with GLContextManager.ServiceContext do
- begin
- if IsValid then
- Activate;
- try
- TaskRec.Task;
- except
- GLSLogger.LogError('Service thread task raised exception');
- end;
- if IsValid then
- Deactivate;
- if Assigned(TaskRec.Event) then
- TaskRec.Event.SetEvent;
- end;
- end
- else
- Synchronize(GLContextManager.QueueTaskDepleted);
- ServiceStarter.WaitFor(30000);
- end;
- finally
- ServiceContext.Destroy;
- FServiceContext := nil;
- GLSLogger.LogNotice('Service thread finished');
- end;
- end;
- end;
- procedure AddTaskForServiceContext(ATask: TTaskProcedure; FinishEvent: TFinishTaskEvent = nil);
- var
- TaskRec: TServiceContextTask;
- rEvent: TFinishTaskEvent;
- begin
- if vMainThread then
- begin
- rEvent := nil;
- if Assigned(GLContextManager.ServiceContext) and Assigned(ATask) then
- begin
- CheckSynchronize;
- with GLContextManager.FThreadTask.LockList do
- try
- TaskRec.Task := ATask;
- if FinishEvent = nil then
- begin // Synchronous call
- rEvent := TFinishTaskEvent.Create;
- TaskRec.Event := rEvent;
- end
- else // Asynchronous call
- TaskRec.Event := FinishEvent;
- Add(TaskRec);
- with TServiceContextThread(GLContextManager.FThread) do
- begin
- FLastTaskStartTime := Now;
- FReported := False;
- end;
- finally
- GLContextManager.FThreadTask.UnlockList;
- end;
- GLContextManager.ServiceStarter.SetEvent;
- end;
- // Wait task finishing
- if Assigned(rEvent) then
- begin
- rEvent.WaitFor(INFINITE);
- rEvent.Destroy;
- CheckSynchronize;
- end;
- end
- else
- begin // Direct task execution in service thread
- try
- ATask;
- except
- GLSLogger.LogError('Service thread task raised exception');
- end;
- if Assigned(FinishEvent) then
- FinishEvent.SetEvent;
- end;
- end;
- {$ENDIF USE_SERVICE_CONTEXT}
- constructor TFinishTaskEvent.Create;
- begin
- inherited Create(nil, True, False, '');
- end;
- // ------------------------------------------------------------------
- initialization
- // ------------------------------------------------------------------
- vMainThread := True;
- {$IFDEF USE_SERVICE_CONTEXT}
- OldInitProc := InitProc;
- InitProc := @OnApplicationInitialize;
- {$ENDIF USE_SERVICE_CONTEXT}
- GLContextManager := TGLContextManager.Create;
- GLwithoutContext := TGLExtensionsAndEntryPoints.Create;
- GLwithoutContext.Close;
- // vLocalGL := @GL;
- finalization
- GLContextManager.Terminate;
- vContextClasses.Free;
- vContextClasses := nil;
- GLwithoutContext.Free;
- GLwithoutContext := nil;
- end.
|