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 virtual ~VertexAttribManagerTest() {
33 virtual void SetUp() {
34 GpuServiceTest::SetUp();
36 for (uint32 ii
= 0; ii
< kNumVertexAttribs
; ++ii
) {
37 EXPECT_CALL(*gl_
, VertexAttrib4f(ii
, 0.0f
, 0.0f
, 0.0f
, 1.0f
))
39 .RetiresOnSaturation();
42 manager_
= new VertexAttribManager();
43 manager_
->Initialize(kNumVertexAttribs
, true);
46 scoped_refptr
<VertexAttribManager
> manager_
;
49 // GCC requires these declarations, but MSVC requires they not be present
51 const uint32
VertexAttribManagerTest::kNumVertexAttribs
;
54 TEST_F(VertexAttribManagerTest
, Basic
) {
55 EXPECT_TRUE(manager_
->GetVertexAttrib(kNumVertexAttribs
) == NULL
);
56 EXPECT_FALSE(manager_
->HaveFixedAttribs());
58 const VertexAttribManager::VertexAttribList
& enabled_attribs
=
59 manager_
->GetEnabledVertexAttribs();
60 EXPECT_EQ(0u, enabled_attribs
.size());
62 for (uint32 ii
= 0; ii
< kNumVertexAttribs
; ii
+= kNumVertexAttribs
- 1) {
63 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(ii
);
64 ASSERT_TRUE(attrib
!= NULL
);
65 EXPECT_EQ(ii
, attrib
->index());
66 EXPECT_TRUE(attrib
->buffer() == NULL
);
67 EXPECT_EQ(0, attrib
->offset());
68 EXPECT_EQ(4, attrib
->size());
69 EXPECT_EQ(static_cast<GLenum
>(GL_FLOAT
), attrib
->type());
70 EXPECT_EQ(GL_FALSE
, attrib
->normalized());
71 EXPECT_EQ(0, attrib
->gl_stride());
72 EXPECT_FALSE(attrib
->enabled());
73 manager_
->Enable(ii
, true);
74 EXPECT_TRUE(attrib
->enabled());
78 TEST_F(VertexAttribManagerTest
, Enable
) {
79 const VertexAttribManager::VertexAttribList
& enabled_attribs
=
80 manager_
->GetEnabledVertexAttribs();
82 VertexAttrib
* attrib1
= manager_
->GetVertexAttrib(1);
83 VertexAttrib
* attrib2
= manager_
->GetVertexAttrib(3);
85 manager_
->Enable(1, true);
86 ASSERT_EQ(1u, enabled_attribs
.size());
87 EXPECT_TRUE(attrib1
->enabled());
88 manager_
->Enable(3, true);
89 ASSERT_EQ(2u, enabled_attribs
.size());
90 EXPECT_TRUE(attrib2
->enabled());
92 manager_
->Enable(1, false);
93 ASSERT_EQ(1u, enabled_attribs
.size());
94 EXPECT_FALSE(attrib1
->enabled());
96 manager_
->Enable(3, false);
97 ASSERT_EQ(0u, enabled_attribs
.size());
98 EXPECT_FALSE(attrib2
->enabled());
101 TEST_F(VertexAttribManagerTest
, SetAttribInfo
) {
102 BufferManager
buffer_manager(NULL
, NULL
);
103 buffer_manager
.CreateBuffer(1, 2);
104 Buffer
* buffer
= buffer_manager
.GetBuffer(1);
105 ASSERT_TRUE(buffer
!= NULL
);
107 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(1);
109 manager_
->SetAttribInfo(1, buffer
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4);
111 EXPECT_EQ(buffer
, attrib
->buffer());
112 EXPECT_EQ(4, attrib
->offset());
113 EXPECT_EQ(3, attrib
->size());
114 EXPECT_EQ(static_cast<GLenum
>(GL_SHORT
), attrib
->type());
115 EXPECT_EQ(GL_TRUE
, attrib
->normalized());
116 EXPECT_EQ(32, attrib
->gl_stride());
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);
127 EXPECT_TRUE(manager_
->HaveFixedAttribs());
128 manager_
->SetAttribInfo(3, NULL
, 4, GL_FIXED
, GL_FALSE
, 0, 16, 0);
129 EXPECT_TRUE(manager_
->HaveFixedAttribs());
130 manager_
->SetAttribInfo(1, NULL
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
131 EXPECT_TRUE(manager_
->HaveFixedAttribs());
132 manager_
->SetAttribInfo(3, NULL
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
133 EXPECT_FALSE(manager_
->HaveFixedAttribs());
136 TEST_F(VertexAttribManagerTest
, CanAccess
) {
137 MockErrorState error_state
;
138 BufferManager
buffer_manager(NULL
, NULL
);
139 buffer_manager
.CreateBuffer(1, 2);
140 Buffer
* buffer
= buffer_manager
.GetBuffer(1);
141 ASSERT_TRUE(buffer
!= NULL
);
143 VertexAttrib
* attrib
= manager_
->GetVertexAttrib(1);
145 EXPECT_TRUE(attrib
->CanAccess(0));
146 manager_
->Enable(1, true);
147 EXPECT_FALSE(attrib
->CanAccess(0));
149 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
150 EXPECT_FALSE(attrib
->CanAccess(0));
152 EXPECT_TRUE(buffer_manager
.SetTarget(buffer
, GL_ARRAY_BUFFER
));
153 TestHelper::DoBufferData(
154 gl_
.get(), &error_state
, &buffer_manager
, buffer
, 15, GL_STATIC_DRAW
,
157 EXPECT_FALSE(attrib
->CanAccess(0));
158 TestHelper::DoBufferData(
159 gl_
.get(), &error_state
, &buffer_manager
, buffer
, 16, GL_STATIC_DRAW
,
161 EXPECT_TRUE(attrib
->CanAccess(0));
162 EXPECT_FALSE(attrib
->CanAccess(1));
164 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 1);
165 EXPECT_FALSE(attrib
->CanAccess(0));
167 TestHelper::DoBufferData(
168 gl_
.get(), &error_state
, &buffer_manager
, buffer
, 32, GL_STATIC_DRAW
,
170 EXPECT_TRUE(attrib
->CanAccess(0));
171 EXPECT_FALSE(attrib
->CanAccess(1));
172 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 16, 0);
173 EXPECT_TRUE(attrib
->CanAccess(1));
174 manager_
->SetAttribInfo(1, buffer
, 4, GL_FLOAT
, GL_FALSE
, 0, 20, 0);
175 EXPECT_TRUE(attrib
->CanAccess(0));
176 EXPECT_FALSE(attrib
->CanAccess(1));
178 // The VertexAttribManager must be destroyed before the BufferManager
179 // so it releases its buffers.
181 buffer_manager
.Destroy(false);
184 TEST_F(VertexAttribManagerTest
, Unbind
) {
185 BufferManager
buffer_manager(NULL
, NULL
);
186 buffer_manager
.CreateBuffer(1, 2);
187 buffer_manager
.CreateBuffer(3, 4);
188 Buffer
* buffer1
= buffer_manager
.GetBuffer(1);
189 Buffer
* buffer2
= buffer_manager
.GetBuffer(3);
190 ASSERT_TRUE(buffer1
!= NULL
);
191 ASSERT_TRUE(buffer2
!= NULL
);
193 VertexAttrib
* attrib1
= manager_
->GetVertexAttrib(1);
194 VertexAttrib
* attrib3
= manager_
->GetVertexAttrib(3);
196 // Attach to 2 buffers.
197 manager_
->SetAttribInfo(1, buffer1
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4);
198 manager_
->SetAttribInfo(3, buffer1
, 3, GL_SHORT
, GL_TRUE
, 32, 32, 4);
199 // Check they were attached.
200 EXPECT_EQ(buffer1
, attrib1
->buffer());
201 EXPECT_EQ(buffer1
, attrib3
->buffer());
202 // Unbind unattached buffer.
203 manager_
->Unbind(buffer2
);
205 EXPECT_EQ(buffer1
, attrib1
->buffer());
206 EXPECT_EQ(buffer1
, attrib3
->buffer());
208 manager_
->Unbind(buffer1
);
209 // Check they were detached
210 EXPECT_TRUE(NULL
== attrib1
->buffer());
211 EXPECT_TRUE(NULL
== attrib3
->buffer());
213 // The VertexAttribManager must be destroyed before the BufferManager
214 // so it releases its buffers.
216 buffer_manager
.Destroy(false);
219 // TODO(gman): Test ValidateBindings
220 // TODO(gman): Test ValidateBindings with client side arrays.