src/third_party/angle/src/tests/gl_tests/DrawElementsTest.cpp
// Test drawing to part of the indices in an index buffer, and then all of them.
Test drawing to part of the indices in an index buffer, and then all of them.
Test drawing to part of the indices in an index buffer and then all of them .
TEST_P(DrawElementsTest, PartOfIndexBufferThenAll)
{
    constexpr char kVS[] =
        "attribute vec2 position;\n"
        "attribute vec2 testFlag;\n"
        "varying vec2 v_data;\n"
        "void main() {\n"
        "  gl_Position = vec4(position, 0, 1);\n"
        "  v_data = testFlag;\n"
        "}";
    constexpr char kFS[] =
        "varying highp vec2 v_data;\n"
        "void main() {\n"
        "  gl_FragColor = vec4(v_data, 0, 1);\n"
        "}";
    mProgram = CompileProgram(kVS, kFS);
    ASSERT_NE(0u, mProgram);
    glUseProgram(mProgram);
    GLint positionLocation = glGetAttribLocation(mProgram, "position");
    ASSERT_NE(-1, positionLocation);
    GLint testFlagLocation = glGetAttribLocation(mProgram, "testFlag");
    ASSERT_NE(-1, testFlagLocation);
    mIndexBuffers.resize(1);
    glGenBuffers(1, &mIndexBuffers[0]);
    mVertexArrays.resize(1);
    glGenVertexArrays(1, &mVertexArrays[0]);
    mVertexBuffers.resize(2);
    glGenBuffers(2, &mVertexBuffers[0]);
    std::vector<GLubyte> indexData[2];
    indexData[0].push_back(0);
    indexData[0].push_back(1);
    indexData[0].push_back(2);
    indexData[0].push_back(2);
    indexData[0].push_back(3);
    indexData[0].push_back(0);
    indexData[0].push_back(4);
    indexData[0].push_back(5);
    indexData[0].push_back(6);
    indexData[0].push_back(6);
    indexData[0].push_back(7);
    indexData[0].push_back(4);
    indexData[1] = indexData[0];
    std::vector<GLfloat> positionData = {
                                         -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
                                         -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f};
    std::vector<GLfloat> testFlagData = {
                                         1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
                                         0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f};
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffers[0]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indexData[0].size(), &indexData[0][0],
                 GL_STATIC_DRAW);
    glBindVertexArray(mVertexArrays[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffers[0]);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffers[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * positionData.size(), &positionData[0],
                 GL_STATIC_DRAW);
    glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, nullptr);
    glEnableVertexAttribArray(positionLocation);
    glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffers[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * testFlagData.size(), &testFlagData[0],
                 GL_STATIC_DRAW);
    glVertexAttribPointer(testFlagLocation, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 2, nullptr);
    glEnableVertexAttribArray(testFlagLocation);
    ASSERT_GL_NO_ERROR();
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, reinterpret_cast<const void *>(6));
    EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, nullptr);
    EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
    glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, nullptr);
    EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indexData[1].size(), &indexData[1][0],
                 GL_STATIC_DRAW);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, nullptr);
    EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
    glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, nullptr);
    EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * indexData[0].size(), &indexData[0][0],
                 GL_STATIC_DRAW);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, reinterpret_cast<const void *>(6));
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, nullptr);
    glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, nullptr);
    EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
    ASSERT_GL_NO_ERROR();
}
TEST_P ( DrawElementsTest , PartOfIndexBufferThenAll ) { constexpr char kVS [ ] = " \n " " \n " " \n " " \n " " \n " " \n " " " ; constexpr char kFS [ ] = " \n " " \n " " \n " " " ; mProgram = CompileProgram ( kVS , kFS ) ; ASSERT_NE ( 0u , mProgram ) ; glUseProgram ( mProgram ) ; GLint positionLocation = glGetAttribLocation ( mProgram , " " ) ; ASSERT_NE ( -1 , positionLocation ) ; GLint testFlagLocation = glGetAttribLocation ( mProgram , " " ) ; ASSERT_NE ( -1 , testFlagLocation ) ; mIndexBuffers . resize ( 1 ) ; glGenBuffers ( 1 , & mIndexBuffers [ 0 ] ) ; mVertexArrays . resize ( 1 ) ; glGenVertexArrays ( 1 , & mVertexArrays [ 0 ] ) ; mVertexBuffers . resize ( 2 ) ; glGenBuffers ( 2 , & mVertexBuffers [ 0 ] ) ; std :: vector < GLubyte > indexData [ 2 ] ; indexData [ 0 ] . push_back ( 0 ) ; indexData [ 0 ] . push_back ( 1 ) ; indexData [ 0 ] . push_back ( 2 ) ; indexData [ 0 ] . push_back ( 2 ) ; indexData [ 0 ] . push_back ( 3 ) ; indexData [ 0 ] . push_back ( 0 ) ; indexData [ 0 ] . push_back ( 4 ) ; indexData [ 0 ] . push_back ( 5 ) ; indexData [ 0 ] . push_back ( 6 ) ; indexData [ 0 ] . push_back ( 6 ) ; indexData [ 0 ] . push_back ( 7 ) ; indexData [ 0 ] . push_back ( 4 ) ; indexData [ 1 ] = indexData [ 0 ] ; std :: vector < GLfloat > positionData = { -1.0f , 1.0f , -1.0f , -1.0f , 1.0f , -1.0f , 1.0f , 1.0f , -1.0f , 1.0f , -1.0f , -1.0f , 1.0f , -1.0f , 1.0f , 1.0f } ; std :: vector < GLfloat > testFlagData = { 1.0f , 0.0f , 1.0f , 0.0f , 1.0f , 0.0f , 1.0f , 0.0f , 0.0f , 1.0f , 0.0f , 1.0f , 0.0f , 1.0f , 0.0f , 1.0f } ; glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , mIndexBuffers [ 0 ] ) ; glBufferData ( GL_ELEMENT_ARRAY_BUFFER , sizeof ( GLubyte ) * indexData [ 0 ] . size ( ) , & indexData [ 0 ] [ 0 ] , GL_STATIC_DRAW ) ; glBindVertexArray ( mVertexArrays [ 0 ] ) ; glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER , mIndexBuffers [ 0 ] ) ; glBindBuffer ( GL_ARRAY_BUFFER , mVertexBuffers [ 0 ] ) ; glBufferData ( GL_ARRAY_BUFFER , sizeof ( GLfloat ) * positionData . size ( ) , & positionData [ 0 ] , GL_STATIC_DRAW ) ; glVertexAttribPointer ( positionLocation , 2 , GL_FLOAT , GL_FALSE , sizeof ( GLfloat ) * 2 , nullptr ) ; glEnableVertexAttribArray ( positionLocation ) ; glBindBuffer ( GL_ARRAY_BUFFER , mVertexBuffers [ 1 ] ) ; glBufferData ( GL_ARRAY_BUFFER , sizeof ( GLfloat ) * testFlagData . size ( ) , & testFlagData [ 0 ] , GL_STATIC_DRAW ) ; glVertexAttribPointer ( testFlagLocation , 2 , GL_FLOAT , GL_FALSE , sizeof ( GLfloat ) * 2 , nullptr ) ; glEnableVertexAttribArray ( testFlagLocation ) ; ASSERT_GL_NO_ERROR ( ) ; glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_BYTE , reinterpret_cast < const void * > ( 6 ) ) ; EXPECT_PIXEL_COLOR_EQ ( 0 , 0 , GLColor :: green ) ; glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_BYTE , nullptr ) ; EXPECT_PIXEL_COLOR_EQ ( 0 , 0 , GLColor :: red ) ; glDrawElements ( GL_TRIANGLES , 12 , GL_UNSIGNED_BYTE , nullptr ) ; EXPECT_PIXEL_COLOR_EQ ( 0 , 0 , GLColor :: green ) ; glBufferData ( GL_ELEMENT_ARRAY_BUFFER , sizeof ( GLubyte ) * indexData [ 1 ] . size ( ) , & indexData [ 1 ] [ 0 ] , GL_STATIC_DRAW ) ; glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_BYTE , nullptr ) ; EXPECT_PIXEL_COLOR_EQ ( 0 , 0 , GLColor :: red ) ; glDrawElements ( GL_TRIANGLES , 12 , GL_UNSIGNED_BYTE , nullptr ) ; EXPECT_PIXEL_COLOR_EQ ( 0 , 0 , GLColor :: green ) ; glBufferData ( GL_ELEMENT_ARRAY_BUFFER , sizeof ( GLubyte ) * indexData [ 0 ] . size ( ) , & indexData [ 0 ] [ 0 ] , GL_STATIC_DRAW ) ; glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_BYTE , reinterpret_cast < const void * > ( 6 ) ) ; glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_BYTE , nullptr ) ; glDrawElements ( GL_TRIANGLES , 12 , GL_UNSIGNED_BYTE , nullptr ) ; EXPECT_PIXEL_COLOR_EQ ( 0 , 0 , GLColor :: green ) ; ASSERT_GL_NO_ERROR ( ) ; }
Test drawing to part of the indices in an index buffer, and then all of them.
Test drawing to part of the indices in an index buffer and then all of them .
// Init program // Make a copy: // quad verts // Repeat position data // red // green // Draw with just the second set of 6 items, then first 6, and then the entire index buffer // Reload the buffer again with a copy of the same data // Draw with just the first 6 indices, and then with the entire index buffer // Reload the buffer again with a copy of the same data // This time, do not check color between draws (which causes a flush): // Draw with just the second set of 6 items, then first 6, and then the entire index buffer