2 * Copyright (c) 2022 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.Get();
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:
382 case Graphics::BlendOp::MULTIPLY:
387 case Graphics::BlendOp::SCREEN:
392 case Graphics::BlendOp::OVERLAY:
397 case Graphics::BlendOp::DARKEN:
402 case Graphics::BlendOp::LIGHTEN:
407 case Graphics::BlendOp::COLOR_DODGE:
412 case Graphics::BlendOp::COLOR_BURN:
417 case Graphics::BlendOp::HARD_LIGHT:
422 case Graphics::BlendOp::SOFT_LIGHT:
427 case Graphics::BlendOp::DIFFERENCE:
432 case Graphics::BlendOp::EXCLUSION:
437 case Graphics::BlendOp::HUE:
442 case Graphics::BlendOp::SATURATION:
444 op = GL_HSL_SATURATION;
447 case Graphics::BlendOp::COLOR:
452 case Graphics::BlendOp::LUMINOSITY:
454 op = GL_HSL_LUMINOSITY;
463 constexpr explicit GLCompareOp(Graphics::CompareOp compareOp)
467 case Graphics::CompareOp::NEVER:
470 case Graphics::CompareOp::LESS:
473 case Graphics::CompareOp::EQUAL:
476 case Graphics::CompareOp::LESS_OR_EQUAL:
479 case Graphics::CompareOp::GREATER:
482 case Graphics::CompareOp::NOT_EQUAL:
485 case Graphics::CompareOp::GREATER_OR_EQUAL:
488 case Graphics::CompareOp::ALWAYS:
498 constexpr explicit GLStencilOp(Graphics::StencilOp stencilOp)
502 case Graphics::StencilOp::KEEP:
505 case Graphics::StencilOp::ZERO:
508 case Graphics::StencilOp::REPLACE:
511 case Graphics::StencilOp::INCREMENT_AND_CLAMP:
514 case Graphics::StencilOp::DECREMENT_AND_CLAMP:
517 case Graphics::StencilOp::INVERT:
520 case Graphics::StencilOp::INCREMENT_AND_WRAP:
523 case Graphics::StencilOp::DECREMENT_AND_WRAP:
531 class TestGraphicsMemory : public Graphics::Memory
534 TestGraphicsMemory(TraceCallStack& callStack, TestGraphicsBuffer& buffer, uint32_t mappedOffset, uint32_t mappedSize)
535 : mCallStack(callStack),
537 mMappedOffset(mappedOffset),
538 mMappedSize(mappedSize),
544 void* LockRegion(uint32_t offset, uint32_t size) override
546 std::ostringstream o;
547 o << offset << ", " << size;
548 mCallStack.PushCall("Memory::LockRegion", o.str());
550 if(offset > mMappedOffset + mMappedSize ||
551 size + offset > mMappedOffset + mMappedSize)
553 fprintf(stderr, "TestGraphics.Memory::LockRegion() Out of bounds");
554 mBuffer.memory.resize(mMappedOffset + offset + size); // Grow to prevent memcpy from crashing
556 mLockedOffset = offset;
558 return &mBuffer.memory[mMappedOffset + offset];
561 void Unlock(bool flush) override
563 mCallStack.PushCall("Memory::Unlock", (flush ? "Flush" : "NoFlush"));
570 void Flush() override
572 mCallStack.PushCall("Memory::Flush", "");
574 mBuffer.Upload(mMappedOffset + mLockedOffset, mLockedSize);
578 TraceCallStack& mCallStack;
579 TestGraphicsBuffer& mBuffer;
580 uint32_t mMappedOffset;
581 uint32_t mMappedSize;
582 uint32_t mLockedOffset;
583 uint32_t mLockedSize;
586 TestGraphicsController::TestGraphicsController()
587 : mCallStack(false, "TestGraphicsController."),
588 mCommandBufferCallStack(false, "TestCommandBuffer."),
589 mFrameBufferCallStack(false, "TestFrameBuffer.")
591 mCallStack.Enable(true);
592 mCommandBufferCallStack.Enable(true);
593 auto& trace = mGl.GetTextureTrace();
595 trace.EnableLogging(false);
598 void TestGraphicsController::SubmitCommandBuffers(const Graphics::SubmitInfo& submitInfo)
600 TraceCallStack::NamedParams namedParams;
601 namedParams["submitInfo"] << "cmdBuffer[" << submitInfo.cmdBuffer.size()
602 << "], flags:" << std::hex << submitInfo.flags;
604 mCallStack.PushCall("SubmitCommandBuffers", "", namedParams);
606 mSubmitStack.emplace_back(submitInfo);
608 for(auto& graphicsCommandBuffer : submitInfo.cmdBuffer)
610 auto commandBuffer = Uncast<TestGraphicsCommandBuffer>(graphicsCommandBuffer);
611 ProcessCommandBuffer(*commandBuffer);
615 void TestGraphicsController::ProcessCommandBuffer(TestGraphicsCommandBuffer& commandBuffer)
617 bool scissorEnabled = false;
618 TestGraphicsFramebuffer* currentFramebuffer{nullptr};
619 TestGraphicsPipeline* currentPipeline{nullptr};
621 for(auto& cmd : commandBuffer.GetCommands())
626 case CommandType::FLUSH:
628 // Nothing to do here
631 case CommandType::BIND_TEXTURES:
633 for(auto& binding : cmd.data.bindTextures.textureBindings)
637 auto texture = Uncast<TestGraphicsTexture>(binding.texture);
638 texture->Bind(binding.binding);
642 auto sampler = Uncast<TestGraphicsSampler>(binding.sampler);
645 sampler->Apply(texture->GetTarget());
649 texture->Prepare(); // Ensure native texture is ready
654 case CommandType::BIND_VERTEX_BUFFERS:
656 for(auto& binding : cmd.data.bindVertexBuffers.vertexBufferBindings)
658 auto graphicsBuffer = binding.buffer;
659 auto vertexBuffer = Uncast<TestGraphicsBuffer>(graphicsBuffer);
660 vertexBuffer->Bind();
664 case CommandType::BIND_INDEX_BUFFER:
666 auto& indexBufferBinding = cmd.data.bindIndexBuffer;
667 if(indexBufferBinding.buffer)
669 auto buffer = Uncast<TestGraphicsBuffer>(indexBufferBinding.buffer);
674 case CommandType::BIND_UNIFORM_BUFFER:
678 auto& bindings = cmd.data.bindUniformBuffers;
679 auto buffer = bindings.standaloneUniformsBufferBinding;
681 // based on reflection, issue gl calls
682 buffer.buffer->BindAsUniformBuffer(static_cast<const TestGraphicsProgram*>(currentPipeline->programState.program), bindings.standaloneUniformsBufferBinding);
686 case CommandType::BIND_SAMPLERS:
690 case CommandType::BIND_PIPELINE:
692 currentPipeline = Uncast<TestGraphicsPipeline>(cmd.data.bindPipeline.pipeline);
693 BindPipeline(currentPipeline);
696 case CommandType::DRAW_NATIVE:
698 auto info = &cmd.data.draw.drawNative.drawNativeInfo;
699 CallbackBase::ExecuteReturn<bool>(*info->callback, info->userData);
702 case CommandType::DRAW:
706 mGl.DrawArrays(GetTopology(currentPipeline->inputAssemblyState.topology),
708 cmd.data.draw.draw.vertexCount);
712 case CommandType::DRAW_INDEXED:
716 mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
717 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
719 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
723 case CommandType::DRAW_INDEXED_INDIRECT:
727 mGl.DrawElements(GetTopology(currentPipeline->inputAssemblyState.topology),
728 static_cast<GLsizei>(cmd.data.draw.drawIndexed.indexCount),
730 reinterpret_cast<void*>(cmd.data.draw.drawIndexed.firstIndex));
734 case CommandType::SET_SCISSOR:
738 auto& rect = cmd.data.scissor.region;
739 mGl.Scissor(rect.x, rect.y, rect.width, rect.height);
743 case CommandType::SET_SCISSOR_TEST:
745 if(cmd.data.scissorTest.enable)
747 mGl.Enable(GL_SCISSOR_TEST);
748 scissorEnabled = true;
752 mGl.Disable(GL_SCISSOR_TEST);
753 scissorEnabled = false;
757 case CommandType::SET_VIEWPORT_TEST:
761 case CommandType::SET_VIEWPORT: // @todo Consider correcting for orientation here?
763 auto& rect = cmd.data.viewport.region;
764 mGl.Viewport(rect.x, rect.y, rect.width, rect.height);
768 case CommandType::SET_COLOR_MASK:
770 // Set all channels to the same mask
771 const bool mask = cmd.data.colorMask.enabled;
772 mGl.ColorMask(mask, mask, mask, mask);
775 case CommandType::CLEAR_STENCIL_BUFFER:
777 mGl.Clear(GL_STENCIL_BUFFER_BIT);
780 case CommandType::CLEAR_DEPTH_BUFFER:
782 mGl.Clear(GL_DEPTH_BUFFER_BIT);
786 case CommandType::SET_STENCIL_TEST_ENABLE:
788 if(cmd.data.stencilTest.enabled)
790 mGl.Enable(GL_STENCIL_TEST);
794 mGl.Disable(GL_STENCIL_TEST);
799 case CommandType::SET_STENCIL_FUNC:
801 mGl.StencilFunc(GLCompareOp(cmd.data.stencilFunc.compareOp).op,
802 cmd.data.stencilFunc.reference,
803 cmd.data.stencilFunc.compareMask);
807 case CommandType::SET_STENCIL_WRITE_MASK:
809 mGl.StencilMask(cmd.data.stencilWriteMask.mask);
812 case CommandType::SET_STENCIL_OP:
814 mGl.StencilOp(GLStencilOp(cmd.data.stencilOp.failOp).op,
815 GLStencilOp(cmd.data.stencilOp.depthFailOp).op,
816 GLStencilOp(cmd.data.stencilOp.passOp).op);
820 case CommandType::SET_DEPTH_COMPARE_OP:
822 mGl.DepthFunc(GLCompareOp(cmd.data.depth.compareOp).op);
825 case CommandType::SET_DEPTH_TEST_ENABLE:
827 if(cmd.data.depth.testEnabled)
829 mGl.Enable(GL_DEPTH_TEST);
833 mGl.Disable(GL_DEPTH_TEST);
837 case CommandType::SET_DEPTH_WRITE_ENABLE:
839 mGl.DepthMask(cmd.data.depth.writeEnabled);
843 case CommandType::EXECUTE_COMMAND_BUFFERS:
845 // Process secondary command buffers
846 for(auto& buf : cmd.data.executeCommandBuffers.buffers)
848 ProcessCommandBuffer(*Uncast<TestGraphicsCommandBuffer>(buf));
852 case CommandType::BEGIN_RENDER_PASS:
854 auto renderTarget = Uncast<TestGraphicsRenderTarget>(cmd.data.beginRenderPass.renderTarget);
858 auto fb = renderTarget->mCreateInfo.framebuffer;
861 if(currentFramebuffer != fb)
863 currentFramebuffer = Uncast<TestGraphicsFramebuffer>(fb);
864 currentFramebuffer->Bind();
869 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
874 mGl.BindFramebuffer(GL_FRAMEBUFFER, 0);
877 auto& clearValues = cmd.data.beginRenderPass.clearValues;
878 if(clearValues.size() > 0)
880 const auto renderPass = static_cast<TestGraphicsRenderPass*>(cmd.data.beginRenderPass.renderPass);
883 const auto& color0 = renderPass->attachments[0];
885 if(color0.loadOp == Graphics::AttachmentLoadOp::CLEAR)
887 mask |= GL_COLOR_BUFFER_BIT;
889 // Set clear color (todo: cache it!)
890 // Something goes wrong here if Alpha mask is GL_TRUE
891 mGl.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
892 mGl.ClearColor(clearValues[0].color.r,
893 clearValues[0].color.g,
894 clearValues[0].color.b,
895 clearValues[0].color.a);
898 // check for depth stencil
899 if(renderPass->attachments.size() > 1)
901 const auto& depthStencil = renderPass->attachments.back();
902 if(depthStencil.loadOp == Graphics::AttachmentLoadOp::CLEAR)
905 uint32_t depthClearColor = 0u;
906 if(clearValues.size() == renderPass->attachments.size())
908 depthClearColor = clearValues.back().depthStencil.depth;
910 mGl.ClearDepthf(depthClearColor);
911 mask |= GL_DEPTH_BUFFER_BIT;
913 if(depthStencil.stencilLoadOp == Graphics::AttachmentLoadOp::CLEAR)
915 uint32_t stencilClearColor = 0u;
916 if(clearValues.size() == renderPass->attachments.size())
918 stencilClearColor = clearValues.back().depthStencil.stencil;
920 mGl.ClearStencil(stencilClearColor);
921 mGl.StencilMask(0xFF); // Clear all the bitplanes (assume 8)
922 mask |= GL_STENCIL_BUFFER_BIT;
928 // Test scissor area and RT size
929 const auto& area = cmd.data.beginRenderPass.renderArea;
933 area.width == renderTarget->mCreateInfo.extent.width &&
934 area.height == renderTarget->mCreateInfo.extent.height)
936 mGl.Disable(GL_SCISSOR_TEST);
941 mGl.Enable(GL_SCISSOR_TEST);
942 mGl.Scissor(cmd.data.beginRenderPass.renderArea.x, cmd.data.beginRenderPass.renderArea.y, cmd.data.beginRenderPass.renderArea.width, cmd.data.beginRenderPass.renderArea.height);
944 mGl.Disable(GL_SCISSOR_TEST);
950 DALI_ASSERT_DEBUG(0 && "BeginRenderPass has no render pass");
955 case CommandType::END_RENDER_PASS:
957 if(cmd.data.endRenderPass.syncObject != nullptr)
959 auto syncObject = Uncast<TestGraphicsSyncObject>(cmd.data.endRenderPass.syncObject);
960 syncObject->InitializeResource(); // create the sync object.
968 void TestGraphicsController::BindPipeline(TestGraphicsPipeline* pipeline)
970 auto& vi = pipeline->vertexInputState;
971 for(auto& attribute : vi.attributes)
973 mGl.EnableVertexAttribArray(attribute.location);
974 uint32_t attributeOffset = attribute.offset;
975 GLsizei stride = vi.bufferBindings[attribute.binding].stride;
977 mGl.VertexAttribPointer(attribute.location,
978 GetNumComponents(attribute.format),
979 GetGlType(attribute.format),
980 GL_FALSE, // Not normalized
982 reinterpret_cast<void*>(attributeOffset));
986 auto& rasterizationState = pipeline->rasterizationState;
987 if(rasterizationState.cullMode == Graphics::CullMode::NONE)
989 mGl.Disable(GL_CULL_FACE);
993 mGl.Enable(GL_CULL_FACE);
994 mGl.CullFace(GetCullFace(rasterizationState.cullMode));
997 mGl.FrontFace(GetFrontFace(rasterizationState.frontFace));
1000 auto& colorBlendState = pipeline->colorBlendState;
1001 if(colorBlendState.blendEnable)
1003 mGl.Enable(GL_BLEND);
1005 mGl.BlendFuncSeparate(GetBlendFactor(colorBlendState.srcColorBlendFactor),
1006 GetBlendFactor(colorBlendState.dstColorBlendFactor),
1007 GetBlendFactor(colorBlendState.srcAlphaBlendFactor),
1008 GetBlendFactor(colorBlendState.dstAlphaBlendFactor));
1009 if(colorBlendState.colorBlendOp != colorBlendState.alphaBlendOp)
1011 mGl.BlendEquationSeparate(GetBlendOp(colorBlendState.colorBlendOp), GetBlendOp(colorBlendState.alphaBlendOp));
1015 mGl.BlendEquation(GetBlendOp(colorBlendState.colorBlendOp));
1017 mGl.BlendColor(colorBlendState.blendConstants[0],
1018 colorBlendState.blendConstants[1],
1019 colorBlendState.blendConstants[2],
1020 colorBlendState.blendConstants[3]);
1024 mGl.Disable(GL_BLEND);
1027 auto* program = static_cast<const TestGraphicsProgram*>(pipeline->programState.program);
1028 mGl.UseProgram(program->mImpl->mId);
1032 * @brief Presents render target
1033 * @param renderTarget render target to present
1035 void TestGraphicsController::PresentRenderTarget(Graphics::RenderTarget* renderTarget)
1037 auto* rt = static_cast<const TestGraphicsRenderTarget*>(renderTarget);
1038 TraceCallStack::NamedParams namedParams;
1039 namedParams["renderTarget"] << std::hex << renderTarget;
1040 namedParams["surface"] << std::hex << rt->mCreateInfo.surface;
1041 mCallStack.PushCall("PresentRenderTarget", namedParams.str(), namedParams);
1045 * @brief Waits until the GPU is idle
1047 void TestGraphicsController::WaitIdle()
1049 mCallStack.PushCall("WaitIdle", "");
1053 * @brief Lifecycle pause event
1055 void TestGraphicsController::Pause()
1057 mCallStack.PushCall("Pause", "");
1061 * @brief Lifecycle resume event
1063 void TestGraphicsController::Resume()
1065 mCallStack.PushCall("Resume", "");
1068 void TestGraphicsController::Shutdown()
1070 mCallStack.PushCall("Shutdown", "");
1073 void TestGraphicsController::Destroy()
1075 mCallStack.PushCall("Destroy", "");
1078 void TestGraphicsController::UpdateTextures(const std::vector<Graphics::TextureUpdateInfo>& updateInfoList,
1079 const std::vector<Graphics::TextureUpdateSourceInfo>& sourceList)
1081 TraceCallStack::NamedParams namedParams;
1082 namedParams["updateInfoList"] << "[" << updateInfoList.size() << "]:";
1083 namedParams["sourceList"] << "[" << sourceList.size() << "]:";
1085 mCallStack.PushCall("UpdateTextures", "", namedParams);
1087 // Call either TexImage2D or TexSubImage2D
1088 for(unsigned int i = 0; i < updateInfoList.size(); ++i)
1090 auto& updateInfo = updateInfoList[i];
1091 auto& source = sourceList[i];
1093 auto texture = static_cast<TestGraphicsTexture*>(updateInfo.dstTexture);
1094 texture->Bind(0); // Use first texture unit during resource update
1095 texture->Update(updateInfo, source);
1099 void TestGraphicsController::GenerateTextureMipmaps(const Graphics::Texture& texture)
1101 mCallStack.PushCall("GenerateTextureMipmaps", "");
1103 auto gfxTexture = Uncast<TestGraphicsTexture>(&texture);
1104 mGl.BindTexture(gfxTexture->GetTarget(), 0);
1105 mGl.GenerateMipmap(gfxTexture->GetTarget());
1108 bool TestGraphicsController::EnableDepthStencilBuffer(bool enableDepth, bool enableStencil)
1110 TraceCallStack::NamedParams namedParams;
1111 namedParams["enableDepth"] << (enableDepth ? "T" : "F");
1112 namedParams["enableStencil"] << (enableStencil ? "T" : "F");
1113 mCallStack.PushCall("EnableDepthStencilBuffer", "", namedParams);
1117 void TestGraphicsController::RunGarbageCollector(size_t numberOfDiscardedRenderers)
1119 TraceCallStack::NamedParams namedParams;
1120 namedParams["numberOfDiscardedRenderers"] << numberOfDiscardedRenderers;
1121 mCallStack.PushCall("RunGarbageCollector", "", namedParams);
1124 void TestGraphicsController::DiscardUnusedResources()
1126 mCallStack.PushCall("DiscardUnusedResources", "");
1129 bool TestGraphicsController::IsDiscardQueueEmpty()
1131 mCallStack.PushCall("IsDiscardQueueEmpty", "");
1132 return isDiscardQueueEmptyResult;
1136 * @brief Test if the graphics subsystem has resumed & should force a draw
1138 * @return true if the graphics subsystem requires a re-draw
1140 bool TestGraphicsController::IsDrawOnResumeRequired()
1142 mCallStack.PushCall("IsDrawOnResumeRequired", "");
1143 return isDrawOnResumeRequiredResult;
1146 Graphics::UniquePtr<Graphics::Buffer> TestGraphicsController::CreateBuffer(const Graphics::BufferCreateInfo& createInfo, Graphics::UniquePtr<Graphics::Buffer>&& oldBuffer)
1148 std::ostringstream oss;
1149 oss << "bufferCreateInfo:" << createInfo;
1150 mCallStack.PushCall("CreateBuffer", oss.str());
1151 return Graphics::MakeUnique<TestGraphicsBuffer>(mCallStack, mGl, createInfo.size, createInfo.usage);
1154 Graphics::UniquePtr<Graphics::CommandBuffer> TestGraphicsController::CreateCommandBuffer(const Graphics::CommandBufferCreateInfo& commandBufferCreateInfo, Graphics::UniquePtr<Graphics::CommandBuffer>&& oldCommandBuffer)
1156 std::ostringstream oss;
1157 oss << "commandBufferCreateInfo:" << commandBufferCreateInfo;
1158 mCallStack.PushCall("CreateCommandBuffer", oss.str());
1159 return Graphics::MakeUnique<TestGraphicsCommandBuffer>(mCommandBufferCallStack, mGl);
1162 Graphics::UniquePtr<Graphics::RenderPass> TestGraphicsController::CreateRenderPass(const Graphics::RenderPassCreateInfo& renderPassCreateInfo, Graphics::UniquePtr<Graphics::RenderPass>&& oldRenderPass)
1164 mCallStack.PushCall("CreateRenderPass", "");
1165 return Graphics::MakeUnique<TestGraphicsRenderPass>(mGl, renderPassCreateInfo);
1168 Graphics::UniquePtr<Graphics::Texture> TestGraphicsController::CreateTexture(const Graphics::TextureCreateInfo& textureCreateInfo, Graphics::UniquePtr<Graphics::Texture>&& oldTexture)
1170 TraceCallStack::NamedParams namedParams;
1171 namedParams["textureCreateInfo"] << textureCreateInfo;
1172 mCallStack.PushCall("CreateTexture", namedParams.str(), namedParams);
1174 return Graphics::MakeUnique<TestGraphicsTexture>(mGl, textureCreateInfo);
1177 Graphics::UniquePtr<Graphics::Framebuffer> TestGraphicsController::CreateFramebuffer(
1178 const Graphics::FramebufferCreateInfo& createInfo,
1179 Graphics::UniquePtr<Graphics::Framebuffer>&& oldFramebuffer)
1181 TraceCallStack::NamedParams namedParams;
1182 namedParams["framebufferCreateInfo"] << createInfo;
1183 mCallStack.PushCall("Controller::CreateFramebuffer", namedParams.str(), namedParams);
1185 return Graphics::MakeUnique<TestGraphicsFramebuffer>(mFrameBufferCallStack, mGl, createInfo);
1188 Graphics::UniquePtr<Graphics::Pipeline> TestGraphicsController::CreatePipeline(const Graphics::PipelineCreateInfo& pipelineCreateInfo, Graphics::UniquePtr<Graphics::Pipeline>&& oldPipeline)
1190 mCallStack.PushCall("CreatePipeline", "");
1191 return std::make_unique<TestGraphicsPipeline>(mGl, pipelineCreateInfo);
1194 Graphics::UniquePtr<Graphics::Program> TestGraphicsController::CreateProgram(const Graphics::ProgramCreateInfo& programCreateInfo, Graphics::UniquePtr<Graphics::Program>&& oldProgram)
1196 mCallStack.PushCall("CreateProgram", "");
1198 for(auto cacheEntry : mProgramCache)
1201 for(auto& shader : *(programCreateInfo.shaderState))
1203 auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
1204 std::vector<uint8_t> source;
1205 source.resize(graphicsShader->mCreateInfo.sourceSize);
1206 memcpy(&source[0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
1208 if(!std::equal(source.begin(), source.end(), cacheEntry.shaders[shader.pipelineStage].begin()))
1216 return Graphics::MakeUnique<TestGraphicsProgram>(cacheEntry.programImpl);
1220 mProgramCache.emplace_back();
1221 mProgramCache.back().programImpl = new TestGraphicsProgramImpl(mGl, programCreateInfo, mVertexFormats, mCustomUniforms);
1222 for(auto& shader : *(programCreateInfo.shaderState))
1224 auto graphicsShader = Uncast<TestGraphicsShader>(shader.shader);
1225 mProgramCache.back().shaders[shader.pipelineStage].resize(graphicsShader->mCreateInfo.sourceSize);
1226 memcpy(&mProgramCache.back().shaders[shader.pipelineStage][0], graphicsShader->mCreateInfo.sourceData, graphicsShader->mCreateInfo.sourceSize);
1228 return Graphics::MakeUnique<TestGraphicsProgram>(mProgramCache.back().programImpl);
1231 Graphics::UniquePtr<Graphics::Shader> TestGraphicsController::CreateShader(const Graphics::ShaderCreateInfo& shaderCreateInfo, Graphics::UniquePtr<Graphics::Shader>&& oldShader)
1233 mCallStack.PushCall("CreateShader", "");
1234 return Graphics::MakeUnique<TestGraphicsShader>(mGl, shaderCreateInfo);
1237 Graphics::UniquePtr<Graphics::Sampler> TestGraphicsController::CreateSampler(const Graphics::SamplerCreateInfo& samplerCreateInfo, Graphics::UniquePtr<Graphics::Sampler>&& oldSampler)
1239 TraceCallStack::NamedParams namedParams;
1240 namedParams["samplerCreateInfo"] << samplerCreateInfo;
1241 mCallStack.PushCall("CreateSampler", namedParams.str(), namedParams);
1243 return Graphics::MakeUnique<TestGraphicsSampler>(mGl, samplerCreateInfo);
1246 Graphics::UniquePtr<Graphics::RenderTarget> TestGraphicsController::CreateRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo, Graphics::UniquePtr<Graphics::RenderTarget>&& oldRenderTarget)
1248 TraceCallStack::NamedParams namedParams;
1249 namedParams["surface"] << std::hex << renderTargetCreateInfo.surface;
1250 mCallStack.PushCall("CreateRenderTarget", namedParams.str(), namedParams);
1252 return Graphics::MakeUnique<TestGraphicsRenderTarget>(mGl, renderTargetCreateInfo);
1255 Graphics::UniquePtr<Graphics::SyncObject> TestGraphicsController::CreateSyncObject(
1256 const Graphics::SyncObjectCreateInfo& syncObjectCreateInfo,
1257 Graphics::UniquePtr<Graphics::SyncObject>&& oldSyncObject)
1259 mCallStack.PushCall("CreateSyncObject", "");
1260 return Graphics::MakeUnique<TestGraphicsSyncObject>(mGraphicsSyncImpl, syncObjectCreateInfo);
1263 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapBufferRange(const Graphics::MapBufferInfo& mapInfo)
1265 mCallStack.PushCall("MapBufferRange", "");
1267 auto buffer = static_cast<TestGraphicsBuffer*>(mapInfo.buffer);
1268 buffer->memory.resize(mapInfo.offset + mapInfo.size); // For initial testing, allow writes past capacity
1270 return std::make_unique<TestGraphicsMemory>(mCallStack, *buffer, mapInfo.offset, mapInfo.size);
1273 Graphics::UniquePtr<Graphics::Memory> TestGraphicsController::MapTextureRange(const Graphics::MapTextureInfo& mapInfo)
1275 mCallStack.PushCall("MapTextureRange", "");
1279 void TestGraphicsController::UnmapMemory(Graphics::UniquePtr<Graphics::Memory> memory)
1281 mCallStack.PushCall("UnmapMemory", "");
1284 Graphics::MemoryRequirements TestGraphicsController::GetTextureMemoryRequirements(Graphics::Texture& texture) const
1286 mCallStack.PushCall("GetTextureMemoryRequirements", "");
1287 return Graphics::MemoryRequirements{};
1290 Graphics::MemoryRequirements TestGraphicsController::GetBufferMemoryRequirements(Graphics::Buffer& buffer) const
1292 mCallStack.PushCall("GetBufferMemoryRequirements", "");
1293 return Graphics::MemoryRequirements{};
1296 const Graphics::TextureProperties& TestGraphicsController::GetTextureProperties(const Graphics::Texture& texture)
1298 static Graphics::TextureProperties textureProperties{};
1299 mCallStack.PushCall("GetTextureProperties", "");
1301 return textureProperties;
1304 const Graphics::Reflection& TestGraphicsController::GetProgramReflection(const Graphics::Program& program)
1306 mCallStack.PushCall("GetProgramReflection", "");
1308 return static_cast<const TestGraphicsProgram*>(&program)->GetReflection();
1311 bool TestGraphicsController::PipelineEquals(const Graphics::Pipeline& pipeline0, const Graphics::Pipeline& pipeline1) const
1313 mCallStack.PushCall("PipelineEquals", "");
1317 bool TestGraphicsController::GetProgramParameter(Graphics::Program& program, uint32_t parameterId, void* outData)
1319 mCallStack.PushCall("GetProgramParameter", "");
1320 auto graphicsProgram = Uncast<TestGraphicsProgram>(&program);
1321 return graphicsProgram->GetParameter(parameterId, outData);