1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gpu/command_buffer/service/vertex_attrib_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "gpu/command_buffer/service/buffer_manager.h"
9 #include "gpu/command_buffer/service/error_state_mock.h"
10 #include "gpu/command_buffer/service/feature_info.h"
11 #include "gpu/command_buffer/service/gpu_service_test.h"
12 #include "gpu/command_buffer/service/test_helper.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gl/gl_mock.h"
16 using ::testing::Pointee
;
22 class VertexAttribManagerTest
: public GpuServiceTest
{
24 static const uint32 kNumVertexAttribs
= 8;
26 VertexAttribManagerTest() {
29 ~VertexAttribManagerTest() override
{}
32 void SetUp() override
{
33 GpuServiceTest::SetUp();
35 for (uint32 ii
= 0; ii
< kNumVertexAttribs
; ++ii
) {
36 EXPECT_CALL(*gl_
, VertexAttrib4f(ii
, 0.0f
, 0.0f
, 0.0f
, 1.0f
))
38 .RetiresOnSaturation();
41 manager_
= new VertexAttribManager();
42 manager_
->Initialize(kNumVertexAttribs
, true);
45 scoped_refptr
<VertexAttribManager
> manager_
;
48 // GCC requires these declarations, but MSVC requires they not be present
50 const uint32
VertexAttribManagerTest::kNumVertexAttribs
;
53 TEST_F(VertexAttribManagerTest
, Basic
) {
54 EXPECT_TRUE(manager_
->GetVertexAttrib(kNumVertexAttribs
) == NULL
);
55 EXPECT_FALSE(manager_
->HaveFixedAttribs());
57 const VertexAttribManager::VertexAttribList
& enabled_attribs
=
58 manager_
->GetEnabledVertexAttribs();
59 EXPECT_EQ(0u, enabled_attribs
.size());
61 for (uint32 ii
= 0; ii
< kNumVertexAttribs
; ii
+= kNumVertexAttribs
- 1) {
62 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(ii
);
63 ASSERT_TRUE(attrib
!= NULL
);
64 EXPECT_EQ(ii
, attrib
->index());
65 EXPECT_TRUE(attrib
->buffer() == NULL
);
66 EXPECT_EQ(0, attrib
->offset());
67 EXPECT_EQ(4, attrib
->size());
68 EXPECT_EQ(static_cast<GLenum
>(GL_FLOAT
), attrib
->type());
69 EXPECT_EQ(GL_FALSE
, attrib
->normalized());
70 EXPECT_EQ(0, attrib
->gl_stride());
71 EXPECT_FALSE(attrib
->enabled());
72 manager_
->Enable(ii
, true);
73 EXPECT_TRUE(attrib
->enabled());
77 TEST_F(VertexAttribManagerTest
, Enable
) {
78 const VertexAttribManager::VertexAttribList
& enabled_attribs
=
79 manager_
->GetEnabledVertexAttribs();
81 VertexAttrib
* attrib1
= manager_
->GetVertexAttrib(1);
82 VertexAttrib
* attrib2
= manager_
->GetVertexAttrib(3);
84 manager_
->Enable(1, true);
85 ASSERT_EQ(1u, enabled_attribs
.size());
86 EXPECT_TRUE(attrib1
->enabled());
87 manager_
->Enable(3, true);
88 ASSERT_EQ(2u, enabled_attribs
.size());
89 EXPECT_TRUE(attrib2
->enabled());
91 manager_
->Enable(1, false);
92 ASSERT_EQ(1u, enabled_attribs
.size());
93 EXPECT_FALSE(attrib1
->enabled());
95 manager_
->Enable(3, false);
96 ASSERT_EQ(0u, enabled_attribs
.size());
97 EXPECT_FALSE(attrib2
->enabled());
100 TEST_F(VertexAttribManagerTest
, SetAttribInfo
) {
101 BufferManager
buffer_manager(NULL
, NULL
);
102 buffer_manager
.CreateBuffer(1, 2);
103 Buffer
* buffer
= buffer_manager
.GetBuffer(1);
104 ASSERT_TRUE(buffer
!= NULL
);
106 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(1);
108 manager_
->SetAttribInfo(1, buffer
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4);
110 EXPECT_EQ(buffer
, attrib
->buffer());
111 EXPECT_EQ(4, attrib
->offset());
112 EXPECT_EQ(3, attrib
->size());
113 EXPECT_EQ(static_cast<GLenum
>(GL_SHORT
), attrib
->type());
114 EXPECT_EQ(GL_TRUE
, attrib
->normalized());
115 EXPECT_EQ(32, attrib
->gl_stride());
117 // The VertexAttribManager must be destroyed before the BufferManager
118 // so it releases its buffers.
120 buffer_manager
.Destroy(false);
123 TEST_F(VertexAttribManagerTest
, HaveFixedAttribs
) {
124 EXPECT_FALSE(manager_
->HaveFixedAttribs());
125 manager_
->SetAttribInfo(1, NULL
, 4, GL_FIXED
, GL_FALSE
, 0, 16, 0);
126 EXPECT_TRUE(manager_
->HaveFixedAttribs());
127 manager_
->SetAttribInfo(3, NULL
, 4, GL_FIXED
, GL_FALSE
, 0, 16, 0);
128 EXPECT_TRUE(manager_
->HaveFixedAttribs());
129 manager_
->SetAttribInfo(1, NULL
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
130 EXPECT_TRUE(manager_
->HaveFixedAttribs());
131 manager_
->SetAttribInfo(3, NULL
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
132 EXPECT_FALSE(manager_
->HaveFixedAttribs());
135 TEST_F(VertexAttribManagerTest
, CanAccess
) {
136 MockErrorState error_state
;
137 BufferManager
buffer_manager(NULL
, NULL
);
138 buffer_manager
.CreateBuffer(1, 2);
139 Buffer
* buffer
= buffer_manager
.GetBuffer(1);
140 ASSERT_TRUE(buffer
!= NULL
);
142 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(1);
144 EXPECT_TRUE(attrib
->CanAccess(0));
145 manager_
->Enable(1, true);
146 EXPECT_FALSE(attrib
->CanAccess(0));
148 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
149 EXPECT_FALSE(attrib
->CanAccess(0));
151 EXPECT_TRUE(buffer_manager
.SetTarget(buffer
, GL_ARRAY_BUFFER
));
152 TestHelper::DoBufferData(
153 gl_
.get(), &error_state
, &buffer_manager
, buffer
, 15, GL_STATIC_DRAW
,
156 EXPECT_FALSE(attrib
->CanAccess(0));
157 TestHelper::DoBufferData(
158 gl_
.get(), &error_state
, &buffer_manager
, buffer
, 16, GL_STATIC_DRAW
,
160 EXPECT_TRUE(attrib
->CanAccess(0));
161 EXPECT_FALSE(attrib
->CanAccess(1));
163 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 1);
164 EXPECT_FALSE(attrib
->CanAccess(0));
166 TestHelper::DoBufferData(
167 gl_
.get(), &error_state
, &buffer_manager
, buffer
, 32, GL_STATIC_DRAW
,
169 EXPECT_TRUE(attrib
->CanAccess(0));
170 EXPECT_FALSE(attrib
->CanAccess(1));
171 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
172 EXPECT_TRUE(attrib
->CanAccess(1));
173 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 20, 0);
174 EXPECT_TRUE(attrib
->CanAccess(0));
175 EXPECT_FALSE(attrib
->CanAccess(1));
177 // The VertexAttribManager must be destroyed before the BufferManager
178 // so it releases its buffers.
180 buffer_manager
.Destroy(false);
183 TEST_F(VertexAttribManagerTest
, Unbind
) {
184 BufferManager
buffer_manager(NULL
, NULL
);
185 buffer_manager
.CreateBuffer(1, 2);
186 buffer_manager
.CreateBuffer(3, 4);
187 Buffer
* buffer1
= buffer_manager
.GetBuffer(1);
188 Buffer
* buffer2
= buffer_manager
.GetBuffer(3);
189 ASSERT_TRUE(buffer1
!= NULL
);
190 ASSERT_TRUE(buffer2
!= NULL
);
192 VertexAttrib
* attrib1
= manager_
->GetVertexAttrib(1);
193 VertexAttrib
* attrib3
= manager_
->GetVertexAttrib(3);
195 // Attach to 2 buffers.
196 manager_
->SetAttribInfo(1, buffer1
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4);
197 manager_
->SetAttribInfo(3, buffer1
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4);
198 // Check they were attached.
199 EXPECT_EQ(buffer1
, attrib1
->buffer());
200 EXPECT_EQ(buffer1
, attrib3
->buffer());
201 // Unbind unattached buffer.
202 manager_
->Unbind(buffer2
);
204 EXPECT_EQ(buffer1
, attrib1
->buffer());
205 EXPECT_EQ(buffer1
, attrib3
->buffer());
207 manager_
->Unbind(buffer1
);
208 // Check they were detached
209 EXPECT_TRUE(NULL
== attrib1
->buffer());
210 EXPECT_TRUE(NULL
== attrib3
->buffer());
212 // The VertexAttribManager must be destroyed before the BufferManager
213 // so it releases its buffers.
215 buffer_manager
.Destroy(false);
218 // TODO(gman): Test ValidateBindings
219 // TODO(gman): Test ValidateBindings with client side arrays.