您好,登錄后才能下訂單哦!
周一到周五,每天一篇,北京時(shí)間早上7點(diǎn)準(zhǔn)時(shí)更新~
After allocating storage space for your buffer object using glBufferStorage(), one possible next step is to fill the buffer with known data. Whether you use the initial data parameter of glBufferStorage(), use glBufferSubData() to put the initial data in the buffer, or use glMapBufferRange() to obtain a pointer to the buffer’s data store and fill it with your application, you will need to get the buffer into a known state before you can use it productively. If the data you want to put into a buffer is a constant value, it is probably much more efficient to call glClearBufferSubData() or glClearNamedBufferSubData(), whose prototypes are
在緩沖區(qū)對(duì)象分配好了內(nèi)存后,下一步可能就輪到給給內(nèi)存填充數(shù)據(jù)了。你可以在分配內(nèi)存的同時(shí)為緩沖區(qū)填充數(shù)據(jù),也可以使用glBufferSubData或者glMapBufferRange在緩沖區(qū)內(nèi)存分配好之后 為緩沖區(qū)填充數(shù)據(jù)。在你使用buffer之前,你需要把buffer置于一個(gè)已知的狀態(tài)。如果你想往緩沖區(qū)里刷入常數(shù),那么你可以使用glClearBufferSubData或者glClearNamedBufferSubData來高效的做這事。
void glClearBufferSubData(GLenum target,
GLenum internalformat,
GLintptr offset,
GLsizeiptr size,
GLenum format,
GLenum type,
const void data);
void glClearNamedBuffeSubData(GLuint buffer,
GLenum internalformat,
GLintptr offset,
GLsizeiptr size,
GLenum format,
GLenum type,
const void data);
These functions take a pointer to a variable containing the values that you want to clear the buffer object to and, after converting it to the format specified in internalformat, replicate the data across the range of the buffer’s data store specified by offset and size, both of which are measured in bytes. format and type tell OpenGL about the data pointed to by data. The format can be one of GL_RED, GL_RG, GL_RGB, or GL_RGBA to specify one-, two-, three-, or four-channel data, for example. Meanwhile, type should represent the data type of the components. For instance, it could be GL_UNSIGNED_BYTE or GL_FLOAT to specify unsigned bytes or floating-point data, respectively. The most common types supported by OpenGL and their corresponding C data types are listed in Table 5.3.
這些函數(shù)使用data指針指向位置的數(shù)據(jù)去刷新緩沖區(qū)的內(nèi)存,數(shù)據(jù)會(huì)被轉(zhuǎn)化成internalformat的格式,數(shù)據(jù)刷新的多少由offset和size指定,數(shù)據(jù)大小的單位為字節(jié)。 format和type告訴OpenGL,data指針指向的數(shù)據(jù)的格式,format的值可以是GGL_RED, GL_RG, GL_RGB,或者 GL_RGBA,用來標(biāo)記數(shù)據(jù)是1、2、3、4通道的數(shù)據(jù)。type這里指定的是每個(gè)通道是什么格式。 比如,它可能是GL_UNSIGNED_BYTE或者GL_FLOAT。比較常見的類型在表5.3里展示出來了
Once your data has been sent to the GPU, it’s entirely possible you may want to share that data between buffers or copy the results from one buffer into another. OpenGL provides an easy-to-use way of doing that. glCopyBufferSubData() and glCopyNamedBufferSubData() let you specify which buffers are involved as well as the size and offsets to use.
當(dāng)你把數(shù)據(jù)發(fā)送給GPU后,你可能希望與其他buffer共享數(shù)據(jù)或者是從一個(gè)buffer拷貝數(shù)據(jù)到另一個(gè)buffer。OpenGL提供的對(duì)應(yīng)的API是 glCopyBufferSubData和glCopyNamedBufferSubData,他們的定義如下
void glCopyBufferSubData(GLenum readtarget,
GLenum writetarget,
GLintptr readoffset,
GLintptr writeoffset,
GLsizeiptr size);
void glCopyNamedBufferSubData(GLuint readBuffer,
GLuint writeBuffer,
GLintptr readOffset,
GLintptr writeOffset,
GLsizeiptr size);
For glCopyBufferSubData(), the readtarget and writetarget are the targets where the two buffers you want to copy data between are bound. They can be buffers bound to any of the available buffer binding points. However, since buffer binding points can have only one buffer bound at a time, you couldn’t copy between two buffers that are both bound to the GL_ARRAY_BUFFER target, for example. Thus, when you perform the copy, you need to pick two targets to bind the buffers to, which will disturb the OpenGL state.
glCopyBufferSubData的數(shù)據(jù)讀取地址和數(shù)據(jù)寫入地址只某個(gè)綁定的節(jié)點(diǎn)而不是buffer對(duì)象本身的標(biāo)記。然而每個(gè)特定的綁定節(jié)點(diǎn)只能綁定一個(gè)buffer,所以你無法將兩個(gè)都綁定在GL_ARRAY_BUFFER 上的buffer進(jìn)行數(shù)據(jù)拷貝。所以,在你要拷貝的時(shí)候,你需要將buffer綁定到兩個(gè)綁定節(jié)點(diǎn)上去,然后執(zhí)行拷貝操作,這實(shí)際上會(huì)擾亂OpenGL狀態(tài)機(jī)
To resolve this, OpenGL provides the GL_COPY_READ_BUFFER and GL_COPY_WRITE_BUFFER targets. These targets were added specifically to allow you to copy data from one buffer to another without any unintended side effects. Because they are not used for anything else in OpenGL, you can bind your read and write buffers to these binding points without affecting any other buffer target.
為了實(shí)現(xiàn)這一目標(biāo),OpenGL提供兩個(gè)專門的綁定節(jié)點(diǎn),GL_COPY_READ_BUFFER和GL_COPY_WRITE_BUFFER,這倆節(jié)點(diǎn)能執(zhí)行buffer間的數(shù)據(jù)拷貝,并且沒有什么副作用。 因?yàn)檫@倆貨除了具備拷貝功能以外不會(huì)有其他功能,所以你執(zhí)行buffer間拷貝的時(shí)候,改變這倆個(gè)節(jié)點(diǎn)的狀態(tài)不會(huì)影響到其他的OpenGL的邏輯。
Alternatively, you can use the glCopyNamedBufferSubData() form, which takes the names of the two buffers directly. Of course, you can specify the same buffer for both readBuffer and writeBuffer to copy a region of data between two offsets in the same buffer object. Be careful that the regions to be copied don’t overlap, though, as in this case the results of the copy are undefined. You can consider glCopyNamedBufferSubData() as a form of the C function memcpy for buffer objects.
同樣的,你可以使用第二個(gè)glCopyNamedBufferSubData,直接傳入寫數(shù)據(jù)的地址和讀數(shù)據(jù)的地址。你同樣可以指定寫入和讀取的是同一個(gè)buffer,并在buffer內(nèi)的不同區(qū)域內(nèi)進(jìn)行數(shù)據(jù)的拷貝。你可以把這個(gè)函數(shù)想象成為memcpy
The readoffset and writeoffset parameters tell OpenGL where in the source and destination buffers to read or write the data, and the size parameter tells it how big the copy should be. Be sure that the ranges you are reading from and writing to remain within the bounds of the buffers; otherwise, your copy will fail. You may notice the types of readoffset, writeoffset, and size, which are GLintptr and GLsizeiptr. These types are special definitions of integer types that are at least wide enough to hold a pointer variable
readoffset和writeoffset告訴OpenGL讀地址和寫地址的數(shù)據(jù)偏移位置。size告訴OpenGL拷貝數(shù)據(jù)的大小。你必須保證別越界了,否則你的拷貝會(huì)失敗。 你可能注意到了GLintptr和GLsizeiptr,這些數(shù)據(jù)都是整型的數(shù)據(jù),它們對(duì)于標(biāo)記指針的地址來說已經(jīng)足夠大了。
本日的翻譯就到這里,明天見,拜拜~~
第一時(shí)間獲取最新橋段,請(qǐng)關(guān)注東漢書院以及圖形之心公眾號(hào)
東漢書院,等你來玩哦
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。