src/third_party/angle/src/tests/gl_tests/UniformBufferTest.cpp
// Test two uniform blocks with large structure array member are in the same program, and they
// share the same uniform buffer.
Test two uniform blocks with large structure array member are in the same program, and they
share the same uniform buffer.
Test two uniform blocks with large structure array member are in the same program and they share the same uniform buffer .
TEST_P(UniformBlockWithOneLargeArrayMemberTest, TwoUniformBlocksInSameProgram)
{
    std::ostringstream stream;
    generateArraySizeAndDivisorsDeclaration(stream, true, true, true);
    const std::string &kFS =
        "#version 300 es\n"
        "precision highp float;\n" +
        stream.str() +
        "out vec4 my_FragColor;\n"
        "struct S { mat4 color;};\n"
        "layout(std140) uniform buffer1 { S s1[arraySize1]; };\n"
        "layout(std140) uniform buffer2 { S s2[arraySize2]; };\n"
        "void main()\n"
        "{\n"
        "    uvec2 coord = uvec2(floor(gl_FragCoord.xy));\n"
        "    uint index = coord.x +  coord.y * 128u;\n"
        "    uint index_x1 = index / divisor1;\n"
        "    uint index_y1 = (index % divisor1) / divisor2;\n"
        "    uint index_x2 = coord.x / divisor3;\n"
        "    uint index_y2 = coord.x % 4u;\n"
        "    my_FragColor = s1[index_x1].color[index_y1] + s2[index_x2].color[index_y2];\n"
        "}\n";
    GLint blockSize1, blockSize2;
    ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS.c_str());
    GLint uniformBufferIndex1 = glGetUniformBlockIndex(program, "buffer1");
    GLint uniformBufferIndex2 = glGetUniformBlockIndex(program, "buffer2");
    glGetActiveUniformBlockiv(program, uniformBufferIndex1, GL_UNIFORM_BLOCK_DATA_SIZE,
                              &blockSize1);
    glGetActiveUniformBlockiv(program, uniformBufferIndex2, GL_UNIFORM_BLOCK_DATA_SIZE,
                              &blockSize2);
    glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
    glBufferData(GL_UNIFORM_BUFFER, blockSize1 + blockSize2, nullptr, GL_STATIC_DRAW);
    glBindBufferRange(GL_UNIFORM_BUFFER, 0, mUniformBuffer, 0, blockSize2);
    glUniformBlockBinding(program, uniformBufferIndex2, 0);
    glBindBufferRange(GL_UNIFORM_BUFFER, 1, mUniformBuffer, blockSize2, blockSize1);
    glUniformBlockBinding(program, uniformBufferIndex1, 1);
    const GLuint arraySize1  = getArraySize();
    const GLuint arraySize2  = getArraySize2();
    const GLuint floatCount1 = arraySize1 * kVectorPerMat * kFloatPerVector;
    const GLuint floatCount2 = arraySize2 * kVectorPerMat * kFloatPerVector;
    const GLuint floatCount  = floatCount1 + floatCount2;
    std::vector<GLfloat> floatData(floatCount, 0.0f);
    setArrayValues(floatData, arraySize2, arraySize1 + arraySize2, 16, 0, 4, 4, 0.0f, 0.0f, 1.0f,
                   1.0f);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, floatCount2 * sizeof(GLfloat), &floatData[0]);
    glBufferSubData(GL_UNIFORM_BUFFER, blockSize2, floatCount1 * sizeof(GLfloat),
                    &floatData[floatCount2]);
    drawQuad(program.get(), essl3_shaders::PositionAttrib(), 0.5f);
    checkResults(GLColor::blue, GLColor::blue, GLColor::blue, GLColor::blue);
    setArrayValues(floatData, 0, arraySize2, 16, 0, 4, 4, 0.0f, 1.0f, 0.0f, 0.0f);
    setArrayValues(floatData, arraySize1 / 4, arraySize2 + arraySize1 / 2, 16, 0, 4, 4, 1.0f, 0.0f,
                   0.0f, 1.0f);
    glBufferSubData(GL_UNIFORM_BUFFER, 0, floatCount2 * sizeof(GLfloat), &floatData[0]);
    glBufferSubData(GL_UNIFORM_BUFFER, blockSize2 + floatCount1 * sizeof(GLfloat) / 4,
                    floatCount1 * sizeof(GLfloat) / 4, &floatData[floatCount2 + floatCount1 / 4]);
    drawQuad(program.get(), essl3_shaders::PositionAttrib(), 0.5f);
    checkResults(GLColor::cyan, GLColor::yellow, GLColor::cyan, GLColor::cyan);
}
TEST_P ( UniformBlockWithOneLargeArrayMemberTest , TwoUniformBlocksInSameProgram ) { std :: ostringstream stream ; generateArraySizeAndDivisorsDeclaration ( stream , true , true , true ) ; const std :: string & kFS = " \n " " \n " + stream . str ( ) + " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " " \n " ; GLint blockSize1 , blockSize2 ; ANGLE_GL_PROGRAM ( program , essl3_shaders :: vs :: Simple ( ) , kFS . c_str ( ) ) ; GLint uniformBufferIndex1 = glGetUniformBlockIndex ( program , " " ) ; GLint uniformBufferIndex2 = glGetUniformBlockIndex ( program , " " ) ; glGetActiveUniformBlockiv ( program , uniformBufferIndex1 , GL_UNIFORM_BLOCK_DATA_SIZE , & blockSize1 ) ; glGetActiveUniformBlockiv ( program , uniformBufferIndex2 , GL_UNIFORM_BLOCK_DATA_SIZE , & blockSize2 ) ; glBindBuffer ( GL_UNIFORM_BUFFER , mUniformBuffer ) ; glBufferData ( GL_UNIFORM_BUFFER , blockSize1 + blockSize2 , nullptr , GL_STATIC_DRAW ) ; glBindBufferRange ( GL_UNIFORM_BUFFER , 0 , mUniformBuffer , 0 , blockSize2 ) ; glUniformBlockBinding ( program , uniformBufferIndex2 , 0 ) ; glBindBufferRange ( GL_UNIFORM_BUFFER , 1 , mUniformBuffer , blockSize2 , blockSize1 ) ; glUniformBlockBinding ( program , uniformBufferIndex1 , 1 ) ; const GLuint arraySize1 = getArraySize ( ) ; const GLuint arraySize2 = getArraySize2 ( ) ; const GLuint floatCount1 = arraySize1 * kVectorPerMat * kFloatPerVector ; const GLuint floatCount2 = arraySize2 * kVectorPerMat * kFloatPerVector ; const GLuint floatCount = floatCount1 + floatCount2 ; std :: vector < GLfloat > floatData ( floatCount , 0.0f ) ; setArrayValues ( floatData , arraySize2 , arraySize1 + arraySize2 , 16 , 0 , 4 , 4 , 0.0f , 0.0f , 1.0f , 1.0f ) ; glBufferSubData ( GL_UNIFORM_BUFFER , 0 , floatCount2 * sizeof ( GLfloat ) , & floatData [ 0 ] ) ; glBufferSubData ( GL_UNIFORM_BUFFER , blockSize2 , floatCount1 * sizeof ( GLfloat ) , & floatData [ floatCount2 ] ) ; drawQuad ( program . get ( ) , essl3_shaders :: PositionAttrib ( ) , 0.5f ) ; checkResults ( GLColor :: blue , GLColor :: blue , GLColor :: blue , GLColor :: blue ) ; setArrayValues ( floatData , 0 , arraySize2 , 16 , 0 , 4 , 4 , 0.0f , 1.0f , 0.0f , 0.0f ) ; setArrayValues ( floatData , arraySize1 / 4 , arraySize2 + arraySize1 / 2 , 16 , 0 , 4 , 4 , 1.0f , 0.0f , 0.0f , 1.0f ) ; glBufferSubData ( GL_UNIFORM_BUFFER , 0 , floatCount2 * sizeof ( GLfloat ) , & floatData [ 0 ] ) ; glBufferSubData ( GL_UNIFORM_BUFFER , blockSize2 + floatCount1 * sizeof ( GLfloat ) / 4 , floatCount1 * sizeof ( GLfloat ) / 4 , & floatData [ floatCount2 + floatCount1 / 4 ] ) ; drawQuad ( program . get ( ) , essl3_shaders :: PositionAttrib ( ) , 0.5f ) ; checkResults ( GLColor :: cyan , GLColor :: yellow , GLColor :: cyan , GLColor :: cyan ) ; }
Test two uniform blocks with large structure array member are in the same program, and they
share the same uniform buffer.
Test two uniform blocks with large structure array member are in the same program and they share the same uniform buffer .