週一到週五,天天一篇,北京時間早上7點準時更新~app
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 areide
在緩衝區對象分配好了內存後,下一步可能就輪到給給內存填充數據了。你能夠在分配內存的同時爲緩衝區填充數據,也能夠使用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.ui
這些函數使用data指針指向位置的數據去刷新緩衝區的內存,數據會被轉化成internalformat的格式,數據刷新的多少由offset和size指定,數據大小的單位爲字節。 format和type告訴OpenGL,data指針指向的數據的格式,format的值能夠是GGL_RED, GL_RG, GL_RGB,或者 GL_RGBA,用來標記數據是一、二、三、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.this
當你把數據發送給GPU後,你可能但願與其餘buffer共享數據或者是從一個buffer拷貝數據到另外一個buffer。OpenGL提供的對應的API是 glCopyBufferSubData和glCopyNamedBufferSubData,他們的定義以下spa
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.prototype
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的邏輯。component
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,這些數據都是整型的數據,它們對於標記指針的地址來講已經足夠大了。
本日的翻譯就到這裏,明天見,拜拜~~
第一時間獲取最新橋段,請關注東漢書院以及圖形之心公衆號
東漢書院,等你來玩哦