1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278

gpu / command_buffer / client / vertex_array_object_manager_unittest.cc [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "gpu/command_buffer/client/vertex_array_object_manager.h"

#include <GLES2/gl2ext.h>
#include <GLES3/gl3.h>
#include <stddef.h>
#include <stdint.h>

#include <memory>

#include "testing/gtest/include/gtest/gtest.h"

namespace gpu {
namespace gles2 {

class VertexArrayObjectManagerTest : public testing::Test {
 protected:
  static const GLuint kMaxAttribs = 4;
  static const GLuint kClientSideArrayBuffer = 0x1234;
  static const GLuint kClientSideElementArrayBuffer = 0x1235;
  static const bool kSupportClientSideArrays = true;

  void SetUp() override {
    manager_.reset(new VertexArrayObjectManager(
        kMaxAttribs,
        kClientSideArrayBuffer,
        kClientSideElementArrayBuffer,
        kSupportClientSideArrays));
  }
  void TearDown() override {}

  std::unique_ptr<VertexArrayObjectManager> manager_;
};

const GLuint VertexArrayObjectManagerTest::kMaxAttribs;
const GLuint VertexArrayObjectManagerTest::kClientSideArrayBuffer;
const GLuint VertexArrayObjectManagerTest::kClientSideElementArrayBuffer;

TEST_F(VertexArrayObjectManagerTest, Basic) {
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
  // Check out of bounds access.
  uint32_t param;
  void* ptr;
  EXPECT_FALSE(manager_->GetVertexAttrib(
      kMaxAttribs, GL_VERTEX_ATTRIB_ARRAY_ENABLED, ¶m));
  EXPECT_FALSE(manager_->GetAttribPointer(
      kMaxAttribs, GL_VERTEX_ATTRIB_ARRAY_POINTER, &ptr));
  // Check defaults.
  for (GLuint ii = 0; ii < kMaxAttribs; ++ii) {
    EXPECT_TRUE(manager_->GetVertexAttrib(
        ii, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, ¶m));
    EXPECT_EQ(0u, param);
    EXPECT_TRUE(manager_->GetVertexAttrib(
        ii, GL_VERTEX_ATTRIB_ARRAY_ENABLED, ¶m));
    EXPECT_EQ(0u, param);
    EXPECT_TRUE(manager_->GetVertexAttrib(
        ii, GL_VERTEX_ATTRIB_ARRAY_SIZE, ¶m));
    EXPECT_EQ(4u, param);
    EXPECT_TRUE(manager_->GetVertexAttrib(
        ii, GL_VERTEX_ATTRIB_ARRAY_TYPE, ¶m));
    EXPECT_EQ(static_cast<uint32_t>(GL_FLOAT), param);
    EXPECT_TRUE(manager_->GetVertexAttrib(
        ii, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, ¶m));
    EXPECT_EQ(0u, param);
    EXPECT_TRUE(manager_->GetAttribPointer(
        ii, GL_VERTEX_ATTRIB_ARRAY_POINTER, &ptr));
    EXPECT_TRUE(nullptr == ptr);
  }
}

TEST_F(VertexArrayObjectManagerTest, UnbindBuffer) {
  const GLuint kBufferToUnbind = 123;
  const GLuint kBufferToRemain = 456;
  const GLuint kElementArray = 789;
  bool changed = false;
  GLuint ids[2] = { 1, 3, };
  manager_->GenVertexArrays(std::size(ids), ids);
  // Bind buffers to attribs on 2 vaos.
  for (size_t ii = 0; ii < std::size(ids); ++ii) {
    EXPECT_TRUE(manager_->BindVertexArray(ids[ii], &changed));
    EXPECT_TRUE(manager_->SetAttribPointer(
        kBufferToUnbind, 0, 4, GL_FLOAT, false, 0, 0, GL_FALSE));
    EXPECT_TRUE(manager_->SetAttribPointer(
        kBufferToRemain, 1, 4, GL_FLOAT, false, 0, 0, GL_FALSE));
    EXPECT_TRUE(manager_->SetAttribPointer(
        kBufferToUnbind, 2, 4, GL_FLOAT, false, 0, 0, GL_FALSE));
    EXPECT_TRUE(manager_->SetAttribPointer(
        kBufferToRemain, 3, 4, GL_FLOAT, false, 0, 0, GL_FALSE));
    for (size_t jj = 0; jj < 4u; ++jj) {
      manager_->SetAttribEnable(jj, true);
    }
    manager_->BindElementArray(kElementArray);
  }
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
  EXPECT_TRUE(manager_->BindVertexArray(ids[0], &changed));
  // Unbind the buffer.
  manager_->UnbindBuffer(kBufferToUnbind);
  manager_->UnbindBuffer(kElementArray);
  // The attribs are still enabled but their buffer is 0.
  EXPECT_TRUE(manager_->HaveEnabledClientSideBuffers());
  // Check the status of the bindings.
  static const uint32_t expected[][4] = {
      {
          0, kBufferToRemain, 0, kBufferToRemain,
      },
      {
          kBufferToUnbind, kBufferToRemain, kBufferToUnbind, kBufferToRemain,
      },
  };
  static const GLuint expected_element_array[] = {
    0, kElementArray,
  };
  for (size_t ii = 0; ii < std::size(ids); ++ii) {
    EXPECT_TRUE(manager_->BindVertexArray(ids[ii], &changed));
    for (size_t jj = 0; jj < 4; ++jj) {
      uint32_t param = 1;
      EXPECT_TRUE(manager_->GetVertexAttrib(
          jj, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, ¶m));
      EXPECT_EQ(expected[ii][jj], param)
          << "id: " << ids[ii] << ", attrib: " << jj;
    }
    EXPECT_EQ(expected_element_array[ii],
              manager_->bound_element_array_buffer());
  }

  // The vao that was not bound still has all service side bufferws.
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());

  // Make sure unbinding 0 does not effect count incorrectly.
  EXPECT_TRUE(manager_->BindVertexArray(0, &changed));
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
  manager_->SetAttribEnable(2, true);
  manager_->UnbindBuffer(0);
  manager_->SetAttribEnable(2, false);
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
}

TEST_F(VertexArrayObjectManagerTest, GetSet) {
  const char* dummy = "dummy";
  const void* p = reinterpret_cast<const void*>(dummy);
  manager_->SetAttribEnable(1, true);
  manager_->SetAttribPointer(123, 1, 3, GL_BYTE, true, 3, p, GL_TRUE);
  uint32_t param;
  void* ptr;
  EXPECT_TRUE(manager_->GetVertexAttrib(
      1, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, ¶m));
  EXPECT_EQ(123u, param);
  EXPECT_TRUE(manager_->GetVertexAttrib(
      1, GL_VERTEX_ATTRIB_ARRAY_ENABLED, ¶m));
  EXPECT_NE(0u, param);
  EXPECT_TRUE(manager_->GetVertexAttrib(
      1, GL_VERTEX_ATTRIB_ARRAY_SIZE, ¶m));
  EXPECT_EQ(3u, param);
  EXPECT_TRUE(manager_->GetVertexAttrib(
      1, GL_VERTEX_ATTRIB_ARRAY_TYPE, ¶m));
  EXPECT_EQ(static_cast<uint32_t>(GL_BYTE), param);
  EXPECT_TRUE(manager_->GetVertexAttrib(
      1, GL_VERTEX_ATTRIB_ARRAY_NORMALIZED, ¶m));
  EXPECT_NE(0u, param);
  EXPECT_TRUE(manager_->GetVertexAttrib(
      1, GL_VERTEX_ATTRIB_ARRAY_INTEGER, ¶m));
  EXPECT_EQ(1u, param);
  EXPECT_TRUE(manager_->GetAttribPointer(
      1, GL_VERTEX_ATTRIB_ARRAY_POINTER, &ptr));
  EXPECT_EQ(p, ptr);

  // Check that getting the divisor is passed to the service.
  // This is because the divisor is an optional feature which
  // only the service can validate.
  EXPECT_FALSE(manager_->GetVertexAttrib(
      0, GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE, ¶m));
}

TEST_F(VertexArrayObjectManagerTest, HaveEnabledClientSideArrays) {
  // Check turning on an array.
  manager_->SetAttribEnable(1, true);
  EXPECT_TRUE(manager_->HaveEnabledClientSideBuffers());
  // Check turning off an array.
  manager_->SetAttribEnable(1, false);
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
  // Check turning on an array and assigning a buffer.
  manager_->SetAttribEnable(1, true);
  manager_->SetAttribPointer(123, 1, 3, GL_BYTE, true, 3, nullptr, GL_FALSE);
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
  // Check unassigning a buffer.
  manager_->SetAttribPointer(0, 1, 3, GL_BYTE, true, 3, nullptr, GL_FALSE);
  EXPECT_TRUE(manager_->HaveEnabledClientSideBuffers());
  // Check disabling the array.
  manager_->SetAttribEnable(1, false);
  EXPECT_FALSE(manager_->HaveEnabledClientSideBuffers());
}

TEST_F(VertexArrayObjectManagerTest, BindElementArray) {
  bool changed = false;
  GLuint ids[2] = { 1, 3, };
  manager_->GenVertexArrays(std::size(ids), ids);

  // Check the default element array is 0.
  EXPECT_EQ(0u, manager_->bound_element_array_buffer());
  // Check binding the same array does not need a service call.
  EXPECT_FALSE(manager_->BindElementArray(0u));
  // Check binding a new element array requires a service call.
  EXPECT_TRUE(manager_->BindElementArray(55u));
  // Check the element array was bound.
  EXPECT_EQ(55u, manager_->bound_element_array_buffer());
  // Check binding the same array does not need a service call.
  EXPECT_FALSE(manager_->BindElementArray(55u));

  // Check with a new vao.
  EXPECT_TRUE(manager_->BindVertexArray(1, &changed));
  // Check the default element array is 0.
  EXPECT_EQ(0u, manager_->bound_element_array_buffer());
  // Check binding a new element array requires a service call.
  EXPECT_TRUE(manager_->BindElementArray(11u));
  // Check the element array was bound.
  EXPECT_EQ(11u, manager_->bound_element_array_buffer());
  // Check binding the same array does not need a service call.
  EXPECT_FALSE(manager_->BindElementArray(11u));

  // check switching vao bindings returns the correct element array.
  EXPECT_TRUE(manager_->BindVertexArray(3, &changed));
  EXPECT_EQ(0u, manager_->bound_element_array_buffer());
  EXPECT_TRUE(manager_->BindVertexArray(0, &changed));
  EXPECT_EQ(55u, manager_->bound_element_array_buffer());
  EXPECT_TRUE(manager_->BindVertexArray(1, &changed));
  EXPECT_EQ(11u, manager_->bound_element_array_buffer());
}

TEST_F(VertexArrayObjectManagerTest, GenBindDelete) {
  // Check unknown array fails.
  bool changed = false;
  EXPECT_FALSE(manager_->BindVertexArray(123, &changed));
  EXPECT_FALSE(changed);

  GLuint ids[2] = { 1, 3, };
  manager_->GenVertexArrays(std::size(ids), ids);
  // Check Genned arrays succeed.
  EXPECT_TRUE(manager_->BindVertexArray(1, &changed));
  EXPECT_TRUE(changed);
  EXPECT_TRUE(manager_->BindVertexArray(3, &changed));
  EXPECT_TRUE(changed);

  // Check binding the same array returns changed as false.
  EXPECT_TRUE(manager_->BindVertexArray(3, &changed));
  EXPECT_FALSE(changed);

  // Check deleted ararys fail to bind
  manager_->DeleteVertexArrays(2, ids);
  EXPECT_FALSE(manager_->BindVertexArray(1, &changed));
  EXPECT_FALSE(changed);
  EXPECT_FALSE(manager_->BindVertexArray(3, &changed));
  EXPECT_FALSE(changed);

  // Check binding 0 returns changed as false since it's
  // already bound.
  EXPECT_TRUE(manager_->BindVertexArray(0, &changed));
  EXPECT_FALSE(changed);
}

TEST_F(VertexArrayObjectManagerTest, IsReservedId) {
  EXPECT_TRUE(manager_->IsReservedId(kClientSideArrayBuffer));
  EXPECT_TRUE(manager_->IsReservedId(kClientSideElementArrayBuffer));
  EXPECT_FALSE(manager_->IsReservedId(0));
  EXPECT_FALSE(manager_->IsReservedId(1));
  EXPECT_FALSE(manager_->IsReservedId(2));
}

}  // namespace gles2
}  // namespace gpu