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, GL_TRUE
);
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());
116 EXPECT_EQ(GL_TRUE
, attrib
->integer());
118 // The VertexAttribManager must be destroyed before the BufferManager
119 // so it releases its buffers.
121 buffer_manager
.Destroy(false);
124 TEST_F(VertexAttribManagerTest
, HaveFixedAttribs
) {
125 EXPECT_FALSE(manager_
->HaveFixedAttribs());
126 manager_
->SetAttribInfo(1, NULL
, 4, GL_FIXED
, GL_FALSE
, 0, 16, 0, GL_FALSE
);
127 EXPECT_TRUE(manager_
->HaveFixedAttribs());
128 manager_
->SetAttribInfo(3, NULL
, 4, GL_FIXED
, GL_FALSE
, 0, 16, 0, GL_FALSE
);
129 EXPECT_TRUE(manager_
->HaveFixedAttribs());
130 manager_
->SetAttribInfo(1, NULL
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0, GL_FALSE
);
131 EXPECT_TRUE(manager_
->HaveFixedAttribs());
132 manager_
->SetAttribInfo(3, NULL
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0, GL_FALSE
);
133 EXPECT_FALSE(manager_
->HaveFixedAttribs());
136 TEST_F(VertexAttribManagerTest
, CanAccess
) {
137 const GLenum kTarget
= GL_ARRAY_BUFFER
;
138 MockErrorState error_state
;
139 BufferManager
buffer_manager(NULL
, NULL
);
140 buffer_manager
.CreateBuffer(1, 2);
141 Buffer
* buffer
= buffer_manager
.GetBuffer(1);
142 ASSERT_TRUE(buffer
!= NULL
);
144 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(1);
146 EXPECT_TRUE(attrib
->CanAccess(0));
147 manager_
->Enable(1, true);
148 EXPECT_FALSE(attrib
->CanAccess(0));
150 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0, GL_FALSE
);
151 EXPECT_FALSE(attrib
->CanAccess(0));
153 EXPECT_TRUE(buffer_manager
.SetTarget(buffer
, kTarget
));
154 TestHelper::DoBufferData(
155 gl_
.get(), &error_state
, &buffer_manager
, buffer
,
156 kTarget
, 15, GL_STATIC_DRAW
, NULL
, GL_NO_ERROR
);
158 EXPECT_FALSE(attrib
->CanAccess(0));
159 TestHelper::DoBufferData(
160 gl_
.get(), &error_state
, &buffer_manager
, buffer
,
161 kTarget
, 16, GL_STATIC_DRAW
, NULL
, GL_NO_ERROR
);
162 EXPECT_TRUE(attrib
->CanAccess(0));
163 EXPECT_FALSE(attrib
->CanAccess(1));
165 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 1, GL_FALSE
);
166 EXPECT_FALSE(attrib
->CanAccess(0));
168 TestHelper::DoBufferData(
169 gl_
.get(), &error_state
, &buffer_manager
, buffer
,
170 kTarget
, 32, GL_STATIC_DRAW
, NULL
, GL_NO_ERROR
);
171 EXPECT_TRUE(attrib
->CanAccess(0));
172 EXPECT_FALSE(attrib
->CanAccess(1));
173 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0, GL_FALSE
);
174 EXPECT_TRUE(attrib
->CanAccess(1));
175 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 20, 0, GL_FALSE
);
176 EXPECT_TRUE(attrib
->CanAccess(0));
177 EXPECT_FALSE(attrib
->CanAccess(1));
179 // The VertexAttribManager must be destroyed before the BufferManager
180 // so it releases its buffers.
182 buffer_manager
.Destroy(false);
185 TEST_F(VertexAttribManagerTest
, Unbind
) {
186 BufferManager
buffer_manager(NULL
, NULL
);
187 buffer_manager
.CreateBuffer(1, 2);
188 buffer_manager
.CreateBuffer(3, 4);
189 Buffer
* buffer1
= buffer_manager
.GetBuffer(1);
190 Buffer
* buffer2
= buffer_manager
.GetBuffer(3);
191 ASSERT_TRUE(buffer1
!= NULL
);
192 ASSERT_TRUE(buffer2
!= NULL
);
194 VertexAttrib
* attrib1
= manager_
->GetVertexAttrib(1);
195 VertexAttrib
* attrib3
= manager_
->GetVertexAttrib(3);
197 // Attach to 2 buffers.
198 manager_
->SetAttribInfo(
199 1, buffer1
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4, GL_FALSE
);
200 manager_
->SetAttribInfo(
201 3, buffer1
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4, GL_FALSE
);
202 // Check they were attached.
203 EXPECT_EQ(buffer1
, attrib1
->buffer());
204 EXPECT_EQ(buffer1
, attrib3
->buffer());
205 // Unbind unattached buffer.
206 manager_
->Unbind(buffer2
);
208 EXPECT_EQ(buffer1
, attrib1
->buffer());
209 EXPECT_EQ(buffer1
, attrib3
->buffer());
211 manager_
->Unbind(buffer1
);
212 // Check they were detached
213 EXPECT_TRUE(NULL
== attrib1
->buffer());
214 EXPECT_TRUE(NULL
== attrib3
->buffer());
216 // The VertexAttribManager must be destroyed before the BufferManager
217 // so it releases its buffers.
219 buffer_manager
.Destroy(false);
222 // TODO(gman): Test ValidateBindings
223 // TODO(gman): Test ValidateBindings with client side arrays.