<legend id='RNNUR'><style id='RNNUR'><dir id='RNNUR'><q id='RNNUR'></q></dir></style></legend>
  • <small id='RNNUR'></small><noframes id='RNNUR'>

  • <i id='RNNUR'><tr id='RNNUR'><dt id='RNNUR'><q id='RNNUR'><span id='RNNUR'><b id='RNNUR'><form id='RNNUR'><ins id='RNNUR'></ins><ul id='RNNUR'></ul><sub id='RNNUR'></sub></form><legend id='RNNUR'></legend><bdo id='RNNUR'><pre id='RNNUR'><center id='RNNUR'></center></pre></bdo></b><th id='RNNUR'></th></span></q></dt></tr></i><div id='RNNUR'><tfoot id='RNNUR'></tfoot><dl id='RNNUR'><fieldset id='RNNUR'></fieldset></dl></div>
    <tfoot id='RNNUR'></tfoot>
        • <bdo id='RNNUR'></bdo><ul id='RNNUR'></ul>
      1. GLSL:用缓冲区或纹理替换大型统一整数数组

        时间:2023-09-17
          <bdo id='vDAJ5'></bdo><ul id='vDAJ5'></ul>
            • <small id='vDAJ5'></small><noframes id='vDAJ5'>

            • <legend id='vDAJ5'><style id='vDAJ5'><dir id='vDAJ5'><q id='vDAJ5'></q></dir></style></legend>

                    <tbody id='vDAJ5'></tbody>
                  <tfoot id='vDAJ5'></tfoot>

                  <i id='vDAJ5'><tr id='vDAJ5'><dt id='vDAJ5'><q id='vDAJ5'><span id='vDAJ5'><b id='vDAJ5'><form id='vDAJ5'><ins id='vDAJ5'></ins><ul id='vDAJ5'></ul><sub id='vDAJ5'></sub></form><legend id='vDAJ5'></legend><bdo id='vDAJ5'><pre id='vDAJ5'><center id='vDAJ5'></center></pre></bdo></b><th id='vDAJ5'></th></span></q></dt></tr></i><div id='vDAJ5'><tfoot id='vDAJ5'></tfoot><dl id='vDAJ5'><fieldset id='vDAJ5'></fieldset></dl></div>

                1. 本文介绍了GLSL:用缓冲区或纹理替换大型统一整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  现在我正在尝试将一个整数数组传递到片段着色器中,并通过一个统一数组执行此操作:

                  Right now I am trying to pass an array of ints into the fragment shader, and am doing this through a uniform array:

                  uniform int myArray[300];
                  

                  并在着色器外用 glUniform1iv 填充它.

                  And filling it outside the shader with glUniform1iv.

                  不幸的是,大于 ~400 的统一数组失败了.我知道我可以使用统一缓冲区"代替,但似乎无法找到将大型一维数组传递到带有缓冲区或其他方式的片段着色器的完整示例.

                  Unfortunately, uniform arrays larger than ~400 fail. I understand that I can use a "uniform buffer" instead, but can't seem to a find a full example of passing a large 1D array into a fragment shader with buffers or otherwise.

                  谁能提供这样的例子?

                  推荐答案

                  这应该会让您开始使用统一缓冲区对象来存储数组.注意GL要求UBO的最小容量为16 KiB,最大容量可以通过GL_MAX_UNIFORM_BLOCK_SIZE查询.

                  This should get you started using a Uniform Buffer Object to store an array. Note that GL requires UBOs to have a minimum capacity of 16 KiB, the maximum capacity can be queried through GL_MAX_UNIFORM_BLOCK_SIZE.

                  #version 140 // GL 3.1
                  
                  // Arrays in a UBO must use a constant expression for their size.
                  const int MY_ARRAY_SIZE = 512;
                  
                  // The name of the block is used for finding the index location only
                  layout (std140) uniform myArrayBlock {
                    int myArray [MY_ARRAY_SIZE]; // This is the important name (in the shader).
                  };
                  
                  void main (void) {
                    gl_FragColor = vec4 ((float)myArray [0] * 0.1, vec3 (1.0));
                  }
                  

                  OpenGL 代码

                  const int MY_ARRAY_SIZE = 512;
                  
                  GLuint myArrayUBO;
                  glGenBuffers (1, &myArrayUBO);
                  
                  // Allocate storage for the UBO
                  glBindBuffer (GL_UNIFORM_BUFFER, myArrayUBO);
                  glBufferData (GL_UNIFORM_BUFFER, sizeof (GLint) * MY_ARRAY_SIZE,
                                NULL, GL_DYNAMIC_DRAW);
                  
                  [...]
                  
                  // When you want to update the data in your UBO, you do it like you would any
                  //   other buffer object.
                  glBufferSubData (GL_UNIFORM_BUFFER, ...);
                  
                  [...]
                  
                  GLuint myArrayBlockIdx = glGetUniformBlockIndex (GLSLProgramID, "myArrayBlock");
                  
                  glUniformBlockBinding (GLSLProgramID,     myArrayBlockIdx, 0);
                  glBindBufferBase      (GL_UNIFORM_BUFFER, 0,               myArrayUBO);
                  

                  我可能忘记了一些东西,我不写教程是有原因的.如果您在执行此操作时遇到任何问题,请发表评论.

                  I am probably forgetting something, there is a reason I do not write tutorials. If you have any trouble implementing this, leave a comment.

                  注意glUniformBlockBinding(...)glBindBufferBase(...)中使用的0是绑定的全局标识符观点.当与 std140 布局结合使用时,这意味着您可以在任何 GLSL 程序中使用此 UBO,在该程序中您将其统一块之一绑定到该绑定位置 (0).当您想在几十个不同的 GLSL 程序之间共享诸如 ModelView 和 Projection 矩阵之类的东西时,这实际上非常方便.

                  Note that the 0 used in glUniformBlockBinding (...) and glBindBufferBase (...) is a global identifier for the binding point. When used in conjunction with the std140 layout, this means that you can use this UBO in any GLSL program where you bind one of its uniform blocks to that binding location (0). This is actually extremely handy when you want to share something like your ModelView and Projection matrices between dozens of different GLSL programs.

                  这篇关于GLSL:用缓冲区或纹理替换大型统一整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:#error gl.h 包含在 glew.h 之前 下一篇:如何让 VBO 工作

                  相关文章

                  最新文章

                  <small id='k8TZk'></small><noframes id='k8TZk'>

                  • <bdo id='k8TZk'></bdo><ul id='k8TZk'></ul>
                    <legend id='k8TZk'><style id='k8TZk'><dir id='k8TZk'><q id='k8TZk'></q></dir></style></legend>

                    <tfoot id='k8TZk'></tfoot>
                  1. <i id='k8TZk'><tr id='k8TZk'><dt id='k8TZk'><q id='k8TZk'><span id='k8TZk'><b id='k8TZk'><form id='k8TZk'><ins id='k8TZk'></ins><ul id='k8TZk'></ul><sub id='k8TZk'></sub></form><legend id='k8TZk'></legend><bdo id='k8TZk'><pre id='k8TZk'><center id='k8TZk'></center></pre></bdo></b><th id='k8TZk'></th></span></q></dt></tr></i><div id='k8TZk'><tfoot id='k8TZk'></tfoot><dl id='k8TZk'><fieldset id='k8TZk'></fieldset></dl></div>