LearnOpenGL

Translation in progress of learnopengl.com.
git clone https://git.mtkn.jp/LearnOpenGL
Log | Files | Refs

Advanced-Data.html (9407B)


      1     <div id="content">
      2     <h1 id="content-title">Advanced Data</h1>
      3 <h1 id="content-url" style='display:none;'>Advanced-OpenGL/Advanced-Data</h1>
      4 <p>
      5   Throughout most chapters we've been extensively using buffers in OpenGL to store data on the GPU. This chapter we'll briefly discuss a few alternative approaches to managing buffers.
      6 </p>
      7 
      8 <p>
      9   A buffer in OpenGL is, at its core, an object that manages a certain piece of GPU memory and nothing more. We give meaning to a buffer when binding it to a specific <def>buffer target</def>. A buffer is only a vertex array buffer when we bind it to <var>GL_ARRAY_BUFFER</var>, but we could just as easily bind it to <var>GL_ELEMENT_ARRAY_BUFFER</var>. OpenGL internally stores a reference to the buffer per target and, based on the target, processes the buffer differently. 
     10 </p>
     11 
     12 <p>
     13   So far we've been filling the buffer's memory by calling <fun><function id='31'>glBufferData</function></fun>, which allocates a piece of GPU memory and adds data into this memory. If we were to pass <code>NULL</code> as its data argument, the function would only allocate memory and not fill it. This is useful if we first want to <em>reserve</em> a specific amount of memory and later come back to this buffer.
     14 </p>
     15 
     16 <p>
     17   Instead of filling the entire buffer with one function call we can also fill specific regions of the buffer by calling <fun><function id='90'>glBufferSubData</function></fun>. This function expects a buffer target, an offset, the size of the data and the actual data as its arguments. What's new with this function is that we can now give an offset that specifies from <em>where</em> we want to fill the buffer. This allows us to insert/update only certain parts of the buffer's memory. Do note that the buffer should have enough allocated memory so a call to <fun><function id='31'>glBufferData</function></fun> is necessary before calling <fun><function id='90'>glBufferSubData</function></fun> on the buffer.
     18 </p>
     19 
     20 <pre><code>
     21 <function id='90'>glBufferSubData</function>(GL_ARRAY_BUFFER, 24, sizeof(data), &data); // Range: [24, 24 + sizeof(data)]
     22 </code></pre>
     23 
     24 <p>
     25   Yet another method for getting data into a buffer is to ask for a pointer to the buffer's memory and directly copy the data in memory yourself. By calling <fun><function id='91'>glMapBuffer</function></fun> OpenGL returns a pointer to the currently bound buffer's memory for us to operate on:
     26 </p>
     27 
     28 <pre><code>
     29 float data[] = {
     30   0.5f, 1.0f, -0.35f
     31   [...]
     32 };
     33 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, buffer);
     34 // get pointer
     35 void *ptr = <function id='91'>glMapBuffer</function>(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
     36 // now copy data into memory
     37 memcpy(ptr, data, sizeof(data));
     38 // make sure to tell OpenGL we're done with the pointer
     39 <function id='92'>glUnmapBuffer</function>(GL_ARRAY_BUFFER);
     40 </code></pre>
     41 
     42 <p>
     43   By telling OpenGL we're finished with the pointer operations via <fun><function id='92'>glUnmapBuffer</function></fun>, OpenGL knows you're done. By unmapping, the pointer becomes invalid and the function returns <var>GL_TRUE</var> if OpenGL was able to map your data successfully to the buffer.
     44 </p>
     45 
     46 <p>
     47   Using <fun><function id='91'>glMapBuffer</function></fun> is useful for directly mapping data to a buffer, without first storing it in temporary memory. Think of directly reading data from file and copying it into the buffer's memory.
     48 </p>
     49 
     50 <h2>Batching vertex attributes</h2>
     51 <p>
     52   Using <fun><function id='30'>glVertexAttribPointer</function></fun> we were able to specify the attribute layout of the vertex array buffer's content. Within the vertex array buffer we <def>interleaved</def> the attributes; that is, we placed the position, normal and/or texture coordinates next to each other in memory for each vertex. Now that we know a bit more about buffers we can take a different approach.
     53 </p>
     54   
     55 <p>
     56   What we could also do is batch all the vector data into large chunks per attribute type instead of interleaving them. Instead of an interleaved layout <code>123123123123</code> we take a batched approach <code>111122223333</code>. 
     57 </p>
     58 
     59 <p>
     60   When loading vertex data from file you generally retrieve an array of positions, an array of normals and/or an array of texture coordinates. It may cost some effort to combine these arrays into one large array of interleaved data. Taking the batching approach is then an easier solution that we can easily implement using <fun><function id='90'>glBufferSubData</function></fun>:
     61 </p>
     62 
     63 <pre><code>
     64 float positions[] = { ... };
     65 float normals[] = { ... };
     66 float tex[] = { ... };
     67 // fill buffer
     68 <function id='90'>glBufferSubData</function>(GL_ARRAY_BUFFER, 0, sizeof(positions), &positions);
     69 <function id='90'>glBufferSubData</function>(GL_ARRAY_BUFFER, sizeof(positions), sizeof(normals), &normals);
     70 <function id='90'>glBufferSubData</function>(GL_ARRAY_BUFFER, sizeof(positions) + sizeof(normals), sizeof(tex), &tex);
     71 </code></pre>
     72 
     73 <p>
     74   This way we can directly transfer the attribute arrays as a whole into the buffer without first having to process them. We could have also combined them in one large array and fill the buffer right away using <fun><function id='31'>glBufferData</function></fun>, but using <fun><function id='90'>glBufferSubData</function></fun> lends itself perfectly for tasks like these.
     75 </p>
     76 <p>
     77   We'll also have to update the vertex attribute pointers to reflect these changes:
     78 </p>
     79 
     80 <pre><code>
     81 <function id='30'>glVertexAttribPointer</function>(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);  
     82 <function id='30'>glVertexAttribPointer</function>(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(sizeof(positions)));  
     83 <function id='30'>glVertexAttribPointer</function>(
     84   2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(sizeof(positions) + sizeof(normals)));  
     85 </code></pre>
     86 
     87 <p>
     88   Note that the <code>stride</code> parameter is equal to the size of the vertex attribute, since the next vertex attribute vector can be found directly after its 3 (or 2) components. 
     89 </p>
     90 
     91 <p>
     92   This gives us yet another approach of setting and specifying vertex attributes. Using either approach is feasible, it is mostly a more organized way to set vertex attributes. However, the interleaved approach is still the recommended approach as the vertex attributes for each vertex shader run are then closely aligned in memory. 
     93 </p>
     94 
     95 <h2>Copying buffers</h2>
     96 <p>
     97   Once your buffers are filled with data you may want to share that data with other buffers or perhaps copy the buffer's content into another buffer. The function <fun><function id='93'>glCopyBufferSubData</function></fun> allows us to copy the data from one buffer to another buffer with relative ease. The function's prototype is as follows:
     98 </p>
     99 
    100 <pre><code>
    101 void <function id='93'>glCopyBufferSubData</function>(GLenum readtarget, GLenum writetarget, GLintptr readoffset,
    102                          GLintptr writeoffset, GLsizeiptr size);
    103 </code></pre>
    104 
    105 <p>
    106   The <code>readtarget</code> and <code>writetarget</code> parameters expect to give the buffer targets that we want to copy from and to. We could for example copy from a  <var>VERTEX_ARRAY_BUFFER</var> buffer to a <var>VERTEX_ELEMENT_ARRAY_BUFFER</var> buffer by specifying those buffer targets as the read and write targets respectively. The buffers currently bound to those buffer targets will then be affected. 
    107 </p>
    108 
    109 <p>
    110   But what if we wanted to read and write data into two different buffers that are both vertex array buffers? We can't bind two buffers at the same time to the same buffer target. For this reason, and this reason alone, OpenGL gives us two more buffer targets called <var>GL_COPY_READ_BUFFER</var> and <var>GL_COPY_WRITE_BUFFER</var>. We then bind the buffers of our choice to these new buffer targets and set those targets as the <code>readtarget</code> and <code>writetarget</code> argument.
    111 </p>
    112 
    113 <p>
    114   <fun><function id='93'>glCopyBufferSubData</function></fun> then reads data of a given <code>size</code> from a given <code>readoffset</code> and writes it into the <code>writetarget</code> buffer at <code>writeoffset</code>. An example of copying the content of two vertex array buffers is shown below:
    115 </p>
    116 
    117 <pre><code>
    118 <function id='32'>glBindBuffer</function>(GL_COPY_READ_BUFFER, vbo1);
    119 <function id='32'>glBindBuffer</function>(GL_COPY_WRITE_BUFFER, vbo2);
    120 <function id='93'>glCopyBufferSubData</function>(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 8 * sizeof(float));
    121 </code></pre>
    122 
    123 <p>
    124   We could've also done this by only binding the <code>writetarget</code> buffer to one of the new buffer target types:
    125 </p>
    126 
    127 <pre><code>
    128 float vertexData[] = { ... };
    129 <function id='32'>glBindBuffer</function>(GL_ARRAY_BUFFER, vbo1);
    130 <function id='32'>glBindBuffer</function>(GL_COPY_WRITE_BUFFER, vbo2);
    131 <function id='93'>glCopyBufferSubData</function>(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 8 * sizeof(float));  
    132 </code></pre>
    133 
    134 
    135 <p>
    136   With some extra knowledge about how to manipulate buffers we can already use them in more interesting ways. The further you get in OpenGL, the more useful these new buffer methods start to become. In the <a href="https://learnopengl.com/Advanced-OpenGL/Advanced-GLSL" target="_blank">next</a> chapter, where we'll discuss <def>uniform buffer objects</def>, we'll make good use of <fun><function id='90'>glBufferSubData</function></fun>.
    137 </p>       
    138 
    139     </div>
    140