Covellite++  Version: 2.3.4 Revision: 2587 Platform: x64 Build: 17:16 22.11.2026
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
OpenGLShader.Present_test.hpp
1
2#pragma once
3#include <gmock/gmock.hpp>
4#include <alicorn/std/vector.hpp>
5#include <alicorn/std/string/encoding.hpp>
6#include <boost/algorithm/string/replace.hpp>
7
8#ifndef OpenGLShader_test
9
10class OpenGLShader_test :
11 public ::testing::Test
12{
13
14};
15
16#endif
17
18namespace
19{
20
21// ************************************************************************** //
22TEST_F(OpenGLShader_test, /*DISABLED_*/Test_Present_Index_CreateBuffer_Fail)
23{
24 using GLProxy_t = ::mock::GLProxy;
25 GLProxy_t GLProxy;
26 GLProxy_t::GetInstance() = &GLProxy;
27
28 const Tested_t Example{ Data_t{} };
29 const ITested_t & IExample = Example;
30
31 auto itCreator = IExample.GetCreators().find(uT("Present"));
32 ASSERT_NE(IExample.GetCreators().end(), itCreator);
33
34 auto TestCallRender = [&](const Component_t::ComponentPtr_t & _pComponent)
35 {
36 using namespace ::testing;
37
38 InSequence Dummy;
39
40 EXPECT_CALL(GLProxy, GenBuffers(_))
41 .Times(1);
42
43 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, _))
44 .Times(AtLeast(1));
45
46 EXPECT_CALL(GLProxy, GetError())
47 .Times(1)
48 .WillOnce(Return(1908232144));
49
50 EXPECT_THROW(itCreator->second(_pComponent), ::std::exception);
51 };
52
53 const ::std::vector<int> Source;
54
55 const auto pBuffer = Component_t::Make(
56 {
57 { uT("content"), Source },
58 });
59
60 TestCallRender(pBuffer);
61
62 const auto pData = Component_t::Make(
63 {
64 { uT("kind"), uT("Buffer") },
65 { uT("content"), Source },
66 });
67
68 TestCallRender(Component_t::Make({ { uT("service"), Object_t{ pData } } }));
69}
70
71// ************************************************************************** //
72TEST_F(OpenGLShader_test, /*DISABLED_*/Test_Present_Index)
73{
74 using GLProxy_t = ::mock::GLProxy;
75 GLProxy_t GLProxy;
76 GLProxy_t::GetInstance() = &GLProxy;
77
78 const Tested_t Example{ Data_t{} };
79 const ITested_t & IExample = Example;
80
81 const ::mock::GLuint BufferId = 1908241121;
82
83 ::std::vector<int> Indices =
84 {
85 1808261927,
86 1808261928,
87 1808261929,
88 1808261930,
89 1808261931
90 };
91
92 IntroduceBufferSize(Indices);
93
94 auto itPresentCreator = IExample.GetCreators().find(uT("Present"));
95 ASSERT_NE(IExample.GetCreators().end(), itPresentCreator);
96
97 auto TestCallRender = [&](const Component_t::ComponentPtr_t & _pPresent)
98 {
99 using namespace ::testing;
100
101 InSequence Dummy;
102
103 EXPECT_CALL(GLProxy, GenBuffers(1))
104 .Times(1)
105 .WillOnce(Return(BufferId));
106
107 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferId))
108 .Times(1);
109
110 const auto Size =
111 static_cast<::mock::GLsizeiptr>(Indices.size() * sizeof(int));
112
113 EXPECT_CALL(GLProxy, BufferData(GL_ELEMENT_ARRAY_BUFFER, Size,
114 GetExpected(Indices), GL_STATIC_DRAW))
115 .Times(1);
116
117 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
118 .Times(1);
119
120 EXPECT_CALL(GLProxy, GetError())
121 .Times(1)
122 .WillOnce(Return(GL_NO_ERROR));
123
124 const auto Render = itPresentCreator->second(_pPresent);
125 ASSERT_NE(nullptr, Render);
126
127 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferId))
128 .Times(1);
129
130 EXPECT_CALL(GLProxy, DrawElements(GL_TRIANGLES, (int)Indices.size(),
131 GL_UNSIGNED_INT, nullptr))
132 .Times(1);
133
134 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
135 .Times(1);
136
137 EXPECT_CALL(GLProxy, BindTexture(GL_TEXTURE_2D, 0))
138 .Times(0); // Нельзя!!!
139
140 Render();
141
142 EXPECT_CALL(GLProxy, DeleteBuffers(1, BufferId))
143 .Times(1);
144 };
145
146 {
147 const auto pPresent = Component_t::Make(
148 {
149 { uT("content"), Indices },
150 });
151
152 TestCallRender(pPresent);
153 }
154
155 {
156 // Индексный буфер через компонент Data
157
158 const auto pData = Component_t::Make(
159 {
160 { uT("kind"), uT("Buffer") },
161 { uT("content"), Indices },
162 });
163
164 TestCallRender(Component_t::Make({ { uT("service"), Object_t{ pData } } }));
165 }
166}
167
168// ************************************************************************** //
169TEST_F(OpenGLShader_test, /*DISABLED_*/Test_Present_Instance_SizeAndCountInvalidValue)
170{
171 ::mock::GLProxy GLProxy;
172
173 const Tested_t Example{ Data_t{} };
174 const ITested_t & IExample = Example;
175
176 auto itCreator = IExample.GetCreators().find(uT("Present"));
177 ASSERT_NE(IExample.GetCreators().end(), itCreator);
178
179 auto TestCallMapper = [&](
180 const Component_t::ComponentPtr_t & _pInstance,
181 const auto & _Mapper)
182 {
183 const ::std::vector<int> Source;
184
185 (*_pInstance)[uT("mapper")] = _Mapper;
186
187 const auto pPresent = Component_t::Make(
188 {
189 { uT("content"), Source },
190 { uT("service"), Object_t{ _pInstance } }
191 });
192
193 using namespace ::testing;
194
195 EXPECT_CALL(GLProxy, GenBuffers(_)) // Индексный буфер
196 .Times(1);
197
198 EXPECT_THROW(itCreator->second(pPresent), ::std::exception);
199 };
200
201 auto TestCall = [&](const Component_t::ComponentPtr_t & _pInstance)
202 {
203 using BufferMapperMaxCount_t = ::std::function<bool(void *)>;
204 using BufferMapperCangeCount_t = ::std::function<bool(void *, ::std::size_t &)>;
205
206 TestCallMapper(_pInstance, BufferMapperMaxCount_t{});
207 TestCallMapper(_pInstance, BufferMapperCangeCount_t{});
208 };
209
210 TestCall(Component_t::Make(
211 {
212 { uT("kind"), uT("Buffer") },
213 }));
214
215 TestCall(Component_t::Make(
216 {
217 { uT("kind"), uT("Buffer") },
218 { uT("size"), ::std::size_t(17) },
219 }));
220
221 TestCall(Component_t::Make(
222 {
223 { uT("kind"), uT("Buffer") },
224 { uT("size"), ::std::size_t(32) },
225 }));
226
227 TestCall(Component_t::Make(
228 {
229 { uT("kind"), uT("Buffer") },
230 { uT("size"), ::std::size_t(32) },
231 { uT("count"), ::std::size_t(5) },
232 }));
233}
234
235// ************************************************************************** //
236TEST_F(OpenGLShader_test, /*DISABLED_*/Test_Present_Instance_CreateBuffer_Fail)
237{
238 using BufferMapperMaxCount_t = ::std::function<bool(void *)>;
239 using BufferMapperCangeCount_t = ::std::function<bool(void *, ::std::size_t &)>;
240
241 ::mock::GLProxy GLProxy;
242
243 const Tested_t Example{ Data_t{} };
244 const ITested_t & IExample = Example;
245
246 auto itCreator = IExample.GetCreators().find(uT("Present"));
247 ASSERT_NE(IExample.GetCreators().end(), itCreator);
248
249 auto TestCallRender = [&](const auto & _Mapper)
250 {
251 const ::std::vector<int> Source;
252
253 const auto pInstanceData = Component_t::Make(
254 {
255 { uT("kind"), uT("Buffer") },
256 { uT("mapper"), _Mapper },
257 { uT("size"), ::std::size_t(16) },
258 { uT("count"), ::std::size_t(1) },
259 });
260
261 const auto pPresent = Component_t::Make(
262 {
263 { uT("content"), Source },
264 { uT("service"), Object_t{ pInstanceData } }
265 });
266
267 using namespace ::testing;
268
269 InSequence Dummy;
270
271 EXPECT_CALL(GLProxy, GenBuffers(_))
272 .Times(1);
273
274 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, _))
275 .Times(AtLeast(1));
276
277 EXPECT_CALL(GLProxy, GetError())
278 .Times(1)
279 .WillOnce(Return(GL_NO_ERROR));
280
281 EXPECT_CALL(GLProxy, GenBuffers(_))
282 .Times(1);
283
284 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, _))
285 .Times(AtLeast(1));
286
287 EXPECT_CALL(GLProxy, GetError())
288 .Times(1)
289 .WillOnce(Return(1908232144));
290
291 EXPECT_THROW(itCreator->second(pPresent), ::std::exception);
292 };
293
294 TestCallRender(BufferMapperMaxCount_t{});
295 TestCallRender(BufferMapperCangeCount_t{});
296}
297
298// ************************************************************************** //
299TEST_F(OpenGLShader_test, /*DISABLED_*/Test_Present_Instance_MaxCount)
300{
301 using BufferMapper_t = ::covellite::api::cbBufferMap_t<void>;
302
303 ::mock::GLProxy GLProxy;
304
305 class MapperProxy_t
306 {
307 public:
308 MOCK_METHOD1(Map, bool(void *));
309 };
310
311 MapperProxy_t MapperProxy;
312
313 const ::mock::GLuint IndexBufferId = 1909231813;
314 const ::mock::GLuint InstanceBufferId = 1909231820;
315 const ::std::size_t InstanceCount = 120;
316 const ::std::size_t InstanceStride = 48;
317 const ::std::size_t InstanceBufferSize = InstanceCount * InstanceStride;
318 const ::mock::GLint ProgramId = 1909231917;
319
320 ::std::vector<int> Indices =
321 {
322 1808261927,
323 1808261928,
324 1808261929,
325 1808261930,
326 1808261931,
327 1909231821,
328 1909231822,
329 1909231823,
330 };
331
332 IntroduceBufferSize(Indices);
333
334 void * pLocalData = (void *)1909232013;
335
336 const BufferMapper_t Mapper = [&](void * _pData)
337 {
338 pLocalData = _pData;
339 return MapperProxy.Map(_pData);
340 };
341
342 const Tested_t Example{ Data_t{} };
343 const ITested_t & IExample = Example;
344
345 auto itCreator = IExample.GetCreators().find(uT("Present"));
346 ASSERT_NE(IExample.GetCreators().end(), itCreator);
347
348 auto TestCallRender = [&](const Component_t::ComponentPtr_t & _pPresent)
349 {
350 using namespace ::testing;
351
352 InSequence Dummy;
353
354 // *************************** Индексный буфер ************************** //
355
356 EXPECT_CALL(GLProxy, GenBuffers(1))
357 .Times(1)
358 .WillOnce(Return(IndexBufferId));
359
360 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId))
361 .Times(1);
362
363 const auto Size =
364 static_cast<::mock::GLsizeiptr>(Indices.size() * sizeof(int));
365
366 EXPECT_CALL(GLProxy, BufferData(GL_ELEMENT_ARRAY_BUFFER, Size,
367 GetExpected(Indices), GL_STATIC_DRAW))
368 .Times(1);
369
370 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
371 .Times(1);
372
373 EXPECT_CALL(GLProxy, GetError())
374 .Times(1)
375 .WillOnce(Return(GL_NO_ERROR));
376
377 // ****************************** Инстанс-буфер ************************* //
378
379 EXPECT_CALL(GLProxy, GenBuffers(1))
380 .Times(1)
381 .WillOnce(Return(InstanceBufferId));
382
383 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
384 .Times(1);
385
386 EXPECT_CALL(GLProxy, BufferData(GL_ARRAY_BUFFER, InstanceBufferSize,
387 ::std::vector<uint8_t>{}, GL_STREAM_DRAW))
388 .Times(1);
389
390 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, 0))
391 .Times(1);
392
393 EXPECT_CALL(GLProxy, GetError())
394 .Times(1)
395 .WillOnce(Return(GL_NO_ERROR));
396
397 // ********************************************************************** //
398
399 const auto Render = itCreator->second(_pPresent);
400 ASSERT_NE(nullptr, Render);
401
402 // ****************** Отрисовка без обновления ************************** //
403
404 const auto TestCallDraw = [&](void)
405 {
406 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId))
407 .Times(1);
408
409 EXPECT_CALL(GLProxy, DrawElementsInstanced(GL_TRIANGLES,
410 (int)Indices.size(), GL_UNSIGNED_INT, nullptr, InstanceCount))
411 .Times(1);
412
413 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
414 .Times(1);
415
416 EXPECT_CALL(GLProxy, BindTexture(GL_TEXTURE_2D, 0))
417 .Times(0); // Нельзя!!!
418 };
419
420 EXPECT_CALL(MapperProxy, Map(nullptr))
421 .Times(1)
422 .WillOnce(Return(false));
423
424 EXPECT_CALL(MapperProxy, Map(_))
425 .Times(0);
426
427 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
428 .Times(0);
429
430 TestCallDraw();
431 Render();
432
433 // ******************* Отрисовка с обновлением ************************** //
434
435 const auto TestCallSetInstanceData = [&](
436 const ::mock::GLenum _iType,
437 const ::mock::GLenum _Type,
438 const ::mock::GLint _LocationIndex0,
439 auto pData)
440 {
441 EXPECT_CALL(MapperProxy, Map(nullptr))
442 .Times(1)
443 .WillOnce(Return(true));
444
445 EXPECT_CALL(MapperProxy, Map(pData))
446 .Times(1);
447
448 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
449 .Times(1);
450
451 EXPECT_CALL(GLProxy, BufferData(GL_ARRAY_BUFFER, InstanceBufferSize,
452 ::std::vector<uint8_t>{}, GL_STREAM_DRAW))
453 .Times(1);
454
455 EXPECT_CALL(GLProxy,
456 BufferSubDataRaw(GL_ARRAY_BUFFER, 0, InstanceBufferSize, pData))
457 .Times(1);
458
459 EXPECT_CALL(GLProxy, GetIntegerv(GL_CURRENT_PROGRAM))
460 .Times(1)
461 .WillOnce(Return(&ProgramId));
462
463 constexpr auto BlockSize = sizeof(float) * 4;
464
465 EXPECT_CALL(GLProxy, GetProgramiv(ProgramId, GL_ACTIVE_ATTRIBUTES))
466 .Times(1)
467 .WillOnce(Return(BlockSize));
468
469 for (int i = 0; i < BlockSize; i++)
470 {
471 EXPECT_CALL(GLProxy, GetActiveAttribType(ProgramId, i))
472 .Times(1)
473 .WillOnce(Return(_iType));
474
475 EXPECT_CALL(GLProxy, GetActiveAttribName(ProgramId, i, 255))
476 .Times(1)
477 .WillOnce(Return("iValue" + ::std::to_string(i + 1)));
478 }
479
480 for (int i = 0; i < InstanceStride / BlockSize; i++)
481 {
482 EXPECT_CALL(GLProxy,
483 GetAttribLocation(ProgramId, "iValue" + ::std::to_string(i + 1)))
484 .Times(1)
485 .WillOnce(Return(_LocationIndex0 + i));
486
487 if (_LocationIndex0 + i >= 0)
488 {
489 EXPECT_CALL(GLProxy, EnableVertexAttribArray(_LocationIndex0 + i))
490 .Times(1);
491
492 EXPECT_CALL(GLProxy, VertexAttribPointer(_LocationIndex0 + i, 4,
493 _Type, GL_FALSE, InstanceStride, (void*)(BlockSize * i)))
494 .Times(1);
495
496 EXPECT_CALL(GLProxy, VertexAttribDivisor(_LocationIndex0 + i, 1))
497 .Times(1);
498 }
499 else
500 {
501 EXPECT_CALL(GLProxy, EnableVertexAttribArray(_))
502 .Times(0);
503
504 EXPECT_CALL(GLProxy, VertexAttribPointer(_, _, _, _, _, _))
505 .Times(0);
506
507 EXPECT_CALL(GLProxy, VertexAttribDivisor(_, _))
508 .Times(0);
509 }
510 }
511
512 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, 0))
513 .Times(1);
514 };
515
516 // Первый вызов без значения, чтобы сохранился указатель на локальные
517 // данные в pLocalData
518 TestCallSetInstanceData(GL_FLOAT_VEC4, GL_FLOAT, 1909232004, _);
519 TestCallDraw();
520 Render();
521
522 TestCallSetInstanceData(GL_FLOAT_VEC4, GL_FLOAT, 1909232004, pLocalData);
523 TestCallDraw();
524 Render();
525
526 TestCallSetInstanceData(GL_INT_VEC4, GL_INT, 1909232005, pLocalData);
527 TestCallDraw();
528 Render();
529
530 TestCallSetInstanceData(2005221201, 2005221201, 2005221202, pLocalData);
531 TestCallDraw();
532 Render();
533
534 TestCallSetInstanceData(GL_FLOAT_VEC4, GL_FLOAT, -1, pLocalData);
535 TestCallDraw();
536 Render();
537
538 TestCallSetInstanceData(GL_INT_VEC4, GL_INT, -1, pLocalData);
539 TestCallDraw();
540 Render();
541
542 // ********************************************************************** //
543
544 EXPECT_CALL(GLProxy, DeleteBuffers(1, IndexBufferId))
545 .Times(1);
546
547 EXPECT_CALL(GLProxy, DeleteBuffers(1, InstanceBufferId))
548 .Times(1);
549 };
550
551 const auto pInstanceData = Component_t::Make(
552 {
553 { uT("kind"), uT("Buffer") },
554 { uT("size"), InstanceBufferSize },
555 { uT("mapper"), Mapper },
556 { uT("count"), InstanceCount },
557 });
558
559 const auto pPresent = Component_t::Make(
560 {
561 { uT("content"), Indices },
562 { uT("service"), Object_t{ pInstanceData } }
563 });
564
565 TestCallRender(pPresent);
566}
567
568// ************************************************************************** //
569TEST_F(OpenGLShader_test, /*DISABLED_*/Test_Present_Instance_ChangeCount)
570{
571 using BufferMapper_t = ::std::function<bool(void *, ::std::size_t &)>;
572
573 ::mock::GLProxy GLProxy;
574
575 class MapperProxy_t
576 {
577 public:
578 MOCK_METHOD1(GetCount, ::std::size_t(::std::size_t));
579 MOCK_METHOD1(Map, bool(void *));
580 };
581
582 MapperProxy_t MapperProxy;
583
584 const ::mock::GLuint IndexBufferId = 1909231813;
585 const ::mock::GLuint InstanceBufferId = 1909231820;
586 const ::std::size_t InstanceCount = 120;
587 const ::std::size_t InstanceStride = 48;
588 const ::std::size_t InstanceBufferSize = InstanceCount * InstanceStride;
589 const ::mock::GLint ProgramId = 1909231917;
590
591 ::std::vector<int> Indices =
592 {
593 1808261927,
594 1808261928,
595 1808261929,
596 1808261930,
597 1808261931,
598 1909231821,
599 1909231822,
600 1909231823,
601 };
602
603 IntroduceBufferSize(Indices);
604
605 void * pLocalData = (void *)1909232013;
606
607 const BufferMapper_t Mapper = [&](void * _pData, ::std::size_t & _Count)
608 {
609 pLocalData = _pData;
610 _Count = MapperProxy.GetCount(_Count);
611 return MapperProxy.Map(_pData);
612 };
613
614 const Tested_t Example{ Data_t{} };
615 const ITested_t & IExample = Example;
616
617 auto itCreator = IExample.GetCreators().find(uT("Present"));
618 ASSERT_NE(IExample.GetCreators().end(), itCreator);
619
620 auto TestCallRender = [&](const Component_t::ComponentPtr_t & _pPresent)
621 {
622 using namespace ::testing;
623
624 InSequence Dummy;
625
626 // *************************** Индексный буфер ************************** //
627
628 EXPECT_CALL(GLProxy, GenBuffers(1))
629 .Times(1)
630 .WillOnce(Return(IndexBufferId));
631
632 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId))
633 .Times(1);
634
635 const auto Size =
636 static_cast<::mock::GLsizeiptr>(Indices.size() * sizeof(int));
637
638 EXPECT_CALL(GLProxy, BufferData(GL_ELEMENT_ARRAY_BUFFER, Size,
639 GetExpected(Indices), GL_STATIC_DRAW))
640 .Times(1);
641
642 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
643 .Times(1);
644
645 EXPECT_CALL(GLProxy, GetError())
646 .Times(1)
647 .WillOnce(Return(GL_NO_ERROR));
648
649 // ****************************** Инстанс-буфер ************************* //
650
651 EXPECT_CALL(GLProxy, GenBuffers(1))
652 .Times(1)
653 .WillOnce(Return(InstanceBufferId));
654
655 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
656 .Times(1);
657
658 EXPECT_CALL(GLProxy, BufferData(GL_ARRAY_BUFFER, InstanceBufferSize,
659 ::std::vector<uint8_t>{}, GL_STREAM_DRAW))
660 .Times(1);
661
662 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, 0))
663 .Times(1);
664
665 EXPECT_CALL(GLProxy, GetError())
666 .Times(1)
667 .WillOnce(Return(GL_NO_ERROR));
668
669 // ********************************************************************** //
670
671 const auto Render = itCreator->second(_pPresent);
672 ASSERT_NE(nullptr, Render);
673
674 // ****************** Отрисовка без обновления ************************** //
675
676 const auto TestCallDraw = [&](const ::std::size_t _Count)
677 {
678 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId))
679 .Times(1);
680
681 EXPECT_CALL(GLProxy, DrawElementsInstanced(GL_TRIANGLES,
682 (int)Indices.size(), GL_UNSIGNED_INT, nullptr, _Count))
683 .Times(1);
684
685 EXPECT_CALL(GLProxy, BindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0))
686 .Times(1);
687
688 EXPECT_CALL(GLProxy, BindTexture(GL_TEXTURE_2D, 0))
689 .Times(0); // Нельзя!!!
690 };
691
692 EXPECT_CALL(MapperProxy, GetCount(InstanceCount))
693 .Times(1)
694 .WillOnce(Return(InstanceCount / 2));
695
696 EXPECT_CALL(MapperProxy, Map(nullptr))
697 .Times(1)
698 .WillOnce(Return(false));
699
700 EXPECT_CALL(MapperProxy, Map(_))
701 .Times(0);
702
703 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
704 .Times(0);
705
706 TestCallDraw(InstanceCount / 2);
707 Render();
708
709 // ********** Отрисовка без обновления (слишком большое число) ********** //
710
711 EXPECT_CALL(MapperProxy, GetCount(InstanceCount))
712 .Times(1)
713 .WillOnce(Return(InstanceCount * 2));
714
715 EXPECT_CALL(MapperProxy, Map(nullptr))
716 .Times(1)
717 .WillOnce(Return(false));
718
719 EXPECT_CALL(MapperProxy, Map(_))
720 .Times(0);
721
722 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
723 .Times(0);
724
725 TestCallDraw(InstanceCount);
726 Render();
727
728 // ******************* Отрисовка с обновлением ************************** //
729
730 const auto TestCallSetInstanceData = [&](
731 const ::mock::GLenum _iType,
732 const ::mock::GLenum _Type,
733 const ::mock::GLint _LocationIndex0,
734 auto pData,
735 const ::std::size_t _ReturnCount)
736 {
737 EXPECT_CALL(MapperProxy, GetCount(InstanceCount))
738 .Times(1)
739 .WillOnce(Return(0));
740
741 EXPECT_CALL(MapperProxy, Map(nullptr))
742 .Times(1)
743 .WillOnce(Return(true));
744
745 EXPECT_CALL(MapperProxy, GetCount(0))
746 .Times(1)
747 .WillOnce(Return(_ReturnCount));
748
749 EXPECT_CALL(MapperProxy, Map(pData))
750 .Times(1);
751
752 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, InstanceBufferId))
753 .Times(1);
754
755 EXPECT_CALL(GLProxy, BufferData(GL_ARRAY_BUFFER, InstanceBufferSize,
756 ::std::vector<uint8_t>{}, GL_STREAM_DRAW))
757 .Times(1);
758
759 EXPECT_CALL(GLProxy,
760 BufferSubDataRaw(GL_ARRAY_BUFFER, 0, InstanceBufferSize, pData))
761 .Times(1);
762
763 EXPECT_CALL(GLProxy, GetIntegerv(GL_CURRENT_PROGRAM))
764 .Times(1)
765 .WillOnce(Return(&ProgramId));
766
767 constexpr auto BlockSize = sizeof(float) * 4;
768
769 EXPECT_CALL(GLProxy, GetProgramiv(ProgramId, GL_ACTIVE_ATTRIBUTES))
770 .Times(1)
771 .WillOnce(Return(BlockSize));
772
773 for (int i = 0; i < BlockSize; i++)
774 {
775 EXPECT_CALL(GLProxy, GetActiveAttribType(ProgramId, i))
776 .Times(1)
777 .WillOnce(Return(_iType));
778
779 EXPECT_CALL(GLProxy, GetActiveAttribName(ProgramId, i, 255))
780 .Times(1)
781 .WillOnce(Return("iValue" + ::std::to_string(i + 1)));
782 }
783
784 for (int i = 0; i < InstanceStride / BlockSize; i++)
785 {
786 EXPECT_CALL(GLProxy,
787 GetAttribLocation(ProgramId, "iValue" + ::std::to_string(i + 1)))
788 .Times(1)
789 .WillOnce(Return(_LocationIndex0 + i));
790
791 if (_LocationIndex0 + i >= 0)
792 {
793 EXPECT_CALL(GLProxy, EnableVertexAttribArray(_LocationIndex0 + i))
794 .Times(1);
795
796 EXPECT_CALL(GLProxy, VertexAttribPointer(_LocationIndex0 + i, 4,
797 _Type, GL_FALSE, InstanceStride, (void *)(BlockSize * i)))
798 .Times(1);
799
800 EXPECT_CALL(GLProxy, VertexAttribDivisor(_LocationIndex0 + i, 1))
801 .Times(1);
802 }
803 else
804 {
805 EXPECT_CALL(GLProxy, EnableVertexAttribArray(_))
806 .Times(0);
807
808 EXPECT_CALL(GLProxy, VertexAttribPointer(_, _, _, _, _, _))
809 .Times(0);
810
811 EXPECT_CALL(GLProxy, VertexAttribDivisor(_, _))
812 .Times(0);
813 }
814 }
815
816 EXPECT_CALL(GLProxy, BindBuffer(GL_ARRAY_BUFFER, 0))
817 .Times(1);
818 };
819
820 // Первый вызов без значения, просто чтобы сохранился указатель
821 // на локальные данные в pLocalData
822 TestCallSetInstanceData(GL_FLOAT_VEC4, GL_FLOAT, 1909232004, _, 0);
823 TestCallDraw(0);
824 Render();
825
826 TestCallSetInstanceData(GL_FLOAT_VEC4, GL_FLOAT, 1909232004, pLocalData, InstanceCount / 3);
827 TestCallDraw(InstanceCount / 3);
828 Render();
829
830 TestCallSetInstanceData(GL_INT_VEC4, GL_INT, 1909232005, pLocalData, InstanceCount / 4);
831 TestCallDraw(InstanceCount / 4);
832 Render();
833
834 TestCallSetInstanceData(2005221201, 2005221201, 2005221202, pLocalData, InstanceCount * 2);
835 TestCallDraw(InstanceCount);
836 Render();
837
838 TestCallSetInstanceData(GL_FLOAT_VEC4, GL_FLOAT, -1, pLocalData, InstanceCount * 3);
839 TestCallDraw(InstanceCount);
840 Render();
841
842 TestCallSetInstanceData(GL_INT_VEC4, GL_INT, -1, pLocalData, InstanceCount + 1);
843 TestCallDraw(InstanceCount);
844 Render();
845
846 // ********************************************************************** //
847
848 EXPECT_CALL(GLProxy, DeleteBuffers(1, IndexBufferId))
849 .Times(1);
850
851 EXPECT_CALL(GLProxy, DeleteBuffers(1, InstanceBufferId))
852 .Times(1);
853 };
854
855 const auto pInstanceData = Component_t::Make(
856 {
857 { uT("kind"), uT("Buffer") },
858 { uT("size"), InstanceBufferSize },
859 { uT("mapper"), Mapper },
860 { uT("count"), InstanceCount },
861 });
862
863 const auto pPresent = Component_t::Make(
864 {
865 { uT("content"), Indices },
866 { uT("service"), Object_t{ pInstanceData } }
867 });
868
869 TestCallRender(pPresent);
870}
871
872} // unnamed namespace