溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Using Extensions in OpenGL(使用OpenGL擴(kuò)展)

發(fā)布時(shí)間:2020-07-22 22:54:00 來源:網(wǎng)絡(luò) 閱讀:556 作者:萌谷王 欄目:游戲開發(fā)

周一到周五,每天一篇,北京時(shí)間早上7點(diǎn)準(zhǔn)時(shí)更新~

All of the examples shown in this book so far have relied on the core functionality of OpenGL(到目前為止的案例都是基于OpenGL的核心標(biāo)準(zhǔn)的). However, one of OpenGL’s greatest strengths is that it can be extended and enhanced by hardware manufacturers, operating system vendors, and even publishers of tools and debuggers(然而還有一些擴(kuò)展的API是某部分廠商的硬件獨(dú)特的特性,可能是在標(biāo)準(zhǔn)API里是沒有的). Extensions can have many different effects on OpenGL functionality(這些擴(kuò)展的特性可能會帶來很不一樣的影響)

An extension is any addition to a core version of OpenGL(擴(kuò)展是指對核心版本API的額外補(bǔ)充). Extensions are listed in the OpenGL extension registry on the OpenGL Web site(擴(kuò)展列表在OpenGL的網(wǎng)站上有展示). These extensions are written as a list of differences from a particular version of the OpenGL specification, and note what that version of OpenGL is(這些擴(kuò)展的實(shí)現(xiàn)與某個特定版本的OpenGL標(biāo)準(zhǔn)可能不一致,你需要注意擴(kuò)展是哪個版本的OpenGL擴(kuò)展). That means the text of the extensions describes how the core OpenGL specification must be changed if the extension is supported(也就是說,擴(kuò)展描述了如果你啟動了擴(kuò)展,OpenGL的實(shí)現(xiàn)會變成什么樣子). However, popular and generally useful extensions are normally “promoted” into the core versions of OpenGL(不過,很多有用的擴(kuò)展一般都會最終被納入核心標(biāo)準(zhǔn)中去); thus, if you are running the latest and greatest version of OpenGL, there might not be that many extensions that are interesting but not part of the core profile(也就是說,每次OpenGL更新到最新版本的時(shí)候,以前那些擴(kuò)展可能就不存在了,而是被納入了新版本OpenGL的標(biāo)準(zhǔn)里). A complete list of the extensions that were promoted to each version of OpenGL and a brief synopsis of what they do is included in Appendix C, “OpenGL Features and Versions.”(一個完整的擴(kuò)展列表以及他們的其他相關(guān)信息在附錄C里)

There are three major classifications of extensions: vendor, EXT, and ARB(主要有三種擴(kuò)展:廠商、EXT、ARB). Vendor extensions are written and implemented on one vendor’s hardware(廠商的擴(kuò)展是在廠商自己的硬件上實(shí)現(xiàn)的). Initials representing the specific vendor are usually part of the extension name—“AMD” for Advanced Micro Devices or “NV” for NVIDIA, for example(通常實(shí)現(xiàn)的表述包含了廠商的名字,比如AMD就代表著Advanced Micro Devices,NV代表的是NVIDIA). It is possible that more than one vendor might support a specific vendor extension, especially if it becomes widely accepted(也可能存在多個廠商實(shí)現(xiàn)同一個廠商的擴(kuò)展的情況,特別是如果那個擴(kuò)展被廣泛接受的時(shí)候). EXT extensions are written together by two or more vendors(EXT擴(kuò)展是被兩個以上的廠商實(shí)現(xiàn)的擴(kuò)展). They often start their lives as vendor-specific extensions, but if another vendor is interested in implementing the extension, perhaps with minor changes, it may collaborate with the original authors to produce an EXT version(一般情況下,可能起初只有一個廠商的擴(kuò)展,后來又有其他廠商對此感興趣,且想對其進(jìn)行細(xì)微調(diào)整,所以他們就聯(lián)合起來搞了,搞出來的擴(kuò)展就是EXT擴(kuò)展). ARB extensions are an official part of OpenGL because they are approved by the OpenGL governing body, the Architecture Review Board (ARB)(ARB擴(kuò)展是OpenGL的官方Architecture Review Board所推動的擴(kuò)展). These extensions are often supported by most or all major hardware vendors and may also have started out as vendor or EXT extensions(ARB擴(kuò)展經(jīng)常被大部分或者全部硬件廠商所支持,它們也可能源自廠商的擴(kuò)展或者是EXT擴(kuò)展)

This extension process may sound confusing at first(剛開始聽到擴(kuò)展的推動過程的時(shí)候可能會感到疑惑). Hundreds of extensions currently are available!(上百個擴(kuò)展現(xiàn)在是可以被使用的) But new versions of OpenGL are often constructed from extensions programmers have found useful(新的OpenGL版本往往是從有用的擴(kuò)展進(jìn)行構(gòu)建的). In this way each extension gets its time in the sun(這樣一來,每一個擴(kuò)展都有見到陽光的那一天). The ones that shine can be promoted to core(那些很屌的擴(kuò)展則會被放到核心OpenGL標(biāo)準(zhǔn)里去); the ones that are less useful are not considered(那些不怎么實(shí)用的則不會被考慮). This “natural selection” process helps to ensure only the most useful and important new features make it into a core version of OpenGL(自然選擇能幫助來篩選出那些最有用和最重要的新特性)

A useful tool to determine which extensions are supported in your computer’s OpenGL implementation is Realtech VR’s OpenGL Extensions Viewer. It is freely available from the Realtech VR Web site (see Figure 3.6)(一個可以探測你電腦所支持的擴(kuò)展的工具叫Realtech VR’s OpenGL Extensions Viewer,如圖3.6所示,它是免費(fèi)的)
Using Extensions in OpenGL(使用OpenGL擴(kuò)展)

Enhancing OpenGL with Extensions(使用擴(kuò)展增強(qiáng)OpenGL)

Before using any extensions, you must make sure that they’re supported by the OpenGL implementation that your application is running on(在使用擴(kuò)展之前,你需要確定,這個擴(kuò)展在你當(dāng)前運(yùn)行的的設(shè)備上是被支持的). To find out which extensions OpenGL supports, there are two functions that you can use. First, to determine the number of supported extensions, you can call glGetIntegerv() with the GL_NUM_EXTENSIONS parameter(你可以通過glGetIntegerv(GL_NUM_EXTENSIONS,*)來獲取當(dāng)前環(huán)境支持的擴(kuò)展的個數(shù)). Next, you can find out the name of each of the supported extensions by calling(然后你可以通過下面的API獲取每個擴(kuò)展的名字)

const GLubyte* glGetStringi(GLenum name,GLuint index);

You should pass GL_EXTENSIONS as the name parameter, and a value between 0 and 1 less than the number of supported extensions in index(第一個參數(shù)要傳GL_EXTENSIONS,第二個參數(shù)是第幾個擴(kuò)展,這個值要小于上一步獲得的擴(kuò)展個數(shù)). The function returns the name of the extension as a string(這個函數(shù)返回一個擴(kuò)展的名字). To see if a specific extension is supported, you can simply query the number of extensions, and then loop through each supported extension and compare its name to the one you’re looking for(為了知道一個擴(kuò)展是否被支持,你可以先拿到所有擴(kuò)展的名字,然后在名字里找找看,是否存在你想查詢的那個). The book’s source code comes with a simple function that does this for you(在本課程中,我們就封裝了這樣一個方法). sb7IsExtensionSupported() has the prototype(它的名字叫sb7IsExtensionSupported,函數(shù)定義如下)

int sb7IsExtensionSupported(const char * extname);

This function is declared in the header, takes the name of an extension, and returns non-zero if it is supported by the current OpenGL context and zero if it is not(這個函數(shù)申明在sb7ext.h中,如果被查詢的擴(kuò)展被支持,則返回非0,否則返回0). Your application should always check for support for extensions you wish to use before using them(你的程序要在使用擴(kuò)展前檢查擴(kuò)展是否被支持)

Extensions generally add to OpenGL in some combination of four different ways(擴(kuò)展通常由四種方式的某種組合添加到OpenGL中):

They can make things legal that weren’t before, by simply removing restrictions from the OpenGL specification.(他們可以通過移除一些OpenGL的限制使得以前不合法的事情合法)
They can add tokens or extend the range of values that can be passed as parameters to existing functions.(他們相對于現(xiàn)存的函數(shù),可以接受范圍更廣的參數(shù)或者token)
They can extend GLSL to add functionality, built-in functions, variables, or data types.(他們可以通過添加功能,內(nèi)置變量或者什么數(shù)據(jù)類型來增強(qiáng)GLSL)
They can add entirely new functions to OpenGL itself(他們?yōu)镺penGL增加了的嶄新的函數(shù))
In the first case, where things that once were considered errors no longer are, your application doesn’t need to do anything besides start using the newly allowed behavior (once you have determined that the extension is supported, of course)(第一種情況下,以前被認(rèn)為是錯誤的調(diào)用會變得正確,除了使用新被允許的行為,你不需要修改你的程序). Likewise, for the second case, you can just start using the new token values in the relevant functions, presuming that you have their values(同樣,對于第二種情況,如果你有這些參數(shù)的話,你可以開始在相關(guān)的API中使用新的參數(shù)). The values of the tokens are in the extension specifications, so you can look them up there if they are not included in your system’s header files.(如果你在系統(tǒng)的頭文件里找不到的話,這些參數(shù)在擴(kuò)展的文檔里)

To enable use of extensions in GLSL, you must first include a line at the beginning of shaders that use them to tell the compiler that you’re going to need their features(為了使用那些GLSL里的擴(kuò)展,你需要在GLSL中告訴編譯器,你需要使用到那些特性). For example, to enable the hypothetical GL_ABC_foobar_feature extension in GLSL,include the following in the beginning of your shader(比如,要使用GL_ABC_foobar_feature擴(kuò)展,你需要在你的shader里寫上下面這句話):

#extension GL_ABC_foobar_feature : enable

This tells the compiler that you intend to use the extension in your shader(這句話告訴編譯器,你要在shader里使用這個特性). If the compiler knows about the extension, it will let you compile the shader, even if the underlying hardware doesn’t support the feature(如果編譯器知道這個特性,即便底層硬件不支持,編譯器也可以編譯出正確的GPU程序). If this is the case, the compiler should issue a warning if it sees that the extension is actually being used(如果是這種情況的話,如果這個特性確實(shí)被shader使用了,編譯器應(yīng)該發(fā)出警告). Typically, extensions to GLSL will add preprocessor tokens to indicate their presence(通常來講,GLSL的擴(kuò)展會被進(jìn)行預(yù)處理,從而確定他們的表現(xiàn)). For example, GL_ABC_foobar_feature will implicitly include(比如GL_ABC_foobar_feature將被隱式包含)

#define GL_ABC_foobar_feature 1
This means that you could write code such as(這句話就表明,你可以寫如下的代碼)

#if GL_ABC_foobar_feature
// Use functions from the foobar extension
#else
// Emulate or otherwise work around the missing functionality
#endif
This allows you to conditionally compile or execute functionality that is part of an extension that may or may not be supported by the underlying OpenGL implementation. If your shader absolutely requires support for an extension and will not work at all without it, you can instead include this more assertive code:

#extension GL_ABC_foobar_feature : require
If the OpenGL implementation does not support the GL_ABC_foobar_feature extension, then it will fail to compile the shader and report an error on the line including the #extension directive(如果OpenGL的實(shí)現(xiàn)不支持GL_ABC_foobar_feature擴(kuò)展,編譯shader時(shí)將會失敗,并報(bào)出錯誤在哪里). In effect, GLSL extensions are opt-in features, and applications must tell compilers up front which extensions they intend to use(實(shí)際上GLSL擴(kuò)展是具有選擇性特性的,應(yīng)用程序應(yīng)該告訴編譯器他們想使用什么樣的擴(kuò)展)

Next we come to extensions that introduce new functions to OpenGL(接下來,我們看看那些為OpenGL添加了新函數(shù)的擴(kuò)展). On most platforms, you don’t have direct access to the OpenGL driver and extension functions don’t just magically appear as available to your applications to call(在大多數(shù)平臺上,你無法直接訪問OpenGL的驅(qū)動,擴(kuò)展函數(shù)不會那么神奇的直接就可以被你的程序訪問). Rather, you must ask the OpenGL driver for a function pointer that represents the function you want to call(更多的是,你需要找OpenGL的驅(qū)動要到你想要訪問的函數(shù)的指針). Function pointers are generally declared in two parts;(函數(shù)指針通常被定義成兩個部分) the first is the definition ofthe function pointer type, and the second is the function pointer variable itself.(第一個部分是函數(shù)的指針類型,第二個部分是函數(shù)指針本身) Consider this code as an example:(例如下面的)

typedef void
(APIENTRYP PFNGLDRAWTRANSFORMFEEDBACKPROC) (GLenum mode,GLuint id);
PFNGLDRAWTRANSFORMFEEDBACKPROC glDrawTransformFeedback = NULL;
This declares the PFNGLDRAWTRANSFORMFEEDBACKPROC type as a pointer to a function taking GLenum and GLuint parameters(這就定義了PFNGLDRAWTRANSFORMFEEDBACKPROC類型的函數(shù)指針,且這個函數(shù)接受一個GLenum和GLuint的參數(shù)). Next, it declares the glDrawTransformFeedback variable as an instance of this type(然后,它定義了該函數(shù)指針類型的實(shí)際的變量). In fact, on many platforms, the declaration of the glDrawTransformFeedback() function is actually just like this(實(shí)際上,在很多平臺上,就如同剛才展示的這樣,glDrawTransformFeedback就是這么定義的). This seems pretty complicated, but fortunately the following header files include declarations of all of the function prototypes, function pointer types, and token values introduced by all registered OpenGL extensions(這看上去非常的復(fù)雜,但是幸運(yùn)的是,下面的這個頭文件定義了所有的函數(shù)類型,函數(shù)指針的類型以及那些參數(shù)):

#include
#include
#include
These files can be found at the OpenGL extension registry Web site(這些可以在OpenGL擴(kuò)展的網(wǎng)站上找到). The glext.h header contains both standard OpenGL extensions and many vendor-specific OpenGL extensions(glext.h包含了標(biāo)準(zhǔn)的OpenGL擴(kuò)展和很多廠商的擴(kuò)展), the wglext.h header contains a number of extensions that are Windows specific(wglext.h包含的是Windows上支持的擴(kuò)展), and the glxext.h header contains definitions that are X specific (glxext.h包含的是X擴(kuò)展,X指代的是Linux或者是Unix系的窗口系統(tǒng))(X is the windowing system used on Linux and many other UNIX derivatives and implementations)

The method for querying the address of extension functions is actually platform specific(查詢擴(kuò)展函數(shù)的訪問地址會因平臺而異). The book’s application framework wraps up these intricacies into a handy function that is declared in the header file(本書的框架把這些方法打包進(jìn)了sb7ext.h的一個函數(shù)里去了). The function sb7GetProcAddress() has this prototype(這個函數(shù)叫sb7GetProcAddress,它的定義如下):

void sb7GetProcAddress(const char funcname);
Here, funcname is the name of the extension function that you wish to use(這里funcname是擴(kuò)展函數(shù)的名字). The return value is the address of the function, if it’s supported, and NULL otherwise(如果擴(kuò)展被支持,那么該函數(shù)返回一個函數(shù)地址,否則返回NULL). Even if OpenGL returns a valid function pointer for a function that’s part of the extension you want to use, that doesn’t mean the extension is present(即便OpenGL返回了一個合法的函數(shù)指針,那也只是一個擴(kuò)展,這并不意味著擴(kuò)展存在). Sometimes the same function is part of more than one extension, and sometimes vendors ship drivers with partial implementations of extensions present(有時(shí)候同樣的函數(shù)是多個擴(kuò)展的一部分,也有的時(shí)候廠商只實(shí)現(xiàn)了擴(kuò)展的一部分). Always check for support for extensions using the official mechanisms or the sb7IsExtensionSupported() function(請確保使用擴(kuò)展之前來檢查擴(kuò)展是否被支持).

本日的翻譯就到這里,明天見,拜拜~~

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI