您好,登錄后才能下訂單哦!
周一到周五,每天一篇,北京時間早上7點準時更新~
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
在緩沖區對象分配好了內存后,下一步可能就輪到給給內存填充數據了。你可以在分配內存的同時為緩沖區填充數據,也可以使用glBufferSubData或者glMapBufferRange在緩沖區內存分配好之后 為緩沖區填充數據。在你使用buffer之前,你需要把buffer置于一個已知的狀態。如果你想往緩沖區里刷入常數,那么你可以使用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.
這些函數使用data指針指向位置的數據去刷新緩沖區的內存,數據會被轉化成internalformat的格式,數據刷新的多少由offset和size指定,數據大小的單位為字節。 format和type告訴OpenGL,data指針指向的數據的格式,format的值可以是GGL_RED, GL_RG, GL_RGB,或者 GL_RGBA,用來標記數據是1、2、3、4通道的數據。type這里指定的是每個通道是什么格式。 比如,它可能是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.
當你把數據發送給GPU后,你可能希望與其他buffer共享數據或者是從一個buffer拷貝數據到另一個buffer。OpenGL提供的對應的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的數據讀取地址和數據寫入地址只某個綁定的節點而不是buffer對象本身的標記。然而每個特定的綁定節點只能綁定一個buffer,所以你無法將兩個都綁定在GL_ARRAY_BUFFER 上的buffer進行數據拷貝。所以,在你要拷貝的時候,你需要將buffer綁定到兩個綁定節點上去,然后執行拷貝操作,這實際上會擾亂OpenGL狀態機
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.
為了實現這一目標,OpenGL提供兩個專門的綁定節點,GL_COPY_READ_BUFFER和GL_COPY_WRITE_BUFFER,這倆節點能執行buffer間的數據拷貝,并且沒有什么副作用。 因為這倆貨除了具備拷貝功能以外不會有其他功能,所以你執行buffer間拷貝的時候,改變這倆個節點的狀態不會影響到其他的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.
同樣的,你可以使用第二個glCopyNamedBufferSubData,直接傳入寫數據的地址和讀數據的地址。你同樣可以指定寫入和讀取的是同一個buffer,并在buffer內的不同區域內進行數據的拷貝。你可以把這個函數想象成為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讀地址和寫地址的數據偏移位置。size告訴OpenGL拷貝數據的大小。你必須保證別越界了,否則你的拷貝會失敗。 你可能注意到了GLintptr和GLsizeiptr,這些數據都是整型的數據,它們對于標記指針的地址來說已經足夠大了。
本日的翻譯就到這里,明天見,拜拜~~
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公眾號
東漢書院,等你來玩哦
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。