2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "test-graphics-controller.h"
19 #include "test-graphics-buffer.h"
20 #include "test-graphics-command-buffer.h"
21 #include "test-graphics-framebuffer.h"
22 #include "test-graphics-reflection.h"
23 #include "test-graphics-render-pass.h"
24 #include "test-graphics-render-target.h"
25 #include "test-graphics-sampler.h"
26 #include "test-graphics-shader.h"
27 #include "test-graphics-sync-object.h"
28 #include "test-graphics-texture.h"
30 #include <dali/integration-api/gl-defines.h>
37 std::ostream& operator<<(std::ostream& o, const Graphics::BufferCreateInfo& bufferCreateInfo)
39 return o << "usage:" << std::hex << bufferCreateInfo.usage << ", size:" << std::dec << bufferCreateInfo.size;
42 std::ostream& operator<<(std::ostream& o, const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo)
44 return o << "level:" << (commandBufferCreateInfo.level == Graphics::CommandBufferLevel::PRIMARY ? "PRIMARY" : "SECONDARY")
45 << ", fixedCapacity:" << std::dec << commandBufferCreateInfo.fixedCapacity;
48 std::ostream& operator<<(std::ostream& o, const Graphics::TextureType& textureType)
52 case Graphics::TextureType::TEXTURE_2D:
55 case Graphics::TextureType::TEXTURE_3D:
58 case Graphics::TextureType::TEXTURE_CUBEMAP:
59 o << "TEXTURE_CUBEMAP";
65 std::ostream& operator<<(std::ostream& o, const Graphics::Extent2D extent)
67 o << "width:" << extent.width << ", height:" << extent.height;
71 std::ostream& operator<<(std::ostream& o, const Graphics::TextureCreateInfo& createInfo)
73 o << "textureType:" << createInfo.textureType
74 << " size:" << createInfo.size
75 << " format:" << static_cast<uint32_t>(createInfo.format)
76 << " mipMapFlag:" << createInfo.mipMapFlag
77 << " layout:" << (createInfo.layout == Graphics::TextureLayout::LINEAR ? "LINEAR" : "OPTIMAL")
78 << " usageFlags:" << std::hex << createInfo.usageFlags
79 << " data:" << std::hex << createInfo.data
80 << " dataSize:" << std::dec << createInfo.dataSize
81 << " nativeImagePtr:" << std::hex << createInfo.nativeImagePtr;
85 std::ostream& operator<<(std::ostream& o, Graphics::SamplerAddressMode addressMode)
89 case Graphics::SamplerAddressMode::REPEAT:
92 case Graphics::SamplerAddressMode::MIRRORED_REPEAT:
93 o << "MIRRORED_REPEAT";
95 case Graphics::SamplerAddressMode::CLAMP_TO_EDGE:
98 case Graphics::SamplerAddressMode::CLAMP_TO_BORDER:
99 o << "CLAMP_TO_BORDER";
101 case Graphics::SamplerAddressMode::MIRROR_CLAMP_TO_EDGE:
102 o << "MIRROR_CLAMP_TO_EDGE";
108 std::ostream& operator<<(std::ostream& o, Graphics::SamplerFilter filterMode)
112 case Graphics::SamplerFilter::LINEAR:
115 case Graphics::SamplerFilter::NEAREST:
122 std::ostream& operator<<(std::ostream& o, Graphics::SamplerMipmapMode mipmapMode)
126 case Graphics::SamplerMipmapMode::NONE:
129 case Graphics::SamplerMipmapMode::LINEAR:
132 case Graphics::SamplerMipmapMode::NEAREST:
139 std::ostream& operator<<(std::ostream& o, const Graphics::SamplerCreateInfo& createInfo)
141 o << "minFilter:" << createInfo.minFilter
142 << " magFilter:" << createInfo.magFilter
143 << " wrapModeU:" << createInfo.addressModeU
144 << " wrapModeV:" << createInfo.addressModeV
145 << " wrapModeW:" << createInfo.addressModeW
146 << " mipMapMode:" << createInfo.mipMapMode;
150 std::ostream& operator<<(std::ostream& o, const Graphics::ColorAttachment& colorAttachment)
152 o << "attachmentId:" << colorAttachment.attachmentId
153 << " layerId:" << colorAttachment.layerId
154 << " levelId:" << colorAttachment.levelId
155 << " texture:" << colorAttachment.texture;
159 std::ostream& operator<<(std::ostream& o, const Graphics::DepthStencilAttachment& depthStencilAttachment)
161 o << "depthTexture:" << depthStencilAttachment.depthTexture
162 << "depthLevel:" << depthStencilAttachment.depthLevel
163 << "stencilTexture:" << depthStencilAttachment.stencilTexture
164 << "stencilLevel:" << depthStencilAttachment.stencilLevel;
168 std::ostream& operator<<(std::ostream& o, const Graphics::FramebufferCreateInfo& createInfo)
170 o << "colorAttachments:";
171 for(auto i = 0u; i < createInfo.colorAttachments.size(); ++i)
173 o << "[" << i << "]=" << createInfo.colorAttachments[i] << " ";
175 o << "depthStencilAttachment:" << createInfo.depthStencilAttachment;
176 o << "size: " << createInfo.size;
180 int GetNumComponents(Graphics::VertexInputFormat vertexFormat)
184 case Graphics::VertexInputFormat::UNDEFINED:
185 case Graphics::VertexInputFormat::FLOAT:
186 case Graphics::VertexInputFormat::INTEGER:
188 case Graphics::VertexInputFormat::IVECTOR2:
189 case Graphics::VertexInputFormat::FVECTOR2:
191 case Graphics::VertexInputFormat::IVECTOR3:
192 case Graphics::VertexInputFormat::FVECTOR3:
194 case Graphics::VertexInputFormat::FVECTOR4:
195 case Graphics::VertexInputFormat::IVECTOR4:
201 GLint GetSize(Graphics::VertexInputFormat vertexFormat)
205 case Graphics::VertexInputFormat::UNDEFINED:
207 case Graphics::VertexInputFormat::INTEGER:
208 case Graphics::VertexInputFormat::IVECTOR2:
209 case Graphics::VertexInputFormat::IVECTOR3:
210 case Graphics::VertexInputFormat::IVECTOR4:
212 case Graphics::VertexInputFormat::FLOAT:
213 case Graphics::VertexInputFormat::FVECTOR2:
214 case Graphics::VertexInputFormat::FVECTOR3:
215 case Graphics::VertexInputFormat::FVECTOR4:
221 GLint GetGlType(Graphics::VertexInputFormat vertexFormat)
225 case Graphics::VertexInputFormat::UNDEFINED:
227 case Graphics::VertexInputFormat::INTEGER:
228 case Graphics::VertexInputFormat::IVECTOR2:
229 case Graphics::VertexInputFormat::IVECTOR3:
230 case Graphics::VertexInputFormat::IVECTOR4:
232 case Graphics::VertexInputFormat::FLOAT:
233 case Graphics::VertexInputFormat::FVECTOR2:
234 case Graphics::VertexInputFormat::FVECTOR3:
235 case Graphics::VertexInputFormat::FVECTOR4:
241 GLenum GetTopology(Graphics::PrimitiveTopology topology)
245 case Graphics::PrimitiveTopology::POINT_LIST:
248 case Graphics::PrimitiveTopology::LINE_LIST:
251 case Graphics::PrimitiveTopology::LINE_LOOP:
254 case Graphics::PrimitiveTopology::LINE_STRIP:
255 return GL_LINE_STRIP;
257 case Graphics::PrimitiveTopology::TRIANGLE_LIST:
260 case Graphics::PrimitiveTopology::TRIANGLE_STRIP:
261 return GL_TRIANGLE_STRIP;
263 case Graphics::PrimitiveTopology::TRIANGLE_FAN:
264 return GL_TRIANGLE_FAN;
269 GLenum GetCullFace(Graphics::CullMode cullMode)
273 case Graphics::CullMode::NONE:
275 case Graphics::CullMode::FRONT:
277 case Graphics::CullMode::BACK:
279 case Graphics::CullMode::FRONT_AND_BACK:
280 return GL_FRONT_AND_BACK;
285 GLenum GetFrontFace(Graphics::FrontFace frontFace)
287 if(frontFace == Graphics::FrontFace::CLOCKWISE)
294 GLenum GetBlendFactor(Graphics::BlendFactor blendFactor)
296 GLenum glFactor = GL_ZERO;
300 case Graphics::BlendFactor::ZERO:
303 case Graphics::BlendFactor::ONE:
306 case Graphics::BlendFactor::SRC_COLOR:
307 glFactor = GL_SRC_COLOR;
309 case Graphics::BlendFactor::ONE_MINUS_SRC_COLOR:
310 glFactor = GL_ONE_MINUS_SRC_COLOR;
312 case Graphics::BlendFactor::DST_COLOR:
313 glFactor = GL_DST_COLOR;
315 case Graphics::BlendFactor::ONE_MINUS_DST_COLOR:
316 glFactor = GL_ONE_MINUS_DST_COLOR;
318 case Graphics::BlendFactor::SRC_ALPHA:
319 glFactor = GL_SRC_ALPHA;
321 case Graphics::BlendFactor::ONE_MINUS_SRC_ALPHA:
322 glFactor = GL_ONE_MINUS_SRC_ALPHA;
324 case Graphics::BlendFactor::DST_ALPHA:
325 glFactor = GL_DST_ALPHA;
327 case Graphics::BlendFactor::ONE_MINUS_DST_ALPHA:
328 glFactor = GL_ONE_MINUS_DST_ALPHA;
330 case Graphics::BlendFactor::CONSTANT_COLOR:
331 glFactor = GL_CONSTANT_COLOR;
333 case Graphics::BlendFactor::ONE_MINUS_CONSTANT_COLOR:
334 glFactor = GL_ONE_MINUS_CONSTANT_COLOR;
336 case Graphics::BlendFactor::CONSTANT_ALPHA:
337 glFactor = GL_CONSTANT_ALPHA;
339 case Graphics::BlendFactor::ONE_MINUS_CONSTANT_ALPHA:
340 glFactor = GL_ONE_MINUS_CONSTANT_ALPHA;
342 case Graphics::BlendFactor::SRC_ALPHA_SATURATE:
343 glFactor = GL_SRC_ALPHA_SATURATE;
345 // GLES doesn't appear to have dual source blending.
346 case Graphics::BlendFactor::SRC1_COLOR:
347 glFactor = GL_SRC_COLOR;
349 case Graphics::BlendFactor::ONE_MINUS_SRC1_COLOR:
350 glFactor = GL_ONE_MINUS_SRC_COLOR;
352 case Graphics::BlendFactor::SRC1_ALPHA:
353 glFactor = GL_SRC_ALPHA;
355 case Graphics::BlendFactor::ONE_MINUS_SRC1_ALPHA:
356 glFactor = GL_ONE_MINUS_SRC_ALPHA;
362 GLenum GetBlendOp(Graphics::BlendOp blendOp)
364 GLenum op = GL_FUNC_ADD;
367 case Graphics::BlendOp::ADD:
370 case Graphics::BlendOp::SUBTRACT:
371 op = GL_FUNC_SUBTRACT;
373 case Graphics::BlendOp::REVERSE_SUBTRACT:
374 op = GL_FUNC_REVERSE_SUBTRACT;
376 case Graphics::BlendOp::MIN:
379 case Graphics::BlendOp::MAX:
383 // @todo Add advanced blend equations
390 constexpr explicit GLCompareOp(Graphics::CompareOp compareOp)
394 case Graphics::CompareOp::NEVER:
397 case Graphics::CompareOp::LESS:
400 case Graphics::CompareOp::EQUAL:
403 case Graphics::CompareOp::LESS_OR_EQUAL:
406 case Graphics::CompareOp::GREATER:
409 case Graphics::CompareOp::NOT_EQUAL:
412 case Graphics::CompareOp::GREATER_OR_EQUAL:
415 case Graphics::CompareOp::ALWAYS:
425 constexpr explicit GLStencilOp(Graphics::StencilOp stencilOp)
429 case Graphics::StencilOp::KEEP:
432 case Graphics::StencilOp::ZERO:
435 case Graphics::StencilOp::REPLACE:
438 case Graphics::StencilOp::INCREMENT_AND_CLAMP:
441 case Graphics::StencilOp::DECREMENT_AND_CLAMP:
444 case Graphics::StencilOp::INVERT:
447 case Graphics::StencilOp::INCREMENT_AND_WRAP:
450 case Graphics::StencilOp::DECREMENT_AND_WRAP:
458 class TestGraphicsMemory : public Graphics::Memory
461 TestGraphicsMemory(TraceCallStack& callStack, TestGraphicsBuffer& buffer, uint32_t mappedOffset, uint32_t mappedSize)
462 : mCallStack(callStack),
464 mMappedOffset(mappedOffset),
465 mMappedSize(mappedSize),
471 void* LockRegion(uint32_t offset, uint32_t size) override
473 std::ostringstream o;
474 o << offset << ", " << size;
475 mCallStack.PushCall("Memory::LockRegion", o.str());
477 if(offset > mMappedOffset + mMappedSize ||
478 size + offset > mMappedOffset + mMappedSize)
480 fprintf(stderr, "TestGraphics.Memory::LockRegion() Out of bounds");
481 mBuffer.memory.resize(mMappedOffset + offset + size); // Grow to prevent memcpy from crashing
483 mLockedOffset = offset;
485 return &mBuffer.memory[mMappedOffset + offset];
488 void Unlock(bool flush) override
490 mCallStack.PushCall("Memory::Unlock", (flush ? "Flush" : "NoFlush"));
497 void Flush() override
499 mCallStack.PushCall("Memory::Flush", "");
501 mBuffer.Upload(mMappedOffset + mLockedOffset, mLockedSize);
505 TraceCallStack& mCallStack;
506 TestGraphicsBuffer& mBuffer;
507 uint32_t mMappedOffset;
508 uint32_t mMappedSize;
509 uint32_t mLockedOffset;
510 uint32_t mLockedSize;
513 TestGraphicsController::TestGraphicsController()
514 : mCallStack(true, "TestGraphicsController."),
515 mCommandBufferCallStack(true, "TestCommandBuffer."),
516 mFrameBufferCallStack(true, "TestFrameBuffer.")
518 mCallStack.Enable(true);
519 mCommandBufferCallStack.Enable(true);
520 auto& trace = mGl.GetTextureTrace();
522 trace.EnableLogging(true);
525 void TestGraphicsController::SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo)
527 TraceCallStack::NamedParams namedParams;
528 namedParams["submitInfo"] << "cmdBuffer[" << submitInfo.cmdBuffer.size()
529 << "], flags:" << std::hex << submitInfo.flags;
531 mCallStack.PushCall("SubmitCommandBuffers", "", namedParams);
533 mSubmitStack.emplace_back(submitInfo);
535 for(auto& graphicsCommandBuffer : submitInfo.cmdBuffer)
537 auto commandBuffer = Uncast<TestGraphicsCommandBuffer>(graphicsCommandBuffer);
538 ProcessCommandBuffer(*commandBuffer);
542 void TestGraphicsController::ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer)
544 bool scissorEnabled = false;
545 TestGraphicsFramebuffer* currentFramebuffer{nullptr};
546 TestGraphicsPipeline* currentPipeline{nullptr};
548 for(auto& cmd : commandBuffer.GetCommands())
553 case CommandType::FLUSH:
555 // Nothing to do here
558 case CommandType::BIND_TEXTURES:
560 for(auto& binding : cmd.data.bindTextures.textureBindings)
564 auto texture = Uncast<TestGraphicsTexture>(binding.texture);
565 texture->Bind(binding.binding);
569 auto sampler = Uncast<TestGraphicsSampler>(binding.sampler);
572 sampler->Apply(texture->GetTarget());
576 texture->Prepare(); // Ensure native texture is ready
581 case CommandType::BIND_VERTEX_BUFFERS:
583 for(auto& binding : cmd.data.bindVertexBuffers.vertexBufferBindings)
585 auto graphicsBuffer = binding.buffer;
586 auto vertexBuffer = Uncast<TestGraphicsBuffer>(graphicsBuffer);
587 vertexBuffer->Bind();
591 case CommandType::BIND_INDEX_BUFFER:
593 auto& indexBufferBinding = cmd.data.bindIndexBuffer;
594 if(indexBufferBinding.buffer)
596 auto buffer = Uncast<TestGraphicsBuffer>(indexBufferBinding.buffer);
601 case CommandType::BIND_UNIFORM_BUFFER:
605 auto& bindings = cmd.data.bindUniformBuffers;
606 auto buffer = bindings.standaloneUniformsBufferBinding;
608 // based on reflection, issue gl calls
609 buffer.buffer->BindAsUniformBuffer(static_cast<const TestGraphicsProgram*>(currentPipeline->programState.program), bindings.standaloneUniformsBufferBinding);
613 case CommandType::BIND_SAMPLERS:
617 case CommandType::BIND_PIPELINE:
619 currentPipeline = Uncast<TestGraphicsPipeline>(cmd.data.bindPipeline.pipeline);
620 BindPipeline(currentPipeline);
623 case CommandType::DRAW:
627 mGl.DrawArrays(GetTopology(currentPipeline->inputAssemblyState.topology),
629 cmd.data.draw.draw.vertexCount);
633 case CommandType::DRAW_INDEXED:
637 mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
638 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
640 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
644 case CommandType::DRAW_INDEXED_INDIRECT:
648 mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
649 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
651 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
655 case CommandType::SET_SCISSOR:
659 auto& rect = cmd.data.scissor.region;
660 mGl.Scissor(rect.x, rect.y, rect.width, rect.height);
664 case CommandType::SET_SCISSOR_TEST:
666 if(cmd.data.scissorTest.enable)
668 mGl.Enable(GL_SCISSOR_TEST);
669 scissorEnabled = true;
673 mGl.Disable(GL_SCISSOR_TEST);
674 scissorEnabled = false;
678 case CommandType::SET_VIEWPORT_TEST:
682 case CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
684 auto& rect = cmd.data.viewport.region;
685 mGl.Viewport(rect.x, rect.y, rect.width, rect.height);
689 case CommandType::SET_COLOR_MASK:
691 // Set all channels to the same mask
692 const bool mask = cmd.data.colorMask.enabled;
693 mGl.ColorMask(mask, mask, mask, mask);
696 case CommandType::CLEAR_STENCIL_BUFFER:
698 mGl.Clear(GL_STENCIL_BUFFER_BIT);
701 case CommandType::CLEAR_DEPTH_BUFFER:
703 mGl.Clear(GL_DEPTH_BUFFER_BIT);
707 case CommandType::SET_STENCIL_TEST_ENABLE:
709 if(cmd.data.stencilTest.enabled)
711 mGl.Enable(GL_STENCIL_TEST);
715 mGl.Disable(GL_STENCIL_TEST);
720 case CommandType::SET_STENCIL_FUNC:
722 mGl.StencilFunc(GLCompareOp(cmd.data.stencilFunc.compareOp).op,
723 cmd.data.stencilFunc.reference,
724 cmd.data.stencilFunc.compareMask);
728 case CommandType::SET_STENCIL_WRITE_MASK:
730 mGl.StencilMask(cmd.data.stencilWriteMask.mask);
733 case CommandType::SET_STENCIL_OP:
735 mGl.StencilOp(GLStencilOp(cmd.data.stencilOp.failOp).op,
736 GLStencilOp(cmd.data.stencilOp.depthFailOp).op,
737 GLStencilOp(cmd.data.stencilOp.passOp).op);
741 case CommandType::SET_DEPTH_COMPARE_OP:
743 mGl.DepthFunc(GLCompareOp(cmd.data.depth.compareOp).op);
746 case CommandType::SET_DEPTH_TEST_ENABLE:
748 if(cmd.data.depth.testEnabled)
750 mGl.Enable(GL_DEPTH_TEST);
754 mGl.Disable(GL_DEPTH_TEST);
758 case CommandType::SET_DEPTH_WRITE_ENABLE:
760 mGl.DepthMask(cmd.data.depth.writeEnabled);
764 case CommandType::EXECUTE_COMMAND_BUFFERS:
766 // Process secondary command buffers
767 for(auto& buf : cmd.data.executeCommandBuffers.buffers)
769 ProcessCommandBuffer(*Uncast<TestGraphicsCommandBuffer>(buf));
773 case CommandType::BEGIN_RENDER_PASS:
775 auto renderTarget = Uncast<TestGraphicsRenderTarget>(cmd.data.beginRenderPass.renderTarget);
779 auto fb = renderTarget->mCreateInfo.framebuffer;
782 if(currentFramebuffer != fb)
784 currentFramebuffer = Uncast<TestGraphicsFramebuffer>(fb);
785 currentFramebuffer->Bind();
790 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
795 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
798 auto& clearValues = cmd.data.beginRenderPass.clearValues;
799 if(clearValues.size() > 0)
801 const auto renderPass = static_cast<TestGraphicsRenderPass*>(cmd.data.beginRenderPass.renderPass);
804 const auto& color0 = renderPass->attachments[0];
806 if(color0.loadOp == Graphics::AttachmentLoadOp::CLEAR)
808 mask |= GL_COLOR_BUFFER_BIT;
810 // Set clear color (todo: cache it!)
811 // Something goes wrong here if Alpha mask is GL_TRUE
812 mGl.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
813 mGl.ClearColor(clearValues[0].color.r,
814 clearValues[0].color.g,
815 clearValues[0].color.b,
816 clearValues[0].color.a);
819 // check for depth stencil
820 if(renderPass->attachments.size() > 1)
822 const auto& depthStencil = renderPass->attachments.back();
823 if(depthStencil.loadOp == Graphics::AttachmentLoadOp::CLEAR)
826 uint32_t depthClearColor = 0u;
827 if(clearValues.size() == renderPass->attachments.size())
829 depthClearColor = clearValues.back().depthStencil.depth;
831 mGl.ClearDepthf(depthClearColor);
832 mask |= GL_DEPTH_BUFFER_BIT;
834 if(depthStencil.stencilLoadOp == Graphics::AttachmentLoadOp::CLEAR)
836 uint32_t stencilClearColor = 0u;
837 if(clearValues.size() == renderPass->attachments.size())
839 stencilClearColor = clearValues.back().depthStencil.stencil;
841 mGl.ClearStencil(stencilClearColor);
842 mGl.StencilMask(0xFF); // Clear all the bitplanes (assume 8)
843 mask |= GL_STENCIL_BUFFER_BIT;
849 // Test scissor area and RT size
850 const auto& area = cmd.data.beginRenderPass.renderArea;
854 area.width == renderTarget->mCreateInfo.extent.width &&
855 area.height == renderTarget->mCreateInfo.extent.height)
857 mGl.Disable(GL_SCISSOR_TEST);
862 mGl.Enable(GL_SCISSOR_TEST);
863 mGl.Scissor(cmd.data.beginRenderPass.renderArea.x, cmd.data.beginRenderPass.renderArea.y, cmd.data.beginRenderPass.renderArea.width, cmd.data.beginRenderPass.renderArea.height);
865 mGl.Disable(GL_SCISSOR_TEST);
871 DALI_ASSERT_DEBUG(0 && "BeginRenderPass has no render pass");
876 case CommandType::END_RENDER_PASS:
878 if(cmd.data.endRenderPass.syncObject != nullptr)
880 auto syncObject = Uncast<TestGraphicsSyncObject>(cmd.data.endRenderPass.syncObject);
881 syncObject->InitializeResource(); // create the sync object.
889 void TestGraphicsController::BindPipeline(TestGraphicsPipeline* pipeline)
891 auto& vi = pipeline->vertexInputState;
892 for(auto& attribute : vi.attributes)
894 mGl.EnableVertexAttribArray(attribute.location);
895 uint32_t attributeOffset = attribute.offset;
896 GLsizei stride = vi.bufferBindings[attribute.binding].stride;
898 mGl.VertexAttribPointer(attribute.location,
899 GetNumComponents(attribute.format),
900 GetGlType(attribute.format),
901 GL_FALSE, // Not normalized
903 reinterpret_cast<void*>(attributeOffset));
907 auto& rasterizationState = pipeline->rasterizationState;
908 if(rasterizationState.cullMode == Graphics::CullMode::NONE)
910 mGl.Disable(GL_CULL_FACE);
914 mGl.Enable(GL_CULL_FACE);
915 mGl.CullFace(GetCullFace(rasterizationState.cullMode));
918 mGl.FrontFace(GetFrontFace(rasterizationState.frontFace));
921 auto& colorBlendState = pipeline->colorBlendState;
922 if(colorBlendState.blendEnable)
924 mGl.Enable(GL_BLEND);
926 mGl.BlendFuncSeparate(GetBlendFactor(colorBlendState.srcColorBlendFactor),
927 GetBlendFactor(colorBlendState.dstColorBlendFactor),
928 GetBlendFactor(colorBlendState.srcAlphaBlendFactor),
929 GetBlendFactor(colorBlendState.dstAlphaBlendFactor));
930 if(colorBlendState.colorBlendOp != colorBlendState.alphaBlendOp)
932 mGl.BlendEquationSeparate(GetBlendOp(colorBlendState.colorBlendOp), GetBlendOp(colorBlendState.alphaBlendOp));
936 mGl.BlendEquation(GetBlendOp(colorBlendState.colorBlendOp));
938 mGl.BlendColor(colorBlendState.blendConstants[0],
939 colorBlendState.blendConstants[1],
940 colorBlendState.blendConstants[2],
941 colorBlendState.blendConstants[3]);
945 mGl.Disable(GL_BLEND);
948 auto* program = static_cast<const TestGraphicsProgram*>(pipeline->programState.program);
949 mGl.UseProgram(program->mImpl->mId);
953 * @brief Presents render target
954 * @param renderTarget render target to present
956 void TestGraphicsController::PresentRenderTarget(Graphics::RenderTarget* renderTarget)
958 TraceCallStack::NamedParams namedParams;
959 namedParams["renderTarget"] << std::hex << renderTarget;
960 mCallStack.PushCall("PresentRenderTarget", "", namedParams);
964 * @brief Waits until the GPU is idle
966 void TestGraphicsController::WaitIdle()
968 mCallStack.PushCall("WaitIdle", "");
972 * @brief Lifecycle pause event
974 void TestGraphicsController::Pause()
976 mCallStack.PushCall("Pause", "");
980 * @brief Lifecycle resume event
982 void TestGraphicsController::Resume()
984 mCallStack.PushCall("Resume", "");
987 void TestGraphicsController::Shutdown()
989 mCallStack.PushCall("Shutdown", "");
992 void TestGraphicsController::Destroy()
994 mCallStack.PushCall("Destroy", "");
997 void TestGraphicsController::UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>& updateInfoList,
998 const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList)
1000 TraceCallStack::NamedParams namedParams;
1001 namedParams["updateInfoList"] << "[" << updateInfoList.size() << "]:";
1002 namedParams["sourceList"] << "[" << sourceList.size() << "]:";
1004 mCallStack.PushCall("UpdateTextures", "", namedParams);
1006 // Call either TexImage2D or TexSubImage2D
1007 for(unsigned int i = 0; i < updateInfoList.size(); ++i)
1009 auto& updateInfo = updateInfoList[i];
1010 auto& source = sourceList[i];
1012 auto texture = static_cast<TestGraphicsTexture*>(updateInfo.dstTexture);
1013 texture->Bind(0); // Use first texture unit during resource update
1014 texture->Update(updateInfo, source);
1018 void TestGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
1020 mCallStack.PushCall("GenerateTextureMipmaps", "");
1022 auto gfxTexture = Uncast<TestGraphicsTexture>(&texture);
1023 mGl.BindTexture(gfxTexture->GetTarget(), 0);
1024 mGl.GenerateMipmap(gfxTexture->GetTarget());
1027 bool TestGraphicsController::EnableDepthStencilBuffer(bool enableDepth, bool enableStencil)
1029 TraceCallStack::NamedParams namedParams;
1030 namedParams["enableDepth"] << (enableDepth ? "T" : "F");
1031 namedParams["enableStencil"] << (enableStencil ? "T" : "F");
1032 mCallStack.PushCall("EnableDepthStencilBuffer", "", namedParams);
1036 void TestGraphicsController::RunGarbageCollector(size_t numberOfDiscardedRenderers)
1038 TraceCallStack::NamedParams namedParams;
1039 namedParams["numberOfDiscardedRenderers"] << numberOfDiscardedRenderers;
1040 mCallStack.PushCall("RunGarbageCollector", "", namedParams);
1043 void TestGraphicsController::DiscardUnusedResources()
1045 mCallStack.PushCall("DiscardUnusedResources", "");
1048 bool TestGraphicsController::IsDiscardQueueEmpty()
1050 mCallStack.PushCall("IsDiscardQueueEmpty", "");
1051 return isDiscardQueueEmptyResult;
1055 * @brief Test if the graphics subsystem has resumed & should force a draw
1057 * @return true if the graphics subsystem requires a re-draw
1059 bool TestGraphicsController::IsDrawOnResumeRequired()
1061 mCallStack.PushCall("IsDrawOnResumeRequired", "");
1062 return isDrawOnResumeRequiredResult;
1065 Graphics::UniquePtr<Graphics::Buffer> TestGraphicsController::CreateBuffer(const Graphics::BufferCreateInfo& createInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer)
1067 std::ostringstream oss;
1068 oss << "bufferCreateInfo:" << createInfo;
1069 mCallStack.PushCall("CreateBuffer", oss.str());
1070 return Graphics::MakeUnique<TestGraphicsBuffer>(mCallStack, mGl, createInfo.size, createInfo.usage);
1073 Graphics::UniquePtr<Graphics::CommandBuffer> TestGraphicsController::CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer)
1075 std::ostringstream oss;
1076 oss << "commandBufferCreateInfo:" << commandBufferCreateInfo;
1077 mCallStack.PushCall("CreateCommandBuffer", oss.str());
1078 return Graphics::MakeUnique<TestGraphicsCommandBuffer>(mCommandBufferCallStack, mGl);
1081 Graphics::UniquePtr<Graphics::RenderPass> TestGraphicsController::CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass)
1083 mCallStack.PushCall("CreateRenderPass", "");
1084 return Graphics::MakeUnique<TestGraphicsRenderPass>(mGl, renderPassCreateInfo);
1087 Graphics::UniquePtr<Graphics::Texture> TestGraphicsController::CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture)
1089 TraceCallStack::NamedParams namedParams;
1090 namedParams["textureCreateInfo"] << textureCreateInfo;
1091 mCallStack.PushCall("CreateTexture", namedParams.str(), namedParams);
1093 return Graphics::MakeUnique<TestGraphicsTexture>(mGl, textureCreateInfo);
1096 Graphics::UniquePtr<Graphics::Framebuffer> TestGraphicsController::CreateFramebuffer(
1097 const Graphics::FramebufferCreateInfo& createInfo,
1098 Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer)
1100 TraceCallStack::NamedParams namedParams;
1101 namedParams["framebufferCreateInfo"] << createInfo;
1102 mCallStack.PushCall("Controller::CreateFramebuffer", namedParams.str(), namedParams);
1104 return Graphics::MakeUnique<TestGraphicsFramebuffer>(mFrameBufferCallStack, mGl, createInfo);
1107 Graphics::UniquePtr<Graphics::Pipeline> TestGraphicsController::CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
1109 mCallStack.PushCall("CreatePipeline", "");
1110 return std::make_unique<TestGraphicsPipeline>(mGl, pipelineCreateInfo);
1113 Graphics::UniquePtr<Graphics::Program> TestGraphicsController::CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram)
1115 mCallStack.PushCall("CreateProgram", "");
1117 for(auto cacheEntry : mProgramCache)
1120 for(auto& shader : *(programCreateInfo.shaderState))
1122 auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
1123 std::vector<uint8_t> source;
1124 source.resize(graphicsShader->mCreateInfo.sourceSize);
1125 memcpy(&source[0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
1127 if(!std::equal(source.begin(), source.end(), cacheEntry.shaders[shader.pipelineStage].begin()))
1135 return Graphics::MakeUnique<TestGraphicsProgram>(cacheEntry.programImpl);
1139 mProgramCache.emplace_back();
1140 mProgramCache.back().programImpl = new TestGraphicsProgramImpl(mGl, programCreateInfo, mVertexFormats, mCustomUniforms);
1141 for(auto& shader : *(programCreateInfo.shaderState))
1143 auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
1144 mProgramCache.back().shaders[shader.pipelineStage].resize(graphicsShader->mCreateInfo.sourceSize);
1145 memcpy(&mProgramCache.back().shaders[shader.pipelineStage][0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
1147 return Graphics::MakeUnique<TestGraphicsProgram>(mProgramCache.back().programImpl);
1150 Graphics::UniquePtr<Graphics::Shader> TestGraphicsController::CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader)
1152 mCallStack.PushCall("CreateShader", "");
1153 return Graphics::MakeUnique<TestGraphicsShader>(mGl, shaderCreateInfo);
1156 Graphics::UniquePtr<Graphics::Sampler> TestGraphicsController::CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler)
1158 TraceCallStack::NamedParams namedParams;
1159 namedParams["samplerCreateInfo"] << samplerCreateInfo;
1160 mCallStack.PushCall("CreateSampler", namedParams.str(), namedParams);
1162 return Graphics::MakeUnique<TestGraphicsSampler>(mGl, samplerCreateInfo);
1165 Graphics::UniquePtr<Graphics::RenderTarget> TestGraphicsController::CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget)
1167 mCallStack.PushCall("CreateRenderTarget", "");
1168 return Graphics::MakeUnique<TestGraphicsRenderTarget>(mGl, renderTargetCreateInfo);
1171 Graphics::UniquePtr<Graphics::SyncObject> TestGraphicsController::CreateSyncObject(
1172 const Graphics::SyncObjectCreateInfo& syncObjectCreateInfo,
1173 Graphics::UniquePtr<Graphics::SyncObject>&& oldSyncObject)
1175 mCallStack.PushCall("CreateSyncObject", "");
1176 return Graphics::MakeUnique<TestGraphicsSyncObject>(mGraphicsSyncImpl, syncObjectCreateInfo);
1179 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapBufferRange(const Graphics::MapBufferInfo& mapInfo)
1181 mCallStack.PushCall("MapBufferRange", "");
1183 auto buffer = static_cast<TestGraphicsBuffer*>(mapInfo.buffer);
1184 buffer->memory.resize(mapInfo.offset + mapInfo.size); // For initial testing, allow writes past capacity
1186 return std::make_unique<TestGraphicsMemory>(mCallStack, *buffer, mapInfo.offset, mapInfo.size);
1189 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapTextureRange(const Graphics::MapTextureInfo& mapInfo)
1191 mCallStack.PushCall("MapTextureRange", "");
1195 void TestGraphicsController::UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory)
1197 mCallStack.PushCall("UnmapMemory", "");
1200 Graphics::MemoryRequirements TestGraphicsController::GetTextureMemoryRequirements(Graphics::Texture& texture) const
1202 mCallStack.PushCall("GetTextureMemoryRequirements", "");
1203 return Graphics::MemoryRequirements{};
1206 Graphics::MemoryRequirements TestGraphicsController::GetBufferMemoryRequirements(Graphics::Buffer& buffer) const
1208 mCallStack.PushCall("GetBufferMemoryRequirements", "");
1209 return Graphics::MemoryRequirements{};
1212 const Graphics::TextureProperties& TestGraphicsController::GetTextureProperties(const Graphics::Texture& texture)
1214 static Graphics::TextureProperties textureProperties{};
1215 mCallStack.PushCall("GetTextureProperties", "");
1217 return textureProperties;
1220 const Graphics::Reflection& TestGraphicsController::GetProgramReflection(const Graphics::Program& program)
1222 mCallStack.PushCall("GetProgramReflection", "");
1224 return static_cast<const TestGraphicsProgram*>(&program)->GetReflection();
1227 bool TestGraphicsController::PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const
1229 mCallStack.PushCall("PipelineEquals", "");
1233 bool TestGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
1235 mCallStack.PushCall("GetProgramParameter", "");
1236 auto graphicsProgram = Uncast<TestGraphicsProgram>(&program);
1237 return graphicsProgram->GetParameter(parameterId, outData);